Example #1
0
    def build_fcn_net(self, inp, use_dice=False):
        def dtype_getter(getter, name, dtype=None, *args, **kwargs):
            var = getter(name, dtype=self.model_dtype, *args, **kwargs)
            return var

        with tf.compat.v1.variable_scope("fcn",
                                         custom_getter=dtype_getter,
                                         dtype=self.model_dtype):
            bn1 = tf.compat.v1.layers.batch_normalization(inputs=inp,
                                                          name='bn1')
            dnn1 = tf.compat.v1.layers.dense(bn1,
                                             200,
                                             activation=None,
                                             name='f1')
            if use_dice:
                dnn1 = dice(dnn1, name='dice_1', data_type=self.model_dtype)
            else:
                dnn1 = prelu(dnn1, 'prelu1')

            dnn2 = tf.compat.v1.layers.dense(dnn1,
                                             80,
                                             activation=None,
                                             name='f2')
            if use_dice:
                dnn2 = dice(dnn2, name='dice_2', data_type=self.model_dtype)
            else:
                dnn2 = prelu(dnn2, 'prelu2')
            dnn3 = tf.compat.v1.layers.dense(dnn2,
                                             2,
                                             activation=None,
                                             name='f3')
            self.y_hat = tf.nn.softmax(dnn3) + 0.00000001

            with tf.name_scope("Metrics"):
                #with tf.compat.v1.variable_scope("Metrics", custom_getter=dtype_getter, dtype=self.model_dtype):
                # Cross-entropy loss and optimizer initialization
                ctr_loss = -tf.reduce_mean(
                    tf.math.log(self.y_hat) * self.target_ph)
                self.loss = ctr_loss
                if self.use_negsampling:
                    self.loss += self.aux_loss
                tf.compat.v1.summary.scalar('loss', self.loss)
                # self.optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=self.lr).minimize(self.loss)
                # self.optimizer = tf.compat.v1.train.GradientDescentOptimizer(learning_rate=self.lr).minimize(self.loss)
                # self.optimizer = tf.compat.v1.train.MomentumOptimizer(learning_rate=self.lr, momentum=0.9).minimize(self.loss)

                # convert sparse optimizer to dense optimizer
                adam_optimizer = tf.compat.v1.train.AdamOptimizer(
                    learning_rate=self.lr)
                gradients = adam_optimizer.compute_gradients(self.loss)
                gradients = self._sparse_to_dense_grads(gradients)
                self.optimizer = adam_optimizer.apply_gradients(gradients)

                # Accuracy metric
                self.accuracy = tf.reduce_mean(
                    tf.cast(tf.equal(tf.round(self.y_hat), self.target_ph),
                            self.model_dtype))
                tf.compat.v1.summary.scalar('accuracy', self.accuracy)

            self.merged = tf.compat.v1.summary.merge_all()
Example #2
0
    def build_fcn_net(self, inp, use_dice=False):
        bn1 = tf.layers.batch_normalization(inputs=inp, name='bn1', training=True)
        dnn1 = tf.layers.dense(
            bn1, 200, activation=None, kernel_initializer=get_tf_initializer(), name='f1')
        if use_dice:
            dnn1 = dice(dnn1, name='dice_1')
        else:
            dnn1 = prelu(dnn1)
        dnn2 = tf.layers.dense(
            dnn1, 80, activation=None, kernel_initializer=get_tf_initializer(), name='f2')
        if use_dice:
            dnn2 = dice(dnn2, name='dice_2')
        else:
            dnn2 = prelu(dnn2)
        dnn3 = tf.layers.dense(
            dnn2, 2, activation=None, kernel_initializer=get_tf_initializer(), name='f3')
        self.y_hat = tf.nn.softmax(dnn3) + 0.00000001

        with tf.name_scope('Metrics'):
            ctr_loss = - \
                tf.reduce_mean(tf.log(self.y_hat) * self.tensors.target)
            self.loss = ctr_loss
            if self.use_negsampling:
                self.loss += self.aux_loss
            else:
                self.aux_loss = tf.constant(0.0)

            # Accuracy metric
            self.accuracy = tf.reduce_mean(
                tf.cast(tf.equal(tf.round(self.y_hat), self.tensors.target), tf.float32))
Example #3
0
    def build_fcn_net(self, inp, use_dice=False):
        bn1 = tf.layers.batch_normalization(inputs=inp, name='bn1', training=True)
        dnn1 = tf.layers.dense(bn1, 200, activation=None, kernel_initializer=get_tf_initializer(), name='f1')
        if use_dice:
            dnn1 = dice(dnn1, name='dice_1')
        else:
            dnn1 = prelu(dnn1)
        dnn2 = tf.layers.dense(dnn1, 80, activation=None, kernel_initializer=get_tf_initializer(), name='f2')
        if use_dice:
            dnn2 = dice(dnn2, name='dice_2')
        else:
            dnn2 = prelu(dnn2)
        dnn3 = tf.layers.dense(dnn2, 2, activation=None, kernel_initializer=get_tf_initializer(), name='f3')
        self.y_hat = tf.nn.softmax(dnn3) + 0.00000001

        with tf.name_scope('Metrics'):
            ctr_loss = - \
                tf.reduce_mean(tf.log(self.y_hat) * self.tensors.target)
            self.loss = ctr_loss
            if self.use_negsampling:
                self.loss += self.aux_loss
            else:
                self.aux_loss = tf.constant(0.0)

            # Accuracy metric
            self.accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.round(self.y_hat), self.tensors.target), tf.float32))
Example #4
0
    def build_fcn_net_rocket(self, booster_inp, light_inp, use_dice = False):
        bn1 = tf.layers.batch_normalization(inputs=booster_inp, name='bn1', training=True)
        bn1_rocket = tf.layers.batch_normalization(inputs=light_inp, name='bn1_rocket', training=True)
        dnn1 = tf.layers.dense(bn1, 200, activation=None, kernel_initializer=get_tf_initializer(), name='f1')
        dnn1_rocket = tf.layers.dense(bn1_rocket, 200, activation=None, kernel_initializer=get_tf_initializer(), name='f1_rocket')

        if use_dice:
            dnn1 = dice(dnn1, name='dice_1')
            dnn1_rocket = dice(dnn1_rocket, name='dice_rocket_1')
        else:
            dnn1 = prelu(dnn1, scope='prelu_1')
            dnn1_rocket = prelu(dnn1_rocket, scope='prelu_rocket_1')

        dnn2 = tf.layers.dense(dnn1, 80, activation=None, kernel_initializer=get_tf_initializer(), name='f2')
        dnn2_rocket = tf.layers.dense(dnn1_rocket, 80, activation=None, kernel_initializer=get_tf_initializer(), name='f2_rocket')

        if use_dice:
            dnn2 = dice(dnn2, name='dice_2')
            dnn2_rocket = dice(dnn2_rocket, name='dice_rocket_2')
        else:
            dnn2 = prelu(dnn2, scope='prelu_2')
            dnn2_rocket = prelu(dnn2_rocket, scope='prelu_rocket_2')


        dnn3 = tf.layers.dense(dnn2, 2, activation=None, kernel_initializer=get_tf_initializer(), name='f3')
        dnn3_rocket = tf.layers.dense(dnn2_rocket, 2, activation=None, kernel_initializer=get_tf_initializer(), name='f3_rocket')

        self.y_hat_booster = tf.nn.softmax(dnn3) + 0.00000001
        self.y_hat_light = tf.nn.softmax(dnn3_rocket) + 0.00000001
        
        self.y_hat = self.y_hat_light

        with tf.name_scope('Metrics'):
            # Cross-entropy loss and optimizer initialization
            ctr_loss = - tf.reduce_mean(tf.log(self.y_hat_booster) * self.tensors.target)
            ctr_loss_rocket = - tf.reduce_mean(tf.log(self.y_hat_light) * self.tensors.target)
            hint_loss = tf.reduce_mean((tf.stop_gradient(dnn3) - dnn3_rocket)**2)

            self.loss = ctr_loss + ctr_loss_rocket + 0.3 * hint_loss
            self.ctr_loss = ctr_loss
            self.ctr_loss_rocket = ctr_loss_rocket
            self.hint_loss = hint_loss

            if self.use_negsampling:
                self.loss += self.aux_loss

            # Accuracy metric
            self.accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.round(self.y_hat), self.tensors.target), tf.float32))
Example #5
0
    def __init__(self,user_count,item_count,cate_count,cate_list):

        # self.u = tf.placeholder(tf.int32,[None,],name='user')
        self.i = tf.placeholder(tf.int32,[None,],name='item')
        self.j = tf.placeholder(tf.int32,[None,],name='item_j')
        self.y = tf.placeholder(tf.float32,[None,],name='label')
        self.hist_i = tf.placeholder(tf.int32,[None,None],name='history_i')
        self.sl = tf.placeholder(tf.int32, [None,] , name='sequence_length')

        self.lr = tf.placeholder(tf.float64,name='learning_rate')

        hidden_units = 32

        # user_emb_w = tf.get_variable("user_emb_w",[user_count,hidden_units])
        item_emb_w = tf.get_variable("item_emb_w",[item_count,hidden_units//2])
        item_b = tf.get_variable("item_b",[item_count],initializer=tf.constant_initializer(0.0))

        cate_emb_w = tf.get_variable("cate_emb_w",[cate_count,hidden_units//2])
        cate_list = tf.convert_to_tensor(cate_list,dtype=tf.int64)

        # u_emb = tf.nn.embedding_lookup(user_emb_w,self.u)

        # ic是item到category的转换
        self.ic = tf.gather(cate_list,self.i)
        i_emb = tf.concat(values=[
            tf.nn.embedding_lookup(item_emb_w,self.i),
            tf.nn.embedding_lookup(cate_emb_w,self.ic)
        ],axis=1)

        i_b = tf.gather(item_b,self.i)

        self.jc = tf.gather(cate_list, self.j)
        j_emb = tf.concat([
            tf.nn.embedding_lookup(item_emb_w, self.j),
            tf.nn.embedding_lookup(cate_emb_w, self.jc),
        ], axis=1)
        j_b = tf.gather(item_b, self.j)

        self.hc = tf.gather(cate_list, self.hist_i)
        h_emb = tf.concat([
            tf.nn.embedding_lookup(item_emb_w, self.hist_i),
            tf.nn.embedding_lookup(cate_emb_w, self.hc),
        ], axis=2)

        hist = attention(i_emb,h_emb,self.sl)

        hist = tf.layers.batch_normalization(inputs=hist)
        hist = tf.reshape(hist,[-1,hidden_units])
        hist = tf.layers.dense(hist,hidden_units)

        u_emb = hist


        # fcn begin
        din_i = tf.concat([u_emb, i_emb], axis=-1)
        din_i = tf.layers.batch_normalization(inputs=din_i, name='b1')
        d_layer_1_i = tf.layers.dense(din_i, 80, activation=None, name='f1')
        d_layer_1_i = dice(d_layer_1_i, name='dice_1_i')
        d_layer_2_i = tf.layers.dense(d_layer_1_i, 40, activation=None, name='f2')
        d_layer_2_i = dice(d_layer_2_i, name='dice_2_i')
        d_layer_3_i = tf.layers.dense(d_layer_2_i, 1, activation=None, name='f3')

        din_j = tf.concat([u_emb, j_emb], axis=-1)
        din_j = tf.layers.batch_normalization(inputs=din_j, name='b1', reuse=True)
        d_layer_1_j = tf.layers.dense(din_j, 80, activation=None, name='f1', reuse=True)
        d_layer_1_j = dice(d_layer_1_j, name='dice_1_j')
        d_layer_2_j = tf.layers.dense(d_layer_1_j, 40, activation=None, name='f2', reuse=True)
        d_layer_2_j = dice(d_layer_2_j, name='dice_2_j')
        d_layer_3_j = tf.layers.dense(d_layer_2_j, 1, activation=None, name='f3', reuse=True)

        d_layer_3_i = tf.reshape(d_layer_3_i, [-1])
        d_layer_3_j = tf.reshape(d_layer_3_j, [-1])

        x = i_b - j_b + d_layer_3_i - d_layer_3_j  # [B]
        self.logits = i_b + d_layer_3_i


        # logits for all item:
        u_emb_all = tf.expand_dims(u_emb, 1)
        u_emb_all = tf.tile(u_emb_all, [1, item_count, 1])

        all_emb = tf.concat([
            item_emb_w,
            tf.nn.embedding_lookup(cate_emb_w, cate_list)
        ], axis=1)
        all_emb = tf.expand_dims(all_emb, 0)
        all_emb = tf.tile(all_emb, [512, 1, 1])
        din_all = tf.concat([u_emb_all, all_emb], axis=-1)
        din_all = tf.layers.batch_normalization(inputs=din_all, name='b1', reuse=True)
        d_layer_1_all = tf.layers.dense(din_all, 80, activation=None, name='f1', reuse=True)
        d_layer_1_all = dice(d_layer_1_all, name='dice_1_all')
        d_layer_2_all = tf.layers.dense(d_layer_1_all, 40, activation=None, name='f2', reuse=True)
        d_layer_2_all = dice(d_layer_2_all, name='dice_2_all')
        d_layer_3_all = tf.layers.dense(d_layer_2_all, 1, activation=None, name='f3', reuse=True)
        d_layer_3_all = tf.reshape(d_layer_3_all, [-1, item_count])


        self.logits_all = tf.sigmoid(item_b + d_layer_3_all)
        # -- fcn end -------

        self.mf_auc = tf.reduce_mean(tf.to_float(x > 0))
        self.score_i = tf.sigmoid(i_b + d_layer_3_i)
        self.score_j = tf.sigmoid(j_b + d_layer_3_j)
        self.score_i = tf.reshape(self.score_i, [-1, 1])
        self.score_j = tf.reshape(self.score_j, [-1, 1])
        self.p_and_n = tf.concat([self.score_i, self.score_j], axis=-1)


        # Step variable
        self.global_step = tf.Variable(0, trainable=False, name='global_step')
        self.global_epoch_step = \
            tf.Variable(0, trainable=False, name='global_epoch_step')
        self.global_epoch_step_op = \
            tf.assign(self.global_epoch_step, self.global_epoch_step + 1)

        # loss and train
        self.loss = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(
                logits=self.logits,
                labels=self.y)
        )

        trainable_params = tf.trainable_variables()
        self.train_op = tf.train.GradientDescentOptimizer(learning_rate=self.lr).minimize(self.loss)
Example #6
0
'''
import matplotlib.pyplot as plt
from Dice import dice

#1) Set up dice: elements
#2) Set up number of dice: dice objects
#3) Set up number of rolls and collect data
#4) Perform probability analysis

elements = [int(x) for x in raw_input('What are the faces of the dice? ').split(' ')]
number_of_dice = int(raw_input('How many dice do you want? '))
number_of_rolls = int(raw_input('How many rolls would you like to perform? '))

#Set up dice

Dice = [dice(elements) for x in range(0, number_of_dice)]
	
#How should I store data?  Dictionary is fast. 

data = {x: 0 for x in range(number_of_dice*min(elements), number_of_dice*max(elements)+1)}
data1 = {x: 0 for x in range(number_of_dice*min(elements), number_of_dice*max(elements)+1)}

#Rolling time! (2 experiments: One with no reroll, the other with blank rerolls) 

#Experiment 1
for n in range(0, number_of_rolls):
	die_sum = 0
	for die in Dice:
		die.roll()
		die_sum += die.face
	data[die_sum] += 1
Example #7
0
    def __init__(self, item_count, keyword_count, tag1_count, tag2_count, tag3_count, ks1_count,
                 ks2_count, user_features_num, item_features_num, user_features_dim, item_features_dim,
                 embedding_size=8, hidden_units=64, deep_layer=[80, 40, 1], lamda=0.1):
        # item_count:item个数
        # keyword_count:关键词个数
        # tag1_count:v4标签1个数
        # tag2_count:v4标签2个数
        # tag3_count:v4标签3个数
        # ks1_count:v4关键词1个数
        # ks2_count:v4关键词2个数
        # user_features_num:用户固有属性个数
        # item_features_num:物品固有属性个数
        # user_features_dim:用户所有固有属性的唯一值个数
        # item_features_dim:物品固有属性的唯一值个数

        # 调参
        # embedding_size :普通嵌入层神经元个数
        # hidden_units:注意力机制嵌入层的隐藏层神经元个数
        # deep_layer:全连接层的神经元个数

        # 组成
        # user_feature 用户固有属性
        # item_feature 产品固有属性
        # item 当前物品
        # keyword v2关键词
        # tag1 语义标签1
        # tag2 语义标签2
        # tag3 语义标签3
        # ks1 语义关键词1
        # ks2 语义关键词2
        # hist_item 历史记录的物品
        # hist_keyword 历史记录的关键词

        # sl 用户,历史评论产品的个数

        # 输入
        self.y = tf.placeholder(tf.float32, [None, ], name='y')  # [B]
        # [B, user_features_num]
        self.user_feature = tf.placeholder(tf.int32, [None, user_features_num], name='user_feature')
        # [B, item_features_num]
        self.item_feature = tf.placeholder(tf.int32, [None, item_features_num], name='item_feature')

        # self.item = tf.placeholder(tf.int32, [None, ], name='item')  # [B]
        self.item_id = tf.placeholder(tf.int32, [None, ], name='item_id')
        self.keyword = tf.placeholder(tf.int32, [None, ], name='keyword')
        self.tag1 = tf.placeholder(tf.int32, [None, ], name='tag1')
        self.tag2 = tf.placeholder(tf.int32, [None, ], name='tag2')
        self.tag3 = tf.placeholder(tf.int32, [None, ], name='tag3')
        self.ks1 = tf.placeholder(tf.int32, [None, ], name='ks1')
        self.ks2 = tf.placeholder(tf.int32, [None, ], name='ks2')

        # self.hist_item = tf.placeholder(tf.int32, [None, None], name='hist_item')  # [B, T] hist就是history,历史评论过的产品
        self.hist_item = tf.placeholder(tf.int32, [None, None], name='hist_item')  # [B, T]
        self.hist_keyword = tf.placeholder(tf.int32, [None, None], name='hist_keyword')  # [B, T]
        self.hist_tag1 = tf.placeholder(tf.int32, [None, None], name='hist_tag1')  # [B, T]
        self.hist_tag2 = tf.placeholder(tf.int32, [None, None], name='hist_tag2')  # [B, T]
        self.hist_tag3 = tf.placeholder(tf.int32, [None, None], name='hist_tag3')  # [B, T]
        self.hist_ks1 = tf.placeholder(tf.int32, [None, None], name='hist_ks1')  # [B, T]
        self.hist_ks2 = tf.placeholder(tf.int32, [None, None], name='hist_ks2')  # [B, T]

        self.sl = tf.placeholder(tf.int32, [None, ], name='sl')  # [B]
        self.lr = tf.placeholder(tf.float64, [], name='lr')  # [1,1]
        self.keep_prob = tf.placeholder(tf.float32, [], name='keep_prob')

        # 变量
        # [user_count,self.embedding_size] user_count一个特征的维度,高维
        user_feature_emb_w = tf.get_variable("user_feature_emb_w",
                                             [user_features_dim,
                                              embedding_size],
                                             initializer=tf.contrib.layers.xavier_initializer(
                                                 uniform=False,
                                                 dtype=tf.float32))

        item_feature_emb_w = tf.get_variable("item_feature_emb_w",
                                             [item_features_dim, embedding_size],
                                             initializer=tf.contrib.layers.xavier_initializer(
                                                 uniform=False,
                                                 dtype=tf.float32))

        # item_emb_w = tf.get_variable("item_emb_w", [item_count, hidden_units // 3])  # 9//2 输出结果 4 , 9.0//2.0 输出结果 4.0
        # 偏差 b
        item_b = tf.get_variable("item_b", [item_count],
                                 initializer=tf.constant_initializer(0.0))
        item_emb_w = tf.get_variable("item_emb_w",
                                     [item_count, hidden_units],
                                     initializer=tf.contrib.layers.xavier_initializer(uniform=False,
                                                                                      dtype=tf.float32))
        keyword_emb_w = tf.get_variable("keyword_emb_w",
                                        [keyword_count, hidden_units],
                                        initializer=tf.contrib.layers.xavier_initializer(uniform=False,
                                                                                         dtype=tf.float32))
        tag1_emb_w = tf.get_variable("tag1_emb_w",
                                     [tag1_count, hidden_units],
                                     initializer=tf.contrib.layers.xavier_initializer(uniform=False, dtype=tf.float32))
        tag2_emb_w = tf.get_variable("tag2_emb_w",
                                     [tag2_count, hidden_units],
                                     initializer=tf.contrib.layers.xavier_initializer(uniform=False, dtype=tf.float32))
        tag3_emb_w = tf.get_variable("tag3_emb_w",
                                     [tag3_count, hidden_units],
                                     initializer=tf.contrib.layers.xavier_initializer(uniform=False, dtype=tf.float32))
        ks1_emb_w = tf.get_variable("ks1_emb_w",
                                    [ks1_count, hidden_units],
                                    initializer=tf.contrib.layers.xavier_initializer(uniform=False, dtype=tf.float32))
        ks2_emb_w = tf.get_variable("ks2_emb_w",
                                    [ks2_count, hidden_units],
                                    initializer=tf.contrib.layers.xavier_initializer(uniform=False, dtype=tf.float32))

        tf.summary.histogram("user_feature_emb_w", user_feature_emb_w)
        tf.summary.histogram("item_feature_emb_w", item_feature_emb_w)
        # tf.summary.histogram("item_emb_w", item_emb_w)
        tf.summary.histogram("item_b", item_b)
        tf.summary.histogram("item_emb_w", item_emb_w)
        tf.summary.histogram("keyword_emb_w", keyword_emb_w)
        tf.summary.histogram("tag1_emb_w", tag1_emb_w)
        tf.summary.histogram("tag2_emb_w", tag2_emb_w)
        tf.summary.histogram("tag3_emb_w", tag3_emb_w)
        tf.summary.histogram("ks1_emb_w", ks1_emb_w)
        tf.summary.histogram("ks2_emb_w", ks2_emb_w)

        # hidden_units = H

        # -- 嵌入层 start ---

        # tf.nn.embedding_lookup(item_emb_w, self.item)  # [B ,hidden_units // 3]
        item_emb = tf.nn.embedding_lookup(item_emb_w, self.item_id)  # [B ,hidden_units // 2] = [B, H // 3]
        keyword_emb = tf.nn.embedding_lookup(keyword_emb_w, self.keyword)
        tag1_emb = tf.nn.embedding_lookup(tag1_emb_w, self.tag1)
        tag2_emb = tf.nn.embedding_lookup(tag2_emb_w, self.tag2)
        tag3_emb = tf.nn.embedding_lookup(tag3_emb_w, self.tag3)
        ks1_emb = tf.nn.embedding_lookup(ks1_emb_w, self.ks1)
        ks2_emb = tf.nn.embedding_lookup(ks2_emb_w, self.ks2)

        i_b = tf.gather(item_b, self.item_id)

        # 在shape【0,1,2】某一个维度上连接
        # tf.nn.embedding_lookup(item_emb_w, self.hist_item) # [B, T, hidden_units // 3]
        hist_item_emb = tf.nn.embedding_lookup(item_emb_w, self.hist_item)  # [B, T, hidden_units // 3]
        hist_keyword_emb = tf.nn.embedding_lookup(keyword_emb_w, self.hist_keyword)  # [B, T, hidden_units // 3]
        hist_tag1_emb = tf.nn.embedding_lookup(tag1_emb_w, self.hist_tag1)  # [B, T, hidden_units // 3]
        hist_tag2_emb = tf.nn.embedding_lookup(tag2_emb_w, self.hist_tag2)  # [B, T, hidden_units // 3]
        hist_tag3_emb = tf.nn.embedding_lookup(tag3_emb_w, self.hist_tag3)  # [B, T, hidden_units // 3]
        hist_ks1_emb = tf.nn.embedding_lookup(ks1_emb_w, self.hist_ks1)  # [B, T, hidden_units // 3]
        hist_ks2_emb = tf.nn.embedding_lookup(ks2_emb_w, self.hist_ks2)  # [B, T, hidden_units // 3]
        # [B, T, H]
        # -- 嵌入层 end ---

        # -- attention start ---
        hist_item = attention(item_emb, hist_item_emb, self.sl)  # [B, 1, H]
        hist_item = tf.layers.batch_normalization(inputs=hist_item)
        hist_item = tf.reshape(hist_item, [-1, hidden_units])  # [B, H]
        hist_item = tf.layers.dense(hist_item, hidden_units)  # [B, H]

        hist_keyword = attention(keyword_emb, hist_keyword_emb, self.sl)  # [B, 1, H]
        hist_keyword = tf.layers.batch_normalization(inputs=hist_keyword)
        hist_keyword = tf.reshape(hist_keyword, [-1, hidden_units])  # [B, H]
        hist_keyword = tf.layers.dense(hist_keyword, hidden_units)  # [B, H]

        hist_tag1 = attention(tag1_emb, hist_tag1_emb, self.sl)  # [B, 1, H]
        hist_tag1 = tf.layers.batch_normalization(inputs=hist_tag1)
        hist_tag1 = tf.reshape(hist_tag1, [-1, hidden_units])  # [B, H]
        hist_tag1 = tf.layers.dense(hist_tag1, hidden_units)  # [B, H]

        hist_tag2 = attention(tag2_emb, hist_tag2_emb, self.sl)  # [B, 1, H]
        hist_tag2 = tf.layers.batch_normalization(inputs=hist_tag2)
        hist_tag2 = tf.reshape(hist_tag2, [-1, hidden_units])  # [B, H]
        hist_tag2 = tf.layers.dense(hist_tag2, hidden_units)  # [B, H]

        hist_tag3 = attention(tag3_emb, hist_tag3_emb, self.sl)  # [B, 1, H]
        hist_tag3 = tf.layers.batch_normalization(inputs=hist_tag3)
        hist_tag3 = tf.reshape(hist_tag3, [-1, hidden_units])  # [B, H]
        hist_tag3 = tf.layers.dense(hist_tag3, hidden_units)  # [B, H]

        hist_ks1 = attention(ks1_emb, hist_ks1_emb, self.sl)  # [B, 1, H]
        hist_ks1 = tf.layers.batch_normalization(inputs=hist_ks1)
        hist_ks1 = tf.reshape(hist_ks1, [-1, hidden_units])  # [B, H]
        hist_ks1 = tf.layers.dense(hist_ks1, hidden_units)  # [B, H]

        hist_ks2 = attention(ks2_emb, hist_ks2_emb, self.sl)  # [B, 1, H]
        hist_ks2 = tf.layers.batch_normalization(inputs=hist_ks2)
        hist_ks2 = tf.reshape(hist_ks2, [-1, hidden_units])  # [B, H]
        hist_ks2 = tf.layers.dense(hist_ks2, hidden_units)  # [B, H]

        # -- attention end ---

        # -- 普通嵌入层 --
        user_feature = tf.nn.embedding_lookup(user_feature_emb_w,
                                              self.user_feature)  # [B, embedding_size, user_features_num]
        user_feature = tf.reshape(user_feature, [-1, embedding_size * user_features_num])

        item_feature = tf.nn.embedding_lookup(item_feature_emb_w,
                                              self.item_feature)  # [B, embedding_size, user_features_num]
        item_feature = tf.reshape(item_feature, [-1, embedding_size * item_features_num])

        # -- 普通嵌入层 --

        # -- fcn begin -------
        # -- 训练集全连接层 开始 -------
        din_i = tf.concat(
            [hist_item, hist_keyword, hist_tag1, hist_tag2, hist_tag3, hist_ks1, hist_ks2,
             item_emb, keyword_emb, tag1_emb, tag2_emb, tag3_emb, ks1_emb, ks2_emb,
             user_feature, item_feature], axis=-1)

        d_layer_1_i = tf.layers.dense(din_i, deep_layer[0], activation=None, name='f1')  # 全连接层  [B, 80]
        d_layer_1_i = dice(d_layer_1_i, name='dice_1_i')
        d_layer_1_i = tf.nn.dropout(d_layer_1_i, self.keep_prob)
        d_layer_1_i = tf.layers.batch_normalization(inputs=d_layer_1_i, name='b1')

        d_layer_2_i = tf.layers.dense(d_layer_1_i, deep_layer[1], activation=None, name='f2')
        d_layer_2_i = dice(d_layer_2_i, name='dice_2_i')
        d_layer_2_i = tf.nn.dropout(d_layer_2_i, self.keep_prob)
        d_layer_2_i = tf.layers.batch_normalization(inputs=d_layer_2_i, name='b2')

        d_layer_3_i = tf.layers.dense(d_layer_2_i, deep_layer[2], activation=None, name='f3')

        d_layer_3_i = tf.reshape(d_layer_3_i, [-1])  # 展开成行向量

        self.logits = i_b + d_layer_3_i
        self.sig_logits = tf.sigmoid(self.logits, name='sig_logits')
        # -- 训练集全连接层 结束 -------

        # Step variable
        self.global_step = tf.Variable(0, trainable=False, name='global_step')
        self.global_epoch_step = \
            tf.Variable(0, trainable=False, name='global_epoch_step')
        self.global_epoch_step_op = \
            tf.assign(self.global_epoch_step, self.global_epoch_step + 1)

        with tf.name_scope('loss'):  # 损失
            self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.logits, labels=self.y),
                                       name='loss') + tf.losses.get_regularization_loss()
            tf.summary.scalar('loss', self.loss)

        optimizer = tf.train.AdamOptimizer(self.lr, name='adam')
        self.train_op = optimizer.minimize(self.loss)
Example #8
0
File: model_.py Project: zaf11/DIN
    def __init__(self, user_count, item_count, cate_count, cate_list):
        # shape: [B],  user id。 (B:batch size)
        self.u = tf.placeholder(tf.int32, [
            None,
        ])
        # shape: [B]  i: 正样本的item
        self.i = tf.placeholder(tf.int32, [
            None,
        ])
        # shape: [B]  j: 负样本的item
        self.j = tf.placeholder(tf.int32, [
            None,
        ])
        # shape: [B], y: label
        self.y = tf.placeholder(tf.float32, [
            None,
        ])
        # shape: [B, T] #用户行为特征(User Behavior)中的item序列。
        # T为最长序列的长度,用户序列少于T的用0填充
        self.hist_i = tf.placeholder(tf.int32, [None, None])
        # shape: [B]; sl:sequence length,User Behavior中每个用户行为序列的真实长度
        self.sl = tf.placeholder(tf.int32, [
            None,
        ])
        # learning rate
        self.lr = tf.placeholder(tf.float64, [])

        hidden_units = 128
        # shape: [U, H], user_id的embedding weight. U是user_id的hash bucket size
        user_emb_w = tf.get_variable("user_emb_w", [user_count, hidden_units])
        # shape: [I, H//2], item_id的embedding weight. I是item_id的hash bucket size
        item_emb_w = tf.get_variable(
            "item_emb_w", [item_count, hidden_units // 2])  # [I, H//2]
        # shape: [I], item_id的embedding bias
        item_b = tf.get_variable("item_b", [item_count],
                                 initializer=tf.constant_initializer(0.0))
        # shape: [C, H//2], cate_id的embedding weight.
        cate_emb_w = tf.get_variable("cate_emb_w",
                                     [cate_count, hidden_units // 2])
        # shape: [C, H//2]
        cate_list = tf.convert_to_tensor(cate_list, dtype=tf.int64)

        # 正样本的cate
        ic = tf.gather(cate_list, self.i)
        # 正样本item_emb和cate_emb拼接,shape: [B, H]
        i_emb = tf.concat(values=[
            tf.nn.embedding_lookup(item_emb_w, self.i),
            tf.nn.embedding_lookup(cate_emb_w, ic),
        ],
                          axis=1)
        # 偏置b
        i_b = tf.gather(item_b, self.i)

        # 从cate_list中取出负样本的cate
        jc = tf.gather(cate_list, self.j)
        # 负样本item_emb和cate_emb拼接,shape: [B, H]
        j_emb = tf.concat([
            tf.nn.embedding_lookup(item_emb_w, self.j),
            tf.nn.embedding_lookup(cate_emb_w, jc),
        ],
                          axis=1)
        # 偏置b
        j_b = tf.gather(item_b, self.j)

        # 用户行为序列(User Behavior)中的cate序列
        hc = tf.gather(cate_list, self.hist_i)

        # 用户行为序列(User Behavior)物品的item_emb和cate_emb拼接,shape: [B, T, H]
        h_emb = tf.concat([
            tf.nn.embedding_lookup(item_emb_w, self.hist_i),
            tf.nn.embedding_lookup(cate_emb_w, hc),
        ],
                          axis=2)
        # attention操作
        hist_i = attention(i_emb, h_emb, self.sl)  # B * 1 * H
        # -- attention end ---

        hist = tf.layers.batch_normalization(inputs=hist_i)
        hist = tf.reshape(hist, [-1, hidden_units])  # B * H

        # 添加一层全连接层,hist为输入,hidden_units为输出维数
        hist = tf.layers.dense(hist, hidden_units)  #为啥?

        u_emb = hist

        # 下面两个全连接用来计算y',i为正样本,j为负样本
        # fcn begin
        din_i = tf.concat([u_emb, i_emb], axis=-1)
        din_i = tf.layers.batch_normalization(inputs=din_i, name='b1')
        d_layer_1_i = tf.layers.dense(din_i, 80, activation=None, name='f1')
        d_layer_1_i = dice(d_layer_1_i, name='dice_1_i')
        d_layer_2_i = tf.layers.dense(d_layer_1_i,
                                      40,
                                      activation=None,
                                      name='f2')
        d_layer_2_i = dice(d_layer_2_i, name='dice_2_i')
        d_layer_3_i = tf.layers.dense(d_layer_2_i,
                                      1,
                                      activation=None,
                                      name='f3')

        din_j = tf.concat([u_emb, j_emb], axis=-1)
        din_j = tf.layers.batch_normalization(inputs=din_j,
                                              name='b1',
                                              reuse=True)
        d_layer_1_j = tf.layers.dense(din_j,
                                      80,
                                      activation=None,
                                      name='f1',
                                      reuse=True)
        d_layer_1_j = dice(d_layer_1_j, name='dice_1_j')
        d_layer_2_j = tf.layers.dense(d_layer_1_j,
                                      40,
                                      activation=None,
                                      name='f2',
                                      reuse=True)
        d_layer_2_j = dice(d_layer_2_j, name='dice_2_j')
        d_layer_3_j = tf.layers.dense(d_layer_2_j,
                                      1,
                                      activation=None,
                                      name='f3',
                                      reuse=True)

        d_layer_3_i = tf.reshape(d_layer_3_i, [-1])  # [B, 1]
        d_layer_3_j = tf.reshape(d_layer_3_j, [-1])  # [B, 1]

        # 预测的(y正-y负)
        x = i_b - j_b + d_layer_3_i - d_layer_3_j  # [B]
        # 预测的(y正)
        self.logits = i_b + d_layer_3_i

        # logits for all item:
        u_emb_all = tf.expand_dims(u_emb, 1)
        u_emb_all = tf.tile(u_emb_all, [1, item_count, 1])
        # 将所有的除u_emb_all外的embedding,concat到一起
        all_emb = tf.concat(
            [item_emb_w,
             tf.nn.embedding_lookup(cate_emb_w, cate_list)],
            axis=1)
        all_emb = tf.expand_dims(all_emb, 0)
        all_emb = tf.tile(all_emb, [512, 1, 1])
        # 将所有的embedding,concat到一起
        din_all = tf.concat([u_emb_all, all_emb], axis=-1)
        din_all = tf.layers.batch_normalization(inputs=din_all,
                                                name='b1',
                                                reuse=True)
        d_layer_1_all = tf.layers.dense(din_all,
                                        80,
                                        activation=None,
                                        name='f1',
                                        reuse=True)
        d_layer_1_all = dice(d_layer_1_all, name='dice_1_all')
        d_layer_2_all = tf.layers.dense(d_layer_1_all,
                                        40,
                                        activation=None,
                                        name='f2',
                                        reuse=True)
        d_layer_2_all = dice(d_layer_2_all, name='dice_2_all')
        d_layer_3_all = tf.layers.dense(d_layer_2_all,
                                        1,
                                        activation=None,
                                        name='f3',
                                        reuse=True)
        d_layer_3_all = tf.reshape(d_layer_3_all, [-1, item_count])

        self.logits_all = tf.sigmoid(item_b + d_layer_3_all)
        # -- fcn end -------

        self.mf_auc = tf.reduce_mean(tf.to_float(x > 0))
        self.score_i = tf.sigmoid(i_b + d_layer_3_i)
        self.score_j = tf.sigmoid(j_b + d_layer_3_j)
        self.score_i = tf.reshape(self.score_i, [-1, 1])
        self.score_j = tf.reshape(self.score_j, [-1, 1])
        self.p_and_n = tf.concat([self.score_i, self.score_j], axis=-1)

        # Step variable
        self.global_step = tf.Variable(0, trainable=False, name='global_step')
        self.global_epoch_step = tf.Variable(0,
                                             trainable=False,
                                             name='global_epoch_step')
        self.global_epoch_step_op = tf.assign(self.global_epoch_step,
                                              self.global_epoch_step + 1)

        # loss and train
        self.loss = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(logits=self.logits,
                                                    labels=self.y))
        trainable_params = tf.trainable_variables()
        self.train_op = tf.train.GradientDescentOptimizer(
            learning_rate=self.lr).minimize(self.loss)