def predict(model, sentence): model, vocab, setup = model sentence = sentence.strip() text = nlp_utils.normalize_text(sentence) words = nlp_utils.split_text(text, char_based=setup['char_based']) xs = nlp_utils.transform_to_array([words], vocab, with_label=False) xs = nlp_utils.convert_seq(xs, device=-1, with_label=False) # todo use GPU with chainer.using_config('train', False), chainer.no_backprop_mode(): prob = model.predict(xs, softmax=True)[0] answer = int(model.xp.argmax(prob)) score = float(prob[answer]) return answer, score
def get_vectors(model, sentences): model, vocab, setup = model vectors = [] for sentence in sentences: sentence = sentence.strip() text = nlp_utils.normalize_text(sentence) words = nlp_utils.split_text(text, char_based=setup['char_based']) xs = nlp_utils.transform_to_array([words], vocab, with_label=False) xs = nlp_utils.convert_seq(xs, device=-1, with_label=False) # todo use GPU with chainer.using_config('train', False), chainer.no_backprop_mode(): vector = model.encoder(xs) vectors.append(vector.data[0]) vectors = numpy.asarray(vectors) return vectors