code: # -*- coding: utf-8 -*-
  # A Survey on Negative Transfer
  # https://github.com/chamwen/NT-Benchmark
  import numpy as np
  import argparse
  import os
  import torch as tr
  import torch.nn as nn
  import torch.optim as optim
  from utils import network, loss, utils
  from utils.network import calc_coeff
  from utils.dataloader import read_syn_src_tar
  from utils.utils import lr_scheduler_full, fix_random_seed, add_label_noise_noimg
  from utils.loss import CELabelSmooth, CDANE, Entropy, RandomLayer
  import torch.utils.data as Data
  
  
  def data_load(Xs, Ys, Xt, Yt, args):
      dset_loaders = {}
      train_bs = args.batch_size
  
      if args.noise_rate > 0:
          Ys = add_label_noise_noimg(Ys, args.seed, args.class_num, args.noise_rate)
  
      sample_idx_tar = tr.from_numpy(np.arange(len(Yt))).long()
      data_src = Data.TensorDataset(Xs, Ys)
      data_tar = Data.TensorDataset(Xt, Yt)
      data_tar_idx = Data.TensorDataset(Xt, Yt, sample_idx_tar)
  
      # for DAN/DANN/CDAN/MCC
      dset_loaders["source"] = Data.DataLoader(data_src, batch_size=train_bs, shuffle=True, drop_last=True)
      dset_loaders["target"] = Data.DataLoader(data_tar_idx, batch_size=train_bs, shuffle=True, drop_last=True)
      dset_loaders["Target"] = Data.DataLoader(data_tar, batch_size=train_bs * 3, shuffle=False, drop_last=False)
  
      return dset_loaders
  
  
  def train_target(args):
      X_src, y_src, X_tar, y_tar = read_syn_src_tar(args)
      dset_loaders = data_load(X_src, y_src, X_tar, y_tar, args)
  
      netF, netC = network.backbone_net(args, args.bottleneck)
      netF.load_state_dict(tr.load(args.mdl_init_dir + 'netF.pt'))
      netC.load_state_dict(tr.load(args.mdl_init_dir + 'netC.pt'))
      base_network = nn.Sequential(netF, netC)
  
      max_len = max(len(dset_loaders["source"]), len(dset_loaders["target"]))
      args.max_iter = args.max_epoch * max_len
  
      ad_net = network.AdversarialNetwork(args.bottleneck, 20).cuda()
      ad_net.load_state_dict(tr.load(args.mdl_init_dir + 'netD_full.pt'))
      random_layer = RandomLayer([args.bottleneck, args.class_num], args.bottleneck)
      random_layer.cuda()
  
      optimizer_f = optim.SGD(netF.parameters(), lr=args.lr * 0.1)
      optimizer_c = optim.SGD(netC.parameters(), lr=args.lr)
      optimizer_d = optim.SGD(ad_net.parameters(), lr=args.lr)
  
      max_len = max(len(dset_loaders["source"]), len(dset_loaders["target"]))
      max_iter = args.max_epoch * max_len
      interval_iter = max_iter // 10
      iter_num = 0
      base_network.train()
  
      class_num = args.class_num
      mem_fea = tr.rand(len(dset_loaders["target"].dataset), args.bottleneck).cuda()
      mem_fea = mem_fea / tr.norm(mem_fea, p=2, dim=1, keepdim=True)
      mem_cls = tr.ones(len(dset_loaders["target"].dataset), class_num).cuda() / class_num
  
      while iter_num < max_iter:
          try:
              inputs_source, labels_source = iter_source.next()
          except:
              iter_source = iter(dset_loaders["source"])
              inputs_source, labels_source = iter_source.next()
  
          try:
              inputs_target, _, idx = iter_target.next()
          except:
              iter_target = iter(dset_loaders["target"])
              inputs_target, _, idx = iter_target.next()
  
          if inputs_source.size(0) == 1:
              continue
  
          iter_num += 1
          lr_scheduler_full(optimizer_f, init_lr=args.lr * 0.1, iter_num=iter_num, max_iter=args.max_iter)
          lr_scheduler_full(optimizer_c, init_lr=args.lr, iter_num=iter_num, max_iter=args.max_iter)
          lr_scheduler_full(optimizer_d, init_lr=args.lr, iter_num=iter_num, max_iter=args.max_iter)
  
          inputs_source, inputs_target, labels_source = inputs_source.cuda(), inputs_target.cuda(), labels_source.cuda()
          features_source, outputs_source = base_network(inputs_source)
          features_target, outputs_target = base_network(inputs_target)
          features = tr.cat((features_source, features_target), dim=0)
  
          # new version img loss
          args.loss_trade_off = 1.0
          outputs = tr.cat((outputs_source, outputs_target), dim=0)
          softmax_out = nn.Softmax(dim=1)(outputs)
          entropy = Entropy(softmax_out)
          transfer_loss = CDANE([features, softmax_out], ad_net, entropy, calc_coeff(iter_num), random_layer=random_layer)
          classifier_loss = CELabelSmooth(num_classes=args.class_num, epsilon=args.smooth)(outputs_source, labels_source)
  
          # ATDOC
          dis = -tr.mm(features_target.detach(), mem_fea.t())
          for di in range(dis.size(0)):
              dis[di, idx[di]] = tr.max(dis)
          _, p1 = tr.sort(dis, dim=1)
  
          w = tr.zeros(features_target.size(0), mem_fea.size(0)).cuda()
          for wi in range(w.size(0)):
              for wj in range(args.K):
                  w[wi][p1[wi, wj]] = 1 / args.K
  
          weight_, pred = tr.max(w.mm(mem_cls), 1)
          loss_ = nn.CrossEntropyLoss(reduction='none')(outputs_target, pred)
          classifier_loss_atdoc = tr.sum(weight_ * loss_) / (tr.sum(weight_).item())
  
          eff = iter_num / args.max_iter
          total_loss = args.loss_trade_off * transfer_loss + classifier_loss + args.tar_par * eff * classifier_loss_atdoc
  
          optimizer_f.zero_grad()
          optimizer_c.zero_grad()
          optimizer_d.zero_grad()
          total_loss.backward()
          optimizer_f.step()
          optimizer_c.step()
          optimizer_d.step()
  
          # label memory
          netF.eval()
          netC.eval()
          with tr.no_grad():
              features_target, outputs_target = netC(netF(inputs_target))
              features_target = features_target / tr.norm(features_target, p=2, dim=1, keepdim=True)
              softmax_out = nn.Softmax(dim=1)(outputs_target)
              outputs_target = softmax_out ** 2 / ((softmax_out ** 2).sum(dim=0))
  
          mem_fea[idx] = (1.0 - args.momentum) * mem_fea[idx] + args.momentum * features_target.clone()
          mem_cls[idx] = (1.0 - args.momentum) * mem_cls[idx] + args.momentum * outputs_target.clone()
  
          if iter_num % interval_iter == 0 or iter_num == max_iter:
              base_network.eval()
  
              acc_t_te = utils.cal_acc_base(dset_loaders["Target"], base_network)
              log_str = 'Task: {}, Iter:{}/{}; Acc = {:.2f}%'.format(args.task_str, iter_num, max_iter, acc_t_te)
              print(log_str)
  
              base_network.train()
  
      return acc_t_te
  
  
  if __name__ == '__main__':
  
      data_name = 'moon'
      if data_name == 'moon': num_class = 2
      base_name_list = ['0', '1', '2', '3_45', '4_15', '6', '7', '8', '9']
      domain_list = ['Raw', 'Tl', 'Sl', 'Rt', 'Sh', 'Sk', 'Ns', 'Ol', 'Sc']
      file_list = [data_name + i for i in base_name_list]
      num_domain = len(domain_list)
  
      args = argparse.Namespace(bottleneck=64, lr=0.01, lr_decay1=0.1, lr_decay2=1.0,
                                epsilon=1e-05, layer='wn', class_num=num_class, smooth=0)
  
      args.K = 5
      args.momentum = 1.0
      args.tar_par = 0.2
  
      args.method = 'CDANE-ATDOC'
      args.dset = data_name
      args.backbone = 'ShallowNet'
      args.batch_size = 32
      args.max_epoch = 50
      args.input_dim = 2
      args.mdl_init_dir = 'outputs/mdl_init/' + args.dset + '/'
      args.noise_rate = 0
      dset_n = args.dset + '_' + str(args.noise_rate)
  
      os.environ["CUDA_VISIBLE_DEVICES"] = '5'
      args.data_env = 'gpu'  # 'local'
      args.seed = 2022
      fix_random_seed(args.seed)
      tr.backends.cudnn.deterministic = True
      print(dset_n, args.method)
  
      args.root_path = './data_synth/'
      args.local_dir = r'/mnt/ssd2/wenz/NT-Benchmark/NT_UDA/'
      args.result_dir = 'results/target/'
  
      acc_all = np.zeros((len(domain_list) - 1))
      for s in range(1, num_domain):  # source
          for t in [0]:  # target
              itr_idx = s - 1
              info_str = '\n%s: %s --> %s' % (itr_idx, domain_list[s], domain_list[t])
              print(info_str)
              args.src, args.tar = file_list[s], file_list[t]
              args.task_str = domain_list[s] + '_' + domain_list[t]
              print(args)
  
              acc_all[itr_idx] = train_target(args)
      print('All acc: ', np.round(acc_all, 2))
      print('Avg acc: ', np.round(np.mean(acc_all), 2))
   # -*- coding: utf-8 -*-
  # A Survey on Negative Transfer
  # https://github.com/chamwen/NT-Benchmark
  import argparse
  import os, sys
  import os.path as osp
  import numpy as np
  import torch as tr
  import torch.nn as nn
  import torch.optim as optim
  from scipy.spatial.distance import cdist
  import torch.utils.data as Data
  from utils import network, loss
  from utils.dataloader import read_syn_single
  from utils.utils import lr_scheduler, fix_random_seed, op_copy, cal_acc_noimg
  
  
  def data_load(X, y, args):
      dset_loaders = {}
      train_bs = args.batch_size
  
      sample_idx = tr.from_numpy(np.arange(len(y))).long()
      data_tar = Data.TensorDataset(X, y, sample_idx)
      data_test = Data.TensorDataset(X, y, sample_idx)
  
      dset_loaders["target"] = Data.DataLoader(data_tar, batch_size=train_bs, shuffle=True)
      dset_loaders["Target"] = Data.DataLoader(data_test, batch_size=train_bs * 3, shuffle=False)
      return dset_loaders
  
  
  def train_target(args):
      X_tar, y_tar = read_syn_single(args, args.tar)
      dset_loaders = data_load(X_tar, y_tar, args)
  
      # base network feature extract
      netF, netC = network.backbone_net(args, args.bottleneck)
  
      modelpath = args.output_dir_src + '/source_F.pt'
      netF.load_state_dict(tr.load(modelpath))
      modelpath = args.output_dir_src + '/source_C.pt'
      netC.load_state_dict(tr.load(modelpath))
      netC.eval()
  
      for k, v in netC.named_parameters():
          v.requires_grad = False
  
      param_group = []
      for k, v in netF.named_parameters():
          if args.lr_decay1 > 0:
              param_group += [{'params': v, 'lr': args.lr * args.lr_decay1}]
          else:
              v.requires_grad = False
  
      optimizer = optim.SGD(param_group)
      optimizer = op_copy(optimizer)
  
      max_iter = args.max_epoch * len(dset_loaders["target"])
      interval_iter = max_iter // args.interval
      iter_num = 0
  
      while iter_num < max_iter:
          try:
              inputs_test, _, tar_idx = iter_test.next()
          except:
              iter_test = iter(dset_loaders["target"])
              inputs_test, _, tar_idx = iter_test.next()
  
          if inputs_test.size(0) == 1:
              continue
  
          inputs_test = inputs_test.cuda()
          if iter_num % interval_iter == 0 and args.cls_par > 0:
              netF.eval()
              mem_label = obtain_label(dset_loaders["Target"], netF, netC, args)
              mem_label = tr.from_numpy(mem_label).cuda()
              netF.train()
  
          iter_num += 1
          lr_scheduler(optimizer, iter_num=iter_num, max_iter=max_iter)
          features_test = netF(inputs_test)
          _, outputs_test = netC(features_test)
  
          # # loss definition
          if args.cls_par > 0:
              pred = mem_label[tar_idx].long()
              classifier_loss = nn.CrossEntropyLoss()(outputs_test, pred)
              classifier_loss *= args.cls_par
          else:
              classifier_loss = tr.tensor(0.0).cuda()
  
          if args.ent:
              softmax_out = nn.Softmax(dim=1)(outputs_test)
              entropy_loss = tr.mean(loss.Entropy(softmax_out))
              if args.gent:
                  msoftmax = softmax_out.mean(dim=0)
                  gentropy_loss = tr.sum(msoftmax * tr.log(msoftmax + args.epsilon))
                  entropy_loss += gentropy_loss
              im_loss = entropy_loss * args.ent_par
              classifier_loss += im_loss
  
          optimizer.zero_grad()
          classifier_loss.backward()
          optimizer.step()
  
          if iter_num % interval_iter == 0 or iter_num == max_iter:
              netF.eval()
              acc_t_te, _ = cal_acc_noimg(dset_loaders["Target"], netF, netC)
              log_str = 'Task: {}, Iter:{}/{}; Acc = {:.2f}%'.format(args.task_str, iter_num, max_iter, acc_t_te)
              print(log_str)
              netF.train()
  
      if iter_num == max_iter:
          print('{}, TL Acc = {:.2f}%'.format(args.task_str, acc_t_te))
          return acc_t_te
  
  
  def obtain_label(loader, netF, netC, args):
      start_test = True
      with tr.no_grad():
          iter_test = iter(loader)
          for _ in range(len(loader)):
              data = iter_test.next()
              inputs = data[0]
              labels = data[1]
              inputs = inputs.cuda()
              feas = netF(inputs)
              _, outputs = netC(feas)
              if start_test:
                  all_fea = feas.float().cpu()
                  all_output = outputs.float().cpu()
                  all_label = labels.float()
                  start_test = False
              else:
                  all_fea = tr.cat((all_fea, feas.float().cpu()), 0)
                  all_output = tr.cat((all_output, outputs.float().cpu()), 0)
                  all_label = tr.cat((all_label, labels.float()), 0)
  
      all_output = nn.Softmax(dim=1)(all_output)
      ent = tr.sum(-all_output * tr.log(all_output + args.epsilon), dim=1)
      unknown_weight = 1 - ent / np.log(args.class_num)
      _, predict = tr.max(all_output, 1)
  
      accuracy = tr.sum(tr.squeeze(predict).float() == all_label).item() / float(all_label.size()[0])
      if args.distance == 'cosine':
          all_fea = tr.cat((all_fea, tr.ones(all_fea.size(0), 1)), 1)
          all_fea = (all_fea.t() / tr.norm(all_fea, p=2, dim=1)).t()
  
      all_fea = all_fea.float().cpu().numpy()
      K = all_output.size(1)
      aff = all_output.float().cpu().numpy()
      initc = aff.transpose().dot(all_fea)
      initc = initc / (1e-8 + aff.sum(axis=0)[:, None])
      cls_count = np.eye(K)[predict].sum(axis=0)
      labelset = np.where(cls_count > args.threshold)
      labelset = labelset[0]
      # print(labelset)
  
      dd = cdist(all_fea, initc[labelset], args.distance)
      pred_label = dd.argmin(axis=1)
      pred_label = labelset[pred_label]
  
      for round in range(1):  # SSL
          aff = np.eye(K)[pred_label]
          initc = aff.transpose().dot(all_fea)
          initc = initc / (1e-8 + aff.sum(axis=0)[:, None])
          dd = cdist(all_fea, initc[labelset], args.distance)
          pred_label = dd.argmin(axis=1)
          pred_label = labelset[pred_label]
  
      acc = np.sum(pred_label == all_label.float().numpy()) / len(all_fea)
      log_str = 'SSL_Acc = {:.2f}% -> {:.2f}%'.format(accuracy * 100, acc * 100)
      print(log_str)
  
      return pred_label.astype('int')
  
  
  if __name__ == "__main__":
  
      data_name = 'moon'
      if data_name == 'moon': num_class = 2
      base_name_list = ['0', '1', '2', '3_45', '4_15', '6', '7', '8', '9']
      domain_list = ['Raw', 'Tl', 'Sl', 'Rt', 'Sh', 'Sk', 'Ns', 'Ol', 'Sc']
      file_list = [data_name + i for i in base_name_list]
      num_domain = len(domain_list)
  
      args = argparse.Namespace(bottleneck=64, lr=0.01, lr_decay1=0.1, lr_decay2=1.0, ent=True,
                                gent=True, cls_par=0.3, ent_par=1.0, epsilon=1e-05, layer='wn',
                                threshold=0, class_num=num_class, distance='cosine')
  
      args.method = 'SHOT'
      args.dset = data_name
      args.backbone = 'ShallowNet'
      args.batch_size = 32
      args.interval = 2
      args.max_epoch = 5
      args.input_dim = 2
      args.mdl_init_dir = 'outputs/mdl_init/' + args.dset + '/'
      args.noise_rate = 0
      dset_n = args.dset + '_' + str(args.noise_rate)
  
      os.environ["CUDA_VISIBLE_DEVICES"] = '3'
      args.data_env = 'gpu'  # 'local'
      args.seed = 2022
      fix_random_seed(args.seed)
      tr.backends.cudnn.deterministic = True
  
      args.dset = data_name
      args.root_path = './data_synth/'
      mdl_path = 'outputs/models/'
      args.output_src = mdl_path + dset_n + '/source/'
      print(dset_n, args.method)
  
      acc_all = np.zeros((len(domain_list) - 1))
      for s in range(1, num_domain):  # source
          for t in [0]:  # target
              itr_idx = s - 1
              info_str = '\n%s: %s --> %s' % (itr_idx, domain_list[s], domain_list[t])
              print(info_str)
              args.src, args.tar = file_list[s], file_list[t]
              args.task_str = domain_list[s] + domain_list[t]
  
              args.name_src = domain_list[s]
              args.output_dir_src = osp.join(args.output_src, args.name_src)
              print(args)
  
              acc_all[itr_idx] = train_target(args)
      print('All acc: ', np.round(acc_all, 2))
      print('Avg acc: ', np.round(np.mean(acc_all), 2))
   # -*- coding: utf-8 -*-
  # A Survey on Negative Transfer
  # https://github.com/chamwen/NT-Benchmark
  import numpy as np
  import argparse
  import os
  import torch as tr
  import torch.nn as nn
  import torch.optim as optim
  from utils import network, loss, utils
  from utils.LogRecord import LogRecord
  from utils.dataloader import read_seed_src_tar
  from utils.utils import lr_scheduler_full, fix_random_seed, data_load_noimg_ssda
  from utils.loss import CELabelSmooth, Entropy, ReverseLayerF
  
  
  def train_target(args):
      X_src, y_src, X_tar, y_tar = read_seed_src_tar(args)
      dset_loaders = data_load_noimg_ssda(X_src, y_src, X_tar, y_tar, args)
  
      netF, netC = network.backbone_net(args, args.bottleneck)
      netF.load_state_dict(tr.load(args.mdl_init_dir + 'netF.pt'))
      netC.load_state_dict(tr.load(args.mdl_init_dir + 'netC.pt'))
      base_network = nn.Sequential(netF, netC)
  
      args.max_iter = args.max_epoch * len(dset_loaders["source"])
  
      ad_net = network.feat_classifier(type=args.layer, class_num=2, bottleneck_dim=args.bottleneck).cuda()
      ad_net.load_state_dict(tr.load(args.mdl_init_dir + 'netD_clf.pt'))
  
      optimizer_f = optim.SGD(netF.parameters(), lr=args.lr * 0.1)
      optimizer_c = optim.SGD(netC.parameters(), lr=args.lr)
      optimizer_d = optim.SGD(ad_net.parameters(), lr=args.lr)
  
      max_iter = args.max_epoch * len(dset_loaders["source"])
      interval_iter = max_iter // 10
      args.max_iter = max_iter
      iter_num = 0
      base_network.train()
  
      while iter_num < max_iter:
          try:
              inputs_source, labels_source = iter_source.next()
          except:
              iter_source = iter(dset_loaders["source"])
              inputs_source, labels_source = iter_source.next()
  
          try:
              inputs_target_tr, labels_target_tr = iter_target_tr.next()
          except:
              iter_target_tr = iter(dset_loaders["target_tr"])
              inputs_target_tr, labels_target_tr = iter_target_tr.next()
  
          try:
              inputs_target, _ = iter_target.next()
          except:
              iter_target = iter(dset_loaders["target_te"])
              inputs_target, _ = iter_target.next()
  
          if inputs_source.size(0) == 1:
              continue
  
          iter_num += 1
          lr_scheduler_full(optimizer_f, init_lr=args.lr * 0.1, iter_num=iter_num, max_iter=args.max_iter)
          lr_scheduler_full(optimizer_c, init_lr=args.lr, iter_num=iter_num, max_iter=args.max_iter)
          lr_scheduler_full(optimizer_d, init_lr=args.lr, iter_num=iter_num, max_iter=args.max_iter)
  
          inputs_source, labels_source = inputs_source.cuda(), labels_source.cuda()
          inputs_target = inputs_target.cuda()
  
          inputs_target_tr, labels_target_tr = inputs_target_tr.cuda(), labels_target_tr.cuda()
          _, outputs_source = netC(netF(inputs_source))
          _, outputs_target_tr = netC(netF(inputs_target_tr))
          outputs_comb = tr.cat((outputs_source, outputs_target_tr), dim=0)
          labels_comb = tr.cat((labels_source, labels_target_tr), dim=0)
  
          feas_source = netF(inputs_source)
          feas_target_tr = netF(inputs_target_tr)
          fea_comb = tr.cat((feas_source, feas_target_tr), dim=0)
          feas_target = netF(inputs_target)
  
          # # loss definition
          p = float(iter_num) / max_iter
          alpha = 2. / (1. + np.exp(-10 * p)) - 1
          reverse_source, reverse_target = ReverseLayerF.apply(fea_comb, alpha), ReverseLayerF.apply(feas_target,
                                                                                                     alpha)
          _, domain_output_s = ad_net(reverse_source)
          _, domain_output_t = ad_net(reverse_target)
          domain_label_s = tr.ones(inputs_source.size()[0] + inputs_target_tr.size()[0]).long().cuda()
          domain_label_t = tr.zeros(inputs_target.size()[0]).long().cuda()
  
          classifier_loss = CELabelSmooth(num_classes=args.class_num, epsilon=args.smooth)(outputs_comb, labels_comb)
          adv_loss = nn.CrossEntropyLoss()(domain_output_s, domain_label_s) + nn.CrossEntropyLoss()(domain_output_t,
                                                                                                    domain_label_t)
          total_loss = classifier_loss + adv_loss
  
          optimizer_f.zero_grad()
          optimizer_c.zero_grad()
          optimizer_d.zero_grad()
          total_loss.backward()
          optimizer_f.step()
          optimizer_c.step()
          optimizer_d.step()
  
          if iter_num % interval_iter == 0 or iter_num == max_iter:
              base_network.eval()
  
              acc_t_te = utils.cal_acc_base(dset_loaders["Target"], base_network)
              log_str = 'Task: {}, Iter:{}/{}; Acc = {:.2f}%'.format(args.task_str, iter_num, max_iter, acc_t_te)
              args.log.record(log_str)
              print(log_str)
  
              base_network.train()
  
      return acc_t_te
  
  
  if __name__ == '__main__':
  
      data_name = 'SEED'
      if data_name == 'SEED': chn, class_num, trial_num = 62, 3, 3394
      focus_domain_idx = [0, 1, 2]
      # focus_domain_idx = np.arange(15)
      domain_list = ['S' + str(i) for i in focus_domain_idx]
      num_domain = len(domain_list)
  
      args = argparse.Namespace(bottleneck=64, lr=0.01, lr_decay1=0.1, lr_decay2=1.0,
                                epsilon=1e-05, layer='wn', smooth=0,
                                N=num_domain, chn=chn, class_num=class_num)
  
      args.dset = data_name
      args.method = 'DANN'
      args.backbone = 'ShallowNet'
      args.batch_size = 32  # 32
      args.max_epoch = 50  # 50
      args.input_dim = 310
      args.norm = 'zscore'
      args.bz_tar_tr = args.batch_size
      args.bz_tar_te = args.batch_size * 2
      args.mdl_init_dir = 'outputs/mdl_init/' + args.dset + '/'
      args.noise_rate = 0
      dset_n = args.dset + '_' + str(args.noise_rate)
      args.tar_lbl_rate = 5  # [5, 10, ..., 50]/100
  
      os.environ["CUDA_VISIBLE_DEVICES"] = '6'
      args.data_env = 'gpu'  # 'local'
      args.seed = 2022
      fix_random_seed(args.seed)
      tr.backends.cudnn.deterministic = True
  
      print(dset_n, args.method)
      print(args)
  
      args.local_dir = r'/mnt/ssd2/wenz/NT-Benchmark/NT_SSDA/'
      args.result_dir = 'results/target/'
      my_log = LogRecord(args)
      my_log.log_init()
      my_log.record('=' * 50 + '\n' + os.path.basename(__file__) + '\n' + '=' * 50)
  
      acc_all = np.zeros(num_domain * (num_domain - 1))
      for s in range(num_domain):
          for t in range(num_domain):
              if s != t:
                  itr_idx = (num_domain - 1) * s + t
                  if t > s: itr_idx -= 1
                  info_str = '\n%s: %s --> %s' % (itr_idx, domain_list[s], domain_list[t])
                  print(info_str)
                  args.src, args.tar = focus_domain_idx[s], focus_domain_idx[t]
                  args.task_str = domain_list[s] + '_' + domain_list[t]
                  print(args)
  
                  my_log.record(info_str)
                  args.log = my_log
                  acc_all[itr_idx] = train_target(args)
      print('\nSub acc: ', np.round(acc_all, 3))
      print('Avg acc: ', np.round(np.mean(acc_all), 3))
  
      acc_sub_str = str(np.round(acc_all, 3).tolist())
      acc_mean_str = str(np.round(np.mean(acc_all), 3).tolist())
      args.log.record("\n==========================================")
      args.log.record(acc_sub_str)
      args.log.record(acc_mean_str)
  
  
apis: torch.sum torch.utils.data.DataLoader torch.load numpy.mean torch.nn.Softmax torch.no_grad torch.nn.CrossEntropyLoss torch.norm torch.nn.Sequential torch.max numpy.round torch.utils.data.TensorDataset torch.cat torch.sort
apis: torch.utils.data.DataLoader scipy.spatial.distance.cdist torch.no_grad torch.log numpy.log torch.max torch.nn.Softmax torch.norm torch.from_numpy numpy.where numpy.mean numpy.eye torch.optim.SGD torch.load torch.tensor torch.utils.data.TensorDataset torch.nn.CrossEntropyLoss numpy.round torch.squeeze
apis: torch.load numpy.zeros torch.nn.CrossEntropyLoss numpy.exp torch.nn.Sequential numpy.round torch.cat numpy.mean