我正在尝试理解Tensorflow,并在查看官方示例之一,即Cifar-10模型。
在cifar10.py中的inference()函数中,你可以看到以下代码行:
with tf.variable_scope('softmax_linear') as scope: weights = _variable_with_weight_decay('weights', [192, NUM_CLASSES], stddev=1/192.0, wd=0.0) biases = _variable_on_cpu('biases', [NUM_CLASSES], tf.constant_initializer(0.0)) softmax_linear = tf.add(tf.matmul(local4, weights), biases, name=scope.name) _activation_summary(softmax_linear)
scope.name应该是softmax_linear,这应该是节点的名称。我使用以下代码行保存了图形协议(与教程中的有所不同):
with tf.Graph().as_default(): global_step = tf.Variable(0, trainable=False) # 获取图像和标签 images, labels = cifar10.distorted_inputs() # 构建一个计算推理模型预测的图形。 logits = cifar10.inference(images) # 计算损失。 loss = cifar10.loss(logits, labels) # 构建一个用一批示例训练模型并更新模型参数的图形。 train_op = cifar10.train(loss, global_step) # 创建一个保存器。 saver = tf.train.Saver(tf.global_variables()) # 根据TF的摘要集合构建摘要操作。 summary_op = tf.summary.merge_all() # 构建一个初始化操作以在下面运行。 init = tf.global_variables_initializer() # 开始在图形上运行操作。 sess = tf.Session(config=tf.ConfigProto( log_device_placement=FLAGS.log_device_placement)) sess.run(init) # 保存图形 tf.train.write_graph(sess.graph_def, FLAGS.train_dir, 'model.pbtxt') ....
但我在model.pbtxt中看不到名为softmax_linear的节点。我做错了什么?我只想知道输出节点的名称,以便导出图形。
回答:
操作符名称不会是"softmax_linear"
。tf.name_scope()
会用其名称作为前缀,并用/
分隔操作符的名称。每个操作符都有自己的名称。例如,如果你写
with tf.name_scope("foo"): a = tf.constant(1, name="bar")
那么常量将具有名称"foo/bar"
。
希望这对你有帮助!