There is an error and I don't know where to fix it, I think I should probably correct the for statement from line 55.
import sys,os
import numpy as np
sys.path.append(os.pardir)
from dataset.mnist import load_mnist
import pickle
from common.functions import sigmoid, softmax
# Create a function to output test data
def get_data():
# Read MNIST dataset
(x_train, t_train), (x_test, t_test) = load_mnist (normalize = True, flatten = True, one_hot_label = False)
# Output test data
return x_test, t_test
# Implement a function to read learned parameters
default_network():
# Loading Learned Parameters
with open("."/deep-learning-from-scratch-master/ch03/sample_weight.pkl", 'rb') asf:
network=pickle.load(f)
# Outputs a dictionary containing learned parameters
return network
# Implement a function that predicts correct answers from handwritten numbers
def predict (network, x):
# Retrieving Learned Parameters from Dictionary
W1, W2, W3 = network ['W1'], network ['W2', network ['W3']
b1, b2, b3 = network['b1'], network['b2'], network['b3']
# first-tier calculation
a1 = np.dot(x,W1) + b1# weighted sum
z1 = sigmoid(a1)# activation
# second-tier calculation
a2 = np.dot(z1,W2) + b2 # weighted sum
z2=sigmoid(a2)#activation
# third-tier calculation
a3=np.dot(z2,W3)+b3# weighted sum
y=softmax(a3)#activation
# Output inference results (output of neural network)
returny
# Get test images and labels
x,t=get_data()
# Get Learned Parameters
network=init_network
acuracy_cnt = 0
for i in range (len(x)):
y=predict(network, x[i])
p=np.argmax(y)#Get index of the most likely element
if p==t[i]:
acuracy_cnt+=1
print("Accuracy:"+str(float(accuracy_cnt)/len(x)))))
Error Contents
Traceback (most recent call last):
File "c:\deep-learning-from-scratch-master\3.6.2.py", line 56, in<module>
y=predict(network, x[i])
File "c:\deep-learning-from-scratch-master\3.6.2.py", line 30, predict
W1, W2, W3 = network ['W1'], network ['W2', network ['W3']
TypeError: 'function' object is not subscriptable
I think there are two problems.
The error itself is caused by the second reason below, and the return value of the result of calling the function should be substituted because the function object is substituted for the function object.
This is the error that leads to the error TypeError: 'function' object is not subscriptable
.
Wrong:
#Implement a function to read learned parameters
default_network():
# Loading Learned Parameters
with open("."/deep-learning-from-scratch-master/ch03/sample_weight.pkl", 'rb') asf:
network=pickle.load(f)
# Outputs a dictionary containing learned parameters
return network
Positive: return
indentation is different, so return one step
#Implement a function to read learned parameters
default_network():
# Loading Learned Parameters
with open("."/deep-learning-from-scratch-master/ch03/sample_weight.pkl", 'rb') asf:
network=pickle.load(f)
# Outputs a dictionary containing learned parameters
return network
Wrong:
# Get learned parameters
network=init_network
Positive: ()
to substitute the result of calling the function
# Get learned parameters
network=init_network()
By the way, the source code is stored here.
deep-learning-from-scratch/ch03/neuralnet_mnist.py
© 2024 OneMinuteCode. All rights reserved.