【问题标题】:LSTM - Matmul error in input while making predictionLSTM - 进行预测时输入中的 Matmul 错误
【发布时间】: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
_________________________________________________________________

请帮助我

【问题讨论】:

标签: python-3.x tensorflow keras lstm recurrent-neural-network


【解决方案1】:

为了社区的利益,在此(答案)部分提及解决方案,即使它出现在评论部分。

问题在于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。更改inputX = X.astype('float32')

【讨论】:

    猜你喜欢
    • 2019-07-01
    • 2018-09-24
    • 2021-08-11
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    相关资源
    最近更新 更多