Migrating from Chainer v1 to Chainer v2

Since Chainer has been updated to 2.0 and is no longer backward compatible, I will write the modified part so that it can be operated with 2.0.2 using AlexNet as an example.

Network part

I think that this part is mainly changed, and there is no need to change anything else. In v1.x, class inheritance was performed directly in init part, but in v2.x, it has been changed to use with self.init_scope () :. The notation of the init part has changed accordingly. Previously, fc_n = L.function (), was described, but self. was added asself.fc_n = L.function (), and the last, is You don't need it anymore.

Note that if you forget to delete , here, you will get TypeError:'tuple' object is not callable. I was addicted to this error, wondering if the dataset was worse.

python:v1.x_Alex.py


class Alex(chainer.Chain):
  def __init__(self):
    super(Alex, self).__init__(
      conv1=L.Convolution2D(None,  96, 11, stride=4),
      conv2=L.Convolution2D(None, 256,  5, pad=2),
      conv3=L.Convolution2D(None, 384,  3, pad=1),
      conv4=L.Convolution2D(None, 384,  3, pad=1),
      conv5=L.Convolution2D(None, 256,  3, pad=1),
      fc6=L.Linear(None, 4096),
      fc7=L.Linear(None, 4096),
      fc8=L.Linear(None, 1000),
    )
  self.train = True
...

v2.x_Alex.py


class Alex(chainer.Chain):
  def __init__(self):
    super(Alex, self).__init__()
    with self.init_scope():
      self.conv1 = L.Convolution2D(None,  96, 11, stride=4)
      self.conv2 = L.Convolution2D(None, 256,  5, pad=2)
      self.conv3 = L.Convolution2D(None, 384,  3, pad=1)
      self.conv4 = L.Convolution2D(None, 384,  3, pad=1)
      self.conv5 = L.Convolution2D(None, 256,  3, pad=1)
      self.fc6 = L.Linear(None, 4096)
      self.fc7 = L.Linear(None, 4096)
      self.fc8 = L.Linear(None, 1000)
...

In addition, the train that is reflected only during learning has been deleted from the function and specified in with chainer.using_config ().

model = L.Classifier(Alex())
with chainer.using_config('train',False):
  y = (x)

Basically, it seems that it is common to set with this chainer.config, and ʻusecudnnis also set with this. If you writechainer.config.use_cudnn ='never'`, you can set not to use all at once.

Repeating part

There is nothing that doesn't work unless you change it, but the types of trainer.extend are increasing.

if extensions.PlotReport.available():
  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'))

It seems that the graph of loss and accuracy is automatically output by writing, but it did not work well because it was said that there was no matplotlib that should be included in my environment.

Try to move

It feels like I changed it and actually moved it, but the feed-forward network consisting only of general Linear is the same as before, but the network based on AlexNet is extremely learning. I feel like it's getting late. Probably badly written ...

Other changes are also written in this official Chainer docs

Recommended Posts

Migrating from Chainer v1 to Chainer v2
Sum from 1 to 10
Changes from Python 2 to Python 3.0
Transition from WSL1 to WSL2
Introduction to Private Chainer
From editing to execution
Mercurial stopped working after migrating from Python 2 to Python 3 (Note)
I read the Chainer reference (updated from time to time)
Cheating from PHP to Python
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Migrated from Flask-RESTPlus to Flask-RESTX
python-social-auth v0.2 to v0.3 migration steps
Chainer v1.21 has been released
Update python-social-auth from 0.1.x to 0.2.x
Migrate from requirements.txt to pipenv
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
Implemented SmoothGrad with Chainer v2
Flow from installing Ubuntu to installing chainer by making your own PC
git / python> git log analysis (v0.1, v0.2)> Implementation to estimate work time from git log
The easiest way to get Chainer v1.5 + CUDA + cuDNN on Windows
Call Matlab from Python to optimize
From Elasticsearch installation to data entry
vtkXMLUnstructuredGridReader Summary (updated from time to time)
vtkOpenFOAMReader Summary (Updated from time to time)
How to use SWIG from waf
Cannot migrate from direct_to_template to TemplateView
Engineer vocabulary (updated from time to time)
Create folders from '01' to '12' with python
Conversion from pdf to txt 1 [pdfminer]
Programming to learn from books May 10
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
Output from Raspberry Pi to Line
[Introduction] From installing kibana to starting
Convert from pdf to txt 2 [pyocr]
Connect to utf8mb4 database from python
OpenMPI installation from download to pass-through
Tensorflow memo [updated from time to time]
Python (from first time to execution)
Post images from Python to Tumblr
Send commands from Atom to Maya
How to launch Explorer from WSL
Programming to learn from books May 7
From Ubuntu 20.04 introduction to environment construction
Ssh connect to GCP from Windows
How to access wikipedia from python
Python to switch from another language
How to convert from .mgz to .nii.gz
Migrate from VS Code to PyCharm
pynq-z1 From purchase to operation check
Review from git init to git push
[Deprecated] Chainer v1.24.0 Tutorial for beginners
Did not change from Python 2 to 3
Update Python on Mac from 2 to 3
I tried to learn the angle from sin and cos with chainer