I am a newbie to deep learning and am trying to build a LSTM model capable of predicting users' points. Points are given to users based on their lifestyle and others parameters. My dataset df contains two columns, one for user ID and the second for points.
I want to input these two columns into the model and output Points
UserID | Points |
---|---|
aifd2345 | 29 |
aifd2345 | 25 |
aifd2345 | 30 |
aifd2345 | 40 |
aifd2345 | 26 |
aifd2345 | 32 |
aifd2346 | 29 |
aifd2346 | 27 |
aifd2346 | 30 |
aifd2346 | 28 |
aifd2346 | 27 |
aifd2346 | 26 |
aifd2347 | 28 |
aifd2347 | 27 |
aifd2347 | 28 |
aifd2347 | 28 |
For the sequences, I chose a windows size of 5 and the sixth will be the label. For Users without the enough number of data to build this sequence, I just skipped them. Here is my sequence function :
def df_to_seq_X_y(df, window_size=5):
df_as_np = df.to_numpy()
X = []
y = []
for i in range(len(df_as_np)-window_size):
if df_as_np[i][0] == df_as_np[i+window_size][0]:
row = [r for r in df_as_np[i:i+window_size]]
X.append(row)
label = df_as_np[i+window_size][1]
y.append(label)
else:
continue
return np.array(X), np.array(y)
Here is the shape of the labels y and sequence X
X.shape, y.shape
Output: ((967, 5, 2), (967,))
I then build my model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import *
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.losses import MeanSquaredError
from tensorflow.keras.metrics import RootMeanSquaredError
from tensorflow.keras.optimizers import Adam
model1 = Sequential()
model1.add(InputLayer((5, 2)))
model1.add(LSTM(64))
model1.add(Dense(8, 'relu'))
model1.add(Dense(1, 'linear'))
cp1 = ModelCheckpoint('model1/', save_best_only=True)
model1.compile(loss=MeanSquaredError(), optimizer=Adam(learning_rate=0.0001), metrics=[RootMeanSquaredError()])
model1.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=10, callbacks=[cp1])
When fitting I got this error :
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).
I suspect the UserID input for this error and run the code below :
[print(i.shape, i.dtype) for i in model1.inputs]
[print(o.shape, o.dtype) for o in model1.outputs]
[print(l.name, l.input_shape, l.dtype) for l in model1.layers]
Output:
(None, 5, 2) <dtype: 'float32'>
(None, 1) <dtype: 'float32'>
lstm (None, 5, 2) float32
dense (None, 64) float32
dense_1 (None, 8) float32
[None, None, None]
But now I really don't know what the problem is. If someone could help me. Do I need to drop the UserID columns because it contains strings? I still wanted to have it because I want my model to user specific prediction also.
As per the error..an integer data type in the NumPy array that cannot be converted to a Tensor data type.
After the seeing the code, it appears that there is an issue might be with the data type of the y_train and y_val variables. As per the code, the labels were converted to NumPy arrays, but the data type was not specified, which might have resulted in the default data type of integer being used.
y_train = should be (float32) y_val = should be (float32) Note - User ID column, it contains string values, and it cannot be directly used as input to the model. As User ID is strings you need to covert it into a numeric vector representation, which can then be concatenated with the LSTM output and passed through the dense layers. then try it might be it will help.