获取 ValueError:在为双向 LSTM 重塑张量时,所有数组的长度必须相同


y_pred length 返回 13,y_test length 也返回 13,但 y_pred.reshape(-1) 返回 130。 y_pred参数需要一个参数,我如何将其重塑为 13?
from keras.layers import Dense, Dropout,Activation, LSTM,Bidirectional
from keras.models import Sequential
import tensorflow as tf
BLSTM = Sequential()
BLSTM.add(Bidirectional(LSTM(100,return_sequences=True, input_shape=(10,1), activation='gelu')))
BLSTM.add(Dense(1))
BLSTM.compile(optimizer = 'adam', loss = 'mean_squared_error')
BLSTM.build(input_shape=(10,1,1))
BLSTM.summary()
history = BLSTM.fit(X_train_t, y_train, epochs=10, batch_size=128)
BLSTM.evaluate(X_test_t, y_test, batch_size=32)
y_pred = BLSTM.predict(X_test_t, batch_size=32)
como = pd.DataFrame({'testdata' : y_test.Price.values,'predictions' : example})

查看模型的摘要和LSTM图层的参数。您使用的是(10,1)的输入形状,这意味着您有 10 个时间步长,每个时间步长有 1 个要素。至少这是你告诉这一层的。请注意,这与数据集中的样本数无关。完整的形状将是(samples, timesteps, features).然后您将return_sequences参数设置为True,这意味着您将从输入中获得所有时间步长,从而产生输出形状(None, 10, 200),其中 200 是输出空间是由于Bidirectional图层,None是一些可变的批大小。

现在基于这些信息,您必须问自己您的数据是否真的具有形状(samples, 10, 1),因为如果是这种情况,那么您正在使用错误的形状构建模型:input_shape=(10,1)!=input_shape=(10,1,1)。时间步长维度不同,这可能会导致模型出现问题。下面是一个示例,说明如果x_trainy_train都具有形状(samples, 10, 1),则模型的外观:

import tensorflow as tf
timesteps = 10
features = 1
BLSTM = tf.keras.Sequential()
BLSTM.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(100,return_sequences=True, input_shape=(timesteps, features))))
BLSTM.add(tf.keras.layers.Dense(1))
BLSTM.compile(optimizer = 'adam', loss = 'mean_squared_error')
BLSTM.build(input_shape=(1, timesteps, features))
BLSTM.summary()
samples = 500
X_train_t = tf.random.normal((samples, timesteps, features))
y_train = tf.random.normal((samples, timesteps, features))
X_test_t = tf.random.normal((samples, timesteps, features))
y_test = tf.random.normal((samples, timesteps, features))
history = BLSTM.fit(X_train_t, y_train, epochs=1, batch_size=128)
BLSTM.evaluate(X_test_t, y_test, batch_size=32)
y_pred = BLSTM.predict(X_test_t, batch_size=32)
print(y_pred.shape)
Model: "sequential_10"
_________________________________________________________________
Layer (type)                Output Shape              Param #   
=================================================================
bidirectional_10 (Bidirecti  (1, 10, 200)             81600     
onal)                                                           

dense_10 (Dense)            (1, 10, 1)                201       

=================================================================
Total params: 81,801
Trainable params: 81,801
Non-trainable params: 0
_________________________________________________________________
4/4 [==============================] - 8s 16ms/step - loss: 0.9933
16/16 [==============================] - 2s 7ms/step - loss: 0.9771
(500, 10, 1)

X_test_ty_test也必须具有相同的形状。

最新更新