def __init__(self, path): super(emgFeature, self).__init__() netParams = snn.params(path + '/network.yaml') # initialize slayer slayer = snn.loihi(netParams['neuron'], netParams['simulation']) self.slayer = slayer # define network functions self.fc1 = slayer.dense(16, 128) self.fc2 = slayer.dense(128, 128) self.fc3 = slayer.dense(128, 5) self.delay1 = slayer.delay(128) self.delay2 = slayer.delay(128) self.fc1.weight.data = torch.FloatTensor( np.load(path + '/Trained/fc1Weights.npy').reshape( self.fc1.weight.shape)) self.fc2.weight.data = torch.FloatTensor( np.load(path + '/Trained/fc2Weights.npy').reshape( self.fc2.weight.shape)) self.fc3.weight.data = torch.FloatTensor( np.load(path + '/Trained/fc3Weights.npy').reshape( self.fc3.weight.shape)) self.delay1.delay.data = torch.FloatTensor( np.load(path + '/Trained/delay1.npy').reshape( self.delay1.delay.shape)) self.delay2.delay.data = torch.FloatTensor( np.load(path + '/Trained/delay2.npy').reshape( self.delay2.delay.shape))
def __init__(self, path): super(dvsFeature, self).__init__() netParams = snn.params(path + '/network.yaml') # initialize slayer slayer = snn.loihi(netParams['neuron'], netParams['simulation']) self.slayer = slayer # define network functions self.conv1 = slayer.conv( 2, 8, 3, padding=1, weightScale=20) self.conv2 = slayer.conv( 8, 16, 3, padding=1, weightScale=100) self.conv3 = slayer.conv(16, 32, 3, padding=1, weightScale=100) self.fc1 = slayer.dense((10*10*32), 512) self.fc2 = slayer.dense(512, 5) self.pool1 = slayer.pool(2) self.pool2 = slayer.pool(2) self.fc1 .weight.data = torch.FloatTensor(np.load(path + '/Trained/fc1Weights.npy' ).reshape(self.fc1 .weight.shape)) self.fc2 .weight.data = torch.FloatTensor(np.load(path + '/Trained/fc2Weights.npy' ).reshape(self.fc2 .weight.shape)) self.conv1.weight.data = torch.FloatTensor(np.load(path + '/Trained/conv1Weights.npy').reshape(self.conv1.weight.shape)) self.conv2.weight.data = torch.FloatTensor(np.load(path + '/Trained/conv2Weights.npy').reshape(self.conv2.weight.shape)) self.conv3.weight.data = torch.FloatTensor(np.load(path + '/Trained/conv3Weights.npy').reshape(self.conv3.weight.shape)) self.pool1.weight.data = torch.FloatTensor(np.load(path + '/Trained/pool1Weights.npy').reshape(self.pool1.weight.shape)) self.pool2.weight.data = torch.FloatTensor(np.load(path + '/Trained/pool2Weights.npy').reshape(self.pool2.weight.shape))
plt.figure(15) plt.hist(pool1Weights, 256) plt.title('pool1 weights') plt.figure(16) plt.hist(pool2Weights, 256) plt.title('pool2 weights') plt.figure(17) plt.hist(pool3Weights, 256) plt.title('pool3 weights') if __name__ == '__main__': netParams = snn.params('network.yaml') # Define the cuda device to run the code on. device = torch.device('cuda') # deviceIds = [2, 3] # Create network instance. net = Network(netParams).to(device) # net = torch.nn.DataParallel(Network(netParams).to(device), device_ids=deviceIds) # Create snn loss instance. error = snn.loss(netParams, spikeLayer).to(device) # Define optimizer module. # optimizer = torch.optim.Adam(net.parameters(), lr = 0.01, amsgrad = True) optimizer = optimizer.Nadam(net.parameters(), lr=0.01, amsgrad=True)
import sys, os CURRENT_TEST_DIR = os.getcwd() sys.path.append(CURRENT_TEST_DIR + "/../../src") from datetime import datetime import numpy as np import matplotlib.pyplot as plt import torch from torch.utils.data import Dataset, DataLoader import slayerSNN as snn from learningStats import learningStats import zipfile #----------------------Nacitanie potrebnych kniznic---------------------- netParams = snn.params('network.yaml') # Nacitanie konfiguracie SNN # Definicia triedy dataset class nmnistDataset(Dataset): def __init__(self, datasetPath, sampleFile, samplingTime, sampleLength): self.path = datasetPath # Cesta k databaze self.samples = np.loadtxt(sampleFile).astype('int') # Cesta k anotaciam self.samplingTime = samplingTime self.nTimeBins = int(sampleLength / samplingTime) def __getitem__(self, index): inputIndex = self.samples[index, 0] # Nazov suboru z anotacie classLabel = self.samples[index, 1] # Trieda inputSpikes = snn.io.read2Dspikes( self.path + str(inputIndex.item()) + '.bs2' ).toSpikeTensor(torch.zeros((2,34,34,self.nTimeBins)), samplingTime=self.samplingTime)# Nacitanie vzoriek
help="Batch Size.", required=True) parser.add_argument( "--loss", type=str, help="Loss function to use.", choices=["NumSpikes", "WeightedNumSpikes"], required=True, ) args = parser.parse_args() LOSS_TYPES = ["NumSpikes", "WeightedNumSpikes"] params = snn.params(args.network_config) if args.task == "cw": output_size = 20 elif args.task == "slip": output_size = 2 elif args.task == "ycb": output_size = 36 else: raise ValueError("Invalid args.task") if args.mode == "tact": model = SlayerMLP model_args = { "params": params, "input_size": 156 if args.fingers == "both" else 78,
import sys, os CURRENT_TEST_DIR = os.path.dirname(os.path.realpath(__file__)) sys.path.append(CURRENT_TEST_DIR + "/../src") import unittest import numpy as np import matplotlib.pyplot as plt import torch import slayerSNN as snn import slayerCuda device = torch.device('cuda') Ts = 0.1 netParams = snn.params('test_files/nmnistNet.yaml') slayer = snn.layer(netParams['neuron'], netParams['simulation']).to(device) # (N, C, H, W, D) = (2, 5, 6, 7, 50) (N, C, H, W, D) = (5, 10, 20, 30, 500) # Uncomment this to test large neuron sizes # (N, C, H, W, D) = (5, 16, 128, 128, 500) delay = slayer.delay((C, H, W)).to(device) inTensor = torch.randn((N, C, H, W, D)).to(device) def checkShift(inTensor, outTensor, shift, verbose=False): if shift > 0: error = torch.norm(inTensor[:-shift] - outTensor[shift:]).item()
# train a multilayer SNN to produce spike raster that resembles Oxford house. # The input and output both consists of 200 neurons each and the spkes span # approximately 1900ms. The input and output spike pair are taken from # SuperSpike repository (https://github.com/fzenke/pub2018superspike). ############################################################################### import sys, os CURRENT_TEST_DIR = os.getcwd() sys.path.append(CURRENT_TEST_DIR + "/../../src") import numpy as np import matplotlib.pyplot as plt import torch import slayerSNN as snn # Read SNN configuration from yaml file netParams = snn.params('oxford.yaml') Ts = netParams['simulation']['Ts'] Ns = int(netParams['simulation']['tSample'] / netParams['simulation']['Ts']) Nin = int(netParams['layer'][0]['dim']) Nhid = int(netParams['layer'][1]['dim']) Nout = int(netParams['layer'][2]['dim']) # Define the network class Network(torch.nn.Module): def __init__(self, netParams): super(Network, self).__init__() # initialize slayer slayer = snn.loihi(netParams['neuron'], netParams['simulation']) self.slayer = slayer
# Extract NMNISTsmall dataset with zipfile.ZipFile('NMNISTsmall.zip') as zip_file: for member in zip_file.namelist(): if not os.path.exists('./' + member): zip_file.extract(member, './') if __name__ == '__main__': modelName = 'nmnist' trainedFolder = 'Trained' os.makedirs(trainedFolder, exist_ok=True) device = torch.device('cuda') # create params object from dictionary / yaml file netParams = snn.params(dict=netDesc) # automatically create the network net = snn.auto.loihi.Network(netParams).to(device) module = net # Create snn loss instance. error = snn.loss(netParams, snn.loihi).to(device) # Define optimizer module. optimizer = snn.utils.optim.Nadam(net.parameters(), lr=0.01, amsgrad=True) # Create training and testing dataset trainingSet = snn.auto.dataset( dataset=nmnistDataset('NMNISTsmall', train=True), network=net,
np.save('Trained/pool{:d}Weights.npy'.format(i + 1), poolWeights[i].squeeze()) plt.figure() plt.hist(poolWeights[i].flatten(), 256) plt.title('pool{:d} weights'.format(i + 1)) for i in range(len(delay)): np.save('Trained/delay{:d}.npy'.format(i + 1), delay[i].squeeze()) plt.figure() plt.hist(delay[i].flatten(), 64) plt.title('delay{:d}'.format(i + 1)) if __name__ == '__main__': netParams = snn.params('network.yaml') print('vDecay:', netParams['neuron']['vDecay']) print('iDecay:', netParams['neuron']['iDecay']) # Define the cuda device to run the code on. device = torch.device('cuda:2') # deviceIds = [1, 2] # Create network instance. net = Network(netParams).to(device) # net = torch.nn.DataParallel(Network(netParams).to(device), device_ids=deviceIds) # Create snn loss instance. error = snn.loss(netParams, snn.loihi).to(device)
import sys, os CURRENT_TEST_DIR = os.path.dirname(os.path.realpath(__file__)) sys.path.append(CURRENT_TEST_DIR + "/../src") import copy import numpy as np import matplotlib.pyplot as plt import torch import slayerSNN as snn from learningStats import learningStats ############################################################################### # oxford spike learning ###################################################### netParams = snn.params("oxford/oxford.yaml") Ts = netParams['simulation']['Ts'] Ns = int(netParams['simulation']['tSample'] / netParams['simulation']['Ts']) Nin = int(netParams['layer'][0]['dim']) Nhid = int(netParams['layer'][1]['dim']) Nout = int(netParams['layer'][2]['dim']) device = torch.device('cuda:3') class Network(torch.nn.Module): def __init__(self, netParams): super(Network, self).__init__() # initialize slayer slayer = snn.layer(netParams['neuron'], netParams['simulation'])
import unittest import numpy as np import matplotlib.pyplot as plt import torch import slayerSNN as snn # Testing filter banks verbose = True if (('-v' in sys.argv) or ('--verbose' in sys.argv)) else False # Select device to run code on device = torch.device('cuda') # Initialization netParams = snn.params('test_files/snnData/network.yaml') N = 5 # number of batch Ts = netParams['simulation']['Ts'] Ns = int(netParams['simulation']['tSample'] / Ts) C = 8 H = 16 W = 16 nFilter = 5 slayer = snn.layer(netParams['neuron'], netParams['simulation']).to(device) pspFilter = slayer.pspFilter(nFilter=nFilter, filterLength=slayer.srmKernel.numel()).to(device) # Generate spikes # input spikes