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): input = input.float() pot = self.s2(input) if self.training and self.dropout[0] > 0: sf.feature_inhibition_(pot, self.to_be_dropped) spk, pot = sf.fire(pot, self.threshold, True) winners = sf.get_k_winners(pot, 1, 0, spk) output = -1 if len(winners) != 0: output = self.decision_map[winners[0][0]] if self.training: self.ctx["input_spikes"] = input self.ctx["potentials"] = pot self.ctx["output_spikes"] = spk self.ctx["winners"] = winners else: self.ctx["input_spikes"] = None self.ctx["potentials"] = None self.ctx["output_spikes"] = None self.ctx["winners"] = None return output
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 forward(self, rec_field): ############################################################################### #output = Column(rec_field) #input: # rec_field: first-layer output [timesteps, 1, k, rf_size] #output: # center word representation [timesteps, k, 1, 1] ############################################################################### #output size: (timesteps, k, 1, 1) out = self.ec(rec_field) spike, pot = sf.fire(out, self.thresh, True) winners = sf.get_k_winners(pot, kwta=self.kwta, inhibition_radius=self.inhibition_radius) coef = torch.zeros_like(out) coef[:, winners, :, :] = 1 return torch.mul(pot, coef).sign()
def forward(self, rec_field, reward): ############################################################################### #output = Column1(rec_field, reward) #input: # rec_field: fixed input encoding [timesteps, 1, rf_size, length] # reward: context word reward [timesteps, k, rf_size] #output: # context word 1st layer output [timesteps, 1, k, rf_size] ############################################################################### outs = [ self.ec_list[i](torch.unsqueeze(rec_field[:, :, i, :], 2)) for i in range(self.rf_size) ] pots = [sf.fire(o, self.thresh, True)[1] for o in outs] #kwta winners = [ sf.get_k_winners(pot, kwta=self.kwta, inhibition_radius=self.inhibition_radius) for pot in pots ] coefs = [torch.zeros_like(out) for out in outs] for i in range(self.rf_size): coefs[i][:, winners[i], :, :] = 1 pots = [ torch.mul(pots[i], coefs[i]).sign() for i in range(self.rf_size) ] pots = torch.cat(pots, 2).squeeze().unsqueeze(1) #feedback for i in range(self.rf_size): self.rstdp_list[i](torch.unsqueeze(rec_field[:, :, i, :], 2), pots[:, :, :, i].squeeze(), reward[:, :, i].squeeze()) return pots
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
test_loader = DataLoader(dataset, sampler=SubsetRandomSampler(test_indices)) import SpykeTorch.snn as snn pool = snn.Pooling(kernel_size=3, stride=2) conv = snn.Convolution(in_channels=4, out_channels=20, kernel_size=30) stdp = snn.STDP(conv_layer=conv, learning_rate=(0.05, -0.015)) for iter in range(300): print('\rIteration:', iter, end="") for data, _ in train_loader: for x in data: x = pool(x) p = conv(x) o, p = sf.fire(p, 20, return_thresholded_potentials=True) winners = sf.get_k_winners(p, kwta=1, inhibition_radius=0, spikes=o) stdp(x, p, o, winners) print() print("Unsupervised Training is Done.") ####################################################### train_x_spike = [] train_x_pot = [] train_y = [] for data, targets in train_loader: for x, t in zip(data, targets): x = pool(x) p = conv(x) o = sf.fire(p, 20) train_x_spike.append(o.reshape(-1).cpu().numpy())