The torch.polar() function is used to construct a complex number by using absolute values and angles. In detail, we will discuss the polar function using PyTorch in Python.
And additionally, we will cover different examples related to the PyTorch polar function. And we will cover these topics.
- What is PyTorch polar
- PyTorch polar() example
- PyTorch polar create angles with float type and constructs complex tensor with data type
- How to use PyTorch Polar Coordinates
- PyTorch polar create angles with double type and construct complex tensor with data type
- PyTorch polar create angles with double type and construct complex tensor with float type
- Pytorch polar constructed a complex tensor of dtype is torch.complex64 with absolute length and angle of tensor
- Pytorch polar constructed a complex tensor of dtype is torch.complex128 with absolute length and angle of tensor
What is PyTorch polar?
In this section, we will learn about the PyTorch polar in python.
The polar is a function that is used to construct a complex number whose elements are cartesian coordinated and correlate to the polar coordinates with the absolute value and angle.
Syntax:
torch.polar(abs, angle, out=None)
Parameters:
- abs: The abs is an absolute value of the complex tensor.
- angle: The angle is a parameter that is the angle of a complex tensor.
- out=None: The out is an output tensor of the polar() function.
So, with this, we understood about the Pytorch polar with the help of a torch.polar() function.
Read: PyTorch Stack Tutorial + Examples
PyTorch polar() example
In this section, we will learn how to implement the polar function with the help of an example in python.
Here we are calling the polar() function that we can construct a complex number with absolute value and angle. The absolute value and angle must be float and double.
Code:
In the following code, we will import the required libraries such as import torch, and import numpy as np.
- absolutevalue = torch.tensor([3, 4], dtype=torch.float64) is used to describing the variable by using the torch.tensor() function.
- p = torch.polar(absolutevalue, and): Here we are calling the torch.polar() function.
- print(p) is used to print the output by using the print() function.
# Import Library
import torch
import numpy as np
# Describing the variables
absolutevalue = torch.tensor([3, 4], dtype=torch.float64)
ang = torch.tensor([np.pi / 2, 7 * np.pi / 6], dtype=torch.float64)
# Calling the Polar() function
p = torch.polar(absolutevalue, ang)
# Print the output
print(p)
Output:
After running the above code, we get the following output in which we can see that the PyTorch Polar values are printed on the screen.
So, with this, we understood about the PyTorch polar with the help of an example.
Read: How to use PyTorch Full() Function
PyTorch polar create angles with float type and constructs complex tensor with data type
In this section, we will learn about the PyTorch polar create angles with float type and constructs complex tensor with data type in python.
Here we are using the torch.polar() function from which we construct the perfect length and angles with float type and also construct the complex tensor along with the data type.
Code:
In the following code, we will import all the necessary libraries such as import torch and import numpy.
- absolutevalue = torch.tensor([25, 47, 69, 56, 34], dtype=torch.float32) is used to create the absolute lengths with float type.
- ang = torch.tensor([numpy.pi / 4, numpy.pi / 6, numpy.pi /5, numpy.pi / 7, 0], dtype=torch.float32): Here we are creating the angles with float type.
- print(torch.polar(absolutevalue, and)) is used to construct the complex tensor.
- print(torch.polar(absolutevalue, ang).dtype): Here we are construct the complex tensor and display the datatype.
# Importing libraries
import torch
import numpy
# Creating the absolute lengths with float type
absolutevalue = torch.tensor([25, 47, 69, 56, 34], dtype=torch.float32)
# Creating the angles with float type
ang = torch.tensor([numpy.pi / 4, numpy.pi / 6, numpy.pi /
5, numpy.pi / 7, 0], dtype=torch.float32)
# Construct the complex tensor
print(torch.polar(absolutevalue, ang))
# Construct the complex tensor and display the datatype
print(torch.polar(absolutevalue, ang).dtype)
Output:
After running the above code, we get the following output in which we can see that the PyTorch polar creates an angle with float type and construct a complex tensor with the data type and the values are printed on the screen.
This is how we understand about the PyTorch polar creates angles with float type and constructs complex tensors with the data type.
Read: Introduction to PyTorch Lenet
How to use PyTorch Polar Coordinates
In this section, we will learn about PyTorch Polar Coordinates in python.
Before moving forward we should have a piece of knowledge about the Coordinates.
The coordinate is that which brings different elements of a compound activity into a systematic relationship.
Code:
In the following code, we will import all the necessary libraries such as import torch and import numpy as np.
- absolutevalue = torch.tensor([2,4], dtype=torch.float32): Here we defining the absolute value of the complex tensor.
- ang = torch.tensor([np.pi / 4, np.pi/6], dtype=torch.float32): Here we are defining the angle of the complex tensor.
- print(“absolutevalue:”, absolutevalue) is used to print the absolute value.
- print(“angle:”, ang) is used to print the angle by using the print() function.
- p = torch.polar(absolutevalue, ang): Here we are calling the torch.polar() function.
# Importing Libraries
import torch
import numpy as np
# define the absolute value and angle of the complex tensor
absolutevalue = torch.tensor([2,4], dtype=torch.float32)
ang = torch.tensor([np.pi / 4, np.pi/6], dtype=torch.float32)
# print absolute value and angle
print("absolutevalue:", absolutevalue)
print("angle:", ang)
p = torch.polar(absolutevalue, ang)
Output:
After running the above code, we get the following output in which we can see that the PyTorch polar coordinates values are printed on the screen.
This is how we can understand the PyTorch polar coordinates in python.
Read: PyTorch Reshape Tensor
PyTorch polar create angles with double type and construct complex tensor with data type
In this section, we will learn about the PyTorch polar create angles with double type and construct complex tensor with data type in python.
Here we are using the torch.polar() function from which we construct the complex tensor with data type and also create angles with double type.
Code:
In the following code, we will import all the necessary libraries such as import torch and import numpy.
- absolutevalue = torch.tensor([25, 47, 69, 54, 36], dtype=torch.double): Here we are creating the absolute lengths with double type.
- ang = torch.tensor([numpy.pi / 4, numpy.pi / 6, numpy.pi / 5, numpy.pi / 7, 0], dtype=torch.double): Here we are creating the angles with float type.
- print(torch.polar(absolutevalue, ang)) is used to construct the complex tensor.
- print(torch.polar(absolutevalue, ang).dtype) is used to print the absolute value and angles by using the print() function.
# Importing libraries
import torch
import numpy
# Creating the absolute lengths with double type
absolutevalue = torch.tensor([25, 47, 69, 54, 36], dtype=torch.double)
# Creating the angles with float type
ang = torch.tensor([numpy.pi / 4, numpy.pi / 6, numpy.pi /
5, numpy.pi / 7, 0], dtype=torch.double)
# Construct the complex tensor
print(torch.polar(absolutevalue, ang))
# Construct the complex tensor and display the datatype
print(torch.polar(absolutevalue, ang).dtype)
Output:
After running the above code, we get the following output in which we can see that the PyTorch polar create with double type and constructed complex tensor with data type values are printed on the screen.
This is how we understand about the PyTorch polar to create angles with double type and construct complex tensors with the data type.
Read: Jax Vs PyTorch
PyTorch polar create angles with double type and construct complex tensor with float type
In this section, we will learn about the Pytorch polar angles with double type and construct complex tensors with float type in python.
Here we are using the torch.polar() function from which we construct the complex tensor with data type and also create angles with float type.
Code:
In the following code, we will import the torch library such as import torch.
- absolutevalue = torch.tensor([5, 4], dtype=torch.float64): Here we are creating the absolute length of angle with float type.
- print(torch.polar(absolutevalue, angle)) is used to construct the complex tensor.
# Importing libraries
import torch
import numpy
# Creating the absolute lengths of angle with float type
absolutevalue = torch.tensor([5, 4], dtype=torch.float64)
# Creating the angles with float type
angle = torch.tensor([numpy.pi / 4, numpy.pi / 6],
dtype=torch.double)
# Construct the complex tensor
print(torch.polar(absolutevalue, angle))
# Construct the complex tensor and display the datatype
print(torch.polar(absolutevalue, angle).dtype)
Output:
In the below output, we can see that the Pytorch polar create angles with double type and construct complex tensor with float type values printed on the screen.
So, with this, we understood about the PyTorch polar create angles with double type and construct complex tensor with float type.
Read: PyTorch Numpy to Tensor
Pytorch polar constructed a complex tensor of dtype is torch.complex64 with absolute length and angle of tensor
In this section, we will learn about the PyTorch polar constructed a complex tensor of dtype is a torch.complex64 with absolute length and angle of tensor.
Here we are using the torch.polar() function from which we construct the complex tensor of data type id torch.complex64 with absolute length and angle of tensor.
Code:
In the following code, we will import all the necessary libraries such as import torch and import numpy.
- absolutevalue = torch.tensor([4,6], dtype=torch.float32) is used to define the absolute value of the complex tensor.
- print(“absolute value:”, absolutevalue) is used to print the absolute value.
- polar = torch.polar(absolutevalue, ang): Here we are calling the polar() function.
- print(“dtype of complex number:\n”, polar.dtype) is used to print the type of complex number.
# Import library
import torch
import numpy as np
# Define the absolute value of the complex tensor
absolutevalue = torch.tensor([4,6], dtype=torch.float32)
# Define the angle of the complex tensor
ang = torch.tensor([np.pi / 4, np.pi / 6], dtype=torch.float32)
# Print the absolute value and angle
print("absolute value:", absolutevalue)
print("ang:", ang)
polar = torch.polar(absolutevalue, ang)
print("Complex number:\n",polar)
print("dtype of complex number:\n", polar.dtype)
Output:
After running the above code, we get the below output in which we can see that the PyTorch polar constructed a complex tensor of dtype is a torch.complex64 is printed on the screen.
So, with this, we can understand about PyTorch polar constructed a complex tensor of dtype is torch complex64 with absolute length and angle of tensor.
Read: PyTorch fully connected layer
Pytorch polar constructed a complex tensor of dtype is torch.complex128 with absolute length and angle of tensor
In this section, we will learn about the PyTorch polar constructed a complex tensor dtype is torch.complex128 with absolute length and angle of tensor.
Here we are using the torch.polar() function from which we construct the complex tensor of data type id torch.complex128 with absolute length and angle of tensor.
Code:
In the following code, we will import the required library such as import torch and import numpy.
- ang = torch.tensor([np.pi / 4, 5 * np.pi / 6], dtype=torch.float64) is used to define the angle of the complex tensor.
- plr = torch.polar(absolutevalue, ang): Here we are calling the torch.polar() function.
- print(“dtype of complex number:\n”, plr.dtype) is used to print the type of the complex datatype.
# Import library
import torch
import numpy as np
# Define the absolute value of the complex tensor
absolutevalue = torch.tensor([3, 5], dtype=torch.float64)
# Define the angle of the complex tensor
ang = torch.tensor([np.pi / 4, 5 * np.pi / 6], dtype=torch.float64)
# Calling the torch.polar() function
plr = torch.polar(absolutevalue, ang)
print("Complex number: \n",plr)
print("dtype of complex number:\n", plr.dtype)
Output:
After running the above code, we get the following output in which we can see that the PyTorcvh polar construct a complex tensor of dtype is a torch.complex128 value is printed on the screen.
This is how we understand about the Pytorch polar constructed a complex tensor of dtype is torch.complex128 with absolute length and angle of tensor.
Also, take a look at some more PyTorch tutorials in Python.
- PyTorch Logistic Regression
- PyTorch Dataloader + Examples
- PyTorch Pretrained Model
- PyTorch Tensor to Numpy
So, in this tutorial, we discussed PyTorch Polar and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.
- What is PyTorch polar
- PyTorch polar() example
- PyTorch polar create angles with float type and constructs complex tensor with data type
- How to use PyTorch Polar Coordinates
- PyTorch polar create angles with double type and construct complex tensor with data type
- PyTorch polar create angles with double type and construct complex tensor with float type
- Pytorch polar constructed a complex tensor of dtype is torch.complex64 with absolute length and angle of tensor
- Pytorch polar constructed a complex tensor of dtype is torch.complex128 with absolute length and angle of tensor
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.