代码
#include <torch/script.h>
#include <ATen/ATen.h>
#include <torch/nn/module.h>
#include <torch/nn/modules/batchnorm.h>
#include <torch/nn/modules/conv.h>
#include <torch/nn/modules/dropout.h>
#include <torch/nn/modules/embedding.h>
#include <torch/nn/modules/functional.h>
#include <torch/nn/modules/linear.h>
#include <torch/nn/modules/sequential.h>
#include <torch/optim.h>
#include <torch/types.h>
#include <torch/utils.h>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <memory>
#include <random>
#include <vector>
using namespace std;
using namespace at;
using namespace torch::nn;
using namespace torch::optim;
int main(int argc, const char* argv[])
{
auto x = torch::randn({2, 3, 5, 5}, torch::requires_grad()); # N * C * H * W
torch::nn::Conv2d model(Conv2dOptions(3, 2, {3,3}).stride(2).padding(1));
auto y = model->forward(x);
std::cout << y.sizes() << endl;
std::cout<< "ok\n";
return 1;
}
编译
make
./bin/demo
结果
[2, 2, 3, 3]
ok
本文介绍了一个使用PyTorch库实现的卷积神经网络(Convolutional Neural Network,CNN)示例,展示了如何创建并运行一个简单的CNN模型。通过随机生成的数据输入,演示了模型的前向传播过程,并输出了模型处理后的结果尺寸。
2702

被折叠的 条评论
为什么被折叠?



