MXNet 符号编程

来源:互联网 发布:淘宝客服欢迎用语 编辑:程序博客网 时间:2024/04/27 15:54

构成符号:

符号对我们想要进行的计算进行了描述, 下图展示了符号如何对计算进行描述. 下图定义了符号变量 A, 符号变量 B, 生成了符号变量 C, 其中, A, B 为参数节点, C 为内部节点mxnet.symbol.Variable 可以生成参数节点, 用于表示计算时的输入.

一个 Symbol 具有的属性和方法如下图所示:

对神经网络进行配置:

一个 Symbol 的 list_argument() 属性可以用来检查计算图的输入参数; list_output() 属性可以返回此 Symbol 的所有输出! 输出的自动命名遵循了一定的规则, 具体见下面的程序!

复制代码
input = mx.sym.Variable('data')  # 生成一个符号变量,名字是可以随便取的fc1 = mx.sym.FullyConnected(data=input, num_hidden=128,name='fc1')  # 全连接层act1 = mx.sym.Activation(fc1, act_type='relu')  # 激活

type(fc1) # mxnet.symbol.Symbol, act1的类型也是这个!!!
fc1.list_outputs() # ['fc1_output'],自动在输入name属性名的后面加上"_output"作为本节点名称
fc1.list_arguments() # ['data','fc1_weight','fc1_bias'],自动生成fc1_weight,fc1_bias两个参数节点

act1.list_outputs() # ['actvation0_output'] 这个名字就不是随便起的了!!!
act1.list_arguments() # ['data','fc1_weight','fc1_bias'
复制代码

mxnet.symbol.Symbol.infer_shape(self, *args, **kwargs): 推测输入参数和输出参数的 shape, 返回一个 list of tuple; 

复制代码
a = mx.sym.Variable('A')b = mx.sym.Variable('B')c = (a + b) / 10d = c + 1input_shapes = {'A':(10,2), 'B':(10,2)}   # 定义输入的shaped.infer_shape(**input_shapes) # ([(10L, 2L), (10L, 2L)], [(10L, 2L)], [])arg_shapes, out_shapes, aux_shapes = d.infer_shape(**input_shapes)In [1]: arg_shapesOut[1]: [(10L, 2L), (10L, 2L)]In [2]: out_shapesOut[2]: [(10L, 2L)]In [3]: aux_shapesOut[3]: []
复制代码

多输入网络实例:

下面的图对一个多输入神经网络进行配置:

绑定以及执行 Symbol:

当需要对符号图进行执行时, 我们需要调用绑定函数 (bind function:*.bind) 来绑定 NDArrays(下图中的 a/b/d) 到参数节点 (argument nodes: A/B/D, 不是内部节点 C/E), 从而获得一个执行器 (Executor)

然后, 调用 Executor.Forward 便可以得到输出结果.

复制代码
A = mx.sym.Variable('A')B = mx.sym.Variable('B')C = A * BD = mx.sym.Variable('D')E = C + Da = mx.nd.empty(1)  # 生成一个维度为1的随机值b = mx.nd.ones(1)    # b等于1d = mx.nd.ones(1)executor = E.bind(ctx=mx.cpu(), args={'A':a, 'B':b, 'D':d})type(executor)  # mxnet.executor.Executorexecutor.arg_dict  # {'A': <NDArray 1 @cpu(0)>, 'B': <NDArray 1 @cpu(0)>, 'D': <NDArray 1 @cpu(0)>}executor.forward()  # [<NDArray 1 @cpu(0)>]executor.outputs[0]  # <NDArray 1 @cpu(0)>, 值呢? 还是看不到值啊???
executor.outputs[0].asnumpy() # array([ 1.], dtype=float32)
复制代码

 

一个 Executor 具有的属性和方法如下图所示:

绑定多个输出:

我们可以使用 mx.symbol.Group([]) 来将 symbols 进行分组, 然后将它们进行绑定: 下图中, A/B/D 为参数节点, C/E 为内部节点, 将 E/C 绑定为 G, 这样, E 和 C 的计算结果都可以得到! 但是建议如果我们只需要 E 的计算结果的话, 建议只绑定 E 自己, 这样更加优化!

梯度计算:

在绑定函数中, 可以指定 NDArrays 来保存梯度, 在 Executor.forward() 的后面调用 Executor.backward() 可以得到相应的梯度值.

神经网络的简单绑定接口:

有时, 将一大推的 NDArrays 传递给绑定函数是很繁琐的,Symbol.simple_bind() 这个函数可以帮助我们简化这个过程, 你仅仅需要指定输入数据的大小 (shape), 这个函数可以定位到这个参数并将其绑定为 Executor.

辅助变量:

 

参考文献:

[1] Symbolic Configuration and Execution in Pictures: http://mxnet-bing.readthedocs.io/en/latest/supported_languages/python/symbol_in_pictures.html

[2] MXNet 实战: http://www.linuxeden.com/html/news/20160413/165698.html

原创粉丝点击