未验证 提交 d8a9ba56 编写于 作者: L lijianshe02 提交者: GitHub

fix random seed in nll_loss unittest test=develop (#30468)

上级 5d8d463c
...@@ -74,6 +74,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -74,6 +74,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_1D_mean(self): def test_NLLLoss_1D_mean(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(10, 10)).astype(np.float64) input_np = np.random.random(size=(10, 10)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 10, size=(10, )).astype(np.int64) label_np = np.random.randint(0, 10, size=(10, )).astype(np.int64)
prog = fluid.Program() prog = fluid.Program()
startup_prog = fluid.Program() startup_prog = fluid.Program()
...@@ -108,6 +109,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -108,6 +109,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_1D_sum(self): def test_NLLLoss_1D_sum(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(10, 10)).astype(np.float64) input_np = np.random.random(size=(10, 10)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 10, size=(10, )).astype(np.int64) label_np = np.random.randint(0, 10, size=(10, )).astype(np.int64)
prog = fluid.Program() prog = fluid.Program()
startup_prog = fluid.Program() startup_prog = fluid.Program()
...@@ -142,6 +144,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -142,6 +144,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_1D_with_weight_mean(self): def test_NLLLoss_1D_with_weight_mean(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(10, 10)).astype(np.float64) input_np = np.random.random(size=(10, 10)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 10, size=(10, )).astype(np.int64) label_np = np.random.randint(0, 10, size=(10, )).astype(np.int64)
weight_np = np.random.random(size=(10, )).astype(np.float64) weight_np = np.random.random(size=(10, )).astype(np.float64)
prog = fluid.Program() prog = fluid.Program()
...@@ -181,6 +184,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -181,6 +184,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_1D_with_weight_sum(self): def test_NLLLoss_1D_with_weight_sum(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(10, 10)).astype(np.float64) input_np = np.random.random(size=(10, 10)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 10, size=(10, )).astype(np.int64) label_np = np.random.randint(0, 10, size=(10, )).astype(np.int64)
weight_np = np.random.random(size=(10, )).astype(np.float64) weight_np = np.random.random(size=(10, )).astype(np.float64)
prog = fluid.Program() prog = fluid.Program()
...@@ -221,6 +225,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -221,6 +225,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_1D_with_weight_mean_cpu(self): def test_NLLLoss_1D_with_weight_mean_cpu(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(10, 10)).astype(np.float64) input_np = np.random.random(size=(10, 10)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 10, size=(10, )).astype(np.int64) label_np = np.random.randint(0, 10, size=(10, )).astype(np.int64)
weight_np = np.random.random(size=(10, )).astype(np.float64) weight_np = np.random.random(size=(10, )).astype(np.float64)
prog = fluid.Program() prog = fluid.Program()
...@@ -258,6 +263,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -258,6 +263,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_1D_with_weight_no_reduce_cpu(self): def test_NLLLoss_1D_with_weight_no_reduce_cpu(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(10, 10)).astype(np.float64) input_np = np.random.random(size=(10, 10)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 10, size=(10, )).astype(np.int64) label_np = np.random.randint(0, 10, size=(10, )).astype(np.int64)
weight_np = np.random.random(size=(10, )).astype(np.float64) weight_np = np.random.random(size=(10, )).astype(np.float64)
prog = fluid.Program() prog = fluid.Program()
...@@ -296,6 +302,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -296,6 +302,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_2D_mean(self): def test_NLLLoss_2D_mean(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64) input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64) label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64)
prog = fluid.Program() prog = fluid.Program()
startup_prog = fluid.Program() startup_prog = fluid.Program()
...@@ -332,6 +339,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -332,6 +339,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_2D_sum(self): def test_NLLLoss_2D_sum(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64) input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64) label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64)
prog = fluid.Program() prog = fluid.Program()
startup_prog = fluid.Program() startup_prog = fluid.Program()
...@@ -368,6 +376,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -368,6 +376,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_2D_with_weight_mean(self): def test_NLLLoss_2D_with_weight_mean(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64) input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64) label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64)
weight_np = np.random.random(size=(3, )).astype(np.float64) weight_np = np.random.random(size=(3, )).astype(np.float64)
prog = fluid.Program() prog = fluid.Program()
...@@ -410,6 +419,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -410,6 +419,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_2D_with_weight_mean_cpu(self): def test_NLLLoss_2D_with_weight_mean_cpu(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64) input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64) label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64)
weight_np = np.random.random(size=(3, )).astype(np.float64) weight_np = np.random.random(size=(3, )).astype(np.float64)
prog = fluid.Program() prog = fluid.Program()
...@@ -450,6 +460,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -450,6 +460,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_2D_with_weight_sum(self): def test_NLLLoss_2D_with_weight_sum(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64) input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64) label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64)
weight_np = np.random.random(size=(3, )).astype(np.float64) weight_np = np.random.random(size=(3, )).astype(np.float64)
prog = fluid.Program() prog = fluid.Program()
...@@ -492,6 +503,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -492,6 +503,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_in_dims_not_2or4_mean(self): def test_NLLLoss_in_dims_not_2or4_mean(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(5, 3, 5, 5, 5)).astype(np.float64) input_np = np.random.random(size=(5, 3, 5, 5, 5)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64) label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64)
prog = fluid.Program() prog = fluid.Program()
startup_prog = fluid.Program() startup_prog = fluid.Program()
...@@ -533,6 +545,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -533,6 +545,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_in_dims_not_2or4_with_weight_mean(self): def test_NLLLoss_in_dims_not_2or4_with_weight_mean(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(5, 3, 5, 5, 5)).astype(np.float64) input_np = np.random.random(size=(5, 3, 5, 5, 5)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64) label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64)
weight_np = np.random.random(size=(3, )).astype(np.float64) weight_np = np.random.random(size=(3, )).astype(np.float64)
prog = fluid.Program() prog = fluid.Program()
...@@ -580,6 +593,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -580,6 +593,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_in_dims_not_2or4_with_weight_sum(self): def test_NLLLoss_in_dims_not_2or4_with_weight_sum(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(5, 3, 5, 5, 5)).astype(np.float64) input_np = np.random.random(size=(5, 3, 5, 5, 5)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64) label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64)
weight_np = np.random.random(size=(3, )).astype(np.float64) weight_np = np.random.random(size=(3, )).astype(np.float64)
prog = fluid.Program() prog = fluid.Program()
...@@ -630,6 +644,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -630,6 +644,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_in_dims_not_2or4_with_weight_no_reduce(self): def test_NLLLoss_in_dims_not_2or4_with_weight_no_reduce(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(5, 3, 5, 5, 5)).astype(np.float64) input_np = np.random.random(size=(5, 3, 5, 5, 5)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64) label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64)
weight_np = np.random.random(size=(3, )).astype(np.float64) weight_np = np.random.random(size=(3, )).astype(np.float64)
prog = fluid.Program() prog = fluid.Program()
...@@ -681,6 +696,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -681,6 +696,7 @@ class TestNLLLoss(unittest.TestCase):
def test_NLLLoss_in_dims_not_2or4_with_weight_no_reduce_cpu(self): def test_NLLLoss_in_dims_not_2or4_with_weight_no_reduce_cpu(self):
np.random.seed(200) np.random.seed(200)
input_np = np.random.random(size=(5, 3, 5, 5, 5)).astype(np.float64) input_np = np.random.random(size=(5, 3, 5, 5, 5)).astype(np.float64)
np.random.seed(200)
label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64) label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64)
weight_np = np.random.random(size=(3, )).astype(np.float64) weight_np = np.random.random(size=(3, )).astype(np.float64)
prog = fluid.Program() prog = fluid.Program()
...@@ -736,11 +752,13 @@ class TestNLLLossOp1DWithReduce(OpTest): ...@@ -736,11 +752,13 @@ class TestNLLLossOp1DWithReduce(OpTest):
np.random.seed(200) np.random.seed(200)
input_np = np.random.uniform(0.1, 0.8, input_np = np.random.uniform(0.1, 0.8,
self.input_shape).astype("float64") self.input_shape).astype("float64")
np.random.seed(200)
label_np = np.random.randint(0, self.input_shape[1], label_np = np.random.randint(0, self.input_shape[1],
self.label_shape).astype("int64") self.label_shape).astype("int64")
output_np, total_weight_np = nll_loss_1d(input_np, label_np) output_np, total_weight_np = nll_loss_1d(input_np, label_np)
self.inputs = {'X': input_np, 'Label': label_np} self.inputs = {'X': input_np, 'Label': label_np}
if self.with_weight: if self.with_weight:
np.random.seed(200)
weight_np = np.random.uniform(0.1, 0.8, weight_np = np.random.uniform(0.1, 0.8,
self.input_shape[1]).astype("float64") self.input_shape[1]).astype("float64")
output_np, total_weight_np = nll_loss_1d( output_np, total_weight_np = nll_loss_1d(
...@@ -778,12 +796,14 @@ class TestNLLLossOp1DNoReduce(OpTest): ...@@ -778,12 +796,14 @@ class TestNLLLossOp1DNoReduce(OpTest):
np.random.seed(200) np.random.seed(200)
input_np = np.random.uniform(0.1, 0.8, input_np = np.random.uniform(0.1, 0.8,
self.input_shape).astype("float64") self.input_shape).astype("float64")
np.random.seed(200)
label_np = np.random.randint(0, self.input_shape[1], label_np = np.random.randint(0, self.input_shape[1],
self.label_shape).astype("int64") self.label_shape).astype("int64")
output_np = nll_loss_1d(input_np, label_np, reduction='none') output_np = nll_loss_1d(input_np, label_np, reduction='none')
total_weight_np = np.array([0]).astype('float64') total_weight_np = np.array([0]).astype('float64')
self.inputs = {'X': input_np, 'Label': label_np} self.inputs = {'X': input_np, 'Label': label_np}
if self.with_weight: if self.with_weight:
np.random.seed(200)
weight_np = np.random.uniform(0.1, 0.8, weight_np = np.random.uniform(0.1, 0.8,
self.input_shape[1]).astype("float64") self.input_shape[1]).astype("float64")
output_np, total_weight_np = nll_loss_1d( output_np, total_weight_np = nll_loss_1d(
...@@ -865,12 +885,14 @@ class TestNLLLossOp2DNoReduce(OpTest): ...@@ -865,12 +885,14 @@ class TestNLLLossOp2DNoReduce(OpTest):
np.random.seed(200) np.random.seed(200)
input_np = np.random.uniform(0.1, 0.8, input_np = np.random.uniform(0.1, 0.8,
self.input_shape).astype("float64") self.input_shape).astype("float64")
np.random.seed(200)
label_np = np.random.randint(0, self.input_shape[1], label_np = np.random.randint(0, self.input_shape[1],
self.label_shape).astype("int64") self.label_shape).astype("int64")
output_np = nll_loss_2d(input_np, label_np, reduction='none') output_np = nll_loss_2d(input_np, label_np, reduction='none')
total_weight_np = np.array([0]).astype('float64') total_weight_np = np.array([0]).astype('float64')
self.inputs = {'X': input_np, 'Label': label_np} self.inputs = {'X': input_np, 'Label': label_np}
if self.with_weight: if self.with_weight:
np.random.seed(200)
weight_np = np.random.uniform(0.1, 0.8, weight_np = np.random.uniform(0.1, 0.8,
self.input_shape[1]).astype("float64") self.input_shape[1]).astype("float64")
output_np, total_weight_np = nll_loss_2d( output_np, total_weight_np = nll_loss_2d(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册