chainerでボストン住宅価格予想を行うためにプログラムを書きました。
素人なので見よう見まねで書いたものの、エラーが発生し実行できずにいます。
#Chainerプログラム
import numpy as np import chainer import chainer.links as L import chainer.functions as F from sklearn.datasets import load_boston from chainer.datasets import TupleDataset from chainer.datasets import split_dataset_random from chainer.iterators import SerialIterator from chainer import optimizers from chainer.serializers import save_npz from chainer.training import extensions from chainer import training from chainer.functions.loss.mean_squared_error import mean_squared_error x,t = load_boston(return_X_y=True) x = x.astype('float32') t = t.astype('float32').reshape(len(t),1) mean = x.mean(axis=0) x -= mean std = x.std(axis=0) x /= std dataset = TupleDataset(x, t) class Net(chainer.Chain): def __init__(self): super().__init__() with self.init_scope(): self.l1 = L.Linear(None, 30) self.l3 = L.Linear(30,1) def forward(self, x): h = F.relu(self.l1(x)) h = self.l3(h) return h net = Net() t = t.astype('int32') train ,test = split_dataset_random(dataset, int(len(dataset)*0.7), seed=0) train_iter = SerialIterator(train, batch_size=1, repeat=True, shuffle=True) test_iter = SerialIterator(test, batch_size=1, repeat=False, shuffle=False) gpu_id = 0 # 使用する GPU 番号 n_epoch = 100# エポック数 net.to_gpu(gpu_id) net = L.Classifier(net, lossfun=mean_squared_error) optimizer = optimizers.Adam() optimizer.setup(net) updater = training.StandardUpdater(train_iter, optimizer, device = gpu_id) trainer = training.Trainer(updater, (n_epoch, 'epoch'), out = 'result') trainer.extend(extensions.LogReport()) trainer.extend(extensions.PrintReport(['epoch', 'main/loss', 'main/accuracy','elapsed_time'])) trainer.run()
#エラー
Exception in main training loop: Invalid operation is performed in: Accuracy (Forward) Expect: t.dtype.kind == i Actual: f != i Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/chainer/training/trainer.py", line 315, in run update() File "/usr/local/lib/python3.6/dist-packages/chainer/training/updaters/standard_updater.py", line 165, in update self.update_core() File "/usr/local/lib/python3.6/dist-packages/chainer/training/updaters/standard_updater.py", line 177, in update_core optimizer.update(loss_func, *in_arrays) File "/usr/local/lib/python3.6/dist-packages/chainer/optimizer.py", line 680, in update loss = lossfun(*args, **kwds) File "/usr/local/lib/python3.6/dist-packages/chainer/link.py", line 242, in __call__ out = forward(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/chainer/links/model/classifier.py", line 147, in forward self.accuracy = self.accfun(self.y, t) File "/usr/local/lib/python3.6/dist-packages/chainer/functions/evaluation/accuracy.py", line 98, in accuracy return Accuracy(ignore_label=ignore_label)(y, t) File "/usr/local/lib/python3.6/dist-packages/chainer/function.py", line 233, in __call__ ret = node.apply(inputs) File "/usr/local/lib/python3.6/dist-packages/chainer/function_node.py", line 245, in apply self._check_data_type_forward(in_data) File "/usr/local/lib/python3.6/dist-packages/chainer/function_node.py", line 330, in _check_data_type_forward self.check_type_forward(in_type) File "/usr/local/lib/python3.6/dist-packages/chainer/function.py", line 130, in check_type_forward self._function.check_type_forward(in_types) File "/usr/local/lib/python3.6/dist-packages/chainer/functions/evaluation/accuracy.py", line 19, in check_type_forward t_type.dtype.kind == 'i' File "/usr/local/lib/python3.6/dist-packages/chainer/utils/type_check.py", line 546, in expect expr.expect() File "/usr/local/lib/python3.6/dist-packages/chainer/utils/type_check.py", line 483, in expect '{0} {1} {2}'.format(left, self.inv, right)) Will finalize trainer extensions and updater before reraising the exception. InvalidType Traceback (most recent call last) <ipython-input-24-040b7ee08bef> in <module>() 71 72 ---> 73 trainer.run() 74 15 frames /usr/local/lib/python3.6/dist-packages/chainer/utils/type_check.py in expect(self) 481 raise InvalidType( 482 '{0} {1} {2}'.format(self.lhs, self.exp, self.rhs), --> 483 '{0} {1} {2}'.format(left, self.inv, right)) 484 485 InvalidType: Invalid operation is performed in: Accuracy (Forward) Expect: t.dtype.kind == i Actual: f != i
floatがintではないことはわかります。
しかしながら、int32のままだとエラー(
x[0].shape == x[1].shape
float32 != int32
)
が出るため、
t をastypeでfloat32に変換しています。
ソフトマックス交差エントロピーを損失関数に指定する場合は実行できました。
平均二乗誤差を使うのは初めてで原因がわからずにいます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。