How to use model.reset_states() in Keras?

你说的曾经没有我的故事 提交于 2020-02-26 08:37:49

问题


I have sequential data and I declared a LSTM model which predicts y with x in Keras. So if I call model.predict(x1) and model.predict(x2), Is it correct to call model.reset_states between the two predict() explicitly? Does model.reset_states clear history of inputs, not weights, right?

# data1
x1 = [2,4,2,1,4]
y1 = [1,2,3,2,1]

# dat2
x2 = [5,3,2,4,5]
y2 = [5,3,2,3,2]

And in my actual code, I use model.evaluate(). In evaluate(), is reset_states called implicitly for each data sample?

model.evaluate(dataX, dataY)

回答1:


reset_states clears only the hidden states of your network. It's worth to mention that depending on if the option stateful=True was set in your network - the behaviour of this function might be different. If it's not set - all states are automatically reset after every batch computations in your network (so e.g. after calling fit, predict and evaluate also). If not - you should call reset_states every time, when you want to make consecutive model calls independent.




回答2:


If you use explicitly either of:

model.reset_states() 

to reset the states of all layers in the model, or

layer.reset_states() 

to reset the states of a specific stateful RNN layer (also LSTM layer), implemented here:

def reset_states(self, states=None):
  if not self.stateful:
     raise AttributeError('Layer must be stateful.')

this means your layer(s) must be stateful.

In LSTM you need to:

  • explicitly specify the batch size you are using, by passing a batch_size argument to the first layer in your model or batch_input_shape argument

  • set stateful=True.

  • specify shuffle=False when calling fit().


The benefits of using stateful models are probable best explained here.



来源:https://stackoverflow.com/questions/42763928/how-to-use-model-reset-states-in-keras

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