Prelu c++ how it works?

username: erict
cooked: <p>Hi,</p>
<p>i’am trying to use the prelu function in pytorch c++, but i cannot understand how can i use it. The function parameters are two Tensors. Why i need two Tensors?. I would expected to pass only the weights.</p>
<p>In the python version i can pass only the weights. How this is function is related to the python version?</p>
<p>Thanks</p>

username: albanD
cooked: <p>I guess the c++ api is similar to the functional one: you need to give the layer’s input and the weights: <a href="https://pytorch.org/docs/stable/nn.functional.html#prelu">https://pytorch.org/docs/stable/nn.functional.html#prelu</a></p>

username: yf225
cooked: <p><a class="mention" href="/u/erict">@erict</a> - As explained by <a class="mention" href="/u/alband">@albanD</a>, the <code>torch::nn::functional::prelu</code> function is equivalent to Python <code>torch.nn.functional.prelu</code> function, and they both take two tensors (<code>input</code> and <code>weight</code>).</p>
<blockquote>
<p>In the python version i can pass only the weights. How this is function is related to the python version?</p>
</blockquote>
<p>Do you mind elaborating how you pass only the weights in the Python version?</p>

username: erict
cooked: <p>Hi <a class="mention" href="/u/alband">@albanD</a>, <a class="mention" href="/u/yf225">@yf225</a></p>
<p>thanks for your answer.  Yes i had a look at the docs. I will try to explain better. On python implementation inside Prelu class here <a href="https://pytorch.org/docs/stable/_modules/torch/nn/modules/activation.html#PReLU" rel="nofollow noopener">https://pytorch.org/docs/stable/_modules/torch/nn/modules/activation.html#PReLU</a> you can find <code>self.weight = Parameter(torch.Tensor(num_parameters).fill_(init))</code> and by default self.weights is added to the model’s Parameters if i have understood well.</p>
<p>On pytorch c++ i have some doubts on how to use “self.weights” Tensor and pass to the prelu function. Now i’m using register_parameter in the constructor of the model like this <code>register_parameter("prelu1", prelu1.fill_(0.25));</code> where prelu is <code>torch::Tensor prelu1 = torch::ones({1})</code></p>
<p>In the forward i use the function like this <code>x = torch::prelu(inputs,prelu1);</code></p>
<p>Is this a correct way to register the Tensor to the model like python version?<br>
This is what i have done.</p>
<pre><code class="lang-auto">struct TestImpl : nn::Module {


	TestImpl() : conv1(register_module("conv1", nn::Conv2d(nn::Conv2dOptions(1, 32, 4).stride(2).padding(1)))){

		register_parameter("prelu1", prelu1.fill_(0.25));

	
	
	}

	torch::Tensor forward(torch::Tensor x) {
	
		x = torch::prelu(x, prelu1);
	
	}


	nn::Conv2d conv1;

	torch::Tensor prelu1 = torch::ones({ 1 });



};
</code></pre>
<p>Thanks</p>

username: yf225
cooked: <p><a class="mention" href="/u/erict">@erict</a> Thanks for the explanation - If your goal is to find an equivalent of <code>torch.nn.PReLU</code> layer in C++ API, you can use <code>torch::nn::PReLU</code> layer which behaves exactly like the Python version, and we can construct it like <code>auto m = torch::nn::PReLU(torch::nn::PReLUOptions().num_parameters(42).init(0.25))</code> (<code>num_parameters</code> and <code>init</code> are optional). And we can access its <code>weight</code> attribute using <code>m.weight</code>, just like how we do it in Python.</p>
<p>If your goal is to make <code>prelu1</code> in your C++ module point to the parameter registered with <code>register_parameter("prelu1", prelu1.fill_(0.25));</code>, we should replace it with <code>prelu1 = register_parameter("prelu1", prelu1.fill_(0.25));</code>, and then it should be properly registered. <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>

username: erict
cooked: <p>Ok perfect. Thank you for the explanation.</p>