C语言扩展怎么实现
发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,本篇内容介绍了"C语言扩展怎么实现"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Extending
千家信息网最后更新 2025年12月01日C语言扩展怎么实现
本篇内容介绍了"C语言扩展怎么实现"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
Extending torch.autograd 扩展torch.autogradAdding 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语言扩展怎么实现"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
函数
参数
数据
数量
梯度
张量
输出
方法
输入
更多
内容
历史
就是
w.r.t.
语言
代码
单个
对象
文档
知识
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全警示教育会议讲话稿
香港软件开发的工作
数据库关系图怎么建立
怎样保护网络安全主体
融媒网络安全
宁波基础网络技术
成都美图网络技术有限公司
国外全光网络技术
廊坊市讯峰网络技术有限公司
用友app登录不上服务器
软件开发的开票项目有哪些
南通计算机网络技术实习
主机空间数据库安装
家里web服务器怎么做域名解析
中国云服务器发展
都市天际线创建分布式服务器
虹口区工商软件开发定制价格
服务器主机不工作怎么办
当今是互联网高科技的时代
网络服务器所在地对营业地
如何连接到网站服务器
网络安全软件的使用实验报告
万得资讯金融数据库
杭州双静网络技术怎么样
黄浦租房网络安全
矿产资料储量数据库管理系统
网络安全生命周期管理
池州软件开发论坛
网络安全 国家倡导
上位机软件开发哪些语言好