https://github.com/althonos/torch-treecrf
A PyTorch implementation of Tree-structured Conditional Random Fields.
https://github.com/althonos/torch-treecrf
conditional-random-fields machine-learning python-library pytorch torch
Last synced: 17 days ago
JSON representation
A PyTorch implementation of Tree-structured Conditional Random Fields.
- Host: GitHub
- URL: https://github.com/althonos/torch-treecrf
- Owner: althonos
- License: mit
- Created: 2023-01-14T15:04:43.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-06T14:00:36.000Z (22 days ago)
- Last Synced: 2025-04-11T16:24:38.242Z (17 days ago)
- Topics: conditional-random-fields, machine-learning, python-library, pytorch, torch
- Language: Python
- Homepage:
- Size: 48.8 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: COPYING
Awesome Lists containing this project
README
# 🌲 `torch-treecrf`
*A [PyTorch](https://pytorch.org/) implementation of Tree-structured Conditional Random Fields.*
[](https://github.com/althonos/torch-treecrf/actions)
[](https://codecov.io/gh/althonos/torch-treecrf/)
[](https://choosealicense.com/licenses/gpl-3.0/)
[](https://pypi.org/project/torch-treecrf)
[](https://pypi.org/project/torch-treecrf/#files)
[](https://pypi.org/project/torch-treecrf/#files)
[](https://pypi.org/project/torch-treecrf/#files)
[](https://github.com/althonos/torch-treecrf/)
[](https://github.com/althonos/torch-treecrf/issues)
[](https://github.com/althonos/torch-treecrf.py/blob/master/CHANGELOG.md)
[](https://pepy.tech/project/torch-treecrf)## 🗺️ Overview
[Conditional Random Fields](https://en.wikipedia.org/wiki/Conditional_random_field)
(CRF) are a family of discriminative graphical learning models that can be used
to model the dependencies between variables. The most common
form of CRFs are Linear-chain CRF, where a prediction depends on
an observed variable, as well as the prediction before and after it
(the *context*). Linear-chain CRFs are widely used in Natural Language Processing.
![]()
$$
P(Y | X) = \frac{1}{Z(X)} \prod_{i=1}^n{ \Psi_i(y_i, x_i) } \prod_{i=2}^n{ \Psi_{i-1,i}(y_{i-1}, y_i)}
$$In 2006, Tang *et al.*[[1]](#ref1) introduced Tree-structured CRFs to model hierarchical
relationships between predicted variables, allowing dependencies between
a prediction variable and its parents and children.
![]()
$$
P(Y | X) = \frac{1}{Z(X)} \prod_{i=1}^{n}{ \Psi_i(y_i, x_i) } \prod_{j \in \mathcal{N}(i)}{ \Psi_{j,i}(y_j, y_i)}
$$This package implements a generic Tree-structured CRF layer in PyTorch. The
layer can be stacked on top of a [linear layer](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) to implement a proper Tree-structured CRF, or on any other kind of model
producing emission scores in log-space for every class of each label. Computation
of marginals is implemented using Belief Propagation[[2]](#ref2), allowing for
exact inference on trees[[3]](#ref3):$$
\begin{aligned}
P(y_i | X)
& =
\frac{1}{Z(X)} \Psi_i(y_i, x_i)
& \underbrace{\prod_{j \in \mathcal{C}(i)}{\mu_{j \to i}(y_i)}} &
& \underbrace{\prod_{j \in \mathcal{P}(i)}{\mu_{j \to i}(y_i)}} \\
& = \frac1Z \Psi_i(y_i, x_i)
& \alpha_i(y_i) &
& \beta_i(y_i) \\
\end{aligned}
$$where for every node $i$, the message from the parents $\mathcal{P}(i)$ and
the children $\mathcal{C}(i)$ is computed recursively with the sum-product algorithm[[4]](#ref4):$$
\begin{aligned}
\forall j \in \mathcal{C}(i), \mu_{j \to i}(y_i) = \sum_{y_j}{
\Psi_{i,j}(y_i, y_j)
\Psi_j(y_j, x_j)
\prod_{k \in \mathcal{C}(j)}{\mu_{k \to j}(y_j)}
} \\
\forall j \in \mathcal{P}(i), \mu_{j \to i}(y_i) = \sum_{y_j}{
\Psi_{i,j}(y_i, y_j)
\Psi_j(y_j, x_j)
\prod_{k \in \mathcal{P}(j)}{\mu_{k \to j}(y_j)}
} \\
\end{aligned}
$$*The implementation should be generic enough that any kind of [Directed acyclic graph](https://en.wikipedia.org/wiki/Directed_acyclic_graph) can be used as a label hierarchy,
not just trees.*## 🔧 Installing
Install the `torch-treecrf` package directly from [PyPi](https://pypi.org/project/peptides)
which hosts universal wheels that can be installed with `pip`:
```console
$ pip install torch-treecrf
```## 📋 Features
- Encoding of directed graphs in an adjacency matrix, with $\mathcal{O}(1)$ retrieval of children and parents for any node, and $\mathcal{O}(N+E)$ storage.
- Support for any acyclic hierarchy representable as a [Directed Acyclic Graph](https://en.wikipedia.org/wiki/Directed_acyclic_graph) and not just directed trees, allowing prediction of classes such as the [Gene Ontology](https://geneontology.org).
- Multiclass output, provided all the target labels have the same number of classes: $Y \in \left\\{ 0, .., C \right\\}^L$.
- Minibatch support, with vectorized computation of the messages $\alpha_i(y_i)$ and $\beta_i(y_i)$.## 💡 Example
To create a Tree-structured CRF, you must first define the tree encoding the
relationships between variables. Let's build a simple CRF for a root variable
with two children:
![]()
First, define an adjacency matrix $M$ representing the hierarchy, such that
$M_{i,j}$ is $1$ if $j$ is a parent of $i$:
```python
adjacency = torch.tensor([
[0, 0, 0],
[1, 0, 0],
[1, 0, 0]
])
```Then create the CRF by giving it the adjacency matrix as the hyperparameter:
```python
crf = torch_treecrf.TreeCRF(adjacency)
```The `TreeCRF` expects local emission scores as a tensor of shape $(\star, L)$
where $\star$ is the minibatch size and $L$ the number of labels, and returns
a tensor of logits of the same shape.You can also use the CRF layer for cases where labels have more than two
classes; in which case use the `TreeCRFLayer` module, which expects an
emission tensor of shape $(\star, C, L)$, where $\star$ is the minibatch size, $L$ the number of labels and $C$ the number of class per label, and returns
a tensor $log P(Y | X)$ of the same shape.## 💭 Feedback
### ⚠️ Issue Tracker
Found a bug ? Have an enhancement request ? Head over to the [GitHub issue
tracker](https://github.com/althonos/torch-treecrf/issues) if you need to report
or ask something. If you are filing in on a bug, please include as much
information as you can about the issue, and try to recreate the same bug
in a simple, easily reproducible situation.### 🏗️ Contributing
Contributions are more than welcome! See
[`CONTRIBUTING.md`](https://github.com/althonos/torch-treecrf/blob/main/CONTRIBUTING.md)
for more details.## ⚖️ License
This library is provided under the [MIT License](https://choosealicense.com/licenses/mit/).
*This library was developed by [Martin Larralde](https://github.com/althonos/)
during his PhD project at the [European Molecular Biology Laboratory](https://www.embl.de/)
in the [Zeller team](https://github.com/zellerlab).*## 📚 References
- [1] Tang, Jie, Mingcai Hong, Juanzi Li, and Bangyong Liang. ‘Tree-Structured Conditional Random Fields for Semantic Annotation’. In The Semantic Web - ISWC 2006, edited by Isabel Cruz, Stefan Decker, Dean Allemang, Chris Preist, Daniel Schwabe, Peter Mika, Mike Uschold, and Lora M. Aroyo, 640–53. Lecture Notes in Computer Science. Berlin, Heidelberg: Springer, 2006. [doi:10.1007/11926078_46](https://doi.org/10.1007/11926078_46).
- [2] Pearl, Judea. ‘Reverend Bayes on Inference Engines: A Distributed Hierarchical Approach’. In Proceedings of the Second AAAI Conference on Artificial Intelligence, 133–136. AAAI’82. Pittsburgh, Pennsylvania: AAAI Press, 1982.
- [3] Bach, Francis, and Guillaume Obozinski. ‘Sum Product Algorithm and Hidden Markov Model’, ENS Course Material, 2016. http://imagine.enpc.fr/%7Eobozinsg/teaching/mva_gm/lecture_notes/lecture7.pdf.
- [4] Kschischang, Frank R., Brendan J. Frey, and Hans-Andrea Loeliger. ‘Factor Graphs and the Sum-Product Algorithm’. IEEE Transactions on Information Theory 47, no. 2 (February 2001): 498–519. [doi:10.1109/18.910572](https://doi.org/10.1109/18.910572).