Exemplo n.º 1
0
    def check_static_result_4(self, place):
        paddle.enable_static()
        with program_guard(Program(), Program()):
            input_shape = (2, 3, 4, 5, 6)
            pad = [1, 2, 1, 1, 3, 4]
            mode = "circular"
            input_data = np.random.rand(*input_shape).astype(np.float32)
            x = paddle.fluid.data(name="x", shape=input_shape)
            result1 = F.pad(x=x, pad=pad, mode=mode, data_format="NCDHW")
            result2 = F.pad(x=x, pad=pad, mode=mode, data_format="NDHWC")
            exe = Executor(place)
            fetches = exe.run(default_main_program(),
                              feed={"x": input_data},
                              fetch_list=[result1, result2])

            np_out1 = self._get_numpy_out(input_data,
                                          pad,
                                          mode,
                                          data_format="NCDHW")
            np_out2 = self._get_numpy_out(input_data,
                                          pad,
                                          mode,
                                          data_format="NDHWC")
            self.assertTrue(np.allclose(fetches[0], np_out1))
            self.assertTrue(np.allclose(fetches[1], np_out2))
Exemplo n.º 2
0
 def test_reflect_3():
     input_shape = (1, 2, 3, 4, 5)
     data = np.random.rand(*input_shape).astype(np.float32)
     x = paddle.fluid.data(name="x", shape=input_shape)
     y = F.pad(x, pad=[1, 1, 1, 1, 2, 3], value=1, mode='reflect')
     place = paddle.CPUPlace()
     exe = Executor(place)
     outputs = exe.run(feed={'x': data}, fetch_list=[y.name])
Exemplo n.º 3
0
    def check_static_result_1(self, place):
        paddle.enable_static()
        with program_guard(Program(), Program()):
            input_shape = (1, 2, 3, 4, 5)
            pad = [1, 2, 1, 1, 3, 4]
            mode = "constant"
            value = 100
            input_data = np.random.rand(*input_shape).astype(np.float32)
            x = paddle.fluid.data(name="x", shape=input_shape)
            result = F.pad(x=x,
                           pad=pad,
                           value=value,
                           mode=mode,
                           data_format="NCDHW")
            exe = Executor(place)
            fetches = exe.run(default_main_program(),
                              feed={"x": input_data},
                              fetch_list=[result])

            np_out = self._get_numpy_out(input_data, pad, mode, value)
            self.assertTrue(np.allclose(fetches[0], np_out))
Exemplo n.º 4
0
    def check_static_result(self, place):
        paddle.enable_static()

        with program_guard(Program(), Program()):
            shape = [10, 15]
            axis = 1
            eps = 1e-8
            np.random.seed(0)
            np_x1 = np.random.rand(*shape).astype(np.float32)
            np_x2 = np.random.rand(*shape).astype(np.float32)

            x1 = paddle.fluid.data(name="x1", shape=shape)
            x2 = paddle.fluid.data(name="x2", shape=shape)
            result = F.cosine_similarity(x1, x2, axis=axis, eps=eps)
            exe = Executor(place)
            fetches = exe.run(default_main_program(),
                              feed={"x1": np_x1,
                                    "x2": np_x2},
                              fetch_list=[result])

            np_out = self._get_numpy_out(np_x1, np_x2, axis=axis, eps=eps)
            self.assertTrue(np.allclose(fetches[0], np_out))
Exemplo n.º 5
0
    def test_static(self):
        paddle.enable_static()
        self.place = fluid.NPUPlace(
            0) if fluid.core.is_compiled_with_npu() else fluid.CPUPlace()
        with program_guard(Program(), Program()):
            input_shape = (1, 2, 3, 4, 5)
            pad = [1, 2, 1, 1, 3, 4]
            mode = "constant"
            value = 0
            input_data = np.random.rand(*input_shape).astype(np.float32)
            x = paddle.fluid.data(name="x", shape=input_shape)
            result1 = F.pad(x=x,
                            pad=pad,
                            value=value,
                            mode=mode,
                            data_format="NCDHW")
            result2 = F.pad(x=x,
                            pad=pad,
                            value=value,
                            mode=mode,
                            data_format="NDHWC")
            exe = Executor(self.place)
            fetches = exe.run(default_main_program(),
                              feed={"x": input_data},
                              fetch_list=[result1, result2])

            np_out1 = self._get_numpy_out(input_data,
                                          pad,
                                          mode,
                                          value,
                                          data_format="NCDHW")
            np_out2 = self._get_numpy_out(input_data,
                                          pad,
                                          mode,
                                          value,
                                          data_format="NDHWC")
            self.assertTrue(np.allclose(fetches[0], np_out1))
            self.assertTrue(np.allclose(fetches[1], np_out2))