我试图对加载的图形执行推断:
ds_graph = load_graph(model)
graph_input = ds_graph.get_tensor_by_name('prefix/input_node:0')
graph_seqlength = ds_graph.get_tensor_by_name('prefix/input_lengths:0')
graph_output = ds_graph.get_tensor_by_name('prefix/output_node:0')我正在迭代的变量是
inp[i]
sl[i]在循环中
for i in range(num):
with tf.Session(graph=ds_graph) as sess:
logits = sess.run(graph_output,feed_dict={graph_input:inp[i],graph_seqlength:sl[i]})
logits = tf.nn.softmax(logits, dim=-1, name=None)
logits = sess.run(logits)
output_length=np.array([logits.shape[0]])
tf_greedy_path, _ = tf.nn.ctc_greedy_decoder(logits,output_length,merge_repeated=True)
tf_greedy_path = tf.convert_to_tensor([tf.sparse_tensor_to_dense(sparse_tensor) for sparse_tensor in tf_greedy_path])
greed_out = ndarray_to_text(sess.run(tf_greedy_path)[0][0])
return greed_out我知道这个片段在每次迭代时都会将元素添加到图中。但我不知道如何具体解决这个问题。
我有限的理解告诉我要在循环之外创建图形元素:
logits = tf.nn.softmax(graph_output, dim=-1, name=None)
tf_greedy_path, _ = tf.nn.ctc_greedy_decoder(logits,output_length,merge_repeated=True)
tf_greedy_path = tf.convert_to_tensor([tf.sparse_tensor_to_dense(sparse_tensor) for sparse_tensor in tf_greedy_path])
for i in range(num):
with tf.Session(graph=ds_graph) as sess:
sess.run(graph_output,feed_dict={graph_input:inp[i],graph_seqlength:sl[i]})
sess.run(logits)
output_length=np.array([logits.shape[0]])
greed_out = ndarray_to_text(sess.run(tf_greedy_path)[0][0]) 但我仍然需要处理一个事实,即output_length是在执行过程中计算出来的。不幸的是,ctc_greedy_decoder没有将output_length作为张量。否则我会通过tf.shape(logits)的
发布于 2018-03-04 14:56:47
没有完整的代码是很难回答的,但是是的,您是对的,您应该在进入循环之前将所有操作添加到图中。似乎没有什么能阻止您使用tensor shape of您的graph_output张量(顺便记住,不需要中间调用,只需计算您感兴趣的张量,任何中间张量都会自动计算):
import tensorflow as tf
graph_output = tf.placeholder(tf.float32, shape=[None, 1, 2]) # graph_output has a dynamic shape
logits = tf.nn.softmax(graph_output, dim=-1, name=None)
tf_greedy_path, _ = tf.nn.ctc_greedy_decoder(logits,[graph_output.shape[0]],merge_repeated=True)
tf_greedy_path = tf.convert_to_tensor([tf.sparse_tensor_to_dense(sparse_tensor) for sparse_tensor in tf_greedy_path])
for i in range(10):
with tf.Session() as sess:
print(sess.run(tf_greedy_path, feed_dict={graph_output:[[[1., 2.]], [[3., 4.]]]})))https://stackoverflow.com/questions/49091058
复制相似问题