欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > Day.42

Day.42

2025/6/29 18:21:15 来源:https://blog.csdn.net/m0_64714591/article/details/148905543  浏览:    关键词:Day.42

hook函数:

import torch

import torch.nn as nn

import numpy as np

import matplotlib.pyplot as plt

torch.manual_seed(42)

np.random.seed(42)

 

张量钩子:

x = torch.tensor([2.0], requires_grad=True)

y = x ** 2

z = y ** 3

def tensor_hook(grad):

    print(f"原始梯度: {grad}")

    return grad / 2

hook_handle = y.register_hook(tensor_hook)

z.backward()

print(f"x的梯度: {x.grad}")

ho@浙大疏锦行ok_handle.remove()

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词