How to use the Keras model to predict future dates or events?

Here is my code for teaching the complete model and saving it.

num_units = 2
activation_function = 'sigmoid'
optimizer = 'adam'
loss_function = 'mean_squared_error'
batch_size = 10
num_epochs = 100

# Initialize the RNN
regressor = Sequential()

# Adding the input layer and the LSTM layer
regressor.add(LSTM(units = num_units, activation = activation_function, input_shape=(None, 1)))

# Adding the output layer
regressor.add(Dense(units = 1))

# Compiling the RNN
regressor.compile(optimizer = optimizer, loss = loss_function)

# Using the training set to train the model
regressor.fit(x_train, y_train, batch_size = batch_size, epochs = num_epochs)
regressor.save('model.h5')

After that, I saw that most of the time people were offering a test data set to test the prediction, which I tried and got a good result.

But the problem is using the model I created. I want to get a forecast for the next 30 days or every minute. Now I have a trained model, but I don’t understand what I can do, or what code I use to use the model and predict prices for the next 30 days or one minute.

Please offer me a way out. I’ve been stuck with this problem for a week and have been unable to make successful attempts.

, , .

+4
1

, stateful=True, , , , , .

, y be shifte x ( ). :

training_set = df_train.values
training_set = min_max_scaler.fit_transform(training_set)

x_train = training_set[0:len(training_set)-1]
y_train = training_set[1:len(training_set)]
x_train = np.reshape(x_train, (len(x_train), 1, 1))

LSTM (number_of_sequences, number_of_steps,features).

, 1 , , LSTM . ( ).

, 1 , (1, len(x_train), 1).

, y_train .

, , , LSTM return_sequences=True. y . , ( ).

, , .


stateful=True LSTM.

, reset : model.reset_states() - , .

X_train ( , , , : ).

predictions = model.predict(`X_train`) #this creates states

, , , :

future = []
currentStep = predictions[:,-1:,:] #last step from the previous prediction

for i in range(future_pred_count):
    currentStep = model.predict(currentStep) #get the next step
    future.append(currentStep) #store the future steps    

#after processing a sequence, reset the states for safety
model.reset_states()

2- , , , .

( stateful=False, reset - , reset , , stateful=True, , )

https://github.com/danmoller/TestRepo/blob/master/TestBookLSTM.ipynb

+3

Source: https://habr.com/ru/post/1693562/


All Articles