从以下函数开始说起:
def init_weights(self):
"""
If needed prunes and maybe initializes weights.
"""
# Prune heads if needed
if self.config.pruned_heads:
self.prune_heads(self.config.pruned_heads)
if _init_weights:
# Initialize weights
self.apply(self._init_weights)
# Tie weights should be skipped when not initializing all weights
# since from_pretrained(...) calls tie weights anyways
self.tie_weights()
1. prune_heads的输入是一个Dict[int, List[int]],即选一些层进行剪枝。例如{1: [0, 2], 2: [2, 3]} will prune heads 0 and 2 on layer 1 and heads 2 and 3 on layer 2
2. self.apply是nn.module的一个方法,它会递归地把每一个子module都调用self._init_weights方法。由于PreTrainedModel本身是个抽象类,所以_init_weights要由PreTrainedModel的派生类来实现
3. tie_weights(weight tying)可以翻译为权值共享或者权重绑定。主要原因有两点,一是减少了参数的数量,加速训练过程,二是tied weights可以被看做是一种正则化形式,在实践中能获得更好的性能。在 NLP 任务中,将输入嵌入和输出嵌入权重绑定是一种常见的实践。这种绑定有助于减少参数数量,并提高模型性能。然而,权重绑定并不是在所有场景下都有效。在某些情况下,权重绑定可能限制了模型的表示能力,从而导致性能下降。因此,是否应用权重绑定需要根据任务和模型的具体需求来权衡。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/105928.html