简体   繁体   English

朱莉娅的简单神经网络

[英]Simple neural network in Julia

I am try to rewrite code from this tutorial on python to julia and getting unexpected result - [0.5; 0.5; 0.5; 0.5] 我尝试将本教程中的python代码重写为julia,并得到意外的结果- [0.5; 0.5; 0.5; 0.5] [0.5; 0.5; 0.5; 0.5] [0.5; 0.5; 0.5; 0.5] I look to the line again and again but not see difference. [0.5; 0.5; 0.5; 0.5]我一遍又一遍地看那条线,但看不到差异。

Python code: Python代码:

from numpy import exp, array, random, dot
training_set_inputs = array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
training_set_outputs = array([[0, 1, 1, 0]]).T
random.seed(1)
synaptic_weights = 2 * random.random((3, 1)) - 1
for iteration in xrange(10000):
    output = 1 / (1 + exp(-(dot(training_set_inputs, synaptic_weights))))
    synaptic_weights += dot(training_set_inputs.T, (training_set_outputs - output) * output * (1 - output))
print 1 / (1 + exp(-(dot(array([1, 0, 0]), synaptic_weights))))

My julia code: 我的茱莉亚代码:

function activate(x)
    return 1./(1+exp(-x))
end

function g_activate(x)
    return x.*(1-x)
end

function test(iter)

Input = [0 0 1;0 1 1;1 0 1;1 1 1]
TInput = transpose(Input)
Test = [0, 1, 1, 0]
Weights = 2 * rand(3, 1) - 1

for i in 1:iter

output = activate(Input*Weights)
error = Test - output
delta = error.*g_activate(output)
Weights +=  TInput*delta

end

println(activate(Input*Weights))
end

What I am doing wrong and how do it more idiomatic way in Julia 我做错了什么以及如何在Julia中使用更惯用的方式

You are using wrong input data in Julia code. 您在Julia代码中使用了错误的输入数据。 To match the Python example 匹配Python示例

Input = [0 0 1;0 1 1;1 0 1;1 1 1]

should be 应该

Input = [0 0 1;1 1 1;1 0 1;0 1 1]

That's what I'm getting with corrected input: 那就是我得到正确输入的结果:

julia> test(10000)
[0.00966854; 0.992117; 0.993589; 0.00786553]

And if I'm running Python code with training_set_inputs = array([[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]]) I'm getting [ 0.5] . 如果我正在使用training_set_inputs = array([[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]])运行Python代码得到[ 0.5]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM