知识图谱补全实验微调记录
2019-11-27 本文已影响0人
布口袋_天晴了
四个数据集:FB5k,FB15k-237,WN18,WN18RR
model = TuckER
iteration%50==0 torch.save(model.sate_dict(), PATH)
训练模型,并保存记录。(√)
TuckER模型代码
class TuckER(torch.nn.Module):
def __init__(self, d, d1, d2, **kwargs):
super(TuckER, self).__init__()
self.E = torch.nn.Embedding(len(d.entities), d1, padding_idx=0)
self.R = torch.nn.Embedding(len(d.relations), d2, padding_idx=0)
self.W = torch.nn.Parameter(torch.tensor(np.random.uniform(-1, 1, (d2, d1, d1)),
dtype=torch.float, device="cuda", requires_grad=True))
self.input_dropout = torch.nn.Dropout(kwargs["input_dropout"])
self.hidden_dropout1 = torch.nn.Dropout(kwargs["hidden_dropout1"])
self.hidden_dropout2 = torch.nn.Dropout(kwargs["hidden_dropout2"])
self.loss = torch.nn.BCELoss()
self.bn0 = torch.nn.BatchNorm1d(d1)
self.bn1 = torch.nn.BatchNorm1d(d1)
def init(self):
xavier_normal_(self.E.weight.data)
xavier_normal_(self.R.weight.data)
def forward(self, e1_idx, r_idx):
e1 = self.E(e1_idx)
x = self.bn0(e1)
x = self.input_dropout(x)
x = x.view(-1, 1, e1.size(1))
r = self.R(r_idx)
W_mat = torch.mm(r, self.W.view(r.size(1), -1))
W_mat = W_mat.view(-1, e1.size(1), e1.size(1))
W_mat = self.hidden_dropout1(W_mat)
x = torch.bmm(x, W_mat)
x = x.view(-1, e1.size(1))
x = self.bn1(x)
x = self.hidden_dropout2(x)
x = torch.mm(x, self.E.weight.transpose(1,0))
pred = torch.sigmoid(x)
return pred
加载预训练好的模型,并在此基础上建立其他模型来微调,使得补全结果变得更好。
1)将原始模型的参数固化,即,让这些参数不用被反向调参。
2)在原始模型的基础之上再搭建一个模型,让这个新模型进一步优化评价指标。
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation
加载固化的参数
class TuckER(torch.nn.Module):
def __init__(self, nfeat, nhid, dropout, d, d1, d2, **kwargs):
super(TuckER, self).__init__()
###用加载的模型继续训练或操作, model_current_dict
dir = os.getcwd() + "/save_models"
PATH = os.path.join(dir, "WN18tucker_200_weight_gpu.pth")
pretrained_dict = torch.load(PATH)
self.E = torch.nn.Embedding(len(d.entities), d1, padding_idx=0)
self.E.weight.data.copy_(pretrained_dict['E.weight'])
self.R = torch.nn.Embedding(len(d.relations), d2, padding_idx=0)
self.R.weight.data.copy_(pretrained_dict['R.weight'])
self.W = pretrained_dict['W']
# self.W = torch.nn.Parameter(torch.tensor(np.random.uniform(-1, 1, (d2, d1, d1)),
# dtype=torch.float, requires_grad=False))
# self.E = torch.nn.Embedding(len(d.entities), d1, padding_idx=0)
# self.R = torch.nn.Embedding(len(d.relations), d2, padding_idx=0)
# self.W = torch.nn.Parameter(torch.tensor(np.random.uniform(-1, 1, (d2, d1, d1)),
# dtype=torch.float, requires_grad=False))
self.E.weight.requires_grad = False
self.R.weight.requires_grad = False
self.bn0 = torch.nn.BatchNorm1d(d1)
self.bn1 = torch.nn.BatchNorm1d(d1)
self.input_dropout = torch.nn.Dropout(kwargs["input_dropout"])
self.hidden_dropout1 = torch.nn.Dropout(kwargs["hidden_dropout1"])
self.hidden_dropout2 = torch.nn.Dropout(kwargs["hidden_dropout2"])
self.loss = torch.nn.BCELoss()