PyTorch is an open-source framework available with a Python programming language. We can process the data in PyTorch in the form of a tensor.
A tensor is a multidimensional array that is used to store the data. For using a tensor, we have to import the torch module.
To create a tensor, the method used is tensor().
Syntax:
torch.tensor(data)
Where data is a multidimensional array.
torch.sin() Function
The torch.sin() function in PyTorch returns sine values of all the elements in a tensor. It takes only one parameter.
Syntax:
torch.sin(tensor_object)
Parameter:
tensor_object is the input tensor
Example 1
Let’s create a one-dimensional tensor, data1 and return sine values, by applying torch.sin().
import torch
#create a 1D tensor - data1 with 5 numeric values.
data1 = torch.tensor([23,45,67,10,0])
#display
print("Tensor: ",data1)
#perform sin() on above tensor
print("Sine values: ",torch.sin(data1))
Output:
Sine values: tensor([-0.8462, 0.8509, -0.8555, -0.5440, 0.0000])
We can see that sine values were returned.
Example 2
Let’s create a two-dimensional tensor, data1 and return sine values, by applying torch.sin() on it.
import torch
#create a 2D tensor - data1 with 5 numeric values in each row.
Data1 = torch.tensor([[23,45,67,10,0],[65,78,90,120,180]])
#display
print("Tensor: ",data1)
#perform sin() on above tensor
print("Sine values: ",torch.sin(data1))
Output:
[ 65, 78, 90, 120, 180]])
Sine values: tensor([[-0.8462, 0.8509, -0.8555, -0.5440, 0.0000],
[ 0.8268, 0.5140, 0.8940, 0.5806, -0.8012]])
We can see that sine values were returned.
torch.cos() Function
The torch.cos() function in PyTorch returns cosine values of all the elements in a tensor. It takes only one parameter.
Syntax:
torch.cos(tensor_object)
Parameter:
tensor_object is the input tensor.
Example 1
Let’s create a one-dimensional tensor, data1 and return cosine values, by applying torch.cos() on it.
import torch
#create a 1D tensor - data1 with 5 numeric values.
Data1 = torch.tensor([23,45,67,10,0])
#display
print("Tensor: ",data1)
#perform cos() on above tensor
print("Cosine values: ",torch.cos(data1))
Output:
Cosine values: tensor([-0.5328, 0.5253, -0.5178, -0.8391, 1.0000])
We can see that cosine values were returned.
Example 2
Let’s create a two-dimensional tensor, data1 and return cosine values, by applying torch.cos() on it.
import torch
#create a 2D tensor - data1 with 5 numeric values in each row.
Data1 = torch.tensor([[23,45,67,10,0],[65,78,90,120,180]])
#display
print("Tensor: ",data1)
#perform cos() on above tensor
print("Cosine values: ",torch.cos(data1))
Output:
[ 65, 78, 90, 120, 180]])
Cosine values: tensor([[-0.5328, 0.5253, -0.5178, -0.8391, 1.0000],
[-0.5625, -0.8578, -0.4481, 0.8142, -0.5985]])
We can see that cosine values were returned.
torch.tan() Function
The torch.tan() function in PyTorch returns tangent values of all the elements in a tensor. It takes only one parameter.
Syntax:
torch.tan(tensor_object)
Parameter:
tensor_object is the input tensor.
Example 1
Let’s create a one-dimensional tensor, data1 and return tangent values, by applying torch.tan() on it.
import torch
#create a 1D tensor - data1 with 5 numeric values.
data1 = torch.tensor([23,45,67,10,0])
#display
print("Tensor: ",data1)
#perform tan() on above tensor
print("Tangent values: ",torch.tan(data1))
Output:
Tangent values: tensor([1.5882, 1.6198, 1.6523, 0.6484, 0.0000])
We can see that tangent values were returned.
Example 2
Let’s create a two-dimensional tensor, data1 and return tangent values, by applying torch.tan() on it.
import torch
#create a 2D tensor - data1 with 5 numeric values in each row.
data1 = torch.tensor([[23,45,67,10,0],[65,78,90,120,180]])
#display
print("Tensor: ",data1)
#perform tan() on above tensor
print("Tangent values: ",torch.tan(data1))
Output:
[ 65, 78, 90, 120, 180]])
Tangent values: tensor([[ 1.5882, 1.6198, 1.6523, 0.6484, 0.0000],
[-1.4700, -0.5992, -1.9952, 0.7131, 1.3387]])
We can see that tangent values were returned.
torch.sinh() Function
The torch.sinh() function in PyTorch returns hyperbolic sine values of all the elements in a tensor. It takes only one parameter.
Syntax:
torch.sinh(tensor_object)
Parameter:
tensor_object is the input tensor.
Example 1
Let’s create a one-dimensional tensor, data1 and return hyperbolic sine values, by applying torch.sinh() on it.
import torch
#create a 1D tensor - data1 with 5 numeric values.
data1 = torch.tensor([0,1,45,10,23])
#display
print("Tensor: ",data1)
#perform sinh() on above tensor
print("Hyperbolic sine values: ",torch.sinh(data1))
Output:
Hyperbolic sine values: tensor([0.0000e+00, 1.1752e+00, 1.7467e+19, 1.1013e+04, 4.8724e+09])
We can see that hyperbolic sine values were returned.
Example 2
Let’s create a two-dimensional tensor, data1 and return hyperbolic sine values, by applying torch.sinh() on it.
import torch
#create a 2D tensor - data1 with 5 numeric values in each row.
data1 = torch.tensor([[23,45,67,10,0],[65,78,90,120,180]])
#display
print("Tensor: ",data1)
#perform sinh() on above tensor
print("Hyperbolic sine values: ",torch.sinh(data1))
Output:
[ 65, 78, 90, 120, 180]])
Hyperbolic sine values: tensor([[4.8724e+09, 1.7467e+19, 6.2618e+28, 1.1013e+04, 0.0000e+00],
[8.4744e+27, 3.7492e+33, inf, inf, inf]])
We can see that hyperbolic sine values were returned.
torch.cosh() Function
The torch.cosh() function in PyTorch returns hyperbolic cosine values of all the elements in a tensor. It takes only one parameter.
Syntax:
torch.cosh(tensor_object)
Parameter:
tensor_object is the input tensor.
Example 1
Let’s create a one-dimensional tensor, data1 and return hyperbolic cosine values by applying torch.cosh() on it.
import torch
#create a 1D tensor - data1 with 5 numeric values.
data1 = torch.tensor([23,45,67,10,0])
#display
print("Tensor: ",data1)
#perform cosh() on above tensor
print("Hyperbolic cosine values: ",torch.cosh(data1))
Output:
Hyperbolic cosine values: tensor([4.8724e+09, 1.7467e+19, 6.2618e+28, 1.1013e+04, 1.0000e+00])
We can see that hyperbolic cosine values were returned.
Example 2
Let’s create a two-dimensional tensor, data1 and return hyperbolic cosine values, by applying torch.cosh() on it.
import torch
#create a 2D tensor - data1 with 5 numeric values in each row.
data1 = torch.tensor([[23,45,67,10,0],[65,78,90,120,180]])
#display
print("Tensor: ",data1)
#perform cosh() on above tensor
print("Hyperbolic cosine values: ",torch.cosh(data1))
Output:
[ 65, 78, 90, 120, 180]])
Hyperbolic cosine values: tensor([[4.8724e+09, 1.7467e+19, 6.2618e+28, 1.1013e+04, 1.0000e+00],
[8.4744e+27, 3.7492e+33, inf, inf,inf]])
We can see that hyperbolic cosine values were returned.
torch.tanh() Function
The torch.tanh() function in PyTorch returns hyperbolic tangent values of all the elements in a tensor. It takes only one parameter.
Syntax:
torch.tanh(tensor_object)
Parameter:
tensor_object is the input tensor.
Example 1
Let’s create a one dimensional tensor, data1 and return hyperbolic tangent values, by applying torch.tanh() on it.
import torch
#create a 1D tensor - data1 with 5 numeric values.
data1 = torch.tensor([23,45,67,10,0])
#display
print("Tensor: ",data1)
#perform tanh() on above tensor
print("Hyperbolic tangent values: ", torch.tanh(data1))
Output:
Hyperbolic tangent values: tensor([1., 1., 1., 1., 0.])
We can see that hyperbolic tangent values were returned.
Example 2
Let’s create a two-dimensional tensor, data1 and return hyperbolic tangent values, by applying torch.tanh() on it.
import torch
#create a 2D tensor - data1 with 5 numeric values in each row.
data1 = torch.tensor([[23,45,67,10,0],[65,78,90,120,180]])
#display
print("Tensor: ",data1)
#perform tanh() on above tensor
print("Hyperbolic tangent values: ",torch.tanh(data1))
Output:
[ 65, 78, 90, 120, 180]])
Hyperbolic tangent values: tensor([[1., 1., 1., 1., 0.],
[1., 1., 1., 1., 1.]])
We can see that hyperbolic tangent values were returned.
Conclusion
In this PyTorch lesson, we saw how to perform Trigonometric functions in PyTorch. We discussed three types of trigonometric functions: sin(), cos() and tan(). If you need to perform hyperbolic functions, you can use sinh(), cosh(), and tan().