Skip to content
Advertisement

Tag: pytorch

TypeError: expected CPU (got CUDA)

X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0) y_train import torch import torch.nn as nn import torch.nn.functional as F when I Run this code I got this error: How to can I solve this error? Answer To transfer the variables to GPU, try the following:

Convert 5D tensor to 4D tensor in PyTorch

In PyTorch I have a 5D tensor X of dimensions B x 9 x C x H x W. I want to convert it into a 4D tensor Y with dimensions B x 9C x H x W such that concatenation happens channel wise. To illustrate let, Then in the tensor Y, a to i should be channel wise concatenated.

Pytorch’s nn.TransformerEncoder “src_key_padding_mask” not functioning as expected

Im working with Pytorch’s nn.TransformerEncoder module. I got input samples with (as normal) the shape (batch-size, seq-len, emb-dim). All samples in one batch have been zero-padded to the size of the biggest sample in this batch. Therefore I want the attention of the all zero values to be ignored. The documentation says, to add an argument src_key_padding_mask to the forward

How to get the last index of model’s prediction?

I am new to PyTorch. I have a variable pred which has a list of a tensor. So I wanted to access the last element which is the class. I did that by first converting the list into a tensor. Now, how do I access the last element or is there any better/efficient way of doing this? EDIT: For further

BERT DataLoader: Difference between shuffle=True vs Sampler?

I trained a DistilBERT model with DistilBertForTokenClassification on ConLL data fro predicting NER. Training seem to have completed with no problems but I have 2 problems during evaluation phase. I’m getting negative loss value During training, I used shuffle=True for DataLoader. But during evaluation, when I do shuffle=True for DataLoader, I get very poor metric results(f_1, accuracy, recall etc). But

How to understand creating leaf tensors in PyTorch?

From PyTorch documentation: But why are e and f leaf Tensors, when they both were also cast from a CPU Tensor, into a Cuda Tensor (an operation)? Is it because Tensor e was cast into Cuda before the in-place operation requires_grad_()? And because f was cast by assignment device=”cuda” rather than by method .cuda()? Answer When a tensor is first

torch.unique does not work for float tensors

I am trying to extract the unique elements from a float tensor. I have tried : However this method only works for int/long tensor. My tensor is quantizied tensor in a non-uniform way, thus its guaranteed to have a small set of float values. Answer You could using numpy.unique instead Outputs:

Multiple values for argument

I am trying to convert this code passing it with pysyft refference like this : But when I try to create a instance of the model I got a TypeError TypeError: multiple values for argument ‘torch_ref’ I tried to change the order of the arguments but i got an error about positional arguments . Can you help me , I

Advertisement