# 命令式和符号式编程def add_str(): return '''def add(a,b): return a + b'''def fancy_func_str(): return '''def fancy_func(a, b, c, d): e = add(a,b) f = add(c,d) g = add(e,f) return g'''def evoke_str(): return add_str() + fancy_func_str() + '''print(fancy_func(1,2,3,4))'''prog = evoke_str()# print(prog)y = compile(prog,'','exec')# exec(y)from mxnet import nd,autograd,symfrom mxnet.gluon import nn,loss as glossdef get_net(): net = nn.HybridSequential() net.add(nn.Dense(256,activation='relu'), nn.Dense(128,activation='relu'), nn.Dense(2)) net.initialize() return netnet = get_net()X = nd.random.normal(shape=(1,512))print(net(X))# 通过net.hybridize()来编译和优化HybridSequential实例中的串联层的计算net.hybridize()print(net(X))# 对比import timedef benchmark(net, x): start = time.time() for i in range(1000): _ = net(x) nd.waitall() return time.time() - startnet = get_net()print('before hybridizing: %.4f sec' % benchmark(net,X))net.hybridize()print('after hybridizing: %.4f sec' % benchmark(net,X))# 保存参数net.export('my_mlp')x = sym.var('data')print(net(x))
from mxnet.gluon import nn,lossfrom mxnet import nd,autogradclass HybirdNet(nn.HybridBlock): def __init__(self, **kwargs): super(HybirdNet,self).__init__(**kwargs) self.hidden = nn.Dense(10) self.output = nn.Dense(2) def hybrid_forward(self, F, x, *args, **kwargs): print('F: ',F) print('x: ',x) x = F.relu(self.hidden(x)) print('hidden: ',x) return self.output(x)net = HybirdNet()net.initialize()X = nd.random.normal(shape=(1,4))print(X)print(net(X))# 编译优化net.hybridize()print(net(X))