-1

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?

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Ameen Ali
  • 245
  • 1
  • 3
  • 10

1 Answers1

-1

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!

Theodor Peifer
  • 3,097
  • 4
  • 17
  • 30
  • Actually I want to replace the conv2d layer with Linear, and still have the same effect – Ameen Ali Sep 05 '20 at 14:59
  • what do you mean with "replace" ? – Theodor Peifer Sep 05 '20 at 15:43
  • Convolution can indeed be expressed as a matrix multiplication, see for example [here](https://stackoverflow.com/questions/16798888/2-d-convolution-as-a-matrix-matrix-multiplication). From what I can see, the answers also provide some ways of how to do this in code. – xdurch0 Sep 07 '20 at 08:45