departing
project_description
Install it from PyPI
pip install departing
Usage
First, upload your trained model using RemoteModel.upload(model) method.
Below, a simple XOR model is trained and uploaded.
Note: remember to install torch,numpy, onnx and onnxscript.
import torch
import numpy as np
from departing import RemoteModel
X = torch.Tensor([[0, 0], [0, 1], [1, 0], [1, 1]])
Y = torch.Tensor([[0], [1], [1], [0]])
class XOR(torch.nn.Module):
def __init__(self, input_dim=2, output_dim=1):
super(XOR, self).__init__()
self.lin1 = torch.nn.Linear(input_dim, 2)
self.lin2 = torch.nn.Linear(2, output_dim)
def forward(self, x):
x = self.lin1(x)
x = torch.nn.functional.sigmoid(x)
x = self.lin2(x)
return x
model = XOR()
loss_func = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
# Run the training itself
epochs = 2000
steps = X.size(0)
for i in range(epochs):
for j in range(steps):
data_point = np.random.randint(X.size(0))
x_var = torch.autograd.Variable(X[data_point], requires_grad=False)
y_var = torch.autograd.Variable(Y[data_point], requires_grad=False)
optimizer.zero_grad()
y_hat = model(x_var)
loss = loss_func.forward(y_hat, y_var)
loss.backward()
optimizer.step()
if i % 100 == 0:
print(f"Epoch: {i}, Loss: {loss.data.numpy()}")
# Export the model to a single ONNX file, so we can upload it.
example_input = torch.tensor([1.0, 1.0])
onnx_program = torch.onnx.dynamo_export(model, example_input)
onnx_file_path = "/tmp/my-xor-model.onnx"
onnx_program.save(onnx_file_path)
# Instantiate a new version of the remote model using your
# api key, and identifying the model name
remote_model = RemoteModel(
api_key="dpt_56583436b1ad4acb8fb9371ea72e9a09",
model="pappacena/my-xor-model",
)
remote_model.upload(onnx_file_path)
# Use the model:
input_values = [
[1.0, 1.0],
[0.0, 0.0],
[0.0, 1.0],
[1.0, 0.0],
]
for input_value in input_values:
output = remote_model(input_value)
print(f"{input_value} ~> {output[0]}")
To call a previously uploaded BERT model, for example:
from departing import RemoteModel
from transformers import BertTokenizer
import numpy as np
# Classes used during training
target_classes = {
0: "happy",
1: "not-relevant",
2: "angry",
3: "disgust",
4: "sad",
5: "surprise",
}
# Create a BERT tokenizer
tokenizer = BertTokenizer.from_pretrained(
"bert-base-uncased",
do_lower_case=True,
)
test_texts = [
"I'm so happy today! Uhuu!",
"My soccer team just lost the finals...",
"OMG! What is happening here? I just won the lotery!",
]
# Create the tokenized version of 2 test phrases
tokenized_tests = tokenizer.batch_encode_plus(
test_texts,
add_special_tokens=True,
return_attention_mask=True,
pad_to_max_length=True,
max_length=256,
return_tensors="pt",
)
# Convert tensors to numpy
np_tokenized_tests = {k: v.numpy() for k, v in tokenized_tests.items()}
# This will run the model remotely, and return the same result it would
# return if executed locally
model = RemoteModel(
api_key="dpt_56583436b1ad4acb8fb9371ea72e9a09",
model="pappacena/my-fine-tuned-bert",
)
outputs = model(**np_tokenized_tests)
for text, logit in zip(test_texts, outputs[0]):
output_cls = int(np.argmax(logit))
print(f"Class #{target_classes[output_cls]}: {text}")
Development
Read the CONTRIBUTING.md file.
Metadata
Release files for departing 0.0.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 |
|---|---|---|---|---|
| departing-0.0.2.tar.gz | 6.0 kB | Without provenance | Details for departing-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| departing-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for departing-0.0.2-py3-none-any.whl |
Total release size: 11.4 kB
Release files / departing-0.0.2.tar.gz
| Download URL | departing-0.0.2.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
348078962ef4b6120c6578e28fc8b6fd99f29bd6ce2e8909e6857f8c37cfd261
|
|
BLAKE2b-256 checksum How to use checksums |
ba7808410684404355aa3e61acecd4a4903b16b816b7f639bdc2043b15730082
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.1
|
Release files / departing-0.0.2-py3-none-any.whl
| Download URL | departing-0.0.2-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8b70b7d99a422bf35196647f82473eccf5f16d321b067f2953272942190db560
|
|
BLAKE2b-256 checksum How to use checksums |
59ed561e8bede606c896930be421601e045b119ca043fd550c4d9a17ba0adcbc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.1
|
Security for departing 0.0.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.