"NameError: name 'T' is not defined" Deep Learning Part 2(dl2) Lesson 10 IMDB


(Aashish Gangwani) #1

Hi,

I am trying to run imdb.ipynb(from dl2 lesson 10) and I have reached the ‘wikitext103 conversion’ topic’s last cell.

wgts['0.encoder.weight'] = T(new_w)
wgts['0.encoder_with_dropout.embed.weight'] = T(np.copy(new_w))
wgts['1.decoder.weight'] = T(np.copy(new_w))

But it gives this error:

NameError                                 Traceback (most recent call last)
<ipython-input-294-678a783a7cd5> in <module>()
----> 1 wgts['0.encoder.weight'] = T(new_w)
      2 wgts['0.encoder_with_dropout.embed.weight'] = T(np.copy(new_w))
      3 wgts['1.decoder.weight'] = T(np.copy(new_w))

NameError: name 'T' is not defined

Basically, it can’t find the function T()

Here is all what I have imported:
from fastai.text import *
import html
import os
from pathlib import Path
import pandas as pd
import numpy as np
import scipy
import sklearn
import sklearn.model_selection
import re
import torch

What all have I tried doing?
I have searched about this on the internet but I can’t find anything about the function “T()”

I came to know about numpy.ndarray.T which is an Attribute which transposes the ndarray but there is no function “T()” anywhere.

I also searched in fastai repo using git search but I couldn’t find it. I think it’s a function from fastai or torch and I am not sure what exactly it does.

Can somebody please help?


(Aashish Gangwani) #2

Hey @jeremy, Sorry to tag you on this but I really tried searching the answer today too and couldn’t find it. If you could please take some time to look at this once?

Sorry, its really urgent for me.


#3

I think you’re looking for “import torch.tensor as T” ?


(Aashish Gangwani) #4

Hey @claeyzre, Thanks so much! I am past that now but again stuck at this one:
trn_dl = LanguageModelLoader(np.concatenate(trn_lm), bs, bptt)
val_dl = LanguageModelLoader(np.concatenate(val_lm), bs, bptt)
md = LanguageModelData(PATH, 1, vs, trn_dl, val_dl, bs=bs, bptt=bptt)

AttributeError                            Traceback (most recent call last)
<ipython-input-111-12702c19051f> in <module>()
----> 1 trn_dl = LanguageModelLoader(np.concatenate(trn_lm), bs, bptt)
      2 val_dl = LanguageModelLoader(np.concatenate(val_lm), bs, bptt)
      3 md = LanguageModelData(PATH, 1, vs, trn_dl, val_dl, bs=bs, bptt=bptt)

/usr/local/lib/python3.6/dist-packages/fastai/text/data.py in __init__(self, dataset, bs, bptt, backwards, shuffle, max_len)
    211         self.dataset,self.bs,self.bptt,self.backwards,self.shuffle = dataset,bs,bptt,backwards,shuffle
    212         self.first,self.i,self.iter = True,0,0
--> 213         self.n = len(np.concatenate(dataset.x)) // self.bs
    214         self.max_len,self.num_workers = max_len,0
    215 

AttributeError: 'numpy.ndarray' object has no attribute 'x'

I don’t get what ‘x’ is here, any ideas?

Thanks again for telling about the T function :slight_smile:


#5

You are using a wrong version of fastai,
Use this installation guide if you want to make it work correctly Fastai v0.7 install issues thread