tensorflow / tensorflow/probability

Triangle matrix operations with 4D matrix

Open
#1,563 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
4.4k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Hello,

this code:

def create_look_ahead_mask(size_0):
  mask = np.ones((size_0, size_0), dtype=np.int32)

  for a in range(size_0):          # Timestep
      for c in range(size_0):      # Timestep
          if c > a:
            mask[a, c] = 0

  return mask  # (seq0_len, seq1_len, seq0_len, seq1_len)

print(create_look_ahead_mask(3), "\n")

is equivalent to:

def create_look_ahead_mask(size):
  n = int(size * (size+1) / 2)
  mask = tfp.math.fill_triangular(tf.ones((n,), dtype=tf.int32), upper=False)
  return mask

print(create_look_ahead_mask(3))

Here is another code very close to the above:

def create_look_ahead_mask(size_0, size_1):
  mask = np.ones((size_0, size_1, size_0, size_1), dtype=np.int32)

  for a in range(size_0):          # Timestep
    for b in range(size_1):        # Patch
      for c in range(size_0):      # Timestep
        for d in range(size_1):    # Patch
          if c > a:
            mask[a, b, c, d] = 0

  return mask  # (seq0_len, seq1_len, seq0_len, seq1_len)

print(create_look_ahead_mask(3, 3), "\n")

But what is the equivalent to it with using tfp.math.fill_triangular or another matrix operation?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Start by comparing the NumPy loop implementation with tfp.math.fill_triangular and other TensorFlow matrix operations, then verify that the chosen operation reproduces the requested (size_0, size_1, size_0, size_1) mask for the provided examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python, tensorflow
Domain
machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.