# Chain#

## LinearChainCRF#

class supar.structs.chain.LinearChainCRF(scores: Tensor, trans: = None, lens: LongTensor | None = None)[source]#

Linear-chain CRFs Lafferty et al. (2001).

Parameters:
• scores (Tensor) – [batch_size, seq_len, n_tags]. Log potentials.

• trans (Tensor) – [n_tags+1, n_tags+1]. Transition scores. trans[-1, :-1]/trans[:-1, -1] represent transitions for start/end positions respectively.

• lens (LongTensor) – [batch_size]. Sentence lengths for masking. Default: None.

Examples

>>> from supar import LinearChainCRF
>>> batch_size, seq_len, n_tags = 2, 5, 4
>>> lens = torch.tensor([3, 4])
>>> value = torch.randint(n_tags, (batch_size, seq_len))
>>> s1 = LinearChainCRF(torch.randn(batch_size, seq_len, n_tags),
torch.randn(n_tags+1, n_tags+1),
lens)
>>> s2 = LinearChainCRF(torch.randn(batch_size, seq_len, n_tags),
torch.randn(n_tags+1, n_tags+1),
lens)
>>> s1.max
>>> s1.argmax
tensor([[2, 0, 3, 0, 0],
[3, 3, 3, 2, 0]])
>>> s1.log_partition
>>> s1.log_prob(value)
>>> s1.entropy
>>> s1.kl(s2)

property argmax#

Computes $$\arg\max_y p(y)$$ of the distribution $$p(y)$$.

topk(k: int) LongTensor[source]#

Computes the k-argmax of the distribution $$p(y)$$.

## SemiMarkovCRF#

class supar.structs.chain.SemiMarkovCRF(scores: Tensor, trans: = None, lens: LongTensor | None = None)[source]#

Semi-markov CRFs Sarawagi & Cohen (2004).

Parameters:
• scores (Tensor) – [batch_size, seq_len, seq_len, n_tags]. Log potentials.

• trans (Tensor) – [n_tags, n_tags]. Transition scores.

• lens (LongTensor) – [batch_size]. Sentence lengths for masking. Default: None.

Examples

>>> from supar import SemiMarkovCRF
>>> batch_size, seq_len, n_tags = 2, 5, 4
>>> lens = torch.tensor([3, 4])
>>> value = torch.tensor([[[ 0, -1, -1, -1, -1],
[-1, -1,  2, -1, -1],
[-1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1]],
[[-1,  1, -1, -1, -1],
[-1, -1,  3, -1, -1],
[-1, -1, -1,  0, -1],
[-1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1]]])
>>> s1 = SemiMarkovCRF(torch.randn(batch_size, seq_len, seq_len, n_tags),
torch.randn(n_tags, n_tags),
lens)
>>> s2 = SemiMarkovCRF(torch.randn(batch_size, seq_len, seq_len, n_tags),
torch.randn(n_tags, n_tags),
lens)
>>> s1.max
>>> s1.argmax
[[[0, 0, 1], [1, 1, 0], [2, 2, 1]], [[0, 0, 1], [1, 1, 3], [2, 2, 0], [3, 3, 1]]]
>>> s1.log_partition
>>> s1.log_prob(value)

Computes $$\arg\max_y p(y)$$ of the distribution $$p(y)$$.
Computes the k-argmax of the distribution $$p(y)$$.