LSTM Keras输入形状混乱 [英] LSTM Keras input shape confusion

查看:44
本文介绍了LSTM Keras输入形状混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立股价预测模型.根据我的阅读,LSTM是一个很好的使用层.我无法完全理解我的模型的 input_shape 需要是什么.

I am trying to build a predictive model on stock prices. From what I've read, LSTM is a good layer to use. I can't fully understand what my input_shape needs to be for my model though.

这是我的 DataFrame

然后我将数据分为训练/测试

I then split the data into train / test

labels = df['close'].values
x_train_df = df.drop(columns=['close'])
x_train, x_test, y_train, y_test = train_test_split(x_train_df.values, labels, test_size=0.2, shuffle=False)

min_max_scaler = MinMaxScaler()
x_train = min_max_scaler.fit_transform(x_train)
x_test = min_max_scaler.transform(x_test)

print('y_train', y_train.shape)
print('y_test', y_test.shape)
print('x_train', x_train.shape)
print('x_test', x_test.shape)
print(x_train)

这将产生:

这是我感到困惑的地方.运行简单的示例,出现以下错误:

Here's where I am getting confused. Running the simple example, I get the following error:

ValueError:lstm_15层的输入0与该层不兼容:预期ndim = 3,找到的ndim = 4.收到完整的图形:[无,1、4026、5]

ValueError: Input 0 of layer lstm_15 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 1, 4026, 5]

我已经尝试过使用 input_shape 进行各种混乱的组合,并且得出结论,我不知道如何确定输入形状.

I've tried various combinations of messing with the input_shape and have came to the conclusion, I have no idea how to determine the input shape.

model = Sequential()
model.add(LSTM(32, input_shape=(1, x_train.shape[0], x_train.shape[1])))

model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10)

鉴于我的数据框,我的 input_shape 应该是什么?我知道输入形状是批量大小时间步长数据暗淡.只是不清楚如何将这些单词映射到我的实际数据,因为我认为值实际上不是.

Given my dataframe, what should be my input_shape? I understand that the input shape is batch size, timesteps, data dim. Just not clear how to map those words to my actual data as what I've thought the values were, are actually not.

我在想:

  • 批处理大小:我传递的记录数(4026)
  • 时间步长:1(我不确定这是否应该与批量大小相同吗?)
  • 数据维度:1,因为我的数据是1维的(我想是)
  • Batch Size: Number of records I'm passing in (4026)
  • Time Steps: 1 (I'm not sure if this is supposed to be the same value as batch size?)
  • Data Dimension: 1 since my data is 1 dimensional (I think?)

推荐答案

首先,我认为您根本不需要LSTM.根据 df.tail(),在我看来,熊猫数据框的行(数据集的样本)之间没有时间依赖性.无论如何,我稍后会再说一遍,首先是您的问题:

First of all, I don't think that you need an LSTM at all. Based on the df.tail(), it appears to me that there is no temporal dependence between the rows of the pandas data-frame (the samples of your dataset). Anyways, I will come back to that later, firstly your question:

  1. 批处理大小:批处理中的元素数.数据集总共包含4026个元素.另一方面,批处理大小是指单个批处理中要处理的元素数.让我们假设它是2.在这种情况下,您将具有这些批次的 2013 .

  1. Batch size: The number of elements in the batch. In total, the dataset contains 4026 elements. On the other hand, the batch size is the number of elements that are processed in a single batch. Let us assume that it is 2. In that case you will have 2013 of these batches.

时间步长:等于样本之间具有时间依赖性的数量.假设在您的数据集中每个3个实例构成数据序列,那么时间步长将为 3 .因此,可以得出结论,数据集中的每个样本现在由 3 个测量值组成,因此元素总数为 1342 (开始时为 4026 ).

Time steps: Equal to the number of samples which have a temporal dependence between them. Assuming that in your dataset each 3 instances constitute data sequence, then the time steps will be 3. Therefore, it follows that each sample in the dataset now consists of 3 measurements, so the total number of elements is 1342 (at the start was 4026).

数据维度:批次中每个时间步的每个元素的功能数量-在您的情况下为 5 ,假设 buy是标签,而 date 是时间相关性列.

Data dimension: The number of features for each element in the batch, for each time step - in your case 5, assuming that buy is the label and date is the temporal dependence column.

因此,单批数据的形状应为(2,3,6),而整个数据集的形状应为(1342,3,6).请注意,如果我们考虑使用LSTM,并且每个时间步之间存在时间依赖性,则这些形状是有效的.这是验证某些内容的示例代码片段:

As a result, the shape of a single batch of data should be (2, 3, 6), while the shape of the whole dataset would be (1342, 3, 6). Please note that these shapes are valid if we consider that you use LSTM and that there is temporal dependence between the each of the time steps. Here is an example code snippet to verify some stuff:

# Random training data
x_train = np.random.rand(1342, 3, 6)
# Random training labels
y_train = np.random.randint(0, 2, 1342)

model = Sequential()
model.add(LSTM(32, input_shape=(3, 6)))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=2, batch_size=32)

现在,回到我的上一点.查看 df.tail(),在我看来,数据集中的样本之间没有任何时间依赖性.话虽如此,我首先将 date 列转换为有意义的内容(一年中月份的一键编码,取决于月份的四个季节的一键编码,等等).然后,代替构建RNN,我将继续使用具有二进制分类输出层的前馈神经网络.

Now, back to my previous point. Looking at df.tail(), it seems to me that there is no temporal dependence whatsoever between the samples in the dataset. With that said, I would firstly convert the date column to something meaningful (one-hot encoding of the month in the year, one-hot encoding of the 4 seasons depending on the month, etc). Then, instead of constructing an RNN, I will proceed with a feed-forward neural network with a binary classification output layer.

对于模型,一旦您处理了所有与数据相关的内容,那么对您来说这样简单的事情就会起作用:

As for the model, once you take care of all data related stuff, something as simple as this should work for you:

# Random training data
x_train = np.random.rand(4026, 5)
# Random training labels
y_train = np.random.randint(0, 2, 4026)

model = Sequential()
model.add(Dense(32, activation='relu', input_dim=5))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(x_train y_train, epochs=2, batch_size=32)

希望有帮助!

这篇关于LSTM Keras输入形状混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆