Found 26504 Articles for Server Side Programming

torch.polar() Method in Python PyTorch

Shahid Akhtar Khan
Updated on 27-Jan-2022 06:03:29

536 Views

With given absolute values and angles, we can construct a complex number in PyTorch using torch.polar() method. The absolute value and angles must be float or double. Both the absolute value and the angle must be of the same type.If abs is a float, then angle must also be float.If the inputs are torch.float32, then the constructed complex tensor must be torch.complex64.If the inputs are torch.float64, then the complex tensor must be torch.complex128.Syntaxtorch.polar(abs, angle)Parametersabs – The absolute length of the complex tensor.angle – The angle of the complex tensor.StepsWe could use the following steps to construct a complex tensor with ... Read More

How to compute the Hessian of a given scalar function in PyTorch?

Shahid Akhtar Khan
Updated on 27-Jan-2022 05:55:46

1K+ Views

The hessian() function computes the Hessian of a given function. The hessian() function can be accessed from the torch.autograd.functional module. The function whose Hessian is being computed takes a tensor as the input and returns a tuple of tensors or a tensor. The hessian() function returns a tensor with the Hessian values computed for a function with the given input.Syntaxtorch.autograd.functional.hessian(func, input)Parametersfunc − It's a Python function for which the Hessian is computed.input − It’s input to the function, func.StepsWe could use the following steps to compute the Hessian of a given function −Import the required library. In all the following ... Read More

How to compute the Jacobian of a given function in PyTorch?

Shahid Akhtar Khan
Updated on 27-Jan-2022 05:46:57

3K+ Views

The jacobian() function computes the Jacobian of a given function. The jacobian() function can be accessed from the torch.autograd.functional module. The function whose Jacobian is being computed takes a tensor as the input and returns a tuple of tensors or a tensor. The jacobian() function returns a tensor with Jacobian values computed for a function with the given input.Syntaxtorch.autograd.functional.jacobian(func, input)Parametersfunc − It's a Python function for which the Jacobian is computed.input − It’s the input to the function,  func.StepsWe could use the following steps to compute the Jacobian of a given function −Import the required library. In all the following ... Read More

Find Sum of all unique subarray sum for a given array in C++

sudhir sharma
Updated on 25-Jan-2022 14:17:49

736 Views

In this problem, we are given an array arr[] consisting of n integer values. Our task is to find the sum of all unique subarray sum for a given array. Subarray sum is the sum of elements of the given subarray.Let's take an example to understand the problem, Input : arr[] = {1, 2, 4} Output : 23Explanation −All subarrays of the given array are : (1), (2), (4), (1, 2), (2, 4), (1, 2, 4) Sum of subarrays = 1 + 2 + 4 + (1+2) + (2+4) + (1+2+4) = 23Solution ApproachA solution to the problem is by ... Read More

Find sum of all right leaves in a given Binary Tree in C++

sudhir sharma
Updated on 25-Jan-2022 14:01:09

430 Views

In this problem, we are given a binary tree. Our task is to find the sum of all left right in a given Binary Tree.Let's take an example to understand the problem, Input :Output : 8Explanation −All leaf nodes of the tree are : 1, 8 Sum = 1 + 8 = 9Solution ApproachA simple solution to the problem is traversing the tree from root to leaf. If a node is a left leaf node, add it to sum. When the whole tree is traversed. Print Sum.ExampleProgram to illustrate the working of our solution#include using namespace std; struct Node{ ... Read More

Find sum of all left leaves in a given Binary Tree in C++

sudhir sharma
Updated on 25-Jan-2022 13:26:08

399 Views

In this problem, we are given a binary tree. Our task is to find the sum of all left leaves in a given Binary Tree.Let's take an example to understand the problem, Input : Output : 11Explanation −All leaf nodes of the tree are : 2, 9 Sum = 2 + 9 = 11Solution ApproachA simple solution to the problem is traversing the tree from root to leaf. If a node is a left leaf node, add it to sum. When the whole tree is traversed. Print Sum.ExampleProgram to illustrate the working of our solution#include using namespace std; struct Node{ ... Read More

Find subarray with given sum - (Handles Negative Numbers) in C++

sudhir sharma
Updated on 25-Jan-2022 13:06:14

594 Views

In this problem, we are given an array arr[] consisting of N integers stored in unsorted order. Our task is to find a subarray with a given sum.Let's take an example to understand the problem, Input : arr[] = {2, 5, -1, 4, 6, -9, 5} sum = 14 Output : subarray = {5, -1, 4, 6}Explanation −Subarray sum = 5 - 1 + 4 + 6 = 14Solution ApproachA simple solution to the problem is using nested loops. We will loop through the array and using an inner loop, we will find subarray. For each subarray we will find ... Read More

Lambda Functions in Ruby

Mukul Latiyan
Updated on 25-Jan-2022 11:52:10

416 Views

In Ruby, we can take the help of lambda functions when we want to use anonymous functions. They are also treated like objects in Ruby, as everything in Ruby is treated as objects.SyntaxThe syntax of declaring a lambda function is shown below.lambda = lambda {}Or, we can also make use of lambda literal.lambda = ->() {} Let's first check the type of the lambda functions in Ruby with the help of a program.Example 1Consider the code shown belowsome_lambda_function = lambda { puts "Welcome to TutorialsPoint!"} puts some_lambda_function.classOutputProc Example 2Now let's create another program where we will use our own ... Read More

Comparable module in Ruby

Mukul Latiyan
Updated on 25-Jan-2022 11:49:27

454 Views

In Ruby, the class whose objects can be ordered uses the Comparable mixin. Class definitions need to include an operator to compare receivers with each other. The operator will return either -1, 0, or 1.It returns -1 if the receiver is less than another object.If it is greater than another object, then it returns 1.It returns 0 if the receiver is equal to another object.In the Comparable module, the operator is used to implement the conventional comparison operators (*, =, and >) and sometimes also between? method as well.Now that we know a little about the comparable module in ... Read More

Array reverse() vs reverse! in Ruby

Mukul Latiyan
Updated on 25-Jan-2022 11:46:03

926 Views

In this article, we will explore the two most widely used methods on arrays in Ruby. These are the reverse() method and the reverse! method.reverse() methodThe reverse() method in Ruby reverses the content of the array and returns a new array. Now, let's take a couple of examples to understand how it works.Example 1# reverse() method in Ruby # array declaration first_arr = [18, 22, 33, nil, 7, 6] # array declaration second_arr = [1, 5, 1, 3, 88, 9] # array declaration third_arr = [18, 22, 55, 6] # reverse method example puts "reversed array ... Read More

Advertisements