背如果Tensorflow是你的首选框架,并且你想找一个简化的、高层的模型定义接口来让自己活的
目录
背景介绍
如果选择Tensorflow作为AI框架,并且想找一个简化的、高层的模型定义接口来让深度学习的工
作变得很便捷,Keras API可以提供很大的帮助 链接
本文旨在介绍如何通过Keras API 获得Keras 模型(Ex:比如预定义的ResNet50 或者自定义的其它模型)的每一层前向输出(Forward Outputs)和每一层的后向梯度(Backward Gradients)
示例代码
from tensorflow.python.keras import backend as K
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.datasets import cifar100
import tensorflow as tf
import tensorflow.keras.layers as layers
import numpy as np,random,os,sys,pickle
#配置Keras运行在Traing Mode
#(Keras模型在Training Mode/Inference Mode有不同行为,比如tf.keras.layers.BatchNormalization)
K.set_learning_phase(1)
#准备训练和测试数据
(x_train, y_train), (x_test, y_test) = cifar100.load_data(label_mode='fine')
x_train, x_test = x_train / 255, x_test / 255 # normalize data
#创建Keras预定义的ResNet50模型
model = ResNet50(input_shape=x_train.shape[1:] include_top=False)
#compile 模型
model.compile(
optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
#显示模型的summary信息
model.summary()
#保存模型的tensorboard的log信息
log_dir = 'logs_resnet_' + datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
#保存模型的计算图到png图片
tf.keras.utils.plot_model(model, to_file="model.png", show_shapes=False)
#开始训练模型并且保存运行信息到tensorboard log
model.fit(
x = x_train,
y = y_train,
epochs = 1,
validation_data = (x_test, y_test)),
callbacks = [tensorboard_callback]
)
#创建前向每一层的output
layer_outputs = [layer.output for layer in model.layers[1:]] #跳过第一层input
f_fwd = K.function(model.inputs, layer_outputs) #通过Keras K.function 计算output
layers_data = f_fwd(x_test)
#保存 outputs到文件
results = []
for i, item in enumerate(layers_data):
results.append((layer_outputs[i].name, layers_data[i]))
with open('fwd.outputs', 'wb') as f:
pickle.dump(results,f)
#创建反向每一层的Gradients
target = model.outputs
grad_target = []
for layer in model.layers[1:]:
if not isinstance(layer.input,list):
grad_target.append(layer.input)
else:
grad_target += layer.input
grads = K.gradients(target, grad_target) #通过Keras K.gradients 计算模型output对于每一层input的梯度
#计算gradients
f = K.function(model.inputs, grads) #通过Keras K.function 计算梯度
layer_grads = f(x_test)
#保存gradients
results = []
for i, item in enumerate(layer_grads):
results.append((grads[i].name, layer_grads[i]))
with open('bwd.grads', 'wb') as f:
pickle.dump(results,f)
如上面的代码所示,关键的Keras API 都加上了注释,包括
Keras backend 链接
K.set_learning_phase
K.function
K.gradients