当前位置:主页 > 软件编程 > Python代码 >

PyTorch实现更新部分网络,其他不更新

时间:2021-03-04 13:54:07 | 栏目:Python代码 | 点击:

torch.Tensor.detach()的使用

detach()的官方说明如下:

Returns a new Tensor, detached from the current graph.
The result will never require gradient.

假设有模型A和模型B,我们需要将A的输出作为B的输入,但训练时我们只训练模型B. 那么可以这样做:

input_B = output_A.detach()

它可以使两个计算图的梯度传递断开,从而实现我们所需的功能。

您可能感兴趣的文章:

相关文章