示例#1
0
 def func_dygraph(self):
     for place in self.places:
         paddle.disable_static()
         x = paddle.to_tensor(self.input, place=place)
         if self.prepend is not None:
             self.prepend = paddle.to_tensor(self.prepend, place=place)
         if self.append is not None:
             self.append = paddle.to_tensor(self.append, place=place)
         out = paddle.diff(x,
                           n=self.n,
                           axis=self.axis,
                           prepend=self.prepend,
                           append=self.append)
         self.assertTrue((out.numpy() == self.output).all(), True)
示例#2
0
 def func_grad(self):
     for place in self.places:
         x = paddle.to_tensor(self.input, place=place, stop_gradient=False)
         if self.prepend is not None:
             self.prepend = paddle.to_tensor(self.prepend, place=place)
         if self.append is not None:
             self.append = paddle.to_tensor(self.append, place=place)
         out = paddle.diff(x,
                           n=self.n,
                           axis=self.axis,
                           prepend=self.prepend,
                           append=self.append)
         try:
             out.backward()
             x_grad = x.grad
         except:
             raise RuntimeError("Check Diff Gradient Failed")
示例#3
0
    def test_static(self):
        paddle.enable_static()
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for place in places:
            with fluid.program_guard(fluid.Program(), fluid.Program()):
                x = paddle.fluid.data(
                    name="input",
                    shape=self.input.shape,
                    dtype=self.input.dtype)
                has_pend = False
                prepend = None
                append = None
                if self.prepend is not None:
                    has_pend = True
                    prepend = paddle.fluid.data(
                        name="prepend",
                        shape=self.prepend.shape,
                        dtype=self.prepend.dtype)
                if self.append is not None:
                    has_pend = True
                    append = paddle.fluid.data(
                        name="append",
                        shape=self.append.shape,
                        dtype=self.append.dtype)

                exe = fluid.Executor(place)
                out = paddle.diff(
                    x, n=self.n, axis=self.axis, prepend=prepend, append=append)
                fetches = exe.run(fluid.default_main_program(),
                                  feed={
                                      "input": self.input,
                                      "prepend": self.prepend,
                                      "append": self.append
                                  },
                                  fetch_list=[out])
                self.assertTrue((fetches[0] == self.output).all(), True)