-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexpr.h
More file actions
2096 lines (1940 loc) · 69.5 KB
/
expr.h
File metadata and controls
2096 lines (1940 loc) · 69.5 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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* \file expr.h
* \defgroup operations operations
* \defgroup inputoperations inputoperations
* \defgroup arithmeticoperations arithmeticoperations
* \defgroup lossoperations lossoperations
* \defgroup flowoperations flowoperations
* \defgroup noiseoperations noiseoperations
* \defgroup convolutionoperations convolutionoperations
* \defgroup tensoroperations tensoroperations
* \defgroup linalgoperations linalgoperations
* \defgroup normoperations normoperations
* \brief The various operations that you can use in building a DyNet graph
*
* \details TODO: **This documentation is incomplete. See expr.h for a full list of expressions.**
*/
#ifndef DYNET_EXPR_H
#define DYNET_EXPR_H
#include "dynet/dynet.h"
#include "dynet/nodes.h"
#include "dynet/nodes-contract.h"
#include <stdexcept>
namespace dynet {
/**
* \ingroup operations
* \brief Expressions are the building block of a Dynet computation graph
* \details [long description]
*/
struct Expression {
ComputationGraph *pg;
VariableIndex i;
unsigned graph_id;
Expression() : pg(nullptr), i(0), graph_id(0) { }
const bool is_stale() const {return (get_number_of_active_graphs() != 1 || graph_id != get_current_graph_id());}
/**
* \brief Base expression constructor
* \details Used when creating operations
*
* \param pg Pointer to the computation graph
* \param i Variable index
* \param name Name of the expression
*/
Expression(ComputationGraph *pg, VariableIndex i) : pg(pg), i(i), graph_id(pg->get_id()) { }
/**
* \brief Get value of the expression
* \details Throws a tuntime_error exception if no computation graph is available
* \return Value of the expression as a tensor
*/
const Tensor& value() const {
if (this->is_stale()) {
throw std::runtime_error("Attempt to use a stale expression.");
}
return pg->get_value(i);
}
/**
* \brief Get gradient of the expression
* \details Throws a tuntime_error exception if no computation graph is available
*
* Make sure to call `backward` on a downstream expression before calling this.
*
* If the expression is a constant expression (meaning it's not a function of a parameter), dynet won't compute it's gradient for the sake of efficiency. You need to manually force the gradient computation by adding the agument `full=true` to `backward`
* \return Value of the expression as a tensor
*/
const Tensor& gradient() const {
if (this->is_stale()) {
throw std::runtime_error("Attempt to use a stale expression.");
}
return pg->get_gradient(i);
}
/**
* \brief Get dimension of the expression
* \details Throws a tuntime_error exception if no computation graph is available
* \return Dimension of the expression
*/
const Dim& dim() const {
if (this->is_stale()) {
throw std::runtime_error("Attempt to use a stale expression.");
}
return pg->get_dimension(i);
}
};
namespace detail {
template <typename F, typename T>
Expression f(const T& xs) {
ComputationGraph *pg = xs.begin()->pg;
std::vector<VariableIndex> xis(xs.size());
int i = 0;
for (auto xi = xs.begin(); xi != xs.end(); ++xi) xis[i++] = xi->i;
return Expression(pg, pg->add_function<F>(xis));
}
template <typename F, typename T, typename T1>
Expression f(const T& xs, const T1& arg1) {
ComputationGraph *pg = xs.begin()->pg;
std::vector<VariableIndex> xis(xs.size());
int i = 0;
for (auto xi = xs.begin(); xi != xs.end(); ++xi) xis[i++] = xi->i;
return Expression(pg, pg->add_function<F>(xis, arg1));
}
}
////////////////////////////////////////////////
// Input operations //
////////////////////////////////////////////////
/**
* \ingroup inputoperations
* \brief Scalar input
* \details Create an expression that represents the scalar value s
*
* \param g Computation graph
* \param s Real number
*
* \return An expression representing s
*/
Expression input(ComputationGraph& g, real s);
/**
* \ingroup inputoperations
* \brief Modifiable scalar input
* \details Create an expression that represents the scalar value *ps.
* If *ps is changed and the computation graph recalculated, the
* next forward pass will reflect the new value.
*
* \param g Computation graph
* \param ps Real number pointer
*
* \return An expression representing *ps
*/
Expression input(ComputationGraph& g, const real *ps);
/**
* \ingroup inputoperations
* \brief Vector/matrix/tensor input
* \details Create an expression that represents a vector, matrix, or tensor
* input. The dimensions of the input are defined by ``d``. So for example
* > ``input(g,{50},data)``: will result in a 50-length vector
* > ``input(g,{50,30},data)``: will result in a 50x30 matrix
* and so on, for an arbitrary number of dimensions.
* This function can also be used to import minibatched inputs. For example,
* if we have 10 examples in a minibatch, each with size 50x30, then we call
* > ``input(g,Dim({50,30},10),data)``
* The data vector "data" will contain the values used to fill the input, in
* column-major format. The length must add to the product of all dimensions in
* d.
*
* \param g Computation graph
* \param d Dimension of the input matrix
* \param data A vector of data points
*
* \return An expression representing data
*/
Expression input(ComputationGraph& g, const Dim& d, const std::vector<float>& data);
/**
* \ingroup inputoperations
* \brief Updatable vector/matrix/tensor input
* \details Similarly to input that takes a vector reference, input a vector, matrix,
* or tensor input. Because we pass the pointer, the data can be updated.
*
* \param g Computation graph
* \param d Dimension of the input matrix
* \param pdata A pointer to an (updatable) vector of data points
*
* \return An expression representing *pdata
*/
Expression input(ComputationGraph& g, const Dim& d, const std::vector<float>* pdata);
/**
* \ingroup inputoperations
* \brief Sparse vector input
* \details This operation takes input as a sparse matrix of index/value pairs. It is
* exactly the same as the standard input via vector reference, but sets all
* non-specified values to "defdata" and resets all others to the appropriate
* input values.
*
* \param g Computation graph
* \param d Dimension of the input matrix
* \param ids The indexes of the data points to update
* \param data The data points corresponding to each index
* \param defdata The default data with which to set the unspecified data points
*
* \return An expression representing data
*/
Expression input(ComputationGraph& g, const Dim& d, const std::vector<unsigned int>& ids, const std::vector<float>& data, float defdata = 0.f);
/**
* \ingroup inputoperations
* \brief Load parameter
* \details Load parameters into the computation graph.
*
* \param g Computation graph
* \param p Parameter object to load
*
* \return An expression representing p
*/
Expression parameter(ComputationGraph& g, Parameter p);
/**
* \ingroup inputoperations
* \brief Load lookup parameter
* \details Load a full tensor of lookup parameters into the computation graph.
* Normally lookup parameters are accessed by using the lookup() function
* to grab a single element. However, in some cases we'll want to access
* all of the parameters in the entire set of lookup parameters for some
* reason. In this case you can use this function. In this case, the
* first dimensions in the returned tensor will be equivalent to the
* dimensions that we would get if we get calling the lookup() function,
* and the size of the final dimension will be equal to the size of the
* vocabulary.
*
* \param g Computation graph
* \param lp LookupParameter object to load
*
* \return An expression representing lp
*/
Expression parameter(ComputationGraph& g, LookupParameter lp);
/**
* \ingroup inputoperations
* \brief Load constant parameters
* \details Load parameters into the computation graph, but prevent them from being
* updated when performing parameter update.
*
* \param g Computation graph
* \param p Parameter object to load
*
* \return An expression representing the constant p
*/
Expression const_parameter(ComputationGraph& g, Parameter p);
/**
* \ingroup inputoperations
* \brief Load constant lookup parameters
* \details Load lookup parameters into the computation graph, but prevent them from being
* updated when performing parameter update.
*
* \param g Computation graph
* \param lp LookupParameter object to load
*
* \return An expression representing the constant lp
*/
Expression const_parameter(ComputationGraph& g, LookupParameter lp);
/**
* \ingroup inputoperations
* \brief Look up parameter
* \details Look up parameters according to an index, and load them into the
* computation graph.
*
* \param g Computation graph
* \param p LookupParameter object from which to load
* \param index Index of the parameters within p
*
* \return An expression representing p[index]
*/
Expression lookup(ComputationGraph& g, LookupParameter p, unsigned index);
/**
* \ingroup inputoperations
* \brief Look up parameters with modifiable index
* \details Look up parameters according to the *pindex, and load them into the
* computation graph. When *pindex changes, on the next computation of
* forward() the values will change.
*
* \param g Computation graph
* \param p LookupParameter object from which to load
* \param pindex Pointer index of the parameters within p
*
* \return An expression representing p[*pindex]
*/
Expression lookup(ComputationGraph& g, LookupParameter p, const unsigned* pindex);
/**
* \ingroup inputoperations
* \brief Look up parameter
* \details Look up parameters according to an index, and load them into the
* computation graph. Do not perform gradient update on the parameters.
*
* \param g Computation graph
* \param p LookupParameter object from which to load
* \param index Index of the parameters within p
*
* \return A constant expression representing p[index]
*/
Expression const_lookup(ComputationGraph& g, LookupParameter p, unsigned index);
/**
* \ingroup inputoperations
* \brief Constant lookup parameters with modifiable index
* \details Look up parameters according to the *pindex, and load them into the
* computation graph. When *pindex changes, on the next computation of
* forward() the values will change. However, gradient updates will not be
performend.
*
* \param g Computation graph
* \param p LookupParameter object from which to load
* \param pindex Pointer index of the parameters within p
*
* \return A constant expression representing p[*pindex]
*/
Expression const_lookup(ComputationGraph& g, LookupParameter p, const unsigned* pindex);
// Batched versions of lookup and const_lookup
/**
* \ingroup inputoperations
* \brief Look up parameters
* \details The mini-batched version of lookup. The resulting expression will be
* a mini-batch of parameters, where the "i"th element of the batch corresponds
* to the parameters at the position specified by the "i"th element of
* "indices"
*
* \param g Computation graph
* \param p LookupParameter object from which to load
* \param indices Index of the parameters at each position in the batch
*
* \return An expression with the "i"th batch element representing p[indices[i]]
*/
Expression lookup(ComputationGraph& g, LookupParameter p, const std::vector<unsigned>& indices);
/**
* \ingroup inputoperations
* \brief Look up parameters
* \details The mini-batched version of lookup with modifiable parameter indices.
*
* \param g Computation graph
* \param p LookupParameter object from which to load
* \param pindices Pointer to lookup indices
*
* \return An expression with the "i"th batch element representing p[*pindices[i]]
*/
Expression lookup(ComputationGraph& g, LookupParameter p, const std::vector<unsigned>* pindices);
/**
* \ingroup inputoperations
* \brief Look up parameters
* \details Mini-batched lookup that will not update the parameters.
*
* \param g Computation graph
* \param p LookupParameter object from which to load
* \param indices Lookup indices
*
* \return A constant expression with the "i"th batch element representing p[indices[i]]
*/
Expression const_lookup(ComputationGraph& g, LookupParameter p, const std::vector<unsigned>& indices);
/**
* \ingroup inputoperations
* \brief Look up parameters
* \details Mini-batched lookup that will not update the parameters, with modifiable
* indices.
*
* \param g Computation graph
* \param p LookupParameter object from which to load
* \param pindices Lookup index pointers.
*
* \return A constant expression with the "i"th batch element representing
* p[*pindices[i]]
*/
Expression const_lookup(ComputationGraph& g, LookupParameter p, const std::vector<unsigned>* pindices);
/**
* \ingroup inputoperations
* \brief Create an input full of zeros
* \details Create an input full of zeros, sized according to dimensions d.
*
* \param g Computation graph
* \param d The dimensions of the input
*
* \return A "d" dimensioned zero vector
*/
Expression zeroes(ComputationGraph& g, const Dim& d);
/**
* \ingroup inputoperations
* \brief Create a random normal vector
* \details Create a vector distributed according to normal distribution with mean
* 0, variance 1.
*
* \param g Computation graph
* \param d The dimensions of the input
*
* \return A "d" dimensioned normally distributed vector
*/
Expression random_normal(ComputationGraph& g, const Dim& d);
/**
* \ingroup inputoperations
* \brief Create a random bernoulli vector
* \details Create a vector distributed according to bernoulli distribution with parameter p.
*
* \param g Computation graph
* \param d The dimensions of the input
* \param p The bernoulli p parameter
* \param scale A scaling factor for the output ("active" elements will receive this value)
*
* \return A "d" dimensioned bernoulli distributed vector
*/
Expression random_bernoulli(ComputationGraph& g, const Dim& d, real p, real scale = 1.0f);
/**
* \ingroup inputoperations
* \brief Create a random uniform vector
* \details Create a vector distributed according to uniform distribution with boundaries left and right.
*
* \param g Computation graph
* \param d The dimensions of the input
* \param left The left boundary
* \param right The right boundary
*
* \return A "d" dimensioned uniform distributed vector
*/
Expression random_uniform(ComputationGraph& g, const Dim& d, real left, real right);
/**
* \ingroup inputoperations
* \brief Create a random Gumbel sampled vector
* \details Create a vector distributed according to a Gumbel distribution with the specified parameters. (Currently only the defaults of mu=0.0 and beta=1.0 supported.
*
* \param g Computation graph
* \param d The dimensions of the input
* \param mu The mu parameter
* \param beta The beta parameter
*
* \return A "d" dimensioned Gumbel distributed vector
*/
Expression random_gumbel(ComputationGraph& g, const Dim& d, real mu = 0.0, real beta = 1.0);
////////////////////////////////////////////////
// Arithmetic operations //
////////////////////////////////////////////////
/**
* \ingroup arithmeticoperations
* \brief Negation
* \details Negate the passed argument.
*
* \param x An input expression
*
* \return The negation of x
*/
Expression operator-(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Expression addition
* \details Add two expressions of the same dimensions.
*
* \param x The first input
* \param y The second input
*
* \return The sum of x and y
*/
Expression operator+(const Expression& x, const Expression& y);
/**
* \ingroup arithmeticoperations
* \brief Scalar addition
* \details Add a scalar to an expression
*
* \param x The expression
* \param y The scalar
*
* \return An expression equal to x, with every component increased by y
*/
Expression operator+(const Expression& x, real y);
/**
* \ingroup arithmeticoperations
* \brief Scalar addition
* \details Add a scalar to an expression
*
* \param x The scalar
* \param y The expression
*
* \return An expression equal to y, with every component increased by x
*/
Expression operator+(real x, const Expression& y);
/**
* \ingroup arithmeticoperations
* \brief Expression subtraction
* \details Subtract one expression from another.
*
* \param x The expression from which to subtract
* \param y The expression to subtract
*
* \return An expression where the ith element is x_i minus y_i
*/
Expression operator-(const Expression& x, const Expression& y);
/**
* \ingroup arithmeticoperations
* \brief Scalar subtraction
* \details Subtract an expression from a scalar
*
* \param x The scalar from which to subtract
* \param y The expression to subtract
*
* \return An expression where the ith element is x_i minus y
*/
Expression operator-(real x, const Expression& y);
/**
* \ingroup arithmeticoperations
* \brief Scalar subtraction
* \details Subtract a scalar from an expression
*
* \param x The expression from which to subtract
* \param y The scalar to subtract
*
* \return An expression where the ith element is x_i minus y
*/
Expression operator-(const Expression& x, real y);
/**
* \ingroup arithmeticoperations
* \brief Matrix multiplication
* \details Multiply two matrices together. Like standard matrix multiplication, the
* second dimension of x and the first dimension of y must match.
*
* \param x The left-hand matrix
* \param y The right-hand matrix
*
* \return An expression x times y
*/
Expression operator*(const Expression& x, const Expression& y);
/**
* \ingroup arithmeticoperations
* \brief Matrix-scalar multiplication
* \details Multiply an expression component-wise by a scalar.
*
* \param x The matrix
* \param y The scalar
*
* \return An expression where the ith element is x_i times y
*/
Expression operator*(const Expression& x, float y);
/**
* \ingroup arithmeticoperations
* \brief Matrix-scalar multiplication
* \details Multiply an expression component-wise by a scalar.
*
* \param x The scalar
* \param y The matrix
*
* \return An expression where the ith element is x_i times y
*/
inline Expression operator*(float y, const Expression& x) { return x * y; }
/**
* \ingroup arithmeticoperations
* \brief Matrix-scalar division
* \details Divide an expression component-wise by a scalar.
*
* \param x The matrix
* \param y The scalar
*
* \return An expression where the ith element is x_i divided by y
*/
inline Expression operator/(const Expression& x, float y) { return x * (1.f / y); }
/**
* \ingroup arithmeticoperations
* \brief Affine transform
* \details This performs an affine transform over an arbitrary (odd) number of expressions
* held in the input initializer list xs.
* The first expression is the "bias," which is added to the expression as-is.
* The remaining expressions are multiplied together in pairs, then added.
* A very common usage case is the calculation of the score for a neural network
* layer (e.g. b + Wz) where b is the bias, W is the weight matrix, and z is the
* input. In this case xs[0] = b, xs[1] = W, and xs[2] = z.
*
* \param xs An initializer list containing an odd number of expressions
*
* \return An expression equal to: xs[0] + xs[1]*xs[2] + xs[3]*xs[4] + ...
*/
inline Expression affine_transform(const std::initializer_list<Expression>& xs) { return detail::f<AffineTransform>(xs); }
template <typename T>
inline Expression affine_transform(const T& xs) { return detail::f<AffineTransform>(xs); }
/**
* \ingroup arithmeticoperations
* \brief Sum
* \details This performs an elementwise sum over all the expressions in xs
*
* \param xs An initializer list containing expressions
*
* \return An expression where the ith element is equal to xs[0][i] + xs[1][i] + ...
*/
inline Expression sum(const std::initializer_list<Expression>& xs) { return detail::f<Sum>(xs); }
template <typename T>
inline Expression sum(const T& xs) { return detail::f<Sum>(xs); }
/**
* \ingroup arithmeticoperations
* \brief Sum all elements
* \details Sum all the elements in an expression.
*
* \param x The input expression
*
* \return The sum of all of its elements
*/
Expression sum_elems(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Compute moment over all elements
* \details Compute the moment of order \f$r\f$, \f$\frac 1 n\sum_{i=1}^nx_i^r\f$ over all the elements in each batch of the expression
*
* \param x The input mini-batched expression
* \param r Order of the moment
*
* \return A scalar expression (with a potential batch dimension)
*/
Expression moment_elems(const Expression& x, unsigned r);
/**
* \ingroup arithmeticoperations
* \brief Compute mean over all elements
* \details Computes \f$\frac 1 n\sum_{i=1}^nx_i\f$ over all the elements in each batch of the expression
*
* \param x The input mini-batched expression
*
* \return A scalar expression (with a potential batch dimension)
*/
Expression mean_elems(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Compute Standard deviation over all elements
* \details Computes \f$\frac 1 n\sum_{i=1}^n(x_i -\mu)^2\f$ where \f$\mu=\frac 1 n\sum_{i=1}^nx_i\f$ over all the elements in each batch of the expression
*
* \param x The input mini-batched expression
*
* \return A scalar expression (with a potential batch dimension)
*/
Expression std_elems(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Average
* \details This performs an elementwise average over all the expressions in xs
*
* \param xs An initializer list containing expressions
*
* \return An expression where the ith element is equal to (xs[0][i] + xs[1][i] + ...)/|xs|
*/
inline Expression average(const std::initializer_list<Expression>& xs) { return detail::f<Average>(xs); }
template <typename T>
inline Expression average(const T& xs) { return detail::f<Average>(xs); }
/**
* \ingroup arithmeticoperations
* \brief Square root
* \details Elementwise square root.
*
* \param x The input expression
*
* \return An expression where the ith element is equal to \f$\sqrt(x_i)\f$
*/
Expression sqrt(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Absolute value
* \details Elementwise absolute value.
*
* \param x The input expression
*
* \return An expression where the ith element is equal to \f$\vert x_i\vert\f$
*/
Expression abs(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Gaussian error function
* \details Elementwise calculation of the Gaussian error function
*
* \param x The input expression
*
* \return An expression where the ith element is equal to erf(x_i)
*/
Expression erf(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Hyperbolic tangent
* \details Elementwise calculation of the hyperbolic tangent
*
* \param x The input expression
*
* \return An expression where the ith element is equal to tanh(x_i)
*/
Expression tanh(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Natural exponent
* \details Calculate elementwise y_i = e^{x_i}
*
* \param x The input expression
*
* \return An expression where the ith element is equal to e^{x_i}
*/
Expression exp(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Square
* \details Calculate elementwise y_i = x_i^2
*
* \param x The input expression
*
* \return An expression where the ith element is equal to x_i^2
*/
Expression square(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Cube
* \details Calculate elementwise y_i = x_i^3
*
* \param x The input expression
*
* \return An expression where the ith element is equal to x_i^3
*/
Expression cube(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Log gamma
* \details Calculate elementwise y_i = ln(gamma(x_i))
*
* \param x The input expression
*
* \return An expression where the ith element is equal to ln(gamma(x_i))
*/
Expression lgamma(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Logarithm
* \details Calculate the elementwise natural logarithm y_i = ln(x_i)
*
* \param x The input expression
*
* \return An expression where the ith element is equal to ln(x_i)
*/
Expression log(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Logistic sigmoid function
* \details Calculate elementwise y_i = 1/(1+e^{-x_i})
*
* \param x The input expression
*
* \return An expression where the ith element is equal to y_i = 1/(1+e^{-x_i})
*/
Expression logistic(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Rectifier
* \details Calculate elementwise the recitifer (ReLU) function y_i = max(x_i,0)
*
* \param x The input expression
*
* \return An expression where the ith element is equal to max(x_i,0)
*/
Expression rectify(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Exponential Linear Unit
* \details Calculate elementwise the function
*
* \f$
* y_i = \left\{\begin{array}{lr}
* x_i, & \text{if } x>0\\
* \alpha\times(e^{x_i} - 1), & \text{if }x\leqslant 0\\
* \end{array}\right.
* \f$
*
* Reference: [Clevert et al., 2015](https://arxiv.org/abs/1511.07289v5)
*
* \param x The input expression
*
* \return An expression where the ith element is equal to \f$\text{ELU}(x_i, \alpha)\f$
*/
Expression elu(const Expression& x, float alpha=1.f);
/**
* \ingroup arithmeticoperations
* \brief Scaled Exponential Linear Unit (SELU)
* \details Calculate elementwise the function
*
* \f$
* y_i = \lambda\times\left\{\begin{array}{lr}
* x_i, & \text{if } x>0\\
* \alpha\times(e^{x_i} - 1), & \text{if }x\leqslant 0\\
* \end{array}\right.
* \f$
*
* With
* \f$
* \begin{split}
* \lambda &=\texttt{1.0507009873554804934193349852946}\\
* \alpha &=\texttt{1.6732632423543772848170429916717}\\
* \end{split}
* \f$
*
* Reference: [Klambaouer et al., 2017](https://arxiv.org/abs/1706.02515)
*
* \param x The input expression
*
* \return An expression where the ith element is equal to \f$\text{SELU}(x_i)\f$
*/
Expression selu(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Soft Sign
* \details Calculate elementwise the softsign function y_i = x_i/(1+|x_i|)
*
* \param x The input expression
*
* \return An expression where the ith element is equal to x_i/(1+|x_i|)
*/
Expression softsign(const Expression& x);
/**
* \ingroup arithmeticoperations
* \brief Power function
* \details Calculate an output where the ith element is equal to x_i^y_i
*
* \param x The input expression
* \param y The exponent expression
*
* \return An expression where the ith element is equal to x_i^y_i
*/
Expression pow(const Expression& x, const Expression& y);
/**
* \ingroup arithmeticoperations
* \brief Minimum
* \details Calculate an output where the ith element is min(x_i,y_i)
*
* \param x The first input expression
* \param y The second input expression
*
* \return An expression where the ith element is equal to min(x_i,y_i)
*/
Expression min(const Expression& x, const Expression& y);
/**
* \ingroup arithmeticoperations
* \brief Maximum
* \details Calculate an output where the ith element is max(x_i,y_i)
*
* \param x The first input expression
* \param y The second input expression
*
* \return An expression where the ith element is equal to max(x_i,y_i)
*/
Expression max(const Expression& x, const Expression& y);
/**
* \ingroup arithmeticoperations
* \brief Max
* \details This performs an elementwise max over all the expressions in xs
*
* \param xs An initializer list containing expressions
*
* \return An expression where the ith element is equal to max(xs[0][i], xs[1][i], ...)
*/
inline Expression max(const std::initializer_list<Expression>& xs) { return detail::f<Max>(xs); }
template <typename T>
inline Expression max(const T& xs) { return detail::f<Max>(xs); }
/**
* \ingroup arithmeticoperations
* \brief Dot Product
* \details Calculate the dot product sum_i x_i*y_i
*
* \param x The input expression
* \param y The input expression
*
* \return An expression equal to the dot product
*/
Expression dot_product(const Expression& x, const Expression& y);
/**
* \ingroup arithmeticoperations
* \brief Componentwise multiply
* \details Do a componentwise multiply where each value is equal to x_i*y_i.
* This function used to be called cwise_multiply.
*
* \param x The first input expression
* \param y The second input expression
*
* \return An expression where the ith element is equal to x_i*y_i
*/
Expression cmult(const Expression& x, const Expression& y);
/**
* \ingroup arithmeticoperations
* \brief Componentwise multiply
* \details Do a componentwise multiply where each value is equal to x_i/y_i
*
* \param x The first input expression
* \param y The second input expression
*
* \return An expression where the ith element is equal to x_i/y_i
*/
Expression cdiv(const Expression& x, const Expression& y);
/**
* \ingroup arithmeticoperations
* \brief Columnwise addition
* \details Add vector "bias" to each column of matrix "x"
*
* \param x An MxN matrix
* \param bias A length M vector
*
* \return An expression where bias is added to each column of x
*/
Expression colwise_add(const Expression& x, const Expression& bias);
////////////////////////////////////////////////
// Probability/loss operations //
////////////////////////////////////////////////
/**
* \ingroup lossoperations
* \brief Softmax
* \details The softmax function normalizes each column to ensure that all
* values are between 0 and 1 and add to one by applying the
* e^{x[i]}/{sum_j e^{x[j]}}.
*
* \param x A vector or matrix
*
* \return A vector or matrix after calculating the softmax
*/
Expression softmax(const Expression& x);
/**
* \ingroup lossoperations
* \brief Log softmax
* \details The log softmax function normalizes each column to ensure that all
* values are between 0 and 1 and add to one by applying the
* e^{x[i]}/{sum_j e^{x[j]}}, then takes the log
*
* \param x A vector or matrix
*
* \return A vector or matrix after calculating the log softmax
*/
Expression log_softmax(const Expression& x);
/**
* \ingroup lossoperations
* \brief Restricted log softmax
* \details The log softmax function calculated over only a subset of the vector elements. The
* elements to be included are set by the ``restriction`` variable. All elements not
* included in ``restriction`` are set to negative infinity.
*
* \param x A vector over which to calculate the softmax
* \param restriction The elements over which to calculate the softmax
*
* \return A vector with the log softmax over the specified elements
*/
Expression log_softmax(const Expression& x, const std::vector<unsigned>& restriction);
/**
* \ingroup lossoperations
* \brief Log, sum, exp
* \details The elementwise "logsumexp" function that calculates
* \f$ln(\sum_i e^{xs_i})\f$, used in adding probabilities in the log domain.
*
* \param xs Expressions with respect to which to calculate the logsumexp.
*
* \return The result.
*/
inline Expression logsumexp(const std::initializer_list<Expression>& xs) { return detail::f<LogSumExp>(xs); }
template <typename T>
inline Expression logsumexp(const T& xs) { return detail::f<LogSumExp>(xs); }