Until the last time, I wrote about the basic objects that make up Chainer. From this time, I would like to actually implement a neural network using those objects.
When writing a program that composes a neural network, the overall view is roughly composed of the following five.
It is a program that prepares data for training the model. There are various types of data such as sentences, images, and voices, but we process them into the form of data suitable for the neural network to be constructed.
Describe a class that inherits the Chain class. For details, refer to here.
class MyChain(Chain):
def __init__(self):
super(MyChain, self).__init__(
Declaration of a function containing parameters
)
def __call__(self, x, y):
Loss (error) function
Describes the process of optimizing the model (minimizing the error). For more information, see here
model = MyChain()
optimizer = optimizer.Adam()
optimizer.setup(model)
Learning is performed a specified number of times. Depending on the item, early termination may be performed, but this time we will simply specify epoch (number of learnings). It seems that the code is almost promised from gradient initialization to parameter update.
for epoch in range(Number of repetitions):
model.zerograds() #Gradient initialization
loss = model(train, target) #Error calculation
loss.backward() #Gradient calculation
optimizer.update() #Parameter update
Save the model of the training result and perform the test.
I don't want to stuff this information into one article, so from the next time
I will write about it in small parts. Up to here for this time
Takayoshi Yamashita Deep learning Kodansha that can be seen in the illustration Hiroyuki Shinno Practical deep learning with Chainer-How to implement complex NN-Ohmsha
Recommended Posts