2017-03-03 100 views
0

我尝试在Python从这个tutorial重写代码朱莉娅并得到意想不到的结果简单的神经网络 - [0.5; 0.5; 0.5; 0.5]我看行连连,但看不出差别。在朱莉娅

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)))) 

我朱莉娅代码:

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 

什么我做错了,如何做到这一点的朱莉娅

更地道的方式
+0

@khelwood,是的。它是矩阵的转置。 – CamFerry

+0

这很有道理。 – khelwood

+0

我编辑与另一种语法。 – CamFerry

回答

4

您正在使用错误的输入数据朱莉娅代码。为了配合Python的例子

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

应该

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

这就是我与校正输入获得:

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

而且如果我有training_set_inputs = array([[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]])运行Python代码我m到处[ 0.5]

+0

谢谢。我必须更注重细节。 – CamFerry