我正在试图弄清楚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内部我发现两个分配操作都是通过以下方式引用的:
[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_1”而不是“assign”和“assign_again”?我知道它看起来像一个愚蠢的问题,但我需要控制我的项目中的节点命名方面。