12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- /// Returns the L1 loss between predictions and expectations.
15
+ /// Computes the L1 loss between `expected` and `predicted`.
16
+ /// `loss = reduction(abs(expected - predicted))`
16
17
///
17
18
/// - Parameters:
18
19
/// - predicted: Predicted outputs from a neural network.
@@ -27,7 +28,8 @@ public func l1Loss<Scalar: TensorFlowFloatingPoint>(
27
28
reduction ( abs ( expected - predicted) )
28
29
}
29
30
30
- /// Returns the L2 loss between predictions and expectations.
31
+ /// Computes the L2 loss between `expected` and `predicted`.
32
+ /// `loss = reduction(square(expected - predicted))`
31
33
///
32
34
/// - Parameters:
33
35
/// - predicted: Predicted outputs from a neural network.
@@ -42,7 +44,8 @@ public func l2Loss<Scalar: TensorFlowFloatingPoint>(
42
44
reduction ( ( expected - predicted) . squared ( ) )
43
45
}
44
46
45
- /// Returns the mean absolute error between predictions and expectations.
47
+ /// Computes the mean of absolute difference between labels and predictions.
48
+ /// `loss = mean(abs(expected - predicted))`
46
49
///
47
50
/// - Parameters:
48
51
/// - predicted: Predicted outputs from a neural network.
@@ -55,7 +58,8 @@ public func meanAbsoluteError<Scalar: TensorFlowFloatingPoint>(
55
58
l1Loss ( predicted: predicted, expected: expected, reduction: _mean)
56
59
}
57
60
58
- /// Returns the mean squared error between predictions and expectations.
61
+ /// Computes the mean of squares of errors between labels and predictions.
62
+ /// `loss = mean(square(expected - predicted))`
59
63
///
60
64
/// - Parameters:
61
65
/// - predicted: Predicted outputs from a neural network.
@@ -68,7 +72,8 @@ public func meanSquaredError<Scalar: TensorFlowFloatingPoint>(
68
72
l2Loss ( predicted: predicted, expected: expected, reduction: _mean)
69
73
}
70
74
71
- /// Returns the mean squared logarithmic error between predictions and expectations.
75
+ /// Computes the mean squared logarithmic error between `predicted` and `expected`
76
+ /// `loss = square(log(expected) - log(predicted))`
72
77
///
73
78
/// - Note: Negative tensor entries will be clamped at `0` to avoid undefined
74
79
/// logarithmic behavior, as `log(_:)` is undefined for negative reals.
@@ -86,7 +91,8 @@ public func meanSquaredLogarithmicError<Scalar: TensorFlowFloatingPoint>(
86
91
return l2Loss ( predicted: logPredicted, expected: logExpected, reduction: _mean)
87
92
}
88
93
89
- /// Returns the mean absolute percentage error between predictions and expectations.
94
+ /// Computes the mean absolute percentage error between `predicted` and `expected`.
95
+ /// `loss = 100 * mean(abs((expected - predicted) / abs(expected)))`
90
96
///
91
97
/// - Parameters:
92
98
/// - predicted: Predicted outputs from a neural network.
@@ -99,7 +105,9 @@ public func meanAbsolutePercentageError<Scalar: TensorFlowFloatingPoint>(
99
105
100 * abs( ( expected - predicted) / abs( expected) ) . mean ( )
100
106
}
101
107
102
- /// Returns the hinge loss between predictions and expectations.
108
+ /// Computes the hinge loss between `predicted` and `expected`.
109
+ /// `loss = reduction(max(0, 1 - predicted * expected))`
110
+ /// `expected` values are expected to be -1 or 1.
103
111
///
104
112
/// - Parameters:
105
113
/// - predicted: Predicted outputs from a neural network.
@@ -114,7 +122,9 @@ public func hingeLoss<Scalar: TensorFlowFloatingPoint>(
114
122
reduction ( max ( Tensor ( 0 ) , Tensor ( 1 ) - expected * predicted) )
115
123
}
116
124
117
- /// Returns the squared hinge loss between predictions and expectations.
125
+ /// Computes the squared hinge loss between `predicted` and `expected`.
126
+ /// `loss = reduction(square(max(0, 1 - predicted * expected)))`
127
+ /// `expected` values are expected to be -1 or 1.
118
128
///
119
129
/// - Parameters:
120
130
/// - predicted: Predicted outputs from a neural network.
@@ -129,7 +139,10 @@ public func squaredHingeLoss<Scalar: TensorFlowFloatingPoint>(
129
139
reduction ( hingeLoss ( predicted: predicted, expected: expected) . squared ( ) )
130
140
}
131
141
132
- /// Returns the hinge loss between predictions and expectations.
142
+ /// Computes the categorical hinge loss between `predicted` and `expected`.
143
+ /// `loss = maximum(negative - positive + 1, 0)`
144
+ /// where `negative = max((1 - expected) * predicted)` and
145
+ /// `positive = sum(predicted * expected)`
133
146
///
134
147
/// - Parameters:
135
148
/// - predicted: Predicted outputs from a neural network.
@@ -146,8 +159,9 @@ public func categoricalHingeLoss<Scalar: TensorFlowFloatingPoint>(
146
159
return reduction ( max ( Tensor ( 0 ) , negative - positive + Tensor( 1 ) ) )
147
160
}
148
161
149
- /// Returns the logarithm of the hyperbolic cosine of the error between predictions and
150
- /// expectations.
162
+ /// Computes the logarithm of the hyperbolic cosine of the prediction error.
163
+ /// `logcosh = log((exp(x) + exp(-x))/2)`,
164
+ /// where x is the error `predicted - expected`
151
165
///
152
166
/// - Parameters:
153
167
/// - predicted: Predicted outputs from a neural network.
@@ -163,7 +177,9 @@ public func logCoshLoss<Scalar: TensorFlowFloatingPoint>(
163
177
return reduction ( x + softplus( Tensor ( - 2 ) * x) - log( Tensor ( 2 ) ) )
164
178
}
165
179
166
- /// Returns the Poisson loss between predictions and expectations.
180
+ /// Computes the Poisson loss between predicted and expected
181
+ /// The Poisson loss is the mean of the elements of the `Tensor`
182
+ /// `predicted - expected * log(predicted)`.
167
183
///
168
184
/// - Parameters:
169
185
/// - predicted: Predicted outputs from a neural network.
@@ -178,8 +194,8 @@ public func poissonLoss<Scalar: TensorFlowFloatingPoint>(
178
194
reduction ( predicted - expected * log( predicted) )
179
195
}
180
196
181
- /// Returns the Kullback-Leibler divergence (KL divergence) between between expectations and
182
- /// predictions. Given two distributions `p` and `q`, KL divergence computes `p * log(p / q)`.
197
+ /// Computes Kullback-Leibler divergence loss between `expected` and `predicted`.
198
+ /// `loss = reduction(expected * log(expected / predicted))`
183
199
///
184
200
/// - Parameters:
185
201
/// - predicted: Predicted outputs from a neural network.
@@ -194,7 +210,10 @@ public func kullbackLeiblerDivergence<Scalar: TensorFlowFloatingPoint>(
194
210
reduction ( expected * log( expected / predicted) )
195
211
}
196
212
197
- /// Returns the softmax cross entropy (categorical cross entropy) between logits and labels.
213
+ /// Computes the sparse softmax cross entropy (categorical cross entropy) between logits and labels.
214
+ /// Use this crossentropy loss function when there are two or more label classes.
215
+ /// We expect labels to be provided as integers. There should be `# classes`
216
+ /// floating point values per feature for `logits` and a single floating point value per feature for `expected`.
198
217
///
199
218
/// - Parameters:
200
219
/// - logits: One-hot encoded outputs from a neural network.
@@ -228,7 +247,10 @@ func _vjpSoftmaxCrossEntropyHelper<Scalar: TensorFlowFloatingPoint>(
228
247
return ( loss, { $0. expandingShape ( at: - 1 ) * grad } )
229
248
}
230
249
231
- /// Returns the softmax cross entropy (categorical cross entropy) between logits and labels.
250
+ /// Computes the sparse softmax cross entropy (categorical cross entropy) between logits and labels.
251
+ /// Use this crossentropy loss function when there are two or more label classes.
252
+ /// We expect labels to be provided provided in a `one_hot` representation.
253
+ /// There should be `# classes` floating point values per feature.
232
254
///
233
255
/// - Parameters:
234
256
/// - logits: Unscaled log probabilities from a neural network.
@@ -263,10 +285,10 @@ func _vjpSoftmaxCrossEntropyHelper<Scalar: TensorFlowFloatingPoint>(
263
285
return ( loss, { $0. expandingShape ( at: - 1 ) * grad } )
264
286
}
265
287
266
- /// Returns the sigmoid cross entropy (binary cross entropy) between logits and labels.
267
- ///
268
- /// The reduction is reduced over all elements. If reduced over batch size is intended, please
269
- /// consider to scale the loss .
288
+ /// Computes the sigmoid cross entropy (binary cross entropy) between logits and labels.
289
+ /// Use this cross-entropy loss when there are only two label classes (assumed to
290
+ /// be 0 and 1). For each example, there should be a single floating-point value
291
+ /// per prediction .
270
292
///
271
293
/// - Parameters:
272
294
/// - logits: The unscaled output of a neural network.
@@ -284,10 +306,10 @@ public func sigmoidCrossEntropy<Scalar: TensorFlowFloatingPoint>(
284
306
return reduction ( maxLogitsWithZero - logits * labels + log1p( exp ( - negAbsLogits) ) )
285
307
}
286
308
287
- /// Returns the Huber loss between predictions and expectations .
309
+ /// Computes the Huber loss between `predicted` and `expected` .
288
310
///
289
- /// For each value `x` in the difference ` expected - predicted`, the loss is :
290
- /// - `0.5 * x^2` if `abs(x) <= δ`.
311
+ /// For each value `x` in `error = expected - predicted`:
312
+ /// - `0.5 * x^2` if `|x| <= δ`.
291
313
/// - `0.5 * δ^2 + δ * (|x| - δ)` otherwise.
292
314
///
293
315
/// - Source: [Uncyclopedia article](https://en.wikipedia.org/wiki/Huber_loss).
0 commit comments