初めに
ディープラーニング初心者です。 chainerでsin関数を学習させています。
内容
input,output数(プログラム内in_units,out_units)を、自由に変更できるプログラムを作成しています。 in_units = 1 out_units = 1 で実行したら、エラーをはかず完了するのですが、 in_units = 2,out_units = 1にしたところ下記のエラーが表示されました。
発生している問題・エラーメッセージ
Exception in main training loop: Invalid operation is performed in: LinearFunction (Forward) Expect: x.shape[1] == W.shape[1] Actual: 1 != 2 Traceback (most recent call last): File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\training\trainer.py", line 343, in run update() File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\training\updaters\standard_updater.py", line 240, in update self.update_core() File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\training\updaters\standard_updater.py", line 253, in update_core optimizer.update(loss_func, *in_arrays) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\optimizer.py", line 874, in update loss = lossfun(*args, **kwds) File "sinTest.py", line 39, in __call__ loss= F.mean_squared_error(self.predict(x), y) File "sinTest.py", line 46, in predict h1 = F.tanh(self.l1(x)) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\link.py", line 287, in __call__ out = forward(*args, **kwargs) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\links\connection\linear.py", line 183, in forward return linear.linear(x, self.W, self.b, n_batch_axes=n_batch_axes) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\functions\connection\linear.py", line 308, in linear y, = LinearFunction().apply(args) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\function_node.py", line 307, in apply self._check_data_type_forward(in_data) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\function_node.py", line 455, in _check_data_type_forward self.check_type_forward(in_type) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\functions\connection\linear.py", line 24, in check_type_forward type_check.expect( File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\utils\type_check.py", line 564, in expect expr.expect() File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\utils\type_check.py", line 495, in expect raise InvalidType( Will finalize trainer extensions and updater before reraising the exception. Traceback (most recent call last): File "sinTest.py", line 131, in <module> trainer.run() File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\training\trainer.py", line 376, in run six.reraise(*exc_info) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\six.py", line 719, in reraise raise value File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\training\trainer.py", line 343, in run update() File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\training\updaters\standard_updater.py", line 240, in update self.update_core() File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\training\updaters\standard_updater.py", line 253, in update_core optimizer.update(loss_func, *in_arrays) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\optimizer.py", line 874, in update loss = lossfun(*args, **kwds) File "sinTest.py", line 39, in __call__ loss= F.mean_squared_error(self.predict(x), y) File "sinTest.py", line 46, in predict h1 = F.tanh(self.l1(x)) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\link.py", line 287, in __call__ out = forward(*args, **kwargs) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\links\connection\linear.py", line 183, in forward return linear.linear(x, self.W, self.b, n_batch_axes=n_batch_axes) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\functions\connection\linear.py", line 308, in linear y, = LinearFunction().apply(args) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\function_node.py", line 307, in apply self._check_data_type_forward(in_data) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\function_node.py", line 455, in _check_data_type_forward self.check_type_forward(in_type) File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\functions\connection\linear.py", line 24, in check_type_forward type_check.expect( File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\utils\type_check.py", line 564, in expect expr.expect() File "C:\Users\syosh\anaconda3\envs\yoshihiro\lib\site-packages\chainer\utils\type_check.py", line 495, in expect raise InvalidType( chainer.utils.type_check.InvalidType: Invalid operation is performed in: LinearFunction (Forward) Expect: x.shape[1] == W.shape[1] Actual: 1 != 2
該当のソースコード
python
1import numpy as np #配列 2import chainer 3from chainer import cuda, Function, gradient_check, Variable, optimizers, serializers, utils 4from chainer import Link, Chain, ChainList 5import chainer.functions as F 6import chainer.links as L 7import time #時間 8import matplotlib 9matplotlib.use('Agg') 10from matplotlib import pyplot as plt #グラフ 11import os # ディレクトリを操作 12 13# データ 14def get_dataset(N): 15 x = np.linspace(0, 2 * np.pi, N) 16 y = np.sin(x) 17 dataset=[] 18 for xx,yy in zip(x,y): 19 dataset.append((xx,yy)) 20 return dataset 21 22# ニューラルネットワーク 23class MyChain(Chain): 24 def __init__(self, in_units=1, n_units=10, out_units=1): 25 super(MyChain, self).__init__( 26 l1=L.Linear(in_units, n_units), 27 l2=L.Linear(n_units, n_units), 28 l3=L.Linear(n_units, n_units), 29 l4=L.Linear(n_units, n_units), 30 l5=L.Linear(n_units, out_units)) 31 self.in_units = in_units 32 self.out_units = out_units 33 34 35 def __call__(self, x_data, y_data): 36 x = Variable(x_data.astype(np.float32).reshape(len(x_data),1)) # Variableオブジェクトに変換 37 y = Variable(y_data.astype(np.float32).reshape(len(y_data),1)) # Variableオブジェクトに変換 38 loss= F.mean_squared_error(self.predict(x), y) 39 chainer.reporter.report({ 40 'loss': loss 41 }, self) 42 return loss 43 44 def predict(self, x): 45 h1 = F.tanh(self.l1(x)) 46 h2 = F.tanh(self.l2(h1)) 47 h3 = F.tanh(self.l3(h2)) 48 h4 = F.tanh(self.l4(h3)) 49 h5 = self.l5(h4) 50 return h5 51 52 def get_predata(self, x): 53 return self.predict(Variable(x.astype(np.float32).reshape(len(x),self.in_units))).data 54 55from chainer.training import trigger as trigger_module 56class MyChartReport(chainer.training.extension.Extension): 57 58 #trigger = (1, 'epoch') 59 60 def __init__(self,model,N_test=900,trigger = (1, 'epoch')): 61 self.N_test=N_test 62 self.model=model 63 self._trigger = trigger_module.get_trigger(trigger) 64 65 def __call__(self,trainer): 66 if self._trigger(trainer): 67 with chainer.configuration.using_config('train', False): 68 theta = np.linspace(0, 2 * np.pi, self.N_test) 69 sin = np.sin(theta) 70 test = self.model.get_predata(theta) 71 plt.plot(theta, sin, label="sin") 72 plt.plot(theta, test, label="test") 73 plt.legend() 74 plt.grid(True) 75 plt.xlim(0, 2 * np.pi) 76 plt.ylim(-1.2, 1.2) 77 plt.title("sin") 78 plt.xlabel("theta") 79 plt.ylabel("amp") 80 plt.savefig("fig_sin_epoch{}.png".format(trainer.updater.epoch)) 81 plt.clf() 82 83# main 84if __name__ == "__main__": 85 86 # 学習データ 87 N = 1000 88 train = get_dataset(N) 89 90 # テストデータ 91 N_test = 900 92 test = get_dataset(N_test) 93 94 # 学習パラメータ 95 batchsize = 10 #バッチサイズ 96 n_epoch = 100 #epoch 97 in_units = 2 #input 98 n_units = 100 #node数 99 out_units = 1 #output 100 gpu=-1 101 102 # モデル作成 103 model = MyChain(in_units, n_units, out_units) 104 optimizer = optimizers.Adam() 105 optimizer.setup(model) 106 107 train_iter = chainer.iterators.SerialIterator(train, batchsize) 108 test_iter = chainer.iterators.SerialIterator(test, batchsize, repeat=False, shuffle=False) 109 110 updater = chainer.training.updaters.StandardUpdater(train_iter, optimizer, device=gpu) 111 trainer = chainer.training.Trainer(updater, (n_epoch, 'epoch'), out="snapshot") 112 113 trainer.extend(chainer.training.extensions.Evaluator(test_iter, model, device=gpu)) 114 trainer.extend(chainer.training.extensions.dump_graph('main/loss')) 115 116 # Take a snapshot for each specified epoch 117 frequency = 10 118 trainer.extend(chainer.training.extensions.snapshot(filename="snapshot_cureent"),trigger=(frequency, 'epoch')) 119 120 # Write a log of evaluation statistics for each epoch 121 #trainer.extend(chainer.training.extensions.LogReport()) 122 123 trainer.extend(MyChartReport(model,trigger=(10, "epoch"))) 124 125 trainer.extend(chainer.training.extensions.LogReport(trigger=(10, "epoch"))) 126 trainer.extend(chainer.training.extensions.PrintReport( 127 ["epoch", "main/loss", "validation/main/loss", "elapsed_time"])) 128 #trainer.extend(chainer.training.extensions.ProgressBar()) 129 trainer.extend(chainer.training.extensions.PlotReport(['main/loss', 'validation/main/loss'],trigger=(10, "epoch"),filename="loss.png")) 130 trainer.run() 131 132 model.to_cpu() 133 chainer.serializers.save_npz("model.h5",model)
あなたの回答
tips
プレビュー