# How to call this network

**URL:** <https://discuss.pytorch.org/t/how-to-call-this-network/51998>\
**Category:** C++\
**Created:** [July 30, 2019, 2:52am UTC](https://discuss.pytorch.org/t/how-to-call-this-network/51998 "2019-07-30T02:52:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CF-chen-feng-CF](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/9fc29f/32.png) [@CF-chen-feng-CF](https://discuss.pytorch.org/u/CF-chen-feng-CF)\
**Post date:** [July 30, 2019, 2:52am UTC](https://discuss.pytorch.org/t/how-to-call-this-network/51998/1 "2019-07-30T02:52:30Z")

</div>

#include “alexnet.h”

#include “modelsimpl.h”

namespace vision {  
namespace models {  
AlexNetImpl::AlexNetImpl(int64\_t num\_classes) {  
features = torch::nn::Sequential(  
torch::nn::Conv2d(  
torch::nn::Conv2dOptions(3, 64, 11).stride(4).padding(2)),  
torch::nn::Functional(modelsimpl::relu\_),  
torch::nn::Functional(modelsimpl::max\_pool2d, 3, 2),  
torch::nn::Conv2d(torch::nn::Conv2dOptions(64, 192, 5).padding(2)),  
torch::nn::Functional(modelsimpl::relu\_),  
torch::nn::Functional(modelsimpl::max\_pool2d, 3, 2),  
torch::nn::Conv2d(torch::nn::Conv2dOptions(192, 384, 3).padding(1)),  
torch::nn::Functional(modelsimpl::relu\_),  
torch::nn::Conv2d(torch::nn::Conv2dOptions(384, 256, 3).padding(1)),  
torch::nn::Functional(modelsimpl::relu\_),  
torch::nn::Conv2d(torch::nn::Conv2dOptions(256, 256, 3).padding(1)),  
torch::nn::Functional(modelsimpl::relu\_),  
torch::nn::Functional(modelsimpl::max\_pool2d, 3, 2));

classifier = torch::nn::Sequential(  
torch::nn::Dropout(),  
torch::nn::Linear(256 \* 6 \* 6, 4096),  
torch::nn::Functional(torch::relu),  
torch::nn::Dropout(),  
torch::nn::Linear(4096, 4096),  
torch::nn::Functional(torch::relu),  
torch::nn::Linear(4096, num\_classes));

register\_module(“features”, features);  
register\_module(“classifier”, classifier);  
}

torch::Tensor AlexNetImpl::forward(torch::Tensor x) {  
x = features-\>forward(x);  
x = torch::adaptive\_avg\_pool2d(x, {6, 6});  
x = x.view({x.size(0), -1});  
x = classifier-\>forward(x);

return x;  
}

} // namespace models  
} // namespace vision

---

<div class="post-metadata">

**Author:** ![yf225](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yf225/32/3418_2.png) [@yf225](https://discuss.pytorch.org/u/yf225)\
**Post date:** [July 30, 2019, 3:10am UTC](https://discuss.pytorch.org/t/how-to-call-this-network/51998/2 "2019-07-30T03:10:14Z")

</div>

You can use `vision::models::AlexNet` like any other `nn::Module`s in C++ frontend. For example: [https://github.com/pytorch/vision/blob/2287c8f2dc9dcad955318cc022cabe4d53051f65/test/test\_models.cpp#L9-L19](https://github.com/pytorch/vision/blob/2287c8f2dc9dcad955318cc022cabe4d53051f65/test/test_models.cpp#L9-L19).

---

<div class="post-metadata">

**Author:** ![CF-chen-feng-CF](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/9fc29f/32.png) [@CF-chen-feng-CF](https://discuss.pytorch.org/u/CF-chen-feng-CF)\
**Post date:** [July 30, 2019, 3:56am UTC](https://discuss.pytorch.org/t/how-to-call-this-network/51998/3 "2019-07-30T03:56:17Z")

</div>

int main() {  
torch::Tensor t = torch::ones({ 2, 3, 224, 224 });  
ResNet resnet = resnet50();  
t = resnet.forward(t);  
std::cout \<\< t.sizes() \<\< t \<\< std::endl;  
}  
This is my own writing a resnet50, by calling it, entering a tensor, and getting a result, you can print out the result. But for the network inside this link, I will not do the same operation.

---

<div class="post-metadata">

**Author:** ![CF-chen-feng-CF](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/9fc29f/32.png) [@CF-chen-feng-CF](https://discuss.pytorch.org/u/CF-chen-feng-CF)\
**Post date:** [July 31, 2019, 12:37am UTC](https://discuss.pytorch.org/t/how-to-call-this-network/51998/5 "2019-07-31T00:37:52Z")

</div>

int main() {  
torch::Tensor t = torch::ones({ 2, 3, 224, 224 });  
ResNet resnet = resnet50();  
t = resnet.forward(t);  
std::cout \<\< t.sizes() \<\< t \<\< std::endl;  
}  
This is my own writing a resnet50, by calling it, entering a tensor, and getting a result, you can print out the result. But for the network inside this link, I will not do the same operation.
