浏览代码

Adding some tests

/develop/singular-embeddings
vincentpierre 4 年前
当前提交
b7c7d773
共有 6 个文件被更改,包括 63 次插入7 次删除
  1. 2
      ml-agents/mlagents/trainers/tests/dummy_config.py
  2. 9
      ml-agents/mlagents/trainers/tests/simple_test_envs.py
  3. 37
      ml-agents/mlagents/trainers/tests/torch/test_simple_rl.py
  4. 4
      ml-agents/mlagents/trainers/torch/attention.py
  5. 12
      ml-agents/mlagents/trainers/torch/networks.py
  6. 6
      ml-agents/mlagents/trainers/torch/utils.py

2
ml-agents/mlagents/trainers/tests/dummy_config.py


obs_specs: List[ObservationSpec] = []
for shape in shapes:
dim_prop = (DimensionProperty.UNSPECIFIED,) * len(shape)
if len(shape) == 2:
dim_prop = (DimensionProperty.VARIABLE_SIZE, DimensionProperty.NONE)
spec = ObservationSpec(shape, dim_prop, ObservationType.DEFAULT)
obs_specs.append(spec)
return obs_specs

9
ml-agents/mlagents/trainers/tests/simple_test_envs.py


OBS_SIZE = 1
VIS_OBS_SIZE = (20, 20, 3)
VAR_LEN_SIZE = (10, 5)
STEP_SIZE = 0.2
TIME_PENALTY = 0.01

step_size=STEP_SIZE,
num_visual=0,
num_vector=1,
num_var_len=0,
var_len_obs_size=VAR_LEN_SIZE,
self.num_var_len = num_var_len
self.var_len_obs_size = var_len_obs_size
continuous_action_size, discrete_action_size = action_sizes
discrete_tuple = tuple(2 for _ in range(discrete_action_size))
action_spec = ActionSpec(continuous_action_size, discrete_tuple)

obs_shape.append((self.vec_obs_size,))
for _ in range(self.num_visual):
obs_shape.append(self.vis_obs_size)
for _ in range(self.num_var_len):
obs_shape.append(self.var_len_obs_size)
obs_spec = create_observation_specs_with_shapes(obs_shape)
return obs_spec

obs.append(np.ones((1, self.vec_obs_size), dtype=np.float32) * value)
for _ in range(self.num_visual):
obs.append(np.ones((1,) + self.vis_obs_size, dtype=np.float32) * value)
for _ in range(self.num_var_len):
obs.append(np.ones((1,) + self.var_len_obs_size, dtype=np.float32) * value)
return obs
@property

37
ml-agents/mlagents/trainers/tests/torch/test_simple_rl.py


check_environment_trains(env, {BRAIN_NAME: config})
@pytest.mark.parametrize("action_sizes", [(0, 1), (1, 0)])
@pytest.mark.parametrize("num_var_len", [1, 2])
@pytest.mark.parametrize("num_visual", [0, 1])
def test_var_len_obs_ppo(num_visual, num_var_len, action_sizes):
env = SimpleEnvironment(
[BRAIN_NAME],
action_sizes=action_sizes,
num_visual=num_visual,
num_vector=0,
num_var_len=num_var_len,
step_size=0.2,
)
new_hyperparams = attr.evolve(
PPO_TORCH_CONFIG.hyperparameters, learning_rate=3.0e-4
)
config = attr.evolve(PPO_TORCH_CONFIG, hyperparameters=new_hyperparams)
check_environment_trains(env, {BRAIN_NAME: config})
@pytest.mark.parametrize("num_visual", [1, 2])
@pytest.mark.parametrize("vis_encode_type", ["resnet", "nature_cnn", "match3"])
def test_visual_advanced_ppo(vis_encode_type, num_visual):

[BRAIN_NAME],
action_sizes=action_sizes,
num_visual=num_visual,
num_vector=0,
step_size=0.2,
)
new_hyperparams = attr.evolve(
SAC_TORCH_CONFIG.hyperparameters, batch_size=16, learning_rate=3e-4
)
config = attr.evolve(SAC_TORCH_CONFIG, hyperparameters=new_hyperparams)
check_environment_trains(env, {BRAIN_NAME: config})
@pytest.mark.parametrize("action_sizes", [(0, 1), (1, 0)])
@pytest.mark.parametrize("num_var_len", [1, 2])
def test_var_len_obs_sac(num_var_len, action_sizes):
env = SimpleEnvironment(
[BRAIN_NAME],
action_sizes=action_sizes,
num_visual=0,
num_var_len=num_var_len,
num_vector=0,
step_size=0.2,
)

4
ml-agents/mlagents/trainers/torch/attention.py


self.entity_num_max_elements: int = -1
if entity_num_max_elements is not None:
self.entity_num_max_elements = entity_num_max_elements
self.concat_self: bool = concat_self
# If not concatenating self, input to encoder is just entity size
if not concat_self:
self.self_size = 0

)
def forward(self, x_self: torch.Tensor, entities: torch.Tensor) -> torch.Tensor:
if self.concat_self:
if self.self_size > 0:
num_entities = self.entity_num_max_elements
if num_entities < 0:
if exporting_to_onnx.is_exporting():

12
ml-agents/mlagents/trainers/torch/networks.py


encodes.append(processed_obs)
else:
var_len_inputs.append(inputs[idx])
encoded_self = torch.cat(encodes, dim=1)
if len(encodes) == 0:
encoded_self = torch.zeros(0, 0)
else:
encoded_self = torch.cat(encodes, dim=1)
if len(var_len_inputs) > 0:
# Some inputs need to be processed with a variable length encoder
masks = get_zero_entities_mask(var_len_inputs)

embeddings.append(var_len_processor(encoded_self, var_len_input))
qkv = torch.cat(embeddings, dim=1)
attention_embedding = self.rsa(qkv, masks)
encoded_self = torch.cat([encoded_self, attention_embedding], dim=1)
if encoded_self is None:
encoded_self = torch.cat([attention_embedding], dim=1)
else:
encoded_self = torch.cat([encoded_self, attention_embedding], dim=1)
if encoded_self.shape[1] == 0:
if encoded_self is None:
raise Exception("No valid inputs to network.")
# Constants don't work in Barracuda

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


for idx in var_len_indices:
var_encoders.append(
EntityEmbedding(
x_self_size, obs_spec[idx].shape[1], obs_spec[idx].shape[0], h_size
x_self_size,
observation_specs[idx].shape[1],
observation_specs[idx].shape[0],
h_size,
concat_self=True,
)
)
return (nn.ModuleList(encoders), nn.ModuleList(var_encoders), embedding_sizes)

正在加载...
取消
保存