[RLlib] Issue 12789: RLlib throws the warning "The given NumPy array is not writeable" (#12793)

This commit is contained in:
Sven Mika
2020-12-22 09:28:07 -05:00
committed by GitHub
parent ea8d782be1
commit 01faeabc17
+8 -1
View File
@@ -1,6 +1,7 @@
from gym.spaces import Discrete, MultiDiscrete
import numpy as np
import tree
import warnings
from ray.rllib.models.repeated_values import RepeatedValues
from ray.rllib.utils.framework import try_import_torch
@@ -62,7 +63,13 @@ def convert_to_torch_tensor(x, device=None):
return RepeatedValues(
tree.map_structure(mapping, item.values), item.lengths,
item.max_len)
tensor = torch.from_numpy(np.asarray(item))
# Non-writable numpy-arrays will cause PyTorch warning.
if isinstance(item, np.ndarray) and item.flags.writeable is False:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
tensor = torch.from_numpy(item)
else:
tensor = torch.from_numpy(np.asarray(item))
# Floatify all float64 tensors.
if tensor.dtype == torch.double:
tensor = tensor.float()