How to use torch::nn::init::calculate_gain() in C++

cj0741: <p>Hi everyone,<br>
I want to use torch::nn::init::calculate_gain() in C++, it defined like following:<br>
<code>TORCH_API double calculate_gain(NonlinearityType nonlinearity, double param = 0.01);</code><br>
the parameter “NonlinearityType” defined like:</p>
<pre><code class="lang-auto">namespace torch {
namespace nn {
namespace init {
using NonlinearityType = c10::variant&lt;
  enumtype::kLinear,
  enumtype::kConv1D,
  enumtype::kConv2D,
  enumtype::kConv3D,
  enumtype::kConvTranspose1D,
  enumtype::kConvTranspose2D,
  enumtype::kConvTranspose3D,
  enumtype::kSigmoid,
  enumtype::kTanh,
  enumtype::kReLU,
  enumtype::kLeakyReLU
&gt;;
</code></pre>
<p>I want to write code like <code>nn.init.calculate_gain('conv2d')</code> in C++, what should I put in the torch::nn::init::calculate_gain() ?</p>
<p>Thanks</p>

Lin_Jia: <p>I would just try: torch::enumtype::kConv2D</p>
<p>The C++ enum convention is that the option starts with “k”, and the naming usually hints what it is about.</p>

Igor_Aherne: <p>we need to use it as</p>
<p><code>torch::enumtype::kConv2D myType;  </code><br>
<code>calculate_gain(myType);</code></p>
<p>Or</p>
<p><code>calculate_gain( torch::enumtype::kConv2D() ); //constructs the temporary object in the arguments </code></p>
<p>This is because if we look in the definition, we’ll see how it’s wrapped in the preprocessor:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/1/913c519fafb55060000eb7d45b506b961c1261d7.png" data-download-href="https://discuss.pytorch.org/uploads/default/913c519fafb55060000eb7d45b506b961c1261d7" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/1/913c519fafb55060000eb7d45b506b961c1261d7_2_690x419.png" alt="image" data-base62-sha1="kIOsSiRY9SFW1YPmWIs7NfsqK5p" width="690" height="419" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/1/913c519fafb55060000eb7d45b506b961c1261d7_2_690x419.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/1/913c519fafb55060000eb7d45b506b961c1261d7_2_1035x628.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/9/1/913c519fafb55060000eb7d45b506b961c1261d7_2_1380x838.png 2x" data-dominant-color="0B293E"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1799×1095 89.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>if you peek into the <code>TORCH_ENUM_DECLARE</code>, you will actually see that it’s declaring structs.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/9/59b3c195537bceb26c1cb1bc594b2791c82b7a8b.png" data-download-href="https://discuss.pytorch.org/uploads/default/59b3c195537bceb26c1cb1bc594b2791c82b7a8b" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/9/59b3c195537bceb26c1cb1bc594b2791c82b7a8b.png" alt="image" data-base62-sha1="cNxCu2SF9a5mF3qiXsiK0pQ3ptp" width="690" height="329" data-dominant-color="062A38"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1124×536 28.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>