资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何进行Function函数的分析

这期内容当中小编将会给大家带来有关如何进行Function函数的分析,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

创新互联公司长期为千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为达坂城企业提供专业的成都网站制作、成都做网站,达坂城网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。

原文以及翻译:

Function 函数
torch.autograd.Function
Records 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函数的分析了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


分享名称:如何进行Function函数的分析
文章链接:http://cdkjz.cn/article/jgishg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220