交互使用训练有素的张量流模型 [英] Use a trained tensorflow model interactively

查看:78
本文介绍了交互使用训练有素的张量流模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用LSTM Tensorflow模型(句子摘要),并且已经达到了相对较好的水平。但是当我尝试导入已保存的模型并以交互方式使用它时,它不会产生任何输出。



我的原始代码使用检查点,但我切换到SavedModelBuilder( )因为我认为它可能更容易使用。





I have been playing around with a LSTM Tensorflow model (sentence summarization) and got it to the point where it is doing a fairly good job. But when I try to import the saved Model and use it interactively it doesn't produce any output.

My original code was using checkpoints, but I switched to SavedModelBuilder() because I thought it might be easier to work with.


import tensorflow as tf
from Seq2SeqModel import Seq2SeqModel
import utils
from time import gmtime, strftime
import sys, os

###############################################################################
# read in our input and output files
###############################################################################
# for this example we will expect our input and output files to have the same number of lines.
#      input[x] should be translated into output[x]
###############################################################################
print("="*60)
DATADIR = "../DATASETS/sentence-compression-master/data/"
INPUT_DATA = DATADIR + "long_sentences.txt"
OUTPUT_DATA = DATADIR + "short_sentences.txt"

# read the files in as 'utf-8-sig' to ignore a leading Byte-Order-Encoding bit if present
with open(INPUT_DATA, 'r', encoding='utf-8') as f:
    in_sentences = f.readlines()

with open(OUTPUT_DATA, 'r', encoding='utf-8') as f:
    out_sentences = f.readlines()

in_chars = []
TEMP_LINE = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ[]()-_,.:;!?0123456789$+'\""
for char in TEMP_LINE:
    in_chars.append(char)

###############################################################################
# create our encoding/decoding scheme
# note since we are not doing any special encoding (make lower case) or gathering the
# corpus (bucket of words, or characters found in text) we could skip this part.
###############################################################################
# our encoder and decoder will be stored as dictionaries converting between Chars and Ints: 
char_to_int = dict()
int_to_char = dict()

for i,char in enumerate(in_chars):
    char_to_int[char] = i
    int_to_char[i]    = char

num_in_chars = len(in_chars)
max_in_chars_per_sample = max([len(sample) for sample in in_sentences])
max_out_chars_per_sample = max([len(sample) for sample in out_sentences])
num_samples = len(in_sentences)

print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " Create our Training Data")
X_train = []
y_train = []

for sent in in_sentences:
    id_sent = []
    for mychar in sent:
        if mychar in in_chars:
            vocab_id = char_to_int[mychar]
            id_sent += [vocab_id]
    X_train += [id_sent]
    
for sent in out_sentences:
    id_sent = []
    for mychar in sent:
        if mychar in in_chars:
            vocab_id = char_to_int[mychar]
            id_sent += [vocab_id]
    y_train += [id_sent]

#tf.set_random_seed(1)
# gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8)

###############################################################################
# train the model, X is our input, Y is our output
###############################################################################
print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " Train our Model")
step = 0
batch_size = 32
max_batches = int(len(X_train) / batch_size)
batches_in_epoch = 1
epoch_be_saved = 1
my_beam_width = 3

# if checkpoint found
CHKPT_FOUND = False
for line in open("../models/checkpoint"):
    if len(line) > 1:
        data = line.split('"')
        checkpoint = "../models/" + data[1]
        data2 = data[1].split("-")
        step = int(data2[1])
        CHKPT_FOUND = True
print("Selecting previous checkpoint ../models/nmt.ckpt-" + str(step) + ".index")

g = tf.Graph()
with g.as_default():
    model = Seq2SeqModel(encoder_num_units = 512, decoder_num_units = 512, embedding_size = 512, num_layers = 2,
        vocab_size = num_in_chars, batch_size = batch_size, bidirectional = False, attention = True,
        beam_search = True, beam_width = my_beam_width, mode = "Train")
    print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " model constructed.")
    
    builder = tf.saved_model.builder.SavedModelBuilder('./SavedModel/')
    with tf.Session(config=tf.ConfigProto()) as sess:
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver()
        summary_writer = tf.summary.FileWriter('../log', graph = sess.graph)
        if CHKPT_FOUND == True:
            print('loading previous checkpoint [' + checkpoint + ']')
            saver.restore(sess, checkpoint)

        builder.add_meta_graph_and_variables(sess, [tf.saved_model.tag_constants.SERVING], signature_def_map=None, assets_collection=None)
        builder.save() 
        
        print('start training.')
        for _epoch in range(1, batches_in_epoch + 1):
            for _batch in range(max_batches + 1):
                if step % 5 == 0:
                    print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " Processing Batch " + str(_batch) + " of " + str(max_batches))
                X, y = utils.input_generator(X_train, y_train, batch_size)
                feed_dict = model.make_train_inputs(x = X, y = y)

                _, l, train_sentences, summary_str = sess.run([model.train_op, model.loss, model.decoder_predictions_train, model.summary_op], feed_dict)

                summary_writer.add_summary(summary_str, _epoch * _batch)
                if step == 0 or step % 25 == 0:
                    print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " Step {}".format(step))
                    print('  minibatch loss: {}'.format(sess.run(model.loss, feed_dict)))
                    for i in range(1):
                        print('train logits:')
                        train_sentence = ''
                        for mychar in train_sentences[i]:
                            train_sentence += str(int_to_char[mychar])
                        print(train_sentence)
                        # we could also print out X and y sentences for better context
                        print(' ')
                if step % 100 == 0:
                    saver.save(sess, '../models/' + 'nmt.ckpt', global_step = step)
                    print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " model saved at step = " + str(step))
                step += 1
                
            print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " epoch finished")

            if _epoch % epoch_be_saved == 0:
                tf.saved_model.simple_save(sess, 'models/0', inputs, outputs)
                saver.save(sess, '../models/' + 'nmt.ckpt', global_step = step)
                print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " model saved at step = " + str(step))

        print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " Finished Training")





然后我可以将模型加载到另一个脚本,但什么时候我尝试做任何事我得到的都是空输出数组。





I was then able to load my model in to another script, but when I try to do anything with it all I get are empty output arrays.

import tensorflow as tf
from tensorflow.python.saved_model import tag_constants
from Seq2SeqModel import Seq2SeqModel
import utils
from time import gmtime, strftime
import sys, os

###############################################################################
# read in our input and output files
###############################################################################
in_sentences = []
out_sentences = []
in_chars = []
TEMP_LINE = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ[]()-_,.:;!?0123456789$+'\""
for char in TEMP_LINE:
    in_chars.append(char)

###############################################################################
# create our encoding/decoding scheme
# note since we are not doing any special encoding (make lower case) or gathering the
# corpus (bucket of words, or characters found in text) we could skip this part.
###############################################################################

# our encoder and decoder will be stored as dictionaries converting between Chars and Ints: 
char_to_int = dict()
int_to_char = dict()

for i,char in enumerate(in_chars):
    char_to_int[char] = i
    int_to_char[i]    = char

num_in_chars = len(in_chars)

###############################################################################
# train the model, X is our input, Y is our output
###############################################################################
checkpoint = 'models/0'
step = 0
batch_size = 32
max_batches = 1
batches_in_epoch = 1
epoch_be_saved = 1
my_beam_width = 3
temperature = 1
top_k = 0

g = tf.Graph()
with g.as_default():
    model = Seq2SeqModel(encoder_num_units = 512, decoder_num_units = 512, embedding_size = 512, num_layers = 2,
        vocab_size = num_in_chars, batch_size = batch_size, bidirectional = False, attention = True,
        beam_search = True, 
        beam_width = my_beam_width, 
        mode = "Infer"
    )
    print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " model constructed.")

    gvars = g.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
    assign_ops = [g.get_operation_by_name(v.op.name + "/Assign") for v in gvars]
    init_values = [assign_op.inputs[1] for assign_op in assign_ops]

    #hparams = model.default_hparams()
    #length = hparams.n_ctx // 2
    with tf.Session(config=tf.ConfigProto()) as sess:
        tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], './SavedModel/')
        #sess.run(tf.global_variables_initializer())
        print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " model loaded.")

        context_tokens = []
        sent = "World renowned Count Olaf performed in a play and got a standing ovation from the crowd."
        for mychar in sent:
            if mychar in in_chars:
                context_tokens.append(char_to_int[mychar])
        generated = 0
        print(sent)
        output = []
        feed_dict = model.make_infer_inputs(x = [context_tokens])
        print(feed_dict)
        out = sess.run(output, feed_dict)
        print(output)
        print("xxxxxxxxxxxxxxxxxx")
        print(out)
        out_string = ""
        for i in range(len(output)):
            out_string += int_to_char[out[i]]
        print(out_string)
        print("=" * 40)





这给出以下输出:



This gives me the following output:

World renowned Count Olaf performed in a play and got a standing ovation from the crowd.
{<tf.Tensor 'encoder_inputs:0' shape=(?, ?) dtype=int32>: array([[49, 15, 18, 12,  4,  0, 18,  5, 14, 15, 23, 14,  5,  4,  0, 29,
        15, 21, 14, 20,  0, 41, 12,  1,  6,  0, 16,  5, 18,  6, 15, 18,
        13,  5,  4,  0,  9, 14,  0,  1,  0, 16, 12,  1, 25,  0,  1, 14,
         4,  0,  7, 15, 20,  0,  1,  0, 19, 20,  1, 14,  4,  9, 14,  7,
         0, 15, 22,  1, 20,  9, 15, 14,  0,  6, 18, 15, 13,  0, 20,  8,
         5,  0,  3, 18, 15, 23,  4, 60]]), <tf.Tensor 'encoder_inputs_length:0' shape=(?,) dtype=int32>: [88]}
[]
xxxxxxxxxxxxxxxxxx
[]





我的feed_dict看起来不错,而且我可能犯了一些明显的错误,但我找不到它。



我怎样才能采用经过训练的模型,将其冻结到位,然后再使用它处理单个句子?



我明白我可以做很多事情来清理代码,遗憾的是我一直在尝试不同的方法es(并且仍然无处可去)代码变得有点草率。在它变成难以理解的混乱之前请帮助:-)



如果有必要,我可以删除一些代码以简化操作,我只是担心有人可能会要求完整的评估代码。



我的尝试:



我我曾尝试在线阅读几个教程,并通过SavedModelBuilder的玩具代码并使用检查点但无济于事。



My feed_dict looks ok, and I'm probably making some obvious mistake, but I can't find it.

How can I take a trained model, freeze it in place, and then use it to process individual sentences?

I understand that there is a lot I can do to clean up the code, unfortunately as I've been trying different approaches (and still getting nowhere) the code has become a bit sloppy. Please help before it devolves into an unreadable mess :-)

If necessary I can remove some of the code to simplify things, I was just worried someone might ask for the full code for evaluation.

What I have tried:

I've tried following several tutorials online, and going through the toy code for SavedModelBuilder and working with checkpoints but to no avail.

推荐答案

+'\
for char in TEMP_LINE:
in_chars.append(char)

################## ################################################## ##########
创建我们的编码/解码方案
注意,因为我们没有做y特殊编码(制作小写)或收集
语料库(一堆文字,或者在文本中找到的字符)我们可以跳过这一部分。
##### ################################################## #######################
我们的编码器和解码器将存储为字符在Irs和Ints之间转换:
char_to_int = dict()
int_to_char = dict()

for i,char in enumerate(in_chars):
char_to_int [char] = i
int_to_char [i] = char

num_in_chars = len(in_chars)
max_in_chars_per_sample = max([len(sample) for 示例 in in_sentences])
max_out_chars_per_sample = max([len(sample)< span class =code-keyword> for sample in out_sentences])
num_samples = len(in_sentences)

print (strftime( %Y-%m- %d%H:%M:%S,gmtime())+ 创建我们的培训数据
X_train = []
y_train = []

发​​送> in_sentences:
id_sent = []
for mychar in 发​​送:
如果 mychar in in_chars:
vocab_id = char_to_int [mychar]
id_sent + = [vocab_id]
X_train + = [id_sent]

发​​送< span class =code-keyword> in out_sentences:
id_sent = []
for mychar in 发​​送:
如果 mychar in in_chars:
vocab_id = char_to_int [mychar]
id_sent + = [vocab_id]
y_train + = [id_sent]

tf.set_random_seed(1)
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction = 0.8)

########################### ################################################## #
训练模型,X是我们的输入,Y是我们的输出
#################### ################################################## ########
print (strftime( %Y-%m- %d%H:%M:%S,gmtime())+ 训练我们的模型
step = 0
batch_size = 32
max_batches = int(len(X_train)/ batch_size)
batches_in_epoch = 1
epoch_be_saved = 1
my_beam_width = 3

如果找到检查点
CHKPT_FOUND = 错误
for line in open( .. / models / checkpoint):
if len(line)> 1
data = line.split(' '
checkpoint = ../ models / + data [ 1 ]
data2 = data [ 1 ] .split( -
step = int(data2 [ 1 ])
CHKPT_FOUND = True
print 选择上一个检查点../ models / nmt.ckpt - + str(step)+ .index

g = tf.Graph()
with g.as_default():
model = Seq2SeqModel(encoder_num_units = 512 ,decoder_num_units = 512 ,embedding_size = 512 ,num_layers = 2
vocab_size = num_in_chars,batch_size = batch_size,bidirectional = False ,注意= True
beam_search = True ,beam_width = my_beam_width,mode = Train\")
print(strftime(\"%Y-%m-%d %H:%M:%S\", gmtime()) + \"< span class=\"code-string\"> model constructed.\")

builder = tf.saved_model.builder.SavedModelBuilder('./SavedModel/')
with tf.Session(config=tf.ConfigProto( )) as sess:
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
summary_writer = tf.summary.FileWriter('../log', graph = sess.graph)
if CHKPT_FOUND == True:
print('loading previous checkpoint [' + checkpoint + ']')
saver.restore(sess, checkpoint)

builder.add_meta_graph_and_variables(sess, [tf.saved_model.tag_constants.SERVING], signature_def_map=None, assets_collection=None)
builder.save()

print('start training.')
for _epoch in range(1, batches_in_epoch + 1):
for _batch in range(max_batches + 1):
if step % 5 == 0:
print(strftime(\"%Y-%m-%d %H:%M:%S\", gmtime()) + \" Processing Batch \" + str(_batch) + \" of \" + str(max_batches))
X, y = utils.input_generator(X_train, y_train, batch_size)
feed_dict = model.make_train_inputs(x = X, y = y)

_, l, train_sentences, summary_str = sess.run([model.train_op, model.loss, model.decoder_predictions_train, model.summary_op], feed_dict)

summary_writer.add_summary(summary_str, _epoch * _batch)
if step == 0 or step % 25 == 0:
print(strftime(\"%Y-%m-%d %H:%M:%S\", gmtime()) + \" Step {}\".format(step))
print(' minibatch loss: {}'.format(sess.run(model.loss, feed_dict)))
for i in range(1):
print('train logits:')
train_sentence = ''
for mychar in train_sentences[i]:
train_sentence += str(int_to_char[mychar])
print(train_sentence)
# we could also print out X and y sentences for better context
print(' ')
if step % 100 == 0:
saver.save(sess, '../models/' + 'nmt.ckpt', global_step = step)
print(strftime(\"%Y-%m-%d %H:%M:%S\", gmtime()) + \" model saved at step = \" + str(step))
step += 1

print(strftime(\"%Y-%m-%d %H:%M:%S\", gmtime()) + \" epoch finished\")

if _epoch % epoch_be_saved == 0:
tf.saved_model.simple_save(sess, 'models/0', inputs, outputs)
saver.save(sess, '../models/' + 'nmt.ckpt', global_step = step)
print(strftime(\"%Y-%m-%d %H:%M:%S\", gmtime()) + \" model saved at step = \" + str(step))

print(strftime(\"%Y-%m-%d %H:%M:%S\", gmtime()) + \" Finished Training\")
+'\"" for char in TEMP_LINE: in_chars.append(char) ############################################################################### # create our encoding/decoding scheme # note since we are not doing any special encoding (make lower case) or gathering the # corpus (bucket of words, or characters found in text) we could skip this part. ############################################################################### # our encoder and decoder will be stored as dictionaries converting between Chars and Ints: char_to_int = dict() int_to_char = dict() for i,char in enumerate(in_chars): char_to_int[char] = i int_to_char[i] = char num_in_chars = len(in_chars) max_in_chars_per_sample = max([len(sample) for sample in in_sentences]) max_out_chars_per_sample = max([len(sample) for sample in out_sentences]) num_samples = len(in_sentences) print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " Create our Training Data") X_train = [] y_train = [] for sent in in_sentences: id_sent = [] for mychar in sent: if mychar in in_chars: vocab_id = char_to_int[mychar] id_sent += [vocab_id] X_train += [id_sent] for sent in out_sentences: id_sent = [] for mychar in sent: if mychar in in_chars: vocab_id = char_to_int[mychar] id_sent += [vocab_id] y_train += [id_sent] #tf.set_random_seed(1) # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8) ############################################################################### # train the model, X is our input, Y is our output ############################################################################### print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " Train our Model") step = 0 batch_size = 32 max_batches = int(len(X_train) / batch_size) batches_in_epoch = 1 epoch_be_saved = 1 my_beam_width = 3 # if checkpoint found CHKPT_FOUND = False for line in open("../models/checkpoint"): if len(line) > 1: data = line.split('"') checkpoint = "../models/" + data[1] data2 = data[1].split("-") step = int(data2[1]) CHKPT_FOUND = True print("Selecting previous checkpoint ../models/nmt.ckpt-" + str(step) + ".index") g = tf.Graph() with g.as_default(): model = Seq2SeqModel(encoder_num_units = 512, decoder_num_units = 512, embedding_size = 512, num_layers = 2, vocab_size = num_in_chars, batch_size = batch_size, bidirectional = False, attention = True, beam_search = True, beam_width = my_beam_width, mode = "Train") print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " model constructed.") builder = tf.saved_model.builder.SavedModelBuilder('./SavedModel/') with tf.Session(config=tf.ConfigProto()) as sess: sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() summary_writer = tf.summary.FileWriter('../log', graph = sess.graph) if CHKPT_FOUND == True: print('loading previous checkpoint [' + checkpoint + ']') saver.restore(sess, checkpoint) builder.add_meta_graph_and_variables(sess, [tf.saved_model.tag_constants.SERVING], signature_def_map=None, assets_collection=None) builder.save() print('start training.') for _epoch in range(1, batches_in_epoch + 1): for _batch in range(max_batches + 1): if step % 5 == 0: print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " Processing Batch " + str(_batch) + " of " + str(max_batches)) X, y = utils.input_generator(X_train, y_train, batch_size) feed_dict = model.make_train_inputs(x = X, y = y) _, l, train_sentences, summary_str = sess.run([model.train_op, model.loss, model.decoder_predictions_train, model.summary_op], feed_dict) summary_writer.add_summary(summary_str, _epoch * _batch) if step == 0 or step % 25 == 0: print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " Step {}".format(step)) print(' minibatch loss: {}'.format(sess.run(model.loss, feed_dict))) for i in range(1): print('train logits:') train_sentence = '' for mychar in train_sentences[i]: train_sentence += str(int_to_char[mychar]) print(train_sentence) # we could also print out X and y sentences for better context print(' ') if step % 100 == 0: saver.save(sess, '../models/' + 'nmt.ckpt', global_step = step) print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " model saved at step = " + str(step)) step += 1 print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " epoch finished") if _epoch % epoch_be_saved == 0: tf.saved_model.simple_save(sess, 'models/0', inputs, outputs) saver.save(sess, '../models/' + 'nmt.ckpt', global_step = step) print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " model saved at step = " + str(step)) print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " Finished Training")





I was then able to load my model in to another script, but when I try to do anything with it all I get are empty output arrays.





I was then able to load my model in to another script, but when I try to do anything with it all I get are empty output arrays.

import tensorflow as tf
from tensorflow.python.saved_model import tag_constants
from Seq2SeqModel import Seq2SeqModel
import utils
from time import gmtime, strftime
import sys, os

###############################################################################
# read in our input and output files
###############################################################################
in_sentences = []
out_sentences = []
in_chars = []
TEMP_LINE = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ[]()-_,.:;!?0123456789


+'\\"\"
for char in TEMP_LINE:
in_chars.append(char)

###############################################################################
# create our encoding/decoding scheme
# note since we are not doing any special encoding (make lower case) or gathering the
# corpus (bucket of words, or character s found in text) we could skip this part.
###############################################################################

# our encoder and decoder will be stored as dictionaries converting between Chars and Ints:
char_to_int = dict()
int_to_char = dict()

for i,char in enumerate(in_chars):
char_to_int[char] = i
int_to_char[i] = char

num_in_chars = len(in_chars)

###############################################################################
# train the model, X is our input, Y is our output
###### #########################################################################
checkpoint = 'models/0'
step = 0
batch_size = 32
max_batches = 1
batches_in_epoch = 1
epoch_be_saved = 1
my_beam_width = 3
temperature = 1
top_k = 0

g = tf.Graph()
with g.as_default():
model = Seq2SeqModel(encoder_num_units = 512, decoder_num_units = 512, embedding_size = 512, num_layers = 2,
vocab_size = num_in_chars, batch_size = batch_size, bidire ctional = False, attention = True,
beam_search = True,
beam_width = my_beam_width,
mode = \"Infer\"
)
print(strftime(\"%Y-%m-%d %H:%M:%S\", gmtime()) + \" model constructed.\")

gvars = g.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
assign_ops = [g.get_operation_by_name(v.op.name + \"/Assign\") for v in gvars]
init_values = [assign_op.inputs[1] for assign_op in assign_ops]

#hparams = model.default_hparams()
#length = hparams.n_ctx // 2
with tf.Session(config=tf.ConfigProto()) as sess:
tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], './SavedModel/')
#sess.run(tf.global_variables_initializer())
print(strftime(\"%Y-%m-%d %H:%M:%S\", gmtime()) + \" model loaded.\")

context_tokens = []
sent = \"World renowned Count Olaf performed in a play and got a standing ovation from the crowd.\"
for mychar in sent:
if mychar in in_chars:
context_tokens.append(char_to_int[mychar])
generated = 0
print(sent)
output = []
feed_dict = model.make_infer_inputs(x = [context_tokens])
print(feed_dict)
out = sess.run(output, feed_dict)
print(output)
print(\"xxxxxxxxxxxxxxxxxx\")
print(out)
out_string = \"\"
for i in range(len(output)):
out_string += int_to_char[out[i]]
print(out_string)
print(\"=\" * 40)
+'\"" for char in TEMP_LINE: in_chars.append(char) ############################################################################### # create our encoding/decoding scheme # note since we are not doing any special encoding (make lower case) or gathering the # corpus (bucket of words, or characters found in text) we could skip this part. ############################################################################### # our encoder and decoder will be stored as dictionaries converting between Chars and Ints: char_to_int = dict() int_to_char = dict() for i,char in enumerate(in_chars): char_to_int[char] = i int_to_char[i] = char num_in_chars = len(in_chars) ############################################################################### # train the model, X is our input, Y is our output ############################################################################### checkpoint = 'models/0' step = 0 batch_size = 32 max_batches = 1 batches_in_epoch = 1 epoch_be_saved = 1 my_beam_width = 3 temperature = 1 top_k = 0 g = tf.Graph() with g.as_default(): model = Seq2SeqModel(encoder_num_units = 512, decoder_num_units = 512, embedding_size = 512, num_layers = 2, vocab_size = num_in_chars, batch_size = batch_size, bidirectional = False, attention = True, beam_search = True, beam_width = my_beam_width, mode = "Infer" ) print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " model constructed.") gvars = g.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) assign_ops = [g.get_operation_by_name(v.op.name + "/Assign") for v in gvars] init_values = [assign_op.inputs[1] for assign_op in assign_ops] #hparams = model.default_hparams() #length = hparams.n_ctx // 2 with tf.Session(config=tf.ConfigProto()) as sess: tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], './SavedModel/') #sess.run(tf.global_variables_initializer()) print(strftime("%Y-%m-%d %H:%M:%S", gmtime()) + " model loaded.") context_tokens = [] sent = "World renowned Count Olaf performed in a play and got a standing ovation from the crowd." for mychar in sent: if mychar in in_chars: context_tokens.append(char_to_int[mychar]) generated = 0 print(sent) output = [] feed_dict = model.make_infer_inputs(x = [context_tokens]) print(feed_dict) out = sess.run(output, feed_dict) print(output) print("xxxxxxxxxxxxxxxxxx") print(out) out_string = "" for i in range(len(output)): out_string += int_to_char[out[i]] print(out_string) print("=" * 40)





This gives me the following output:



This gives me the following output:

World renowned Count Olaf performed in a play and got a standing ovation from the crowd.
{<tf.Tensor 'encoder_inputs:0' shape=(?, ?) dtype=int32>: array([[49, 15, 18, 12,  4,  0, 18,  5, 14, 15, 23, 14,  5,  4,  0, 29,
        15, 21, 14, 20,  0, 41, 12,  1,  6,  0, 16,  5, 18,  6, 15, 18,
        13,  5,  4,  0,  9, 14,  0,  1,  0, 16, 12,  1, 25,  0,  1, 14,
         4,  0,  7, 15, 20,  0,  1,  0, 19, 20,  1, 14,  4,  9, 14,  7,
         0, 15, 22,  1, 20,  9, 15, 14,  0,  6, 18, 15, 13,  0, 20,  8,
         5,  0,  3, 18, 15, 23,  4, 60]]), <tf.Tensor 'encoder_inputs_length:0' shape=(?,) dtype=int32>: [88]}
[]
xxxxxxxxxxxxxxxxxx
[]





My feed_dict looks ok, and I’m probably making some obvious mistake, but I can’t find it.



How can I take a trained model, freeze it in place, and then use it to process individual sentences?



I understand that there is a lot I can do to clean up the code, unfortunately as I’ve been trying different approach es (and still getting nowhere) the code has become a bit sloppy. Please help before it devolves into an unreadable mess :-)



If necessary I can remove some of the code to simplify things, I was just worried someone might ask for the full code for evaluation.



What I have tried:



I’ve tried following several tutorials online, and going through the toy code for SavedModelBuilder and working with checkpoints but to no avail.



My feed_dict looks ok, and I'm probably making some obvious mistake, but I can't find it.

How can I take a trained model, freeze it in place, and then use it to process individual sentences?

I understand that there is a lot I can do to clean up the code, unfortunately as I've been trying different approaches (and still getting nowhere) the code has become a bit sloppy. Please help before it devolves into an unreadable mess :-)

If necessary I can remove some of the code to simplify things, I was just worried someone might ask for the full code for evaluation.

What I have tried:

I've tried following several tutorials online, and going through the toy code for SavedModelBuilder and working with checkpoints but to no avail.


Quick update, my problems stemmed from using an older (1.5) version of Tensorflow. After upgrading to the latest version I was able to follow the tutorials and get things to work.
Quick update, my problems stemmed from using an older (1.5) version of Tensorflow. After upgrading to the latest version I was able to follow the tutorials and get things to work.


这篇关于交互使用训练有素的张量流模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆