オライリーの「ゼロから作るDeep Learning」の3.6.2でニューラルネットワークの推論処理を行うのですが、Accuracy:0.9352となるはずのところがAccuracy:0.0001となってしまいます。カレントディレクトリはch03で、sample_weight.pklはch03の中に、mnist.pklはdatasetの中に入っています。macで行っています。
以下、ソースコードです。何が間違っているのかを教えてくださると嬉しいです。よろしくお願いします。
Python 3.6.3 |Anaconda, Inc.| (default, Oct 6 2017, 12:04:38)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import numpy as np
import pickle
import sys,os
sys.path.append(os.pardir)
from dataset.mnist import load_mnist
from PIL import Image
def sigmoid(x):
... y = 1/(1+np.exp(-x))
... return y
...
def softmax(x):
... c=np.max(x)
... exp_x=np.exp(x-c)
... sum_exp_x=np.sum(exp_x)
... y=exp_x/sum_exp_x
... return y
...
def get_data():
... (x_train, t_train), (x_test, t_test)=load_mnist(normalize=True, flatten=True, one_hot_label=False)
... return x_test, t_test
...
def init_network():
... with open("sample_weight.pkl", 'rb') as f:
... network=pickle.load(f)
... return network
...
def predict(network,x):
... W1,W2,W3=network['W1'],network['W2'],network['W3']
... b1,b2,b3=network['b1'],network['b2'],network['b3']
... a1=np.dot(x,W1)+b1
... z1=sigmoid(a1)
... a2=np.dot(z1,W2)+b2
... z2=sigmoid(a2)
... a3=np.dot(z2,W3)+b3
... y=softmax(a3)
... return y
...
x,t=get_data()
network=init_network()
accuracy_cnt=0
for i in range(len(x)):
... y=predict(network,x[i])
... p=np.argmax(y)
...
if p==t[i]:
... accuracy_cnt =+ 1
...
print("Accuracy:" +str(float(accuracy_cnt)/len(x)))
Accuracy:0.0001
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/30 10:19