Found 135 Articles for PyTorch

PyTorch – How to compute the determinant of a square matrix?

Shahid Akhtar Khan
Updated on 07-Jan-2022 06:14:10

567 Views

To compute the determinant of a square matrix, we could apply torch.linalg.det() method. It returns a new tensor with computed determinant. It accepts a square matrix, a batch of square matrices and also batches of square matrices. It supports matrix of float, double, cfloat, and cdouble data types.We could also apply torch.det() method to compute the determinant. It is an alias of the torch.linalg.det() method.Syntaxtorch.linalg.det(mat) torch.det(mat)Where mat is a square matrix or batch/s of square matrices. A matrix is a 2D torch tensor.StepsWe could use the following steps to compute determinant of a square matrix −Import the required library. In ... Read More

PyTorch – How to compute the logistic sigmoid function of tensor elements?

Shahid Akhtar Khan
Updated on 07-Jan-2022 06:12:35

879 Views

To compute the logistic function of elements of a tensor, we use torch.special.expit() method. It returns a new tensor with computed logistic function element-wise. It accepts torch tensor of any dimension. We could also apply torch.sigmoid() method to compute the logistic function of elements of the tensor. It is an alias of the torch.special.expit() method.Syntaxtorch.special.expit(input) torch.sigmoid(input)Where input is a torch tensor of any dimension.StepsWe could use the following steps to compute logistic sigmoid function of a tensor element-wise −Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.import ... Read More

PyTorch – How to compute the error function of a tensor?

Shahid Akhtar Khan
Updated on 07-Jan-2022 06:10:14

280 Views

To compute the error function of a tensor, we use the torch.special.erf() method. It returns a new tensor with computed error function. It accepts torch tensor of any dimension. It is also known as Gauss error functionStepsWe could use the following steps to compute the error function of a tensor element-wise −Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.import torchDefine a torch tensor. Here we define a 2D tensor of random numbers.tensor = torch.randn(2, 3, 3)Compute the error function of the above-defined tensor using torch.special.erf(tensor). Optionally ... Read More

PyTorch – How to compute element-wise entropy of an input tensor?

Shahid Akhtar Khan
Updated on 06-Jan-2022 13:29:57

2K+ Views

To compute the element-wise entropy of an input tensor, we use torch.special.entr() method. It returns a new tensor with entropy computed element-wise.If the element of tensor is negative, the entropy is negative infinity.If the element of the tensor is a zero, the entropy is zero.The entropy for a positive number element is computed as the negative value of the element multiplied by its natural logarithm. It accepts torch tensor of any dimension.StepsWe could use the following steps to compute the entropy on a tensor element-wise −Import the required library. In all the following examples, the required Python library is torch. Make ... Read More

How to convert a Torch Tensor to PIL image?

Shahid Akhtar Khan
Updated on 10-Sep-2023 08:19:41

35K+ Views

The ToPILImage() transform converts a torch tensor to PIL image. The torchvision.transforms module provides many important transforms that can be used to perform different types of manipulations on the image data. ToPILImage() accepts torch tensors of shape [C, H, W] where C, H, and W are the number of channels, image height, and width of the corresponding PIL images, respectively.StepsWe could use the following steps to convert a torch tensor to a PIL image −Import the required libraries. In all the following examples, the required Python libraries are torch, Pillow, and torchvision. Make sure you have already installed them.import torch ... Read More

PyTorch – torchvision.transforms – RandomErasing()

Shahid Akhtar Khan
Updated on 06-Jan-2022 13:04:01

1K+ Views

The RandomErasing() transform randomly selects a rectangular region in an input image and erases its pixels. The torchvision.transforms module provides many important transforms that can be used to perform different types of manipulations on the image data. RandomErasing() transformation accepts only tensor images of any size. A tensor image is a torch tensor.As this transform supports only tensor image, the PIL images should be first converted to a torch tensor. And after applying the RandomErasing() transform, we convert torch tensor image to PIL image.StepsWe could use the following steps to randomly select a rectangular region in an input image and ... Read More

PyTorch – How to normalize an image with mean and standard deviation?

Shahid Akhtar Khan
Updated on 06-Jan-2022 12:28:30

6K+ Views

The Normalize() transform normalizes an image with mean and standard deviation. The torchvision.transforms module provides many important transforms that can be used to perform different types of manipulations on the image data.Normalize() accepts only tensor images of any size. A tensor image is a torch tensor. A tensor image may have n number of channels. The Normalize() transform normalizes the tensor image for each channel.As this transform supports only tensor image, the PIL images should be first converted to a torch tensor. And after applying Normalize() transform, we convert the normalized torch tensor to a PIL image.StepsWe could use the ... Read More

PyTorch – How to invert the colors of an image randomly with a given probability?

Shahid Akhtar Khan
Updated on 06-Jan-2022 13:47:32

505 Views

The RandomInvert() transform inverts the colors of an image randomly with a given probability. The torchvision.transforms module provides many important transforms that can be used to perform different types of manipulations on the image data.RandomInvert() accepts both PIL and tensor images or batch of tensor images. A tensor image is a PyTorch Tensor with shape [3, H, W], where H is the image height and W is the image width. A batch of tensor images is also a torch tensor with [B, 3, H, W] where B is the number of images in the batch.Syntaxtorchvision.transforms.RandomInvert(p)(img)It returns a randomly color inverted ... Read More

PyTorch – torchvision.transforms – GaussianBlur()

Shahid Akhtar Khan
Updated on 06-Jan-2022 11:42:24

5K+ Views

The torchvision.transforms module provides many important transformations that can be used to perform different types of manipulations on the image data. GaussianBlur() transformation is used to blur an image with randomly chosen Gaussian blur.The GaussianBlur() transformation accepts both PIL and tensor images or a batch of tensor images. A tensor image is a PyTorch Tensor with shape [3, H, W], where H is the image height and W is the image width. A batch of tensor images is also a torch tensor with [B, 3, H, W] where B is the number of images in the batch.Syntaxtorchvision.transforms.GaussianBlur(kernel_size, sigma=(0.1, .2))(img)kernel_size – ... Read More

PyTorch – How to resize an image to a given size?

Shahid Akhtar Khan
Updated on 06-Jan-2022 11:38:02

28K+ Views

The Resize() transform resizes the input image to a given size. It's one of the transforms provided by the torchvision.transforms module. Resize() accepts both PIL and tensor images. A tensor image is a torch tensor with shape [C, H, W], where C is the number of channels, H is the image height, and W is the image width.This transform also accepts a batch of tensor images, which is a tensor with [B, C, H, W] where B is the number of images in the batch. If the image is neither a PIL image nor a tensor image, then we first convert ... Read More

Previous 1 ... 6 7 8 9 10 ... 14 Next
Advertisements