1414
1515import paddle
1616import paddle .fluid .layers as layers
17- from paddle .fluid .framework import Program , program_guard , default_main_program , default_startup_program
17+ from paddle .fluid .framework import Program , program_guard
1818from paddle .fluid .executor import Executor
1919from paddle .fluid .optimizer import MomentumOptimizer
2020import paddle .fluid .core as core
21+ import paddle .fluid as fluid
2122import unittest
2223import numpy as np
2324
@@ -31,22 +32,21 @@ def test_raw_api(self):
3132
3233 label = layers .data (name = 'y' , shape = [1 ], dtype = 'int64' )
3334
34- limit = layers .fill_constant_batch_size_like (
35- input = label , dtype = 'int64' , shape = [1 ], value = 5.0 )
35+ limit = layers .fill_constant (shape = [1 ], dtype = 'int64' , value = 5 )
3636 cond = layers .less_than (x = label , y = limit )
3737 true_image , false_image = layers .split_lod_tensor (
3838 input = image , mask = cond )
3939
4040 true_out = layers .create_tensor (dtype = 'float32' )
41- true_cond = layers .ConditionalBlock ([true_image ])
41+ true_cond = layers .ConditionalBlock ([cond ])
4242
4343 with true_cond .block ():
4444 hidden = layers .fc (input = true_image , size = 100 , act = 'tanh' )
4545 prob = layers .fc (input = hidden , size = 10 , act = 'softmax' )
4646 layers .assign (input = prob , output = true_out )
4747
4848 false_out = layers .create_tensor (dtype = 'float32' )
49- false_cond = layers .ConditionalBlock ([false_image ])
49+ false_cond = layers .ConditionalBlock ([cond ])
5050
5151 with false_cond .block ():
5252 hidden = layers .fc (input = false_image , size = 200 , act = 'tanh' )
@@ -64,7 +64,7 @@ def test_raw_api(self):
6464 train_reader = paddle .batch (
6565 paddle .reader .shuffle (
6666 paddle .dataset .mnist .train (), buf_size = 8192 ),
67- batch_size = 200 )
67+ batch_size = 10 )
6868
6969 place = core .CPUPlace ()
7070 exe = Executor (place )
@@ -94,8 +94,7 @@ def test_ifelse(self):
9494
9595 label = layers .data (name = 'y' , shape = [1 ], dtype = 'int64' )
9696
97- limit = layers .fill_constant_batch_size_like (
98- input = label , dtype = 'int64' , shape = [1 ], value = 5.0 )
97+ limit = layers .fill_constant (shape = [1 ], dtype = 'int64' , value = 5 )
9998 cond = layers .less_than (x = label , y = limit )
10099 ie = layers .IfElse (cond )
101100
@@ -125,15 +124,15 @@ def test_ifelse(self):
125124 place = core .CPUPlace ()
126125 exe = Executor (place )
127126
128- exe .run (kwargs [ 'startup_program' ] )
127+ exe .run (startup_prog )
129128 PASS_NUM = 100
130129 for pass_id in range (PASS_NUM ):
131130 for data in train_reader ():
132131 x_data = np .array (map (lambda x : x [0 ], data )).astype ("float32" )
133132 y_data = np .array (map (lambda x : x [1 ], data )).astype ("int64" )
134133 y_data = y_data .reshape ((y_data .shape [0 ], 1 ))
135134
136- outs = exe .run (kwargs [ 'main_program' ] ,
135+ outs = exe .run (prog ,
137136 feed = {'x' : x_data ,
138137 'y' : y_data },
139138 fetch_list = [avg_loss ])
@@ -143,6 +142,67 @@ def test_ifelse(self):
143142 self .assertFalse (True )
144143
145144
145+ class TestIfElse (unittest .TestCase ):
146+ def set_test_case (self ):
147+ # condiction is: self.data < self.cond_value
148+ self .cond_value = 0.5
149+ self .data = np .random .rand (25 , 1 ).astype (np .float32 )
150+
151+ def compare_ifelse_op_and_numpy (self , place ):
152+ self .set_test_case ()
153+
154+ prog = Program ()
155+ startup_prog = Program ()
156+ with program_guard (prog , startup_prog ):
157+ src = layers .data (name = 'data' , shape = [1 ], dtype = 'float32' )
158+ cond = layers .fill_constant (
159+ [1 ], dtype = 'float32' , value = self .cond_value )
160+ ifcond = layers .less_than (x = src , y = cond )
161+ ie = layers .IfElse (ifcond )
162+ with ie .true_block ():
163+ true_target = ie .input (src )
164+ ie .output (true_target )
165+
166+ with ie .false_block ():
167+ false_target = ie .input (src )
168+ ie .output (false_target )
169+ if_out = ie ()
170+ out = layers .reduce_sum (if_out )
171+
172+ exe = fluid .Executor (place )
173+ exe .run (fluid .default_startup_program ())
174+ fetch_list = [out ]
175+ o1 , = exe .run (fluid .default_main_program (),
176+ feed = {'data' : self .data },
177+ fetch_list = [out ])
178+ o2 = np .sum (self .data )
179+ self .assertTrue (
180+ np .allclose (
181+ o1 , o2 , atol = 1e-8 ),
182+ "IfElse result : " + str (o1 ) + "\n Numpy result :" + str (o2 ))
183+
184+ def test_cpu (self ):
185+ self .compare_ifelse_op_and_numpy (fluid .CPUPlace ())
186+
187+ def test_cuda (self ):
188+ if not core .is_compiled_with_cuda ():
189+ return
190+ self .compare_ifelse_op_and_numpy (fluid .CUDAPlace (0 ))
191+
192+
193+ class TestIfElseTrueBranch (TestIfElse ):
194+ def set_test_case (self ):
195+ # condiction is: self.data < self.cond_value
196+ self .cond_value = 10.
197+ self .data = np .random .rand (25 , 1 ).astype (np .float32 )
198+
199+
200+ class TestIfElseFalseBranch (TestIfElse ):
201+ def set_test_case (self ):
202+ # condiction is: self.data < self.cond_value
203+ self .cond_value = - 10.
204+ self .data = np .random .rand (25 , 1 ).astype (np .float32 )
205+
206+
146207if __name__ == '__main__' :
147- # temp disable if else unittest since it could be buggy.
148- exit (0 )
208+ unittest .main ()
0 commit comments