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.
One solution is to save the scaler's parameters to a JSON file in Python and then load those parameters in Node.js.
In Node.js you load the JSON file and reconstruct the scaler with the saved parameters: