mxnet深度学习(Symbol)

mxnet深度学习(Symbol)


自动标志化区分

NDArray是一个基础的计算单元在MXNet里面的。除此之外,MXNet提供一个标志化的接口,叫做Symbol,为了简化构造神经网络。标志化结合了灵活性和效率。一方面,它是和Caffe里面的神经网络构造是相似的和CXXNet,另一方面,标志化还定义Theano里面的计算图源。


基本的标志组成

下面的代码创造了2层感知器。

>>> import mxnet as mx
>>> net = mx.symbol.Variable('data')
>>> net = mx.symbol.FullyConnected(data=net, name='fc1', num_hidden=128)
>>> net = mx.symbol.Activation(data=net, name='relu1', act_type="relu")
>>> net = mx.symbol.FullyConnected(data=net, name='fc2', num_hidden=64)
>>> net = mx.symbol.SoftmaxOutput(data=net, name='out')
>>> type(net)
<class 'mxnet.symbol.Symbol'>

每个标志带有一个特定的名字。Variable经常定义为输入,或者空的变量。其它的标志把一个标志(data)作为输入,同时还可以接收其它的假设参数比如隐藏神经元的个数或者激活类型。

这个标志可以简单可见的以一个函数和几个函数参数,不过它们的名字都是随机生成的。我们可以用下面的语句来看

>>> net.list_arguments()
['data', 'fc1_weight', 'fc1_bias', 'fc2_weight', 'fc2_bias', 'out_label']


我们可以看到,这些参数是被每个symbol所需要的


data:variable data里面所需要的数据

fc1_weighted和fc1_bias:与第一层fc1相连的权重和偏执项。

fc2_weighted和fc2_bias:与第一层fc2相连的权重和偏执项。

out_label:损失(函数)所需要的标签


我们也可以显示指定这些自动的名字:

>>> net = mx.symbol.Variable('data')
>>> w = mx.symbol.Variable('myweight')
>>> net = mx.symbol.FullyConnected(data=net, weight=w, name='fc1', num_hidden=128)
>>> net.list_arguments()
['data', 'myweight', 'fc1_bias']

更加复杂的构造

MXNet提供了优化好的标志(src/operator)对于深度学习里面常用层级。我们也能简单的定义新的操作在python里面。下面的例子第一次执行了一个元素级加法操作在两个层级之间,然后把他们传给完全连接的操作。

>>> lhs = mx.symbol.Variable('data1')
>>> rhs = mx.symbol.Variable('data2')
>>> net = mx.symbol.FullyConnected(data=lhs + rhs, name='fc1', num_hidden=128)
>>> net.list_arguments()
['data1', 'data2', 'fc1_weight', 'fc1_bias']
我们也可以生成一个标志以另一个灵活的方式而不是像前面的类似一条龙的服务。

>>> net = mx.symbol.Variable('data')
>>> net = mx.symbol.FullyConnected(data=net, name='fc1', num_hidden=128)
>>> net2 = mx.symbol.Variable('data2')
>>> net2 = mx.symbol.FullyConnected(data=net2, name='net2', num_hidden=128)
>>> composed_net = net(data=net2, name='compose')
>>> composed_net.list_arguments()
['data2', 'net2_weight', 'net2_bias', 'compose_fc1_weight', 'compose_fc1_bias']
在上面的例子里面,net是用来应用于一个存在的标志,然后结果的composed_net将会取代net里面原来的data,通过net2.(
 composed_net = net(data=net2, name='compose')

模型参数推断

在下面,我们将推断所有的需要作为输入数据的模型的参数

>>> net = mx.symbol.Variable('data')
>>> net = mx.symbol.FullyConnected(data=net, name='fc1', num_hidden=10)
>>> arg_shape, out_shape, aux_shape = net.infer_shape(data=(100, 100))
>>> dict(zip(net.list_arguments(), arg_shape))
{'data': (100, 100), 'fc1_weight': (10, 100), 'fc1_bias': (10,)}
>>> out_shape
[(100, 10)]
我们可以看一下net.infer_shape函数的功能

infer_shape(self, *args, **kwargs) method of mxnet.symbol.Symbol instance
    Infer the shape of outputs and arguments of given known shapes of arguments.
    
    User can either pass in the known shapes in positional way or keyword argument way.
    Tuple of Nones is returned if there is not enough information passed in.
    An error will be raised if there is inconsistency found in the known shapes passed in.
    
    Parameters
    ----------
    *args :
        Provide shape of arguments in a positional way.
        Unknown shape can be marked as None
    
    **kwargs :
        Provide keyword arguments of known shapes.
    
    Returns
    -------
    arg_shapes : list of tuple or None

这个模型推断将被用来作为一个调试机制来检测模型的不一致。


绑定标志并且运行

现在我们可以绑定空的标志,来实行前向传播和后向传播的操作。bind这个函数将创建一个Executor(用来执行真实的计算)

>>> # define computation graphs
>>> A = mx.symbol.Variable('A')
>>> B = mx.symbol.Variable('B')
>>> C = A * B
>>> a = mx.nd.ones(3) * 4
>>> b = mx.nd.ones(3) * 2
>>> # bind the symbol with real arguments
>>> c_exec = C.bind(ctx=mx.cpu(), args={'A' : a, 'B': b})
>>> # do forward pass calclation.
>>> c_exec.forward()
>>> c_exec.outputs[0].asnumpy()
[ 8.  8.  8.]
对于神经网络,一个更常用的使用模式是simple_bind,这个将会创建所有的参数数组。接下去你将会调用forward,和backward(如果梯度需要的话)来得到梯度。

>>> # define computation graphs
>>> net = some symbol
>>> texec = net.simple_bind(data=input_shape)
>>> texec.forward()
>>> texec.backward()


最后

 model API是一个简单的对标志执行器的封装来支持神经网络的训练。


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值