【问题标题】:Simple neural network in JuliaJulia 中的简单神经网络
【发布时间】:2017-03-04 01:12:22
【问题描述】:

我正在尝试将代码从 python 上的 tutorial 重写为 julia 并得到意想不到的结果 - [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

我做错了什么以及如何在 Julia 中更惯用的方式

【问题讨论】:

  • @khelwood,是的。它是矩阵的转置。
  • 有道理。
  • 我用另一种语法编辑。

标签: python neural-network julia


【解决方案1】:

您在 Julia 代码中使用了错误的输入数据。匹配 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 代码,我会得到 [ 0.5]

【讨论】:

    猜你喜欢
    • 2022-01-08
    • 2019-08-07
    • 1970-01-01
    • 2018-12-23
    • 2016-08-18
    • 2011-06-10
    • 2018-04-10
    • 2018-05-24
    • 1970-01-01
    相关资源
    最近更新 更多