Created
March 15, 2016 02:22
-
-
Save EthanRosenthal/f00a8711932818ba9c61 to your computer and use it in GitHub Desktop.
This file contains hidden or 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": "code", | |
"execution_count": 16, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Rate: 1e-05\n", | |
"New optimal hyperparameters\n", | |
"learning_rate 1e-05\n", | |
"model <__main__.ExplicitMF instance at 0x7fa542e4b5f0>\n", | |
"n_iter 200\n", | |
"test_mse 1.006278\n", | |
"train_mse 1.071964\n", | |
"dtype: object\n", | |
"Rate: 0.0001\n", | |
"New optimal hyperparameters\n", | |
"learning_rate 0.0001\n", | |
"model <__main__.ExplicitMF instance at 0x7fa542e67950>\n", | |
"n_iter 200\n", | |
"test_mse 0.8663474\n", | |
"train_mse 0.8787174\n", | |
"dtype: object\n", | |
"Rate: 0.001\n", | |
"New optimal hyperparameters\n", | |
"learning_rate 0.001\n", | |
"model <__main__.ExplicitMF instance at 0x7fa542e67908>\n", | |
"n_iter 100\n", | |
"test_mse 0.8061162\n", | |
"train_mse 0.7461311\n", | |
"dtype: object\n", | |
"Rate: 0.01\n" | |
] | |
} | |
], | |
"source": [ | |
"iter_array = [1, 2, 5, 10, 25, 50, 100, 200]\n", | |
"learning_rates = [1e-5, 1e-4, 1e-3, 1e-2]\n", | |
"\n", | |
"best_params = {}\n", | |
"best_params['learning_rate'] = None\n", | |
"best_params['n_iter'] = 0\n", | |
"best_params['train_mse'] = np.inf\n", | |
"best_params['test_mse'] = np.inf\n", | |
"best_params['model'] = None\n", | |
"\n", | |
"\n", | |
"for rate in learning_rates:\n", | |
" print 'Rate: {}'.format(rate)\n", | |
" MF_SGD = ExplicitMF(train, n_factors=40, learning='sgd')\n", | |
" MF_SGD.calculate_learning_curve(iter_array, test, learning_rate=rate)\n", | |
" min_idx = np.argmin(MF_SGD.test_mse)\n", | |
" if MF_SGD.test_mse[min_idx] < best_params['test_mse']:\n", | |
" best_params['n_iter'] = iter_array[min_idx]\n", | |
" best_params['learning_rate'] = rate\n", | |
" best_params['train_mse'] = MF_SGD.train_mse[min_idx]\n", | |
" best_params['test_mse'] = MF_SGD.test_mse[min_idx]\n", | |
" best_params['model'] = MF_SGD\n", | |
" print 'New optimal hyperparameters'\n", | |
" print pd.Series(best_params)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment