本文整理汇总了Python中torchtext.data.RawField方法的典型用法代码示例。如果您正苦于以下问题:Python data.RawField方法的具体用法?Python data.RawField怎么用?Python data.RawField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torchtext.data
的用法示例。
在下文中一共展示了data.RawField方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_process
# 需要导入模块: from torchtext import data [as 别名]
# 或者: from torchtext.data import RawField [as 别名]
def test_process(self):
raw_field = data.RawField()
field = data.Field(sequential=True, use_vocab=False, batch_first=True)
# Test tensor-like batch data which is accepted by both RawField and Field
batch = [[1, 2, 3], [2, 3, 4]]
batch_tensor = torch.LongTensor(batch)
raw_field_processed = raw_field.process(batch)
field_processed = field.process(batch, device=-1, train=False)
assert raw_field_processed == batch
assert field_processed.data.equal(batch_tensor)
# Test non-tensor data which is only accepted by RawField
any_obj = [object() for _ in range(5)]
raw_field_processed = raw_field.process(any_obj)
assert any_obj == raw_field_processed
with pytest.raises(TypeError):
field.process(any_obj)
示例2: test_process
# 需要导入模块: from torchtext import data [as 别名]
# 或者: from torchtext.data import RawField [as 别名]
def test_process(self):
raw_field = data.RawField()
field = data.Field(sequential=True, use_vocab=False, batch_first=True)
# Test tensor-like batch data which is accepted by both RawField and Field
batch = [[1, 2, 3], [2, 3, 4]]
batch_tensor = torch.LongTensor(batch)
raw_field_processed = raw_field.process(batch)
field_processed = field.process(batch)
assert raw_field_processed == batch
assert field_processed.data.equal(batch_tensor)
# Test non-tensor data which is only accepted by RawField
any_obj = [object() for _ in range(5)]
raw_field_processed = raw_field.process(any_obj)
assert any_obj == raw_field_processed
with pytest.raises(TypeError):
field.process(any_obj)
示例3: __init__
# 需要导入模块: from torchtext import data [as 别名]
# 或者: from torchtext.data import RawField [as 别名]
def __init__(self, args):
self.RAW = data.RawField()
self.RAW.is_target = False
tokenize = lambda x: list(x)
self.TEXT = data.Field(batch_first=True, tokenize=tokenize)
self.LABEL = data.Field(sequential=False, unk_token=None)
self.train, self.dev, self.test = data.TabularDataset.splits(
path='/data/nfsdata/nlp/datasets/sentence_pair/bq_corpus_torch10',
train='BQ_train.json',
validation='BQ_dev.json',
test='BQ_test.json',
format='json',
fields={"gold_label": ("label", self.LABEL),
"sentence1": ("q1", self.TEXT),
"sentence2": ("q2", self.TEXT),
"ID": ("id", self.RAW)})
self.TEXT.build_vocab(self.train, self.dev, self.test, vectors=Vectors("BQ300", args.data))
self.LABEL.build_vocab(self.train)
sort_key = lambda x: data.interleave_keys(len(x.q1), len(x.q2))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.train_iter = data.BucketIterator(self.train, batch_size=args.batch_size, device=device, sort_key=sort_key, sort=True)
self.dev_iter = data.BucketIterator(self.dev, batch_size=args.batch_size, device=device, sort_key=sort_key, sort=True)
self.test_iter = data.BucketIterator(self.test, batch_size=args.batch_size, device=device, sort_key=sort_key, sort=True)
示例4: __init__
# 需要导入模块: from torchtext import data [as 别名]
# 或者: from torchtext.data import RawField [as 别名]
def __init__(self, path, format, fields, skip_header=True, **kwargs):
super(WikiQA, self).__init__(path, format, fields, skip_header, **kwargs)
# We want to keep a raw copy of the sentence for some models and for debugging
RAW_TEXT_FIELD = RawField()
for ex in self.examples:
raw_sentence_a, raw_sentence_b = ex.sentence_a[:], ex.sentence_b[:]
setattr(ex, 'raw_sentence_a', raw_sentence_a)
setattr(ex, 'raw_sentence_b', raw_sentence_b)
self.fields['raw_sentence_a'] = RAW_TEXT_FIELD
self.fields['raw_sentence_b'] = RAW_TEXT_FIELD
示例5: iters
# 需要导入模块: from torchtext import data [as 别名]
# 或者: from torchtext.data import RawField [as 别名]
def iters(cls, batch_size=64, device=-1, shuffle=True, vectors='glove.840B.300d'):
cls.TEXT = Field(sequential=True, tokenize='spacy', lower=True, batch_first=True)
cls.LABEL = Field(sequential=False, use_vocab=False, batch_first=True, tensor_type=torch.FloatTensor, postprocessing=Pipeline(get_class_probs))
cls.ID = RawField()
train, val, test = cls.splits(cls.TEXT, cls.LABEL, cls.ID)
cls.TEXT.build_vocab(train, vectors=vectors)
return BucketIterator.splits((train, val, test), batch_size=batch_size, shuffle=shuffle, repeat=False, device=device)
示例6: __init__
# 需要导入模块: from torchtext import data [as 别名]
# 或者: from torchtext.data import RawField [as 别名]
def __init__(self, path, format, fields, skip_header=True, **kwargs):
super(SICK, self).__init__(path, format, fields, skip_header, **kwargs)
# We want to keep a raw copy of the sentence for some models and for debugging
RAW_TEXT_FIELD = RawField()
for ex in self.examples:
raw_sentence_a, raw_sentence_b = ex.sentence_a[:], ex.sentence_b[:]
setattr(ex, 'raw_sentence_a', raw_sentence_a)
setattr(ex, 'raw_sentence_b', raw_sentence_b)
self.fields['raw_sentence_a'] = RAW_TEXT_FIELD
self.fields['raw_sentence_b'] = RAW_TEXT_FIELD