Write a cnn lstm code for time series forecasting with data split as well
- User
Write a cnn lstm code for time series forecasting with data split as well
Assistant
from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten, TimeDistributed, Reshape from keras.callbacks import EarlyStopping
define the network
nb_filters = 32 nb_hidden = 64 nb_epoch = 10
load the dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
convert to one-hot format
x_train = x_train.reshape(x_train.shape[0], nb_filters, nb_hidden) x_test = x_test.reshape(x_test.shape[0], nb_filters, nb_hidden)
add random noise
x_train = x_train + np.random.normal(size = x_train.shape) * 0.05 x_test = x_test + np.random.normal(size = x_test.shape) * 0.05
define the network
model = Sequential() model.add(TimeDistributedDropout(dropout_ratio=0.2)) model.add(Reshape((-1, nb_hidden), (nb_filters, nb_hidden))) model.add(Dense(64, activation='relu')) model.add(Dense(NB_CLASS, activation='softmax'))
Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
monitor the training status
early_stop = EarlyStopping(monitor='val_accuracy', patience=4) model.fit(x_train, y_train, batch_size=128, epochs=nb_epoch, validation_data=(x_test, y_test), callbacks=[early_stop])
evaluate the model
score = model.evaluate(x_test, y_test, verbose=0) print('Final score: %f' % score)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.