### PyTorch ConvGNP Model Example Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/basic_usage.md Shows how to construct and utilize a Convolutional Gaussian Neural Process (ConvGNP) model with PyTorch. This example covers model instantiation, data input, and subsequent probabilistic calculations. ```python import lab as B import torch import neuralprocesses.torch as nps cnp = nps.construct_convgnp(dim_x=2, dim_y=3, likelihood="lowrank") dist = cnp( B.randn(torch.float32, 16, 2, 10), # Context inputs B.randn(torch.float32, 16, 3, 10), # Context outputs B.randn(torch.float32, 16, 2, 15), # Target inputs ) mean, var = dist.mean, dist.var # Prediction for target outputs print(dist.logpdf(B.randn(torch.float32, 16, 3, 15))) print(dist.sample()) print(dist.kl(dist)) print(dist.entropy()) ``` -------------------------------- ### Install Neural Processes with TensorFlow or PyTorch Source: https://github.com/wesselb/neuralprocesses/blob/main/README.md Install the neuralprocesses package along with the necessary deep learning framework. Use TensorFlow or PyTorch depending on your needs. ```bash pip install neuralprocesses tensorflow tensorflow-probability # For use with TensorFlow pip install neuralprocesses torch # For use with PyTorch ``` -------------------------------- ### Construct and Use ConvGNP Model in TensorFlow Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/basic_usage.md Shows how to build a Convolutional Gaussian Neural Process (ConvGNP) model and perform predictions. Ensure 'lab' and 'neuralprocesses.tensorflow' are installed. ```python import lab as B import tensorflow as tf import neuralprocesses.tensorflow as nps cnp = nps.construct_convgnp(dim_x=2, dim_y=3, likelihood="lowrank") dist = cnp( B.randn(tf.float32, 16, 2, 10), # Context inputs B.randn(tf.float32, 16, 3, 10), # Context outputs B.randn(tf.float32, 16, 2, 15), # Target inputs ) mean, var = dist.mean, dist.var # Prediction for target outputs print(dist.logpdf(B.randn(tf.float32, 16, 3, 15))) print(dist.sample()) print(dist.kl(dist)) print(dist.entropy()) ``` -------------------------------- ### PyTorch GNP Model Example Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/basic_usage.md Demonstrates constructing and using a Gaussian Neural Process (GNP) model with PyTorch. This includes generating a model, passing context and target data, and performing various probabilistic operations. ```python import lab as B import torch import neuralprocesses.torch as nps cnp = nps.construct_gnp(dim_x=2, dim_y=3, likelihood="lowrank") dist = cnp( B.randn(torch.float32, 16, 2, 10), # Context inputs B.randn(torch.float32, 16, 3, 10), # Context outputs B.randn(torch.float32, 16, 2, 15), # Target inputs ) mean, var = dist.mean, dist.var # Prediction for target outputs print(dist.logpdf(B.randn(torch.float32, 16, 3, 15))) print(dist.sample()) print(dist.kl(dist)) print(dist.entropy()) ``` -------------------------------- ### ConvGNP with Auxiliary Variables in TensorFlow Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/build_your_own_model.md Implements a ConvGNP model that incorporates auxiliary variables using TensorFlow. This setup handles multiple context sets and auxiliary target information for enhanced model flexibility. ```python import lab as B import tensorflow as tf import neuralprocesses.tensorflow as nps dim_x = 2 # We will use two target sets with output dimensionalities `dim_y` and `dim_y2`. dim_y = 1 dim_y2 = 10 # We will also use auxiliary target information of dimensionality `dim_aux_t`. dim_aux_t = 7 # CNN architecture: unet = nps.UNet( dim=dim_x, # The outputs are `dim_y`-dimensional, and we will use another context set # consisting of `dim_y2` variables. Both of these context sets will also have a # density channel. in_channels=dim_y + 1 + dim_y2 + 1, out_channels=8, channels=(8, 16, 16, 32, 32, 64), ) # Discretisation of the functional embedding: disc = nps.Discretisation( points_per_unit=32, multiple=2**unet.num_halving_layers, margin=0.1, dim=dim_x, ) # Create the encoder and decoder and construct the model. encoder = nps.FunctionalCoder( disc, nps.Chain( nps.PrependDensityChannel(), # Use a separate set conv for every context set. Here we initialise the length # scales of these set convs both to `1 / disc.points_per_unit`. nps.Parallel( nps.SetConv(scale=1 / disc.points_per_unit), nps.SetConv(scale=1 / disc.points_per_unit), ), nps.DivideByFirstChannel(), # Concatenate the encodings of the context sets. nps.Concatenate(), nps.DeterministicLikelihood(), ), ) decoder = nps.Chain( unet, nps.SetConv(scale=1 / disc.points_per_unit), # `nps.Augment` will concatenate any auxiliary information to the current encoding # before proceedings. nps.Augment( nps.Chain( nps.MLP( # Input dimensionality is equal to the number of channels coming out of # `unet` plus the dimensionality of the auxiliary target information. in_dim=8 + dim_aux_t, layers=(128,) * 3, out_dim=(2 + 512) * dim_y, ), nps.LowRankGaussianLikelihood(512), ) ) ) convgnp = nps.Model(encoder, decoder) # Run the model on some random data. dist = convgnp( [ ( B.randn(tf.float32, 16, dim_x, 10), B.randn(tf.float32, 16, dim_y, 10), ), ( # The second context set is given on a grid. (B.randn(tf.float32, 16, 1, 12), B.randn(tf.float32, 16, 1, 12)), B.randn(tf.float32, 16, dim_y2, 12, 12), ) ], B.randn(tf.float32, 16, dim_x, 15), aux_t=B.randn(tf.float32, 16, dim_aux_t, 15), ) ``` -------------------------------- ### PyTorch ConvCNP Model Construction, Training, and Prediction Source: https://github.com/wesselb/neuralprocesses/blob/main/README.md Demonstrates how to construct a ConvCNP model, set up an optimizer, train the model with sample data, and make predictions. Ensure you replace sample data with your actual datasets. ```python import torch import neuralprocesses.torch as nps # Construct a ConvCNP. convcnp = nps.construct_convgnp(dim_x=1, dim_y=2, likelihood="het") # Construct optimiser. opt = torch.optim.Adam(convcnp.parameters(), 1e-3) # Training: optimise the model for 32 batches. for _ in range(32): # Sample a batch of new context and target sets. Replace this with your data. The # shapes are `(batch_size, dimensionality, num_data)`. xc = torch.randn(16, 1, 10) # Context inputs yc = torch.randn(16, 2, 10) # Context outputs xt = torch.randn(16, 1, 15) # Target inputs yt = torch.randn(16, 2, 15) # Target output # Compute the loss and update the model parameters. loss = -torch.mean(nps.loglik(convcnp, xc, yc, xt, yt, normalise=True)) opt.zero_grad(set_to_none=True) loss.backward() opt.step() # Testing: make some predictions. mean, var, noiseless_samples, noisy_samples = nps.predict( convcnp, torch.randn(16, 1, 10), # Context inputs torch.randn(16, 2, 10), # Context outputs torch.randn(16, 1, 15), # Target inputs ) ``` -------------------------------- ### Constructing a ConvGNP Model Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/advanced_usage.md Initialises a ConvGNP model with specified dimensions for input, context outputs, and target outputs. It also prepares sample context and target data. ```python import lab as B import torch import neuralprocesses.torch as nps cnp = nps.construct_convgnp( dim_x=2, dim_yc=(1, 1), # Two context sets, both with one channel dim_yt=1, ) # Construct two sample context sets with one on a grid. xc = B.randn(torch.float32, 1, 2, 20) yc = B.randn(torch.float32, 1, 1, 20) xc_grid = (B.randn(torch.float32, 1, 1, 10), B.randn(torch.float32, 1, 1, 15)) yc_grid = B.randn(torch.float32, 1, 1, 10, 15) # Contruct sample target inputs xt = B.randn(torch.float32, 1, 2, 50) ``` -------------------------------- ### Import NeuralProcesses for PyTorch Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/basic_usage.md Import the PyTorch backend for NeuralProcesses. This is the first step before using any PyTorch-specific functionalities. ```python import neuralprocesses.torch as nps ``` -------------------------------- ### Construct and Use GNP Model in TensorFlow Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/basic_usage.md Demonstrates how to construct a Gaussian Neural Process (GNP) model and use it for predictions. Requires the 'lab' and 'neuralprocesses.tensorflow' libraries. ```python import lab as B import tensorflow as tf import neuralprocesses.tensorflow as nps cnp = nps.construct_gnp(dim_x=2, dim_y=3, likelihood="lowrank") dist = cnp( B.randn(tf.float32, 16, 2, 10), # Context inputs B.randn(tf.float32, 16, 3, 10), # Context outputs B.randn(tf.float32, 16, 2, 15), # Target inputs ) mean, var = dist.mean, dist.var # Prediction for target outputs print(dist.logpdf(B.randn(tf.float32, 16, 3, 15))) print(dist.sample()) print(dist.kl(dist)) print(dist.entropy()) ``` -------------------------------- ### Constructing Additional Context Sets Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/advanced_usage.md Defines additional sample context sets with different sizes, intended for batching with existing context sets. ```python # Construct another two sample context sets with one on a grid. xc2 = B.randn(torch.float32, 1, 2, 30) yc2 = B.randn(torch.float32, 1, 1, 30) xc2_grid = (B.randn(torch.float32, 1, 1, 5), B.randn(torch.float32, 1, 1, 20)) yc2_grid = B.randn(torch.float32, 1, 1, 5, 20) ``` -------------------------------- ### Displaying Neural Process Data in a Table Source: https://github.com/wesselb/neuralprocesses/blob/main/tables.ipynb This snippet shows the raw output of a table containing various neural process models and their associated metrics. It is useful for visualizing experimental results. ```text CNP & -1.03 {\small \pm 0.02} & -1.10 {\small \pm 0.02} & -1.17 {\small \pm 0.02} \\ ACNP & -0.46 {\small \pm 0.02} & -1.24 {\small \pm 0.02} & -1.22 {\small \pm 0.01} \\ ConvCNP & 0.07 {\small \pm 0.04} & 0.06 {\small \pm 0.04} & -1.21 {\small \pm 0.01} \\ ConvCNP (AR) & 0.56 {\small \pm 0.04} & 0.56 {\small \pm 0.04} & 0.34 {\small \pm 0.04} \\ GNP & -0.69 {\small \pm 0.01} & -2.58 {\small \pm 0.07} & -1.74 {\small \pm 0.04} \\ AGNP & -0.34 {\small \pm 0.02} & -0.94 {\small \pm 0.02} & -1.29 {\small \pm 0.03} \\ ConvGNP & 0.16 {\small \pm 0.06} & 0.16 {\small \pm 0.06} & -0.91 {\small \pm 0.02} \\ FullConvGNP & 0.30 {\small \pm 0.03} & 0.30 {\small \pm 0.03} & -0.46 {\small \pm 0.01} \\ NP & -0.63 {\small \pm 0.01} & -1.43 {\small \pm 0.01} & -1.25 {\small \pm 0.02} \\ ANP & -0.25 {\small \pm 0.02} & -1.04 {\small \pm 0.02} & -1.04 {\small \pm 0.02} \\ ConvNP & 0.38 {\small \pm 0.04} & 0.39 {\small \pm 0.04} & -0.85 {\small \pm 0.02} \\ NP (ELBO) & -0.59 {\small \pm 0.01} & -2.72 {\small \pm 0.03} & -1.88 {\small \pm 0.01} \\ ANP (ELBO) & -0.40 {\small \pm 0.02} & -2.81 {\small \pm 0.07} & -3.27 {\small \pm 0.04} \\ ConvNP (ELBO) & 0.45 {\small \pm 0.04} & 0.45 {\small \pm 0.04} & -0.10 {\small \pm 0.04} \\ ``` -------------------------------- ### Define Neural Process Models and Datasets Source: https://github.com/wesselb/neuralprocesses/blob/main/tables.ipynb Defines a list of neural process models with their names and objectives, and a list of datasets. ```python models = [ ("cnp", "CNP", "loglik"), ("acnp", "ACNP", "loglik"), ("convcnp", "ConvCNP", "loglik"), ("gnp", "GNP", "loglik"), ("agnp", "AGNP", "loglik"), ("convgnp", "ConvGNP", "loglik"), ("fullconvgnp", "FullConvGNP", "loglik"), ("np", "NP", "loglik"), ("anp", "ANP", "loglik"), ("convnp", "ConvNP", "loglik"), ("np", "NP", "elbo"), ("anp", "ANP", "elbo"), ("convnp", "ConvNP", "elbo"), ] datasets = [ "sawtooth", # "mixture", ] ``` -------------------------------- ### Import NeuralProcesses for TensorFlow Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/basic_usage.md Import the TensorFlow backend for NeuralProcesses. Use this when working with TensorFlow models. ```python import neuralprocesses.tensorflow as nps ``` -------------------------------- ### Import Libraries Source: https://github.com/wesselb/neuralprocesses/blob/main/tables.ipynb Imports necessary libraries for numerical operations and itertools. ```python from itertools import product import numpy as np ``` -------------------------------- ### Processing Experimental Log Files Source: https://github.com/wesselb/neuralprocesses/blob/main/tables.ipynb This script processes log files from experiments to extract and format performance metrics. It iterates through predefined models and datasets, constructs file paths, and parses log data based on the objective function (loglik or ELBO). ```python rows = [] inters = [] datasets = ["mixture"] for (model, string, objective), dataset in product(models, datasets): root = f"_experiments/{dataset}/x1_y1/{model}" if "conv" in model: root = root + "/unet" root = root + f"/{objective}/log_evaluate_out.txt" _file = open(root, "r").read() try: if objective == "loglik": file = _file.split("| Loglik:")[1].strip().split("\n")[:12] file = file[:2] + file[4:6] + file[8:10] process = lambda x: float(x.strip()) interpolation = list(map(process, file[1].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) extrapolation = list(map(process, file[3].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) out_of_range = list(map(process, file[5].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) row = format_line(string, interpolation, extrapolation, out_of_range) print(row) rows.append(row) inters.append(interpolation[0]) if "cnp" in model and "| AR:" in _file: file = _file.split("| AR:")[1].strip().split("\n")[:12] file = file[:2] + file[4:6] + file[8:10] process = lambda x: float(x.strip()) interpolation = list(map(process, file[1].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) extrapolation = list(map(process, file[3].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) out_of_range = list(map(process, file[5].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) row = format_line(string + " (AR)", interpolation, extrapolation, out_of_range) print(row) rows.append(row) inters.append(interpolation[0]) else: file = _file.split("| Loglik:")[1].strip().split("\n")[:12] file = file[:2] + file[4:6] + file[8:10] process = lambda x: float(x.strip()) interpolation = list(map(process, file[1].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) extrapolation = list(map(process, file[3].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) out_of_range = list(map(process, file[5].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) row = format_line(string + ' (ELBO, ML)', interpolation, extrapolation, out_of_range) file = _file.split("| ELBO:")[1].strip().split("\n")[:12] file = file[:2] + file[4:6] + file[8:10] process = lambda x: float(x.strip()) interpolation = list(map(process, file[1].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) extrapolation = list(map(process, file[3].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) out_of_range = list(map(process, file[5].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) row = format_line(string + ' (ELBO)', interpolation, extrapolation, out_of_range) print(row) rows.append(row) inters.append(interpolation[0]) except: row = format_line(string, None, None, None) print(row) ``` -------------------------------- ### Making Predictions with ConvGNP Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/advanced_usage.md Performs predictions using the constructed ConvGNP model with two different context sets. ```python >>> pred = cnp([(xc, yc), (xc_grid, yc_grid)], xt) ``` -------------------------------- ### Sorted Neural Process Data Table Source: https://github.com/wesselb/neuralprocesses/blob/main/tables.ipynb This snippet shows the same neural process data as the first table, but sorted in descending order based on a specific metric. This is useful for quickly identifying the best-performing models. ```text Output: ConvCNP (AR) & 0.56 {\small \pm 0.04} & 0.56 {\small \pm 0.04} & 0.34 {\small \pm 0.04} \\ ConvNP (ELBO) & 0.45 {\small \pm 0.04} & 0.45 {\small \pm 0.04} & -0.10 {\small \pm 0.04} \\ ConvNP & 0.38 {\small \pm 0.04} & 0.39 {\small \pm 0.04} & -0.85 {\small \pm 0.02} \\ FullConvGNP & 0.30 {\small \pm 0.03} & 0.30 {\small \pm 0.03} & -0.46 {\small \pm 0.01} \\ ConvGNP & 0.16 {\small \pm 0.06} & 0.16 {\small \pm 0.06} & -0.91 {\small \pm 0.02} \\ ConvCNP & 0.07 {\small \pm 0.04} & 0.06 {\small \pm 0.04} & -1.21 {\small \pm 0.01} \\ ANP & -0.25 {\small \pm 0.02} & -1.04 {\small \pm 0.02} & -1.04 {\small \pm 0.02} \\ AGNP & -0.34 {\small \pm 0.02} & -0.94 {\small \pm 0.02} & -1.29 {\small \pm 0.03} \\ ANP (ELBO) & -0.40 {\small \pm 0.02} & -2.81 {\small \pm 0.07} & -3.27 {\small \pm 0.04} \\ ACNP & -0.46 {\small \pm 0.02} & -1.24 {\small \pm 0.02} & -1.22 {\small \pm 0.01} \\ NP (ELBO) & -0.59 {\small \pm 0.01} & -2.72 {\small \pm 0.03} & -1.88 {\small \pm 0.01} \\ NP & -0.63 {\small \pm 0.01} & -1.43 {\small \pm 0.01} & -1.25 {\small \pm 0.02} \\ GNP & -0.69 {\small \pm 0.01} & -2.58 {\small \pm 0.07} & -1.74 {\small \pm 0.04} \\ CNP & -1.03 {\small \pm 0.02} & -1.10 {\small \pm 0.02} & -1.17 {\small \pm 0.02} \\ ``` -------------------------------- ### Predicting with Merged Context Sets Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/advanced_usage.md Performs predictions using a ConvGNP model with merged context sets, demonstrating the ability to handle varying context set sizes in a single batch. ```python >>> pred = cnp( [(xc_merged, yc_merged), (xc_grid_merged, yc_grid_merged)], B.concat(xt, xt, axis=0) ) ``` -------------------------------- ### Process Neural Process Model Results Source: https://github.com/wesselb/neuralprocesses/blob/main/tables.ipynb This script processes log files from neural process experiments to extract and format performance metrics. It iterates through models and datasets, parsing log-likelihood and ELBO values for different evaluation scenarios. ```python rows = [] inters = [] datasets = ["sawtooth"] for (model, string, objective), dataset in product(models, datasets): root = f"_experiments/{dataset}/x1_y2/{model}" if "conv" in model: root = root + "/unet" root = root + f"/{objective}/log_evaluate_out.txt" _file = open(root, "r").read() try: if objective == "loglik": file = _file.split("| Loglik:")[1].strip().split("\n")[:6] process = lambda x: float(x.strip()) interpolation = list(map(process, file[1].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) extrapolation = list(map(process, file[3].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) out_of_range = list(map(process, file[5].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) row = format_line(string, interpolation, extrapolation, out_of_range) inters.append(interpolation[0]) rows.append(row) print(row) if "cnp" in model and "| AR:" in _file: file = _file.split("| AR:")[1].strip().split("\n")[:6] process = lambda x: float(x.strip()) interpolation = list(map(process, file[1].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) extrapolation = list(map(process, file[3].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) out_of_range = list(map(process, file[5].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) row = format_line(string + " (AR)", interpolation, extrapolation, out_of_range) inters.append(interpolation[0]) rows.append(row) print(row) else: file = _file.split("| Loglik:")[1].strip().split("\n")[:6] process = lambda x: float(x.strip()) interpolation = list(map(process, file[1].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) extrapolation = list(map(process, file[3].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) out_of_range = list(map(process, file[5].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) row = format_line(string + ' (EL., ML.)', interpolation, extrapolation, out_of_range) inters.append(interpolation[0]) rows.append(row) print(row) file = _file.split("| ELBO:")[1].strip().split("\n")[:6] process = lambda x: float(x.strip()) interpolation = list(map(process, file[1].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) extrapolation = list(map(process, file[3].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) out_of_range = list(map(process, file[5].split("|")[1].strip().split("Loglik (V):")[1].split("(")[0].split("+-"))) row = format_line(string + ' (ELBO)', interpolation, extrapolation, out_of_range) inters.append(interpolation[0]) rows.append(row) print(row) except: row = format_line(string, None, None, None) inters.append(-100000) print(row) ``` -------------------------------- ### ConvGNP Model in TensorFlow Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/build_your_own_model.md Defines a Convolutional Gaussian Neural Process (ConvGNP) model using TensorFlow. This snippet sets up the UNet architecture, discretisation, encoder, and decoder for a functional coder. ```python import lab as B import tensorflow as tf import neuralprocesses.tensorflow as nps dim_x = 1 dim_y = 1 # CNN architecture: unet = nps.UNet( dim=dim_x, in_channels=2 * dim_y, out_channels=(2 + 512) * dim_y, channels=(8, 16, 16, 32, 32, 64), ) # Discretisation of the functional embedding: disc = nps.Discretisation( points_per_unit=64, multiple=2**unet.num_halving_layers, margin=0.1, dim=dim_x, ) # Create the encoder and decoder and construct the model. encoder = nps.FunctionalCoder( disc, nps.Chain( nps.PrependDensityChannel(), nps.SetConv(scale=1 / disc.points_per_unit), nps.DivideByFirstChannel(), nps.DeterministicLikelihood(), ), ) decoder = nps.Chain( unet, nps.SetConv(scale=1 / disc.points_per_unit), nps.LowRankGaussianLikelihood(512), ) convgnp = nps.Model(encoder, decoder) # Run the model on some random data. dist = convgnp( B.randn(tf.float32, 16, 1, 10), B.randn(tf.float32, 16, 1, 10), B.randn(tf.float32, 16, 1, 15), ) ``` -------------------------------- ### Creating a Mask for Grid Context Outputs Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/advanced_usage.md Generates a mask tensor to indicate observed data points in a gridded context output. A value of 0 in the mask signifies an unobserved data point. ```python >>> mask = B.ones(torch.float32, 1, 1, *B.shape(yc_grid, 2, 3)) >>> mask[:, :, 5, 5] = 0 ``` -------------------------------- ### Format Output Line Function Source: https://github.com/wesselb/neuralprocesses/blob/main/tables.ipynb Formats a line of output for displaying interpolation, extrapolation, and out-of-range metrics. Handles missing values and special formatting for large numbers. ```python def format_line(string, interpolation, extrapolation, out_of_range, phantom=True): p = "\hphantom{-}" if phantom else "" if interpolation is not None: p1 = p if interpolation[0] > 0 else "" inter = f"${{p1}}{{interpolation[0]:8.2f}} {{'\small' }} \pm {{interpolation[1]:6.2f}}}$" if interpolation[0] > -20. else " " * 13 + "F" + " " * 14 else: inter = f"{ 'Missing':<19}" if interpolation is not None: p2 = p if extrapolation[0] > 0 else "" extra = f"${{p2}}{{extrapolation[0]:8.2f}} {{'\small' }} \pm {{extrapolation[1]:6.2f}}}$" if extrapolation[0] > -20. else " " * 13 + "F" + " " * 14 else: extra = f"{ 'Missing':<19}" if out_of_range is not None: p3 = p if out_of_range[0] > 0 else "" oor = f"${{p3}}{{out_of_range[0]:8.2f}} {{'\small' }} \pm {{out_of_range[1]:6.2f}}}$" if out_of_range[0] > -20. else " " * 13 + "F" + " " * 14 else: oor = f"{ 'Missing':<19}" return f"{string:<20} & {inter} & {extra} & {oor} \\" ``` -------------------------------- ### Creating a Mask for Non-Gridded Context Outputs Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/advanced_usage.md Generates a mask tensor for non-gridded context outputs, specifying a range of elements that are considered missing. ```python >>> mask = B.ones(torch.float32, 1, 1, B.shape(yc, 2)) >>> mask[:, :, 2:7] = 0 # Elements 3 to 7 are missing. ``` -------------------------------- ### Merging Context Sets of Different Sizes Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/advanced_usage.md Utilises `nps.merge_contexts` to automatically pad and mask context sets of varying sizes, preparing them for batched processing. ```python xc_merged, yc_merged = nps.merge_contexts((xc, yc), (xc2, yc2)) xc_grid_merged, yc_grid_merged = nps.merge_contexts( (xc_grid, yc_grid), (xc2_grid, yc2_grid) ) ``` -------------------------------- ### Masking Grid Context Outputs during Prediction Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/advanced_usage.md Makes predictions using a ConvGNP model where a specific gridded context output is masked, indicating it was not observed. ```python >>> pred = cnp([(xc, yc), (xc_grid, nps.Masked(yc_grid, mask))], xt) ``` -------------------------------- ### Masking Non-Gridded Context Outputs during Prediction Source: https://github.com/wesselb/neuralprocesses/blob/main/docs/advanced_usage.md Makes predictions using a ConvGNP model where a specific non-gridded context output is masked, indicating a range of unobserved data points. ```python >>> pred = cnp([(xc, nps.Masked(yc, mask)), (xc_grid, yc_grid)], xt) ``` -------------------------------- ### Checking Array Lengths Source: https://github.com/wesselb/neuralprocesses/blob/main/tables.ipynb Verifies the lengths of two arrays, 'rows' and 'inters'. This is often used for debugging to ensure data consistency before further processing. ```python len(rows), len(inters) ``` -------------------------------- ### Sorting Rows Based on Intersections Source: https://github.com/wesselb/neuralprocesses/blob/main/tables.ipynb Sorts rows based on the descending order of intersections. This snippet is useful for reordering data for analysis. ```python for r in np.array(rows)[np.argsort(-np.array(inters))]: print(r) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.