Skip to main content

Torch PSO

Particle Swarm Optimization is an optimization technique that iteratively attempts to improve a list of candidate solutions. Each candidate solution is called a "particle", and collectively they are called a "swarm". In each step of the optimization, each particle moves in a random directly while simultaneously being pulled towards the other particles in the swarm. A simple introduction to the algorithm can be found on its Wikipedia article.

This package implements the Particle Swarm Optimization using the PyTorch Optimizer API, making it compatible with most pre-existing Torch training loops.

Installation

To install Torch PSO using PyPI, run the following command:

$ pip install torch-pso

Getting Started

To use the ParticleSwarmOptimizer, simply import it, and use it as with any other PyTorch Optimizer. Hyperparameters of the optimizer can also be specified. In practice, most PyTorch tutorials could be used to create a use-case, simply substituting the ParticleSwarmOptimizer for any other optimizer. A simplified use-case can be seen below, which trains a simple neural network to match its output to a target.

import torch
from torch.nn import Sequential, Linear, MSELoss
from torch_pso import ParticleSwarmOptimizer

net = Sequential(Linear(10,100), Linear(100,100), Linear(100,10))
optim = ParticleSwarmOptimizer(net.parameters(),
                               inertial_weight=0.5,
                               num_particles=100,
                               max_param_value=1,
                               min_param_value=-1)
criterion = MSELoss()
target = torch.rand((10,)).round()

x = torch.rand((10,))
for _ in range(100):
    
    def closure():
        # Clear any grads from before the optimization step, since we will be changing the parameters
        optim.zero_grad()  
        return criterion(net(x), target)
    
    optim.step(closure)
    print('Prediciton', net(x))
    print('Target    ', target)

Metadata

Release files for torch-pso 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torch-pso 1.2.1
File Size Uploaded
torch_pso-1.2.1.tar.gz 13.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torch-pso 1.2.1
File Interpreter ABI Platform
torch_pso-1.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 33.4 kB

Release files / torch_pso-1.2.1.tar.gz

Download URL torch_pso-1.2.1.tar.gz
Size 13.2 kB
Tags Source
SHA-256 checksum
How to use checksums
27803976b9fd106c918e80f0f2e563d1ac8fd2c69ab05a34d23f378dca24d37f
BLAKE2b-256 checksum
How to use checksums
f7372fd738ceb78d0b4f71792ed838752a1757bab789e91b752ba98d9399c38f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / torch_pso-1.2.1-py3-none-any.whl

Download URL torch_pso-1.2.1-py3-none-any.whl
Size 20.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f050b9121127a50804786a152604ecffd66310fb3b5161c78cecf1e011081605
BLAKE2b-256 checksum
How to use checksums
ea92da167d0cbc3d325d482b8cb787983fd9af3985fa286ac10f76a47804e8d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page