Skip to content

Commit 75f8a2b

Browse files
author
Ervin T
authored
[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
1 parent aa61eca commit 75f8a2b

File tree

6 files changed

+12
-9
lines changed

6 files changed

+12
-9
lines changed

docs/Installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ On Windows, you'll have to install the PyTorch package separately prior to
125125
installing ML-Agents. Activate your virtual environment and run from the command line:
126126

127127
```sh
128-
pip3 install torch -f https://download.pytorch.org/whl/torch_stable.html
128+
pip3 install torch==1.7.0 -f https://download.pytorch.org/whl/torch_stable.html
129129
```
130130

131131
Note that on Windows, you may also need Microsoft's

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def test_reward_decreases_vail(
128128
RewardSignalType.GAIL, behavior_spec, gail_settings
129129
)
130130

131-
for _ in range(200):
131+
for _ in range(300):
132132
gail_rp.update(buffer_policy)
133133
reward_expert = gail_rp.evaluate(buffer_expert)[0]
134134
reward_policy = gail_rp.evaluate(buffer_policy)[0]

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ def create_agent_buffer(
99
) -> AgentBuffer:
1010
buffer = AgentBuffer()
1111
curr_observations = [
12-
np.random.normal(size=shape) for shape in behavior_spec.observation_shapes
12+
np.random.normal(size=shape).astype(np.float32)
13+
for shape in behavior_spec.observation_shapes
1314
]
1415
next_observations = [
15-
np.random.normal(size=shape) for shape in behavior_spec.observation_shapes
16+
np.random.normal(size=shape).astype(np.float32)
17+
for shape in behavior_spec.observation_shapes
1618
]
1719
action = behavior_spec.action_spec.random_action(1)[0, :]
1820
for _ in range(number):

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def test_polynomial_decay():
101101

102102
def test_list_to_tensor():
103103
# Test converting pure list
104-
unconverted_list = [[1, 2], [1, 3], [1, 4]]
104+
unconverted_list = [[1.0, 2], [1, 3], [1, 4]]
105105
tensor = ModelUtils.list_to_tensor(unconverted_list)
106106
# Should be equivalent to torch.tensor conversion
107107
assert torch.equal(tensor, torch.tensor(unconverted_list))
@@ -116,7 +116,7 @@ def test_list_to_tensor():
116116
list_of_np = [np.asarray(_el) for _el in unconverted_list]
117117
tensor = ModelUtils.list_to_tensor(list_of_np)
118118
# Should be equivalent to torch.tensor conversion
119-
assert torch.equal(tensor, torch.tensor(unconverted_list))
119+
assert torch.equal(tensor, torch.tensor(unconverted_list, dtype=torch.float32))
120120

121121

122122
def test_break_into_branches():

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def create_input_processors(
194194

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

ml-agents/setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ def run(self):
6363
"Pillow>=4.2.1",
6464
"protobuf>=3.6",
6565
"pyyaml>=3.1.0",
66-
# Windows ver. of PyTorch doesn't work from PyPi
67-
'torch>=1.6.0;platform_system!="Windows"',
66+
# Windows ver. of PyTorch doesn't work from PyPi. Installation:
67+
# https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Installation.md#windows-installing-pytorch
68+
'torch>=1.6.0,<1.8.0;platform_system!="Windows"',
6869
"tensorboard>=1.15",
6970
"cattrs>=1.0.0",
7071
"attrs>=19.3.0",

0 commit comments

Comments
 (0)