garage.misc.tensor_utils module

Utiliy functions for tensors.

concat_tensor_dict_list(tensor_dict_list)[source]

Concatenate dictionary of list of tensor.

Parameters:tensor_dict_list (dict[list]) – a list of dictionaries of {tensors or dictionary of tensors}.
Returns:
a dictionary of {stacked tensors or dictionary of
stacked tensors}
Return type:dict
discount_cumsum(x, discount)[source]

Discounted cumulative sum.

See https://docs.scipy.org/doc/scipy/reference/tutorial/signal.html#difference-equation-filtering # noqa: E501 Here, we have y[t] - discount*y[t+1] = x[t] or rev(y)[t] - discount*rev(y)[t-1] = rev(x)[t]

Parameters:
  • x (np.ndarrary) – Input.
  • discount (float) – Discount factor.
Returns:

Discounted cumulative sum.

Return type:

float

explained_variance_1d(ypred, y)[source]

Explained variation for 1D inputs.

It is the proportion of the variance in one variable that is explained or predicted from another variable.

Parameters:
  • ypred (np.ndarray) – Sample data from the first variable.
  • y (np.ndarray) – Sample data from the second variable.
Returns:

The explained variance.

Return type:

float

flatten_tensors(tensors)[source]

Flatten a list of tensors.

Parameters:tensors (list[numpy.ndarray]) – List of tensors to be flattened.
Returns:Flattened tensors.
Return type:numpy.ndarray
normalize_pixel_batch(env_spec, observations)[source]

Normalize the observations (images).

If the input are images, it normalized into range [0, 1].

Parameters:
  • env_spec (garage.envs.EnvSpec) – Environment specification.
  • observations (numpy.ndarray) – Observations from environment.
Returns:

Normalized observations.

Return type:

numpy.ndarray

pad_tensor(x, max_len, mode='zero')[source]

Pad tensors.

Parameters:
  • x (numpy.ndarray) – Tensors to be padded.
  • max_len (int) – Maximum length.
  • mode (str) – If ‘last’, pad with the last element, otherwise pad with 0.
Returns:

Padded tensor.

Return type:

numpy.ndarray

pad_tensor_dict(tensor_dict, max_len, mode='zero')[source]

Pad dictionary of tensors.

Parameters:
  • tensor_dict (dict[numpy.ndarray]) – Tensors to be padded.
  • max_len (int) – Maximum length.
  • mode (str) – If ‘last’, pad with the last element, otherwise pad with 0.
Returns:

Padded tensor.

Return type:

dict[numpy.ndarray]

pad_tensor_n(xs, max_len)[source]

Pad array of tensors.

Parameters:
  • xs (numpy.ndarray) – Tensors to be padded.
  • max_len (int) – Maximum length.
Returns:

Padded tensor.

Return type:

numpy.ndarray

split_tensor_dict_list(tensor_dict)[source]

Split dictionary of list of tensor.

Parameters:tensor_dict (dict[numpy.ndarray]) – a dictionary of {tensors or dictionary of tensors}.
Returns:
a dictionary of {stacked tensors or dictionary of
stacked tensors}
Return type:dict
stack_tensor_dict_list(tensor_dict_list)[source]

Stack a list of dictionaries of {tensors or dictionary of tensors}.

Parameters:tensor_dict_list (dict[list]) – a list of dictionaries of {tensors or dictionary of tensors}.
Returns:
a dictionary of {stacked tensors or dictionary of
stacked tensors}
Return type:dict
truncate_tensor_dict(tensor_dict, truncated_len)[source]

Truncate dictionary of list of tensor.

Parameters:
  • tensor_dict (dict[numpy.ndarray]) – a dictionary of {tensors or dictionary of tensors}.
  • truncated_len (int) – Length to truncate.
Returns:

a dictionary of {stacked tensors or dictionary of

stacked tensors}

Return type:

dict

unflatten_tensors(flattened, tensor_shapes)[source]

Unflatten a flattened tensors into a list of tensors.

Parameters:
  • flattened (numpy.ndarray) – Flattened tensors.
  • tensor_shapes (tuple) – Tensor shapes.
Returns:

Unflattened list of tensors.

Return type:

list[numpy.ndarray]