def forward(self, source, target): source = self.sharedNet(source) loss = 0 if self.training == True: target = self.sharedNet(target) loss = mmd.mmd_linear(source, target) source = self.cls_fc(source) return source, loss
def forward(self, source, target): source = self.features(source) source = source.view(source.size(0), -1) source = self.classifier(source) source = self.bottleneck(source) mmd_loss = 0 if self.training: target = self.features(target) target = target.view(target.size(0), -1) target = self.classifier(target) target = self.bottleneck(target) mmd_loss += mmd.mmd_linear(source, target) result = self.final_classifier(source) return result, mmd_loss
def compute_adapt_loss(self, X, Y, adapt_loss): """Compute adaptation loss, currently we support mmd and coral Arguments: X {tensor} -- source matrix Y {tensor} -- target matrix adapt_loss {string} -- loss type, 'mmd' or 'coral'. You can add your own loss Returns: [tensor] -- adaptation loss tensor """ if adapt_loss == 'mmd': loss = mmd.mmd_linear(X, Y) elif adapt_loss == 'coral': loss = CORAL(X, Y) else: loss = 0 return loss
def train(model, config, epoch): model.class_classifier.train() model.feature.train() iter_source = iter(config['source_train_loader']) iter_target = iter(config['target_train_loader']) len_source_loader = len(config['source_train_loader']) len_target_loader = len(config['target_train_loader']) num_iter = len_source_loader for i in range(1, num_iter): data_source, label_source = iter_source.next() data_target, _ = iter_target.next() if i % len_target_loader == 0: iter_target = iter(config['target_train_loader']) if torch.cuda.is_available(): data_source, label_source = data_source.cuda( ), label_source.cuda() data_target = data_target.cuda() optimizer.zero_grad() preds = model.class_classify(data_source) loss_cls = criterion(preds, label_source) source = model.feature(data_source) source = source.view(source.size(0), -1) target = model.feature(data_target) target = target.view(target.size(0), -1) loss_mmd = mmd.mmd_linear(source, target) loss = loss_cls + config['mmd_gamma'] * loss_mmd if i % 50 == 0: print( 'loss_cls {}, loss_mmd {}, gamma {}, total loss {}'.format( loss_cls.item(), loss_mmd.item(), config['mmd_gamma'], loss.item())) loss.backward() optimizer.step()