Skip to main content

hierarchicalsoftmax

pypi badge testing badge coverage badge docs badge black badge

A Hierarchical Softmax Framework for PyTorch.

Documentation available here: https://rbturnbull.github.io/hierarchicalsoftmax/.

Installation

hierarchicalsoftmax can be installed from PyPI:

pip install hierarchicalsoftmax

Alternatively, hierarchicalsoftmax can be installed using pip from the git repository:

pip install git+https://github.com/rbturnbull/hierarchicalsoftmax.git

Usage

Build up a hierarchy tree for your categories using the SoftmaxNode instances:

from hierarchicalsoftmax import SoftmaxNode

root = SoftmaxNode("root")
a = SoftmaxNode("a", parent=root)
aa = SoftmaxNode("aa", parent=a)
ab = SoftmaxNode("ab", parent=a)
b = SoftmaxNode("b", parent=root)
ba = SoftmaxNode("ba", parent=b)
bb = SoftmaxNode("bb", parent=b)

The SoftmaxNode class inherits from the anytree Node class which means that you can use methods from that library to build and interact with your hierarchy tree.

The tree can be rendered as a string with the render method:

root.render(print=True)

This results in a text representation of the tree:

root
├── a
│   ├── aa
│   └── ab
└── b
    ├── ba
    └── bb

The tree can also be rendered to a file using graphviz if it is installed:

root.render(filepath="tree.svg")
An example tree rendering.

Then you can add a final layer to your network that has the right size of outputs for the softmax layers. You can do that manually by setting the output number of features to root.layer_size. Alternatively you can use the HierarchicalSoftmaxLinear or HierarchicalSoftmaxLazyLinear classes:

from torch import nn
from hierarchicalsoftmax import HierarchicalSoftmaxLinear

model = nn.Sequential(
    nn.Linear(in_features=20, out_features=100),
    nn.ReLU(),
    HierarchicalSoftmaxLinear(in_features=100, root=root)
)

Once you have the hierarchy tree, then you can use the HierarchicalSoftmaxLoss module:

from hierarchicalsoftmax import HierarchicalSoftmaxLoss

loss = HierarchicalSoftmaxLoss(root=root)

Metric functions are provided to show accuracy and the F1 score:

from hierarchicalsoftmax import greedy_accuracy, greedy_f1_score

accuracy = greedy_accuracy(predictions, targets, root=root)
f1 = greedy_f1_score(predictions, targets, root=root)

The nodes predicted from the final layer of the model can be inferred using the greedy_predictions function which provides a list of the predicted nodes:

from hierarchicalsoftmax import greedy_predictions

outputs = model(inputs)
inferred_nodes = greedy_predictions(outputs)

Relative contributions to the loss

The loss for each node can be weighted relative to each other by setting the alpha value for each parent node. By default the alpha value of a node is 1.

For example, the loss for the first level of classification (under the root node) will contribute twice as much to the loss than under the a or b nodes.

from hierarchicalsoftmax import SoftmaxNode

root = SoftmaxNode("root", alpha=2.0)
a = SoftmaxNode("a", parent=root)
aa = SoftmaxNode("aa", parent=a)
ab = SoftmaxNode("ab", parent=a)
b = SoftmaxNode("b", parent=root)
ba = SoftmaxNode("ba", parent=b)
bb = SoftmaxNode("bb", parent=b)

Example Usage

In the documentation, we provide an example of how to use this package with the CIFAR-10 and CIFAR-100 datasets.:

TreeDict

We provide a helper class to create a dictionary where items point to the nodes in the tree. This is useful for creating a dataloader in a machine learning model. See the TreeDict documentation for more information.

Label Smoothing

You can add label smoothing to the loss by setting the label_smoothing parameter to any of the nodes.

Focal Loss

You can use the Focal Loss instead of a basic cross-entropy loss for any of the nodes by setting the gamma parameter to any of the nodes.

Credits

Metadata

Release files for hierarchicalsoftmax 1.4.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hierarchicalsoftmax 1.4.4
File Size Uploaded Provenance
hierarchicalsoftmax-1.4.4.tar.gz 181.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for hierarchicalsoftmax 1.4.4
File Interpreter ABI Platform
hierarchicalsoftmax-1.4.4-py3-none-any.whl Python 3 none any Details

Total release size: 371.6 kB

Release files / hierarchicalsoftmax-1.4.4.tar.gz

Download URL hierarchicalsoftmax-1.4.4.tar.gz
Size 181.9 kB
Tags Source
SHA-256 checksum
How to use checksums
0f072d83193311b48ad36492c0e070a6e9d6729ef3e85e2a5e67a434ae1f0486
BLAKE2b-256 checksum
How to use checksums
799cf7e04251e053360fffd0dfcaa10ce7dc8602f5baa37a72f57300cb73e8af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.12.3 Linux/6.11.0-1015-azure

Release files / hierarchicalsoftmax-1.4.4-py3-none-any.whl

Download URL hierarchicalsoftmax-1.4.4-py3-none-any.whl
Size 189.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7187c0a672312073128ca50b92a750a0c8f8f143d62933469cca9d2ff18d03c7
BLAKE2b-256 checksum
How to use checksums
d2301752e04c65360d49a4fcd93806bd3cc18fabc91219ac02cf98a59cf23c30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.12.3 Linux/6.11.0-1015-azure

Security for hierarchicalsoftmax 1.4.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page