参考文档

  • Using the PyTorch C++ Frontend
  • The C++ Frontend

文章目录

  • Linear
  • Conv2d & BatchNorm2d
  • Pool & ReLU

Linear

Linear 的注册方式有两种。

第一种是冒号初始化时注册,如下

struct Net : torch::nn::Module {Net(int64_t N, int64_t M): fc(register_module("fc", torch::nn::Linear(N, M))) {b = register_parameter("b", torch::randn(M));}torch::Tensor forward(torch::Tensor input) {return fc(input) + b;}torch::nn::Linear fc;torch::Tensor b;
};

第二种是使用 nullptr 初始化,然后在构建函数内部赋值注册

struct Net : torch::nn::Module {Net(int64_t N, int64_t M) {fc = register_module("fc", torch::nn::Linear(N, M));b = register_parameter("b", torch::randn(M));}torch::Tensor forward(torch::Tensor input) {return fc(input) + b;}torch::nn::Linear fc(nullptr);torch::Tensor b;
};

Linear 的使用方式也有两种。

fc(input);

或者

fc->forward(input);

Conv2d & BatchNorm2d

Impl 意为 接口,TORCH_MODULE(Net) 可以将之改为模块持有人模型,即引用语义。具体请参考 模块所有权。

struct NetImpl : torch::nn::Module {NetImpl(int k) : conv1(torch::nn::Conv2dOptions(k, 256, 4).bias(false)),batch_norm1(256),conv2(torch::nn::Conv2dOptions(256, 128, 3).stride(2).padding(1).bias(false)),batch_norm2(128),conv3(torch::nn::Conv2dOptions(128, 64, 4).stride(2).padding(1).bias(false)),batch_norm3(64),conv4(torch::nn::Conv2dOptions(64, 1, 4).stride(2).padding(1).bias(false)){// register_module() is needed if we want to use the parameters() method later onregister_module("conv1", conv1);register_module("conv2", conv2);register_module("conv3", conv3);register_module("conv4", conv4);register_module("batch_norm1", batch_norm1);register_module("batch_norm2", batch_norm2);register_module("batch_norm3", batch_norm3);}torch::Tensor forward(torch::Tensor x) {x = torch::relu(batch_norm1(conv1(x)));x = torch::relu(batch_norm2(conv2(x)));x = torch::relu(batch_norm3(conv3(x)));x = torch::tanh(conv4(x));return x;}torch::nn::Conv2d conv1, conv2, conv3, conv4;torch::nn::BatchNorm2d batch_norm1, batch_norm2, batch_norm3;
};
TORCH_MODULE(Net);

Pool & ReLU

因为这两个模块没有参数,所以注册与否都可以,可以直接使用

x = torch::max_pool2d(/*tensor=*/x, /*kernel_size=*/3, /*stride=*/2, /*padding=*/1, /*dilation=*/1, /*ceil_mode=*/true);
x = torch::relu(/*inplace=*/true)

[LibTorch] 参数注册 模块注册相关推荐

  1. 医疗项目的一个讲解(医疗项目)搜索模块 药品添加模块 订单生成 注册模块 支付模块

    架构: 我们这个项目是由Maven搭建的项目,前后端分离,使用的springcloud微服务架构,结合了springboot搭建.前台页面使用了VUE,持久层用的是Mybatis框架,图片上传使用的f ...

  2. 【金猿技术展】模块注册机制——打造AI算法流水线

    上海人工智能实验室&商汤科技技术 本项目由上海人工智能实验室&商汤科技投递并参与"数据猿年度金猿策划活动--2021大数据产业创新技术突破榜榜单及奖项"评选. 数据 ...

  3. 为.Text加了注册模块

    为.Text源码加了注册模块,要改造的地方好多阿 转载于:https://www.cnblogs.com/evernory/archive/2004/07/28/28114.html

  4. BeeHive模块注册

    BeeHive是阿里巴巴开源的一个模块化框架,是app模块化编程的一种实现方案.都知道项目一旦大了起来单单的mvc架构就很难支撑了,由此我们需要将代码模块化处理,对业务模块进行业务分离,达到内聚.分离 ...

  5. Codeigniter 用户登录注册模块

    Codeigniter 用户登录注册模块 以下皆是基于Codeigniter + MySQL 一.要实现用户登录注册功能,首先就要和MySQL数据库连接,操作流程如下: CI中贯彻MVC模型,即Mod ...

  6. 登录及注册模块设置与流程图

    登录及注册模块设置与流程图 原文地址:http://www.cocoachina.com/design/20170320/18918.html 一.登录/注册模块流程图 1.电商&O2O类产品 ...

  7. oracle 静态监听 端口,侦听动态注册静态注册local_listener参数端口PORT

    之前都是网上看过整理的,今天看到local_listener竟然一点印象都没有,太恐怖,索性再好好整理一下,避免再次忘记. 一.什么是注册 注册就是将数据库作为一个服务注册到监听程序.客户端不需要知道 ...

  8. 如何设计登录注册模块

    大家好,我是来自IT修真院的一枚PM~~今天和大家来分享一下如何设计登录注册模块~ 一.为什么要做登录注册? 二.核心要素 三.业务闭环 四.将业务嵌入使用场景 五.用户体验需要打磨 六.更多功能 七 ...

  9. 商用短链平台_第8章_ 账号微服务注册模块+短信验证码+阿里云OSS开发实战

    商用短链平台_第8章_ 账号微服务注册模块+短信验证码+阿里云OSS开发实战 文章目录 商用短链平台_第8章_ 账号微服务注册模块+短信验证码+阿里云OSS开发实战 第八章 账号微服务注册模块+短信验 ...

最新文章

  1. 改變人生的21種好習慣
  2. 3dContactPointAnnotationTool开发日志(九)
  3. Flex DataGrid可编辑对象实现Enter跳转
  4. 【C++】 vector.erase()
  5. Taro+react开发(7)--控制跳转
  6. 若依前后端分离部署到tomcat中详细教程
  7. random是python标准库吗_python标准库介绍——27 random 模块详解
  8. 2019 The Preliminary Contest for ICPC China Nanchang National Invitational
  9. 5. Document open() 方法
  10. 神经网络与深度学习第1章:绪论 阅读提问
  11. 【Transfer Learning】泛化到未知域:域泛化 (Domain Generalization) 综述论文
  12. 汇编程序求助,window.inc报错
  13. wpf linux 开发教程 pdf,深入浅出WPF
  14. goeasy连接初始化,vue
  15. html5中按钮点击事件,javascript按钮点击事件
  16. office2019卸载组件_禁止电脑随意安装和卸载软件,用这个方法就够了
  17. python @property 解释
  18. 计算机内部数据的传输 进制,计算机内部数据加工处理和传送的形式是什么
  19. vue-element-admin安装指南
  20. MySQL国内省市直辖区

热门文章

  1. linux centos7.9中安装docker的3种方式-图解
  2. linux下的微博客户端,Linux下非官方的新浪微博客户端:WeCase(微盒),附安装方法...
  3. Fibonacci in the Pocket 模拟
  4. ## CSP 201509-2 日期计算(C语言)(100分)
  5. ROS2——Windows上的安装笔记(legacy)
  6. python爬取qq音乐周杰伦首页歌词
  7. python 绘图中设置颜色对比强烈的组合
  8. 一种编程范式:对拍编程
  9. 马虎的算式子 (枚举法)
  10. 【Spring Cloud】注册中心-Nacos