我正在尝试使用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
_________________________________________________________________
请帮助我
答案 0 :(得分:0)
在本(回答)部分中提及解决方案,即使它存在于“注释”部分中,也可以为社区带来好处。
问题是input
的数据类型。默认情况下,tensorflow keras
模型期望float32
,但是您要传递double
。
您可以更改模型的dtype,如下面的代码所示:
def make_model():
net = tf.keras.Sequential()
net.add(tf.keras.layers.Dense(4, activation='relu', dtype='float32'))
net.add(tf.keras.layers.Dense(4, activation='relu'))
net.add(tf.keras.layers.Dense(1))
return net
或将输入更改为float32
。更改input
:X = X.astype('float32')