Ejemplo n.º 1
0
Archivo: model.py Proyecto: Ocxs/MUIR
    def build_model(self, user_ids, item_vec, cate_ids, labels,
                    neighbor_user_ids, keep_prob, phase):

        with tf.variable_scope('feature_representation'):
            # user
            neighbor_user_emb = tf.nn.embedding_lookup(self.user_embedding,
                                                       neighbor_user_ids)

            # item
            item_emb = dense(item_vec, self.emb_dim, ['w1'], keep_prob)
            cate_emb = tf.nn.embedding_lookup(self.item_category_embedding,
                                              cate_ids)

            item_emb = item_emb + cate_emb

        with tf.variable_scope('neighborhood_level_interest_network'):
            avg_neighbor_emb = tf.reduce_mean(neighbor_user_emb,
                                              axis=1,
                                              keep_dims=True)
            neighbor_emb = neighbor_attention(neighbor_user_emb, item_emb,
                                              avg_neighbor_emb, 1.0, 'relu',
                                              False)

        # ef: early fusion
        # if: intermediate fusion
        # lf: late fusion
        with tf.variable_scope('interaction'):
            if self.interaction == 'inner':
                logits = tf.reduce_sum(neighbor_emb * item_emb, axis=1)
            else:
                if self.interaction == 'product':
                    fusion_input = neighbor_emb * item_emb
                else:
                    raise ValueError('invalid fusion way: {}'.format(
                        self.interaction))

                with tf.variable_scope('aggregation_layer'):
                    logits = dnn(fusion_input, self.agg_layers, keep_prob)

        inference_loss = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(logits=logits,
                                                    labels=labels))

        acc = self.compute_acc(logits, self.labels_ph)

        if phase == 'train':
            emb_l2_loss = tf.nn.l2_loss(neighbor_user_emb) + tf.nn.l2_loss(
                cate_emb)

            w_l2_loss = tf.add_n([
                tf.nn.l2_loss(v) for v in tf.trainable_variables()
                if 'w' in v.name
            ])
            l2_w_loss = w_l2_loss * self.w_reg
            l2_emb_loss = emb_l2_loss * self.emb_reg
            return inference_loss, l2_w_loss, l2_emb_loss, acc, logits
        else:
            return inference_loss, acc, logits
Ejemplo n.º 2
0
Archivo: model.py Proyecto: Ocxs/MUIR
    def build_model(self, user_ids, item_ids, cate_ids, labels, keep_prob,
                    phase):

        with tf.variable_scope('feature_representation'):
            # user
            user_click_cate_distribution = tf.nn.embedding_lookup(
                self.user_click_cate_distribution, user_ids)

            # item
            cate_emb = tf.reduce_mean(tf.nn.embedding_lookup(
                self.item_category_embedding, cate_ids),
                                      axis=1)

            visual_feat = tf.nn.embedding_lookup(self.visual_feature, item_ids)
            visual_emb = dense(visual_feat, self.emb_dim, ['w1'], keep_prob)

            item_emb = visual_emb + cate_emb

            with tf.variable_scope('category_level_interest'):
                category_interest = tf.matmul(user_click_cate_distribution,
                                              self.category_embedding)

        with tf.variable_scope('aggregation'):
            if self.interaction == 'inner':
                fusion_input = category_interest * item_emb
                logits = tf.reduce_sum(fusion_input, axis=1)
            else:
                if self.interaction == 'product':
                    fusion_input = category_interest * item_emb
                else:
                    raise ValueError('invalid fusion way: {}'.format(
                        self.interaction))

                with tf.variable_scope('aggregation_layer'):
                    logits = dnn(fusion_input, self.agg_layers, keep_prob)

        inference_loss = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(logits=logits,
                                                    labels=labels))

        acc = self.compute_acc(logits, self.labels_ph)

        if phase == 'train':
            emb_l2_loss = tf.nn.l2_loss(cate_emb) + tf.nn.l2_loss(
                self.category_embedding)
            w_l2_loss_lst = [
                tf.nn.l2_loss(v) for v in tf.trainable_variables()
                if 'w' in v.name
            ]
            if len(w_l2_loss_lst) != 0:
                w_l2_loss = tf.add_n(w_l2_loss_lst)
                reg_loss = w_l2_loss * self.w_reg + emb_l2_loss * self.emb_reg
            else:
                reg_loss = emb_l2_loss * self.emb_reg
            return inference_loss, reg_loss, acc, logits
        else:
            return inference_loss, acc, logits
Ejemplo n.º 3
0
    def build_model(self, item_vec, cate_ids, att_iids, att_cids, intra_mask,
                    inter_mask, labels, keep_prob):

        with tf.variable_scope('item_embedding'):
            att_item_vec = self.get_train_cover_image_feature(att_iids)
            att_cate_emb = self.get_cate_emb(att_cids)

            item_vec = dense(item_vec, self.item_dim, ['w1'], 1.0)
            att_item_vec = dense(att_item_vec,
                                 self.item_dim, ['w1'],
                                 1.0,
                                 reuse=True)
            cate_emb = self.get_cate_emb(cate_ids)
            item_emb = tf.concat([item_vec, cate_emb], axis=-1)

        with tf.variable_scope('temporal_hierarchical_attention'):
            user_profiles = temporal_hierarchical_attention(
                att_cate_emb, att_item_vec, intra_mask, inter_mask,
                self.num_heads, keep_prob)

        with tf.variable_scope('micro_video_click_through_prediction'):
            user_profile = vanilla_attention(user_profiles, item_emb,
                                             inter_mask, keep_prob)
            y = dnn(tf.concat([user_profile, item_emb], axis=-1),
                    self.fusion_layers, keep_prob)
        logits = y

        # regularization
        emb_l2_loss = tf.add_n([
            tf.nn.l2_loss(v) for v in tf.trainable_variables()
            if 'embedding' in v.name
        ])
        w_l2_loss = tf.add_n([
            tf.nn.l2_loss(v) for v in tf.trainable_variables() if 'w' in v.name
        ])
        l2_norm = emb_l2_loss + w_l2_loss

        loss = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(
                logits=logits, labels=labels)) + l2_norm * self.reg
        acc = self.compute_acc(logits, self.labels_ph)
        return loss, acc, logits
Ejemplo n.º 4
0
    def build_model(self, user_ids, item_ids, cate_ids, labels, hist_iids,
                    hist_length, keep_prob, att_dropout, phase):

        with tf.variable_scope('feature_representation'):
            # user
            # item
            visual_feat = tf.nn.embedding_lookup(self.visual_feature, item_ids)
            cate_emb = tf.reduce_mean(tf.nn.embedding_lookup(
                self.item_category_embedding, cate_ids),
                                      axis=1)
            visual_emb = dense(visual_feat, self.emb_dim, ['w1'], 1.0)
            item_emb = visual_emb + cate_emb

            hist_visual_feat = tf.nn.embedding_lookup(self.visual_feature,
                                                      hist_iids)
            hist_visual_emb = dense(hist_visual_feat, self.emb_dim, ['w2'],
                                    keep_prob)
            hist_item_emb = hist_visual_emb
            mask = tf.sequence_mask(hist_length, self.hist_length)

            with tf.variable_scope('item_interest_network'):
                avg_hist_item_emb = tf.reduce_sum(hist_item_emb * tf.cast(tf.expand_dims(mask, dim=2), tf.float32), axis=1) / \
                                    tf.expand_dims(tf.cast(hist_length, tf.float32), axis=1)
                item_interest = item_interest_attention(
                    hist_item_emb, hist_item_emb, item_emb, avg_hist_item_emb,
                    mask, 1.0, 'sigmoid', True, att_dropout)

        with tf.variable_scope('aggregation'):
            if self.interaction == 'inner':
                fusion_input = item_interest * item_emb
                logits = tf.reduce_sum(fusion_input, axis=1)
            else:
                if self.interaction == 'product':
                    fusion_input = item_interest * item_emb
                else:
                    raise ValueError('invalid fusion way: {}'.format(
                        self.interaction))

                with tf.variable_scope('aggregation_layer'):
                    logits = dnn(fusion_input, self.agg_layers, keep_prob)

        inference_loss = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(logits=logits,
                                                    labels=labels))

        acc = self.compute_acc(logits, self.labels_ph)

        if phase == 'train':
            emb_l2_loss = tf.nn.l2_loss(cate_emb)
            w_l2_loss_lst = [
                tf.nn.l2_loss(v) for v in tf.trainable_variables()
                if 'w' in v.name
            ]
            if len(w_l2_loss_lst) != 0:
                w_l2_loss = tf.add_n(w_l2_loss_lst)
                reg_loss = w_l2_loss * self.w_reg + emb_l2_loss * self.emb_reg
            else:
                reg_loss = emb_l2_loss * self.emb_reg
            return inference_loss, reg_loss, acc, logits
        else:
            return inference_loss, acc, logits