{ "cells": [ { "cell_type": "markdown", "id": "f5c4abc0", "metadata": {}, "source": [ "# Intel® Extension for Scikit-learn Logistic Regression for Cifar dataset" ] }, { "cell_type": "code", "execution_count": 1, "id": "23512089", "metadata": {}, "outputs": [], "source": [ "from timeit import default_timer as timer\n", "from sklearn import metrics\n", "from sklearn.model_selection import train_test_split\n", "import warnings\n", "from IPython.display import HTML\n", "\n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "markdown", "id": "fbb52aca", "metadata": {}, "source": [ "### Download the data" ] }, { "cell_type": "code", "execution_count": 2, "id": "27b99b44", "metadata": {}, "outputs": [], "source": [ "from sklearn.datasets import fetch_openml\n", "\n", "x, y = fetch_openml(name=\"CIFAR-100\", return_X_y=True)" ] }, { "cell_type": "markdown", "id": "bc8ba7c8", "metadata": {}, "source": [ "Split the data into train and test sets" ] }, { "cell_type": "code", "execution_count": 3, "id": "96e14dd7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((54000, 3072), (6000, 3072), (54000,))" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=43)\n", "x_train.shape, x_test.shape, y_train.shape" ] }, { "cell_type": "markdown", "id": "0341cac9", "metadata": {}, "source": [ "### Patch original Scikit-learn with Intel® Extension for Scikit-learn\n", "Intel® Extension for Scikit-learn (previously known as daal4py) contains drop-in replacement functionality for the stock Scikit-learn package. You can take advantage of the performance optimizations of Intel® Extension for Scikit-learn by adding just two lines of code before the usual Scikit-learn imports:" ] }, { "cell_type": "code", "execution_count": 4, "id": "244c5bc9", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Intel(R) Extension for Scikit-learn* enabled (https://github.com/uxlfoundation/scikit-learn-intelex)\n" ] } ], "source": [ "from sklearnex import patch_sklearn\n", "\n", "patch_sklearn()" ] }, { "cell_type": "markdown", "id": "6bb14ac8", "metadata": {}, "source": [ "Intel® Extension for Scikit-learn patching affects performance of specific Scikit-learn functionality. Refer to the [list of supported algorithms and parameters](https://uxlfoundation.github.io/scikit-learn-intelex/latest/algorithms.html) for details. In cases when unsupported parameters are used, the package fallbacks into original Scikit-learn. If the patching does not cover your scenarios, [submit an issue on GitHub](https://github.com/uxlfoundation/scikit-learn-intelex/issues)." ] }, { "cell_type": "markdown", "id": "693b4e26", "metadata": {}, "source": [ "Training of the Logistic Regression algorithm with Intel® Extension for Scikit-learn for CIFAR dataset" ] }, { "cell_type": "code", "execution_count": 5, "id": "e9b8f06b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Intel® extension for Scikit-learn time: 24.82 s'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.linear_model import LogisticRegression\n", "\n", "params = {\n", " \"C\": 0.1,\n", " \"solver\": \"lbfgs\",\n", " \"multi_class\": \"multinomial\",\n", " \"n_jobs\": -1,\n", "}\n", "start = timer()\n", "classifier = LogisticRegression(**params).fit(x_train, y_train)\n", "train_patched = timer() - start\n", "f\"Intel® extension for Scikit-learn time: {train_patched:.2f} s\"" ] }, { "cell_type": "markdown", "id": "d01cdabc", "metadata": {}, "source": [ "Predict probability and get a result of the Logistic Regression algorithm with Intel® Extension for Scikit-learn" ] }, { "cell_type": "code", "execution_count": 6, "id": "9ead2a44", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Intel® extension for Scikit-learn Log Loss: 3.7073530800931587 s'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_predict = classifier.predict_proba(x_test)\n", "log_loss_opt = metrics.log_loss(y_test, y_predict)\n", "f\"Intel® extension for Scikit-learn Log Loss: {log_loss_opt} s\"" ] }, { "cell_type": "markdown", "id": "bd8e7b0b", "metadata": {}, "source": [ "### Train the same algorithm with original Scikit-learn\n", "In order to cancel optimizations, we use *unpatch_sklearn* and reimport the class LogisticRegression" ] }, { "cell_type": "code", "execution_count": 7, "id": "5bb884d5", "metadata": {}, "outputs": [], "source": [ "from sklearnex import unpatch_sklearn\n", "\n", "unpatch_sklearn()" ] }, { "cell_type": "markdown", "id": "8cfa0dba", "metadata": {}, "source": [ "Training of the Logistic Regression algorithm with original Scikit-learn library for CIFAR dataset" ] }, { "cell_type": "code", "execution_count": 8, "id": "ae421d8e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Original Scikit-learn time: 395.03 s'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.linear_model import LogisticRegression\n", "\n", "start = timer()\n", "classifier = LogisticRegression(**params).fit(x_train, y_train)\n", "train_unpatched = timer() - start\n", "f\"Original Scikit-learn time: {train_unpatched:.2f} s\"" ] }, { "cell_type": "markdown", "id": "2d38dfb5", "metadata": {}, "source": [ "Predict probability and get a result of the Logistic Regression algorithm with original Scikit-learn" ] }, { "cell_type": "code", "execution_count": 9, "id": "7644999d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Original Scikit-learn Log Loss: 3.7140870590578428 s'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_predict = classifier.predict_proba(x_test)\n", "log_loss_original = metrics.log_loss(y_test, y_predict)\n", "f\"Original Scikit-learn Log Loss: {log_loss_original} s\"" ] }, { "cell_type": "code", "execution_count": 10, "id": "b7d17e2f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "

Compare Log Loss metric of patched Scikit-learn and original

Log Loss metric of patched Scikit-learn: 3.7073530800931587
Log Loss metric of unpatched Scikit-learn: 3.7140870590578428
Metrics ratio: 0.9981869086917978

With Scikit-learn-intelex patching you can:

" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "HTML(\n", " f\"

Compare Log Loss metric of patched Scikit-learn and original

\"\n", " f\"Log Loss metric of patched Scikit-learn: {log_loss_opt}
\"\n", " f\"Log Loss metric of unpatched Scikit-learn: {log_loss_original}
\"\n", " f\"Metrics ratio: {log_loss_opt/log_loss_original}
\"\n", " f\"

With Scikit-learn-intelex patching you can:

\"\n", " f\"\"\n", ")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.12" } }, "nbformat": 4, "nbformat_minor": 5 }