Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
#!/usr/bin/env python3
import torch as th
import numpy as np
from cherry._utils import EPS
def totensor(array, dtype=None):
Converts the argument `array` to a torch.tensor 1xN, regardless of its
type or dimension.
* **array** (int, float, ndarray, tensor) - Data to be converted to array.
* **dtype** (dtype, *optional*, default=None) - Data type to use for representation.
By default, uses `torch.get_default_dtype()`.
* Tensor of shape 1xN with the appropriate data type.
array = [5, 6, 7.0]
tensor = cherry.totensor(array, dtype=th.float32)
array = np.array(array, dtype=np.float64)
tensor = cherry.totensor(array, dtype=th.float16)
if dtype is None:
dtype = th.get_default_dtype()
if isinstance(array, (list, tuple)):
array =[totensor(x) for x in array], dim=0)
if isinstance(array, int):
array = float(array)
if isinstance(array, float):
array = [array, ]
if isinstance(array, list):
array = np.array(array)
if isinstance(array, (np.ndarray,
if array.dtype == np.bool_:
array = array.astype(np.uint8)
array = th.tensor(array, dtype=dtype)
array = array.unsqueeze(0)
while len(array.shape) < 2:
array = array.unsqueeze(0)
return array
def normalize(tensor, epsilon=EPS):
Normalizes a tensor to have zero mean and unit standard deviation values.
* **tensor** (tensor) - The tensor to normalize.
* **epsilon** (float, *optional*, default=1e-8) - Numerical stability constant for
* A new tensor, containing the normalized values.
tensor = torch.arange(23) / 255.0
tensor = cherry.normalize(tensor, epsilon=1e-3)
if tensor.numel() <= 1:
return tensor
return (tensor - tensor.mean()) / (tensor.std() + epsilon)
def onehot(x, dim):
Creates a new onehot tensor of the specified dimension.
* **x** (int, ndarray, tensor) - Index or N-dimensional tensor of indices to be one-hot encoded.
* **dim** (int) - Size of the one-hot vector.
* A new Nxdim tensor containing one(s) at position(s) `x`, zeros everywhere else.
action = 2
action = cherry.onehot(action, dim=5)
actions = torch.tensor([[2], [1], [2]]).long() # 3x1 tensor
actions = cherry.onehot(actions, dim=5) # 3x5 tensor
size = 1
if isinstance(x, np.ndarray):
size = x.shape[0]
x = th.from_numpy(x).long()
if isinstance(x, (int, float, np.integer, np.float)):
x = [int(x), ]
if isinstance(x, list):
x = th.tensor(x).view(-1, 1).long()
if isinstance(x, th.Tensor):
size = x.size(0)
x = x.long()
onehot = th.zeros(size, dim, device=x.device)
onehot.scatter_(1, x.view(-1, 1), 1.0)
return onehot