srush / Tensor-Puzzles

Solve puzzles. Improve your pytorch.
MIT License
2.96k stars 242 forks source link

Looking for additional instructions #7

Closed pengzhangzhi closed 2 years ago

pengzhangzhi commented 2 years ago

A sincerely Thank you to srush for such an amazing exercises. I have questions when dealing with the following puzzles. I am wondering if there are more cues to support me complete them.


## Puzzle 2 - sum

Compute [sum](https://numpy.org/doc/stable/reference/generated/numpy.sum.html) - the sum of a vector.

## Puzzle 7 - cumsum

Compute [cumsum](https://numpy.org/doc/stable/reference/generated/numpy.cumsum.html) - the cumulative sum.

## Puzzle 12 - compress

Compute [compress](https://numpy.org/doc/stable/reference/generated/numpy.compress.html) - keep only masked entries (left-aligned).

def compress(g: TT["i", bool], v: TT["i"], i:int) -> TT["i"]:
    return torch.tensor([v[j] if g[i] else None for j in range(i)])

## Puzzle 15 - bincount

Compute [bincount](https://numpy.org/doc/stable/reference/generated/numpy.bincount.html) - count number of times an entry was seen.
srush commented 2 years ago

For Puzzle 2 and 7 my recommendation would be to try to come up with a special vector or matrix that can compute the necessary output values. Then it just becomes a matrix multiplication.

For puzzle 15, I would think about one-hot vectors and how they can be used.

pengzhangzhi commented 2 years ago

Thanks. your instructions are very insightful!