chainer是比较早期的神经网络结构,2015年发布。目前用pytorch的还是比较多,但是chainer实现的辅助工具依然是非常实用和受欢迎的。
一、chainer结构
dataset,iterator,optimizer and model是大家所熟知的,就不细讲了
Updater:
Now that we have the training iterator and optimizer set up, we link them both together into the updater. The updater uses the minibatches from the iterator, does the forward and backward processing of the model, and updates the parameters of the model according to the optimizer.
49# Create the updater, using the optimizer
50updater = training.StandardUpdater(train_iter, optimizer, device=-1)
Finally we create a Trainer object. The trainer
processes minibatches using the updater。
52# Set up a trainer
53trainer = training.Trainer(updater, (50, 'epoch'), out='result')
Extensions:
(这是最主要的部分)
这个模块可以在训练的中途进行操作,比如训练多少iterations或者多少epoches之后,甚至是steps之后。主要是用来评估模型,记录模型进程,或者用来保存模型的。
功能如下:
First, use the testing iterator defined above for an Evaluator extension to the trainer to provide test scores.
#54 Evaluate the model with the test dataset for each epoch
trainer.extend(extensions.Evaluator(test_iter, model, device=-1))
Save a computational graph from loss
variable at the first iteration:
#57 Dump a computational graph from 'loss' variable at the first iteration
# The "main" refers to the target link of the "main" optimizer.
trainer.extend(extensions.DumpGraph('main/loss'))
Take a snapshot of the trainer object every 20 epochs.
trainer.extend(extensions.snapshot(), trigger=(20, 'epoch'))
Write a log of evaluation statistics for each epoch.
# 63 Write a log of evaluation statistics for each epoch
trainer.extend(extensions.LogReport())
Save two plot images to the result directory.
# Save two plot images to the result dir
trainer.extend(
extensions.PlotReport(['main/loss', 'validation/main/loss'],
'epoch', file_name='loss.png'))
trainer.extend(
extensions.PlotReport(
['main/accuracy', 'validation/main/accuracy'],
'epoch', file_name='accuracy.png'))
Print selected entries of the log to standard output.
# Print selected entries of the log to stdout
trainer.extend(extensions.PrintReport(
['epoch', 'main/loss', 'validation/main/loss',
'main/accuracy', 'validation/main/accuracy', 'elapsed_time']))
Main Loop:
# Run the training
trainer.run()
What is trainer Extension?
By adding an Extension to a Trainer using the extend() method, the Extension will be called according to the schedule specified by using a trigger
object.
The Trainer object contains all information used in a training loop, e.g., models, optimizers, updaters, iterators, and datasets, etc. This makes it possible to change settings such as the learning rate of an optimizer.
用好trigger就可以了
trainer.extend(lr_drop, trigger=(10, 'epoch'))
使用装饰器@make_extension
@training.make_extension(trigger=(10, 'epoch'))
def lr_drop(trainer):
trainer.updater.get_optimizer('main').lr *= 0.1
详细请看chainer官网