def tensor_array_pop(array, idx): assert isinstance(idx, int) def cond(i, new_array): return less_than(i, arr_len) def body(i, new_array): item = array_read(array=array, i=i) array_write(item, array_length(new_array), new_array) i = increment(i) return i, new_array arr_len = array_length(array) if idx < 0: idx = idx + arr_len else: idx = fill_constant(shape=[1], dtype="int64", value=idx) pop_item = array_read(array, idx) new_array = slice_tensor_array(array, 0, idx) i = idx + 1 _, new_array = while_loop(cond, body, [i, new_array]) assign(input=new_array, output=array) return pop_item
def static_func(x): x = fluid.layers.assign(x) iter_num = fluid.layers.fill_constant(shape=[1], value=3, dtype='int32') a = fluid.layers.create_array(dtype='float32') i = 0 a = fluid.dygraph.dygraph_to_static.variable_trans_func.to_static_variable( a) i = fluid.dygraph.dygraph_to_static.variable_trans_func.to_static_variable( i) iter_num = (fluid.dygraph.dygraph_to_static.variable_trans_func. to_static_variable(iter_num)) x = fluid.dygraph.dygraph_to_static.variable_trans_func.to_static_variable( x) def while_condition_0(a, i, iter_num, x): return i < iter_num def while_body_0(a, i, iter_num, x): fluid.layers.array_write(x=x, i=fluid.layers.array_length(a), array=a) i += 1 return a, i, iter_num, x a, i, iter_num, x = fluid.layers.while_loop(while_condition_0, while_body_0, [a, i, iter_num, x]) length = layers.array_length(a) layers.Print(length) return a[0]
def test_array_length(self): tmp = layers.zeros(shape=[10], dtype='int32') i = layers.fill_constant(shape=[1], dtype='int64', value=10) arr = layers.array_write(tmp, i=i) arr_len = layers.array_length(arr) cpu = core.CPUPlace() exe = Executor(cpu) result = exe.run(fetch_list=[arr_len])[0] self.assertEqual(11, result[0])
def test_array_length(self): tmp = layers.zeros(shape=[10], dtype='int32') i = layers.fill_constant(shape=[1], dtype='int64', value=10) arr = layers.array_write(tmp, i=i) arr_len = layers.array_length(arr) cpu = core.CPUPlace() exe = Executor(cpu) result = exe.run(fetch_list=[arr_len])[0] self.assertEqual(11, result[0])
def dygraph_func(x): x = fluid.dygraph.to_variable(x) iter_num = fluid.layers.fill_constant(shape=[1], value=3, dtype="int32") a = [] i = 0 while i < iter_num: a.append(x) i += 1 length = layers.array_length(a) layers.Print(length) return a[0]
def set_program_and_run(self, main_program, case_num): with fluid.program_guard(main_program): x = [ fluid.data(name='x0', shape=self.shape, dtype="float32"), fluid.data(name='x1', shape=self.shape, dtype="float32"), fluid.data(name='x2', shape=self.shape, dtype="float32") ] for each_x in x: each_x.stop_gradient = False arr = layers.create_array(dtype="float32") for i in range(3): idx = layers.array_length(arr) arr = layers.array_write(x=x[i], i=idx, array=arr) if case_num == 1: self.sliced_arr = output = arr[0] elif case_num == 2: end = fluid.layers.array_length( arr) - 1 # dtype of end is int64 self.sliced_arr = slice_arr = arr[self.start:end] output, _ = fluid.layers.tensor_array_to_tensor(slice_arr, axis=self.axis, use_stack=True) elif case_num == 3: value_int64 = fluid.layers.fill_constant([1], "int64", 2147483648) self.sliced_arr = slice_arr = arr[self.start:value_int64] output, _ = fluid.layers.tensor_array_to_tensor(slice_arr, axis=self.axis, use_stack=True) loss = fluid.layers.reduce_sum(output) fluid.backward.append_backward(loss) g_vars = list( map(main_program.global_block().var, [each_x.name + "@GRAD" for each_x in x])) self.out, self.g_x0, self.g_x1, self.g_x2 = \ self.exe.run(main_program, feed = {'x0': self.data, 'x1': self.data, 'x2': self.data}, fetch_list=[output] + g_vars)
def body(i, new_array): item = array_read(array=array, i=i) array_write(item, array_length(new_array), new_array) i = increment(i) return i, new_array