def _forward_pass(self, ): # 用户的向量表示 with tf.name_scope('user_express'): # 用户隐向量 self.Usr_Emb = tf.nn.embedding_lookup( self.Wu_Emb, tf.cast(self.user_indices, tf.int32)) # [batch_size, dim_k] self.Usr_Feat = tf.nn.embedding_lookup( self.user_emb_feat, tf.cast(self.user_indices, tf.int32)) # [batch_size, 128] self.Usr_Feat_Emb = tf.matmul( self.Usr_Feat, self.W_usr_feat_emb) # [batch_size, dim_k] self.Usr_Feat_Emb = tf.nn.relu(self.Usr_Feat_Emb) # self.Usr_Feat_Emb = tf.layers.dropout(self.Usr_Feat_Emb, self.dropout_emb) # self.Usr_Feat_Emb = self._batch_norm_layer(self.Usr_Feat_Emb, self.train_phase, 'user_emb_bn') self.Usr_Expr_a = self.Usr_Emb + self.Usr_Feat_Emb # [batch_size, dim_k] # 环境的向量表示 with tf.name_scope('context_express'): self.att_oh_ctx = [] self.Ctx_oh_emb = [] self.att_u_ctx = tf.matmul( self.Usr_Emb, self.Wu_ctx_oh_Att) # [batch_size, att_dim_k] for i in range(len(self.ctx_oh_dims)): Ctx_emb_temp = tf.nn.embedding_lookup( self.W_ctx_oh[i], tf.cast(self.ctx_oh[:, i], tf.int32)) # [batch_size, dim_k] att_oh_temp = tf.matmul( Ctx_emb_temp, self.Woh_ctx_Att[i]) # [batch_size, att_dim_k] att_temp = tf.nn.relu(self.att_u_ctx + att_oh_temp + self.b_oh_Att) # [batch_size, att_dim_k] att_temp = tf.matmul( att_temp, self.w_oh_Att) + self.c_oh_Att # [batch_size, 1] self.att_oh_ctx.append(att_temp) self.Ctx_oh_emb.append(Ctx_emb_temp) self.att_oh_ctx = tf.nn.softmax(tf.concat( self.att_oh_ctx, axis=1)) # [batch_size, ctx_dim] for i in range(0, len(self.ctx_oh_dims)): self.Ctx_oh_emb[i] = self.Ctx_oh_emb[i] * self.att_u_ctx[:, i:i + 1] self.Ctx_Emb = tf.add_n(self.Ctx_oh_emb) # [batch_size, dim_k] # 物品的向量表示 with tf.name_scope('item_express'): self.I_Wds_a = tf.SparseTensor( indices=tf.cast(self.item_words_indices_a, dtype=np.int64), values=self.item_words_values_a, dense_shape=[ tf.cast(self.batch_size, dtype=np.int64), self.num_words ]) self.att_u_a = tf.matmul(self.Usr_Emb, self.Wu_oh_Att) # [batch_size, att_dim_k] self.att_ctx = tf.matmul(self.Ctx_Emb, self.Wctx_Att) self.att_oh = [] self.I_Wds_Emb_a = tf.sparse_tensor_dense_matmul( self.I_Wds_a, self.Wwords_Emb) # [batch_size, dim_k] self.I_Wds_Emb_a = tf.nn.relu(self.I_Wds_Emb_a) self.att_I_Wds = tf.matmul(self.I_Wds_Emb_a, self.WW_Att) # 词的attention self.att_I_Wds = tf.nn.relu(self.att_u_a + self.att_ctx + self.att_I_Wds + self.b_oh_Att) self.att_I_Wds = tf.matmul(self.att_I_Wds, self.w_oh_Att) + self.c_oh_Att self.att_oh.append(self.att_I_Wds) vae_encoder = VAE(input_dim=2048, hidden_encoder_dim=1024, latent_dim=96, lam=0.001, kld_loss=0.001) self.I_visual_Emb, self.vae_loss = vae_encoder.get_vae_embbeding( self.visual_emb_feat) # TODO self.I_visual_Emb = self._batch_norm_layer(self.I_visual_Emb, self.train_phase, 'vis_bn') # self.I_visual_Emb = tf.layers.dropout(self.I_visual_Emb, self.dropout_emb) self.att_I_visual = tf.matmul(self.I_visual_Emb, self.W_visual_Att) # 图像的attention self.att_I_visual = tf.nn.relu(self.att_u_a + self.att_ctx + self.att_I_visual + self.b_oh_Att) self.att_I_visual = tf.matmul(self.att_I_visual, self.w_oh_Att) + self.c_oh_Att self.att_oh.append(self.att_I_visual) self.I_LDA_Emb = tf.matmul(self.words_lda, self.W_LDA_emb) # [batch_size, dim_k] self.att_I_LDA = tf.matmul(self.I_LDA_Emb, self.W_LDA_Att) # LDA的attention self.att_I_LDA = tf.nn.relu(self.att_u_a + self.att_ctx + self.att_I_LDA + self.b_oh_Att) self.att_I_LDA = tf.matmul(self.att_I_LDA, self.w_oh_Att) + self.c_oh_Att self.att_oh.append(self.att_I_LDA) self.I_One_hot_a = [] for i in range(len(self.one_hots_dims)): I_Emb_temp_a = tf.nn.embedding_lookup( self.W_one_hots[i], tf.cast(self.one_hots_a[:, i], tf.int32)) # [batch_size, dim_k] att_oh_temp = tf.matmul( I_Emb_temp_a, self.Woh_Att[i]) # [batch_size, att_dim_k] att_temp = tf.nn.relu(self.att_u_a + self.att_ctx + att_oh_temp + self.b_oh_Att) # [batch_size, att_dim_k] att_temp = tf.matmul( att_temp, self.w_oh_Att) + self.c_oh_Att # [batch_size, 1] self.att_oh.append(att_temp) self.I_One_hot_a.append(I_Emb_temp_a) self.att_oh = tf.nn.softmax(tf.concat( self.att_oh, axis=1)) # [batch_size, oh_dim] 第一列是词attention self.I_Wds_Emb_a = self.I_Wds_Emb_a * self.att_oh[:, 0:1] self.I_visual_Emb = self.I_visual_Emb * self.att_oh[:, 1:2] self.I_LDA_Emb = self.I_LDA_Emb * self.att_oh[:, 2:3] for i in range(3, len(self.one_hots_dims) + 3): self.I_One_hot_a[ i - 3] = self.I_One_hot_a[i - 3] * self.att_oh[:, i:i + 1] self.Item_Expr_a = tf.add_n( self.I_One_hot_a + [self.I_visual_Emb, self.I_Wds_Emb_a, self.I_LDA_Emb]) with tf.name_scope('deep'): if self.use_deep: self.Usr_emb_deep = tf.nn.embedding_lookup( self.Wu_deep_emb, tf.cast(self.user_indices, tf.int32)) # [batch_size, dim_k] self.I_Wds_emb_deep = tf.sparse_tensor_dense_matmul( self.I_Wds_a, self.Wwords_deep_emb) # [batch_size, dim_k] self.I_one_hot_deep = [] for i in range(len(self.one_hots_dims)): I_Emb_temp_a = tf.nn.embedding_lookup( self.W_deep_one_hots[i], tf.cast(self.one_hots_a[:, i], tf.int32)) # [batch_size, dim_k] self.I_one_hot_deep.append(I_Emb_temp_a) # self.deep_input = tf.concat([self.num_features, self.Usr_Feat, self.face_num, self.visual_emb_feat], # axis=1) # [batch_size, input_dim] self.deep_input = tf.concat( [self.num_features, self.visual_emb_feat] + self.I_one_hot_deep, axis=1) # 输入加入batch_norm # self.deep_input = self._batch_norm_layer(self.deep_input, self.train_phase, 'input_bn') for i, deep_dim in enumerate(self.deep_dims): self.deep_input = tf.layers.dense( inputs=self.deep_input, kernel_initializer=tf.glorot_uniform_initializer(), units=deep_dim, activation=tf.nn.relu, ) if i == 0: self.deep_input = self._batch_norm_layer( self.deep_input, self.train_phase, 'deep_bn_{}'.format(i)) # 加入dropout self.deep_input = tf.layers.dropout(inputs=self.deep_input, rate=self.dropout_deep) self.deep_output = tf.layers.dense(self.deep_input, 1, activation=None) self.deep_output = tf.reshape(self.deep_output, [-1]) # [batch_size] with tf.name_scope('output'): self.cf_out = tf.layers.dropout(self.Item_Expr_a * self.Usr_Expr_a, rate=self.dropout_emb) self.ctx_usr_out = tf.layers.dropout(self.Ctx_Emb * self.Usr_Expr_a, rate=self.dropout_emb) self.ctx_item_out = tf.layers.dropout(self.Ctx_Emb * self.Item_Expr_a, rate=self.dropout_emb) # self.cf_out, self.ctx_usr_out, self.ctx_item_out = self._attentioned_layer( # [self.cf_out, self.ctx_usr_out, self.ctx_item_out]) # self.in_prd_out = tf.add_n([self.cf_out, self.ctx_usr_out, self.ctx_item_out]) # if self.use_deep: # self.concated = tf.concat([self.in_prd_out, self.deep_input], axis=1) # else: # self.concated = tf.concat([self.in_prd_out], axis=1) if self.use_deep: self.concated = tf.concat([ self.cf_out, self.ctx_usr_out, self.ctx_item_out, self.deep_input ], axis=1) else: self.concated = tf.concat( [self.cf_out, self.ctx_usr_out, self.ctx_item_out], axis=1) self.hidden = self.concated for i, dim in enumerate(self.dim_hidden_out): self.hidden = tf.layers.dense( inputs=self.hidden, kernel_initializer=tf.glorot_uniform_initializer(), units=dim, activation=tf.nn.relu) self.hidden = tf.layers.dropout(inputs=self.hidden, rate=self.dropout_deep) self.bu = tf.nn.embedding_lookup( self.bias_u, tf.cast(self.user_indices, dtype=tf.int32)) self.y_ui_a = tf.layers.dense(self.hidden, 1, activation=None) + self.bu self.y_ui_a = tf.reshape(tf.nn.sigmoid(self.y_ui_a), [-1])
def _forward_pass(self, ): # 用户的向量表示 with tf.name_scope('user_express'): # 用户隐向量 self.Usr_Emb = tf.nn.embedding_lookup(self.Wu_Emb, tf.cast(self.user_indices, tf.int32)) # [batch_size, dim_k] self.Usr_Feat = tf.nn.embedding_lookup(self.user_emb_feat, tf.cast(self.user_indices, tf.int32)) # [batch_size, dim_cf_emb] self.bias_usr_feat_emb = tf.get_variable(shape=[self.dim_k], initializer=tf.zeros_initializer(), dtype=tf.float32, name='bias_usr_feat_emb') self.Usr_Feat_Emb = tf.matmul(self.Usr_Feat, self.W_usr_feat_emb) + self.bias_usr_feat_emb # [batch_size, dim_k] self.Usr_Feat_Emb = tf.nn.relu(self.Usr_Feat_Emb) # self.Usr_Feat_Emb = tf.layers.dropout(self.Usr_Feat_Emb, self.dropout_emb) # self.Usr_Feat_Emb = self._batch_norm_layer(self.Usr_Feat_Emb, self.train_phase, 'user_emb_bn') self.Usr_Expr_a = self.Usr_Emb + self.Usr_Feat_Emb # [batch_size, dim_k] # 环境的向量表示 with tf.name_scope('context_express'): self.bias_ctx_emb = tf.get_variable(shape=[self.dim_k], initializer=tf.zeros_initializer(), dtype=tf.float32, name='bias_ctx_emb') self.Ctx_Emb = tf.matmul(self.num_features, self.W_Ctx) + self.bias_ctx_emb # [batch_size, dim_k] self.Ctx_Emb = self._batch_norm_layer(self.Ctx_Emb, self.train_phase, 'ctx_bn') self.Ctx_Emb = tf.nn.relu(self.Ctx_Emb) # 物品的向量表示 with tf.name_scope('item_express'): self.att_u_a = tf.matmul(self.Usr_Expr_a, self.Wu_oh_Att) # [batch_size, dim_att] self.att_ctx = tf.matmul(self.Ctx_Emb, self.Wctx_Att) self.att_oh = [] vae_encoder = VAE(input_dim=2048, hidden_encoder_dim=1024, latent_dim=self.dim_k, lam=0.001, kld_loss=0.001) self.I_visual_Emb, self.vae_loss = vae_encoder.get_vae_embbeding(self.visual_emb_feat) # TODO self.I_visual_Emb = self._batch_norm_layer(self.I_visual_Emb, self.train_phase, 'vis_bn') # self.I_visual_Emb = tf.layers.dropout(self.I_visual_Emb, self.dropout_emb) self.att_I_visual = tf.matmul(self.I_visual_Emb, self.W_visual_Att) # 图像的attention self.att_I_visual = tf.nn.relu(self.att_u_a + self.att_ctx + self.att_I_visual + self.b_oh_Att) self.att_I_visual = tf.matmul(self.att_I_visual, self.w_oh_Att) + self.c_oh_Att self.att_oh.append(self.att_I_visual) self.bias_lda_emb = tf.get_variable(shape=[self.dim_k], initializer=tf.zeros_initializer(), dtype=tf.float32, name='bias_lda_emb') self.I_LDA_Emb = tf.matmul(self.words_lda, self.W_LDA_emb) + self.bias_lda_emb # [batch_size, dim_k] self.I_LDA_Emb = tf.nn.relu(self.I_LDA_Emb) self.att_I_LDA = tf.matmul(self.I_LDA_Emb, self.W_LDA_Att) # LDA的attention self.att_I_LDA = tf.nn.relu(self.att_u_a + self.att_ctx + self.att_I_LDA + self.b_oh_Att) self.att_I_LDA = tf.matmul(self.att_I_LDA, self.w_oh_Att) + self.c_oh_Att self.att_oh.append(self.att_I_LDA) self.I_One_hot_a = [] for i in range(len(self.one_hots_dims)): I_Emb_temp_a = tf.nn.embedding_lookup(self.W_one_hots[i], tf.cast(self.one_hots_a[:, i], tf.int32)) # [batch_size, dim_k] att_oh_temp = tf.matmul(I_Emb_temp_a, self.Woh_Att[i]) # [batch_size, att_dim_k] att_temp = tf.nn.relu( self.att_u_a + self.att_ctx + att_oh_temp + self.b_oh_Att) # [batch_size, att_dim_k] att_temp = tf.matmul(att_temp, self.w_oh_Att) + self.c_oh_Att # [batch_size, 1] self.att_oh.append(att_temp) self.I_One_hot_a.append(I_Emb_temp_a) self.att_oh = tf.nn.softmax(tf.concat(self.att_oh, axis=1)) # [batch_size, oh_dim] 第一列是词attention self.I_visual_Emb = self.I_visual_Emb * self.att_oh[:, 0:1] self.I_LDA_Emb = self.I_LDA_Emb * self.att_oh[:, 1:2] for i in range(2, len(self.one_hots_dims) + 2): self.I_One_hot_a[i - 2] = self.I_One_hot_a[i - 2] * self.att_oh[:, i:i + 1] self.Item_Expr_a = tf.add_n(self.I_One_hot_a + [self.I_visual_Emb, self.I_LDA_Emb]) with tf.name_scope('deep'): if self.use_deep: self.Usr_emb_deep = tf.nn.embedding_lookup(self.Wu_deep_emb, tf.cast(self.user_indices, tf.int32)) # [batch_size, dim_k] self.I_one_hot_deep = [] for i in range(len(self.one_hots_dims)): I_Emb_temp_a = tf.nn.embedding_lookup(self.W_deep_one_hots[i], tf.cast(self.one_hots_a[:, i], tf.int32)) # [batch_size, dim_k] self.I_one_hot_deep.append(I_Emb_temp_a) # self.deep_input = tf.concat([self.num_features, self.Usr_Feat, self.face_num, self.visual_emb_feat], # axis=1) # [batch_size, input_dim] self.deep_input = tf.concat( [self.num_features, self.visual_emb_feat] + self.I_one_hot_deep, axis=1) # 输入加入batch_norm # self.deep_input = self._batch_norm_layer(self.deep_input, self.train_phase, 'input_bn') for i, deep_dim in enumerate(self.deep_dims): self.deep_input = tf.layers.dense(inputs=self.deep_input, kernel_initializer=tf.glorot_uniform_initializer(), units=deep_dim, activation=tf.nn.relu, ) if i == 0: self.deep_input = self._batch_norm_layer(self.deep_input, self.train_phase, 'deep_bn_{}'.format(i)) # 加入dropout self.deep_input = tf.layers.dropout(inputs=self.deep_input, rate=self.dropout_deep) self.deep_output = tf.layers.dense(self.deep_input, 1, activation=None) self.deep_output = tf.reshape(self.deep_output, [-1]) # [batch_size] with tf.name_scope('output'): self.ctx_item_out = tf.layers.dropout(self.Ctx_Emb * self.Item_Expr_a, rate=self.dropout_emb) self.cf_outs = [] for usr_expr in [self.Usr_Emb, self.Usr_Feat_Emb]: cf_out = tf.layers.dropout(self.Item_Expr_a * usr_expr, rate=self.dropout_emb) ctx_usr_out = tf.layers.dropout(self.Ctx_Emb * usr_expr, rate=self.dropout_emb) self.cf_outs.extend([cf_out, ctx_usr_out]) if self.use_deep: self.concated = tf.concat(self.cf_outs + [self.ctx_item_out, self.deep_input], axis=1) else: self.concated = tf.concat(self.cf_outs + [self.ctx_item_out], axis=1) self.hidden = self.concated for i, dim in enumerate(self.dim_hidden_out): self.hidden = tf.layers.dense(inputs=self.hidden, kernel_initializer=tf.glorot_uniform_initializer(), units=dim, activation=tf.nn.relu) self.hidden = tf.layers.dropout(inputs=self.hidden, rate=self.dropout_deep) self.bu = tf.nn.embedding_lookup(self.bias_u, tf.cast(self.user_indices, dtype=tf.int32)) self.y_ui_a = tf.layers.dense(self.hidden, 1, activation=None) + self.bu self.y_ui_a = tf.reshape(tf.nn.sigmoid(self.y_ui_a), [-1])
def _forward_pass(self, ): # 用户的向量表示 with tf.name_scope('user_express'): # 用户隐向量 self.Usr_Emb = tf.nn.embedding_lookup( self.Wu_Emb, tf.cast(self.user_indices, tf.int32)) # [batch_size, dim_k] self.Usr_Feat = tf.nn.embedding_lookup( self.user_emb_feat, tf.cast(self.user_indices, tf.int32)) # [batch_size, dim_cf_emb] self.bias_usr_feat_emb = tf.get_variable( shape=[self.dim_k], initializer=tf.zeros_initializer(), dtype=tf.float32, name='bias_usr_feat_emb') self.Usr_Feat_Emb = tf.matmul( self.Usr_Feat, self.W_usr_feat_emb ) + self.bias_usr_feat_emb # [batch_size, dim_k] self.Usr_Feat_Emb = tf.nn.relu(self.Usr_Feat_Emb) # self.Usr_Feat_Emb = tf.layers.dropout(self.Usr_Feat_Emb, self.dropout_emb) # self.Usr_Feat_Emb = self._batch_norm_layer(self.Usr_Feat_Emb, self.train_phase, 'user_emb_bn') self.Usr_Expr_a = self.Usr_Emb + self.Usr_Feat_Emb # [batch_size, dim_k] # 环境的向量表示 with tf.name_scope('context_express'): self.bias_ctx_emb = tf.get_variable( shape=[self.dim_k], initializer=tf.zeros_initializer(), dtype=tf.float32, name='bias_ctx_emb') self.Ctx_Emb = tf.matmul( self.num_features, self.W_Ctx) + self.bias_ctx_emb # [batch_size, dim_k] self.Ctx_Emb = self._batch_norm_layer(self.Ctx_Emb, self.train_phase, 'ctx_bn') self.Ctx_Emb = tf.nn.relu(self.Ctx_Emb) # 物品的向量表示 with tf.name_scope('item_express'): self.I_Wds_a = tf.SparseTensor( indices=tf.cast(self.item_words_indices_a, dtype=np.int64), values=self.item_words_values_a, dense_shape=[ tf.cast(self.batch_size, dtype=np.int64), self.num_words ]) self.att_u_a = tf.matmul(self.Usr_Expr_a, self.Wu_oh_Att) # [batch_size, dim_att] self.att_ctx = tf.matmul(self.Ctx_Emb, self.Wctx_Att) self.att_oh = [] self.bias_wds_emb = tf.get_variable( shape=[self.dim_k], initializer=tf.zeros_initializer(), dtype=tf.float32, name='bias_wds_emb') self.I_Wds_Emb_a = tf.sparse_tensor_dense_matmul( self.I_Wds_a, self.Wwords_Emb) + self.bias_wds_emb # [batch_size, dim_k] self.I_Wds_Emb_a = tf.nn.relu(self.I_Wds_Emb_a) self.att_I_Wds = tf.matmul(self.I_Wds_Emb_a, self.WW_Att) # 词的attention self.att_I_Wds = tf.nn.relu(self.att_u_a + self.att_ctx + self.att_I_Wds + self.b_oh_Att) self.att_I_Wds = tf.matmul(self.att_I_Wds, self.w_oh_Att) + self.c_oh_Att self.att_oh.append(self.att_I_Wds) vae_encoder = VAE(input_dim=2048, hidden_encoder_dim=1024, latent_dim=96, lam=0.001, kld_loss=0.001) self.I_visual_Emb, self.vae_loss = vae_encoder.get_vae_embbeding( self.visual_emb_feat) # TODO self.I_visual_Emb = self._batch_norm_layer(self.I_visual_Emb, self.train_phase, 'vis_bn') self.bias_lda_emb = tf.get_variable( shape=[self.dim_k], initializer=tf.zeros_initializer(), dtype=tf.float32, name='bias_lda_emb') self.I_LDA_Emb = tf.matmul( self.words_lda, self.W_LDA_emb) + self.bias_lda_emb # [batch_size, dim_k] self.I_LDA_Emb = tf.nn.relu(self.I_LDA_Emb) self.att_I_LDA = tf.matmul(self.I_LDA_Emb, self.W_LDA_Att) # LDA的attention self.att_I_LDA = tf.nn.relu(self.att_u_a + self.att_ctx + self.att_I_LDA + self.b_oh_Att) self.att_I_LDA = tf.matmul(self.att_I_LDA, self.w_oh_Att) + self.c_oh_Att self.att_oh.append(self.att_I_LDA) self.I_One_hot_a = [] for i in range(len(self.one_hots_dims)): I_Emb_temp_a = tf.nn.embedding_lookup( self.W_one_hots[i], tf.cast(self.one_hots_a[:, i], tf.int32)) # [batch_size, dim_k] att_oh_temp = tf.matmul( I_Emb_temp_a, self.Woh_Att[i]) # [batch_size, att_dim_k] att_temp = tf.nn.relu(self.att_u_a + self.att_ctx + att_oh_temp + self.b_oh_Att) # [batch_size, att_dim_k] att_temp = tf.matmul( att_temp, self.w_oh_Att) + self.c_oh_Att # [batch_size, 1] self.att_oh.append(att_temp) self.I_One_hot_a.append(I_Emb_temp_a) self.att_oh = tf.nn.softmax(tf.concat( self.att_oh, axis=1)) # [batch_size, oh_dim] 第一列是词attention self.I_Wds_Emb_a = self.I_Wds_Emb_a * self.att_oh[:, 0:1] self.I_LDA_Emb = self.I_LDA_Emb * self.att_oh[:, 1:2] for i in range(2, len(self.one_hots_dims) + 2): self.I_One_hot_a[ i - 2] = self.I_One_hot_a[i - 2] * self.att_oh[:, i:i + 1] with tf.name_scope('cross'): self.cross_inputs = [ self.Usr_Emb, self.Usr_Feat_Emb, self.num_features, self.I_visual_Emb, self.I_Wds_Emb_a, self.I_LDA_Emb ] + self.I_One_hot_a self.cross_x0 = tf.concat(self.cross_inputs, axis=1) # [batch_size, self.dim_cross] self.cross_x0 = self._batch_norm_layer(self.cross_x0, self.train_phase, 'cross_bn') self.dim_cross = self.cross_x0.get_shape().as_list()[1] # self.cross_x0 = tf.reshape(self.cross_x0, [-1, self.dim_cross, 1]) self.cross_xl = self.cross_x0 for l in range(self.num_cross_layer): self.cross_xl = self._cross_layer(self.cross_xl, 'cross_{}'.format(l)) self.cross_xl = tf.layers.dropout(inputs=self.cross_xl, rate=self.dropout_emb) self.cross_output = self.cross_xl # self.cross_output = tf.reshape(self.cross_xl, [-1, self.dim_cross]) with tf.name_scope('deep'): if self.use_deep: self.I_one_hot_deep = [] for i in range(len(self.one_hots_dims)): I_Emb_temp_a = tf.nn.embedding_lookup( self.W_deep_one_hots[i], tf.cast(self.one_hots_a[:, i], tf.int32)) # [batch_size, dim_k] self.I_one_hot_deep.append(I_Emb_temp_a) # self.deep_input = tf.concat( [self.num_features, self.visual_emb_feat] + self.I_one_hot_deep, axis=1) # 输入加入batch_norm for i, deep_dim in enumerate(self.deep_dims): if i == 0: self.deep_input = tf.layers.dense( inputs=self.deep_input, kernel_initializer=tf.glorot_uniform_initializer(), units=deep_dim, activation=tf.nn.relu, ) self.deep_input = self._batch_norm_layer( self.deep_input, self.train_phase, 'deep_bn_{}'.format(i)) else: self.deep_input = self._highway_layer( self.deep_input, deep_dim, 'deep_highway_{}'.format(i)) # 加入dropout self.deep_input = tf.layers.dropout(inputs=self.deep_input, rate=self.dropout_deep) self.deep_output = tf.layers.dense(self.deep_input, 1, activation=None) self.deep_output = tf.reshape(self.deep_output, [-1]) # [batch_size] with tf.name_scope('output'): if self.use_deep: self.concated = tf.concat([self.cross_output, self.deep_input], axis=1) else: self.concated = self.cross_output self.hidden = self.concated for i, dim in enumerate(self.dim_hidden_out): if i == 0: self.hidden = tf.layers.dense( inputs=self.hidden, kernel_initializer=tf.glorot_uniform_initializer(), units=dim, activation=tf.nn.relu) else: self.hidden = self._highway_layer( self.hidden, dim, 'out_highway_{}'.format(i)) self.hidden = tf.layers.dropout(inputs=self.hidden, rate=self.dropout_deep) self.bu = tf.nn.embedding_lookup( self.bias_u, tf.cast(self.user_indices, dtype=tf.int32)) self.y_ui_a = tf.layers.dense(self.hidden, 1, activation=None) + self.bu self.y_ui_a = tf.reshape(tf.nn.sigmoid(self.y_ui_a), [-1])