为什么我不能为赋值运算设置一个名称

2024-09-26 22:50:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我想弄清楚TensorFlow是如何将一个图序列化和反序列化为protobuf的。运行这个Python脚本,我能够生成一个包含protobuf的检查点文件(我可以稍后使用它来恢复图形):

import tensorflow as tf


# variable
w = tf.get_variable(name="weights", dtype=tf.float32, shape=[1], initializer=tf.zeros_initializer, use_resource=True)

# placeholders
x1 = tf.placeholder(name="x1", dtype=tf.float32)
x2 = tf.placeholder(name="x2", dtype=tf.float32)

# assign 
new_w = tf.assign(w, x1, name="assign")
new_w_again = tf.assign(w, x2, name="assign_again") # not used

# session
init = tf.global_variables_initializer()
with tf.Session() as sess:
        sess.run(init)

        before = sess.run(w)
        sess.run(new_w, feed_dict={x1: 10.0})
        after = sess.run(w)

        print("before/after: {}/{}".format(before, after))

        saver = tf.train.Saver()
        path_to_checkpoint = saver.save(sess, "./../model-store-python/assign-model")

为了完整性,脚本按预期工作,运行时的输出是:

before/after: [0.]/10.0

顺便说一句,在protobuf中我发现两个assign操作是由:

[protobuf stuff ...]

  node {
    name: "AssignVariableOp"
    op: "AssignVariableOp"
    input: "weights"
    input: "x1"
    attr {
      key: "dtype"
      value {
        type: DT_FLOAT
      }
    }
  }

  [... protobuf stuff ...]

  node {
    name: "AssignVariableOp_1"
    op: "AssignVariableOp"
    input: "weights"
    input: "x2"
    attr {
      key: "dtype"
      value {
        type: DT_FLOAT
      }
    }
  }

  [... protobuf stuff]

为什么分配操作的两个名称是“AssignVariableOp”和“AssignVariableOp\u 1”而不是“assign”和“assign\u again”?我知道这看起来像个愚蠢的问题,但我需要在我的项目中控制节点命名方面。你知道吗


Tags: runnameinputtfsessx1x2protobuf

热门问题