 # Calculating Loss Function in Sigmoid Neuron

Code:

``````def calc_loss(X,Y,w_est,b_est):
loss = 0
for x,y in zip(X,Y):
loss += (y - sigmoid(X,w_est,b_est))**2
return loss

W = np.linspace(-1,1,100)
B = np.linspace(-1,1,100)
WW,BB = np.meshgrid(W,B)
loss =np.zeros(WW.shape)

for i in range (WW.shape):
for j in range (WW.shape):
loss[i,j ] = calc_loss(X, Y, WW[i,j], BB[i,j]
``````

ValueError Traceback (most recent call last)
in
1 for i in range (WW.shape):
2 for j in range (WW.shape):
----> 3 loss[i,j ] = calc_loss(X, Y, WW[i,j], BB[i,j])

ValueError: setting an array element with a sequence.

I tried fixing this with one of the earlier suggested ways :

``````loss =np.zeros(WW.shape, dtype='object')

for i in range (WW.shape):
for j in range (WW.shape):
loss[i,j ] = calc_loss(X, Y, WW[i,j], BB[i,j])

from mpl_toolkits import mplot3d
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(WW,BB,loss,cmap='YlOrRd')
ax.set_xlabel('w_est')
ax.set_ylabel('b_est')
ax.set_zlabel('Loss')
``````

But this gave another error :

ValueError Traceback (most recent call last)
in
2 fig = plt.figure()
3 ax = plt.axes(projection=‘3d’)
----> 4
ax.plot_surface(WW,BB,loss,cmap=‘YlOrRd’)
5 ax.set_xlabel(‘w_est’)
6 ax.set_ylabel(‘b_est’)

ValueError: Collections can only map rank 1 arrays``

Can anyone please suggest some way of fixing either of these errors to plot the Loss Function.

Hey @Prateek_Mantri,
Can you please take a look at the shape of WW, BB, loss ?

`print(WW.shape,BB.shape,Loss.shape)`
(100, 100) (100, 100) (100, 100)

It’s due to the reason that the plot needs to be fed with numpy array.
Try referring to the solution from this article, let me know if the problem persists.