Created
June 9, 2020 17:33
-
-
Save MaxHalford/09ecefc4b09ab4200226bc50a09effac to your computer and use it in GitHub Desktop.
creme mini-batch performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Standard scaling in mini-batches" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Mean" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Batch." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4.95" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import numpy as np\n", | |
"\n", | |
"x = np.random.randint(0, 10, size=20)\n", | |
"x.mean()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Updating with one example at a time." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4.95" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"m = 0\n", | |
"n = 0\n", | |
"\n", | |
"for xi in x:\n", | |
" n += 1\n", | |
" m += (xi - m) / n\n", | |
" \n", | |
"m" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Updating in mini-batches." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4.949999999999999" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"m = 0\n", | |
"n = 0\n", | |
"\n", | |
"for chunk in np.array_split(x, 3):\n", | |
" chunk_mean = chunk.mean()\n", | |
" \n", | |
" n += len(chunk)\n", | |
" m += len(chunk) * (chunk_mean - m) / n\n", | |
" \n", | |
"m" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Variance" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Batch." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"6.1475" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"x.var(ddof=0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"6.471052631578948" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"x.var(ddof=1)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Updating with one example at a time." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"6.471052631578946" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"m = 0\n", | |
"v = 0\n", | |
"n = 0\n", | |
"ddof = 1\n", | |
"\n", | |
"for xi in x:\n", | |
" \n", | |
" prev_m = m\n", | |
" \n", | |
" n += 1\n", | |
" m += (xi - m) / n\n", | |
" \n", | |
" if n > ddof:\n", | |
" v += ((xi - m) * (xi - prev_m) - v) / (n - ddof)\n", | |
" \n", | |
"v" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Updating in mini-batches." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 191, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"7.1275" | |
] | |
}, | |
"execution_count": 191, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"mean = 0\n", | |
"var = 0\n", | |
"m = 0\n", | |
"\n", | |
"for chunk in np.array_split(x, 3):\n", | |
" \n", | |
" new_mean = chunk.mean()\n", | |
" new_var = chunk.var()\n", | |
" \n", | |
" n = len(chunk)\n", | |
" \n", | |
" old_mean = mean\n", | |
" \n", | |
" a = m / (m + n)\n", | |
" b = n / (m + n)\n", | |
" \n", | |
" mean = a * old_mean + b * new_mean\n", | |
" var = (\n", | |
" a * var +\n", | |
" b * new_var +\n", | |
" a * b * (old_mean - new_mean) ** 2\n", | |
" )\n", | |
" \n", | |
" m += n\n", | |
" \n", | |
"var" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Benchmarks" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2.78 s ± 259 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n", | |
"233 µs ± 10.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"import pandas as pd\n", | |
"from creme import preprocessing\n", | |
"\n", | |
"scaler = preprocessing.StandardScaler()\n", | |
"\n", | |
"chunksize = 1000\n", | |
"\n", | |
"%timeit for batch in pd.read_csv('/Users/mhalford/creme_data/CreditCard/creditcard.csv', chunksize=chunksize): scaler.fit_many(batch)\n", | |
"%timeit scaler.transform_many(batch)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"3.23 s ± 186 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n", | |
"659 µs ± 24.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"from sklearn import preprocessing\n", | |
"\n", | |
"sk_scaler = preprocessing.StandardScaler()\n", | |
"\n", | |
"%timeit for batch in pd.read_csv('/Users/mhalford/creme_data/CreditCard/creditcard.csv', chunksize=chunksize): sk_scaler.partial_fit(batch)\n", | |
"%timeit pd.DataFrame(sk_scaler.transform(batch), columns=batch.columns)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.7.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment