Issue
I want to train my LSTM to classify a data point. I have one feature to train on and then the class.
My X looks like this: X=[3.13, 4.12, 9.12]
and my y looks like this: y=[1, 0, 0]
This is my code:
model = Sequential()
model.add(LSTM(lstm_layer_size_1, activation='tanh', return_sequences=True, input_shape=(1, 1)))
model.add(LSTM(lstm_layer_size_2, activation='tanh'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X,y,batch_size=len(X), epochs=epochs,verbose=0)
I am getting this error:
ValueError: Input 0 of layer sequential_9 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (6, 1)
I tried adding the input_shape=(1,1)
parameter on the theird line as well, it does not help.
I also tried to reshape my data by doing this
import numpy as np
X=np.resize(X,(X.shape[0],1,X.shape[1]))
model.fit(X,y,batch_size=len(X), epochs=epochs,verbose=0)
but then I got this error:
ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 1)
I am unsure how to reshape my data.
Can you please help me?
Solution
Please use this:
X = X.reshape(X.shape[0], 1, 1)
Code Sample:
X = np.array([3.13, 4.12, 9.12])
y = np.array([1, 0, 0])
X = X.reshape(X.shape[0], 1, 1) # reshape X to (batch_size, time_steps, input_dim)
lstm_layer_size_1 = 64
lstm_layer_size_2 = 32
optimizer = 'adam'
epochs = 50
model = Sequential()
model.add(LSTM(lstm_layer_size_1, activation='tanh', return_sequences=True, input_shape=(1, 1)))
model.add(LSTM(lstm_layer_size_2, activation='tanh'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X, y, batch_size=len(X), epochs=epochs, verbose=0)
Answered By - Ömer Sezer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.