Skip to content

Commit 130d611

Browse files
vincentpierreawjuliani
authored andcommitted
[bugfix] summary writer no longer crashes if Hyperparameters could not be written (#4265)
* Bug fix, returnning an empty string in case of error breaks the summary writter * addressing comments
1 parent 8327ddc commit 130d611

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

ml-agents/mlagents/trainers/stats.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections import defaultdict
22
from enum import Enum
3-
from typing import List, Dict, NamedTuple, Any
3+
from typing import List, Dict, NamedTuple, Any, Optional
44
import numpy as np
55
import abc
66
import csv
@@ -211,11 +211,14 @@ def add_property(
211211
) -> None:
212212
if property_type == StatsPropertyType.HYPERPARAMETERS:
213213
assert isinstance(value, dict)
214-
text = self._dict_to_tensorboard("Hyperparameters", value)
214+
summary = self._dict_to_tensorboard("Hyperparameters", value)
215215
self._maybe_create_summary_writer(category)
216-
self.summary_writers[category].add_summary(text, 0)
216+
if summary is not None:
217+
self.summary_writers[category].add_summary(summary, 0)
217218

218-
def _dict_to_tensorboard(self, name: str, input_dict: Dict[str, Any]) -> str:
219+
def _dict_to_tensorboard(
220+
self, name: str, input_dict: Dict[str, Any]
221+
) -> Optional[bytes]:
219222
"""
220223
Convert a dict to a Tensorboard-encoded string.
221224
:param name: The name of the text.
@@ -232,8 +235,10 @@ def _dict_to_tensorboard(self, name: str, input_dict: Dict[str, Any]) -> str:
232235
s = sess.run(s_op)
233236
return s
234237
except Exception:
235-
logger.warning("Could not write text summary for Tensorboard.")
236-
return ""
238+
logger.warning(
239+
f"Could not write {name} summary for Tensorboard: {input_dict}"
240+
)
241+
return None
237242

238243

239244
class CSVWriter(StatsWriter):

0 commit comments

Comments
 (0)