-4
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

YoungChae
  • 13
  • 2

1 Answers1

0

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).

Ivan
  • 34,531
  • 8
  • 55
  • 100
  • Thanks! I modified the tensorflow code to pytorch. – YoungChae Nov 14 '22 at 08:54
  • ``` input_shape = [765, 500, 72] conv1d_1 = nn.Conv1d(72, 256, stride=2, kernel_size=3) conv1d_2 = nn.Conv1d(249, 256, stride=2, kernel_size=3) relu = nn.ReLU() softmax = nn.Softmax() dropout = nn.Dropout(0.5) dense_1 = nn.Linear(256, 100) dense_2 = nn.Linear(100, 3) out = input.transpose(1, 2) out = relu(conv1d_1(out)).transpose(1, 2) out = dropout(out) out = relu(conv1d_2(out)).transpose(1, 2) out = dropout(out) out = out[:, -1, :] out = relu(dense_1(out)) out = softmax(dense_2(out)) ``` – YoungChae Nov 14 '22 at 08:55
  • but, It showed a different structure from the tensorflow model summary. When the conv1d_2 model proceeds, the size was output as [765, 127, 256] instead of [765, 124, 256]. Can you tell me how to fix it? – YoungChae Nov 14 '22 at 08:57
  • Please edit your question with the updated logs. – Ivan Nov 14 '22 at 09:01
  • Adds a TensorFlow summary image! – YoungChae Nov 16 '22 at 00:16