@@ -22,69 +22,69 @@ Getting started
22
22
23
23
.. code-block :: python
24
24
25
- import pytensor
26
- from pytensor import tensor as pt
27
-
28
- # Declare two symbolic floating-point scalars
29
- a = pt.dscalar(" a" )
30
- b = pt.dscalar(" b" )
31
-
32
- # Create a simple example expression
33
- c = a + b
34
-
35
- # Convert the expression into a callable object that takes `(a, b)`
36
- # values as input and computes the value of `c`.
37
- f_c = pytensor.function([a, b], c)
38
-
39
- assert f_c(1.5 , 2.5 ) == 4.0
40
-
41
- # Compute the gradient of the example expression with respect to `a`
42
- dc = pytensor.grad(c, a)
43
-
44
- f_dc = pytensor.function([a, b], dc)
45
-
46
- assert f_dc(1.5 , 2.5 ) == 1.0
47
-
48
- # Compiling functions with `pytensor.function` also optimizes
49
- # expression graphs by removing unnecessary operations and
50
- # replacing computations with more efficient ones.
51
-
52
- v = pt.vector(" v" )
53
- M = pt.matrix(" M" )
54
-
55
- d = a/ a + (M + a).dot(v)
56
-
57
- pytensor.dprint(d)
58
- # Elemwise{add,no_inplace} [id A] ''
59
- # |InplaceDimShuffle{x } [id B] ''
60
- # | |Elemwise{true_div,no_inplace} [id C] ''
61
- # | | a [id D]
62
- # | | a [id D]
63
- # | dot [id E] ''
64
- # |Elemwise{add,no_inplace} [id F] ''
65
- # | | M [id G]
66
- # | |InplaceDimShuffle{x,x } [id H] ''
67
- # | | a [id D]
68
- # | v [id I]
69
-
70
- f_d = pytensor.function([a, v, M], d)
71
-
72
- # `a/a` -> `1` and the dot product is replaced with a BLAS function
73
- # (i.e. CGemv)
74
- pytensor.dprint(f_d)
75
- # Elemwise{ Add}[(0, 1)] [id A] '' 5
76
- # |TensorConstant{(1,) of 1.0} [id B]
77
- # | CGemv{inplace} [id C] '' 4
78
- # | AllocEmpty{dtype='float64'} [id D] '' 3
79
- # | | Shape_i{0} [id E] '' 2
80
- # | | M [id F]
81
- # |TensorConstant{ 1.0} [id G]
82
- # |Elemwise{add,no_inplace} [id H] '' 1
83
- # | | M [id F]
84
- # | |InplaceDimShuffle{x,x } [id I] '' 0
85
- # | | a [id J]
86
- # | v [id K]
87
- # |TensorConstant{ 0.0} [id L]
25
+ import pytensor
26
+ from pytensor import tensor as pt
27
+
28
+ # Declare two symbolic floating-point scalars
29
+ a = pt.dscalar(" a" )
30
+ b = pt.dscalar(" b" )
31
+
32
+ # Create a simple example expression
33
+ c = a + b
34
+
35
+ # Convert the expression into a callable object that takes `(a, b)`
36
+ # values as input and computes the value of `c`.
37
+ f_c = pytensor.function([a, b], c)
38
+
39
+ assert f_c(1.5 , 2.5 ) == 4.0
40
+
41
+ # Compute the gradient of the example expression with respect to `a`
42
+ dc = pytensor.grad(c, a)
43
+
44
+ f_dc = pytensor.function([a, b], dc)
45
+
46
+ assert f_dc(1.5 , 2.5 ) == 1.0
47
+
48
+ # Compiling functions with `pytensor.function` also optimizes
49
+ # expression graphs by removing unnecessary operations and
50
+ # replacing computations with more efficient ones.
51
+
52
+ v = pt.vector(" v" )
53
+ M = pt.matrix(" M" )
54
+
55
+ d = a/ a + (M + a).dot(v)
56
+
57
+ pytensor.dprint(d)
58
+ # Add [id A]
59
+ # ├─ ExpandDims{axis=0 } [id B]
60
+ # │ └─ True_div [id C]
61
+ # │ ├─ a [id D]
62
+ # │ └─ a [id D]
63
+ # └─ dot [id E]
64
+ # ├─ Add [id F]
65
+ # │ ├─ M [id G]
66
+ # │ └─ ExpandDims{axes=[0, 1] } [id H]
67
+ # │ └─ a [id D]
68
+ # └─ v [id I]
69
+
70
+ f_d = pytensor.function([a, v, M], d)
71
+
72
+ # `a/a` -> `1` and the dot product is replaced with a BLAS function
73
+ # (i.e. CGemv)
74
+ pytensor.dprint(f_d)
75
+ # Add [id A] 5
76
+ # ├─ [1.] [id B]
77
+ # └─ CGemv{inplace} [id C] 4
78
+ # ├─ AllocEmpty{dtype='float64'} [id D] 3
79
+ # │ └─ Shape_i{0} [id E] 2
80
+ # │ └─ M [id F]
81
+ # ├─ 1.0 [id G]
82
+ # ├─ Add [id H] 1
83
+ # │ ├─ M [id F]
84
+ # │ └─ ExpandDims{axes=[0, 1] } [id I] 0
85
+ # │ └─ a [id J]
86
+ # ├─ v [id K]
87
+ # └─ 0.0 [id L]
88
88
89
89
See `the PyTensor documentation <https://pytensor.readthedocs.io/en/latest/ >`__ for in-depth tutorials.
90
90
0 commit comments