0

I'm trying to train an LSTM network with some sequential data with vehicle trajectories, in order to get driver's intention. Here is how I have structures my data:

  • 3 positional features per vehicle, with 5 vehicles for each entry. Each one of this features consists of an array of 50 elements.
  • The output of my model would be a classification for each individual vehicle intention, which is coded in 3 intentions (turn left, turn right, keep straight). This way we have 3x5=15 (5 vehicles) output columns. This columns are one hot encoded.

In case this clears it up:

Variable description

The code I'm trying to execute to build and train my model is the following:

input_columns = ['Local_X_0', 'Local_X_dot_0',  'Local_X_ddot_0', 'Local_X_1', 'Local_X_dot_1',  'Local_X_ddot_1', 'Local_X_2', 'Local_X_dot_2',  'Local_X_ddot_2', 
                    'Local_X_3', 'Local_X_dot_3',  'Local_X_ddot_3', 'Local_X_4', 'Local_X_dot_4',  'Local_X_ddot_4']

output_columns = ['Left_Change_0', 'Right_Change_0', 'No_Change_0', 'Left_Change_1', 'Right_Change_1', 'No_Change_1', 'Left_Change_2', 'Right_Change_2', 'No_Change_2',
                    'Left_Change_3', 'Right_Change_3', 'No_Change_3', 'Left_Change_4', 'Right_Change_4', 'No_Change_4']

X = np.array(df_id_arr[input_columns])
Y = np.array(df_id_arr[output_columns])


X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)

print ('X_train shape: ', X_train.shape)
print ('X_test shape: ', X_test.shape)


model = tf.keras.Sequential([
    tf.keras.layers.LSTM(64, input_shape=(50, 15)),  # Assuming 50 timesteps
    tf.keras.layers.Dense(15, activation='softmax')  # 15 output categories
])

# Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

# Train the model
history = model.fit(X_train, Y_train, epochs=10, batch_size=32, validation_data=(X_test, Y_test))

But when trying to execute model.fit I get the the following error:

ValueError                                Traceback (most recent call last)
c:\Users\peroanjo\OneDrive - Universidad Politécnica de Cartagena\Master IA\TFM\Code\LSTM.ipynb Cell 88 in ()
     23 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
     25 # Train the model
---> 26 history = model.fit(X_train, Y_train, epochs=10, batch_size=32, validation_data=(X_test, Y_test))

File c:\Users\peroanjo\anaconda3\envs\tf\lib\site-packages\keras\utils\traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File c:\Users\peroanjo\anaconda3\envs\tf\lib\site-packages\tensorflow\python\framework\constant_op.py:102, in convert_to_eager_tensor(value, ctx, dtype)
    100     dtype = dtypes.as_dtype(dtype).as_datatype_enum
    101 ctx.ensure_initialized()
--> 102 return ops.EagerTensor(value, ctx.device_name, dtype)

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

Any idea on what I'm missing? I think I'm not reshaping my input columns correctly, but I can't seem to get it right. Thank you for your help!

I tried reshaping or converting the dataframe column into a numpy array, but this hasn't helped.

1 Answer 1

0

From the screenshot, the issue should be related with how data is converted from Dataframe to np.ndarray. In this case, it appears that X has shape (15499, 15) but each of the elements is an individual array.

In my small-scale test, it appears that such an incomplete conversion could happen when the shape of the arrays being concatenated are not the same. Consider the following example:

import numpy as np

print(np.array([np.array([3, 5]), np.array([5, 3, 3])]))
# [array([3, 5]) array([5, 3, 3])] plus some warnings

print(np.array([np.array([3, 5, 5]), np.array([5, 3, 3])]))
# [[3 5 5]
# [5 3 3]]

So I would double check if there are entries in your data frame with a different number of elements than 50, e.g. if there are empty entries.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I tried checking if there are any arrays with a different shape, but it seems like all of them are 50 elements long, any other idea?
In that case, I would slice out a few rows from the data frame and see, for example, what np.array(X.head(3)[input_columns]) gives. There it would be easier to directly observe the resulting array. If that's one array with shape (3, 15, 50), there's something in the remaining rows that prevented the same conversion.
Or maybe try X = df_id_arr[input_columns].to_numpy() (see pandas docs).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.