一种可能的选择是使用tensorboard
并打印以下操作tf.Graph
:
import tensorflow as tf
x = tf.Variable(3.0)
@tf.function
def f(x):
with tf.GradientTape() as tape:
y = x**2
return tape.gradient(y, x)
print(*[tensor for op in f.get_concrete_function(x).graph.get_operations() for tensor in op.values()], sep="\n")
logdir = 'logs/func/'
writer = tf.summary.create_file_writer(logdir)
tf.summary.trace_on(graph=True, profiler=True)
z = f(x)
with writer.as_default():
tf.summary.trace_export(
name="func_trace",
step=0,
profiler_outdir=logdir)
Tensor("x:0", shape=(), dtype=resource)
Tensor("ReadVariableOp:0", shape=(), dtype=float32)
Tensor("pow/y:0", shape=(), dtype=float32)
Tensor("pow:0", shape=(), dtype=float32)
Tensor("ones:0", shape=(), dtype=float32)
Tensor("gradient_tape/pow/mul:0", shape=(), dtype=float32)
Tensor("gradient_tape/pow/sub/y:0", shape=(), dtype=float32)
Tensor("gradient_tape/pow/sub:0", shape=(), dtype=float32)
Tensor("gradient_tape/pow/Pow:0", shape=(), dtype=float32)
Tensor("gradient_tape/pow/mul_1:0", shape=(), dtype=float32)
Tensor("Identity:0", shape=(), dtype=float32)
在终端中打开张量板:
%load_ext tensorboard
%tensorboard --logdir logs/func