2層のニューラルネットワークのプログラムで発生したエラーを直したい
2層のニューラルネットワークの構築を行っているのですが、1層で行ったときは成功したのですが、2層に変更した場合エラーが発生しました。どのように直せばいいか教えていただきたいです。
発生している問題・エラーメッセージ
InvalidType Traceback (most recent call last) <ipython-input-7-613018698fb8> in <module>() 68 data = [xtrain, xtest, ttrain, ttest] 69 result = [train_loss, test_loss] ---> 70 learning_regression(model, optNN, data, result, 100) 71 plot_result2(result[0], result[1], "loss function", "step", "loss function", 0.0,3) 72 config.train = False 6 frames /usr/local/lib/python3.7/dist-packages/chainer/utils/type_check.py in expect(self) 495 raise InvalidType( 496 '{0} {1} {2}'.format(self.lhs, self.exp, self.rhs), --> 497 '{0} {1} {2}'.format(left, self.inv, right)) 498 499 InvalidType: Invalid operation is performed in: MeanSquaredError (Forward) Expect: x0.shape == x1.shape Actual: (50, 2) != (50, 1)
該当のソースコード
#!pip install chainer #(run it only the first time) import chainer.optimizers as Opt import chainer.functions as F import chainer.links as L from chainer import Variable,Chain,config,cuda import numpy as np import matplotlib.pyplot as plt ######### D = 100 ndata = np.linspace(-5.0,5.0,D) N = 1 xdata = ndata.reshape(D,N).astype(np.float32) tdata = (np.sin(ndata)+np.sin(2.0*ndata)).reshape(D,N).astype(np.float32) plt.plot(xdata, tdata) plt.show C=2 H=20 NN=Chain(l1=L.Linear(N,H), l2=L.Linear(H,C), bnorm1=L.BatchNormalization(H)) def model(x): h=NN.l1(x) # Confirm the difference between each function h=F.relu(h) #case-1 # h=F.sigmoid(h) #case-2 h=F.sin(h) #case-3 # h=NN.bnorm1(h) y=NN.l2(h) return y def data_divide(Dtrain,D,xdata,tdata): index = np.random.permutation(range(D)) xtrain = xdata[index[0:Dtrain],:] ttrain = tdata[index[0:Dtrain]] xtest = xdata[index[Dtrain:D],:] ttest = tdata[index[Dtrain:D]] return xtrain,xtest,ttrain,ttest def learning_regression(model, optNN, data, result, T=10): for time in range(T): config.train = True optNN.target.cleargrads() ytrain=model(data[0]) loss_train=F.mean_squared_error(ytrain,data[2]) loss_train.backward() optNN.update() config.train = False ytest = model(data[1]) loss_test = F.mean_squared_error(ytest,data[3]) result[0].append(cuda.to_cpu(loss_train.data)) result[1].append(cuda.to_cpu(loss_test.data)) def plot_result2(result1,result2,title,xlabel,ylabel,ymin=0.0,ymax=1.0): Tall = len(result1) plt.figure(figsize=(8,6)) plt.plot(range(Tall),result1) plt.plot(range(Tall),result2) plt.title(title) plt.xlabel(xlabel) plt.ylabel(ylabel) plt.xlim([0,Tall]) plt.ylim([ymin,ymax]) plt.show() optNN=Opt.MomentumSGD() optNN.setup(NN) train_loss = [] test_loss = [] Dtrain = D//2 xtrain,xtest,ttrain,ttest = data_divide(Dtrain,D,xdata,tdata) data = [xtrain, xtest, ttrain, ttest] result = [train_loss, test_loss] learning_regression(model, optNN, data, result, 100) plot_result2(result[0], result[1], "loss function", "step", "loss function", 0.0,3) config.train = False ytrain = model(xtrain).data ytest = model(xtest).data plt.plot(xtrain, ytrain, marker="x", linestyle="None") plt.plot(xtest, ytest, marker="o", linestyle="None") plt.plot(xdata,tdata) plt.show() #extrapolation De = 200 ndatae = np.linspace(-10.0,10.0,De) xext = ndatae.reshape(De,N).astype(np.float32) config.train = False ytrain = model(xtrain).data yext = model(xext).data plt.plot(xtrain, ytrain, marker="x", linestyle="None") plt.plot(xtest, ytest, marker="o", linestyle="None") plt.plot(xdata,tdata) plt.plot(xext, yext) plt.show() optNN=Opt.MomentumSGD() optNN.setup(NN) train_loss = [] test_loss = [] Dtrain = D//2 xtrain,xtest,ttrain,ttest = data_divide(Dtrain,D,xdata,tdata) data = [xtrain, xtest, ttrain, ttest] result = [train_loss, test_loss] learning_regression(model, optNN, data, result, 100) plot_result2(result[0], result[1], "loss function", "step", "loss function", 0.0,3) ydata = F.relu(xdata).data plt.plot(xdata,ydata) plt.show() ydata = F.sigmoid(xdata).data plt.plot(xdata,ydata) plt.show() ydata = F.sin(xdata).data plt.plot(xdata,ydata) plt.show()
試したこと
1層の時では成功していたので、def(x)の部分などを名をして数パターン試した。
補足情報(FW/ツールのバージョンなど)
python google colaboratryを使用しています。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。