Utility functions¶
-
dynn.util.
conditional_dropout
(x, dropout_rate, flag)¶ This helper function applies dropout only if the flag is set to
True
and thedropout_rate
is positive.Parameters: - x (
dynet.Expression
) – Input expression - dropout_rate (float) – Dropout rate
- flag (bool) – Setting this to false ensures that dropout is never applied (for testing for example)
- x (
-
dynn.util.
image_to_matrix
(M)¶ Transforms an ‘image’ with one channel (d1, d2, 1) into a matrix (d1, d2)
-
dynn.util.
list_to_matrix
(l)¶ Transforms a list of N vectors of dimension d into a (N, d) matrix
-
dynn.util.
mask_batches
(x, mask, value=0.0)¶ Apply a mask to the batch dimension
Parameters: - x (list,
dynet.Expression
) – The expression we want to mask. Either adynet.Expression
or a list thereof with the same batch dimension. - mask (np.array, list,
dynet.Expression
) – The mask. Either a list, 1d numpy array ordynet.Expression
. - value (float) – Mask value
- x (list,
-
dynn.util.
matrix_to_image
(M)¶ Transforms a matrix (d1, d2) into an ‘image’ with one channel (d1, d2, 1)
-
dynn.util.
num_params
(pc, params=True, lookup_params=True)¶ Number of parameters in a given ParameterCollection
-
dynn.util.
sin_embeddings
(length, dim, transposed=False)¶ Returns sinusoidal position encodings.
As described in Vaswani et al. (2017)
Specifically this return a
length x dim
matrix \(PE\) such that \(PE[p, 2i]=\sin(\frac{p}/{1000^{\frac{2i}{dim}}})\) and \(PE[p, 2i+1]=\cos(\frac{p}/{1000^{\frac{2i}{dim}}})\)Parameters: