A program that learns MNIST image data in the sample code of the Chainer
https://github.com/pfnet/chainer/blob/master/examples/mnist/train_mnist.py
I wrote my own program referring to , but I got an error a few seconds after I started learning.
The contents of the error are
'int' object is not callable
This is happening in
self.trainer.run()
That's the part.All programs are listed below.
main.py
from__future_import print_function
import MultiLayerPerceptron
import chain
import chain.functions as F
import chain.links as L
from chain import training
from chain.training import extensions
GPU = 0
UNIT = 1000
O_UNIT = 10
BACTHSIZE= 100
EPOCH=20
OUT = 'result'
RESUME='"
defmain():
setParams=MultiLayerPerceptron.SetParams (UNIT, O_UNIT, GPU, BACTHSIZE, EPOCH, OUT, RESUME)
setParams.SetGPU()
setParams.SetOptimizer()
setParams.SetMNISTData()
setParams.SetTrainer()
setParams.SetExtension()
setParams.RunTrainer()
if__name__=='__main__':
main()
MultiLayerPerceptron.py
#-*-coding:utf-8-*-
import chain
import chain.links as L
import chain.functions as F
from chain import training
from chain.training import extensions
class MLP (chain.Chain):
def_init__(self, n_units, n_out):
super(MLP,self).__init__(
l1 = L. Linear (None, n_units),
l2 = L. Linear (None, n_units),
l3 = L. Linear (None, n_out),
)
def__call__(self, x):
h1 = F.relu(self.l1(x))
h2 = F.relu(self.l2(h1))
return self.l3(h2)
class SetParams:
def__init__(self, unit, o_unit, gpu, batchSize, epoch, out, resume):
self.model=L.Classifier (MLP(unit,o_unit))
self.gpu=gpu
self.batchSize =batchSize
self.epoch=epoch
self.out = out
self.resume=resume
self.optimiszer=None
self.train=None
self.test = None
self.trainIter=None
self.testIter=None
self.trainer=None
# GPU Configuration
def SetGPU(self):
if self.gpu>=0:
chain.cuda.get_device(self.gpu).use()
self.model.to_gpu()
print "Set GPU - OK"
# Configuring Optimization Functions
def SetOptimizer (self):
self.optimizer=chainer.optimizers.Adam()
self.optimizer.setup(self.model)
print "Set Optimizer - OK"
# Obtaining MNIST Image Data
def SetMNISTData(self):
self.train, self.test=chainer.datasets.get_mnist()#labeled dataset, 1D, data Type=float32, label Type=int32
# Configuring Mini Batch
self.trainIter=chain.iterators.SerialIterator(self.train,self.batchSize)#Repeated, reordered
self.testIter=chainer.iterators.SerialIterator(self.test,self.batchSize,False,False)# No iteration, no reorder
print "Set MNIST Image - OK"
def SetTrainer (self):
self.updater=training.StandardUpdater(self.trainIter, self.optimizer, device=self.gpu)
self.trainer=training.trainer(self.updater,(self.epoch, 'epoch'), out=self.out)#****Unknown*****
def SetExtension (self):
self.trainer.extend(extensions.Evaluator(self.testIter, self.model, self.gpu))
self.trainer.extend(extensions.dump_graph('main/loss'))
self.trainer.extend(extensions.snapshot(), trigger=(self.epoch, 'epoch')))
self.trainer.extend(extensions.LogReport())
self.trainer.extend(extensions.PrintReport(
['epoch', 'main/loss', 'validaton/main/loss',
'main/accuracy', 'validation/main/accuracy', 'elapped_time'))
self.trainer.extend(extensions.ProgressBar())
print "Set Extension - OK"
def RunTrainer (self):
if self.resume:
chain.serializers.load_npz(self.resume,self.trainer)
self.trainer.run()
Invalid Evaluator argument.
If you take a variable argument, you must specify an argument name or you will pass an unintended variable.
self.trainer.extend(extensions.Evaluator(self.testIter, self.model, device=self.gpu))
© 2024 OneMinuteCode. All rights reserved.