### Install PyTensor from PyPI Source: https://github.com/pymc-devs/pytensor/blob/main/doc/install.rst Install the latest release of PyTensor using pip. ```bash pip install pytensor ``` -------------------------------- ### Link against MKL library Source: https://github.com/pymc-devs/pytensor/blob/main/doc/troubleshooting.rst Example flags for linking against Intel's MKL library. The exact flags may vary based on your MKL installation and system. ```bash blas__ldflags="-lmkl -lguide -lpthread" ``` ```bash blas__ldflags="-lmkl_intel_lp64 -lmkl_intel_thread -lmkl_core -lguide -liomp5 -lmkl_mc -lpthread" ``` -------------------------------- ### Install Pre-commit Hooks Source: https://github.com/pymc-devs/pytensor/blob/main/doc/dev_start_guide.rst Command to install pre-commit, which ensures linting and code quality checks are performed before each commit. ```bash pre-commit install ``` -------------------------------- ### Run PyTensor Test Suite with Pytest Source: https://github.com/pymc-devs/pytensor/blob/main/doc/troubleshooting.rst Install pytest and run the PyTensor test suite to confirm a correct installation. Skipped tests and known failures are expected. ```bash pip install pytest PYTENSOR_FLAGS='' pytest tests/ ``` -------------------------------- ### Install PyTensor from Development Version Source: https://github.com/pymc-devs/pytensor/blob/main/doc/index.rst Clone the PyTensor repository and install the development version. This allows for automatic reflection of Git updates. ```bash git clone git://github.com/pymc-devs/pytensor.git cd pytensor python setup.py develop ``` -------------------------------- ### Command Line Execution Example Source: https://github.com/pymc-devs/pytensor/blob/main/doc/tutorial/conditions.rst Shows the expected output when running the Python script that compares IfElse and Switch performance. ```none $ python ifelse_switch.py time spent evaluating both values 0.6700 sec time spent evaluating one value 0.3500 sec ``` -------------------------------- ### Example Text Generation Output Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/applications/tiny_transformer_llm.ipynb Demonstrates the output of the `generate` function, showing text generated by the tiny transformer model starting with a given prompt. The generation stops at the first newline or after the specified number of new tokens. ```text ROMEO: He have shut thing disp: eat fitorer, which bee spose! ``` -------------------------------- ### Serve Documentation Locally Source: https://github.com/pymc-devs/pytensor/blob/main/doc/dev_start_guide.rst Command to start a simple HTTP server in the 'html' directory to view the built documentation in a browser. ```bash python -m http.server ``` -------------------------------- ### View Mapping Examples Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/inplace.rst Demonstrates various ways to map outputs to input views. Note that an output can only view a single input. ```python myop.view_map = {0: [0]} # first output is a view of first input myop.view_map = {0: [1]} # first output is a view of second input myop.view_map = {1: [0]} # second output is a view of first input myop.view_map = {0: [0], # first output is a view of first input 1: [1]} # *AND* second output is a view of second input myop.view_map = {0: [0], # first output is a view of first input 1: [0]} # *AND* second output is *ALSO* a view of first input myop.view_map = {0: [0, 1]} # THIS IS NOT SUPPORTED YET! Only put a single input number in the list! ``` -------------------------------- ### Install PyTensor from Source Source: https://github.com/pymc-devs/pytensor/blob/main/doc/_drafts/benchmark_mlx_v_jax_corrected.ipynb Installs the PyTensor package in editable mode from a local directory. This command is useful for development and testing. ```bash %pip install -e ../.. --no-deps ``` -------------------------------- ### Install PyTensor Development Branch from GitHub Source: https://github.com/pymc-devs/pytensor/blob/main/doc/install.rst Install the current development branch of PyTensor directly from GitHub using pip. ```bash pip install git+https://github.com/pymc-devs/pytensor ``` -------------------------------- ### JAX CumOp Test Example Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/creating_a_numba_jax_op.rst Example test for the JAX conversion of the `CumOp` `Op`. It uses `compare_jax_and_py` to verify the implementation against `Op.perform`. ```python import numpy as np import pytensor.tensor as pt from pytensor.configdefaults import config from tests.link.jax.test_basic import compare_jax_and_py from pytensor.graph import FunctionGraph def test_jax_CumOp(): """Test JAX conversion of the `CumOp` `Op`. """ # Create a symbolic input for the first input of `CumOp` a = pt.matrix("a") test_value = np.arange(9, dtype=config.floatX).reshape((3, 3)) ``` -------------------------------- ### Example NumPy Array Initialization Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/creating_a_c_op.rst Illustrates the structure of a 2D NumPy array for explaining C-contiguous and F-contiguous memory layouts. ```python x = [[1, 2, 3], [4, 5, 6]] ``` -------------------------------- ### Execute Text Generation Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/applications/tiny_transformer_llm.ipynb Example of how to call the `generate_full` function with a specific prompt and number of tokens, and print the resulting generated text. ```python print(generate_full("ROMEO:\n", n_new_tokens=400)) ``` -------------------------------- ### Test PyTensor Installation with IPython Source: https://github.com/pymc-devs/pytensor/blob/main/doc/troubleshooting.rst Verify your PyTensor installation by checking its file path and version within an IPython session. ```python import pytensor pytensor.__file__ pytensor.__version__ ``` -------------------------------- ### Frontend Visualization Setup Source: https://github.com/pymc-devs/pytensor/blob/main/doc/library/d3viz/examples/mlp2.html Initializes D3.js variables and settings for graph visualization, including layout, color schemes, and interaction behaviors. ```javascript var graph = {}; var forceLayout; var isProfiled = false; // true if profiling information available var useProfileColors = false; var fixOnInit = true; // fix nodes on initialization var maxProfilePer = 0; var profileColors = ["#fff5f0","#fee0d2","#fcbba1","#fc9272","#fb6a4a","#ef3b2c","#cb181d","#a50f15"]; var pad = 10; var isEditNode = false; // true if node is edited var menuItems = [ { title: 'Edit', action: function(elm, d, i) { editNode(elm, d); } }, { title: 'Release', action: function(elm, d, i) { releaseNode(d); } } ]; ``` -------------------------------- ### Setup Plotting Style Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/introduction/pytensor_intro.ipynb Sets up matplotlib for plotting with a seaborn style and retina display format. ```python import matplotlib.pyplot as plt plt.style.use("seaborn-v0_8") %config InlineBackend.figure_format = "retina" ``` -------------------------------- ### Logistic Regression Example with PyTensor Source: https://github.com/pymc-devs/pytensor/blob/main/doc/tutorial/modes.rst This example demonstrates building and training a logistic regression model using PyTensor. It includes defining symbolic variables, constructing the expression graph, and compiling functions for training and prediction. It also checks if the CPU was used during compilation. ```python import numpy as np import pytensor import pytensor.tensor as pt rng = np.random.default_rng(2498) N = 400 feats = 784 D = (rng.standard_normal((N, feats)).astype(pytensor.config.floatX), rng.integers(size=N,low=0, high=2).astype(pytensor.config.floatX)) training_steps = 10000 # Declare PyTensor symbolic variables x = pt.matrix("x") y = pt.vector("y") w = pytensor.shared(rng.standard_normal(feats).astype(pytensor.config.floatX), name="w") b = pytensor.shared(np.asarray(0., dtype=pytensor.config.floatX), name="b") # Construct PyTensor expression graph p_1 = 1 / (1 + pt.exp(-pt.dot(x, w)-b)) # Probability of having a one prediction = p_1 > 0.5 # The prediction that is done: 0 or 1 xent = -y*pt.log(p_1) - (1-y)*pt.log(1-p_1) # Cross-entropy cost = xent.mean() + 0.01*(w**2).sum() # The cost to optimize gw,gb = pt.grad(cost, [w,b]) # Compile expressions to functions train = pytensor.function( inputs=[x,y], outputs=[prediction, xent], updates=[(w, w-0.01*gw), (b, b-0.01*gb)], name = "train" ) predict = pytensor.function( inputs=[x], outputs=prediction, name = "predict" ) if any(x.op.__class__.__name__ in ['Gemv', 'CGemv', 'Gemm', 'CGemm'] for x in train.maker.fgraph.toposort()): print('Used the cpu') else: print('ERROR, not able to tell if pytensor used the cpu or another device') print(train.maker.fgraph.toposort()) for i in range(training_steps): pred, err = train(D[0], D[1]) print("target values for D") print(D[1]) print("prediction on D") print(predict(D[0])) ``` -------------------------------- ### Scan Example: Computing a Sequence with PyTensor Source: https://github.com/pymc-devs/pytensor/blob/main/doc/tutorial/loop.rst This example demonstrates using `pytensor.scan` to compute a sequence where each element depends on the previous one and other inputs. It defines a recursive function and compares the PyTensor output with a manual NumPy implementation. ```python import pytensor import pytensor.tensor as pt import numpy as np # define tensor variables X = pt.vector("X") W = pt.matrix("W") b_sym = pt.vector("b_sym") U = pt.matrix("U") Y = pt.matrix("Y") V = pt.matrix("V") P = pt.matrix("P") results, updates = pytensor.scan(lambda y, p, x_tm1: pt.tanh(pt.dot(x_tm1, W) + pt.dot(y, U) + pt.dot(p, V)), sequences=[Y, P[::-1]], outputs_info=[X]) compute_seq = pytensor.function(inputs=[X, W, Y, U, P, V], outputs=results) # test values x = np.zeros((2), dtype=pytensor.config.floatX) x[1] = 1 w = np.ones((2, 2), dtype=pytensor.config.floatX) y = np.ones((5, 2), dtype=pytensor.config.floatX) y[0, :] = -3 u = np.ones((2, 2), dtype=pytensor.config.floatX) p = np.ones((5, 2), dtype=pytensor.config.floatX) p[0, :] = 3 v = np.ones((2, 2), dtype=pytensor.config.floatX) print(compute_seq(x, w, y, u, p, v)) # comparison with numpy x_res = np.zeros((5, 2), dtype=pytensor.config.floatX) x_res[0] = np.tanh(x.dot(w) + y[0].dot(u) + p[4].dot(v)) for i in range(1, 5): x_res[i] = np.tanh(x_res[i - 1].dot(w) + y[i].dot(u) + p[4-i].dot(v)) print(x_res) ``` -------------------------------- ### Example of PyTensor Variable Usage Source: https://github.com/pymc-devs/pytensor/blob/main/doc/glossary.rst Demonstrates the creation and manipulation of PyTensor Variables, which are the primary data structures for symbolic computation. ```python >>> x = pt.ivector() >>> y = -x**2 ``` -------------------------------- ### Example of MergeOptimizer for Identical Computations Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/graph_rewriting.rst Illustrates a scenario where a direct rewrite fails because identical subexpressions are not recognized as such. It shows how `MergeOptimizer` can be used to consolidate these, enabling subsequent simplification. ```python >>> x = float64('x') >>> y = float64('y') >>> z = float64('z') >>> a = true_div(mul(add(y, z), x), add(y, z)) >>> e = pytensor.graph.fg.FunctionGraph([x, y, z], [a]) >>> e FunctionGraph(true_div(mul(add(y, z), x), add(y, z))) >>> simplify.rewrite(e) >>> e FunctionGraph(true_div(mul(add(y, z), x), add(y, z))) ``` -------------------------------- ### DebugMode Usage Example Source: https://github.com/pymc-devs/pytensor/blob/main/doc/library/compile/debugmode.rst Demonstrates how to use DebugMode when defining a PyTensor function. ```APIDOC ## DebugMode Usage Example ### Description This example shows how to use `DebugMode` when compiling a PyTensor function. It illustrates setting the mode directly during function definition. ### Method `pytensor.function` ### Endpoint N/A (SDK usage) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```python import pytensor from pytensor import tensor as pt from pytensor.compile.debug.debugmode import DebugMode x = pt.dscalar('x') # Using DebugMode as a string f = pytensor.function([x], 10*x, mode='DebugMode') # Using DebugMode as an instance with configuration f_configured = pytensor.function([x], 10*x, mode=DebugMode(check_c_code=False)) f(5) f(0) f(7) ``` ### Response #### Success Response (200) N/A (SDK usage, returns function object) #### Response Example N/A ``` -------------------------------- ### Example CumsumOp make_node Implementation Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/creating_a_numba_jax_op.rst This `make_node` implementation for `CumsumOp` defines the input and output types, handling cases where the axis is not specified or is out of bounds. ```python def make_node(self, x): x = ptb.as_tensor_variable(x) out_type = x.type() if self.axis is None: out_type = vector(dtype=x.dtype) # Flatten elif self.axis >= x.ndim or self.axis < -x.ndim: raise ValueError(f"axis(={self.axis}) out of bounds") return Apply(self, [x], [out_type]) ``` -------------------------------- ### Basic Variable and Computation Example Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/graphstructures.rst Illustrates the creation of symbolic variables and a simple computation (negation) in PyTensor. Shows how variables represent inputs and outputs of operations. ```python import pytensor x = pytensor.tensor.ivector() y = -x ``` -------------------------------- ### Example of Simplification Rewrite Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/graph_rewriting.rst Demonstrates how to rewrite a graph to simplify expressions involving division and multiplication. It shows an initial complex expression and the simplified result after applying a rewrite. ```python >>> from pytensor.scalar import float64, add, mul, true_div >>> x = float64('x') >>> y = float64('y') >>> z = float64('z') >>> a = add(z, mul(true_div(mul(y, x), y), true_div(z, x))) >>> e = pytensor.graph.fg.FunctionGraph([x, y, z], [a]) >>> e FunctionGraph(add(z, mul(true_div(mul(y, x), y), true_div(z, x)))) >>> simplify.rewrite(e) >>> e FunctionGraph(add(z, mul(x, true_div(z, x)))) ``` -------------------------------- ### Generate Static Graph Visualization with pydotprint Source: https://github.com/pymc-devs/pytensor/blob/main/doc/library/d3viz/index.ipynb Create a static PNG image of the PyTensor compute graph using pydotprint. Ensure the 'examples' directory exists before saving the file. ```python from pytensor.printing import pydotprint import os if not os.path.exists('examples'): os.makedirs('examples') pydotprint(predict, 'examples/mlp.png') ``` -------------------------------- ### Re-tokenize and Rebuild Model Configuration Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/applications/tiny_transformer_llm.ipynb Prepares a larger text slice, defines vocabulary, and configures a deeper transformer model. This setup is independent of previous configurations. ```python text_full = full_text[:100_000] chars_full = sorted(set(text_full)) vocab_size_full = len(chars_full) stoi_full = {c: i for i, c in enumerate(chars_full)} itos_full = {i: c for i, c in enumerate(chars_full)} data_full = np.array([stoi_full[c] for c in text_full], dtype="int64") cfg_full = Config( vocab_size=vocab_size_full, block_size=64, n_embd=64, n_head=4, n_layer=4, ) print(f"corpus slice: {len(data_full):,} chars, vocab: {vocab_size_full}") print(f"cfg_full : {cfg_full}") init_arrays_full = init_params(cfg_full, np.random.default_rng(7)) print(f"params : {sum(a.size for a in init_arrays_full.values()):,}") ``` -------------------------------- ### Define a Custom PyTensor Op with Docstring Source: https://github.com/pymc-devs/pytensor/blob/main/doc/dev_start_guide.rst Example of how to define a custom PyTensor Op, including a detailed docstring with notes and see also references. ```python from pytensor.graph.basic import Variable from pytensor.graph.op import Op class DoubleOp(Op): """Double each element of a tensor. Notes ----- this is a test note See Also -------- `Elemwise`: This functionality is already available; just execute ``x * 2`` with ``x`` being an PyTensor variable. """ def make_node(self, x: Variable): """Construct an `Apply` node for this `Op`. Parameters ---------- x Input tensor """ ... ``` -------------------------------- ### Shared Constructor Example Source: https://github.com/pymc-devs/pytensor/blob/main/doc/library/compile/shared.rst Demonstrates how a registered shared constructor is called with value, name, strict, and other keyword arguments. The constructor must raise a TypeError if it does not support the given value. ```python ctor(value, name=name, strict=strict, **kwargs) ``` -------------------------------- ### Install PyTensor via conda-forge Source: https://github.com/pymc-devs/pytensor/blob/main/doc/install.rst Install PyTensor using the conda-forge channel. ```bash conda install -c conda-forge pytensor ``` -------------------------------- ### Prepare Notebook Environment Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/applications/tiny_transformer_llm.ipynb Imports necessary libraries and sets up the PyTensor environment, including configuration for float type and random number generation. ```python from __future__ import annotations import time import urllib.request from dataclasses import dataclass from pathlib import Path import matplotlib.pyplot as plt import numpy as np import pytensor import pytensor.tensor as pt import pytensor.xtensor as ptx plt.style.use("seaborn-v0_8") %config InlineBackend.figure_format = "retina" rng_np = np.random.default_rng(0) floatX = pytensor.config.floatX print("pytensor:", pytensor.__version__, "| floatX:", floatX) ``` -------------------------------- ### Create Matrix Variables and Compare Source: https://github.com/pymc-devs/pytensor/blob/main/doc/library/tensor/basic.rst Demonstrates how to create symbolic matrix variables and perform a less-than-or-equal-to comparison between them. ```python import pytensor.tensor as pt x,y = pt.dmatrices('x','y') z = pt.le(x,y) ``` -------------------------------- ### Install pydot for d3viz Source: https://github.com/pymc-devs/pytensor/blob/main/doc/library/d3viz/index.ipynb Install the pydot package using pip, which is a requirement for d3viz. ```python !pip install pydot ``` -------------------------------- ### Build HTML Documentation Source: https://github.com/pymc-devs/pytensor/blob/main/doc/internal/metadocumentation.rst Navigate to the PyTensor directory and execute the docgen.py script to generate HTML documentation. The output will be placed in the 'html' directory. ```bash cd PyTensor/ python ./doc/scripts/docgen.py ``` -------------------------------- ### Create and Activate Documentation Environment Source: https://github.com/pymc-devs/pytensor/blob/main/doc/dev_start_guide.rst Commands to create a separate conda environment for building documentation using 'doc/environment.yml' and activate it. ```bash conda env create -f doc/environment.yml conda activate pytensor-docs ``` -------------------------------- ### Executing RootOp with Initial Guess [1., 1.] Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/optimize/root.ipynb This snippet demonstrates the execution of the defined root-finding function with an initial guess of [1., 1.]. The output includes the computed root and a boolean indicating success. ```python fn([1., 1.]) ``` -------------------------------- ### Main Benchmark Execution Function Source: https://github.com/pymc-devs/pytensor/blob/main/doc/_drafts/benchmark_mlx_v_jax_corrected.ipynb Sets up and runs the PyTensor benchmarks, collecting system information and benchmark results. Requires PyTensor, JAX, and optionally MLX. ```python def main(N=1000): """Main benchmark execution""" # Display system info system_info = { 'JAX version': jax.__version__, 'PyTensor version': pytensor.__version__, 'MLX Available': 'Yes' if MLX_AVAILABLE else 'No', 'Platform': 'Apple Silicon' if MLX_AVAILABLE else 'Generic', 'Repetitions (N)': N } if MLX_AVAILABLE: system_info['MLX version'] = mx.__version__ import pandas as pd info_df = pd.DataFrame([system_info]) # Then run benchmarks results_df = run_benchmark(N=N) return info_df, results_df ``` -------------------------------- ### PyTensor Scan with Strict Flag Enabled (Error Example) Source: https://github.com/pymc-devs/pytensor/blob/main/doc/library/scan.rst Illustrates the use of the strict=True flag in the scan function, which enforces explicit passing of all shared variables used within the function. This example shows the expected MissingInputError when shared variables are not passed. ```python # Same OneStep as in original example, but without explicit non_sequences. def OneStep(rng, vsample) : hmean = pt.sigmoid(pytensor.dot(vsample, W) + bhid) next_rng, hsample = rng.binomial(n=1, p=hmean, size=hmean.shape) vmean = pt.sigmoid(pytensor.dot(hsample, W.T) + bvis) next_rng, vsample = next_rng.binomial(n=1, p=vmean, size=vsample.shape) return next_rng, vsample # The new scan, adding strict=True to the original call. final_rng, values, = pytensor.scan( OneStep, outputs_info=[rng, sample], n_steps=10, strict=True, return_updates=False, ) ``` -------------------------------- ### Getting Number of Dimensions Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/creating_a_c_op.rst Returns the number of dimensions of a NumPy array. ```c int PyArray_NDIM(PyArrayObject* arr) ``` -------------------------------- ### Executing RootOp with Initial Guess [0., 0.] Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/optimize/root.ipynb This snippet demonstrates the execution of the defined root-finding function with an initial guess of [0., 0.]. The output includes the computed root and a boolean indicating success. ```python fn([0., 0.]) ``` -------------------------------- ### floor Source: https://github.com/pymc-devs/pytensor/blob/main/doc/library/tensor/basic.rst Returns a variable representing the floor of `a` (for example floor(2.9) is 2). ```APIDOC ## floor(a) ### Description Returns a variable representing the floor of a (for example floor(2.9) is 2). ### Parameters * **a**: symbolic tensor ``` -------------------------------- ### Executing RootOp with Initial Guess [1, -1] Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/optimize/root.ipynb This snippet demonstrates the execution of the defined root-finding function with an initial guess of [1, -1]. The output includes the computed root and a boolean indicating success. ```python fn([1, -1]) ``` -------------------------------- ### ceil Source: https://github.com/pymc-devs/pytensor/blob/main/doc/library/tensor/basic.rst Returns a variable representing the ceiling of `a` (for example ceil(2.1) is 3). ```APIDOC ## ceil(a) ### Description Returns a variable representing the ceiling of a (for example ceil(2.1) is 3). ### Parameters * **a**: symbolic tensor ``` -------------------------------- ### Getting Total Number of Elements Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/creating_a_c_op.rst Returns the total count of elements within the NumPy array. ```c npy_intp PyArray_SIZE(PyArrayObject* arr) ``` -------------------------------- ### Getting Array Typenumber Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/creating_a_c_op.rst Returns the typenumber for the array's elements, which describes the data type. ```c int PyArray_TYPE(PyArrayObject* arr) ``` -------------------------------- ### Testing Op Gradient with Multiple Outputs Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/creating_an_op.rst Demonstrates how to test the gradient of an Op with multiple outputs using `verify_grad`. It shows scenarios where all outputs are connected and where only specific outputs are used. ```python transpose_and_sum_op = TransposeAndSumOp() def both_outs_connected(x, y): out1, out2 = transpose_and_sum_op(x, y) return out1.sum() + out2.sum() def only_out1_connected(x, y): out1, _ = transpose_and_sum_op(x, y) return out1.sum() def only_out2_connected(x, y): _, out2 = transpose_and_sum_op(x, y) return out2.sum() rng = np.random.default_rng(seed=37) x_np = rng.random((5, 4, 3)).astype(np.float32) y_np = rng.random((2, 1)).astype(np.float32) verify_grad(both_outs_connected, [x_np, y_np], rng=rng) # PyTensor will raise a warning about the disconnected gradient with warnings.catch_warnings(): warnings.simplefilter("ignore") verify_grad(only_out1_connected, [x_np, y_np], rng=rng) verify_grad(only_out2_connected, [x_np, y_np], rng=rng) ``` -------------------------------- ### Executing RootOp with Initial Guess [-1, 1] Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/optimize/root.ipynb This snippet demonstrates the execution of the defined root-finding function with an initial guess of [-1, 1]. The output includes the computed root and a boolean indicating success. ```python fn([-1, 1]) ``` -------------------------------- ### Data Preparation for Training Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/introduction/pytensor_intro.ipynb Prepare data using scikit-learn's make_classification and initialize model parameters and hyperparameters. Ensure numpy and scikit-learn are imported. ```python import numpy as np from sklearn.datasets import make_classification seed = sum(map(ord, "I <3 Pytensor")) rng = np.random.default_rng(seed) X, y = make_classification( n_samples=100, n_features=5, n_redundant=0, n_informative=5, n_classes=2, random_state=seed, ) # Initial values for parameteres beta = rng.normal(0, 1, size=(5,)) alpha = rng.normal(0, 1) learning_rate = 1e-1 ``` -------------------------------- ### Get all supported sparse matrix dtypes Source: https://github.com/pymc-devs/pytensor/blob/main/doc/tutorial/sparse.rst Retrieves a set of all data types supported for sparse matrices in PyTensor. ```python sparse.all_dtypes ``` -------------------------------- ### Build PyTensor Documentation Source: https://github.com/pymc-devs/pytensor/blob/main/doc/dev_start_guide.rst Command to build the HTML version of the PyTensor documentation using Sphinx. ```bash python -m sphinx -b html ./doc ./html ``` -------------------------------- ### Test Pushforward Method of an Op Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/creating_an_op.rst Use `PushforwardPullbackChecker` to test the `pushforward` method of an Op. This example demonstrates testing the `DoubleOp`. ```python import numpy import tests from tests.test_rop import PushforwardPullbackChecker class TestDoubleOpPushforward(PushforwardPullbackChecker): def test_double_pushforward(self): self.check_pushforward_pullback(DoubleOp()(self.x), self.in_shape) ``` -------------------------------- ### Initialize and Draw Graph Source: https://github.com/pymc-devs/pytensor/blob/main/doc/library/d3viz/examples/mlp2.html Initializes the graph using a dot graph representation and then draws the frontend graph. Assumes `processDotGraph`, `frontEndGraph`, and `drawGraph` are defined elsewhere. ```javascript // Initialize graph processDotGraph(dotGraph); graph = frontEndGraph(dotGraph); drawGraph(); ``` -------------------------------- ### Inplace Operation Example Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/inplace.rst Illustrates how inplace operations are handled in PyTensor, emphasizing the functional nature and the distinction between variables before and after modification. ```python from pytensor.tensor import dscalars, log from pytensor.tensor.inplace import add_inplace x, y = dscalars('x', 'y') r1 = log(x) # r2 is x AFTER the add_inplace - x still represents the value before adding y r2 = add_inplace(x, y) # r3 is log(x) using the x from BEFORE the add_inplace # r3 is the SAME as r1, even if we wrote this line after the add_inplace line # PyTensor is actually going to compute r3 BEFORE r2 r3 = log(x) # this is log(x) using the x from AFTER the add_inplace (so it's like log(x + y)) r4 = log(r2) ``` -------------------------------- ### Get Tensor Shape Source: https://github.com/pymc-devs/pytensor/blob/main/doc/library/tensor/basic.rst Obtain the shape of a tensor as an lvector. This function is equivalent to NumPy's shape attribute for tensors. ```python >>> shape(x) array([2, 3]) ``` -------------------------------- ### Using DebugMode with a Class Instance Source: https://github.com/pymc-devs/pytensor/blob/main/doc/library/compile/debugmode.rst Shows how to use DebugMode by creating an instance of the DebugMode class, allowing for specific configuration options to be set. This example disables C code checking. ```python f = pytensor.function([x], 10*x, mode=DebugMode(check_c_code=False)) ``` -------------------------------- ### Setup Pytensor Variables for Scan Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/scan/scan_tutorial.ipynb Defines Pytensor symbolic variables for input vectors. These are necessary before using the `scan` function. ```python import pytensor import pytensor.tensor as pt import numpy as np vector1 = pt.dvector('vector1') vector2 = pt.dvector('vector2') ``` -------------------------------- ### Example CumsumOp Definition Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/creating_a_numba_jax_op.rst This snippet shows the definition of a `CumsumOp` which inherits from `Op` and sets a 'mode' attribute. It's a variant of `CumOp`. ```python class CumsumOp(Op): __props__ = ("axis",) def __new__(typ, *args, **kwargs): obj = object.__new__(CumOp, *args, **kwargs) obj.mode = "add" return obj ``` -------------------------------- ### D3.js Graph Visualization Setup Source: https://github.com/pymc-devs/pytensor/blob/main/doc/library/d3viz/examples/ofg2.html Initializes the SVG canvas, appends a group for the graph, and sets up D3's zoom behavior. It also configures tooltips for edges and node information, and defines arrow markers for edges. ```javascript var graph = {}; var forceLayout; var isProfiled = false; // true if profiling information available var useProfileColors = false; var fixOnInit = true; // fix nodes on initialization var maxProfilePer = 0; var profileColors = ["#fff5f0","#fee0d2","#fcbba1","#fc9272","#fb6a4a","#ef3b2c","#cb181d","#a50f15"]; var pad = 10; var isEditNode = false; // true if node is edited var menuItems = [ { title: 'Edit', action: function(elm, d, i) { editNode(elm, d); } }, { title: 'Release', action: function(elm, d, i) { releaseNode(d); } } ]; // Create main panel d3.select('body').select('svg').remove(); var svg = d3.select('body').append('svg') .attr('width', '100%') .attr('height', '95%'); var pane = svg.append('g'); // Zoom behaviour var zoom = d3.behavior.zoom() .scaleExtent([0.2, 8]) .on('zoom', function(d) { var trans = d3.event.translate; trans[0] += 300; trans[1] += 100; pane.attr('transform', 'translate(' + trans + ') scale(' + d3.event.scale + ')'); }); svg.call(zoom); zoom.event(svg); svg.on("dblclick.zoom", null); // Edges var edgeDiv = d3.select('body').append('div') .attr('class', 'edgeTooltip') .style('opacity', 0.0); // Div for node details var nodeInfo = d3.select('body').append('div') .attr('class', 'nodeInfo') .style('opacity', 0.0); // Definition head of edges var markerData = [ {'id': 'n', 'color': 'black'}, {'id': 'r', 'color': 'red'}, {'id': 'b', 'color': 'dodgerblue'}]; svg.append("defs").selectAll('marker').data(markerData).enter().append("marker") .attr("id", function(d) { return 'edgeArrow\_' + d.id; }) .attr("markerWidth", 4) .attr("markerHeight", 4) .attr("refX", 2) .attr("refY", 2) .attr("orient", "auto") .append("path") .attr("d", "M0,0 L4,2 L0,4 Z") .attr('fill', function(d) { return d.color; }); // Initialize graph processDotGraph(dotGraph); graph = frontEndGraph(dotGraph); drawGraph(); ``` -------------------------------- ### Logistic Regression Model Setup in PyTensor Source: https://github.com/pymc-devs/pytensor/blob/main/doc/tutorial/examples.rst Sets up the initial components for a logistic regression model using PyTensor. This includes generating a dataset, declaring symbolic variables, and initializing shared weight and bias variables. ```python import numpy as np import pytensor import pytensor.tensor as pt rng = np.random.default_rng(2882) N = 400 # training sample size feats = 784 # number of input variables # generate a dataset: D = (input_values, target_class) D = (rng.standard_normal((N, feats)), rng.integers(size=N, low=0, high=2)) training_steps = 10000 # Declare PyTensor symbolic variables x = pt.dmatrix("x") y = pt.dvector("y") # initialize the weight vector w randomly # # this and the following bias variable b # are shared so they keep their values # between training iterations (updates) w = pytensor.shared(rng.standard_normal(feats), name="w") # initialize the bias term b = pytensor.shared(0., name="b") print("Initial model:") print(w.get_value()) print(b.get_value()) ``` -------------------------------- ### Compile function with Numba backend Source: https://github.com/pymc-devs/pytensor/blob/main/doc/troubleshooting.rst Compile a PyTensor function using the Numba backend for potential BLAS optimizations. This requires Numba to be installed. ```python from pytensor import function, config from pytensor.tensor import matrix x = matrix('x') y = x @ x.T f = function([x], y, mode='NUMBA') ``` -------------------------------- ### PyTensor Linker Options Source: https://github.com/pymc-devs/pytensor/blob/main/doc/tutorial/modes.rst Compares different linker options available in PyTensor, detailing their garbage collection support, error raising behavior, overhead, and definitions. ```text linker gc [#gc]_ Raise error by op Overhead Definition c============= ========= ================= ========= === cvm yes yes "++" As c|py, but the runtime algo to execute the code is in c cvm_nogc no yes "+" As cvm, but without gc c|py [#cpy1]_ yes yes "+++" Try C code. If none exists for an op, use Python c|py_nogc no yes "++" As c|py, but without gc c no yes "+" Use only C code (if none available for an op, raise an error) py yes yes "+++" Use only Python code NanGuardMode yes yes "++++" Check if nodes generate NaN DebugMode no yes VERY HIGH Make many checks on what PyTensor computes ``` -------------------------------- ### Link against default BLAS library Source: https://github.com/pymc-devs/pytensor/blob/main/doc/troubleshooting.rst Use the '-lblas' flag to link against the default BLAS library. This is useful if you have installed a system-wide BLAS library. ```bash blas__ldflags=-lblas ``` -------------------------------- ### Basic PyTensor Function Definition Source: https://github.com/pymc-devs/pytensor/blob/main/doc/tutorial/debug_faq.rst Defines a simple PyTensor function that multiplies two matrices. This serves as a basic example for debugging with pdb. ```python import numpy as np import pytensor import pytensor.tensor as pt a = pt.dmatrix('a') b = pt.dmatrix('b') f = pytensor.function([a, b], [a * b]) ``` -------------------------------- ### PyTensor Symbolic Graph Definition Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/introduction/pytensor_intro.ipynb Defines a symbolic computational graph in PyTensor for the same operation as the NumPy example. 'y' is a symbolic TensorVariable, not a computed number. ```python import pytensor import pytensor.tensor as pt # Pytensor x = pt.tensor(shape=(3,), dtype="float64") # Symbolic vector y = pt.log(1 + x) # Symbolic computation y, type(y) ``` -------------------------------- ### Import RewriteDatabase Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/rewrites/graph_rewrites.ipynb Import the optdb RewriteDatabase to manage graph rewrites. ```python from pytensor.compile.mode import optdb ``` -------------------------------- ### Configure floatX and device in .pytensorrc Source: https://github.com/pymc-devs/pytensor/blob/main/doc/library/config.rst Example of a .pytensorrc file configuration. Sets the default floating-point type to float32 and specifies the CUDA device. ```cfg [global] floatX = float32 device = cuda0 ``` -------------------------------- ### Define a PatternNodeRewriter Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/rewrites/graph_rewrites.ipynb Create a rewrite using PatternNodeRewriter for template matching. This example defines a rewrite for useless abs(x**2) patterns. ```python from pytensor.graph.rewriting.basic import PatternNodeRewriter local_useless_abs_square = PatternNodeRewriter( (pt.abs, (pt.pow, "x", 2)), (pt.pow, "x", 2), name="local_useless_abs_square", ) ``` -------------------------------- ### Import Statements for PyTensor Sparse Tutorial Source: https://github.com/pymc-devs/pytensor/blob/main/doc/tutorial/sparse.rst These are the necessary import statements for using PyTensor's sparse module and related libraries in the tutorial. Ensure these are executed before running any sparse matrix operations. ```python import pytensor import numpy as np import scipy.sparse as sp from pytensor import sparse ``` -------------------------------- ### Manual convolution calculation for explanation Source: https://github.com/pymc-devs/pytensor/blob/main/doc/gallery/autodiff/vector_jacobian_product.ipynb Demonstrates the manual calculation of discrete convolution by padding 'x' with zeros and performing dot products with the flipped 'y'. This serves to explain the output of `pt.signal.convolve1d`. ```python x_padded = np.array([0, 0, 1, 2, 0]) res = np.array([ x_padded[0:2] @ [-1, 1], x_padded[1:3] @ [-1, 1], x_padded[2:4] @ [-1, 1], x_padded[3:5] @ [-1, 1], ]) res ``` -------------------------------- ### Get C-contiguous copy of array Source: https://github.com/pymc-devs/pytensor/blob/main/doc/extending/creating_a_c_op.rst Returns a C-contiguous and well-behaved copy of the input array. If the input is already C-contiguous, a new reference is returned. ```c PyArrayObject* PyArray_GETCONTIGUOUS(PyObject* op) ``` -------------------------------- ### Query Optimization Summary Source: https://github.com/pymc-devs/pytensor/blob/main/doc/optimizations.rst Use this command to list the optimizations executed for a specific optimization mode. Replace with a valid mode like 'o1', 'o2', 'o3', 'o4', 'Stabilization', or 'unsafe'. ```bash python -c "import pytensor; pytensor.compile.optdb.query(pytensor.compile.mode.predefined_optimizers['']).print_summary()" ```