Central object of the ``bindsnet`` package. Responsible for the simulation and interaction of nodes and connections. **Example:** .. code-block:: python import torch import matplotlib.pyplot as plt from bindsnet import encoding from bindsn
| 29 | |
| 30 | |
| 31 | class Network(torch.nn.Module): |
| 32 | # language=rst |
| 33 | """ |
| 34 | Central object of the ``bindsnet`` package. Responsible for the simulation and |
| 35 | interaction of nodes and connections. |
| 36 | |
| 37 | **Example:** |
| 38 | |
| 39 | .. code-block:: python |
| 40 | |
| 41 | import torch |
| 42 | import matplotlib.pyplot as plt |
| 43 | |
| 44 | from bindsnet import encoding |
| 45 | from bindsnet.network import Network, nodes, topology, monitors |
| 46 | |
| 47 | network = Network(dt=1.0) # Instantiates network. |
| 48 | |
| 49 | X = nodes.Input(100) # Input layer. |
| 50 | Y = nodes.LIFNodes(100) # Layer of LIF neurons. |
| 51 | C = topology.Connection(source=X, target=Y, w=torch.rand(X.n, Y.n)) # Connection from X to Y. |
| 52 | |
| 53 | # Spike monitor objects. |
| 54 | M1 = monitors.Monitor(obj=X, state_vars=['s']) |
| 55 | M2 = monitors.Monitor(obj=Y, state_vars=['s']) |
| 56 | |
| 57 | # Add everything to the network object. |
| 58 | network.add_layer(layer=X, name='X') |
| 59 | network.add_layer(layer=Y, name='Y') |
| 60 | network.add_connection(connection=C, source='X', target='Y') |
| 61 | network.add_monitor(monitor=M1, name='X') |
| 62 | network.add_monitor(monitor=M2, name='Y') |
| 63 | |
| 64 | # Create Poisson-distributed spike train inputs. |
| 65 | data = 15 * torch.rand(100) # Generate random Poisson rates for 100 input neurons. |
| 66 | train = encoding.poisson(datum=data, time=5000) # Encode input as 5000ms Poisson spike trains. |
| 67 | |
| 68 | # Simulate network on generated spike trains. |
| 69 | inputs = {'X' : train} # Create inputs mapping. |
| 70 | network.run(inputs=inputs, time=5000) # Run network simulation. |
| 71 | |
| 72 | # Plot spikes of input and output layers. |
| 73 | spikes = {'X' : M1.get('s'), 'Y' : M2.get('s')} |
| 74 | |
| 75 | fig, axes = plt.subplots(2, 1, figsize=(12, 7)) |
| 76 | for i, layer in enumerate(spikes): |
| 77 | axes[i].matshow(spikes[layer], cmap='binary') |
| 78 | axes[i].set_title('%s spikes' % layer) |
| 79 | axes[i].set_xlabel('Time'); axes[i].set_ylabel('Index of neuron') |
| 80 | axes[i].set_xticks(()); axes[i].set_yticks(()) |
| 81 | axes[i].set_aspect('auto') |
| 82 | |
| 83 | plt.tight_layout(); plt.show() |
| 84 | """ |
| 85 | |
| 86 | def __init__( |
| 87 | self, |
| 88 | dt: float = 1.0, |
no outgoing calls