Reputation: 31
I am trying to test different types of implementations of LSTM and facing this error in the code while predicting.
Tensorflow version - '2.0.0'
I am not removing this question as I still need to know what went wrong. Do i always need to worry about having float32 as the datatypes while feeding the models?
Sample Code
X = list()
Y = list()
X = [x+1 for x in range(20)]
Y = [y * 15 for y in X]
X = np.array(X,dtype=int)
Y= np.array(Y,dtype=int)
X=array(X).reshape(20, 1, 1)
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(1, 1)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
print(model.summary())
model.fit(X, Y, epochs=2, validation_split=0.2, batch_size=5)
test_input = np.array(30,dtype=int)
test_input = test_input.reshape((1, 1, 1))
test_output = model.predict(test_input) <---- ERROR IN THIS LINE
Error :
ValueError Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow_core\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
526 as_ref=input_arg.is_ref,
--> 527 preferred_dtype=default_dtype)
528 except TypeError as err:
~\AppData\Local\Continuum\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow_core\python\framework\ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx, accept_composite_tensors)
1270 "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
-> 1271 (dtype.name, value.dtype.name, value))
1272 return value
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: <tf.Tensor 'sequential/lstm/MatMul/ReadVariableOp:0' shape=(1, 200) dtype=float32>
During handling of the above exception, another exception occurred:
.....
.....
.....
TypeError: Input 'b' of 'MatMul' Op has type float32 that does not match type int32 of argument 'a'.
Upvotes: 1
Views: 948
Reputation: 31
I tried different permutations of datatypes and turns out I just had to change all the datatypes in all the arrays to 'float32' to solve the error.
Upvotes: 1