Save keras model and StandartScaller to TensorflowJS

56 views Asked by At

I have the following model that I want to save in tensorflowjs format for later use in nodejs.

X = df.drop(columns=['Age'])
y = df['Age']

X_train, X_test, y_train, y_test = train_test_split(X, y,
                                                    test_size=0.1,
                                                    random_state=42)

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

model_00 = keras.Sequential([
    layers.Dense(20, input_shape=(X_train_scaled.shape[1],)),
    layers.Activation('selu'),
    layers.Dropout(0.1),
    layers.Dense(40),
    layers.Activation('selu'),
    layers.Dropout(0.2),
    layers.Dense(40),
    layers.Activation('selu'),
    layers.Dropout(0.2),
    layers.Dense(40),
    layers.Activation('selu'),
    layers.Dropout(0.1),
    layers.Dense(20),
    layers.Activation('selu'),
    layers.Dense(10),
    layers.Activation('selu'),
    layers.Dense(1),
])

optimizer = optimizers.Adagrad(learning_rate=0.01)

model_00._name = "BA_model_male_00"

model_00.compile(loss='mean_squared_error',
                 optimizer=optimizer,
                 metrics=[metrics.MeanSquaredError(),
                          metrics.MeanAbsoluteError()])

history = model_00.fit(X_train_scaled, y_train,
                       epochs=500,
                       batch_size=200,
                       validation_data=(X_test_scaled, y_test),
                       verbose=0)

prediction = model_00.predict(X_test_scaled)

Saving the model is not difficult, like that:

tfjs.converters.save_keras_model(model, tfjs_target_dir)

But I also have to save the scaler and I don't know how to do it.

1

There are 1 answers

0
Guapi-zh On BEST ANSWER

One solution is to save the scaler's parameters to a JSON file in Python and then load those parameters in Node.js.

import json

# Save the scaler's parameters to a JSON file
scaler_params = {
    'mean': scaler.mean_.tolist(),
    'scale': scaler.scale_.tolist()
}

with open('scaler_params.json', 'w') as json_file:
    json.dump(scaler_params, json_file)

In Node.js you load the JSON file and reconstruct the scaler with the saved parameters:

const tf = require('@tensorflow/tfjs-node');
const fs = require('fs');

// Load the JSON file containing the scaler parameters
const scalerParamsJson = fs.readFileSync('scaler_params.json', 'utf8');
const scalerParams = JSON.parse(scalerParamsJson);


// Create tensors from your data
const dataTensor = tf.tensor(newDataArray);

// Apply manual normalization using the loaded mean and scale
const normalizedData = dataTensor.sub(scalerParams.mean).div(scalerParams.scale);