Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve last logged val from result by key #3049

Merged
merged 6 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pytorch_lightning/core/step_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def __init__(
}
}

def __getitem__(self, key: Union[str, Any]) -> Any:
try:
return super().__getitem__(key)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.get(key) does not work?

except KeyError:
return super().__getitem__(f'step_{key}')

def __getattr__(self, key: str) -> Any:
try:
if key == 'callback_metrics':
Expand Down
8 changes: 8 additions & 0 deletions tests/core/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,11 @@ def test_result_gather_mixed_types():
expected = [1.2, ["bar", None], torch.tensor(1)]
assert isinstance(result["foo"], list)
assert result["foo"] == expected


def test_result_retrieve_last_logged_item():
result = Result()
result.log('a', 5., on_step=True, on_epoch=True)
assert result['epoch_a'] == 5.
assert result['step_a'] == 5.
assert result['a'] == 5.