def main(_): FLAGS.start_string = FLAGS.start_string converter = TextConverter(filename=FLAGS.converter_path) if os.path.isdir(FLAGS.checkpoint_path): FLAGS.checkpoint_path =\ tf.train.latest_checkpoint(FLAGS.checkpoint_path) model = CharRNN(converter.vocab_size, sampling=True, lstm_size=FLAGS.lstm_size, num_layers=FLAGS.num_layers, use_embedding=FLAGS.use_embedding, embedding_size=FLAGS.embedding_size) model.load(FLAGS.checkpoint_path) start = converter.text_to_arr(FLAGS.start_string) arr = model.predict(FLAGS.max_length, start, converter.vocab_size, 10) for c, p in arr: prediction = converter.arr_to_text(c) prediction = remove_return(prediction) # 如果有中文字生成,请将 {1:^14} 改为 {1:{4}^14} 以修复对齐问题。 # {1:^14}中的 14 随着生成的字符数量而定,一般可以设为字符数+4 print("{0} -> {1:^14} {2} {3}".format(FLAGS.start_string, prediction, "probability:", p, chr(12288)))
from model import CharRNN import preprocess as pre import numpy as np from model import * if __name__ == '__main__': classes, classDict = pre.build_class() model = CharRNN() model.load_model() sent = "The mobile phase consisted of methanol, acetonitrile and dichloromethane (42:42:16) with a flow rate of 1.0 mL/min at 30°C." words = sent.split(" ") sample = np.array(pre.words2sample(words), dtype=int) labels = model.predict(sample) tags = [classes[label] for label in labels] size = len(words) for i in range(size): print(words[i], ":::", tags[i])