如何通过在Keras中使用LSTM将仅部分序列训练为输入来预测班级?

时间:2019-07-19 20:57:15

标签: python keras deep-learning generator lstm

客观

如果只向模型中提供部分输入,我想预测类。 (使用序列数据。使用Keras LSTM)

我做了什么

我已根据从here answered by @Kbrose获得的答案实施了模型 这样,我应该以与特定类别相对应的可变长度序列训练我的训练数据。

在这里,我想澄清一些与fit.generator,批处理大小,validation_steps和我的模型结果有关的查询

数据

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

目标:

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

代码

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)
  

问题1 :以25个step_per_epoch进行训练的模型。会是什么   第一步的batch_sizes?它的默认值为32或>   样本数// steps_per_epoch(在我的情况下为243 // 25 = 9)

     

问题2 :如何选择或选择validate_steps?正确的选择是什么? (就我而言,有31个validation_data样本)

enter image description here

培训与结果

经过将近100个时代的训练

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

enter image description here

  

问题3 :我可以从图片中了解什么?从图中,我认为这是过度拟合。如何改善模型?
  我的模型是否经过适当的训练?请发表评论

0 个答案:

没有答案