如何将1.13.1转换为2.0.0-alpha0?
摘自Francois Chollet所著的“用Python进行深度学习”,第172-176页。
源自论文:“ Grad-CAM:深度网络通过基于梯度的本地化的可视化解释”,Ramprassarth R. Selvaraju等。等arXiv(2017,https://arxiv.org/abs/1610.02391
我正在尝试将K.gradient(〜)[0]调用转换为tensorflow == 2.0.0-alpha0
我尝试过:
grads = tf.GradientTape(aftrican_elephant_output, last_conv_layer.output)
print(grads)
似乎不正确(注意:否[0],因为GradientTape无法下标。
如何将1.13.1转换为2.0.0-alpha0?
摘自:Francois Chollet,“用Python进行深度学习”,第172-176页。
来自:“ Grad-CAM:深度网络通过基于梯度的本地化的视觉说明”,Ramprassarth R. Selvaraju等。等arXiv(2017,https://arxiv.org/abs/1610.02391
import tensorflow as tf
print(tf.__version__)
print(tf.test.is_gpu_available())
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras import backend as K
model = VGG16(weights='imagenet')
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions
import numpy as np
img_path = '.\\creative_commons_elephant.jpg' # input image (any will work)
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=3)[0])
aftrican_elephant_output = model.output[:, 386]
last_conv_layer = model.get_layer('block5_conv3')
grads = K.gradients(aftrican_elephant_output, last_conv_layer.output)[0]
RuntimeError跟踪(最近的调用) 最后) ----> 1个等级= K.gradients(aftrican_elephant_output,last_conv_layer.output)[0]
〜\ AppData \ Roaming \ Python \ Python37 \ site-packages \ tensorflow \ python \ keras \ backend.py 渐变(损失,变量)3265“”“ 3266返回 gradients_module.gradients( -> 3267损失,变量,colocate_gradients_with_ops = True)3268 3269
〜\ AppData \ Roaming \ Python \ Python37 \ site-packages \ tensorflow \ python \ ops \ gradients_impl.py 以渐变(ys,xs,grad_ys,名称,colocate_gradients_with_ops, gate_gradients,aggregation_method,stop_gradients, unconnected_gradients) 156 ys,xs,grad_ys,name,colocate_gradients_with_ops, 157 gate_gradients,aggregation_method,stop_gradients, -> 158个unconnected_gradients) 159#pylint:enable =受保护的访问 160
〜\ AppData \ Roaming \ Python \ Python37 \ site-packages \ tensorflow \ python \ ops \ gradients_util.py 在_GradientsHelper(ys,xs,grad_ys,name, colocate_gradients_with_ops,gate_gradients,aggregation_method, stop_gradients,unconnected_gradients,src_graph) 556“”“ gradients()的实现。”“” 第557章,你是我的老公 -> 558提高RuntimeError(“急于执行时不支持tf.gradients” 559“启用。请改用tf.GradientTape。”) 560,如果src_graph为None:
RuntimeError:急于执行时不支持tf.gradients 已启用。改用tf.GradientTape。
我正在尝试将K.gradient()[0]调用转换为tensorflow == 2.0.0-alpha0