keras 如何训练LSTM架构来预测数字序列?

siotufzp  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(118)

我已经实现了以下LSTM架构。我特灵训练它来预测数字序列,但当我测试它不工作。我认为我给出了错误的输入和错误的测试数据。

import numpy as np
import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import LSTM,Dense

X_train = np.array([
    [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
    [[10, 11, 12], [13, 14, 15], [16, 17, 18]],
])

y_train = np.array([
    [4, 5, 6],
    [13, 14, 15],
])

#X_train = X_train.reshape((X_train.shape[0], 5, 5))

model = keras.Sequential()
model.add(keras.layers.LSTM(3,input_shape =(3, 3)))  #### The input_shape has to correspond to the input data
model.compile(loss="mean_squared_error", optimizer="adam")
model.fit(X_train, y_train, epochs=100)

X_new = np.array([[1,2,3]])
X_new = np.reshape(X_new, (1,3))

y_pred = model.predict(X_new)
print(y_pred)

有人能给予我正确的输入数据和测试数据来训练这个架构吗?

von4xj4u

von4xj4u1#

为问题设置输入和输出大小的一种方法如下(不一定是唯一的方法):

import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense

# train input
X_train = np.array([
    [[1], [2], [3]],
    [[4], [5], [6]],
    [[7], [8], [9]],
    [[10], [11], [12]],
    [[13], [14], [15]],
    [[16], [17], [18]],
])

y_train = np.array([
    [4],
    [5],
    [6],
    [13],
    [14],
    [15],
])

# Build the model
model = Sequential()
model.add(LSTM(3, input_shape=(3, 1)))  # Input shape should be (sequence_length, input_dimension)
model.add(Dense(1))  # just one neuron for regression
model.compile(loss="mean_squared_error", optimizer="adam")

# Reshape the input data to match the model's input shape
X_train = X_train.reshape((X_train.shape[0], 3, 1))

model.fit(X_train, y_train, epochs=100)

# Test the model
X_new = np.array([[1], [2], [3]])  # Input a sequence of numbers
X_new = X_new.reshape((1, 3, 1))
y_pred = model.predict(X_new)
print(y_pred)

相关问题