首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将形状参数传递给tf.nn.ctc_greedy_decoder?

如何将形状参数传递给tf.nn.ctc_greedy_decoder?
EN

Stack Overflow用户
提问于 2018-03-04 01:32:23
回答 1查看 271关注 0票数 0

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

代码语言:javascript
复制
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')

我正在迭代的变量是

代码语言:javascript
复制
inp[i]
sl[i]

在循环中

代码语言:javascript
复制
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

我知道这个片段在每次迭代时都会将元素添加到图中。但我不知道如何具体解决这个问题。

我有限的理解告诉我要在循环之外创建图形元素:

代码语言:javascript
复制
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)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-04 14:56:47

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

代码语言:javascript
复制
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.]]]})))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49091058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档