在Keras中如何在训练时获得关于图层权重和图层输出的梯度

时间:2019-10-19 03:28:15

标签: python tensorflow keras

我想在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)

0 个答案:

没有答案
相关问题