1 回答

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
好的,我確實(shí)找到了解決方案,也許不是最好的解決方案。本質(zhì)上只是創(chuàng)建了一個(gè)使用 TensorFlow 的 GradientTape 的新方法。本質(zhì)上,獲取預(yù)測(cè)、生成目標(biāo)、計(jì)算損失然后更新梯度。
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 子類化有更好的方法來(lái)做到這一點(diǎn),但這確實(shí)起到了作用。
添加回答
舉報(bào)