Skip to content

Commit

Permalink
[feat] implement cross validation of softmax classifier #1
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongjae96 committed Feb 6, 2023
1 parent fcc518f commit 0da89ea
Showing 1 changed file with 68 additions and 9 deletions.
77 changes: 68 additions & 9 deletions cs231n_2022/assignment1/softmax.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"id": "434e757f",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"naive loss: 2.386387e+00 computed in 0.042010s\n",
"vectorized loss: 2.386387e+00 computed in 0.004000s\n",
"Loss difference: 0.000000\n",
"Gradient difference: 0.000000\n"
]
}
],
"source": [
"# Now that we have a naive implementation of the softmax loss function and its gradient,\n",
"# implement a vectorized version in softmax_loss_vectorized.\n",
Expand All @@ -308,15 +319,41 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"id": "2e5e374d",
"metadata": {
"tags": [
"code"
],
"test": "tuning"
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"lr 2.000000e-06 reg 1.000000e+03 train accuracy: 0.392286 val accuracy: 0.376000\n",
"lr 2.000000e-06 reg 1.000000e+04 train accuracy: 0.333816 val accuracy: 0.346000\n",
"lr 2.000000e-06 reg 2.000000e+04 train accuracy: 0.299286 val accuracy: 0.317000\n",
"lr 2.000000e-06 reg 2.500000e+04 train accuracy: 0.305898 val accuracy: 0.316000\n",
"lr 2.000000e-06 reg 3.000000e+04 train accuracy: 0.309490 val accuracy: 0.307000\n",
"lr 2.000000e-06 reg 5.000000e+04 train accuracy: 0.280694 val accuracy: 0.283000\n",
"lr 2.500000e-06 reg 1.000000e+03 train accuracy: 0.389408 val accuracy: 0.377000\n",
"lr 2.500000e-06 reg 1.000000e+04 train accuracy: 0.332571 val accuracy: 0.337000\n",
"lr 2.500000e-06 reg 2.000000e+04 train accuracy: 0.315347 val accuracy: 0.338000\n",
"lr 2.500000e-06 reg 2.500000e+04 train accuracy: 0.289531 val accuracy: 0.288000\n",
"lr 2.500000e-06 reg 3.000000e+04 train accuracy: 0.300184 val accuracy: 0.327000\n",
"lr 2.500000e-06 reg 5.000000e+04 train accuracy: 0.269163 val accuracy: 0.267000\n",
"lr 3.000000e-06 reg 1.000000e+03 train accuracy: 0.381061 val accuracy: 0.382000\n",
"lr 3.000000e-06 reg 1.000000e+04 train accuracy: 0.323408 val accuracy: 0.342000\n",
"lr 3.000000e-06 reg 2.000000e+04 train accuracy: 0.274347 val accuracy: 0.293000\n",
"lr 3.000000e-06 reg 2.500000e+04 train accuracy: 0.289388 val accuracy: 0.310000\n",
"lr 3.000000e-06 reg 3.000000e+04 train accuracy: 0.264939 val accuracy: 0.269000\n",
"lr 3.000000e-06 reg 5.000000e+04 train accuracy: 0.264510 val accuracy: 0.279000\n",
"best validation accuracy achieved during cross-validation: 0.382000\n"
]
}
],
"source": [
"# Use the validation set to tune hyperparameters (regularization strength and\n",
"# learning rate). You should experiment with different ranges for the learning\n",
Expand All @@ -336,12 +373,26 @@
"################################################################################\n",
"\n",
"# Provided as a reference. You may or may not want to change these hyperparameters\n",
"learning_rates = [1e-7, 5e-7]\n",
"regularization_strengths = [2.5e4, 5e4]\n",
"learning_rates = [2e-6, 2.5e-6, 3e-6]\n",
"regularization_strengths = [1e3, 1e4, 2e4, 2.5e4, 3e4, 5e4]\n",
"\n",
"# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****\n",
"\n",
"pass\n",
"for lr in learning_rates:\n",
" for rs in regularization_strengths:\n",
" softmax = Softmax()\n",
" softmax.train(X_train, y_train, lr, rs, 1000)\n",
"\n",
" y_train_pred = softmax.predict(X_train)\n",
" y_val_pred = softmax.predict(X_val)\n",
" train_accuracy = np.mean(y_train == y_train_pred)\n",
" val_accuracy = np.mean(y_val == y_val_pred)\n",
"\n",
" if val_accuracy > best_val:\n",
" best_val = val_accuracy\n",
" best_softmax = softmax\n",
"\n",
" results[(lr, rs)] = train_accuracy, val_accuracy\n",
"\n",
"# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****\n",
" \n",
Expand All @@ -356,12 +407,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"id": "deb37cc6",
"metadata": {
"test": "test"
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"softmax on raw pixels final test set accuracy: 0.373000\n"
]
}
],
"source": [
"# evaluate on test set\n",
"# Evaluate the best softmax on test set\n",
Expand Down

0 comments on commit 0da89ea

Please sign in to comment.