kaknikhil commented on a change in pull request #355: Keras fit interface
URL: https://github.com/apache/madlib/pull/355#discussion_r266105010
 
 

 ##########
 File path: src/ports/postgres/modules/convex/madlib_keras_helper.py_in
 ##########
 @@ -0,0 +1,180 @@
+# coding=utf-8
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import os
+import plpy
+from keras import backend as K
+from keras import utils as keras_utils
+from keras.optimizers import *
+import numpy as np
+
+#######################################################################
+########### Keras specific functions #####
+#######################################################################
+
+def get_device_name_for_keras(use_gpu, seg, gpus_per_host):
+    if use_gpu:
+        device_name = '/gpu:0'
+        os.environ["CUDA_VISIBLE_DEVICES"] = str(seg % gpus_per_host)
+    else: # cpu only
+        device_name = '/cpu:0'
+        os.environ["CUDA_VISIBLE_DEVICES"] = '-1'
+
+    return device_name
+
+def set_keras_session(use_gpu):
+    config = K.tf.ConfigProto()
+    if use_gpu:
+        config.gpu_options.allow_growth = False
+        config.gpu_options.per_process_gpu_memory_fraction = 0.9
+    session = K.tf.Session(config=config)
+    K.set_session(session)
+
+def clear_keras_session():
+    sess = K.get_session()
+    K.clear_session()
+    sess.close()
+
+def compile_and_set_weights(segment_model, compile_params, device_name,
+                            previous_state):
+    model_shapes = []
+    with K.tf.device(device_name):
+        compile_params = convert_string_of_args_to_dict(compile_params)
+        segment_model.compile(**compile_params)
+        # prev_segment_model.compile(**compile_params)
+        for a in segment_model.get_weights():
+            model_shapes.append(a.shape)
+
+        agg_loss, agg_accuracy, _, model_weights = deserialize_weights(
+            previous_state, model_shapes)
+        segment_model.set_weights(model_weights)
+    # prev_model.set_weights(model_weights)
+
+#######################################################################
+########### Helper functions to serialize and deserialize weights #####
+#######################################################################
+
+def deserialize_weights(model_state, model_shapes):
 
 Review comment:
   I would recommend creating a separate python file for all the 
serialization/deserialization logic including other functions like 
`deserialize_weights_merge`, `serialize_weights_merge` etc. This would make 
both the files easy to maintain and read and then we can rename the helper 
function file from `madlib_keras_helper.py_in` to something like 
`madlib_keras_wrapper.py_in`. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to