LSTM--预测时输入的Matmul误差

问题描述 投票:1回答:1

我试图使用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个回答
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')

© www.soinside.com 2019 - 2024. All rights reserved.