Text: How to prepare dataset for Hierarchical LSTM using torchtext?

Created on 21 May 2018  路  10Comments  路  Source: pytorch/text

I want to do document classification using hierarchical lstm described in http://aclweb.org/anthology/N/N16/N16-1174.pdf. I don't know how to prepare data for this task. Could anybody help me?

Most helpful comment

I know how to do that. Just use NestedField!

All 10 comments

Each document is stored in a separate file. I want to use sentence level RNN to get sentence level representation, which will be sent to document level RNN to get the document level representation. I don't know how to process data for this task using torchtext. Could anybody help me?

In another way, I want to construct dataset for list of sentences. The number of sentences is variable, and the number of words in a sentence is variable too.

I know how to do that. Just use NestedField!

@nh007cs did you figure this out?

@nh007cs Took a bit of poking around in the source code but I got this working in the end. Example below where I'm trying to predict where payments will come in on legal cases.

from torchtext import data
import spacy
import pandas as pd

df = pd.read_csv('./batch-by-matter.csv')

NLP = spacy.load('en')

def tokenizer(input):
    return [x.text for x in NLP.tokenizer(input) if x.text != " "]

NESTED_TEXT = data.Field(sequential=True, tokenize=tokenizer)
TEXT = data.NestedField(NESTED_TEXT)

df.sort_values(['MatterNo'], inplace=True)
df = df.groupby('MatterNo', as_index=False).agg({'Narrative': list, 'AnyPay2Months': list})

fields = [("MatterNo", data.RawField()), ("Narratives", TEXT), ("AnyPay2Months", None)]
examples = [data.Example.fromlist(i, fields) for i in df.values.tolist()]
dataset = data.Dataset(examples, fields)

TEXT.build_vocab(dataset, min_freq=1)

train_iter = data.Iterator(
    dataset,
    batch_size=2,
    shuffle=True,
    repeat=False)

for epoch in [1,2,3]:
    print('\nEpoch:' + str(epoch))
    for bnum ,b in enumerate(train_iter):
        print('\nBatch number: {}'.format(bnum+1))
        for matter in range(b.Narratives.shape[0]):
            all_narrative = ''
            for ex in range(b.Narratives.shape[1]):
               all_narrative += ' | ' + ' '.join([TEXT.vocab.itos[x] for x in b.Narratives[matter,ex].cpu().numpy()])
            print(all_narrative)

Data looks like this:

MatterNo            Narrative  AnyPay2Months

1 Opened case 0
1 Called client 0
1 Raised bill 0
1 Closed case 1
2 PI case 0
2 Not going well 0
2 Lost 0
3 Clin Neg case 0
3 Cut off wrong leg 0
3 Raised bill 0

@nh007cs Would you mind sharing a bit of your code? Thx!

I know how to do that. Just use NestedField!

Hi锛宑ould you please share your solving method? Thank you in advance!

@larry0123du @yyHaker The code is as follows:

image
image
image

@nh007cs Thank you for proving your code. I have a doubt, where are you using the DataIter class in your code? I am just trying to understand the flow of the code.

My problem is similar, I have to read my data and each sample has three fields: first list of texts, second list of texts, and a label associated with these two lists. Then length of each list is a variable and each sentence in list has variable length as well.

Thanks for sharing the code, I was wondering how much does it take for you to run the code, I'm using Tabulardataset.split function and it runs forever. If I use the data.Dataset,on the other hand, since defined fields are not part of the training set, I cannot run the build_vocab on the training_dataset

Was this page helpful?
0 / 5 - 0 ratings