TypeError: '<' not supported between instances of 'Example' and 'Example'

dedf21972b190932c756: <p>i’m making sentiment analysis in Korean version.<br>
But I got this error, “TypeError: ‘&lt;’ not supported between instances of ‘Example’ and ‘Example’”</p>
<h2>This is my abstract code :</h2>
<p>import torch<br>
from torchtext import data<br>
from torchtext import datasets<br>
from soynlp.tokenizer import MaxScoreTokenizer<br>
SEED = 1234</p>
<p>torch.manual_seed(SEED)<br>
torch.backends.cudnn.deterministic = True</p>
<p><span class="hashtag">#When</span> tokenizing, some text will be null<br>
TEXT = data.Field(tokenize = tokenizer, preprocessing = generate_bigrams, stop_words = stop_words)<br>
LABEL = data.LabelField(dtype = torch.float)</p>
<p>from torchtext.data import TabularDataset<br>
fields = [(“text”, TEXT),(“label”, LABEL)]</p>
<p>train_data, test_data = data.TabularDataset.splits(<br>
path = ‘’,<br>
train = ‘train.csv’,<br>
test = ‘train.csv’,<br>
format = ‘csv’,<br>
fields = fields,<br>
skip_header = True<br>
)</p>
<p>import torchtext<br>
vec = torchtext.vocab.Vectors(‘wiki.ko.vec’, cache=’./Downloads/’)<br>
TEXT.build_vocab(train_data,<br>
max_size = MAX_VOCAB_SIZE,<br>
vectors = vec,<br>
unk_init = torch.Tensor.normal_)<br>
LABEL.build_vocab(train_data)</p>
<p>BATCH_SIZE = 32</p>
<p>device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)</p>
<p>train_iterator, test_iterator = data.BucketIterator.splits(<br>
(train_data, test_data),<br>
batch_size = BATCH_SIZE,<br>
device = device)<br>
print(‘Train:’)<br>
for batch in train_iterator:<br>
print(batch)</p>
<h2>print(‘Test:’)<br>
for batch in test_iterator:<br>
print(batch)</h2>
<p>And then, I got the this error</p>
<h2>Test:</h2>
<p>TypeError                                 Traceback (most recent call last)<br>
 in <br>
12<br>
13 print(‘Test:’)<br>
—&gt; 14 for batch in test_iterator:<br>
15     print(batch)</p>
<p>/anaconda3/lib/python3.7/site-packages/torchtext/data/iterator.py in <strong>iter</strong>(self)<br>
140     def <strong>iter</strong>(self):<br>
141         while True:<br>
–&gt; 142             self.init_epoch()<br>
143             for idx, minibatch in enumerate(self.batches):<br>
144                 # fast-forward if loaded from state</p>
<p>/anaconda3/lib/python3.7/site-packages/torchtext/data/iterator.py in init_epoch(self)<br>
116             self._random_state_this_epoch = self.random_shuffler.random_state<br>
117<br>
–&gt; 118         self.create_batches()<br>
119<br>
120         if self._restored_from_state:</p>
<p>/anaconda3/lib/python3.7/site-packages/torchtext/data/iterator.py in create_batches(self)<br>
240     def create_batches(self):<br>
241         if self.sort:<br>
–&gt; 242             self.batches = batch(self.data(), self.batch_size,<br>
243                                  self.batch_size_fn)<br>
244         else:</p>
<p>/anaconda3/lib/python3.7/site-packages/torchtext/data/iterator.py in data(self)<br>
101         “”“Return the examples in the dataset in order, sorted, or shuffled.”""<br>
102         if self.sort:<br>
–&gt; 103             xs = sorted(self.dataset, key=self.sort_key)<br>
104         elif self.shuffle:<br>
105             xs = [self.dataset[i] for i in self.random_shuffler(range(len(self.dataset)))]</p>
<p>TypeError: ‘&lt;’ not supported between instances of ‘Example’ and ‘Example’<br>
I don’t know why the error happend, even I set the same file in train and test for finding the reasons!!</p>
<p>Thank you in advance.</p>

alex.veuthey: <p>This happens because the dataset iterator from <code>torchtext</code> tries to sort the dataset, which implies comparing two elements.</p>
<p>Apparently this is a known “feature” and you have to provide a sorting function to the constructor of the <code>BucketIterator</code>, as seen in <a href="https://github.com/pytorch/text/issues/474" rel="nofollow noopener">this issue</a>.</p>