import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
def f(x):
return 2 * x + 1.0
np.random.seed(4)
x_train = np.linspace(-1, 1, 100)
y_train = f(x_train) + np.random.randn(*x_train.shape) * 0.4
model = tf.keras.Sequential(
tf.keras.layers.Dense(1, input_shape=(1,))
)
model.compile(optimizer='adam', loss='mse')
model.fit(x_train, y_train, epochs=800)
x_test = np.linspace(-1, 1, 30)
y_test = f(x_test)
model.evaluate(x_test, y_test)
y_predict = model.predict(x_test)
plt.plot(x_test, y_test, color='red', linewidth=1)
plt.scatter(x_train, y_train)
plt.scatter(x_test, y_predict, edgecolors='yellow')
plt.show()
