如何在 MNIST 的 Keras 中观察梯度

问题描述

我想绘制每个时期的梯度并尝试使用梯度磁带获取值。但我收到以下错误

batch_size = 128
epochs = 15
model.compile(loss="categorical_crossentropy",optimizer="adam",metrics=["accuracy"])
history = model.fit(x_train,y_train,batch_size=batch_size,epochs=epochs,validation_split=0.1)
loss_val = history.history['loss']
with tf.GradientTape() as tape:
    # Forward pass.
    tape.watch(model.trainable_weights)
    grads = tape.gradient(loss_val,model.trainable_weights)
"""
## Evaluate the trained model
"""
score = model.evaluate(x_test,y_test,verbose=0)
print("Test loss:",score[0])
print("Test accuracy:",score[1])

我收到以下错误

AttributeError                            Traceback (most recent call last)
<ipython-input-7-f04339bdcbcf> in <module>
     12     # Forward pass.
     13     tape.watch(model.trainable_weights)
---> 14     grads = tape.gradient(loss_val,model.trainable_weights)
     15 """
     16 ## Evaluate the trained model

~/.conda/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/backprop.py in gradient(self,target,sources,output_gradients,unconnected_gradients)
    983     flat_targets = []
    984     for t in nest.flatten(target):
--> 985       if not t.dtype.is_floating:
    986         logging.vlog(
    987             logging.WARN,"The dtype of the target tensor must be "

AttributeError: 'numpy.dtype' object has no attribute 'is_floating'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)