| by Arround The Web | No comments

Change the View of PyTorch Tensor

“In this PyTorch tutorial, we will see how to change the view of a tensor in PyTorch. PyTorch is an open-source framework available with a Python programming language.

A tensor is a multidimensional array that is used to store the data. So 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 multi-dimensional array.

tensor.view()

view() in PyTorch is used to change the tensor object view by converting it into a specified number of rows and columns.

Syntax:

tensor_object.view(r,c)

It takes two parameters.

  1. r specifies the number of rows to be formed from the tensor_object.
  2. c specifies the number of columns to be formed from the tensor_object.

Be sure that the actual tensor object contains an even count of elements.

Example 1

Here, we will create a tensor that holds six elements with Float type and change its view that has 3 rows and 2 columns.

#import torch module

import torch

 

 

#create 1D tensor with Float data type that hold 6 elements

data1 = torch.FloatTensor([23,45,54,32,23,78])

#display

print("Actual Tensor: ",data1)

 

#change the data1 view to 3 rows and 2 columns.

print("Tensor with 3 rows and 2 columns: ",data1.view(3,2))

Output:

Actual Tensor: tensor([23., 45., 54., 32., 23., 78.])

Tensor with 3 rows and 2 columns: tensor([[23., 45.],

[54., 32.],

[23., 78.]])

We can see that the view of the tensor is changed to 3 rows and 2 columns.

Example 2

Here, we will create a tensor that holds six elements with Float type and change its view that has 2 rows and 3 columns.

#import torch module

import torch

 

 

#create 1D tensor with Float data type that hold 6 elements

data1 = torch.FloatTensor([23,45,54,32,23,78])

#display

print("Actual Tensor: ",data1)

 

#change the data1 view to 2 rows and 3 columns.

print("Tensor with 2 rows and 3 columns: ",data1.view(2,3))

Output:

Actual Tensor: tensor([23., 45., 54., 32., 23., 78.])

Tensor with 2 rows and 3 columns: tensor([[23., 45., 54.],

[32., 23., 78.]])

We can see that the view of the tensor is changed to 2 rows and 3 columns.

Change the datatype

It can be possible to change the datatype of the tensor using view().

We need to specify the datatype inside the view method.

Syntax:

tensor_object.view(torch.datatype)

Parameter:

It takes datatype as a parameter like int8,int16, etc.

Example 1

In this example, we will create a tensor with Float type and convert it to int data types.

dtype is used to return the datatype of a tensor.

#import torch module

import torch

 

 

#create 1D tensor with Float data type that hold 6 elements

data1 = torch.FloatTensor([23,45,54,32,23,78])

#display

print("Actual Tensor data type: ",data1.dtype)

 

#change the data1 data type to int8

print("Converting to int8: ",data1.view(torch.int8).dtype)

#change the data1 data type to int16

print("Converting to int16: ",data1.view(torch.int16).dtype)

#change the data1 data type to int32

print("Converting to int32: ",data1.view(torch.int32).dtype)

#change the data1 data type to int64

print("Converting to int64: ",data1.view(torch.int64).dtype)

Output:

Actual Tensor data type: torch.float32

Converting to int8: torch.int8

Converting to int16: torch.int16

Converting to int32: torch.int32

Converting to int64: torch.int64

Example 2

In this example, we will create a tensor with Float type and convert it to int data types and get the size.

#import torch module

import torch

 

 

#create 1D tensor with Float data type that hold 6 elements

data1 = torch.FloatTensor([23,45,54,32,23,78])

#display

print("Actual Tensor datatype: ",data1.size())

#change the data1 datatype to int8

print("Converting to int8: ",data1.view(torch.int8).size())

#change the data1 datatype to int16

print("Converting to int16: ",data1.view(torch.int16).size())

#change the data1 datatype to int32

print("Converting to int32: ",data1.view(torch.int32).size())

#change the data1 datatype to int64

print("Converting to int64: ",data1.view(torch.int64).size())

Output:

Actual Tensor datatype: torch.Size([6])

Converting to int8: torch.Size([24])

Converting to int16: torch.Size([12])

Converting to int32: torch.Size([6])

Converting to int64: torch.Size([3])

Conclusion

In this PyTorch lesson, we discussed how to change the view of a tensor in pytorch using view() and also modify the datatypes of an existing tensor by specifying data types inside the view() method.

Share Button

Source: linuxhint.com

Leave a Reply