See the python script of Keras model

mansour_ebrahimmansour_ebrahim Member Posts: 14 Newbie
edited July 12 in Help
I do wonder whether it would be possible to see the python script of Kear's model in Rapidminer? I tried python script operator but it didn't work.


  • MarlaBotMarlaBot The Friendly RapidMiner Dog Bot Administrator, Moderator, Employee, Member Posts: 57  Community Manager
    Hi @mansour_ebrahim - this is MarlaBot. I found these great videos on our RapidMiner Academy that you may find helpful:
    Instructional Video: Python and PyCharm in RapidMiner (Viewing time: ~5m)
    Please LIKE my comment if it helps! 👇

    MarlaBot <3
  • varunm1varunm1 Member Posts: 722   Unicorn
    edited July 15
    Hello @mansour_ebrahim

    Right now, you can load the Keras model created in python into rapidminer but I don't see any option to see the python script of Keras model created in rapidminer.

    I am not sure if anyone working on Keras in RM as the focus is now on the Deep learning extension based on DL4J.

    @pschlunder any comment here?
  • jacobcybulskijacobcybulski Member, University Professor Posts: 83   Unicorn
    edited July 21
    @mansour_ebrahim ; I regularly use Keras in RapidMiner. I think Keras extension is currently more mature than the new Deep Learning. The main problem with Keras extension is that it relies on the external ML stack which includes NVIDIA cuDNN, Python, Tensorflow and Keras. The Deep Learning extension is much simpler to set up. Both can take advantage of a GPU. You can also run Keras or Tensorflow from the Python extension, which I assume is what you are after. Here is a simple example how to do so.

    First have a look at a sample of how to arrange the Python elements within a RapidMiner process, which aims at solving the well-known MNIST problem.

    Note that Load MNIST gets the data, Existing Model macro provides an optional path to the saved model (I think I later replaced this by Generate Data by User Specification), Keras Model creates a model, Keras Evaluate measures the model performance, and Keras History Plot produces a nice chart of the training and validation history. I attach the Python script of the main process and you'll probably figure out how to do the rest.

    Keras Model - Python script
    from __future__ import print_function
    import keras
    from keras.models import Sequential
    from keras.layers import Dense, Dropout, Flatten
    from keras.layers import Conv2D, MaxPooling2D
    from keras.callbacks import TensorBoard
    from keras.utils import plot_model
    from keras.models import load_model
    from datetime import datetime
    import numpy as np
    import pandas as pd
    import json

    def rm_main(train_data, test_data):

    ### Set training defaults, could be parametrised
    batch_size = 128
    epochs = 12

    ### Get data for training and testing
    x_train = train_data['x']
    y_train = train_data['y']
    x_test = test_data['x']
    y_test = test_data['y']
    img_rows = train_data['rows']
    img_cols = train_data['cols']
    input_shape = train_data['shape']
    num_classes = len(y_train[0])

    print('Training shape:', input_shape)
    print(x_train.shape[0], 'training samples')
    print(x_test.shape[0], 'test samples')
    print(num_classes, 'classes')

    ### Create a model
    mpath = '%{mpath}'
    print('Loading model from: '+mpath)
    if not mpath.isspace() and mpath != '':
    # Load existing model
    model = load_model(mpath)
    model_name = mpath
    # Create a new model architecture
    model = Sequential()
    model.add(Conv2D(32, kernel_size=(3, 3),
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dense(128, activation='relu'))
    model.add(Dense(num_classes, activation='softmax'))


    # Train the model and retain epoch evaluation
    history =, y_train,
    validation_data=(x_test, y_test),

    # Save the model, its architecture (in JSON) and its weights
    random_name ="%Y%m%d_%H%M%S")
    model_name = "/tmp/keras_model_"+"%Y%m%d_%H%M%S")+".h5"
    with open("/tmp/keras_model_"+random_name+".json", 'w') as f:
    json.dump(model.to_json(), f, ensure_ascii=False)

    ### Evaluate the trained model

    # Evaluate the model on training data and test data
    train_score = model.evaluate(x_train, y_train, verbose=0)
    valid_score = model.evaluate(x_test, y_test, verbose=0)

    ### Prepare results

    # Return the model location
    model_spec = pd.DataFrame.from_dict({'Keras_Model': [model_name]})

    # Prepare evaluation results
    eval_data = np.array([
    ['', 'acc', 'loss', 'val_acc', 'val_loss'],
    ['', round(train_score[1], 4), round(train_score[0], 4),
    round(valid_score[1], 4), round(valid_score[0], 4)]])

    eval_result = pd.DataFrame(data=eval_data[1:,1:],

    # Prepare evaluation history
    if not mpath.isspace() and mpath != '':
    df_history = eval_result
    df_history = pd.DataFrame.from_dict(history.history)

    return model_spec, df_history, eval_result

    The main difficulty of Python scripting in RapidMiner is getting data in and out. The rest is rather standard.

    Good luck -- Jacob
  • jacobcybulskijacobcybulski Member, University Professor Posts: 83   Unicorn
    Also note that if you wanted to do an honest testing of the model at the end, you'd probably use the "test data" for this and split the "training data" into two parts, i.e. one to train the model and another one to validate it during model fitting.
  • varunm1varunm1 Member Posts: 722   Unicorn
    edited July 21

    Is the script written using python extension or generated from the keras model built using RM keras extension?

    If its the latter, please inform how you did it? This will be helpful.

    Update: Sorry, for some reason I was not able to see the image earlier, Now I got it. You were using python scripting operators in RM. I assumed that you generated script after building a model using Keras extension (Keras_Extension)

  • jacobcybulskijacobcybulski Member, University Professor Posts: 83   Unicorn
    Indeed, this Python code was all crafted by hand. There is probably one more small detail worth mentioning - the Keras model is passed from Keras Model to Keras Evaluate by passing the path to the model saved on disk. At the time this seemed like the simplest solution, however, it should be possible to pass the model weights or a serialized model via RM ports.
  • jacobcybulskijacobcybulski Member, University Professor Posts: 83   Unicorn
    @varunm1 I can see now that perhaps @mansour_ebrahim asked about the script of the Keras model as it is generated from Keras Extension in RapidMiner. No, I am not aware how to retrieve it from the Keras Model operator. However, the "lay" port returns the text which looks alike Python code - perhaps it could be adapted for use in Python.
  • sgenzersgenzer 12Administrator, Moderator, Employee, RapidMiner Certified Analyst, Community Manager, Member, University Professor, PM Moderator Posts: 2,434  Community Manager
    @jacobcybulski I've been looking for a nice example of a solution to NMIST. Can I post this on the community repo?
  • varunm1varunm1 Member Posts: 722   Unicorn
    Hello @jacobcybulski

    If possible can you do this "it should be possible to pass the model weights or a serialized model via RM ports." and provide complete XML code or .rmp file to Scott so that he will place this in community samples and many users can refer it a for keras application with python scripting in RM.

    Thank you.
  • jacobcybulskijacobcybulski Member, University Professor Posts: 83   Unicorn
    @sgenzer I have previously posted a MNIST example using Keras here on the forum and it later appeared in the new edition of the RapidMiner related book Data Science: Concept and Practice by Kotu and Deshpande (with acknowledgement of course). I am happy to show an improved version of this one. I will also try reworking this example to use Deep Learning extension, however, I cannot see the way to "reshape" data to use convolutional layer - perhaps some clever way of using the DL tensors would work. Both examples require some Python to read images in. 
  • jacobcybulskijacobcybulski Member, University Professor Posts: 83   Unicorn
    @varunm1 @sgenzer just a clarification, you'd want to see specifically a Keras example of serialising a model or a generic Python example of passing a model between two Python operators? 
  • varunm1varunm1 Member Posts: 722   Unicorn

    I cannot see the way to "reshape" data to use convolutional layer 

    This is the one that bugged me with the new deep learning extension for CNN and I requested @pschlunder support to look into this in earlier threads, he might be working on it (not sure). Thread here

     you'd want to see specifically a Keras example of serializing a model or a generic Python example of passing a model between two Python operators?

    For the current process, my interest would be related to keras example with serializing a model as I see many users asking questions related to keras, even though the questions are related to keras model extension, as it is not maintained currently we can refer to the example (from you) with python scripting if they particularly want to use keras library, instead of DL4J (new Deep learning extension). But anyway @sgenzer might have other ideas for the community samples.


Sign In or Register to comment.