![b034aa885a29fc372e8eb95bc3257e35.png](https://i-blog.csdnimg.cn/blog_migrate/e70a05628e28759f9dbee776379003c2.jpeg)
1. 先来看看没有任何参数的模型
class MyModel(nn.Module):
def __init__(self):
print("run __init__")
super(MyModel, self).__init__()
def get_parameter(self):
print("run output_parameter")
print(type(self.named_parameters()))
# 注意只有当模型具有参数时,self.named_paramters()迭代器才会有值
parameters_li = [w for w in self.named_parameters()]
print(parameters_li)
model = MyModel()
model.get_parameter()
输出:
run __init__
run output_parameter
<class 'generator'>
[]
2. 再来看看有参数模型
class MyModel(nn.Module):
def __init__(self, vocab_size, embedding_dim):
print("run __init__")
super(MyModel, self).__init__()
self.word_embeds = nn.Embedding(vocab_size, embedding_dim)
def output_parameter(self):
print("run output_parameter")
print(type(self.named_parameters()))
# 遍历模型参数
for name, param in self.named_parameters():
print(name, param.size())
model = MyModel(10, 3)
model.output_parameter()
输出:
run __init__
run output_parameter
word_embeds.weight tensor([[-0.1012, 0.4560, -0.6860],
[-0.7001, -0.2842, 0.8513],
[ 1.5048, -0.6218, -0.5249],
[-0.4236, -1.5744, 1.5675],
[-0.3904, 0.0451, -1.6845],
[ 0.4153, -0.0883, 1.1259],
[ 0.6372, 0.5849, 0.6626],
[-0.0312, -0.0170, 0.2092],
[-2.1321, -0.7156, -1.7835],
[-0.5531, 0.4732, -0.6747]])
3. 可以直接操作模型参数
class MyModel(nn.Module):
def __init__(self, vocab_size, embedding_dim):
print("run __init__")
super(MyModel, self).__init__()
self.word_embeds = nn.Embedding(vocab_size, embedding_dim)
def output_parameter(self):
print("run output_parameter")
# 遍历模型
for name, param in self.named_parameters():
print(name, param.data)
# 直接操作参数
print(self.word_embeds.weight)
model = MyModel(10, 3)
model.output_parameter()
输出:
run __init__
run output_parameter
word_embeds.weight tensor([[ 0.3675, 0.3408, 0.1545],
[ 0.7712, 0.5315, -0.9827],
[-0.0351, 1.9727, 0.8911],
[-0.2503, 0.6004, -0.6274],
[ 0.7397, -0.0736, 0.3195],
[-0.1690, 0.5512, -0.0648],
[-0.8347, -0.9638, 0.5015],
[-1.0703, 0.6631, 1.4699],
[ 0.9917, -1.1515, 0.3213],
[ 0.5136, -0.1434, 0.3430]])
Parameter containing:
tensor([[ 0.3675, 0.3408, 0.1545],
[ 0.7712, 0.5315, -0.9827],
[-0.0351, 1.9727, 0.8911],
[-0.2503, 0.6004, -0.6274],
[ 0.7397, -0.0736, 0.3195],
[-0.1690, 0.5512, -0.0648],
[-0.8347, -0.9638, 0.5015],
[-1.0703, 0.6631, 1.4699],
[ 0.9917, -1.1515, 0.3213],
[ 0.5136, -0.1434, 0.3430]], requires_grad=True)
再一次重复,嵌入层参数就是一个词向量矩阵,每行表示一个词,每列表示维度。
嗯,按照我老师的教诲,一时的努力不是真努力,要细水长流。