python-3.x - LSTM - 进行预测时输入中的 Matmul 错误
问题描述
我正在尝试使用 Keras 训练单步 LSTM 模型。但是,当我调用 predict 函数时,出现以下错误:
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
_________________________________________________________________
请帮助我
解决方案
为了社区的利益,即使它出现在评论部分中,也要在此(答案)部分中提及解决方案。
问题是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')
推荐阅读
- multithreading - 模拟器不起作用/在展开可选值时意外发现 nil/if else 代码问题
- java - 我如何在某个视角内与另一个代理(anylogic)进行通信?
- jenkins - 管道中每个阶段的 Jenkins 构建日志
- maven - jOOQ 不能自动生成类
- asp.net - 将 Web 应用程序发布到 IIS 时出现 HTTP 500 错误
- java - java.lang.AssertionError: Status expected:<200> but was:<404> 测试用例失败
- javascript - 通过绕过缓存强制浏览器使用 HTTP 或 HTTPS 打开网页
- c++ - 颜色未显示在 OpenGL 窗口中
- javascript - 想要修复联系表格,直到我们进入该特定部分
- python - 在 Beautifulsoup 的网络文章中查找某些名词