def shared_step(self, batch, batch_idx): (aud1, aud2), y = batch y = y.squeeze() # ENCODE # encode -> representations # (b, 3, 32, 32) -> (b, 2048, 2, 2) h1 = self.forward(aud1) h2 = self.forward(aud2) # print(h1.shape) # PROJECT # img -> E -> h -> || -> z # (b, 2048, 2, 2) -> (b, 128) z1, y_hat1 = self.projection(h1) z2, y_hat2 = self.projection(h2) idx = torch.nonzero((y != -1)).flatten() y = y[idx] y_hat1 = y_hat1[idx] y_hat2 = y_hat2[idx] label_loss = F.cross_entropy(y_hat1, y) + F.cross_entropy(y_hat2, y) label_loss[torch.isnan(label_loss)] = 0 loss = self.nt_xent_loss(z1, z2, self.hparams.loss_temperature) + label_loss return loss
def ToolCheckIp(): public_ip = get_public_ip(external=False).strip('\n') if check_ip_and_allow(public_ip): return MessageContainer(header=INFO_HEADER, message=F(messages.INFO.IP_ALLOWED, public_ip)) return MessageContainer(header=ERROR_HEADER, message=F(messages.ERROR.IP_NO_PREMIUM, public_ip))
def on_test_batch_end(self, trainer, pl_module, batch, batch_idx, dataloader_idx): x, y = self.to_device(batch, pl_module.device) with torch.no_grad(): representations = self.get_representations(pl_module, x) # print(representations.shape) # forward pass mlp_preds = pl_module.non_linear_evaluator(representations) # print(mlp_preds.shape, y.shape) mlp_loss = F.cross_entropy(mlp_preds, y) # log metrics if trainer.datamodule is not None: acc = accuracy(mlp_preds, y, num_classes=trainer.datamodule.num_classes) else: acc = accuracy(mlp_preds, y, num_classes=self.num_classes) if 'test_acc' in self.output: self.output['test_acc'].append(acc.item()) else: self.output['test_acc'] = [acc.item()]
def on_train_epoch_end(self, trainer, pl_module): train_sampler = torch.utils.data.distributed.DistributedSampler( self.train_dataset, num_replicas=4, rank=int(str(pl_module.device)[-1])) train_loader = torch.utils.data.DataLoader( dataset=self.train_dataset, batch_size=self.batch_size, ############################## shuffle=False, # ############################## num_workers=0, pin_memory=True, ############################# sampler=train_sampler) for batch in train_loader: x, y = self.to_device(batch, pl_module.device) with torch.no_grad(): representations = self.get_representations(pl_module, x) # print(representations.shape) # forward pass mlp_preds = pl_module.non_linear_evaluator(representations) # print(mlp_preds.shape, y.shape) mlp_loss = F.cross_entropy(mlp_preds, y) # update finetune weights mlp_loss.backward() self.optimizer.step() self.optimizer.zero_grad() # log metrics if trainer.datamodule is not None: acc = accuracy(mlp_preds, y, num_classes=trainer.datamodule.num_classes) else: acc = accuracy(mlp_preds, y) metrics = {'mlp_train/loss': mlp_loss, 'mlp_train/acc': acc} pl_module.logger.log_metrics(metrics, step=trainer.global_step)
def GetUpdatesFolder(query=1): try: query = int(query) except: return MessageContainer(header=ERROR_HEADER, message=messages.ERROR.ONLY_NUMBERS_ALLOWED) oc = ObjectContainer(title2=F('LATEST_UPDATES_FOR', query)) updates_data = factory.get_latest_updates(day_count=query) if not updates_data: pass # TODO: api > web > error SetCache(UPDATES_CACHE_KEY, updates_data) for season in updates_data.values(): oc.add( DirectoryObject(key=Callback(GetSeasonFolder, show_name=season['name'], season_id=season['season_id'], check_updates=True), thumb=season['thumb'], title=season['title'], art=season['thumb'])) return oc
def HistoryFolder(): history = Dict[HISTORY_KEY] oc = ObjectContainer( title2=F('HISTORY_ITEMS', len(history) if history else 0, HISTORY_MAX_LEN)) if not history: return MessageContainer(header=INFO_HEADER, message=messages.INFO.HISTORY_EMPTY) oc.add( DirectoryObject(key=Callback(HistoryClear), title=L('HISTORY_CLEAN'), thumb=R('icon_clear.png'))) for season in sorted(history.values(), key=lambda k: k['time_added'], reverse=True): oc.add( DirectoryObject(key=Callback(GetSeasonFolder, show_name=season['name'], season_id=season['season_id']), title=season['title'], thumb=season['thumb'], art=season['thumb'])) return oc
def on_validation_batch_end(self, trainer, pl_module, batch, batch_idx, dataloader_idx): x, y = self.to_device(batch, pl_module.device) with torch.no_grad(): representations = self.get_representations(pl_module, x) # print(representations.shape) # forward pass mlp_preds = pl_module.non_linear_evaluator(representations) # print(mlp_preds.shape, y.shape) mlp_loss = F.cross_entropy(mlp_preds, y) # log metrics if trainer.datamodule is not None: acc = accuracy(mlp_preds, y, num_classes=trainer.datamodule.num_classes) else: acc = accuracy(mlp_preds, y, num_classes=self.num_classes) metrics = {'mlp_val/loss': mlp_loss, 'mlp_val/acc': acc} pl_module.logger.log_metrics(metrics, step=trainer.global_step)
def solve(params_dict, step, t, regions, connexion_ignored, dict_get_default, ext_stim, t_to_steady_state): # ----------- PARAMS ----------- # # Initial firing rate, time constant EPSC and ??? # I0, tauEPSC = params_dict['I0'], params_dict['tauEPSC'] for I0_key, I0_value in I0.items(): if I0_value < 0.0: print 'WARNING: I0 value out of bounds set to 0.0', I0_key, I0_value I0[I0_key] = 0.0 # ----------- Weight and tau values of connections between regions ----------- # WT = params_dict['WT'] # ----------- Matrix of connections ----------- # # pandas.Panel.swapaxes() is for interchanging major and minor axes in a Panel with the proper values # pnWT = pd.Panel.from_dict(WT).swapaxes(1, 0) # ----------- Matrix of weight ----------- # pnW = pnWT['w'] # ----------- Matrix of tau in msec----------- # pnT = pnWT['tau'] # Matrix of tau in step # pnT = pnT / step # ----------- INITIALISATION ----------- # # Arrays r, I and ext_stim have their columns in the order of the vector regions # # Array of instantaneous firing rate; frequency in 1/msec # r = np.zeros((len(t), len(regions))) # Initial instantaneous firing rate; F(I0) # r[0, :] = [F(I0[zone]) for zone in regions] # Array of input currents # I = np.zeros((len(t), len(regions))) # Basal input current # I_basal = [I0[zone] for zone in regions] # Initial instantaneous firing rate; I_basal # I[0, :] = I_basal # Vector of total input for each region at one iteration # itot = np.zeros(len(regions)) # ----------- TIME'S LOOP ----------- # for it in np.arange(1, len(t)): # On average, the input from a given pre-synaptic neuron is proportional to its firing rate rj # I[it, :] = r[it - 1, :] # Inputs from connection # I_connections = np.zeros((len(regions), len(regions))) # Get the tau and weight vectors for each connection and link it to the r activity # for zone_to in pnT.columns: # Connection delays (tau) vector of each departure regions # tau_s = pnT.ix[:, zone_to].dropna() # Connection weights vector of each departure regions # weights = pnW.ix[:, zone_to].dropna() # Get the departure regions vector # zone_departure = tau_s.keys() # Get the tau and weight values for each connection and link it to the r activity # for zone_from in zone_departure: # Connection delays (tau) value from each regions # tau = tau_s[zone_from] # Connection weights vector from each regions # weight = weights[zone_from] # Correspondant time of the delay # ind_delay = it - tau # ceil() round always to the higher integer; transform to the correspondant iteration of the delay # ind_delay = ceil(ind_delay) if ind_delay >= 0 else 0 try: # To ignore connexion and replace their activity by steady-state activity # # If the zone_from is in the dictionary keys AND # If the zone_to is in the dictionary value if ((zone_from in connexion_ignored.keys()) and (zone_to in connexion_ignored.get( zone_from, dict_get_default)) and (it > t_to_steady_state)): # We replace by initial value # i_connect = I[t_to_steady_state, regions.index(zone_from)] * weight else: # Input from each connections # i_connect = I[ind_delay, regions.index(zone_from)] * weight except Exception, e: print e print 'tau', tau print 'weight', weight print 'ind_delay', ind_delay i_connect = 0.0 # Array of input from each connections # I_connections[regions.index(zone_from), regions.index(zone_to)] = i_connect # Vector of total input for each region at one iteration # itot = I_basal + ext_stim[it, :] + sum(I_connections) r[it, :] = r[it - 1, :] + (-r[it - 1, :] + [F(i_total) for i_total in itot]) * step / tauEPSC
def forward(self, x): x = self.model(x) return F.normalize(x, dim=1), self.class_out(x)