Skip to content
Advertisement

What does Tensor[batch_mask, …] do?

I saw this line of code in an implementation of BiLSTM:

JavaScript

I assume this is some kind of “masking” operation, but found little information on Google about the meaning of .... Please help:).

Original Code:

JavaScript

Advertisement

Answer

I assume that batch_mask is a boolean tensor. In that case, batch_output[batch_mask] performs a boolean indexing that selects the elements corresponding to True in batch_mask.

... is usually referred as ellipsis, and in the case of PyTorch (but also other NumPy-like libraries), it is a shorthand for avoiding repeating the column operator (:) multiple times. For example, given a tensor v, with v.shape equal to (2, 3, 4), the expression v[1, :, :] can be rewritten as v[1, ...].

I performed some tests and using either batch_output[batch_mask, ...] or batch_output[batch_mask] seems to work identically:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement