Skip to content

Commit 332194c

Browse files
Haichao-Zhangemailweixu
authored andcommitted
add type compatible check for ParamAttr (#113)
* add type compatible check for ParamAttr
1 parent 77ed98d commit 332194c

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

python/paddle/trainer_config_helpers/attrs.py

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,42 @@
1717
'ExtraLayerAttribute']
1818

1919

20+
def convert_and_compare(x, Type):
21+
"""
22+
Convert x to be the same type as Type and then convert back to
23+
check whether there is a loss of information
24+
:param x: object to be checked
25+
:param Type: target type to check x over
26+
27+
"""
28+
return type(x)(Type(x))==x
29+
30+
def is_compatible_with(x, Type):
31+
"""
32+
Check if x has a type compatible with Type
33+
:param x: object to be checked
34+
:param Type: target type to check x over
35+
36+
"""
37+
if type(x) == Type:
38+
return True
39+
try:
40+
if float == Type or int == Type:
41+
# avoid those types that can be converted to float/int but not very
42+
# meaningful and could potentially lead to error
43+
# i.e., str and bool typed value should not be used for initializing float/int variable
44+
if not isinstance(x, str) and not isinstance(x, bool):
45+
return convert_and_compare(x, Type)
46+
elif bool == Type:
47+
# should not use string type to initialize bool variable
48+
if not isinstance(x, str):
49+
return convert_and_compare(x, Type)
50+
else:
51+
return False
52+
except:
53+
return False
54+
55+
2056
class ParameterAttribute(object):
2157
"""
2258
Parameter Attributes object. To fine-tuning network training process, user
@@ -65,14 +101,18 @@ def __init__(self, name=None, is_static=False, initial_std=None,
65101
elif initial_std is None and initial_mean is None and initial_max \
66102
is None and initial_min is None:
67103
self.attr = {'initial_smart': True}
68-
elif isinstance(initial_std, float) or isinstance(initial_mean, float):
104+
elif is_compatible_with(initial_std, float) or \
105+
is_compatible_with(initial_mean, float):
69106
self.attr = dict()
70107
if initial_std is not None:
71108
self.attr['initial_std'] = initial_std
72109
if initial_mean is not None:
73110
self.attr['initial_mean'] = initial_mean
74111
self.attr['initial_strategy'] = 0 # Gauss Random
75-
elif isinstance(initial_max, float) and isinstance(initial_min, float):
112+
elif is_compatible_with(initial_max, float) and \
113+
is_compatible_with(initial_min, float):
114+
initial_max = initial_max
115+
initial_min = initial_min
76116
assert initial_min < initial_max
77117
initial_mean = (initial_max + initial_min) / 2
78118
initial_std = initial_mean - initial_min
@@ -83,16 +123,16 @@ def __init__(self, name=None, is_static=False, initial_std=None,
83123
else:
84124
raise RuntimeError("Unexpected branch.")
85125

86-
if not is_static and isinstance(l1_rate, float):
126+
if not is_static and is_compatible_with(l1_rate, float):
87127
self.attr['decay_rate_l1'] = l1_rate
88128

89-
if not is_static and isinstance(l2_rate, float):
129+
if not is_static and is_compatible_with(l2_rate, float):
90130
self.attr['decay_rate'] = l2_rate
91131

92-
if not is_static and isinstance(learning_rate, float):
132+
if not is_static and is_compatible_with(learning_rate, float):
93133
self.attr['learning_rate'] = learning_rate
94134

95-
if not is_static and isinstance(momentum, float):
135+
if not is_static and is_compatible_with(momentum, float):
96136
self.attr['momentum'] = momentum
97137

98138
if name is not None:

python/paddle/trainer_config_helpers/tests/layers_test_config.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,20 @@
3939
outputs(classification_cost(out, data_layer(name="label", size=num_classes)))
4040

4141
dotmul = mixed_layer(input=[dotmul_operator(x=x1, y=y1),
42-
dotmul_projection(input=y1)])
42+
dotmul_projection(input=y1)])
43+
44+
proj_with_attr_init = mixed_layer(input=full_matrix_projection(input=y1,
45+
param_attr=ParamAttr(learning_rate = 0,
46+
initial_mean = 0,
47+
initial_std = 0)),
48+
bias_attr = ParamAttr(initial_mean=0, initial_std=0, learning_rate=0),
49+
act = LinearActivation(),
50+
size = 5,
51+
name='proj_with_attr_init')
52+
4353

4454
# for ctc
45-
tmp = fc_layer(input=[x1, dotmul],
55+
tmp = fc_layer(input=[x1, dotmul, proj_with_attr_init],
4656
size=num_classes + 1,
4757
act=SoftmaxActivation())
4858
ctc = ctc_layer(input=tmp,

0 commit comments

Comments
 (0)