Skip to main content
 首页 » 编程设计

python之在张量上计算为图中的 numpy 数组

2025年04月02日13sharpest

有没有办法对图中的张量进行一些计算。

例如我的图表:

slim = tf.contrib.slim 
 
def slim_graph(images, train=False): 
    with slim.arg_scope([slim.conv2d, slim.fully_connected], 
                  activation_fn=tf.nn.relu, 
                  weights_initializer=tf.truncated_normal_initializer(0.0, 0.01), 
                  weights_regularizer=slim.l2_regularizer(0.0005)): 
        net = slim.repeat(images, 2, slim.conv2d, 64, [3, 3], scope='conv1') 
        // Do my compute by numpy on net 
 
        np_array_result = my_func(net) 
 
        // It will return a numpy array 
        // Use numpy array as input of graph 
 
        net = slim.max_pool2d(np_array_result, [2, 2], scope='pool1') 
 
        ... 
        return logits 
  • 我们可以做类似的事情吗?
  • 如何获取图中的特征图进行计算?

我可以将图形分成两部分并使用 Session.run([part1]) 之后使用结果输入我的函数,然后将其提供给 Session.run([part2])

但这看起来很奇怪。

请您参考如下方法:

您可以使用 tf.py_func python 函数的包装器。