Keras LSTM输入/输出维度

2024-04-19 14:09:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Keras构建一个LSTM预测因子。我的输入数组是历史价格数据。我将数据分割成window_size块,以便预测前面的prediction length块。我的数据是4246个浮点数的列表。我将数据分成4055个数组,每个数组长度为168,以便预测前方24个单位。你知道吗

这给了我一个维度为(4055,168)x_train集。然后我缩放我的数据并尝试拟合数据,但遇到了尺寸错误。你知道吗

df = pd.DataFrame(data)
print(f"Len of df: {len(df)}")
min_max_scaler = MinMaxScaler()
H = 24

window_size = 7*H
num_pred_blocks = len(df)-window_size-H+1

x_train = []
y_train = []
for i in range(num_pred_blocks):
    x_train_block = df['C'][i:(i + window_size)]
    x_train.append(x_train_block)
    y_train_block = df['C'][(i + window_size):(i + window_size + H)]
    y_train.append(y_train_block)

LEN = int(len(x_train)*window_size)
x_train = min_max_scaler.fit_transform(x_train)
batch_size = 1

def build_model():
    model = Sequential()
    model.add(LSTM(input_shape=(window_size,batch_size),
                   return_sequences=True,
                   units=num_pred_blocks))
    model.add(TimeDistributed(Dense(H)))
    model.add(Activation("linear"))
    model.compile(loss="mse", optimizer="rmsprop")
    return model

num_epochs = epochs
model= build_model()
model.fit(x_train, y_train, batch_size = batch_size, epochs = 50)

返回的错误是这样的。你知道吗

ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 4055 arrays: [array([[0.00630006],

我没有正确分割吗?加载正确吗?单元的数量是否应该与预测块的数量不同?谢谢你的帮助。谢谢。你知道吗

编辑

将它们转换为Numpy数组的建议是正确的但是MinMixScalar()返回Numpy数组。我重新调整了数组的大小,但是现在我的计算机出现了CUDA内存错误。我认为问题解决了。非常感谢。你知道吗

df = pd.DataFrame(data)
min_max_scaler = MinMaxScaler()
H = prediction_length

window_size = 7*H
num_pred_blocks = len(df)-window_size-H+1

x_train = []
y_train = []
for i in range(num_pred_blocks):
    x_train_block = df['C'][i:(i + window_size)].values
    x_train.append(x_train_block)
    y_train_block = df['C'][(i + window_size):(i + window_size + H)].values
    y_train.append(y_train_block)

x_train = min_max_scaler.fit_transform(x_train)
y_train = min_max_scaler.fit_transform(y_train)
x_train = np.reshape(x_train, (len(x_train), 1, window_size))
y_train = np.reshape(y_train, (len(y_train), 1, H))
batch_size = 1

def build_model():
    model = Sequential()
    model.add(LSTM(batch_input_shape=(batch_size, 1, window_size),
                   return_sequences=True,
                   units=100))
    model.add(TimeDistributed(Dense(H)))
    model.add(Activation("linear"))
    model.compile(loss="mse", optimizer="rmsprop")
    return model

num_epochs = epochs
model = build_model()
model.fit(x_train, y_train, batch_size = batch_size, epochs = 50)

Tags: 数据adddfsizemodellenbatchtrain
2条回答

我认为你没有通过模型中的批量大小。你知道吗

input_shape=(window_size,batch_size)是数据维度。这是正确的,但是您应该使用input_shape=(window_size, 1)

如果要使用批处理,必须添加另一个维度,如LSTM(n_neurons, batch_input_shape=(n_batch, X.shape[1], X.shape[2]))(引用自Keras)

就你而言:

def build_model():
    model = Sequential()
    model.add(LSTM(input_shape=(batch_size, 1, window_size),
                   return_sequences=True,
                   units=num_pred_blocks))
    model.add(TimeDistributed(Dense(H)))
    model.add(Activation("linear"))
    model.compile(loss="mse", optimizer="rmsprop")
    return model

您还需要使用np.shape来更改数据的维度,它应该是(batch_dimdata_dim_1data_dim_2)。我使用numpy,所以numpy.reshape()可以工作。你知道吗

首先,您的数据应该是按行的,因此对于每一行,您应该有一个(1, 168)的形状,然后添加批处理维度,它将是(batch_n, 1, 168)。你知道吗

希望这有帮助。你知道吗

这可能是因为x_trainy_train没有更新到numpy数组。仔细看看github上的这个issue。你知道吗

model = build_model()
x_train, y_train = np.array(x_train), np.array(y_train)
model.fit(x_train, y_train, batch_size = batch_size, epochs = 50)

相关问题 更多 >