如何将TensorFlow模型转换为PyTorch模型?

我是PyTorch的新手。这里有一个TensorFlow模型的架构,我希望将其转换为PyTorch模型。 enter image description here

我已经完成了大部分代码,但在几个地方感到困惑。

1) 在TensorFlow中,Conv2D函数接受过滤器作为输入。然而,在PyTorch中,该函数接受输入通道和输出通道的尺寸作为输入。那么,如何根据过滤器的尺寸找到等效的输入通道和输出通道数?

2) 在TensorFlow中,密集层有一个名为’nodes’的参数。然而,在PyTorch中,同样的层有两个不同的输入(输入参数的尺寸和目标参数的尺寸),我如何根据节点数来确定它们?

这是TensorFlow的代码。

from keras.utils import to_categoricalfrom keras.models import Sequential, load_modelfrom keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropoutmodel = Sequential()model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu', input_shape=X_train.shape[1:]))model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu'))model.add(MaxPool2D(pool_size=(2, 2)))model.add(Dropout(rate=0.25))model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))model.add(MaxPool2D(pool_size=(2, 2)))model.add(Dropout(rate=0.25))model.add(Flatten())model.add(Dense(256, activation='relu'))model.add(Dropout(rate=0.5))model.add(Dense(43, activation='softmax'))

这是我的代码:

import torch.nn.functional as Fimport torch# The network should inherit from the nn.Moduleclass Net(nn.Module):    def __init__(self):        super(Net, self).__init__()        # Define 2D convolution layers        # 3: input channels, 32: output channels, 5: kernel size, 1: stride        self.conv1 = nn.Conv2d(3, 32, 5, 1)   # The size of input channel is 3 because all images are coloured        self.conv2 = nn.Conv2d(32, 64, 5, 1)        self.conv3 = nn.Conv2d(64, 128, 3, 1)        self.conv3 = nn.Conv2d(128, 256, 3, 1)        # It will 'filter' out some of the input by the probability(assign zero)        self.dropout1 = nn.Dropout2d(0.25)        self.dropout2 = nn.Dropout2d(0.5)        # Fully connected layer: input size, output size        self.fc1 = nn.Linear(36864, 128)        self.fc2 = nn.Linear(128, 10)    # forward() link all layers together,    def forward(self, x):        x = self.conv1(x)        x = F.relu(x)        x = self.conv2(x)        x = F.relu(x)        x = F.max_pool2d(x, 2)        x = self.dropout1(x)        x = self.conv3(x)        x = F.relu(x)        x = self.conv4(x)        x = F.relu(x)        x = F.max_pool2d(x, 2)        x = self.dropout1(x)        x = torch.flatten(x, 1)        x = self.fc1(x)        x = F.relu(x)        x = self.dropout2(x)        x = self.fc2(x)        output = F.log_softmax(x, dim=1)        return output

提前感谢!


回答:

1) 在PyTorch中,我们将输入通道和输出通道作为输入。在你的第一层,输入通道将是图像中的颜色通道数。之后,它总是与你前一层的输出通道相同(TensorFlow中通过filters参数指定的输出通道)。

2) PyTorch在扁平化卷积输出时稍微有些麻烦,因为你需要自己计算形状。你可以使用公式来计算这个值(输出大小 = (宽度 – 过滤器大小 + 2 * 填充) / 步长 + 1),或者创建一个形状计算函数来获取虚拟图像通过网络卷积部分后的形状。这个参数将是你的输入尺寸参数;你的输出尺寸参数将只是你希望在下一完全连接层中的节点数。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注