From 8a140f4ab65806e07ebbe738bdba1f3c7d672ca5 Mon Sep 17 00:00:00 2001 From: kshitij12345 Date: Thu, 27 Jul 2023 09:36:36 +0000 Subject: [PATCH 1/5] don't fail if optim name doesn't exist --- src/neptune_fastai/impl/__init__.py | 4 +++- tests/neptune_fastai/test_e2e.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/neptune_fastai/impl/__init__.py b/src/neptune_fastai/impl/__init__.py index abe784c..7815bb5 100644 --- a/src/neptune_fastai/impl/__init__.py +++ b/src/neptune_fastai/impl/__init__.py @@ -154,7 +154,9 @@ def _batch_size(self) -> int: @property def _optimizer_name(self) -> Optional[str]: - return self.opt_func.__name__ + optim_name = getattr(self.opt_func, "__name__", "NA") + warnings.warn("NeptuneCallback: Couldn't retrieve the optimizer name, so it will not be logged.") + return optim_name @property def _device(self) -> str: diff --git a/tests/neptune_fastai/test_e2e.py b/tests/neptune_fastai/test_e2e.py index 8f06735..f8baf89 100644 --- a/tests/neptune_fastai/test_e2e.py +++ b/tests/neptune_fastai/test_e2e.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from functools import partial from itertools import islice from pathlib import Path @@ -24,6 +25,7 @@ untar_data, ) from fastai.callback.all import SaveModelCallback +from fastai.optimizer import Adam from fastai.tabular.all import ( Categorify, FillMissing, @@ -71,12 +73,15 @@ def test_vision_classification_with_handler(self): device=torch.device("cpu"), ) + opt_func = partial(Adam, lr=3e-3, wd=0.01) + learn = cnn_learner( dls, squeezenet1_0, metrics=error_rate, cbs=[NeptuneCallback(run, "experiment")], pretrained=False, + opt_func=opt_func, ) learn.fit(1) @@ -91,6 +96,7 @@ def test_vision_classification_with_handler(self): exp_config = run["experiment/config"].fetch() assert exp_config["batch_size"] == 64 assert exp_config["criterion"] == "CrossEntropyLoss()" + assert exp_config["optimizer"]["name"] == "NA" assert exp_config["input_shape"] == {"x": "[3, 224, 224]", "y": 1} # and From 3bdda49d94a0b8444d098ad8c2c28fbc990fd5e4 Mon Sep 17 00:00:00 2001 From: kshitij12345 Date: Thu, 27 Jul 2023 09:37:58 +0000 Subject: [PATCH 2/5] update warning msg --- src/neptune_fastai/impl/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neptune_fastai/impl/__init__.py b/src/neptune_fastai/impl/__init__.py index 7815bb5..cd39b97 100644 --- a/src/neptune_fastai/impl/__init__.py +++ b/src/neptune_fastai/impl/__init__.py @@ -155,7 +155,7 @@ def _batch_size(self) -> int: @property def _optimizer_name(self) -> Optional[str]: optim_name = getattr(self.opt_func, "__name__", "NA") - warnings.warn("NeptuneCallback: Couldn't retrieve the optimizer name, so it will not be logged.") + warnings.warn("NeptuneCallback: Couldn't retrieve the optimizer name, so it will logged as 'NA'.") return optim_name @property From 5b63751261d06467c4dc94da0cf432bcc234f62c Mon Sep 17 00:00:00 2001 From: Kshiteej K Date: Thu, 27 Jul 2023 16:07:52 +0530 Subject: [PATCH 3/5] update CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15e2957..2e9205b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## neptune-fastai 1.1.1 + +### Fixes +- Don't error if `optim.__name__` is not present. (https://github.com/neptune-ai/neptune-fastai/pull/54) + ## neptune-fastai 1.1.0 ### Changes From 7135fd167e7b3307a9699f2c90f85696cdd5512a Mon Sep 17 00:00:00 2001 From: kshitij12345 Date: Thu, 27 Jul 2023 11:24:17 +0000 Subject: [PATCH 4/5] address review --- src/neptune_fastai/impl/__init__.py | 10 ++++++++-- tests/neptune_fastai/test_e2e.py | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/neptune_fastai/impl/__init__.py b/src/neptune_fastai/impl/__init__.py index cd39b97..2ca1a31 100644 --- a/src/neptune_fastai/impl/__init__.py +++ b/src/neptune_fastai/impl/__init__.py @@ -154,8 +154,14 @@ def _batch_size(self) -> int: @property def _optimizer_name(self) -> Optional[str]: - optim_name = getattr(self.opt_func, "__name__", "NA") - warnings.warn("NeptuneCallback: Couldn't retrieve the optimizer name, so it will logged as 'NA'.") + optim_name = getattr(self.opt_func, "__name__", "N/A") + warning_msg = ( + "NeptuneCallback: Couldn't retrieve the optimizer name, " + "so it will be logged as 'N/A'. You can set the optimizer " + "name by assigning it to the __name__ attribute. " + "Eg. >>> optimizer.__name__ = 'NAME'" + ) + warnings.warn(warning_msg) return optim_name @property diff --git a/tests/neptune_fastai/test_e2e.py b/tests/neptune_fastai/test_e2e.py index f8baf89..e6b0c49 100644 --- a/tests/neptune_fastai/test_e2e.py +++ b/tests/neptune_fastai/test_e2e.py @@ -96,7 +96,7 @@ def test_vision_classification_with_handler(self): exp_config = run["experiment/config"].fetch() assert exp_config["batch_size"] == 64 assert exp_config["criterion"] == "CrossEntropyLoss()" - assert exp_config["optimizer"]["name"] == "NA" + assert exp_config["optimizer"]["name"] == "N/A" assert exp_config["input_shape"] == {"x": "[3, 224, 224]", "y": 1} # and From de5ec0429724a3dacb1eaa20d10b82f2225837a2 Mon Sep 17 00:00:00 2001 From: Kshiteej K Date: Thu, 27 Jul 2023 19:29:08 +0530 Subject: [PATCH 5/5] warning only for na --- src/neptune_fastai/impl/__init__.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/neptune_fastai/impl/__init__.py b/src/neptune_fastai/impl/__init__.py index 2ca1a31..7107b7c 100644 --- a/src/neptune_fastai/impl/__init__.py +++ b/src/neptune_fastai/impl/__init__.py @@ -154,14 +154,16 @@ def _batch_size(self) -> int: @property def _optimizer_name(self) -> Optional[str]: - optim_name = getattr(self.opt_func, "__name__", "N/A") - warning_msg = ( - "NeptuneCallback: Couldn't retrieve the optimizer name, " - "so it will be logged as 'N/A'. You can set the optimizer " - "name by assigning it to the __name__ attribute. " - "Eg. >>> optimizer.__name__ = 'NAME'" - ) - warnings.warn(warning_msg) + NA = "N/A" + optim_name = getattr(self.opt_func, "__name__", NA) + if optim_name == NA: + warning_msg = ( + "NeptuneCallback: Couldn't retrieve the optimizer name, " + "so it will be logged as 'N/A'. You can set the optimizer " + "name by assigning it to the __name__ attribute. " + "Eg. >>> optimizer.__name__ = 'NAME'" + ) + warnings.warn(warning_msg) return optim_name @property