In [1]:
# import and check version
import tensorflow as tf
# tf can be really verbose
tf.logging.set_verbosity(tf.logging.ERROR)
print(tf.__version__)
In [2]:
# a small sanity check, does tf seem to work ok?
sess = tf.Session()
hello = tf.constant('Hello TF!')
print(sess.run(hello))
sess.close()
In [3]:
import tensorflow.keras as keras
print(keras.__version__)
In [0]:
input = [[-1], [0], [1], [2], [3], [4]]
output = [[2], [1], [0], [-1], [-2], [-3]]
In [5]:
import numpy as np
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import SGD
model = Sequential([
Dense(units=1, input_dim=1)
])
sgd = SGD(lr=0.01)
model.compile(optimizer=sgd, loss='mse')
BATCH_SIZE=1
EPOCHS = 500
x = np.array(input)[:, 0]
y = np.array(output)[:, 0]
%time history = model.fit(x=x, y=y, epochs=EPOCHS, batch_size=BATCH_SIZE, verbose=0)
In [6]:
import matplotlib.pyplot as plt
plt.yscale('log')
plt.plot(history.history['loss'])
Out[6]:
In [7]:
model.summary()
In [8]:
model.get_weights()
Out[8]:
In [9]:
output_pred = model.predict(x)
print(output_pred)
plt.plot(input, output_pred)
plt.plot(input, output, 'ro')
Out[9]:
In [0]: