A Tiny Test Suite for pytorch based Machine Learning models, inspired by https://github.com/Thenerdstation/mltest/blob/master/mltest/mltest.py[mltest].
Chase Roberts lists out 4 basic tests in his https://medium.com/@keeper6928/mltest-automatically-test-neural-network-models-in-one-function-call-eb6f1fa5019d[medium post] about mltest.
torchtest is mostly a pytorch port of mltest(which was written for tensorflow).
== Installation
[source, bash]
----
pip install --upgrade torchtest
----
== Tests
[source, python]
----
# imports for examples
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
----
=== Variables Change
[source, python]
----
from torchtest import assert_vars_change
inputs = Variable(torch.randn(20, 20))
targets = Variable(torch.randint(0, 2, (20,))).long()
batch = [inputs, targets]
model = nn.Linear(20, 2)
# what are the variables?
print('Our list of parameters', [ np[0] for np in model.named_parameters() ])
# do they change after a training step?
# let's run a train step and see
assert_vars_change(
model=model,
loss_fn=F.cross_entropy,
optim=torch.optim.Adam(model.parameters()),
batch=batch)
----
[source, python]
----
""" FAILURE """
# let's try to break this, so the test fails
params_to_train = [ np[1] for np in model.named_parameters() if np[0] is not 'bias' ]
# run test now
assert_vars_change(
model=model,
loss_fn=F.cross_entropy,
optim=torch.optim.Adam(params_to_train),
batch=batch)
# YES! bias did not change
----
=== Variables Don't Change
[source, python]
----
from torchtest import assert_vars_same
# What if bias is not supposed to change, by design?
# test to see if bias remains the same after training
assert_vars_same(
model=model,
loss_fn=F.cross_entropy,
optim=torch.optim.Adam(params_to_train),
batch=batch,
params=[('bias', model.bias)]
)
# it does? good. let's move on
----
=== Output Range
[source, python]
----
from torchtest import test_suite
# NOTE : bias is fixed (not trainable)
optim = torch.optim.Adam(params_to_train)
loss_fn=F.cross_entropy
test_suite(model, loss_fn, optim, batch,
output_range=(-2, 2),
test_output_range=True
)
# seems to work
----
[source, python]
----
""" FAILURE """
# let's tweak the model to fail the test
model.bias = nn.Parameter(2 + torch.randn(2, ))
test_suite(
model,
loss_fn, optim, batch,
output_range=(-1, 1),
test_output_range=True
)
# as expected, it fails; yay!
----
=== NaN Tensors
[source, python]
----
""" FAILURE """
model.bias = nn.Parameter(float('NaN') * torch.randn(2, ))
test_suite(
model,
loss_fn, optim, batch,
test_nan_vals=True
)
----
=== Inf Tensors
[source, python]
----
""" FAILURE """
model.bias = nn.Parameter(float('Inf') * torch.randn(2, ))
test_suite(
model,
loss_fn, optim, batch,
test_inf_vals=True
)
----
Metadata
Release files for torchtest 0.5
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 | |
|---|---|---|---|---|
| torchtest-0.5.tar.gz | 4.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| torchtest-0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.5 kB
Release files / torchtest-0.5.tar.gz
| Download URL | torchtest-0.5.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e6cffe5a9c8a6d6dbc927413ec9f9f0ed00f9123b8eb1b08e0a84a4b2f6e0772
|
|
BLAKE2b-256 checksum How to use checksums |
de03c38ae616c3bd12ac99958df9e1fba9ba36c52c04bb87143d02f97c87cf6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/38.5.1 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.5.2
|
Release files / torchtest-0.5-py3-none-any.whl
| Download URL | torchtest-0.5-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9b70896df8f3e79b0cf77a80e7ebf28eae4e330e04477031f6b0e463cba14b9a
|
|
BLAKE2b-256 checksum How to use checksums |
42b1eb572ef3dce32bfcc3bf60be6360cef0d01a58bde0c76f2e205234d1ff08
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/38.5.1 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.5.2
|
Security for torchtest 0.5
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.