"name 'self' is not defined"



我正在读塔里克·拉希德(Tariq Rashid(的《制作自己的神经网络》(Make Your Own Neural Network(一书。

这是我的代码:

导入数字

class neuralNetwork:
    def _init_(self,inputnodes,hiddennodes,outputnodes,learningrate): 
        self.inodes=inputnodes  
        self.hnodes=hiddennodes
        self.onodes=outputnodes
        self.lr=learningrate
        pass
    def train():
        pass
    def query():
        pass
self.wih=(numpy.random.rand(self.hnodes,self.inodes)-0.5)
self.who=(numpy.random.rand(self.onodes,self.hnodes)-0.5)

它会产生此错误:

NameError: name 'self' is not defined

我做错了什么?

您需要首先使用以下命令安装软件包:

pip3 install numpy 

在你的 shell 上(我假设你使用 Python 3(。

在你需要在顶部写下你的代码之后:

import numpy

编辑:在谷歌上快速搜索,我发现这个:

class neuralNetwork:
    # initialise the neural network:
    def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
        #set number of nodes in each input, hidden, output layer:
        self.inodes = inputnodes #why can't we immediately use the inputnodes?
        self.hnodes = hiddennodes
        self.onodes = outputnodes
        #Setting the weights:
        self.wih = np.random.normal(0.0, pow(self.hnodes, -0.5),(self.hnodes,self.inodes))
        self.who = np.random.normal(0.0, pow(self.onodes, -0.5),(self.onodes, self.hnodes))    
        #learning rate:
        self.lr = learningrate
        #activation function:
        self.activation_function = lambda x: scipy.special.expit(x) 
        pass

您应该首先检查 Python 教程并注意缩进。

最新更新