我有一个形状[a,n] 的张量A,我需要my_op用另一个形状[b,n]的张量B执行一个op ,使得得到的张量C具有形状[a,b].
换句话说:对于A中的每个子指标(A [0],A 1,... A [n]),我需要对B中的每个子指标执行元素操作.
因此产生的张量将包含以下内容:
[ [ A[0] op B[0] , A[0] op B[1], ... , A[0] op B[b] ],
[ A[1] op B[0] , A[1] op B[1], ... , A[1] op B[b] ],
[ ... ],
[ A[a] op B[0] , A[a] op B[1], ... , A[a] op B[b] ] ]
Run Code Online (Sandbox Code Playgroud)
我能够找到的唯一方法就是通过嵌套使用tf.map_fn 这样:
import tensorflow as tf
import time …Run Code Online (Sandbox Code Playgroud) 我试图更多地了解在执行tf图时看到的某些令人惊讶的结果。我正在使用的图形只是一片森林(一堆树)。这只是一个简单的前向推论图,与训练无关。我正在分享2个实施的摘要
代码段1:
with tf.name_scope("main"):
def get_tree_output(offset):
loop_vars = (offset,)
leaf_indice = tf.while_loop(cond,
body,
loop_vars,
back_prop=False,
parallel_iterations=1,
name="while_loop")
tree_score = tf.gather(score_tensor, leaf_indice, name="tree-scores")
output = tf.add(tree_score, output)
leaf_indices = tf.map_fn(get_tree_output,
tree_offsets_tensor,
dtype=INT_TYPE,
parallel_iterations=n_trees,
back_prop=False,
name="tree-scores")
tree_scores = tf.gather(score_tensor, leaf_indices, name="tree-scores")
output = tf.reduce_sum(tree_scores, name="sum-output")
output = tf.sigmoid(output, name="sigmoid-output")
Run Code Online (Sandbox Code Playgroud)
代码段2:
with tf.name_scope("main"):
tree_offsets_tensor = tf.constant(tree_offsets, dtype=INT_TYPE, name="tree_offsets_tensor")
loop_vars = (tree_offsets_tensor,)
leaf_indices = tf.while_loop(cond,
body,
loop_vars,
back_prop=False,
parallel_iterations=n_trees,
name="while_loop")
tree_scores = tf.gather(score_tensor, leaf_indices, name="tree-scores")
output = tf.reduce_sum(tree_scores, name="sum-output")
output = tf.sigmoid(output, name="sigmoid-output")
Run Code Online (Sandbox Code Playgroud)
其余代码完全相同:while循环的常量张量,变量,条件和主体。在两种情况下,线程和并行性也相同代码snippet2:需要大约500微秒来进行推理代码片段1:需要大约12毫秒来进行推理 …
machine-learning deep-learning tensorflow tensorflow-serving tensor