2017-09-06 14 views
1

クイック質問人!ニューラルネットワーク(シンプル)

先日学んだ学習機械の学習を開始し、神経回路網に遭遇し、簡単な示唆を得ました。コードにエラーがないので、私はなぜ出力が印刷されないのか不思議でした。

import numpy as np 


class NN(): 
    def _init_(self): 
     # Seed random number generator, so it generates the same number 
     # every time program runs 
     np.random.seed(1) 

     # Model single neuron, with 3 input connections and 1 output connection 
     # Assign random weights to a 3x1 matrix, with values in range -1 to 1 
     # and mean of 0 
     self.synaptic_weights = 2 * np.random.random((3, 1)) - 1 

    # Describes an s shaped curve we pass the weighted sum of the inputs 
    # through this function to normalise them between 0 and 1 
    def __sigmoid(self, x): 
     return 1/(1 + np.exp(-x)) 

    # Gradient of the sigmoid curve 
    def __sigmoid_derivative(self, x): 
     return x * (1 - x) 

    def train(self, training_set_input, training_set_output, number_of_training_iterations): 
     for iteration in np.xrange(number_of_training_iterations): 
      # pass training set through neural net 
      output = self.predict(training_set_input) 

      error = training_set_output - output 

      # multiply error by input and again by the gradient of the sigmoid curve 
      adjustment = np.dot(training_set_input.T, error * self.__sigmoid_derivative(output)) 

      # Adjust the weights 
      self.synaptic_weights += adjustment 

    def predict(self, inputs): 
     # Pass inputs through neural network (single neuron) 
     return self.__sigmoid(np.dot(inputs, self.synaptic_weights)) 


if __name__ == "__NN__": 
    # init single neuron neural network 
    nn = NN() 
    weightz = nn.synaptic_weights 
    new_predict = nn.predict(np.array[1, 0, 0]) 

    print("Random starting synaptic weights") 
    print(weightz) 

    # T flips the matrix vertically 
    training_set_input = np.array([0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]) 
    training_set_output = np.array([0, 1, 0, 0]).T 

    # train network using training set 
    # do it 10,000 times and make small adjustments each time 
    nn.train(training_set_input, training_set_output, 10000) 

    print("New starting synaptic weights") 
    print(weightz) 

    # test 
    print("Predicting") 
    print(new_predict) 

ご迷惑をおかけして申し訳ありませんが、問題の原因を特定しようとしています。 NN.pyとして保存されたファイル 多くの感謝!

+0

エラーはありませんが、あなたの 'print'呼び出しはどれも動作しませんか? '__name__'はあなたが思うものだと確認しましたか? –

+0

ありがとうございます! :) – restores

答えて

1

明らかに、__name__ではなく、"__NN__"に等しい。むしろ、それは"__main__"と等しいです。ドキュメントから

__name__属性は、モジュールの完全修飾名に設定する必要があります。この名前は、インポートシステムでモジュールを一意に識別するために使用されます。

関連する問題