add pad func (#26106)

* add pad func

* add pad

* test=develop, add pad op and apis

* restore pad2d

* test=develop, fix paddl declare

* fix pad interface

* test=develop, fix pad

* test=develop, add all pad api and cos_sim

* test=develop, remove padding default value

* test=develop, rename var to tensor

* test=develop, add more tests

* test=develop, rename tovar to totensor

* test=develop, fix init

* test=develop, add more test

* test=develop, add more tests
revert-24895-update_cub
littletomatodonkey 5 years ago committed by GitHub
parent a39bd3acc9
commit bcf03273f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -0,0 +1,121 @@
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import numpy as np
from op_test import OpTest
import paddle
import paddle.nn as nn
import paddle.nn.functional as F
import paddle.fluid.core as core
from paddle.fluid import Program, program_guard, Executor, default_main_program
class TestCosineSimilarityAPI(unittest.TestCase):
def setUp(self):
self.places = [paddle.CPUPlace()]
if core.is_compiled_with_cuda():
self.places.append(paddle.CUDAPlace(0))
def _get_numpy_out(self, x1, x2, dim=1, eps=1e-8):
w12 = np.sum(x1 * x2, axis=dim)
w1 = np.sum(x1 * x1, axis=dim)
w2 = np.sum(x2 * x2, axis=dim)
n12 = np.sqrt(np.clip(w1 * w2, eps * eps, None))
cos_sim = w12 / n12
return cos_sim
def check_static_result(self, place):
paddle.enable_static()
with program_guard(Program(), Program()):
shape = [10, 15]
dim = 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.data(name="x1", shape=shape)
x2 = paddle.data(name="x2", shape=shape)
result = F.cosine_similarity(x1, x2, dim=dim, 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, dim=dim, eps=eps)
self.assertTrue(np.allclose(fetches[0], np_out))
def test_static(self):
for place in self.places:
self.check_static_result(place=place)
def test_dygraph_1(self):
paddle.disable_static()
shape = [10, 15]
dim = 1
eps = 1e-8
np.random.seed(1)
np_x1 = np.random.rand(*shape).astype(np.float32)
np_x2 = np.random.rand(*shape).astype(np.float32)
np_out = self._get_numpy_out(np_x1, np_x2, dim=dim, eps=eps)
tesnor_x1 = paddle.to_variable(np_x1)
tesnor_x2 = paddle.to_variable(np_x2)
y = F.cosine_similarity(tesnor_x1, tesnor_x2, dim=dim, eps=eps)
self.assertTrue(np.allclose(y.numpy(), np_out))
def test_dygraph_2(self):
paddle.disable_static()
shape = [12, 13]
dim = 0
eps = 1e-6
np.random.seed(1)
np_x1 = np.random.rand(*shape).astype(np.float32)
np_x2 = np.random.rand(*shape).astype(np.float32)
np_out = self._get_numpy_out(np_x1, np_x2, dim=dim, eps=eps)
tesnor_x1 = paddle.to_variable(np_x1)
tesnor_x2 = paddle.to_variable(np_x2)
y = F.cosine_similarity(tesnor_x1, tesnor_x2, dim=dim, eps=eps)
self.assertTrue(np.allclose(y.numpy(), np_out))
def test_dygraph_3(self):
paddle.disable_static()
shape1 = [10, 12, 10]
shape2 = [10, 1, 10]
dim = 2
eps = 1e-6
np.random.seed(1)
np_x1 = np.random.rand(*shape1).astype(np.float32)
np_x2 = np.random.rand(*shape2).astype(np.float32)
np_out = self._get_numpy_out(np_x1, np_x2, dim=dim, eps=eps)
tesnor_x1 = paddle.to_variable(np_x1)
tesnor_x2 = paddle.to_variable(np_x2)
y = F.cosine_similarity(tesnor_x1, tesnor_x2, dim=dim, eps=eps)
self.assertTrue(np.allclose(y.numpy(), np_out))
if __name__ == '__main__':
unittest.main()

File diff suppressed because it is too large Load Diff

@ -65,6 +65,16 @@ from .layer.activation import HSigmoid #DEFINE_ALIAS
from .layer.common import BilinearTensorProduct #DEFINE_ALIAS
from .layer.common import Pool2D #DEFINE_ALIAS
from .layer.common import Pad2D #DEFINE_ALIAS
from .layer.common import ReflectionPad1d #DEFINE_ALIAS
from .layer.common import ReplicationPad1d #DEFINE_ALIAS
from .layer.common import ConstantPad1d #DEFINE_ALIAS
from .layer.common import ReflectionPad2d #DEFINE_ALIAS
from .layer.common import ReplicationPad2d #DEFINE_ALIAS
from .layer.common import ConstantPad2d #DEFINE_ALIAS
from .layer.common import ZeroPad2d #DEFINE_ALIAS
from .layer.common import ReplicationPad3d #DEFINE_ALIAS
from .layer.common import ConstantPad3d #DEFINE_ALIAS
from .layer.common import CosineSimilarity #DEFINE_ALIAS
from .layer.common import Embedding #DEFINE_ALIAS
from .layer.common import Linear #DEFINE_ALIAS
from .layer.common import Flatten #DEFINE_ALIAS

@ -58,6 +58,7 @@ from .common import one_hot #DEFINE_ALIAS
from .common import pad #DEFINE_ALIAS
from .common import pad_constant_like #DEFINE_ALIAS
from .common import pad2d #DEFINE_ALIAS
from .common import cosine_similarity #DEFINE_ALIAS
from .common import unfold #DEFINE_ALIAS
# from .common import bilinear_tensor_product #DEFINE_ALIAS
from .common import assign #DEFINE_ALIAS

File diff suppressed because it is too large Load Diff

@ -38,6 +38,16 @@ from .activation import HSigmoid #DEFINE_ALIAS
from .common import BilinearTensorProduct #DEFINE_ALIAS
from .common import Pool2D #DEFINE_ALIAS
from .common import Pad2D #DEFINE_ALIAS
from .common import ReflectionPad1d #DEFINE_ALIAS
from .common import ReplicationPad1d #DEFINE_ALIAS
from .common import ConstantPad1d #DEFINE_ALIAS
from .common import ReflectionPad2d #DEFINE_ALIAS
from .common import ReplicationPad2d #DEFINE_ALIAS
from .common import ConstantPad2d #DEFINE_ALIAS
from .common import ZeroPad2d #DEFINE_ALIAS
from .common import ReplicationPad3d #DEFINE_ALIAS
from .common import ConstantPad3d #DEFINE_ALIAS
from .common import CosineSimilarity #DEFINE_ALIAS
from .common import Embedding #DEFINE_ALIAS
from .common import Linear #DEFINE_ALIAS
from .common import Flatten #DEFINE_ALIAS

File diff suppressed because it is too large Load Diff

@ -111,6 +111,7 @@ from .math import cumsum #DEFINE_ALIAS
from .math import elementwise_add #DEFINE_ALIAS
from .math import elementwise_div #DEFINE_ALIAS
from .math import elementwise_floordiv #DEFINE_ALIAS
from .math import elementwise_mul #DEFINE_ALIAS
from .math import elementwise_mod #DEFINE_ALIAS
from .math import elementwise_pow #DEFINE_ALIAS
from .math import elementwise_sub #DEFINE_ALIAS

Loading…
Cancel
Save