Python Pytorch ones() method Last Updated : 22 Apr, 2020 Comments Improve Suggest changes Like Article Like Report PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes. The function torch.ones() returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size. Syntax: torch.ones(size, out=None) Parameters: size: a sequence of integers defining the shape of the output tensor out (Tensor, optional): the output tensor Return type: A tensor filled with scalar value 1, of same shape as size. Code #1: Python3 # Importing the PyTorch library import torch # Applying the ones function and # storing the resulting tensor in 't' a = torch.ones([3, 4]) print("a = ", a) b = torch.ones([1, 5]) print("b = ", b) c = torch.ones([5, 1]) print("c = ", c) d = torch.ones([3, 3, 2]) print("d = ", d) Output: a = tensor([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]) b = tensor([[1., 1., 1., 1., 1.]]) c = tensor([[1.], [1.], [1.], [1.], [1.]]) d = tensor([[[1., 1.], [1., 1.], [1., 1.]], [[1., 1.], [1., 1.], [1., 1.]], [[1., 1.], [1., 1.], [1., 1.]]]) Comment More infoAdvertise with us Next Article Python Pytorch ones() method S sanskar27jain Follow Improve Article Tags : Python Python-PyTorch Practice Tags : python Similar Reads Python - PyTorch numel() method PyTorch torch.numel() method returns the total number of elements in the input tensor. Syntax: torch.numel(input) Arguments input: This is input tensor. Return: It returns the length of the input tensor. Let's see this concept with the help of few examples: Example 1: Python3 # Importing the PyTorch 1 min read Python | sympy.ones() method With the help of sympy.ones() method, we can create a matrix having dimension nxm and filled with ones by using sympy.ones() method. Syntax : sympy.ones() Return : Return a ones matrix. Example #1 : In this example, we can see that by using sympy.ones() method, we are able to create the ones matrix 1 min read Python - PyTorch div() method PyTorch torch.div() method divides every element of the input with a constant and returns a new modified tensor. Syntax: torch.div(inp, other, out=None) Arguments inp: This is input tensor. other: This is a number to be divided to each element of input inp. out: The output tensor. Return: It returns 1 min read Python - PyTorch abs() method PyTorch torch.abs() method computes the element-wise absolute value of the given input tensor. Syntax: torch.abs(inp, out=None) ? Tensor Arguments inp: This is input tensor. out: This is optional parameter and is the output tensor. Return: It returns a Tensor having absolute value of input inp. Let' 1 min read Python - PyTorch add() method PyTorch torch.add() method adds a constant value to each element of the input tensor and returns a new modified tensor. Syntax: torch.add(inp, c, out=None) Arguments inp: This is input tensor. c: The value that is to be added to every element of tensor. out: This is optional parameter and it is the 1 min read Python | PyTorch asin() method PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes.The function torch.asin() provides support for the inverse sine function in PyTorch. It expects the input to be in the range [-1, 1] and gives the out 2 min read Python - PyTorch frac() method PyTorch torch.frac() method computes the fractional portion of each element in input. Syntax: torch.frac(input, out=None) Arguments input: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see this concept with the help of few examples: Example 1: Python3 # Importing t 1 min read Python PyTorch log2() method PyTorch log2() method computes the logarithm to the base 2 of the elements of an input tensor. Â It computes the logarithm values element-wise. Â It takes a tensor as an input and returns a new tensor with computed logarithm values. The elements of the input tensor must be between zero and the positiv 4 min read Python | PyTorch acos() method PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes. The function torch.acos() provides support for the inverse cosine function in PyTorch. It expects the input to be in the range [-1, 1] and gives the 2 min read Like