Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 166a-Intro_to_time_series_Forecasting_using_LSTM.py #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions 166a-Intro_to_time_series_Forecasting_using_LSTM.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def to_sequences(dataset, seq_size=1):
x = []
y = []

for i in range(len(dataset)-seq_size-1):
for i in range(len(dataset)-seq_size):
#print(i)
window = dataset[i:(i+seq_size), 0]
x.append(window)
Expand All @@ -82,12 +82,12 @@ def to_sequences(dataset, seq_size=1):

######################################################
# Reshape input to be [samples, time steps, features]
#trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
#testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
#trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1],1))
#testX = np.reshape(testX, (testX.shape[0], testX.shape[1],1))
#
#print('Single LSTM with hidden Dense...')
#model = Sequential()
#model.add(LSTM(64, input_shape=(None, seq_size)))
#model.add(LSTM(64, input_shape=(seq_size,1)))
#model.add(Dense(32))
#model.add(Dense(1))
#model.compile(loss='mean_squared_error', optimizer='adam')
Expand All @@ -99,11 +99,11 @@ def to_sequences(dataset, seq_size=1):

#Stacked LSTM with 1 hidden dense layer
# reshape input to be [samples, time steps, features]
#trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
#testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
#trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1],1))
#testX = np.reshape(testX, (testX.shape[0], testX.shape[1],1))
#
#model = Sequential()
#model.add(LSTM(50, activation='relu', return_sequences=True, input_shape=(None, seq_size)))
#model.add(LSTM(50, activation='relu', return_sequences=True, input_shape=(seq_size,1)))
#model.add(LSTM(50, activation='relu'))
#model.add(Dense(32))
#model.add(Dense(1))
Expand All @@ -115,14 +115,14 @@ def to_sequences(dataset, seq_size=1):

#Bidirectional LSTM
# reshape input to be [samples, time steps, features]
#trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
#testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
#trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1],1))
#testX = np.reshape(testX, (testX.shape[0],testX.shape[1],1))
#
##For some sequence forecasting problems we may need LSTM to learn
## sequence in both forward and backward directions
#from keras.layers import Bidirectional
#model = Sequential()
#model.add(Bidirectional(LSTM(50, activation='relu'), input_shape=(None, seq_size)))
#model.add(Bidirectional(LSTM(50, activation='relu'), input_shape=(seq_size,1)))
#model.add(Dense(1))
#model.compile(optimizer='adam', loss='mean_squared_error')
#model.summary()
Expand Down Expand Up @@ -182,10 +182,10 @@ def to_sequences(dataset, seq_size=1):
# shift test predictions for plotting
testPredictPlot = np.empty_like(dataset)
testPredictPlot[:, :] = np.nan
testPredictPlot[len(trainPredict)+(seq_size*2)+1:len(dataset)-1, :] = testPredict
testPredictPlot[len(trainPredict)+(seq_size*2):len(dataset), :] = testPredict

# plot baseline and predictions
plt.plot(scaler.inverse_transform(dataset))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()
plt.show()