浏览代码

[bug-fix] Use float64 when converting np.ndarray to torch.tensor, cap Torch version to 1.7.x (#4610)

* Use float64 in GAIL tests

* Use float32 when converting np arrays by default

* Enforce torch 1.7.x or below

* Add comment about Windows install

* Adjust tests
/MLA-1734-demo-provider
GitHub 4 年前
当前提交
64e998a2
共有 6 个文件被更改,包括 12 次插入9 次删除
  1. 2
      docs/Installation.md
  2. 2
      ml-agents/mlagents/trainers/tests/torch/test_reward_providers/test_gail.py
  3. 6
      ml-agents/mlagents/trainers/tests/torch/test_reward_providers/utils.py
  4. 4
      ml-agents/mlagents/trainers/tests/torch/test_utils.py
  5. 2
      ml-agents/mlagents/trainers/torch/utils.py
  6. 5
      ml-agents/setup.py

2
docs/Installation.md


installing ML-Agents. Activate your virtual environment and run from the command line:
```sh
pip3 install torch -f https://download.pytorch.org/whl/torch_stable.html
pip3 install torch==1.7.0 -f https://download.pytorch.org/whl/torch_stable.html
```
Note that on Windows, you may also need Microsoft's

2
ml-agents/mlagents/trainers/tests/torch/test_reward_providers/test_gail.py


RewardSignalType.GAIL, behavior_spec, gail_settings
)
for _ in range(200):
for _ in range(300):
gail_rp.update(buffer_policy)
reward_expert = gail_rp.evaluate(buffer_expert)[0]
reward_policy = gail_rp.evaluate(buffer_policy)[0]

6
ml-agents/mlagents/trainers/tests/torch/test_reward_providers/utils.py


) -> AgentBuffer:
buffer = AgentBuffer()
curr_observations = [
np.random.normal(size=shape) for shape in behavior_spec.observation_shapes
np.random.normal(size=shape).astype(np.float32)
for shape in behavior_spec.observation_shapes
np.random.normal(size=shape) for shape in behavior_spec.observation_shapes
np.random.normal(size=shape).astype(np.float32)
for shape in behavior_spec.observation_shapes
]
action = behavior_spec.action_spec.random_action(1)[0, :]
for _ in range(number):

4
ml-agents/mlagents/trainers/tests/torch/test_utils.py


def test_list_to_tensor():
# Test converting pure list
unconverted_list = [[1, 2], [1, 3], [1, 4]]
unconverted_list = [[1.0, 2], [1, 3], [1, 4]]
tensor = ModelUtils.list_to_tensor(unconverted_list)
# Should be equivalent to torch.tensor conversion
assert torch.equal(tensor, torch.tensor(unconverted_list))

list_of_np = [np.asarray(_el) for _el in unconverted_list]
tensor = ModelUtils.list_to_tensor(list_of_np)
# Should be equivalent to torch.tensor conversion
assert torch.equal(tensor, torch.tensor(unconverted_list))
assert torch.equal(tensor, torch.tensor(unconverted_list, dtype=torch.float32))
def test_break_into_branches():

2
ml-agents/mlagents/trainers/torch/utils.py


@staticmethod
def list_to_tensor(
ndarray_list: List[np.ndarray], dtype: Optional[torch.dtype] = None
ndarray_list: List[np.ndarray], dtype: Optional[torch.dtype] = torch.float32
) -> torch.Tensor:
"""
Converts a list of numpy arrays into a tensor. MUCH faster than

5
ml-agents/setup.py


"Pillow>=4.2.1",
"protobuf>=3.6",
"pyyaml>=3.1.0",
# Windows ver. of PyTorch doesn't work from PyPi
'torch>=1.6.0;platform_system!="Windows"',
# Windows ver. of PyTorch doesn't work from PyPi. Installation:
# https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Installation.md#windows-installing-pytorch
'torch>=1.6.0,<1.8.0;platform_system!="Windows"',
"tensorboard>=1.15",
"cattrs>=1.0.0",
"attrs>=19.3.0",

正在加载...
取消
保存