コード例 #1
0
    def train_test(batch_data, reward):
        place = fluid.CPUPlace()
        feed_dict = {}
        for name in batch_data.conf.user_slot_names + batch_data.conf.item_slot_names:
            ft = batch_data.tensor_dict[name]
            feed_dict[name] = create_tensor(ft.values, lod=ft.lod, place=place)

        decode_len = batch_data.decode_len().reshape([-1, 1]).astype('int64')
        lod = [seq_len_2_lod([1] * len(decode_len))]
        feed_dict['decode_len'] = create_tensor(decode_len,
                                                lod=lod,
                                                place=place)
        reward = reward.reshape([-1, 1]).astype('float32')
        lod = [seq_len_2_lod(batch_data.decode_len())]
        feed_dict['reward'] = create_tensor(reward, lod=lod, place=place)
        return feed_dict
コード例 #2
0
ファイル: train_simulator.py プロジェクト: wgcn96/MBCAL
    def infer_onestep(batch_data, prev_hidden, selected_items, last_click_id):
        """
        batch_data: the complete data
            item_feature: (b*seq_len,)
        prev_hidden: (b, dim)
        last_click_id: (b,)
        selected_items: (b,)
        """
        batch_size = batch_data.batch_size()
        seq_len = batch_data.seq_lens()[0]
        batch_offset = batch_data.offset()

        place = fluid.CPUPlace()
        feed_dict = {}
        lod = [seq_len_2_lod([1] * batch_size)]
        offset_selected_items = np.array(selected_items) + batch_offset  # (b,)
        for name in batch_data.conf.item_slot_names:
            if name == 'last_click_id':
                v = last_click_id.reshape([-1, 1])
            else:
                v = batch_data.tensor_dict[name].values[offset_selected_items]
            feed_dict[name] = create_tensor(v, lod=lod, place=place)

        # prev_hidden
        feed_dict['prev_hidden'] = create_tensor(prev_hidden,
                                                 lod=lod,
                                                 place=place)
        return feed_dict
コード例 #3
0
    def softmax_sampling(batch_data, eta):
        place = fluid.CPUPlace()
        feed_dict = {}
        for name in batch_data.conf.user_slot_names + batch_data.conf.item_slot_names:
            ft = batch_data.tensor_dict[name]
            feed_dict[name] = create_tensor(ft.values, lod=ft.lod, place=place)

        decode_len = batch_data.decode_len().reshape([-1, 1]).astype('int64')
        lod = [seq_len_2_lod([1] * len(decode_len))]
        feed_dict['decode_len'] = create_tensor(decode_len,
                                                lod=lod,
                                                place=place)
        eta = np.array([eta]).astype('float32')
        feed_dict['eta'] = create_tensor(eta, lod=[], place=place)
        return feed_dict
コード例 #4
0
ファイル: gen_utils.py プロジェクト: wgcn96/MBCAL
    def infer_onestep(batch_data,
                      prev_hidden,
                      candidate_items,
                      last_click_id,
                      conf=None):
        """
        batch_data: the complete data
            item_feature: (b*seq_len,)
        prev_hidden: (b, dim)
        last_click_id: (b,)
        candidate_items: 2d list
        """
        if conf is None:
            conf = batch_data.conf

        batch_size = batch_data.batch_size()
        seq_len = batch_data.seq_lens()[0]
        cand_len = len(candidate_items[0])
        batch_offset = batch_data.offset()

        # expand last_click_id
        last_click_id = np.repeat(last_click_id, cand_len,
                                  axis=0).reshape([-1, 1])  # (b*cand_len, 1)

        place = fluid.CPUPlace()
        feed_dict = {}
        lod = [seq_len_2_lod([1] * (batch_size * cand_len))]
        offset_candidate_items = np.array(candidate_items).flatten(
        ) + np.repeat(batch_offset, cand_len, axis=0)  # (b*cand_len)
        for name in conf.item_slot_names:
            if name == 'last_click_id':
                v = last_click_id
            else:
                v = batch_data.tensor_dict[name].values[offset_candidate_items]
            feed_dict[name] = create_tensor(v, lod=lod, place=place)

        # expand prev_hidden
        prev_hidden = np.repeat(prev_hidden, cand_len,
                                axis=0)  # (b*cand_len, dim)
        feed_dict['prev_hidden'] = create_tensor(prev_hidden,
                                                 lod=lod,
                                                 place=place)
        return feed_dict
コード例 #5
0
ファイル: gen_utils.py プロジェクト: wgcn96/MBCAL
    def apply_masks(batch_data, list_item_masks, conf=None):
        """
        list_item_masks: (n_masks, seq_len), a list of 1d item_mask
        Apply mask on item_level_slot_names except last_click_id
        """
        if conf is None:
            conf = batch_data.conf

        batch_size = batch_data.batch_size()
        seq_len = batch_data.seq_lens()[0]
        AssertEqual(len(list_item_masks[0]), seq_len)

        batch_data.add_last_click_id()

        n_masks = len(list_item_masks)
        batch_item_masks = np.tile(
            np.array(list_item_masks).flatten(),
            [batch_size])  # (batch_size * n_masks * seq_len)

        place = fluid.CPUPlace()
        feed_dict = {}
        for name in conf.recent_slot_names + \
                    conf.item_slot_names:
            ft = batch_data.tensor_dict[name]
            v = ft.values
            extra_shape = list(v.shape[1:])
            v = v.reshape([batch_size, -1] +
                          extra_shape)  # (batch_size, seq_len/recent_len, ...)
            v = np.repeat(
                v, n_masks,
                axis=0)  # (batch_size * n_masks, seq_len/recent_len, ...)
            seq_lens = [v.shape[1]] * (batch_size * n_masks)
            v = v.reshape([-1] + extra_shape
                          )  # (batch_size * n_masks * seq_len/recent_len, ...)
            if name in conf.item_slot_names and name != 'last_click_id':
                v = v * batch_item_masks.reshape([-1] + [1] *
                                                 (len(v.shape) - 1))
            feed_dict[name] = create_tensor(v,
                                            lod=[seq_len_2_lod(seq_lens)],
                                            place=place)
        return feed_dict
コード例 #6
0
ファイル: gen_utils.py プロジェクト: wgcn96/MBCAL
    def infer_onestep(batch_data,
                      prev_hidden,
                      candidate_items,
                      last_click_id,
                      last_item,
                      step,
                      conf=None):
        """
        batch_data: the complete data
            item_feature: (b*seq_len,)
        prev_hidden: (b, dim)
        last_click_id: (b,)
        last_item: (b,)
        step: int
        candidate_items: 2d list

        Different from other model, ddpg don't need to expand.
        """
        if conf is None:
            conf = batch_data.conf

        batch_size = batch_data.batch_size()
        seq_len = batch_data.seq_lens()[0]
        cand_len = len(candidate_items[0])
        batch_offset = batch_data.offset()

        place = fluid.CPUPlace()
        feed_dict = {}
        item_slot_names = list(conf.item_slot_names)
        item_slot_names.remove('last_click_id')
        last_item_slot_names = ['last_' + name for name in item_slot_names]

        ### candidates
        lod = [seq_len_2_lod([cand_len] * batch_size)]
        offset_candidate_items = np.array(candidate_items).flatten(
        ) + np.repeat(batch_offset, cand_len, axis=0)  # (b*cand_len)
        for name in item_slot_names:
            v = batch_data.tensor_dict[name].values[offset_candidate_items]
            feed_dict[name] = create_tensor(v, lod=lod, place=place)

        ### last item
        lod = [seq_len_2_lod([1] * batch_size)]
        offset_last_item = np.array(last_item).flatten() + batch_offset  # (b,)
        for last_name in last_item_slot_names:
            name = last_name[len('last_'):]
            v = batch_data.tensor_dict[name].values[offset_last_item]
            feed_dict[last_name] = create_tensor(v, lod=lod, place=place)

        ### last click
        lod = [seq_len_2_lod([1] * batch_size)]
        feed_dict['last_click_id'] = create_tensor(last_click_id.reshape(
            [-1, 1]),
                                                   lod=lod,
                                                   place=place)  # (b, 1)

        ### prev_hidden
        lod = [seq_len_2_lod([1] * batch_size)]
        feed_dict['prev_hidden'] = create_tensor(prev_hidden,
                                                 lod=lod,
                                                 place=place)  # (b, dim)

        ### first_step_mask
        lod = [seq_len_2_lod([1] * batch_size)]
        first_step_mask = np.full([batch_size, 1],
                                  float(step > 0)).astype('float32')
        feed_dict['first_step_mask'] = create_tensor(first_step_mask,
                                                     lod=lod,
                                                     place=place)  # (b, 1)
        return feed_dict