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 18ms/sample - loss: 11.7218
Epoch 2/100
5/5 [==============================] - 0s 217us/sample - loss: 6.8713
Epoch 3/100
5/5 [==============================] - 0s 206us/sample - loss: 4.0444
Epoch 4/100
5/5 [==============================] - 0s 200us/sample - loss: 2.3967
Epoch 5/100
5/5 [==============================] - 0s 215us/sample - loss: 1.4363
Epoch 6/100
5/5 [==============================] - 0s 225us/sample - loss: 0.8763
Epoch 7/100
5/5 [==============================] - 0s 233us/sample - loss: 0.5497
Epoch 8/100
5/5 [==============================] - 0s 213us/sample - loss: 0.3592
Epoch 9/100
5/5 [==============================] - 0s 211us/sample - loss: 0.2478
Epoch 10/100
5/5 [==============================] - 0s 227us/sample - loss: 0.1827
Epoch 11/100
5/5 [==============================] - 0s 214us/sample - loss: 0.1445
Epoch 12/100
5/5 [==============================] - 0s 218us/sample - loss: 0.1220
Epoch 13/100
5/5 [==============================] - 0s 196us/sample - loss: 0.1086
Epoch 14/100
5/5 [==============================] - 0s 182us/sample - loss: 0.1005
Epoch 15/100
5/5 [==============================] - 0s 173us/sample - loss: 0.0956
Epoch 16/100
5/5 [==============================] - 0s 179us/sample - loss: 0.0925
Epoch 17/100
5/5 [==============================] - 0s 184us/sample - loss: 0.0904
Epoch 18/100
5/5 [==============================] - 0s 189us/sample - loss: 0.0889
Epoch 19/100
5/5 [==============================] - 0s 185us/sample - loss: 0.0878
Epoch 20/100
5/5 [==============================] - 0s 194us/sample - loss: 0.0870
Epoch 21/100
5/5 [==============================] - 0s 188us/sample - loss: 0.0862
Epoch 22/100
5/5 [==============================] - 0s 185us/sample - loss: 0.0855
Epoch 23/100
5/5 [==============================] - 0s 191us/sample - loss: 0.0849
Epoch 24/100
5/5 [==============================] - 0s 179us/sample - loss: 0.0843
Epoch 25/100
5/5 [==============================] - 0s 179us/sample - loss: 0.0837
Epoch 26/100
5/5 [==============================] - 0s 178us/sample - loss: 0.0831
Epoch 27/100
5/5 [==============================] - 0s 196us/sample - loss: 0.0826
Epoch 28/100
5/5 [==============================] - 0s 179us/sample - loss: 0.0820
Epoch 29/100
5/5 [==============================] - 0s 178us/sample - loss: 0.0814
Epoch 30/100
5/5 [==============================] - 0s 179us/sample - loss: 0.0809
Epoch 31/100
5/5 [==============================] - 0s 188us/sample - loss: 0.0803
Epoch 32/100
5/5 [==============================] - 0s 179us/sample - loss: 0.0798
Epoch 33/100
5/5 [==============================] - 0s 178us/sample - loss: 0.0793
Epoch 34/100
5/5 [==============================] - 0s 174us/sample - loss: 0.0787
Epoch 35/100
5/5 [==============================] - 0s 176us/sample - loss: 0.0782
Epoch 36/100
5/5 [==============================] - 0s 177us/sample - loss: 0.0777
Epoch 37/100
5/5 [==============================] - 0s 170us/sample - loss: 0.0771
Epoch 38/100
5/5 [==============================] - 0s 170us/sample - loss: 0.0766
Epoch 39/100
5/5 [==============================] - 0s 176us/sample - loss: 0.0761
Epoch 40/100
5/5 [==============================] - 0s 170us/sample - loss: 0.0756
Epoch 41/100
5/5 [==============================] - 0s 170us/sample - loss: 0.0751
Epoch 42/100
5/5 [==============================] - 0s 168us/sample - loss: 0.0746
Epoch 43/100
5/5 [==============================] - 0s 172us/sample - loss: 0.0741
Epoch 44/100
5/5 [==============================] - 0s 167us/sample - loss: 0.0736
Epoch 45/100
5/5 [==============================] - 0s 168us/sample - loss: 0.0731
Epoch 46/100
5/5 [==============================] - 0s 170us/sample - loss: 0.0726
Epoch 47/100
5/5 [==============================] - 0s 182us/sample - loss: 0.0721
Epoch 48/100
5/5 [==============================] - 0s 171us/sample - loss: 0.0716
Epoch 49/100
5/5 [==============================] - 0s 169us/sample - loss: 0.0711
Epoch 50/100
5/5 [==============================] - 0s 171us/sample - loss: 0.0706
Epoch 51/100
5/5 [==============================] - 0s 174us/sample - loss: 0.0702
Epoch 52/100
5/5 [==============================] - 0s 178us/sample - loss: 0.0697
Epoch 53/100
5/5 [==============================] - 0s 170us/sample - loss: 0.0692
Epoch 54/100
5/5 [==============================] - 0s 169us/sample - loss: 0.0688
Epoch 55/100
5/5 [==============================] - 0s 174us/sample - loss: 0.0683
Epoch 56/100
5/5 [==============================] - 0s 170us/sample - loss: 0.0678
Epoch 57/100
5/5 [==============================] - 0s 168us/sample - loss: 0.0674
Epoch 58/100
5/5 [==============================] - 0s 174us/sample - loss: 0.0669
Epoch 59/100
5/5 [==============================] - 0s 173us/sample - loss: 0.0665
Epoch 60/100
5/5 [==============================] - 0s 169us/sample - loss: 0.0660
Epoch 61/100
5/5 [==============================] - 0s 172us/sample - loss: 0.0656
Epoch 62/100
5/5 [==============================] - 0s 166us/sample - loss: 0.0651
Epoch 63/100
5/5 [==============================] - 0s 171us/sample - loss: 0.0647
Epoch 64/100
5/5 [==============================] - 0s 167us/sample - loss: 0.0642
Epoch 65/100
5/5 [==============================] - 0s 170us/sample - loss: 0.0638
Epoch 66/100
5/5 [==============================] - 0s 172us/sample - loss: 0.0634
Epoch 67/100
5/5 [==============================] - 0s 542us/sample - loss: 0.0630
Epoch 68/100
5/5 [==============================] - 0s 183us/sample - loss: 0.0625
Epoch 69/100
5/5 [==============================] - 0s 188us/sample - loss: 0.0621
Epoch 70/100
5/5 [==============================] - 0s 180us/sample - loss: 0.0617
Epoch 71/100
5/5 [==============================] - 0s 171us/sample - loss: 0.0613
Epoch 72/100
5/5 [==============================] - 0s 144us/sample - loss: 0.0609
Epoch 73/100
5/5 [==============================] - 0s 120us/sample - loss: 0.0604
Epoch 74/100
5/5 [==============================] - 0s 111us/sample - loss: 0.0600
Epoch 75/100
5/5 [==============================] - 0s 114us/sample - loss: 0.0596
Epoch 76/100
5/5 [==============================] - 0s 113us/sample - loss: 0.0592
Epoch 77/100
5/5 [==============================] - 0s 127us/sample - loss: 0.0588
Epoch 78/100
5/5 [==============================] - 0s 115us/sample - loss: 0.0584
Epoch 79/100
5/5 [==============================] - 0s 113us/sample - loss: 0.0580
Epoch 80/100
5/5 [==============================] - 0s 115us/sample - loss: 0.0576
Epoch 81/100
5/5 [==============================] - 0s 115us/sample - loss: 0.0573
Epoch 82/100
5/5 [==============================] - 0s 112us/sample - loss: 0.0569
Epoch 83/100
5/5 [==============================] - 0s 114us/sample - loss: 0.0565
Epoch 84/100
5/5 [==============================] - 0s 112us/sample - loss: 0.0561
Epoch 85/100
5/5 [==============================] - 0s 112us/sample - loss: 0.0557
Epoch 86/100
5/5 [==============================] - 0s 114us/sample - loss: 0.0554
Epoch 87/100
5/5 [==============================] - 0s 115us/sample - loss: 0.0550
Epoch 88/100
5/5 [==============================] - 0s 110us/sample - loss: 0.0546
Epoch 89/100
5/5 [==============================] - 0s 112us/sample - loss: 0.0542
Epoch 90/100
5/5 [==============================] - 0s 109us/sample - loss: 0.0539
Epoch 91/100
5/5 [==============================] - 0s 111us/sample - loss: 0.0535
Epoch 92/100
5/5 [==============================] - 0s 110us/sample - loss: 0.0531
Epoch 93/100
5/5 [==============================] - 0s 108us/sample - loss: 0.0528
Epoch 94/100
5/5 [==============================] - 0s 113us/sample - loss: 0.0524
Epoch 95/100
5/5 [==============================] - 0s 109us/sample - loss: 0.0521
Epoch 96/100
5/5 [==============================] - 0s 111us/sample - loss: 0.0517
Epoch 97/100
5/5 [==============================] - 0s 116us/sample - loss: 0.0514
Epoch 98/100
5/5 [==============================] - 0s 113us/sample - loss: 0.0510
Epoch 99/100
5/5 [==============================] - 0s 117us/sample - loss: 0.0507
Epoch 100/100
5/5 [==============================] - 0s 115us/sample - loss: 0.0503
[[21.927652]]
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.