test_feed_fetch_method.py 938 字节
Newer Older
Q
Qiao Longfei 已提交
1
import paddle.v2.fluid.core as core
Q
init  
qijun 已提交
2 3 4 5 6 7
import unittest
import numpy as np


class TestFeedFetch(unittest.TestCase):
    def test_feed_fetch(self):
Y
Yu Yang 已提交
8
        scope = core.Scope()
Q
init  
qijun 已提交
9
        place = core.CPUPlace()
Q
qijun 已提交
10
        input_array = np.ones((4, 4, 6)).astype("float32")
Q
init  
qijun 已提交
11 12
        input_array[0, 0, 0] = 3
        input_array[3, 3, 5] = 10
Q
qijun 已提交
13
        input_tensor = core.LoDTensor([[0, 2, 4]])
Q
init  
qijun 已提交
14 15
        input_tensor.set(input_array, place)

Y
Yu Yang 已提交
16
        core.set_feed_variable(scope, input_tensor, "feed", 0)
Q
qijun 已提交
17

Y
Yu Yang 已提交
18
        output_tensor = core.get_fetch_variable(scope, "feed", 0)
Q
qijun 已提交
19 20 21 22 23 24 25 26 27 28

        output_lod = output_tensor.lod()
        self.assertEqual(0, output_lod[0][0])
        self.assertEqual(2, output_lod[0][1])
        self.assertEqual(4, output_lod[0][2])

        output_array = np.array(output_tensor)
        self.assertEqual(3, output_array[0, 0, 0])
        self.assertEqual(10, output_array[3, 3, 5])

Q
init  
qijun 已提交
29 30 31

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