代码拉取完成,页面将自动刷新
import numpy as np
from pathlib import Path
import json
import random
import torch
import os
def save_json(data, file_path):
'''
save json
:param data:
:param json_path:
:param file_name:
:return:
'''
if not isinstance(file_path, Path):
file_path = Path(file_path)
# if isinstance(data,dict):
# data = json.dumps(data)
with open(str(file_path), 'w') as f:
json.dump(data, f)
def load_json(file_path):
'''
load json
:param json_path:
:param file_name:
:return:
'''
if not isinstance(file_path, Path):
file_path = Path(file_path)
with open(str(file_path), 'r') as f:
data = json.load(f)
return data
class AverageMeter(object):
'''
# computes and stores the average and current value
# Example:
# >>> loss = AverageMeter()
# >>> for step,batch in enumerate(train_data):
# >>> pred = self.model(batch)
# >>> raw_loss = self.metrics(pred,target)
# >>> loss.update(raw_loss.item(),n = 1)
# >>> cur_loss = loss.avg
# '''
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def seed_everything(seed=1029):
'''
:param seed:
:param device:
:return:
'''
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# some cudnn methods can be random even after fixing the seed
# unless you tell it to be deterministic
torch.backends.cudnn.deterministic = True
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。