0

I have input with 3 features and I want to predict only one feature. I want to split the data into 10 samples in each sequence and then train the LSTM model. My code is below.

def split_sequences(sequence_x, sequence_y, n_steps):
    X, y = [], []
    for i in range(0,len(sequence_x),n_steps):
        X.append(sequence_x[i:i+n_steps])
        y.append(sequence_y[i:i+n_steps])
    X = np.array(X)
    y = np.array(y)
    return X,y

First I separated the feature that I would predictfrom the dataframe.

sequence_y = df['feature4'].to_list()
df = df.drop(columns = ['feature4'])
n_steps_s = 10
X, y = split_sequences(df.values.tolist(), sequence_y, n_steps_s)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)

LSTM model:

n_features = len(X_train[0][0])

# define model
model = Sequential()
model.add(LSTM(100, activation='relu', return_sequences=True, input_shape=(n_steps_in, n_features)))
model.add(LSTM(100, activation='relu'))
model.add(Dense(n_steps_out))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X_train, y_train, epochs=2, verbose=1)

But then I get this error:

ValueError                                Traceback (most recent call last)
<ipython-input-682-fa5811eb8173> in <module>
      8 model.compile(optimizer='adam', loss='mse')
      9 # fit model
---> 10 model.fit(X_train, y_train, epochs=2, verbose=1)
.
.
.
.
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).
1dll
  • 39
  • 5
  • where do you import 'train_test_split' from? Have you tried casting your 'X_train' and 'y_train' objects to numpy arrays just before passing them to the 'fit' function like the error suggests you need to? – yann ziselman Jun 07 '21 at 06:33
  • I imported 'train_test_split' from 'sklearn.model_selection'. The type of X_train numpy.ndarray now, I even so tried X_train = np.asarray(X_train) before passing it to the fit function. The same is follows for y_train too – 1dll Jun 07 '21 at 06:41
  • `X_train` is indeed a numpy array, but what about its entries? `type(X_train[0])` gives `list`? Then you might need to modify `split_sequences` function. – Mustafa Aydın Jun 07 '21 at 06:49
  • Yes it gives list. I updated split_sequences function. ' def split_sequences(sequence_x, sequence_y, n_steps): X, y = [], [] for i in range(0,len(sequence_x),n_steps): X.append(np.array(sequence_x[i:i+n_steps])) y.append(np.array(sequence_y[i:i+n_steps])) X = np.array(X) y = np.array(y) return X,y ' It currently gives this error when running the fit function: ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray). – 1dll Jun 07 '21 at 06:56
  • @nehiridil I tried with sample data and didn't get that error. Did you check [this](https://stackoverflow.com/questions/58636087/tensorflow-valueerror-failed-to-convert-a-numpy-array-to-a-tensor-unsupporte) thread (the error is not exactly the same as yours but one of the answers might help still)? – Mustafa Aydın Jun 07 '21 at 09:04
  • Unfortunately those solutions didn't work for me – 1dll Jun 08 '21 at 06:11

1 Answers1

0
 def split_sequence(sequence, n_steps):
X,y = list(),list()
for i in range(len(sequence)):
    end_ix=i+n_steps
    if end_ix > len(sequence)-1:
        break
    seq_x, seq_y =sequence[i:end_ix], sequence[end_ix]
    X.append(seq_x)
    y.append(seq_y)
return array(X),array(y)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 08 '22 at 13:30