tf.GraphOptions.build_cost_model
选项定义为在生成成本模型之前将要运行的步骤数。为什么我们有此选项?换句话说,“步骤数”到底是什么意思?
我有这段代码可以看到矩阵乘法的成本估算:
import tensorflow as tf
import datetime
import numpy as np
A = np.random.rand(100, 10).astype('float32')
B = np.random.rand(10, 200).astype('float32')
with tf.device('/cpu:0'):
a = tf.placeholder(tf.float32, [100, 10])
b = tf.placeholder(tf.float32, [10, 200])
mul = tf.matmul(a,b)
metadata = tf.RunMetadata()
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
ops = tf.GraphOptions(build_cost_model=2)
with tf.Session(config=tf.ConfigProto(log_device_placement=True, graph_options=ops)) as sess:
# Run the op.
for _ in range(2):
sess.run([mul], feed_dict={a:A, b:B}, options=run_options,run_metadata=metadata)
print ("Cost graph " + str(metadata.cost_graph)
然后,我将其作为输出:
Cost graph
Cost graph node {
name: "_SOURCE"
compute_cost: 2
}
node {
name: "_SINK"
id: 1
control_input: 0
control_input: 0
control_input: 5
}
node {
name: "_arg_Placeholder_38_0_1"
device: "/job:localhost/replica:0/task:0/device:CPU:0"
id: 2
output_info {
alias_input_port: -1
shape {
dim {
size: 10
}
dim {
size: 200
}
}
dtype: DT_FLOAT
}
control_input: 0
compute_cost: 1
}
node {
name: "_arg_Placeholder_37_0_0"
device: "/job:localhost/replica:0/task:0/device:CPU:0"
id: 3
output_info {
alias_input_port: -1
shape {
dim {
size: 100
}
dim {
size: 10
}
}
dtype: DT_FLOAT
}
control_input: 0
compute_cost: 1
}
node {
name: "MatMul_11"
device: "/job:localhost/replica:0/task:0/device:CPU:0"
id: 4
input_info {
preceding_node: 3
}
input_info {
preceding_node: 2
}
output_info {
size: 80000
alias_input_port: -1
shape {
dim {
size: 100
}
dim {
size: 200
}
}
dtype: DT_FLOAT
}
compute_cost: 24
}
node {
name: "_retval_MatMul_11_0_0"
device: "/job:localhost/replica:0/task:0/device:CPU:0"
id: 5
input_info {
preceding_node: 4
}
compute_cost: 2
}
这里的计算成本如何估算?为什么运行更多次会影响成本?