python OOP attributeError:对象没有属性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python OOP attributeError:对象没有属性相关的知识,希望对你有一定的参考价值。

我遇到有关对象属性的问题。如您所见,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, kernel_initializer='normal', 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, num_pixels)), 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的实例,而该实例实际上没有属性test_img。例如,如果您在哪里做:

class mnist_network(Sequential):
    def __init__(self):
         self = "foo bar"

然后,当您执行instance = mnist_network()时,实际上将引用内存中保留的位置,该位置保留为在您的实例中显示“ foo bar”的字符串,而不是class mnist_network的实例。一般来说,您永远不会做类似的事情

self = some_stuff

任何地方。这是没有道理的。我对tensorflow和keras不太熟悉,但是我怀疑您可能想做的事情更多是:

class mnist_network(Sequential):
    def __init__(self):
         # do stuff....

或者也许您真正想做的是:

class mnist_network:
    def __init__(self):
         # do stuff
         self.model = Sequential()

以上是关于python OOP attributeError:对象没有属性的主要内容,如果未能解决你的问题,请参考以下文章

我遇到错误:AttributeError:'str'对象没有属性'isfloat'

Python类 - 从父类继承属性到顶层

Python - AttributeError:模块'mysql'没有属性'connector'

初学者 Python:AttributeError:'list' 对象没有属性

Python:AttributeError:'str'对象没有属性'datetime'

Python类继承:AttributeError:'[SubClass]'对象没有属性'xxx'