@@ -919,6 +919,121 @@ def test_dygraph(self):
919919 )
920920
921921
922+ class  TestDropout1DFAPI (unittest .TestCase ):
923+  def  setUp (self ):
924+  np .random .seed (123 )
925+  self .places  =  get_places ()
926+ 
927+  def  check_static_result (
928+  self , place , input_name , input_shape , training = False , p = 0.0 
929+  ):
930+  paddle .enable_static ()
931+  main_prog  =  paddle .static .Program ()
932+  startup_prog  =  paddle .static .Program ()
933+  with  paddle .static .program_guard (main_prog , startup_prog ):
934+  input_var  =  paddle .static .data (
935+  name = input_name , shape = input_shape , dtype = "float32" 
936+  )
937+  res  =  paddle .nn .functional .dropout1d (
938+  input = input_var , p = p , training = training 
939+  )
940+  in_np  =  np .random .random (input_shape ).astype ("float32" )
941+  exe  =  base .Executor (place )
942+  fetches  =  exe .run (
943+  main_prog ,
944+  feed = {input_name : in_np },
945+  fetch_list = [res ],
946+  )
947+ 
948+  np .testing .assert_allclose (fetches [0 ], in_np , rtol = 1e-05 )
949+ 
950+  def  test_static (self ):
951+  for  place  in  self .places :
952+  self .check_static_result (
953+  place = place ,
954+  input_name = "input_2d" ,
955+  input_shape = [3 , 4 ],
956+  training = False ,
957+  p = 0.0 ,
958+  )
959+ 
960+  self .check_static_result (
961+  place = place ,
962+  input_name = "input_3d" ,
963+  input_shape = [2 , 3 , 4 ],
964+  training = False ,
965+  p = 0.0 ,
966+  )
967+ 
968+  self .check_static_result (
969+  place = place ,
970+  input_name = "input_2d_1" ,
971+  input_shape = [3 , 4 ],
972+  training = False ,
973+  p = 1.0 ,
974+  )
975+ 
976+  self .check_static_result (
977+  place = place ,
978+  input_name = "input_3d_1" ,
979+  input_shape = [2 , 3 , 4 ],
980+  training = False ,
981+  p = 1.0 ,
982+  )
983+ 
984+  def  test_dygraph (self ):
985+  for  place  in  self .places :
986+  with  base .dygraph .guard (place ):
987+  # Test 2D input 
988+  in_np_2d  =  np .random .random ([3 , 4 ]).astype ("float32" )
989+  input_2d  =  paddle .to_tensor (in_np_2d )
990+  res1  =  paddle .nn .functional .dropout1d (
991+  input = input_2d , p = 0.0 , training = False 
992+  )
993+  np .testing .assert_allclose (res1 .numpy (), in_np_2d , rtol = 1e-05 )
994+ 
995+  # Test 3D input 
996+  in_np_3d  =  np .random .random ([2 , 3 , 4 ]).astype ("float32" )
997+  input_3d  =  paddle .to_tensor (in_np_3d )
998+  res2  =  paddle .nn .functional .dropout1d (
999+  input = input_3d , p = 0.0 , training = False 
1000+  )
1001+  np .testing .assert_allclose (res2 .numpy (), in_np_3d , rtol = 1e-05 )
1002+ 
1003+ 
1004+ class  TestDropout1DFAPIError (unittest .TestCase ):
1005+  def  test_errors (self ):
1006+  paddle .enable_static ()
1007+  main_prog  =  paddle .static .Program ()
1008+  startup_prog  =  paddle .static .Program ()
1009+  with  paddle .static .program_guard (main_prog , startup_prog ):
1010+ 
1011+  def  test_xdim_1d ():
1012+  # dimensions of x should be 2 or 3 
1013+  x  =  paddle .static .data (name = 'x1' , shape = [4 ], dtype = "float32" )
1014+  paddle .nn .functional .dropout1d (x )
1015+ 
1016+  self .assertRaises (RuntimeError , test_xdim_1d )
1017+ 
1018+  def  test_xdim_4d ():
1019+  # dimensions of x should be 2 or 3 
1020+  x  =  paddle .static .data (
1021+  name = 'x2' , shape = [2 , 3 , 4 , 5 ], dtype = "float32" 
1022+  )
1023+  paddle .nn .functional .dropout1d (x )
1024+ 
1025+  self .assertRaises (RuntimeError , test_xdim_4d )
1026+ 
1027+  def  test_prob_range ():
1028+  # p should be in [0, 1] 
1029+  x  =  paddle .static .data (
1030+  name = 'x3' , shape = [2 , 3 , 4 ], dtype = "float32" 
1031+  )
1032+  paddle .nn .functional .dropout1d (x , p = 1.5 )
1033+ 
1034+  self .assertRaises (ValueError , test_prob_range )
1035+ 
1036+ 
9221037class  TestDropout2DFAPI (unittest .TestCase ):
9231038 def  setUp (self ):
9241039 np .random .seed (123 )
@@ -1404,6 +1519,12 @@ def test_p_tensor(self):
14041519 np .testing .assert_array_equal (static_res , dygraph_res )
14051520
14061521
1522+ class  TestDropOut1DWithProbTensor (TestDropOutWithProbTensor ):
1523+  def  init_info (self ):
1524+  self .shape  =  [2 , 3 , 4 ]
1525+  self .api  =  paddle .nn .functional .dropout1d 
1526+ 
1527+ 
14071528class  TestDropOut2DWithProbTensor (TestDropOutWithProbTensor ):
14081529 def  init_info (self ):
14091530 self .shape  =  [2 , 3 , 10 , 10 ]
0 commit comments