4646 AdvancedIncSubtensor1 ,
4747 AdvancedSubtensor ,
4848 AdvancedSubtensor1 ,
49- AdvancedBooleanSubtensor ,
50- AdvancedBooleanIncSubtensor ,
5149 advanced_inc_subtensor ,
5250 advanced_inc_subtensor1 ,
5351 advanced_set_subtensor ,
6765 IncSubtensor ,
6866 AdvancedSubtensor1 ,
6967 AdvancedIncSubtensor1 ,
70- AdvancedBooleanSubtensor ,
71- AdvancedBooleanIncSubtensor ,
7268)
7369
7470
@@ -368,12 +364,10 @@ def numpy_inc_subtensor(x, idx, a):
368364
369365 # indexing with a mask for some dimensions
370366 mask = np .array ([True , False ])
371- val = self .eval_output_and_check (
372- test_array [mask ], op_type = AdvancedBooleanSubtensor
373- )
367+ val = self .eval_output_and_check (test_array [mask ], op_type = AdvancedSubtensor )
374368 assert_array_equal (test_array_np [mask ], val )
375369 val = self .eval_output_and_check (
376- inc_subtensor (test_array [mask ], 1 ), op_type = AdvancedBooleanIncSubtensor
370+ inc_subtensor (test_array [mask ], 1 ), op_type = AdvancedIncSubtensor
377371 )
378372 assert_array_equal (numpy_inc_subtensor (test_array_np , mask , 1 ), val )
379373 assert_array_equal (
@@ -580,8 +574,8 @@ def test_grad_1d(self):
580574 topo_ = [node for node in topo if not isinstance (node .op , DeepCopyOp )]
581575 if not self .fast_compile :
582576 assert len (topo_ ) == 6
583- assert np . sum ([ isinstance (node .op , IncSubtensor ) for node in topo_ ]) == 1
584- assert np . sum ([ isinstance (node .op , Subtensor ) for node in topo_ ]) == 1
577+ assert any ( isinstance (node .op , IncSubtensor ) for node in topo_ )
578+ assert any ( isinstance (node .op , Subtensor ) for node in topo_ )
585579 gval = f ()
586580
587581 good = np .zeros_like (data )
@@ -1161,47 +1155,6 @@ def test_shape_list(self):
11611155 val = f ()
11621156 assert np .allclose (val , data [idx ].shape )
11631157
1164- def test_grad_advanced_inc_subtensor (self ):
1165- def inc_slice (* s ):
1166- def just_numeric_args (a , b ):
1167- cost = (a [s ] + b ).sum ()
1168- cost_wrt_a = tensor .grad (cost , a )
1169- cost_wrt_b = tensor .grad (cost , b )
1170- grads = cost_wrt_a .sum () + cost_wrt_b .sum ()
1171- return grads
1172-
1173- return just_numeric_args
1174-
1175- # vector
1176- utt .verify_grad (
1177- inc_slice (slice (2 , 4 , None )),
1178- (
1179- np .asarray ([0 , 1 , 2 , 3 , 4 , 5.0 ]),
1180- np .asarray ([9 , 9.0 ]),
1181- ),
1182- mode = self .mode ,
1183- )
1184-
1185- # matrix
1186- utt .verify_grad (
1187- inc_slice (slice (1 , 2 , None ), slice (None , None , None )),
1188- (
1189- np .asarray ([[0 , 1 ], [2 , 3 ], [4 , 5.0 ]]),
1190- np .asarray ([[9 , 9.0 ]]),
1191- ),
1192- mode = self .mode ,
1193- )
1194-
1195- # single element
1196- utt .verify_grad (
1197- inc_slice (2 , 1 ),
1198- (
1199- np .asarray ([[0 , 1 ], [2 , 3 ], [4 , 5.0 ]]),
1200- np .asarray (9.0 ),
1201- ),
1202- mode = self .mode ,
1203- )
1204-
12051158 def test_inc_and_set_subtensor (self ):
12061159 # Test increment and set with broadcast
12071160
@@ -1323,21 +1276,6 @@ def test_advanced1_inc_and_set(self):
13231276 all_params .append (
13241277 (set_instead_of_inc , inplace , data_shape , inc_shape )
13251278 )
1326- if False : # Enable for debugging purpose.
1327- f = self .function (
1328- [data_var , idx_var , inc_var ],
1329- output ,
1330- accept_inplace = inplace ,
1331- op = AdvancedIncSubtensor1 ,
1332- )
1333- if inplace :
1334- # Ensure calling `f` will not alter `data_num`.
1335- data_num = data_num .copy ()
1336- f_out = f (data_num .copy (), idx_num , inc_num )
1337- assert np .allclose (f_out , data_copy )
1338- if not inplace :
1339- # Sanity check: `data_num` should be intact.
1340- assert (data_num == data_num_init ).all ()
13411279
13421280 # Actual test (we compile a single Theano function to make it faster).
13431281 orig_warn = theano .config .warn .gpu_set_subtensor1
@@ -1647,18 +1585,18 @@ def check(idx, y_val, x_val, true):
16471585 rep [idx ] += y_val
16481586 check (idx , y_val , x_val , rep )
16491587
1650- def eval_output_and_check (self , t ):
1588+ def eval_output_and_check (self , t , op ):
16511589 f = inplace_func ([], t , mode = self .mode )
16521590 topo = f .maker .fgraph .toposort ()
16531591 topo_ = [node for node in topo if not isinstance (node .op , DeepCopyOp )]
16541592 assert len (topo_ ) == 1
1655- assert isinstance (topo_ [0 ].op , AdvancedSubtensor )
1593+ assert isinstance (topo_ [0 ].op , op )
16561594 tval = f ()
16571595 return tval
16581596
16591597 def test_cant_adv_idx_into_scalar (self ):
16601598 with pytest .raises (IndexError ):
1661- ( lambda : self .s [self .ix1 ])()
1599+ self .s [self .ix1 ]
16621600
16631601 def test_index_into_vec_w_vec (self ):
16641602 a = self .v [self .ix1 ]
@@ -1698,7 +1636,7 @@ def test_index_w_int_and_vec(self):
16981636
16991637 assert isinstance (t .owner .op , AdvancedSubtensor )
17001638
1701- val = self .eval_output_and_check (t )
1639+ val = self .eval_output_and_check (t , AdvancedSubtensor )
17021640 if isinstance (idx , list ):
17031641 good = data [0 , idx ]
17041642 else :
@@ -1942,6 +1880,35 @@ def fun(x, y):
19421880 mode = self .mode ,
19431881 )
19441882
1883+ # Test boolean gradients
1884+ def fun (x , y ):
1885+ return advanced_inc_subtensor (
1886+ x , y , tensor .as_tensor (np .array ([[True , False ], [False , True ]]))
1887+ )
1888+
1889+ utt .verify_grad (
1890+ fun ,
1891+ [
1892+ np .random .rand (2 , 2 ).astype (self .dtype ),
1893+ np .random .rand (2 ).astype (self .dtype ),
1894+ ],
1895+ mode = self .mode ,
1896+ )
1897+
1898+ def fun (x , y ):
1899+ return advanced_set_subtensor (
1900+ x , y , tensor .as_tensor (np .array ([[True , False ], [False , True ]]))
1901+ )
1902+
1903+ utt .verify_grad (
1904+ fun ,
1905+ [
1906+ np .random .rand (2 , 2 ).astype (self .dtype ),
1907+ np .random .rand (2 ).astype (self .dtype ),
1908+ ],
1909+ mode = self .mode ,
1910+ )
1911+
19451912
19461913class TestInferShape (utt .InferShapeTester ):
19471914 def test_IncSubtensor (self ):
@@ -2216,7 +2183,7 @@ def test_AdvancedSubtensor(self):
22162183 AdvancedSubtensor ,
22172184 )
22182185
2219- def test_AdvancedBooleanSubtensor (self ):
2186+ def test_AdvancedSubtensor_bool (self ):
22202187 n = dmatrix ()
22212188 n_val = np .arange (6 ).reshape ((2 , 3 ))
22222189
@@ -2225,14 +2192,14 @@ def test_AdvancedBooleanSubtensor(self):
22252192 [n ],
22262193 [n [n [:, 0 ] > 2 , n [0 , :] > 2 ]],
22272194 [n_val ],
2228- AdvancedBooleanSubtensor ,
2195+ AdvancedSubtensor ,
22292196 check_topo = False ,
22302197 )
22312198 self ._compile_and_check (
22322199 [n ],
22332200 [n [n [:, 0 ] > 2 ]],
22342201 [n_val ],
2235- AdvancedBooleanSubtensor ,
2202+ AdvancedSubtensor ,
22362203 check_topo = False ,
22372204 )
22382205
0 commit comments