如何进行Function函数的分析
发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,这期内容当中小编将会给大家带来有关如何进行Function函数的分析,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。原文以及翻译:Function 函数torch.a
千家信息网最后更新 2025年02月04日如何进行Function函数的分析
这期内容当中小编将会给大家带来有关如何进行Function函数的分析,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
原文以及翻译:
Function 函数torch.autograd.FunctionRecords operation history and defines formulas for differentiating ops.记录操作历史,并且定义求导操作的公式.Every operation performed on Tensor s creates a new function object, that performs the computation, and records that it happened. The history is retained in the form of a DAG of functions, with edges denoting data dependencies (input <- output). Then, when backward is called, the graph is processed in the topological ordering, by calling backward() methods of each Function object, and passing returned gradients on to next Function s.作用在每个Tensor上的操作都会新创建一个新的function对象,这些function对象执行计算,并且记录计算的发生.这些历史以函数functions的有向无环图的形式保留下来.有向无环图的边表示数据的依赖关系(输入 <- 输出)(input <- output).之后,当反向传播backward被调用时,计算图会以拓扑顺序被处理执行.这个处理过程是通过调用每个Function对象的backward()方法来完成的,并且依次将返回得到的梯度传递到下一个Function对象.Normally, the only way users interact with functions is by creating subclasses and defining new operations. This is a recommended way of extending torch.autograd.一般而言,用户和functions交互的唯一方式是创建一个子类,并定义新的操作.这也是扩展torch.autograd推荐使用的方式.Each function object is meant to be used only once (in the forward pass).每个function对象只会被使用一次(在前向传播过程中).
Examples:例子
>>> class Exp(Function):>>>>>> @staticmethod>>> def forward(ctx, i):>>> result = i.exp()>>> ctx.save_for_backward(result)>>> return result>>>>>> @staticmethod>>> def backward(ctx, grad_output):>>> result, = ctx.saved_tensors>>> return grad_output * result
static backward(ctx, *grad_outputs) Defines a formula for differentiating the operation. 定义求导操作的公式. This function is to be overridden by all subclasses. 这个函数将会被所有子类所重写. It must accept a context ctx as the first argument, followed by as many outputs did forward() return, and it should return as many tensors, as there were inputs to forward(). Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. 它必须接收一个上下文ctx作为第一个参数, 然后接收forward()函数返回的所有参数, 而且它必须返回forward()函数接收的所有张量tensor. 每个参数是相对于给定输出的梯度. 并且每个返回的值都应该是相应输入的梯度. The context can be used to retrieve tensors saved during the forward pass. It also has an attribute ctx.needs_input_grad as a tuple of booleans representing whether each input needs gradient. E.g., backward() will have ctx.needs_input_grad[0] = True if the first input to forward() needs gradient computated w.r.t. the output. 我们可以使用上下文context来获取在前向传递过程中保存的张量. 它同时具有属性ctx.needs_input_grad,他是一个元素为布尔类型的元组, 布尔值表示每个输入数据是否需要梯度.举个例子, 如果forward()函数的第一个输入数据需要根据输出计算梯度, 那么backward()中的属性ctx.needs_input_grad[0] = True. static forward(ctx, *args, **kwargs) Performs the operation. 执行操作. This function is to be overridden by all subclasses. 该函数将会被所有子类所重写. It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types). 它必需接收一个上下文ctx作为第一个参数, 然后可以接着接收任意数量的参数(张量或者其他类型) The context can be used to store tensors that can be then retrieved during the backward pass. 上下文可以被用来保存张量,这样就可以在后向传递的过程中获取这些张量.
上述就是小编为大家分享的如何进行Function函数的分析了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。
函数
参数
对象
张量
梯度
分析
上下
上下文
过程
输入
子类
数据
输出
例子
公式
内容
历史
属性
布尔
方式
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
朝阳区技术软件开发服务供应
地下城与勇士软件开发
山西新一代软件开发价格
docker容器中操作数据库
重大节点网络安全保障风险点
济南大学的网络安全专业
邯郸网络技术调试
csgo怎么匹配到服务器
编写一个数据库管理系统
闵行区营销软件开发管理方法
网络安全行业蜜罐
出租服务器赚钱吗
电商系统数据库设计图
服务器售后维护工程师面试问题
环境安全网络安全手抄报
上海直销软件开发培训
名誉侵权能适用网络安全法
给服务器设置公网ip
深圳网络技术开发销售价格
幼儿园网络安全制度和规定
数据库建库主要步骤
中国互联网科技开发公司
数据库的完整性代码
es文件管理器sftp服务器
单核服务器承载量
连接java数据库
ntp时间源服务器
医院网络安全评估报告
网络游戏软件开发大数据
银易通终端服务器