def forward(self, input, max_layer): input = sf.pad(input, (2,2,2,2)) if self.training: #forward pass for train pot = self.conv1(input) spk, pot = sf.fire(pot, Threshold_1, True) pot = sf.pointwise_inhibition(pot) # inter-channel inhibition if max_layer == 1: winners = sf.get_k_winners(pot, 8, 5) self.save_data(input, pot, spk, winners) return spk, pot spk_in = sf.pad(sf.pooling(spk, 2, 2), (1,1,1,1)) pot = self.conv2(spk_in) spk, pot = sf.fire(pot, Threshold_2, True) pot = sf.pointwise_inhibition(pot) # inter-channel inhibition if max_layer == 2: winners = sf.get_k_winners(pot, 8, 5) self.save_data(spk_in, pot, spk, winners) return spk, pot else: pot = self.conv1(input) spk, pot = sf.fire(pot, Threshold_1, True) pot = self.conv2(sf.pad(sf.pooling(spk, 2, 2), (1,1,1,1))) # spk, pot = sf.fire(pot, 10, True) # pot = self.conv3(sf.pad(sf.pooling(spk, 2, 2), (1,1,1,1))) # spk = sf.fire(pot, 10) # pot = self.conv3(sf.pad(sf.pooling(spk, 3, 3), (2,2,2,2))) # spk = sf.fire_(pot) return spk, pot
def forward(self, input, max_layer): input = sf.pad(input, (2,2,2,2)) if self.training: #forward pass for train if max_layer < 3: pot = self.conv1(input) spk, pot = sf.fire(pot, 15, True) pot = sf.pointwise_inhibition(pot) # inter-channel inhibition if max_layer == 1: winners = sf.get_k_winners(pot, 5, 3) self.save_data(input, pot, spk, winners) return spk, pot spk_in = sf.pad(sf.pooling(spk, 2, 2), (1,1,1,1)) pot = self.conv2(spk_in) spk, pot = sf.fire(pot, 10, True) pot = sf.pointwise_inhibition(pot) # inter-channel inhibition if max_layer == 2: winners = sf.get_k_winners(pot, 8, 5) self.save_data(spk_in, pot, spk, winners) return spk, pot else: pot = self.conv3(input) spk, pot = sf.fire(pot, 15, True) pot = sf.pointwise_inhibition(pot) # inter-channel inhibition if max_layer == 3: winners = sf.get_k_winners(pot, 5, 3) self.save_data(input, pot, spk, winners) return spk, pot else: pot = self.conv1(input) spk, pot = sf.fire(pot, 15, True) pot_1 = self.conv2(sf.pad(sf.pooling(spk, 2, 2), (1,1,1,1))) pot_2 = self.conv3(input) pot_2 = self.pool(pot_2) return spk, torch.cat([pot_1, pot_2], dim=1)
def forward(self, input, max_layer): input = sf.pad(input.float(), (2, 2, 2, 2), 0) if self.training: pot = self.conv1(input) spk, pot = sf.fire(pot, self.conv1_t, True) if max_layer == 1: self.spk_cnt1 += 1 if self.spk_cnt1 >= 500: self.spk_cnt1 = 0 ap = torch.tensor( self.stdp1.learning_rate[0][0].item(), device=self.stdp1.learning_rate[0][0].device) * 2 ap = torch.min(ap, self.max_ap) an = ap * -0.75 self.stdp1.update_all_learning_rate(ap.item(), an.item()) pot = sf.pointwise_inhibition(pot) spk = pot.sign() winners = sf.get_k_winners(pot, self.k1, self.r1, spk) self.save_data(input, pot, spk, winners) return spk, pot spk_in = sf.pad(sf.pooling(spk, 2, 2, 1), (1, 1, 1, 1)) spk_in = sf.pointwise_inhibition(spk_in) pot = self.conv2(spk_in) spk, pot = sf.fire(pot, self.conv2_t, True) if max_layer == 2: pot = sf.pointwise_inhibition(pot) spk = pot.sign() winners = sf.get_k_winners(pot, self.k2, self.r2, spk) self.save_data(spk_in, pot, spk, winners) return spk, pot spk_out = sf.pooling(spk, 2, 2, 1) return spk_out else: print("input:", input.shape) pot = self.conv1(input) spk, pot = sf.fire(pot, self.conv1_t, True) print("spk_conv1:", spk.shape) pot = self.conv2(sf.pad(sf.pooling(spk, 2, 2, 1), (1, 1, 1, 1))) spk, pot = sf.fire(pot, self.conv2_t, True) print("spk_conv2:", spk.shape) spk = sf.pooling(spk, 2, 2, 1) print("spk_pool1:", spk.shape) return spk
def forward(self, input, max_layer): input = sf.pad(input, (2,2,2,2)) if self.training: #forward pass for train pot = self.conv1(input) spk, pot = sf.fire(pot, Threshold_1, True) pot = sf.pointwise_inhibition(pot) # inter-channel inhibition if max_layer == 1: winners = sf.get_k_winners(pot, 5, 3) self.save_data(input, pot, spk, winners) return spk, pot spk_in = sf.pad(sf.pooling(spk, 2, 2), (1,1,1,1)) pot = self.conv2(spk_in) spk, pot = sf.fire(pot, Threshold_2, True) pot = sf.pointwise_inhibition(pot) # inter-channel inhibition if max_layer == 2: winners = sf.get_k_winners(pot, 8, 2) self.save_data(spk_in, pot, spk, winners) return spk, pot spk_in = sf.pad(sf.pooling(spk, 3, 3), (2,2,2,2)) pot = self.conv3(spk_in) spk = sf.fire_(pot) winners = sf.get_k_winners(pot, 1) self.save_data(spk_in, pot, spk, winners) output = -1 if len(winners) != 0: output = self.decision_map[winners[0][0]] return output else: pot = self.conv1(input) spk = sf.fire(pot, 15) pot = self.conv2(sf.pad(sf.pooling(spk, 2, 2), (1,1,1,1))) spk = sf.fire(pot, 10) pot = self.conv3(sf.pad(sf.pooling(spk, 3, 3), (2,2,2,2))) spk = sf.fire_(pot) winners = sf.get_k_winners(pot, 1) output = -1 if len(winners) != 0: output = self.decision_map[winners[0][0]] return output
def __call__(self, image): image = self.to_tensor(self.grayscale(image)) image.unsqueeze_(0) image = self.filter(image) image = sf.pooling(image, self.pooling_size, self.pooling_stride, padding=self.pooling_size // 2) if self.lateral_inhibition is not None: image = self.lateral_inhibition(image) temporal_image = self.temporal_transform(image) temporal_image = sf.pointwise_inhibition(temporal_image) return temporal_image.sign()
def forward(self, input, max_layer): input = sf.pad(input, (2, 2, 2, 2), 0) if self.training: pot = self.conv1(input) spk, pot = sf.fire(pot, self.conv1_t, True) if max_layer == 1: self.spk_cnt1 += 1 if self.spk_cnt1 >= 500: self.spk_cnt1 = 0 ap = torch.tensor( self.stdp1.learning_rate[0][0].item(), device=self.stdp1.learning_rate[0][0].device) * 2 ap = torch.min(ap, self.max_ap) an = ap * -0.75 self.stdp1.update_all_learning_rate(ap.item(), an.item()) pot = sf.pointwise_inhibition(pot) spk = pot.sign() winners = sf.get_k_winners(pot, self.k1, self.r1, spk) self.ctx["input_spikes"] = input self.ctx["potentials"] = pot self.ctx["output_spikes"] = spk self.ctx["winners"] = winners return spk, pot spk_in = sf.pad(sf.pooling(spk, 2, 2), (1, 1, 1, 1)) pot = self.conv2(spk_in) spk, pot = sf.fire(pot, self.conv2_t, True) if max_layer == 2: self.spk_cnt2 += 1 if self.spk_cnt2 >= 500: self.spk_cnt2 = 0 ap = torch.tensor( self.stdp2.learning_rate[0][0].item(), device=self.stdp2.learning_rate[0][0].device) * 2 ap = torch.min(ap, self.max_ap) an = ap * -0.75 self.stdp2.update_all_learning_rate(ap.item(), an.item()) pot = sf.pointwise_inhibition(pot) spk = pot.sign() winners = sf.get_k_winners(pot, self.k2, self.r2, spk) self.ctx["input_spikes"] = spk_in self.ctx["potentials"] = pot self.ctx["output_spikes"] = spk self.ctx["winners"] = winners return spk, pot spk_in = sf.pad(sf.pooling(spk, 3, 3), (2, 2, 2, 2)) pot = self.conv3(spk_in) spk = sf.fire(pot) winners = sf.get_k_winners(pot, 1, 0, spk) self.ctx["input_spikes"] = spk_in self.ctx["potentials"] = pot self.ctx["output_spikes"] = spk self.ctx["winners"] = winners output = -1 if len(winners) != 0: output = self.decision_map[winners[0][0]] return output else: pot = self.conv1(input) spk, pot = sf.fire(pot, self.conv1_t, True) if max_layer == 1: return spk, pot pot = self.conv2(sf.pad(sf.pooling(spk, 2, 2), (1, 1, 1, 1))) spk, pot = sf.fire(pot, self.conv2_t, True) if max_layer == 2: return spk, pot pot = self.conv3(sf.pad(sf.pooling(spk, 3, 3), (2, 2, 2, 2))) spk = sf.fire(pot) winners = sf.get_k_winners(pot, 1, 0, spk) output = -1 if len(winners) != 0: output = self.decision_map[winners[0][0]] return output