Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # I got this off the youtube channel https://www.youtube.com/watch?v=swJFNjIjxIE. By Patrick Cooper
- # but it seems that one Cannot register 2 metrics with the same name: /tensorflow/api/keras/optimizers...
- # I must have not set up my workspace correctly, I am assuming. By Snowcatman
- from tensorflow import keras
- from tensorflow.keras import layers
- import matplotlib.pyplot as plt
- import numpy as np
- x = np.random.uniform(-10, 10, 100000)
- y = (x**2+5)*2
- print(x)
- def build_model():
- model = keras.Sequential([
- layers.Dense(64, input_dim=1, activation='relu'),
- layers.Dense(128, activation='relu'),
- layers.Dense(128, activation='relu'),
- layers.Dense(64, activation='relu'),
- layers.Dense(1)
- ])
- # mean square error will work for our purposes here
- model.compile(loss='mean_squared_error', optimizer='adam')
- return model
- model = build_model()
- model.summary()
- model.fit(x, y, epochs=12, batch_size=500)
- eval_set_in = x[:3]
- eval_set_out = y[:3]
- print("Generate predictions for 3 samples using neural network model:")
- print("First 3 Evaluation Inputs:", eval_set_in)
- dl_predictions = model.predict(eval_set_in).flatten()
- print("First 3 Actual Outputs:", eval_set_out)
- print("First 3 Evaluation Outputs:", dl_predictions)
- print("Error:", abs(eval_set_out - dl_predictions))
- poly_line = np.linspace(-10, 10, num=20)
- plt.title('Input (x) versus Output (y)')
- plt.xlabel('Input Variable (x)')
- plt.ylabel('Output Variable (y)')
- plt.plot(poly_line, model.predict(poly_line), linewidth=8, color="blue",
- label="Approximated")
- plt.plot(poly_line, (poly_line**2+5)*2, linewidth=4, color="lightblue",
- label="Actual")
- plt.legend()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement