Jump to content

PyTorch: Difference between revisions

429 bytes added ,  15 August 2022
no edit summary
No edit summary
Line 38: Line 38:


==Usage==
==Usage==
Note that there are some useful functions under <code>torch.nn.functional</code> which is typically imported as <code>F</code>.
===torch.meshgrid===
===torch.meshgrid===
Note that this is transposed compared to <code>np.meshgrid</code>.
Note that this is transposed compared to <code>np.meshgrid</code>.


===torch.nn.functional===
===torch.multinomial===
[https://pytorch.org/docs/stable/nn.functional.html PyTorch Documentation]
[https://pytorch.org/docs/stable/generated/torch.multinomial.html torch.multinomial]<br>
====F.grid_sample====
If you need to sample with a lot of categories and with replacement, it may be faster to use `torch.cumsum` to build a CDF and `torch.searchsorted`.
{{hidden | torch.searchsorted example |
<syntaxhighlight lang="python">
# Create your weights variable.
weights_cdf = torch.cumsum(weights, dim=0)
weights_cdf_max = weights_cdf[0]
sample = torch.searchsorted(weights_cdf,
                            weights_cdf_max * torch.rand(num_samples))
</syntaxhighlight>
}}
 
===F.grid_sample===
[https://pytorch.org/docs/stable/nn.functional.html#grid-sample Doc]<br>
[https://pytorch.org/docs/stable/nn.functional.html#grid-sample Doc]<br>
This function allows you to perform interpolation on your input tensor.<br>
This function allows you to perform interpolation on your input tensor.<br>
Line 108: Line 121:


Note that [https://en.wikipedia.org/wiki/Bfloat16_floating-point_format <code>bfloat16</code>] is different from IEEE float16. bfloat16 has fewer mantissa bits (8 exp, 7 mantissa) and is used by Google's TPUs. In contrast, float16 has 5 exp and 10 mantissa bits.
Note that [https://en.wikipedia.org/wiki/Bfloat16_floating-point_format <code>bfloat16</code>] is different from IEEE float16. bfloat16 has fewer mantissa bits (8 exp, 7 mantissa) and is used by Google's TPUs. In contrast, float16 has 5 exp and 10 mantissa bits.
===torch.multinomial===
If you need to sample with a lot of categories and with replacement, it may be faster to use `torch.cumsum` to build a CDF and `torch.searchsorted`.


==Classification==
==Classification==