本篇内容介绍了“C语言扩展怎么实现”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
Extending torch.autograd 扩展torch.autograd Adding operations to autograd requires implementing a new Function subclass for each operation. Recall that Function s are what autograd uses to compute the results and gradients, and encode the operation history. Every new function requires you to implement 2 methods: 向autograd自动梯度中添加操作需要我们为每个操作实现一个新的Function子类. 我们知道,autograd使用Function来计算结果和计算梯度,并且编码操作历史. 每一个新的function需要我们实现两个方法: forward() - the code that performs the operation. It can take as many arguments as you want, with some of them being optional, if you specify the default values. All kinds of Python objects are accepted here. Tensor arguments that track history (i.e., with requires_grad=True) will be converted to ones that don’t track history before the call, and their use will be registered in the graph. Note that this logic won’t traverse lists/dicts/any other data structures and will only consider Tensor s that are direct arguments to the call. You can return either a single Tensor output, or a tuple of Tensor s if there are multiple outputs. Also, please refer to the docs of Function to find descriptions of useful methods that can be called only from forward(). forward()方法 - 执行操作的代码.它可以接收你需要的任意数量的参数,如果你指定默认 值,可以将其中部分设置成可选参数.这里可以接收任意Python对象. 跟踪历史的(即requires_grad=True) 张量Tensor参数在该函数调用之前将会被转换成 不跟踪历史的张量,并且他们的使用会被登记注册到计算图中.注意这个逻辑不会遍历 列表/字典/以及其他任何数据结构,并且只会作用于作为参数直接传递给该函数调用的 张量. 你可以返回单个张量Tensor作为函数输出,或者返回张量构成的元组作为函数的 多个输出.同时你可以参阅Function文档, 在文档中你可以找到更多信息,它们介绍了一 些只能在forward()函数中使用的好用的方法. backward() - gradient formula. It will be given as many Tensor arguments as there were outputs, with each of them representing gradient w.r.t. that output. It should return as many Tensor s as there were inputs, with each of them containing the gradient w.r.t. its corresponding input. If your inputs didn’t require gradient (needs_input_grad is a tuple of booleans indicating whether each input needs gradient computation), or were non-Tensor objects, you can return None. Also, if you have optional arguments to forward() you can return more gradients than there were inputs, as long as they’re all None. backward() - 梯度公式. 这个方法接收一定数量的Tensor张量参数,参数的数量,就是这 个运算操作的输出数据数量(即前向传递函数输出数据的数量),并且这个函数接收的参 数就是相对于输出数据(前向传递的输出数据)的梯度. 该方法也返回一定数量的 Tensor张量参数,参数的数量就是输入数据(前向传递的输入数据,也就是forward函数接 收参数的数量)的数量,并且它的值是相对于输入数据的梯度.如果你的数据不需要梯度 (needs_input_grad是一个布尔类型构成的元组,他表示输入的每个数据是否需要计算梯 度), 或者是非张量的对象,你可以返回None. 同样,如果有可选参数传递到forward(),那 么你可以返回比输入数据更多数量的梯度,只要把他们设置成None即可. Below you can find code for a Linear function from torch.nn, with additional comments: 以下内容你可以看到torch.nn库中Linear 函数的代码:
# Inherit from Function# 继承Functionclass LinearFunction(Function):# Note that both forward and backward are @staticmethods# 注意forward方法和backward方法都需要用@staticmethod来装饰@staticmethod# bias is an optional argument# bias 是可选参数def forward(ctx, input, weight, bias=None):ctx.save_for_backward(input, weight, bias)output = input.mm(weight.t())if bias is not None:output += bias.unsqueeze(0).expand_as(output)return output# This function has only a single output, so it gets only one gradient# 该函数只有单个输出,因此他只会接收一个梯度@staticmethoddef backward(ctx, grad_output):# This is a pattern that is very convenient - at the top of backward# unpack saved_tensors and initialize all gradients w.r.t. inputs to# None. Thanks to the fact that additional trailing Nones are# ignored, the return statement is simple even when the function has# optional inputs.# 这是一个非常方便的模式,在backward函数开头解包saved_tensors# 然后初始化相对于输入的梯度,将他们设置成None# 由于尾部多余的None值会被忽略,因此尽管函数有可选参数,# 返回语句依然很简单.input, weight, bias = ctx.saved_tensors grad_input = grad_weight = grad_bias = None# These needs_input_grad checks are optional and there only to# improve efficiency. If you want to make your code simpler, you can# skip them. Returning gradients for inputs that don't require it is# not an error.# if ctx.needs_input_grad[0]:grad_input = grad_output.mm(weight)if ctx.needs_input_grad[1]:grad_weight = grad_output.t().mm(input)if bias is not None and ctx.needs_input_grad[2]:grad_bias = grad_output.sum(0).squeeze(0)return grad_input, grad_weight, grad_bias
“C语言扩展怎么实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。