python tqdm pytorch_Pytorch 实验中非常有效的代码段

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[

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值