The Altair Community is migrating to a new platform to provide a better experience for you. In preparation for the migration, the Altair Community is on read-only mode from October 28 - November 6, 2024. Technical support via cases will continue to work as is. For any urgent requests from Students/Faculty members, please submit the form linked here

keras sequential model

koichikoichi Member Posts: 18 Learner I
I'm trying to excute the following code by using Execute Python. I'd like to use the model tarined by this code in prediction phase. 
import numpy as np
import pandas as pd
from tensorflow import keras
from tensorflow.keras import layers

def rm_main(data):

print(data.columns)
x_train = (data.drop(["Window id"],axis=1).values)[:, :, np.newaxis]
print(x_train.shape)



    model = keras.Sequential(
        [
            layers.Input(shape=(x_train.shape[1], x_train.shape[2])),
            layers.Conv1D(
                filters=32, kernel_size=7, padding="same", strides=2, activation="relu"
            ),
            layers.Dropout(rate=0.2),
            layers.Conv1D(
                filters=16, kernel_size=7, padding="same", strides=2, activation="relu"
            ),
            layers.Conv1DTranspose(
                filters=16, kernel_size=7, padding="same", strides=2, activation="relu"
            ),
            layers.Dropout(rate=0.2),
            layers.Conv1DTranspose(
                filters=32, kernel_size=7, padding="same", strides=2, activation="relu"
            ),
            layers.Conv1DTranspose(filters=1, kernel_size=7, padding="same"),
        ]
    )
    model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss="mse")
    model.summary()
    
    history = model.fit(
    x_train,
    x_train,
    epochs=50,
    batch_size=128,
    validation_split=0.1,
    callbacks=[
        keras.callbacks.EarlyStopping(monitor="val_loss", patience=5, mode="min")
        ],
    )
    
    return model

But, I got the following error. 

TypeError: can't pickle weakref objects

It's because keras sequential model is weakref object. 

And I found this thead. 

https://community.rapidminer.com/discussion/55837/see-the-python-script-of-keras-model

In this thead, I found the following post.

jacobcybulski Posts: 376   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.

I can successfully path the model by storing on disk. But, I'm wondering if there are another solution to pass Keras Model to prediction phaase without saving model on disk.





Sign In or Register to comment.