python - LSTM 神经网络输入/输出维度错误

标签 python tensorflow machine-learning neural-network lstm

我对 TensorFlow 和 LSTM 架构还很陌生。我在计算数据集的输入和输出(x_train、x_test、y_train、y_test)时遇到问题。

我输入的原始形状:

  • x_train: (366,4)
  • x_test: (104,4)
  • y_train: (366,)
  • y_test: (104,)

y_train 和 y_test 是一系列股票价格。 x_train 和 x_test 是我想用来预测股票价格的四个特征。

# Splitting the training and testing data

train_start_date = '2010-01-08'
train_end_date = '2017-01-06'
test_start_date = '2017-01-13'
test_end_date = '2019-01-04'

train = df.ix[train_start_date : train_end_date]
test = df.ix[test_start_date:test_end_date]


X_test = sentimentScorer(test)
X_train = sentimentScorer(train)

Y_test = test['prices'] 
Y_train = train['prices']

#Conversion in 3D array for LSTM INPUT

X_test = X_test.reshape(1, 104, 4)
X_train = X_train.reshape(1, 366, 4)





model = Sequential()

model.add(LSTM(128, input_shape=(366,4), activation='relu', 
return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(128, activation='relu'))
model.add(Dropout(0.1))

model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))

model.add(Dense(10, activation='softmax'))

opt = tf.keras.optimizers.Adam(lr=0.001, decay=1e-6)

# Compile model
model.compile(
    loss='sparse_categorical_crossentropy',
    optimizer=opt,
    metrics=['accuracy'],
)

model.fit(X_train,
          Y_train,
          epochs=3,
          validation_data=(X_test, Y_test))

这是产生的错误:

> --------------------------------------------------------------------------- ValueError                                Traceback (most recent call
> last) <ipython-input-101-fd4099583529> in <module>
>      65           Y_train,
>      66           epochs=3,
> ---> 67           validation_data=(X_test, Y_test))
> 
> c:\users\talal\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\keras\engine\training.py
> in fit(self, x, y, batch_size, epochs, verbose, callbacks,
> validation_split, validation_data, shuffle, class_weight,
> sample_weight, initial_epoch, steps_per_epoch, validation_steps,
> **kwargs)    1507         steps_name='steps_per_epoch',    1508         steps=steps_per_epoch,
> -> 1509         validation_split=validation_split)    1510     1511     # Prepare validation data.
> 
> c:\users\talal\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\keras\engine\training.py
> in _standardize_user_data(self, x, y, sample_weight, class_weight,
> batch_size, check_steps, steps_name, steps, validation_split)
>     991       x, y = next_element
>     992     x, y, sample_weights = self._standardize_weights(x, y, sample_weight,
> --> 993                                                      class_weight, batch_size)
>     994     return x, y, sample_weights
>     995 
> 
> c:\users\talal\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\keras\engine\training.py
> in _standardize_weights(self, x, y, sample_weight, class_weight,
> batch_size)    1110         feed_input_shapes,    1111        
> check_batch_axis=False,  # Don't enforce the batch size.
> -> 1112         exception_prefix='input')    1113     1114     if y is not None:
> 
> c:\users\talal\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\keras\engine\training_utils.py
> in standardize_input_data(data, names, shapes, check_batch_axis,
> exception_prefix)
>     314                            ': expected ' + names[i] + ' to have ' +
>     315                            str(len(shape)) + ' dimensions, but got array '
> --> 316                            'with shape ' + str(data_shape))
>     317         if not check_batch_axis:
>     318           data_shape = data_shape[1:]
> 
> ValueError: Error when checking input: expected lstm_18_input to have
> 3 dimensions, but got array with shape (366, 4)

最佳答案

您的代码几乎没问题。

您的 y_testy_train 应该是一个只有一个元素的数组或形状为 (1,1) 的数组,这并不重要。

虽然你的输入形状是错误的,第一个 LSTM 应该是:

model.add(LSTM(128, input_shape=(None,4), activation='relu', return_sequences=True))

注意 None,因为您的测试和训练序列长度不同,您不能指定它(并且 Keras 接受未指定的第一维)。错误是由于长度分别为 366 和 104。如果您想对 RNN 使用批处理,您应该使用 keras.preprocessing.sequence.pad_sequences 执行零填充。

无需为批处理指定 input_shape,网络的其余部分应该没问题。

如果您正在执行回归,而不是分类,可能是这种情况,您应该执行@Ankish Bansal 编写的最后两个步骤,例如将损失更改为 均方误差 并使最后一层输出 1 个值而不是 10。

关于python - LSTM 神经网络输入/输出维度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54139115/

相关文章:

python - 一半(不是 split !)seaborn 中的 fiddle 情节

python - 动态设置scrapy请求回调

tensorflow - 如何命名要在 Tensorflow Serving 中使用的 Tensorflow 模型?

python - 优化 tf.map_fn 的 GPU 使用

python - 使用预训练的 Inception_v4 模型

matlab - 清晰可分离数据的机会级别精度

python - 在 Python 中订购 Logit?

python - 如何在 Python 中为查询集创建 switch 语句

python - Django 1.7 数据迁移和用户组

python - tensorflow 抛出 "model_dir should be non-empty"