<jupyter_start><jupyter_text>构建训练需要的样本数据<jupyter_code># 训练集数据
class DataInput(object):
    def __init__(self,data, batch_size):
        # len(data), batch_size= 2608764, 32
        self.batch_size = batch_size
        self.data = data
        self.epoch_size = len(self.data) // self.batch_size
        # 计算迭代次数
        self.epoch_size + 1 if self.epoch_size * self.batch_size <len(self.data) else self.epoch_size
        self.idx = 0
    def __iter__(self):
        return self
    def __next__(self):
        if self.idx == self.epoch_size:
            raise StopIteration
        start, end = self.idx*self.batch_size, min((self.idx+1)*self.batch_size, len(self.data))
        b_data = self.data[start: end]
        self.idx += 1
        user_id, item_id, y, sample_len = [],[],[],[]
        for i in b_data:
            user_id.append(i[0])
            item_id.append(i[2])
            y.append(i[3])
            sample_len.append(len(i[1]))
        # 获取最长列表
        max_sl = max(sample_len)
        # 获取兴趣矩阵(以最长的为主) [用户个数，最长兴趣列表长度]
        hist_i = np.zeros([len(b_data),max_sl], np.int64)
        # 填充兴趣列表，并且用0填充的方法
        k = 0
        for l in b_data:
            for j in range(len(l[1])):
                hist_i[k][j] = l[1][j]
            k += 1
        return self.idx, (user_id, item_id, y, hist_i, sample_len)
# user_count item_count cate_count cate_list
random.seed(1234)
np.random.seed(1234)
tf.set_random_seed(1234)

predict_users_num = 1000
predict_batch_size = 32
predict_ads_num = 100

train_batch_size, test_barch_size = 32, 512<jupyter_output><empty_output><jupyter_text>定义baselines模型<jupyter_code>class Model(object):
    # 传入用户id列表，物品id列表，不感兴趣的item_id, [0,1]标签，用户兴趣二维列表，各个用户兴趣个数列表，学习率
    def __init__(self,user_count,item_count,cate_count,cate_list):
        self.u = tf.placeholder(tf.int32, [None, ])# user_id
        self.i = tf.placeholder(tf.int32, [None, ])# item_id
        self.j = tf.placeholder(tf.int32, [None, ])# not_like_id
        self.y = tf.placeholder(tf.float32, [None, ]) # lable
        self.hist_i = tf.placeholder(tf.int32, [None,None])# [Batch, like] 
        self.sl = tf.placeholder(tf.int32,[None, ]) # sample len list
        self.lr = tf.placeholder(tf.float64, []) # learning rate
        # 定义神经网络的层数
        hidden_units = 128
        with tf.variable_scope("wight",reuse=tf.AUTO_REUSE):
        # 定义变量 用户层数是128层，物品层数是64层，类别变量128层
            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)
        ic = tf.gather(cate_list,self.i)
        i_b = tf.gather(item_b, self.i)
        # 对H层进行拼接
        item_emb = tf.concat(
            values=[tf.nn.embedding_lookup(item_emb_w, self.i), tf.nn.embedding_lookup(cate_emb_w,ic)],
            axis=1
        )
        hc = tf.gather(cate_list,self.hist_i)
        # 对H层进行拼接
        hist_emb = tf.concat(
            values=[tf.nn.embedding_lookup(item_emb_w, self.hist_i),tf.nn.embedding_lookup(cate_emb_w,hc)],
            axis = 2
        )
        # 对未购买的物品对应的W进行归0
        mask = tf.sequence_mask(self.sl, tf.shape(hist_emb)[1], dtype=tf.float32)
        mask = tf.tile(tf.expand_dims(mask, -1), [1, 1, tf.shape(hist_emb)[2]])
        hist_emb *= mask
        hist = hist_emb
        # 对每个商品进行拼接
        hist = tf.reduce_sum(hist, 1)
        # Sum pooling
        hist = tf.div(hist , tf.cast(tf.tile(tf.expand_dims(self.sl,1),[1,128]), tf.float32))
        # 归一化以及前向传播
        hist = tf.layers.batch_normalization(inputs=hist)
        hist = tf.reshape(hist, [-1, hidden_units])
        hist = tf.layers.dense(hist, hidden_units)
        user_emb = hist
        base_i = tf.concat([user_emb, item_emb], axis = -1)
        base_i = tf.layers.batch_normalization(inputs=base_i, name="b1",reuse=tf.AUTO_REUSE)
        d_layer_1_i = tf.layers.dense(base_i, 80, activation=tf.nn.sigmoid, name="f1",reuse=tf.AUTO_REUSE)
        d_layer_2_i = tf.layers.dense(base_i, 40, activation=tf.nn.sigmoid, name="f2",reuse=tf.AUTO_REUSE)
        d_layer_3_i = tf.layers.dense(base_i, 1, activation=None, name="f3",reuse=tf.AUTO_REUSE)
        # 特征平铺
        d_layer_3_i = tf.reshape(d_layer_3_i,[-1])
        self.y_p = i_b + d_layer_3_i
        # 定义一轮的epoch的步数
        self.global_step = tf.Variable(0, trainable=False, name='global_step')
        # 定义epoch的步数
        self.global_epoch_step = tf.Variable(0,trainable=False, name="global_epoch_step")
        self.global_epoch_step_op = tf.assign(self.global_epoch_step, 1)
        self.loss = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(
            logits = self.y_p,
            labels = self.y
            )
        )
        
        trainable_params = tf.trainable_variables()
        self.opt = tf.train.GradientDescentOptimizer(learning_rate=self.lr)
        gradients = tf.gradients(self.loss, trainable_params)
        # 对梯度进行修剪
        clip_gradients, _ = tf.clip_by_global_norm(gradients, 5)
        self.train_op = self.opt.apply_gradients(
            zip(clip_gradients, trainable_params),global_step=self.global_step
        )
        
    def train(self, sess, item, lr):
        loss, _ = sess.run([self.loss, self.train_op], feed_dict={
            self.u: item[0],
            self.i: item[1],
            self.y: item[2],
            self.hist_i: item[3],
            self.sl: item[4],
            self.lr: lr,
        })
        return loss

gpu_options = tf.GPUOptions(allow_growth=True)
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
    model = Model(user_count, item_count, cate_count, cate_list)
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    lr = 0.1
    start_time = time.time()
    for epoch in range(2):
        random.shuffle(train_set)
        loss_sum = 0.0
        for idx, item in DataInput(train_set, train_batch_size):
            loss = model.train(sess, item, lr)
            loss_sum += loss
            if(model.global_step.eval() % 336000 == 0):
                lr  =0.0
        print('epoch: %d, loss: %.4f' % (epoch, loss_sum))<jupyter_output>epoch:0, loss86.0406
epoch:1, loss85.0239
{'fraction_non_alphanumeric': 0.12804782129935505, 'fraction_numerical': 0.02469718420638666, 'mean_word_length': 2.5559284116331096, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '92324', 'license': 'MIT', 'n_tokens_mistral': 2556, 'n_tokens_neox': 2542, 'n_words': 362, 'path': 'RecSys And Deep Learning/Attention/DIN/baselines.ipynb', 'repo_name': 'linohan/Recommender-Systems-Samples', 'size': '6357'}