How train_on_batch() is different from fit()? What are the cases when we should use train_on_batch()?
相关问题
- batch_dot with variable batch size in Keras
- How to use Reshape keras layer with two None dimen
- How to use Reshape keras layer with two None dimen
- Why keras use “call” instead of __call__?
- How to conditionally scale values in Keras Lambda
相关文章
- Tensorflow: device CUDA:0 not supported by XLA ser
- How to downgrade to cuda 10.0 in arch linux?
- Change loss function dynamically during training i
- How to use cross_val_score with random_state
- Why does this Keras model require over 6GB of memo
- How to measure overfitting when train and validati
- keras model subclassing examples
- McNemar's test in Python and comparison of cla
train_on_batch()
gives you greater control of the state of the LSTM, for example, when using a stateful LSTM and controlling calls tomodel.reset_states()
is needed. You may have multi-series data and need to reset the state after each series, which you can do withtrain_on_batch()
, but if you used.fit()
then the network would be trained on all the series of data without resetting the state. There's no right or wrong, it depends on what data you're using, and how you want the network to behave.I believe you mean to compare
train_on_batch
withfit
(and variations likefit_generator
), sincetrain
is not a commonly available API function for Keras.For this question, it's a simple answer from the primary author:
train_on_batch
allows you to expressly update weights based on a collection of samples you provide, without regard to any fixed batch size. You would use this in cases when that is what you want: to train on an explicit collection of samples. You could use that approach to maintain your own iteration over multiple batches of a traditional training set but allowingfit
orfit_generator
to iterate batches for you is likely simpler.One case when it might be nice to use
train_on_batch
is for updating a pre-trained model on a single new batch of samples. Suppose you've already trained and deployed a model, and sometime later you've received a new set of training samples previously never used. You could usetrain_on_batch
to directly update the existing model only on those samples. Other methods can do this too, but it is rather explicit to usetrain_on_batch
for this case.Apart from special cases like this (either where you have some pedagogical reason to maintain your own cursor across different training batches, or else for some type of semi-online training update on a special batch), it is probably better to just always use
fit
(for data that fits in memory) orfit_generator
(for streaming batches of data as a generator).