Hello There
Table of Contents
Beginning
This 'Hello World' takes data created by a simple linear model and trains a neural network to model it. The actual model will take this form:
\[ y = mx + b \]
Imports
Python
from argparse import Namespace
from functools import partial
from pathlib import Path
import random
PyPi
from sklearn import linear_model
from tensorflow import keras
import holoviews
import numpy
import pandas
import tensorflow
My Stuff
from graeae.visualization.embed import EmbedHoloview as EmbedHoloviews
Set Up
Plotting
Embed = partial(EmbedHoloviews,
folder_path=Path("../../files/posts/keras/hello-there/"))
Plot = Namespace(
height=800,
width=1000,
)
holoviews.extension("bokeh")
The Random Seed
numpy.random.seed(2019)
Middle
The Data
X = 20 * numpy.random.random_sample((10,)) - 10
slope = random.randrange(2, 10)
intercept = random.randrange(200)
Y = slope * X + intercept
print(X)
print(Y)
data = pandas.DataFrame(dict(X=X,
Y=Y))
[ 8.06964429 -2.13838987 2.47939923 2.75754802 7.60998138 -4.01655961 4.0439654 8.06412323 7.62763853 -1.88500404] [121.48751002 50.03127093 82.35579458 84.30283614 118.26986963 36.88408272 93.30775783 121.44886259 118.39346971 51.80497172]
Our line is
print("\\[")
print(f"y = {slope} x + {intercept}")
print("\\]")
\[ y = 7 x + 65 \] \[ y = 7 x + 32 \]
plot = holoviews.Scatter(dict(x=data.X, y=data.Y)).opts(
height=Plot.height,
width=Plot.width,
)
Embed(plot=plot, file_name="data_scatter")()
Defining the Neural Network
Our model will be a fully-connected network with one layer with one neuron that takes one input.
- Sequential : A linear stack of layers
- Dense: A densely connected neural network layer
model = keras.Sequential()
model.add(keras.layers.Dense(units=1, input_shape=[1]))
Note: The original notebook passed the Dense layer into the constructor, but this gives a warning that you should pass in the dtype instead. Adding it to the constructed object seems to be the way they prefer to do it currently.
Compiling the Model
"Compiling" in this case means telling the model what optimizer and loss methods to use. In this case it will be Stochastic Gradient Descent and Mean Squared Error.
model.compile(optimizer='sgd', loss='mean_squared_error')
Training The Model
Training is done with the model's fit method. epochs
is the number of times to repeat training.
model.fit(X, Y, epochs=500)
Epoch 1/500 10/10 [==============================] - 0s 7ms/sample - loss: 9059.2910 Epoch 2/500 10/10 [==============================] - 0s 142us/sample - loss: 3522.3921 Epoch 3/500 10/10 [==============================] - 0s 117us/sample - loss: 2619.3438 Epoch 4/500 10/10 [==============================] - 0s 96us/sample - loss: 2428.1226 Epoch 5/500 10/10 [==============================] - 0s 117us/sample - loss: 2347.3765 Epoch 6/500 10/10 [==============================] - 0s 114us/sample - loss: 2284.8735 Epoch 7/500 10/10 [==============================] - 0s 141us/sample - loss: 2226.4395 Epoch 8/500 10/10 [==============================] - 0s 125us/sample - loss: 2169.8687 Epoch 9/500 10/10 [==============================] - 0s 121us/sample - loss: 2114.7925 Epoch 10/500 10/10 [==============================] - 0s 131us/sample - loss: 2061.1226 Epoch 11/500 10/10 [==============================] - 0s 126us/sample - loss: 2008.8164 Epoch 12/500 10/10 [==============================] - 0s 139us/sample - loss: 1957.8376 Epoch 13/500 10/10 [==============================] - 0s 140us/sample - loss: 1908.1527 Epoch 14/500 10/10 [==============================] - 0s 147us/sample - loss: 1859.7283 Epoch 15/500 10/10 [==============================] - 0s 152us/sample - loss: 1812.5332 Epoch 16/500 10/10 [==============================] - 0s 140us/sample - loss: 1766.5355 Epoch 17/500 10/10 [==============================] - 0s 160us/sample - loss: 1721.7054 Epoch 18/500 10/10 [==============================] - 0s 112us/sample - loss: 1678.0129 Epoch 19/500 10/10 [==============================] - 0s 116us/sample - loss: 1635.4291 Epoch 20/500 10/10 [==============================] - 0s 114us/sample - loss: 1593.9260 Epoch 21/500 10/10 [==============================] - 0s 113us/sample - loss: 1553.4761 Epoch 22/500 10/10 [==============================] - 0s 108us/sample - loss: 1514.0527 Epoch 23/500 10/10 [==============================] - 0s 109us/sample - loss: 1475.6299 Epoch 24/500 10/10 [==============================] - 0s 98us/sample - loss: 1438.1820 Epoch 25/500 10/10 [==============================] - 0s 124us/sample - loss: 1401.6846 Epoch 26/500 10/10 [==============================] - 0s 118us/sample - loss: 1366.1134 Epoch 27/500 10/10 [==============================] - 0s 111us/sample - loss: 1331.4449 Epoch 28/500 10/10 [==============================] - 0s 102us/sample - loss: 1297.6560 Epoch 29/500 10/10 [==============================] - 0s 115us/sample - loss: 1264.7249 Epoch 30/500 10/10 [==============================] - 0s 118us/sample - loss: 1232.6293 Epoch 31/500 10/10 [==============================] - 0s 107us/sample - loss: 1201.3483 Epoch 32/500 10/10 [==============================] - 0s 108us/sample - loss: 1170.8608 Epoch 33/500 10/10 [==============================] - 0s 106us/sample - loss: 1141.1475 Epoch 34/500 10/10 [==============================] - 0s 113us/sample - loss: 1112.1881 Epoch 35/500 10/10 [==============================] - 0s 113us/sample - loss: 1083.9634 Epoch 36/500 10/10 [==============================] - 0s 117us/sample - loss: 1056.4552 Epoch 37/500 10/10 [==============================] - 0s 96us/sample - loss: 1029.6449 Epoch 38/500 10/10 [==============================] - 0s 120us/sample - loss: 1003.5151 Epoch 39/500 10/10 [==============================] - 0s 101us/sample - loss: 978.0483 Epoch 40/500 10/10 [==============================] - 0s 108us/sample - loss: 953.2279 Epoch 41/500 10/10 [==============================] - 0s 104us/sample - loss: 929.0374 Epoch 42/500 10/10 [==============================] - 0s 111us/sample - loss: 905.4608 Epoch 43/500 10/10 [==============================] - 0s 104us/sample - loss: 882.4824 Epoch 44/500 10/10 [==============================] - 0s 113us/sample - loss: 860.0872 Epoch 45/500 10/10 [==============================] - 0s 118us/sample - loss: 838.2604 Epoch 46/500 10/10 [==============================] - 0s 117us/sample - loss: 816.9874 Epoch 47/500 10/10 [==============================] - 0s 100us/sample - loss: 796.2543 Epoch 48/500 10/10 [==============================] - 0s 108us/sample - loss: 776.0475 Epoch 49/500 10/10 [==============================] - 0s 128us/sample - loss: 756.3533 Epoch 50/500 10/10 [==============================] - 0s 120us/sample - loss: 737.1589 Epoch 51/500 10/10 [==============================] - 0s 139us/sample - loss: 718.4517 Epoch 52/500 10/10 [==============================] - 0s 144us/sample - loss: 700.2191 Epoch 53/500 10/10 [==============================] - 0s 126us/sample - loss: 682.4492 Epoch 54/500 10/10 [==============================] - 0s 126us/sample - loss: 665.1305 Epoch 55/500 10/10 [==============================] - 0s 137us/sample - loss: 648.2512 Epoch 56/500 10/10 [==============================] - 0s 156us/sample - loss: 631.8002 Epoch 57/500 10/10 [==============================] - 0s 131us/sample - loss: 615.7667 Epoch 58/500 10/10 [==============================] - 0s 117us/sample - loss: 600.1400 Epoch 59/500 10/10 [==============================] - 0s 119us/sample - loss: 584.9100 Epoch 60/500 10/10 [==============================] - 0s 138us/sample - loss: 570.0665 Epoch 61/500 10/10 [==============================] - 0s 102us/sample - loss: 555.5996 Epoch 62/500 10/10 [==============================] - 0s 117us/sample - loss: 541.4998 Epoch 63/500 10/10 [==============================] - 0s 117us/sample - loss: 527.7579 Epoch 64/500 10/10 [==============================] - 0s 118us/sample - loss: 514.3649 Epoch 65/500 10/10 [==============================] - 0s 104us/sample - loss: 501.3116 Epoch 66/500 10/10 [==============================] - 0s 113us/sample - loss: 488.5895 Epoch 67/500 10/10 [==============================] - 0s 113us/sample - loss: 476.1904 Epoch 68/500 10/10 [==============================] - 0s 109us/sample - loss: 464.1059 Epoch 69/500 10/10 [==============================] - 0s 179us/sample - loss: 452.3281 Epoch 70/500 10/10 [==============================] - 0s 117us/sample - loss: 440.8490 Epoch 71/500 10/10 [==============================] - 0s 118us/sample - loss: 429.6613 Epoch 72/500 10/10 [==============================] - 0s 118us/sample - loss: 418.7576 Epoch 73/500 10/10 [==============================] - 0s 116us/sample - loss: 408.1307 Epoch 74/500 10/10 [==============================] - 0s 115us/sample - loss: 397.7733 Epoch 75/500 10/10 [==============================] - 0s 119us/sample - loss: 387.6787 Epoch 76/500 10/10 [==============================] - 0s 122us/sample - loss: 377.8404 Epoch 77/500 10/10 [==============================] - 0s 124us/sample - loss: 368.2518 Epoch 78/500 10/10 [==============================] - 0s 120us/sample - loss: 358.9065 Epoch 79/500 10/10 [==============================] - 0s 123us/sample - loss: 349.7983 Epoch 80/500 10/10 [==============================] - 0s 104us/sample - loss: 340.9213 Epoch 81/500 10/10 [==============================] - 0s 107us/sample - loss: 332.2696 Epoch 82/500 10/10 [==============================] - 0s 117us/sample - loss: 323.8374 Epoch 83/500 10/10 [==============================] - 0s 108us/sample - loss: 315.6192 Epoch 84/500 10/10 [==============================] - 0s 117us/sample - loss: 307.6096 Epoch 85/500 10/10 [==============================] - 0s 113us/sample - loss: 299.8033 Epoch 86/500 10/10 [==============================] - 0s 108us/sample - loss: 292.1950 Epoch 87/500 10/10 [==============================] - 0s 97us/sample - loss: 284.7798 Epoch 88/500 10/10 [==============================] - 0s 116us/sample - loss: 277.5528 Epoch 89/500 10/10 [==============================] - 0s 123us/sample - loss: 270.5092 Epoch 90/500 10/10 [==============================] - 0s 122us/sample - loss: 263.6443 Epoch 91/500 10/10 [==============================] - 0s 118us/sample - loss: 256.9538 Epoch 92/500 10/10 [==============================] - 0s 121us/sample - loss: 250.4329 Epoch 93/500 10/10 [==============================] - 0s 118us/sample - loss: 244.0775 Epoch 94/500 10/10 [==============================] - 0s 124us/sample - loss: 237.8834 Epoch 95/500 10/10 [==============================] - 0s 118us/sample - loss: 231.8465 Epoch 96/500 10/10 [==============================] - 0s 136us/sample - loss: 225.9629 Epoch 97/500 10/10 [==============================] - 0s 140us/sample - loss: 220.2286 Epoch 98/500 10/10 [==============================] - 0s 148us/sample - loss: 214.6397 Epoch 99/500 10/10 [==============================] - 0s 144us/sample - loss: 209.1927 Epoch 100/500 10/10 [==============================] - 0s 153us/sample - loss: 203.8839 Epoch 101/500 10/10 [==============================] - 0s 146us/sample - loss: 198.7099 Epoch 102/500 10/10 [==============================] - 0s 145us/sample - loss: 193.6671 Epoch 103/500 10/10 [==============================] - 0s 142us/sample - loss: 188.7523 Epoch 104/500 10/10 [==============================] - 0s 184us/sample - loss: 183.9622 Epoch 105/500 10/10 [==============================] - 0s 121us/sample - loss: 179.2937 Epoch 106/500 10/10 [==============================] - 0s 115us/sample - loss: 174.7437 Epoch 107/500 10/10 [==============================] - 0s 143us/sample - loss: 170.3092 Epoch 108/500 10/10 [==============================] - 0s 163us/sample - loss: 165.9872 Epoch 109/500 10/10 [==============================] - 0s 123us/sample - loss: 161.7748 Epoch 110/500 10/10 [==============================] - 0s 112us/sample - loss: 157.6694 Epoch 111/500 10/10 [==============================] - 0s 139us/sample - loss: 153.6682 Epoch 112/500 10/10 [==============================] - 0s 131us/sample - loss: 149.7685 Epoch 113/500 10/10 [==============================] - 0s 127us/sample - loss: 145.9678 Epoch 114/500 10/10 [==============================] - 0s 145us/sample - loss: 142.2635 Epoch 115/500 10/10 [==============================] - 0s 134us/sample - loss: 138.6532 Epoch 116/500 10/10 [==============================] - 0s 111us/sample - loss: 135.1345 Epoch 117/500 10/10 [==============================] - 0s 116us/sample - loss: 131.7051 Epoch 118/500 10/10 [==============================] - 0s 148us/sample - loss: 128.3628 Epoch 119/500 10/10 [==============================] - 0s 120us/sample - loss: 125.1053 Epoch 120/500 10/10 [==============================] - 0s 118us/sample - loss: 121.9304 Epoch 121/500 10/10 [==============================] - 0s 127us/sample - loss: 118.8361 Epoch 122/500 10/10 [==============================] - 0s 121us/sample - loss: 115.8204 Epoch 123/500 10/10 [==============================] - 0s 117us/sample - loss: 112.8811 Epoch 124/500 10/10 [==============================] - 0s 106us/sample - loss: 110.0165 Epoch 125/500 10/10 [==============================] - 0s 112us/sample - loss: 107.2246 Epoch 126/500 10/10 [==============================] - 0s 97us/sample - loss: 104.5035 Epoch 127/500 10/10 [==============================] - 0s 132us/sample - loss: 101.8514 Epoch 128/500 10/10 [==============================] - 0s 92us/sample - loss: 99.2667 Epoch 129/500 10/10 [==============================] - 0s 96us/sample - loss: 96.7476 Epoch 130/500 10/10 [==============================] - 0s 125us/sample - loss: 94.2923 Epoch 131/500 10/10 [==============================] - 0s 185us/sample - loss: 91.8994 Epoch 132/500 10/10 [==============================] - 0s 127us/sample - loss: 89.5672 Epoch 133/500 10/10 [==============================] - 0s 131us/sample - loss: 87.2943 Epoch 134/500 10/10 [==============================] - 0s 157us/sample - loss: 85.0790 Epoch 135/500 10/10 [==============================] - 0s 134us/sample - loss: 82.9199 Epoch 136/500 10/10 [==============================] - 0s 150us/sample - loss: 80.8156 Epoch 137/500 10/10 [==============================] - 0s 124us/sample - loss: 78.7647 Epoch 138/500 10/10 [==============================] - 0s 121us/sample - loss: 76.7658 Epoch 139/500 10/10 [==============================] - 0s 111us/sample - loss: 74.8177 Epoch 140/500 10/10 [==============================] - 0s 109us/sample - loss: 72.9190 Epoch 141/500 10/10 [==============================] - 0s 122us/sample - loss: 71.0685 Epoch 142/500 10/10 [==============================] - 0s 113us/sample - loss: 69.2649 Epoch 143/500 10/10 [==============================] - 0s 119us/sample - loss: 67.5071 Epoch 144/500 10/10 [==============================] - 0s 125us/sample - loss: 65.7940 Epoch 145/500 10/10 [==============================] - 0s 110us/sample - loss: 64.1243 Epoch 146/500 10/10 [==============================] - 0s 159us/sample - loss: 62.4970 Epoch 147/500 10/10 [==============================] - 0s 115us/sample - loss: 60.9109 Epoch 148/500 10/10 [==============================] - 0s 136us/sample - loss: 59.3652 Epoch 149/500 10/10 [==============================] - 0s 139us/sample - loss: 57.8586 Epoch 150/500 10/10 [==============================] - 0s 127us/sample - loss: 56.3903 Epoch 151/500 10/10 [==============================] - 0s 122us/sample - loss: 54.9593 Epoch 152/500 10/10 [==============================] - 0s 102us/sample - loss: 53.5645 Epoch 153/500 10/10 [==============================] - 0s 159us/sample - loss: 52.2052 Epoch 154/500 10/10 [==============================] - 0s 129us/sample - loss: 50.8803 Epoch 155/500 10/10 [==============================] - 0s 122us/sample - loss: 49.5891 Epoch 156/500 10/10 [==============================] - 0s 129us/sample - loss: 48.3307 Epoch 157/500 10/10 [==============================] - 0s 132us/sample - loss: 47.1041 Epoch 158/500 10/10 [==============================] - 0s 136us/sample - loss: 45.9088 Epoch 159/500 10/10 [==============================] - 0s 130us/sample - loss: 44.7437 Epoch 160/500 10/10 [==============================] - 0s 117us/sample - loss: 43.6082 Epoch 161/500 10/10 [==============================] - 0s 121us/sample - loss: 42.5016 Epoch 162/500 10/10 [==============================] - 0s 119us/sample - loss: 41.4230 Epoch 163/500 10/10 [==============================] - 0s 125us/sample - loss: 40.3718 Epoch 164/500 10/10 [==============================] - 0s 125us/sample - loss: 39.3473 Epoch 165/500 10/10 [==============================] - 0s 106us/sample - loss: 38.3487 Epoch 166/500 10/10 [==============================] - 0s 183us/sample - loss: 37.3755 Epoch 167/500 10/10 [==============================] - 0s 133us/sample - loss: 36.4271 Epoch 168/500 10/10 [==============================] - 0s 139us/sample - loss: 35.5026 Epoch 169/500 10/10 [==============================] - 0s 118us/sample - loss: 34.6017 Epoch 170/500 10/10 [==============================] - 0s 136us/sample - loss: 33.7236 Epoch 171/500 10/10 [==============================] - 0s 140us/sample - loss: 32.8677 Epoch 172/500 10/10 [==============================] - 0s 138us/sample - loss: 32.0336 Epoch 173/500 10/10 [==============================] - 0s 127us/sample - loss: 31.2207 Epoch 174/500 10/10 [==============================] - 0s 139us/sample - loss: 30.4284 Epoch 175/500 10/10 [==============================] - 0s 121us/sample - loss: 29.6562 Epoch 176/500 10/10 [==============================] - 0s 185us/sample - loss: 28.9036 Epoch 177/500 10/10 [==============================] - 0s 126us/sample - loss: 28.1701 Epoch 178/500 10/10 [==============================] - 0s 213us/sample - loss: 27.4552 Epoch 179/500 10/10 [==============================] - 0s 115us/sample - loss: 26.7585 Epoch 180/500 10/10 [==============================] - 0s 111us/sample - loss: 26.0794 Epoch 181/500 10/10 [==============================] - 0s 201us/sample - loss: 25.4176 Epoch 182/500 10/10 [==============================] - 0s 124us/sample - loss: 24.7726 Epoch 183/500 10/10 [==============================] - 0s 130us/sample - loss: 24.1439 Epoch 184/500 10/10 [==============================] - 0s 130us/sample - loss: 23.5312 Epoch 185/500 10/10 [==============================] - 0s 127us/sample - loss: 22.9340 Epoch 186/500 10/10 [==============================] - 0s 130us/sample - loss: 22.3520 Epoch 187/500 10/10 [==============================] - 0s 100us/sample - loss: 21.7848 Epoch 188/500 10/10 [==============================] - 0s 128us/sample - loss: 21.2319 Epoch 189/500 10/10 [==============================] - 0s 124us/sample - loss: 20.6931 Epoch 190/500 10/10 [==============================] - 0s 150us/sample - loss: 20.1680 Epoch 191/500 10/10 [==============================] - 0s 122us/sample - loss: 19.6561 Epoch 192/500 10/10 [==============================] - 0s 154us/sample - loss: 19.1573 Epoch 193/500 10/10 [==============================] - 0s 132us/sample - loss: 18.6711 Epoch 194/500 10/10 [==============================] - 0s 128us/sample - loss: 18.1973 Epoch 195/500 10/10 [==============================] - 0s 127us/sample - loss: 17.7355 Epoch 196/500 10/10 [==============================] - 0s 128us/sample - loss: 17.2854 Epoch 197/500 10/10 [==============================] - 0s 151us/sample - loss: 16.8468 Epoch 198/500 10/10 [==============================] - 0s 125us/sample - loss: 16.4192 Epoch 199/500 10/10 [==============================] - 0s 105us/sample - loss: 16.0025 Epoch 200/500 10/10 [==============================] - 0s 135us/sample - loss: 15.5964 Epoch 201/500 10/10 [==============================] - 0s 102us/sample - loss: 15.2006 Epoch 202/500 10/10 [==============================] - 0s 119us/sample - loss: 14.8149 Epoch 203/500 10/10 [==============================] - 0s 100us/sample - loss: 14.4389 Epoch 204/500 10/10 [==============================] - 0s 109us/sample - loss: 14.0725 Epoch 205/500 10/10 [==============================] - 0s 122us/sample - loss: 13.7154 Epoch 206/500 10/10 [==============================] - 0s 108us/sample - loss: 13.3673 Epoch 207/500 10/10 [==============================] - 0s 113us/sample - loss: 13.0281 Epoch 208/500 10/10 [==============================] - 0s 109us/sample - loss: 12.6975 Epoch 209/500 10/10 [==============================] - 0s 118us/sample - loss: 12.3753 Epoch 210/500 10/10 [==============================] - 0s 138us/sample - loss: 12.0612 Epoch 211/500 10/10 [==============================] - 0s 118us/sample - loss: 11.7551 Epoch 212/500 10/10 [==============================] - 0s 123us/sample - loss: 11.4568 Epoch 213/500 10/10 [==============================] - 0s 126us/sample - loss: 11.1661 Epoch 214/500 10/10 [==============================] - 0s 127us/sample - loss: 10.8827 Epoch 215/500 10/10 [==============================] - 0s 130us/sample - loss: 10.6065 Epoch 216/500 10/10 [==============================] - 0s 171us/sample - loss: 10.3374 Epoch 217/500 10/10 [==============================] - 0s 140us/sample - loss: 10.0750 Epoch 218/500 10/10 [==============================] - 0s 150us/sample - loss: 9.8193 Epoch 219/500 10/10 [==============================] - 0s 144us/sample - loss: 9.5702 Epoch 220/500 10/10 [==============================] - 0s 154us/sample - loss: 9.3273 Epoch 221/500 10/10 [==============================] - 0s 187us/sample - loss: 9.0906 Epoch 222/500 10/10 [==============================] - 0s 160us/sample - loss: 8.8599 Epoch 223/500 10/10 [==============================] - 0s 156us/sample - loss: 8.6350 Epoch 224/500 10/10 [==============================] - 0s 113us/sample - loss: 8.4159 Epoch 225/500 10/10 [==============================] - 0s 109us/sample - loss: 8.2023 Epoch 226/500 10/10 [==============================] - 0s 111us/sample - loss: 7.9942 Epoch 227/500 10/10 [==============================] - 0s 107us/sample - loss: 7.7913 Epoch 228/500 10/10 [==============================] - 0s 196us/sample - loss: 7.5936 Epoch 229/500 10/10 [==============================] - 0s 129us/sample - loss: 7.4009 Epoch 230/500 10/10 [==============================] - 0s 132us/sample - loss: 7.2131 Epoch 231/500 10/10 [==============================] - 0s 107us/sample - loss: 7.0300 Epoch 232/500 10/10 [==============================] - 0s 104us/sample - loss: 6.8516 Epoch 233/500 10/10 [==============================] - 0s 122us/sample - loss: 6.6777 Epoch 234/500 10/10 [==============================] - 0s 124us/sample - loss: 6.5083 Epoch 235/500 10/10 [==============================] - 0s 178us/sample - loss: 6.3431 Epoch 236/500 10/10 [==============================] - 0s 196us/sample - loss: 6.1821 Epoch 237/500 10/10 [==============================] - 0s 177us/sample - loss: 6.0253 Epoch 238/500 10/10 [==============================] - 0s 196us/sample - loss: 5.8724 Epoch 239/500 10/10 [==============================] - 0s 111us/sample - loss: 5.7233 Epoch 240/500 10/10 [==============================] - 0s 113us/sample - loss: 5.5781 Epoch 241/500 10/10 [==============================] - 0s 128us/sample - loss: 5.4365 Epoch 242/500 10/10 [==============================] - 0s 110us/sample - loss: 5.2986 Epoch 243/500 10/10 [==============================] - 0s 121us/sample - loss: 5.1641 Epoch 244/500 10/10 [==============================] - 0s 98us/sample - loss: 5.0330 Epoch 245/500 10/10 [==============================] - 0s 119us/sample - loss: 4.9053 Epoch 246/500 10/10 [==============================] - 0s 96us/sample - loss: 4.7808 Epoch 247/500 10/10 [==============================] - 0s 130us/sample - loss: 4.6595 Epoch 248/500 10/10 [==============================] - 0s 146us/sample - loss: 4.5413 Epoch 249/500 10/10 [==============================] - 0s 159us/sample - loss: 4.4260 Epoch 250/500 10/10 [==============================] - 0s 117us/sample - loss: 4.3137 Epoch 251/500 10/10 [==============================] - 0s 121us/sample - loss: 4.2042 Epoch 252/500 10/10 [==============================] - 0s 168us/sample - loss: 4.0975 Epoch 253/500 10/10 [==============================] - 0s 124us/sample - loss: 3.9936 Epoch 254/500 10/10 [==============================] - 0s 131us/sample - loss: 3.8922 Epoch 255/500 10/10 [==============================] - 0s 147us/sample - loss: 3.7934 Epoch 256/500 10/10 [==============================] - 0s 130us/sample - loss: 3.6972 Epoch 257/500 10/10 [==============================] - 0s 131us/sample - loss: 3.6034 Epoch 258/500 10/10 [==============================] - 0s 110us/sample - loss: 3.5119 Epoch 259/500 10/10 [==============================] - 0s 120us/sample - loss: 3.4228 Epoch 260/500 10/10 [==============================] - 0s 110us/sample - loss: 3.3359 Epoch 261/500 10/10 [==============================] - 0s 142us/sample - loss: 3.2513 Epoch 262/500 10/10 [==============================] - 0s 169us/sample - loss: 3.1688 Epoch 263/500 10/10 [==============================] - 0s 125us/sample - loss: 3.0884 Epoch 264/500 10/10 [==============================] - 0s 125us/sample - loss: 3.0100 Epoch 265/500 10/10 [==============================] - 0s 133us/sample - loss: 2.9336 Epoch 266/500 10/10 [==============================] - 0s 124us/sample - loss: 2.8591 Epoch 267/500 10/10 [==============================] - 0s 122us/sample - loss: 2.7866 Epoch 268/500 10/10 [==============================] - 0s 122us/sample - loss: 2.7159 Epoch 269/500 10/10 [==============================] - 0s 110us/sample - loss: 2.6469 Epoch 270/500 10/10 [==============================] - 0s 121us/sample - loss: 2.5798 Epoch 271/500 10/10 [==============================] - 0s 136us/sample - loss: 2.5143 Epoch 272/500 10/10 [==============================] - 0s 111us/sample - loss: 2.4505 Epoch 273/500 10/10 [==============================] - 0s 117us/sample - loss: 2.3883 Epoch 274/500 10/10 [==============================] - 0s 118us/sample - loss: 2.3277 Epoch 275/500 10/10 [==============================] - 0s 104us/sample - loss: 2.2686 Epoch 276/500 10/10 [==============================] - 0s 117us/sample - loss: 2.2110 Epoch 277/500 10/10 [==============================] - 0s 129us/sample - loss: 2.1549 Epoch 278/500 10/10 [==============================] - 0s 163us/sample - loss: 2.1002 Epoch 279/500 10/10 [==============================] - 0s 132us/sample - loss: 2.0469 Epoch 280/500 10/10 [==============================] - 0s 120us/sample - loss: 1.9950 Epoch 281/500 10/10 [==============================] - 0s 126us/sample - loss: 1.9444 Epoch 282/500 10/10 [==============================] - 0s 121us/sample - loss: 1.8950 Epoch 283/500 10/10 [==============================] - 0s 127us/sample - loss: 1.8469 Epoch 284/500 10/10 [==============================] - 0s 143us/sample - loss: 1.8001 Epoch 285/500 10/10 [==============================] - 0s 134us/sample - loss: 1.7544 Epoch 286/500 10/10 [==============================] - 0s 116us/sample - loss: 1.7099 Epoch 287/500 10/10 [==============================] - 0s 112us/sample - loss: 1.6665 Epoch 288/500 10/10 [==============================] - 0s 118us/sample - loss: 1.6242 Epoch 289/500 10/10 [==============================] - 0s 104us/sample - loss: 1.5830 Epoch 290/500 10/10 [==============================] - 0s 126us/sample - loss: 1.5428 Epoch 291/500 10/10 [==============================] - 0s 120us/sample - loss: 1.5036 Epoch 292/500 10/10 [==============================] - 0s 120us/sample - loss: 1.4655 Epoch 293/500 10/10 [==============================] - 0s 120us/sample - loss: 1.4283 Epoch 294/500 10/10 [==============================] - 0s 114us/sample - loss: 1.3920 Epoch 295/500 10/10 [==============================] - 0s 102us/sample - loss: 1.3567 Epoch 296/500 10/10 [==============================] - 0s 120us/sample - loss: 1.3223 Epoch 297/500 10/10 [==============================] - 0s 107us/sample - loss: 1.2887 Epoch 298/500 10/10 [==============================] - 0s 113us/sample - loss: 1.2560 Epoch 299/500 10/10 [==============================] - 0s 198us/sample - loss: 1.2242 Epoch 300/500 10/10 [==============================] - 0s 163us/sample - loss: 1.1931 Epoch 301/500 10/10 [==============================] - 0s 146us/sample - loss: 1.1628 Epoch 302/500 10/10 [==============================] - 0s 149us/sample - loss: 1.1333 Epoch 303/500 10/10 [==============================] - 0s 153us/sample - loss: 1.1045 Epoch 304/500 10/10 [==============================] - 0s 113us/sample - loss: 1.0765 Epoch 305/500 10/10 [==============================] - 0s 190us/sample - loss: 1.0492 Epoch 306/500 10/10 [==============================] - 0s 171us/sample - loss: 1.0226 Epoch 307/500 10/10 [==============================] - 0s 135us/sample - loss: 0.9966 Epoch 308/500 10/10 [==============================] - 0s 107us/sample - loss: 0.9713 Epoch 309/500 10/10 [==============================] - 0s 103us/sample - loss: 0.9467 Epoch 310/500 10/10 [==============================] - 0s 118us/sample - loss: 0.9227 Epoch 311/500 10/10 [==============================] - 0s 105us/sample - loss: 0.8992 Epoch 312/500 10/10 [==============================] - 0s 125us/sample - loss: 0.8764 Epoch 313/500 10/10 [==============================] - 0s 122us/sample - loss: 0.8542 Epoch 314/500 10/10 [==============================] - 0s 132us/sample - loss: 0.8325 Epoch 315/500 10/10 [==============================] - 0s 100us/sample - loss: 0.8114 Epoch 316/500 10/10 [==============================] - 0s 118us/sample - loss: 0.7908 Epoch 317/500 10/10 [==============================] - 0s 136us/sample - loss: 0.7707 Epoch 318/500 10/10 [==============================] - 0s 147us/sample - loss: 0.7511 Epoch 319/500 10/10 [==============================] - 0s 155us/sample - loss: 0.7321 Epoch 320/500 10/10 [==============================] - 0s 126us/sample - loss: 0.7135 Epoch 321/500 10/10 [==============================] - 0s 127us/sample - loss: 0.6954 Epoch 322/500 10/10 [==============================] - 0s 126us/sample - loss: 0.6778 Epoch 323/500 10/10 [==============================] - 0s 131us/sample - loss: 0.6606 Epoch 324/500 10/10 [==============================] - 0s 129us/sample - loss: 0.6438 Epoch 325/500 10/10 [==============================] - 0s 127us/sample - loss: 0.6275 Epoch 326/500 10/10 [==============================] - 0s 136us/sample - loss: 0.6115 Epoch 327/500 10/10 [==============================] - 0s 125us/sample - loss: 0.5960 Epoch 328/500 10/10 [==============================] - 0s 118us/sample - loss: 0.5809 Epoch 329/500 10/10 [==============================] - 0s 134us/sample - loss: 0.5661 Epoch 330/500 10/10 [==============================] - 0s 160us/sample - loss: 0.5518 Epoch 331/500 10/10 [==============================] - 0s 124us/sample - loss: 0.5378 Epoch 332/500 10/10 [==============================] - 0s 156us/sample - loss: 0.5241 Epoch 333/500 10/10 [==============================] - 0s 121us/sample - loss: 0.5108 Epoch 334/500 10/10 [==============================] - 0s 118us/sample - loss: 0.4979 Epoch 335/500 10/10 [==============================] - 0s 126us/sample - loss: 0.4852 Epoch 336/500 10/10 [==============================] - 0s 139us/sample - loss: 0.4729 Epoch 337/500 10/10 [==============================] - 0s 143us/sample - loss: 0.4609 Epoch 338/500 10/10 [==============================] - 0s 122us/sample - loss: 0.4492 Epoch 339/500 10/10 [==============================] - 0s 123us/sample - loss: 0.4378 Epoch 340/500 10/10 [==============================] - 0s 157us/sample - loss: 0.4267 Epoch 341/500 10/10 [==============================] - 0s 124us/sample - loss: 0.4159 Epoch 342/500 10/10 [==============================] - 0s 128us/sample - loss: 0.4053 Epoch 343/500 10/10 [==============================] - 0s 129us/sample - loss: 0.3950 Epoch 344/500 10/10 [==============================] - 0s 215us/sample - loss: 0.3850 Epoch 345/500 10/10 [==============================] - 0s 135us/sample - loss: 0.3752 Epoch 346/500 10/10 [==============================] - 0s 123us/sample - loss: 0.3657 Epoch 347/500 10/10 [==============================] - 0s 133us/sample - loss: 0.3564 Epoch 348/500 10/10 [==============================] - 0s 109us/sample - loss: 0.3474 Epoch 349/500 10/10 [==============================] - 0s 152us/sample - loss: 0.3386 Epoch 350/500 10/10 [==============================] - 0s 122us/sample - loss: 0.3300 Epoch 351/500 10/10 [==============================] - 0s 125us/sample - loss: 0.3216 Epoch 352/500 10/10 [==============================] - 0s 136us/sample - loss: 0.3134 Epoch 353/500 10/10 [==============================] - 0s 119us/sample - loss: 0.3055 Epoch 354/500 10/10 [==============================] - 0s 121us/sample - loss: 0.2977 Epoch 355/500 10/10 [==============================] - 0s 144us/sample - loss: 0.2902 Epoch 356/500 10/10 [==============================] - 0s 128us/sample - loss: 0.2828 Epoch 357/500 10/10 [==============================] - 0s 137us/sample - loss: 0.2756 Epoch 358/500 10/10 [==============================] - 0s 129us/sample - loss: 0.2686 Epoch 359/500 10/10 [==============================] - 0s 137us/sample - loss: 0.2618 Epoch 360/500 10/10 [==============================] - 0s 137us/sample - loss: 0.2552 Epoch 361/500 10/10 [==============================] - 0s 139us/sample - loss: 0.2487 Epoch 362/500 10/10 [==============================] - 0s 132us/sample - loss: 0.2424 Epoch 363/500 10/10 [==============================] - 0s 121us/sample - loss: 0.2362 Epoch 364/500 10/10 [==============================] - 0s 134us/sample - loss: 0.2303 Epoch 365/500 10/10 [==============================] - 0s 132us/sample - loss: 0.2244 Epoch 366/500 10/10 [==============================] - 0s 128us/sample - loss: 0.2187 Epoch 367/500 10/10 [==============================] - 0s 126us/sample - loss: 0.2132 Epoch 368/500 10/10 [==============================] - 0s 117us/sample - loss: 0.2078 Epoch 369/500 10/10 [==============================] - 0s 118us/sample - loss: 0.2025 Epoch 370/500 10/10 [==============================] - 0s 136us/sample - loss: 0.1973 Epoch 371/500 10/10 [==============================] - 0s 156us/sample - loss: 0.1923 Epoch 372/500 10/10 [==============================] - 0s 141us/sample - loss: 0.1875 Epoch 373/500 10/10 [==============================] - 0s 117us/sample - loss: 0.1827 Epoch 374/500 10/10 [==============================] - 0s 151us/sample - loss: 0.1781 Epoch 375/500 10/10 [==============================] - 0s 113us/sample - loss: 0.1735 Epoch 376/500 10/10 [==============================] - 0s 120us/sample - loss: 0.1691 Epoch 377/500 10/10 [==============================] - 0s 123us/sample - loss: 0.1648 Epoch 378/500 10/10 [==============================] - 0s 127us/sample - loss: 0.1607 Epoch 379/500 10/10 [==============================] - 0s 130us/sample - loss: 0.1566 Epoch 380/500 10/10 [==============================] - 0s 135us/sample - loss: 0.1526 Epoch 381/500 10/10 [==============================] - 0s 141us/sample - loss: 0.1487 Epoch 382/500 10/10 [==============================] - 0s 174us/sample - loss: 0.1450 Epoch 383/500 10/10 [==============================] - 0s 139us/sample - loss: 0.1413 Epoch 384/500 10/10 [==============================] - 0s 144us/sample - loss: 0.1377 Epoch 385/500 10/10 [==============================] - 0s 118us/sample - loss: 0.1342 Epoch 386/500 10/10 [==============================] - 0s 135us/sample - loss: 0.1308 Epoch 387/500 10/10 [==============================] - 0s 110us/sample - loss: 0.1275 Epoch 388/500 10/10 [==============================] - 0s 139us/sample - loss: 0.1242 Epoch 389/500 10/10 [==============================] - 0s 158us/sample - loss: 0.1211 Epoch 390/500 10/10 [==============================] - 0s 116us/sample - loss: 0.1180 Epoch 391/500 10/10 [==============================] - 0s 117us/sample - loss: 0.1150 Epoch 392/500 10/10 [==============================] - 0s 139us/sample - loss: 0.1121 Epoch 393/500 10/10 [==============================] - 0s 118us/sample - loss: 0.1093 Epoch 394/500 10/10 [==============================] - 0s 115us/sample - loss: 0.1065 Epoch 395/500 10/10 [==============================] - 0s 105us/sample - loss: 0.1038 Epoch 396/500 10/10 [==============================] - 0s 112us/sample - loss: 0.1012 Epoch 397/500 10/10 [==============================] - 0s 111us/sample - loss: 0.0986 Epoch 398/500 10/10 [==============================] - 0s 107us/sample - loss: 0.0961 Epoch 399/500 10/10 [==============================] - 0s 110us/sample - loss: 0.0936 Epoch 400/500 10/10 [==============================] - 0s 121us/sample - loss: 0.0913 Epoch 401/500 10/10 [==============================] - 0s 100us/sample - loss: 0.0890 Epoch 402/500 10/10 [==============================] - 0s 103us/sample - loss: 0.0867 Epoch 403/500 10/10 [==============================] - 0s 107us/sample - loss: 0.0845 Epoch 404/500 10/10 [==============================] - 0s 110us/sample - loss: 0.0824 Epoch 405/500 10/10 [==============================] - 0s 114us/sample - loss: 0.0803 Epoch 406/500 10/10 [==============================] - 0s 141us/sample - loss: 0.0782 Epoch 407/500 10/10 [==============================] - 0s 129us/sample - loss: 0.0762 Epoch 408/500 10/10 [==============================] - 0s 133us/sample - loss: 0.0743 Epoch 409/500 10/10 [==============================] - 0s 142us/sample - loss: 0.0724 Epoch 410/500 10/10 [==============================] - 0s 107us/sample - loss: 0.0706 Epoch 411/500 10/10 [==============================] - 0s 181us/sample - loss: 0.0688 Epoch 412/500 10/10 [==============================] - 0s 162us/sample - loss: 0.0670 Epoch 413/500 10/10 [==============================] - 0s 102us/sample - loss: 0.0653 Epoch 414/500 10/10 [==============================] - 0s 189us/sample - loss: 0.0637 Epoch 415/500 10/10 [==============================] - 0s 117us/sample - loss: 0.0621 Epoch 416/500 10/10 [==============================] - 0s 109us/sample - loss: 0.0605 Epoch 417/500 10/10 [==============================] - 0s 116us/sample - loss: 0.0590 Epoch 418/500 10/10 [==============================] - 0s 110us/sample - loss: 0.0575 Epoch 419/500 10/10 [==============================] - 0s 104us/sample - loss: 0.0560 Epoch 420/500 10/10 [==============================] - 0s 128us/sample - loss: 0.0546 Epoch 421/500 10/10 [==============================] - 0s 119us/sample - loss: 0.0532 Epoch 422/500 10/10 [==============================] - 0s 114us/sample - loss: 0.0518 Epoch 423/500 10/10 [==============================] - 0s 199us/sample - loss: 0.0505 Epoch 424/500 10/10 [==============================] - 0s 124us/sample - loss: 0.0493 Epoch 425/500 10/10 [==============================] - 0s 200us/sample - loss: 0.0480 Epoch 426/500 10/10 [==============================] - 0s 192us/sample - loss: 0.0468 Epoch 427/500 10/10 [==============================] - 0s 136us/sample - loss: 0.0456 Epoch 428/500 10/10 [==============================] - 0s 134us/sample - loss: 0.0444 Epoch 429/500 10/10 [==============================] - 0s 132us/sample - loss: 0.0433 Epoch 430/500 10/10 [==============================] - 0s 111us/sample - loss: 0.0422 Epoch 431/500 10/10 [==============================] - 0s 119us/sample - loss: 0.0411 Epoch 432/500 10/10 [==============================] - 0s 119us/sample - loss: 0.0401 Epoch 433/500 10/10 [==============================] - 0s 120us/sample - loss: 0.0391 Epoch 434/500 10/10 [==============================] - 0s 112us/sample - loss: 0.0381 Epoch 435/500 10/10 [==============================] - 0s 146us/sample - loss: 0.0371 Epoch 436/500 10/10 [==============================] - 0s 148us/sample - loss: 0.0362 Epoch 437/500 10/10 [==============================] - 0s 145us/sample - loss: 0.0353 Epoch 438/500 10/10 [==============================] - 0s 146us/sample - loss: 0.0344 Epoch 439/500 10/10 [==============================] - 0s 167us/sample - loss: 0.0335 Epoch 440/500 10/10 [==============================] - 0s 138us/sample - loss: 0.0326 Epoch 441/500 10/10 [==============================] - 0s 135us/sample - loss: 0.0318 Epoch 442/500 10/10 [==============================] - 0s 149us/sample - loss: 0.0310 Epoch 443/500 10/10 [==============================] - 0s 135us/sample - loss: 0.0302 Epoch 444/500 10/10 [==============================] - 0s 150us/sample - loss: 0.0295 Epoch 445/500 10/10 [==============================] - 0s 163us/sample - loss: 0.0287 Epoch 446/500 10/10 [==============================] - 0s 135us/sample - loss: 0.0280 Epoch 447/500 10/10 [==============================] - 0s 152us/sample - loss: 0.0273 Epoch 448/500 10/10 [==============================] - 0s 142us/sample - loss: 0.0266 Epoch 449/500 10/10 [==============================] - 0s 158us/sample - loss: 0.0259 Epoch 450/500 10/10 [==============================] - 0s 160us/sample - loss: 0.0252 Epoch 451/500 10/10 [==============================] - 0s 173us/sample - loss: 0.0246 Epoch 452/500 10/10 [==============================] - 0s 161us/sample - loss: 0.0240 Epoch 453/500 10/10 [==============================] - 0s 171us/sample - loss: 0.0234 Epoch 454/500 10/10 [==============================] - 0s 190us/sample - loss: 0.0228 Epoch 455/500 10/10 [==============================] - 0s 192us/sample - loss: 0.0222 Epoch 456/500 10/10 [==============================] - 0s 194us/sample - loss: 0.0216 Epoch 457/500 10/10 [==============================] - 0s 197us/sample - loss: 0.0211 Epoch 458/500 10/10 [==============================] - 0s 181us/sample - loss: 0.0206 Epoch 459/500 10/10 [==============================] - 0s 185us/sample - loss: 0.0200 Epoch 460/500 10/10 [==============================] - 0s 193us/sample - loss: 0.0195 Epoch 461/500 10/10 [==============================] - 0s 209us/sample - loss: 0.0190 Epoch 462/500 10/10 [==============================] - 0s 209us/sample - loss: 0.0185 Epoch 463/500 10/10 [==============================] - 0s 212us/sample - loss: 0.0181 Epoch 464/500 10/10 [==============================] - 0s 201us/sample - loss: 0.0176 Epoch 465/500 10/10 [==============================] - 0s 154us/sample - loss: 0.0172 Epoch 466/500 10/10 [==============================] - 0s 157us/sample - loss: 0.0167 Epoch 467/500 10/10 [==============================] - 0s 153us/sample - loss: 0.0163 Epoch 468/500 10/10 [==============================] - 0s 112us/sample - loss: 0.0159 Epoch 469/500 10/10 [==============================] - 0s 153us/sample - loss: 0.0155 Epoch 470/500 10/10 [==============================] - 0s 156us/sample - loss: 0.0151 Epoch 471/500 10/10 [==============================] - 0s 144us/sample - loss: 0.0147 Epoch 472/500 10/10 [==============================] - 0s 156us/sample - loss: 0.0143 Epoch 473/500 10/10 [==============================] - 0s 142us/sample - loss: 0.0140 Epoch 474/500 10/10 [==============================] - 0s 144us/sample - loss: 0.0136 Epoch 475/500 10/10 [==============================] - 0s 153us/sample - loss: 0.0133 Epoch 476/500 10/10 [==============================] - 0s 145us/sample - loss: 0.0129 Epoch 477/500 10/10 [==============================] - 0s 157us/sample - loss: 0.0126 Epoch 478/500 10/10 [==============================] - 0s 130us/sample - loss: 0.0123 Epoch 479/500 10/10 [==============================] - 0s 111us/sample - loss: 0.0120 Epoch 480/500 10/10 [==============================] - 0s 111us/sample - loss: 0.0117 Epoch 481/500 10/10 [==============================] - 0s 111us/sample - loss: 0.0114 Epoch 482/500 10/10 [==============================] - 0s 121us/sample - loss: 0.0111 Epoch 483/500 10/10 [==============================] - 0s 121us/sample - loss: 0.0108 Epoch 484/500 10/10 [==============================] - 0s 106us/sample - loss: 0.0105 Epoch 485/500 10/10 [==============================] - 0s 106us/sample - loss: 0.0103 Epoch 486/500 10/10 [==============================] - 0s 107us/sample - loss: 0.0100 Epoch 487/500 10/10 [==============================] - 0s 105us/sample - loss: 0.0098 Epoch 488/500 10/10 [==============================] - 0s 107us/sample - loss: 0.0095 Epoch 489/500 10/10 [==============================] - 0s 109us/sample - loss: 0.0093 Epoch 490/500 10/10 [==============================] - 0s 120us/sample - loss: 0.0090 Epoch 491/500 10/10 [==============================] - 0s 113us/sample - loss: 0.0088 Epoch 492/500 10/10 [==============================] - 0s 116us/sample - loss: 0.0086 Epoch 493/500 10/10 [==============================] - 0s 113us/sample - loss: 0.0084 Epoch 494/500 10/10 [==============================] - 0s 146us/sample - loss: 0.0081 Epoch 495/500 10/10 [==============================] - 0s 110us/sample - loss: 0.0079 Epoch 496/500 10/10 [==============================] - 0s 125us/sample - loss: 0.0077 Epoch 497/500 10/10 [==============================] - 0s 110us/sample - loss: 0.0075 Epoch 498/500 10/10 [==============================] - 0s 116us/sample - loss: 0.0074 Epoch 499/500 10/10 [==============================] - 0s 122us/sample - loss: 0.0072 Epoch 500/500 10/10 [==============================] - 0s 108us/sample - loss: 0.0070
Make a Prediction
What would y be if x=100?
input_value = [100.0]
predicted = model.predict(input_value)
print(predicted)
[[766.0348]]
The actual value is
actual = (100 * slope) + intercept
print(f"y = {actual}")
print(f"difference = {actual - predicted[0][0]}")
y = 765 difference = -1.0347900390625
So it was pretty close, but not exact.
Comparing to a Linear Regression Model
regression = linear_model.LinearRegression()
regression.fit(X.reshape(-1, 1), Y)
prediction = regression.predict([input_value])
print(prediction)
[765.]
The linear model got it exactly right, as you might expect, but this isn't really a problem that exploits the positive features of machine learning.