|
172 | 172 | }
|
173 | 173 | ],
|
174 | 174 | "source": [
|
| 175 | + "# Get the data for the cubic function, injected with noise and missing-ness\n", |
175 | 176 | "def gen_data(x_min, x_max, n, train=True):\n",
|
176 | 177 | " x = np.random.triangular(x_min, 2, x_max, size=(n, 1))\n",
|
177 | 178 | "\n",
|
|
180 | 181 | "\n",
|
181 | 182 | " return x, y\n",
|
182 | 183 | "\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", |
187 | 189 | "\n",
|
188 | 190 | "plt.legend()\n",
|
189 | 191 | "plt.show()"
|
|
209 | 211 | "id": "mXMOYRHnv8tF"
|
210 | 212 | },
|
211 | 213 | "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." |
214 | 217 | ]
|
215 | 218 | },
|
216 | 219 | {
|
|
221 | 224 | },
|
222 | 225 | "outputs": [],
|
223 | 226 | "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", |
225 | 231 | " return tf.keras.Sequential(\n",
|
226 | 232 | " [\n",
|
227 | 233 | " tf.keras.Input(shape=(1,)),\n",
|
|
231 | 237 | " ]\n",
|
232 | 238 | " )\n",
|
233 | 239 | "\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" |
235 | 251 | ]
|
236 | 252 | },
|
237 | 253 | {
|
|
240 | 256 | "id": "ovwYBUG3wTDv"
|
241 | 257 | },
|
242 | 258 | "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", |
289 | 260 | "\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:" |
291 | 262 | ]
|
292 | 263 | },
|
293 | 264 | {
|
|
326 | 297 | }
|
327 | 298 | ],
|
328 | 299 | "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", |
331 | 306 | "plt.legend()"
|
332 | 307 | ]
|
333 | 308 | },
|
| 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 | + }, |
334 | 323 | {
|
335 | 324 | "cell_type": "markdown",
|
336 | 325 | "metadata": {
|
|
0 commit comments