Pytorch C++ 隨機分佈與定義變數
阿新 • • 發佈:2019-01-05
pytorch c++ 環境搭建,點選
#include <torch/script.h> #include <ATen/ATen.h> #include <iostream> #include <memory> using namespace std; using namespace at; int main(int argc, const char* argv[]) { at::Tensor one = at::ones({2,2}, at::kInt); std::cout<< "at::Tensor one is\n" << one << std::endl; std::cout<< "at::Tensor one size is " << one.sizes() << std::endl; std::cout<< "at::Tensor one dtype is " << one.dtype() << std::endl; std::cout<< "------------------------" << std::endl; at::Tensor zero = at::zeros({2,2}, at::kInt); std::cout<< "at::Tensor zero is\n" << zero << std::endl; std::cout<< "at::Tensor zero size is " << zero.sizes() << std::endl; std::cout<< "at::Tensor zero dtype is " << zero.dtype() << std::endl; std::cout<< "------------------------" << std::endl; torch::Tensor t_one = at::ones({2,2}, torch::kInt); std::cout<< "torch::Tensor one is\n" << t_one << std::endl; std::cout<< "torch::Tensor one size is " << t_one.sizes() << std::endl; std::cout<< "torch::Tensor one dtype is " << t_one.dtype() << std::endl; std::cout<< "------------------------" << std::endl; torch::Tensor t_zero = at::zeros({2,2}, torch::kInt); std::cout<< "torch::Tensor zero is\n" << t_zero << std::endl; std::cout<< "torch::Tensor zero size is " << t_zero.sizes() << std::endl; std::cout<< "torch::Tensor zero dtype is " << t_zero.dtype() << std::endl; std::cout<< "------------------------" << std::endl; torch::Tensor gaussian = torch::randn({2,2}, torch::kFloat32); //標準的高斯分佈 std::cout<< "torch::Tensor gaussian is\n" << gaussian << std::endl; std::cout<< "torch::Tensor gaussian size is " << gaussian.sizes() << std::endl; std::cout<< "torch::Tensor gaussian dtype is " << gaussian.dtype() << std::endl; std::cout<< "------------------------" << std::endl; torch::Tensor uniform = torch::rand({2,2}, torch::kFloat32); //0-1的隨機分佈 std::cout<< "torch::Tensor uniform is\n" << uniform << std::endl; std::cout<< "torch::Tensor uniform size is " << uniform.sizes() << std::endl; std::cout<< "torch::Tensor uniform dtype is " << uniform.dtype() << std::endl; std::cout<< "------------------------" << std::endl; auto c = gaussian + uniform; std::cout<< "torch::Tensor c is\n" << c << std::endl; std::cout<< "torch::Tensor c size is " << c.sizes() << std::endl; std::cout<< "torch::Tensor c dtype is " << c.dtype() << std::endl; std::cout<< "------------------------" << std::endl; std::cout<< "ok\n"; return 1; }
編譯並執行
make clean
make
./bin/demo
輸出結果
[email protected]:~/pytorch_c++/test$ ./bin/demo at::Tensor one is 1 1 1 1 [ CPUIntType{2,2} ] at::Tensor one size is [2, 2] at::Tensor one dtype is int ------------------------ at::Tensor zero is 0 0 0 0 [ CPUIntType{2,2} ] at::Tensor zero size is [2, 2] at::Tensor zero dtype is int ------------------------ torch::Tensor one is 1 1 1 1 [ CPUIntType{2,2} ] torch::Tensor one size is [2, 2] torch::Tensor one dtype is int ------------------------ torch::Tensor zero is 0 0 0 0 [ CPUIntType{2,2} ] torch::Tensor zero size is [2, 2] torch::Tensor zero dtype is int ------------------------ torch::Tensor gaussian is 0.4986 -0.4284 -0.0741 -0.7684 [ Variable[CPUFloatType]{2,2} ] torch::Tensor gaussian size is [2, 2] torch::Tensor gaussian dtype is float ------------------------ torch::Tensor uniform is 0.7840 0.3166 0.1904 0.2926 [ Variable[CPUFloatType]{2,2} ] torch::Tensor uniform size is [2, 2] torch::Tensor uniform dtype is float ------------------------ torch::Tensor c is 1.2827 -0.1118 0.1163 -0.4759 [ Variable[CPUFloatType]{2,2} ] torch::Tensor c size is [2, 2] torch::Tensor c dtype is float ------------------------ ok