检查输入(lstm_1)时,Tensorflow抛出值错误

时间:2019-10-08 17:35:30

标签: python tensorflow keras

model.fit产生异常:

ValueError:检查输入时出错:预期lstm_1_input具有3个维,但数组的形状为(60752,29)

这是模型结构:

 train_x, valid_x = arr[:split, :], arr[split:, :]

train_y, valid_y = target[:split], target[split:]


train_x = train_x.reshape(train_x.shape[0], -1)
train_y = train_y.reshape(train_y.shape[0], -1)
valid_x = valid_x.reshape(valid_x.shape[0], -1)
valid_y = valid_y.reshape(valid_y.shape[0], -1)

print(train_x.shape)

input_params = train_x.shape[1]
print(input_params)

model = Sequential()
#start
model.add(LSTM(100, return_sequences=True, input_shape=(input_params, 1)))
model.add(LeakyReLU(alpha=2))
model.add(LSTM(100, return_sequences=True))
model.add(LeakyReLU(alpha=2))
model.add(LSTM(100))
model.add(LeakyReLU(alpha=2))
model.add(Dense(2, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

`

这些是模型的参数:

lstm_1 (LSTM)                (None, 29, 100)           40800     

_________________________________________________________________
leaky_re_lu_1 (LeakyReLU)    (None, 29, 100)           0         
_________________________________________________________________
lstm_2 (LSTM)                (None, 29, 100)           80400     
_________________________________________________________________
leaky_re_lu_2 (LeakyReLU)    (None, 29, 100)           0         
_________________________________________________________________
lstm_3 (LSTM)                (None, 100)               80400     
_________________________________________________________________
leaky_re_lu_3 (LeakyReLU)    (None, 100)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 2)                 202       
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 3         
=================================================================
Total params: 201,805
Trainable params: 201,805
Non-trainable params: 0

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

如果您希望输入只有1个通道,则可以选择

[1]重塑x

train_x = train_x.reshape(train_x.shape[0], -1)
train_y = train_y.reshape(train_y.shape[0], -1)
valid_x = valid_x.reshape(valid_x.shape[0], -1)
valid_y = valid_y.reshape(valid_y.shape[0], -1)

train_x = np.expand_dims(train_x, -1)
valid_x = np.expand_dims(valid_x, -1)

或[2]添加一个Reshape

model = Sequential()
model.add(keras.layers.Reshape((-1, 1)))
#start

应该这样做。