2017-11-19 11 views
-6

私はTariq Rashidの "自分のニューラルネットワークを作る"という本を読んでいます。"name 'self'は定義されていません"

これは私のコードです:

輸入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 

何をしているのですか?

+3

私は 'Python'の基本的なチュートリアルから始まり、次に最も高度なテーマから始めたいと思います。あなたのエラーについては、' numpy'を先に読み込む必要があります。 'numpy'はすべての種類の有名なライブラリです数学的なものの – Jan

+0

あなたは 'import numpy'を使ってそれをインポートする必要がありますが、@ Janに同意します。 – DimKoim

+0

'numpy'をインポートしましたか?あなたの質問のタイトルはあなたの質問のものとは異なるエラーです。 – roganjosh

答えて

1

あなたは最初のコマンドでパッケージをインストールする必要があります:あなたのシェル上

pip3 install numpy 

(私はあなたは、Python 3を使用すると仮定)。あなたが上にあなたのコードを記述する必要が後

import numpy 

EDIT:あなたが最初のPythonのチュートリアルをチェックしなければなりません

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 

:Googleでクイック検索では、私はこれを見つけましたインデントに注意してください。

+0

私はnumpyをインストールしてインポートしましたが、 "NameError:name 'self'が定義されていません。 – justbeginner

関連する問題