ValueError:形状为(3,1)的不可广播的输出操作数与广播形状(3,4)不匹配

dpopp783

我最近开始关注YouTube上Siraj Raval的深度学习教程,但是当我尝试运行代码时出现了错误。该代码来自他的系列第二集“如何制作神经网络”。运行代码时出现错误:

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剧集中的相同代码,我仍然遇到相同的错误。

我刚开始研究人工智能,却不了解错误的含义。有人可以解释一下这是什么意思以及如何解决吗?谢谢!

第二次世界大战

更改self.synaptic_weights += adjustment

self.synaptic_weights = self.synaptic_weights + adjustment

self.synaptic_weights必须具有(3,1)的形状,并且adjustment必须具有(3,4)的形状。虽然形状是可广播的,但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]])
>>> 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python ValueError:形状为(124,1)的不可广播的输出操作数与广播形状(124,13)不匹配

RuntimeError:形状为[1,224,224]的输出与广播形状[3,224,224]不匹配

ValueError:操作数无法与形状一起广播 (1,108) (3,) (1,108)

ValueError:操作数不能与形状(3,)(100,)一起广播

ValueError:操作数不能与形状(7410,)(3,)一起广播

ValueError:操作数不能与形状(3,)(2,)一起广播

StandardScaler ValueError:操作数无法与形状一起广播 (75000,3) (50,) (75000,3)

如何修复Tensorflow中的“ ValueError:操作数无法与形状(2592,)(4,)一起广播”?

ValueError:在进行加权预测时,操作数无法与形状 (7,) (624,3) 一起广播

ValueError:操作数不能与形状(1,55)(42,)一起广播

如何解决:ValueError:操作数不能与形状(4,)(4,6)一起广播

SpaCy-ValueError:操作数不能与形状(1,2)(1,5)一起广播

ACF时间序列:ValueError:操作数不能与形状一起广播

ValueError:操作数无法与形状(720,1280)一起广播(720,1281)

ValueError:操作数无法与形状(54、54、128)(54、54、64)一起广播

ValueError:操作数不能与形状(10)一起广播(11)

ValueError:操作数不能与形状一起广播-inverse_transform- Python

ValueError:操作数不能与朴素贝叶斯分类器中的形状一起广播

ValueError:操作数无法与跨熊猫列的并置数组中的形状一起广播

预测sciklearn ValueError问题:操作数不能与形状一起广播

logistic回归和numpy:ValueError:操作数不能与形状一起广播

numpy ValueError:操作数不能与形状一起广播

Pandas Merge ValueError: 操作数无法与形状一起广播 (323,) (324,) ()

操作数不能与形状(780,1080)一起广播(780,1080,3)

操作数无法与形状一起广播 (5,2) (1,5)

ValueError:形状不匹配:无法将对象广播为单个形状

严重的LSTM(Keras,TensorFlow)ValueError:形状不匹配:无法将对象广播为单个形状

熊猫数据框,ValueError:形状不匹配:对象无法广播为单个形状

ValueError形状不匹配:无法将对象广播为单个形状