Neural network 向Pyrotch上的现有模型添加线性层
我正在尝试向现有网络添加一个新层(作为第一层),并在原始输入上对其进行训练。当我添加一个卷积层时,一切都很完美,但当我将其更改为线性时,它似乎无法训练。你知道为什么吗? 以下是整个网络:Neural network 向Pyrotch上的现有模型添加线性层,neural-network,deep-learning,conv-neural-network,pytorch,Neural Network,Deep Learning,Conv Neural Network,Pytorch,我正在尝试向现有网络添加一个新层(作为第一层),并在原始输入上对其进行训练。当我添加一个卷积层时,一切都很完美,但当我将其更改为线性时,它似乎无法训练。你知道为什么吗? 以下是整个网络: class ActorCritic(torch.nn.Module): #original model def __init__(self, num_inputs, action_space): super(ActorCritic, self).__init__() se
class ActorCritic(torch.nn.Module): #original model
def __init__(self, num_inputs, action_space):
super(ActorCritic, self).__init__()
self.conv1 = nn.Conv2d(num_inputs, 32, 3, stride=2, padding=1)
self.conv2 = nn.Conv2d(32, 32, 3, stride=2, padding=1)
self.conv3 = nn.Conv2d(32, 32, 3, stride=2, padding=1)
self.conv4 = nn.Conv2d(32, 32, 3, stride=2, padding=1)
self.lstm = nn.LSTMCell(32 * 3 * 3, 256)
num_outputs = action_space.n
self.critic_linear = nn.Linear(256, 1)
self.actor_linear = nn.Linear(256, num_outputs)
def forward(self, inputs):
inputs, (hx, cx) = inputs
x = F.elu(self.conv1(inputs))
x = F.elu(self.conv2(x))
x = F.elu(self.conv3(x))
x = F.elu(self.conv4(x))
x = x.view(-1, 32 * 3 * 3)
hx, cx = self.lstm(x, (hx, cx))
x = hx
return self.critic_linear(x), self.actor_linear(x), (hx, cx)
class TLModel(torch.nn.Module): #new model
def __init__(self, pretrained_model, num_inputs):
super(TLModel, self).__init__()
self.new_layer = nn.Linear(1*1*42*42, 1*1*42*42)
self.pretrained_model = pretrained_model
def forward(self, inputs):
inputs, (hx, cx) = inputs
x = F.elu(self.new_layer(inputs.view(-1, 1*1*42*42)))
return self.pretrained_model((x.view(1,1,42,42), (hx, cx)))
我尝试了不同的激活功能(不仅仅是elu)。它与conv一起工作:
class TLModel(torch.nn.Module):
def __init__(self, pretrained_model, num_inputs):
super(TLModel, self).__init__()
self.new_layer = nn.Conv2d(num_inputs, num_inputs, 1)
self.pretrained_model = pretrained_model
def forward(self, inputs):
inputs, (hx, cx) = inputs
x = F.elu(self.new_layer(inputs))
return self.pretrained_model((x, (hx, cx)))
输入的数量为1,输入的大小为1x1x42x42如果您提供了错误消息,这将非常有用。从你所写的,我只能猜测你忘记了压缩你的输入。您编写的输入大小为1x1x42x42,即为4维
nn.Conv2D
需要四维输入<代码>nn.线性需要一个二维输入
因此,在将其输入模型之前,请尝试调用input=input.squence()
。这将删除单例维度,因此将使您的输入成为二维的,因为有两个单例维度
作为旁注,nn.Linear
要求输入尺寸批次尺寸x专长尺寸
。线性层对数据真的有意义吗?
另一方面,当人们通常在网络中添加层时,他们会将层放在最后而不是开始,但我相信你有充分的理由这样做,并且知道你在做什么:)
祝你好运 尺寸不应该是问题,因为您正在“前进”功能中更改视图。我相信在网络开始时添加FCN并不会产生太大的影响,你可以通过移除图层并重新训练网络来检查,它会产生类似的结果
试着想象一下,如果你试图检测猫的特征(胡须、耳朵等),你并不真正需要图像中所有像素的信息,只需要相邻像素的信息,因此一个完全连接的层只会使网络复杂化。我实际上没有收到任何错误信息,它似乎不需要线性训练(但需要conv).我有理由添加fcn,但首先我想检查一个基本示例,看看它是否有效。我设法将权重初始化为单位矩阵,所以现在它在第一次运行时就可以工作了。我认为激活也在影响这个过程,因为它第一次只在某些功能下工作(比如tanh或none)。