Spooky Sale: 10% off Premium yearly · $118.80 for the first year · until Nov 6Get it

Deep-ML
Learn
Problems1,200+ hands-on ML problemsLearning PathA guided route through the fundamentalsLabsInteractive labs on real techniquesProjectsBuild a GPT, an RL agent, CUDA kernelsCollectionsCurated problem sets and videosMathPen-and-paper math for MLIntuitionInvent the idea behind the formula yourselfPlaylistsYour own problem lists, shareableStudy AssistantAn AI teacher that knows your progress
Interview Prep
Compete
ContestsFresh problem sets, ranked liveLeaderboardWhere you stand, globallyBuddiesSolve with a friend: streaks, a weekly board, chat
Discuss
Deep-Path
Premium
      Intuition
      87 of 102

      Distance, not place

      Attention scores how much one word listens to another with a dot product: the first word’s query (what it looks for) times the other’s key (what it offers), as in “Who should “it” listen to?”. On their own they don’t say where the words are. Write tag(vector, position): change a 2D query or key so it includes its word’s position, such that the score between a query at i and a key at j depends only on how far apart they are, not on where in the sentence they sit.

      A query word and a key word in a sentence. Untagged, their score is the same wherever they sit and however far apart they are. Tag each with its position so that the gap between them matters, and the place doesn’t.

      A query word and a key word in a sentence.05101520position in the sentencequery[2, 1]key[1, 2]3 apartuntagged score4wherever they sittagged score?same for any place · different for a different gap
      Your functiontag(vector, position)
      vectora query or a key, [x, y]e.g. [−1, 0.5]
      positionwhere its word sits in the sentence, 0, 1, 2, ...e.g. 0
      returnsthe vector with the position worked in
      solution.pyLoading Python
      Sign in for an AI hint
      ← Word order mattersSoftmax on the go →