“名称'自我'未定义”

时间:2017-11-19 10:36:53

标签: python

我正在阅读Tariq Rashid撰写的“制作你自己的神经网络”一书。

这是我的代码:

import numpy

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

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

pip3 install numpy 
你的shell上的

(我假设你使用的是Python 3)。

您需要在代码顶部写下代码:

import numpy
编辑:通过快速搜索Google,我发现了这一点:

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教程,并注意缩进。