View Source Exercises: 1-20
Mix.install([{:nx, "~> 0.6"}])
Introduction
Inspired by the Python notebook 100 Numpy Exercises.
1-10
1. Install Nx
in a Livebook. (★☆☆)
# Add your solution here.
Example solution
2. Create a 1-D tensor of 10 zeros. (★☆☆)
# Add your solution here.
Example solution
3. Find the number of elements in tensor
. (★☆☆)
tensor = Nx.tensor([[1, 2, 3], [4, 5, 6]])
# Add your solution here.
Example solution
4. Find the number of bytes of memory in tensor
. (★☆☆)
tensor = Nx.tensor([[1, 2, 3], [4, 5, 6]])
# Add your solution here.
Example solution
5a. Use Nx.sum/2
to find the sum of all elements of tensor
. (★☆☆)
tensor = Nx.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Add your solution here.
Example solution
5b. Read the documentation for Nx.sum/2
then provide the correct option to sum across the rows. (★☆☆)
# Add your solution here.
Example solution
6. Create a tensor of zeros of size 10 but where the fifth value is 1. (★☆☆)
# Add your solution here.
Example solution
7. Create a 3x3 tensor with values ranging from 0 to 8. (★☆☆)
# Add your solution here.
Example solution
8. Create a tensor with values ranging from 10 to 49. (★☆☆)
# Add your solution here.
Example solution 1
Example solution 2
9. Reverse tensor
(first element becomes last). (★☆☆)
tensor = Nx.tensor([2, 4, 6, 8])
# Add your solution here.
Example solution
10a. Given an initial tensor
, build a "mask" of non-zero elements. That is, build a second tensor with the same shape as the original, but that has a 1 wherever the original has a non-zero element and a 0 elsewhere. (★☆☆)
tensor = Nx.tensor([1, 2, 0, 0, 4, 0])
# Add your solution here.
Example solution
10b. Use the mask from 10a to replace each 0 from tensor
with -1. (★☆☆)
# Add your solution here.
Example solution
11-20
11. Create a 3x3 identity tensor. (★☆☆)
# Add your solution here.
Example solution
12. Create a 3x3x3 tensor with random values. (★☆☆)
# Add your solution here.
Example solution
13. Create a random 10x10 tensor then find its minimum and maximum values. (★☆☆)
# Add your solution here.
Example solution
14. Create a random 1D tensor of size 30 then find its mean. (★☆☆)
# Add your solution here.
Example solution
15. Create a 4x4 tensor with 1 on the border and 0 inside. (★☆☆)
# Add your solution here.
Example solution
16. Add a border of 0 around tensor
(end result will be a 5x5 tensor). (★☆☆)
tensor = Nx.broadcast(1, {3, 3})
# Add your solution here.
Example solution
17. Determine the results of the following expressions. (★☆☆)
nan = Nx.Constants.nan()
Nx.multiply(0, nan)
Nx.equal(nan, nan)
Nx.greater(nan, nan)
Nx.subtract(nan, nan)
# Add your solution here.
Example solution
18. Create a 5x5 tensor with values 1,2,3,4 just below the diagonal. (★☆☆)
# Add your solution here.
Example solution
19. Create a 8x8 tensor of 0 and 1 in a checkerboard pattern with 0 as the first element using Nx.tile
. (★☆☆)
# Add your solution here.
Example solution
20. Produce the same checkerboard pattern from exercise 19, but without using Nx.tile
. (★☆☆)
# Add your solution here.
# Hint: try using `Nx.iota` in combination with `Nx.remainder`.