Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

python - ValueError: shapes (4,1) and (3,1) not aligned: 1 (dim 1) != 3 (dim 0)

import numpy as np


class NeuralNetwork():

    def __init__(self):
        np.random.seed(1)
        self.synapticweights = 2 * np.random.random((3,1)) - 1


    def sigmoid(self, x):
        return 1 / (1 + np.exp(-x))


    def sigmoidervative(self, x):
        return x * (1-x)

    def think(self, inputs):
        inputs = inputs.astype(float)
        output = self.sigmoid(np.dot(inputs, self.synapticweights))

        return output


if __name__ == '__main__':

    neuralnetwork = NeuralNetwork()

    print("Random synaptic weights")
    print(neuralnetwork.synapticweights)

    """Defining Training Input"""
    traininginputs = np.array([[0, 0, 1],
                               [1, 1, 1],
                               [1, 0, 1],
                               [0, 1, 1]])

    '''training output'''
    trainingoutput = np.array([[0, 1, 1, 0]]).T

    neuralnetwork.train(traininginput, trainingoutput, 5000)

    print("Synaptic Weight After Training: ")
    print(neuralnetwork.synapticweights)

    a = str(input("Input 1: "))
    b = str(input("Input 2: "))
    c = str(input("Input 3: "))

    print("Now situation: input data = ", a, b, c)
    print("Output data: ")
    print(neuralnetwork.think(np.array([a, b, c])))

Traceback:

Traceback (most recent call last):
  File "/Users/mirzasamibaig/Documents/PycharmProjects/pythonProjects/lib/python3.6/site-packages/mlproject/neuralnet.py", line 50, in <module>
    neuralnetwork.train(trainingoutput, trainingoutput, 5000)
  File "/Users/mirzasamibaig/Documents/PycharmProjects/pythonProjects/lib/python3.6/site-packages/mlproject/neuralnet.py", line 21, in train
    actualoutput = self.think(traininginput)
  File "/Users/mirzasamibaig/Documents/PycharmProjects/pythonProjects/lib/python3.6/site-packages/mlproject/neuralnet.py", line 28, in think
    output = self.sigmoid(np.dot(inputs, self.synapticweights))
ValueError: shapes (4,1) and (3,1) not aligned: 1 (dim 1) != 3 (dim 0)

I know what the error says 4 by 1 matix can only be multiply by 4 by 1 or equivalent with the same rows. i tried to change the self.synapticweights = 2 * np.random.random((3,1)) - 1 to (4,1) to make it work but i still get the same error. How should i control synapticweights What am i doing wrong? Need help.

question from:https://stackoverflow.com/questions/66057403/valueerror-shapes-4-1-and-3-1-not-aligned-1-dim-1-3-dim-0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have it the wrong way round

When multiplying matricies, you need to have the 2 inner values the same. so for a (A, B) matrix and a (C, D) matrix, in order to be able to multiply them, B needs to equal C.

in your case, you have a (4,1) and a (3,1). and because 1 != 3, you cannot multiply them. the second matrix needs to be a (1,n) matrix. This is why it says they are not aligned.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...