|
|
@ -32,6 +32,7 @@ class TestLinspaceOpCommonCase(OpTest):
|
|
|
|
'Stop': np.array([10]).astype(dtype),
|
|
|
|
'Stop': np.array([10]).astype(dtype),
|
|
|
|
'Num': np.array([11]).astype('int32')
|
|
|
|
'Num': np.array([11]).astype('int32')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.attrs = {'dtype': int(core.VarDesc.VarType.FP32)}
|
|
|
|
|
|
|
|
|
|
|
|
self.outputs = {'Out': np.arange(0, 11).astype(dtype)}
|
|
|
|
self.outputs = {'Out': np.arange(0, 11).astype(dtype)}
|
|
|
|
|
|
|
|
|
|
|
@ -48,6 +49,7 @@ class TestLinspaceOpReverseCase(OpTest):
|
|
|
|
'Stop': np.array([0]).astype(dtype),
|
|
|
|
'Stop': np.array([0]).astype(dtype),
|
|
|
|
'Num': np.array([11]).astype('int32')
|
|
|
|
'Num': np.array([11]).astype('int32')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.attrs = {'dtype': int(core.VarDesc.VarType.FP32)}
|
|
|
|
|
|
|
|
|
|
|
|
self.outputs = {'Out': np.arange(10, -1, -1).astype(dtype)}
|
|
|
|
self.outputs = {'Out': np.arange(10, -1, -1).astype(dtype)}
|
|
|
|
|
|
|
|
|
|
|
@ -64,6 +66,7 @@ class TestLinspaceOpNumOneCase(OpTest):
|
|
|
|
'Stop': np.array([0]).astype(dtype),
|
|
|
|
'Stop': np.array([0]).astype(dtype),
|
|
|
|
'Num': np.array([1]).astype('int32')
|
|
|
|
'Num': np.array([1]).astype('int32')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.attrs = {'dtype': int(core.VarDesc.VarType.FP32)}
|
|
|
|
|
|
|
|
|
|
|
|
self.outputs = {'Out': np.array(10, dtype=dtype)}
|
|
|
|
self.outputs = {'Out': np.array(10, dtype=dtype)}
|
|
|
|
|
|
|
|
|
|
|
@ -72,6 +75,26 @@ class TestLinspaceOpNumOneCase(OpTest):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestLinspaceAPI(unittest.TestCase):
|
|
|
|
class TestLinspaceAPI(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_variable_input1(self):
|
|
|
|
|
|
|
|
start = paddle.full(shape=[1], fill_value=0, dtype='float32')
|
|
|
|
|
|
|
|
stop = paddle.full(shape=[1], fill_value=10, dtype='float32')
|
|
|
|
|
|
|
|
num = paddle.full(shape=[1], fill_value=5, dtype='int32')
|
|
|
|
|
|
|
|
out = paddle.linspace(start, stop, num, dtype='float32')
|
|
|
|
|
|
|
|
exe = fluid.Executor(place=fluid.CPUPlace())
|
|
|
|
|
|
|
|
res = exe.run(fluid.default_main_program(), fetch_list=[out])
|
|
|
|
|
|
|
|
np_res = np.linspace(0, 10, 5, dtype='float32')
|
|
|
|
|
|
|
|
self.assertEqual((res == np_res).all(), True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_variable_input2(self):
|
|
|
|
|
|
|
|
paddle.disable_static()
|
|
|
|
|
|
|
|
start = paddle.full(shape=[1], fill_value=0, dtype='float32')
|
|
|
|
|
|
|
|
stop = paddle.full(shape=[1], fill_value=10, dtype='float32')
|
|
|
|
|
|
|
|
num = paddle.full(shape=[1], fill_value=5, dtype='int32')
|
|
|
|
|
|
|
|
out = paddle.linspace(start, stop, num, dtype='float32')
|
|
|
|
|
|
|
|
np_res = np.linspace(0, 10, 5, dtype='float32')
|
|
|
|
|
|
|
|
self.assertEqual((out.numpy() == np_res).all(), True)
|
|
|
|
|
|
|
|
paddle.enable_static()
|
|
|
|
|
|
|
|
|
|
|
|
def test_dtype(self):
|
|
|
|
def test_dtype(self):
|
|
|
|
out_1 = paddle.linspace(0, 10, 5, dtype='float32')
|
|
|
|
out_1 = paddle.linspace(0, 10, 5, dtype='float32')
|
|
|
|
out_2 = paddle.linspace(0, 10, 5, dtype=np.float32)
|
|
|
|
out_2 = paddle.linspace(0, 10, 5, dtype=np.float32)
|
|
|
@ -89,10 +112,16 @@ class TestLinspaceAPI(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
|
|
def test_imperative(self):
|
|
|
|
def test_imperative(self):
|
|
|
|
paddle.disable_static()
|
|
|
|
paddle.disable_static()
|
|
|
|
out = paddle.linspace(0, 10, 5, dtype='float32')
|
|
|
|
out1 = paddle.linspace(0, 10, 5, dtype='float32')
|
|
|
|
np_out = np.linspace(0, 10, 5, dtype='float32')
|
|
|
|
np_out1 = np.linspace(0, 10, 5, dtype='float32')
|
|
|
|
|
|
|
|
out2 = paddle.linspace(0, 10, 5, dtype='int32')
|
|
|
|
|
|
|
|
np_out2 = np.linspace(0, 10, 5, dtype='int32')
|
|
|
|
|
|
|
|
out3 = paddle.linspace(0, 10, 200, dtype='int32')
|
|
|
|
|
|
|
|
np_out3 = np.linspace(0, 10, 200, dtype='int32')
|
|
|
|
paddle.enable_static()
|
|
|
|
paddle.enable_static()
|
|
|
|
self.assertEqual((out.numpy() == np_out).all(), True)
|
|
|
|
self.assertEqual((out1.numpy() == np_out1).all(), True)
|
|
|
|
|
|
|
|
self.assertEqual((out2.numpy() == np_out2).all(), True)
|
|
|
|
|
|
|
|
self.assertEqual((out3.numpy() == np_out3).all(), True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestLinspaceOpError(unittest.TestCase):
|
|
|
|
class TestLinspaceOpError(unittest.TestCase):
|
|
|
@ -100,7 +129,12 @@ class TestLinspaceOpError(unittest.TestCase):
|
|
|
|
with program_guard(Program(), Program()):
|
|
|
|
with program_guard(Program(), Program()):
|
|
|
|
|
|
|
|
|
|
|
|
def test_dtype():
|
|
|
|
def test_dtype():
|
|
|
|
fluid.layers.linspace(0, 10, 1, dtype="int32")
|
|
|
|
fluid.layers.linspace(0, 10, 1, dtype="int8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, test_dtype)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_dtype():
|
|
|
|
|
|
|
|
fluid.layers.linspace(0, 10, 1.33, dtype="int32")
|
|
|
|
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, test_dtype)
|
|
|
|
self.assertRaises(TypeError, test_dtype)
|
|
|
|
|
|
|
|
|
|
|
@ -120,20 +154,20 @@ class TestLinspaceOpError(unittest.TestCase):
|
|
|
|
self.assertRaises(TypeError, test_step_dtype)
|
|
|
|
self.assertRaises(TypeError, test_step_dtype)
|
|
|
|
|
|
|
|
|
|
|
|
def test_start_dtype():
|
|
|
|
def test_start_dtype():
|
|
|
|
start = fluid.data(shape=[1], type="int32", name="start")
|
|
|
|
start = fluid.data(shape=[1], dtype="int32", name="start")
|
|
|
|
fluid.layers.linspace(start, 10, 1, dtype="float32")
|
|
|
|
fluid.layers.linspace(start, 10, 1, dtype="float32")
|
|
|
|
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, test_start_dtype)
|
|
|
|
self.assertRaises(TypeError, test_start_dtype)
|
|
|
|
|
|
|
|
|
|
|
|
def test_end_dtype():
|
|
|
|
def test_end_dtype():
|
|
|
|
end = fluid.data(shape=[1], type="int32", name="end")
|
|
|
|
end = fluid.data(shape=[1], dtype="int32", name="end")
|
|
|
|
fluid.layers.linspace(0, end, 1, dtype="float32")
|
|
|
|
fluid.layers.linspace(0, end, 1, dtype="float32")
|
|
|
|
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, test_end_dtype)
|
|
|
|
self.assertRaises(TypeError, test_end_dtype)
|
|
|
|
|
|
|
|
|
|
|
|
def test_step_dtype():
|
|
|
|
def test_num_dtype():
|
|
|
|
step = fluid.data(shape=[1], type="int32", name="step")
|
|
|
|
num = fluid.data(shape=[1], dtype="int32", name="step")
|
|
|
|
fluid.layers.linspace(0, 10, step, dtype="float32")
|
|
|
|
fluid.layers.linspace(0, 10, num, dtype="float32")
|
|
|
|
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, test_step_dtype)
|
|
|
|
self.assertRaises(TypeError, test_step_dtype)
|
|
|
|
|
|
|
|
|
|
|
|