Top Related Projects
Tensors and Dynamic neural networks in Python with strong GPU acceleration
scikit-learn: machine learning in Python
An Open Source Machine Learning Framework for Everyone
Gaussian processes in TensorFlow
Deep universal probabilistic programming with Python and PyTorch
Deep Learning for humans
Quick Overview
GPyTorch is a highly efficient and modular Gaussian Process library implemented in PyTorch. It provides a flexible framework for scalable Gaussian Processes, allowing users to combine GP models with deep learning techniques and leverage GPU acceleration for faster computations.
Pros
- Seamless integration with PyTorch, enabling easy combination of GPs with deep learning models
- Highly scalable, capable of handling large datasets through various approximation methods
- Supports GPU acceleration for faster computations
- Extensive documentation and examples for various use cases
Cons
- Steeper learning curve compared to some simpler GP libraries
- Requires familiarity with PyTorch
- May be overkill for small-scale or simple GP tasks
- Documentation can be overwhelming for beginners
Code Examples
- Simple GP Regression:
import torch
import gpytorch
class ExactGPModel(gpytorch.models.ExactGP):
def __init__(self, train_x, train_y, likelihood):
super(ExactGPModel, self).__init__(train_x, train_y, likelihood)
self.mean_module = gpytorch.means.ConstantMean()
self.covar_module = gpytorch.kernels.ScaleKernel(gpytorch.kernels.RBFKernel())
def forward(self, x):
mean_x = self.mean_module(x)
covar_x = self.covar_module(x)
return gpytorch.distributions.MultivariateNormal(mean_x, covar_x)
# Train the model
model = ExactGPModel(train_x, train_y, likelihood)
model.train()
optimizer = torch.optim.Adam(model.parameters(), lr=0.1)
- Multi-task GP:
class MultitaskGPModel(gpytorch.models.ExactGP):
def __init__(self, train_x, train_y, likelihood):
super(MultitaskGPModel, self).__init__(train_x, train_y, likelihood)
self.mean_module = gpytorch.means.MultitaskMean(
gpytorch.means.ConstantMean(), num_tasks=2
)
self.covar_module = gpytorch.kernels.MultitaskKernel(
gpytorch.kernels.RBFKernel(), num_tasks=2, rank=1
)
def forward(self, x):
mean_x = self.mean_module(x)
covar_x = self.covar_module(x)
return gpytorch.distributions.MultitaskMultivariateNormal(mean_x, covar_x)
- Deep Kernel Learning:
class DeepGPHiddenLayer(gpytorch.models.deep_gps.DeepGPLayer):
def __init__(self, input_dims, output_dims, num_inducing=128, mean_type='constant'):
inducing_points = torch.randn(num_inducing, input_dims)
batch_shape = torch.Size([])
variational_distribution = gpytorch.variational.CholeskyVariationalDistribution(
num_inducing_points=num_inducing,
batch_shape=batch_shape
)
variational_strategy = gpytorch.variational.VariationalStrategy(
self,
inducing_points,
variational_distribution,
learn_inducing_locations=True
)
super().__init__(variational_strategy, input_dims, output_dims)
if mean_type == 'constant':
self.mean_module = gpytorch.means.ConstantMean(batch_shape=batch_shape)
else:
self.mean_module = gpytorch.means.LinearMean(input_dims)
self.covar_module = gpytorch.kernels.ScaleKernel(
gpytorch.kernels.RBFKernel(batch_shape=batch_shape, ard_num_dims
Competitor Comparisons
Tensors and Dynamic neural networks in Python with strong GPU acceleration
Pros of PyTorch
- Broader scope and functionality for general deep learning tasks
- Larger community and more extensive documentation
- More frequent updates and active development
Cons of PyTorch
- Steeper learning curve for beginners
- Larger codebase and installation size
- Not specialized for Gaussian Processes like GPyTorch
Code Comparison
PyTorch (general neural network):
import torch
import torch.nn as nn
model = nn.Sequential(
nn.Linear(10, 20),
nn.ReLU(),
nn.Linear(20, 1)
)
GPyTorch (Gaussian Process model):
import gpytorch
class GPModel(gpytorch.models.ExactGP):
def __init__(self, train_x, train_y, likelihood):
super().__init__(train_x, train_y, likelihood)
self.mean_module = gpytorch.means.ConstantMean()
self.covar_module = gpytorch.kernels.ScaleKernel(gpytorch.kernels.RBFKernel())
PyTorch is a more general-purpose deep learning framework, while GPyTorch is specifically designed for Gaussian Process models. PyTorch offers a wider range of tools for various machine learning tasks, whereas GPyTorch provides specialized functionality for GP-based models, building on top of PyTorch's infrastructure.
scikit-learn: machine learning in Python
Pros of scikit-learn
- Comprehensive machine learning library with a wide range of algorithms
- Well-established, mature project with extensive documentation and community support
- Seamless integration with other scientific Python libraries (NumPy, SciPy, Pandas)
Cons of scikit-learn
- Limited support for GPU acceleration and distributed computing
- Less specialized in Gaussian Processes compared to GPyTorch
- May be slower for certain large-scale machine learning tasks
Code Comparison
scikit-learn (Gaussian Process Regression):
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF
gpr = GaussianProcessRegressor(kernel=RBF())
gpr.fit(X_train, y_train)
y_pred, sigma = gpr.predict(X_test, return_std=True)
GPyTorch (Gaussian Process Regression):
import gpytorch
class GPModel(gpytorch.models.ExactGP):
def __init__(self, train_x, train_y, likelihood):
super().__init__(train_x, train_y, likelihood)
self.mean_module = gpytorch.means.ConstantMean()
self.covar_module = gpytorch.kernels.ScaleKernel(gpytorch.kernels.RBFKernel())
def forward(self, x):
mean_x = self.mean_module(x)
covar_x = self.covar_module(x)
return gpytorch.distributions.MultivariateNormal(mean_x, covar_x)
An Open Source Machine Learning Framework for Everyone
Pros of TensorFlow
- Broader scope and functionality for general machine learning tasks
- Larger community and ecosystem with more resources and third-party libraries
- Better support for deployment across various platforms (mobile, web, cloud)
Cons of TensorFlow
- Steeper learning curve, especially for those new to deep learning
- Can be more complex and verbose for specific Gaussian Process tasks
- Slower development cycle for new features compared to GPyTorch
Code Comparison
GPyTorch (for Gaussian Process regression):
class ExactGPModel(gpytorch.models.ExactGP):
def __init__(self, train_x, train_y, likelihood):
super().__init__(train_x, train_y, likelihood)
self.mean_module = gpytorch.means.ConstantMean()
self.covar_module = gpytorch.kernels.ScaleKernel(gpytorch.kernels.RBFKernel())
TensorFlow (for Gaussian Process regression):
import tensorflow_probability as tfp
kernel = tfp.math.psd_kernels.ExponentiatedQuadratic()
gp = tfp.distributions.GaussianProcess(kernel, index_points=X)
While both libraries can handle Gaussian Processes, GPyTorch is more specialized and concise for this task, whereas TensorFlow requires additional modules and potentially more setup for the same functionality.
Gaussian processes in TensorFlow
Pros of GPflow
- Built on TensorFlow, leveraging its powerful computational graph and GPU acceleration
- Supports a wider range of GP models, including variational GPs and multi-output GPs
- More extensive documentation and tutorials for beginners
Cons of GPflow
- Steeper learning curve due to TensorFlow backend
- Less flexible for custom kernel implementations
- Slower development cycle compared to PyTorch-based libraries
Code Comparison
GPflow example:
import gpflow
import tensorflow as tf
X = tf.random.uniform((100, 1))
Y = tf.sin(X) + 0.1 * tf.random.normal((100, 1))
kernel = gpflow.kernels.SquaredExponential()
model = gpflow.models.GPR(data=(X, Y), kernel=kernel)
GPyTorch example:
import gpytorch
import torch
class ExactGPModel(gpytorch.models.ExactGP):
def __init__(self, train_x, train_y, likelihood):
super().__init__(train_x, train_y, likelihood)
self.mean_module = gpytorch.means.ConstantMean()
self.covar_module = gpytorch.kernels.ScaleKernel(gpytorch.kernels.RBFKernel())
def forward(self, x):
mean_x = self.mean_module(x)
covar_x = self.covar_module(x)
return gpytorch.distributions.MultivariateNormal(mean_x, covar_x)
Deep universal probabilistic programming with Python and PyTorch
Pros of Pyro
- Broader scope: Pyro is a general-purpose probabilistic programming framework, offering more flexibility for various statistical models beyond just Gaussian Processes
- Deep learning integration: Seamlessly integrates with PyTorch, allowing for easy combination of deep learning and probabilistic models
- Active community: Larger user base and more frequent updates, potentially leading to better support and resources
Cons of Pyro
- Learning curve: Due to its broader scope, Pyro may have a steeper learning curve for users specifically interested in Gaussian Processes
- Performance: GPyTorch is optimized specifically for Gaussian Processes, potentially offering better performance for GP-related tasks
Code Comparison
Pyro (defining a simple Gaussian Process):
import pyro
import pyro.distributions as dist
def model(X, y):
lengthscale = pyro.sample("lengthscale", dist.LogNormal(0.0, 1.0))
variance = pyro.sample("variance", dist.LogNormal(0.0, 1.0))
kernel = RBFKernel(lengthscale)
f = pyro.sample("f", dist.GaussianProcess(kernel, X))
pyro.sample("y", dist.Normal(f, variance), obs=y)
GPyTorch (defining a similar Gaussian Process):
import gpytorch
class GPModel(gpytorch.models.ExactGP):
def __init__(self, train_x, train_y, likelihood):
super().__init__(train_x, train_y, likelihood)
self.mean_module = gpytorch.means.ConstantMean()
self.covar_module = gpytorch.kernels.ScaleKernel(gpytorch.kernels.RBFKernel())
def forward(self, x):
mean_x = self.mean_module(x)
covar_x = self.covar_module(x)
return gpytorch.distributions.MultivariateNormal(mean_x, covar_x)
Deep Learning for humans
Pros of Keras
- Broader scope: Keras is a high-level neural networks API, supporting multiple backend engines (TensorFlow, Theano, etc.)
- Larger community and ecosystem: More resources, tutorials, and third-party extensions available
- User-friendly: Designed for ease of use and rapid prototyping of deep learning models
Cons of Keras
- Less specialized: Not specifically designed for Gaussian Processes like GPyTorch
- Lower flexibility: May offer less control over low-level operations compared to GPyTorch
- Performance: Potentially slower for specific GP tasks compared to GPyTorch's optimized implementation
Code Comparison
Keras (creating a simple neural network):
from keras.models import Sequential
from keras.layers import Dense
model = Sequential([
Dense(64, activation='relu', input_shape=(10,)),
Dense(1, activation='sigmoid')
])
GPyTorch (creating a simple GP model):
import gpytorch
class ExactGPModel(gpytorch.models.ExactGP):
def __init__(self, train_x, train_y, likelihood):
super().__init__(train_x, train_y, likelihood)
self.mean_module = gpytorch.means.ConstantMean()
self.covar_module = gpytorch.kernels.ScaleKernel(gpytorch.kernels.RBFKernel())
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
GPyTorch
GPyTorch is a Gaussian process library implemented using PyTorch. GPyTorch is designed for creating scalable, flexible, and modular Gaussian process models with ease.
Internally, GPyTorch differs from many existing approaches to GP inference by performing most inference operations using numerical linear algebra techniques like preconditioned conjugate gradients.
Implementing a scalable GP method is as simple as providing a matrix multiplication routine with the kernel matrix and its derivative via our LinearOperator interface,
or by composing many of our already existing LinearOperators
.
This allows not only for easy implementation of popular scalable GP techniques,
but often also for significantly improved utilization of GPU computing compared to solvers based on the Cholesky decomposition.
GPyTorch provides (1) significant GPU acceleration (through MVM based inference); (2) state-of-the-art implementations of the latest algorithmic advances for scalability and flexibility (SKI/KISS-GP, stochastic Lanczos expansions, LOVE, SKIP, stochastic variational deep kernel learning, ...); (3) easy integration with deep learning frameworks.
Examples, Tutorials, and Documentation
See our documentation, examples, tutorials on how to construct all sorts of models in GPyTorch.
Installation
Requirements:
- Python >= 3.8
- PyTorch >= 2.0
Install GPyTorch using pip or conda:
pip install gpytorch
conda install gpytorch -c gpytorch
(To use packages globally but install GPyTorch as a user-only package, use pip install --user
above.)
Latest (Unstable) Version
To upgrade to the latest (unstable) version, run
pip install --upgrade git+https://github.com/cornellius-gp/linear_operator.git
pip install --upgrade git+https://github.com/cornellius-gp/gpytorch.git
Development version
If you are contributing a pull request, it is best to perform a manual installation:
git clone https://github.com/cornellius-gp/gpytorch.git
cd gpytorch
pip install -e .[dev,docs,examples,keops,pyro,test] # keops and pyro are optional
ArchLinux Package
Note: Experimental AUR package. For most users, we recommend installation by conda or pip.
GPyTorch is also available on the ArchLinux User Repository (AUR).
You can install it with an AUR helper, like yay
, as follows:
yay -S python-gpytorch
To discuss any issues related to this AUR package refer to the comments section of
python-gpytorch
.
Citing Us
If you use GPyTorch, please cite the following papers:
@inproceedings{gardner2018gpytorch,
title={GPyTorch: Blackbox Matrix-Matrix Gaussian Process Inference with GPU Acceleration},
author={Gardner, Jacob R and Pleiss, Geoff and Bindel, David and Weinberger, Kilian Q and Wilson, Andrew Gordon},
booktitle={Advances in Neural Information Processing Systems},
year={2018}
}
Contributing
See the contributing guidelines CONTRIBUTING.md for information on submitting issues and pull requests.
The Team
GPyTorch is primarily maintained by:
- Jake Gardner (University of Pennsylvania)
- Geoff Pleiss (Columbia University)
- Kilian Weinberger (Cornell University)
- Andrew Gordon Wilson (New York University)
- Max Balandat (Meta)
We would like to thank our other contributors including (but not limited to) Eytan Bakshy, Wesley Maddox, Ke Alexander Wang, Ruihan Wu, Sait Cakmak, David Eriksson, Sam Daulton, Martin Jankowiak, Sam Stanton, Zitong Zhou, David Arbour, Karthik Rajkumar, Bram Wallace, Jared Frank, and many more!
Acknowledgements
Development of GPyTorch is supported by funding from the Bill and Melinda Gates Foundation, the National Science Foundation, SAP, the Simons Foundation, and the Gatsby Charitable Trust.
License
GPyTorch is MIT licensed.
Top Related Projects
Tensors and Dynamic neural networks in Python with strong GPU acceleration
scikit-learn: machine learning in Python
An Open Source Machine Learning Framework for Everyone
Gaussian processes in TensorFlow
Deep universal probabilistic programming with Python and PyTorch
Deep Learning for humans
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot