I’m trying to convert the python code(left) into Julia (right), python code is working fine, but Julia code gave an error at the highlighted line l1_delta = ...
:
DimensionMismatch("A has dimensions (4,1) but B has dimensions (4,1)")
Julia full code is here:
# sigmoid function
sigmoid(x::Real, derive::Bool=false) =
(derive==true) ? x*(one(x)-x) : one(x)/(one(x) + exp(-x))
x = [0 0 1; 0 1 1; 1 0 1; 1 1 1]
y = [0 0 1 1]'
syn0 = 2 * rand(3,1)
for iter in (:)(1,10000)
l0 = x
l1 = sigmoid.(l0 * syn0)
l1_error = y - l1
l1_delta = l1_error * sigmoid.(l1,true) # here I get the error
syn0 += l0' * l1_delta
end
println("Output After Training:")
l1
hasanOryx:
syn0 = 2 * rand(3,1)
syn0 = 2 * rand(3,1) - 1?
Thanks for your feedback, the error is in
l1_delta = l1_error * sigmoid.(l1,true)
Regarding your comment, see the below
You are missing a point in the subtraction:
syn0 = 2 * rand(3,1) .- 1
Thanks, but I still getting error at:
l1_delta = l1_error * sigmoid.(l1,true)
Now that I noticed the comment of the error.
The same broadcast (dot) solution:
l1_delta = l1_error .* sigmoid.(l1,true) # here I get the error
1 Like
The problem, in my opinion, is that you are doing a matrix multiplication, and in that case, both matrixes cannot have the same dimension.
Or you use the transpose of one of them or you use
l1_delta = l1_error .* sigmoid.(l1,true) # Multiply element by element
1 Like
Thanks,
So the python code:
l1_delta = l1_error * sigmoid(l1,True)
Is not a matrix multiplication, and different from the python code using np
which is generating the same error got by Julia:
l1_delta = np.dot(l1_error,sigmoid(l1,True))
Now the code is correct, thanks
Transpose can not work here as it will generate array in different dimensions, but the .*
worked fine, thanks