gpt4 book ai didi

python - 用于一维输入的 LSTM - TensorFlow 异常

转载 作者:太空宇宙 更新时间:2023-11-03 21:29:25 25 4
gpt4 key购买 nike

我正在研究 2 层 RNN (LSTM)。我认为我已经成功地 reshape 了我的火车和测试集,但是当我尝试运行代码时,它因异常而停止:

Exception: When using TensorFlow, you should define explicitly the number of timesteps of your sequences. If your first layer is an Embedding, make sure to pass it an "input_length" argument. Otherwise, make sure the first layer has an "input_shape" or "batch_input_shape" argument, including the time axis.

我尝试了多种配置,但没有一个能正常工作。我不知道如何解决它..

这是我创建模型并 reshape X_train 和 X_test 的代码

X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], EMB_SIZE))
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], EMB_SIZE))


print 'Building model...'

model = Sequential()

model.add(LSTM(input_dim=EMB_SIZE, output_dim=100, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2], 1)))
model.add(LSTM(input_dim=EMB_SIZE, output_dim=100, return_sequences=False,input_shape=(X_train.shape[1], X_train.shape[2], 1)))

model.add(Dense(2))
model.add(Activation('softmax'))
model.compile(optimizer='adam',
loss='mse',
metrics=['accuracy'])

model.fit(X_train,
Y_train,
nb_epoch=5,
batch_size = 128,
verbose=1,
validation_split=0.1)


score= model.evaluate(X_test, Y_test, batch_size=128)
print score

非常感谢任何帮助!

先谢谢你<3

最佳答案

最后一层的单元数量定义了模型的输出形状。

输出形状必须与目标 (Y) 形状相同。

  • 密集(2) -> 输出形状 = (无, 2)
  • 密集(1) -> 输出形状 = (无, 1)
  • Y_train -> 目标形状 = (15015,1)

哇哦...Keras 0.3.3?难怪一切都会有问题。

关于python - 用于一维输入的 LSTM - TensorFlow 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53620882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com