Tensorflow 如何将shape参数传递给tf.nn.ctc\u贪婪\u解码器?

Tensorflow 如何将shape参数传递给tf.nn.ctc\u贪婪\u解码器?,tensorflow,Tensorflow,我正在尝试对加载的图形执行推断: 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')

我正在尝试对加载的图形执行推断:

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]) 

但我仍然需要处理这样一个事实,即输出长度是在执行期间计算的。不幸的是,
ctc\u贪婪\u解码器
没有将
output\u length
作为张量。或者我会输入
tf.shape(logits)

如果没有完整的代码,很难回答,但是您是对的,您应该在进入循环之前将所有操作添加到图形中。而且似乎没有什么可以阻止您使用
图形输出的
张量形状
(顺便记住,不需要中间调用,只需计算您感兴趣的张量,任何中间张量都将自动计算):


问题是,
ctc\u贪婪\u解码器
不获取张量列表,而是获取
int32
列表。在构建这部分图之前,我正试图想出一种方法来推断动态形状,这很烦人。它确实需要一个张量或张量列表:
shape\u tensor=tf.constant([1])tf\u greedy\u path,\u=tf.nn.ctc\u greedy\u解码器(logits,shape\u tensor,merge\u repeated=True)
工作正常。您正在使用
tf.constant
定义张量的形状。在我的情况下,
graph\u输出的维度取决于输入的维度(动态形状)。我得到了
graph\u output.shape[0]->?
tf.shape(graph\u output)[0]->张量(“跨步切片:0”,shape=(),dtype=int32)
tf.constant
是一个张量,所以它只是为了说明
tf.nn.ctc\u贪婪的解码器可以采用张量参数。无论如何,请看我的更新答案,了解动态形状的示例。您帮助我理解了这个问题,非常感谢!我在执行中间结果的
sess.run()
时遇到了麻烦。我假设如果我运行
sess.run(logits)
,那么
sess.run(贪婪的输出)
,我就不必对这两个都使用
feed\u dict
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.]]]})))