# Kala Torch
Kala Torch Module is a comprehensive PyTorch-based utility module designed to streamline and enhance deep learning workflows. It provides an abstraction over common PyTorch functionalities, making development faster and more intuitive.
## Features
- **Tensor Operations**
- Supports a wide range of tensor manipulations, including creation, slicing, and memory management.
- Check tensor types, perform tensor transformations, and handle complex tensors seamlessly.
- **Default Settings Management**
- Easily set and retrieve default data types and tensor configurations.
- Configure default tensor types for consistent operations across your codebase.
- **TorchScript Utilities**
- Simplify scripting and tracing models for deployment in production environments.
- Enable optimization and execution of models in non-Python setups, like mobile or embedded systems.
- Save and load scripted modules for reproducibility and deployment.
- **Neural Network (nn) Module Creation**
- Streamline the creation of common layers like Linear, Convolutional, ReLU, BatchNorm, and Dropout.
- Build complex architectures, including Transformers, LSTMs, and GRUs, with minimal effort.
- Define custom activation functions and loss functions to suit your specific needs.
- **Generative Adversarial Networks (GANs)**
- Easily create and train GANs with pre-defined Generator and Discriminator architectures.
- Train DCGAN models with minimal code using integrated utility functions.
- Generate high-quality images and visualize results with built-in sample generation.
- **Multiprocessing Utilities**
- Facilitate distributed training with tools for parallel processing.
- Utilize efficient tensor sharing strategies to optimize memory usage in multiprocessing environments.
- Easily spawn and manage worker processes for model training or data processing.
- **DataLoader Utilities**
- Seamlessly handle batching, shuffling, and multi-worker setups for datasets.
- Support for both map-style and iterable-style datasets, with custom collation functions.
- Enable memory pinning for faster GPU transfers and efficient data loading pipelines.
- **Custom Gradient Functions**
- Design and implement advanced gradient operations tailored for specific backpropagation needs.
- Customize both forward and backward passes for unique computational requirements.
- **Advanced Model Deployment**
- Utilize TorchScript and multiprocessing for efficient deployment in various environments.
- Enable GPU acceleration and optimize models for inference.
## Installation
Install the module using pip:
```bash
pip install kala-torch
```
## Usage
### Example: Neural Network Creation and Training
```python
import torch
import torch.nn as nn
from Kala_torch.Kala_torch import Kala_torch
# Initialize Kala Torch utility
kala = Kala_torch()
# Define a simple neural network using Kala Torch
class SimpleNN(nn.Module):
def __init__(self):
super(SimpleNN, self).__init__()
self.fc1 = kala.linear(10, 50) # Linear layer
self.relu = kala.relu() # ReLU activation
self.fc2 = kala.linear(50, 1) # Output layer
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x
# Create the model
model = SimpleNN()
# Define loss and optimizer
criterion = kala.cross_entropy_loss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
# Example input
inputs = torch.randn(32, 10)
targets = torch.randint(0, 2, (32, 1), dtype=torch.float)
# Training step
outputs = model(inputs)
loss = criterion(outputs, targets)
kala.backward(loss) # Perform backpropagation
optimizer.step()
```
### Example: Generative Adversarial Networks (GANs)
Kala Torch includes an easy-to-use interface for creating and training GANs.
```python
import torch
from torchvision.datasets import CIFAR10
from torchvision.transforms import Compose, ToTensor, Normalize, Resize
from torch.utils.data import DataLoader
from Kala_torch.Kala_torch import Kala_torch
# Initialize Kala Torch utility
kala = Kala_torch()
gan = kala.KalaGAN(latent_dim=100, image_channels=3, image_size=64)
# Define transformation for CIFAR10 dataset
transform = Compose([
Resize(64),
ToTensor(),
Normalize([0.5], [0.5]) # Normalize to [-1, 1]
])
# Load dataset
dataset = CIFAR10(root="./data", train=True, download=True, transform=transform)
data_loader = DataLoader(dataset, batch_size=64, shuffle=True)
# Device setup
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
gan.generator.to(device)
gan.discriminator.to(device)
# Training loop
num_epochs = 5
for epoch in range(num_epochs):
for i, (images, _) in enumerate(data_loader):
images = images.to(device)
losses = gan.train_step(images, device) # Pass the `device` argument here
if i % 100 == 0:
print(f"Epoch [{epoch + 1}/{num_epochs}], Step [{i + 1}/{len(data_loader)}], "
f"D Loss: {losses['d_loss']:.4f}, G Loss: {losses['g_loss']:.4f}")
# Save generated samples
gan.generate_samples(num_samples=64, save_path=f"samples_epoch_{epoch + 1}.png", device=device)
```
### Example: DataLoader with Multiprocessing
```python
from torch.utils.data import DataLoader, TensorDataset
# Sample dataset
data = torch.randn(100, 10)
targets = torch.randint(0, 2, (100,))
dataset = TensorDataset(data, targets)
# Create DataLoader
loader = kala.data_loader(dataset, batch_size=16, shuffle=True, num_workers=4)
# Iterate through DataLoader
for batch_idx, (inputs, labels) in enumerate(loader):
print(f"Batch {batch_idx}: {inputs.shape}, {labels.shape}")
```
### Example: TorchScript for Model Deployment
```python
# Convert model to TorchScript
scripted_model = kala.script(model)
# Save the scripted model
kala.save_script_module(scripted_model, "model_scripted.pt")
# Load the scripted model
loaded_model = kala.load_script_module("model_scripted.pt")
# Perform inference
example_input = torch.randn(1, 10)
output = loaded_model(example_input)
print("Scripted model output:", output)
```
## Contributing
Contributions are welcome! If you encounter issues or have feature requests, please open an issue or submit a pull request on the [GitHub repository](https://github.com/Kalasaikamesh944/Kala-torch).
## License
This project is licensed under the MIT License. See the LICENSE file for details.
Raw data
{
"_id": null,
"home_page": "https://github.com/Kalasaikamesh944",
"name": "kala-torch",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": null,
"author": "N V R K SAI KAMESH YADAVALLI",
"author_email": "saikamesh.y@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/de/6a/0b6ed079d0c8ec4ff834f9ea72bfb722957c21d424ce0de21263276fec06/kala_torch-0.1.3.tar.gz",
"platform": null,
"description": "# Kala Torch \r\n\r\nKala Torch Module is a comprehensive PyTorch-based utility module designed to streamline and enhance deep learning workflows. It provides an abstraction over common PyTorch functionalities, making development faster and more intuitive.\r\n\r\n## Features\r\n\r\n- **Tensor Operations**\r\n - Supports a wide range of tensor manipulations, including creation, slicing, and memory management.\r\n - Check tensor types, perform tensor transformations, and handle complex tensors seamlessly.\r\n\r\n- **Default Settings Management**\r\n - Easily set and retrieve default data types and tensor configurations.\r\n - Configure default tensor types for consistent operations across your codebase.\r\n\r\n- **TorchScript Utilities**\r\n - Simplify scripting and tracing models for deployment in production environments.\r\n - Enable optimization and execution of models in non-Python setups, like mobile or embedded systems.\r\n - Save and load scripted modules for reproducibility and deployment.\r\n\r\n- **Neural Network (nn) Module Creation**\r\n - Streamline the creation of common layers like Linear, Convolutional, ReLU, BatchNorm, and Dropout.\r\n - Build complex architectures, including Transformers, LSTMs, and GRUs, with minimal effort.\r\n - Define custom activation functions and loss functions to suit your specific needs.\r\n\r\n- **Generative Adversarial Networks (GANs)**\r\n - Easily create and train GANs with pre-defined Generator and Discriminator architectures.\r\n - Train DCGAN models with minimal code using integrated utility functions.\r\n - Generate high-quality images and visualize results with built-in sample generation.\r\n\r\n- **Multiprocessing Utilities**\r\n - Facilitate distributed training with tools for parallel processing.\r\n - Utilize efficient tensor sharing strategies to optimize memory usage in multiprocessing environments.\r\n - Easily spawn and manage worker processes for model training or data processing.\r\n\r\n- **DataLoader Utilities**\r\n - Seamlessly handle batching, shuffling, and multi-worker setups for datasets.\r\n - Support for both map-style and iterable-style datasets, with custom collation functions.\r\n - Enable memory pinning for faster GPU transfers and efficient data loading pipelines.\r\n\r\n- **Custom Gradient Functions**\r\n - Design and implement advanced gradient operations tailored for specific backpropagation needs.\r\n - Customize both forward and backward passes for unique computational requirements.\r\n\r\n- **Advanced Model Deployment**\r\n - Utilize TorchScript and multiprocessing for efficient deployment in various environments.\r\n - Enable GPU acceleration and optimize models for inference.\r\n\r\n## Installation\r\n\r\nInstall the module using pip:\r\n\r\n```bash\r\npip install kala-torch\r\n```\r\n\r\n## Usage\r\n\r\n### Example: Neural Network Creation and Training\r\n\r\n```python\r\nimport torch\r\nimport torch.nn as nn\r\nfrom Kala_torch.Kala_torch import Kala_torch\r\n\r\n# Initialize Kala Torch utility\r\nkala = Kala_torch()\r\n\r\n# Define a simple neural network using Kala Torch\r\nclass SimpleNN(nn.Module):\r\n def __init__(self):\r\n super(SimpleNN, self).__init__()\r\n self.fc1 = kala.linear(10, 50) # Linear layer\r\n self.relu = kala.relu() # ReLU activation\r\n self.fc2 = kala.linear(50, 1) # Output layer\r\n\r\n def forward(self, x):\r\n x = self.fc1(x)\r\n x = self.relu(x)\r\n x = self.fc2(x)\r\n return x\r\n\r\n# Create the model\r\nmodel = SimpleNN()\r\n\r\n# Define loss and optimizer\r\ncriterion = kala.cross_entropy_loss()\r\noptimizer = torch.optim.Adam(model.parameters(), lr=0.001)\r\n\r\n# Example input\r\ninputs = torch.randn(32, 10)\r\ntargets = torch.randint(0, 2, (32, 1), dtype=torch.float)\r\n\r\n# Training step\r\noutputs = model(inputs)\r\nloss = criterion(outputs, targets)\r\nkala.backward(loss) # Perform backpropagation\r\noptimizer.step()\r\n```\r\n\r\n### Example: Generative Adversarial Networks (GANs)\r\n\r\nKala Torch includes an easy-to-use interface for creating and training GANs.\r\n\r\n```python\r\nimport torch\r\nfrom torchvision.datasets import CIFAR10\r\nfrom torchvision.transforms import Compose, ToTensor, Normalize, Resize\r\nfrom torch.utils.data import DataLoader\r\nfrom Kala_torch.Kala_torch import Kala_torch\r\n\r\n# Initialize Kala Torch utility\r\nkala = Kala_torch()\r\ngan = kala.KalaGAN(latent_dim=100, image_channels=3, image_size=64)\r\n\r\n# Define transformation for CIFAR10 dataset\r\ntransform = Compose([\r\n Resize(64),\r\n ToTensor(),\r\n Normalize([0.5], [0.5]) # Normalize to [-1, 1]\r\n])\r\n\r\n# Load dataset\r\ndataset = CIFAR10(root=\"./data\", train=True, download=True, transform=transform)\r\ndata_loader = DataLoader(dataset, batch_size=64, shuffle=True)\r\n\r\n# Device setup\r\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\r\ngan.generator.to(device)\r\ngan.discriminator.to(device)\r\n\r\n# Training loop\r\nnum_epochs = 5\r\nfor epoch in range(num_epochs):\r\n for i, (images, _) in enumerate(data_loader):\r\n images = images.to(device)\r\n losses = gan.train_step(images, device) # Pass the `device` argument here\r\n\r\n if i % 100 == 0:\r\n print(f\"Epoch [{epoch + 1}/{num_epochs}], Step [{i + 1}/{len(data_loader)}], \"\r\n f\"D Loss: {losses['d_loss']:.4f}, G Loss: {losses['g_loss']:.4f}\")\r\n\r\n # Save generated samples\r\n gan.generate_samples(num_samples=64, save_path=f\"samples_epoch_{epoch + 1}.png\", device=device)\r\n\r\n```\r\n\r\n### Example: DataLoader with Multiprocessing\r\n\r\n```python\r\nfrom torch.utils.data import DataLoader, TensorDataset\r\n\r\n# Sample dataset\r\ndata = torch.randn(100, 10)\r\ntargets = torch.randint(0, 2, (100,))\r\ndataset = TensorDataset(data, targets)\r\n\r\n# Create DataLoader\r\nloader = kala.data_loader(dataset, batch_size=16, shuffle=True, num_workers=4)\r\n\r\n# Iterate through DataLoader\r\nfor batch_idx, (inputs, labels) in enumerate(loader):\r\n print(f\"Batch {batch_idx}: {inputs.shape}, {labels.shape}\")\r\n```\r\n\r\n### Example: TorchScript for Model Deployment\r\n\r\n```python\r\n# Convert model to TorchScript\r\nscripted_model = kala.script(model)\r\n\r\n# Save the scripted model\r\nkala.save_script_module(scripted_model, \"model_scripted.pt\")\r\n\r\n# Load the scripted model\r\nloaded_model = kala.load_script_module(\"model_scripted.pt\")\r\n\r\n# Perform inference\r\nexample_input = torch.randn(1, 10)\r\noutput = loaded_model(example_input)\r\nprint(\"Scripted model output:\", output)\r\n```\r\n\r\n## Contributing\r\n\r\nContributions are welcome! If you encounter issues or have feature requests, please open an issue or submit a pull request on the [GitHub repository](https://github.com/Kalasaikamesh944/Kala-torch).\r\n\r\n## License\r\n\r\nThis project is licensed under the MIT License. See the LICENSE file for details.\r\n\r\n",
"bugtrack_url": null,
"license": null,
"summary": "A comprehensive PyTorch-based utility module.",
"version": "0.1.3",
"project_urls": {
"Homepage": "https://github.com/Kalasaikamesh944"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "30477b0ac1d142898d874fd0999861fb051dd8a14fef8ece0d2844276d94ff02",
"md5": "a5783bc6960c5409fd11e3271fbcf2ba",
"sha256": "4150703845d7822911ec98d30a3d5b3f4fcdfa1553a037188f590f9b293a4d15"
},
"downloads": -1,
"filename": "kala_torch-0.1.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a5783bc6960c5409fd11e3271fbcf2ba",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 7805,
"upload_time": "2024-12-21T07:00:02",
"upload_time_iso_8601": "2024-12-21T07:00:02.356811Z",
"url": "https://files.pythonhosted.org/packages/30/47/7b0ac1d142898d874fd0999861fb051dd8a14fef8ece0d2844276d94ff02/kala_torch-0.1.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "de6a0b6ed079d0c8ec4ff834f9ea72bfb722957c21d424ce0de21263276fec06",
"md5": "5a3c175bb648795215d86aa6e0a43b35",
"sha256": "1fededc55d40c3532b98035d530aa96cc0feaec560f4c84697e92864db4c9c64"
},
"downloads": -1,
"filename": "kala_torch-0.1.3.tar.gz",
"has_sig": false,
"md5_digest": "5a3c175bb648795215d86aa6e0a43b35",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 7311,
"upload_time": "2024-12-21T07:00:04",
"upload_time_iso_8601": "2024-12-21T07:00:04.830746Z",
"url": "https://files.pythonhosted.org/packages/de/6a/0b6ed079d0c8ec4ff834f9ea72bfb722957c21d424ce0de21263276fec06/kala_torch-0.1.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-21 07:00:04",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "kala-torch"
}