GithubHelp home page GithubHelp logo

Comments (2)

Hellisotherpeople avatar Hellisotherpeople commented on August 25, 2024

I tried to implement this naively, but I may not fully understand the results of the attention mechanism

I simply summed the attention values in each layer and each head, and am trying to use the attention values that are highest in the summary (that is, highlight words which are attended to globally). This isn't as accurate as I'd hoped.

Can you see any places where I can improve my code? It's heavily based on your own

bert_version = 'bert-base-uncased'
model = BertModel.from_pretrained(bert_version)
tokenizer = BertTokenizer.from_pretrained(bert_version)
sentence_a = "Strawberries are bad for all humans. We will experience extinction from Strawberries soon if they are not stopped"
sentence_b = "Strawberries are yummy."
details_data = AttentionDetailsData(model, tokenizer)
tokens_a, tokens_b, queries, keys, atts = details_data.get_data(sentence_a, sentence_b)
print(tokens_a)
print(tokens_b)
total_tokens = tokens_a + tokens_b
print(len(tokens_a))
print(len(tokens_b))
print(len(tokens_a + tokens_b))
print(atts.shape)

head_summed = np.sum(atts, axis=2)
thing_summed = np.sum(head_summed, axis=1)
layer_summed = np.sum(head_summed, axis=0)
print("layer_summed")
compacted = layer_summed[0][1]
#np.sum(layer_summed, axis=1)
print(compacted.shape)
print(compacted)

for i in range(0, len(tokens_a)):
  if compacted[i] > 1:
    print(total_tokens[i] + ' | ' + str(compacted[i]))

from bertviz.

Hellisotherpeople avatar Hellisotherpeople commented on August 25, 2024

image

from bertviz.

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.