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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…