Exemplo n.º 1
0
    def forward(self, x):
        batch_size = x.shape[0]
        self.spiking_linear3.initial_state(batch_size=batch_size,
                                           device=self.device)
        self.spiking_linear4.initial_state(batch_size=batch_size,
                                           device=self.device)
        self.spiking_linear1.initial_state(batch_size=batch_size,
                                           device=self.device)
        self.spiking_linear2.initial_state(batch_size=batch_size,
                                           device=self.device)
        spike_out = torch.zeros((batch_size, self.num_classes),
                                device=self.device)
        accuracy_record = torch.zeros((self.seq_length, 2), device='cpu')

        for i in range(self.seq_length):
            rescale_fac = 1 / (self.max_firing_rate *
                               self.dt) * (torch.max(x) - torch.min(x))
            spike_snapshot = rescale_fac * torch.rand_like(x,
                                                           device=self.device)
            inp_image = threshold(x, spike_snapshot)

            x_spike = self.spiking_linear1.forward(inp_image)
            r_spike = self.spiking_linear2.forward(x_spike)
            x_spike = self.Weighted * x_spike + r_spike
            x_spike = self.spiking_linear3.forward(x_spike)
            x_spike = self.spiking_linear4.forward(x_spike)
            spike_out += x_spike

            pred = spike_out.data.max(1, keepdim=True)[1]
            cur_correct = pred.eq(self.y.data.view_as(pred)).cpu().sum()
            accuracy_record[i, 0] = cur_correct
            accuracy_record[i, 1] = batch_size

        return spike_out, accuracy_record
    def forward(self, input_current):
        self.v_mem += input_current

        spike = threshold(self.v_mem, self.if_parameters.v_th)
        self.v_mem = (1 -
                      spike) * self.v_mem + spike * self.if_parameters.v_reset
        return spike
    def forward(self, input_current):
        self.v_mem += input_current * self.rC

        spike = threshold(self.v_mem, self.if_parameters.v_th)
        delta_rC = 1.0 / self.rC - self.sigma * spike * input_current + self.beta * (
            1 - self.sigma * spike) * input_current
        # delta_rR = self.tau * (-self.rR + self.sigma * spike - self.beta * (1 - self.sigma * spike))
        self.rC += self.tau * delta_rC
        # self.rR += delta_rR
        return spike
Exemplo n.º 4
0
    def forward(self, x):
        batch_size = x.shape[0]
        self.spiking_conv1.initial_state(batch_size=batch_size,
                                         device=self.device)
        self.spiking_conv2_0.initial_state(batch_size=batch_size,
                                           device=self.device)
        self.spiking_conv2_1.initial_state(batch_size=batch_size,
                                           device=self.device)
        self.spiking_conv3.initial_state(batch_size=batch_size,
                                         device=self.device)
        self.spiking_conv4.initial_state(batch_size=batch_size,
                                         device=self.device)
        self.spiking_linear1.initial_state(batch_size=batch_size,
                                           device=self.device)
        self.spiking_linear2.initial_state(batch_size=batch_size,
                                           device=self.device)
        spike_out = torch.zeros((batch_size, self.num_classes),
                                device=self.device)
        output_record = torch.zeros((self.seq_length, batch_size),
                                    device='cpu')

        for i in range(self.seq_length):
            rescale_fac = 1 / (self.max_firing_rate *
                               self.dt) * (torch.max(x) - torch.min(x))
            spike_snapshot = rescale_fac * torch.rand_like(x,
                                                           device=self.device)
            inp_image = threshold(x, spike_snapshot)

            x_spike = self.spiking_conv1.forward(inp_image)
            r_spike = self.spiking_conv2_0.forward(x_spike)
            r_spike = self.spiking_conv2_1.forward(r_spike)
            x_spike = r_spike + self.Weighted * x_spike
            x_spike = self.spiking_conv3.forward(x_spike)
            x_spike = torch.nn.AvgPool2d(kernel_size=(2, 2),
                                         stride=(2, 2))(x_spike)
            x_spike = self.spiking_conv4.forward(x_spike)
            x_spike = torch.nn.AvgPool2d(kernel_size=(2, 2),
                                         stride=(2, 2))(x_spike)
            x_spike = x_spike.view(-1, self.num_flat_features(x_spike))
            x_spike = self.spiking_linear1.forward(x_spike)
            x_spike = self.spiking_linear2.forward(x_spike)

            spike_out += x_spike

            pred = spike_out.data.max(1, keepdim=True)[1]
            output_record[i, :] = torch.transpose(pred, 1, 0)

        return spike_out, output_record
    def forward(self, input_current):
        self.v_mem += input_current * self.rC

        spike = threshold(self.v_mem, self.if_parameters.v_th)

        input_event = torch.where(
            input_current >
            0.001,  # Use a small positive value instead of zero.
            torch.zeros_like(input_current),
            torch.ones_like(input_current),
        )

        delta_rC = 1.0 / self.rC - self.sigma * spike * input_current + self.beta * (
            1 - self.sigma * spike) * input_current
        # delta_rR = self.tau * (-self.rR + self.sigma * spike - self.beta * (1 - self.sigma * spike))
        self.rC += self.tau * delta_rC * input_event
        # self.rR += delta_rR
        return spike
Exemplo n.º 6
0
    def forward(self, x):
        batch_size = x.shape[0]
        self.spiking_conv1.initial_state(batch_size=batch_size,
                                         device=self.device)
        self.spiking_conv2.initial_state(batch_size=batch_size,
                                         device=self.device)
        self.spiking_linear1.initial_state(batch_size=batch_size,
                                           device=self.device)
        self.spiking_linear2.initial_state(batch_size=batch_size,
                                           device=self.device)
        spike_out = torch.zeros((batch_size, self.num_classes),
                                device=self.device)
        accuracy_record = torch.zeros((self.seq_length, 2), device='cpu')

        for i in range(self.seq_length):
            rescale_fac = 1 / (self.max_firing_rate *
                               self.dt) * (torch.max(x) - torch.min(x))
            spike_snapshot = rescale_fac * torch.rand_like(x,
                                                           device=self.device)
            inp_image = threshold(x, spike_snapshot)

            x_spike = self.spiking_conv1.forward(inp_image)
            x_spike = torch.nn.functional.avg_pool2d(x_spike,
                                                     kernel_size=(2, 2),
                                                     stride=(2, 2))
            x_spike = self.spiking_conv2.forward(x_spike)
            x_spike = torch.nn.functional.avg_pool2d(x_spike,
                                                     kernel_size=(2, 2),
                                                     stride=(2, 2))
            x_spike = x_spike.view(-1, self.num_flat_features(x_spike))
            x_spike = self.spiking_linear1.forward(x_spike)
            x_spike = self.spiking_linear2.forward(x_spike)

            spike_out += x_spike

            pred = spike_out.data.max(1, keepdim=True)[1]
            cur_correct = pred.eq(self.y.data.view_as(pred)).cpu().sum()
            accuracy_record[i, 0] = cur_correct
            accuracy_record[i, 1] = batch_size

        return spike_out, accuracy_record