博客
关于我
LibTorch实现LeNet
阅读量:800 次
发布时间:2023-01-31

本文共 1474 字,大约阅读时间需要 4 分钟。

L i b T o r c h 实现 L e N e t LibTorch实现LeNet LibTorch实现LeNet

#include
#include
#include
using namespace std;///
/// LeNet实现类/// class LeNet :public torch::nn::Module { public: // 构造器 LeNet(int num_classes,int num_linear); // 前向传播 torch::Tensor forward(torch::Tensor x);private: // 具体实现放到构造器实现中 torch::nn::Conv2d conv1{ nullptr}; torch::nn::Conv2d conv2{ nullptr}; torch::nn::Linear fc1{ nullptr }; torch::nn::Linear fc2{ nullptr}; torch::nn::Linear fc3{ nullptr};};LeNet::LeNet(int num_classes, int num_linear){ conv1 = register_module("conv1", torch::nn::Conv2d(torch::nn::Conv2dOptions(3, 6, 5))); conv2 = register_module("conv2", torch::nn::Conv2d(torch::nn::Conv2dOptions(6, 16, 5))); fc1 = register_module("fc1", torch::nn::Linear(torch::nn::LinearOptions(num_linear, 128))); fc2 = register_module("fc2", torch::nn::Linear(torch::nn::LinearOptions(128, 32))); fc3 = register_module("fc3", torch::nn::Linear(torch::nn::LinearOptions(32, num_classes)));}torch::Tensor LeNet::forward(torch::Tensor x){ auto out = torch::relu(conv1->forward(x)); out = torch::max_pool2d(out, 2); out = torch::relu(conv2(out)); out = torch::max_pool2d(out, 2); out = out.view({ 1, -1 }); out = torch::relu(fc1(out)); out = torch::relu(fc2(out)); out = fc3(out); return out;}int main(){ //step0:定义使用cuda auto device = torch::Device(torch::kCUDA, 0); // step1:生成测试数据 auto input = torch::ones({ 1,3,224,224}); cout << input.sizes() <

在这里插入图片描述

转载地址:http://xwwfk.baihongyu.com/

你可能感兴趣的文章
NIO_通道之间传输数据
查看>>
NIO三大组件基础知识
查看>>
NIO与零拷贝和AIO
查看>>
NIO同步网络编程
查看>>
NIO基于UDP协议的网络编程
查看>>
NIO笔记---上
查看>>
Vue3.0中的响应式原理(第九课)
查看>>
NIO蔚来 面试——IP地址你了解多少?
查看>>
NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
查看>>
NISP国家信息安全水平考试,收藏这一篇就够了
查看>>
NIS服务器的配置过程
查看>>
NIS认证管理域中的用户
查看>>
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NiuShop开源商城系统 SQL注入漏洞复现
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>
NLog类库使用探索——详解配置
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
NLP 时事和见解【2023】
查看>>
NLP 模型中的偏差和公平性检测
查看>>