web-dev-qa-db-ja.com

ValueError:形状(3,1)の非ブロードキャスト出力オペランドは、ブロードキャスト形状(3,4)と一致しません

最近、YouTubeでSiraj Ravalのディープラーニングチュートリアルをフォローし始めましたが、コードを実行しようとしたときにエラーが発生しました。コードは、彼のシリーズの2番目のエピソードである「How To Make A Neural Network」からのものです。コードを実行すると、エラーが発生しました:

Traceback (most recent call last):
File "C:\Users\dpopp\Documents\Machine Learning\first_neural_net.py", line 66, in <module>
neural_network.train(training_set_inputs, training_set_outputs, 10000)
File "C:\Users\dpopp\Documents\Machine Learning\first_neural_net.py", line 44, in train
self.synaptic_weights += adjustment
ValueError: non-broadcastable output operand with shape (3,1) doesn't match the broadcast shape (3,4)

私は彼のコードで何度もチェックし、違いを見つけることができず、GitHubリンクからコードをコピーして貼り付けようとしました。これは私が今持っているコードです:

from numpy import exp, array, random, dot

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

        # We model a single neuron, with 3 input connections and 1 output connection.
        # We assign random weights to a 3 x 1 matrix, with values in the range -1 to 1
        # and mean 0.
        self.synaptic_weights = 2 * random.random((3, 1)) - 1

    # The Sigmoid function, which 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 + exp(-x))

    # The derivative of the Sigmoid function.
    # This is the gradient of the Sigmoid curve.
    # It indicates how confident we are about the existing weight.
    def __sigmoid_derivative(self, x):
        return x * (1 - x)

    # We train the neural network through a process of trial and error.
    # Adjusting the synaptic weights each time.
    def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):
        for iteration in range(number_of_training_iterations):
            # Pass the training set through our neural network (a single neuron).
            output = self.think(training_set_inputs)

            # Calculate the error (The difference between the desired output
            # and the predicted output).
            error = training_set_outputs - output

            # Multiply the error by the input and again by the gradient of the Sigmoid curve.
            # This means less confident weights are adjusted more.
            # This means inputs, which are zero, do not cause changes to the weights.
            adjustment = dot(training_set_inputs.T, error * self.__sigmoid_derivative(output))

            # Adjust the weights.
            self.synaptic_weights += adjustment

    # The neural network thinks.
    def think(self, inputs):
        # Pass inputs through our neural network (our single neuron).
        return self.__sigmoid(dot(inputs, self.synaptic_weights))

if __name__ == '__main__':

    # Initialize a single neuron neural network
    neural_network = NeuralNetwork()

    print("Random starting synaptic weights:")
    print(neural_network.synaptic_weights)

    # The training set. We have 4 examples, each consisting of 3 input values
    # and 1 output value.
    training_set_inputs = array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
    training_set_outputs = array([[0, 1, 1, 0]])

    # Train the neural network using a training set
    # Do it 10,000 times and make small adjustments each time
    neural_network.train(training_set_inputs, training_set_outputs, 10000)

    print("New Synaptic weights after training:")
    print(neural_network.synaptic_weights)

    # Test the neural net with a new situation
    print("Considering new situation [1, 0, 0] -> ?:")
    print(neural_network.think(array([[1, 0, 0]])))

Sirajのエピソードで機能したのと同じコードをコピーして貼り付けた後でも、同じエラーが発生します。

人工知能の調査を始めたばかりで、エラーの意味がわかりません。誰かがそれの意味とそれを修正する方法を説明してもらえますか?ありがとう!

7
dpopp783

変化する self.synaptic_weights += adjustment

self.synaptic_weights = self.synaptic_weights + adjustment

self.synaptic_weightsの形状は(3,1)でなければならず、adjustmentの形状は(3,4)でなければなりません。シェイプは broadcastable であるが、numpyはシェイプ(3,4)の結果を配列に割り当てることを好まない形状(3,1)

a = np.ones((3,1))
b = np.random.randint(1,10, (3,4))

>>> a
array([[1],
       [1],
       [1]])
>>> b
array([[8, 2, 5, 7],
       [2, 5, 4, 8],
       [7, 7, 6, 6]])

>>> a + b
array([[9, 3, 6, 8],
       [3, 6, 5, 9],
       [8, 8, 7, 7]])

>>> b += a
>>> b
array([[9, 3, 6, 8],
       [3, 6, 5, 9],
       [8, 8, 7, 7]])
>>> a
array([[1],
       [1],
       [1]])

>>> a += b
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    a += b
ValueError: non-broadcastable output operand with shape (3,1) doesn't match the broadcast shape (3,4)

numpy.add を使用し、出力配列としてaを指定すると、同じエラーが発生します

>>> np.add(a,b, out = a)
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    np.add(a,b, out = a)
ValueError: non-broadcastable output operand with shape (3,1) doesn't match the broadcast shape (3,4)
>>> 

新しいaを作成する必要があります

>>> a = a + b
>>> a
array([[10,  4,  7,  9],
       [ 4,  7,  6, 10],
       [ 9,  9,  8,  8]])
>>> 
6
wwii

うまくいけば、今までにコードを実行しているはずですが、彼のコードとあなたのコードの間の問題は次の行です:

training_output = np.array([[0,1,1,0]]).T  

転置は2つの角かっこを追加することを忘れないでくださいが、同じコードでも同じ問題がありました。ありがとう

0
user10864598