1

Ive been working on some code recently for a project in python and im confused by the output im getting from this code

def sigmoid(input_matrix):
    rows = input_matrix.shape[0]
    columns = input_matrix.shape[1]
    for i in range(0,rows):
        for j in range(0,columns):
            input_matrix[i,j] = (1 / (1 + math.exp(-(input_matrix[i,j]))))
    return input_matrix

def feed_forward(W_L_1 , A_L_0 , B_L_1):
    weighted_sum = np.add(np.dot(W_L_1,A_L_0), B_L_1)
    activation = sigmoid(weighted_sum)
    return [weighted_sum,activation]

a = np.zeros((1,1))
b = feed_forward(a,a,a)
print(b[0])
print(b[1])

when I print both b[0] and b[1] give values of .5 even though b[0] should equal 0. Also in addition to this when I place the '''weighted_sum = np.add(np.dot(W_L_1,A_L_0), B_L_1)''' again after the 'actvation' line it provides the correct result. Its as if the 'activation' line has changed the value of the weighted sum value. Was wondering if anyone could spread some light on this I can work around this but am interested as to why this is happening. Thanks!!

1
  • your sigmoid function changes the array you pass to it. input_matrix[i,j] = ... why did you expect it not to change Commented Jan 22, 2021 at 16:18

1 Answer 1

1

Inside sigmoid, you're changing the value of the matrix passed as parameter, in this line:

input_matrix[i,j] = ...

If you want to prevent this from happening, create a copy of the matrix before calling sigmoid, and call it like this: sigmoid(copy_of_weighted_sum).

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the idea I've gave this a try but still got the same result!
My recommendation has to work. How did you copy the matrix?
def sigmoid(input_matrix): test = input_matrix rows = test.shape[0] columns = test.shape[1] for i in range(0,rows): for j in range(0,columns): test[i,j] = (1 / (1 + math.exp(-(input_matrix[i,j])))) return test sorry about the formatting
I was under the impression too and I could be very wrong about this that the scope of the variable was only within that function and wasnt passed down to an inner function ie:def inner(a): a = 3 def outer(): a = 4 inner(a) print(a) outer()gives 4 as the result
Here's the problem, this line is not copying the matrix, at all! test = input_matrix. That's just creating another reference pointing to the exact, same object. You've been passing around and modifying a single matrix, and of course any change you do on it will be reflected on all the variables referencing it.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.