Skip to main content

A lightweight PyTorch utility toolkit for tensors

Project description

tensor-toolkit

A lightweight PyTorch utility toolkit for tensor operations, validation, and model utilities.


Features and Function Descriptions

Tensor Utilities

  • to_device(tensor, device=None)
    Move a tensor to the specified device (CPU or GPU). Automatically selects GPU if available when no device is provided.

  • get_device(tensor)
    Returns the current device of the given tensor.

  • seed_all(seed=42)
    Set random seeds for PyTorch (CPU and GPU) and NumPy to ensure reproducible results.

  • count_params(model, trainable_only=True)
    Count the number of parameters in a PyTorch model. Optionally count only trainable parameters.

  • tensor_summary(tensor)
    Print a detailed summary of a tensor including shape, dtype, device, and statistics (min, max, mean, std).

  • one_hot(labels, num_classes)
    Convert a tensor of label indices into one-hot encoded format.

  • accuracy(outputs, targets)
    Compute classification accuracy by comparing model outputs (logits or probabilities) to target labels.


Tensor Checks

  • is_tensor(x)
    Check whether an object is a PyTorch tensor.

  • has_nan(x)
    Check if the tensor contains any NaN values.

  • has_inf(x)
    Check if the tensor contains any positive or negative infinity values.

  • check_tensor_validity(x, raise_error=False)
    Validate the tensor for correct type, and absence of NaN or Inf values. Raises exceptions if raise_error=True; otherwise, returns status messages.


Installation

pip install tensor-toolkit

Requirements

  • torch >=1.10.0
  • numpy >=1.21.0 (used for seeding and numerical utilities)

To install requirements:

pip install -r requirements.txt

Usage Example

import torch
import tensorkit as tk

def main():
    # Create a random tensor
    x = torch.randn(3, 3)
    print("Original tensor device:", tk.get_device(x))

    # Move tensor to device (GPU if available)
    x = tk.to_device(x)
    print("Tensor moved to device:", tk.get_device(x))

    # Set seed for reproducibility
    tk.seed_all(42)
    t1 = torch.randn(2, 2)
    tk.seed_all(42)
    t2 = torch.randn(2, 2)
    print("Seed reproducible two tensors equal:", torch.equal(t1, t2))

    # Count parameters of a model
    model = torch.nn.Linear(4, 2)
    print(f"Model trainable parameters: {tk.count_params(model)}")

    # Print tensor summary
    print("Tensor summary:")
    tk.tensor_summary(x)

    # One-hot encode labels
    labels = torch.tensor([0, 1, 2])
    one_hot_labels = tk.one_hot(labels, num_classes=3)
    print("One-hot encoded labels:\n", one_hot_labels)

    # Calculate accuracy given model outputs and targets
    outputs = torch.tensor([[0.8, 0.1, 0.1],
                            [0.2, 0.7, 0.1],
                            [0.1, 0.2, 0.7]])
    targets = torch.tensor([0, 1, 2])
    acc = tk.accuracy(outputs, targets)
    print(f"Accuracy: {acc:.2%}")

    # Check tensor validity
    print("Is tensor:", tk.is_tensor(x))
    print("Contains NaN:", tk.has_nan(x))
    print("Contains Inf:", tk.has_inf(x))
    print("Tensor validity:", tk.check_tensor_validity(x))

if __name__ == "__main__":
    main()

Project Structure

tensor-toolkit/             # Project root folder
├── examples/               # Example scripts
│ └── usage_example.py      # Usage demonstration
├── tensorkit/              # Main package folder
│ ├── init.py               # API exports
│ ├── tensor_utils.py       # Tensor utility functions
│ ├── tensor_checks.py      # Tensor validation functions
├── tests/                  # Unit tests
│ ├── init.py
│ ├── test_tensor_utils.py  # Tests for tensor_utils
│ └── test_tensor_checks.py # Tests for tensor_checks
├── LICENSE                 # MIT License
├── pyproject.toml          # Project configuration
├── README.md               # Project documentation
├── requirements.txt        # Package dependencies

Contact

Created by Suresh K — GitHub


License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tensor_toolkit-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tensor_toolkit-0.1.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file tensor_toolkit-0.1.0.tar.gz.

File metadata

  • Download URL: tensor_toolkit-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for tensor_toolkit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b1894032a19d9303f313e65231aa1b40073a867cc4526cfccfa17f03af7b2563
MD5 3c2b2f3544b5b0303dfe4aaefc735212
BLAKE2b-256 d4d6fd4e9500d7bcbc667dcec8a1ce06d0862dc0ddc945fc1dcc2bdf8fcef819

See more details on using hashes here.

File details

Details for the file tensor_toolkit-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tensor_toolkit-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for tensor_toolkit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 078a6d13609d32fb5c5f3b2e4a2315b95497205af4f5937f4275372bd8e6f990
MD5 4c9b25908b71150492adc065e28a0008
BLAKE2b-256 8f8612f5c186f4a43306f7635fc982e251498f7ef0759c7ff402185ebe839d10

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page