How to get the probability density function of a normal distribution

username: vitto
cooked: <p>So I’m trying to implement a gaussian policy in C++ I have a my gaussian tensor defined as:</p>
<p>this-&gt;dist = at::normal( mu[0], sigma );</p>
<p>dist will be a torch::Tensor</p>
<p>For the learning part I need something like: this-&gt;dist.log_prob(action)</p>
<p>Obviously the problem is that Tensors don’t have a log_prob function, can anyone help me out with this?</p>
<p>Thanks a lot for you time <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: ptrblck
cooked: <p>I guess your use case should use <a href="https://pytorch.org/docs/stable/distributions.html">torch.distributions</a> instead of a sampled tensor.<br>
However, I’m not sure what the current status of distributions support is in libtorch, but <a class="mention" href="/u/yf225">@yf225</a> might know. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: vitto
cooked: <p>Thanks for your reply! yeah I knew that the python framework had the normal distribution object but apparently libtorch doesn’t, so what I have done is this:<br>
To select an action I do:</p>
<blockquote>
<p>torch::Tensor out = policy_mu.forward(state);<br>
this-&gt;mu = out[0];<br>
auto action = torch::normal(this-&gt;mu.item(), sigma, {1,1});<br>
action = torch::clamp(action, -2.0, 2.0);</p>
</blockquote>
<p>To update my policy I do:<br>
The first bit is basically what TF does in the log_prob() function</p>
<blockquote>
<pre><code>  auto log_unnormalized = -0.5 * pow((action / this-&gt;sigma) - (this-&gt;mu / this-&gt;sigma),2);
  auto log_normalization = 0.5 * std::log(2. * M_PI) + std::log(this-&gt;sigma);
  auto log_prob = log_unnormalized - log_normalization;

  auto loss = log_prob * (reward + gamma*next_state_value- state_value);

  //auto loss = -this-&gt;dist.log_prob(action) * ((reward + gamma*next_state_value) - state_value);

  policy_mu.zero_grad();
  //sigma_optim.zero_grad();
  loss.backward();
  for (auto&amp; p : policy_mu.parameters())
  {
    //std::cout &lt;&lt; "----- p grad " &lt;&lt; p.grad() &lt;&lt; std::endl;
    auto tmp = 0.001*p.grad();
    p = p.detach();
    p -= tmp;
  }
</code></pre>
</blockquote>
<p>This isn’t working tho <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=12" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"> maybe I’m doing something wrong in the algorithm idk</p>
<p>If anyone can spot something wrong would be much appreciated!</p>

username: saba
cooked: <p>Hi Ptrblck,<br>
Sorry, I need to compute the CDF of a distribution. which function can do it for me? I search but found torch.distributions.cauchy which is confusing for me</p>

username: ptrblck
cooked: <p><code>torch.distributions</code> provide a <code>cdf</code> function, which you could pass a value in and would get the cdf output.<br>
<code>Cauchy</code> also provides it in <a href="https://pytorch.org/docs/stable/distributions.html#torch.distributions.cauchy.Cauchy"><code>torch.distributions.cauchy.Cauchy().cdf()</code></a>.</p>