You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@madlib.apache.org by nj...@apache.org on 2019/04/29 19:33:10 UTC
[madlib] 01/03: DL: Do not compile params in predict
This is an automated email from the ASF dual-hosted git repository.
njayaram pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/madlib.git
commit 3e2869db20ac24d4d1353e1a4a9d9d5d756e4682
Author: Nandish Jayaram <nj...@apache.org>
AuthorDate: Thu Apr 25 11:24:36 2019 -0700
DL: Do not compile params in predict
JIRA: MADLIB-1330
Do not compile params in predict, but instead directly get the model
weights and architecture and use it for prediction. Compiling params
during predict is not necessary from Keras 1.0.3.
Closes #377
---
src/ports/postgres/modules/deep_learning/madlib_keras.sql_in | 2 --
.../postgres/modules/deep_learning/madlib_keras_predict.py_in | 10 +++-------
.../modules/deep_learning/madlib_keras_validator.py_in | 1 -
.../postgres/modules/deep_learning/madlib_keras_wrapper.py_in | 9 +++++++++
.../postgres/modules/deep_learning/predict_input_params.py_in | 4 ----
5 files changed, 12 insertions(+), 14 deletions(-)
diff --git a/src/ports/postgres/modules/deep_learning/madlib_keras.sql_in b/src/ports/postgres/modules/deep_learning/madlib_keras.sql_in
index 8e13933..37b1068 100644
--- a/src/ports/postgres/modules/deep_learning/madlib_keras.sql_in
+++ b/src/ports/postgres/modules/deep_learning/madlib_keras.sql_in
@@ -203,7 +203,6 @@ CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.internal_keras_predict(
model_architecture TEXT,
model_data bytea,
input_shape integer[],
- compile_params TEXT,
is_response BOOLEAN,
normalizing_const DOUBLE PRECISION
) RETURNS DOUBLE PRECISION[] AS $$
@@ -214,7 +213,6 @@ CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.internal_keras_predict(
model_architecture,
model_data,
input_shape,
- compile_params,
is_response,
normalizing_const)
$$ LANGUAGE plpythonu VOLATILE
diff --git a/src/ports/postgres/modules/deep_learning/madlib_keras_predict.py_in b/src/ports/postgres/modules/deep_learning/madlib_keras_predict.py_in
index 739f042..e726f57 100644
--- a/src/ports/postgres/modules/deep_learning/madlib_keras_predict.py_in
+++ b/src/ports/postgres/modules/deep_learning/madlib_keras_predict.py_in
@@ -30,7 +30,7 @@ import numpy as np
from madlib_keras_helper import expand_input_dims
from madlib_keras_helper import MODEL_DATA_COLNAME
from madlib_keras_validator import PredictInputValidator
-from madlib_keras_wrapper import compile_and_set_weights
+from madlib_keras_wrapper import set_model_weights
from predict_input_params import PredictParamsProcessor
from utilities.model_arch_info import get_input_shape
from utilities.utilities import add_postfix
@@ -87,7 +87,6 @@ def predict(schema_madlib, model_table, test_table, id_col,
param_proc = PredictParamsProcessor(model_table, MODULE_NAME)
class_values = param_proc.get_class_values()
input_validator.validate_pred_type(class_values)
- compile_params = param_proc.get_compile_params()
dependent_varname = param_proc.get_dependent_varname()
dependent_vartype = param_proc.get_dependent_vartype()
model_data = param_proc.get_model_data()
@@ -95,7 +94,6 @@ def predict(schema_madlib, model_table, test_table, id_col,
normalizing_const = param_proc.get_normalizing_const()
input_shape = get_input_shape(model_arch)
input_validator.validate_input_shape(input_shape)
- compile_params = "$madlib$" + compile_params + "$madlib$"
is_response = True if pred_type == 'response' else False
intermediate_col = unique_string()
@@ -122,7 +120,6 @@ def predict(schema_madlib, model_table, test_table, id_col,
$MAD${model_arch}$MAD$,
{0},
ARRAY{input_shape},
- {compile_params},
{is_response},
{normalizing_const})
) AS {intermediate_col}
@@ -131,13 +128,12 @@ def predict(schema_madlib, model_table, test_table, id_col,
""".format(MODEL_DATA_COLNAME, **locals()))
def internal_keras_predict(x_test, model_arch, model_data, input_shape,
- compile_params, is_response, normalizing_const):
+ is_response, normalizing_const):
model = model_from_json(model_arch)
device_name = '/cpu:0'
os.environ["CUDA_VISIBLE_DEVICES"] = '-1'
model_shapes = madlib_keras_serializer.get_model_shapes(model)
- compile_and_set_weights(model, compile_params, device_name,
- model_data, model_shapes)
+ set_model_weights(model, device_name, model_data, model_shapes)
# Since the test data isn't mini-batched,
# we have to make sure that the test data np array has the same
# number of dimensions as input_shape. So we add a dimension to x.
diff --git a/src/ports/postgres/modules/deep_learning/madlib_keras_validator.py_in b/src/ports/postgres/modules/deep_learning/madlib_keras_validator.py_in
index cbe8f3c..ee667d0 100644
--- a/src/ports/postgres/modules/deep_learning/madlib_keras_validator.py_in
+++ b/src/ports/postgres/modules/deep_learning/madlib_keras_validator.py_in
@@ -130,7 +130,6 @@ class PredictInputValidator:
def _validate_summary_tbl_cols(self):
cols_to_check_for = [CLASS_VALUES_COLNAME,
- COMPILE_PARAMS_COLNAME,
DEPENDENT_VARNAME_COLNAME,
DEPENDENT_VARTYPE_COLNAME,
MODEL_ARCH_ID_COLNAME,
diff --git a/src/ports/postgres/modules/deep_learning/madlib_keras_wrapper.py_in b/src/ports/postgres/modules/deep_learning/madlib_keras_wrapper.py_in
index 71c257f..e0fd8f7 100644
--- a/src/ports/postgres/modules/deep_learning/madlib_keras_wrapper.py_in
+++ b/src/ports/postgres/modules/deep_learning/madlib_keras_wrapper.py_in
@@ -73,6 +73,15 @@ def compile_and_set_weights(segment_model, compile_params, device_name,
previous_state, model_shapes)
segment_model.set_weights(model_weights)
+# TODO: This can be refactored to be part of compile_and_set_weights(),
+# by making compile_params an optional param in that function. Doing that
+# now might create more merge conflicts with other JIRAs, so get to this later.
+def set_model_weights(segment_model, device_name, state, model_shapes):
+ with K.tf.device(device_name):
+ _, _, _, model_weights = madlib_keras_serializer.deserialize_weights(
+ state, model_shapes)
+ segment_model.set_weights(model_weights)
+
"""
Used to convert compile_params and fit_params to actual argument dictionaries
"""
diff --git a/src/ports/postgres/modules/deep_learning/predict_input_params.py_in b/src/ports/postgres/modules/deep_learning/predict_input_params.py_in
index 69ee961..aba6dce 100644
--- a/src/ports/postgres/modules/deep_learning/predict_input_params.py_in
+++ b/src/ports/postgres/modules/deep_learning/predict_input_params.py_in
@@ -23,7 +23,6 @@ from utilities.utilities import add_postfix
from utilities.validate_args import input_tbl_valid
from madlib_keras_helper import CLASS_VALUES_COLNAME
-from madlib_keras_helper import COMPILE_PARAMS_COLNAME
from madlib_keras_helper import DEPENDENT_VARNAME_COLNAME
from madlib_keras_helper import DEPENDENT_VARTYPE_COLNAME
from madlib_keras_helper import MODEL_ARCH_ID_COLNAME
@@ -63,9 +62,6 @@ class PredictParamsProcessor:
def get_class_values(self):
return self.model_summary_dict[CLASS_VALUES_COLNAME]
- def get_compile_params(self):
- return self.model_summary_dict[COMPILE_PARAMS_COLNAME]
-
def get_dependent_varname(self):
return self.model_summary_dict[DEPENDENT_VARNAME_COLNAME]