Skip to content

Commit

Permalink
Merge pull request #36 from zcxia23/patch-2
Browse files Browse the repository at this point in the history
share embedding
  • Loading branch information
wzhe06 authored Mar 27, 2021
2 parents 313b86d + 8571489 commit 4aa5588
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions TFRecModel/src/com/sparrowrecsys/offline/tensorflow/DIN.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def get_dataset(file_path):
}

# movie id embedding feature
movie_col = tf.feature_column.categorical_column_with_identity(key='movieId', num_buckets=1001)
movie_emb_col = tf.feature_column.embedding_column(movie_col, EMBEDDING_SIZE)
#movie_col = tf.feature_column.categorical_column_with_identity(key='movieId', num_buckets=1001)
#movie_emb_col = tf.feature_column.embedding_column(movie_col, EMBEDDING_SIZE)

# user id embedding feature
user_col = tf.feature_column.categorical_column_with_identity(key='userId', num_buckets=30001)
Expand All @@ -79,7 +79,21 @@ def get_dataset(file_path):
vocabulary_list=genre_vocab)
item_genre_emb_col = tf.feature_column.embedding_column(item_genre_col, EMBEDDING_SIZE)

# user behaviors

'''
candidate_movie_col = [tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_identity(key='movieId', num_buckets=1001,default_value=0))]
recent_rate_col = [
tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_identity(key='userRatedMovie1', num_buckets=1001,default_value=0)),
tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_identity(key='userRatedMovie2', num_buckets=1001,default_value=0)),
tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_identity(key='userRatedMovie3', num_buckets=1001,default_value=0)),
tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_identity(key='userRatedMovie4', num_buckets=1001,default_value=0)),
tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_identity(key='userRatedMovie5', num_buckets=1001,default_value=0)),
]
'''


candidate_movie_col = [ tf.feature_column.numeric_column(key='movieId', default_value=0), ]

recent_rate_col = [
tf.feature_column.numeric_column(key='userRatedMovie1', default_value=0),
tf.feature_column.numeric_column(key='userRatedMovie2', default_value=0),
Expand All @@ -88,6 +102,8 @@ def get_dataset(file_path):
tf.feature_column.numeric_column(key='userRatedMovie5', default_value=0),
]



# user profile
user_profile = [
user_emb_col,
Expand All @@ -106,15 +122,20 @@ def get_dataset(file_path):
tf.feature_column.numeric_column('movieRatingStddev'),
]

candidate_emb_layer = tf.keras.layers.DenseFeatures([movie_emb_col])(inputs)
candidate_layer = tf.keras.layers.DenseFeatures(candidate_movie_col)(inputs)
user_behaviors_layer = tf.keras.layers.DenseFeatures(recent_rate_col)(inputs)
user_profile_layer = tf.keras.layers.DenseFeatures(user_profile)(inputs)
context_features_layer = tf.keras.layers.DenseFeatures(context_features)(inputs)

# Activation Unit
user_behaviors_emb_layer = tf.keras.layers.Embedding(input_dim=1001,
output_dim=EMBEDDING_SIZE,
mask_zero=True)(user_behaviors_layer) # mask zero

movie_emb_layer = tf.keras.layers.Embedding(input_dim=1001,output_dim=EMBEDDING_SIZE,mask_zero=True)# mask zero

user_behaviors_emb_layer = movie_emb_layer(user_behaviors_layer)

candidate_emb_layer = movie_emb_layer(candidate_layer)
candidate_emb_layer = tf.squeeze(candidate_emb_layer,axis=1)

repeated_candidate_emb_layer = tf.keras.layers.RepeatVector(RECENT_MOVIES)(candidate_emb_layer)

activation_sub_layer = tf.keras.layers.Subtract()([user_behaviors_emb_layer,
Expand Down

0 comments on commit 4aa5588

Please sign in to comment.