Challenge¶
Create a 10x10 tensor of 32bit integers filled with zeros. Then select a random 3x3 block inside the tensor and switch the values from 0 to 1.
Bonus
You can interpret this tensor as an image where 0s represent black pixels and 1s represent white pixels. Plot the image.
Solution¶
Explanation¶

Instantiate a 10x10 tensor of 32bit integer 0s.
By default,
torch.zeros()
creates floats, so we explicitly tell it to use 32bit integers withdtype=torch.int32
. 
Randomly choose the topleft cell of the 3x3 block.
We choose random a random (i,j) element such that the entire 3x3 block will fit inside
foo
. 
Select the 3x3 block and update 0s to 1s.
Bonus
We can plot the array as an image using matplotlib.pyplot.imshow()
with cmap='gray'
.