Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tensorflow as tf
- import numpy as np
- import matplotlib.pyplot as plt
- model = tf.keras.models.Sequential([
- tf.keras.layers.Dense(10, activation='relu'),
- tf.keras.layers.Dense(100, activation='relu'),
- tf.keras.layers.Dense(100, activation='relu'),
- tf.keras.layers.Dense(1, activation='relu')
- ])
- model.compile(optimizer='adam',
- loss='mse',
- metrics=['accuracy'])
- def train_function(x):
- return x
- def split_sequence(sequence, n_steps):
- X, y = list(), list()
- for i in range(len(sequence)):
- end_ix = i + n_steps
- if end_ix > len(sequence)-1:
- break
- seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
- X.append(seq_x)
- y.append(seq_y)
- return np.array(X), np.array(y)# define input sequence
- xaxis = np.arange(-50*np.pi, 50*np.pi, 0.1)
- train_seq = train_function(xaxis)
- n_steps = 20
- X, y = split_sequence(train_seq, n_steps)# reshape from [samples, timesteps] into [samples, timesteps, features]
- n_features = 1
- X = X.reshape((X.shape[0], X.shape[1], n_features))
- print("X.shape = {}".format(X.shape))
- print("y.shape = {}".format(y.shape))
- history = model.fit(X, y, epochs=20, verbose=1)
- plt.plot(history.history['loss'], label="loss")
- plt.legend(loc="upper right")
- plt.show()
- test_xaxis = np.arange(0, 10*np.pi, 0.1)
- def test_function(x):
- return train_function(x)
- calc_y = test_function(test_xaxis)# start with initial n values, rest will be predicted
- test_y = calc_y[:n_steps]
- results = []
- for i in range( len(test_xaxis) - n_steps ):
- net_input = test_y[i : i + n_steps]
- net_input = net_input.reshape((1, n_steps, n_features))
- y = model.predict(net_input, verbose=0)
- test_y = np.append(test_y, y)
- plt.plot(test_xaxis[n_steps:], test_y[n_steps:], label="predicitons")
- plt.plot(test_xaxis, calc_y, label="ground truth")
- plt.plot(test_xaxis[:n_steps], test_y[:n_steps], label="initial sequence", color="red")
- plt.legend(loc='upper left')
- plt.ylim(-2, 2)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment