Skip to content

Fix some little bugs #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def test_model(configs):

def main():
opts = parse_opts()
configs = yaml.load(codecs.open(opts.config))
configs = yaml.load(codecs.open(opts.config, encoding='uft-8'))

if opts.train: # train
# 判断是否需要预处理
Expand Down
13 changes: 7 additions & 6 deletions sltk/infer/inference.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import sys
import codecs
import sys

import torch

from ..utils import read_conllu
Expand Down Expand Up @@ -52,9 +53,10 @@ def infer(self):

logits = self.model(**feed_tensor_dict)
# mask
mask = feed_tensor_dict[str(self.feature_names[0])] > 0
actual_lens = torch.sum(feed_tensor_dict[self.feature_names[0]] > 0, dim=1).int()
labels_batch = self.model.predict(logits, actual_lens, mask)
mask = feed_tensor_dict[str(self.model.feature_names[0])] > 0
actual_lens = torch.sum(feed_tensor_dict[self.model.feature_names[0]] > 0, dim=1).int()
label_ids_batch = self.model.predict(logits, actual_lens, mask)
labels_batch = self.id2label(label_ids_batch)
labels_pred.extend(labels_batch)
sys.stdout.write('sentence: {0} / {1}\r'.format(self.data_iter.iter_variable, self.data_iter.data_count))
sys.stdout.write('sentence: {0} / {1}\n'.format(self.data_iter.data_count, self.data_iter.data_count))
Expand Down Expand Up @@ -84,7 +86,7 @@ def infer2file(self):
sent_len = len(feature_items) # 句子实际长度
labels = labels_batch[i]
if len(labels) < sent_len: # 补全为`O`
labels = labels + ['O'] * (sent_len-len(labels))
labels = labels + ['O'] * (sent_len - len(labels))
for j in range(sent_len):
file_result.write('{0} {1}\n'.format(' '.join(feature_items[j]), labels[j]))
file_result.write('\n')
Expand Down Expand Up @@ -128,4 +130,3 @@ def tensor_from_numpy(data, dtype='long', use_cuda=True):
if use_cuda:
data = data.cuda()
return data