Example #1
0
        def reader():
            self.inputs = []
            cnt = 0
            while True:
                tensors = fluid.LoDTensorArray()
                in_data = np.random.uniform(
                    low=0, high=1, size=(1, self.in_size)).astype('float32')
                tensors.append(as_tensor(in_data))
                label = np.random.random_integers(low=0,
                                                  high=self.class_num - 1,
                                                  size=(1, 1)).astype('int64')
                tensors.append(as_tensor(label))

                if cnt < self.iterations * self.batch_size * self.batch_size_times:
                    if cnt % (self.batch_size * self.batch_size_times) == 0:
                        self.inputs.append([in_data, label])
                    else:
                        self.inputs[-1][0] = np.concatenate(
                            (self.inputs[-1][0], in_data), axis=0)
                        self.inputs[-1][1] = np.concatenate(
                            (self.inputs[-1][1], label), axis=0)
                elif not self.use_double_buffer:
                    break

                yield tensors
                cnt += 1

            yield None
Example #2
0
    def test_pin_memory_pyreader(self):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
            place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda(
            ) else fluid.CPUPlace()
            executor = fluid.Executor(place)

            data_file = fluid.layers.py_reader(
                capacity=self.capacity,
                dtypes=self.dtypes,
                lod_levels=self.lod_levels,
                shapes=self.shapes)
            # feed_queue = data_file.queue
            read_out_data = fluid.layers.read_file(data_file)

            self.inputs = []
            for _ in range(10):
                sample = np.random.uniform(
                    low=0, high=1, size=[3, 2, 1]).astype("float32")
                label = np.random.uniform(
                    low=0, high=10, size=[1]).astype("int64")
                self.inputs.append((sample, label))

            self.input_tensors = []
            for d, l in batch_feeder(
                    paddle.batch(
                        user_reader(self.inputs), batch_size=2),
                    pin_memory=True
                    if fluid.core.is_compiled_with_cuda() else False)():
                ta = fluid.LoDTensorArray()
                ta.append(d)
                ta.append(l)
                self.input_tensors.append(ta)

            self.batched_inputs = []
            for batch in paddle.batch(user_reader(self.inputs), batch_size=2)():
                feed_d = []
                feed_l = []
                for d, l in batch:
                    feed_d.append(d)
                    feed_l.append([l])
                self.batched_inputs.append([feed_d, feed_l])

            data_file.decorate_tensor_provider(
                batch_feeder(
                    paddle.batch(
                        user_reader(self.inputs), batch_size=2),
                    pin_memory=True
                    if fluid.core.is_compiled_with_cuda() else False))

            executor.run(fluid.default_startup_program())
            self.outputs = []

            data_file.start()
            for _ in self.input_tensors:
                self.outputs.append(
                    executor.run(fetch_list=list(read_out_data)))
            data_file.reset()
            self.validate()
Example #3
0
    def main(self, use_thread=False):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
            place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda(
            ) else fluid.CPUPlace()
            executor = fluid.Executor(place)

            data_file = fluid.layers.py_reader(
                capacity=self.capacity,
                dtypes=self.dtypes,
                lod_levels=self.lod_levels,
                shapes=self.shapes)
            feed_queue = data_file.queue
            read_out_data = fluid.layers.read_file(data_file)
            self.inputs = []

            for i in range(self.iterations):
                in_data = fluid.LoDTensorArray()
                batch_size = np.random.random_integers(self.batch_size_min,
                                                       self.batch_size_max)
                for shape, dtype in zip(self.shapes, self.dtypes):
                    next_data = np.random.uniform(
                        low=0, high=1000,
                        size=(batch_size, ) + shape[1:]).astype(dtype)
                    in_data.append(
                        fluid.executor._as_lodtensor(next_data, place))

                self.inputs.append(in_data)

            executor.run(fluid.default_startup_program())
            self.outputs = []
            if use_thread:
                thread = Thread(
                    target=feed_data, args=(feed_queue, self.inputs))
                thread.start()
                for in_data in self.inputs:
                    self.outputs.append(
                        executor.run(fetch_list=list(read_out_data)))
            else:
                for in_data in self.inputs:
                    feed_queue.push(in_data)
                    self.outputs.append(
                        executor.run(fetch_list=list(read_out_data)))

            feed_queue.close()
            self.validate()
Example #4
0
def sample_list_to_tensor_array(sample_list):
    slot_num = None
    slots = None
    for sample in sample_list:
        if slot_num is None:
            slot_num = len(sample)
            slots = [None] * len(sample)
        else:
            assert slot_num == len(sample)

        for slot_id, slot_item in enumerate(sample):
            if slots[slot_id] is None:
                slots[slot_id] = []
            slots[slot_id].append(slot_item)

    tensor_array = fluid.LoDTensorArray()
    for slot in slots:
        t = fluid.LoDTensor()
        t.set(np.array(slot), fluid.CPUPlace())
        tensor_array.append(t)

    return tensor_array
Example #5
0
    def _slot_gate(encoder_outs, encoder_last_h, slots_embedding, sent_mask, words_emb, story):

        slots_embedding1 = fluid.layers.transpose(x= slots_embedding, perm=[1,0,2])
        slots_embedding1 = fluid.layers.reshape(x=slots_embedding1, shape=[args['all_slot_num'] * args['batch_size'], -1, args['slot_emb_dim']])
        dec_input = fluid.layers.dropout(slots_embedding1, dropout_prob=args['dropout'])
        hidden = fluid.layers.expand(encoder_last_h, expand_times=[args['all_slot_num'], 1])
        

        # hidden2gate = fluid.layers.create_parameter(shape=[args['slot_emb_dim'], args['gate_kind']], dtype='float32')
        # hidden2pgen = fluid.layers.create_parameter(shape=[args['slot_emb_dim'] * 3, 1],dtype='float32')

        # all_point_outputs_list = fluid.Tensor()
        # all_point_outputs_list = LodTensor_to_Tensor(all_point_outputs_list)
        # all_point_outputs_list.set(np.zeros(shape=(args['all_slot_num'], args['batch_size'], 10 ,data_processor.get_vocab_size('utterances')), 
                                    # dtype='float32'),
                                # fluid.CPUPlace())
        
        all_point_outputs_list = fluid.LoDTensorArray()
        
        out_gate_probs = []
        i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0)     
        n = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10)
        cond = fluid.layers.less_than(x=i, y=n)
        while_op = fluid.layers.While(cond = cond)
        with while_op.block():
        # fluid.layers.arr
        # i, n, hidden, dec_input, encoder_outs, words_emb, story, all_point_outputs_list
        # i, n, hidden, dec_input, encoder_outs, words_emb, story, all_point_outputs_list = \
        # fluid.layers.while_loop(cond=while_cond, 
        #                         body=while_body,
        #                         loop_vars=[i, n, hidden, dec_input, encoder_outs, words_emb, story, all_point_outputs_list])
        # for i in range(10):
            # cell = fluid.layers.GRUCell(hidden_size=args['slot_emb_dim'])
            # dec_outs, hidden = fluid.layers.rnn(cell = cell,
            #                                     inputs = dec_input,
            #                                     initial_states= hidden)
            init_c = fluid.layers.fill_constant(shape=[1, args['batch_size'] * args['all_slot_num'], args['slot_emb_dim']], dtype='float32', value=0.0 )
            dec_outs, hidden, _ = fluid.layers.lstm(input = dec_input,
                                                init_h= fluid.layers.unsqueeze(hidden, axes=[0]),
                                                init_c=init_c,
                                                max_len=400,
                                                hidden_size=args['slot_emb_dim'],
                                                num_layers=1)
            hidden = fluid.layers.squeeze(hidden, axes=[0])
            enc_out = fluid.layers.expand(encoder_outs, expand_times=[args['all_slot_num'], 1, 1])
            context, prob = attend(enc_out, hidden, sent_mask)
            gate_probs = fluid.layers.fc(context, size = args['gate_kind'], act='softmax')
            # gate_probs = fluid.layers.softmax(fluid.layers.matmul(context, hidden2gate))

            if i == 0:
                out_gate_probs = gate_probs
            
            
            p_vocab = attend_vocab(words_emb, hidden)
            p_gen_vec=  fluid.layers.concat([fluid.layers.squeeze(dec_outs, axes=[1]), context, fluid.layers.squeeze(dec_input, axes=[1])], axis=-1)
            vocab_pointer_switches = fluid.layers.fc(p_gen_vec, size=1,act='sigmoid')
            # vocab_pointer_switches = fluid.layers.sigmoid(fluid.layers.matmul(p_gen_vec, hidden2pgen))
            p_context_ptr = fluid.layers.fill_constant(shape=[args['batch_size'] * args['all_slot_num'], data_processor.get_vocab_size('utterances')],dtype='float32',value=0.0)
            

            story_index = fluid.layers.unsqueeze(fluid.layers.expand(story, expand_times=[args['all_slot_num'], 1]),axes=[2])
            updates = fluid.layers.expand(fluid.layers.unsqueeze(prob,axes=[2]), expand_times=[1,1,data_processor.get_vocab_size('utterances')])
            p_context_ptr = fluid.layers.scatter_nd_add(ref = p_context_ptr, index = story_index, updates=updates)

            # print('vocab_pointer_switches size: %s'%(str(vocab_pointer_switches.shape)))
            # print('p_context_ptr size: %s'%(str(p_context_ptr.shape)))
            # print('p_vocab size: %s'%(str(p_vocab.shape)))


            final_p_vocab = fluid.layers.expand((1 - vocab_pointer_switches), expand_times=[1, data_processor.get_vocab_size('utterances')]) * p_context_ptr + \
                            fluid.layers.expand(vocab_pointer_switches, expand_times=[1, data_processor.get_vocab_size('utterances')]) * p_vocab

            # p_final_word = fluid.layers.argmax(final_p_vocab, axis=1)

            npfw = fluid.layers.reshape(final_p_vocab,shape=[args['all_slot_num'], args['batch_size'], -1 ,data_processor.get_vocab_size('utterances')])
            # if i == 0:
            #     c = npfw
            # else:
            #     c = fluid.layers.concat([c, npfw], axis=2)
            all_point_outputs_list.append(npfw)
            i=fluid.layers.increment(x=i, value=1, in_place=True)
            fluid.layers.less_than(x=i, y=n, cond=cond)
            # all_point_outputs_list.append(npfw)
            # all_point_outputs_list[:, :, i, :] = npfw
            # dec_input =
        # gate_probs = all_point_outputs_list[0] 
        # all_point_outputs = fluid.layers.concat(all_point_outputs_list, axis=2)
        c = fluid.layers.reshape(x=c, shape=[args['all_slot_num'], args['batch_size'], -1 ,data_processor.get_vocab_size('utterances')])
        # all_point_outputs_list
        return out_gate_probs, c