标签: gradienttape

在 Tensorflow 2.0 的自定义训练循环中应用回调

我正在使用 Tensorflow DCGAN 实施指南中提供的代码编写自定义训练循环。我想在训练循环中添加回调。在 Keras 中,我知道我们将它们作为参数传递给 'fit' 方法,但找不到有关如何在自定义训练循环中使用这些回调的资源。我正在从 Tensorflow 文档中添加自定义训练循环的代码:

# Notice the use of `tf.function`
# This annotation causes the function to be "compiled".
@tf.function
def train_step(images):
    noise = tf.random.normal([BATCH_SIZE, noise_dim])

    with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
      generated_images = generator(noise, training=True)

      real_output = discriminator(images, training=True)
      fake_output = discriminator(generated_images, training=True)

      gen_loss = generator_loss(fake_output)
      disc_loss = discriminator_loss(real_output, fake_output)

    gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)
    gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)

    generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))
    discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))

def train(dataset, epochs):
  for epoch in range(epochs):
    start = time.time() …
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow tensorflow2.0 gradienttape

11
推荐指数
3
解决办法
3621
查看次数

使用 tf.function 时获取梯度

我对以下示例中观察到的行为感到困惑:

import tensorflow as tf

@tf.function
def f(a):
    c = a * 2
    b = tf.reduce_sum(c ** 2 + 2 * c)
    return b, c

def fplain(a):
    c = a * 2
    b = tf.reduce_sum(c ** 2 + 2 * c)
    return b, c


a = tf.Variable([[0., 1.], [1., 0.]])

with tf.GradientTape() as tape:
    b, c = f(a)
    
print('tf.function gradient: ', tape.gradient([b], [c]))

# outputs: tf.function gradient:  [None]

with tf.GradientTape() as tape:
    b, c = fplain(a)
    
print('plain gradient: ', tape.gradient([b], …
Run Code Online (Sandbox Code Playgroud)

python decorator tensorflow2.0 gradienttape

7
推荐指数
1
解决办法
1077
查看次数

如何将 Tensorflow BatchNormalization 与 GradientTape 结合使用?

假设我们有一个使用 BatchNormalization 的简单 Keras 模型:

model = tf.keras.Sequential([
                     tf.keras.layers.InputLayer(input_shape=(1,)),
                     tf.keras.layers.BatchNormalization()
])
Run Code Online (Sandbox Code Playgroud)

如何实际使用 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))
Run Code Online (Sandbox Code Playgroud)

特别是,如果您检查移动平均值,它们将保持不变(检查 model.variables,平均值始终为 0 和 1)。我知道可以使用 .fit() 和 .predict(),但我想使用 GradientTape 并且我不知道如何执行此操作。某些版本的文档建议更新 update_ops,但这似乎在急切模式下不起作用。

特别是,经过上述训练后,以下代码将不会输出任何接近 150 的结果。

x = np.random.randint(200, 210, 100).astype(np.float32)
print(model(np.expand_dims(x, axis=1)))
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow batch-normalization gradienttape

5
推荐指数
1
解决办法
1488
查看次数

张量流 v2 梯度未显示在张量板直方图上

我有一个简单的神经网络,我尝试使用张量板通过使用回调来绘制梯度,如下所示:

class GradientCallback(tf.keras.callbacks.Callback):
    console = False
    count = 0
    run_count = 0

    def on_epoch_end(self, epoch, logs=None):
        weights = [w for w in self.model.trainable_weights if 'dense' in w.name and 'bias' in w.name]
        self.run_count += 1
        run_dir = logdir+"/gradients/run-" + str(self.run_count)
        with tf.summary.create_file_writer(run_dir).as_default(),tf.GradientTape() as g:
          # use test data to calculate the gradients
          _x_batch = test_images_scaled_reshaped[:100]
          _y_batch = test_labels_enc[:100]
          g.watch(_x_batch)
          _y_pred = self.model(_x_batch)  # forward-propagation
          per_sample_losses = tf.keras.losses.categorical_crossentropy(_y_batch, _y_pred) 
          average_loss = tf.reduce_mean(per_sample_losses) # Compute the loss value
          gradients = g.gradient(average_loss, self.model.weights) # …
Run Code Online (Sandbox Code Playgroud)

tensorboard tensorflow2.0 gradienttape

5
推荐指数
1
解决办法
1342
查看次数

TF2 - GradientTape 与 Model.fit() - 为什么 GradientTape 不起作用?

晚上好,

我想使用 tf2 和 Gradient Tape 函数实现一个简单回归问题的玩具示例。使用 Model.fit 它可以正确学习,但使用 GradientTape 也可以做一些事情,但与 model.fit() 相比,损失不会移动。这是我的示例代码和结果。我找不到问题所在。

model_opt = tf.keras.optimizers.Adam() 
loss_fn = tf.keras.losses.MeanSquaredError()
with tf.GradientTape() as tape:
    y = model(X, training=True)
    loss_value = loss_fn(y_true, y)
grads = tape.gradient(loss_value, model.trainable_variables)
model_opt.apply_gradients(zip(grads, model.trainable_variables))

#Results:
42.47433806265809
42.63973672226078
36.687397360178586
38.744844324717526
36.59080452300609
...
Run Code Online (Sandbox Code Playgroud)

这里是 model.fit() 的常规情况

model.compile(optimizer=tf.keras.optimizers.Adam(),loss=tf.keras.losses.MSE,metrics="mse")
...
model.fit(X,y_true,verbose=0)
#Results
[40.97759069299212]
[28.04145720307729]
[17.643483147375473]
[7.575242056454791]
[5.83682193867299]
Run Code Online (Sandbox Code Playgroud)

准确率应该大致相同,但看起来它根本没有学习。输入 X 是张量,y_true 也是。

编辑以进行测试

import pathlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf …
Run Code Online (Sandbox Code Playgroud)

machine-learning deep-learning tensorflow tensorflow2.0 gradienttape

2
推荐指数
1
解决办法
2614
查看次数