Sentiment classification part in Lesson 4 results in TypeError: 'TextDataLoader' object does not support indexing

#1

Hi,

I’m trying to follow along lesson4-imdb notebook on windows and everything is working nicely until I try to finetune the new layers for the sentiment analysis.

I think the data splits should be working correctly as looking at the first element produces the sentiment label and text as expected, and calling next(iter(md2.trn_ds)) returns a tensor of the correct batch size.

However when running the m3.fit(lrs/2, 1, metrics=[accuracy]) part, I’m getting the following error:

~\fastai\courses\dl1\fastai\dataloader.py in (.0)
66
67 def get_batch(self, indices):
—> 68 res = self.collate_fn([self.dataset[i] for i in indices], self.pad_idx)
69 if not self.transpose: return res
70 res[0] = res[0].T

TypeError: ‘TextDataLoader’ object does not support indexing

If someone has ideas on what I might be doing wrong, I would appreciate any help!

2 Likes

(RobG) #2

This appears to be a bug in a recent fastai release. I raised an issue on github a couple of days ago. Alas my python skills do not stretch to being able to debug and repair the issue myself. I reverted to an earlier version of the codebase and all is fine.

1 Like

#3

You are correct.

git checkout 6d0e116 worked for me.

Big thanks!

0 Likes

(Adam Ta) #4

I’m getting the same error here. Tried digging around the library too to no avail.

reverting to 6d0e116 fixed my problem.

Thanks phynnien and digitalspecialists for posting about this issue!

0 Likes

(Jeremy Howard (Admin)) #5

Thanks for letting me know - just pushed a fix. Let me know if you have any further problems.

1 Like

(German Goldszmidt) #6

Another Dataloader issue: TypeError: unsupported operand type(s) for *: ‘NoneType’ and ‘int’

from fastai.nlp import *
PATH=‘data/aclImdb/’
names = [‘neg’,‘pos’]
trn,trn_y = texts_labels_from_folders(f’{PATH}train’,names)
val,val_y = texts_labels_from_folders(f’{PATH}test’,names)
veczr = CountVectorizer(tokenizer=tokenize)
trn_term_doc = veczr.fit_transform(trn)
val_term_doc = veczr.transform(val)
md = TextClassifierData.from_bow(trn_term_doc, trn_y, val_term_doc, val_y, 2000)
learner = md.dotprod_nb_learner()
learner.fit(0.02, 1, wds=1e-6, cycle_len=1)

Epoch
0% 0/1 [00:00<?, ?it/s]
0%| | 0/391 [00:00<?, ?it/s]
TypeError Traceback (most recent call last)
in ()
----> 1 learner.fit(0.02, 1, wds=1e-6, cycle_len=1)

~/fastai/courses/ml1/fastai/learner.py in fit(self, lrs, n_cycle, wds, **kwargs)
250 self.sched = None
251 layer_opt = self.get_layer_opt(lrs, wds)
–> 252 return self.fit_gen(self.model, self.data, layer_opt, n_cycle, **kwargs)
253
254 def warm_up(self, lr, wds=None):

~/fastai/courses/ml1/fastai/learner.py in fit_gen(self, model, data, layer_opt, n_cycle, cycle_len, cycle_mult, cycle_save_name, best_save_name, use_clr, use_clr_beta, metrics, callbacks, use_wd_sched, norm_wds, wds_sched_mult, **kwargs)
197 n_epoch = sum_geom(cycle_len if cycle_len else 1, cycle_mult, n_cycle)
198 return fit(model, data, n_epoch, layer_opt.opt, self.crit,
–> 199 metrics=metrics, callbacks=callbacks, reg_fn=self.reg_fn, clip=self.clip, **kwargs)
200
201 def get_layer_groups(self): return self.models.get_layer_groups()

~/fastai/courses/ml1/fastai/model.py in fit(model, data, epochs, opt, crit, metrics, callbacks, stepper, **kwargs)
120 i = 0
121 if all_val: val_iter = IterBatch(data.val_dl)
–> 122 for (*x,y) in t:
123 batch_num += 1
124 for cb in callbacks: cb.on_batch_begin()

~/anaconda3/lib/python3.6/site-packages/tqdm/_tqdm.py in iter(self)
953 “”", fp_write=getattr(self.fp, ‘write’, sys.stderr.write))
954
–> 955 for obj in iterable:
956 yield obj
957 # Update and possibly print the progressbar.

~/fastai/courses/ml1/fastai/dataloader.py in iter(self)
81 with ThreadPoolExecutor(max_workers=self.num_workers) as e:
82 # avoid py3.6 issue where queue is infinite and can result in memory exhaustion
—> 83 for c in chunk_iter(iter(self.batch_sampler), self.num_workers*10):
84 for batch in e.map(self.get_batch, c): yield get_tensor(batch, self.pin_memory)
85

TypeError: unsupported operand type(s) for *: ‘NoneType’ and ‘int’

0 Likes

(jacob lawrence ) #8
TypeError                                 Traceback (most recent call last)
<ipython-input-11-7ca6121fb20f> in <module>
----> 1 learn=ConvLearner.pretrained(models.vgg16,data,precompute=False)

~/anaconda3/envs/img_cap/lib/python3.6/site-packages/fastai/conv_learner.py in pretrained(cls, f, data, ps, xtra_fc, xtra_cut, custom_head, precompute, pretrained, **kwargs)
    111                    pretrained=True, **kwargs):
    112         models = ConvnetBuilder(f, data.c, data.is_multi, data.is_reg,
--> 113             ps=ps, xtra_fc=xtra_fc, xtra_cut=xtra_cut, custom_head=custom_head, pretrained=pretrained)
    114         return cls(data, models, precompute, **kwargs)
    115 

~/anaconda3/envs/img_cap/lib/python3.6/site-packages/fastai/conv_learner.py in __init__(self, f, c, is_multi, is_reg, ps, xtra_fc, xtra_cut, custom_head, pretrained)
     39         cut-=xtra_cut
     40         layers = cut_model(f(pretrained), cut)
---> 41         self.nf = model_features[f] if f in model_features else (num_features(layers)*2)
     42         if not custom_head: layers += [AdaptiveConcatPool2d(), Flatten()]
     43         self.top_model = nn.Sequential(*layers)

TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

I have similar issue where i cant find solution for it, i try to use vgg16 model for training custom dataset, but when feeding the model along with the data in ConvLearner. This error occurs

0 Likes