Data and AI on Power

Data and AI on Power

IBM Power systems provide a robust and scalable platform for a wide range of data and AI workloads, offering benefits in performance, security, and ease of use.

 View Only

How to build an AI model to take advantage of MMA on IBM Power

By Theresa Xu posted Tue August 30, 2022 01:38 PM

  

The purpose of this blog is to show you how to build an AI model that exploits the benefits of Matrix Math Acceleration (MMA) on an IBM Power10 system. An AI model is a tool or algorithm, which is based on a certain data set through which it can arrive at a decision while MMAs provide an alternative to external accelerators, such as GPUs, for executing statistical machine learning and inferencing (scoring) workloads. In this blog, we’ll show you how to create a LSTM Model which can be deployed using an in-house Keras H5 or ONNX format. 

Prerequisites

Estimated time to complete

This depends on the size of the data set, the type and shape of the model, as well as the number of epochs and steps per epoch. 

Steps

Follow these steps to build your AI model.

  1. Activate a conda environment of your choice, and layout the Python code and data set in this environment. An example of a file that creates an AI model can be found at this link: https://github.com/ppc64le/gists-ai-inferencing/blob/main/lstm-credit-card-fraud/ccf_220_keras_lstm_static-OS.py

Before doing the model build, it is often necessary convert the columns that have categorical data into numerical data. In our example, this is done by creating a DataFrameMapper from sklearn_pandas which takes all the non-numerical columns and uses encoders to convert them to numerical data. See lines 86-98 of the example file.

  1. Our  LSTM model has three layers that consist of a recurrent neural network (RNN) with two layers of LSTM with 200 units in each layer, followed by a dense layer. There is one output, which is Fraud/Non-fraud as shown below:
  2. You can now compile, train, and fit the model to increase its accuracy. See lines 198-206 of the example file for reference. The number of epochs and the number of steps per epoch are tunable. Having a higher number of each takes a longer time for the model to build but increases accuracy, so there is a trade-off. Through trial and error, we found 20 epochs and 10,000 steps per epoch gave us a 98% accurate model and it only took 47 hours to build
  3. Finally, you can save the model in either Keras H5 or ONNX formats:

    To save it to an H5 format, take the model’s name from the code and simply save it by writing the following:

    new_model.save(‘model.h5’)

    To save the model to ONNX format:

    from tensorflow import keras
    
    import keras2onnx 
    
    import onnx  
    
    # convert to onnx 
    
    onnx_model = keras2onnx.convert_keras(new_model, new_model.name)  
    
    # Save 
    
    onnx.save_model(onnx_model, "ccf_lstm_static_keras2onnx.onnx")
    
    import tf2onnx  
    
    # convert to onnx 
    
    spec = (tf.TensorSpec((7, 16, 220), tf.float32, name="input"),) 
    
    output_path = new_model.name + ".onnx"  
    
    onnx_model = tf2onnx.convert.from_keras(new_model, spec, output_path='')

    Summary

    You can now build an AI model from scratch using a provided dataset and convert that model to both Keras H5 and ONNX formats. The process and deployment of different AI models varies; for example, a Gated Recurrent Unit (GRU) or logistic regression model is created in a different way than a LSTM, but the process of saving the model into a h5 or ONNX format remains the same. Next, you may want to try AI inferencing on DB2, which you can read about in this blog: How to create and test MMA accelerated UDF under DB2 (coming soon).

    1 comment
    151 views

    Permalink

    Comments

    Tue February 13, 2024 03:14 PM