TensorIterator expected type torch.cuda.DoubleTensor but got torch.cuda.FloatTensor

Hi everyone, i’m having the error from the title in a text Multilabel Classification problems. My data is already tokenized, so I use

data_clas = TextClasDataBunch.from_ids(dir_path,train_ids,train_lbls,val_ids,val_lbls, 258, bs = 16)

to create my DataBunch. I then create a RNNLearner with :

learn = RNNLearner.classifier(data_clas, bptt = bptt, max_len = bptt*20  ,qrnn=_p.qrnn, drop_mult=0.3) 

I make sure i have the loss i want with :

learn.loss_func = torch.nn.functional.binary_cross_entropy_with_logits

Now i want to find the best learning rate, so I do


But i get the following error :

RuntimeError: TensorIterator expected type torch.cuda.DoubleTensor but got torch.cuda.FloatTensor[8, 5]

I have 5 labels so that’s where the 5 is coming from. The weird thing is that i do almost the same exact thing with another dataset, the only difference being using TextClasDataBunch.from_id_files instead of TextClasDataBunch.from_id and it works perfectly there.

Any tips to solve that error would be welcomed :slight_smile:

PS : I’m not on the latest commit of fastai, i’m just before the refactoring of NLP.

That’s super weird. I don’t get why a DoubleTensor would be expected anywhere… Can you share the type of your labels passed as trn_lbl and the type you get when using from_id_fnames?

train_lbls and val_lbls are both float array obtained from :


This is a matrix with 5 columns. I changed the type, because if you have integers, you get long tensor which are not valid to compute BCEloss with logits.

In my other case when i use from_id_files, the labels are stored in an .npy files and are already converted to float.

I managed to fix my issue. I changed




And it worked. Maybe calling TextClasDataBunch.from_ids should convert the labels to float 32 automatically to avoid that kind of problem.

From 1.0.21 and onward, this is dealt with at the very base of the fastai datasets, so you shouldn’t have that problemn anymore.

1 Like


def get_ip(img,pts): return ImagePoints(FlowField(img.size , pts), scale = True ,y_first = False)

def format_ip(pts): return torch.from_numpy(np.array(np.split(pts,9)))

points = np.genfromtxt(‘CAT_01/00000100_002.jpg.cat’, delimiter= " ",usecols= cols);points

array([251., 477., 463., 489., 360., 672., 104., 350., 141., 110., 280., 269., 451., 282., 634., 144., 612., 394.])

pts = get_ip(img, format_ip(points));pts

RuntimeError: expected device cpu and dtype Double but got device cpu and dtype Float

Any function that turns double into float??