Skip to content

Adding network module (bare bones for now). #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added bindsnet/__init__.py
Empty file.
113 changes: 113 additions & 0 deletions bindsnet/network/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import torch
import os, sys
import numpy as np
import pickle as p

sys.path.append(os.path.abspath(os.path.join('..', 'bindsnet', 'network')))


def load_network(fname):
try:
return p.load(open(fname, 'rb'))
except FileNotFoundError:
print('Network not found on disk.')


class Network:
'''
Combines neuron nodes and connections into a spiking neural network.
'''
def __init__(self, dt=1):
self.dt = dt
self.layers = {}
self.connections = {}
self.monitors = {}

def add_layer(self, layer, name):
self.layers[name] = layer

def add_connections(self, connections, source, target):
self.connections[(source, target)] = connections

def add_monitor(self, monitor, name):
self.monitors[name] = monitor

def train(self, inputs, targets):
pass

def test(self, inputs):
pass

def evaluate(self, targets, predictions):
pass

def save(self, fname):
p.dump(self, open(fname, 'wb'))

def get_inputs(self):
inpts = {}
for key in self.connections:
weights = self.connections[key].w

source = self.connections[key].source
target = self.connections[key].target

if not key[1] in inpts:
inpts[key[1]] = {}

inpts[key[1]][key[0]] = source.s.float() @ weights

return inpts

def run(self, inpts, time):
'''
Run network for a single iteration.
'''
# Record spikes from each population over the iteration.
spikes = {}
for key in self.nodes:
spikes[key] = torch.zeros(int(time / self.dt), self.nodes[key].n)

for monitor in self.monitors:
self.monitors[monitor].reset()

# Get inputs to all neuron nodes from their parent neuron nodes.
inpts.update(self.get_inputs())

# Simulate neuron and synapse activity for `time` timesteps.
for timestep in range(int(time / self.dt)):
# Update each layer of nodes in turn.
for key in self.nodes:
self.nodes[key].step(inpts[key], self.mode, self.dt)

# Record spikes from this population at this timestep.
spikes[key][timestep, :] = self.nodes[key].s

# Update synapse weights if we're in training mode.
if self.train:
for synapse in self.connections:
if type(self.connections[synapse]) == connections.STDPconnections:
self.connections[synapse].update()

# Get inputs to all neuron nodes from their parent neuron nodes.
inpts.update(self.get_inputs())

for monitor in self.monitors:
self.monitors[monitor].record()

# Normalize synapse weights if we're in training mode.
if self.train:
for synapse in self.connections:
if type(self.connections[synapse]) == connections.STDPconnections:
self.connections[synapse].normalize()

return spikes

def reset(self, attrs):
'''
Reset state variables.
'''
for layer in self.layers:
for attr in attrs:
if hasattr(self.nodes[layer], attr):
self.nodes[layer].reset(attr)
Empty file added bindsnet/network/connections.py
Empty file.
Empty file added bindsnet/network/nodes.py
Empty file.