def build_convolution(self, layer, weights=None):

        delay = self.config.getfloat('cell', 'delay')
        transpose_kernel = \
            self.config.get('simulation', 'keras_backend') == 'tensorflow'
        conns, biases = build_convolution(layer, delay, transpose_kernel)
        connections = np.array(conns)

        self.set_biases(biases)

        print("Connecting layer...")

        self.connections[-1].connect(i=connections[:, 0].astype('int64'),
                                     j=connections[:, 1].astype('int64'))

        w = connections[:, 2] if weights is None else weights.flatten()
        self.connections[-1].w = w
Example #2
0
    def build_convolution(self, layer, input_weight=None):
        from snntoolbox.simulation.utils import build_convolution

        delay = self.config.getfloat('cell', 'delay')
        transpose_kernel = \
            self.config.get('simulation', 'keras_backend') == 'tensorflow'
        self._conns, self._biases = build_convolution(layer, delay,
                                                      transpose_kernel)
        self.set_biases()

        print("Connecting layer...")
        for conn in self._conns:
            i = conn[0]
            j = conn[1]
            self.connections[-1].connect(i=i, j=j)
        if input_weight is not None:
            self.connections[-1].w = input_weight.flatten()
        else:
            self.connections[-1].w[i, j] = conn[2]
    def build_convolution(self, layer):
        from snntoolbox.simulation.utils import build_convolution

        delay = self.config.getfloat('cell', 'delay')
        transpose_kernel = \
            self.config.get('simulation', 'keras_backend') == 'tensorflow'
        self._conns, self._biases = build_convolution(layer, delay,
                                                      transpose_kernel)
        print("layer name {}".format(layer.name))

        weights = self._conns
        for i, w in enumerate(weights):
            if w[2] >= 0:
                self._con_exconns.append(w)
            else:
                self._con_inconns.append(w)

        self.set_biases()
        #self.connections.append(self.sim.Projection(
        #   self.layers[-2], self.layers[-1],
        #   self.sim.FromListConnector(self._conns, ['weight', 'delay'])))

        self.proj_excon = self.sim.Projection(self.layers[-2],
                                              self.layers[-1],
                                              self.sim.FromListConnector(
                                                  self._con_exconns,
                                                  ['weight', 'delay']),
                                              receptor_type='excitatory')

        self.connections.append(self.proj_excon)

        self.proj_incon = self.sim.Projection(self.layers[-2],
                                              self.layers[-1],
                                              self.sim.FromListConnector(
                                                  self._con_inconns,
                                                  ['weight', 'delay']),
                                              receptor_type='inhibitory')

        self.connections.append(self.proj_incon)
    def build_convolution(self, layer):

        # If the parsed model contains a ZeroPadding layer, we need to tell the
        # Conv layer about it here, because ZeroPadding layers are removed when
        # building the pyNN model.
        if self.change_padding:
            if layer.padding == 'valid':
                self.change_padding = False
                layer.padding = 'ZeroPadding'
            else:
                raise NotImplementedError(
                    "Border_mode {} in combination with ZeroPadding is not "
                    "supported.".format(layer.padding))

        delay = self.config.getfloat('cell', 'delay')
        transpose_kernel = \
            self.config.get('simulation', 'keras_backend') == 'tensorflow'

        if get_type(layer) in ['Conv2D', 'SparseConv2D']:
            weights, biases = build_convolution(layer, delay, transpose_kernel)
        elif get_type(layer) in ['DepthwiseConv2D', 'SparseDepthwiseConv2D']:
            weights, biases = build_depthwise_convolution(
                layer, delay, transpose_kernel)
        elif get_type(layer) == 'Conv1D':
            weights, biases = build_1d_convolution(layer, delay)
        else:
            ValueError("Layer {} of type {} unrecognised here. "
                       "How did you get into this function?".format(
                           layer.name, get_type(layer)))

        self.set_biases(biases)
        weights = self.scale_weights(weights)

        exc_connections = [c for c in weights if c[2] > 0]
        inh_connections = [c for c in weights if c[2] < 0]

        if self.config.getboolean('tools', 'simulate'):
            self.connections.append(
                self.sim.Projection(
                    self.layers[-2],
                    self.layers[-1], (self.sim.FromListConnector(
                        exc_connections, ['weight', 'delay'])),
                    receptor_type='excitatory',
                    label=self.layers[-1].label + '_excitatory'))

            self.connections.append(
                self.sim.Projection(
                    self.layers[-2],
                    self.layers[-1], (self.sim.FromListConnector(
                        inh_connections, ['weight', 'delay'])),
                    receptor_type='inhibitory',
                    label=self.layers[-1].label + '_inhibitory'))
        else:
            # The spinnaker implementation of Projection.save() is not working
            # yet, so we do save the connections manually here.
            filepath = os.path.join(self.config.get('paths', 'path_wd'),
                                    self.layers[-1].label)
            # noinspection PyTypeChecker
            np.savetxt(filepath + '_excitatory',
                       np.array(exc_connections),
                       ['%d', '%d', '%.18f', '%.3f'],
                       header="columns = ['i', 'j', 'weight', 'delay']")
            # noinspection PyTypeChecker
            np.savetxt(filepath + '_inhibitory',
                       np.array(inh_connections),
                       ['%d', '%d', '%.18f', '%.3f'],
                       header="columns = ['i', 'j', 'weight', 'delay']")