代码片段记录18

[不定长iterator的写法]

不确定iterator的长度的写法。

1
2
3
4
5
6
7
8
9
10
11
12
class :
def __next__(self):
span_length = self.c * self.data_len
rand_index = random.sample(list(range(math.ceil(span_length))), self.batch_size)
sampled_sents = [self.data['sents'][i] for i in rand_index]
sampled_labels = [self.data['labels'][i] for i in rand_index]
batch = {'sents': sampled_sents, 'labels': sampled_labels}
batch = convert2tensor(batch)
return batch

def __iter__(self):
return self