代码
#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