from mxnet import init , nd
from mxnet.gluon import nn

net = nn.Sequential()

net.add(nn.Dense(256, activation= "relu"),
        nn.Dense(10))

net.initialize()

X = nd.random.uniform(shape=(2,20))
Y = net(X)
# print(Y)

# print(net[0].params, type(net[0].params))
# print(net[0].params["dense0_weight"], net[0].weight)
# print(net[0].weight.data())

# print(net[0].weight.grad())

# print(net[1].bias.data())

# print(net.collect_params())

# print(net.collect_params('.*weight'))

# 我们将权重参数初始化成均值为0、标准差为0.01的正态分布随机数，偏差参数清零。
# 非首次对模型初始化需要指定force_reinit为真
# net.initialize(init = init.Normal(sigma=0.01),force_reinit=True)
# net[0].weight.data()[0]
#
# # 下面使用常数来初始化权重参数。
# net.initialize(init = init.Constant(1), force_reinit= True)
# net[0].weight.data()[0]
#
# # 特定参数进行初始化
# net[0].weight.initialize(init=init.Xavier(),force_reinit = True)
# print(net[0].weight.data()[0])

class MyIinit(init.Initializer):
    def _init_weight(self, name, data):
        print('name',name, data.shape)
        data[:] = nd.random.uniform(low=-10,high=10,shape=data.shape)
        data *= data.abs() >= 5

# net.initialize(MyIinit(),force_reinit = True)
# print(net[0].weight.data())

# 我们还可以通过Parameter类的set_data函数来直接改写模型参数

net[0].weight.set_data(net[0].weight.data()+1)

# print(net[0].weight.data()[0])

# 4.2.4. 共享模型参数
net = nn.Sequential()
shared = nn.Dense(8, activation='relu')
net.add(nn.Dense(8, activation='relu'),
        shared,
        nn.Dense(8,activation='relu',params=shared.params),
        nn.Dense(10))

net.initialize()
X = nd.random.uniform(shape=(2, 20))
net(X)

print(net[1].weight.data()[0] == net[2].weight.data()[0])
