This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
/
utils.py
136 lines (107 loc) · 4.75 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import torch
# from torchviz import make_dot, make_dot_from_trace
from models import SpKBGATModified, SpKBGATConvOnly
from layers import ConvKB
from torch.autograd import Variable
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from copy import deepcopy
from preprocess import read_entity_from_id, read_relation_from_id, init_embeddings, build_data
from create_batch import Corpus
import random
import argparse
import os
import logging
import time
import pickle
CUDA = torch.cuda.is_available()
def save_model(model, name, epoch, folder_name):
print("Saving Model")
torch.save(model.state_dict(),
(folder_name + "trained_{}.pth").format(epoch))
print("Done saving Model")
gat_loss_func = nn.MarginRankingLoss(margin=0.5)
def GAT_Loss(train_indices, valid_invalid_ratio):
len_pos_triples = train_indices.shape[0] // (int(valid_invalid_ratio) + 1)
pos_triples = train_indices[:len_pos_triples]
neg_triples = train_indices[len_pos_triples:]
pos_triples = pos_triples.repeat(int(valid_invalid_ratio), 1)
source_embeds = entity_embed[pos_triples[:, 0]]
relation_embeds = relation_embed[pos_triples[:, 1]]
tail_embeds = entity_embed[pos_triples[:, 2]]
x = source_embeds + relation_embeds - tail_embeds
pos_norm = torch.norm(x, p=2, dim=1)
source_embeds = entity_embed[neg_triples[:, 0]]
relation_embeds = relation_embed[neg_triples[:, 1]]
tail_embeds = entity_embed[neg_triples[:, 2]]
x = source_embeds + relation_embeds - tail_embeds
neg_norm = torch.norm(x, p=2, dim=1)
y = torch.ones(int(args.valid_invalid_ratio)
* len_pos_triples).cuda()
loss = gat_loss_func(pos_norm, neg_norm, y)
return loss
def render_model_graph(model, Corpus_, train_indices, relation_adj, averaged_entity_vectors):
graph = make_dot(model(Corpus_.train_adj_matrix, train_indices, relation_adj, averaged_entity_vectors),
params=dict(model.named_parameters()))
graph.render()
def print_grads(model):
print(model.relation_embed.weight.grad)
print(model.relation_gat_1.attention_0.a.grad)
print(model.convKB.fc_layer.weight.grad)
for name, param in model.named_parameters():
print(name, param.grad)
def clip_gradients(model, gradient_clip_norm):
torch.nn.utils.clip_grad_norm_(model.parameters(), gradient_clip_norm)
for name, param in model.named_parameters():
if param.requires_grad:
print(name, "norm before clipping is -> ", param.grad.norm())
torch.nn.utils.clip_grad_norm_(param, args.gradient_clip_norm)
print(name, "norm beafterfore clipping is -> ", param.grad.norm())
def plot_grad_flow(named_parameters, parameters):
'''Plots the gradients flowing through different layers in the net during training.
Can be used for checking for possible gradient vanishing / exploding problems.
Usage: Plug this function in Trainer class after loss.backwards() as
"plot_grad_flow(self.model.named_parameters())" to visualize the gradient flow'''
ave_grads = []
max_grads = []
layers = []
for n, p in zip(named_parameters, parameters):
if(p.requires_grad) and ("bias" not in n):
layers.append(n)
ave_grads.append(p.grad.abs().mean())
max_grads.append(p.grad.abs().max())
plt.bar(np.arange(len(max_grads)), max_grads, alpha=0.1, lw=1, color="r")
plt.bar(np.arange(len(max_grads)), ave_grads, alpha=0.1, lw=1, color="b")
plt.hlines(0, 0, len(ave_grads) + 1, lw=2, color="g")
plt.xticks(range(0, len(ave_grads), 1), layers, rotation="vertical")
plt.xlim(left=0, right=len(ave_grads))
plt.ylim(bottom=-0.001, top=0.02) # zoom in on the lower gradient regions
plt.xlabel("Layers")
plt.ylabel("average gradient")
plt.title("Gradient flow")
plt.grid(True)
plt.legend([Line2D([0], [0], color="r", lw=4),
Line2D([0], [0], color="b", lw=4),
Line2D([0], [0], color="g", lw=4)], ['max-gradient', 'mean-gradient', 'zero-gradient'])
plt.savefig('initial.png')
plt.close()
def plot_grad_flow_low(named_parameters, parameters):
ave_grads = []
layers = []
for n, p in zip(named_parameters, parameters):
# print(n)
if(p.requires_grad) and ("bias" not in n):
layers.append(n)
ave_grads.append(p.grad.abs().mean())
plt.plot(ave_grads, alpha=0.3, color="b")
plt.hlines(0, 0, len(ave_grads) + 1, linewidth=1, color="k")
plt.xticks(range(0, len(ave_grads), 1), layers, rotation="vertical")
plt.xlim(xmin=0, xmax=len(ave_grads))
plt.xlabel("Layers")
plt.ylabel("average gradient")
plt.title("Gradient flow")
plt.grid(True)
plt.savefig('initial.png')
plt.close()