So, I'm coding a LSTM model to find the next number in a sequence. But when I fit the model I get this error,
TypeError: Input 'b' of 'MatMul' Op has type float32 that does not match type int64 of argument 'a'.
I don't understand this question because Ive checked and all of the data and labels are dtype int64.
Here is my code:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM
import random
import numpy as np
def makeData(total, types=5):
#PATTERNS:
#1)going up by 1
#2)going up by 2
#3)muliply by 3
#4)multiple by 2
#5)square number
NumPer = int(total/types)
Labels = []
Data = []
print(NumPer)
for type in range(types):
for i in range(NumPer):
preData = []
if type == 0:
x = random.random()*100
x = round(x)
preData.append(x)
preData.append(x+1)
preData.append(x+2)
Labels.append([x+3])
if type == 1:
x = random.random()*100
x = round(x)
preData.append(x)
preData.append(x+2)
preData.append(x+4)
Labels.append([x+6])
if type == 2:
x = random.random()*100
x = round(x)
preData.append(x)
preData.append(x*3)
preData.append(x*9)
Labels.append([x*27])
if type == 3:
x = random.random()*100
x = round(x)
preData.append(x)
preData.append(x*2)
preData.append(x*4)
Labels.append([x*8])
if type == 4:
x = random.random()*100
x = round(x)
preData.append(x)
preData.append(x*x)
preData.append((x*x)*(x*x))
Labels.append([(x*x)*(x*x)*((x*x)*(x*x))])
Data.append(preData)
return np.array(Data), np.array(Labels)
x, y = makeData(5)
def MakeRNN():
model = Sequential()
model.add(LSTM(3, activation='tanh'))
model.add(Dense(9, activation='relu'))
model.add(LSTM(9, activation='tanh'))
model.add(Dense(36, activation='relu'))
model.add(Dense(1, activation='relu'))
model.compile(optimizer='Adam', loss="mse", metrics=['accuracy'])
return model
Model = MakeRNN()
print(x.shape)
x = x.reshape(-1,1,3)
print(y[0].dtype)
print(x.shape)
Model.fit(x,y, epochs=20)
print(Model.predict([[1,2,3]]))
I don't understand this, I would appreciate any help from you guys. I don't know too much in the field of keras.
Following code worked for me. Just made some tweaks to the x, y from makeData to convert them to floats. Also, the argument passed to predict needs to be 3 dimensional (at present you have a 2d argument) and it needs to be a float. Also need to add return sequences = True at first LSTM because it still has another LSTM after it. Also, your model is a little bit strange. Keras expects the inputs to a sequential model to be of form (batch_size, timesteps, input_dim). You have timesteps = 1 and hence no sequence really. But that is a separate thing about what you want to do. As for getting the code to run, above tweaks worked.
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM
import random
import numpy as np
def makeData(total, types=5):
#PATTERNS:
#1)going up by 1
#2)going up by 2
#3)muliply by 3
#4)multiple by 2
#5)square number
NumPer = int(total/types)
Labels = []
Data = []
print(NumPer)
for type in range(types):
for i in range(NumPer):
preData = []
if type == 0:
x = random.random()*100
x = round(x)
preData.append(x)
preData.append(x+1)
preData.append(x+2)
Labels.append([x+3])
if type == 1:
x = random.random()*100
x = round(x)
preData.append(x)
preData.append(x+2)
preData.append(x+4)
Labels.append([x+6])
if type == 2:
x = random.random()*100
x = round(x)
preData.append(x)
preData.append(x*3)
preData.append(x*9)
Labels.append([x*27])
if type == 3:
x = random.random()*100
x = round(x)
preData.append(x)
preData.append(x*2)
preData.append(x*4)
Labels.append([x*8])
if type == 4:
x = random.random()*100
x = round(x)
preData.append(x)
preData.append(x*x)
preData.append((x*x)*(x*x))
Labels.append([(x*x)*(x*x)*((x*x)*(x*x))])
Data.append(preData)
return np.array(Data), np.array(Labels)
x, y = makeData(5)
x = x.astype(float)
y = y.astype(float)
def MakeRNN():
model = Sequential()
model.add(LSTM(3, activation='tanh', return_sequences=True))
model.add(Dense(9, activation='relu'))
model.add(LSTM(9, activation='tanh'))
model.add(Dense(36, activation='relu'))
model.add(Dense(1, activation='relu'))
model.compile(optimizer='Adam', loss="mse", metrics=['accuracy'])
return model
Model = MakeRNN()
print(x.shape)
x = x.reshape(-1,1,3)
print(y[0].dtype)
print(x.shape)
Model.fit(x,y, epochs=20)
print(Model.predict(np.array([[[1.0,2.0,3.0]]])))