【发布时间】:2020-02-16 21:06:23
【问题描述】:
我正在尝试使用 Keras 训练单步 LSTM 模型。但是,当我调用预测函数时,出现以下错误:
InvalidArgumentError: cannot compute MatMul as input #0 was expected to be a float tensor but is a double tensor [Op:MatMul] name: lstm_5/MatMul/
我的输入形状是 (250, 7, 3)
下面是模型的配置和总结:
single_step_model = tf.keras.models.Sequential()
single_step_model.add(tf.keras.layers.LSTM(7,
input_shape=x_train_single.shape[-2:]))
single_step_model.add(tf.keras.layers.Dense(1))
single_step_model.compile(loss='mae', optimizer=tf.train.RMSPropOptimizer(learning_rate=0.001), metrics=['accuracy'])
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_5 (LSTM) (None, 7) 308
_________________________________________________________________
dense_5 (Dense) (None, 1) 8
=================================================================
Total params: 316
Trainable params: 316
Non-trainable params: 0
_________________________________________________________________
请帮助我
【问题讨论】:
-
这个错误看起来很简单;您是否尝试过将张量转换为
tf.float32? -
是的,我将我的 numpy 数组转换为 float32,它解决了问题。
标签: python-3.x tensorflow keras lstm recurrent-neural-network