forked from fhamborg/NewsMTSC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFXBaseModel.py
More file actions
42 lines (34 loc) · 1.16 KB
/
Copy pathFXBaseModel.py
File metadata and controls
42 lines (34 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import torch.nn as nn
from abc import abstractmethod
from transformers import XLNetModel, AlbertModel, BertModel, RobertaModel
class FXBaseModel(nn.Module):
def __init__(self):
super().__init__()
@staticmethod
@abstractmethod
def get_language_models():
return
@staticmethod
@abstractmethod
def get_input_field_ids():
return
def invoke_language_model(self, lm, input_ids, token_type_ids=None):
type_lm = type(lm)
if type_lm == XLNetModel:
last_hidden_state, mems, all_hidden_states = lm(
input_ids=input_ids,
token_type_ids=token_type_ids,
)
elif type_lm in [AlbertModel, BertModel, RobertaModel]:
if token_type_ids is None:
last_hidden_state, pooler_output, hidden_states = lm(
input_ids=input_ids,
)
else:
last_hidden_state, pooler_output, hidden_states = lm(
input_ids=input_ids,
token_type_ids=token_type_ids
)
else:
raise NotImplementedError
return last_hidden_state