Programming Articles - Page 830 of 3363

How to perform an expand operation in PyTorch?

Shahid Akhtar Khan
Updated on 06-Dec-2021 10:59:35

3K+ Views

Tensor.expand() attribute is used to perform expand operation. It expands the Tensor to new dimensions along the singleton dimension.Expanding a tensor only creates a new view of the original tensor; it doesn't make a copy of the original tensor.If you set a particular dimension as -1, the tensor will not be expanded along this dimension.For example, if we have a tensor of size (3, 1), we can expand this tensor along the dimension of size 1.StepsTo expand a tensor, one could follow the steps given below −Import the torch library. Make sure you have already installed it.import torchDefine a tensor ... Read More

How to create tensors with gradients in PyTorch?

Shahid Akhtar Khan
Updated on 06-Dec-2021 10:54:45

5K+ Views

To create a tensor with gradients, we use an extra parameter "requires_grad = True" while creating a tensor.requires_grad is a flag that controls whether a tensor requires a gradient or not.Only floating point and complex dtype tensors can require gradients.If requires_grad is false, then the tensor is same as the tensor without the requires_grad parameter.Syntaxtorch.tensor(value, requires_grad = True)Parametersvalue – tensor data, user-defined or randomly generated.requires_grad – a flag, if True, the tensor is included in the gradient computation.OutputIt returns a tensor with requires_grad as True.StepsImport the required library. The required library is torch.Define a tensor with requires_grad = TrueDisplay the ... Read More

How to find element-wise remainder in PyTorch?

Shahid Akhtar Khan
Updated on 06-Dec-2021 10:49:37

339 Views

Element-wise remainder when a tensor is divided by other tensor is computed using the torch.remainder() method. We can also apply torch.fmod() to find the remainder.The difference between these two methods is that in torch.remainder(), when the sign of result is different than the sign of divisor, then the divisor is added to the result; whereas in torch.fmod(), it is not added.Syntaxtorch.remainder(input, other) torch.fmod(input, other)ParametersInput – It is a PyTorch tensor or scalar, the dividend.Other – It is also a PyTorch tensor or scalar, the divisor.OutputIt returns a tensor of element-wise remainder values.StepsImport the torch library.Define tensors, the dividend and the ... Read More

PyTorch – How to compute Singular Value Decomposition (SVD) of a matrix?

Shahid Akhtar Khan
Updated on 06-Dec-2021 10:43:33

1K+ Views

torch.linalg.svd() computes the singular value decomposition (SVD) of a matrix or a batch of matrices. Singular value decomposition is represented as a named tuple (U, S, Vh).U and Vh are orthogonal for real matrix and unitary for input complex matrix.Vh is transpose of V when V is a real value and conjugate transpose when V is complex.S is always real valued even when the input is complex.SyntaxU, S, Vh = torch.linalg.svd(A, full_matrices=True)ParametersA – PyTorch tensor (matrix or batch of matrices).full_matrices – If True, the output is a full SVD, else a reduced SVD. Default is True.OutputIt returns a named tuple ... Read More

How to perform in-place operations in PyTorch?

Shahid Akhtar Khan
Updated on 06-Dec-2021 10:31:27

2K+ Views

In-place operations directly change the content of a tensor without making a copy of it. Since it does not create a copy of the input, it reduces the memory usage when dealing with high-dimensional data. An in-place operation helps to utilize less GPU memory.In PyTorch, in-place operations are always post-fixed with a "_", like add_(), mul_(), etc.StepsTo perform an in-place operation, one could follow the steps given below −Import the required library. The required library is torch.Define/create tensors on which in-place operation is to be performed.Perform both normal and in-place operations to see the clear difference between them.Display the tensors ... Read More

While loop in Lua Programming

Mukul Latiyan
Updated on 01-Dec-2021 10:58:07

672 Views

A while loop is an indefinite loop that can be modified to run for a finite number of iterations based on the condition we provide.In Lua, the while condition is tested first. If the condition turns out to be false, then the loop ends, otherwise, Lua executes the body of the loop and repeats the process.Syntaxwhile( condition ){ // do this }ExampleConsider the example shown below −a = {1, 2, 3, 4, 5} local i = 1 while a[i] do print(a[i]) i = i + 1 endOutput1 2 3 4 5It ... Read More

Variable number of arguments in Lua Programming

Mukul Latiyan
Updated on 01-Dec-2021 10:50:06

4K+ Views

There are functions in Lua that accept a variable number of arguments. These are very helpful in cases where we want to run the same function with many different arguments that might vary in length. So, instead of creating a different function, we pass them in a variable arguments fashion.Syntaxfunction add(...) -- function code endIt should be noted that the three dots (...) in the parameter list indicate that the function has a variable number of arguments. Whenever this function will be called, all its arguments will be collected in a single table, which the function addresses ... Read More

Table Type in Lua Programming

Mukul Latiyan
Updated on 01-Dec-2021 10:46:45

2K+ Views

A table is a data type in Lua, which is used to implement associative arrays. These associative arrays can be used to implement different data structures like queues, maps, lists, etc.An associative array in Lua is an array that can be indexed not only with numbers, but also with strings or any other value of the language, except nil.Tables in Lua don't have any fixed size, and we can insert as many elements as we want in them, dynamically.Tables in Lua are neither values nor variables; they are objects.We can create tables by means of a constructor expression, which in ... Read More

Return statement in Lua Programming

Mukul Latiyan
Updated on 01-Dec-2021 10:44:34

5K+ Views

There are certain cases where we want to return a value from a given function so that we can use it later. These return values make use of a return keyword which in turn allows a function to return values.There is an implicit return at the end of any function, so you do not need to use one if your function ends naturally, without returning any value.It should be noted that the return statement is optional; if not specified, the function returns nil.Also, only one return statement is allowed in a function.Syntaxreturn expression/valueNow let’s consider an example where we would ... Read More

Numeric for in Lua Programming

Mukul Latiyan
Updated on 01-Dec-2021 10:42:21

1K+ Views

In Lua, there are two types of for loops − the numeric for and the generic for.SyntaxThe numeric for uses the following syntax −for var=exp1, exp2, exp3 do something endIt should be noted that we can write exp1, exp2, exp3 at the same time or we can omit one of them, and the numeric loop will not result in a compile error, though its functionality will change.ExampleLet’s consider a simple variation of a numeric for loop, where we will try to print numbers from 1 to 10.Consider the example shown below −for i = 1, 10 do ... Read More

Advertisements