SelfAttentive icon indicating copy to clipboard operation
SelfAttentive copied to clipboard

cannot visualize attention weights

Open shahbazsyed opened this issue 8 years ago • 3 comments

I have successfully completed the training and also extracted the attention weights. I get the following error when I try to run the Attention Visualization notebook

     16 
     17     vector = weights.sum( 0 )
---> 18     vector = vector / vector.sum( 1 )[ 0,0 ]
     19     att, ids_to_show =  vector.sort( 1, descending=True )
     20 

RuntimeError: dimension out of range (expected to be in range of [-1, 0], but got 1)

Can you let me know what is wrong here?

shahbazsyed avatar Nov 17 '17 15:11 shahbazsyed

I think this should be due to pytorch version change. At least we have a matrix of 30x review_len for the weights varialbe for each review. This is the weight we want to manipulate for visualization. You can then follow the steps in the paper for different variants of attention visualization.

yufengm avatar Nov 17 '17 22:11 yufengm

vector = weights.sum( 0 )
vector = vector / vector.sum()
att, ids_to_show =  vector.sort( 0, descending=True )

Try this. It sums all the 30 attention weights and normalized.

yufengm avatar Nov 17 '17 23:11 yufengm

Thanks! This seems to work for the word attention visualization. For the sentence visualization, I tried this

  weights = Weights[ batchedReviewsAndTokens[ reviewID ][3] ][ 0 ][ batchedReviewsAndTokens[ reviewID ][ 4 ] ].data
    vector = weights

instead of the original

weights = Weights[ batchedReviewsAndTokens[ reviewID ][3] ][ 0 ][ batchedReviewsAndTokens[ reviewID ][ 4 ] ].data
   vector = weights.sum( 0 )

It seems to work, but I still want to confirm that it is the correct way to do it.

shahbazsyed avatar Nov 20 '17 12:11 shahbazsyed