def ones(*sizes, **kwargs): """Return a float tensor with values of ``1``. Parameters ---------- sizes : tuple, list or int The sizes indicating the shape of the output tensor. out : vm.torch.Tensor The optional output tensor. Returns ------- vm.torch.FloatTensor The output tensor. """ arguments = {'value': 1.0, 'dims': sizes} out = kwargs['out'] if 'out' in kwargs else None if out is None: out = LeafTensor(sizes, requires_grad=kwargs['requires_grad'] \ if 'requires_grad' in kwargs else False) inputs = [] outputs = [out] ctx = MakeContext(inputs, outputs) meta = ('ONCE', 'Fill', ctx) return RunOperator(inputs, outputs, meta, **arguments)
def zeros_like(input, out=None, **kwargs): """Return a float tensor with values of ``0``, shape as the input. Parameters ---------- input : vm.torch.Tensor The tensor for indicating shape. out : vm.torch.Tensor The optional output tensor. Returns ------- vm.torch.FloatTensor The output tensor. """ if not hasattr(input, 'shape'): raise ValueError('Input does not have the shape attribute.') arguments = {'value': 0.0, 'dims': input.shape} if out is None: out = LeafTensor(input.shape, requires_grad=kwargs['requires_grad'] \ if 'requires_grad' in kwargs else False) inputs = [] outputs = [out] ctx = MakeContext(inputs, outputs) meta = ('ONCE', 'Fill', ctx) return RunOperator(inputs, outputs, meta, **arguments)
def randn(*sizes, **kwargs): """Return a float tensor with a normal distribution of N(0, 1). Parameters ---------- sizes : tuple, list or int The sizes indicating the shape of the output tensor. out : vm.torch.Tensor The optional output tensor. Returns ------- vm.torch.FloatTensor The output tensor. """ arguments = {'mean': 0.0, 'std': 1.0, 'dims': sizes} out = kwargs['out'] if 'out' in kwargs else None if out is None: out = LeafTensor(sizes, requires_grad=kwargs['requires_grad'] \ if 'requires_grad' in kwargs else False) inputs = [] outputs = [out] ctx = MakeContext(inputs, outputs) meta = ('ONCE', 'RandomNormal', ctx) return RunOperator(inputs, outputs, meta, **arguments)
def uniform_(self, low=0, high=1): """Fill self tensor with the specified uniform distribution. Parameters ---------- low : numerical type The lower bound. high : numerical type The higher bound. Returns ------- vm.torch.Tensor The self. """ # TODO(PhyscalX): To support various dtypes, not only float32. arguments = {'low': float(low), 'high': float(high), 'dims': self.shape} inputs = []; outputs = [self]; ctx = MakeContext(inputs, outputs) meta = ('ONCE', 'RandomUniform', ctx) return RunOperator(inputs, outputs, meta, **arguments)
def normal_(self, mean=0, std=1): """Fill self tensor with the specified normal distribution. Parameters ---------- mean : numerical type The mean(mu) of normal distribution. std : numerical type The std(sigma) of normal distribution. Returns ------- vm.torch.Tensor The self. """ # TODO(PhyscalX): To support various dtypes, not only float32. arguments = {'mean': float(mean), 'std': float(std), 'dims': self.shape} inputs = []; outputs = [self]; ctx = MakeContext(inputs, outputs) meta = ('ONCE', 'RandomNormal', ctx) return RunOperator(inputs, outputs, meta, **arguments)
def run(self, inputs, outputs, auto_grad=True): meta = ('PERSISTENT', self.persistent_key, self.op) return RunOperator(inputs, outputs, meta, auto_grad=auto_grad)