Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "YHI3vyhv5p85"
},
"source": [
"# Exercise 1 : Conversion from Celsius to Fahrenheit (Simple Regression Analysis)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "F8YVA_634OFk"
},
"source": [
"The problem we will solve is to convert from Celsius to Fahrenheit, where the approximate formula is:\n",
"\n",
"$$ f = c \\times 1.8 + 32 $$\n",
"\n",
"\n",
"Of course, it would be simple enough to create a conventional Python function that directly performs this calculation, but that wouldn't be machine learning.\n",
"\n",
"\n",
"Instead, we will give TensorFlow some sample Celsius values (0, 8, 15, 22, 38) and their corresponding Fahrenheit values (32, 46, 59, 72, 100).\n",
"Then, we will train a model that figures out the above formula through the training process. This is a _simple regression analysis_ problem."
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "fA93WUy1zzWf"
},
"source": [
"## Import dependencies\n",
"\n",
"First, import TensorFlow. Here, we're calling it `tf` for ease of use. We also tell it to only display errors.\n",
"\n",
"Next, import [NumPy](http://www.numpy.org/) as `np`. Numpy helps us to represent our data as highly performant lists."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "X9uIpOS2zx7k"
},
"outputs": [],
"source": [
"from __future__ import absolute_import, division, print_function, unicode_literals\n",
"import tensorflow as tf\n",
"\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "y_WQEM5MGmg3"
},
"outputs": [],
"source": [
"import logging\n",
"logger = tf.get_logger()\n",
"logger.setLevel(logging.ERROR)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "AC3EQFi20buB"
},
"source": [
"## Set up training data\n",
"\n",
"As we saw before, supervised Machine Learning is all about figuring out an algorithm given a set of inputs and outputs. Since the task in this Codelab is to create a model that can give the temperature in Fahrenheit when given the degrees in Celsius, we create two lists `celsius_q` and `fahrenheit_a` that we can use to train our model."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "gg4pn6aI1vms"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-40.0 degrees Celsius = -40.0 degrees Fahrenheit\n",
"-10.0 degrees Celsius = 14.0 degrees Fahrenheit\n",
"0.0 degrees Celsius = 32.0 degrees Fahrenheit\n",
"8.0 degrees Celsius = 46.0 degrees Fahrenheit\n",
"15.0 degrees Celsius = 59.0 degrees Fahrenheit\n",
"22.0 degrees Celsius = 72.0 degrees Fahrenheit\n",
"38.0 degrees Celsius = 100.0 degrees Fahrenheit\n"
]
}
],
"source": [
"celsius_q = np.array([-40, -10, 0, 8, 15, 22, 38], dtype=float)\n",
"fahrenheit_a = np.array([-40, 14, 32, 46, 59, 72, 100], dtype=float)\n",
"\n",
"for i,c in enumerate(celsius_q):\n",
" print(\"{} degrees Celsius = {} degrees Fahrenheit\".format(c, fahrenheit_a[i]))"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "wwJGmDrQ0EoB"
},
"source": [
"### Some Machine Learning terminology\n",
"\n",
" - **Feature** — The input(s) to our model. In this case, a single value — the degrees in Celsius.\n",
"\n",
" - **Labels/response variable** — The output our model predicts. In this case, a single value — the degrees in Fahrenheit. In a classification setting, we would predict labels (discrete classes), in a regression setting, we predict a continuous response variable, such as Fahrenheit.\n",
" - **Example** — A pair of inputs/outputs used during training. In our case a pair of values from `celsius_q` and `fahrenheit_a` at a specific index, such as `(22,72)`.\n"
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "VM7_9Klvq7MO"
},
"source": [
"## 1. Define the Network\n",
"\n",
"Next create the model. We will use simplest possible model we can, a Dense network. Since the problem is straightforward, this network will require only a single layer, with a single neuron.\n",
"\n",
"### Build a layer\n",
"\n",
"We'll call the layer `l0` and create it by instantiating `tf.keras.layers.Dense` with the following configuration:\n",
"\n",
"* `input_shape=[1]` — This specifies that the input to this layer is a single value. That is, the shape is a one-dimensional array with one member. Since this is the first (and only) layer, that input shape is the input shape of the entire model. The single value is a floating point number, representing degrees Celsius.\n",
"\n",
"* `units=1` — This specifies the number of neurons in the layer. The number of neurons defines how many internal variables the layer has to try to learn how to solve the problem (more later). Since this is the final layer, it is also the size of the model's output — a single float value representing degrees Fahrenheit. (In a multi-layered network, the size and shape of the layer would need to match the `input_shape` of the next layer.)\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "pRllo2HLfXiu"
},
"outputs": [],
"source": [
"l0 = tf.keras.layers.Dense(units=1, input_shape=[1])"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "_F00_J9duLBD"
},
"source": [
"### Assemble layers into the model\n",
"\n",
"Once layers are defined, they need to be assembled into a model. The Sequential model definition takes a list of layers as argument, specifying the calculation order from the input to the output.\n",
"\n",
"This model has just a single layer, l0."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = <--------- Your Code here -------------->"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "kiZG7uhm8qCF"
},
"source": [
"## 2. Compile the network, with loss and optimizer functions\n",
"\n",
"Before training, the model has to be compiled. When compiled for training, the model is given:\n",
"\n",
"- **Loss function** — A way of measuring how far off predictions are from the desired outcome. (The measured difference is called the \"loss\".)\n",
"\n",
"- **Optimizer function** — A way of adjusting internal values in order to reduce the loss.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"model.compile(loss='mean_squared_error',\n",
" optimizer=tf.keras.optimizers.Adam(0.1))"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "17M3Pqv4P52R"
},
"source": [
"These are used during training (`model.fit()`, below) to first calculate the loss at each point, and then improve it. In fact, the act of calculating the current loss of a model and then improving it is precisely what training is.\n",
"\n",
"During training, the optimizer function is used to calculate adjustments to the model's internal variables. The goal is to adjust the internal variables until the model (which is really a math function) mirrors the actual equation for converting Celsius to Fahrenheit.\n",
"\n",
"TensorFlow uses numerical analysis to perform this tuning, and all this complexity is hidden from you so we will not go into the details here. What is useful to know about these parameters are:\n",
"\n",
"The loss function ([mean squared error](https://en.wikipedia.org/wiki/Mean_squared_error)) and the optimizer ([Adam](https://machinelearningmastery.com/adam-optimization-algorithm-for-deep-learning/)) used here are standard for simple models like this one, but many others are available. It is not important to know how these specific functions work at this point.\n",
"\n",
"One part of the Optimizer you may need to think about when building your own models is the learning rate (`0.1` in the code above). This is the step size taken when adjusting values in the model. If the value is too small, it will take too many iterations to train the model. Too large, and accuracy goes down. Finding a good value often involves some trial and error, but the range is usually within 0.001 (default), and 0.1"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "c-Jk4dG91dvD"
},
"source": [
"## 3. Fit the model\n",
"\n",
"Train the model by calling the `fit` method.\n",
"\n",
"During training, the model takes in Celsius values, performs a calculation using the current internal variables (called \"weights\") and outputs values which are meant to be the Fahrenheit equivalent. Since the weights are initially set randomly, the output will not be close to the correct value. The difference between the actual output and the desired output is calculated using the loss function, and the optimizer function directs how the weights should be adjusted.\n",
"\n",
"This cycle of calculate, compare, adjust is controlled by the `fit` method. The first argument is the inputs, the second argument is the desired outputs. The `epochs` argument specifies how many times this cycle should be run, and the `verbose` argument controls how much output the method produces."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "lpRrl7WK10Pq"
},
"outputs": [],
"source": [
"history = model.fit(<--- your code here --->, <--- your code here --->, epochs=500, verbose=False)\n",
"print(\"Finished training the model\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "0-QsNCLD4MJZ"
},
"source": [
"## 4. Evaluate the Model - Display training statistics\n",
"\n",
"The `fit` method returns a history object. We can use this object to plot how the loss of our model goes down after each training epoch. A high loss means that the Fahrenheit degrees the model predicts is far from the corresponding value in `fahrenheit_a`.\n",
"\n",
"We'll use [Matplotlib](https://matplotlib.org/) to visualize this (you could use another tool). As you can see, our model improves very quickly at first, and then has a steady, slow improvement until it is very near \"perfect\" towards the end.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "IeK6BzfbdO6_"
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"plt.xlabel('Epoch Number')\n",
"plt.ylabel(\"Loss Magnitude\")\n",
"plt.plot(history.history['loss'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "LtQGDMob5LOD"
},
"source": [
"## 5. Use the model to predict values\n",
"\n",
"Now you have a model that has been trained to learn the relationship between `celsius_q` and `fahrenheit_a`. You can use the predict method to have it calculate the Fahrenheit degrees for a previously unknown Celsius degrees.\n",
"\n",
"So, for example, if the Celsius value is 100, what do you think the Fahrenheit result will be? Take a guess before you run this code."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "oxNzL4lS2Gui"
},
"outputs": [],
"source": [
"print(model.predict(<---- your code here ---->))"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "jApk6tZ1fBg1"
},
"source": [
"The correct answer is $100 \\times 1.8 + 32 = 212$, so our model is doing really well.\n",
"\n",
"### To review\n",
"\n",
"\n",
"* We created a model with a Dense layer\n",
"* We trained it with 3500 examples (7 pairs, over 500 epochs).\n",
"\n",
"Our model tuned the variables (weights) in the Dense layer until it was able to return the correct Fahrenheit value for any Celsius value. (Remember, 100 Celsius was not part of our training data.)\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "zRrOky5gm20Z"
},
"source": [
"## Looking at the layer weights\n",
"\n",
"Finally, let's print the internal variables of the Dense layer. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "kmIkVdkbnZJI"
},
"outputs": [],
"source": [
"print(\"These are the layer variables: {}\".format(l0.get_weights()))"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "RSplSnMvnWC-"
},
"source": [
"The first variable is close to ~1.8 and the second to ~32. These values (1.8 and 32) are the actual variables in the real conversion formula.\n",
"\n",
"This is really close to the values in the conversion formula. We'll explain this in an upcoming video where we show how a Dense layer works, but for a single neuron with a single input and a single output, the internal math looks the same as [the equation for a line](https://en.wikipedia.org/wiki/Linear_equation#Slope%E2%80%93intercept_form), $y = mx + b$, which has the same form as the conversion equation, $f = 1.8c + 32$.\n",
"\n",
"Since the form is the same, the variables should converge on the standard values of 1.8 and 32, which is exactly what happened.\n",
"\n",
"With additional neurons, additional inputs, and additional outputs, the formula becomes much more complex, but the idea is the same.\n",
"\n",
"### A little experiment\n",
"\n",
"Just for fun, what if we created more Dense layers with different units, which therefore also has more variables?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "Y2zTA-rDS5Xk"
},
"outputs": [],
"source": [
"l0 = tf.keras.layers.Dense(units=4, input_shape=[1])\n",
"l1 = tf.keras.layers.Dense(units=4)\n",
"l2 = tf.keras.layers.Dense(units=1)\n",
"model = tf.keras.Sequential([l0, l1, l2])\n",
"model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(0.1))\n",
"model.fit(celsius_q, fahrenheit_a, epochs=500, verbose=False)\n",
"print(\"Finished training the model\")\n",
"print(model.predict([100.0]))\n",
"print(\"Model predicts that 100 degrees Celsius is: {} degrees Fahrenheit\".format(model.predict([100.0])))\n",
"print(\"These are the l0 variables: {}\".format(l0.get_weights()))\n",
"print(\"These are the l1 variables: {}\".format(l1.get_weights()))\n",
"print(\"These are the l2 variables: {}\".format(l2.get_weights()))"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "xrpFFlgYhCty"
},
"source": [
"As you can see, this model is also able to predict the corresponding Fahrenheit value really well. But when you look at the variables (weights) in the `l0` and `l1` layers, they are nothing even close to ~1.8 and ~32. The added complexity hides the \"simple\" form of the conversion equation.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 2 : O-Rings seen with Logistic Regression"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook calculates a logistic regression using Keras. It's basically meant to show the principles of Keras.\n",
"\n",
"### Datset\n",
"\n",
"We investigate the data set of the challenger flight with broken O-rings (`Y=1`) versus start temperature."
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Text(0, 0.5, 'Broken O-rings')"
]
},
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEKCAYAAAAIO8L1AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAZM0lEQVR4nO3deZhldX3n8ffH7kZbgqDSY7QXRR+WOC4gpag4StxY4gBmnBGUPGqMPUZJ3IaMJA4hLiORuD0uKOKuD7g8hGkV05q4LyjdoiJgG0SUblQQA26tQPOdP+4pvBSnbt3q5tS5Xbxfz3Ofe8/vbN/6ddX59FnuOakqJEma6Q59FyBJmkwGhCSplQEhSWplQEiSWhkQkqRWBoQkqVVnAZHkXUmuSvKdWcY/Pcm3k1yY5CtJHtxVLZKk+etyD+I9wGEjxv8AeExVPRB4BXB6h7VIkuZpaVcLrqovJLnPiPFfGRo8D1jVVS2SpPnrLCDm6dnAJ2cbmWQtsBZg1113PXC//fZbqLokaVHYuHHjz6pqxXzm6T0gkvwxg4B41GzTVNXpNIegpqamasOGDQtUnSQtDkl+ON95eg2IJA8CzgAOr6pr+qxFknRLvV3mmmQNcDbwZ1X1vb7qkCS162wPIsmZwCHAnkk2A38PLAOoqrcBJwF3B96aBODGqprqqh5J0vx0eRXTsXOM/wvgL7pavyRpx/hNaklSKwNCktTKgJAktTIgJEmtDAhJUisDQpLUyoCQJLUyICRJrQwISVIrA0KS1MqAkCS1MiAkSa0MCElSKwNCktTKgJAktTIgJEmtDAhJUisDQpLUyoCQJLUyICRJrQwISVIrA0KS1MqAkCS1MiAkSa0MCElSq6VdLTjJu4AnAVdV1QNaxgd4I3AE8BvgmVX1ja7q0Y4754ItnLp+E1deu5V77bGcEw7dl6MPWNnrevuqaXvr3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5i0s7ud8CKJI8ASLIsyX+e5zLWTM8PPI3BIaNN81ju7vz+ds7PGGr/JbDb0PDlwIHN5yOBZbMsbz3wl81hLpLsk98/QEhaMAaEdnY3Mbit8j8m+RbwTeCR81zGJgYPNLqEwV1UT2sulx13uScDH0myEfjZUPvHgCdPn6Rm8KyCxzTLewS33GsYdgaDW0Z/I8l3GFym6t6+Fpx3c9XtWnO10cd3tu8IzKfunfVnVP/cg5B2TtuA3ZN8c9REzZ7Lx7jlno00FvcgJEmt3IOQJLUyICRJrQwISVIrA0KS1MqAkCS1+v/YKMuQWSKjDwAAAABJRU5ErkJggg==\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.image as imgplot\n",
"import numpy as np\n",
"import pandas as pd\n",
"import tempfile\n",
"data = np.asarray(pd.read_csv('./challenger.txt', sep=','), dtype='float32')\n",
"plt.plot(data[:,0], data[:,1], 'o')\n",
"plt.axis([40, 85, -0.1, 1.2])\n",
"plt.xlabel('Temperature [F]')\n",
"plt.ylabel('Broken O-rings')"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0. 1. 0. 0. 0. 0. 0. 0. 1. 1. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0. 1. 0. 1.]\n"
]
}
],
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Mathematical Notes\n",
"\n",
"We are considering the probability $P(y_i=1|x_i)$ for the class $y_i=1$ given the $i-$th data point $x_i$ ($x_i$ could be a vector). This is given by:\n",
"\n",
"$\n",
"P(y_i=1 | x_i) = \\frac{e^{(b + x_i w)}}{1 + e^{(b + x_i w)}} = [1 + e^{-(b + x_i w)}]^{-1}\n",
"$\n",
"\n",
"If we have more than one data point, which we usually do, we have to apply the equation above to each of the N data points. In this case we can used a vectorized version with $x=(x_1,x_2,\\ldots,x_N)$ and $y=(y_1,y_2,\\ldots,y_N$)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Numpy code\n",
"This numpy code, shows the calculation for one value using numpy (like a single forward pass)"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.882916\n",
"[0.999 0.998 0.998 0.998 0.999 0.996 0.996 0.998 1. 0.999 0.998 0.988\n",
" 0.999 1. 0.999 0.993 0.998 0.978 0.992 0.985 0.993 0.992 1. ]\n"
]
}
],
"source": [
"# Data\n",
"N = len(data)\n",
"x = data[:,0]\n",
"y = data[:,1]\n",
"# Initial Value for the weights\n",
"w = -0.20\n",
"b = 20.0\n",
"p_1 = 1 / (1 + np.exp(-x*w - b))\n",
"# cross-entropy loss function\n",
"cross_entropy = y * np.log(p_1) + (1-y) * np.log(1-p_1)\n",
"print(-np.mean(cross_entropy))\n",
"print(np.round(p_1,3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Better values from intuition\n",
"\n",
"Now lets try to find better values for $W$ and $b$. Lets assume $W$ is given with $-1$. We want the probability\n",
"for a damage $P(y_i=1 | x_i)$ to be $0.5$.\n",
"Determine an appropriate value for $b$.\n",
"Hint: at which $x$ value should $P(y_i=1 | x_i)$ be $0.5$, look at the data. At this $x$ value the term $1 + e^{-(b + W’ x_i)}$ must be $2$.\n",
"\n",
"**Solution**\n",
"\n",
"$P(y=1 | x) = 0.5$ at $x \\approx 65$ \n",
"\n",
"$-(b + (-1) x_i) = 0 \\rightarrow b = 65$ "
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.9094435\n",
"[0.269 0.007 0.018 0.047 0.119 0.001 0. 0.007 1. 0.881 0.007 0.\n",
" 0.119 1. 0.119 0. 0.007 0. 0. 0. 0. 0. 0.999]\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSU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AAASUVORK5CYII=\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"w_val = -1\n",
"b_val = 65\n",
"plt.plot(data[:,0], data[:,1], 'o')\n",
"plt.axis([40, 85, -0.1, 1.2])\n",
"x_pred = np.linspace(40,85)\n",
"x_pred = np.resize(x_pred,[len(x_pred),1])\n",
"y_pred = 1 / (1 + np.exp(-x_pred*w_val - b_val))\n",
"plt.plot(x_pred, y_pred)\n",
"\n",
"p_1 = 1 / (1 + np.exp(-x*w_val - b_val))\n",
"\n",
"# cross-entropy loss function\n",
"cross_entropy = -np.mean(y * np.log(p_1) + (1-y) * np.log(1-p_1))\n",
" \n",
"print(cross_entropy)\n",
"print(np.round(p_1,3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that the value of the cross-entropy has decreased from 3.882916 to 0.9094435."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## TODO : determine the accuracy of this logistic regression model and the value of the cross-entropy function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## TODO : set up a Keras model\n",
"\n",
"If there are two labels, we use `binary_crossentropy` as loss function. In this case, we use"
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
},
{
"data": {
"text/plain": [
"<tensorflow.python.keras.callbacks.History at 0x14081c048>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l0 = tf.keras.layers.Dense(<--- your code here ---->)\n",
"model = tf.keras.Sequential([l0])\n",
"model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(0.01))\n",
"model.fit(x, y, epochs=10000, verbose=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.plot(data[:,0], data[:,1], 'o')\n",
"plt.axis([40, 85, -0.1, 1.2])\n",
"x_pred = np.linspace(40,85)\n",
"x_pred = np.resize(x_pred,[len(x_pred),1])\n",
"y_pred = <---- your code here ---->\n",
"plt.plot(x_pred, y_pred)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 3 : MNIST and Multinomial Logistic Regression"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this exercise we use multinomial logistic regression to predict the handwritten digits of the MNIST dataset."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## TODO : read MNIST data and compute validation accuracy for a multinomial logistic regression model, see [Multinomial Logistic Regression](https://en.wikipedia.org/wiki/Multinomial_logistic_regression)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from __future__ import absolute_import, division, print_function, unicode_literals\n",
"from tensorflow.keras.datasets import mnist\n",
"from tensorflow.keras.utils import to_categorical\n",
"\n",
"\n",
"# Import TensorFlow and TensorFlow Datasets\n",
"import tensorflow as tf\n",
"\n",
"# Helper libraries\n",
"import math\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Load MNIST data\n",
"(X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
"\n",
"# One-hot-encoded label vector\n",
"y_train_cat = to_categorical(y_train, 10)\n",
"y_test_cat = to_categorical(y_test, 10)\n",
"\n",
"model = tf.keras.Sequential()\n",
"model.add(tf.keras.layers.Flatten(<---- your code here ---->))\n",
"model.add(tf.keras.layers.Dense(<---- your code here ---->))\n",
"model.compile(<---- your code here ---->)\n",
"\n",
"history = model.fit(<---- your code here ---->)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## TODO : use different regularization terms, see [Keras Regularizer](https://keras.io/regularizers/)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import TensorFlow and TensorFlow Datasets\n",
"import tensorflow as tf\n",
"\n",
"# Helper libraries\n",
"import math\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Load MNIST data\n",
"(X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
"\n",
"# One-hot-encode label vector\n",
"y_train_cat = to_categorical(y_train, 10)\n",
"y_test_cat = to_categorical(y_test, 10)\n",
"\n",
"# Define Network\n",
"model = tf.keras.Sequential()\n",
"model.add(tf.keras.layers.Flatten(<---- your code here ---->)\n",
"model.add(tf.keras.layers.Dense(<---- your code here ---->\n",
" kernel_regularizer=<---- your code here ---->))\n",
"\n",
"# Compile Network\n",
"model.compile(<---- your code here ---->)\n",
"\n",
"# Fit Network\n",
"history = model.fit(<---- your code here ---->)"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "Celsius to Fahrenheit.ipynb",
"private_outputs": true,
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}