polytorch
Embeddings and loss functions for different data types.
Installation
Requires Python 3.10 or later. Install using pip:
pip install polytorch
Or install the latest version from GitHub:
pip install git+https://github.com/rbturnbull/polytorch.git
Static image export uses Kaleido 1.x and requires Chrome or Chromium. If no compatible browser is installed, install one with:
plotly_get_chrome
Data Types
This package allows you to input and output different data types in PyTorch models.
Binary Data
from polytorch import BinaryData
binary_data = BinaryData()
# Or with labels and colors
binary_data = BinaryData(labels=["no_feature", "with_feature"], colors=["red", "blue"])
Categorical Data
from polytorch import CategoricalData
category_count = 5 # Number of categories
categorical_data = CategoricalData(category_count)
# Or with labels, colors and label smoothing
categorical_data = CategoricalData(
category_count=category_count,
labels=["cat", "dog", "fish", "bird", "reptile"],
colors=["red", "blue", "green", "yellow", "purple"],
label_smoothing=0.1,
)
Ordinal Data
from polytorch import OrdinalData
ordinal_data = OrdinalData(category_count=5)
# Or with color
ordinal_data = OrdinalData(category_count=5, color="pink")
Continuous Data
from polytorch import ContinuousData
continuous_data = ContinuousData()
# Or with color
continuous_data = ContinuousData(color="orange")
Hierarchical Data
from polytorch import HierarchicalData
from hierarchicalsoftmax import SoftmaxNode
root = SoftmaxNode("root")
child1 = SoftmaxNode("child1", parent=root)
child2 = SoftmaxNode("child2", parent=root)
tip1 = SoftmaxNode("tip1", parent=child1)
tip2 = SoftmaxNode("tip2", parent=child1)
tip3 = SoftmaxNode("tip3", parent=child2)
tip4 = SoftmaxNode("tip4", parent=child2)
hierarchical_data = HierarchicalData(root)
Build a hierarchy from slash-separated paths with from_series:
import pandas as pd
hierarchical_data, node_ids = HierarchicalData.from_series(
"taxonomy",
pd.Series(["Animal/Mammal/Dog", "Animal/Bird/Eagle", " Animal / Mammal / Dog "]),
)
assert node_ids[0] == node_ids[2]
Whitespace around each path component is stripped. Shared paths reuse nodes. Multiple top-level names are joined under a virtual __root__ node.
Embedding your data
from torch import nn
from polytorch import PolyEmbedding
class MyModule(nn.Module):
def __init__(self, embedding_size:int=128):
super(MyModule, self).__init__()
input_types = [binary_data, categorical_data] # for example. Could be other data types as well.
self.embedding = PolyEmbedding( input_types=input_types, embedding_size=embedding_size)
# Other modules
...
def forward(self, x_binary, x_categorical):
embedded = self.embedding( x_binary, x_categorical )
# Use the embedded features in your model
...
Outputting your data
You can also get your model to output to different data types.
from torch import nn
from polytorch import PolyLazyLinear
output_types = [
CategoricalData(category_count=5, loss_weighting=0.5), # For example, a categorical output with 5 categories
BinaryData(loss_weighting=1.0), # A binary output
ContinuousData(loss_weighting=0.1) # A continuous output
]
class MyModule(nn.Module):
def __init__(self, output_types):
super(MyModule, self).__init__()
self.output = PolyLazyLinear(output_types=output_types)
def forward(self, x):
# Your model logic
...
# Output to different data types
return self.output(x)
Use these data types to configure the loss:
from polytorch import PolyLoss
loss_module = PolyLoss(data_types=output_types)
# In your training loop
loss = loss_module(predictions, categorical_target, binary_target, continuous_target)
DataFrames
Use PolyDataFrame with a pandas dataframe or a CSV file supplied as a pathlib.Path. Annotate columns as name:type; unannotated columns are available by name but are omitted from row tuples and data_types.
import pandas as pd
from polytorch import PolyDataFrame
frame = PolyDataFrame(pd.DataFrame({
"id": [100, 101],
"tree:Hierarchical": ["Animal/Dog", "Animal/Cat"],
"score:continuous": [0.25, 0.75],
}))
tree_id, score = frame[0]
assert score == 0.25
assert frame["score", 1] == 0.75
assert frame["id"][0] == 100
assert [data_type.name for data_type in frame.data_types] == ["tree", "score"]
Types accept an optional Data suffix (for example, ContinuousData). Use data_columns={"score"} to select annotated columns by name. Row tuples and data_types follow the original column order. Categorical and ordinal values are returned unchanged; supply integer category indices for use with embeddings and cross-entropy loss.
Credits
Robert Turnbull (Melbourne Data Analytics Platform, The University of Melbourne)
Metadata
Release files for PolyTorch 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| polytorch-0.2.2.tar.gz | 21.5 kB | Without provenance | Details for polytorch-0.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| polytorch-0.2.2-py3-none-any.whl | Python 3 | none | any | Details for polytorch-0.2.2-py3-none-any.whl |
Total release size: 45.4 kB
Release files / polytorch-0.2.2.tar.gz
| Download URL | polytorch-0.2.2.tar.gz |
|---|---|
| Size | 21.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
726336107ffb6864a0339e60c9c68c6a34db10d021245fb54c3272d9e9dbacda
|
|
BLAKE2b-256 checksum How to use checksums |
3a2061ac4eac232cd57b33f60c4ef050fa15f6034e4536c2ca61d0c60f26e480
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.13.1 Darwin/25.6.0
|
Release files / polytorch-0.2.2-py3-none-any.whl
| Download URL | polytorch-0.2.2-py3-none-any.whl |
|---|---|
| Size | 23.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d5b1575ba4eb0b415687332fae521b9b9551c259c5d539262665da1f5b905432
|
|
BLAKE2b-256 checksum How to use checksums |
ba49675a8d354b2688cbc53518696fb61df7bbb1b6b7c9de253d28f04f0ef915
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.13.1 Darwin/25.6.0
|
Security for PolyTorch 0.2.2
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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.