Value Error in Lesson 4 -NLP

After successfully reproducing the sentiment training on IMDB of lesson 4 I tried to reproduce it on my own data set.

The code I used to generate the datasets and training.
home = os.getenv(“HOME”)
PATH = home + “/fastai/courses/dl1/”
arquivo = “model_txt_sedi.pkl”
opt_fn = partial(optim.Adam, betas=(0.7, 0.99))
TEXT = pickle.load(open(arquivo,‘rb’))
spacy_tok_pt = spacy.load(‘pt_core_news_sm’)

def tokenizer(text): # create a tokenizer function
return [tok.text for tok in spacy_tok_pt.tokenizer(text)]

LABEL = data.Field(sequential=False, use_vocab=False )
splits = data.TabularDataset.splits(path=PATH, train=‘train_.csv’, validation=‘val_.csv’, test=‘test_.csv’, format=‘csv’, fields=[(‘text’, TEXT), (‘label’, LABEL)])

md2 = TextData.from_splits(PATH, splits, bs)
m3 = md2.get_model(opt_fn, 1500, bptt, emb_sz=em_sz, n_hid=nh, n_layers=nl,
dropout=0.1, dropouti=0.4, wdrop=0.5, dropoute=0.05, dropouth=0.3)
m3.reg_fn = partial(seq2seq_reg, alpha=2, beta=1)
m3.freeze_to(-1), 3)
m3.unfreeze(), 3, metrics=[accuracy], cycle_len=1)
However after the first epoch I receive the following Error:

100%|██████████| 313/313 [03:17<00:00, 1.58it/s, loss=0.0891]
Traceback (most recent call last):
File “/Applications/PyCharm”, line 53, in run_file
pydev_imports.execfile(file, globals, locals) # execute the script
File “/Applications/PyCharm”, line 18, in execfile
exec(compile(contents+"\n", file, ‘exec’), glob, loc)
File “/Users/castilla/fastai/courses/dl1/”, line 58, in, 3)
File “/Users/castilla/fastai/courses/dl1/fastai/”, line 287, in fit
return self.fit_gen(self.model,, layer_opt, n_cycle, **kwargs)
File “/Users/castilla/fastai/courses/dl1/fastai/”, line 234, in fit_gen
swa_eval_freq=swa_eval_freq, **kwargs)
File “/Users/castilla/fastai/courses/dl1/fastai/”, line 159, in fit
vals = validate(model_stepper, cur_data.val_dl, metrics)
File “/Users/castilla/fastai/courses/dl1/fastai/”, line 216, in validate
for (*x,y) in iter(dl):
File “/Users/castilla/fastai/courses/dl1/fastai/”, line 324, in iter
b = next(it)
File “/Users/castilla/anaconda3/envs/fastai-cpu/lib/python3.6/site-packages/torchtext/data/”, line 134, in iter
File “/Users/castilla/anaconda3/envs/fastai-cpu/lib/python3.6/site-packages/torchtext/data/”, line 111, in init_epoch
File “/Users/castilla/anaconda3/envs/fastai-cpu/lib/python3.6/site-packages/torchtext/data/”, line 234, in create_batches
self.batches = batch(, self.batch_size, self.batch_size_fn)
File “/Users/castilla/anaconda3/envs/fastai-cpu/lib/python3.6/site-packages/torchtext/data/”, line 96, in data
xs = sorted(self.dataset, key=self.sort_key)
TypeError: ‘<’ not supported between instances of ‘Example’ and ‘Example’

Any clues where my code is failing?

1 Like

I have the exact same problem.

For some reason it is working when using the IMDB dataset from torchtext:

splits = torchtext.datasets.IMDB.splits(TEXT, IMDB_LABEL, PATH)
md2 = TextData.from_splits(PATH, splits, bs)

This is not working:

splits = data.TabularDataset.splits(
        path=PATH, train='train.tsv',
        validation='valid.tsv', test='testData.tsv', format='tsv',
        fields=[('Text', TEXT), ('Label', IMDB_LABEL)],
# For some reason the field names in the Examples are capitalized. But this shouldn't cause a problem (?)
md2 = TextData.from_splits(PATH, splits, bs, text_name='Text', label_name='Label')

Results in:

TypeError: ‘<’ not supported between instances of ‘Example’ and ‘Example’

One clue to what could be wrong is that it takes 01:51 to run an epoch on the IMDB dataset, while it takes 00:02 to run one epoch on the custom dataset.

This is surprising, considering that both datasets contain IMDB movie reviews and the IMDB set has:
Training set: 25000 examples
Validation set: 25000 examples

While the custom dataset has:

train, val, test = splits
len(train.examples), len(val.examples), len(test.examples)

Training set: 20000 examples
Validation set: 5000 examples
Test set: 25000 examples

Anyone have any clue how to fix this?


I have exactly the same problem when using TabularDataset. Did you get this worked out?



Hi. Sorry for the late reply.

No, unfortunately I never got this to work.

No problem - I did get round it:

1 Like