-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can i obtain the attentional weights of feature interaction in AFM? #10
Comments
Hi @chenfu12138 there are 2 steps to get the attentional weights of feature interactions in AFM. Step1: Modify two lines of the source codesPlease modify the In line 134 and 135 attention_weight =tf.nn.softmax(tf.tensordot(attention_temp,self.projection_h,axes=(-1,0)),dim=1)
attention_output = tf.reduce_sum(attention_weight*bi_interaction,axis=1) to self.normalized_att_score=tf.nn.softmax(tf.tensordot(attention_temp,self.projection_h,axes=(-1,0)),dim=1)
attention_output = tf.reduce_sum(self.normalized_att_score*bi_interaction,axis=1) I will modify it in the next released version. Step2: Get the attentional weights !After you have finished training the AFM model. from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Lambda
afmlayer = model.layers[-3]
afm_weight_model = Model(model.input,outputs=Lambda(lambda x:afmlayer.normalized_att_score)(model.input))
attentional_weights = afm_weight_model.predict(model_input,batch_size=4096) You can try it~ |
@shenweichen It worked,thanks! I have obtained the attentional weights like this |
Hi @chenfu12138 import itertools
import deepctr
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Lambda
feature_dim_dict = {"sparse": sparse_feature_dict, "dense": dense_feature_list}
model = deepctr.models.AFM(feature_dim_dict)
model.fit(model_input,target)#
afmlayer = model.layers[-3]
afm_weight_model = Model(model.input,outputs=Lambda(lambda x:afmlayer.normalized_att_score)(model.input))
attentional_weights = afm_weight_model.predict(model_input,batch_size=4096)
feature_interactions = list(itertools.combinations(list(feature_dim_dict['sparse'].keys()) + feature_dim_dict['dense'] ,2)) The Try it and star it if it helps 😉 |
@shenweichen Perfect solution! Its so nice of you! Star it of course!!! |
Hi, the latest version |
No description provided.
The text was updated successfully, but these errors were encountered: