Skip to content

Commit 16abb37

Browse files
Svetlana Karslioglugeorge-qi
Svetlana Karslioglu
authored andcommitted
undo requirements.txt changes
1 parent 9d44ef8 commit 16abb37

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

prototype_source/maskedtensor_overview.py

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"""
44
(Prototype) MaskedTensor Overview
5-
=================================
5+
*********************************
66
"""
77

88
######################################################################
@@ -14,16 +14,33 @@
1414
# * use any masked semantics (for example, variable length tensors, nan* operators, etc.)
1515
# * differentiation between 0 and NaN gradients
1616
# * various sparse applications (see tutorial below)
17-
#
17+
#
1818
# For a more detailed introduction on what MaskedTensors are, please find the
1919
# `torch.masked documentation <https://pytorch.org/docs/master/masked.html>`__.
20-
#
20+
#
2121
# 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+
# -----------
2329
#
30+
# We'll begin by doing the necessary setup for the tutorial:
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+
######################################################################
2441
# Construction
2542
# ------------
26-
#
43+
#
2744
# There are a few different ways to construct a MaskedTensor:
2845
#
2946
# * The first way is to directly invoke the MaskedTensor class
@@ -52,24 +69,24 @@
5269
# as :class:`torch.Tensor`. Below are some examples of common indexing and slicing patterns:
5370
#
5471

55-
import torch
56-
from torch.masked import masked_tensor, as_masked_tensor
57-
5872
data = torch.arange(24).reshape(2, 3, 4)
5973
mask = data % 2 == 0
6074

61-
print("data\n", data)
62-
print("mask\n", mask)
75+
print("data:\n", data)
76+
print("mask:\n", mask)
77+
78+
######################################################################
79+
#
6380

6481
# float is used for cleaner visualization when being printed
6582
mt = masked_tensor(data.float(), mask)
6683

67-
print ("mt[0]:\n", mt[0])
68-
print ("mt[:, :, 2:4]", mt[:, :, 2:4])
84+
print("mt[0]:\n", mt[0])
85+
print("mt[:, :, 2:4]:\n", mt[:, :, 2:4])
6986

7087
######################################################################
7188
# Why is MaskedTensor useful?
72-
# +++++++++++++++++++++++++++
89+
# ===========================
7390
#
7491
# Because of :class:`MaskedTensor`'s treatment of specified and unspecified values as a first-class citizen
7592
# instead of an afterthought (with filled values, nans, etc.), it is able to solve for several of the shortcomings
@@ -90,8 +107,8 @@
90107
#
91108
# :class:`MaskedTensor` is the perfect solution for this!
92109
#
93-
# :func:`torch.where`
94-
# ^^^^^^^^^^^^^^^^^^^
110+
# torch.where
111+
# ^^^^^^^^^^^
95112
#
96113
# In `Issue 10729 <https://github.com/pytorch/pytorch/issues/10729>`__, we notice a case where the order of operations
97114
# can matter when using :func:`torch.where` because we have trouble differentiating between if the 0 is a real 0
@@ -121,8 +138,8 @@
121138
# The gradient here is only provided to the selected subset. Effectively, this changes the gradient of `where`
122139
# to mask out elements instead of setting them to zero.
123140
#
124-
# Another :func:`torch.where`
125-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^
141+
# Another torch.where
142+
# ^^^^^^^^^^^^^^^^^^^
126143
#
127144
# `Issue 52248 <https://github.com/pytorch/pytorch/issues/52248>`__ is another example.
128145
#
@@ -174,15 +191,14 @@
174191
x = torch.tensor([1., 1.], requires_grad=True)
175192
div = torch.tensor([0., 1.])
176193
y = x/div # => y is [inf, 1]
177-
>>>
178194
mask = (div != 0) # => mask is [0, 1]
179195
loss = as_masked_tensor(y, mask)
180196
loss.sum().backward()
181197
x.grad
182198

183199
######################################################################
184200
# :func:`torch.nansum` and :func:`torch.nanmean`
185-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
201+
# ----------------------------------------------
186202
#
187203
# In `Issue 67180 <https://github.com/pytorch/pytorch/issues/67180>`__,
188204
# the gradient isn't calculate properly (a longstanding issue), whereas :class:`MaskedTensor` handles it correctly.
@@ -213,7 +229,7 @@
213229
# Safe Softmax
214230
# ------------
215231
#
216-
# Safe softmax is another great example of `an issue <https://github.com/pytorch/pytorch/issues/55056>`_
232+
# Safe softmax is another great example of `an issue <https://github.com/pytorch/pytorch/issues/55056>`__
217233
# that arises frequently. In a nutshell, if there is an entire batch that is "masked out"
218234
# or consists entirely of padding (which, in the softmax case, translates to being set `-inf`),
219235
# then this will result in NaNs, which can lead to training divergence.
@@ -247,24 +263,31 @@
247263

248264
######################################################################
249265
# Implementing missing torch.nan* operators
250-
# --------------------------------------------------------------------------------------------------------------
266+
# -----------------------------------------
251267
#
252-
# In `Issue 61474 <<https://github.com/pytorch/pytorch/issues/61474>`__,
268+
# In `Issue 61474 <https://github.com/pytorch/pytorch/issues/61474>`__,
253269
# there is a request to add additional operators to cover the various `torch.nan*` applications,
254270
# such as ``torch.nanmax``, ``torch.nanmin``, etc.
255271
#
256272
# 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:
273+
# operators, we propose using :class:`MaskedTensor` instead.
274+
# Since `nanmean has already landed <https://github.com/pytorch/pytorch/issues/21987>`__,
275+
# we can use it as a comparison point:
259276
#
260277

261278
x = torch.arange(16).float()
262279
y = x * x.fmod(4)
263280
z = y.masked_fill(y == 0, float('nan')) # we want to get the mean of y when ignoring the zeros
264281

265-
print("y:\n, y")
282+
######################################################################
283+
#
284+
print("y:\n", y)
266285
# z is just y with the zeros replaced with nan's
267286
print("z:\n", z)
287+
288+
######################################################################
289+
#
290+
268291
print("y.mean():\n", y.mean())
269292
print("z.nanmean():\n", z.nanmean())
270293
# MaskedTensor successfully ignores the 0's
@@ -296,13 +319,13 @@
296319
# This is a similar problem to safe softmax where `0/0 = nan` when what we really want is an undefined value.
297320
#
298321
# Conclusion
299-
# ++++++++++
322+
# ==========
300323
#
301324
# In this tutorial, we've introduced what MaskedTensors are, demonstrated how to use them, and motivated their
302325
# value through a series of examples and issues that they've helped resolve.
303326
#
304327
# Further Reading
305-
# +++++++++++++++
328+
# ===============
306329
#
307330
# To continue learning more, you can find our
308331
# `MaskedTensor Sparsity tutorial <https://pytorch.org/tutorials/prototype/maskedtensor_sparsity.html>`__

0 commit comments

Comments
 (0)