Utility functions

dynn.util.conditional_dropout(x, dropout_rate, flag)

This helper function applies dropout only if the flag is set to True and the dropout_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)
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:
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:
  • length (int) – Length
  • dim (int) – Dimension of the embeddings