Пример #1
0
    def sampling_rnn(self,
                     item_fc,
                     h_0,
                     pos_embed,
                     forward_func,
                     sampling_type,
                     eps=0,
                     eta=1):
        mask = layers.reduce_sum(item_fc, dim=1, keep_dim=True) * 0 + 1
        drnn = fluid.layers.DynamicRNN()
        with drnn.block():
            # e.g. batch_size = 2
            _ = drnn.step_input(item_fc)
            cur_pos_embed = drnn.step_input(pos_embed)  # lod = []
            cur_h_0 = drnn.memory(init=h_0, need_reorder=True)  # lod = [0,1,2]
            item_fc = drnn.static_input(item_fc)
            mask = drnn.memory(init=mask, need_reorder=True)

            # step_input will remove lod info
            cur_pos_embed = layers.lod_reset(cur_pos_embed, cur_h_0)

            # expand
            expand_h_0 = layers.sequence_expand(
                cur_h_0, item_fc)  # lod = [0,1,2,3,4,5,6,7]
            expand_pos_embed = layers.sequence_expand(
                cur_pos_embed, item_fc)  # lod = [0,1,2,3,4,5,6,7]
            expand_item_fc = layers.lod_reset(item_fc, expand_h_0)
            # forward
            expand_next_h_0, expand_scores = forward_func(
                expand_item_fc, expand_h_0, expand_pos_embed)
            # reset result lod
            expand_next_h_0 = layers.lod_reset(expand_next_h_0,
                                               item_fc)  # lod = [0,4,7]
            expand_scores = layers.lod_reset(expand_scores,
                                             item_fc)  # lod = [0,4,7]

            if sampling_type == 'eps_greedy':
                selected_index = self.eps_greedy_sampling(expand_scores,
                                                          mask,
                                                          eps=eps)
            elif sampling_type == 'softmax':
                selected_index = self.softmax_sampling(expand_scores,
                                                       mask,
                                                       eta=eta)

            drnn.output(selected_index)

            next_h_0 = fluid_sequence_index(expand_next_h_0, selected_index)
            next_mask = fluid_sequence_scatter(
                mask, layers.reshape(selected_index, [-1]), 0.0)

            # update
            drnn.update_memory(cur_h_0, next_h_0)
            drnn.update_memory(mask, next_mask)

        drnn_output = drnn()
        return drnn_output
Пример #2
0
    def train_rnn(self, item_fc, h_0, pos, pos_embed, output_type=''):
        drnn = fluid.layers.DynamicRNN()
        with drnn.block():
            cur_item_fc = drnn.step_input(item_fc)
            cur_pos_embed = drnn.step_input(pos_embed)
            cur_h_0 = drnn.memory(init=h_0, need_reorder=True)

            # step_input will remove lod info
            cur_item_fc = layers.lod_reset(cur_item_fc, cur_h_0)
            cur_pos_embed = layers.lod_reset(cur_pos_embed, cur_h_0)

            next_h_0, Q = self.sampling_rnn_forward(cur_item_fc, cur_h_0,
                                                    cur_pos_embed)

            if output_type == 'c_Q':
                drnn.output(Q)

            elif output_type == 'max_Q':
                # e.g. batch_size = 2
                # cur_h_0: lod = [0,1,2]
                cur_pos = drnn.step_input(pos)
                pos = drnn.static_input(pos)  # lod = [0,4,7]
                item_fc = drnn.static_input(item_fc)  # lod = [0,4,7]

                # expand
                expand_h_0 = layers.sequence_expand(
                    cur_h_0, item_fc)  # lod = [0,1,2,3,4,5,6,7]
                expand_pos_embed = layers.sequence_expand(
                    cur_pos_embed, item_fc)  # lod = [0,1,2,3,4,5,6,7]
                expand_item_fc = layers.lod_reset(item_fc, expand_h_0)
                # forward
                _, expand_scores = self.sampling_rnn_forward(
                    expand_item_fc, expand_h_0, expand_pos_embed)
                # reset result lod
                expand_Q = layers.lod_reset(expand_scores,
                                            item_fc)  # lod = [0,4,7]

                cur_step_id = layers.slice(cur_pos,
                                           axes=[0, 1],
                                           starts=[0, 0],
                                           ends=[1, 1])
                mask = layers.cast(pos >= cur_step_id, 'float32')
                expand_Q = expand_Q * mask
                max_Q = layers.sequence_pool(expand_Q, 'max')  # lod = [0,1,2]
                drnn.output(max_Q)

            else:
                raise NotImplementedError(output_type)

            # update
            drnn.update_memory(cur_h_0, next_h_0)

        drnn_output = drnn()
        return drnn_output
Пример #3
0
    def dynamic_rnn(self, item_fc, h_0, output_type=None, double_type=None, double_id=None):
        drnn = fluid.layers.DynamicRNN()
        pos = fluid_sequence_get_pos(item_fc)
        with drnn.block():
            cur_item_fc = drnn.step_input(item_fc)
            cur_h_0 = drnn.memory(init=h_0, need_reorder=True)

            cur_item_fc = layers.lod_reset(cur_item_fc, cur_h_0)
            next_h_0 = self.simple_step_rnn(cur_item_fc, h_0=cur_h_0)

            if output_type == 'c_Q':
                Q = self.out_Q_fc2_op(self.out_Q_fc1_op(next_h_0))
                drnn.output(Q)

            elif output_type in ['max_Q', 'double_Q']:
                # batch_size = 2
                # item_fc: lod = [0,4,7]
                # cur_h_0: lod = [0,1,2]
                item_fc = drnn.static_input(item_fc)
                pos = drnn.static_input(pos)
                cur_step = drnn.memory(shape=[1], dtype='int64', value=0)

                expand_h_0 = layers.sequence_expand(cur_h_0, item_fc)               # lod = [0,1,2,3,4,5,6,7]
                new_item_fc = layers.lod_reset(item_fc, expand_h_0)                 # lod = [0,1,2,3,4,5,6,7]
                next_expand_h_0 = self.simple_step_rnn(new_item_fc, expand_h_0)     # lod = [0,1,2,3,4,5,6,7]
                next_expand_h_0 = layers.lod_reset(next_expand_h_0, item_fc)        # lod = [0,4,7]

                expand_Q = self.out_Q_fc2_op(self.out_Q_fc1_op(next_expand_h_0))
                cur_step_id = layers.slice(cur_step, axes=[0, 1], starts=[0, 0], ends=[1, 1])
                mask = layers.cast(pos >= cur_step_id, 'float32')
                expand_Q = expand_Q * mask

                if output_type == 'max_Q':
                    max_Q = layers.sequence_pool(expand_Q, 'max')                       # lod = [0,1,2]
                    drnn.output(max_Q)
                elif output_type == 'double_Q':
                    if double_type == 'max_id':
                        max_id = self.eps_greedy_sampling(expand_Q, mask, eps=0)
                        drnn.output(max_id)
                    elif double_type == 'double_Q':
                        cur_double_id = drnn.step_input(double_id)

                        double_Q = fluid_sequence_index(expand_Q, cur_double_id)
                        drnn.output(double_Q)

                # update
                next_step = cur_step + 1
                drnn.update_memory(cur_step, next_step)

            elif output_type == 'hidden':
                drnn.output(next_h_0)                

            else:
                raise NotImplementedError(output_type)

            # update
            drnn.update_memory(cur_h_0, next_h_0)

        drnn_output = drnn()
        return drnn_output
Пример #4
0
    def infer_onestep(self, inputs):
        """inference the gru-unit by one step"""
        prev_hidden = inputs['prev_hidden']
        first_step_mask = inputs['first_step_mask']
        item_embedding = self._build_embeddings(
            inputs, self.item_slot_names)  # (b*cand_len, dim), as candidates
        last_click_embedding = self._build_embeddings(
            inputs, self.last_click_slot_names)  # (b, dim)
        last_item_embedding = self._build_embeddings(
            inputs, self.last_item_slot_names)  # (b, dim)

        item_fc = self.item_fc_op(item_embedding)
        last_item_fc = self.item_fc_op(last_item_embedding) * first_step_mask
        item_hidden = self.simple_step_rnn(last_item_fc,
                                           last_click_embedding,
                                           h_0=prev_hidden)
        action_hat = self.actor_policy(item_hidden)

        # inner product
        expand_action_hat = layers.sequence_expand(
            action_hat, item_fc)  # (b*cand_len, dim)
        scores = layers.reduce_sum(expand_action_hat * item_fc, 1)

        output_dict = OrderedDict()
        output_dict['hidden'] = item_hidden
        output_dict['scores'] = scores
        return output_dict
Пример #5
0
 def _dot_attention(self, input, atten_items):
     """
     args:
         input: (batch, dim), lod_level = 0
         atten_items: (batch*seq_len, dim), lod_level = 1
     return:
         atten_weights: (batch*seq_len, 1), lod_level = 1
     """
     expand_input = layers.sequence_expand(
         input, atten_items)  #(batch*seq_len, dim), lod_level = 0
     expand_input = layers.lod_reset(
         expand_input, atten_items)  #(batch*seq_len, dim), lod_level = 1
     if self._attention_type == 'concat_fc':
         atten_weights = self.atten_fc_op(
             layers.concat([expand_input, atten_items], 1))
     elif self._attention_type == 'dot':
         atten_weights = layers.reduce_sum(
             expand_input * atten_items, dim=1,
             keep_dim=True)  #(batch*seq_len, 1), lod_level = 1
     return atten_weights
Пример #6
0
    def forward(self, inputs, mode):
        """forward"""
        item_embedding = self._build_embeddings(inputs, self.item_slot_names)
        recent_embedding = self._build_embeddings(inputs,
                                                  self.recent_slot_names)

        user_feature = self.user_encode(recent_embedding)  # (batch, dim)

        item_fc = self.item_fc_op(item_embedding)  # (batch*seq_len, dim)
        hidden = layers.concat(
            [layers.sequence_expand(user_feature, item_fc), item_fc], 1)

        output_dict = OrderedDict()
        if 'click' in self._output_type:
            output_dict['click_prob'] = self.out_click_fc2_op(
                self.out_click_fc1_op(hidden))
        if 'credit' in self._output_type:
            output_dict['credit_pred'] = self.out_credit_fc2_op(
                self.out_credit_fc1_op(hidden))
        if 'rate' in self._output_type:
            output_dict['rate_pred'] = self.out_rate_fc2_op(
                self.out_rate_fc1_op(hidden))
        return output_dict