Skip to main content

PyYeast logo

Python PyTorch HackClub

LATEST RELEASE → See on PyPI ←

The project

What's this stuff?

Good question. PyYeast is a Python package built for create and handle SENN, aka Self Expanding Neural Networks. A SENN can be used to create an AI model that expand itself during the training, to improve its capacity.

I built it because I think SENN are very useful and powerful, so my goal is to create a package to make easier build and manage them, also implementing them in already existing NN.

All started when I was trying to build my first model using PyTorch, and I noticed that, every time I trained it, the loss would get stuck, at a certain point. So I thought: <<There is a way, when the loss freezes, to add a new layer and improve the results?>> Then I started do some researches on the Internet, discovering SENN and understanding better how do they specifically work.

After I learned the basics, I started to build this package, using low-level interaction with PyTorch components and trying to provide optimized logic and data flow.

I hope you'll find my project useful. Remember to leave a star ★!

Resources

yeast.layers

XLinear

The XLinear class provides an easy and quick way to create and use a SELL (Self Expanding Linear Layer)

Features:

  • It can expand its structure vertically, increasing its neurons number
  • It has got an input detecting function, so it can expand its number of weights at run-time to adapt for a new input size, due to a previous layer expansion
  • Every time it creates new weights (both vertically or inside a single neuron), it initializes them at 0 (or with tiny numbers), avoiding loss spikes in the firsts epochs.
  • With exp_rate, every layer can grow in a different way: a layer with a higher exp_rate grows faster than the others, so you can control which part of the model expands more
  • You can omit in_features: the layer detects it automatically on the first forward, so you don't need to count sizes by hand

Initializing

from yeast.layers import XLinear

sell = XLinear(in_features=10, out_features=22, exp_rate=2)

This is how create a XLinear object (it's pretty similar to a classic PyTorch Linear):

  • in_features: int/None: the input values in the incoming Tensor (like PyTorch). You can also omit this, the layer will detect it automatically the first epoch

  • out_features: int: the number of neurons of the layer, or if you prefer, the number of outputs value in the Tensor the layer calculates (here too, the same as PyTorch).

  • exp_rate: int: this value is used in the expansion. When the optimizer gives an expansion rate for the layers, that value is multiplied with this, so every layer can be expanded in a different way

Usage

import torch

from yeast.layers import XLinear

sell = XLinear(in_features=10, out_features=22, exp_rate=2)

sell.expand(5)

x = torch.randn(15, 10)  # creating a random input 

output = sell.forward(x)

These are the 2 attributes an XLinear instance has:

  • expand(expansion: int): how many neurons add to the layer. This value is multiplied with the exp_rate of the layer

  • forward(x: torch.Tensor): works like a PyTorch linear layer forward: get the input tensor and applies the layer's weights and biases to it, returning the output tensor.

XAdapter

Features

  • XAdapter is a double-layer sistem, consisting of two expanding layer, with A ReLU in the middle

  • What makes it special is that the second layer, the Filter Layer, that only expand "horizontally", keeping the size of its output tensor always the same.

  • In this way, you can add expanding layers in static neural networks, because the Filter Layer takes care, all by itself (you don't need to do anything) to give to the next layer, a static one, always the same size tensor

  • No manual reshaping: you can put a classic nn.Linear after an XAdapter and it will never break, even if the adapter grows inside

Initializing

from yeast.layers import XAdapter

adapter = XAdapter(in_features=15, out_features=20, exp_rate=2)

See XLinear, it's the same

Usage

See XLinear, it's the same


yeast.optimizer

XOptimWrapper

[!WARNING] This resource is deprecated. Use XOptimizer instead. XOptimWrapper needs a manual expansion and a manual step(). XOptimizer does both automatically, with the same object.

Features

  • XOptimWrapper is a special wrapper for PyTorch optimizers. In fact, a normal optimizers cannot work with an expanding layer, because the dicts of the parameters are set when the optimizer itself is created.

  • This class is built to solve this problem: it takes as arguments the model and the related optimizer, and every time the layers expand, it automatically updates the dicts of the parameters.

  • It also manages the expansion of all the layers (that it detects by itself when it's created) of the model

[!WARNING] If you're using a XOptimWrapper instance, be sure of use only it to expand the layers. Because if you expand a single layer manually, its parameters dicts won't be updated. I'll fix this in the next versions

Initializing

from torch import nn, optim

from yeast import optimizer

model = nn.Module()  

optim = optim.Adam(params=model.parameters())

x_optim_wrapper = optimizer.XOptimWrapper(module=model, optimizer=optim, max_params=1e7)

There are two arguments an XOptimWrapper needs when it's created:

  • module: nn.Module: the model, instance of nn.Module (it requires the entire module because it needs to get the expanding layers from it)

  • optimizer: optim.Optimizer: given the optimizer, every time the layers expand themselves, it updates the optimizer dict, leaving old progress and adding only the blank spaces for new data. All it's done automatically.

  • max_params: int | float: this value set a limit to the model size, so the expansion can't cause a memory saturation. It accepts both float and int so you can use exponential, but remember the number will be passed in an int() function!

Usage

"""
USING THE PRECEDENT SNIPPET RESOURCES
"""

x_optim_wrapper.layers_expand(expansion=3)

print(x_optim_wrapper.model_size(human=False)) # OUTPUT EXAMPLE: 2'589'567'776

print(x_optim_wrapper.model_size(human=True)) # OUTPUT EXAMPLE: 2.5T
  • layers_expand(expansion: int): for every expanding layers in the module passed in the argument, it expands his structure, like you calling layer.expand(expansion=4) for every expanding layer in the module

  • model_size(human: bool): return the number of parameters (understood as the individual weights and biases) of the model. With human as True, returns the number as a more readable alphanumeric combination (2.3T, 4B...), else returns the pure number (22345, 123...)

[!WARNING] If you use XOptimWrapper you've still to do loss.backward(), optimizer.step(), optimizer.zero_grad() and all that stuff, because the XOptimWrapper don't do it by itself

XOptimizer

Features

  • XOptimizer is a special version of PyTorch optimizers. In fact, a normal optimizer cannot work with an expanding layer, because the dicts of the parameters are set when the optimizer itself is created.

  • This class is built to solve this problem: you don't create the optimizer outside, and then you use a wrapper, instead you create the PyTorch optimizer and the SENN-compatible version with the same object.

  • It also manages the expansion of all the layers (that it detects by itself when it's created) of the model, using the GSS of every neuron. Every time it does a step, it checks if a layer needs to expand, and if yes, it expands it and automatically updates the dicts of the parameters.

  • No reset, no lost progress: when it expands a layer, it keeps the momentum of the old neurons exactly as it was, and the new neurons start with zero momentum. Nothing is restarted from scratch

  • It skips what must not grow: it skips the Filter Layers of the XAdapter, because they have exp_rate = 0, and it skips the layers with less than 2 neurons

[!WARNING] If you're using a XOptimizer instance, be sure to use only it to expand the layers and to do the step. Don't call optimizer.step() by yourself, because xstep() already does it. Because if you expand a single layer manually, its parameters dicts won't be updated.

Initializing

from torch import nn, optim

from yeast import optimizer

model = nn.Sequential(
    XLinear(in_features=10, out_features=16),
    nn.ReLU(),
    XLinear(in_features=16, out_features=1)
)

x_optimizer = optimizer.XOptimizer(algorithm=optim.Adam, model=model, params=model.parameters(), lr=0.01)

There are three arguments an XOptimizer needs when it's created, plus the ones of the classic optimizer:

  • algorithm: type[Optimizer]: the optimizer algorithm you choose for the optimizer. Use the same name you find on PyTorch (Adam, SGD...). It must be a subclass of PyTorch Optimizer.

  • model: nn.Module | Iterable[nn.Module]: the model, instance of nn.Module, or a list of them (it requires the entire module because it needs to get the expanding layers

Quickstart

To use this package, you first need Python 3.12 (or newest) on your device.

Then, run in your terminal:

pip install pyyeast

If you want to see the package in action, run the demonstration file (click here to see it) with

yeast

Credits

For create this projects, I used the incredible framework PyTorch. It gave my the resources to customize my componentes and build from scratch new solutions.

Thank also, to Hack Club for give a way to build, meet and play to dev teenagers from all around the world.

About me

Hi! I'm Alessio, a young dev from Verona, Italy 🇮🇹. CS is my passion, and I'm working to make it my future. If you're interested to know me, or you just want to have a talk about projects, development or something else... See my GitHub profile or my website. Have a good day!

Metadata

Release files for pyyeast 0.4.0

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

Source distribution (sdist)

Source distribution for pyyeast 0.4.0
File Size Uploaded Provenance Actions
pyyeast-0.4.0.tar.gz 15.2 kB Without provenance Details for pyyeast-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyyeast 0.4.0
File Interpreter ABI Platform Actions
pyyeast-0.4.0-py3-none-any.whl Python 3 none any Details for pyyeast-0.4.0-py3-none-any.whl

Total release size: 33.8 kB

Release files / pyyeast-0.4.0.tar.gz

Download URL pyyeast-0.4.0.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b75e57d9a2a6eb52e177890cd7caa31f1a865d3376de3952319037c9e993ec6a
BLAKE2b-256 checksum
How to use checksums
e9815b6e55e0bb50f8e24817c0731439f16d24594aab8151e9e079accab9cb8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Release files / pyyeast-0.4.0-py3-none-any.whl

Download URL pyyeast-0.4.0-py3-none-any.whl
Size 18.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3d78e3aa2129c8eca3eedc23b79206fc3f8fc0451bebb66d83985004b0c14ac2
BLAKE2b-256 checksum
How to use checksums
6e6fcfe4040bf313e6bf8da8df9391e9ec69a88403ad4401f56fe2201f46a95a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Security for pyyeast 0.4.0

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.

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