- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我们有一个使用 BatchNormalization 的简单 Keras 模型:
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(1,)),
tf.keras.layers.BatchNormalization()
])
如何实际使用 GradientTape?以下似乎不起作用,因为它没有更新移动平均线?
# model training... we want the output values to be close to 150
for i in range(1000):
x = np.random.randint(100, 110, 10).astype(np.float32)
with tf.GradientTape() as tape:
y = model(np.expand_dims(x, axis=1))
loss = tf.reduce_mean(tf.square(y - 150))
grads = tape.gradient(loss, model.variables)
opt.apply_gradients(zip(grads, model.variables))
特别是,如果您检查移动平均值,它们将保持不变(检查 model.variables,平均值始终为 0 和 1)。我知道可以使用 .fit() 和 .predict(),但我想使用 GradientTape 并且我不知道如何执行此操作。某些版本的文档建议更新 update_ops,但这似乎在 eager 模式下不起作用。
特别是,经过上述训练后,以下代码将不会输出任何接近 150 的值。
x = np.random.randint(200, 210, 100).astype(np.float32)
print(model(np.expand_dims(x, axis=1)))
最佳答案
使用梯度磁带模式 BatchNormalization 层应使用参数training=True 进行调用
示例:
inp = KL.Input( (64,64,3) )
x = inp
x = KL.Conv2D(3, kernel_size=3, padding='same')(x)
x = KL.BatchNormalization()(x, training=True)
model = KM.Model(inp, x)
然后移动变量被正确更新
>>> model.layers[2].weights[2]
<tf.Variable 'batch_normalization/moving_mean:0' shape=(3,) dtype=float32, numpy
=array([-0.00062087, 0.00015137, -0.00013239], dtype=float32)>
关于python - 如何将 Tensorflow BatchNormalization 与 GradientTape 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56588353/
BatchNormalization 和 TimeDistributed(BatchNormalization) 是否对顺序数据(例如视频)有相同的影响?如果不是有什么区别? 最佳答案 在 tf.ke
我正在尝试将预训练的 BN 权重从 pytorch 模型复制到其等效的 Keras 模型,但我不断得到不同的输出。 我阅读了 Keras 和 Pytorch BN 文档,我认为区别在于它们计算“均值”
我有两种类型的灰度图像,比如汽车和飞机。在我的训练集中,我有 1000 张图像(大约 50/50 分割)。在这个训练集中,我所有的飞机示例都在白色背景上,而所有汽车示例都在黑色背景上(这是故意的,模型
我正在训练一个 CNN,为了调试我的问题,我正在研究实际训练数据的一小部分。 在训练期间,损失和准确性似乎非常合理且相当不错。 (在示例中我使用了相同的小子集进行验证,问题已经在这里显示了) 适合 x
我有这些 Backprop 更新,请让我知道 dx 部分哪里出了问题。在计算图中,我使用X、sample_mean 和sample_var。感谢您的帮助 (x, norm, sample_mean,
我对如何使用/插入"BatchNorm"有点困惑我的模型中的图层。 我看到了几种不同的方法,例如: ResNets : "BatchNorm"+"Scale" (无参数共享) “BatchNorm”
我想弄清楚 Keras 中的批量归一化究竟做了什么。现在我有以下代码。 for i in range(8): c = Convolution2D(128, 3, 3, border_mode
我正在尝试将批量规范添加到 Keras 中的 vgg 样式模型。当我添加 batch norm 层时,出现错误: FailedPreconditionError: Attempting to use
keras BatchNormalization layer使用 axis=-1 作为默认值,并说明特征轴通常是归一化的。为什么会这样? 我想这很令人惊讶,因为我更熟悉使用类似 StandardSca
是 batchnorm momentum convention (默认值=0.1)与其他库一样正确,例如Tensorflow 好像一般默认是 0.9 或者 0.99?或者也许我们只是使用了不同的约定?
在 Keras 中实现 Residual Network 时出现错误。下面是给我错误的代码(错误来自函数定义最后一步的第一行): 加载包: import numpy as np from keras
我正在尝试保存模型,然后稍后加载它以进行一些预测;发生的事情是训练后模型的准确率是95%+ ,但是当我保存它然后加载它时,准确度下降到接近 10%在 同 数据集。 要重现此错误结果,您可以运行 thi
我有以下代码。 x = keras.layers.Input(batch_shape = (None, 4096)) hidden = keras.layers.Dense(512, activati
我正在通过时间序列数据训练 LSTM 网络,并希望对数据进行标准化,因为我的特征具有不同的规模。 我的数据形状是 (n_samples x n_timestamps x n_features) 我想使
我目前正在学习在我的项目中使用 Tensorflow-2.0。我想使用卷积神经网络(CNN)完成一个语义分割任务,在编码时发现一个奇怪的错误。 首先,构建了一个简单的模型并且运行良好。 X_train
我的问题是 BatchNormalization (BN) 正在标准化什么。 我问,BN 是单独对每个像素的 channel 进行归一化,还是对所有像素一起归一化。它是在每个图像的基础上还是在整个批处
我的 LSTM 网络有 5 个输入。第一个输入的典型值为 1000 到 3000。其余输入的值为 -1 到 1。 我想插入 BatchNormalization 作为第一层。但输入 2-5 已经在 -
inputs = Input((img_height, img_width, img_ch)) conv1 = Conv2D(n_filters, (k, k), padding=padding)(i
我正在尝试使用 Tensorflow 从 GAN 保存生成器模型。我正在使用的模型有几个批量归一化层。当我保存权重时,只有运行全局变量初始值设定项才能成功恢复它们,但我不必这样做,因为所有变量都正在恢
过了一段时间我又回到了tensorflow,看起来情况已经完全改变了。 但是,之前我曾经在训练循环中使用 tf.contrib....batch_normalization 并进行以下操作: upda
我是一名优秀的程序员,十分优秀!