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是一個簡單的對標志執行器的封裝來支持神經網絡的訓練。
總結
以上是生活随笔為你收集整理的mxnet深度学习(Symbol)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python小白的数学建模课-10.微分
- 下一篇: 【OpenCV 例程200篇】41. 图