如何用标签信息做半监督学习
2021-12-02 本文已影响0人
heishanlaoniu
# 主要是用索引信息 用监督样本来做训练
def train(model, epochs):
model.train()
optimizer.zero_grad()
output, att, emb1, com1, com2, emb2, emb= model(features, sadj, fadj)
loss_class = F.nll_loss(output[idx_train], labels[idx_train])
# !!!主要是这里 监督损失的索引只用idx_train的!!!
loss_dep = (loss_dependence(emb1, com1, config.n) + loss_dependence(emb2, com2, config.n))/2
loss_com = common_loss(com1,com2)
loss = loss_class + config.beta * loss_dep + config.theta * loss_com
acc = accuracy(output[idx_train], labels[idx_train])
loss.backward()
optimizer.step()
acc_test, macro_f1, emb_test = main_test(model)
print('e:{}'.format(epochs),
'ltr: {:.4f}'.format(loss.item()),
'atr: {:.4f}'.format(acc.item()),
'ate: {:.4f}'.format(acc_test.item()),
'f1te:{:.4f}'.format(macro_f1.item()))
return loss.item(), acc_test.item(), macro_f1.item(), emb_test
def main_test(model):
model.eval()
output, att, emb1, com1, com2, emb2, emb = model(features, sadj, fadj)
acc_test = accuracy(output[idx_test], labels[idx_test])
# !!!这里测试的时候就用了测试的索引!!!
label_max = []
for idx in idx_test:
label_max.append(torch.argmax(output[idx]).item())
labelcpu = labels[idx_test].data.cpu()
macro_f1 = f1_score(labelcpu, label_max, average='macro')