tensorflow 如何对来自不同名称范围的操作进行分组?

标签 tensorflow

例如:

for s in xrange(2):

   with tf.name_scope('myscope_%d % s) as scope:

      some ops

      result = a op

然后我想将 myscope_0/resultmyscope_1/result 分组在一起,如下所示:

allops = tf.group(myscope_0/result, myscope_1/result)

怎么做?上面的行不正确。

最佳答案

你可以这样做:

graph = tf.get_default_graph()

var = tf.Variable(0)

for s in xrange(2):
    with tf.name_scope('myscope_%d' % s):
        op = var.assign_add(s + 1)

all_operations = [graph.get_operation_by_name('myscope_%d/AssignAdd' % s) for s in range(2)]
all_op = tf.group(*all_operations)

在这里,我将变量 var 的更新操作组合在一起(第一个将其递增 1,第二个将递增 2)。


你可以测试一下:

sess = tf.Session()
sess.run(tf.initialize_all_variables())
print sess.run(var)  # prints 0
sess.run(all_op)
print sess.run(var)  # prints 3

关于 tensorflow 如何对来自不同名称范围的操作进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38045592/

相关文章:

python - 无法在 Google COLAB 上将我的 Dataframe 保存/导出为 CSV

tensorflow - 从 tf.data.Dataset.map() 返回数据集会导致 'TensorSliceDataset' 对象没有属性 'get_shape' 错误

tensorflow - 我可以在 Maya 和 Blender 中导入 tensorflow 和 keras

android - TensorFlow 对象检测模型与股票模型一起正常工作,但失败并出现有关未实现已实现操作的错误

python - 我应该使用 tf.keras.utils.normalize 来标准化我的目标吗?

tensorflow - 从 TensorFlow 中给定的非均匀分布中进行无放回采样

python - 是否可以创建同一 CNN 的多个实例,这些实例接收多个图像并连接成一个密集层? (喀拉斯)

python - TensorFlow 无法识别 feed_dict 输入

machine-learning - 如何获取神经网络参数的输出 'mathematical' 梯度并计算两个此类梯度之间的余弦

tensorflow - 如何打印 `tf.data.Dataset.from_tensor_slices` 的结果?