test_assign_op.py 2.7 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15 16
from __future__ import print_function

17
import op_test
18
import numpy as np
Y
Yu Yang 已提交
19
import unittest
20 21 22 23
import paddle.fluid.core as core
from paddle.fluid.op import Operator
import paddle.fluid as fluid
from paddle.fluid import compiler, Program, program_guard
Y
Yu Yang 已提交
24 25 26 27 28


class TestAssignOp(op_test.OpTest):
    def setUp(self):
        self.op_type = "assign"
29
        x = np.random.random(size=(100, 10)).astype('float64')
Y
Yu Yang 已提交
30 31 32 33 34 35 36 37 38 39
        self.inputs = {'X': x}
        self.outputs = {'Out': x}

    def test_forward(self):
        self.check_output()

    def test_backward(self):
        self.check_grad(['X'], 'Out')


40 41 42 43 44 45 46 47 48 49 50 51 52 53
class TestAssignFP16Op(op_test.OpTest):
    def setUp(self):
        self.op_type = "assign"
        x = np.random.random(size=(100, 10)).astype('float16')
        self.inputs = {'X': x}
        self.outputs = {'Out': x}

    def test_forward(self):
        self.check_output()

    def test_backward(self):
        self.check_grad(['X'], 'Out')


54
class TestAssignOpError(unittest.TestCase):
55 56 57 58 59 60
    def test_errors(self):
        with program_guard(Program(), Program()):
            # The type of input must be Variable or numpy.ndarray.
            x1 = fluid.create_lod_tensor(
                np.array([[-1]]), [[1]], fluid.CPUPlace())
            self.assertRaises(TypeError, fluid.layers.assign, x1)
61 62
            # When the type of input is Variable, the dtype of input must be float16, float32, float64, int32, int64, bool.
            x3 = fluid.layers.data(name='x3', shape=[4], dtype="uint8")
63 64
            self.assertRaises(TypeError, fluid.layers.assign, x3)
            # When the type of input is numpy.ndarray, the dtype of input must be float32, int32.
65 66 67
            x4 = np.array([[2.5, 2.5]], dtype='bool')
            self.assertRaises(TypeError, fluid.layers.assign, x4)
            x5 = np.array([[2.5, 2.5]], dtype='float64')
68
            self.assertRaises(TypeError, fluid.layers.assign, x5)
69
            x6 = np.array([[2.5, 2.5]], dtype='int64')
70
            self.assertRaises(TypeError, fluid.layers.assign, x6)
71
            x7 = np.array([[2.5, 2.5]], dtype='uint8')
72 73 74
            self.assertRaises(TypeError, fluid.layers.assign, x7)


Y
Yu Yang 已提交
75 76
if __name__ == '__main__':
    unittest.main()