I have a PyTorch Conv2d
layer:
Conv2d(96, 1000, kernel_size=torch.Size([10, 10]), stride=(1, 1))
I know that a Conv2d
layer is a special case of a Linear
layer. How can I convert a Conv2d
layer to Linear
layer in PyTorch?
I wouldn't call a convolutional layer a "special case of a linear layer". But to your question: Do you mean how to flatten a conv layer output to a linear layer? Since you need matrices for conv. layers and for vectors linear layers, you have to take the matrix an flatten it, for example a matrix of shape (m, n) would become a vector (m*n, 1). Here is how to do that in Pytorch:
x = nn.Conv2d(96, 1000, kernel_size=torch.Size([10, 10]), stride=(1, 1))
x = x.view(-1, x.size()[1] * x.size()[2] * x.size()[3])
x can now be passed into a flatten layer. Hope that helped!