如何使用张量流梯度带找到解析梯度

发布于 2025-01-11 08:28:20 字数 356 浏览 6 评论 0原文

假设我们有一些函数 y=x^2

然后我们可以使用梯度带自动计算梯度(当我们向tensorflow提供一些x值时,

x = tf.Variable(3.0)

with tf.GradientTape() as tape:
  y = x**2
  dy_dx = tape.gradient(y, x)

我是否可以找出tensorflow对我的输入做了什么?例如在本例中是 容易找出 dy/dx=2x,这是否意味着张量流会将 2 乘以我的 x 输入值,然后返回 6(即 3*2)?

很 区分所以我想要从 TensorFlow 梯度带中找到见解,看看 TensorFlow 如何使用我的 x 输入计算出导数。

Suppose we have some function
y=x^2

We can then use gradient tape to automatically calculate the gradient for us (when we provide some values of x to tensorflow

x = tf.Variable(3.0)

with tf.GradientTape() as tape:
  y = x**2
  dy_dx = tape.gradient(y, x)

Is there anyway I can find out what did tensorflow do to my input? For example in this case it is easy to find out the dy/dx=2x, does that mean tensorflow will multiply 2 to my input value of x and then return me 6 (which is 3*2)?

I have a very complicated function which I don't know how to differentiate so I want to find insights from tensorflow gradienttape to see how tensorflow works out the derivative using my input of x.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙气飘飘 2025-01-18 08:28:20

一种可能的选择是使用 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)

在终端中打开tensorboard:

%load_ext tensorboard
%tensorboard --logdir logs/func

在此处输入图像描述

One possible option is using tensorboard and also printing the operations of the 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)

Open tensorboard in your terminal:

%load_ext tensorboard
%tensorboard --logdir logs/func

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文