Last active
September 17, 2020 09:34
-
-
Save JiaminXuan/f8588714cfb97260e32f222251d9bc10 to your computer and use it in GitHub Desktop.
implicit Matrix Factorization - keras
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from numpy import bincount,ravel,log | |
from scipy.sparse import coo_matrix | |
import pandas as pd | |
from keras import backend as K | |
from keras.layers import * | |
from keras.models import Model,Sequential | |
def imf_loss(y_true, y_pred): | |
C=1+2*y_true | |
y=K.cast(K.cast(y_true, bool),'float32') | |
diff = C*K.square(y - y_pred) | |
return K.mean(diff, axis=-1) | |
viewer=Input(shape=(1,),name='resp') | |
program=Input(shape=(1,),name='prog') | |
r=Embedding(input_dim=24587,input_length=1,output_dim=16,embeddings_regularizer='l2')(viewer) | |
p=Embedding(input_dim=6771,input_length=1,output_dim=16,embeddings_regularizer='l2')(program) | |
r=Flatten()(r) | |
p=Flatten()(p) | |
cat=concatenate([r,p]) | |
d=Dense(units=4,activation='sigmoid',kernel_regularizer='l1')(cat) | |
output=Dense(units=1,activation='sigmoid',kernel_regularizer='l1')(d) | |
model=Model(inputs=[viewer,program],outputs=[output]) | |
model.summary() | |
model.compile(optimizer='adam',loss=imf_loss) | |
model.fit([respondent,program],y_true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment