Example #1
0
 def test_tuple_2_flexible_data(self):
     input = (1, 2, 3)
     flexible_data = module_desc_pb2.FlexibleData()
     from_pyobj_to_flexible_data(input, flexible_data)
     assert flexible_data.type == module_desc_pb2.LIST, "type conversion error"
     assert len(
         flexible_data.list.data) == len(input), "value convesion error"
     for index in range(len(input)):
         _check_int(input[index], flexible_data.list.data[str(index)])
Example #2
0
 def test_set_2_flexible_data(self):
     input = {1, 2, 3}
     flexible_data = module_desc_pb2.FlexibleData()
     from_pyobj_to_flexible_data(input, flexible_data)
     assert flexible_data.type == module_desc_pb2.SET, "type conversion error"
     assert len(
         flexible_data.set.data) == len(input), "value convesion error"
     for index in range(len(input)):
         assert flexible_data.set.data[str(
             index)].i in input, "value convesion error"
Example #3
0
 def test_dict_2_flexible_data(self):
     input = {1: 1, 2: 2, 3: 3}
     flexible_data = module_desc_pb2.FlexibleData()
     from_pyobj_to_flexible_data(input, flexible_data)
     assert flexible_data.type == module_desc_pb2.MAP, "type conversion error"
     assert len(
         flexible_data.map.data) == len(input), "value convesion error"
     for key, value in flexible_data.map.data.items():
         realkey = get_pykey(key, flexible_data.map.keyType[key])
         assert realkey in input, "key convesion error"
         _check_int(input[realkey], flexible_data.map.data[key])
Example #4
0
 def test_convert_compound_object(self):
     input = {
         False: 1,
         '2': 3,
         4.0: [5, 6.0, ['7', {
             8: 9
         }]],
         'set': {10},
         'dict': {
             11: 12
         }
     }
     flexible_data = module_desc_pb2.FlexibleData()
     from_pyobj_to_flexible_data(input, flexible_data)
     output = from_flexible_data_to_pyobj(flexible_data)
     assert input == output, "dict convesion error"
Example #5
0
    def test_obj_2_flexible_data(self):
        class TestObj:
            def __init__(self):
                self.a = 1
                self.b = 2.0
                self.c = "str"
                self.d = {'a': 123}

        input = TestObj()
        flexible_data = module_desc_pb2.FlexibleData()
        from_pyobj_to_flexible_data(input, flexible_data)
        assert flexible_data.type == module_desc_pb2.OBJECT, "type conversion error"
        assert len(flexible_data.object.data) == len(
            input.__dict__), "value convesion error"
        _check_int(input.a, flexible_data.object.data['a'])
        _check_float(input.b, flexible_data.object.data['b'])
        _check_str(input.c, flexible_data.object.data['c'])
        _check_int(input.d['a'], flexible_data.object.data['d'].map.data['a'])
 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_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_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!"
Example #10
0
 def test_convert_bool(self):
     input = False
     flexible_data = module_desc_pb2.FlexibleData()
     from_pyobj_to_flexible_data(input, flexible_data)
     output = from_flexible_data_to_pyobj(flexible_data)
     assert input == output, "bool convesion error"
Example #11
0
 def test_string_2_flexible_data(self):
     input = "123"
     flexible_data = module_desc_pb2.FlexibleData()
     from_pyobj_to_flexible_data(input, flexible_data)
     _check_str(input, flexible_data)
Example #12
0
 def test_bool_2_flexible_data(self):
     input = False
     flexible_data = module_desc_pb2.FlexibleData()
     from_pyobj_to_flexible_data(input, flexible_data)
     _check_bool(input, flexible_data)
Example #13
0
 def test_int_2_flexible_data(self):
     input = 1
     flexible_data = module_desc_pb2.FlexibleData()
     from_pyobj_to_flexible_data(input, flexible_data)
     _check_int(input, flexible_data)
Example #14
0
 def test_float_2_flexible_data(self):
     input = 2.012
     flexible_data = module_desc_pb2.FlexibleData()
     from_pyobj_to_flexible_data(input, flexible_data)
     _check_float(input, flexible_data)
Example #15
0
 def test_convert_float(self):
     input = 2.012
     flexible_data = module_desc_pb2.FlexibleData()
     from_pyobj_to_flexible_data(input, flexible_data)
     output = from_flexible_data_to_pyobj(flexible_data)
     assert _compare_float(input, output), "float convesion error"
Example #16
0
 def test_convert_dict(self):
     input = {1: 1, 2: 2, 3: 3}
     flexible_data = module_desc_pb2.FlexibleData()
     from_pyobj_to_flexible_data(input, flexible_data)
     output = from_flexible_data_to_pyobj(flexible_data)
     assert input == output, "dict convesion error"
Example #17
0
 def test_convert_tuple(self):
     input = (1, 2, 3)
     flexible_data = module_desc_pb2.FlexibleData()
     from_pyobj_to_flexible_data(input, flexible_data)
     output = from_flexible_data_to_pyobj(flexible_data)
     assert list(input) == output, "tuple convesion error"
Example #18
0
 def test_convert_list(self):
     input = [1, 2, 3]
     flexible_data = module_desc_pb2.FlexibleData()
     from_pyobj_to_flexible_data(input, flexible_data)
     output = from_flexible_data_to_pyobj(flexible_data)
     assert input == output, "list convesion error"
Example #19
0
 def test_convert_str(self):
     input = "123"
     flexible_data = module_desc_pb2.FlexibleData()
     from_pyobj_to_flexible_data(input, flexible_data)
     output = from_flexible_data_to_pyobj(flexible_data)
     assert input == output, "str convesion error"