情報系学生です。Pythonめちゃくちゃ初心者です。
前提・実現したいこと
プログラムをいじってる途中でエラーが出てしまい、原因がよくわからず困っています
発生している問題・エラーメッセージ
/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chainer/_environment_check.py:38: UserWarning: Accelerate has been detected as a NumPy backend library. vecLib, which is a part of Accelerate, is known not to work correctly with Chainer. We recommend using other BLAS libraries such as OpenBLAS. For details of the issue, please see https://docs.chainer.org/en/stable/tips.html#mnist-example-does-not-converge-in-cpu-mode-on-mac-os-x. Also note that Chainer does not officially support Mac OS X. Please use it at your own risk. ''') # NOQA ----------------------------------------------------------- epoch 1 Traceback (most recent call last): File "iris.py", line 99, in <module> y=model(image_batch) File "iris.py", line 56, in __call__ h1=F.dropout(F.sigmoid(self.l1(x))) File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chainer/links/connection/linear.py", line 129, in __call__ return linear.linear(x, self.W, self.b) File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chainer/functions/connection/linear.py", line 175, in linear y, = LinearFunction().apply(args) File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chainer/function_node.py", line 230, in apply self._check_data_type_forward(in_data) File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chainer/function_node.py", line 298, in _check_data_type_forward self.check_type_forward(in_type) File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chainer/functions/connection/linear.py", line 20, in check_type_forward x_type.shape[1] == w_type.shape[1], File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chainer/utils/type_check.py", line 524, in expect expr.expect() File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/chainer/utils/type_check.py", line 482, in expect '{0} {1} {2}'.format(left, self.inv, right)) chainer.utils.type_check.InvalidType: Invalid operation is performed in: LinearFunction (Forward) Expect: in_types[0].shape[1] == in_types[1].shape[1] Actual: 30 != 4
該当のソースコード
python3
1# -*- coding: utf-8 -*- 2import numpy as np 3import chainer.functions as F 4import chainer.links as L 5from chainer import cuda,Variable,optimizers,Chain 6from chainer import Function 7from sklearn.datasets import load_breast_cancer 8import chainer 9import random 10import matplotlib 11matplotlib.use("Agg") 12import matplotlib.pyplot as plt 13 14print('-----------------------------------------------------------') 15 16cancer=load_breast_cancer() 17image_train=[] 18image_test=[] 19label_train=[] 20label_test=[] 21 22image=cancer.data 23label=cancer.target 24 25N=len(cancer.data) 26 27 28perm=np.random.permutation(N) 29 30image = np.asarray(cancer.data[perm]) 31label = np.asarray(cancer.target[perm]) 32 33image_train=np.array(image[0:int(len(image)*0.8)], dtype=np.float32) 34image_test=np.array(image[int(len(image)*0.8):len(image)], dtype=np.float32) 35label_train=np.array(label[0:int(len(label)*0.8)], dtype=np.float32) 36label_test=np.array(label[int(len(label)*0.8):len(label)], dtype=np.float32) 37 38N_train=len(label_train) 39N_test=len(label_test) 40 41 42 43batchsize=1 44epoch=200 #epoch数 45 46class Model(Chain): 47 def __init__(self): 48 super(Model, self).__init__( 49 l1=L.Linear(4,30), 50 l2=L.Linear(30,20), 51 l3=L.Linear(20,10), 52 l4=L.Linear(10,3) 53 ) 54 def __call__(self,x): 55 x= chainer.Variable(x) 56 h1=F.dropout(F.sigmoid(self.l1(x))) 57 #F.dropout()を消すとdropoutなしになる 58 h2=F.dropout(F.sigmoid(self.l2(h1))) 59 #発火関数にシグモイド関数を選択 relu 60 h3=F.dropout(F.sigmoid(self.l3(h2))) 61 62 h4=self.l4(h3) 63 return h4 64 65 66model=Model() 67 68optimizer = optimizers.MomentumSGD() #optimizerの選択可能 .Adam() ,.SGD 69optimizer.setup(model) 70 71y2=[] 72y_array=[] 73 74train_result = [] 75test_result = [] 76train_result2 = [] 77test_result2 = [] 78 79sum = 1 80cul_loss=0.0 81cul_acc=0.0 82all_loss=0.0 83all_acc=0.0 84 85for epoch in range(1,epoch+1): 86 print ('epoch',epoch) 87 perm=np.random.permutation(N_train) 88 89 sum_loss=0 90 sum_accuracy=0 91 92 93 for i in range(0,N_train): 94 image_batch = np.asarray(image_train[perm[i:i+1]]) 95 label_batch = np.asarray(label_train[perm[i:i+1]]) 96 97 model.cleargrads() 98 99 y=model(image_batch) 100 101 image_batch=chainer.Variable(image_batch) 102 label_batch=chainer.Variable(label_batch) 103 104 label_batch1 = np.array(label_batch.data,dtype='int32') 105 106 loss=F.softmax_cross_entropy(y, label_batch1) 107 #誤差関数としてソフトマックス関数を選択している 108 acc=F.accuracy(y, label_batch1) 109 110 111 loss.backward() 112 optimizer.update() 113 114 sum_loss += float(cuda.to_cpu(loss.data)) * batchsize 115 sum_accuracy += float(cuda.to_cpu(acc.data)) * batchsize 116 117 118 print ('train mean loss={}, accuracy={}'.format(sum_loss / N_train, sum_accuracy / N_train)) 119 train_result.append(sum_loss/N_train) 120 train_result2.append(sum_accuracy/N_train)
試したこと
ネットでエラーメッセージやコードについて調べて見ましたが、解決策が見つかりません
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。