mirror of
https://github.com/wassname/ray.git
synced 2026-07-17 11:32:33 +08:00
[RLlib] Issue 12789: RLlib throws the warning "The given NumPy array is not writeable" (#12793)
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user