如何设置requires_grad_为false(冻结?)PyTorch惰性层?

mw3dktmi  于 2023-03-02  发布在  其他
关注(0)|答案(1)|浏览(184)

PyTorch提供了惰性层,例如torch.nn.LazyLinear。我想冻结网络中的一些层,即确保它们不采用渐变步骤。当我尝试.requires_grad_(False)时,收到错误:
ValueError: Attempted to use an uninitialized parameter in <method 'requires_grad_' of 'torch._C._TensorBase' objects>. This error happens when you are using a惰性模块or explicitly manipulating焊炬.nn.参数.未初始化参数objects. When using LazyModules Call正向with a dummy batch to initialize the parameters before calling torch functions
如何冻结惰性层?

6yt4nkrj

6yt4nkrj1#

正如错误消息所建议的,您需要在惰性模块上执行伪推理,以便完全初始化它的所有参数。
换句话说,考虑正确的输入形状shape

>>> model(torch.rand(shape))

只有这样,才能使用requires_grad_(False)冻结所需的层。

相关问题