mirror of
https://github.com/vale981/ray
synced 2025-03-08 11:31:40 -05:00
[tune] Fix _SafeFallbackEncoder type checks (#4238)
* Fix numpy type checks for _SafeFallbackEncoder * Format changes * Fix usage of nan_str in _SafeFallbackEncoder
This commit is contained in:
parent
27cd6ea401
commit
2a046116ce
1 changed files with 15 additions and 5 deletions
|
@ -5,10 +5,12 @@ from __future__ import print_function
|
|||
import csv
|
||||
import json
|
||||
import logging
|
||||
import numpy as np
|
||||
import os
|
||||
import yaml
|
||||
import distutils.version
|
||||
import numbers
|
||||
|
||||
import numpy as np
|
||||
|
||||
import ray.cloudpickle as cloudpickle
|
||||
from ray.tune.log_sync import get_syncer
|
||||
|
@ -255,11 +257,19 @@ class _SafeFallbackEncoder(json.JSONEncoder):
|
|||
def default(self, value):
|
||||
try:
|
||||
if np.isnan(value):
|
||||
return None
|
||||
if np.issubdtype(value, float):
|
||||
return float(value)
|
||||
if np.issubdtype(value, int):
|
||||
return self.nan_str
|
||||
|
||||
if (type(value).__module__ == np.__name__
|
||||
and isinstance(value, np.ndarray)):
|
||||
return value.tolist()
|
||||
|
||||
if issubclass(type(value), numbers.Integral):
|
||||
return int(value)
|
||||
if issubclass(type(value), numbers.Number):
|
||||
return float(value)
|
||||
|
||||
return super(_SafeFallbackEncoder, self).default(value)
|
||||
|
||||
except Exception:
|
||||
return str(value) # give up, just stringify it (ok for logs)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue