Tensor-Puzzles icon indicating copy to clipboard operation
Tensor-Puzzles copied to clipboard

Help with puzzles 4 and 5

Open SnehaMondal opened this issue 3 years ago • 6 comments

I'm trying to solve puzzles 4 and 5 by using the predefined where(q, a, b) function, which expects q to be a boolean tensor. To arrive at a boolean tensor, I create a boolean list and use tensor on it. I suppose this is not allowed? How else could I implement this?

This is my current implementation for puzzle 5 (identity matrix of dimension j)

def eye(j: int) -> TT["j", "j"]:
   return where(tensor([[x==y for x in range(j)] for y in range(j)]), ones(j) - ones(j)[:, None] + ones(j), ones(j) - ones(j)[:, None])

SnehaMondal avatar Jul 14 '22 10:07 SnehaMondal

Everything you are currently doing with list comprehensions, you can do faster and simpler with the arange function .

For example if one does arange(10) == 2 that gives you a tensor that is all False with a True at position 2.

srush avatar Jul 14 '22 12:07 srush

After some experimentation, I came up with the following:

def eye(j: int) -> TT["j", "j"]:
   return where(arange(j) - arange(j)[:, None] == 0, 1, 0)

Wonder if there's even a better way...

atgctg avatar Aug 21 '22 19:08 atgctg

That's great! There are a couple ways to do it, but this is the way I did it.

The other thing I saw was to multiply to force type changes something like:

(arange(n)==arange(n)[:, None]) * 1

srush avatar Aug 22 '22 01:08 srush

I really loved, the way you thought about it, it's out of the box thinking :P. This is how I solved it. It would be cool if you have your solutions, out there. Although, it would incentives people to maybe give up easily. But person who gave up after trying would still learn. a = tensor([0] * j) index = arange(j) data = outer(a,a) data[index, index] = 1 return data

CreativeSelf0 avatar Mar 20 '23 21:03 CreativeSelf0

where(arange(j)[:, None] == arange(j), (outer(ones(j), ones(j))), 0)

jselvam11 avatar Aug 26 '23 00:08 jselvam11