import numpy as np
import tensorflow as tf
# os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
# import keras
# Generate some example data (x values and corresponding y values)
x_train = np.array ( [ 1 , 2 , 3 , 4 , 5 ] , dtype= float )
y_train = np.array ( [ 3 , 5 , 7 , 9 , 11 ] , dtype= float ) # y = 2x + 1
# Define a simple model
model = tf.keras .Sequential ( [
tf.keras .layers .Dense ( units= 1 , input_shape= [ 1 ] )
] )
# Compile the model
model.compile ( optimizer= 'sgd' , loss= 'mean_squared_error' )
# Train the model
model.fit ( x_train, y_train, epochs= 100 )
# Predict
print ( model.predict ( [ 10 ] ) ) # Should output something close to 21 (since y = 2*10 + 1)
aW1wb3J0IG51bXB5IGFzIG5wCmltcG9ydCB0ZW5zb3JmbG93IGFzIHRmCiMgb3MuZW52aXJvblsnVEZfRU5BQkxFX09ORUROTl9PUFRTJ10gPSAnMCcKIyBpbXBvcnQga2VyYXMKCiMgR2VuZXJhdGUgc29tZSBleGFtcGxlIGRhdGEgKHggdmFsdWVzIGFuZCBjb3JyZXNwb25kaW5nIHkgdmFsdWVzKQp4X3RyYWluID0gbnAuYXJyYXkoWzEsIDIsIDMsIDQsIDVdLCBkdHlwZT1mbG9hdCkKeV90cmFpbiA9IG5wLmFycmF5KFszLCA1LCA3LCA5LCAxMV0sIGR0eXBlPWZsb2F0KSAgIyB5ID0gMnggKyAxCgojIERlZmluZSBhIHNpbXBsZSBtb2RlbAptb2RlbCA9IHRmLmtlcmFzLlNlcXVlbnRpYWwoWwogICAgdGYua2VyYXMubGF5ZXJzLkRlbnNlKHVuaXRzPTEsIGlucHV0X3NoYXBlPVsxXSkKXSkKCiMgQ29tcGlsZSB0aGUgbW9kZWwKbW9kZWwuY29tcGlsZShvcHRpbWl6ZXI9J3NnZCcsIGxvc3M9J21lYW5fc3F1YXJlZF9lcnJvcicpCgojIFRyYWluIHRoZSBtb2RlbAptb2RlbC5maXQoeF90cmFpbiwgeV90cmFpbiwgZXBvY2hzPTEwMCkKCiMgUHJlZGljdApwcmludChtb2RlbC5wcmVkaWN0KFsxMF0pKSAgIyBTaG91bGQgb3V0cHV0IHNvbWV0aGluZyBjbG9zZSB0byAyMSAoc2luY2UgeSA9IDIqMTAgKyAxKQ==
stdout
Epoch 1/100
5/5 [==============================] - 0s 27ms/sample - loss: 3.9200
Epoch 2/100
5/5 [==============================] - 0s 169us/sample - loss: 2.3382
Epoch 3/100
5/5 [==============================] - 0s 142us/sample - loss: 1.4161
Epoch 4/100
5/5 [==============================] - 0s 125us/sample - loss: 0.8784
Epoch 5/100
5/5 [==============================] - 0s 121us/sample - loss: 0.5646
Epoch 6/100
5/5 [==============================] - 0s 120us/sample - loss: 0.3814
Epoch 7/100
5/5 [==============================] - 0s 116us/sample - loss: 0.2743
Epoch 8/100
5/5 [==============================] - 0s 123us/sample - loss: 0.2116
Epoch 9/100
5/5 [==============================] - 0s 117us/sample - loss: 0.1747
Epoch 10/100
5/5 [==============================] - 0s 116us/sample - loss: 0.1528
Epoch 11/100
5/5 [==============================] - 0s 117us/sample - loss: 0.1397
Epoch 12/100
5/5 [==============================] - 0s 116us/sample - loss: 0.1317
Epoch 13/100
5/5 [==============================] - 0s 114us/sample - loss: 0.1268
Epoch 14/100
5/5 [==============================] - 0s 162us/sample - loss: 0.1235
Epoch 15/100
5/5 [==============================] - 0s 143us/sample - loss: 0.1213
Epoch 16/100
5/5 [==============================] - 0s 120us/sample - loss: 0.1197
Epoch 17/100
5/5 [==============================] - 0s 167us/sample - loss: 0.1184
Epoch 18/100
5/5 [==============================] - 0s 179us/sample - loss: 0.1173
Epoch 19/100
5/5 [==============================] - 0s 175us/sample - loss: 0.1164
Epoch 20/100
5/5 [==============================] - 0s 177us/sample - loss: 0.1155
Epoch 21/100
5/5 [==============================] - 0s 176us/sample - loss: 0.1146
Epoch 22/100
5/5 [==============================] - 0s 176us/sample - loss: 0.1138
Epoch 23/100
5/5 [==============================] - 0s 174us/sample - loss: 0.1130
Epoch 24/100
5/5 [==============================] - 0s 177us/sample - loss: 0.1123
Epoch 25/100
5/5 [==============================] - 0s 174us/sample - loss: 0.1115
Epoch 26/100
5/5 [==============================] - 0s 183us/sample - loss: 0.1108
Epoch 27/100
5/5 [==============================] - 0s 183us/sample - loss: 0.1100
Epoch 28/100
5/5 [==============================] - 0s 168us/sample - loss: 0.1093
Epoch 29/100
5/5 [==============================] - 0s 168us/sample - loss: 0.1085
Epoch 30/100
5/5 [==============================] - 0s 179us/sample - loss: 0.1078
Epoch 31/100
5/5 [==============================] - 0s 171us/sample - loss: 0.1071
Epoch 32/100
5/5 [==============================] - 0s 172us/sample - loss: 0.1063
Epoch 33/100
5/5 [==============================] - 0s 179us/sample - loss: 0.1056
Epoch 34/100
5/5 [==============================] - 0s 180us/sample - loss: 0.1049
Epoch 35/100
5/5 [==============================] - 0s 184us/sample - loss: 0.1042
Epoch 36/100
5/5 [==============================] - 0s 176us/sample - loss: 0.1035
Epoch 37/100
5/5 [==============================] - 0s 175us/sample - loss: 0.1028
Epoch 38/100
5/5 [==============================] - 0s 181us/sample - loss: 0.1021
Epoch 39/100
5/5 [==============================] - 0s 176us/sample - loss: 0.1014
Epoch 40/100
5/5 [==============================] - 0s 175us/sample - loss: 0.1007
Epoch 41/100
5/5 [==============================] - 0s 175us/sample - loss: 0.1000
Epoch 42/100
5/5 [==============================] - 0s 180us/sample - loss: 0.0994
Epoch 43/100
5/5 [==============================] - 0s 173us/sample - loss: 0.0987
Epoch 44/100
5/5 [==============================] - 0s 176us/sample - loss: 0.0980
Epoch 45/100
5/5 [==============================] - 0s 179us/sample - loss: 0.0974
Epoch 46/100
5/5 [==============================] - 0s 176us/sample - loss: 0.0967
Epoch 47/100
5/5 [==============================] - 0s 174us/sample - loss: 0.0961
Epoch 48/100
5/5 [==============================] - 0s 177us/sample - loss: 0.0954
Epoch 49/100
5/5 [==============================] - 0s 179us/sample - loss: 0.0948
Epoch 50/100
5/5 [==============================] - 0s 182us/sample - loss: 0.0941
Epoch 51/100
5/5 [==============================] - 0s 180us/sample - loss: 0.0935
Epoch 52/100
5/5 [==============================] - 0s 188us/sample - loss: 0.0929
Epoch 53/100
5/5 [==============================] - 0s 187us/sample - loss: 0.0922
Epoch 54/100
5/5 [==============================] - 0s 176us/sample - loss: 0.0916
Epoch 55/100
5/5 [==============================] - 0s 174us/sample - loss: 0.0910
Epoch 56/100
5/5 [==============================] - 0s 179us/sample - loss: 0.0904
Epoch 57/100
5/5 [==============================] - 0s 175us/sample - loss: 0.0898
Epoch 58/100
5/5 [==============================] - 0s 173us/sample - loss: 0.0892
Epoch 59/100
5/5 [==============================] - 0s 180us/sample - loss: 0.0886
Epoch 60/100
5/5 [==============================] - 0s 177us/sample - loss: 0.0880
Epoch 61/100
5/5 [==============================] - 0s 185us/sample - loss: 0.0874
Epoch 62/100
5/5 [==============================] - 0s 181us/sample - loss: 0.0868
Epoch 63/100
5/5 [==============================] - 0s 179us/sample - loss: 0.0862
Epoch 64/100
5/5 [==============================] - 0s 176us/sample - loss: 0.0856
Epoch 65/100
5/5 [==============================] - 0s 188us/sample - loss: 0.0850
Epoch 66/100
5/5 [==============================] - 0s 182us/sample - loss: 0.0845
Epoch 67/100
5/5 [==============================] - 0s 618us/sample - loss: 0.0839
Epoch 68/100
5/5 [==============================] - 0s 191us/sample - loss: 0.0833
Epoch 69/100
5/5 [==============================] - 0s 187us/sample - loss: 0.0828
Epoch 70/100
5/5 [==============================] - 0s 191us/sample - loss: 0.0822
Epoch 71/100
5/5 [==============================] - 0s 197us/sample - loss: 0.0816
Epoch 72/100
5/5 [==============================] - 0s 193us/sample - loss: 0.0811
Epoch 73/100
5/5 [==============================] - 0s 187us/sample - loss: 0.0806
Epoch 74/100
5/5 [==============================] - 0s 177us/sample - loss: 0.0800
Epoch 75/100
5/5 [==============================] - 0s 178us/sample - loss: 0.0795
Epoch 76/100
5/5 [==============================] - 0s 177us/sample - loss: 0.0789
Epoch 77/100
5/5 [==============================] - 0s 206us/sample - loss: 0.0784
Epoch 78/100
5/5 [==============================] - 0s 183us/sample - loss: 0.0779
Epoch 79/100
5/5 [==============================] - 0s 184us/sample - loss: 0.0773
Epoch 80/100
5/5 [==============================] - 0s 184us/sample - loss: 0.0768
Epoch 81/100
5/5 [==============================] - 0s 181us/sample - loss: 0.0763
Epoch 82/100
5/5 [==============================] - 0s 181us/sample - loss: 0.0758
Epoch 83/100
5/5 [==============================] - 0s 182us/sample - loss: 0.0753
Epoch 84/100
5/5 [==============================] - 0s 179us/sample - loss: 0.0748
Epoch 85/100
5/5 [==============================] - 0s 176us/sample - loss: 0.0743
Epoch 86/100
5/5 [==============================] - 0s 177us/sample - loss: 0.0738
Epoch 87/100
5/5 [==============================] - 0s 172us/sample - loss: 0.0733
Epoch 88/100
5/5 [==============================] - 0s 177us/sample - loss: 0.0728
Epoch 89/100
5/5 [==============================] - 0s 172us/sample - loss: 0.0723
Epoch 90/100
5/5 [==============================] - 0s 171us/sample - loss: 0.0718
Epoch 91/100
5/5 [==============================] - 0s 170us/sample - loss: 0.0713
Epoch 92/100
5/5 [==============================] - 0s 184us/sample - loss: 0.0708
Epoch 93/100
5/5 [==============================] - 0s 176us/sample - loss: 0.0703
Epoch 94/100
5/5 [==============================] - 0s 172us/sample - loss: 0.0699
Epoch 95/100
5/5 [==============================] - 0s 172us/sample - loss: 0.0694
Epoch 96/100
5/5 [==============================] - 0s 179us/sample - loss: 0.0689
Epoch 97/100
5/5 [==============================] - 0s 175us/sample - loss: 0.0685
Epoch 98/100
5/5 [==============================] - 0s 179us/sample - loss: 0.0680
Epoch 99/100
5/5 [==============================] - 0s 181us/sample - loss: 0.0675
Epoch 100/100
5/5 [==============================] - 0s 184us/sample - loss: 0.0671
[[22.07085]]
stderr
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/utils/losses_utils.py:170: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.