-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathloss.go
205 lines (189 loc) · 6.09 KB
/
loss.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package gan_go
import (
"fmt"
"github.com/pkg/errors"
"gorgonia.org/gorgonia"
)
type LossReduction uint16
const (
LossReductionSum = LossReduction(iota)
LossReductionMean
)
// MSELoss See ref. https://en.wikipedia.org/wiki/Mean_squared_error
// Default reduction is 'mean'
func MSELoss(a, b *gorgonia.Node, reduction ...LossReduction) (*gorgonia.Node, error) {
sub, err := gorgonia.Sub(a, b)
if err != nil {
return nil, errors.Wrap(err, "Can't do (A-B)")
}
sqr, err := gorgonia.Square(sub)
if err != nil {
return nil, errors.Wrap(err, "Can't do (x^2)")
}
reductionDefault := LossReductionMean
if len(reduction) != 0 {
reductionDefault = reduction[0]
}
switch reductionDefault {
case LossReductionSum:
return gorgonia.Sum(sqr)
case LossReductionMean:
return gorgonia.Mean(sqr)
default:
return nil, fmt.Errorf("Reduction type %d is not supported", reductionDefault)
}
}
// CrossEntropyLoss See ref. https://en.wikipedia.org/wiki/Cross_entropy#Cross-entropy_loss_function_and_logistic_regression
// Default reduction is 'mean'
func CrossEntropyLoss(a, b *gorgonia.Node, reduction ...LossReduction) (*gorgonia.Node, error) {
log, err := gorgonia.Log(a)
if err != nil {
return nil, errors.Wrap(err, "Can't do log(A)")
}
neg, err := gorgonia.Neg(log)
if err != nil {
return nil, errors.Wrap(err, "Can't do -1*x")
}
hprod, err := gorgonia.HadamardProd(neg, b)
if err != nil {
return nil, errors.Wrap(err, "Can't do (x.*B)")
}
reductionDefault := LossReductionMean
if len(reduction) != 0 {
reductionDefault = reduction[0]
}
switch reductionDefault {
case LossReductionSum:
return gorgonia.Sum(hprod)
case LossReductionMean:
return gorgonia.Mean(hprod)
default:
return nil, fmt.Errorf("Reduction type %d is not supported", reductionDefault)
}
}
// BinaryCrossEntropyLoss See ref. https://en.wikipedia.org/wiki/Cross_entropy#Cross-entropy_loss_function_and_logistic_regression
// Pretty the same as CrossEntropyLoss. BUT for C=2, where C - number of classes
// In case of binary variation of cross entropy loss: sample could belong to 0 or 1 only.
// Default reduction is 'mean'
func BinaryCrossEntropyLoss(a, b *gorgonia.Node, reduction ...LossReduction) (*gorgonia.Node, error) {
// Main part the same as cross entropy
logMain, err := gorgonia.Log(a)
if err != nil {
return nil, errors.Wrap(err, "Can't do log(A)")
}
negMain, err := gorgonia.Neg(logMain)
if err != nil {
return nil, errors.Wrap(err, "Can't do -1*x")
}
hprodMain, err := gorgonia.HadamardProd(negMain, b)
if err != nil {
return nil, errors.Wrap(err, "Can't do (x.*B)")
}
// Here comes another part
onesTensor := gorgonia.NewTensor(a.Graph(), a.Dtype(), a.Dims(), gorgonia.WithShape(a.Shape()...), gorgonia.WithInit(gorgonia.Ones()))
logBin, err := gorgonia.Sub(onesTensor, a)
if err != nil {
return nil, errors.Wrap(err, "Can't do log(1-A)")
}
negBin, err := gorgonia.Neg(logBin)
if err != nil {
return nil, errors.Wrap(err, "Can't do -1*x")
}
preLogBin, err := gorgonia.Sub(onesTensor, b)
if err != nil {
return nil, errors.Wrap(err, "Can't do (1-B)")
}
hprodBin, err := gorgonia.HadamardProd(negBin, preLogBin)
if err != nil {
return nil, errors.Wrap(err, "Can't do (x.*B)")
}
hprod, err := gorgonia.Add(hprodMain, hprodBin)
if err != nil {
return nil, errors.Wrap(err, "Can't do (x+y)")
}
reductionDefault := LossReductionMean
if len(reduction) != 0 {
reductionDefault = reduction[0]
}
switch reductionDefault {
case LossReductionSum:
return gorgonia.Sum(hprod)
case LossReductionMean:
return gorgonia.Mean(hprod)
default:
return nil, fmt.Errorf("Reduction type %d is not supported", reductionDefault)
}
}
// L1Loss See ref. https://en.wikipedia.org/wiki/Least_absolute_deviations
// Default reduction is 'mean'
func L1Loss(a, b *gorgonia.Node, reduction ...LossReduction) (*gorgonia.Node, error) {
sub, err := gorgonia.Sub(a, b)
if err != nil {
return nil, errors.Wrap(err, "Can't do (A-B)")
}
abs, err := gorgonia.Abs(sub)
if err != nil {
return nil, errors.Wrap(err, "Can't do |x|")
}
reductionDefault := LossReductionMean
if len(reduction) != 0 {
reductionDefault = reduction[0]
}
switch reductionDefault {
case LossReductionSum:
return gorgonia.Sum(abs)
case LossReductionMean:
return gorgonia.Mean(abs)
default:
return nil, fmt.Errorf("Reduction type %d is not supported", reductionDefault)
}
}
// HuberLoss See ref. https://en.wikipedia.org/wiki/Huber_loss
// This is actually Pseudo Huber Loss - see ref. https://en.wikipedia.org/wiki/Huber_loss#Pseudo-Huber_loss_function
// Delta value type should match Dtype of provided nodes. tensor.Float32 -> float32, tensor.Float64 -> float64 and etc.
// Default reduction is 'mean'
func HuberLoss(a, b *gorgonia.Node, delta interface{}, reduction ...LossReduction) (*gorgonia.Node, error) {
deltaScalar := gorgonia.NewScalar(a.Graph(), a.Dtype(), gorgonia.WithValue(delta))
sqrDelta := gorgonia.NewScalar(a.Graph(), a.Dtype(), gorgonia.WithValue(delta))
oneScalar := gorgonia.NewScalar(a.Graph(), a.Dtype(), gorgonia.WithValue(1.0))
sub, err := gorgonia.Sub(a, b)
if err != nil {
return nil, errors.Wrap(err, "Can't do (A-B)")
}
div, err := gorgonia.Div(sub, deltaScalar)
if err != nil {
return nil, errors.Wrap(err, "Can't do (X/delta)")
}
sqr, err := gorgonia.Square(div)
if err != nil {
return nil, errors.Wrap(err, "Can't do (x^2)")
}
addOneScalar, err := gorgonia.Add(oneScalar, sqr)
if err != nil {
return nil, errors.Wrap(err, "Can't do (1.+X)")
}
sqrt, err := gorgonia.Sqrt(addOneScalar)
if err != nil {
return nil, errors.Wrap(err, "Can't do √x")
}
subOneScalar, err := gorgonia.Sub(sqrt, oneScalar)
if err != nil {
return nil, errors.Wrap(err, "Can't do (X.-1)")
}
matMul, err := gorgonia.Mul(sqrDelta, subOneScalar)
if err != nil {
return nil, errors.Wrap(err, "Can't do (y@x)")
}
reductionDefault := LossReductionMean
if len(reduction) != 0 {
reductionDefault = reduction[0]
}
switch reductionDefault {
case LossReductionSum:
return gorgonia.Sum(matMul)
case LossReductionMean:
return gorgonia.Mean(matMul)
default:
return nil, fmt.Errorf("Reduction type %d is not supported", reductionDefault)
}
}