Skip to content

Instantly share code, notes, and snippets.

@eggie5
Created June 24, 2020 15:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eggie5/fd79b72642cde7f007321d55a781a8bd to your computer and use it in GitHub Desktop.
Save eggie5/fd79b72642cde7f007321d55a781a8bd to your computer and use it in GitHub Desktop.
# Generalized Matrix Factorization
def GMF(num_users, num_items, k, reg):
"""Models the interaction between user/item LV w/ linear dot-product"""
print("using dot-product interaction term")
seed = 1
uid = Input((1,), name="uid")
iid = Input((1,), name="iid")
item_bias = Embedding(
num_items,
1,
input_length=1,
name="item_bias",
embeddings_regularizer=regularizers.l2(reg),
embeddings_initializer=initializers.glorot_normal(seed=seed),
)
user_emb = Embedding(
num_users,
k,
input_length=1,
name="user_embedding",
embeddings_regularizer=regularizers.l2(reg),
embeddings_initializer=initializers.glorot_normal(seed=seed),
)
item_emb = Embedding(
num_items,
k,
input_length=1,
name="item_embedding",
embeddings_regularizer=regularizers.l2(reg),
embeddings_initializer=initializers.glorot_normal(seed=seed),
)
u = Flatten(name="u")(user_emb(uid))
i = Flatten(name="i")(item_emb(iid))
i_b = Flatten(name="i_b")(item_bias(iid))
xui = Add(name="xui")([i_b, Dot(1)([u, i])])
model = Model(inputs=[uid, iid], outputs=xui, name="xui")
return model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment