简体   繁体   English

在 Keras 中重塑 LSTM 的批次

[英]Reshaping a batch for LSTM in Keras

By using an LSTM AutoEncoder I am facing some problems in the shaping of the dataset accordingly for the LSTM needs.通过使用 LSTM 自动编码器,我在根据 LSTM 需要相应地调整数据集时遇到了一些问题。 Since I am training on batches, I generate a loop of windows of my time series - the code looks like this:因为我是批量训练,所以我生成了时间序列的窗口循环——代码如下所示:

X_batch = np.array(file.loc[window * WINDOWS_SIZE:(window + 1) * WINDOWS_SIZE - 1], dtype="f")
print(X_batch.shape)
X_batch = np.reshape(1, WINDOWS_SIZE, cluster_feature_size)
print(X_batch.shape)
history = model.fit(X_batch, X_batch, epochs=1, verbose=False)

My batches are shaped of 48 data points(WINDOWS_SIZE) and 45 metrics (cluster_feature_size variable).我的批次由 48 个数据点(WINDOWS_SIZE)和 45 个指标(cluster_feature_size 变量)组成。

I have read that I need to reshape my data in the following format (samples, timesteps, features) but I am failing somewhere and lacking of some information.我读到我需要按照以下格式(samples, timesteps, features)重塑我的数据,但我在某处失败并且缺少一些信息。

My assumption is that 1 sample is 1 batch and in a batch I have 48 data points and therefore I set 48 timesteps.我的假设是 1 个样本是 1 个批次,在一个批次中我有 48 个数据点,因此我设置了 48 个时间步长。

A draft of the model architecture I built so far is the following:到目前为止,我构建的模型架构草稿如下:

model = Sequential()
model.add(LSTM(100, activation='relu', input_shape=(WINDOWS_SIZE, cluster_feature_size)))
model.add(RepeatVector(WINDOWS_SIZE))
model.add(Dense(1))
model.add(LSTM(100, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(1)))

I followed an online tutorial and I am still working on it.我遵循了一个在线教程,但我仍在努力。

The error I get is this one, while I am reshaping:我得到的错误是这个,而我正在重塑:

(48, 45)
---> 17 X_batch = np.reshape(1, WINDOWS_SIZE, cluster_feature_size)
ValueError: cannot reshape array of size 1 into shape (48,)

No need of a reshape here:这里不需要重塑:

import numpy as np
X = np.random.rand(48, 45)
X = np.array([X])
print(X.shape)

gives me:给我:

>>> (1, 48, 45)

As i don't know more the context i can't help you more, but that should solve the reshape issue.由于我不了解更多上下文,因此我无法为您提供更多帮助,但这应该可以解决重塑问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM