-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathutils.py
More file actions
32 lines (22 loc) · 999 Bytes
/
Copy pathutils.py
File metadata and controls
32 lines (22 loc) · 999 Bytes
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
from os.path import exists, join
import torch
from bi_lstm_crf.model import BiRnnCrf
FILE_ARGUMENTS = "arguments.json"
FILE_MODEL = "model.pth"
def arguments_filepath(model_dir):
return join(model_dir, FILE_ARGUMENTS)
def model_filepath(model_dir):
return join(model_dir, FILE_MODEL)
def build_model(args, processor, load=True, verbose=False):
model = BiRnnCrf(len(processor.vocab), len(processor.tags),
embedding_dim=args.embedding_dim, hidden_dim=args.hidden_dim, num_rnn_layers=args.num_rnn_layers)
# weights
model_path = model_filepath(args.model_dir)
if exists(model_path) and load:
state_dict = torch.load(model_path, map_location=running_device(args.device))
model.load_state_dict(state_dict)
if verbose:
print("load model weights from {}".format(model_path))
return model
def running_device(device):
return device if device else torch.device("cuda:0" if torch.cuda.is_available() else "cpu")