Barebones Pytorch
作业的第一个小问题是为了帮我们理解Pytorch的一些基本元素,首先就是Tensor,一个Tensor类似于一个Numpy矩阵,只不过Tensor可以再GPU上运行,所以省去了我们在CPU和GPU之间传输Munpy浪费的时间。当我们创建一个Tensor的时候,可以指定其requires_grad=True,这样就会搭建所有涉及到这个Tensor的运算的计算图,以方便我们的Pytorch框架自动地去做反向传播。
def three_layer_convnet(x, params):
"""
Performs the forward pass of a three-layer convolutional network with the
architecture defined above.
Inputs:
- x: A PyTorch Tensor of shape (N, 3, H, W) giving a minibatch of images
- params: A list of PyTorch Tensors giving the weights and biases for the
network; should contain the following:
- conv_w1: PyTorch Tensor of shape (channel_1, 3, KH1, KW1) giving weights
for the first convolutional layer
- conv_b1: PyTorch Tensor of shape (channel_1,) giving biases for the first
convolutional layer
- conv_w2: PyTorch Tensor of shape (channel_2, channel_1, KH2, KW2) giving
weights for the second convolutional layer
- conv_b2: PyTorch Tensor of shape (channel_2,) giving biases for the second
convolutional layer
- fc_w: PyTorch Tensor giving weights for the fully-connected layer. Can you
figure out what the shape should be?
- fc_b: PyTorch Tensor giving biases for the fully-connected layer. Can you
figure out what the shape should be?
Returns:
- scores: PyTorch Tensor of shape (N, C) giving classification scores for x
"""
conv_w1, conv_b1, conv_w2, conv_b2, fc_w, fc_b = params
scores = None
################################################################################
# TODO: Implement the forward pass for the three-layer ConvNet. #
################################################################################
x = F.conv2d(x, conv_w1, conv_b1, padding=2)
x = F.relu(x)
x = F.conv2d(x, conv_w2, conv_b2, padding=1)
x = F.relu(x)
x = flatten(x)
x = x.mm(fc_w) + fc_b
scores = x
################################################################################
# END OF YOUR CODE #
################################################################################
return scores
下一个小问题是显示的写一下参数初始化代码:
learning_rate = 3e-3
channel_1 = 32
channel_2 = 16
conv_w1 = None
conv_b1 = None
conv_w2 = None
conv_b2 = None
fc_w = None
fc_b = None
################################################################################
# TODO: Initialize the parameters of a three-layer ConvNet. #
################################################################################
conv_w1 = random_weight((channel_1, 3, 5, 5))
conv_b1 = zero_weight((channel_1))
conv_w2 = random_weight((channel_2, channel_1, 3, 3))
conv_b2 = zero_weight((channel_2