deep-learning - 可以结合2个神经网络吗?
问题描述
我有一个 NET (示例来自这里)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# 1 input image channel, 6 output channels, 5x5 square convolution
# kernel
self.conv1 = nn.Conv2d(1, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
# an affine operation: y = Wx + b
self.fc1 = nn.Linear(16 * 5 * 5, 120) # 5*5 from image dimension
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
# Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
# If the size is a square, you can specify with a single number
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = torch.flatten(x, 1) # flatten all dimensions except the batch dimension
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Net()
和另一个网络(例如here)
class binaryClassification(nn.Module):
def __init__(self):
super(binaryClassification, self).__init__()
# Number of input features is 12.
self.layer_1 = nn.Linear(12, 64)
self.layer_2 = nn.Linear(64, 64)
self.layer_out = nn.Linear(64, 1)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(p=0.1)
self.batchnorm1 = nn.BatchNorm1d(64)
self.batchnorm2 = nn.BatchNorm1d(64)
def forward(self, inputs):
x = self.relu(self.layer_1(inputs))
x = self.batchnorm1(x)
x = self.relu(self.layer_2(x))
x = self.batchnorm2(x)
x = self.dropout(x)
x = self.layer_out(x)
return x
我想更改,例如“self.fc2 = nn.Linear(120, 84)”以便有 121 个输入,其中第 121 个是 binaryClassification 网络的 x(输出)。想法是:我想同时使用 CNN 网络和非 CNN 网络来训练两者,相互影响。
可能吗?我该怎么做?(Keras 或 Pytorch 示例都可以)。
或者这个想法很疯狂,并且有更简单的方法可以将数据和图像混合为独特网络的输入?
解决方案
最简单的方法是实例化两个模型,将两个预测相加并用它计算损失。这将通过两个模型进行反向传播:
net1 = Net1()
net2 = Net2()
bce = torch.nn.BCEWithLogitsLoss()
params = list(net1.parameters()) + list(net2.parameters())
optimizer = optim.SGD(params)
for (x, ground_truth) in enumerate(your_data_loader):
optimizer.zero_grad()
prediction = net1(x) + net2(x) # the 2 models must output tensors of same shape
loss = bce(prediction, ground_truth)
train_loss.backward()
optimizer.step()
你也可以例如
- 在单个模型中实现 Net1 和 Net2 的层
- 分别训练 Net1 和 Net2 并在以后集成它们
推荐阅读
- arrays - 如何创建包含范围的 Google 表格数组公式
- c# - Teradata c# 与 tdwallet 连接
- python - 从 Python 到 R 的翻译。执行时间差异很大
- drake - Drake Rigid_body_tree calculating the jacobian question
- python-3.x - 如何计算线方向的逆时针/顺时针角度?
- azure - Luis or text analytics for conversational data extraction
- function - Type mismatch on basic "apply twice" hello world in Haskell
- python - SCOPE_IDENTITY() 返回 NULL 而不是预期值
- spring-cloud-stream - spring cloud dataflow kafka binder 发送时出错-Magic v1 不支持记录标头
- computational-geometry - CGAL - 使用 corefine 布尔运算在顶点上保留属性