import numpy as np
import matplotlib.pyplot as plt
#import pandas as pd
import math
import keras
from keras.layers import Input
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.layers.recurrent import SimpleRNN, LSTM
from tensorflow import set_random_seed
%run -i "C:\Users\justb\Dropbox\Code\Python\viztools.py"
#%run -i "C:\Users\rbeane\Dropbox\Code\Python\viztools.py"
def sine_curve(length=500, freq=1, amp=1, noise=0):
x = np.linspace(0,length-1,length)
return amp*np.sin(x*freq*2*math.pi/360) + np.random.normal(0, noise, length)
plt.plot(sine_curve(length=360, freq=1, amp=1))
plt.plot(sine_curve(length=360, freq=2, amp=1))
plt.plot(sine_curve(length=360, freq=2, amp=3, noise=0.2))
plt.plot(sine_curve(length=540, freq=0.5, amp=0.5))
plt.show()
def make_samples(data, input_length, output_length):
X = []
y = []
n_samples = len(data) + 1 - output_length - input_length
for i in range(n_samples):
i1 = i + input_length
i2 = i + input_length + output_length
X.append(data[i : i1])
y.append(data[i1 : i2])
return np.array(X)[:,:,np.newaxis], np.array(y)
seq_len = 360 * 5
in_len = 60
out_len = 1
sample_len = in_len + out_len
cut = 360 * 3
n_samples = seq_len + 1 - sample_len
n_train = cut + 1 - sample_len
n_val = seq_len - cut
print('Sequence Length: ', seq_len)
print('Number of samples: ', n_samples)
print('Training samples: ', n_train)
print('Validation samples:', n_val)
np.random.seed(1)
seq = sine_curve(length=360*5, noise=0.1)
X, y = make_samples(seq, in_len, out_len)
print('seq shape:', seq.shape)
print('X shape: ', X.shape)
print('y shape: ', y.shape)
# Sequences
seq_train, seq_val = seq[:cut], seq[cut:]
# Timestep ranges
t_train = range(0, cut)
t_val = range(cut, seq_len)
# Training and Validation Samples
X_train, X_val = X[:n_train,:], X[n_train:,:]
y_train, y_val = y[:n_train], y[n_train:]
print('Training set shapes:')
print('seq:', seq_train.shape)
print('X: ', X_train.shape)
print('y: ', y_train.shape)
print()
print('Validation set shapes:')
print('seq:', seq_val.shape)
print('X: ', X_val.shape)
print('y: ', y_val.shape)
plt.figure(figsize=[8,6])
plt.plot(t_train, seq_train, label='Training')
plt.plot(t_val, seq_val, label='Validation')
plt.legend()
plt.show()
%%time
np.random.seed(1)
set_random_seed(1)
model = Sequential()
model.add(SimpleRNN(4, activation='tanh', input_shape=(in_len,1),
return_sequences=False))
model.add(Dense(out_len))
opt = keras.optimizers.Adam(lr=0.001)
model.compile(loss='mean_squared_error', optimizer=opt)
h = model.fit(X_train, y_train, epochs=1000, batch_size=50, verbose=0,
validation_data=[X_val, y_val])
print(model.evaluate(X_train, y_train, verbose=0))
print(model.evaluate(X_val, y_val, verbose=0))
lag = 0
plt.figure(figsize=[6,4])
plt.plot(range(lag, 1000), h.history['loss'][lag:], label='Training')
plt.plot(range(lag, 1000), h.history['val_loss'][lag:], label='Validation')
plt.legend()
plt.show()
val_pred = model.predict(X_val)
print(np.mean( (y_val - val_pred)**2))
pred = model.predict(X)
print(pred.shape)
plt.figure(figsize=[8,6])
plt.plot(t_train, seq_train, linewidth=2, label='Training')
plt.plot(t_val, seq_val, linewidth=2, label='Validation')
plt.plot(range(in_len,1800), pred, linewidth=1, label='Prediction', c='k')
plt.legend(bbox_to_anchor=(1, 1), loc='upper left', ncol=1)
plt.show()
forecast = seq_train
for i in range(0, 720):
X_temp = forecast[-in_len:]
X_temp = X_temp.reshape(1,in_len,1)
new_val = model.predict(X_temp)
forecast = np.hstack([forecast, new_val[0,0]])
plt.figure(figsize=[8,6])
plt.plot(t_train, seq_train, linewidth=4, label='Training')
plt.plot(t_val, seq_val, linewidth=4, label='Validation')
plt.plot(forecast, linewidth=1, label='Prediction', c='k')
plt.legend(bbox_to_anchor=(1, 1), loc='upper left', ncol=1)
plt.show()
print(np.mean( (y_val - forecast[cut:])**2))
seq_len = 360 * 6
in_len = 360
out_len = 180
sample_len = in_len + out_len
cut = 360 * 4
n_samples = seq_len + 1 - sample_len
n_train = cut + 1 - sample_len
n_val = seq_len - cut
print('Sequence Length: ', seq_len)
print('Number of samples: ', n_samples)
print('Training samples: ', n_train)
print('Validation samples:', n_val)
np.random.seed(1)
seq = sine_curve(length=seq_len, noise=0.1)
X, y = make_samples(seq, in_len, out_len)
print('seq shape:', seq.shape)
print('X shape: ', X.shape)
print('y shape: ', y.shape)
# Sequences
seq_train, seq_val = seq[:cut], seq[cut:]
# Timestep ranges
t_train = range(0, cut)
t_val = range(cut, seq_len)
# Training and Validation Samples
X_train, X_val = X[:n_train,:], X[n_train:,:]
y_train, y_val = y[:n_train], y[n_train:]
print('Training set shapes:')
print('seq:', seq_train.shape)
print('X: ', X_train.shape)
print('y: ', y_train.shape)
print()
print('Validation set shapes:')
print('seq:', seq_val.shape)
print('X: ', X_val.shape)
print('y: ', y_val.shape)
plt.figure(figsize=[8,6])
plt.plot(t_train, seq_train, label='Training')
plt.plot(t_val, seq_val, label='Validation')
plt.legend()
plt.show()
%%time
np.random.seed(1)
set_random_seed(1)
model = Sequential()
model.add(SimpleRNN(4, activation='tanh', input_shape=(in_len,1),
return_sequences=False))
model.add(Dense(out_len))
opt = keras.optimizers.Adam(lr=0.001)
model.compile(loss='mean_squared_error', optimizer=opt)
h = model.fit(X_train, y_train, epochs=1000, batch_size=1000, verbose=0,
validation_data=[X_val, y_val])
print(model.evaluate(X_train, y_train, verbose=0))
print(model.evaluate(X_val, y_val, verbose=0))
lag = 0
plt.figure(figsize=[6,4])
plt.plot(range(lag, 1000), h.history['loss'][lag:], label='Training')
plt.plot(range(lag, 1000), h.history['val_loss'][lag:], label='Validation')
plt.legend()
plt.show()
val_pred = model.predict(X_val)
print(np.mean( (y_val - val_pred)**2))
pred = model.predict(X)
print(pred.shape)
ts1 = cut
ts2 = cut + 240
ts3 = cut + 540
input1 = seq[(ts1 - in_len):ts1].reshape(1,in_len,1)
output1 = model.predict(input1)
input2 = seq[(ts2 - in_len):ts2].reshape(1,in_len,1)
output2 = model.predict(input2)
input3 = seq[(ts3 - in_len):ts3].reshape(1,in_len,1)
output3 = model.predict(input3)
plt.figure(figsize=[8,6])
plt.plot(t_train, seq_train, linewidth=2, label='Training')
plt.plot(t_val, seq_val, linewidth=2, label='Validation')
plt.plot(range(ts1, ts1+out_len), output1[0], c='k', label='Prediction')
plt.plot(range(ts2, ts2+out_len), output2[0], c='k', label='Prediction')
plt.plot(range(ts3, ts3+out_len), output3[0], c='k', label='Prediction')
plt.legend(bbox_to_anchor=(1, 1), loc='upper left', ncol=1)
plt.show()
seq_len = 360 * 4
in_len = 720
out_len = 360
sequences = 250
np.random.seed(1)
X_list = []
y_list = []
for i in range(sequences):
#freq = np.random.uniform(1,3)
freq = 1
amp = np.random.uniform(1,2)
noise = np.random.uniform(0.05, 0.2)
seq = sine_curve(seq_len, freq, amp, noise)
X_temp, y_temp = make_samples(seq, in_len, out_len)
X_list.append(X_temp)
y_list.append(y_temp)
X = np.vstack(X_list)
y = np.vstack(y_list)
print(X.shape)
print(y.shape)
%%time
np.random.seed(1)
set_random_seed(1)
model = Sequential()
model.add(SimpleRNN(16, activation='tanh', input_shape=(in_len,1),
return_sequences=True))
model.add(SimpleRNN(16, activation='tanh', return_sequences=False))
model.add(Dense(out_len))
opt = keras.optimizers.Adam(lr=0.001)
model.compile(loss='mean_squared_error', optimizer=opt)
h = model.fit(X, y, epochs=25, batch_size=256, verbose=1)
print(model.evaluate(X, y, verbose=0))
lag = 0
plt.figure(figsize=[6,4])
plt.plot(h.history['loss'][lag:], label='Training')
plt.legend()
plt.show()
freq = 1
amp = np.random.uniform(0.5,3)
noise = np.random.uniform(0.05, 0.2)
test_seq = sine_curve(720 + 360, freq, amp, noise)
X_input = test_seq[:720].reshape(1,720,1)
forecast = model.predict(X_input).reshape(360,)
plt.figure(figsize=[8,6])
plt.plot(range(0,720), test_seq[:720], label='Input')
plt.plot(range(720,1080), test_seq[720:], label='True Values')
plt.plot(range(720,1080), forecast, label='Forecast')
plt.ylim([-2.5,2.5])
plt.legend()
plt.show()