def __init__(self, model_path, word_dim=None, char_dim=None,
nlayers=2, hidden_dim=128, relu_dim=64, dropout_ratio=0.5):
self.model_path = model_path
defs_file = model_path + "/tagger_defs.txt"
if word_dim is None:
# use as supertagger
with open(defs_file) as f:
defs = json.load(f)
self.word_dim = defs["word_dim"]
self.char_dim = defs["char_dim"]
self.hidden_dim = defs["hidden_dim"]
self.relu_dim = defs["relu_dim"]
self.nlayers = defs["nlayers"]
self.train = False
self.extractor = FeatureExtractor(model_path)
else:
# training
self.word_dim = word_dim
self.char_dim = char_dim
self.hidden_dim = hidden_dim
self.relu_dim = relu_dim
self.nlayers = nlayers
self.train = True
with open(defs_file, "w") as f:
json.dump({"model": self.__class__.__name__,
"word_dim": self.word_dim, "char_dim": self.char_dim,
"hidden_dim": hidden_dim, "relu_dim": relu_dim,
"nlayers": nlayers}, f)
self.targets = read_model_defs(model_path + "/target.txt")
self.words = read_model_defs(model_path + "/words.txt")
self.chars = read_model_defs(model_path + "/chars.txt")
self.in_dim = self.word_dim + self.char_dim
self.dropout_ratio = dropout_ratio
super(JaLSTMTagger, self).__init__(
emb_word=L.EmbedID(len(self.words), self.word_dim),
emb_char=L.EmbedID(len(self.chars), 50, ignore_label=IGNORE),
conv_char=L.Convolution2D(1, self.char_dim,
(3, 50), stride=1, pad=(1, 0)),
lstm_f=L.NStepLSTM(nlayers, self.in_dim,
self.hidden_dim, 0.),
lstm_b=L.NStepLSTM(nlayers, self.in_dim,
self.hidden_dim, 0.),
conv1=L.Convolution2D(1, 2 * self.hidden_dim,
(7, 2 * self.hidden_dim), stride=1, pad=(3, 0)),
linear1=L.Linear(2 * self.hidden_dim, self.relu_dim),
linear2=L.Linear(self.relu_dim, len(self.targets)),
)
评论列表
文章目录