Skip to main content

pytorch implementation of Stigmergic Neural Netowrks

Project description


pytorch implementation of the Stigmergic Neural Networks as presented in the paper Using stigmergy to incorporate the time into artificial neural networks.

This package was wrote with the intent to make as easy as possible to integrate the Stigmergic Neural Networks into the existing models.

You can safely mix native pytorch Modules with ours.
The only catch is that you should use StigmergicModule (which extends pytorch's Module) as base class for your models in order to be able to tick() and reset() them.

Implementing our proposed architecture to solve MNIST becomes as easy as:

import torch
import torchsnn

net = torchsnn.Sequential(
    torchsnn.SimpleLayer(28, 10),
    torchsnn.FullLayer(10, 10),
    torchsnn.TemporalAdapter(10, 28),
    torch.nn.Linear(10*28, 10),

You can train a StigmergicModule as you would do with a pytorch's Module, but don't forget to reset() and tick() it!

optimizer = torch.optim.Adam(net.parameters(), lr = 0.001)

for i in range(0,N):
    for X,Y in zip(dataset_X, dataset_Y):
        out = None
        for xi in X:
            out = net(torch.tensor(xi, dtype=torch.float32))
        loss = (Y-out)**2

Does it support batch inputs?

Yes! if you forward into a StigmergicModule a batch of inputs it will return a batch of outputs

for t in range(0, num_ticks):
    batch_out[0], batch_out[1], ... = net(torch.tensor([batch_in[0][t], batch_in[1][t], ...]))

Can it run on CUDA?

Yes and as you will expect from a pytorch Module!
You just need to call the to(device) method on a model to move it in the GPU memory

device = torch.device("cuda")

net =

net.forward(torch.tensor(..., device=device))



Base class for a stigmergic network or layer.
If you are writing your own StigmergicModule you have to implement the functions

  • tick()
  • reset()

If you are using others StigmergicModule it will propagate these calls in its subtree.
For example if you want to build a network with a Linear and a SimpleLayer you can do something like:

import torch
import torchsnn

class TestNetwork(torchsnn.StigmergicModule):
    def __init__(self):
        self.linear = torch.nn.Linear(2,5)
        self.stigmergic = torchsnn.SimpleLayer(5,2)

    def forward(self, input):
        l1 = torch.sigmoid(self.linear(input))
        l2 = self.stigmergic(l1)
        return l2

net = TestNetwork()


Function with the same interface of torch.nn.Sequential for building sequential networks.
The same network of the previous example can be built with:

import torch
import torchsnn

net = torchsnn.Sequential(


It this layer only the thresholds are stigmergic variables and their stimuli are the output values.


In this layer both thresholds and weights are stigmergic variables and their stimuli are respectively the output values and the input ones.


We can't wait to see what you will build with the Stigmergic Neural Networks!
When you will publish your work you can use this BibTex to cite us :)

,	author	= {Galatolo, Federico A and Cimino, Mario GCA and Vaglini, Gigliola}
,	title	= {Using stigmergy to incorporate the time into artificial neural networks}
,	journal	= {MIKE 2018}
,	year	= {2018}


This code is released under GNU/GPLv3 so feel free to fork it and submit your changes, every PR helps.
If you need help using it or for any question please reach me at or on Telegram @galatolo

Project details

Download files

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

Files for torchsnn, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size torchsnn-0.1.0.tar.gz (5.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page