作者:袁立红第_593 | 来源:互联网 | 2023-09-03 09:47
我遇到了有关对象属性的问题。如您所见,mnist_network对象具有一些必须在某些方法中使用的属性(属性:train_img,train_res,test_img,test_res)。当我调用函数test_predict(self)时,出现以下错误:AttributeError: 'mnist_network' object has no attribute 'test_img'
。你能解释一下吗?我对使用Python进行OOP相当陌生。
我使用Spyder,尝试使用“帮助”来找出属性出了什么问题,但我仅获得“没有可用的文档”。所以那没有帮助...
这是我的代码(应该可以预测手写数字):
# import keras and the MNIST dataset
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from keras.utils import np_utils
# import matplotlib to show pictures
import matplotlib.pyplot as plt
# numpy is necessary since keras uses numpy arrays
import numpy as np
class mnist_network():
def __init__(self):
""" load data,create and train model """
# load data
(X_train,y_train),(X_test,y_test) = mnist.load_data()
# flatten 28*28 images to a 784 vector for each image
num_pixels = X_train.shape[1] * X_train.shape[2]
X_train = X_train.reshape((X_train.shape[0],num_pixels)).astype('float32')
X_test = X_test.reshape((X_test.shape[0],num_pixels)).astype('float32')
# normalize inputs from 0-255 to 0-1
X_train = X_train / 255
X_test = X_test / 255
# one hot encode outputs
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
# create model
model = Sequential()
model.add(Dense(num_pixels,input_dim=num_pixels,kernel_initializer='normal',activation='relu'))
model.add(Dense(num_classes,activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
# train the model
model.fit(X_train,y_train,validation_data=(X_test,y_test),epochs=10,batch_size=200,verbose=2)
self = model
self.train_img = X_train
self.train_res = y_train
self.test_img = X_test
self.test_res = y_test
def test_all(self):
""" evaluates the success rate using all the test data """
scores = self.evaluate(self.test_img,self.test_res,verbose=0)
print("Baseline Error: %.2f%%" % (100-scores[1]*100))
def predict(self,img,show=False):
""" img has to be a 784 vector """
""" predicts the number in a picture (vector) """
if show:
# show the picture
plt.imshow(img,cmap='Greys')
plt.show()
num_pixels = img.shape[1] * img.shape[2]
# the actual number
res_number = np.argmax(self.predict(img.reshape(-1,num_pixels)),axis = 1)
return res_number[0] # res_number is a list containing one element
def test_predict(self):
""" test a random number from the test part of the data set """
index = random.randrange(0,10000) # there are 10000 images in the test part of the data set
""" the actual result stored in the data set
It's represented as a list of 10 elements one of which being 1,the rest 0 """
num_pixels = self.test_img.shape[1] * self.test_img.shape[2]
correct_res = self.test_res[index].index(1)
predicted_res = np.argmax(self.predict(self.test_img[index].reshape(-1,axis = 1)
if correct_res != predicted_res:
print("Error in predict ! \
index = ",index," predicted result = ",predicted_res," correct result = ",correct_res)
else:
print("alright")
network = mnist_network()
network.test_predict()
因为您已将self
(这是对该对象的当前实例的引用)分配给model
,它是Sequential
的实例,而它实际上没有属性{ {1}}。例如,如果您要在哪里做
test_img
然后,当您执行class mnist_network(Sequential):
def __init__(self):
self = "foo bar"
时,实际上将引用内存中保留的位置,该位置用于实例中的字符串“ foo bar”,而不是instance = mnist_network()
的实例。一般来说,您永远不会做类似的事情
class mnist_network
任何地方。这是没有道理的。我对tensorflow和keras不太熟悉,但是我怀疑您可能想做的事情更多是:
self = some_stuff
或者您真正想做的是:
class mnist_network(Sequential):
def __init__(self):
# do stuff....
如果您尝试使用Sequential类的方法,属性但想对其进行扩展,则可以使用前者-在OOP中将其称为继承。作为Sequantial对象实例,您可以获得所有好处,但是您可以在类mnist_network内自由地向其添加新的方法和属性。
稍后,如果您需要在您的班级中访问一个Sequential对象并要求他执行操作,您将稍后做。这称为合成。