Skip to content
Advertisement

Tag: pytorch

What’s the difference between torch.stack() and torch.cat() functions?

OpenAI’s REINFORCE and actor-critic example for reinforcement learning has the following code: REINFORCE: actor-critic: One is using torch.cat, the other uses torch.stack, for similar use cases. As far as my understanding goes, the doc doesn’t give any clear distinction between them. I would be happy to know the differences between the functions. Answer stack Concatenates sequence of tensors along a

How can i process multi loss in pytorch?

Such as this, I want to using some auxiliary loss to promoting my model performance. Which type code can implement it in pytorch? Thanks for your answer! Answer First and 3rd attempt are exactly the same and correct, while 2nd approach is completely wrong. In Pytorch, low layer gradients are Not “overwritten” by subsequent backward() calls, rather they are accumulated,

Convert PyTorch tensor to python list

How do I convert a PyTorch Tensor into a python list? I want to convert a tensor of size [1, 2048, 1, 1] into a list of 2048 elements. My tensor has floating point values. Is there a solution which also works with other data types such as int? Answer Use Tensor.tolist() e.g: To remove all dimensions of size 1,

Calling cuda() with async results in SyntaxError

I’m trying to run this PyTorch code: But when I try I am getting this error message: What am I doing wrong? I already installed cuda. Answer Your code does not work because: async is a reserved keyword in python which cannot be used in that way, that is why you get the SyntaxError cuda() no longer has an argument

Pytorch tensor – How to get the indexes by a specific tensor

I have a tensor and a query tensor Is there a way to get the indexes of q like in pytorch? Answer How about Comparing t == q performs element-wise comparison between t and q, since you are looking for entire row match, you need to .sum(dim=1) along the rows and see what row is a perfect match == t.size(1).

Data Augmentation in PyTorch

I am a little bit confused about the data augmentation performed in PyTorch. Now, as far as I know, when we are performing data augmentation, we are KEEPING our original dataset, and then adding other versions of it (Flipping, Cropping…etc). But that doesn’t seem like happening in PyTorch. As far as I understood from the references, when we use data.transforms

Calculate the accuracy every epoch in PyTorch

I am working on a Neural Network problem, to classify data as 1 or 0. I am using Binary cross entropy loss to do this. The loss is fine, however, the accuracy is very low and isn’t improving. I am assuming I did a mistake in the accuracy calculation. After every epoch, I am calculating the correct predictions after thresholding

How does pytorch broadcasting work?

produces a Tensor with size: torch.Size([4,4]). Can someone provide a logic behind this? Answer PyTorch broadcasting is based on numpy broadcasting semantics which can be understood by reading numpy broadcasting rules or PyTorch broadcasting guide. Expounding the concept with an example would be intuitive to understand it better. So, please see the example below: Now for torch.add(t_rand, t_ones), visualize it

How can I generate and display a grid of images in PyTorch with plt.imshow and torchvision.utils.make_grid?

I am trying to understand how torchvision interacts with mathplotlib to produce a grid of images. It’s easy to generate images and display them iteratively: However, displaying these images in a grid does not seem to be as straightforward. Even though PyTorch’s documentation indicates that w is the correct shape, Python says that it isn’t. So I tried to permute

Advertisement