Affine Layers

Biaffine

class supar.modules.affine.Biaffine(n_in, n_out=1, scale=0, bias_x=True, bias_y=True)[source]

Biaffine layer for first-order scoring [Dozat & Manning 2017].

This function has a tensor of weights \(W\) and bias terms if needed. The score \(s(x, y)\) of the vector pair \((x, y)\) is computed as \(x^T W y / d^s\), where d and s are vector dimension and scaling factor respectively. \(x\) and \(y\) can be concatenated with bias terms.

Parameters
  • n_in (int) – The size of the input feature.

  • n_out (int) – The number of output channels.

  • scale (float) – Factor to scale the scores. Default: 0.

  • bias_x (bool) – If True, adds a bias term for tensor \(x\). Default: True.

  • bias_y (bool) – If True, adds a bias term for tensor \(y\). Default: True.

forward(x, y)[source]
Parameters
Returns

A scoring tensor of shape [batch_size, n_out, seq_len, seq_len]. If n_out=1, the dimension for n_out will be squeezed automatically.

Return type

Tensor

Triaffine

class supar.modules.affine.Triaffine(n_in, n_out=1, scale=0, bias_x=False, bias_y=False)[source]

Triaffine layer for second-order scoring [Wang et al. 2019, Zhang et al. 2020a].

This function has a tensor of weights \(W\) and bias terms if needed. The score \(s(x, y, z)\) of the vector triple \((x, y, z)\) is computed as \(x^T z^T W y / d^s\), where d and s are vector dimension and scaling factor respectively. \(x\) and \(y\) can be concatenated with bias terms.

Parameters
  • n_in (int) – The size of the input feature.

  • n_out (int) – The number of output channels.

  • scale (float) – Factor to scale the scores. Default: 0.

  • bias_x (bool) – If True, adds a bias term for tensor \(x\). Default: False.

  • bias_y (bool) – If True, adds a bias term for tensor \(y\). Default: False.

forward(x, y, z)[source]
Parameters
Returns

A scoring tensor of shape [batch_size, n_out, seq_len, seq_len, seq_len]. If n_out=1, the dimension for n_out will be squeezed automatically.

Return type

Tensor