- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
https://www.tensorflow.org/versions/r1.6/api_docs/python/tf/gradients
在 tf.gradients(ys, xs) 的文档中指出
Constructs symbolic derivatives of sum of ys w.r.t. x in xs
我对求和部分感到困惑,我在其他地方读到过,这对批处理中的每个 x 求和了批处理中的导数 dy/dx。但是,每当我使用它时,我都看不到这种情况的发生。举个简单的例子:
x_dims = 3
batch_size = 4
x = tf.placeholder(tf.float32, (None, x_dims))
y = 2*(x**2)
grads = tf.gradients(y,x)
sess = tf.Session()
x_val = np.random.randint(0, 10, (batch_size, x_dims))
y_val, grads_val = sess.run([y, grads], {x:x_val})
print('x = \n', x_val)
print('y = \n', y_val)
print('dy/dx = \n', grads_val[0])
这给出了以下输出:
x =
[[5 3 7]
[2 2 5]
[7 5 0]
[3 7 6]]
y =
[[50. 18. 98.]
[ 8. 8. 50.]
[98. 50. 0.]
[18. 98. 72.]]
dy/dx =
[[20. 12. 28.]
[ 8. 8. 20.]
[28. 20. 0.]
[12. 28. 24.]]
这是我期望的输出,只是批处理中每个元素的导数 dy/dx。我没有看到任何求和发生。我在其他示例中看到,此操作之后除以批处理大小以计算 tf.gradients() 对批处理的梯度求和(参见此处:https://pemami4911.github.io/blog/2016/08/21/ddpg-rl.html)。为什么这是必要的?
我正在使用 Tensorflow 1.6 和 Python 3。
最佳答案
如果 y 和 x 具有相同的形状,则 dy/dx 的总和恰好是一个值的总和。但是,如果每个 x 有多个 y,则对梯度求和。
import numpy as np
import tensorflow as tf
x_dims = 3
batch_size = 4
x = tf.placeholder(tf.float32, (None, x_dims))
y = 2*(x**2)
z = tf.stack([y, y]) # There are twice as many z's as x's
dy_dx = tf.gradients(y,x)
dz_dx = tf.gradients(z,x)
sess = tf.Session()
x_val = np.random.randint(0, 10, (batch_size, x_dims))
y_val, z_val, dy_dx_val, dz_dx_val = sess.run([y, z, dy_dx, dz_dx], {x:x_val})
print('x.shape =', x_val.shape)
print('x = \n', x_val)
print('y.shape = ', y_val.shape)
print('y = \n', y_val)
print('z.shape = ', z_val.shape)
print('z = \n', z_val)
print('dy/dx = \n', dy_dx_val[0])
print('dz/dx = \n', dz_dx_val[0])
产生以下输出:
x.shape = (4, 3)
x =
[[1 4 8]
[0 2 8]
[2 8 1]
[4 5 2]]
y.shape = (4, 3)
y =
[[ 2. 32. 128.]
[ 0. 8. 128.]
[ 8. 128. 2.]
[ 32. 50. 8.]]
z.shape = (2, 4, 3)
z =
[[[ 2. 32. 128.]
[ 0. 8. 128.]
[ 8. 128. 2.]
[ 32. 50. 8.]]
[[ 2. 32. 128.]
[ 0. 8. 128.]
[ 8. 128. 2.]
[ 32. 50. 8.]]]
dy/dx =
[[ 4. 16. 32.]
[ 0. 8. 32.]
[ 8. 32. 4.]
[16. 20. 8.]]
dz/dx =
[[ 8. 32. 64.]
[ 0. 16. 64.]
[16. 64. 8.]
[32. 40. 16.]]
特别注意,dz/dx 的值是 dy/dz 的两倍,因为它们是在堆栈的输入上求和的。
关于python - tf.gradients() 对 ys 求和,是吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51858970/
我想替换以下特定于 Mozilla 的 CSS 规则: background: -moz-linear-gradient(center top , #F5F5F5, #E4E4E4); 使用标准 li
我有一个当前正在使用的渐变,我想将其从 -webkit 转换为默认的线性渐变。我曾尝试使用所有在线 CSS 生成工具来导入代码,以便查看其他浏览器特定的变体,但由于某种原因,当我导入代码时,结果部分中
我正在尝试在段落中添加行,但我无法将以下语法的线性渐变转换为 -webkit-gradient 语法,因为 wicked_pdf for rails 不支持线性渐变。 任何帮助,我无法在网上找到 -w
-moz-linear-gradient 和 -webkit-gradient 之间的基本区别是什么。我知道更改是在参数中进行的,-moz 是针对 mozilla 的。但是为什么我们为 Mozilla
作为 Tensorflow 新手,我试图了解 tf.gradients 和 tf.keras.backend.gradients 底层功能之间的区别。 后者找到输入特征值相对成本函数的梯度。 但是我无
我为按钮的背景使用了一些 CSS,但它在 Firefox、Chrome 和 IE 中看起来不同。 我正在使用 -webkit-linear-gradient 和 -moz-linear-gradien
在具有嵌入层和 SimpleRNN 层的模型中,我想为每个步骤 t 计算偏导数 dh_t/dh_0。 我的模型结构,包括导入和数据预处理。 有毒评论列车数据可用:https://www.kaggle.
我刚看完the notes CNN 上的斯坦福 CS231n 和现场链接 demo ;然而,我不确定演示中的“激活”、“激活梯度”、“权重”和“权重梯度”指的是什么。 以下屏幕截图已从演示中复制。 混
Opera 和其他主要浏览器是否有 -moz-linear-gradient 或 -webkit-gradient 类型的 CSS? 最佳答案 IE 实际上支持渐变,Opera 则不支持(从 10.5
TL;DR -webkit-linear-gradient 正在打破 -moz-linear-gradient 听说是个难题,我使用的是带有两个 handle 的 Jquery Slider 插件,我
正如标题中所述,我已经阅读了多个来源,Policy Gradient 方法是 RL 适用于大型 Action 空间,但我不明白为什么会这样。 我想看看 RL 是否可以解决我的一个具有巨大组合数的问题。
我想做以下事情: import theano, numpy, theano.tensor as T a = T.fvector('a') w = theano.shared(numpy.array([
这是我需要在 CSS 中实现的(它非常丑陋,但它作为一个例子很好地说明了我的问题): 我们在文本上设置了渐变效果,背景上有阴影。 我已经尝试了所有我能找到的方法。 This method不适用于文本阴
我有这个代码: #likebuttonsmob {margin-left:auto; margin-right:auto; width: 340px; margin-bottom: 8px; padd
根据 CSS Tricks , 以下 CSS 语法将导致左边框渐变。 .left-to-right { border-width:3px 0 3px 3px; -webkit-border-image
我正在尝试将最小化功能拆分到两台机器上。在一台机器上,我调用“compute_gradients”,在另一台机器上调用“apply_gradients”,渐变通过网络发送。问题是无论我做什么,调用 a
我在学习后记我正在尝试创建一种方法来绘制垂直 渐变 . 这是我的代码: %!PS-Adobe-3.0 %%%%%%%%%%%%%%%%%%%%%%% % draw a RECTANGLE /Rect
我正在尝试TensorFlow,但遇到了一个奇怪的错误。我编辑了较深的MNIST示例,以使用另一组图像,该算法再次很好地收敛,直到迭代8000(此时的精度为91%)崩溃并出现以下错误。 tensorf
我在 xml 布局中有以下代码,我想将其移动到撰写中,但很难将其正确化 我尝试使用 Box 进行 Compose,但不起作用
func fadeLightBar(){ UIView.animateWithDuration(fadeTime, delay: 0, options: UIViewAnimation
我是一名优秀的程序员,十分优秀!