1. 大幅度提升 Pytorch 的训练速度
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch.backends.cudnn.benchmark= True
但加了这一行,似乎运行结果可能会存在细微差异,由于随机数种子带来的不同。
2. 把原有的记录文件加个后缀变为 .bak 文件,避免直接覆盖
# from co-teaching train code
txtfile = save_dir + "/" + model_str + "_%s.txt"%str(args.optimizer) ## good job!
nowTime=datetime.datetime.now().strftime('%Y-%m-%d-%H:%M:%S')ifos.path.exists(txtfile):
os.system('mv %s %s' % (txtfile, txtfile+".bak-%s" % nowTime)) # bakeup 备份文件
3. 计算 Accuracy 返回list, 调用函数时,直接提取值,而非提取list
# from co-teaching code but MixMatch_pytorch code also has it
def accuracy(logit, target, topk=(1,)):"""Computes the precision@k for the specified values of k"""output= F.softmax(logit, dim=1) # but actually not need it
maxk=max(topk)
batch_size= target.size(0)
_, pred= output.topk(maxk, 1, True, True) # _, pred = logit.topk(maxk, 1, True, True)
pred=pred.t()
correct= pred.eq(target.view(1, -1).expand_as(pred))
res=[]for k intopk:
correct_k= correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 /batch_size)) # it seems this is a bug, when not all batch has same size, the mean of accuracy of each batch is not the mean of accu of all datasetreturnres
prec1,= accuracy(logit, labels, topk=(1,)) # , indicate tuple unpackage
prec1, prec5= accuracy(logits, labels, topk=(1, 5))
4. 善于利用 logger 文件来记录每一个 epoch 的实验值
# fromPytorch_MixMatch code
class Logger(object):'''Save training process to log file with simple plot function.'''def __init__(self, fpath, title=None, resume=False):
self.file=None
self.resume=resume
self.title= '' if title == None elsetitleif fpath isnot None:ifresume:
self.file= open(fpath, 'r')
name=self.file.readline()
self.names= name.rstrip().split('\t')
self.numbers={}for _, name inenumerate(self.names):
self.numbers[name]=[]for numbers inself.file:
numbers= numbers.rstrip().split('\t')for i in range(0, len(numbers)):
self.numbers[self.names[