Bert+Resnet联合学习,pytorch模型实例化后为空
Bert + Resnet joint learning, pytorch model is empty after instantiation
我正在写一个简单的联合模型,它有两个分支,一个分支是resnet50,另一个是bert
。我连接两个输出并将其传递给具有 2 个输出神经元的简单线性层。
我实现了以下模型:
import torch
from torch import nn
import torchvision.models as models
import torch.nn as nn
from collections import OrderedDict
from transformers import BertModel
class BertResNet(nn.Module):
def __init__(self):
super(BertResNet, self).__init__()
# resnet
resnet50 = models.resnet50(pretrained=True)
n_inputs = resnet50.fc.in_features
# compressed embedding space
classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(n_inputs, 512))
]))
resnet50.fc = classifier # 512 out resnet
bert = BertModel.from_pretrained('bert-base-uncased')
# final classification layer
classification = nn.Linear(512 + 768, 2)
#print(resnet50)
#print(bert)
def forward(self, img, text):
res_emb = self.resnet50(img)
bert_emb = self.bert(text)
combined = torch.cat(res_emb,
bet_emb, dim=1)
out = self.classification(combined)
return out
但是当我实例化时,我得到一个空模型:
bert_resnet = BertResNet()
print(bert_resnet)
输出:
BertResNet()
list(bert_resnet.parameters())
也 returns []
您从未将模型分配给 BertResNet
class 对象的任何属性。 __init__
方法中有临时变量,但一旦完成,这些变量将被丢弃。他们应该被分配给 self
:
def __init__(self):
super(BertResNet, self).__init__()
# resnet
self.resnet50 = models.resnet50(pretrained=True)
n_inputs = self.resnet50.fc.in_features
# compressed embedding space
self.classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(n_inputs, 512))
]))
self.resnet50.fc = classifier # 512 out resnet
self.bert = BertModel.from_pretrained('bert-base-uncased')
# final classification layer
self.classification = nn.Linear(512 + 768, 2)
我正在写一个简单的联合模型,它有两个分支,一个分支是resnet50,另一个是bert
。我连接两个输出并将其传递给具有 2 个输出神经元的简单线性层。
我实现了以下模型:
import torch
from torch import nn
import torchvision.models as models
import torch.nn as nn
from collections import OrderedDict
from transformers import BertModel
class BertResNet(nn.Module):
def __init__(self):
super(BertResNet, self).__init__()
# resnet
resnet50 = models.resnet50(pretrained=True)
n_inputs = resnet50.fc.in_features
# compressed embedding space
classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(n_inputs, 512))
]))
resnet50.fc = classifier # 512 out resnet
bert = BertModel.from_pretrained('bert-base-uncased')
# final classification layer
classification = nn.Linear(512 + 768, 2)
#print(resnet50)
#print(bert)
def forward(self, img, text):
res_emb = self.resnet50(img)
bert_emb = self.bert(text)
combined = torch.cat(res_emb,
bet_emb, dim=1)
out = self.classification(combined)
return out
但是当我实例化时,我得到一个空模型:
bert_resnet = BertResNet()
print(bert_resnet)
输出:
BertResNet()
list(bert_resnet.parameters())
也 returns []
您从未将模型分配给 BertResNet
class 对象的任何属性。 __init__
方法中有临时变量,但一旦完成,这些变量将被丢弃。他们应该被分配给 self
:
def __init__(self):
super(BertResNet, self).__init__()
# resnet
self.resnet50 = models.resnet50(pretrained=True)
n_inputs = self.resnet50.fc.in_features
# compressed embedding space
self.classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(n_inputs, 512))
]))
self.resnet50.fc = classifier # 512 out resnet
self.bert = BertModel.from_pretrained('bert-base-uncased')
# final classification layer
self.classification = nn.Linear(512 + 768, 2)