Пример #1
0
 def get_model(self):
     self.model = TENER(tag_vocab=self.data_bundle.get_vocab('target'), embed=self.embed, num_layers=num_layers,
                     d_model=d_model, n_head=n_heads,
                     feedforward_dim=dim_feedforward, dropout=dropout,
                     after_norm=after_norm, attn_type=attn_type,
                     bi_embed=None,
                     fc_dropout=fc_dropout,
                     pos_embed=pos_embed,
                     scale=attn_type=='transformer')
     #Thay model path trước khi chạy
     model_path = './model/model.bin'
     states = torch.load(model_path).state_dict()
     self.model.load_state_dict(states)
     return self.model, self.data
Пример #2
0
def make_model(model_id, dataset, config):
    if model_id == config['model_ids']['tener']:
        data_bundle, embed = get_data_loading_function(dataset,
                                                       config)(dataset, config)
        return TENER(data_bundle=data_bundle,
                     config=config,
                     embed=embed,
                     num_layers=config['num_layers'],
                     d_model=config['n_heads'] * config['head_dims'],
                     n_head=config['n_heads'],
                     feedforward_dim=int(2 * config['n_heads'] *
                                         config['head_dims']),
                     dropout=config['dropout'],
                     after_norm=config['after_norm'],
                     attn_type=config['attn_type'],
                     bi_embed=None,
                     fc_dropout=config['fc_dropout'],
                     pos_embed=config['pos_embed'],
                     scale=config['attn_type'] == 'transformer')
Пример #3
0
dev_data = list(data_bundle.get_dataset("dev"))
test_data = list(data_bundle.get_dataset("test"))

vocab_size = len(data_bundle.get_vocab('chars'))
feature_vocab_size = len(feature2id)

model = TENER(tag_vocab=data_bundle.get_vocab('target'),
              embed=embed,
              num_layers=num_layers,
              d_model=d_model,
              n_head=n_heads,
              feedforward_dim=dim_feedforward,
              dropout=trans_dropout,
              after_norm=after_norm,
              attn_type=attn_type,
              bi_embed=None,
              fc_dropout=fc_dropout,
              pos_embed=pos_embed,
              scale=attn_type == 'naive',
              vocab_size=vocab_size,
              feature_vocab_size=feature_vocab_size,
              kv_attn_type=kv_attn_type,
              memory_dropout=memory_dropout,
              fusion_dropout=fusion_dropout,
              fusion_type=fusion_type,
              highway_layer=highway_layer)

optimizer = optim.Adam(model.parameters(), lr=lr, betas=(0.9, 0.99))

callbacks = []
clip_callback = GradientClipCallback(clip_type='value', clip_value=5)
evaluate_callback = EvaluateCallback(data=data_bundle.get_dataset('test'),
Пример #4
0
                           dropout=0,
                           word_dropout=0.02)

    return data, embed


data_bundle, embed = load_data()
print(data_bundle)

model = TENER(tag_vocab=data_bundle.get_vocab('target'),
              embed=embed,
              num_layers=num_layers,
              d_model=d_model,
              n_head=n_heads,
              feedforward_dim=dim_feedforward,
              dropout=trans_dropout,
              after_norm=after_norm,
              attn_type=attn_type,
              bi_embed=None,
              fc_dropout=fc_dropout,
              pos_embed=pos_embed,
              scale=attn_type == 'naive')

if optim_type == 'sgd':
    optimizer = optim.SGD(model.parameters(), lr=lr, momentum=0.9)
else:
    optimizer = optim.Adam(model.parameters(), lr=lr, betas=(0.9, 0.99))

callbacks = []
clip_callback = GradientClipCallback(clip_type='value', clip_value=5)
evaluate_callback = EvaluateCallback(data_bundle.get_dataset('test'))
Пример #5
0
    print("[Info] Zen Mode, Zen dataset loaded ...")

model = TENER(tag_vocab=data_bundle.get_vocab('target'),
              embed=embed,
              num_layers=num_layers,
              d_model=d_model,
              n_head=n_heads,
              feedforward_dim=dim_feedforward,
              dropout=args.trans_dropout,
              after_norm=after_norm,
              attn_type=attn_type,
              bi_embed=bi_embed,
              fc_dropout=fc_dropout,
              pos_embed=pos_embed,
              scale=attn_type == 'transformer',
              use_knowledge=True,
              feature2count=feature2count,
              vocab_size=vocab_size,
              feature_vocab_size=feature_vocab_size,
              kv_attn_type=args.kv_attn_type,
              memory_dropout=args.memory_dropout,
              fusion_dropout=args.fusion_dropout,
              fusion_type=args.fusion_type,
              highway_layer=args.highway_layer,
              key_embed_dropout=args.key_embed_dropout,
              knowledge_type=args.knowledge_type,
              use_zen=args.zen_model != "")

if args.optim_type == 'sgd':
    optimizer = optim.SGD(model.parameters(), lr=lr, momentum=0.9)
Пример #6
0
                                 requires_grad=True, lower=True, word_dropout=0, dropout=0.5,
                                 only_norm_found_vector=normalize_embed)
    if char_embed is not None:
        embed = StackEmbedding([word_embed, char_embed], dropout=0, word_dropout=0.02)
    else:
        word_embed.word_drop = 0.02
        embed = word_embed
    data__ = data.get_vocab('words')
    data.rename_field('words', 'chars')
    return data, embed, data__, word_embed

data_bundle, embed, data__, word_embed = load_data()

model = TENER(tag_vocab=data_bundle.get_vocab('target'), embed=embed, num_layers=num_layers,
                d_model=d_model, n_head=n_heads,
                feedforward_dim=dim_feedforward, dropout=dropout,
                after_norm=after_norm, attn_type=attn_type,
                bi_embed=None,
                fc_dropout=fc_dropout,
                pos_embed=pos_embed,
                scale=attn_type=='transformer')
#Thay model path trước khi chạy
model_path = './w2v_n6'
states = torch.load(model_path).state_dict()
model.load_state_dict(states)

evaluation = Tester(data_bundle.get_dataset('test'),
                    model,
                    metrics=SpanFPreRecMetric(tag_vocab=data_bundle.get_vocab('target'), only_gross=False),
                    batch_size=batch_size)
evaluation.test()