jingyimei commented on a change in pull request #359: DL: Remove eval statement
URL: https://github.com/apache/madlib/pull/359#discussion_r270529920
##########
File path: src/ports/postgres/modules/deep_learning/madlib_keras_wrapper.py_in
##########
@@ -85,7 +96,57 @@ def convert_string_of_args_to_dict(str_of_args):
stack.pop(-1)
result_str += char
elif not stack and char == "=":
- result_str += ":"
+ key_str = result_str
+ result_str = ""
+ elif not stack and char == ",":
+ value_str = result_str
+ result_str = ""
+ compile_dict[key_str.strip()]=value_str.strip('\'')
else:
result_str += char
- return eval('{' + result_str + '}')
+ value_str = result_str
+ result_str = ""
+ compile_dict[key_str.strip()]=value_str.strip('\'')
+ return compile_dict
+
+
+# Parse the compile parameters and the optimizer.
+# Optimizer name and its arguments are returned in addition to the rest of the
+# compile parameters.
+def parse_compile_params(str_of_args):
+
+ compile_dict = convert_string_of_args_to_dict(str_of_args)
+ compile_dict['metrics'] = ast.literal_eval(compile_dict['metrics']) if
'metrics' in compile_dict.keys() else None
+ compile_dict['loss_weights'] =
ast.literal_eval(compile_dict['loss_weights']) if 'loss_weights' in
compile_dict.keys() else None
+
+ opt_name = compile_dict['optimizer'].split('(')[0]
+ opt_params = compile_dict['optimizer'].split('(')[1][:-1]
+ opt_params_array = opt_params.split(',')
+ opt_params_clean = map(split_and_strip, opt_params_array)
+ key_value_params = { x[0] : x[1] for x in opt_params_clean}
+ final_args = { key: bool(value) if value == 'True' or value == 'False'
else float(value) for key,value in key_value_params.iteritems() }
+
+ return (opt_name,final_args,compile_dict)
+
+# Parse the fit parameters into a dictionary.
+def parse_fit_params(str_of_args):
+ compile_dict = convert_string_of_args_to_dict(str_of_args)
+ for key in compile_dict.keys():
+ compile_dict[key] = ast.literal_eval(compile_dict[key])
+ return compile_dict
+
+# Split and strip the whispace of key=value formatted strings
+def split_and_strip(x):
+ y = x.split('=')
+ return (y[0].strip(),y[1].strip())
+
+# Return the list of keras optimizers
+def get_optimizers():
+ import keras.optimizers as opt
+ optimizers = dict()
+ names = dir(opt)
+ for n in names:
+ o = eval('opt.' + n)
Review comment:
Can we give it a more specific variable name instead of `o`
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services