2

我想分析一个非常简单的神经网络模型的 FLOPs,用于对 MNIST 数据集进行分类,批量大小为 128。按照官方教程,我得到了以下模型的结果,但我无法理解部分输出。

w1 = tf.Variable(tf.random_uniform([784, 15]), name='w1')
w2 = tf.Variable(tf.random_uniform([15, 10]), name='w2')
b1 = tf.Variable(tf.zeros([15, ]), name='b1')
b2 = tf.Variable(tf.zeros([10, ]), name='b2')

hidden_layer = tf.add(tf.matmul(images_iter, w1), b1)
logits = tf.add(tf.matmul(hidden_layer, w2), b2)

loss_op = tf.reduce_sum(\
    tf.nn.softmax_cross_entropy_with_logits(logits=logits, 
                                            labels=labels_iter))
opetimizer = tf.train.AdamOptimizer(learning_rate=0.01)
train_op = opetimizer.minimize(loss_op)

theimages_iter和 thelabels_iter是 tf.data 的迭代器,类似于占位符。

tf.profiler.profile(
    tf.get_default_graph(),
    options=tf.profiler.ProfileOptionBuilder.float_operation())

我使用此代码(相当于scope -min_float_ops 1 -select float_ops -account_displayed_op_onlytfprof 注释行工具中的代码)来分析 FLOP 并得到以下结果。

Profile:
node name | # float_ops
_TFProfRoot (--/23.83k flops)
  random_uniform (11.76k/23.52k flops)
    random_uniform/mul (11.76k/11.76k flops)
    random_uniform/sub (1/1 flops)
  random_uniform_1 (150/301 flops)
    random_uniform_1/mul (150/150 flops)
    random_uniform_1/sub (1/1 flops)
  Adam/mul (1/1 flops)
  Adam/mul_1 (1/1 flops)
  softmax_cross_entropy_with_logits_sg/Sub (1/1 flops)
  softmax_cross_entropy_with_logits_sg/Sub_1 (1/1 flops)
  softmax_cross_entropy_with_logits_sg/Sub_2 (1/1 flops)

我的问题是

  1. 括号里的数字是什么意思?例如random_uniform_1 (150/301 flops),什么是 150 和 301?
  2. 为什么_TFProfRoot括号中的第一个数字是“--”?
  3. 为什么 Adam/mul 和 softmax_cross_entropy_with_logits_sg/Sub 的 flop 是 1?

我知道读这么久的问题令人沮丧,但是一个无法从官方文档中找到相关信息的绝望男孩需要你们的帮助。

4

1 回答 1

1

我会试一试:

(1) 从这个例子看,第一个数字是“self” flops,第二个数字表示命名范围内的“total” flops。例如:对于分别命名为random_uniform(如果有这样的节点)、random_uniform/mul、random_uniform/sub的3个节点,它们分别占用了11.76k、11.76k、1个flops,总共23.52k个flops。

再举一个例子:23.83k = 23.52k + 300。

这有意义吗?

(2) 根节点是profiler添加的一个“虚拟”顶级节点,它没有“self” flops,或者说,它的self flops为零。

(3) 不知道为什么是 1. 如果您可以打印 GraphDef 并找出该节点的真正含义,这将有所帮助 print(sess.graph_def)

希望这可以帮助。

于 2018-07-15T17:55:06.893 回答