命令式和符號式混合程式設計
阿新 • • 發佈:2019-01-06
# 命令式和符號式程式設計 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,sym from mxnet.gluon import nn,loss as gloss def get_net(): net = nn.HybridSequential() net.add(nn.Dense(256,activation='relu'), nn.Dense(128,activation='relu'), nn.Dense(2)) net.initialize() return net net = get_net() X = nd.random.normal(shape=(1,512)) print(net(X)) # 通過net.hybridize()來編譯和優化HybridSequential例項中的串聯層的計算 net.hybridize() print(net(X)) # 對比 import time def benchmark(net, x): start = time.time()for i in range(1000): _ = net(x) nd.waitall() return time.time() - start net = 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,loss from mxnet import nd,autograd class 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))