Quiz 2¶
For Penn State student, access quiz here
import ipywidgets as widgets
Question 1¶
Consider \(f(x,y)=e^{x^2+y^2}\) , compute the Hessian matrix and determine whether \(f(x,y)\) is a convex function.
Show answer
Answer: Hessian matrix is
\(f(x,y)\) is not a convex function
Question 2¶
Given any \(w\in R^n,\:b\in R,\:\) consider the multivariable function \(f(\boldsymbol x)=e^{\boldsymbol w\cdot \boldsymbol x+b}\) Whether \(f(\boldsymbol x)\) is a convex function?
Show answer
Answer: Yes
Question 3¶
Consider \(f(x,y)=x^2.\) Whether \(f(x,y)\) is a \(\lambda-\) strongly convex function?
Show answer
Answer: No
Question 4¶
Consider
Given initial guess
, compute
two steps of the gradient descent method for \(f(x,y)\):
Show answer
Answer: \(\frac{1}{4},\frac{1}{2}\)
Question 5¶
Suppose a point \(x\) is drawn at random uniformly from the square \([-1,1]\times[-1,1].\) Let
and consider the random variable \(\mathcal X_{\boldsymbol v} ={\boldsymbol x} \cdot {\boldsymbol v}\). What are \(\mathbb{E} [\mathcal X_{\boldsymbol v}]\) and \(\big(\mathbb{V}[ \mathcal X_{\boldsymbol v}]\big)^2\).
Show answer
Answer: Unavailable
Question 6¶
def model(100,10):
return nn.Linear(100,10)
File "<ipython-input-2-413e0725d122>", line 1
def model(100,10):
^
SyntaxError: invalid syntax
What are the sizes of W and b of the model?
Show answer
Answer: Size of W: torch.Size([10, 100]), Size of b: torch.Size([10])
Question 7¶
Load MNIST dataset with batch_size=100 as follows
trainset = torchvision.datasets.MNIST(root='./data', train= True, download=True,transform=torchvision.transforms.ToTensor())
trainloader = torch.utils.data.DataLoader(trainset, batch_size=100, shuffle=True)
for i, (images, labels) in enumerate(trainloader):
What are the sizes of variable images and labels?
Show answer
Answer: Size of images: torch.Size([100, 1, 28, 28]), Size of labels: torch.Size([100])
Question 8¶
In the training process of MNIST dataset with mini-batch stochastic gradient descent(SGD) method, if we set bath_size = 600, how many iterations (or SGD steps) are there in one epoch?
Show answer
Answer: 100
Question 9¶
What is the output of the following code?
sequence = torch.tensor(([[4,2,3],[1,5,6],[0,7,2]]))
maxvalue, index = torch.max(sequence, 1)
print(maxvalue,',',index)
Show answer
Answer: tensor([4, 6, 7]) , tensor([0, 2, 1])
Question 10¶
What is the output of the following code?
num_correct = 0
labels = torch.tensor([1,2,3,4,0,0,0])
predicted = torch.tensor([0,2,3,4,1,2,0])
num_correct += (predicted == labels).sum()
print(num_correct)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-6-4c7bc87f85fb> in <module>
1 num_correct = 0
----> 2 labels = torch.tensor([1,2,3,4,0,0,0])
3 predicted = torch.tensor([0,2,3,4,1,2,0])
4 num_correct += (predicted == labels).sum()
5 print(num_correct)
NameError: name 'torch' is not defined
Show answer
Answer: tensor([4, 7, 6]) , tensor([0, 2, 1])