Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
moocarme committed Jan 10, 2020
1 parent 0dfd573 commit 5dbdfc1
Show file tree
Hide file tree
Showing 10 changed files with 588 additions and 596 deletions.
6 changes: 3 additions & 3 deletions Chapter04/Activity4.02/Activity4.02_Unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_model_iter(self):
self.results_1 =[]
models = [build_model_1, build_model_2, build_model_3]
for m in range(len(models)):
classifier = KerasClassifier(build_fn=models[m], epochs=epochs, batch_size=batch_size, verbose=0)
classifier = KerasClassifier(build_fn=models[m], epochs=epochs, batch_size=batch_size, verbose=0, shuffle=False)
kfold = StratifiedKFold(n_splits=self.n_folds, shuffle=True, random_state=self.seed)
result = cross_val_score(classifier, self.X, self.y, cv=kfold)
self.results_1.append(result)
Expand All @@ -99,7 +99,7 @@ def test_batch_epoch_iter(self):
self.results_2 =[]
for e in range(len(epochs)):
for b in range(len(batches)):
classifier = KerasClassifier(build_fn=build_model_2, epochs=epochs[e], batch_size=batches[b], verbose=0)
classifier = KerasClassifier(build_fn=build_model_2, epochs=epochs[e], batch_size=batches[b], verbose=0, shuffle=False)
kfold = StratifiedKFold(n_splits=self.n_folds, shuffle=True, random_state=self.seed)
result = cross_val_score(classifier, self.X, self.y, cv=kfold)
self.results_2.append(result)
Expand All @@ -122,7 +122,7 @@ def test_opt_act_iter(self):
for a in range(len(activations)):
optimizer = optimizers[o]
activation = activations[a]
classifier = KerasClassifier(build_fn=build_model_2, epochs=epochs, batch_size=batch_size, verbose=0)
classifier = KerasClassifier(build_fn=build_model_2, epochs=epochs, batch_size=batch_size, verbose=0, shuffle=False)
kfold = StratifiedKFold(n_splits=self.n_folds, shuffle=True, random_state=self.seed)
result = cross_val_score(classifier, self.X, self.y, cv=kfold)
self.results_3.append(result)
Expand Down
48 changes: 20 additions & 28 deletions Chapter04/Activity4.02/Activity4_02.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"# Load the dataset\n",
Expand All @@ -45,9 +43,7 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"# Create the function that returns the keras model 1\n",
Expand Down Expand Up @@ -88,9 +84,7 @@
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"# define a seed for random number generator so the result will be reproducible\n",
Expand All @@ -109,7 +103,7 @@
"# loop over models\n",
"for m in range(len(models)):\n",
" # build the Scikit-Learn interface for the keras model\n",
" classifier = KerasClassifier(build_fn=models[m], epochs=epochs, batch_size=batch_size, verbose=0)\n",
" classifier = KerasClassifier(build_fn=models[m], epochs=epochs, batch_size=batch_size, verbose=0, shuffle=False)\n",
" # define the cross validation iterator\n",
" kfold = StratifiedKFold(n_splits=n_folds, shuffle=True, random_state=seed)\n",
" # perform the k-fold cross validation and store the scores in result\n",
Expand All @@ -127,9 +121,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Model 1 Test Accuracy = 0.49602887630462644\n",
"Model 2 Test Accuracy = 0.5090252697467804\n",
"Model 3 Test Accuracy = 0.513357400894165\n"
"Model 1 Test Accuracy = 0.5046931445598603\n",
"Model 2 Test Accuracy = 0.5054151535034179\n",
"Model 3 Test Accuracy = 0.507581228017807\n"
]
}
],
Expand Down Expand Up @@ -159,7 +153,7 @@
"for e in range(len(epochs)):\n",
" for b in range(len(batches)):\n",
" # build the Scikit-Learn interface for the keras model\n",
" classifier = KerasClassifier(build_fn=build_model_2, epochs=epochs[e], batch_size=batches[b], verbose=0)\n",
" classifier = KerasClassifier(build_fn=build_model_2, epochs=epochs[e], batch_size=batches[b], verbose=0, shuffle=False)\n",
" # define the cross validation iterator\n",
" kfold = StratifiedKFold(n_splits=n_folds, shuffle=True, random_state=seed)\n",
" # perform the k-fold cross validation. \n",
Expand All @@ -178,10 +172,10 @@
"name": "stdout",
"output_type": "stream",
"text": [
"batch_size = 10 , epochs = 100 , Test Accuracy = 0.4916967451572418\n",
"batch_size = 20 , epochs = 100 , Test Accuracy = 0.5364620923995972\n",
"batch_size = 10 , epochs = 200 , Test Accuracy = 0.5061371803283692\n",
"batch_size = 20 , epochs = 200 , Test Accuracy = 0.505415165424347\n"
"batch_size = 10 , epochs = 100 , Test Accuracy = 0.5371841073036194\n",
"batch_size = 20 , epochs = 100 , Test Accuracy = 0.5025270760059357\n",
"batch_size = 10 , epochs = 200 , Test Accuracy = 0.49386281371116636\n",
"batch_size = 20 , epochs = 200 , Test Accuracy = 0.49891695380210876\n"
]
}
],
Expand All @@ -197,9 +191,7 @@
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"# define a seed for random number generator so the result will be reproducible\n",
Expand All @@ -220,7 +212,7 @@
" optimizer = optimizers[o]\n",
" activation = activations[a]\n",
" # build the Scikit-Learn interface for the keras model\n",
" classifier = KerasClassifier(build_fn=build_model_2, epochs=epochs, batch_size=batch_size, verbose=0)\n",
" classifier = KerasClassifier(build_fn=build_model_2, epochs=epochs, batch_size=batch_size, verbose=0, shuffle=False)\n",
" # define the cross validation iterator\n",
" kfold = StratifiedKFold(n_splits=n_folds, shuffle=True, random_state=seed)\n",
" # perform the k-fold cross validation. \n",
Expand All @@ -239,12 +231,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"activation = relu , optimizer = rmsprop , Test accuracy = 0.4815884530544281\n",
"activation = tanh , optimizer = rmsprop , Test accuracy = 0.5046931385993958\n",
"activation = relu , optimizer = adam , Test accuracy = 0.4974729120731354\n",
"activation = tanh , optimizer = adam , Test accuracy = 0.5090252697467804\n",
"activation = relu , optimizer = sgd , Test accuracy = 0.5075812220573426\n",
"activation = tanh , optimizer = sgd , Test accuracy = 0.5140794217586517\n"
"activation = relu , optimizer = rmsprop , Test accuracy = 0.5523465752601624\n",
"activation = tanh , optimizer = rmsprop , Test accuracy = 0.5032491028308869\n",
"activation = relu , optimizer = adam , Test accuracy = 0.5039711177349091\n",
"activation = tanh , optimizer = adam , Test accuracy = 0.5119133591651917\n",
"activation = relu , optimizer = sgd , Test accuracy = 0.5046931326389312\n",
"activation = tanh , optimizer = sgd , Test accuracy = 0.4830324828624725\n"
]
}
],
Expand Down
26 changes: 13 additions & 13 deletions Chapter04/Activity4.03/Activity4_03.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"# loop over models\n",
"for i in range(len(models)):\n",
" # build the Scikit-Learn interface for the keras model\n",
" regressor = KerasRegressor(build_fn=models[i], epochs=100, batch_size=50, verbose=0)\n",
" regressor = KerasRegressor(build_fn=models[i], epochs=100, batch_size=50, verbose=0, shuffle=False)\n",
" # build the pipeline of transformations so for each fold trainind set will be scaled \n",
" # and test set will be scaled accordingly.\n",
" model = make_pipeline(StandardScaler(), regressor)\n",
Expand All @@ -135,9 +135,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Model 1 test error rate = 25.667806420326237\n",
"Model 2 test error rate = 25.12541407108307\n",
"Model 3 test error rate = 25.41868120670319\n"
"Model 1 test error rate = 25.5814275598526\n",
"Model 2 test error rate = 24.995588760375977\n",
"Model 3 test error rate = 25.413555855751035\n"
]
}
],
Expand Down Expand Up @@ -167,7 +167,7 @@
"for i in range(len(epochs)):\n",
" for j in range(len(batches)):\n",
" # build the Scikit-Learn interface for the keras model\n",
" regressor = KerasRegressor(build_fn=build_model_2, epochs=epochs[i], batch_size=batches[j], verbose=0)\n",
" regressor = KerasRegressor(build_fn=build_model_2, epochs=epochs[i], batch_size=batches[j], verbose=0, shuffle=False)\n",
" # build the pipeline of transformations so for each fold trainind set will be scaled \n",
" # and test set will be scaled accordingly.\n",
" model = make_pipeline(StandardScaler(), regressor)\n",
Expand All @@ -189,10 +189,10 @@
"name": "stdout",
"output_type": "stream",
"text": [
"batch_size = 50 , epochs = 80 , Test error rate = 25.261057367324828\n",
"batch_size = 25 , epochs = 80 , Test error rate = 25.33181362867355\n",
"batch_size = 50 , epochs = 100 , Test error rate = 25.049520788192748\n",
"batch_size = 25 , epochs = 100 , Test error rate = 25.159788784980773\n"
"batch_size = 50 , epochs = 80 , Test error rate = 24.9977765750885\n",
"batch_size = 25 , epochs = 80 , Test error rate = 25.185024490356447\n",
"batch_size = 50 , epochs = 100 , Test error rate = 25.031687560081483\n",
"batch_size = 25 , epochs = 100 , Test error rate = 25.14500049829483\n"
]
}
],
Expand Down Expand Up @@ -224,7 +224,7 @@
"for i in range(len(optimizers)):\n",
" optimizer=optimizers[i]\n",
" # build the Scikit-Learn interface for the keras model\n",
" regressor = KerasRegressor(build_fn=build_model_2, epochs=100, batch_size=50, verbose=0)\n",
" regressor = KerasRegressor(build_fn=build_model_2, epochs=100, batch_size=50, verbose=0, shuffle=False)\n",
" # build the pipeline of transformations so for each fold trainind set will be scaled \n",
" # and test set will be scaled accordingly.\n",
" model = make_pipeline(StandardScaler(), regressor)\n",
Expand All @@ -246,9 +246,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"optimizer= adam test error rate = 25.30731126308441\n",
"optimizer= sgd test error rate = 25.128445773124696\n",
"optimizer= rmsprop test error rate = 25.159849672317506\n"
"optimizer= adam test error rate = 25.013688187599183\n",
"optimizer= sgd test error rate = 25.04590551376343\n",
"optimizer= rmsprop test error rate = 25.028708510398864\n"
]
}
],
Expand Down
Loading

0 comments on commit 5dbdfc1

Please sign in to comment.