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
- forward(x, y)[source]¶
- Parameters
x (torch.Tensor) –
[batch_size, seq_len, n_in].y (torch.Tensor) –
[batch_size, seq_len, n_in].
- Returns
A scoring tensor of shape
[batch_size, n_out, seq_len, seq_len]. Ifn_out=1, the dimension forn_outwill be squeezed automatically.- Return type
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
- forward(x, y, z)[source]¶
- Parameters
x (torch.Tensor) –
[batch_size, seq_len, n_in].y (torch.Tensor) –
[batch_size, seq_len, n_in].z (torch.Tensor) –
[batch_size, seq_len, n_in].
- Returns
A scoring tensor of shape
[batch_size, n_out, seq_len, seq_len, seq_len]. Ifn_out=1, the dimension forn_outwill be squeezed automatically.- Return type