1 回答

TA貢獻1865條經驗 獲得超7個贊
好的,我確實找到了解決方案,也許不是最好的解決方案。本質上只是創建了一個使用 TensorFlow 的 GradientTape 的新方法。本質上,獲取預測、生成目標、計算損失然后更新梯度。
with tf.GradientTape() as tape:
# Forward pass
y_preds = self.model(x, training=True)
# Generate the target values from the predictions
actual_deltas, actual_objectness = self.generate_target_values(y_preds, labels)
#Get the loss
loss = self.model.compiled_loss([actual_deltas, actual_objectness], y_preds, regularization_losses=self.model.losses)
# Compute gradients
trainable_vars = self.model.trainable_variables
gradients = tape.gradient(loss, trainable_vars)
# Update weights
self.model.optimizer.apply_gradients(zip(gradients, trainable_vars))
我知道使用 Keras 子類化有更好的方法來做到這一點,但這確實起到了作用。
添加回答
舉報