Operations¶
This extends the base dynet library with useful operations.
-
dynn.operations.nll_softmax(logit, y)¶ This is the same as
dy.pickneglogsoftmax.The main difference is the shorter name and transparent handling of batches. It computes:
\[-\texttt{logit[y]}+\log(\sum_{\texttt{c'}}e^{logit[c']})\](softmax then negative log likelihood of
y)Parameters: - logit (
dynet.Expression) – Logit - y (int,list) – Either a class or a list of class
(if
logitis batched)
- logit (
-
dynn.operations.seq_mask(size, lengths, base_val=1, mask_val=0, left_aligned=True)¶ Returns a mask for a batch sequences of different lengths.
This will return a
(size,), len(lengths)shaped expression where theith element of batchbisbase_valiffi<=lengths[b](andmask_valotherwise).For example, if
sizeis4andlengthsis[1,2,4]then the returned mask will be:(here each row is a batch element)
Parameters: - size (int) – Max size of the sequence (must be
>=max(lengths)) - lengths (list) – List of lengths
- base_val (int, optional) – Value of the mask for non-masked indices (typically 1 for multiplicative masks and 0 for additive masks). Defaults to 1.
- mask_val (int, optional) – Value of the mask for masked indices (typically 0 for multiplicative masks and -inf for additive masks). Defaults to 0.
- left_aligned (bool, optional) – Defaults to True.
Returns: dynet.Expression: Mask expression- size (int) – Max size of the sequence (must be
-
dynn.operations.squeeze(x, d=0)¶ Removes a dimension of size 1 at the given position.
Example:
# (1, 20) x = dy.zeros((1, 20)) # (20,) squeeze(x, 0) # (20, 1) x = dy.zeros((20, 1)) # (20,) squeeze(x, 1) # (20,) squeeze(x, -1)
-
dynn.operations.stack(xs, d=0)¶ Like concatenated but inserts a dimension
d=-1to insert a dimension at the last positionParameters:
-
dynn.operations.unsqueeze(x, d=0)¶ Insert a dimension of size 1 at the given position
Example:
# (10, 20) x = dy.zeros((10, 20)) # (1, 10, 20) unsqueeze(x, 0) # (10, 20, 1) unsqueeze(x, -1)