def __init__(self, data, dtype=None, device=None): if isinstance(data, (tuple, list, int, float)): data = np.array(data) if not isinstance(data, np.ndarray): raise ValueError("data must be a list, tuple, or Numpy array.") if dtype is None: dtype = _numpy_dtype_to_dtype(data.dtype) if device is None: device = Device("CPU:0") super(Tensor, self).__init__(data, dtype, device)
def eye(n, dtype=Dtype.Float64, device=Device("CPU:0")): """ Create an identity square matrix. Args: n (int): size of square matrix dtype (Dtype): Data type of the tensor. device (Device): Device where the tensor is created. """ return super(Tensor, Tensor).eye(n, dtype, device)
def ones(shape, dtype, device=Device("CPU:0")): """ Create a tensor with fill with ones. Args: shape (list, tuple, SizeVector): Shape of the tensor. dtype (Dtype): Data type of the tensor. device (Device): Device where the tensor is created. """ if not isinstance(shape, SizeVector): shape = SizeVector(shape) return super(Tensor, Tensor).ones(shape, dtype, device)
def empty(shape, dtype, device=Device("CPU:0")): """ Create a tensor with uninitilized values. Args: shape (list, tuple, SizeVector): Shape of the tensor. dtype (Dtype): Data type of the tensor. device (Device): Device where the tensor is created. """ if not isinstance(shape, SizeVector): shape = SizeVector(shape) return super(Tensor, Tensor).empty(shape, dtype, device)
def full(shape, fill_value, dtype, device=Device("CPU:0")): """ Create a tensor with fill with the specified value. Args: shape (list, tuple, SizeVector): Shape of the tensor. fill_value (scalar): The value to be filled. dtype (Dtype): Data type of the tensor. device (Device): Device where the tensor is created. """ if not isinstance(shape, SizeVector): shape = SizeVector(shape) return super(Tensor, Tensor).full(shape, fill_value, dtype, device)
def __init__(self, shape, dtype=None, device=None, size=None): if isinstance(shape, list) or isinstance(shape, tuple): shape = SizeVector(shape) elif isinstance(shape, SizeVector): pass else: raise ValueError('shape must be a list, tuple, or SizeVector') if dtype is None: dtype = Dtype.Float32 if device is None: device = Device("CPU:0") if size is None: size = 0 super(TensorList, self).__init__(shape, dtype, device, size)
def from_tensors(tensors, device=None): """ Returns a TensorList from a list of existing tensors. Args: tensors: The list of Tensor to construct from. The tensors' shapes should be compatible. device: The device where the tensorlist is targeted. """ if not isinstance(tensors, list) and not isinstance(tensors, tuple): raise ValueError('tensors must be a list or tuple') for tensor in tensors: if not isinstance(tensor, Tensor): raise ValueError( 'every element of the input list must be a valid tensor') if device is None: device = Device("CPU:0") return super(TensorList, TensorList).from_tensors(tensors, device)