def forward(self, dist_a, dist_b, x, y, z, cls, target, ids): if not cls.volatile: cls.register_hook( lambda grad: dprint('cls norm: {}', grad.data.norm())) inds1 = [i for i, t in enumerate(target) if t.item() > 0] inds2 = [i for i, t in enumerate(target) if not t.item() > 0] dprint('#triplets: {} \t #class: {}', len(inds1), len(inds2)) fix = hack(inds1, inds2) # ActorObserverLoss final, weights = [], torch.zeros(x.shape[0], ) if len(inds1) > 0: vars1 = var_subset([dist_a, dist_b, x, y, z, target], inds1) vars1 += [[ids[i] for i in inds1]] f, w = super(ActorObserverLossWithClassifier, self).forward(*vars1) final.append(f) for i, ii in enumerate(inds1): weights[ii] = w[i] # Classification loss if len(inds2) > 0: vars2 = var_subset([cls, -target.long()], inds2) clsloss = self.clsloss(*vars2) f = self.clsweight * clsloss.sum() final.append(f) final[0], weights = fix(final[0], weights) dprint('losses: {}', ' '.join(['{}'.format(r.item()) for r in final])) return sum(final), weights
def base(self, x, y, z): base_x = self.basenet(x) base_y = self.basenet(y) base_z = self.basenet(z) dist_a = F.pairwise_distance(base_x, base_y, 2).view(-1) dist_b = F.pairwise_distance(base_y, base_z, 2).view(-1) dprint('fc7 norms: {} \t {} \t {}', base_x.data.norm(), base_y.data.norm(), base_z.data.norm()) dprint('pairwise dist means: {} \t {}', dist_a.data.mean(), dist_b.data.mean()) return base_x, base_y, base_z, dist_a, dist_b
def forward(self, x, y, z): """ assuming: x: first person positive y: third person z: first person negative """ base_x = self.basenet(x) base_y = self.basenet(y) w_x = self.firstpos_fc(base_x).view(-1) * torch.exp(self.firstpos_scale) w_y = self.third_fc(base_y).view(-1) * torch.exp(self.third_scale) dprint('fc7 norms: {}\t {}', base_x.data.norm(), base_y.data.norm()) self.verbose() return base_x, base_y, w_x, w_y
def forward(self, x): """ assuming: x: first person positive y: third person z: first person negative """ base_x = self.basenet(x) y = self.classifier(base_x) w_x = self.firstpos_fc(base_x).view(-1) * torch.exp( self.firstpos_scale) w_z = self.firstneg_fc(base_x).view(-1) * torch.exp( self.firstneg_scale) dprint('fc7 norms: {}', base_x.data.norm()) self.verbose() return y, w_x, w_z
def forward(self, dist_a, dist_b, x, y, z, cls, target, ids): if not cls.volatile: cls.register_hook(lambda grad: dprint('cls norm: {}', grad.data.norm())) inds1 = [i for i, t in enumerate(target) if t.data[0] > 0] inds2 = [i for i, t in enumerate(target) if not t.data[0] > 0] dprint('#triplets: {} \t #class: {}', len(inds1), len(inds2)) # Fix for avoiding degenerate autograd graph building # that causes out of gpu memory correction = 1.0 if len(inds1) == 0: inds1 = [0] correction = 0.0 # ActorObserverLoss final, weights = [], torch.zeros(x.shape[0],) if len(inds1) > 0: vars1 = var_subset([dist_a, dist_b, x, y, z, target], inds1) vars1 += [[ids[i] for i in inds1]] f, w = super(ActorObserverLossAllWithClassifier, self).forward(*vars1) final.append(f) for i, ii in enumerate(inds1): weights[ii] = w[i] # fix final[0] *= correction weights *= correction correctioncls = 1.0 if len(inds2) == 0: inds2 = [0] correctioncls = 0.0 # Classification loss if len(inds2) > 0: vars2 = var_subset([cls, -target.long()], inds2) clsloss = self.clsloss(*vars2) f = self.clsweight * clsloss.sum() final.append(f) # fix final[1] *= correctioncls dprint('losses: {}', ' '.join(['{}'.format(r.data[0]) for r in final])) return sum(final), weights
def verbose(self): dprint('scales:{}\t{}\t{}', math.exp(self.firstpos_scale.data[0]), math.exp(self.third_scale.data[0]), math.exp(self.firstneg_scale.data[0]))