Text: Can't deal with text label (invalid literal for int() with base 10: 'my_label1')

Created on 16 Nov 2018  路  5Comments  路  Source: pytorch/text

I wanna do a multi-class classification task
Here is my sample data (updated, thanks to @bentrevett )

text, label
My string 1, my_label1
My string 2, my_label2
My string 3, my_label3
My string 4, my_label3
My string 5, my_label4
...

Sample code

TEXT = data.Field(sequential=True, tokenize=word_tokenize, lower=True, fix_length=None)
LABEL = data.Field(sequential=False, use_vocab=False, unk_token=None)

train, valid, test = data.TabularDataset.splits(
    path=path , train='train.csv', validation='valid.csv', test='test.csv',
    skip_header=True, format='csv',
    fields=[('text', TEXT), ('label', LABEL)])

# Building vocabulary
TEXT.build_vocab(train, valid, test, max_size=10000, 
                 vectors='glove.6B.300d',  
                 unk_init=torch.nn.init.xavier_uniform_)
LABEL.build_vocab(train, valid, test)
vocab = TEXT.vocab

iter_train, iter_valid = data.BucketIterator.splits((train, valid), batch_size=64, device=device, sort_key=lambda x: len(x.text), sort_within_batch=False, repeat=False)
iter_test = data.Iterator(test, batch_size=64, train=False, device=device, sort=False,  sort_within_batch=False, repeat=False)

When I call

batch = next(iter(iter_valid))
batch.text

It will pop out error invalid literal for int() with base 10: 'my_label1'
Can anyone give me any hint?
Google for whole day but still can't solve it...

Most helpful comment

You have set use_vocab to False for your LABEL field, this is incorrect.

From: https://github.com/pytorch/text/blob/master/torchtext/data/field.py#L80
You should only set use_vocab = False when your labels are already integer values. TorchText is trying to convert "my_label1" into an integer, which it can't, hence it throws this error.

Also, your fields argument to data.TabularDataset.splits are the wrong way around. In your example dataset your label is first, whereas you've got TEXT first in your fields argument. TorchText doesn't do any matching of strings to headers in .csv files.

All 5 comments

Hi did you get any solution yet? I have a similar problem

You have set use_vocab to False for your LABEL field, this is incorrect.

From: https://github.com/pytorch/text/blob/master/torchtext/data/field.py#L80
You should only set use_vocab = False when your labels are already integer values. TorchText is trying to convert "my_label1" into an integer, which it can't, hence it throws this error.

Also, your fields argument to data.TabularDataset.splits are the wrong way around. In your example dataset your label is first, whereas you've got TEXT first in your fields argument. TorchText doesn't do any matching of strings to headers in .csv files.

I have use_vocab = False for my labels, and I am running into this same issue at about 40% through training my first epoch. It is odd that it is coming up after training for a bit.

Here is how I am handling my data:

class DataSet:

    def __init__(self, path):
        self.path = path
        self.textfield = Field(sequential=True, tokenize=tokenizer, lower=True)
        self.labelfield = Field(sequential=False, use_vocab=False)

    def __repr__(self):
        return f'Competition dataset at {self.path}'

    def load_splits(self):
        print(f'Tokenizing data...')
        train, val, test = TabularDataset.splits(
            path=self.path, 
            train='train.csv',
            validation='val.csv', 
            test='test.csv', 
            format='csv',
            fields=[
                ('id', None),
                ('text', self.textfield),
                ('genre', None),
                ('labels', self.labelfield)
            ]
        )
        return train, val, test

I went through all the labels in my data, each one of them is an int, so no sneaky strs in there.

I think I have my workaround. Instead of using the integer valued labels from my csv, I am switching over genre, which is a string, and setting use_vocab=True.

class DataSet:

    def __init__(self, path):
        self.path = path
        self.textfield = Field(sequential=True, tokenize=tokenizer, lower=True)
        self.labelfield = Field(sequential=False, use_vocab=True)

    def __repr__(self):
        return f'Competition dataset at {self.path}'

    def load_splits(self):
        print(f'Tokenizing data...')
        train, val, test = TabularDataset.splits(
            path=self.path, 
            train='train.csv',
            validation='val.csv', 
            test='test.csv', 
            format='csv',
            fields=[
                ('id', None),
                ('text', self.textfield),
                ('genre', self.labelfield),
                ('labels', None)
            ]
        )

        self.textfield.build_vocab(train, val, test, vectors='glove.6B.100d')
        self.labelfield.build_vocab(train, val)
        return train, val, test

Feel free to re-open the issue if you still have any questions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wpfnlp picture wpfnlp  路  5Comments

aatkinson picture aatkinson  路  3Comments

pasinit picture pasinit  路  3Comments

D-i-l-r-u-k-s-h-i picture D-i-l-r-u-k-s-h-i  路  4Comments

felipebravom picture felipebravom  路  3Comments