This post presents super minimal tf.keras example
Imports
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
Train data
x = np.arange(-1, 1, 0.001)
x = np.array(x, ndmin=2).T
y = np.sin(x * 8)
print('x shape:', x.shape, '\ty shape:', y.shape)
print(x)
print(y)
plt.plot(x, y, label='data')
plt.legend()
Create model
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(units=256, activation='relu', input_dim=1))
model.add(tf.keras.layers.Dense(units=256, activation='relu'))
model.add(tf.keras.layers.Dense(units=1, activation='linear'))
model.compile(loss='mse', optimizer='sgd')
Plot data and untrained model
plt.plot(x, y, label='data')
plt.plot(x, model.predict(x), label='model')
plt.legend()
Train model
history = model.fit(x, y, epochs=200, batch_size=50, verbose=0)
Plot history
plt.plot(history.history['loss'])
plt.xlabel('Epoch')
plt.ylabel('Loss')
Plot data and trained model
plt.plot(x, y, label='data')
plt.plot(x, model.predict(x), label='model')
plt.legend()