|
20 | 20 | # |
21 | 21 | # Using MaskedTensor |
22 | 22 | # ++++++++++++++++++ |
| 23 | +# |
| 24 | +# In this section we discuss how to use MaskedTensor including how to construct, access, the data |
| 25 | +# and mask, as well as indexing and slicing. |
| 26 | +# |
| 27 | +# Preparation |
| 28 | +# ----------- |
| 29 | +# |
| 30 | +# We'll begin by doing the necessary setup for the tutorial: |
23 | 31 | # |
| 32 | + |
| 33 | +import torch |
| 34 | +from torch.masked import masked_tensor, as_masked_tensor |
| 35 | +import warnings |
| 36 | + |
| 37 | +# Disable prototype warnings and such |
| 38 | +warnings.filterwarnings(action='ignore', category=UserWarning) |
| 39 | + |
| 40 | +###################################################################### |
24 | 41 | # Construction |
25 | 42 | # ------------ |
26 | 43 | # |
|
52 | 69 | # as :class:`torch.Tensor`. Below are some examples of common indexing and slicing patterns: |
53 | 70 | # |
54 | 71 |
|
55 | | -import torch |
56 | | -from torch.masked import masked_tensor, as_masked_tensor |
57 | | - |
58 | 72 | data = torch.arange(24).reshape(2, 3, 4) |
59 | 73 | mask = data % 2 == 0 |
60 | 74 |
|
|
174 | 188 | x = torch.tensor([1., 1.], requires_grad=True) |
175 | 189 | div = torch.tensor([0., 1.]) |
176 | 190 | y = x/div # => y is [inf, 1] |
177 | | - >>> |
178 | 191 | mask = (div != 0) # => mask is [0, 1] |
179 | 192 | loss = as_masked_tensor(y, mask) |
180 | 193 | loss.sum().backward() |
|
213 | 226 | # Safe Softmax |
214 | 227 | # ------------ |
215 | 228 | # |
216 | | -# Safe softmax is another great example of `an issue <https://github.com/pytorch/pytorch/issues/55056>`_ |
| 229 | +# Safe softmax is another great example of `an issue <https://github.com/pytorch/pytorch/issues/55056>`__ |
217 | 230 | # that arises frequently. In a nutshell, if there is an entire batch that is "masked out" |
218 | 231 | # or consists entirely of padding (which, in the softmax case, translates to being set `-inf`), |
219 | 232 | # then this will result in NaNs, which can lead to training divergence. |
|
247 | 260 |
|
248 | 261 | ###################################################################### |
249 | 262 | # Implementing missing torch.nan* operators |
250 | | -# -------------------------------------------------------------------------------------------------------------- |
| 263 | +# ----------------------------------------- |
251 | 264 | # |
252 | 265 | # In `Issue 61474 <<https://github.com/pytorch/pytorch/issues/61474>`__, |
253 | 266 | # there is a request to add additional operators to cover the various `torch.nan*` applications, |
254 | 267 | # such as ``torch.nanmax``, ``torch.nanmin``, etc. |
255 | 268 | # |
256 | 269 | # In general, these problems lend themselves more naturally to masked semantics, so instead of introducing additional |
257 | | -# operators, we propose using :class:`MaskedTensor`s instead. Since |
258 | | -# `nanmean has already landed <https://github.com/pytorch/pytorch/issues/21987>`_, we can use it as a comparison point: |
| 270 | +# operators, we propose using :class:`MaskedTensor`s instead. |
| 271 | +# Since `nanmean has already landed <https://github.com/pytorch/pytorch/issues/21987>`__, |
| 272 | +# we can use it as a comparison point: |
259 | 273 | # |
260 | 274 |
|
261 | 275 | x = torch.arange(16).float() |
|
0 commit comments