Learn practical skills, build real-world projects, and advance your career
Updated 4 years ago
PyTorch Functions
Manipulating torch.Tensor objects.
In this article, you can find definitions and examples of 5 PyTorch functions that can be used to aid in the manipulation of tensor objects. These functions are:
- torch.as_tensor
- torch.cat
- torch.index_select
- torch.reshape
- torch.t
# Import torch and other required modules
import torch
import numpy as np
import pandas as pd
Function 1 - torch.as_tensor
torch.as_tensor(data, dypte=None, device=None)
This function is used to convert data into a torch.Tensor object and can take in a variety of data types. The ease of conversion allows data scientists to manipulate the data using a library like Numpy before utilzing the power of Torch to build models.
Parameters:
- data: This is the initial data that will be converted into a tensor and can be a list, tuple, Numpy ndarray, scalar, and others.
- dypte (optional): the desired data type of the returned tensor. Default is None, which means it will infer the data type from data.
- device (option): the desired device of the returned tensor. Default is None, and this will use the current device for the default tensor type - CPU for CPU tensor types and CUDA for CUDA tensor types
# Example 1 - Working
a = 1
torch.as_tensor(a)
tensor(1)
With a scalar as an input.