{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Saving and loading an OPU for inference" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A common need among users is being able to apply the same pipeline in production, after training and testing the algorithm. In this use case, it is common to save the model and the transformation, and load them later for reuse. The `OPU` and `OPUMap` objects are no exception, and can be pickled and unpickled as any Python object. When loaded, they will maintain the properties they had at save time." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `lightonml.OPU`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import torch" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from lightonml import OPU" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "train_data = np.random.randint(0, 2, (3000, 1000), dtype=np.uint8)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "opu = OPU(n_components=10000)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "opu.fit1d(train_data)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "train_y = opu.transform(train_data)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'exposure_us': 400,\n", " 'frametime_us': 500,\n", " 'output_roi': ((0, 512), (2040, 64)),\n", " 'start': datetime.datetime(2020, 10, 9, 13, 29, 31, 971721),\n", " 'gain_dB': 0.0,\n", " 'end': datetime.datetime(2020, 10, 9, 13, 29, 33, 626370),\n", " 'input_roi': ((0, 0), (912, 1140)),\n", " 'n_ones': 518723,\n", " 'fmt_type': 'lined',\n", " 'fmt_factor': 1039}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "train_y.context.as_dict()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import pickle" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "with open(\"opu.pkl\", \"wb\") as f:\n", " pickle.dump(opu, f)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "with open(\"opu.pkl\", \"rb\") as f:\n", " opu = pickle.load(f)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "test_data = np.random.randint(0, 2, (3000, 1000), dtype=np.uint8)\n", "test_y = opu.transform(test_data)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'exposure_us': 400,\n", " 'frametime_us': 500,\n", " 'output_roi': ((0, 512), (2040, 64)),\n", " 'start': datetime.datetime(2020, 10, 9, 13, 29, 36, 892864),\n", " 'gain_dB': 0.0,\n", " 'end': datetime.datetime(2020, 10, 9, 13, 29, 38, 536101),\n", " 'input_roi': ((0, 0), (912, 1140)),\n", " 'n_ones': 518723,\n", " 'fmt_type': 'lined',\n", " 'fmt_factor': 1039}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_y.context.as_dict()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly for the `scikit-learn` wrapper:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `lightonml.projections.sklearn.OPUMap`" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/iacolippo/data/miniconda3/envs/dev/lib/python3.8/site-packages/sklearn/base.py:209: FutureWarning: From version 0.24, get_params will raise an AttributeError if a parameter cannot be retrieved as an instance attribute. Previously it would return None.\n", " warnings.warn('From version 0.24, get_params will raise an '\n" ] }, { "data": { "text/plain": [ "OPUMap(n_components=10000, opu=,\n", " verbose_level=None)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from lightonml.projections.sklearn import OPUMap\n", "mapping = OPUMap(opu=opu, n_components=10000)\n", "mapping.fit(train_data)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mapping.fitted" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "with open(\"mapping.pkl\", \"wb\") as f:\n", " pickle.dump(mapping, f)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "with open(\"mapping.pkl\", \"rb\") as f:\n", " mapping = pickle.load(f)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mapping.fitted" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "mapping.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And for the `Pytorch` wrapper" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OPU output is detached from the computational graph.\n" ] } ], "source": [ "from lightonml.projections.torch import OPUMap\n", "mapping = OPUMap(n_components=10000)\n", "mapping.fit(torch.from_numpy(train_data))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mapping.fitted" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "with open(\"mapping.pkl\", \"wb\") as f:\n", " pickle.dump(mapping, f)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "with open(\"mapping.pkl\", \"rb\") as f:\n", " mapping = pickle.load(f)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mapping.fitted" ] } ], "metadata": { "kernelspec": { "display_name": "dev", "language": "python", "name": "dev" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }