0

Trying to run an LSTM model where the data is separated into few columns in csv and i'm trying to prepare date from such csv's.

Getting the error of

ValueError: Failed to convert a NumPy array to a Tensor 
(Unsupported object type numpy.ndarray)
# Load CSV data # 3 features (1, 2, 3), 1 label (5) columns
# All data is in float and 1,0 in Labels
data = pd.read_csv("data_1.csv", usecols=["Column_1","Column_2","Column_3","Column_5"]) 
data2 = pd.read_csv("data_2.csv", usecols=["Column_1","Column_2","Column_3","Column_5"])


window_size = 1  # For timesteps

# Function to create sequences
def create_sequences(data, window_size, label_col):
  sequences = []
  labels = []
  for i in range(len(data) - window_size + 1):
    window = data.loc[i:i+window_size,["Column_1","Column_2","Column_3"]].astype('float64')
    label = data.loc[i + window_size - 1, label_col]  # Label at the end of window
    sequences.append(window.to_numpy())
    labels.append(label)
  return np.array(sequences), np.array(labels)

# Create sequences and labels
sequences2, labels2 = create_sequences(data, window_size, "Column_5")
sequences2 = sequences2.reshape(-1, 1)  # (samples, timesteps, features)
print("Data 1 preparation is done")

sequences3, labels3 = create_sequences(data2, window_size, "Column_5")
sequences3 = sequences3.reshape(-1, 1)  # (samples, timesteps, features)

print("Data 2 preparation is done")

sequences = np.concatenate((sequences2, sequences3))
labels = np.concatenate((labels2, labels3), axis=0)

# Define LSTM model
model = keras.Sequential([
  keras.layers.LSTM(64, return_sequences=True, input_shape=(window_size, sequences.shape[2])),
  keras.layers.LSTM(32),
  keras.layers.Dense(len(np.unique(labels)), activation='softmax')  # Multi-class output
])

# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(sequences, keras.utils.to_categorical(labels), epochs=1)

I can also replicate it by running the code,

sequences=tf.convert_to_tensor(sequences)

Expecting the dataset taken properly in the LSTM Layer.

2
  • Hi Athul Srinivas ,There might be an issue with the data type of your sequences variable when passing it to the LSTM layer.you reshape sequences2 and sequences3 using sequences2.reshape(-1, 1) and sequences3.reshape(-1, 1), respectively. This reshaping operation is converting the sequences into a 2D NumPy array with only one feature dimension. However, in the LSTM layer, you're specifying input_shape=(window_size, sequences.shape[2]), where sequences.shape[2] refers to the number of features in the sequence. Commented Jun 4, 2024 at 10:46
  • where sequences.shape[2] refers to the number of features in the sequence. This inconsistency between the input shape specified in the LSTM layer and the actual shape of the sequences might be causing the error.you should reshape your sequences variable to have the correct shape, which should be (samples, timesteps, features).This will ensure that sequences2 and sequences3 have the correct shape before concatenating them. Additionally, when concatenating sequences2 and sequences3, you should concatenate along the second axis (axis=1) to maintain the correct shape. Commented Jun 4, 2024 at 10:47

0

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.