# Convolutions for Images

Hey @anirudh in section 6.2.4 Learning A Kernel
when printing this at the bottom of our for loop:

``````if (i+1) % 2 == 0:
print(f'batch {i+1}, loss {l.sum():.3f}')
``````

should it be batch or epoch? I thought it was epoch, could you explain why its batch instead?

1 Like
1. When you try to automatically find the gradient for the Conv2D class we created, what kind of error message do you see?

Got Error Message: Inplace operations are not supported using autograd .

1. How do you represent a cross-correlation operation as a matrix multiplication by changing the input and kernel tensors?

–> flip the two-dimensional kernel tensor both horizontally and vertically, and then perform the cross-correlation operation with the input tensor

``````K = torch.tensor([[1.0, -1.0]]) # filter shape: (1, 2)
# flip horizontally
K = torch.flip(K, )
# flip vertically
K = torch.flip(K, )
print(K)
print(K)

Y = corr2d(X, K)
plt.imshow(Y, cmap="gray")
``````

What is the minimum size of a kernel to obtain a derivative of degree d?

1 Like

For Exercise 2, l.sum().backward() is already computing the gradient, is it not?

how did you automatically try to find gradient?

yes through backpropagation the leaf tensor gradient is stored in `net.grad`

### Exercises

1. Construct an image X with diagonal edges.

1. What happens if you apply the kernel K in this section to it?

• zero matrix.
2. What happens if you transpose X?

• No change
3. What happens if you transpose K?

• zero matrix.
2. When you try to automatically find the gradient for the Conv2D class we created, what kind

of error message do you see?

``````* I am able to do `net.weights.grad`, when I try `net.grad` I get the error `'Conv2d' object has no attribute 'grad'`
``````
1. How do you represent a cross-correlation operation as a matrix multiplication by changing

the input and kernel tensors?

``````* cross correlation is basically matrix multiplication between slices of tensorfrom X of the shape of kernel and summing.

* It can be done by padding Kand X based on what is needed to multiply
``````
1. Design some kernels manually.

1. What is the form of a kernel for the second derivative?

2. What is the kernel for an integral?

• how do you actually make it manually
2. What is the minimum size of a kernel to obtain a derivative of degree d

`` * dont know.``

I think so，epoch is batter than batch.

Thanks, @jimmiemunyi @tiger_st for bringing this up. Makes sense to me. Raised a PR for the fix.

Construct an image `X` with diagonal edges.

1. What happens if you apply the kernel `K` in this section to it?
it detects the diagonal edges

2. What happens if you transpose `X` ?
same

3. What happens if you transpose `K` ?
same also

4. How do you represent a cross-correlation operation as a matrix multiplication by changing the input and kernel tensors?
transforming the kernal in a matrix
Km = torch.zeros((9,5))
kv = torch.tensor([0.0,1.0,0.0,2.0,3.0])
for i in range(4):
Km[i:i+5,i] = kv
Km = Km.t()
Km = Km[torch.arange(Km.size(0))!=2]
Km = Km.t()

Km = tensor([[0., 0., 0., 0.],
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[2., 0., 0., 0.],
[3., 2., 1., 0.],
[0., 3., 0., 0.],
[0., 0., 2., 0.],
[0., 0., 3., 0.],
[0., 0., 0., 0.]])

transforming the input X in a vector
X = torch.tensor([[float(i) for i in range(9)]])

X = tensor([[0., 1., 2., 3., 4., 5., 6., 7., 8.]])

X @ Km #matrix multiplication
result : tensor([[19., 25., 37., 0.]])