1
+ ``` @meta
2
+ DocTestSetup = quote
3
+ using LossFunctions
4
+ end
5
+ ```
6
+
1
7
# Efficient Sum and Mean
2
8
3
9
In many situations we are not really that interested in the
@@ -50,14 +56,14 @@ common accumulations efficiently without allocating temporary
50
56
arrays. These methods can be invoked using an additional
51
57
parameter which specifies how the values should be accumulated /
52
58
averaged. The type of this parameter has to be a subtype of
53
- ` AverageMode ` .
59
+ ` AggregateMode ` .
54
60
55
61
## Average Modes
56
62
57
63
Before we discuss these memory-efficient methods, let us briefly
58
64
introduce the available average mode types. We provide a number
59
65
of different averages modes, all of which are contained within
60
- the namespace ` AvgMode ` . An instance of such type can then be
66
+ the namespace ` AggMode ` . An instance of such type can then be
61
67
used as additional parameter to [ ` value ` ] ( @ref ) , [ ` deriv ` ] ( @ref ) ,
62
68
and [ ` deriv2 ` ] ( @ref ) , as we will see further down.
63
69
@@ -66,11 +72,11 @@ a short description of what their effect would be when used as an
66
72
additional parameter to the functions mentioned above.
67
73
68
74
``` @docs
69
- AvgMode .None
70
- AvgMode .Sum
71
- AvgMode .Mean
72
- AvgMode .WeightedSum
73
- AvgMode .WeightedMean
75
+ AggMode .None
76
+ AggMode .Sum
77
+ AggMode .Mean
78
+ AggMode .WeightedSum
79
+ AggMode .WeightedMean
74
80
```
75
81
76
82
## Unweighted Sum and Mean
@@ -82,15 +88,15 @@ broadcasted) results of [`value`](@ref), [`deriv`](@ref), and
82
88
temporary array and instead compute the result directly.
83
89
84
90
``` @docs
85
- value(::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AverageMode )
91
+ value(::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AggregateMode )
86
92
```
87
93
88
94
The exact same method signature is also implemented for
89
95
[ ` deriv ` ] ( @ref ) and [ ` deriv2 ` ] ( @ref ) respectively.
90
96
91
97
``` @docs
92
- deriv(::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AverageMode )
93
- deriv2(::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AverageMode )
98
+ deriv(::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AggregateMode )
99
+ deriv2(::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AggregateMode )
94
100
```
95
101
96
102
## Sum and Mean per Observation
@@ -110,7 +116,7 @@ that denotes the observations. For that purpose we provide the
110
116
types contained in the namespace ` ObsDim ` .
111
117
112
118
``` @docs
113
- value(::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AverageMode , ::LearnBase.ObsDimension)
119
+ value(::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AggregateMode , ::LearnBase.ObsDimension)
114
120
```
115
121
116
122
Consider the following two matrices, ` targets ` and ` outputs ` .
@@ -136,12 +142,12 @@ the observations. Thus this data would consist of two
136
142
observations with four variables each.
137
143
138
144
``` jldoctest obsdim
139
- julia> value(L1DistLoss(), targets, outputs, AvgMode .Sum(), ObsDim.First())
145
+ julia> value(L1DistLoss(), targets, outputs, AggMode .Sum(), ObsDim.First())
140
146
2-element Array{Float64,1}:
141
147
1.5
142
148
2.0
143
149
144
- julia> value(L1DistLoss(), targets, outputs, AvgMode .Mean(), ObsDim.First())
150
+ julia> value(L1DistLoss(), targets, outputs, AggMode .Mean(), ObsDim.First())
145
151
2-element Array{Float64,1}:
146
152
0.375
147
153
0.5
@@ -152,14 +158,14 @@ second/last dimension denotes the observations. In that case our
152
158
data consists of four observations with two variables each.
153
159
154
160
``` jldoctest obsdim
155
- julia> value(L1DistLoss(), targets, outputs, AvgMode .Sum(), ObsDim.Last())
161
+ julia> value(L1DistLoss(), targets, outputs, AggMode .Sum(), ObsDim.Last())
156
162
4-element Array{Float64,1}:
157
163
0.125
158
164
0.625
159
165
1.125
160
166
1.625
161
167
162
- julia> value(L1DistLoss(), targets, outputs, AvgMode .Mean(), ObsDim.Last())
168
+ julia> value(L1DistLoss(), targets, outputs, AggMode .Mean(), ObsDim.Last())
163
169
4-element Array{Float64,1}:
164
170
0.0625
165
171
0.3125
@@ -172,17 +178,17 @@ mutating version that can make use a preallocated vector to write
172
178
the results into.
173
179
174
180
``` @docs
175
- value!(::AbstractArray, ::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AverageMode , ::LearnBase.ObsDimension)
181
+ value!(::AbstractArray, ::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AggregateMode , ::LearnBase.ObsDimension)
176
182
```
177
183
178
184
Naturally we also provide both of these methods for
179
185
[ ` deriv ` ] ( @ref ) and [ ` deriv2 ` ] ( @ref ) respectively.
180
186
181
187
``` @docs
182
- deriv(::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AverageMode , ::LearnBase.ObsDimension)
183
- deriv!(::AbstractArray, ::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AverageMode , ::LearnBase.ObsDimension)
184
- deriv2(::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AverageMode , ::LearnBase.ObsDimension)
185
- deriv2!(::AbstractArray, ::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AverageMode , ::LearnBase.ObsDimension)
188
+ deriv(::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AggregateMode , ::LearnBase.ObsDimension)
189
+ deriv!(::AbstractArray, ::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AggregateMode , ::LearnBase.ObsDimension)
190
+ deriv2(::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AggregateMode , ::LearnBase.ObsDimension)
191
+ deriv2!(::AbstractArray, ::Loss, ::AbstractArray, ::AbstractArray, ::LossFunctions.AggregateMode , ::LearnBase.ObsDimension)
186
192
```
187
193
188
194
## Weighted Sum and Mean
@@ -233,7 +239,7 @@ each observation (which results in a vector), and then we compute
233
239
the weighted sum of all observations.
234
240
235
241
The following code snipped demonstrates how to compute the
236
- ` AvgMode .WeightedSum([2,1])` manually. This is ** not** meant as
242
+ ` AggMode .WeightedSum([2,1])` manually. This is ** not** meant as
237
243
an example of how to do it, but simply to show what is happening
238
244
qualitatively. In this example we assume that we are working in a
239
245
multi-variable regression setting, in which our data set has four
@@ -261,7 +267,7 @@ julia> sum(tmp .* [2, 1]) # weigh 1st observation twice as high
261
267
5.0
262
268
```
263
269
264
- To manually compute the result for ` AvgMode .WeightedMean([2,1])`
270
+ To manually compute the result for ` AggMode .WeightedMean([2,1])`
265
271
we follow a similar approach, but use the normalized weight
266
272
vector in the last step.
267
273
@@ -282,8 +288,8 @@ julia> sum(tmp .* [0.6666, 0.3333]) # weigh 1st observation twice as high
282
288
Note that you can specify explicitly if you want to normalize the
283
289
weight vector. That option is supported for computing the
284
290
weighted sum, as well as for computing the weighted mean. See the
285
- documentation for [ ` AvgMode .WeightedSum` ] ( @ref ) and
286
- [ ` AvgMode .WeightedMean` ] ( @ref ) for more information.
291
+ documentation for [ ` AggMode .WeightedSum` ] ( @ref ) and
292
+ [ ` AggMode .WeightedMean` ] ( @ref ) for more information.
287
293
288
294
The code-snippets above are of course very inefficient, because
289
295
they allocate (multiple) temporary arrays. We only included them
@@ -293,32 +299,32 @@ special methods for [`value`](@ref), [`deriv`](@ref),
293
299
[ ` deriv2 ` ] ( @ref ) and their mutating counterparts.
294
300
295
301
``` jldoctest weight
296
- julia> value(L1DistLoss(), [1.,2,3], [2,5,-2], AvgMode .WeightedSum([1,2,1]))
302
+ julia> value(L1DistLoss(), [1.,2,3], [2,5,-2], AggMode .WeightedSum([1,2,1]))
297
303
12.0
298
304
299
- julia> value(L1DistLoss(), [1.,2,3], [2,5,-2], AvgMode .WeightedMean([1,2,1]))
305
+ julia> value(L1DistLoss(), [1.,2,3], [2,5,-2], AggMode .WeightedMean([1,2,1]))
300
306
3.0
301
307
302
- julia> value(L1DistLoss(), targets, outputs, AvgMode .WeightedSum([2,1]), ObsDim.First())
308
+ julia> value(L1DistLoss(), targets, outputs, AggMode .WeightedSum([2,1]), ObsDim.First())
303
309
5.0
304
310
305
- julia> value(L1DistLoss(), targets, outputs, AvgMode .WeightedMean([2,1]), ObsDim.First())
311
+ julia> value(L1DistLoss(), targets, outputs, AggMode .WeightedMean([2,1]), ObsDim.First())
306
312
0.4166666666666667
307
313
```
308
314
309
315
We also provide this functionality for [ ` deriv ` ] ( @ref ) and
310
316
[ ` deriv2 ` ] ( @ref ) respectively.
311
317
312
318
``` jldoctest weight
313
- julia> deriv(L2DistLoss(), [1.,2,3], [2,5,-2], AvgMode .WeightedSum([1,2,1]))
319
+ julia> deriv(L2DistLoss(), [1.,2,3], [2,5,-2], AggMode .WeightedSum([1,2,1]))
314
320
4.0
315
321
316
- julia> deriv(L2DistLoss(), [1.,2,3], [2,5,-2], AvgMode .WeightedMean([1,2,1]))
322
+ julia> deriv(L2DistLoss(), [1.,2,3], [2,5,-2], AggMode .WeightedMean([1,2,1]))
317
323
1.0
318
324
319
- julia> deriv(L2DistLoss(), targets, outputs, AvgMode .WeightedSum([2,1]), ObsDim.First())
325
+ julia> deriv(L2DistLoss(), targets, outputs, AggMode .WeightedSum([2,1]), ObsDim.First())
320
326
10.0
321
327
322
- julia> deriv(L2DistLoss(), targets, outputs, AvgMode .WeightedMean([2,1]), ObsDim.First())
328
+ julia> deriv(L2DistLoss(), targets, outputs, AggMode .WeightedMean([2,1]), ObsDim.First())
323
329
0.8333333333333334
324
330
```
0 commit comments