赞
踩
1.metrics 指标函数
- def recur(fn, input, *args):
- if isinstance(input, torch.Tensor) or isinstance(input, np.ndarray): # tensor
- output = fn(input, *args)
- elif isinstance(input, list): # list
- output = []
- for i in range(len(input)):
- output.append(recur(fn, input[i], *args))
- elif isinstance(input, tuple): # tuple
- output = []
- for i in range(len(input)):
- output.append(recur(fn, input[i], *args))
- output = tuple(output)
- elif isinstance(input, dict): # dict
- output = {}
- for key in input:
- output[key] = recur(fn, input[key], *args)
- elif isinstance(input, str): # str
- output = input
- elif input is None:
- output = None
- else:
- raise ValueError('Not valid input type')
- return output
- def Accuracy(output, target, topk=1):
- with torch.no_grad():
- if target.dtype != torch.int64:
- target = (target.topk(1, 1, True, True)[1]).view(-1)
- batch_size = target.size(0)
- pred_k = output.topk(topk, 1, True, True)[1]
- correct_k = pred_k.eq(target.view(-1, 1).expand_as(pred_k)).float().sum()
- acc = (correct_k * (100.0 / batch_size)).item()
- return acc
-
-
- def MAccuracy(output, target, mask, topk=1):
- if torch.any(mask):
- output = output[mask]
- target = target[mask]
- acc = Accuracy(output, target, topk)
- else:
- acc = 0
- return acc
-
-
- def LabelRatio(mask):
- with torch.no_grad():
- lr = mask.float().mean().item()
- return lr
-
-
- class Metric(object):
- def __init__(self, metric_name):
- self.metric_name = self.make_metric_name(metric_name)
- self.metric = {'Loss': (lambda input, output: output['loss'].item()),
- 'Accuracy': (lambda input, output: recur(Accuracy, output['target'], input['target'])),
- 'PAccuracy': (lambda input, output: recur(Accuracy, output['target'], input['target'])),
- 'MAccuracy': (lambda input, output: recur(MAccuracy, output['target'], input['target'],
- output['mask'])),
- 'LabelRatio': (lambda input, output: recur(LabelRatio, output['mask']))}
-
- def make_metric_name(self, metric_name):
- return metric_name
-
- def evaluate(self, metric_names, input, output):
- evaluation = {}
- for metric_name in metric_names:
- evaluation[metric_name] = self.metric[metric_name](input, output)
- return evaluation
-
- def compare(self, val):
- if self.pivot_direction == 'down':
- compared = self.pivot > val
- elif self.pivot_direction == 'up':
- compared = self.pivot < val
- else:
- raise ValueError('Not valid pivot direction')
- return compared
-
- def update(self, val):
- self.pivot = val
- return
========================================================================
2.Python collections模块之Counter
主要功能:可以支持方便、快速的计数,将元素数量统计,然后计数并返回一个字典,键为元素,值为元素个数。
- from collections import Counter
-
- list1 = ["a", "a", "a", "b", "c", "c", "f", "g", "g", "g", "f"]
- dic = Counter(list1)
- print(dic)
- #结果:次数是从高到低的
- #Counter({'a': 3, 'g': 3, 'c': 2, 'f': 2, 'b': 1})
-
- print(dict(dic))
- #结果:按字母顺序排序的
- #{'a': 3, 'b': 1, 'c': 2, 'f': 2, 'g': 3}
-
- print(dic.items()) #dic.items()获取字典的key和value
- #结果:按字母顺序排序的
- #dict_items([('a', 3), ('b', 1), ('c', 2), ('f', 2), ('g', 3)])
-
- print(dic.keys())
- #结果:
- #dict_keys(['a', 'b', 'c', 'f', 'g'])
-
- print(dic.values())
- #结果:
- #dict_values([3, 1, 2, 2, 3])
-
- print(sorted(dic.items(), key=lambda s: (-s[1])))
- #结果:按统计次数降序排序
- #[('a', 3), ('g', 3), ('c', 2), ('f', 2), ('b', 1)]
-
- for i, v in dic.items():
- if v == 1:
- print(i)
- #结果:
- #b
- from collections import Counter
-
- str1 = "aabbfkrigbgsejaae"
- print(Counter(str1))
- print(dict(Counter(str1)))
- #结果:
- #Counter({'a': 4, 'b': 3, 'g': 2, 'e': 2, 'f': 1, 'k': 1, 'r': 1, 'i': 1, 's': 1, 'j': 1})
- #{'a': 4, 'b': 3, 'f': 1, 'k': 1, 'r': 1, 'i': 1, 'g': 2, 's': 1, 'e': 2, 'j': 1}
-
- dic1 = {'a': 3, 'b': 4, 'c': 0, 'd': -2}
- print(Counter(dic1))
pop()
用于删除并返回列表中的一个元素(默认为最后一个元素)
- >>> list1 = [1,2,4,"hello","xy","你好"]
- >>> a = list1.pop()#默认弹出最后一个元素
- >>> print(a,list1)
- 你好 [1,2,4,"hello","xy"]
- >>> list2 = [1,2,4,"hello","xy","你好"]
- >>> b = list2.pop(3)#弹出列表中第四个元素
- >>> print(b,list2)
- hello [1,2,4,"xy","你好"]
张量操作:
torch.cat() 功能:将张量按维度dim进行拼接
torch.stack() 功能:在新创建的维度dim上进行拼接
---------------------------------------------------
1.随机抽取一个元素
- from random import choice
- l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- print(choice(l)) # 随机抽取一个
2.随机抽取若干的元素(无重复)
- from random import sample
- l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- print(sample(l, 5)) # 随机抽取5个元素
3.随机抽取若干个元素(有重复)
- import numpy as np
- l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- idxs = np.random.randint(0, len(l), size=5) # 生成长度为5的随机数组,范围为 [0,10),作为索引
- print([l[i] for i in idxs]) # 按照索引,去l中获取到对应的值
----------------------------------------------------
打乱列表的顺序
- import random
-
- x = [i for i in range(10)]
- print(x)
- random.shuffle(x)
- print(x)
----------------------------------------------------
set()
set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。
----------------------------------------------------
--------------------------------------------------
iid数据划分:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。