Code Monkey home page Code Monkey logo

Comments (5)

shenweichen avatar shenweichen commented on May 27, 2024

Hi @chenfu12138 there are 2 steps to get the attentional weights of feature interactions in AFM.
First,make sure you have installed the latest release version of deepctr(now v0.1.5) from pip.
You can installed that version through pip install deepctr==0.1.5.

Step1: Modify two lines of the source codes

Please modify the layers.py in your local machine,maybe the path is
xxx\Anaconda3\Lib\site-packages\deepctr\layers.py

In line 134 and 135
change the following two lines

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~

from deepctr.

chenfu12138 avatar chenfu12138 commented on May 27, 2024

@shenweichen It worked,thanks! I have obtained the attentional weights like this
[[0.05291238]
[0.05570798]
[0.29247418]
...
[0.2924742 ]
[0.05001876]
[0.04996691]]
But i dont konw how to match the attentional weight in the array with the specific feature interaction one by one,And it seems that the match relationship between them varies from every training?

from deepctr.

shenweichen avatar shenweichen commented on May 27, 2024

Hi @chenfu12138
You can use the following codes

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 attentional_weights[:,i,0] is the feature_interactions[i]'s attentional weight of all samples

Try it and star it if it helps 😉

from deepctr.

chenfu12138 avatar chenfu12138 commented on May 27, 2024

@shenweichen Perfect solution! Its so nice of you! Star it of course!!!

from deepctr.

shenweichen avatar shenweichen commented on May 27, 2024

Hi, the latest version v0.2.0 has been released ,please upgrade through pip install -U deepctr ~

from deepctr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.