FreudianSip
FreudianSip

Reputation: 21

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

Is the activation function for each layer stored in the .h5 file produced by model.save()? Or is it already "baked in" to the weights?

I am writing an AWS Lambda function to generate time-series predictions from multiple regression models every five minutes. Unfortunately, TensorFlow is too large of a library to be loaded into an AWS Lambda function, so I am writing my own Python code to load the saved .h5 model file and generate predictions based on the weights and input data. Here's where I'm at so far:

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

I understand I'll probably want some kind of case/switch statement to handle the various activation functions.

Upvotes: 0

Views: 1133

Answers (2)

Zabir Al Nazi Nabil
Zabir Al Nazi Nabil

Reputation: 11218

If you save the full model with model.save, you can access each layer and it's activation function.

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>

Here, for example, softmax is used in the last layer.

If you don't want to import tensorflow, you can also read from 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

Here, last layer is a dense layer which has softmax activation.

Upvotes: 1

FreudianSip
FreudianSip

Reputation: 21

The model configuration is accessible through an attribute called "model_config" on the top group that seems to contain the full model configuration JSON that is produced by model.to_json().

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

Upvotes: 2

Related Questions