How should the complete sequence be classified?

I want to classify the complete sequence into two categories. I searched a lot on the internet but couldn't find a result for this. My preferred way is to use keras' LSTM model to classify the "complete" string parsing sequence into two categories. The problem with this approach is the different shapes of X and y . This is a sample code I wrote to explain my problem.

import numpy as np
from keras.layers import Dense,LSTM
from keras.models import Sequential

#(no of samples, no of rows,step, feature)
feat= np.random.randint(0, 100, (150, 250, 25,10))

#(no of samples, binary_output)
target= np.random.randint(0, 2, (150, 1))

#model
model = Sequential()
model.add(LSTM(10, input_shape=(25, 10), return_sequences=True))
model.add(LSTM(10,return_sequences=False))
model.add(Dense(1, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop')
print model.summary()

for i in xrange(target.shape[0]):
    X=feat[i]
    y=target[i]
    model.fit(X,y)

      

Here I have 150 sample sequences that I want to classify into 0 or 1 . The problem is that

ValueError: Input arrays must have the same number of samples as target arrays. Found 250 input samples and 1 target sample.

If there is no way to accomplish this in deep learning techniques, can you suggest any other machine learning algorithms?

EDIT

Many have questioned this

#(no of samples, no of rows,step, feature)
feat= np.random.randint(0, 100, (150, 250, 25,10))

      

150 is the number of samples (count this as 150 time series). 250 and 10 are time series data with 250 rows and 10 columns. (250, 25,10)

is adding 25 time steps such that it can be piped to keras lstm input

+3


source to share


1 answer


The problem is when you do

X=feat[i]
y=target[i]

      

This removes the first axis that calls X.shape = (250, 25, 10)

and y.shape == (1,)

. When you call model.fit(X, y)

keras then it is assumed to X

have 250 samples and y

only one sample. This is why you are getting this error.



This can be solved by fetching fragments feat

and target

, for example, calling

X=feat[i:i+batch_size]
y=target[i:i+batch_size]

      

Where batch_size

is how many samples you want to use per iteration. If you install batch_size = 1

, you should get the behavior you specified in your code.

+2


source







All Articles