-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathranking-expressions.html
More file actions
883 lines (849 loc) · 36.2 KB
/
ranking-expressions.html
File metadata and controls
883 lines (849 loc) · 36.2 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
---
# Copyright Vespa.ai. All rights reserved.
title: "Ranking Expressions"
redirect_from:
- /en/reference/ranking-expressions.html
---
<p>
This is a complete reference to the <em>ranking expressions</em>
used to configure application specific ranking functions.
For examples and an overview of how to use ranking expressions,
see the <a href="../../basics/ranking.html">ranking overview</a>.
</p>
<p>
Ranking expressions are written in a simple language similar to ordinary functional notation.
The atoms in ranking expressions are <em>rank features</em> and <em>constants</em>.
These atoms can be combined by <em>arithmetic operations</em> and other
<em>built-in functions</em> over scalars and tensor.
</p>
<table class="table">
<thead>
</thead><tbody>
<tr><th style="white-space:nowrap;">Rank Features</th>
<td>
<p id="rank-features">
A rank feature is a named value calculated or looked up by vespa for
each query/document combination. See the
<a href="rank-features.html">rank feature reference</a>
for a list of all the rank features available to ranking expressions.
</p>
</td>
</tr>
<tr><th>Constants</th>
<td>
<p id="constants">
A constant is either a floating point number, a boolean (true/false) or a quoted string.
Since ranking expressions can only work on scalars and tensors, strings and booleans are
immediately converted to scalars - true becomes 1.0, false 0.0 and a string its hash value.
This means that <b>strings can only be used for equality comparisons</b>, other purposes
such as parametrizing the key to slice out of a tensor will not work correctly.
</p>
</td>
</tr>
</tbody>
</table>
<h2 id="arithmetic-operations">Arithmetic operations</h2>
<p>Basic mathematical operations are expressed in in-fix notation:</p>
<pre>
a + b * c
</pre>
<p>Arithmetic operations work on any tensor in addition to scalars, and are a short form
of joining the tensors with the arithmetic operation used to join the cells.
For example <code>tensorA * tensorB</code> is the same as <code>join(tensorA, tensorB, f(a,b)(a * b))</code>.</p>
<p>All arithmetic operators in order of decreasing precedence:</p>
<table class="table">
<thead>
<tr>
<th>Arithmetic operator</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr><td>^</td> <td>Power</td></tr>
<tr><td>%</td> <td>Modulo</td></tr>
<tr><td>/</td> <td>Division</td></tr>
<tr><td>*</td> <td>Multiplication</td></tr>
<tr><td>-</td> <td>Subtraction</td></tr>
<tr><td>+</td> <td>Addition</td></tr>
<tr><td>&&</td> <td>And: 1 if both arguments are non-zero, 0 otherwise.</td></tr>
<tr><td>||</td> <td>Or: 1 if either argument is non-zero, 0 otherwise.</td></tr>
</tbody>
</table>
<h2 id="mathematical-scalar-functions">Mathematical scalar functions</h2>
<table class="table">
<thead>
<tr>
<th>Function</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr><th>acos(<em>x</em>)</th> <td><p id="acos-x">Inverse cosine of <em>x</em></p></td></tr>
<tr><th>asin(<em>x</em>)</th> <td><p id="asin-x">Inverse sine of <em>x</em></p></td></tr>
<tr><th>atan(<em>x</em>)</th> <td><p id="atan-x">Inverse tangent of <em>x</em></p></td></tr>
<tr><th>atan2(<em>y</em>, <em>x</em>)</th>
<td><p id="atan2-x">Inverse tangent of <em>y / x</em>,
using signs of both arguments to determine correct quadrant.</p></td>
</tr>
<tr><th>bit(<em>x</em>, <em>y</em>)</th>
<td><p id="bit-x-y">Returns value of bit <em>y</em> in value <em>x</em> (for int8 values)</p></td>
</tr>
<tr><th>ceil(<em>x</em>)</th> <td><p id="ceil-x">Lowest integral value not less than <em>x</em></p></td></tr>
<tr><th>cos(<em>x</em>)</th> <td><p id="cos-x">Cosine of <em>x</em></p></td></tr>
<tr><th>cosh(<em>x</em>)</th> <td><p id="cosh-x">Hyperbolic cosine of <em>x</em></p></td></tr>
<tr><th>elu(<em>x</em>)</th> <td><p id="elu-x">The Exponential Linear Unit activation function for value <em>x</em></p></td></tr>
<tr><th>erf(<em>x</em>)</th> <td><p id="erf-x">The Gauss error function for value <em>x</em></p></td></tr>
<tr><th>exp(<em>x</em>)</th> <td><p id="exp-x">Base-e exponential function.</p></td></tr>
<tr><th>fabs(<em>x</em>)</th> <td><p id="fabs-x">Absolute value of (floating-point) number <em>x</em></p></td></tr>
<tr><th>floor(<em>x</em>)</th> <td><p id="floor-x">Largest integral value not greater than <em>x</em></p></td></tr>
<tr><th>fmod(<em>x</em>, <em>y</em>)</th> <td><p id="fmod-x">Remainder of <em>x / y</em></p></td></tr>
<tr><th>isNan(<em>x</em>)</th> <td><p id="isnan-x">Returns 1.0 if <em>x</em> is NaN, 0.0 otherwise</p></td></tr>
<tr><th>ldexp(<em>x</em>, <em>exp</em>)</th> <td><p id="ldexp-x">Multiply <em>x</em> by 2 to the power of <em>exp</em></p></td></tr>
<tr><th>log(<em>x</em>)</th> <td><p id="log-x">Base-e logarithm of <em>x</em></p></td></tr>
<tr><th>log10(<em>x</em>)</th> <td><p id="log10-x">Base-10 logarithm of <em>x</em></p></td></tr>
<tr><th>max(<em>x</em>, <em>y</em>)</th> <td><p id="max-x">Larger of <em>x</em> and <em>y</em></p></td></tr>
<tr><th>min(<em>x</em>, <em>y</em>)</th> <td><p id="min-x">Smaller of <em>x</em> and <em>y</em></p></td></tr>
<tr><th>pow(<em>x</em>, <em>y</em>)</th> <td><p id="pow-x">Return <em>x</em> raised to the power of <em>y</em></p></td></tr>
<tr><th>relu(<em>x</em>)</th> <td><p id="relu-x">The Rectified Linear Unit activation function for value <em>x</em></p></td></tr>
<tr><th>sigmoid(<em>x</em>)</th> <td><p id="sigmoid-x">The sigmoid (logistic) activation function for value <em>x</em></p></td></tr>
<tr><th>sin(<em>x</em>)</th> <td><p id="sin-xx">Sine of <em>x</em></p></td></tr>
<tr><th>sinh(<em>x</em>)</th> <td><p id="sinh-x">Hyperbolic sine of <em>x</em></p></td></tr>
<tr><th>sqrt(<em>x</em>)</th> <td><p id="sqrt-x">Square root of <em>x</em></p></td></tr>
<tr><th>tan(<em>x</em>)</th> <td><p id="tan-x">Tangent of <em>x</em></p></td></tr>
<tr><th>tanh(<em>x</em>)</th> <td><p id="tanh-x">Hyperbolic tangent of <em>x</em></p></td></tr>
<tr><th>hamming(<em>x</em>, <em>y</em>)</th>
<td><p id="hamming-x-y">Hamming (bit-wise) distance between <em>x</em> and <em>y</em> (considered as 8-bit integers).</p></td>
</tr>
</tbody>
</table>
<p><code>x</code> and <code>y</code> may be any ranking expression.</p>
<h2 id="the-if-function">The if function</h2>
<p>
The <code>if</code> function chooses between two sub-expressions based on the truth value of a condition.
</p>
<pre>
if (expression1 <span class="pre-hilite">operator</span> expression2, trueExpression, falseExpression)
</pre>
<p>
If the condition given in the first argument is true,
the expression in argument 2 is used, otherwise argument 3.
The four expressions may be any ranking expression.
Conditional operators in ranking expression if functions:
</p>
<table class="table">
<thead>
<tr>
<th>Boolean operator</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr><td><=</td> <td>Less than or equal</td></tr>
<tr><td><</td> <td>Less than</td></tr>
<tr><td>==</td> <td>Equal</td></tr>
<tr><td>~=</td> <td>Approximately equal</td></tr>
<tr><td>>=</td> <td>Greater than or equal</td></tr>
<tr><td>></td> <td>Greater than</td></tr>
</tbody>
</table>
<p>The <code>in</code> membership operator uses a slightly modified if-syntax:</p>
<pre>
if (expression1 <span class="pre-hilite">in</span> [expression2, expression3, ..., expressionN], trueExpression, falseExpression)
</pre>
<p>
If expression1 is equal to either of expression2 through expressionN,
then trueExpression is used, otherwise falseExpression.
</p>
<h2 id="the-switch-function">The switch function</h2>
{% include note.html content='Available from Vespa 8.626.55' %}
<p>
The <code>switch</code> function chooses between multiple sub-expressions based on matching a value.
It provides a more readable alternative to chained <code>if</code> statements when selecting from several options.
</p>
<pre>
switch (discriminant) {
case value1: result1,
case value2: result2,
...
default: defaultResult
}
</pre>
<p>
The <code>discriminant</code> expression is compared for equality against each <code>case</code> value expression in order.
When a match is found, the corresponding result expression is evaluated and returned.
If no case matches, the <code>default</code> result is returned.
All expressions may be any ranking expression.
At least one <code>case</code> must be specified.
The <code>default</code> must be specified.
</p>
<h2 id="the-foreach-function">The foreach function</h2>
<p>
The foreach function is not really part of the expression language but implemented as a
<a href="rank-features.html#foreach(dimension,variable,feature,condition,operation)">rank feature</a>.
</p>
<h2 id="tensor-functions">Tensor functions</h2>
<p>
The following set of tensors functions are available to use in ranking expressions.
The functions are grouped in primitive functions and convenience
functions that can be implemented in terms of the primitive ones.
</p>
<h3 id="primitive-functions">Primitive functions</h3>
<table class="table">
<thead>
<tr>
<th>Function</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<th>map(<br/> tensor,<br/> f(x)(expr)<br/>)</th>
<td>
<p id="map">
Returns a new tensor with the lambda function defined in <code>f(x)(expr)</code> applied to each cell.
</p><p>
Arguments:
</p>
<ul>
<li><code>tensor</code>: a tensor expression. For example <code>attribute(tensor_field)</code></li>
<li><code>f(x)(expr)</code>: a <a href="#lambda">lambda function</a> with one argument.</li>
</ul>
Returns a new tensor where the expression in the lambda function is
evaluated in each cell in <code>tensor</code>. Examples:
<pre>
map(t, f(x)(x*x))
</pre>
<a href="https://docs.vespa.ai/playground/#N4KABGBEBmkFxgNrgmUrWQPYAd5QFNIAaFDSPBdDTAO30gEcBXAgJwE8AKAFwEoSZGpCIJIPArQDOWNgB5oAGywBDHgD4uAD0QAWALp84iAAzEAjMQBMxAMz7IQiAF8hz0hmrlcDIh+GUaE50DAC2KjgA+gRaKqE4in7BECJhEVws7Nz8xGDQ2nzaAHpWfALBrhiVYPogzkA">
playground example map
</a>
</td>
</tr>
<tr>
<th>map_subspaces(<br/> tensor,<br/> f(x)(expr)<br/>)</th>
<td>
<p id="map-subspaces">
Returns a new tensor with the lambda function defined in <code>f(x)(expr)</code> applied to each dense subspace.
</p><p>
Arguments:
</p>
<ul>
<li><code>tensor</code>: a tensor expression. For example <code>attribute(tensor_field)</code></li>
<li><code>f(x)(expr)</code>: a <a href="#lambda">lambda function</a> with one argument.</li>
</ul>
Returns a new tensor where the lambda function is evaluated for
each dense subspace in <code>tensor</code>. This is an advanced
feature that enables using dense <a href="#tensor">tensor
generator</a> expressions to transform mixed tensors. Example:
<pre>
map_subspaces(tensor(x{},y[3]):{a:[1,2,3]},f(d)(tensor(z[2])(d{y:(z)}+d{y:(z+1)})))
</pre>
<a href="https://docs.vespa.ai/playground/#N4KABGBEBmkFxgNrgmUrWQPYAd5QFNIAaFDSPBdDTAO30gEcBXAgJwE8AKAFwEoSZGpCIJIPArQDOWNgB4AlrR4AOAHxcAHsAC+xDogBMAXT5xgAQziIAjAFZiAWhsA2YzshCIOoXqHVyXAYiUhooSjQvOgZmWhwLAGMAawB9ACMFHikUgHdMgAsUgFsLHBSpZjSpeISCKUEwiBEGErKKqpq6rhZ2bn5iaC4AEz5eSRl5JVUNA1dTLgyeYeAOOC4OAHoVPj0AdkcOAFJtvlPPMJ8MS7BjEB0gA">
playground example for map_subspaces
</a>
</td>
</tr>
<tr>
<th>filter_subspaces(<br/> tensor,<br/> f(x)(expr)<br/>)</th>
<td>
<p id="filter-subspaces">
Returns a new tensor containing only the subspaces for which the lambda function defined in <code>f(x)(expr)</code> returns true.
</p><p>
Arguments:
</p>
<ul>
<li><code>tensor</code>: a tensor expression. Must have at least one mapped dimension.</li>
<li><code>f(x)(expr)</code>: a <a href="#lambda">lambda function</a> with one argument.</li>
</ul>
Returns a new tensor containing only the subspaces for which the
lambda function defined in <code>f(x)(expr)</code> returns true.
Typically used to get rid of unneeded values in sparse
tensors. Example:
<pre>
filter_subspaces(tensor(x{}):{a:1,b:2,c:3,d:4},f(value)(value>2)) # tensor(x{}):{c:3,d:4}
</pre>
<a href="https://docs.vespa.ai/playground/#N4IgZiBcDaoPYAcogMYgDQiZUAXZABLgBYCmBAjgK4CGANgJa4CeBcYB9dBKxVAdgGsAziAC+Y9PGwhSGLFFD9kuAIzy5kELlL9hcAE4AeMHTg1cAPgAUvAYOBiAlDgAMkVQGZ0qyAHZ0ACZIAFZ0Tw8wgBZIT1d0EMhAsXFJaWQ0TGw8QgA5AgZhAgATZn4aAFsGNAkpEERkOSzFEGUtXI1kT1S6hq1MhRxtQjAGfmK2A2LSAzGAczYOFFI6OlFa9K0mwaUVQM7+lboAfUNpg2s1dAIKmgAPJx7N1Hls4a0bmkFyGk-hQQIYEMRDIBAARqRhLgCB0NvUZNs3m1tN1MJptIEjLC0vCMq8WvgPsUDIhOKsCMIqGDhAgaMsimASRUQeRbv8QRZOAZyGB6MI5HC+rJ8UNkbgogdwAw6DoDMdKdTafTLt5AdZhE51U5HoKZAM3oSQDw4BUwWMedLZaQJmyAQB3JjESYMOZjehEXT6AyA4Hcykyp64rYi3ZaXAhSXigBUalSAF0xEA">
playground example for filter_subspaces
</a>
</td>
</tr>
<tr>
<th>reduce(<br/> tensor,<br/> aggregator,<br/> dim1,<br/> dim2,<br/> ...<br/>)</th>
<td>
<p id="reduce">
Returns a new tensor with the <code>aggregator</code> applied across dimensions dim1, dim2, etc.
If no dimensions are specified, reduce over all dimensions.
</p><p>
Arguments:
</p>
<ul>
<li><code>tensor</code>: a tensor expression.</li>
<li><code>aggregator</code>: the aggregator to use. See below.</li>
<li><code>dim1, dim2, ...</code>: the dimensions to reduce over. Optional.</li>
</ul>
<p>
Returns a new tensor with the aggregator applied across dimensions
<code>dim1</code>, <code>dim2</code>, etc.
If no dimensions are specified, reduce over all dimensions.
</p><p>
Available aggregators are:
</p>
<ul>
<li><code>avg</code>: arithmetic mean</li>
<li><code>count</code>: number of elements</li>
<li><code>max</code>: maximum value</li>
<li><code>median</code>: median value</li>
<li><code>min</code>: minimum value</li>
<li><code>prod</code>: product of all values</li>
<li><code>sum</code>: sum of all values</li>
</ul>
Examples:
<pre>
reduce(t, sum) # Sum all values in tensor
reduce(t, count, x) # Count number of cells along dimension x
</pre>
<a href="https://docs.vespa.ai/playground/#N4KABGBEBmkFxgNrgmUrWQPYAd5QFNIAaFDSPBdDTAO30gEcBXAgJwE8AKAFwEoSZGpCIJIPArQDOWNgB5oAGywBDHgD4uAD0QAWALp84iAAzEAjMQBMxAMz7IQiAF8hz0hmrlcDIh+GUaE50DGwEACbMAMYEAPpSzAC2sQRaKok4in7BECKhEdEEXCzs3PzEYAmJAsGuGHVg+iDOQA">
playground example reduce
</a>
</td>
</tr>
<tr>
<th>join(<br/> tensor1,<br/> tensor2,<br/> f(x,y)(expr)<br/>)</th>
<td>
<p id="join">
Returns a new tensor constructed from the <em>natural join</em> between <code>tensor1</code> and <code>tensor2</code>,
with the resulting cells having the value as calculated from <code>f(x,y)(expr)</code>, where <code>x</code>
is the cell value from <code>tensor1</code> and <code>y</code> from <code>tensor2</code>.
</p><p>
Arguments:
</p>
<ul>
<li><code>tensor1</code>: a tensor expression.</li>
<li><code>tensor2</code>: a tensor expression.</li>
<li><code>f(x,y)(expr)</code>: a <a href="#lambda">lambda function</a> with two arguments.</li>
</ul>
<p>
Returns a new tensor constructed from the <em>natural join</em>
between <code>tensor1</code> and <code>tensor2</code>,
with the resulting cells having the value as calculated from <code>f(x,y)(expr)</code>,
where <code>x</code> is the cell value from <code>tensor1</code> and
<code>y</code> from <code>tensor2</code>.
</p><p>
Formally, the result of the <code>join</code> is a new tensor with dimensions
the union of dimension between <code>tensor1</code> and <code>tensor2</code>.
The cells are the set of all combinations of cells that have equal values
on their common dimensions.
</p><p>
Examples:
</p>
<pre>{% raw %}
join(t1, t2, f(x,y)(x * y))
{% endraw %}</pre>
<a href="https://docs.vespa.ai/playground/#N4KABGBEBmkFxgNrgmUrWQPYAd5QFNIAaFDSPBdDTAO30gEcBXAgJwE8AKAFwEoSZGpCIJIPArQDOWNgB5oAGywBDHgD4uAD2ABfPnGDAtcAAy6EARgB0p4mhOWLYAEy3dkIRF1DdpDNTkuAxE-sKUaF50DGo8bACWAEbMErwCYTSEDBLSsgrKapo6fhx6BkYmdhxmzgDMtvbGZsTVTggALA0OcJYtNQgArF1Nva3OAGzunpk+GH5CgZjBYqFRFPiL5PRiAFZY8fQZwqJQewdcLOzc-PaxCcmpN2DQ2i182mAAVGAcfAJRs1QgIAuiBdEA">
playground example join
</a>
</td>
</tr>
<tr>
<th>merge(<br/> tensor1,<br/> tensor2,<br/> f(x,y)(expr)<br/>)</th>
<td>
<p id="merge">
Returns a new tensor consisting of all cells from both the arguments, where the lambda function is used
to produce a single value in the cases where both arguments provide a value for a cell.
</p><p>
Arguments:
</p>
<ul>
<li><code>tensor1</code>: a tensor expression.</li>
<li><code>tensor2</code>: a tensor expression.</li>
<li><code>f(x,y)(expr)</code>: a <a href="#lambda">lambda function</a> with two arguments.</li>
</ul>
<p>
Returns a new tensor having all the cells of both arguments,
where the lambda is invoked to produce a single value only when both arguments have a value for the same cell.
</p><p>
The argument tensors must have the same type, and that will be the type of the resulting tensor. Example:
</p>
<pre>{% raw %}
merge(t1, t2, f(left,right)(right))
{% endraw %}</pre>
<a href="https://docs.vespa.ai/playground/#N4KABGBEBmkFxgNrgmUrWQPYAd5QFNIAaFDSPBdDTAO30gEcBXAgJwE8AKAFwEoSZGpCIJIPArQDOWNlwDWBDsAC+xAB6IATAF0+cYAEM4iAIzFdxAEYmAzMQAsOlZCEQVQtUOrlcDIqQ0UJRobnQMhjw8bACWVswSvAKBQYQMEtKyCkqqGtp6BjaIAKzEAGw6xADGJgDsxAAczq5BHhheGD6YfmIBYRT4XeT0YgC27ADmBAD6BOqGozgANn2paWOTBFws7Nz8xGCR0XEJW-tg0Fwr0DzEsRMAFvxc9098AmFtqF86ICpAA">
playground example merge
</a>
</td>
</tr>
<tr>
<th>tensor(<br/> tensor-type-spec<br/>)(expr)</th>
<td>
<p id="tensor">
Generates new tensors according to type specification and expression <code>expr</code>.
</p><p>
Arguments:
</p>
<ul>
<li><code>tensor-type-spec</code>: an <a href="tensor.html#tensor-type-spec">indexed tensor type specification.</a></li>
<li><code>(expression)</code>: a <a href="#lambda">lambda function</a> expressing how to generate the tensor.</li>
</ul>
<p>
Generates new tensors according to the type specification and expression <code>expr</code>.
The tensor type must be an indexed tensor (e.g. <code>tensor<float>(x[10])</code>).
The expression in <code>expr</code> will be evaluated for each cell.
The arguments in the expression is implicitly the names of the dimensions defined in the type spec.
</p><p>
Useful for creating transformation tensors. Examples:
</p>
<pre>{% raw %}
tensor<float>(x[3])(x)
{% endraw %}</pre>
<a href="https://docs.vespa.ai/playground/#N4KABGBEBmkFxgNrgmUrWQPYAd5QFNIAaFDSPBdDTAO30gHMDaCAnAQwBcCB9VgO4kyNSEQSQetAM5Y2ACgAeiAMwBdAJRKNkERAC+I-aQzVyuBkROjKaPXQbNWnHvwIDeAJmE1M4qFKybAA80AA2WNwAfEqInmrEAJ5xmkpgALzpYIk69oYYxiJmmBYSVvYU+MXk9BJcPr6EDIFysQAsmnCIAIwAdAAMxGCeA0Mqo2BtA2q6vvmohaYVpU3W5LbVDhJO7Nx8grzQbFgAtrwctFhcABbsvC1sDb5izSxB8tApWlzAinDQAGpuvpcnMjCg1CB9EA">
playground generate examples
</a>
</td>
</tr>
<tr>
<th>rename(<br/> tensor,<br/> dim-to-rename,<br/> new-names<br/>)</th>
<td>
<p id="rename">
Renames one or more dimensions in the tensor.
</p><p>
Arguments:
</p>
<ul>
<li><code>tensor</code>: a tensor expression.</li>
<li><code>dim-to-rename</code>: a dimension, or list of dimensions, to rename.</li>
<li><code>new-names</code>: new names for the dimensions listed above.</li>
</ul>
Returns a new tensor with one or more dimension renamed. Examples:
<pre>{% raw %}
rename(t1,x,z)
{% endraw %}</pre>
<a href="https://docs.vespa.ai/playground/#N4KABGBEBmkFxgNrgmUrWQPYAd5QFNIAaFDSPBdDTAO30gBcSybIiEmDaBnLAJwAUAD2ABfYgE9xASjjBgwuAAYpKsQgCMAOlVolqyXE0awy3cX3G1y0+b2LrRk1t1jIrCGNYTW1crgMRKQ0UJRonnQM-NwAhgC2BAD6BMIJOAA2wZEQ7NFxiYKMxMLEAF4yHqHeGL4Y-piBnNmhFPgN5PScMbQJyanpWUmMAO5YLKG5HFA9fUXEIlIyCwCWxABWMpWRNai7ALogYkA">
playground rename examples
</a>
</td>
</tr>
<tr>
<th>concat(<br/> tensor1,<br/> tensor2,<br/> dim<br/>)</th>
<td>
<p id="concat">
Concatenates two tensors along dimension <code>dim</code>.
</p><p>
Arguments:
</p>
<ul>
<li><code>tensor1</code>: a tensor expression.</li>
<li><code>tensor2</code>: a tensor expression.</li>
<li><code>dim</code>: the dimension to concatenate along.</li>
</ul>
Returns a new tensor with the two tensors <code>tensor1</code> and
<code>tensor2</code> concatenated along dimension <code>dim</code>. Examples:
<pre>{% raw %}
concat(t,t2,x)
{% endraw %}</pre>
<a href="https://docs.vespa.ai/playground/#N4KABGBEBmkFxgNrgmUrWQPYAd5QFNIAaFDSPBdDTAO30gBcSybIiEmDaBnLAJwAUAD0QBmALoBKOIgAsxAKzEAbBMisIAX1ZbSGauVwMi+tpTSa6DRgCYWNTByiNufIaNvTZY4nPVWOhh6rIaYxpymVhT4YeT0nADGWLSJAIbMZo7sDMmpGYKMxHbEwlIajkGoIQbREYQO5rFWEJAJUHnpjAD6WIwAFgT83QDuaQCejdnOkJ0FJUVlFTRV2igSIFpAA">
playground concat examples
</a>
</td>
</tr>
<tr>
<th style="white-space:nowrap;">(tensor)partial-address</th>
<td>
<p id="slice">
Slice - returns a new tensor containing the cells matching the partial address.
</p><p>
Arguments:
</p>
<ul>
<li><code>tensor</code>: a tensor expression.</li>
<li><code>partial-address</code>: Can be given in the form of a tensor address <code>{dimension:label,..}</code>,
or for tensors referenced directly and having a single mapped or indexed type respectively
as just a label in curly brackets <code>{label}</code>
or just an index in square brackets, <code>[index]</code>.
Index labels may be specified by a lambda expression enclosed in parentheses.</li>
</ul>
Returns a new tensor containing the cells matching the partial address.
A common special case is producing a single value by specifying a full address.
The type of the resulting tensor is the dimensions of the argument tensor not specified by the partial address. Examples:
<pre>{% raw %}
# a_tensor is of type tensor(key{},x[2])
a_tensor{key:key1,x:1}
{% endraw %}</pre>
<a href="https://docs.vespa.ai/playground/#N4KABGBEBmkFxgNrgmUrWQPYAd5QFNIAaFDSPBdDTAO30gBcSybIiEmDaBnLAJwAUAD0QBmALoBKOIgAsxAKzEAbBMisIAX1ZbSGauVwMi+tpTSa6DHgBsAlgGNTViOwaNgwuAAYtGmjAdDD1WQ0xjThdAinxw8npOOycCAH0AJhZAtw4oT290-ytg1FCDK2wLdzNyC3jrTkZMmrZcrl4BQQBrAgBPYD1RdOk4YDAe3oBGWUmAOh9iMHT5iUWJ9NkxecW5FaCAmhLtFvrKkyzzONcoRKhk51SAWwBDHBwCABNU+1oPgmFPhcYm0msAJnAJpNiN5JkVAkcgicKpFCEDMHVrpBbpB7mkXm9Pt9fv9Cc1MSD0mC+hC+lDvIIxABadJSOGHXQoCQgLRAA">
playground slice examples
</a>
</td>
</tr>
<tr>
<th style="white-space:nowrap;">tensor-literal-form</th>
<td>
<p id="literal">
Returns a new tensor having the type and cell values given explicitly.
Each cell value may be supplied by a lambda which can access other features.
</p><p>
Returns a new tensor from the <a href="tensor.html#tensor-literal-form">literal form</a>,
where the type must be specified explicitly.
Each value may be supplied by a lambda,
which - in contrast to all other lambdas -
<em>may refer to features and expressions from the context</em>. Examples:
</p>
<pre>{% raw %}
# Declare an indexed tensor
tensor(x[2]):[1.0, 2.0]
# Declare an mapped tensor
tensor(x{}):{x1:3, x2:4}
{% endraw %}</pre>
</td>
</tr>
<tr>
<th>cell_cast(<br/> tensor,<br/> cell_type<br/>)</th>
<td>
<p id="cell_cast">
Returns a new tensor that is the same as the argument, except
that all cell values are converted to the given
<a href="tensor.html#tensor-type-spec">cell type</a>.
</p><p>
Arguments:
</p>
<ul>
<li><code>tensor</code>: a tensor expression.</li>
<li><code>cell_type</code>: wanted cell type.</li>
</ul>
Example, casting from <code>bfloat16</code> to <code>float</code>:
<pre>{% raw %}
# With a tensor t of the type tensor<bfloat16>(x[5])(x+1)
cell_cast(t, float)
{% endraw %}</pre>
</td>
</tr>
<tr>
<th>cell_order(<br/> tensor,<br/> order<br/>)</th>
<td>
<p id="cell-order">
Returns a new tensor with the rank of the original cells based on the given order.
</p><p>
Arguments:
</p>
<ul>
<li><code>tensor</code>: a tensor expression.</li>
<li><code>order</code>: <code>max</code> or <code>min</code></li>
</ul>
Returns a new tensor with the rank of the original cells based on
the given order. With <code>max</code> the largest value gets
rank 0. With <code>min</code> the smallest value gets rank
0. Examples:
<pre>
cell_order(tensor(x[3]):[2,3,1],max) # tensor(x[3]):[1,0,2]
cell_order(tensor(x[3]):[2,3,1],min) # tensor(x[3]):[1,2,0]
</pre>
<a href="https://docs.vespa.ai/playground/#N4IgZiBcDaoPYAcogMYgDQiZUAXZABLgBYCmBAjgK4CGANgJa4CeBcYB9dBKxVAdgGsAziAC+Y9PGwhSGLFFD9kuAIzy5kELlL9hcAE4AeMHTg1cAPgAUvAYOBiAlDgAMkVQGZ0qyAHZ0ACZIAFZ0Tw8wgBZIT1d0EMhAsXFJaWQ0TGw8QgA5AgZhAgATZn4aAFsGNAkpEERkOSzFEGUtXI1kT1S6hq1MhRxtQjAGfmK2A2LSAzGAczYOFFI6OlFa9K0mwaUVQM7+lboAfUNpg2s1dAIKmgAPJx7N1Hls4a0bmkFyGk-hQQIYEMRDIBAARqRhLgCB0NvUZNs3m1tN1MJptIEjLC0vCMq8WvgPsUDIhOKsCMIqGDhAgaMsimASRUQeRbv8QRZOAZyGB6MI5HC+rJ8UNkbgogdwAw6DoDMdKdTafTLt5AdZhE51U5HoKZAM3oSQDw4BUwWMedLZaQJmyAQB3JjESYMOZjehEXT6AyA4Hcykyp64rYi3ZaXAhSXigBUalSAF0xEA">
playground example for cell_order
</a>
</td>
</tr>
</tbody>
</table>
<h3 id="lambda">Lambda functions in primitive functions</h3>
<p>
Some of the primitive functions accept lambda functions that are evaluated
and applied to a set of tensor cells.
The functions contain a single expression that have the same format and built-in functions as
<a href="ranking-expressions.html">general ranking expressions</a>.
However, the atoms are the arguments defined in the argument list of the lambda.
</p><p>
The expression cannot access variables or data structures outside the lambda,
i.e. they are not closures.
</p><p>
Examples:
</p>
<pre>
f(x)(log(x))
f(x,y)(if(x < y, 0, 1))
</pre>
<h3 id="non-primitive-functions">Non-primitive functions</h3>
<p>
Non-primitive functions can be implemented by primitive functions,
but are not necessarily so for performance reasons.
Note that all the arithmetic operators, comparison operators, and
scalar operations can also be applied to tensors directly,
those are not repeated below here.
</p>
<table class="table">
<thead>
<tr>
<th>Function</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<th>argmax(t, dim)</th>
<td>
<p id="argmax"><code>join(t, reduce(t, max, dim), f(x,y)(if (x == y, 1, 0)))</code></p>
<p>
Returns a tensor with cell(s) of the highest value(s) in the tensor set to 1.
The dimension argument follows the same format as reduce as multiple dimensions can be given and is optional.
</p>
</td>
</tr>
<tr>
<th>argmin(t, dim)</th>
<td>
<p id="argmin"><code>join(t, reduce(t, min, dim), f(x,y)(if (x == y, 1, 0)))</code></p>
<p>
Returns a tensor with cell(s) of the lowest value(s) in the tensor set to 1.
The dimension argument follows the same format as reduce as multiple dimensions can be given and is optional.
</p>
</td>
</tr>
<tr>
<th>avg(t, dim)</th>
<td>
<p id="avg-t"><code>reduce(t, avg, dim)</code></p>
<p>
Reduce the tensor with the <code>average</code> aggregator along dimension <code>dim</code>.
If the dimension argument is omitted, this reduces over all dimensions.
</p>
</td>
</tr>
<tr>
<th>count(t, dim)</th>
<td>
<p id="count-t"><code>reduce(t, count, dim)</code></p>
<p>
Reduce the tensor with the <code>count</code> aggregator along dimension <code>dim</code>.
If the dimension argument is omitted, this reduces over all dimensions.
</p>
</td>
</tr>
<tr>
<th>cosine_similarity(t1, t2, dim)</th>
<td>
<p id="cosine-similarity-t"><code>reduce(t1*t2, sum, dim) / sqrt(reduce(t1*t1, sum, dim) * reduce(t2*t2, sum, dim))</code></p>
<p>The cosine similarity between the two vectors in the given dimension.</p>
</td>
</tr>
<tr>
<th>diag(n1, n2)</th>
<td>
<p id="diag-n1-n2"><code>tensor(i[n1],j[n2])(if (i==j, 1.0, 0.0)))</code></p>
<p>Returns a tensor with the diagonal set to 1.0.</p>
</td>
</tr>
<tr>
<th>elu(t)</th>
<td>
<p id="elu-t"><code>map(t, f(x)(if(x < 0, exp(x)-1, x)))</code></p>
<p><a href="https://arxiv.org/abs/1511.07289">Exponential linear unit</a>.</p>
</td>
</tr>
<tr>
<th>euclidean_distance(t1, t2, dim)</th>
<td>
<p id="euclidean-distance-t"><code>join(reduce(map(join(t1, t2, f(x,y)(x-y)), f(x)(x * x)), sum, dim), f(x)(sqrt(x)))</code></p>
<p>euclidean_distance: <code>sqrt(sum((t1-t2)^2, dim))</code>.</p>
</td>
</tr>
<tr>
<th>expand(t, dim)</th>
<td>
<p id="expand-t"><code>t * tensor(dim[1])(1)</code></p>
<p>Adds an indexed dimension with name <code>dim</code> to the tensor <code>t</code>.</p>
</td>
</tr>
<tr>
<th>hamming(t1, t2)</th>
<td>
<p id="hamming-t1-t2"><code>join(t1, t2, f(x,y)(hamming(x,y)))</code></p>
<p>
Join and return the Hamming distance between matching cells of <code>t1</code> and <code>t2</code>.
This function is mostly useful when the input contains vectors with binary data
and summing the hamming distance over the vector dimension, e.g.:
</p>
<table class="table">
<thead></thead><tbody>
<tr>
<td>type of input <em>t1</em> →</td><td>
<code>tensor<int8>(dimone{},z[32])</code>
</td>
</tr><tr>
<td>type of input <em>t2</em> →</td><td>
<code>tensor<int8>(dimtwo{},z[32])</code>
</td>
</tr><tr>
<td>expression →</td><td>
<code>reduce(join(t1, t2, f(a,b)(hamming(a,b)), sum, z)</code>
</td>
</tr><tr>
<td>output type →</td><td>
<code>tensor<float>(dimone{},dimtwo{})</code>
</td>
</tr>
</tbody>
</table>
<p>
Note that the cell values are always treated as if they were both 8-bit integers in the range [-128,127],
and only then counting the number of bits that are different.
See also the corresponding <a href="../schemas/schemas.html#distance-metric">distance metric</a>.
Arguments can be scalars.
</p>
</td>
</tr>
<tr>
<th>l1_normalize(t, dim)</th>
<td>
<p id="l1-normalize-t"><code>join(t, reduce(t, sum, dim), f(x,y) (x / y))</code></p>
<p>L1 normalization: <code>t / sum(t, dim)</code>.</p>
</td>
</tr>
<tr>
<th>l2_normalize(t, dim)</th>
<td>
<p id="l2-normalize-t"><code>join(t, map(reduce(map(t, f(x)(x * x)), sum, dim), f(x)(sqrt(x))), f(x,y)(x / y))</code></p>
<p>L2 normalization: <code>t / sqrt(sum(t^2, dim)</code>.</p>
</td>
</tr>
<tr>
<th>matmul(t1, t2, dim)</th>
<td>
<p id="matmul-t1-t2"><code>reduce(join(t1, t2, f(x,y)(x * y)), sum, dim)</code></p>
<p>
Matrix multiplication of two tensors.
This is the product of the two tensors summed along a shared dimension.
</p>
</td>
</tr>
<tr>
<th>max(t, dim)</th>
<td>
<p id="max-t"><code>reduce(t, max, dim)</code></p>
<p>Reduce the tensor with the <code>max</code> aggregator along dimension <code>dim</code>.</p>
</td>
</tr>
<tr>
<th>median(t, dim)</th>
<td>
<p id="median-t"><code>reduce(t, median, dim)</code></p>
<p>
Reduce the tensor with the <code>median</code> aggregator along dimension <code>dim</code>.
If the dimension argument is omitted, this reduces over all dimensions.
</p>
</td>
</tr>
<tr>
<th>min(t, dim)</th>
<td>
<p id="min-t"><code>reduce(t, min, dim)</code></p>
<p>Reduce the tensor with the <code>min</code> aggregator along dimension <code>dim</code>.</p>
</td>
</tr>
<tr>
<th>prod(t, dim)</th>
<td>
<p id="prod-t"><code>reduce(t, prod, dim)</code></p>
<p>
Reduce the tensor with the <code>product</code> aggregator along dimension <code>dim</code>.
If the dimension argument is omitted, this reduces over all dimensions.
</p>
</td>
</tr>
<tr>
<th>random(n1, n2, ...)</th>
<td>
<p id="random-n1-n2"><code>tensor(i1[n1],i2[n2],...)(random(1.0))</code></p>
<p>Returns a tensor with random values between 0.0 and 1.0, uniform distribution.</p>
</td>
</tr>
<tr>
<th>range(n)</th>
<td>
<p id="range-t"><code>tensor(i[n])(i)</code></p>
<p>Returns a tensor with increasing values.</p>
</td>
</tr>
<tr>
<th>relu(t)</th>
<td>
<p id="relu-t"><code>map(t, f(x)(max(0,x)))</code></p>
<p>Rectified linear unit.</p>
</td>
</tr>
<tr>
<th>sigmoid(t)</th>
<td>
<p id="sigmoid-t"><code>map(t, f(x)(1.0 / (1.0 + exp(0.0-x))))</code></p>
<p>Returns the sigmoid of each element.</p>
</td>
</tr>
<tr>
<th>softmax(t, dim)</th>
<td>
<p id="softmax-t"><code>join(map(t, f(x)(exp(x))), reduce(map(t, f(x)(exp(x))), sum, dim), f(x,y)(x / y))</code></p>
<p>The softmax of the tensor, e.g. <code>e^x / sum(e^x)</code>.</p>
</td>
</tr>
<tr>
<th>sum(t, dim)</th>
<td>
<p id="sum-t"><code>reduce(t, sum, dim)</code></p>
<p>
Reduce the tensor with the <code>summation</code> aggregator along dimension <code>dim</code>.
If the dimension argument is omitted, this reduces over all dimensions.
</p>
</td>
</tr>
<tr>
<th>top(n, t)</th>
<td>
<p id="top-t"><code>t * filter_subspaces(cell_order(t, max) < n, f(s)(s))</code></p>
<p>top N function: Picks top N cells in a simple mapped tensor.</p>
</td>
</tr>
<tr>
<th>unpack_bits(t)</th>
<td>
<p id="unpack-bits">unpacks bits from int8 input to 8 times as many floats</p>
<p>The innermost indexed dimension will expand to have 8 times as many cells,
each with a float value of either 0.0 or 1.0 determined by one bit in
the 8-bit input value. Comparable to <code>numpy.unpackbits</code>
which gives the same basic functionality.
A minimal input such as <code>tensor<int8>(x[1]):[9]</code>
would give output <code>tensor<float>(x[8]):[0,0,0,0,1,0,0,1]</code>
(default bit-order is big-endian).
As a very complex example, an input with type <code>tensor<int8>(foo{},x[3],y[11],z{})</code>
will produce output with type <code>tensor<float>(foo{},x[3],y[88],z{})</code>
where "foo", "x" and "z" are unchanged, as "y" is the innermost indexed dimension.
</p>
</td>
</tr>
<tr>
<th>unpack_bits(t, cell_type)</th>
<td>
<p id="unpack-bits-cell-type">unpacks bits from int8 input to 8 times as many values</p>
<p>Same as above, but with optionally different cell_type (could be <code>double</code>
for example, if you will combine the output with other tensors using
double).
</p>
</td>
</tr>
<tr>
<th>unpack_bits(t, cell_type, endian)</th>
<td>
<p id="unpack-bits-cell-type-endian">unpacks bits from int8 input to 8 times as many values</p>
<p>Same as above, but also optionally different endian for the bits; must be
either <code>big</code> (default) or <code>little</code>.
</p>
</td>
</tr>
<tr>
<th>xw_plus_b(x, w, b, dim)</th>
<td>
<p id="xw-plus-b"><code>join(reduce(join(x, w, f(x,y)(x * y)), sum, dim), b, f(x,y)(x+y))</code></p>
<p>
Matrix multiplication of <code>x</code> (usually a vector) and <code>w</code> (weights),
with <code>b</code> added (bias).
A typical operation for activations in a neural network layer,
e.g. <code>sigmoid(xw_plus_b(x,w,b)))</code>.
</p>
</td>
</tr>
</tbody>
</table>