有没有办法在flask(0.12.2)应用程序中使用预先训练好的char-lstm tensorflow和GPU(1.2)模型?
模型在通过shell启动时无缝运行。我使用下面的代码加载它(没有初始化变量,占位符或类似代码):
....
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, SAVE_PATH)
....
当尝试通过Flask加载相同的预训练模型(包含3个文件:.meta
,.index
& .data
)时,它会抛出
ValueError:没有要保存的变量
有没有办法让它发挥作用?非常感谢!
答案 0 :(得分:0)
我认为这是因为您需要正确初始化和使用变量。
这是我能找到的最简单的例子:
import tensorflow as tf
# Create TensorFlow object called hello_constant
hello_constant = tf.constant('Hello World!')
with tf.Session() as sess:
# Run the tf.constant operation in the session
output = sess.run(hello_constant)
print(output)
对您更有用的更复杂的例子是:
def model_inputs():
"""
Create TF Placeholders for input, targets, learning rate, and lengths of source and target sequences.
:return: Tuple (input, targets, learning rate, keep probability, target sequence length,
max target sequence length, source sequence length)
"""
input = tf.placeholder(tf.int32,[None,None],name='input')
target = tf.placeholder(tf.int32,[None,None])
target_sequence_len = tf.placeholder(tf.int32, [None], name='target_sequence_length')
max_target_len = tf.reduce_max(target_sequence_len,name='max_target_length' )
source_sequence_len = tf.placeholder(tf.int32, [None], name='source_sequence_length')
learning_rate = tf.placeholder(tf.float32)
keep_prob = tf.placeholder(tf.float32, name='keep_prob')
return input, target, learning_rate, keep_prob, target_sequence_len, max_target_len, source_sequence_len
以下是如何在会话中使变量可用
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
主题重要,我发现很难在单个StackOverflow回复中有效地总结它。
如果您想深入了解系统的工作原理,我建议您阅读TensorFlow documentation about variables.