以下のコードを実装すると、
ImportError: cannot import name 'chainerx'
と出てきてしまいます。
これはchainerのサンプルコードなのですが、
どうして実行できないのかわかりません
教えてください。
python
1#!/usr/bin/env python 2import argparse 3import re 4 5import numpy 6 7import chainer 8import chainer.functions as F 9import chainer.links as L 10from chainer import training 11from chainer.training import extensions 12import chainerx 13 14 15# Network definition 16class MLP(chainer.Chain): 17 18 def __init__(self, n_units, n_out): 19 super(MLP, self).__init__() 20 with self.init_scope(): 21 # the size of the inputs to each layer will be inferred 22 self.l1 = L.Linear(None, n_units) # n_in -> n_units 23 self.l2 = L.Linear(None, n_units) # n_units -> n_units 24 self.l3 = L.Linear(None, n_out) # n_units -> n_out 25 26 def forward(self, x): 27 h1 = F.relu(self.l1(x)) 28 h2 = F.relu(self.l2(h1)) 29 return self.l3(h2) 30 31 32def parse_device(args): 33 gpu = None 34 if args.gpu is not None: 35 gpu = args.gpu 36 elif re.match(r'(-|+|)[0-9]+$', args.device): 37 gpu = int(args.device) 38 39 if gpu is not None: 40 if gpu < 0: 41 return chainer.get_device(numpy) 42 else: 43 import cupy 44 return chainer.get_device((cupy, gpu)) 45 46 return chainer.get_device(args.device) 47 48 49def main(): 50 parser = argparse.ArgumentParser(description='Chainer example: MNIST') 51 parser.add_argument('--batchsize', '-b', type=int, default=100, 52 help='Number of images in each mini-batch') 53 parser.add_argument('--epoch', '-e', type=int, default=20, 54 help='Number of sweeps over the dataset to train') 55 parser.add_argument('--frequency', '-f', type=int, default=-1, 56 help='Frequency of taking a snapshot') 57 parser.add_argument('--device', '-d', type=str, default='-1', 58 help='Device specifier. Either ChainerX device ' 59 'specifier or an integer. If non-negative integer, ' 60 'CuPy arrays with specified device id are used. If ' 61 'negative integer, NumPy arrays are used') 62 parser.add_argument('--out', '-o', default='result', 63 help='Directory to output the result') 64 parser.add_argument('--resume', '-r', default='', 65 help='Resume the training from snapshot') 66 parser.add_argument('--unit', '-u', type=int, default=1000, 67 help='Number of units') 68 parser.add_argument('--noplot', dest='plot', action='store_false', 69 help='Disable PlotReport extension') 70 group = parser.add_argument_group('deprecated arguments') 71 group.add_argument('--gpu', '-g', type=int, nargs='?', const=0, 72 help='GPU ID (negative value indicates CPU)') 73 args = parser.parse_args() 74 75 device = parse_device(args) 76 77 print('Device: {}'.format(device)) 78 print('# unit: {}'.format(args.unit)) 79 print('# Minibatch-size: {}'.format(args.batchsize)) 80 print('# epoch: {}'.format(args.epoch)) 81 print('') 82 83 # Set up a neural network to train 84 # Classifier reports softmax cross entropy loss and accuracy at every 85 # iteration, which will be used by the PrintReport extension below. 86 model = L.Classifier(MLP(args.unit, 10)) 87 model.to_device(device) 88 device.use() 89 90 # Setup an optimizer 91 optimizer = chainer.optimizers.Adam() 92 optimizer.setup(model) 93 94 # Load the MNIST dataset 95 train, test = chainer.datasets.get_mnist() 96 97 train_iter = chainer.iterators.SerialIterator(train, args.batchsize) 98 test_iter = chainer.iterators.SerialIterator(test, args.batchsize, 99 repeat=False, shuffle=False) 100 101 # Set up a trainer 102 updater = training.updaters.StandardUpdater( 103 train_iter, optimizer, device=device) 104 trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) 105 106 # Evaluate the model with the test dataset for each epoch 107 trainer.extend(extensions.Evaluator(test_iter, model, device=device)) 108 109 # Dump a computational graph from 'loss' variable at the first iteration 110 # The "main" refers to the target link of the "main" optimizer. 111 # TODO(niboshi): Temporarily disabled for chainerx. Fix it. 112 if device.xp is not chainerx: 113 trainer.extend(extensions.dump_graph('main/loss')) 114 115 # Take a snapshot for each specified epoch 116 frequency = args.epoch if args.frequency == -1 else max(1, args.frequency) 117 trainer.extend(extensions.snapshot(), trigger=(frequency, 'epoch')) 118 119 # Write a log of evaluation statistics for each epoch 120 trainer.extend(extensions.LogReport()) 121 122 # Save two plot images to the result dir 123 if args.plot and extensions.PlotReport.available(): 124 trainer.extend( 125 extensions.PlotReport(['main/loss', 'validation/main/loss'], 126 'epoch', file_name='loss.png')) 127 trainer.extend( 128 extensions.PlotReport( 129 ['main/accuracy', 'validation/main/accuracy'], 130 'epoch', file_name='accuracy.png')) 131 132 # Print selected entries of the log to stdout 133 # Here "main" refers to the target link of the "main" optimizer again, and 134 # "validation" refers to the default name of the Evaluator extension. 135 # Entries other than 'epoch' are reported by the Classifier link, called by 136 # either the updater or the evaluator. 137 trainer.extend(extensions.PrintReport( 138 ['epoch', 'main/loss', 'validation/main/loss', 139 'main/accuracy', 'validation/main/accuracy', 'elapsed_time'])) 140 141 # Print a progress bar to stdout 142 trainer.extend(extensions.ProgressBar()) 143 144 if args.resume: 145 # Resume from a snapshot 146 chainer.serializers.load_npz(args.resume, trainer) 147 148 # Run the training 149 trainer.run() 150 151 152if __name__ == '__main__': 153 main() 154
あなたがインストールしているChainerのバージョンを教えてください。
どこにあるサンプルコードですか? ChainerXのサンプルをChainerのサンプルだと思ってしまっているだけでは。
https://github.com/chainer/chainer/blob/master/examples/mnist/train_mnist.py
このサンプルコードなのですが。。
chainerのバージョンはこれです! '5.1.0'
回答1件
あなたの回答
tips
プレビュー