Quiz 4

For Penn State student, access quiz here

import ipywidgets as widgets

Question 1

Consider

\[\begin{split} g=\begin{pmatrix} 1 & 2 & 1 & 1\\ 0 & 0 & 2 & 0\\ 0 & 2 & 2 & 0\\ 1 & 1 & 1 & 1 \end{pmatrix}, \end{split}\]

and kernel

\[\begin{split} K = \begin{pmatrix} 0 & 0 & 1\\ 0 & 1 & 0\\ 1 & 0 & 0 \end{pmatrix}. \end{split}\]

Then what is the outcome of convolution for one channel

with stride one and zero padding \( f = K\ast g \)

Question 2

How many parameters are needed when using a 3x3 convolutional operations with 2 input and 5 output channels?

Question 3

Consider a two channel image (tensor) \(g \in \mathbb{R}^{2\times 5\times 5}\). Let define \(f = K\ast g\) by convolution for multi-channel with stride-two and zero padding, where \(K \in \mathbb{R}^{3\times 2\times 3\times3}\) (\(K_{i,j} \in \mathbb{R}^{3\times 3}\) for \(i=1,2,3\) and \(j=1,2\)). What is the right dimension (size) of \(f\).

Question 4

Given

\[\begin{split} \tilde{M}= \left( \begin{array}{ccccc} 4& 1&&&\\ 1&4&1&&\\ &\ddots&\ddots&\ddots&\\ &&1&4&1\\ &&&1&4\\ \end{array} \right)\in \mathbb R^{n\times n} \end{split}\]

Write out the kernel \(M\) such that for any \(\mu\in \mathbb R^n\) (with zero padding) \(\tilde{M}\mu=M\ast \mu.\)

Question 5

Consider a mesh \(\mathcal T_1\) of \([0, 1]\) with mesh size \(h_1=\frac14\), namely \([0,1]=[0,\frac14]\cup [\frac14,\frac12]\cup [\frac12, \frac34]\cup[\frac34,1]\) and the corresponding basis function \(\phi^1_1(x), \phi^1_2(x), \phi^1_3(x)\). Next we consider another mesh \(\mathcal T_2\) of \([0, 1]\) with mesh size \(h_2=\frac12\), namely \([0, 1]=[0,\frac12]\cup [\frac12,1]\) and the corresponding basis function \(\phi_1^2(x)\). Find \(a_1,a_2,a_3\) such that \( \phi_1^2(x)=a_1\phi_1^1(x)+a_2\phi_2^1(x)+a_3\phi_3^1(x). \)

Question 6

What is output of the following code?

conv1 = torch.nn.Conv2d(in_channels=1, out_channels=2, kernel_size=3)

print(conv1.weight.size())
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-e462f8aaae23> in <module>
----> 1 conv1 = torch.nn.Conv2d(in_channels=1, out_channels=2, kernel_size=3)
      2 
      3 print(conv1.weight.size())

NameError: name 'torch' is not defined

Question 7

What is the output of the following code:

import torch.nn.functional as F

x = torch.tensor([[[1,1,2,2],[0,0,0,0],[3,3,4,4],[0,0,0,0]]],dtype=float)

max_x = F.max_pool2d(x,2)

avg_x = F.avg_pool2d(x,2)


print(max_x,'\n',avg_x)

Question 8

What is the output of the following code:

x = torch.randn(1, 3, 4, 4)

x = x.view(x.size(0), -1) 

print(x.size())

Question 9

If we ask that the learning rate is divided by 5 every 30 epochs, given the initial learning is 1, what code should we run?