0
$\begingroup$

This is my code to train a sigmoid neuron for Iris dataset .

convergence = np.Infinity
    
    temp_y_hat = np.zeros((80,1))
    alpha = 1
    epochs = 0
    
    
    for i in range(100):
        temp_y_hat = np.zeros((80,1))
        for i in range(X_train.shape[0]):
            
            print("W : ",W.shape)
            print("X_train :",X_train.iloc[[i]].shape)
                       
            #res = np.dot(W,np.reshape(X_train.iloc[i].values,(-1,1))) + b
            res =np.dot(W,X_train.iloc[[i]].T) + b
            
            z = 1/(1 + np.exp(-res))
            
            if z >= 0.5:
                temp_y_hat[i] = 1
            elif z < 0.5:
                temp_y_hat[1] = 0
        
            if y_train[i]==temp_y_hat[i]:
                continue
            else:
                if y_train[i]==1 and temp_y_hat[i]==0:
                    #W = W + alpha*(np.reshape(X_train.iloc[i].values,(1,4)))
                    W = W + alpha*X_train.iloc[[i]]
                    b = b + alpha
                elif y_train[i]==0 and temp_y_hat[i]==1:
                    #W = W - alpha*(np.reshape(X_train.iloc[i].values,(1,4)))
                    W = W - alpha*X_train.iloc[[i]]
                    b = b - alpha
        
        
        convergence =  np.sum(np.square(np.subtract(y_train,temp_y_hat)))
        
        print("convergence ",convergence)
        epochs = epochs + 1
        
    print("epochs ",epochs)
    
                    
        
    return W , b 


When I run this code , I am getting the following error :

if z >= 0.5:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

When I print out the shape of W and X_train.iloc[[i]] in each iteration : I get the following output which is fine in the first 4 iterations whereas in the 5th iteration , the shape of matrix W becomes wrong and hence I get the above error .

1st iteration : W :  (1, 4)
X_train : (1, 4)
2nd iteration : W :  (1, 4)
X_train : (1, 4)
3rd iteration : W :  (1, 4)
X_train : (1, 4)
4th iteration : W :  (1, 4)
X_train : (1, 4)
5th iteration : W :  (2, 4)  --- wrong shape 
X_train : (1, 4)

I am suspecting the problem is in the segment where I'm updating the value of W .

 if y_train[i]==1 and temp_y_hat[i]==0:
                    #Commented W = W + alpha*(np.reshape(X_train.iloc[i].values,(1,4)))
                    W = W + alpha*X_train.iloc[[i]]
                    b = b + alpha
                elif y_train[i]==0 and temp_y_hat[i]==1:
                    # Commented W = W - alpha*(np.reshape(X_train.iloc[i].values,(1,4)))
                    W = W - alpha*X_train.iloc[[i]]
                    b = b - alpha

But , given that , W is (1,4) and X_train.iloc[[i]] is also(1,4) , and that I take transpose of X_train.iloc[[i]] in np.dot , Why am I getting this error ??

Can someone please help me understand the problem and get rid of this problem ?

$\endgroup$
8
  • $\begingroup$ dump numpy. use pytorch. $\endgroup$
    – truth
    Commented Aug 28, 2020 at 6:52
  • $\begingroup$ So this error won't go away? $\endgroup$
    – Bharathi
    Commented Aug 28, 2020 at 6:54
  • $\begingroup$ Hi. no need to switch to pytorch, don't worry. What about this line : temp_y_hat[1] = 0, shouldn't it be temp_y_hat[i] = 0 ? This could cause the error $\endgroup$
    – 16Aghnar
    Commented Aug 28, 2020 at 9:03
  • 1
    $\begingroup$ Well, If I were you I'd add many print(...) to see the shapes and type of each object, at each computation step... $\endgroup$
    – 16Aghnar
    Commented Aug 28, 2020 at 11:25
  • 1
    $\begingroup$ @Bharathi , I am not saying that. I am just providing you with a suggestion. I have found PyTorch to be much more convenient for calculations related to tensors. It is very well-designed and nicely optimized. $\endgroup$
    – truth
    Commented Aug 28, 2020 at 15:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.