class DictFieldTest(unittest.TestCase): def setUp(self): self.dict_field = DictFeatureField( batch_first=True, pad_token=VocabMeta.PAD_TOKEN, unk_token=VocabMeta.UNK_TOKEN, ) self.dict_field.build_vocab(FEATS_VOCAB) print(self.dict_field.vocab.stoi) def test_pad_numericalize(self): minibatch = [dict_feat for dict_feat in zip(DICT_FEATS_STR, WEIGHTS, LENGTHS)] padded_feats, padded_weights, padded_lengths = self.dict_field.pad(minibatch) self.assertEqual(padded_feats, PADDED_DICT_FEATS) self.assertEqual(padded_weights, PADDED_DICT_WEIGHTS) self.assertEqual(padded_lengths, PADDED_LENGTHS) feats, _, _ = self.dict_field.numericalize( (padded_feats, padded_weights, padded_lengths), device="cpu" ) np.testing.assert_array_equal(feats.data.numpy(), NUMERICAL_FEATS) precision._FP16_ENABLED = True padded_feats, padded_weights, padded_lengths = self.dict_field.pad(minibatch) self.assertTrue(len(padded_feats[0]) % 8 == 0) self.assertTrue(len(padded_weights[0]) % 8 == 0) precision._FP16_ENABLED = False
def setUp(self): self.dict_field = DictFeatureField( batch_first=True, pad_token=VocabMeta.PAD_TOKEN, unk_token=VocabMeta.UNK_TOKEN, ) self.dict_field.build_vocab(FEATS_VOCAB) print(self.dict_field.vocab.stoi)