ding.envs.common.common_function¶
ding.envs.common.common_function
¶
sqrt_one_hot(v, max_val)
¶
Overview
Sqrt the input value v and transform it into one-hot.
Arguments:
- v (:obj:torch.Tensor): the value to be processed with sqrt and one-hot
- max_val (:obj:int): the input v's estimated max value, used to calculate one-hot bit number. v would be clamped by (0, max_val).
Returns:
- ret (:obj:torch.Tensor): the value processed after sqrt and one-hot
div_one_hot(v, max_val, ratio)
¶
Overview
Divide the input value v by ratio and transform it into one-hot.
Arguments:
- v (:obj:torch.Tensor): the value to be processed with divide and one-hot
- max_val (:obj:int): the input v's estimated max value, used to calculate one-hot bit number. v would be clamped by (0, max_val).
- ratio (:obj:int): input v would be divided by ratio
Returns:
- ret (:obj:torch.Tensor): the value processed after divide and one-hot
div_func(inputs, other, unsqueeze_dim=1)
¶
Overview
Divide inputs by other and unsqueeze if needed.
Arguments:
- inputs (:obj:torch.Tensor): the value to be unsqueezed and divided
- other (:obj:float): input would be divided by other
- unsqueeze_dim (:obj:int): the dim to implement unsqueeze
Returns:
- ret (:obj:torch.Tensor): the value processed after unsqueeze and divide
clip_one_hot(v, num)
¶
Overview
Clamp the input v in (0, num-1) and make one-hot mapping.
Arguments:
- v (:obj:torch.Tensor): the value to be processed with clamp and one-hot
- num (:obj:int): number of one-hot bits
Returns:
- ret (:obj:torch.Tensor): the value processed after clamp and one-hot
reorder_one_hot(v, dictionary, num, transform=None)
¶
Overview
Reorder each value in input v according to reorder dict dictionary, then make one-hot mapping
Arguments:
- v (:obj:torch.LongTensor): the original value to be processed with reorder and one-hot
- dictionary (:obj:Dict[int, int]): a reorder lookup dict, map original value to new reordered index starting from 0
- num (:obj:int): number of one-hot bits
- transform (:obj:int): an array to firstly transform the original action to general action
Returns:
- ret (:obj:torch.Tensor): one-hot data indicating reordered index
reorder_one_hot_array(v, array, num, transform=None)
¶
Overview
Reorder each value in input v according to reorder dict dictionary, then make one-hot mapping.
The difference between this function and reorder_one_hot is
whether the type of reorder lookup data structure is np.ndarray or dict.
Arguments:
- v (:obj:torch.LongTensor): the value to be processed with reorder and one-hot
- array (:obj:np.ndarray): a reorder lookup array, map original value to new reordered index starting from 0
- num (:obj:int): number of one-hot bits
- transform (:obj:np.ndarray): an array to firstly transform the original action to general action
Returns:
- ret (:obj:torch.Tensor): one-hot data indicating reordered index
reorder_boolean_vector(v, dictionary, num, transform=None)
¶
Overview
Reorder each value in input v to new index according to reorder dict dictionary,
then set corresponding position in return tensor to 1.
Arguments:
- v (:obj:torch.LongTensor): the value to be processed with reorder
- dictionary (:obj:Dict[int, int]): a reorder lookup dict, map original value to new reordered index starting from 0
- num (:obj:int): total number of items, should equals to max index + 1
- transform (:obj:np.ndarray): an array to firstly transform the original action to general action
Returns:
- ret (:obj:torch.Tensor): boolean data containing only 0 and 1, indicating whether corresponding original value exists in input v
get_to_and(num_bits)
cached
¶
Overview
Get an np.ndarray with num_bits elements, each equals to :math:2^n (n decreases from num_bits-1 to 0).
Used by batch_binary_encode to make bit-wise and.
Arguments:
- num_bits (:obj:int): length of the generating array
Returns:
- to_and (:obj:np.ndarray): an array with num_bits elements, each equals to :math:2^n (n decreases from num_bits-1 to 0)
batch_binary_encode(x, bit_num)
¶
Overview
Big endian binary encode x to float tensor
Arguments:
- x (:obj:torch.Tensor): the value to be unsqueezed and divided
- bit_num (:obj:int): number of bits, should satisfy :math:2^{bit num} > max(x)
Example:
>>> batch_binary_encode(torch.tensor([131,71]), 10)
tensor([[0., 0., 1., 0., 0., 0., 0., 0., 1., 1.],
[0., 0., 0., 1., 0., 0., 0., 1., 1., 1.]])
Returns:
- ret (:obj:torch.Tensor): the binary encoded tensor, containing only 0 and 1
compute_denominator(x)
¶
Overview
Compute the denominator used in get_postion_vector. Divide 1 at the last step, so you can use it as an multiplier.
Arguments:
- x (:obj:torch.Tensor): Input tensor, which is generated from torch.arange(0, d_model).
Returns:
- ret (:obj:torch.Tensor): Denominator result tensor.
get_postion_vector(x)
¶
Overview
Get position embedding used in Transformer, even and odd :math:lpha are stored in POSITION_ARRAY
Arguments:
- x (:obj:list): original position index, whose length should be 32
Returns:
- v (:obj:torch.Tensor): position embedding tensor in 64 dims
affine_transform(data, action_clip=True, alpha=None, beta=None, min_val=None, max_val=None)
¶
Overview
do affine transform for data in range [-1, 1], :math:lpha imes data + eta
Arguments:
- data (:obj:Any): the input data
- action_clip (:obj:bool): whether to do action clip operation ([-1, 1])
- alpha (:obj:float): affine transform weight
- beta (:obj:float): affine transform bias
- min_val (:obj:float): min value, if min_val and max_val are indicated, scale input data to [min_val, max_val]
- max_val (:obj:float): max value
Returns:
- transformed_data (:obj:Any): affine transformed data
save_frames_as_gif(frames, path)
¶
Overview
save frames as gif to a specified path.
Arguments:
- frames (:obj:List): list of frames
- path (:obj:str): the path to save gif
Full Source Code
../ding/envs/common/common_function.py