Rate this Page

torch.Tensor.new_ones#

Tensor.new_ones(size,*,dtype=None,device=None,requires_grad=False,layout=torch.strided,pin_memory=False)Tensor#

Returns a Tensor of sizesize filled with1.By default, the returned Tensor has the sametorch.dtype andtorch.device as this tensor.

Parameters

size (int...) – a list, tuple, ortorch.Size of integers defining theshape of the output tensor.

Keyword Arguments
  • dtype (torch.dtype, optional) – the desired type of returned tensor.Default: if None, sametorch.dtype as this tensor.

  • device (torch.device, optional) – the desired device of returned tensor.Default: if None, sametorch.device as this tensor.

  • requires_grad (bool,optional) – If autograd should record operations on thereturned tensor. Default:False.

  • layout (torch.layout, optional) – the desired layout of returned Tensor.Default:torch.strided.

  • pin_memory (bool,optional) – If set, returned tensor would be allocated inthe pinned memory. Works only for CPU tensors. Default:False.

Example:

>>>tensor=torch.tensor((),dtype=torch.int32)>>>tensor.new_ones((2,3))tensor([[ 1,  1,  1],        [ 1,  1,  1]], dtype=torch.int32)