1 回答

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊
我目前正面臨同樣的問(wèn)題,并使用Tensorboard以類似的方式處理了這個(gè)問(wèn)題。
即使不推薦使用,您仍然可以通過(guò)子類化并在方法中手動(dòng)計(jì)算梯度來(lái)管理網(wǎng)絡(luò)每一層的梯度。write_gradstf.keras.Modelgradient.Tapetrain_step
與此類似的東西對(duì)我有用
from tensorflow.keras import Model
class TrainWithCustomLogsModel(Model):
def __init__(self, **kwargs):
super(TrainWithCustomLogsModel, self).__init__(**kwargs)
self.step = tf.Variable(0, dtype=tf.int64,trainable=False)
def train_step(self, data):
# Get batch images and labels
x, y = data
# Compute the batch loss
with tf.GradientTape() as tape:
p = self(x , training = True)
loss = self.compiled_loss(y, p, regularization_losses=self.losses)
# Compute gradients for each weight of the network. Note trainable_vars and gradients are list of tensors
trainable_vars = self.trainable_variables
gradients = tape.gradient(loss, trainable_vars)
# Log gradients in Tensorboard
self.step.assign_add(tf.constant(1, dtype=tf.int64))
#tf.print(self.step)
with train_summary_writer.as_default():
for var, grad in zip(trainable_vars, gradients):
name = var.name
var, grad = tf.squeeze(var), tf.squeeze(grad)
tf.summary.histogram(name, var, step = self.step)
tf.summary.histogram('Gradients_'+name, grad, step = self.step)
# Update model's weights
self.optimizer.apply_gradients(zip(gradients, trainable_vars))
del tape
# Update metrics (includes the metric that tracks the loss)
self.compiled_metrics.update_state(y, p)
# Return a dict mapping metric names to current value
return {m.name: m.result() for m in self.metrics}
然后,您應(yīng)該能夠可視化訓(xùn)練中任何訓(xùn)練步驟的梯度分布,以及核值的分布。
此外,可能值得嘗試?yán)L制規(guī)范隨時(shí)間分布圖,而不是單個(gè)值。
添加回答
舉報(bào)