如何在Tensorflow 2.0中累积梯度?

时间:2020-01-24 09:44:32

标签: python tensorflow tensorflow2.0

我正在使用tensorflow 2.0训练模型。我的训练集中的图像具有不同的分辨率。我建立的模型可以处理可变分辨率(转换层,然后进行全局平均)。我的训练集很小,我想在一个批次中使用完整的训练集。

由于我的图像分辨率不同,因此无法使用model.fit()。因此,我计划将每个样本分别通过网络传递,累积误差/梯度,然后应用一个优化程序步骤。我可以计算损失值,但是我不知道如何累计损失/梯度。如何累积损失/梯度,然后应用单个优化程序步骤?

代码

for i in range(num_epochs):
    print(f'Epoch: {i + 1}')
    total_loss = 0
    for j in tqdm(range(num_samples)):
        sample = samples[j]
        with tf.GradientTape as tape:
            prediction = self.model(sample)
            loss_value = self.loss_function(y_true=labels[j], y_pred=prediction)
        gradients = tape.gradient(loss_value, self.model.trainable_variables)
        self.optimizer.apply_gradients(zip(gradients, self.model.trainable_variables))
        total_loss += loss_value

    epoch_loss = total_loss / num_samples
    print(f'Epoch loss: {epoch_loss}')

2 个答案:

答案 0 :(得分:4)

Stack Overflow AnswerTensorflow Website中提供的解释一致,下面提到的是Tensorflow 2.0版中的累积梯度代码:

def train(epochs):
  for epoch in range(epochs):
    for (batch, (images, labels)) in enumerate(dataset):
       with tf.GradientTape() as tape:
        logits = mnist_model(images, training=True)
        tvs = mnist_model.trainable_variables
        accum_vars = [tf.Variable(tf.zeros_like(tv.initialized_value()), trainable=False) for tv in tvs]
        zero_ops = [tv.assign(tf.zeros_like(tv)) for tv in accum_vars]
        loss_value = loss_object(labels, logits)

       loss_history.append(loss_value.numpy().mean())
       grads = tape.gradient(loss_value, tvs)
       #print(grads[0].shape)
       #print(accum_vars[0].shape)
       accum_ops = [accum_vars[i].assign_add(grad) for i, grad in enumerate(grads)]



    optimizer.apply_gradients(zip(grads, mnist_model.trainable_variables))
    print ('Epoch {} finished'.format(epoch))

# Call the above function    
train(epochs = 3)

完整代码可在此Github Gist中找到。

答案 1 :(得分:3)

如果我从此陈述中正确理解:

如何累积损耗/梯度,然后应用单个优化程序步骤?

@Nagabhushan尝试累积梯度,然后将优化应用于(平均)累积梯度。 @TensorflowSupport提供的答案无法回答。 为了仅执行一次优化并从多个磁带中累积梯度,可以执行以下操作:

for i in range(num_epochs):
    print(f'Epoch: {i + 1}')
    total_loss = 0

    # get trainable variables
    train_vars = self.model.trainable_variables
    # Create empty gradient list (not a tf.Variable list)
    accum_gradient = [tf.zeros_like(this_var) for this_var in train_vars]

    for j in tqdm(range(num_samples)):
        sample = samples[j]
        with tf.GradientTape as tape:
            prediction = self.model(sample)
            loss_value = self.loss_function(y_true=labels[j], y_pred=prediction)
        total_loss += loss_value

        # get gradients of this tape
        gradients = tape.gradient(loss_value, train_vars)
        # Accumulate the gradients
        accum_gradient = [(acum_grad+grad) for acum_grad, grad in zip(accum_gradient, gradients)]


    # Now, after executing all the tapes you needed, we apply the optimization step
    # (but first we take the average of the gradients)
    accum_gradient = [this_grad/num_samples for this_grad in accum_gradient]
    # apply optimization step
    self.optimizer.apply_gradients(zip(accum_gradient,train_vars))
        

    epoch_loss = total_loss / num_samples
    print(f'Epoch loss: {epoch_loss}')

应避免在训练循环中使用tf.Variable(),因为在尝试将代码作为图形执行时会产生错误。如果您在训练函数中使用tf.Variable(),然后用“ @ tf.function”装饰它或应用“ tf.function(my_train_fcn)”以获得图形函数(即为了提高性能),则执行将增加错误。 发生这种情况是因为对tf.Variable函数的跟踪导致的行为与渴望执行(分别为重新利用或创建)时所观察到的行为不同。您可以在tensorflow help page中找到更多信息。