test_variable.py 1.7 KB
Newer Older
Y
Yu Yang 已提交
1
import unittest
2
from paddle.v2.fluid.framework import g_main_program, Program, convert_np_dtype_to_dtype_
Q
Qiao Longfei 已提交
3
import paddle.v2.fluid.core as core
Y
Yu Yang 已提交
4 5 6 7 8 9
import numpy as np


class TestVariable(unittest.TestCase):
    def test_np_dtype_convert(self):
        DT = core.DataType
10
        convert = convert_np_dtype_to_dtype_
Y
Yu Yang 已提交
11 12 13 14 15 16 17 18 19
        self.assertEqual(DT.FP32, convert(np.float32))
        self.assertEqual(DT.FP16, convert("float16"))
        self.assertEqual(DT.FP64, convert("float64"))
        self.assertEqual(DT.INT32, convert("int32"))
        self.assertEqual(DT.INT16, convert("int16"))
        self.assertEqual(DT.INT64, convert("int64"))
        self.assertEqual(DT.BOOL, convert("bool"))
        self.assertRaises(ValueError, lambda: convert("int8"))

Y
Yu Yang 已提交
20
    def test_var(self):
21
        b = g_main_program.current_block()
Y
Yu Yang 已提交
22 23
        w = b.create_var(
            dtype="float64", shape=[784, 100], lod_level=0, name="fc.w")
24
        self.assertNotEqual(str(w), "")
Y
Yu Yang 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38
        self.assertEqual(core.DataType.FP64, w.data_type)
        self.assertEqual((784, 100), w.shape)
        self.assertEqual("fc.w", w.name)
        self.assertEqual(0, w.lod_level)

        w = b.create_var(name='fc.w')
        self.assertEqual(core.DataType.FP64, w.data_type)
        self.assertEqual((784, 100), w.shape)
        self.assertEqual("fc.w", w.name)
        self.assertEqual(0, w.lod_level)

        self.assertRaises(ValueError,
                          lambda: b.create_var(name="fc.w", shape=(24, 100)))

Y
Yu Yang 已提交
39 40 41 42 43 44 45
    def test_step_scopes(self):
        prog = Program()
        b = prog.current_block()
        var = b.create_var(
            name='step_scopes', type=core.VarDesc.VarType.STEP_SCOPES)
        self.assertEqual(core.VarDesc.VarType.STEP_SCOPES, var.type)

Y
Yu Yang 已提交
46 47 48

if __name__ == '__main__':
    unittest.main()