Chainer的示例代码

时间:2016-12-05 15:04:06

标签: chainer

我自己重写了chainer的MNIST示例代码。但是发生了错误。

self.trainer.run()
  

'int'对象不可调用'

这里的所有代码。

# main.py

from __future__ import print_function
import MultiLayerPerceptron

import chainer
import chainer.functions as F
import chainer.links as L
from chainer import training
from chainer.training import extensions

GPU = 0
UNIT = 1000
O_UNIT = 10
BACTHSIZE = 100
EPOCH = 20
OUT = 'result'
RESUME = ''



def main():

    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()

MulutiLayerPerceptron.py

# -*- coding: utf-8 -*-

import chainer
import chainer.links as L
import chainer.functions as F
from chainer import training
from chainer.training import extensions


class MLP(chainer.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の設定       
    def SetGPU(self):
        if self.gpu >= 0:
            chainer.cuda.get_device(self.gpu).use()
            self.model.to_gpu()
        print "Set GPU - OK"

    def SetOptimizer(self):
        self.optimizer = chainer.optimizers.Adam()
        self.optimizer.setup(self.model)
        print "Set Optimizer - OK"
        return self.optimizer, self.model

    def SetMNISTData(self):
        self.train,self.test = chainer.datasets.get_mnist() 
        self.trainIter = chainer.iterators.SerialIterator(self.train, self.batchSize) 
        self.testIter = chainer.iterators.SerialIterator(self.test, self.batchSize, False, False) 
        print "Set MNIST Image - OK"

        return self.trainIter, self.testIter

    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)
        return self.trainer

    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', 'elapsed_time']))
        self.trainer.extend(extensions.ProgressBar())
        print "Set Extension - OK"
        return self.trainer

    def RunTrainer(self):
        if self.resume:
            chainer.serializers.load_npz(self.resume, self.trainer)
        self.trainer.run()

错误

File "E:\Programs\NNW\NNW_MNIST_for_GPU_2\NNW_MNIST_for_GPU_2\main.py", line 32, in <module>
main()
File "E:\Programs\NNW\NNW_MNIST_for_GPU_2\NNW_MNIST_for_GPU_2\main.py", line 29, in main
setParams.RunTrainer()
File"E:\Programs\NNW\NNW_MNIST_for_GPU_2\NNW_MNIST_for_GPU_2\MultiLayerPerceptron.py", line 78, in RunTrainer
self.trainer.run()
File "C:\Anaconda2\lib\site-packages\chainer\training\trainer.py", line 269, in run
entry.extension(self)
File "C:\Anaconda2\lib\site-packages\chainer\training\extensions\evaluator.py", line 134, in __call__
result = self.evaluate()
File "C:\Anaconda2\lib\site-packages\chainer\training\extensions\evaluator.py", line 165, in evaluate
in_arrays = self.converter(batch, self.device)
TypeError: 'int' object is not callable

1 个答案:

答案 0 :(得分:0)

请更改以下代码。

def SetExtension(self):
    self.trainer.extend(extensions.Evaluator(self.testIter, self.model, self.gpu))
    self.trainer.extend(extensions.dump_graph('main/loss'))

def SetExtension(self):
    self.trainer.extend(extensions.Evaluator(self.testIter, self.model, device = self.gpu))
    self.trainer.extend(extensions.dump_graph('main/loss'))