def _process_parameter(self): global_block = self.inference_program.global_block() param_attrs = self.config.desc.extra_info.map.data['param_attrs'] for key, param_attr in param_attrs.map.data.items(): param = from_flexible_data_to_param(param_attr) param['name'] = HUB_VAR_PREFIX + key if (param['name'] not in global_block.vars): continue var = global_block.var(param['name']) global_block.create_parameter(**param, shape=var.shape, dtype=var.dtype, type=var.type, lod_level=var.lod_level, error_clip=var.error_clip, stop_gradient=var.stop_gradient, is_data=var.is_data)
def test_convert_do_model_average(self): program = fluid.Program() with fluid.program_guard(program): input = fluid.layers.data(name="test", shape=[1], dtype="float32") fluid.layers.fc(input=input, size=10, param_attr=fluid.ParamAttr(name="fc_w", do_model_average=True)) fc_w = [ param for param in fluid.default_main_program().global_block().iter_parameters() ][0] flexible_data = module_desc_pb2.FlexibleData() from_param_to_flexible_data(fc_w, flexible_data) param_dict = from_flexible_data_to_param(flexible_data) assert fc_w.do_model_average.__class__ == param_dict[ 'do_model_average'].__class__, "do_model_average type convert error!" assert fc_w.do_model_average == param_dict[ 'do_model_average'], "do_model_average value convert error!"
def test_convert_optimize_attr(self): program = fluid.Program() with fluid.program_guard(program): input = fluid.layers.data(name="test", shape=[1], dtype="float32") fluid.layers.fc(input=input, size=10, param_attr=fluid.ParamAttr(name="fc_w", learning_rate=5)) fc_w = [ param for param in fluid.default_main_program().global_block().iter_parameters() ][0] flexible_data = module_desc_pb2.FlexibleData() from_param_to_flexible_data(fc_w, flexible_data) param_dict = from_flexible_data_to_param(flexible_data) assert fc_w.optimize_attr.__class__ == param_dict[ 'optimize_attr'].__class__, "optimize_attr type convert error!" assert fc_w.optimize_attr == param_dict[ 'optimize_attr'], "optimize_attr value convert error!"
def test_convert_l2_regularizer(self): program = fluid.Program() with fluid.program_guard(program): input = fluid.layers.data(name="test", shape=[1], dtype="float32") fluid.layers.fc(input=input, size=10, param_attr=fluid.ParamAttr( name="fc_w", regularizer=fluid.regularizer.L2Decay( regularization_coeff=1.5))) fc_w = [ param for param in fluid.default_main_program().global_block().iter_parameters() ][0] flexible_data = module_desc_pb2.FlexibleData() from_param_to_flexible_data(fc_w, flexible_data) param_dict = from_flexible_data_to_param(flexible_data) assert fc_w.regularizer.__class__ == param_dict[ 'regularizer'].__class__, "regularzier type convert error!" assert fc_w.regularizer._regularization_coeff == param_dict[ 'regularizer']._regularization_coeff, "regularzier value convert error!"
def test_convert_gradient_clip_by_normal(self): program = fluid.Program() with fluid.program_guard(program): input = fluid.layers.data(name="test", shape=[1], dtype="float32") fluid.layers.fc( input=input, size=10, param_attr=fluid.ParamAttr( name="fc_w", gradient_clip=fluid.clip.GradientClipByNorm(clip_norm=1))) fc_w = [ param for param in fluid.default_main_program().global_block().iter_parameters() ][0] flexible_data = module_desc_pb2.FlexibleData() from_param_to_flexible_data(fc_w, flexible_data) param_dict = from_flexible_data_to_param(flexible_data) assert fc_w.gradient_clip_attr.__class__ == param_dict[ 'gradient_clip_attr'].__class__, "clip type convert error!" assert fc_w.gradient_clip_attr.clip_norm == param_dict[ 'gradient_clip_attr'].clip_norm, "clip value convert error!"