简体   繁体   中英

How to call reset_states() during training in keras?

I have sequential data which is ..

  • input dim of X are all same
  • sequence length of x is different among X

I am using LSTM, so I want to call reset_states for each x data ( x1 and x2 ). x1 and x2 are independent data, so I have to reset history of LSTM when I test x2 after x1 .

My code is here. Should I use stateful option?

# input dimension is two
# but data length is differenct between x1 and y1
x1 = [[1,2],[3,3],[2,1],[2,4]] # x1 length == 4
y1 = [2,3,2,1]

x2 = [[3,2], [2,1]] # x2 length == 2
y2 = [2,4]

input_dim = 2
max_len = 4 # max(len(x1), len(x2)
max_y = 4 # y -> (1,2,3,4)

trainX = [x1, x2]
trainY = [y1, y2]

m = Sequential()
m.add(LSTM(128, 
      input_shape=(max_len, input_dim),
      activation='tanh', 
      return_sequences=True))
m.add(TimeDistributed(Dense(max_y, activation='softmax')))
m.compile(...)
m.fit(trainX, trainY, nb_epoch=10)

Edited

I found a stateful LSTM example. But it calls reset_states() every epoch. What I want to do is calling every x . https://github.com/fchollet/keras/blob/aff40d800891799dc9ed765617fcbfa665349d0d/examples/stateful_lstm.py

The link that you referred uses fit function with epochs = 1 . I think you can either use train_on_batch() or use fit() function with on_batch_end() callback. This way, you can reset states after each x (by setting appropriate batch size).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM