- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
import numpy
# Data and parameters
X = numpy.array([[-1.086, 0.997, 0.283, -1.506]])
T = numpy.array([[-0.579]])
W1 = numpy.array([[-0.339, -0.047, 0.746, -0.319, -0.222, -0.217],
[ 1.103, 1.093, 0.502, 0.193, 0.369, 0.745],
[-0.468, 0.588, -0.627, -0.319, 0.454, -0.714],
[-0.070, -0.431, -0.128, -1.399, -0.886, -0.350]])
W2 = numpy.array([[ 0.379, -0.071, 0.001, 0.281, -0.359, 0.116],
[-0.329, -0.705, -0.160, 0.234, 0.138, -0.005],
[ 0.977, 0.169, 0.400, 0.914, -0.528, -0.424],
[ 0.712, -0.326, 0.012, 0.437, 0.364, 0.716],
[ 0.611, 0.437, -0.315, 0.325, 0.128, -0.541],
[ 0.579, 0.330, 0.019, -0.095, -0.489, 0.081]])
W3 = numpy.array([[ 0.191, -0.339, 0.474, -0.448, -0.867, 0.424],
[-0.165, -0.051, -0.342, -0.656, 0.512, -0.281],
[ 0.678, 0.330, -0.128, -0.443, -0.299, -0.495],
[ 0.852, 0.067, 0.470, -0.517, 0.074, 0.481],
[-0.137, 0.421, -0.443, -0.557, 0.155, -0.155],
[ 0.262, -0.807, 0.291, 1.061, -0.010, 0.014]])
W4 = numpy.array([[ 0.073],
[-0.760],
[ 0.174],
[-0.655],
[-0.175],
[ 0.507]])
B1 = numpy.array([-0.760, 0.174, -0.655, -0.175, 0.507, -0.300])
B2 = numpy.array([ 0.205, 0.413, 0.114, -0.560, -0.136, 0.800])
B3 = numpy.array([-0.827, -0.113, -0.225, 0.049, 0.305, 0.657])
B4 = numpy.array([-0.270])
# Forward pass
Z1 = X.dot(W[0])+B[0]
A1 = numpy.maximum(0,Z1)
Z2 = A1.dot(W[1])+B[1]
A2 = numpy.maximum(0,Z2)
Z3 = A2.dot(W[2])+B[2]
A3 = numpy.maximum(0,Z3)
Y = A3.dot(W[3])+B[3];
# Error
err = ((Y-T)**2).mean()
给出这个例子,我想实现向后传递,并获得关于权重和偏置参数的梯度。显然,最后一层的梯度如下:
DY = 2*(Y-T)
DB4 = DY.mean(axis=0)
DW4 = A3.T.dot(DY) / len(X)
DZ3 = DY.dot(W4.T)*(Z3 > 0)
我知道不同的导数是使用链式法则计算的,但我不太明白你是如何得出这个解决方案的。
最佳答案
让我们使用(偏)导数的链式法则和矩阵微分法则,引用下图显示了神经网络的最后一个隐藏层,用于回归(MSE)误差的反向传播:
E = err = (Y - T)**2 (take mean over the batch to compute MSE)
DY = ∂E/∂Y= 2 * (Y - T)
∂E/∂W3= (∂E/∂Y).(∂Y/∂W3)
= DY. (∂/∂W3 (A3.W3+B3))= DY.A3.T= A3.T.DY(take mean over all training examples in training batch X: sum and divide by batch size |X|)
∂E/∂B3= (∂E/∂Y).(∂Y/∂B3)
= DY. (∂/∂B3 (A3.W3+B3))= DY.1= DY (take mean over all the examples in a batch)
∂E/∂Z3
= (∂E/∂Y).(∂Y/∂A3).(∂A3/∂Z3)= DY.(∂/∂A3 (A3.W3+B3)).(1.𝟙{Z3>0} + 0.𝟙{Z3 <= 0})
= DY. W3.T. 𝟙{Z3 > 0), where 𝟙(.) is the indicator function. Using thedefinition of nonlinear RELU activation, the derivative is 1 whenZ3>0, otherwise 0.
关于python - 计算纯python中NN的梯度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66628950/
我正在尝试调整 tf DeepDream 教程代码以使用另一个模型。现在当我调用 tf.gradients() 时: t_grad = tf.gradients(t_score, t_input)[0
考虑到 tensorflow 中 mnist 上的一个简单的小批量梯度下降问题(就像在这个 tutorial 中),我如何单独检索批次中每个示例的梯度。 tf.gradients()似乎返回批次中所有
当我在 numpy 中计算屏蔽数组的梯度时 import numpy as np import numpy.ma as ma x = np.array([100, 2, 3, 5, 5, 5, 10,
除了数值计算之外,是否有一种快速方法来获取协方差矩阵(我的网络激活)的导数? 我试图将其用作深度神经网络中成本函数中的惩罚项,但为了通过我的层反向传播误差,我需要获得导数。 在Matlab中,如果“a
我有一个计算 3D 空间标量场值的函数,所以我为它提供 x、y 和 z 坐标(由 numpy.meshgrid 获得)的 3D 张量,并在各处使用元素运算。这按预期工作。 现在我需要计算标量场的梯度。
我正在使用内核密度估计 (KDE) ( http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gaussian_kde.htm
我对 tensorflow gradient documentation 中的示例感到困惑用于计算梯度。 a = tf.constant(0.) b = 2 * a g = tf.gradients(
我有一个 softmax 层(只有激活本身,没有将输入乘以权重的线性部分),我想对其进行向后传递。 我找到了很多关于 SO 的教程/答案来处理它,但它们似乎都使用 X 作为 (1, n_inputs)
仅供引用,我正在尝试使用 Tensorflow 实现梯度下降算法。 我有一个矩阵X [ x1 x2 x3 x4 ] [ x5 x6 x7 x8 ] 我乘以一些特征向量 Y 得到 Z [ y
我目前有一个由几百万个不均匀分布的粒子组成的体积,每个粒子都有一个属性(对于那些好奇的人来说是潜在的),我想为其计算局部力(加速度)。 np.gradient 仅适用于均匀间隔的数据,我在这里查看:S
我正在寻找有关如何实现 Gradient (steepest) Descent 的建议在 C 中。我正在寻找 f(x)=||Ax-y||^2 的最小值,其中给出了 A(n,n) 和 y(n)。 这在
我正在查看 SVM 损失和导数的代码,我确实理解了损失,但我无法理解如何以矢量化方式计算梯度 def svm_loss_vectorized(W, X, y, reg): loss = 0.0 dW
我正在寻找一种有效的方法来计算 Julia 中多维数组的导数。准确地说,我想要一个等效的 numpy.gradient在 Julia 。但是,Julia 函数 diff : 仅适用于二维数组 沿微分维
我在cathesian 2D 系统中有两个点,它们都给了我向量的起点和终点。现在我需要新向量和 x 轴之间的角度。 我知道梯度 = (y2-y1)/(x2-x1) 并且我知道角度 = arctan(g
我有一个 2D 数组正弦模式,想要绘制该函数的 x 和 y 梯度。我有一个二维数组 image_data : def get_image(params): # do some maths on
假设我有一个针对 MNIST 数据的简单 TensorFlow 模型,如下所示 import tensorflow as tf from tensorflow.examples.tutorials.m
我想查看我的 Tensorflow LSTM 随时间变化的梯度,例如,绘制从 t=N 到 t=0 的梯度范数。问题是,如何从 Tensorflow 中获取每个时间步长的梯度? 最佳答案 在图中定义:
我有一个简单的神经网络,我试图通过使用如下回调使用张量板绘制梯度: class GradientCallback(tf.keras.callbacks.Callback): console =
在CIFAR-10教程中,我注意到变量被放置在CPU内存中,但它在cifar10-train.py中有说明。它是使用单个 GPU 进行训练的。 我很困惑..图层/激活是否存储在 GPU 中?或者,梯度
我有一个 tensorflow 模型,其中层的输出是二维张量,例如 t = [[1,2], [3,4]] . 下一层需要一个由该张量的每一行组合组成的输入。也就是说,我需要把它变成t_new = [[
我是一名优秀的程序员,十分优秀!