質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1953閲覧

行列積エラーが生じるが、どこが原因か分からない。

ques346

総合スコア47

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/06/08 12:35

編集2021/06/08 14:01

python

1from google.colab import drive 2drive.mount('/content/drive') 3 4import sys 5import numpy as np 6import matplotlib.pyplot as plt 7 8sys.path.append('/content/drive/My Drive') 9 10import ActivationFunction as AF 11 12from PIL import Image 13from IPython.display import display 14 15img = Image.open("drive/My Drive/mnist_dataset/rei.jpeg") 16img = img.resize((100, 100)) 17img = np.asarray(img) 18 19size = 5 20 21v_split = img.shape[0] // size 22h_split = img.shape[1] // size 23out_img = [] 24[out_img.extend(np.hsplit(h_img, h_split)) 25 for h_img in np.vsplit(img, v_split)] 26 27plt.subplot(17, 6, 1).imshow(out_img[1]) 28plt.subplot(17, 6, 2).imshow(out_img[2]) 29plt.subplot(17, 6, 3).imshow(out_img[3]) 30plt.subplot(17, 6, 4).imshow(out_img[4]) 31plt.subplot(17, 6, 5).imshow(out_img[5]) 32plt.subplot(17, 6, 6).imshow(out_img[6]) 33plt.subplot(17, 6, 7).imshow(out_img[7]) 34plt.subplot(17, 6, 8).imshow(out_img[8]) 35plt.subplot(17, 6, 9).imshow(out_img[9]) 36plt.subplot(17, 6, 10).imshow(out_img[10]) 37 38def extract(x, y): 39 # カラー画像の時Gだけ抜き取りたい 40 if len(x.shape) == 3: 41 h, w, ch = x.shape 42 43 # RGBのGだけ抜き取りたい 44 return x[:,:,y] 45 46v_max, v_min = 300, 200 47 48def diff(x): 49 imgrows, lenrows, imgcolumns, lencolumns = [], [], [], [] 50 for (img, imgt) in zip(x, x.T): 51 rows = img[(v_min<img)&(v_max>img)] 52 columns = imgt[(v_min<imgt)&(v_max>imgt)] 53 imgrows.append(rows) 54 lenrows.append(len(rows)) 55 imgcolumns.append(columns) 56 lencolumns.append(len(columns)) 57 return lenrows + lencolumns 58 59out_data_list0 = [] 60 61out_data_list0.append([0] + diff(extract(out_img[0], 1)) + diff(extract(out_img[0], 2)) + diff(extract(out_img[0], 0))) 62 63out_data_list1 = [] 64 65out_data_list1.append([0] + diff(extract(out_img[1], 1)) + diff(extract(out_img[1], 2)) + diff(extract(out_img[1], 0))) 66 67out_data_list2 = [] 68 69out_data_list2.append([0] + diff(extract(out_img[2], 1)) + diff(extract(out_img[2], 2)) + diff(extract(out_img[2], 0))) 70 71out_data_list3 = [] 72 73out_data_list3.append([0] + diff(extract(out_img[3], 1)) + diff(extract(out_img[3], 2)) + diff(extract(out_img[3], 0))) 74 75out_data_list4 = [] 76 77out_data_list4.append([0] + diff(extract(out_img[4], 1)) + diff(extract(out_img[4], 2)) + diff(extract(out_img[4], 0))) 78 79out_data_list5 = [] 80 81out_data_list5.append([0] + diff(extract(out_img[5], 1)) + diff(extract(out_img[5], 2)) + diff(extract(out_img[5], 0))) 82 83out_data_list6 = [] 84 85out_data_list6.append([0] + diff(extract(out_img[6], 1)) + diff(extract(out_img[6], 2)) + diff(extract(out_img[6], 0))) 86 87out_data_list7 = [] 88 89out_data_list7.append([0] + diff(extract(out_img[7], 1)) + diff(extract(out_img[7], 2)) + diff(extract(out_img[7], 0))) 90 91out_data_list8 = [] 92 93out_data_list8.append([0] + diff(extract(out_img[8], 1)) + diff(extract(out_img[8], 2)) + diff(extract(out_img[8], 0))) 94 95out_data_list9 = [] 96 97out_data_list9.append([0] + diff(extract(out_img[9], 1)) + diff(extract(out_img[9], 2)) + diff(extract(out_img[9], 0))) 98 99out_data_list10 = [] 100 101out_data_list10.append([0] + diff(extract(out_img[10], 1)) + diff(extract(out_img[10], 2)) + diff(extract(out_img[10], 0))) 102 103out_data_list11 = [] 104 105out_data_list11.append([0] + diff(extract(out_img[11], 1)) + diff(extract(out_img[11], 2)) + diff(extract(out_img[11], 0))) 106 107out_data_list12 = [] 108 109out_data_list12.append([0] + diff(extract(out_img[12], 1)) + diff(extract(out_img[12], 2)) + diff(extract(out_img[12], 0))) 110 111out_data_list13 = [] 112 113out_data_list13.append([0] + diff(extract(out_img[13], 1)) + diff(extract(out_img[13], 2)) + diff(extract(out_img[13], 0))) 114 115out_data_list14 = [] 116 117out_data_list14.append([0] + diff(extract(out_img[14], 1)) + diff(extract(out_img[14], 2)) + diff(extract(out_img[14], 0))) 118 119out_data_list15 = [] 120 121out_data_list15.append([0] + diff(extract(out_img[15], 1)) + diff(extract(out_img[15], 2)) + diff(extract(out_img[15], 0))) 122 123# 見本データに対しても同様に 124# exについて同様に 125training_data_list = [] 126 127for i in range(len(out_img)): 128 #g #b #r 抽出後diffしてappend 129 training_data_list.append([i] + diff(extract(out_img[i], 1)) + diff(extract(out_img[i], 2)) + diff(extract(out_img[i], 0))) # 略 130 131print("training_data_list" ,training_data_list) 132print("training_data_list[1:]" ,training_data_list[1:]) 133print(len(training_data_list)) 134 135# 3層ニューラルネットワーク 136class ThreeLayerNetwork: 137 # コンストラクタ 138 def __init__(self, inodes, hnodes, onodes, lr): 139 # 各レイヤーのノード数 140 self.inodes = inodes 141 self.hnodes = hnodes 142 self.onodes = onodes 143 144 # 学習率 145 self.lr = lr 146 147 # 重みの初期化 148 self.w_ih = np.random.normal(0.0, 1.0, (self.hnodes, self.inodes)) 149 self.w_ho = np.random.normal(0.0, 1.0, (self.onodes, self.hnodes)) 150 151 # 活性化関数 152 self.af = AF.sigmoid 153 self.daf = AF.derivative_sigmoid 154 155 # 誤差逆伝搬 156 def backprop(self, idata, tdata): 157 158 # 縦ベクトルに変換 159 o_i = np.array(idata, ndmin=2).T 160 t = np.array(tdata, ndmin=2).T 161 162 # 隠れ層 163 np.set_printoptions(threshold=10000) 164 x_h = np.dot(self.w_ih, o_i) 165 o_h = self.af(x_h) 166 167 # 出力層 168 x_o = np.dot(self.w_ho, o_h) 169 o_o = self.af(x_o) 170 171 # 誤差計算 172 e_o = (t - o_o) 173 e_h = np.dot(self.w_ho.T, e_o) 174 175 # 重みの更新 176 self.w_ho += self.lr * np.dot((e_o * self.daf(o_o)), o_h.T) 177 self.w_ih += self.lr * np.dot((e_h * self.daf(o_h)), o_i.T) 178 179 180 # 順伝搬 181 def feedforward(self, idata): 182 # 入力のリストを縦ベクトルに変換 183 o_i = np.array(idata, ndmin=2).T 184 185 # 隠れ層 186 x_h = np.dot(self.w_ih, o_i) 187 o_h = self.af(x_h) 188 189 # 出力層 190 x_o = np.dot(self.w_ho, o_h) 191 o_o = self.af(x_o) 192 193 return o_o 194 195if __name__=='__main__': 196 # パラメータ 197 #inodes=784から30に変更 198 inodes = 30 199 hnodes = 100 200 onodes = 400 201 lr = 0.3 202 203 # ニューラルネットワークの初期化 204 nn = ThreeLayerNetwork(inodes, hnodes, onodes, lr) 205 206 # 学習 207 epoch = 50 208 for e in range(epoch): 209 print('#epoch ', e) 210 data_size = len(training_data_list) 211 for i in range(data_size): 212 if i % 1000 == 0: 213 print(' train: {0:>5d} / {1:>5d}'.format(i, data_size)) 214 idata = (np.array(training_data_list[i][1:]) / 255.0 * 0.99) + 0.01 215 # 変更の余地あり 216 tdata = np.zeros(onodes) + 0.01 217 tdata[training_data_list[i][0]] = 0.99 218 nn.backprop(idata, tdata) 219 pass 220 pass 221 222 # テスト 223 scoreboard = [] 224 for record in test_data_list: 225 idata = (np.array(out_data_list0[1:]) / 255.0 * 0.99) + 0.01 226 predict = nn.feedforward(idata) 227 plabel = np.argmax(predict) 228 print("predict" ,predict) 229 print("plabel" ,plabel) 230 pass 231 232 scoreboard_array = np.asarray(scoreboard) 233 print('performance: ', scoreboard_array.sum() / scoreboard_array.size)

出力一部
Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
training_data_list [[0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], [1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], [2中略 5, 5, 5, 5, 5, 5, 5, 5], [398, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], [399, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]]
400
中略

エラーは
x_h = np.dot(self.w_ih, o_i)
ここで出るのですが、

# パラメータ #inodes=784から30に変更 inodes = 30 hnodes = 100 onodes = 400 lr = 0.3

ここは、前に作ったプログラムから数値の変更点を変更しただけなので、他が問題ですかね・・・。

エラー詳細は、

ValueError Traceback (most recent call last)
<ipython-input-22-1bf6b94b7030> in <module>()
224 for record in test_data_list:
225 idata = (np.array(out_data_list0[1:]) / 255.0 * 0.99) + 0.01
--> 226 predict = nn.feedforward(idata)
227 plabel = np.argmax(predict)
228 print("predict" ,predict)

<ipython-input-22-1bf6b94b7030> in feedforward(self, idata)
184
185 # 隠れ層
--> 186 x_h = np.dot(self.w_ih, o_i)
187 o_h = self.af(x_h)
188

<array_function internals> in dot(*args, **kwargs)

ValueError: shapes (60,30) and (0,1) not aligned: 30 (dim 1) != 0 (dim 0)

です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

meg_

2021/06/08 13:27

エラー詳細はどうなっていますか?
meg_

2021/06/09 00:10

エラーを見ると、idataが原因のように思われます。
jbpb0

2021/06/09 01:27

idata = (np.array(out_data_list0[1:]) / 255.0 * 0.99) + 0.01 ↓ idata = (np.array(out_data_list0[0][1:]) / 255.0 * 0.99) + 0.01 かな
ques346

2021/06/09 03:56 編集

なるほど!それで一度やってみます! エラーありませんでした!出力結果は、 plabel 398 performance: nan /usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:233: RuntimeWarning: invalid value encountered in double_scalars でした。
guest

回答1

0

ベストアンサー

python

1idata = (np.array(out_data_list0[1:]) / 255.0 * 0.99) + 0.01

↓ 修正

python

1idata = (np.array(out_data_list0[0][1:]) / 255.0 * 0.99) + 0.01

で、

ValueError: shapes (60,30) and (0,1) not aligned: 30 (dim 1) != 0 (dim 0)

は出なくなると思います

投稿2021/06/09 07:50

jbpb0

総合スコア7651

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問