0

i am writing python program that implements the NAND gate, it calculates the weight 20 times for the algorithm. but I'm getting an error "ValueError: operands could not be broadcast together with shapes (4,) (3,)" on the line where i am calculating the weight.

       
%matplotlib inline
import numpy as np
import matplotlib.pylab as plt

learningRate=0.2
epochs=5
n_inputs = 2
w=np.zeros(n_inputs+1)

training_inputs=np.array([[1, 0, 0], [1, 0, 1], [1, 1, 0],[1, 1, 1]])

d=np.array([[1,1,1,-1]])

for j in range(epochs):
    for inputs, targets in zip(training_inputs, d):
        sum=np.dot(inputs, w)
        if sum>0:
            activation = 1;
        elif sum == 0:
            activation = 0;
        else:
            activation = -1;
            
        w =w+learningRate*(targets-activation)*inputs
        print("weight= ",w)
      
fig = plt.figure();
ax=fig.add_subplot(1,1,1)
x = np.linspace(0,2.)
ax.scatter(training_inputs[:,1],training_inputs[:,2])
ax.plot(x, ((x*w[1]+w[0])/-w[2]))
ranka47
  • 995
  • 8
  • 25
  • Note: the libararies (%matplotlib inline ; import numpy as np; import matplotlib.pylab as plt) are imported – user12345216 Mar 23 '21 at 14:58
  • `print(inputs, targets)` and you should see why this error is there. – dibery Mar 23 '21 at 15:07
  • @dibery why is it a runtime warning tho ? – user12345216 Mar 23 '21 at 15:25
  • @user12345216 the shapes and the compatibility of the operation gets calculated at runtime. Hence, it is a runtime error. If you refer this answer https://stackoverflow.com/questions/24560298/python-numpy-valueerror-operands-could-not-be-broadcast-together-with-shapes and check shape and value of `inputs` and `targets` as @dibery suggested you would find the issue. `w` is incompatible with `inputs` for the operation because of the way `d` is defined. – ranka47 Mar 23 '21 at 15:45

0 Answers0