我想在Keras训练时获得关于图层权重和图层输出的渐变。高度赞赏,如果有人知道怎么做。
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
np.set_printoptions(suppress=True)
model = ResNet50(weights='imagenet')
img_path = 'images/elephant.jpg'
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)
y = np.zeros((1, 1000))
y[0, 386] = 1
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'] )
model.train_on_batch(x, y)