Google colaboratory上で以下のコードを実行したのですが、下記のエラーが出てつまづいています。
解決方法を教えてください。
AttributeError Traceback (most recent call last)
<ipython-input-7-ab72e94bd54e> in <module>()
81 trainer.extend(extensions.PlotReport(['main/loss'],'epoch',file_name='loss.png'))
82 trainer.extend(extensions.PlotReport(['main/accuracy'],'epoch',file_name='accuracy.png'))
---> 83 trainer.run()
84
85 serializer.save_npz("mymodel.npz",model)
8 frames
<ipython-input-7-ab72e94bd54e> in call(self, x)
22
23 def call(self,x):
---> 24 h = F.max_pooling_2d(F.relu(self.conv1(x)),ksize=5,stride=2,pad=2)
25 h = F.max_pooling_2d(F.relu(self.conv2(x)),ksize=5,stride=2,pad=2)
26 h = F.dropout(F.relu(self.l3(h)))
AttributeError: module 'chainer.function' has no attribute 'max_pooling_2d'
python
1import chainer 2import glob 3from itertools import chain 4from chainer.datasets import LabeledImageDataset 5from chainer import iterators,training,optimizers,datasets,serializers 6from chainer.training import extensions,triggers 7from chainer.dataset import concat_examples 8import chainer.function as F 9import chainer.links as L 10 11#-----MyChain----- 12 13class MyChain(chainer.Chain): 14 15 def __init__(self): 16 super(MyChain,self).__init__() 17 with self.init_scope(): 18 self.conv1 = L.Convolution2D(None,16,5,pad=2) 19 self.conv2 = L.Convolution2D(None,32,5,pad=2) 20 self.l3 = L.Linear(None,256) 21 self.l4 = L.Linear(None,5) 22 23 def __call__(self,x): 24 h = F.max_pooling_2d(F.relu(self.conv1(x)),ksize=5,stride=2,pad=2) 25 h = F.max_pooling_2d(F.relu(self.conv2(x)),ksize=5,stride=2,pad=2) 26 h = F.dropout(F.relu(self.l3(h))) 27 y = self.l4(h) 28 return y 29 30 31#-----img----- 32 33train_path = glob.glob('img/Deers/Deers_train/*') 34 35tr = 0 36label = [] 37for index,item in enumerate(train_path): 38 label.append(tr) 39 tr = tr+1 40 41pa_tuple = tuple(train_path) 42la_tuple = tuple(label) 43 44train_data = [(0,0)] * len(label) 45 46tr = 0 47for index,item in enumerate(train_path): 48 train_data[tr] = [train_path[tr],label[tr]] 49 tr = tr + 1 50 51dataset_train = chainer.datasets.LabeledImageDataset(train_data) 52 53from chainercv.transforms import resize 54from chainer.datasets import TransformDataset 55 56def transform(data): 57 img,label = data 58 img = resize(img,(500,500)) 59 return img,label 60 61deerset_train = chainer.datasets.TransformDataset(dataset_train,transform) 62 63#-----train----- 64 65epoch = 10 66batch = 5 67 68model = L.Classifier(MyChain()) 69optimizer = optimizers.Adam() 70optimizer.setup(model) 71 72train_iter = iterators.SerialIterator(deerset_train,batch) 73updater = training.StandardUpdater(train_iter,optimizer) 74trainer = training.Trainer(updater,(epoch,'epoch'),out='result') 75 76trainer.extend(extensions.dump_graph('main/loss')) 77trainer.extend(extensions.snapshot(),trigger=(epoch,'epoch')) 78trainer.extend(extensions.LogReport()) 79trainer.extend(extensions.PrintReport(['epoch','main/loss','main/accuracy'])) 80trainer.extend(extensions.ProgressBar()) 81trainer.extend(extensions.PlotReport(['main/loss'],'epoch',file_name='loss.png')) 82trainer.extend(extensions.PlotReport(['main/accuracy'],'epoch',file_name='accuracy.png')) 83trainer.run() 84 85serializer.save_npz("mymodel.npz",model) 86
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。