Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e608db

Browse files
committed
Minor fixes to MaskedTensor tutorial
1 parent 46aeafa commit 6e608db

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

unstable_source/maskedtensor_advanced_semantics.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
===========================================
66
"""
77

8+
import warnings
9+
10+
import numpy as np
11+
812
######################################################################
9-
#
13+
#
1014
# Before working on this tutorial, please make sure to review our
1115
# `MaskedTensor Overview tutorial <https://pytorch.org/tutorials/prototype/maskedtensor_overview.html>`.
1216
#
1317
# The purpose of this tutorial is to help users understand how some of the advanced semantics work
1418
# and how they came to be. We will focus on two particular ones:
1519
#
16-
# *. Differences between MaskedTensor and `NumPy's MaskedArray <https://numpy.org/doc/stable/reference/maskedarray.html>`__
20+
# *. Differences between MaskedTensor and `NumPy's MaskedArray <https://numpy.org/doc/stable/reference/maskedarray.html>`__
1721
# *. Reduction semantics
1822
#
1923
# Preparation
@@ -22,11 +26,9 @@
2226

2327
import torch
2428
from torch.masked import masked_tensor
25-
import numpy as np
26-
import warnings
2729

2830
# Disable prototype warnings and such
29-
warnings.filterwarnings(action='ignore', category=UserWarning)
31+
warnings.filterwarnings(action="ignore", category=UserWarning)
3032

3133
######################################################################
3234
# MaskedTensor vs NumPy's MaskedArray
@@ -43,7 +45,7 @@
4345
# `apply the logical_or operator <https://github.com/numpy/numpy/blob/68299575d8595d904aff6f28e12d21bf6428a4ba/numpy/ma/core.py#L1016-L1024>`__.
4446
#
4547

46-
data = torch.arange(5.)
48+
data = torch.arange(5.0)
4749
mask = torch.tensor([True, True, False, True, False])
4850
npm0 = np.ma.masked_array(data.numpy(), (~mask).numpy())
4951
npm1 = np.ma.masked_array(data.numpy(), (mask).numpy())
@@ -65,7 +67,7 @@
6567
try:
6668
mt0 + mt1
6769
except ValueError as e:
68-
print ("mt0 + mt1 failed. Error: ", e)
70+
print("mt0 + mt1 failed. Error: ", e)
6971

7072
######################################################################
7173
# However, if this behavior is desired, MaskedTensor does support these semantics by giving access to the data and masks
@@ -125,10 +127,14 @@
125127
# In other words, why don't we use the same semantics as ``np.ma.masked_array``? Consider the following example:
126128
#
127129

128-
data0 = torch.arange(10.).reshape(2, 5)
129-
data1 = torch.arange(10.).reshape(2, 5) + 10
130-
mask0 = torch.tensor([[True, True, False, False, False], [False, False, False, True, True]])
131-
mask1 = torch.tensor([[False, False, False, True, True], [True, True, False, False, False]])
130+
data0 = torch.arange(10.0).reshape(2, 5)
131+
data1 = torch.arange(10.0).reshape(2, 5) + 10
132+
mask0 = torch.tensor(
133+
[[True, True, False, False, False], [False, False, False, True, True]]
134+
)
135+
mask1 = torch.tensor(
136+
[[False, False, False, True, True], [True, True, False, False, False]]
137+
)
132138
npm0 = np.ma.masked_array(data0.numpy(), (mask0).numpy())
133139
npm1 = np.ma.masked_array(data1.numpy(), (mask1).numpy())
134140

@@ -155,7 +161,7 @@
155161
mt0 = masked_tensor(data0, ~mask0)
156162
mt1 = masked_tensor(data1, ~mask1)
157163

158-
(mt0.to_tensor(0) + mt1.to_tensor(0)).sum(0)
164+
print((mt0.to_tensor(0) + mt1.to_tensor(0)).sum(0))
159165

160166
######################################################################
161167
# Conclusion
@@ -167,4 +173,3 @@
167173
# the associative property amongst binary operations), which in turn can necessitate the user
168174
# to be more intentional with their code at times, but we believe this to be the better move.
169175
# If you have any thoughts on this, please `let us know <https://github.com/pytorch/pytorch/issues>`__!
170-
#

0 commit comments

Comments
 (0)