如何在不导入 tensorflow 的情况下从保存的模型 .h5 访问激活函数?

How to Access Activation Functions from Saved Model .h5 without importing tensorflow?

每一层的激活函数是否存储在model.save()生成的.h5文件中?还是已经 "baked in" 到权重了?

我正在编写一个 AWS Lambda 函数,以每五分钟从多个回归模型生成时间序列预测。不幸的是,TensorFlow 是一个太大的库,无法加载到 AWS Lambda 函数中,因此我正在编写自己的 Python 代码来加载保存的 .h5 模型文件并根据权重和输入数据生成预测。这是我目前所在的位置:

def generate_predictions(model_path, df):
    model_info = h5py.File(model_path, 'r')
    model_weights = model_info['model_weights']
    # Initialize predictions matrix with preprocessed inputs
    predictions = preprocessing.scale(df[inputs])
    layer_list = list(model_weights.keys())
    for layer in layer_list:
        weights = model_weights[layer][layer]['kernel:0'][:]
        bias = model_weights[layer][layer]['bias:0'][:]
        predictions = predictions.dot(weights)
        predictions += bias
        # How to retrieve activation function for layer?
        # predictions = activation_function(predictions)

    return predictions

我知道我可能需要某种 case/switch 语句来处理各种激活函数。

如果使用 model.save 保存完整模型,则可以访问每个层及其激活函数。

from tensorflow.keras.models import load_model
model = load_model('model.h5')

for l in model.layers:
  try:
    print(l.activation)
  except: # some layers don't have any activation
    pass
<function tanh at 0x7fa513b4a8c8>
<function softmax at 0x7fa513b4a510>

这里以最后一层使用softmax为例

如果不想导入tensorflow,也可以从h5py读取。

import h5py
import json

model_info = h5py.File('model.h5', 'r')

model_config = json.loads(model_info.attrs.get('model_config').decode('utf-8'))

for k in model_config['config']['layers']:
  if 'activation' in k['config']:
      print(f"{k['class_name']}: {k['config']['activation']}")
LSTM: tanh
Dense: softmax

在这里,最后一层是具有 softmax 激活的密集层。

模型配置可通过顶部组中名为 "model_config" 的属性访问,该属性似乎包含由 model.to_json() 生成的完整模型配置 JSON。

import json
import h5py
model_info = h5py.File('model.h5', 'r')
model_config_json = json.loads(model_info.attrs['model_config'])