Skip to content

Instantly share code, notes, and snippets.

@nahidalam
Created July 8, 2020 17:19
Show Gist options
  • Save nahidalam/42c80944dc7c971866f36ca49f92e68b to your computer and use it in GitHub Desktop.
Save nahidalam/42c80944dc7c971866f36ca49f92e68b to your computer and use it in GitHub Desktop.
def EmbeddingRec(EMBEDDING_SIZE, NUM_MOVIES, NUM_USERS, ROW_COUNT):
movie_input = keras.Input(shape=(1,), name='movie_id')
movie_emb = layers.Embedding(output_dim=EMBEDDING_SIZE, input_dim=NUM_MOVIES, input_length=ROW_COUNT, name='movie_emb')(movie_input)
movie_vec = layers.Flatten(name='FlattenMovie')(movie_emb)
movie_model = keras.Model(inputs=movie_input, outputs=movie_vec)
user_input = keras.Input(shape=(1,), name='user_id')
user_emb = layers.Embedding(output_dim=EMBEDDING_SIZE, input_dim=NUM_USERS, input_length=ROW_COUNT, name='user_emb')(user_input)
user_vec = layers.Flatten(name='FlattenUser')(user_emb)
user_model = keras.Model(inputs=user_input, outputs=user_vec)
merged = layers.Dot(name = 'dot_product', normalize = True, axes = 2)([movie_emb, user_emb])
merged_dropout = layers.Dropout(0.2)(merged)
dense_1 = layers.Dense(70,name='FullyConnected-1')(merged)
dropout_1 = layers.Dropout(0.2,name='Dropout_1')(dense_1)
dense_2 = layers.Dense(50,name='FullyConnected-2')(dropout_1)
dropout_2 = layers.Dropout(0.2,name='Dropout_2')(dense_2)
dense_3 = keras.layers.Dense(20,name='FullyConnected-3')(dropout_2)
dropout_3 = keras.layers.Dropout(0.2,name='Dropout_3')(dense_3)
dense_4 = keras.layers.Dense(10,name='FullyConnected-4', activation='relu')(dropout_3)
result = layers.Dense(1, name='result', activation="relu") (dense_4)
adam = keras.optimizers.Adam(lr=0.001)
model = keras.Model([movie_input, user_input], result)
model.compile(optimizer=adam,loss= 'mean_absolute_error')
return model, movie_model, user_model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment