.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/kernel_ridge/plot_model_on_gpu.py.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__auto_examples_kernel_ridge_plot_model_on_gpu.py.py: Fitting a model on GPU ====================== This example demonstrates how to fit a model using GPU computations. Himalaya implements different computational backends to fit the models: - "numpy" (CPU) (default) - "torch" (CPU) - "torch_cuda" (GPU) - "cupy" (GPU) Each backend is only available if you installed the corresponding package with CUDA enabled. Check the ``pytorch``/``cupy`` documentation for installation instructions. .. GENERATED FROM PYTHON SOURCE LINES 20-22 Create a random dataset ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-27 .. code-block:: Python import numpy as np n_samples, n_features, n_targets = 10, 20, 4 X = np.random.randn(n_samples, n_features) Y = np.random.randn(n_samples, n_targets) .. GENERATED FROM PYTHON SOURCE LINES 28-34 Change backend -------------- To change the backend, you need to call the function ``himalaya.backend.set_backend``. With the option ``on_error="warn"``, the function does not raise an error if the new backend fails to be imported, and the backend is kept unchanged. .. GENERATED FROM PYTHON SOURCE LINES 34-38 .. code-block:: Python from himalaya.backend import set_backend backend = set_backend("cupy", on_error="warn") .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/himalaya/himalaya/himalaya/backend/_utils.py:66: UserWarning: Setting backend to cupy failed: Cupy not installed..Falling back to numpy backend. warnings.warn(f"Setting backend to {backend} failed: {str(error)}." .. GENERATED FROM PYTHON SOURCE LINES 39-47 GPU backend ----------- To fit a himalaya model on GPU, you don't need to move the input arrays to GPU, the method ``fit`` will do it for you. However, the float precision will not be changed. To make the most of GPU memory and computational speed, you might want to change the float precision to float32. .. GENERATED FROM PYTHON SOURCE LINES 47-53 .. code-block:: Python X = X.astype("float32") from himalaya.kernel_ridge import KernelRidge model_him = KernelRidge(kernel="linear", alpha=0.1) model_him.fit(X, Y) .. raw:: html
KernelRidge(alpha=0.1)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 54-57 The results are stored in GPU memory, using an array object specific to the backend used. To use the results in other libraries (for example matplotlib), you can create a numpy array using the function ``backend.to_numpy``. .. GENERATED FROM PYTHON SOURCE LINES 57-61 .. code-block:: Python scores = model_him.score(X, Y) print(scores.__class__) scores = backend.to_numpy(scores) print(scores.__class__) .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.005 seconds) .. _sphx_glr_download__auto_examples_kernel_ridge_plot_model_on_gpu.py.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_model_on_gpu.py.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_model_on_gpu.py.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_model_on_gpu.py.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_