如何计算给定小批量的每层的最大梯度
我尝试使用 MNIST 数据集实现一个全连接的分类模型。部分代码如下:
n = 5
act_func = 'relu'
classifier = tf.keras.models.Sequential()
classifier.add(layers.Flatten(input_shape = (28, 28, 1)))
for i in range(n):
classifier.add(layers.Dense(32, activation=act_func))
classifier.add(layers.Dense(10, activation='softmax'))
opt = tf.keras.optimizers.SGD(learning_rate=0.01)
classifier.compile(optimizer=opt,loss="categorical_crossentropy",metrics ="accuracy")
classifier.summary()
history = classifier.fit(x_train, y_train, batch_size=32, epochs=3, validation_data=(x_test,y_test))
有没有办法打印给定小批量的每层的最大梯度?
I try to implement a fully-connected model for classification using the MNIST dataset. A part of the code is the following:
n = 5
act_func = 'relu'
classifier = tf.keras.models.Sequential()
classifier.add(layers.Flatten(input_shape = (28, 28, 1)))
for i in range(n):
classifier.add(layers.Dense(32, activation=act_func))
classifier.add(layers.Dense(10, activation='softmax'))
opt = tf.keras.optimizers.SGD(learning_rate=0.01)
classifier.compile(optimizer=opt,loss="categorical_crossentropy",metrics ="accuracy")
classifier.summary()
history = classifier.fit(x_train, y_train, batch_size=32, epochs=3, validation_data=(x_test,y_test))
Is there a way to print the maximum gradient for each layer for a given mini-batch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 tf.GradientTape 开始自定义训练循环:
You could start off with a custom training loop using
tf.GradientTape
:您可以编写一个自定义循环,而不是调用
compile
和fit
,然后您可以循环遍历各层并检查
Instead of invoking
compile
andfit
you may write a custom loopYou can then loop over the layers and inspect