-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
96 lines (66 loc) · 3.34 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#author:zhaochao time:2021/5/18
import torch as t
import torch.nn.functional as F
import numpy as np
import random
import torch.nn as nn
def CrossEntropyLoss(label, predict_prob, class_level_weight=None, instance_level_weight=None, epsilon=1e-12):
N, C = label.size()
N_, C_ = predict_prob.size()
assert N == N_ and C == C_, 'fatal error: dimension mismatch!'
if class_level_weight is None:
class_level_weight = 1.0
else:
if len(class_level_weight.size()) == 1:
class_level_weight = class_level_weight.view(1, class_level_weight.size(0))
assert class_level_weight.size(1) == C, 'fatal error: dimension mismatch!'
if instance_level_weight is None:
instance_level_weight = 1.0
else:
if len(instance_level_weight.size()) == 1:
instance_level_weight = instance_level_weight.view(instance_level_weight.size(0), 1)
assert instance_level_weight.size(0) == N, 'fatal error: dimension mismatch!'
ce = -label * t.log(predict_prob + epsilon)
return t.sum(instance_level_weight * ce * class_level_weight) / float(N)
def EntropyLoss(predict_prob, class_level_weight=None, instance_level_weight=None, epsilon=1e-20):
N, C = predict_prob.size()
if class_level_weight is None:
class_level_weight = 1.0
else:
if len(class_level_weight.size()) == 1:
class_level_weight = class_level_weight.view(1, class_level_weight.size(0))
assert class_level_weight.size(1) == C, 'fatal error: dimension mismatch!'
if instance_level_weight is None:
instance_level_weight = 1.0
else:
if len(instance_level_weight.size()) == 1:
instance_level_weight = instance_level_weight.view(instance_level_weight.size(0), 1)
assert instance_level_weight.size(0) == N, 'fatal error: dimension mismatch!'
mask = predict_prob.ge(0.000001) # 逐元素比较
mask_out = t.masked_select(predict_prob, mask)#
entropy =-mask_out * t.log(mask_out)
#
return t.sum(instance_level_weight * entropy * class_level_weight) / float(N)
def normalization(input):
kethe=0.0000000001
output=(input-min(input))/(max(input)-min(input)+kethe)
return output
def BCELossForMultiClassification(label, predict_prob, class_level_weight=None, instance_level_weight=None,
epsilon=1e-12):
N, C = label.size()
N_, C_ = predict_prob.size()
assert N == N_ and C == C_, 'fatal error: dimension mismatch!'
if class_level_weight is None:
class_level_weight = 1.0
else:
if len(class_level_weight.size()) == 1:
class_level_weight = class_level_weight.view(1, class_level_weight.size(0))
assert class_level_weight.size(1) == C, 'fatal error: dimension mismatch!'
if instance_level_weight is None:
instance_level_weight = 1.0
else:
if len(instance_level_weight.size()) == 1:
instance_level_weight = instance_level_weight.view(instance_level_weight.size(0), 1)
assert instance_level_weight.size(0) == N, 'fatal error: dimension mismatch!'
bce = -label * t.log(predict_prob + epsilon) - (1.0 - label) * t.log(1.0 - predict_prob + epsilon)
return t.sum(instance_level_weight * bce * class_level_weight) / float(N)