简体   繁体   中英

Tensorflow to Pytorch CNN(Use nn.Conv1d)

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]

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM