How to predict the class by training only partial sequence as Input using LSTM's in Keras?

↘锁芯ラ 提交于 2019-12-11 14:50:02

问题


Objective

I would like to predict the class if I give only partial input into the model. (Working with sequence data. Using Keras LSTM's)

What I have done

I have implemented my model based on the answers what I got from here answered by @Kbrose In such a way, i should train my training data with variable length sequence which corresponds to particular class.

Here, I would like to clarify some queries related to fit.generator, batch sizes, validation_steps and my Model results

Data

X_train.shape = (243*100*4)  # Samples * Time steps * Features 
Y_train.shape = (243,)       # either 0 or 1 for each samples

X_validate.shape : (31, 100, 4) # Samples * Time steps * Features 
Y_validate.shape : (31,)        # either 0 or 1 for each samples

X_test.shape : (28, 100, 4) # Samples * Time steps * Features 
Y_test.shape : (28,)        # either 0 or 1 for each samples

Objective:

1. Train the model with random time length batches
2. Predict the class, if random time length batches provided as input to the model

Code

input_ = Input(shape=(None,4))
x = LSTM(16, return_sequences=True)(input_)   
x = LSTM(8, return_sequences=True)(x)  
output = TimeDistributed(Dense(2, activation='sigmoid'))(x)

# Model
model = Model(inputs=input_, outputs=output)

print(model.summary())

# Model Compile
model.compile(
  loss='binary_crossentropy',
  optimizer=Adam(lr=1e-4),
  metrics=['accuracy']
)


def common_generator(X, Y):
  while True:
    sequence_length = random.randrange(60,100,5) 
    # I want my model to be trained with random time length b/w 50 to 100 with multiples of 5

    x_train = X[:, :sequence_length, :]

    y = to_categorical(Y) 
    y_train = np.repeat(y[:, np.newaxis], sequence_length, axis=1)

   # For my convenience i changed my Y_train shape from (243,) to (243, sequence_length, 2) 
   # Refer picture below for better understanding

    yield (x_train, y_train)

trainGen = common_generator(X_train,Y_train)
ValGen  = common_generator(X_validate, Y_validate)

H = model.fit_generator(trainGen, steps_per_epoch=30, validation_data=ValGen, validation_steps=3, epochs=100)

Question 1 : The model trained with 25 steps_per_epoch. What will be the batch_sizes for one step ? It takes default value as 32 or > number of samples//steps_per_epoch (In my case 243//25 = 9)

Question 2 : How to choose or select validation_steps ? What is the correct selection ? (In my case, there were 31 samples of validation_data)

Training & Results

After training for almost 100 epochs

Epoch 99/100
30/30 [==============================] - 6s 200ms/step - loss: 0.3055 - acc: 0.8789 - val_loss: 0.4075 - val_acc: 0.8259
Epoch 100/100
30/30 [==============================] - 6s 201ms/step - loss: 0.3051 - acc: 0.8791 - val_loss: 0.4051 - val_acc: 0.8260

Question 3 : What can i understand from the picture ? From the graph, i believe it is Over fitting. How can I improve my model ?
Whether my model gets trained properly or not ? Kindly comment

来源:https://stackoverflow.com/questions/57119513/how-to-predict-the-class-by-training-only-partial-sequence-as-input-using-lstms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!