Skip to content

Commit 3eb396a

Browse files
committed
final text and code comments up to 1.3
1 parent 367603f commit 3eb396a

File tree

1 file changed

+46
-57
lines changed

1 file changed

+46
-57
lines changed

lab3/solutions/Lab3_Part_1_Introduction_to_CAPSA.ipynb

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
}
173173
],
174174
"source": [
175+
"# Get the data for the cubic function, injected with noise and missing-ness\n",
175176
"def gen_data(x_min, x_max, n, train=True):\n",
176177
" x = np.random.triangular(x_min, 2, x_max, size=(n, 1))\n",
177178
"\n",
@@ -180,10 +181,11 @@
180181
"\n",
181182
" return x, y\n",
182183
"\n",
183-
"x, y = gen_data(-4, 4, 2000)\n",
184-
"x_val, y_val = gen_data(-6, 6, 500)\n",
185-
"plt.scatter(x_val,y_val, s=1.5, label='test data')\n",
186-
"plt.scatter(x,y, s=1.5, label='train data')\n",
184+
"# Plot the dataset and visualize the train and test datapoints\n",
185+
"x_train, y_train = gen_data(-4, 4, 2000) # train data\n",
186+
"x_test, y_test = gen_data(-6, 6, 500) # test data\n",
187+
"plt.scatter(x_train, y_train, s=1.5, label='train data')\n",
188+
"plt.scatter(x_test, y_test, s=1.5, label='test data')\n",
187189
"\n",
188190
"plt.legend()\n",
189191
"plt.show()"
@@ -209,8 +211,9 @@
209211
"id": "mXMOYRHnv8tF"
210212
},
211213
"source": [
212-
"### 1.2 Vanilla regression\n",
213-
"Let's define a small model that can predict `y` given `x`: this is a classical regression task!"
214+
"## 1.2 Regression on cubic dataset\n",
215+
"\n",
216+
"Next we will define a small dense neural network model that can predict `y` given `x`: this is a classical regression task! We will build the model and use the [`model.fit()`](https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit) function to train the model -- normally, without any risk-awareness -- using the train dataset that we visualized above."
214217
]
215218
},
216219
{
@@ -221,7 +224,10 @@
221224
},
222225
"outputs": [],
223226
"source": [
224-
"def create_standard_classifier():\n",
227+
"### Define and train a dense NN model for the regression task###\n",
228+
"\n",
229+
"'''Function to define a small dense NN'''\n",
230+
"def create_dense_NN():\n",
225231
" return tf.keras.Sequential(\n",
226232
" [\n",
227233
" tf.keras.Input(shape=(1,)),\n",
@@ -231,7 +237,17 @@
231237
" ]\n",
232238
" )\n",
233239
"\n",
234-
"standard_classifier = create_standard_classifier()"
240+
"dense_NN = create_dense_NN()\n",
241+
"\n",
242+
"# Build the model for regression, defining the loss function and optimizer\n",
243+
"dense_NN.compile(\n",
244+
" optimizer=tf.keras.optimizers.Adam(learning_rate=2e-3),\n",
245+
" loss=tf.keras.losses.MeanSquaredError(), # MSE loss for the regression task\n",
246+
")\n",
247+
"\n",
248+
"# TODO: Train the model for 10 epochs. Use model.fit().\n",
249+
"loss_history = dense_NN.fit(x_train, y_train, epochs=10) \n",
250+
"# loss_history = # TODO"
235251
]
236252
},
237253
{
@@ -240,54 +256,9 @@
240256
"id": "ovwYBUG3wTDv"
241257
},
242258
"source": [
243-
"Let's first train this model normally, without any wrapping. Which areas would you expect the model to do well in? Which areas should it do worse in?"
244-
]
245-
},
246-
{
247-
"cell_type": "code",
248-
"execution_count": null,
249-
"metadata": {
250-
"colab": {
251-
"base_uri": "https://localhost:8080/"
252-
},
253-
"id": "oPNxsGBRwaNA",
254-
"outputId": "0598cef9-350c-4785-a7a9-51ed3b54fd4b"
255-
},
256-
"outputs": [
257-
{
258-
"name": "stdout",
259-
"output_type": "stream",
260-
"text": [
261-
"Epoch 1/10\n",
262-
"63/63 [==============================] - 1s 2ms/step - loss: 5.5708\n",
263-
"Epoch 2/10\n",
264-
"63/63 [==============================] - 0s 2ms/step - loss: 4.3687\n",
265-
"Epoch 3/10\n",
266-
"63/63 [==============================] - 0s 2ms/step - loss: 3.9064\n",
267-
"Epoch 4/10\n",
268-
"63/63 [==============================] - 0s 2ms/step - loss: 3.1653\n",
269-
"Epoch 5/10\n",
270-
"63/63 [==============================] - 0s 4ms/step - loss: 2.1027\n",
271-
"Epoch 6/10\n",
272-
"63/63 [==============================] - 0s 3ms/step - loss: 1.6488\n",
273-
"Epoch 7/10\n",
274-
"63/63 [==============================] - 0s 2ms/step - loss: 1.3093\n",
275-
"Epoch 8/10\n",
276-
"63/63 [==============================] - 0s 2ms/step - loss: 1.1078\n",
277-
"Epoch 9/10\n",
278-
"63/63 [==============================] - 0s 2ms/step - loss: 0.9919\n",
279-
"Epoch 10/10\n",
280-
"63/63 [==============================] - 0s 3ms/step - loss: 0.8937\n"
281-
]
282-
}
283-
],
284-
"source": [
285-
"standard_classifier.compile(\n",
286-
" optimizer=tf.keras.optimizers.Adam(learning_rate=2e-3),\n",
287-
" loss=tf.keras.losses.MeanSquaredError(),\n",
288-
")\n",
259+
"Now, we are ready to evaluate our neural network. We use the test data to assess performance on the regression task, and visualize the predicted values against the true values.\n",
289260
"\n",
290-
"history = standard_classifier.fit(x, y, epochs=10)\n"
261+
"Given your observation of the data in the previous plot, where do you expect the model to perform well? Let's test the model and see:"
291262
]
292263
},
293264
{
@@ -326,11 +297,29 @@
326297
}
327298
],
328299
"source": [
329-
"plt.scatter(x_val, y_val, s=0.5, label='truth')\n",
330-
"plt.scatter(x_val, standard_classifier(x_val), s=0.5, label='predictions')\n",
300+
"# Pass the test data through the network and predict the y values\n",
301+
"y_predicted = dense_NN(x_test)\n",
302+
"\n",
303+
"# Visualize the true (x, y) pairs for the test data vs. the predicted values\n",
304+
"plt.scatter(x_test, y_test, s=0.5, label='truth')\n",
305+
"plt.scatter(x_test, y_predicted, s=0.5, label='predictions')\n",
331306
"plt.legend()"
332307
]
333308
},
309+
{
310+
"cell_type": "markdown",
311+
"source": [
312+
"\n",
313+
"Write short (~1 sentence) answers to the questions below to complete the `TODO`s:\n",
314+
"\n",
315+
"#### **TODO: Analyzing the performance of standard regression model**\n",
316+
"1. Where does the model perform well? How does this relate to aleatoric and epistemic uncertainty?\n",
317+
"2. Where does the model perform poorly? How does this relate to aleatoric and epistemic uncertainty?"
318+
],
319+
"metadata": {
320+
"id": "7Vktjwfu0ReH"
321+
}
322+
},
334323
{
335324
"cell_type": "markdown",
336325
"metadata": {

0 commit comments

Comments
 (0)