From 8b60d8a53550e9cd3870e92ff4e5835e785a3e4a Mon Sep 17 00:00:00 2001 From: Arun Maiya Date: Tue, 4 Jun 2019 22:13:23 -0400 Subject: [PATCH] fixed set_weight_decay --- CHANGELOG.md | 12 +++++++ ktrain/core.py | 3 ++ ktrain/version.py | 2 +- tutorial-A1-additional-tricks.ipynb | 51 ++++++++++++++++------------- 4 files changed, 44 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93b8d8a7a..eb18b743e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,18 @@ Most recent releases are shown at the top. Each release shows: - **Changed**: Additional parameters, changes to inputs or outputs, etc - **Fixed**: Bug fixes that don't change documented behaviour +## 0.1.8 (2019-06-04) + +### New: +- N/A + +### Changed: +- N/A + +### Fixed: +- ```Learner.set_weight_decay``` now works correctly + + ## 0.1.7 (2019-05-24) ### New: diff --git a/ktrain/core.py b/ktrain/core.py index 66a046d68..d1c096962 100644 --- a/ktrain/core.py +++ b/ktrain/core.py @@ -152,8 +152,11 @@ def set_weight_decay(self, wd=0.005): for layer in self.model.layers: if hasattr(layer, 'kernel_regularizer'): layer.kernel_regularizer= regularizers.l2(wd) + layer.add_loss(regularizers.l2(wd)(layer.kernel)) if hasattr(layer, 'bias_regularizer'): layer.bias_regularizer= regularizers.l2(wd) + layer.add_loss(regularizers.l2(wd)(layer.bias)) + self._recompile() return diff --git a/ktrain/version.py b/ktrain/version.py index c6d6737b5..bbbfaa856 100644 --- a/ktrain/version.py +++ b/ktrain/version.py @@ -1,2 +1,2 @@ __all__ = ['__version__'] -__version__ = '0.1.7' +__version__ = '0.1.8' diff --git a/tutorial-A1-additional-tricks.ipynb b/tutorial-A1-additional-tricks.ipynb index 9dc32225f..998f2b323 100644 --- a/tutorial-A1-additional-tricks.ipynb +++ b/tutorial-A1-additional-tricks.ipynb @@ -99,9 +99,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "0 (trainable=True) : \n", - "1 (trainable=True) : \n", - "2 (trainable=True) : \n" + "0 (trainable=True) : \n", + "1 (trainable=True) : \n", + "2 (trainable=True) : \n" ] } ], @@ -198,9 +198,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "0 (trainable=True) : \n", - "1 (trainable=True) : \n", - "2 (trainable=True, wd=None) : \n" + "0 (trainable=True) : \n", + "1 (trainable=True) : \n", + "2 (trainable=True, wd=None) : \n" ] } ], @@ -214,7 +214,7 @@ "metadata": {}, "outputs": [], "source": [ - "learner.set_weight_decay(0.1)" + "learner.set_weight_decay(1e-3)" ] }, { @@ -225,7 +225,7 @@ { "data": { "text/plain": [ - "[array(0.1, dtype=float32)]" + "[array(0.001, dtype=float32)]" ] }, "execution_count": 7, @@ -246,9 +246,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "0 (trainable=True) : \n", - "1 (trainable=True) : \n", - "2 (trainable=True, wd=0.1) : \n" + "0 (trainable=True) : \n", + "1 (trainable=True) : \n", + "2 (trainable=True, wd=0.001) : \n" ] } ], @@ -258,7 +258,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -267,30 +267,35 @@ "text": [ "\n", "\n", - "begin training using onecycle policy with max lr of 0.005...\n", + "begin training using triangular learning rate policy with max lr of 0.005...\n", "Train on 25000 samples, validate on 25000 samples\n", - "Epoch 1/3\n", - "25000/25000 [==============================] - 6s 251us/step - loss: 0.5552 - acc: 0.7462 - val_loss: 0.3613 - val_acc: 0.8609\n", - "Epoch 2/3\n", - "25000/25000 [==============================] - 6s 244us/step - loss: 0.2686 - acc: 0.8970 - val_loss: 0.2907 - val_acc: 0.8841\n", - "Epoch 3/3\n", - "25000/25000 [==============================] - 6s 246us/step - loss: 0.1738 - acc: 0.9406 - val_loss: 0.2780 - val_acc: 0.8902\n" + "Epoch 1/6\n", + "25000/25000 [==============================] - 7s 270us/step - loss: 0.5387 - acc: 0.7776 - val_loss: 0.4369 - val_acc: 0.8681\n", + "Epoch 2/6\n", + "25000/25000 [==============================] - 6s 255us/step - loss: 0.3808 - acc: 0.8934 - val_loss: 0.3947 - val_acc: 0.8810\n", + "Epoch 3/6\n", + "25000/25000 [==============================] - 6s 254us/step - loss: 0.3363 - acc: 0.9124 - val_loss: 0.3755 - val_acc: 0.8861\n", + "Epoch 4/6\n", + "25000/25000 [==============================] - 6s 254us/step - loss: 0.3081 - acc: 0.9220 - val_loss: 0.3623 - val_acc: 0.8881\n", + "Epoch 5/6\n", + "25000/25000 [==============================] - 6s 257us/step - loss: 0.2867 - acc: 0.9319 - val_loss: 0.3539 - val_acc: 0.8891\n", + "Epoch 6/6\n", + "25000/25000 [==============================] - 6s 257us/step - loss: 0.2703 - acc: 0.9363 - val_loss: 0.3469 - val_acc: 0.8905\n" ] }, { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 10, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "learner.set_model(get_model())\n", - "learner.fit_onecycle(0.005, 3)" + "learner.autofit(0.005, 6)" ] }, {