1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import numpy as np import tensorflow as tf from tensorflow import keras
x = np.random.rand(100, 1) y = 5 * x + 5 * np.random.rand(1)
model = keras.Sequential([ keras.layers.Dense(32, activation=tf.nn.relu, input_shape=(1,)), keras.layers.Dense(32, activation=tf.nn.relu), keras.layers.Dense(1) ])
model.compile(optimizer='adam', loss='mse', metrics=['mse', 'binary_crossentropy'])
history = model.fit(x, y, epochs=500, batch_size=100)
import matplotlib.pyplot as plt plt.scatter(x, y, label='y_true') plt.scatter(x, model.predict(x), label='y_pred') plt.legend() plt.show()
|