1

I am trying to train an single step LSTM model using Keras. However, when I call the predict function I get the following error:

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/

My input shape is (250, 7, 3)

Here are the configuration and summary of the model:

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
_________________________________________________________________

Kindly assist me

user1584253
  • 975
  • 2
  • 18
  • 55
  • 1
    The error seems pretty straightforward; have you tried casting the tensor to `tf.float32`? – o-90 Oct 20 '19 at 17:09
  • Yes, I converted my numpy array to float32, and it solved the problem. – user1584253 Oct 20 '19 at 17:44
  • 1
    Possible duplicate of [InvalidArgumentError: cannot compute MatMul as input #0(zero-based) was expected to be a float tensor but is a double tensor \[Op:MatMul\]](https://stackoverflow.com/questions/54255431/invalidargumenterror-cannot-compute-matmul-as-input-0zero-based-was-expected) – o-90 Oct 20 '19 at 18:54

1 Answers1

0

Mentioning the solution in this (Answer) section even though it is present in the Comments Section, for the benefit of the community.

The problem is the datatype of the input. By default tensorflow keras model expects float32 but you are passing a double.

You can either change the dtype of the model as shown in the code below:

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

or Change the input to float32. To change the input: X = X.astype('float32')