n_obs = n_days * n_features
train = scaled[:n_train_days, :] #rmb to update train size to consistent with batch
test = scaled[n_train_days:, :]

train_X, train_y = train[:, 1:], train[:, 0]
test_X, test_y = test[:, 1:], test[:, 0]
# reshape input to be 3D [samples, timesteps, features]
train_X = train_X.reshape((train_X.shape[0], n_days, n_features))
test_X = test_X.reshape((test_X.shape[0], n_days, n_features))
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)

# design network
# Define early_stopping_monitor
#early_stopping_monitor = EarlyStopping(patience=3)
model = Sequential()
model.add(LSTM(112, return_sequences = True, input_shape=(train_X.shape[1], train_X.shape[2]))) #return_sequences=True, 
model.add(Activation('tanh'))
model.add(Dropout(0.1))
#model.add(Dropout(0.3))
model.add(LSTM(112))
model.add(Dropout(0.28))
#model.add(Dense(35))
#model.add(Activation('hard_sigmoid'))
model.add(Dense(1))
model.add(Activation('hard_sigmoid'))
model.compile(loss='mse', optimizer='adam')
# fit network
history = model.fit(train_X, train_y, epochs=100, batch_size=42, validation_data=(test_X, test_y) ,verbose=2, shuffle=False) #validation_split=0.1, callbacks=[early_stopping_monitor],
# plot history
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='test')
Exemple #2
0
#targets = np.array(y_train.reshape(y_train.shape[0],1))
targets = np.array(y_train.values.reshape(y_train.shape[0],1))
features_validation= np.array(X_test)
#targets_validation = np.array(y_test.reshape(y_test.shape[0],1))
targets_validation = np.array(y_test.values.reshape(y_test.shape[0],1))

print(features[:10])
print(targets[:10])


# In[95]:


# Building the model
model = Sequential()
model.add(Dense(32, activation='relu', input_shape=(X_train.shape[1],)))
model.add(Dropout(.2))
model.add(Dense(16, activation='relu'))
model.add(Dropout(.1))
model.add(Dense(1))

# Compiling the model
model.compile(loss = 'mse', optimizer='adam', metrics=['mse']) #mse: mean_square_error
model.summary()


# In[101]:


# Training the model
epochs_tot = 1000