https://blog.csdn.net/pandav5/article/details/53993684

(1)Mxnet的数据格式为NDArray,当需要读取可观看的数据,就要调用:

numpy_d = d.asnumpy()
converts it to a Numpy array.

(2)list_arguments (给出当前符号d的输入变量)与list_outputs(给出符号d的输出变量)的说明

import mxnet as mx
a = mx.sym.Variable("A") # represent a placeholder. These can be inputs, weights, or anything else.
b = mx.sym.Variable("B")
c = (a + b) / 10
d = c + 1
调用list_arguments 得到的一定就是用于d计算的所有symbol
d.list_arguments()
# ['A', 'B']
调用list_outputs()得到的就是输出的名字:

d.list_outputs()
# ['_plusscalar0_output'] This is the default name from adding to scalar.,
上面在查看名称,下面教你如何查看各个层的大小

# define input shapes
inp_shapes = {'A':(10,), 'B':(10,)}
arg_shapes, out_shapes, aux_shapes = d.infer_shape(**inp_shapes)

arg_shapes # the shapes of all the inputs to the graph. Order matches d.list_arguments()
# [(10, ), (10, )]

out_shapes # the shapes of all outputs. Order matches d.list_outputs()
# [(10, )]

aux_shapes # the shapes of auxiliary variables. These are variables that are not trainable such as batch normalization population statistics. For now, they are save to ignore.
# []

关于Grad_req的使用,符号描述完后,需要bind,得到一个executor

在使用bing进行绑定,且不需要做反向递归时:

input_arguments = {}
input_arguments['A'] = mx.nd.ones((10, ), ctx=mx.cpu())
input_arguments['B'] = mx.nd.ones((10, ), ctx=mx.cpu())
executor = d.bind(ctx=mx.cpu(),
args=input_arguments, # this can be a list or a dictionary mapping names of inputs to NDArray
grad_req='null') # don't request gradients
args :指出输入的符号以及大小,以词典类型传入
grad_req : 设置为Null,说明不需要进行gradient计算

bind完之后,还需要调用一个forward(),就可以运算整个过程。当然,还可以通过executor,对输入的

变量再次进行相关的赋值。

import numpy as np
# The executor
executor.arg_dict
# {'A': NDArray, 'B': NDArray}

executor.arg_dict['A'][:] = np.random.rand(10,) # Note the [:]. This sets the contents of the array instead of setting the array to a new value instead of overwriting the variable.
executor.arg_dict['B'][:] = np.random.rand(10,)
executor.forward()
executor.outputs
# [NDArray]
output_value = executor.outputs[0].asnumpy()
executor.arg_dict['A']是NDArray类型,再使用executor.arg_dict['A'][:]=赋值,表示以numpy的值覆盖NDArray类型的值,类型依旧是NDArray;如果不加[:],表示以numpy值的array类型直接覆盖。但运算的结果却仍然是以mx.nd.ones(10,)得到的.

获取输出的结果:excutor.outputs[0].asnumpy()

本章最重要的一个环节出现了:与上面的例子的区别在于,添加了一个后向传播过程。那么就需要对grad_req = 'write' ,同时调用backforwad.

# allocate space for inputs
input_arguments = {}
input_arguments['A'] = mx.nd.ones((10, ), ctx=mx.cpu())
input_arguments['B'] = mx.nd.ones((10, ), ctx=mx.cpu())
# allocate space for gradients
grad_arguments = {}
grad_arguments['A'] = mx.nd.ones((10, ), ctx=mx.cpu())
grad_arguments['B'] = mx.nd.ones((10, ), ctx=mx.cpu())

executor = d.bind(ctx=mx.cpu(),
args=input_arguments, # this can be a list or a dictionary mapping names of inputs to NDArray
args_grad=grad_arguments, # this can be a list or a dictionary mapping names of inputs to NDArray
grad_req='write') # instead of null, tell the executor to write gradients. This replaces the contents of grad_arguments with the gradients computed.

executor.arg_dict['A'][:] = np.random.rand(10,)
executor.arg_dict['B'][:] = np.random.rand(10,)

executor.forward()
# in this particular example, the output symbol is not a scalar or loss symbol.
# Thus taking its gradient is not possible.
# What is commonly done instead is to feed in the gradient from a future computation.
# this is essentially how backpropagation works.
out_grad = mx.nd.ones((10,), ctx=mx.cpu())
executor.backward([out_grad]) # because the graph only has one output, only one output grad is needed.

executor.grad_arrays
# [NDarray, NDArray]
在调用Bind时,需要提前手动为gradient分配一个空间args_grad并且传入,同时grad_req 设置为 write。
再调用executor.forward()前向运行。

再调用excutor.backward()后向运行。输出的symbol既不是一个单量,也不是loss symbol。需要手动传入梯度。

与bind 相对的是 simple_bind,他有一个好处:不需要手动分配计算的梯度空间大小。

input_shapes = {'A': (10,), 'B': (10, )}
executor = d.simple_bind(ctx=mx.cpu(),
grad_req='write', # instead of null, tell the executor to write gradients
**input_shapes)
executor.arg_dict['A'][:] = np.random.rand(10,)
executor.arg_dict['B'][:] = np.random.rand(10,)

executor.forward()
out_grad = mx.nd.ones((10,), ctx=mx.cpu())
executor.backward([out_grad])
只需要为simple_bind 设定 输入的大小,它会自动推断梯度所需的空间大小。

一套清晰简单的网络流程就为你摆放在面前了:

import mxnet as mx
import numpy as np
# First, the symbol needs to be defined
data = mx.sym.Variable("data") # input features, mxnet commonly calls this 'data'
label = mx.sym.Variable("softmax_label")

# One can either manually specify all the inputs to ops (data, weight and bias)
w1 = mx.sym.Variable("weight1")
b1 = mx.sym.Variable("bias1")
l1 = mx.sym.FullyConnected(data=data, num_hidden=128, name="layer1", weight=w1, bias=b1)
a1 = mx.sym.Activation(data=l1, act_type="relu", name="act1")

# Or let MXNet automatically create the needed arguments to ops
l2 = mx.sym.FullyConnected(data=a1, num_hidden=10, name="layer2")

# Create some loss symbol
cost_classification = mx.sym.SoftmaxOutput(data=l2, label=label)

# Bind an executor of a given batch size to do forward pass and get gradients
batch_size = 128
input_shapes = {"data": (batch_size, 28*28), "softmax_label": (batch_size, )}
executor = cost_classification.simple_bind(ctx=mx.gpu(0),
grad_req='write',
**input_shapes)
此时executor是训练时用

# The above executor computes gradients. When evaluating test data we don't need this.
# We want this executor to share weights with the above one, so we will use bind
# (instead of simple_bind) and use the other executor's arguments.
executor_test = cost_classification.bind(ctx=mx.gpu(0),
grad_req='null',
args=executor.arg_arrays)
executor_test 是测试时用
# executor 里含有arg_dict表示每层的名称
:bias1,data,layer2_bias,layer2_weight...
#executor 里含有 arg_arrays对应每层的具体数(诀窍:带arrays的表示数值)

# initialize the weights
for r in executor.arg_arrays:
r[:] = np.random.randn(*r.shape)*0.02

# Using skdata to get mnist data. This is for portability. Can sub in any data loading you like.
from skdata.mnist.views import OfficialVectorClassification

data = OfficialVectorClassification()
trIdx = data.sel_idxs[:]
teIdx = data.val_idxs[:]
for epoch in range(10):
print "Starting epoch", epoch
np.random.shuffle(trIdx)
#每128个样本,作为一个batchsize
for x in range(0, len(trIdx), batch_size):
# extract a batch from mnist
batchX = data.all_vectors[trIdx[x:x+batch_size]]
batchY = data.all_labels[trIdx[x:x+batch_size]]

# our executor was bound to 128 size. Throw out non matching batches.
if batchX.shape[0] != batch_size:
continue
# Store batch in executor 'data'
#通过executor的 arg_dict 给予“名称”,就能获取该层的数值信息,例如设置'data',也就是赋予
#输入数据信息。一定要加上[:] ,表示overwritting
executor.arg_dict['data'][:] = batchX / 255.
# Store label's in 'softmax_label'
executor.arg_dict['softmax_label'][:] = batchY
executor.forward()
executor.backward()

#进行一次forward以及一次backward之后,需要对权值进行一次更新。
#pname表示
# do weight updates in imperative
for pname, W, G in zip(cost_classification.list_arguments(), executor.arg_arrays, executor.grad_arrays):
# Don't update inputs
# MXNet makes no distinction between weights and data.
if pname in ['data', 'softmax_label']:
continue
# what ever fancy update to modify the parameters
W[:] = W - G * .001

# Evaluation at each epoch
num_correct = 0
num_total = 0
for x in range(0, len(teIdx), batch_size):
batchX = data.all_vectors[teIdx[x:x+batch_size]]
batchY = data.all_labels[teIdx[x:x+batch_size]]
if batchX.shape[0] != batch_size:
continue
# use the test executor as we don't care about gradients
executor_test.arg_dict['data'][:] = batchX / 255.
executor_test.forward()
num_correct += sum(batchY == np.argmax(executor_test.outputs[0].asnumpy(), axis=1))
num_total += len(batchY)
print "Accuracy thus far", num_correct / float(num_total)
---------------------
作者:不良CV研究生
来源:CSDN
原文:https://blog.csdn.net/pandav5/article/details/53993684
版权声明:本文为博主原创文章,转载请附上博文链接!

转载于:https://www.cnblogs.com/jukan/p/10797356.html

固定权重 关于Mxnet的一些基础知识理解(1)相关推荐

  1. python基础知识理解

    一.概述 看了一天的python基础语法,基本对python语法有了一个大概的了解(其实之前断断续续也看过python),学习网址:Python 基础教程.因为之前我学过C++,因此在学习python ...

  2. python语法基础知识案例_python基础知识理解

    一.概述 看了一天的python基础语法,基本对python语法有了一个大概的了解(其实之前断断续续也看过python),学习网址:Python 基础教程.因为之前我学过C++,因此在学习python ...

  3. C语言指针——基础知识理解

    目录 前言: (1)内存是如何编址的? (2)如何对变量进行寻址? 直接寻址:直接到变量名标识的存储单元读取变量的值--& 间接寻址:通过存放变量地址的其他变量访问该变量(通过其他变量间接找到 ...

  4. 软考之路(1)——浅解网络基础知识

    对网络这一块的基础知识理解例如以下,以图文并茂的形式展出.便于分析和理解.解析与图例如以下: 物理层: 功能: 提供为建立.维护和拆除物理链路所需的机械.电气.功能和规程的特性: 提供有关在传输介质上 ...

  5. Windows核心编程 第六章 线程基础知识 (上)

    第6章 线程的基础知识 理解线程是非常关键的,因为每个进程至少需要一个线程.本章将更加详细地介绍线程的知识.尤其是要讲述进程与线程之间存在多大的差别,它们各自具有什么作用.还要介绍系统如何使用线程内核 ...

  6. 面试官,你考我那么多基础知识干什么?

    本文转载自公众号  大飞码字 经常有同学跟我说,很多的基础知识学过就忘, 比如操作系统.数据库.网络协议等方面的底层原理.而这些往往都是技术面试必考的内容. 每次被问到这个,我都不知怎么回答,跟他说多 ...

  7. 基础知识的学习,来自十年程序员的经验分享

    本文转载自微信公众号<大飞码字> 前面分享过一篇算法学习的文章 : 不懂算法,还想进大厂?做梦吧. 虽然有点标题党,但内容还是不错的. 后来就想能不能对计算机学习方法和学习心得写个系列的文 ...

  8. Java基础知识复习(二)

    对最近java基础知识理解不足的方面进行了巩固,写个博客记录一下!

  9. 计算机术语new一个,微机原理第一章计算机基础知识(new)

    <微机原理第一章计算机基础知识(new)>由会员分享,可在线阅读,更多相关<微机原理第一章计算机基础知识(new)(47页珍藏版)>请在人人文库网上搜索. 1.1.第一章计算机 ...

最新文章

  1. python皮同_Python OpenCV 图像的双线性插值算法,全网最细致的算法说明_橡皮擦,一个逗趣的互联网高级网虫-CSDN博客...
  2. 如何在linux下修改组权限
  3. HDFS文件系统基本文件命令、编程读写HDFS
  4. 为什么编码不同会出现乱码?
  5. libevent源码学习-----Reactor模型
  6. cesium模型不遮挡点线面_cesium点线面测试数据
  7. 利用opencv从USB摄像头获取图片
  8. php 环信easyui_环信easeui集成:坑总结2018
  9. DVB电视机顶盒工作原理
  10. 【JZOJ A组】拯救奶牛
  11. 从 radix tree 到 xarray
  12. 搞个笑?用Verilog产生一个三角波吧!
  13. python画venn图
  14. 小米10获取root权限_小米手机:刷机卡刷、线刷、root权限获取
  15. 把你的阿里巴巴图标库转成你自己的@ant-design/icons
  16. 花滑三周连跳_花滑未来能有人做五周跳吗 专家:四周半或是极限
  17. html按住语音如何看前面内容,单音语音内容.html
  18. 前后端分离项目实战(vue2.0 + SSM)
  19. 实时系统vxWorks - zynq7020移植vxWorks
  20. python中from import*的*什么意思_[Python]Python中的import和from import

热门文章

  1. vue 将字符串最后一个字符给替换_前端开发:Vue项目实战-Music
  2. 阿里开发规范_阿里开发强制要求的11条SQL编写规范
  3. ppt扇形图怎么显示数据_PPT图表除了显示数据变化,还可以干嘛?
  4. 安装MySQL数据库无法启动服务的完美解决办法
  5. 客户永远是对的---我的理解:做事不要抱怨,别为失败找借口。
  6. Go语言中命令行参数的实现
  7. RHEL下SendMail修改发邮箱地址
  8. 微软Skype即将抛弃Windows Phone 8和8.1用户
  9. 字符串的切割操作(strtok,split)
  10. 具体解释http 协议