実現したいこと
『ゼロから作るDeepLearnig』をcolabで実装したい
- p76の手書き数字認識に関して
前提
(例)
ゼロからをcolabで実装しているのですが、エラーが出てしまいます。
調べた限り以下のサイトが見つかったのですが、これをどうすればいいのかわからなくて
https://marshmallow444.github.io/tech_blog/2021/08/09/zero-tsuku-chpt3.html
発生している問題・エラーメッセージ
#54行でエラーが出ています shapes (784,) and (2,3) not aligned: 784 (dim 0) != 2 (dim 0)
該当のソースコード
Python
1# googleドライブへの接続(テキストに記載なし)2from google.colab import drive 3drive.mount('/content/drive')4# パスの指定(テキストに記載なし)5import sys 6sys.path.append('/content/drive/My Drive/zero_to_deep-learning/dataset')7from mnist import load_mnist 8from PIL import Image 9from IPython.display import display 10 11def img_show(img):12 pil_img = Image.fromarray(np.uint8(img))13 display(pil_img)14 15(x_train, t_train), (x_test,y_test) = load_mnist(flatten=True, normalize=False)16 17img = x_train[0]18label = t_train[0]19print(label)20print(img.shape)21img = img.reshape(28,28)22print(img.shape)23 24img_show(img)25 26def get_data():27 (x_train, t_train), (x_test, y_test) = load_mnist(normalize=True, flatten=True, one_hot_label=False) 28 return x_test, y_test 29def init_neteork():30# pickleファイルに格納されているファイルに格納されているsample_weight.pklに保存された学習済み重みパラメータを読み込む31 with open("sample_weight.pkl", 'rb')as f:32 network = pickle.load(f)33 return network 34 35def predict(nework, x): 36 W1, W2, W3 = network['W1'], network['W2'], network['W3']37 b1, b2, b3 = network['b1'], network['b2'], network['b3']38 39 a1 = np.dot(x, W1) + b1 40 z1 = sigmoid(a1)41 a2 = np.dot(z1, W2) +b2 42 z2 = sigmoid(a2)43 a3 = np.dot(z2, W3) +b3 44 y = softmax(a3)45 46 return y 47 48# 性能を評価49x, t = get_data()50network = init_network()51 52accuracy_cnt = 053for i in range(len(x)):54 y = predict(network, x[1])55# 最も確率の高い要素のインデックスを取得56 p = np.argmax(y)57 if p == t[i]:58 accuracy_cnt += 159 60print("Accuracy:" + str(float(accuracy_cnt)/len(x)))
試したこと
https://marshmallow444.github.io/tech_blog/2021/08/09/zero-tsuku-chpt3.html
このサイトに書いてある、いろいろなところに挿入して試したのですが成功しませんでした。
shapeが違くて、行列の計算ができないためにエラーが出ているというところは理解できていますが、
どの行列と行列が違うのか、どうすれば行列のshapeを変換できるのかが分かりません。
補足情報(FW/ツールのバージョンなど)
ゼロつくは第8版です。
基本的にはゼロつく通りにコードを書いています。
環境はmacbookのM1で、safariを使用しています。

0 コメント