When using the python interface, if we serialize and then deserialize a RandomForest object, we will get a segfault if we try to call the apply_regression method from the deserialized object. See the code below for an example.
#!/usr/bin/env python
# coding: utf-8
import shogun as sg
import numpy as np
# Create random features
X_train = np.random.normal(0, 1, (100, 5))
betas = np.random.normal(0,1, 5)
y_train = np.dot(X_train, betas)
X_test = np.random.normal(0, 1, (10, 5))
y_test = np.dot(X_test, betas)
features_train = sg.create_features(X_train.T)
features_test = sg.create_features(X_test.T)
labels_train = sg.create_labels(y_train)
labels_test = sg.create_labels(y_test)
# Create the random forest object
mean_rule = sg.create_combination_rule("MeanRule")
rand_forest = sg.create_machine("RandomForest", labels=labels_train, num_bags=5,
seed=1, combination_rule=mean_rule)
rand_forest.train(features_train)
labels_predict = rand_forest.apply_regression(features_test)
# Serialize the model
model_file_path = './sample_model.json'
sg.serialize(model_file_path, rand_forest, sg.JsonSerializer())
# Deserialize the model and return
deserialized_rand_forest = sg.as_machine(sg.deserialize(model_file_path, sg.JsonDeserializer()))
labels_train_predict = deserialized_rand_forest.apply_regression(features_test)
When using the python interface, if we serialize and then deserialize a
RandomForestobject, we will get asegfaultif we try to call theapply_regressionmethod from the deserialized object. See the code below for an example.