Tensorflow to Pytorch CNN(Use nn.Conv1d)

102 Views Asked by At
input_size = [765, 500, 72]

model = Sequential()
add = model.add

add(l.Conv1D(256, kernel_size=3, strides=2, activation='relu')
add(l.Dropout(0.5))
add(l.Conv1D(256, kernel_size=3, strides=2, activation='relu')
add(l.Dropout(0.5))
add(l.GlobalAveragePooling1D())
add(l.Dense(100, activation="relu"))
add(l.Dense(3, activation="softmax"))


(None, 249, 256)
(None, 249, 256)
(None, 124, 256)
(None, 124, 256)
(None, 256)
(None, 100)
(None, 3)

This is tensorflow model struc and summary. Tensorflow to Pytorch CNN model. Use Conv1D

[Tensorflow Model summary]

enter image description here

1

There are 1 best solutions below

5
On

To jump-start your research, here is an example usage of nn.Conv1d:

>>> f = nn.Conv1d(72, 256, kernel_size=3, stride=2)
>>> f(torch.rand(765, 72, 500)).shape
torch.Size([765, 256, 249])

Regarding this case keep in mind a few PyTorch-related things :

  • Unlike Tensorflow, it handles data in the BHC format.

  • You have to provide the input feature sizes for each linear layer.

  • The activation function is not included in nn.Conv1d, you have to use a dedicated module for that (eg. nn.ReLU).