ValueError: Found input variables with inconsistent numbers of samples: [1, 74]

ValueError: Found input variables with inconsistent numbers of samples: [1, 74]

我想申请LSTM。 我有 12 个特征和 74 行

删除目标变量并为 3d 数组重塑后我的数据形状:(1, 74, 12) 和我的目标形状:(74,) 当我使用此代码拆分数据时:

x_train, x_test, y_train, y_test = train_test_split(data_1, target, test_size = 0.2,random_state =25)

我收到这个错误:

ValueError: Found input variables with inconsistent numbers of samples: [1, 74]

我很好地定义了模型,但是当我拟合模型时我也有另一个错误

定义模型:

model = Sequential()
model.add(LSTM(1, batch_input_shape=(1, 74, 12), return_sequences = True))
model.add(Dense(units = 1, activation = 'sigmoid'))
model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['accurecy'])
model.summary()

拟合模型:

history = model.fit(x_train, y_train, epochs = 100, validation_data= (x_test, y_test))

这里我也有这个错误:

ValueError: Input 0 of layer sequential_14 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 12)

我该如何解决这个错误?

tf.keras.layers.LSTM 期望输入:形状为 [batch, timesteps, feature].

的 3D 张量
import tensorflow as tf
inputs = tf.random.normal([32, 10, 8])
lstm = tf.keras.layers.LSTM(4, return_sequences=True, return_state=True)
whole_seq_output, final_memory_state, final_carry_state = lstm(inputs)
print(whole_seq_output.shape)

输出

(1, 74, 4)

如果您的输入形状是二维的,请使用 tf.expand_dims(input, axis=0) 添加额外的维度。