def BindsNET_cpu(n_neurons, time): t0 = t() torch.set_default_tensor_type("torch.FloatTensor") t1 = t() network = Network() network.add_layer(Input(n=n_neurons), name="X") network.add_layer(LIFNodes(n=n_neurons), name="Y") network.add_connection( Connection(source=network.layers["X"], target=network.layers["Y"]), source="X", target="Y", ) data = {"X": poisson(datum=torch.rand(n_neurons), time=time)} network.run(inputs=data, time=time) return t() - t0, t() - t1
norm=0.5 * layers["E"].n, ) # Spike recordings for all layers. spikes = {} for layer in layers: spikes[layer] = Monitor(layers[layer], ["s"], time=plot_interval) # Voltage recordings for excitatory and readout layers. voltages = {} for layer in set(layers.keys()) - {"X"}: voltages[layer] = Monitor(layers[layer], ["v"], time=plot_interval) # Add all layers and connections to the network. for layer in layers: network.add_layer(layers[layer], name=layer) network.add_connection(input_exc_conn, source="X", target="E") network.add_connection(exc_readout_conn, source="E", target="R") # Add all monitors to the network. for layer in layers: network.add_monitor(spikes[layer], name="%s_spikes" % layer) if layer in voltages: network.add_monitor(voltages[layer], name="%s_voltages" % layer) # Load the Breakout environment. environment = GymEnvironment("BreakoutDeterministic-v4") environment.reset()
traces=True, ) conv_conn = Conv2dConnection( input_layer, conv_layer, kernel_size=kernel_size, stride=stride, update_rule=PostPre, norm=0.4 * kernel_size ** 2, nu=[1e-4, 1e-2], wmax=1.0, ) network.add_layer(input_layer, name="X") network.add_layer(conv_layer, name="Y") network.add_connection(conv_conn, source="X", target="Y") # Train the network. print("Begin training.\n") if args.tensorboard: analyzer = TensorboardAnalyzer("logs/conv") else: analyzer = MatplotlibAnalyzer() for step, batch in enumerate(tqdm(train_dataloader)): # batch contains image, label, encoded_image since an image_encoder # was provided
from bindsnet_qa.pipeline.action import select_softmax # Build network. network = Network(dt=1.0) # Layers of neurons. inpt = Input(n=80 * 80, shape=[80, 80], traces=True) middle = LIFNodes(n=100, traces=True) out = LIFNodes(n=4, refrac=0, traces=True) # Connections between layers. inpt_middle = Connection(source=inpt, target=middle, wmin=0, wmax=1e-1) middle_out = Connection(source=middle, target=out, wmin=0, wmax=1) # Add all layers and connections to the network. network.add_layer(inpt, name="Input Layer") network.add_layer(middle, name="Hidden Layer") network.add_layer(out, name="Output Layer") network.add_connection(inpt_middle, source="Input Layer", target="Hidden Layer") network.add_connection(middle_out, source="Hidden Layer", target="Output Layer") # Load the Breakout environment. environment = GymEnvironment("BreakoutDeterministic-v4") environment.reset() # Build pipeline from specified components. pipeline = EnvironmentPipeline(
def ann_to_snn( ann: Union[nn.Module, str], input_shape: Sequence[int], data: Optional[torch.Tensor] = None, percentile: float = 99.9, node_type: Optional[nodes.Nodes] = SubtractiveResetIFNodes, **kwargs, ) -> Network: # language=rst """ Converts an artificial neural network (ANN) written as a ``torch.nn.Module`` into a near-equivalent spiking neural network. :param ann: Artificial neural network implemented in PyTorch. Accepts either ``torch.nn.Module`` or path to network saved using ``torch.save()``. :param input_shape: Shape of input data. :param data: Data to use to perform data-based weight normalization of shape ``[n_examples, ...]``. :param percentile: Percentile (in ``[0, 100]``) of activations to scale by in data-based normalization scheme. :param node_type: Class of ``Nodes`` to use in replacing ``torch.nn.Linear`` layers in original ANN. :return: Spiking neural network implemented in PyTorch. """ if isinstance(ann, str): ann = torch.load(ann) else: ann = deepcopy(ann) assert isinstance(ann, nn.Module) if data is None: import warnings warnings.warn("Data is None. Weights will not be scaled.", RuntimeWarning) else: ann = data_based_normalization( ann=ann, data=data.detach(), percentile=percentile ) snn = Network() input_layer = nodes.Input(shape=input_shape) snn.add_layer(input_layer, name="Input") children = [] for c in ann.children(): if isinstance(c, nn.Sequential): for c2 in list(c.children()): children.append(c2) else: children.append(c) i = 0 prev = input_layer while i < len(children) - 1: current, nxt = children[i : i + 2] layer, connection = _ann_to_snn_helper(prev, current, node_type, **kwargs) i += 1 if layer is None or connection is None: continue snn.add_layer(layer, name=str(i)) snn.add_connection(connection, source=str(i - 1), target=str(i)) prev = layer current = children[-1] layer, connection = _ann_to_snn_helper( prev, current, node_type, last=True, **kwargs ) i += 1 if layer is not None or connection is not None: snn.add_layer(layer, name=str(i)) snn.add_connection(connection, source=str(i - 1), target=str(i)) return snn
device_id = args.device_id np.random.seed(seed) torch.cuda.manual_seed_all(seed) torch.manual_seed(seed) # Sets up Gpu use if gpu and torch.cuda.is_available(): torch.cuda.set_device(device_id) # torch.set_default_tensor_type('torch.cuda.FloatTensor') else: torch.manual_seed(seed) network = Network(dt=dt) inpt = Input(784, shape=(1, 28, 28)) network.add_layer(inpt, name="I") output = LIFNodes(n_neurons, thresh=-52 + np.random.randn(n_neurons).astype(float)) network.add_layer(output, name="O") C1 = Connection(source=inpt, target=output, w=0.5 * torch.randn(inpt.n, output.n)) C2 = Connection(source=output, target=output, w=0.5 * torch.randn(output.n, output.n)) network.add_connection(C1, source="I", target="O") network.add_connection(C2, source="O", target="O") spikes = {} for l in network.layers: