def execute():
    startup_program = fluid.Program()
    main_program = fluid.Program()
    with fluid.program_guard(main_program, startup_program):
        # prepare input
        np_x = np.array([1, 2, 3, 4, 5], dtype='int32')

        # add feed var
        x = data_layer_not_check(name='x',
                                 shape=list(np_x.shape),
                                 dtype=str(np_x.dtype))

        # build net
        result = static_func(x)

        print(main_program)

        # prepare exe
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)

        # run
        # exe.run(startup_program)
        out, = exe.run(main_program, feed={'x': np_x}, fetch_list=[result])
        print(out[0])
 def test_feed_mismatch_shape(self):
     main_program = fluid.Program()
     with fluid.program_guard(main_program):
         d = data_layer_not_check(name="d", shape=(1, 2, 3))
     feed_in_data = np.random.uniform(size=[1, 2, 4]).astype(np.float32)
     place = fluid.CUDAPlace(
         0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace()
     exe = fluid.Executor(place)
     ret = exe.run(main_program,
                   feed={d.name: feed_in_data},
                   fetch_list=[d.name])
     self.assertTrue(np.allclose(ret, feed_in_data))
Esempio n. 3
0
 def _add_feed_layers(self, args, kwargs):
     """
     Adds `fluid.data` if the input `numpy.ndarray` is converted into `Variable`
     by `to_variable()`, it makes program to be executed dynamically.
     """
     if not self._feed_name_to_idx:
         self._feed_name_to_idx = self._get_name_to_idx(self._forward_func)
     with framework.program_guard(self._main_program,
                                  self._startup_program):
         for feed_name, idx in self.feed_name_to_idx.items():
             batch_data = args[idx]
             assert isinstance(
                 batch_data, numpy.ndarray
             ), "Input {} should be numpy.ndarray, but received {}.".format(
                 feed_name, type(batch_data))
             feed_layer = data_layer_not_check(name=feed_name,
                                               shape=list(batch_data.shape),
                                               dtype=str(batch_data.dtype))
             self._inputs.append(feed_layer)
 def test_create_none_shape(self):
     main_program = fluid.Program()
     with fluid.program_guard(main_program):
         d = data_layer_not_check(name="d", shape=(None, -1, 3))
         self.assertEqual(d.shape, (-1, -1, 3))
         self.assertEqual(d.name, "d")