Getting error while building a network

Hi I am trying to build DCGAN sample from this link: https://github.com/pytorch/examples/tree/master/cpp/dcgan.
But I am getting errors like: error: ‘ConvTranspose2dOptions’ is not a member of ‘torch::nn’
Full error message is below:

[ 50%] Building CXX object CMakeFiles/dcgan.dir/dcgan.cpp.o
/home/fugurcal/dcgan/dcgan.cpp:72:6: error: ‘ConvTranspose2d’ in namespace ‘torch::nn’ does not name a type
  nn::ConvTranspose2d conv1, conv2, conv3, conv4;
      ^~~~~~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:73:6: error: ‘BatchNorm2d’ in namespace ‘torch::nn’ does not name a type
  nn::BatchNorm2d batch_norm1, batch_norm2, batch_norm3;
      ^~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp: In constructor ‘DCGANGeneratorImpl::DCGANGeneratorImpl(int)’:
/home/fugurcal/dcgan/dcgan.cpp:36:9: error: class ‘DCGANGeneratorImpl’ does not have any field named ‘conv1’
       : conv1(nn::ConvTranspose2dOptions(kNoiseSize, 256, 4)
         ^~~~~
/home/fugurcal/dcgan/dcgan.cpp:36:19: error: ‘ConvTranspose2dOptions’ is not a member of ‘torch::nn’
       : conv1(nn::ConvTranspose2dOptions(kNoiseSize, 256, 4)
                   ^~~~~~~~~~~~~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:36:19: note: suggested alternative: ‘Conv2dOptions’
       : conv1(nn::ConvTranspose2dOptions(kNoiseSize, 256, 4)
                   ^~~~~~~~~~~~~~~~~~~~~~
                   Conv2dOptions
/home/fugurcal/dcgan/dcgan.cpp:38:9: error: class ‘DCGANGeneratorImpl’ does not have any field named ‘batch_norm1’
         batch_norm1(256),
         ^~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:39:9: error: class ‘DCGANGeneratorImpl’ does not have any field named ‘conv2’
         conv2(nn::ConvTranspose2dOptions(256, 128, 3)
         ^~~~~
/home/fugurcal/dcgan/dcgan.cpp:39:19: error: ‘ConvTranspose2dOptions’ is not a member of ‘torch::nn’
         conv2(nn::ConvTranspose2dOptions(256, 128, 3)
                   ^~~~~~~~~~~~~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:39:19: note: suggested alternative: ‘Conv2dOptions’
         conv2(nn::ConvTranspose2dOptions(256, 128, 3)
                   ^~~~~~~~~~~~~~~~~~~~~~
                   Conv2dOptions
/home/fugurcal/dcgan/dcgan.cpp:43:9: error: class ‘DCGANGeneratorImpl’ does not have any field named ‘batch_norm2’
         batch_norm2(128),
         ^~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:44:9: error: class ‘DCGANGeneratorImpl’ does not have any field named ‘conv3’
         conv3(nn::ConvTranspose2dOptions(128, 64, 4)
         ^~~~~
/home/fugurcal/dcgan/dcgan.cpp:44:19: error: ‘ConvTranspose2dOptions’ is not a member of ‘torch::nn’
         conv3(nn::ConvTranspose2dOptions(128, 64, 4)
                   ^~~~~~~~~~~~~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:44:19: note: suggested alternative: ‘Conv2dOptions’
         conv3(nn::ConvTranspose2dOptions(128, 64, 4)
                   ^~~~~~~~~~~~~~~~~~~~~~
                   Conv2dOptions
/home/fugurcal/dcgan/dcgan.cpp:48:9: error: class ‘DCGANGeneratorImpl’ does not have any field named ‘batch_norm3’
         batch_norm3(64),
         ^~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:49:9: error: class ‘DCGANGeneratorImpl’ does not have any field named ‘conv4’
         conv4(nn::ConvTranspose2dOptions(64, 1, 4)
         ^~~~~
/home/fugurcal/dcgan/dcgan.cpp:49:19: error: ‘ConvTranspose2dOptions’ is not a member of ‘torch::nn’
         conv4(nn::ConvTranspose2dOptions(64, 1, 4)
                   ^~~~~~~~~~~~~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:49:19: note: suggested alternative: ‘Conv2dOptions’
         conv4(nn::ConvTranspose2dOptions(64, 1, 4)
                   ^~~~~~~~~~~~~~~~~~~~~~
                   Conv2dOptions
/home/fugurcal/dcgan/dcgan.cpp:55:29: error: ‘conv1’ was not declared in this scope
    register_module("conv1", conv1);
                             ^~~~~
/home/fugurcal/dcgan/dcgan.cpp:55:29: note: suggested alternative: ‘lconv’
    register_module("conv1", conv1);
                             ^~~~~
                             lconv
/home/fugurcal/dcgan/dcgan.cpp:56:29: error: ‘conv2’ was not declared in this scope
    register_module("conv2", conv2);
                             ^~~~~
/home/fugurcal/dcgan/dcgan.cpp:56:29: note: suggested alternative: ‘lconv’
    register_module("conv2", conv2);
                             ^~~~~
                             lconv
/home/fugurcal/dcgan/dcgan.cpp:57:29: error: ‘conv3’ was not declared in this scope
    register_module("conv3", conv3);
                             ^~~~~
/home/fugurcal/dcgan/dcgan.cpp:57:29: note: suggested alternative: ‘lconv’
    register_module("conv3", conv3);
                             ^~~~~
                             lconv
/home/fugurcal/dcgan/dcgan.cpp:58:29: error: ‘conv4’ was not declared in this scope
    register_module("conv4", conv4);
                             ^~~~~
/home/fugurcal/dcgan/dcgan.cpp:58:29: note: suggested alternative: ‘lconv’
    register_module("conv4", conv4);
                             ^~~~~
                             lconv
/home/fugurcal/dcgan/dcgan.cpp:59:35: error: ‘batch_norm1’ was not declared in this scope
    register_module("batch_norm1", batch_norm1);
                                   ^~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:60:35: error: ‘batch_norm2’ was not declared in this scope
    register_module("batch_norm2", batch_norm2);
                                   ^~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:61:35: error: ‘batch_norm3’ was not declared in this scope
    register_module("batch_norm3", batch_norm3);
                                   ^~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp: In member function ‘at::Tensor DCGANGeneratorImpl::forward(at::Tensor)’:
/home/fugurcal/dcgan/dcgan.cpp:65:32: error: ‘conv1’ was not declared in this scope
    x = torch::relu(batch_norm1(conv1(x)));
                                ^~~~~
/home/fugurcal/dcgan/dcgan.cpp:65:32: note: suggested alternative: ‘lconv’
    x = torch::relu(batch_norm1(conv1(x)));
                                ^~~~~
                                lconv
/home/fugurcal/dcgan/dcgan.cpp:65:20: error: ‘batch_norm1’ was not declared in this scope
    x = torch::relu(batch_norm1(conv1(x)));
                    ^~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:66:32: error: ‘conv2’ was not declared in this scope
    x = torch::relu(batch_norm2(conv2(x)));
                                ^~~~~
/home/fugurcal/dcgan/dcgan.cpp:66:32: note: suggested alternative: ‘lconv’
    x = torch::relu(batch_norm2(conv2(x)));
                                ^~~~~
                                lconv
/home/fugurcal/dcgan/dcgan.cpp:66:20: error: ‘batch_norm2’ was not declared in this scope
    x = torch::relu(batch_norm2(conv2(x)));
                    ^~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:67:32: error: ‘conv3’ was not declared in this scope
    x = torch::relu(batch_norm3(conv3(x)));
                                ^~~~~
/home/fugurcal/dcgan/dcgan.cpp:67:32: note: suggested alternative: ‘lconv’
    x = torch::relu(batch_norm3(conv3(x)));
                                ^~~~~
                                lconv
/home/fugurcal/dcgan/dcgan.cpp:67:20: error: ‘batch_norm3’ was not declared in this scope
    x = torch::relu(batch_norm3(conv3(x)));
                    ^~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:68:20: error: ‘conv4’ was not declared in this scope
    x = torch::tanh(conv4(x));
                    ^~~~~
/home/fugurcal/dcgan/dcgan.cpp:68:20: note: suggested alternative: ‘lconv’
    x = torch::tanh(conv4(x));
                    ^~~~~
                    lconv
/home/fugurcal/dcgan/dcgan.cpp: In function ‘int main(int, const char**)’:
/home/fugurcal/dcgan/dcgan.cpp:94:60: error: ‘struct torch::nn::ConvOptions<2>’ has no member named ‘bias’
           nn::Conv2dOptions(1, 64, 4).stride(2).padding(1).bias(false)),
                                                            ^~~~
/home/fugurcal/dcgan/dcgan.cpp:95:11: error: ‘LeakyReLU’ is not a member of ‘torch::nn’
       nn::LeakyReLU(nn::LeakyReLUOptions().negative_slope(0.2)),
           ^~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:95:25: error: ‘LeakyReLUOptions’ is not a member of ‘torch::nn’
       nn::LeakyReLU(nn::LeakyReLUOptions().negative_slope(0.2)),
                         ^~~~~~~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:95:25: note: suggested alternative: ‘GRUOptions’
       nn::LeakyReLU(nn::LeakyReLUOptions().negative_slope(0.2)),
                         ^~~~~~~~~~~~~~~~
                         GRUOptions
/home/fugurcal/dcgan/dcgan.cpp:98:62: error: ‘struct torch::nn::ConvOptions<2>’ has no member named ‘bias’
           nn::Conv2dOptions(64, 128, 4).stride(2).padding(1).bias(false)),
                                                              ^~~~
/home/fugurcal/dcgan/dcgan.cpp:99:11: error: ‘BatchNorm2d’ is not a member of ‘torch::nn’
       nn::BatchNorm2d(128),
           ^~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:99:11: note: suggested alternative: ‘BatchNorm’
       nn::BatchNorm2d(128),
           ^~~~~~~~~~~
           BatchNorm
/home/fugurcal/dcgan/dcgan.cpp:100:11: error: ‘LeakyReLU’ is not a member of ‘torch::nn’
       nn::LeakyReLU(nn::LeakyReLUOptions().negative_slope(0.2)),
           ^~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:100:25: error: ‘LeakyReLUOptions’ is not a member of ‘torch::nn’
       nn::LeakyReLU(nn::LeakyReLUOptions().negative_slope(0.2)),
                         ^~~~~~~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:100:25: note: suggested alternative: ‘GRUOptions’
       nn::LeakyReLU(nn::LeakyReLUOptions().negative_slope(0.2)),
                         ^~~~~~~~~~~~~~~~
                         GRUOptions
/home/fugurcal/dcgan/dcgan.cpp:103:63: error: ‘struct torch::nn::ConvOptions<2>’ has no member named ‘bias’
           nn::Conv2dOptions(128, 256, 4).stride(2).padding(1).bias(false)),
                                                               ^~~~
/home/fugurcal/dcgan/dcgan.cpp:104:11: error: ‘BatchNorm2d’ is not a member of ‘torch::nn’
       nn::BatchNorm2d(256),
           ^~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:104:11: note: suggested alternative: ‘BatchNorm’
       nn::BatchNorm2d(256),
           ^~~~~~~~~~~
           BatchNorm
/home/fugurcal/dcgan/dcgan.cpp:105:11: error: ‘LeakyReLU’ is not a member of ‘torch::nn’
       nn::LeakyReLU(nn::LeakyReLUOptions().negative_slope(0.2)),
           ^~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:105:25: error: ‘LeakyReLUOptions’ is not a member of ‘torch::nn’
       nn::LeakyReLU(nn::LeakyReLUOptions().negative_slope(0.2)),
                         ^~~~~~~~~~~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:105:25: note: suggested alternative: ‘GRUOptions’
       nn::LeakyReLU(nn::LeakyReLUOptions().negative_slope(0.2)),
                         ^~~~~~~~~~~~~~~~
                         GRUOptions
/home/fugurcal/dcgan/dcgan.cpp:108:61: error: ‘struct torch::nn::ConvOptions<2>’ has no member named ‘bias’
           nn::Conv2dOptions(256, 1, 3).stride(1).padding(0).bias(false)),
                                                             ^~~~
/home/fugurcal/dcgan/dcgan.cpp:109:11: error: ‘Sigmoid’ is not a member of ‘torch::nn’
       nn::Sigmoid());
           ^~~~~~~
/home/fugurcal/dcgan/dcgan.cpp:124:64: error: ‘struct torch::optim::AdamOptions’ has no member named ‘betas’; did you mean ‘beta1’?
       generator->parameters(), torch::optim::AdamOptions(2e-4).betas(std::make_tuple (0.5, 0.5)));
                                                                ^~~~~
                                                                beta1
/home/fugurcal/dcgan/dcgan.cpp:126:68: error: ‘struct torch::optim::AdamOptions’ has no member named ‘betas’; did you mean ‘beta1’?
       discriminator->parameters(), torch::optim::AdamOptions(2e-4).betas(std::make_tuple (0.5, 0.5)));
                                                                    ^~~~~
                                                                    beta1
CMakeFiles/dcgan.dir/build.make:62: recipe for target 'CMakeFiles/dcgan.dir/dcgan.cpp.o' failed
make[2]: *** [CMakeFiles/dcgan.dir/dcgan.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/dcgan.dir/all' failed
make[1]: *** [CMakeFiles/dcgan.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

How can I resolve this issue?

Which libtorch version did you install?
I assume you are just running the DCGAN example or did you modify it somehow?

I am using libtorch 1.5.0 with gpu support (cuda 10.2). I didn’t modify the example. I have also tried a small CNN with conv2d, ReLU and Maxpool2d. It returned same error for ReLU and MaxPool2d. Is it possible that error caused by broken installation of torch?

Problem solved. After changing to directory of libtorch from “/home/user/folder/libtorch” to “/home/user/libtorch”, I successfully built the network.

Hello @Fugurcali, I have the same question. where do you change the directory of libtorch ? In CMakeLists.txt or cmake command?

As far as I remember I didn’t change anything in CMakeLists.txt, the problem was caused by the directory structure of my libtorch folder. My folder structure for libtorch was like below:


├── Home
│   ├── username
│      └── libtorch
│          └── libtorch
│                ├── bin
│                ├── include
│                ├── lib
│                ├── share
                  ........

After I changed the file structure as below, the built was completed successfully:


├── Home
│   ├── username
│      └── libtorch
│         ├── bin
│         ├── include
│         ├── lib
│         ├── share
            ........

Hope this would help.

BuildingConnected alerts users when they experience a disruption to their internet connection. If our site is unable to connect with your internet, you will encounter the following error message:
To start, determine if the issue is specific to your computer. Ask a member of your office to access BuildingConnected from their computer. If the colleague encounters the same error message, please reach out to your company’s IT department to troubleshoot your company’s internet connection. Please note that BuildingConnected Support does not troubleshoot company-wide internet issues.

If the error is only occurring on your computer, please disconnect from your internet connection for sixty seconds and then re-connect. Taking this step should help because it refreshes the connection between your computer and your internet. Once you are re-connected to the internet, refresh your browser and try again. Please note that the steps to disconnect from the internet depends on the type of connection that you are using (WiFi or hardwired). If you are not sure of the type of connection that your computer is using, please reach out to your IT department for assistance.

This may help you,
Rachel Gomez