Skip to content

Commit 4af20b7

Browse files
committed
update stuff for last two tutorials
1 parent 13fc156 commit 4af20b7

4 files changed

+110
-75
lines changed

exercise12_HyperParameterTuning.ipynb

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@
2727
"id": "46f6f98a-12a4-4bcd-975e-18a7d404cb9c",
2828
"metadata": {},
2929
"source": [
30-
"# Exercise 12\n",
30+
"# Multiclass Classification with Hyper Parameter Tuning\n",
3131
"\n",
32-
"## Multiclass Classification with Hyper Parameter Tuning\n",
33-
"\n",
34-
"- Softmax Activation Function at Output\n",
35-
"- CategoricalCrossentropy Loss\n",
36-
"- Split Data into Training, Validating, Testing Set\n",
37-
"- Avoid Over-/Underfit by\n",
38-
" - deploying Early Stopping\n",
39-
" - deploying Hyper Parameter Tuning using Keras Tuner\n",
40-
"- we use convenient stuff from Scikit-learn"
32+
"- **Softmax** activation Function at Output\n",
33+
"- Categorical crossentropy loss\n",
34+
"- Split data into training, validating, testing data sets\n",
35+
" - training, validating used for hyper parameter tuning (validate serves as the unseen test data here)\n",
36+
" - training, testing used for train/test the best model (test data was never used before! and is here only and once used to finally check model performance) \n",
37+
"- Avoid over-/underfit by\n",
38+
" - deploying early stopping\n",
39+
" - deploying hyper parameter tuning using Keras tuner\n",
40+
"- we use convenient stuff from scikit-learn"
4141
]
4242
},
4343
{
@@ -67,8 +67,8 @@
6767
"import tensorflow.keras.backend as K\n",
6868
"import time\n",
6969
"\n",
70-
"print('TF version', tf.__version__, # we used 2.4.3\n",
71-
" '\\nKeras version', keras.__version__, # we used 2.4.0\n",
70+
"print('TF version', tf.__version__, # we used 2.10.0\n",
71+
" '\\nKeras version', keras.__version__, # we used 2.10.0\n",
7272
" '\\nKeras Tuner version', kt.__version__) # we used 1.1.0\n",
7373
"verbose = 1 # plot training status"
7474
]
@@ -78,7 +78,7 @@
7878
"id": "5249aa49-d2da-4aec-be0a-769001eb010d",
7979
"metadata": {},
8080
"source": [
81-
"## Folder Structure"
81+
"## Folder Structure For Log Data"
8282
]
8383
},
8484
{
@@ -134,12 +134,12 @@
134134
"source": [
135135
"nlabels = 3 # number of classes\n",
136136
"labels = np.arange(nlabels) # we encode as integers\n",
137-
"nx = 2*nlabels # number of features\n",
137+
"nx = 2*nlabels # number of features, here we use 6\n",
138138
"m = 100000 # data examples\n",
139139
"\n",
140-
"train_size = 7/10 # 7/10 of whole data set\n",
141-
"validate_size = 3/10 * 2/3 # 1/5 of whole data set\n",
142-
"test_size = 1 - train_size - validate_size # must be > 0\n",
140+
"train_size = 7/10 # 7/10 of the whole data set\n",
141+
"validate_size = 3/10 * 2/3 # 1/5 of the whole data set\n",
142+
"test_size = 1 - train_size - validate_size # remaining data, must be > 0\n",
143143
"\n",
144144
"X, Y = make_classification(n_samples=m,\n",
145145
" n_features=nx, n_informative=nx,\n",
@@ -159,9 +159,7 @@
159159
"X_val, X_test, Y_val, Y_test = train_test_split(\n",
160160
" X_tmp, Y_tmp, train_size=val_size, random_state=None)\n",
161161
"\n",
162-
"m_train = X_train.shape[0]\n",
163-
"m_val = X_val.shape[0]\n",
164-
"m_test = X_test.shape[0]\n",
162+
"m_train, m_val, m_test = X_train.shape[0], X_val.shape[0], X_test.shape[0]\n",
165163
"\n",
166164
"print(train_size, validate_size, test_size)\n",
167165
"print(m_train, m_val, m_test, m_train+m_val+m_test == m)\n",
@@ -197,7 +195,7 @@
197195
"metadata": {},
198196
"outputs": [],
199197
"source": [
200-
"# in future we might also consider dropout / regularization in the model\n",
198+
"# as homework we might also consider dropout and regularization in the model\n",
201199
"def build_model(hp): # with hyper parameter ranges\n",
202200
" model = keras.Sequential()\n",
203201
" # input layer\n",
@@ -238,7 +236,7 @@
238236
"model = build_model(kt.HyperParameters())\n",
239237
"hptuner = kt.RandomSearch(\n",
240238
" hypermodel=build_model,\n",
241-
" objective='val_categorical_accuracy', # on val data!\n",
239+
" objective='val_categorical_accuracy', # check performance on val data!\n",
242240
" max_trials=max_trials,\n",
243241
" executions_per_trial=executions_per_trial,\n",
244242
" overwrite=True,\n",
@@ -288,7 +286,7 @@
288286
"outputs": [],
289287
"source": [
290288
"# we might check the best XX models in detail\n",
291-
"# for didactical purpose we choose only the very best one\n",
289+
"# for didactical purpose we choose only the very best one, located in [0]:\n",
292290
"model = hptuner.get_best_models(num_models=1)[0]\n",
293291
"model.save(tf_logdir+'/best_model')"
294292
]
@@ -326,7 +324,7 @@
326324
"source": [
327325
"# load best model and reset weights\n",
328326
"model = keras.models.load_model(tf_logdir+'/best_model')\n",
329-
"reset_weights(model)\n",
327+
"reset_weights(model) # start training from scratch \n",
330328
"print(model.summary())"
331329
]
332330
},

exercise12_MulticlassClassification_CategoricalCrossentropy.ipynb

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@
2727
"id": "46f6f98a-12a4-4bcd-975e-18a7d404cb9c",
2828
"metadata": {},
2929
"source": [
30-
"# Exercise 12\n",
31-
"\n",
32-
"## Multiclass Classification\n",
33-
"- One Hot Encoding\n",
30+
"# Multiclass Classification\n",
31+
"- One Hot encoding\n",
3432
"- Data set splitting into train, test\n",
35-
"- Softmax Activation Function at Output\n",
36-
"- CategoricalCrossentropy loss\n",
37-
"- we use convenient stuff from Scikit-learn\n"
33+
"- **Softmax** activation function at output layer\n",
34+
"- categorical cross-entropy loss\n",
35+
"- we use convenient stuff from scikit-learn"
3836
]
3937
},
4038
{
@@ -61,8 +59,10 @@
6159
"import tensorflow.keras as keras\n",
6260
"# import tensorflow.keras.backend as K\n",
6361
"\n",
64-
"print('TF version', tf.__version__, # we used 2.4.3\n",
65-
" '\\nKeras version', keras.__version__) # we used 2.4.0\n",
62+
"print('TF version', tf.__version__, # we used 2.10.0\n",
63+
" '\\nKeras version', keras.__version__) # we used 2.10.0\n",
64+
"\n",
65+
"tf.keras.backend.set_floatx('float64') # we could use double precision\n",
6666
"\n",
6767
"verbose = 1 # plot training status"
6868
]
@@ -72,7 +72,7 @@
7272
"id": "8902728d-14a9-4f64-8f49-364953c88f08",
7373
"metadata": {},
7474
"source": [
75-
"## Data Synthesis / One Hot Encoding / Splitting"
75+
"## Data Synthesis, One Hot Encoding, Train/TestSplitting"
7676
]
7777
},
7878
{
@@ -86,9 +86,9 @@
8686
"labels = np.arange(nlabels) # we encode as integers\n",
8787
"\n",
8888
"m = int(5/4*80000) # data examples\n",
89-
"nx = 2*nlabels # number of features\n",
89+
"nx = 2*nlabels # number of features, we set it to 6 here\n",
9090
"\n",
91-
"train_size = 4/5 # 80% are used for training\n",
91+
"train_size = 4/5 # 80% of data are used for training\n",
9292
"\n",
9393
"X, Y = make_classification(n_samples=m,\n",
9494
" n_features=nx, n_informative=nx,\n",
@@ -100,9 +100,8 @@
100100
"encoder = OneHotEncoder(sparse=False)\n",
101101
"Y = encoder.fit_transform(Y.reshape(-1, 1))\n",
102102
"\n",
103-
"X_train, X_test,\\\n",
104-
" Y_train, Y_test = train_test_split(\n",
105-
" X, Y, train_size=train_size, random_state=None)\n",
103+
"X_train, X_test, Y_train, Y_test = train_test_split(\n",
104+
" X, Y, train_size=train_size, random_state=None)\n",
106105
"m_train = X_train.shape[0]\n",
107106
"m_test = X_test.shape[0]\n",
108107
"print('m_train', m_train)\n",
@@ -116,7 +115,7 @@
116115
"id": "c0445a58-1b59-4e48-84df-2ebe2d233649",
117116
"metadata": {},
118117
"source": [
119-
"## Setup of Model "
118+
"## Setup of Model Using Fully Connected Layers"
120119
]
121120
},
122121
{
@@ -126,27 +125,29 @@
126125
"metadata": {},
127126
"outputs": [],
128127
"source": [
129-
"# hyper parameters should be learned as well,\n",
130-
"# for this toy example we set them for reasonable\n",
131-
"# computing time and results\n",
128+
"# hyper parameters should be learned as well, however for this toy example\n",
129+
"# we set them for reasonable computing time and approriate results\n",
132130
"epochs = 10\n",
133-
"batch_size = 32\n",
134131
"no_perceptron_in_hl = np.array([2*nx, 4*nx, nlabels])\n",
132+
"batch_size = 32\n",
135133
"\n",
134+
"# model architecture\n",
136135
"optimizer = keras.optimizers.Adam()\n",
137-
"loss = keras.losses.CategoricalCrossentropy(\n",
138-
" from_logits=False, label_smoothing=0)\n",
136+
"loss = keras.losses.CategoricalCrossentropy(from_logits=False,\n",
137+
" label_smoothing=0)\n",
139138
"metrics = [keras.metrics.CategoricalCrossentropy(),\n",
140139
" keras.metrics.CategoricalAccuracy()]\n",
141140
"\n",
142141
"model = keras.Sequential()\n",
143-
"# input layer\n",
142+
"# apply input layer\n",
144143
"model.add(keras.Input(shape=(nx,)))\n",
145-
"# hidden layers\n",
144+
"# apply hidden layers\n",
146145
"for n in no_perceptron_in_hl:\n",
147146
" model.add(keras.layers.Dense(n, activation='relu'))\n",
148-
"# output layer with softmax for multi-label classificaton\n",
147+
"# apply output layer with softmax for multi-label classificaton\n",
149148
"model.add(keras.layers.Dense(nlabels, activation='softmax'))\n",
149+
"# let TF compile the model architecture, one key step in compiling\n",
150+
"# is to set up the forward and backward propagation workflow through the model\n",
150151
"model.compile(optimizer=optimizer, loss=loss, metrics=metrics)\n",
151152
"print(model.summary())\n",
152153
"# tw = np.sum([K.count_params(w) for w in model.trainable_weights])\n",

exercise12_MusicGenreClassification.ipynb

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"id": "a46602eb-0171-412d-990c-a0a63f4d47f9",
2828
"metadata": {},
2929
"source": [
30-
"# Exercise 12: Music Genre Classification\n",
30+
"# Music Genre Classification with Fully Connected Layers\n",
3131
"\n",
3232
"In this toy example we have a special look at\n",
3333
"- feature design (loudness, crest, peak, rms, spectral weight)\n",
@@ -89,8 +89,8 @@
8989
"import time\n",
9090
"\n",
9191
"\n",
92-
"print('TF version', tf.__version__, # we used 2.4.3\n",
93-
" '\\nKeras version', keras.__version__, # we used 2.4.0\n",
92+
"print('TF version', tf.__version__, # we used 2.10.0\n",
93+
" '\\nKeras version', keras.__version__, # we used 2.10.0\n",
9494
" '\\nKeras Tuner version', kt.__version__) # we used 1.1.0\n",
9595
"verbose = 1 # plot training status"
9696
]
@@ -100,7 +100,7 @@
100100
"id": "41669170-f69b-43e4-acc8-8ab001d90404",
101101
"metadata": {},
102102
"source": [
103-
"## Folder Structure"
103+
"## Folder Structure for Log Data"
104104
]
105105
},
106106
{
@@ -111,7 +111,7 @@
111111
"outputs": [],
112112
"source": [
113113
"audiofolder = './audio_ex12/'\n",
114-
"ex_str = 'ex12_'\n",
114+
"ex_str = 'mgc_'\n",
115115
"time_str = '%Y_%m_%d_%H_%M_'\n",
116116
"\n",
117117
"\n",
@@ -271,8 +271,13 @@
271271
"metadata": {},
272272
"outputs": [],
273273
"source": [
274-
"# with np.load(audiofolder+'/_raw_data.npz') as data: # use this line when features were extracted above\n",
274+
"# we use this option when features were extracted in cell [5] above\n",
275+
"# with np.load(audiofolder+'/_raw_data.npz') as data: \n",
276+
"\n",
277+
"# we use this when we want to use 'large' toy example data set \n",
275278
"# with np.load(audiofolder+'/_raw_data_large.npz') as data:\n",
279+
"\n",
280+
"# we use this when we want to use 'small' toy example data set \n",
276281
"with np.load(audiofolder+'/_raw_data_small.npz') as data:\n",
277282
"\n",
278283
" Xdata = data['Xdata']\n",
@@ -387,7 +392,7 @@
387392
"# 9 crest_lin\n",
388393
"# 10 crest_db\n",
389394
"# 11 low_high_ratio\n",
390-
"# 2, 3, 5, 7, 8, 10, 11 might be useful:\n",
395+
"# the seven features [2, 3, 5, 7, 8, 10, 11] might be useful:\n",
391396
"which_features = [2, 3, 5, 7, 8, 10, 11]\n",
392397
"X = np.copy(Xdata_norm[:, which_features])\n",
393398
"Y = np.copy(Ydata)\n",
@@ -458,7 +463,7 @@
458463
"source": [
459464
"train_size = 5/10\n",
460465
"validate_size = 5/10 * 1/2\n",
461-
"test_size = 1 - train_size - validate_size # must be > 0\n",
466+
"test_size = 1 - train_size - validate_size # remaining data, must be > 0\n",
462467
"\n",
463468
"# split into train, val, test data:\n",
464469
"X_train, X_tmp, Y_train, Y_tmp = train_test_split(\n",
@@ -517,7 +522,7 @@
517522
"metadata": {},
518523
"outputs": [],
519524
"source": [
520-
"# in future we might also consider dropout / regularization in the model\n",
525+
"# as homework we might also consider dropout / regularization in the model\n",
521526
"def build_model(hp): # with hyper parameter ranges\n",
522527
" model = keras.Sequential()\n",
523528
" # input layer\n",
@@ -558,7 +563,7 @@
558563
"model = build_model(kt.HyperParameters())\n",
559564
"hptuner = kt.RandomSearch(\n",
560565
" hypermodel=build_model,\n",
561-
" objective='val_categorical_accuracy', # on val data!\n",
566+
" objective='val_categorical_accuracy', # check performance on val data!\n",
562567
" max_trials=max_trials,\n",
563568
" executions_per_trial=executions_per_trial,\n",
564569
" overwrite=True,\n",
@@ -608,7 +613,7 @@
608613
"outputs": [],
609614
"source": [
610615
"# we might check (train) the best XX models in detail\n",
611-
"# for didactical purpose we choose only the very best one\n",
616+
"# for didactical purpose we choose only the very best one, located in [0]:\n",
612617
"model = hptuner.get_best_models(num_models=1)[0]\n",
613618
"model.save(tf_logdir+'/best_model')"
614619
]
@@ -646,7 +651,7 @@
646651
"source": [
647652
"# load best model and reset weights\n",
648653
"model = keras.models.load_model(tf_logdir+'/best_model')\n",
649-
"reset_weights(model)\n",
654+
"reset_weights(model) # start training from scratch \n",
650655
"print(model.summary())"
651656
]
652657
},

0 commit comments

Comments
 (0)