Listado de la etiqueta: 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:

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:

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 coetáneo 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(«Coetáneo 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:

Coetáneo 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(«Coetáneo 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:

Coetáneo 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(«Coetáneo 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:

Coetáneo 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(«Coetáneo 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:

Coetáneo 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.



Source link


tensor([[0.2807, 0.0260, 0.3326,0.1958, 2.7080],

[ 1.3534,0.2371, 0.0085, 0.1877, 1.4870],

[ 1.2967, 0.4262,0.6323, 0.4446, 3.0513],

[ 0.4478,0.0436,0.4577, 1.3098, 0.7293],

[0.4575,1.4020,0.9323,0.4406, 0.5844]])

natural log values:

tensor([[ nan,3.6494,1.1009, nan, 0.9962],

[ 0.3026, nan,4.7711,1.6731, 0.3968],

[ 0.2598,0.8529, nan,0.8107, 1.1156],

[0.8034, nan, nan, 0.2699,0.3157],

[ nan, nan, nan, nan,0.5371]])

logarithmic values to the almohadilla 2:

tensor([[ nan,5.2650,1.5882, nan, 1.4372],

[ 0.4366, nan,6.8833,2.4138, 0.5724],

[ 0.3748,1.2304, nan,1.1696, 1.6094],

[1.1591, nan, nan, 0.3893,0.4554],

[ nan, nan, nan, nan,0.7749]])

logarithmic values to the almohadilla 10:

tensor([[ nan,1.5849,0.4781, nan, 0.4327],

[ 0.1314, nan,2.0721,0.7266, 0.1723],

[ 0.1128,0.3704, nan,0.3521, 0.4845],

[0.3489, nan, nan, 0.1172,0.1371],

[ nan, nan, nan, nan,0.2333]])



Source link