def _flush_metrics(self) -> None: """ Flush metrics files """ with open(self._get_file_name(True), 'w') as out: json.dump(self._storage, out) if self._final_metrics_file is not None: res = dict_recursive_bypass(self._storage, lambda v: v[-1]) with open(self._final_metrics_file, 'w') as out: json.dump(res, out)
def test_dict_recursive_bypass(self): d = { 'data': np.array([1]), 'target': { 'a': np.array([1]), 'b': np.array([1]) } } d = dict_recursive_bypass(d, lambda v: torch.from_numpy(v)) self.assertTrue(isinstance(d['data'], Tensor)) self.assertTrue(isinstance(d['target']['a'], Tensor)) self.assertTrue(isinstance(d['target']['b'], Tensor))
def _pass_data_to_device(self, data) -> torch.Tensor or dict: """ Internal method, that pass data to specified device :param data: data as any object type. If will passed to device if it's instance of :class:`torch.Tensor` or dict with key ``data``. Otherwise data will be doesn't changed :return: processed on target device """ if isinstance(data, dict): return dict_recursive_bypass(data, lambda v: v.to(self._device)) elif isinstance(data, torch.Tensor): return data.to(self._device) else: return data