在Julia中使用MXNet的神经网络示例

时间:2017-03-14 11:59:41

标签: neural-network julia mxnet

我正在尝试构建一个可以解决xor问题的神经网络。我的代码如下:

using MXNet
using Distributions
using PyPlot

xor_data = zeros(4,2)
xor_data[1:0] = 1
xor_data[1:1] = 1
xor_data[2:0] = 1
xor_data[2:1] = 0
xor_data[3:0] = 0
xor_data[3:1] = 1
xor_data[4:0] = 0
xor_data[4:1] = 0

xor_labels = zeros(4)
xor_labels[1] = 0
xor_labels[2] = 1
xor_labels[3] = 1
xor_labels[4] = 0

batchsize = 4
trainprovider = mx.ArrayDataProvider(:data => xor_data, batch_size=batchsize, shuffle=true, :label => xor_labels)
evalprovider = mx.ArrayDataProvider(:data => xor_data, batch_size=batchsize, shuffle=true, :label => xor_labels)

data = mx.Variable(:data)
label = mx.Variable(:label)
net = @mx.chain     mx.Variable(:data) =>
                    mx.FullyConnected(num_hidden=2) =>
                    mx.Activation(act_type=:relu) =>
                    mx.FullyConnected(num_hidden=2) =>
                    mx.Activation(act_type=:relu) =>
                    mx.FullyConnected(num_hidden=1) =>
                    mx.Activation(act_type=:relu) =>

model = mx.FeedForward(net, context=mx.cpu())
optimizer = mx.SGD(lr=0.01, momentum=0.9, weight_decay=0.00001)
initializer = mx.NormalInitializer(0.0,0.1)
eval_metric = mx.MSE()

mx.fit(model, optimizer, initializer, eval_metric, trainprovider, eval_data = evalprovider, n_epoch = 100)
mx.fit(model, optimizer, eval_metric, trainprovider, eval_data = evalprovider, n_epoch = 100)

但我收到以下错误:

  

LoadError:AssertionError:标签中的样本数不匹配   有数据   表达式从#ArrayDataProvider#6428(:: Int64,   :: Bool,:: Int64,:: Int64,:: Type {T},:: Pair {Symbol,Array {Float64,2}},   ::在io.jl:324 in中配对{符号,数组{Float64,1}})   (:: Core。#kw#Type)(:: Array {Any,1},:: Type {MXNet.mx.ArrayDataProvider},   ::对{符号,数组{Float64,2}},::对{符号,数组{Float64,1}})at   :在loading.jl:441中的include_string(:: String,:: String)中为0   在sys.dylib中的include_string(:: String,:: String):?在   eval.jl中的include_string(:: Module,:: String,:: String):32 in   (:: Atom。## 59#62 {String,String})()at eval.jl:81 in   utils.jl:30 in中的withpath(:: Atom。## 59#62 {String,String},:: String)   eval.jl中的withpath(:: Function,:: String):宏扩展时为46   eval.jl:79 [inlined] in(:: Atom。## 58#61 {Dict {String,Any}})()at   task.jl:60

我想向网络提供值(0或1)并获取单个值。是我的错误吗?

1 个答案:

答案 0 :(得分:1)

xor_data的维度是错误的,它应该有4列,而不是4行(顺便说一下,你没有像你想象的那样初始化它,因为Julia中的数组是从1开始索引的,而不是从0)。

查找

julia> xor_data = [ [1. 1]; [0 1]; [1 0]; [0 0] ]
4×2 Array{Float64,2}:
 1.0  1.0
 0.0  1.0
 1.0  0.0
 0.0  0.0

julia> xor_labels
4-element Array{Float64,1}:
 0.0
 1.0
 1.0
 0.0

julia> mx.ArrayDataProvider(:data => xor_data, :labels => xor_labels)
ERROR: AssertionError: Number of samples in  labels is mismatch with data
 in #ArrayDataProvider#6428(::Int64, ::Bool, ::Int64, ::Int64, ::Type{T}, ::Pair{Symbol,Array{Float64,2}}, ::Pair{Symbol,Array{Float64,1}}) at /Users/alexey/.julia/v0.5/MXNet/src/io.jl:324
 in MXNet.mx.ArrayDataProvider(::Pair{Symbol,Array{Float64,2}}, ::Pair{Symbol,Array{Float64,1}}) at /Users/alexey/.julia/v0.5/MXNet/src/io.jl:280

julia> xor_data = [ [1. 0 1 0]; [1 1 0 0] ]
2×4 Array{Float64,2}:
 1.0  0.0  1.0  0.0
 1.0  1.0  0.0  0.0

julia> mx.ArrayDataProvider(:data => xor_data, :labels => xor_labels)
MXNet.mx.ArrayDataProvider(Array{Float32,N}[
Float32[1.0 0.0 1.0 0.0; 1.0 1.0 0.0 0.0]],Symbol[:data],Array{Float32,N}[
Float32[0.0 1.0 1.0 0.0]],Symbol[:labels],4,4,false,0.0f0,0.0f0,MXNet.mx.NDArray[mx.NDArray{Float32}(2,4)],MXNet.mx.NDArray[mx.NDArray{Float32}(4,)])