mxnet-多層前向網絡
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 10 16:13:29 2018
@author: myhaspl
"""
from mxnet import nd
from mxnet.gluon import nn
class MixMLP(nn.Block):
def __init__(self, **kwargs):
# Run `nn.Block`‘s init method
super(MixMLP, self).__init__(**kwargs)
self.blk = nn.Sequential()
self.blk.add(nn.Dense(6, activation=‘relu‘),nn.Dense(4, activation=‘relu‘))
self.dense = nn.Dense(3)
def forward(self, x):
y = nd.relu(self.blk(x))
print(y)
return self.dense(y)
net = MixMLP()
print net
net.initialize()
x = nd.random.uniform(shape=(5,2))
z=net(x)
print net.blk[0].weight.data()
print net.blk[1].weight.data()
print z
1、網絡層次:
MixMLP(
? (dense): Dense(None -> 3, linear)
? (blk): Sequential(
? ? (0): Dense(None -> 6, Activation(relu))
? ? (1): Dense(None -> 4, Activation(relu))
? )
)
2、第二層輸出的y
[[0.0020287 ?0. ? ? ? ? 0.00173523 0.00095254]
?[0.00282071 0. ? ? ? ? 0.00248448 0.00107639]
?[0.00274506 0. ? ? ? ? 0.00262306 0.00067603]
?[0.00266313 0. ? ? ? ? 0.0026484 ?0.00052397]
?[0.00198146 0. ? ? ? ? 0.0019231 ?0.00045017]]
<NDArray 5x4 @cpu(0)>
3、第一層權值:
[[ 0.02042518 -0.01618656]
?[-0.00873779 -0.02834515]
?[ 0.05484822 -0.06206018]
?[ 0.06491279 -0.03182812]
?[ 0.0408415 ? 0.04370362]]
<NDArray 6x2 @cpu(0)>
第二層權值:
[[ 0.00404529 -0.0028032 ? 0.00952624 -0.01501013 ?0.05958354 ?0.04705103]
?[-0.06005495 -0.02276454 -0.0578019 ? 0.02074406 -0.06716943 -0.01844618]
?[ 0.04656678 ?0.06400172 ?0.03894195 -0.05035089 ?0.0518017 ? 0.05181222]
?[ 0.06700657 -0.00369488 ?0.0418822 ? 0.0421275 ?-0.00539289 ?0.00286685]]
通過網絡最終輸出結果:
[[ 1.6364756e-05 -4.1916697e-05 ?7.9047706e-05]
?[ 1.1254386e-05 -6.3164698e-05 ?1.2381122e-04]
?[-1.1489021e-05 -7.3660660e-05 ?1.4161502e-04]
?[-2.0757943e-05 -7.7326593e-05 ?1.4498169e-04]
?[-1.1048716e-05 -5.4851542e-05 ?1.0439851e-04]]
<NDArray 5x3 @cpu(0)>
mxnet-多層前向網絡