Functional

pytorch_wrapper.functional.create_mask_from_length(length_tensor, mask_size, zeros_at_end=True)

Creates a binary mask based on length.

Parameters:
  • length_tensor – ND Tensor containing the lengths.
  • mask_size – Int specifying the mask size. Usually the largest length.
  • zeros_at_end – Whether to put the zeros of the mask at the end.
Returns:

(N+1)D Int Tensor (…, mask_size).

pytorch_wrapper.functional.get_first_non_masked_element(data_tensor, lengths_tensor, dim, is_end_padded=True)

Returns the first non masked element of a Tensor along the specified dimension.

Parameters:
  • data_tensor – ND Tensor.
  • lengths_tensor – (dim)D Tensor containing lengths.
  • dim – Int that corresponds to the dimension.
  • is_end_padded – Whether the Tensor is padded at the end.
Returns:

(N-1)D Tensor containing the first non-masked elements along the specified dimension.

pytorch_wrapper.functional.get_last_non_masked_element(data_tensor, lengths_tensor, dim, is_end_padded=True)

Returns the last non masked element of a Tensor along the specified dimension.

Parameters:
  • data_tensor – ND Tensor.
  • lengths_tensor – (dim)D Tensor containing lengths.
  • dim – Int that corresponds to the dimension.
  • is_end_padded – Whether the Tensor is padded at the end.
Returns:

(N-1)D Tensor containing the last non-masked elements along the specified dimension.

pytorch_wrapper.functional.get_last_state_of_rnn(rnn_out, batch_sequence_lengths, is_bidirectional, is_end_padded=True)

Returns the last state(s) of the output of an RNN.

Parameters:
  • rnn_out – 3D Tensor (batch_size, sequence_length, time_step_size).
  • batch_sequence_lengths – 1D Tensor (batch_size) containing the lengths of the sequences.
  • is_bidirectional – Whether the RNN is bidirectional or not.
  • is_end_padded – Whether the Tensor is padded at the end.
Returns:

2D Tensor (batch_size, time_step_size or time_step_size * 2) containing the last state(s) of the RNN.

pytorch_wrapper.functional.masked_max_pooling(data_tensor, mask, dim)

Performs masked max-pooling across the specified dimension of a Tensor.

Parameters:
  • data_tensor – ND Tensor.
  • mask – Tensor containing a binary mask that can be broad-casted to the shape of data_tensor.
  • dim – Int that corresponds to the dimension.
Returns:

(N-1)D Tensor containing the result of the max-pooling operation.

pytorch_wrapper.functional.masked_mean_pooling(data_tensor, mask, dim)

Performs masked mean-pooling across the specified dimension of a Tensor.

Parameters:
  • data_tensor – ND Tensor.
  • mask – Tensor containing a binary mask that can be broad-casted to the shape of data_tensor.
  • dim – Int that corresponds to the dimension.
Returns:

(N-1)D Tensor containing the result of the mean-pooling operation.

pytorch_wrapper.functional.masked_min_pooling(data_tensor, mask, dim)

Performs masked min-pooling across the specified dimension of a Tensor.

Parameters:
  • data_tensor – ND Tensor.
  • mask – Tensor containing a binary mask that can be broad-casted to the shape of data_tensor.
  • dim – Int that corresponds to the dimension.
Returns:

(N-1)D Tensor containing the result of the min-pooling operation.

pytorch_wrapper.functional.pad(data_tensor, pad_size, dim, pad_at_end=True)

Pads a Tensor with zeros along a dimension.

Parameters:
  • data_tensor – Tensor to pad.
  • pad_size – How many zeros to append.
  • dim – The dimension to pad.
  • pad_at_end – Whether to pad at the end.
Returns:

Padded Tensor.

pytorch_wrapper.functional.same_dropout(data_tensor, dropout_p, dim, is_model_training)

Drops the same random elements of a Tensor across the specified dimension, during training.

Parameters:
  • data_tensor – ND Tensor.
  • dropout_p – The dropout probability.
  • dim – Int that corresponds to the dimension.
  • is_model_training – Whether the model is currently training.
Returns:

ND Tensor.

pytorch_wrapper.functional.sub_tensor_dropout(data_tensor, dropout_p, dim, is_model_training)

Drops (zeroes-out) random sub-Tensors of a Tensor across the specified dimension, during training.

Parameters:
  • data_tensor – ND Tensor.
  • dropout_p – The dropout probability.
  • dim – Int that corresponds to the dimension.
  • is_model_training – Whether the model is currently training.
Returns:

ND Tensor.