本文共 1609 字,大约阅读时间需要 5 分钟。
在下面,我们将推断所有的需要作为输入数据的模型的参数>>> 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)]————————————————版权声明:本文为CSDN博主「奋斗路上的产品狗」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/qq_25491201/article/details/51277817
import mxnet as mxa = mx.sym.Variable('data')b = mx.sym.FullyConnected(data=a,name='fc1',num_hidden=100)data_shape = {'data':(256,64)}arg_shape,_,_ = b.infer_shape(**data_shape)# b.list_arguments() # 列出symbol中的所有参数,这里是输入以及全连接层的权值和偏置# ['data', 'fc1_weight', 'fc1_bias']print(arg_shape) # 这里展示上面三个参数的size
绑定标志并且运行
现在我们可以绑定空的标志,来实行前向传播和后向传播的操作。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()————————————————版权声明:本文为CSDN博主「奋斗路上的产品狗」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/qq_25491201/article/details/51277817