LSTM网络进行的时间序列预测

时间:2020-10-23 13:11:24

标签: python tensorflow keras lstm

我正在使用LSTM进行时间序列预测,并且我使用了不同的参数值,而且总是精度很差。这是我的代码。请帮忙!

dataset = df["value"].values
dataset = dataset.astype('float32')
dataset = np.reshape(dataset, (-1, 1))
scaler = MinMaxScaler(feature_range=(-1, 1))
dataset = scaler.fit_transform(dataset)
# prepare data
train_size = int(len(dataset) * 0.99)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size], dataset[train_size:len(dataset)]

def create_dataset(dataset, look_back=1):
    X, Y = [], []
    for i in range(len(dataset)-look_back-1):
        a = dataset[i:(i+look_back), 0]
        X.append(a)
        Y.append(dataset[i + look_back, 0])
    return np.array(X), np.array(Y)
    
look_back = 1
X_train, Y_train = create_dataset(train, look_back)
X_test, Y_test = create_dataset(test, look_back)

# reshape input to be [samples, time steps, features]
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))

# build LSTM Network 
model = Sequential()
model.add(LSTM(64, activation='tanh', return_sequences=True, recurrent_activation="sigmoid",
                  recurrent_dropout =0.2, input_shape=(X_train.shape[1], 1)))

model.add(Dense(units=1))  
model.add(Dropout(rate=0.2))

model.compile(optimizer=keras.optimizers.Adam(learning_rate = 0.001), loss='mse', metrics=['accuracy'])
history = model.fit(X_train, Y_train, epochs=10, batch_size=32,verbose=1, validation_split=0.2,
                    validation_data=(X_test, Y_test), 
                    callbacks=[EarlyStopping(monitor='val_loss',patience=5)])

我得到的输出:

Epoch 1/10
252/252 [==============================] - 3s 11ms/step - loss: 0.1615 - accuracy: 0.0020 - val_loss: 0.1215 - val_accuracy: 0.0015
Epoch 2/10
252/252 [==============================] - 2s 9ms/step - loss: 0.1405 - accuracy: 0.0020 - val_loss: 0.1196 - val_accuracy: 0.0015
Epoch 3/10
252/252 [==============================] - 2s 9ms/step - loss: 0.1386 - accuracy: 0.0020 - val_loss: 0.1187 - val_accuracy: 0.0015
Epoch 4/10
252/252 [==============================] - 2s 9ms/step - loss: 0.1379 - accuracy: 0.0020 - val_loss: 0.1183 - val_accuracy: 0.0015
Epoch 5/10
252/252 [==============================] - 2s 9ms/step - loss: 0.1386 - accuracy: 0.0020 - val_loss: 0.1190 - val_accuracy: 0.0015
Epoch 6/10
252/252 [==============================] - 2s 10ms/step - loss: 0.1388 - accuracy: 0.0020 - val_loss: 0.1189 - val_accuracy: 0.0015
Epoch 7/10
252/252 [==============================] - 2s 9ms/step - loss: 0.1375 - accuracy: 0.0020 - val_loss: 0.1180 - val_accuracy: 0.0015
Epoch 8/10
252/252 [==============================] - 2s 9ms/step - loss: 0.1383 - accuracy: 0.0020 - val_loss: 0.1178 - val_accuracy: 0.0015
Epoch 9/10
252/252 [==============================] - 2s 8ms/step - loss: 0.1368 - accuracy: 0.0020 - val_loss: 0.1179 - val_accuracy: 0.0015
Epoch 10/10
252/252 [==============================] - 2s 9ms/step - loss: 0.1370 - accuracy: 0.0020 - val_loss: 0.1191 - val_accuracy: 0.0015

0 个答案:

没有答案