注意力机制的特点是,它的输入向量长度可变,通过将注意力集中在最相关的部分来做出决定。注意力机制结合RNN或者CNN的方法。
1 实战描述
【主要目的:将注意力机制用在图神经网络中,完成图注意力神经网络的结构和搭建】
1.1 实现目的
有一个记录论文信息的数据集,数据集里面含有每一篇论文的关键词以及分类信息,同时还有论文间互相引用的信息。搭建AI模型,对数据集中的论文信息进行分析,使模型学习已有论文的分类特征,以便预测出未知分类的论文类别。
1.2 图注意力网络图
图注意力网络(GraphAttention Network,GAT)在GCN的基础上添加了一个隐藏的自注意力(self-attention)层。通过叠加Self-attention层,在卷积过程中可将不同的权重分配给邻域内的不同顶点,同时处理不同大小的邻域。
在实际计算时,自注意力机制可以使用多套权重同时进行计算,并且彼此之间不共享权重,能够使顶点确定知识的相关性,是否可忽略。
2 代码编写
本次要构建的图网络
2.1 代码实战:引入基础模块,设置运行环境----Cora_GAT.py(第1部分)
from pathlib import Path # 引入提升路径的兼容性
# 引入矩阵运算的相关库
import numpy as np
import pandas as pd
from scipy.sparse import coo_matrix,csr_matrix,diags,eye
# 引入深度学习框架库
import torch
from torch import nn
import torch.nn.functional as F
# 引入绘图库
import matplotlib.pyplot as plt
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"# 1.1 导入基础模块,并设置运行环境
# 输出计算资源情况
device = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu')
print(device) # 输出 cuda# 输出样本路径
path = Path('./data/cora')
print(path) # 输出 cuda
输出结果:
2.2 代码实现:读取并解析论文数据----Cora_GAT.py(第2部分)
# 1.2 读取并解析论文数据
# 读取论文内容数据,将其转化为数据
paper_features_label = np.genfromtxt(path/'cora.content',dtype=np.str_) # 使用Path对象的路径构造,实例化的内容为cora.content。path/'cora.content'表示路径为'data/cora/cora.content'的字符串
print(paper_features_label,np.shape(paper_features_label)) # 打印数据集内容与数据的形状# 取出数据集中的第一列:论文ID
papers = paper_features_label[:,0].astype(np.int32)
print("论文ID序列:",papers) # 输出所有论文ID
# 论文重新编号,并将其映射到论文ID中,实现论文的统一管理
paper2idx = {k:v for v,k in enumerate(papers)}# 将数据中间部分的字标签取出,转化成矩阵
features = csr_matrix(paper_features_label[:,1:-1],dtype=np.float32)
print("字标签矩阵的形状:",np.shape(features)) # 字标签矩阵的形状# 将数据的最后一项的文章分类属性取出,转化为分类的索引
labels = paper_features_label[:,-1]
lbl2idx = { k:v for v,k in enumerate(sorted(np.unique(labels)))}
labels = [lbl2idx[e] for e in labels]
print("论文类别的索引号:",lbl2idx,labels[:5])
输出:
2.3 读取并解析论文关系数据
载入论文的关系数据,将数据中用论文ID表示的关系转化成重新编号后的关系,将每篇论文当作一个顶点,论文间的引用关系作为边,这样论文的关系数据就可以用一个图结构来表示。
计算该图结构的邻接矩阵并将其转化为无向图邻接矩阵。
2.3.1 代码实现:转化矩阵----Cora_GAT.py(第3部分)
# 1.3 读取并解析论文关系数据
# 读取论文关系数据,并将其转化为数据
edges = np.genfromtxt(path/'cora.cites',dtype=np.int32) # 将数据集中论文的引用关系以数据的形式读入
print(edges,np.shape(edges))
# 转化为新编号节点间的关系:将数据集中论文ID表示的关系转化为重新编号后的关系
edges = np.asarray([paper2idx[e] for e in edges.flatten()],np.int32).reshape(edges.shape)
print("新编号节点间的对应关系:",edges,edges.shape)
# 计算邻接矩阵,行与列都是论文个数:由论文引用关系所表示的图结构生成邻接矩阵。
adj = coo_matrix((np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),shape=(len(labels), len(labels)), dtype=np.float32)
# 生成无向图对称矩阵:将有向图的邻接矩阵转化为无向图的邻接矩阵。Tip:转化为无向图的原因:主要用于对论文的分类,论文的引用关系主要提供单个特征之间的关联,故更看重是不是有关系,所以无向图即可。
adj_long = adj.multiply(adj.T < adj)
adj = adj_long + adj_long.T
输出:
2.4 代码实现:加工图结构的矩阵数据----Cora_GAT.py(第4部分)
# 1.4 加工图结构的矩阵数据
def normalize_adj(mx):rowsum = np.array(mx.sum(1))r_inv = np.power(rowsum,-0.5).flatten()r_inv[np.isinf(r_inv)] = 0.0r_mat_inv = diags(r_inv)return mx.dot(r_mat_inv).transpose().dot(r_mat_inv) # 兑成归一化拉普拉斯矩阵实现邻接矩阵的转化adj = normalize_adj(adj + eye(adj.shape[0])) # 对邻接矩阵进行转化对称归一化拉普拉斯矩阵转化
2.5 将数据转化为张量,并分配运算资源
将加工好的图结构矩阵数据转为PyTorch支持的张量类型,并将其分成3份,分别用来进行训练、测试和验证。
2.5.1 代码实现:将数据转化为张量,并分配运算资源----Cora_GAT.py(第5部分)
# 1.5 将数据转化为张量,并分配运算资源
adj = torch.FloatTensor(adj.todense()) # 节点间关系 todense()方法将其转换回稠密矩阵。
features = torch.FloatTensor(features.todense()) # 节点自身的特征
labels = torch.LongTensor(labels) # 对每个节点的分类标签# 划分数据集
n_train = 200 # 训练数据集大小
n_val = 300 # 验证数据集大小
n_test = len(features) - n_train - n_val # 测试数据集大小
np.random.seed(34)
idxs = np.random.permutation(len(features)) # 将原有的索引打乱顺序# 计算每个数据集的索引
idx_train = torch.LongTensor(idxs[:n_train]) # 根据指定训练数据集的大小并划分出其对应的训练数据集索引
idx_val = torch.LongTensor(idxs[n_train:n_train+n_val])# 根据指定验证数据集的大小并划分出其对应的验证数据集索引
idx_test = torch.LongTensor(idxs[n_train+n_val:])# 根据指定测试数据集的大小并划分出其对应的测试数据集索引# 分配运算资源
adj = adj.to(device)
features = features.to(device)
labels = labels.to(device)
idx_train = idx_train.to(device)
idx_val = idx_val.to(device)
idx_test = idx_test.to(device)
2.6 代码实现:定义Mish激活函数与图注意力层类----Cora_GAT.py(第6部分)
# 1.6 定义Mish激活函数与图注意力层类
def mish(x): # 性能优于RElu函数return x * (torch.tanh(F.softplus(x)))
# 图注意力层类
class GraphAttentionLayer(nn.Module): # 图注意力层# 初始化def __init__(self,in_features,out_features,dropout=0.6):super(GraphAttentionLayer, self).__init__()self.dropout = dropoutself.in_features = in_features # 定义输入特征维度self.out_features = out_features # 定义输出特征维度self.W = nn.Parameter(torch.zeros(size=(in_features,out_features)))nn.init.xavier_uniform_(self.W) # 初始化全连接权重self.a = nn.Parameter(torch.zeros(size=(2 * out_features,1)))nn.init.xavier_uniform_(self.a) # 初始化注意力权重def forward(self,input,adj):h = torch.mm(input,self.W) # 全连接处理N = h.size()[0]# 对全连接后的特征数据分别进行基于批次维度和特征维度的复制,并将复制结果连接在一起。# 这种操作使得顶点中的特征数据进行了充分的排列组合,结果中的每行信息都包含两个顶点特征。接下来的注意力机制便是基于每对顶点特征进行计算的。a_input = torch.cat([h.repeat(1,N).view(N * N ,-1),h.repeat(N,1)],dim=1).view(N,-1,2 * self.out_features) # 主要功能将顶点特征两两搭配,连接在一起,生成数据形状[N,N,2 * self.out_features]e = mish(torch.matmul(a_input,self.a).squeeze(2)) # 计算注意力zero_vec = -9e15 * torch.ones_like(e) # 初始化最小值:该值用于填充被过滤掉的特征对象atenion。如果在过滤时,直接对过滤排的特征赋值为0,那么模型会无法收敛。attention = torch.where(adj>0,e,zero_vec) # 过滤注意力 :按照邻接矩阵中大于0的边对注意力结果进行过滤,使注意力按照图中的顶点配对的范围进行计算。attention = F.softmax(attention,dim=1) # 对注意力分数进行归一化:使用F.Sofmax()函数对最终的注意力机制进行归一化,得到注意力分数(总和为1)。attention = F.dropout(attention,self.dropout,training=self.training)h_prime = torch.matmul(attention,h) # 使用注意力处理特征:将最终的注意力作用到全连接的结果上以完成计算。return mish(h_prime)
2.7 代码实现:搭建图注意力模型----Cora_GAT.py(第7部分)
# 1.7 搭建图注意力模型
class GAT(nn.Module):# 图注意力模型类def __init__(self,nfeat,nclasses,nhid,dropout,nheads): # 图注意力模型类的初始化方法,支持多套注意力机制同时运算,其参数nheads用于指定注意力的计算套数。super(GAT, self).__init__()# 注意力层self.attentions = [GraphAttentionLayer(nfeat,nhid,dropout) for _ in range(nheads)] # 按照指定的注意力套数生成多套注意力层for i , attention in enumerate(self.attentions): # 将注意力层添加到模型self.add_module('attention_{}'.format(i),attention)# 输出层self.out_att = GraphAttentionLayer(nhid * nheads,nclasses,dropout)def forward(self,x,adj): # 定义正向传播方法x = torch.cat([att(x, adj) for att in self.attentions], dim=1)return self.out_att(x, adj)n_labels = labels.max().item() + 1 # 获取分类个数7
n_features = features.shape[1] # 获取节点特征维度 1433
print(n_labels,n_features) # 输出7与1433def accuracy(output,y): # 定义函数计算准确率return (output.argmax(1) == y).type(torch.float32).mean().item()### 定义函数来实现模型的训练过程。与深度学习任务不同,图卷积在训练时需要传入样本间的关系数据。
# 因为该关系数据是与节点数相等的方阵,所以传入的样本数也要与节点数相同,在计算loss值时,可以通过索引从总的运算结果中取出训练集的结果。
def step(): # 定义函数来训练模型 Tip:在图卷积任务中,无论是用模型进行预测还是训练,都需要将全部的图结构方阵输入model.train()optimizer.zero_grad()output = model(features,adj) # 将全部数据载入模型,只用训练数据计算损失loss = F.cross_entropy(output[idx_train],labels[idx_train])acc = accuracy(output[idx_train],labels[idx_train]) # 计算准确率loss.backward()optimizer.step()return loss.item(),accdef evaluate(idx): # 定义函数来评估模型 Tip:在图卷积任务中,无论是用模型进行预测还是训练,都需要将全部的图结构方阵输入model.eval()output = model(features, adj) # 将全部数据载入模型,用指定索引评估模型结果loss = F.cross_entropy(output[idx], labels[idx]).item()return loss, accuracy(output[idx], labels[idx])
2.8 Ranger优化器
图卷积神经网络的层数不宜过多,一般在3层左右即可。本例将实现一个3层的图卷积神经网络,每层的维度变化如图9-15所示。
使用循环语句训练模型,并将模型结果可视化。
2.8.1 代码实现:用Ranger优化器训练模型并可视化结果----Cora_GAT.py(第8部分)
# 1.8 使用Ranger优化器训练模型并可视化
model = GAT(n_features, n_labels, 16,0.1,8).to(device) # 向GAT传入的后3个参数分别代表输出维度(16)、Dropout的丢弃率(0.1)、注意力的计算套数(8)from tqdm import tqdm
from Cora_ranger import * # 引入Ranger优化器
optimizer = Ranger(model.parameters()) # 使用Ranger优化器# 训练模型
epochs = 1000
print_steps = 50
train_loss, train_acc = [], []
val_loss, val_acc = [], []
for i in tqdm(range(epochs)):tl,ta = step()train_loss = train_loss + [tl]train_acc = train_acc + [ta]if (i+1) % print_steps == 0 or i == 0:tl,ta = evaluate(idx_train)vl,va = evaluate(idx_val)val_loss = val_loss + [vl]val_acc = val_acc + [va]print(f'{i + 1:6d}/{epochs}: train_loss={tl:.4f}, train_acc={ta:.4f}' + f', val_loss={vl:.4f}, val_acc={va:.4f}')# 输出最终结果
final_train, final_val, final_test = evaluate(idx_train), evaluate(idx_val), evaluate(idx_test)
print(f'Train : loss={final_train[0]:.4f}, accuracy={final_train[1]:.4f}')
print(f'Validation: loss={final_val[0]:.4f}, accuracy={final_val[1]:.4f}')
print(f'Test : loss={final_test[0]:.4f}, accuracy={final_test[1]:.4f}')# 可视化训练过程
fig, axes = plt.subplots(1, 2, figsize=(15,5))
ax = axes[0]
axes[0].plot(train_loss[::print_steps] + [train_loss[-1]], label='Train')
axes[0].plot(val_loss, label='Validation')
axes[1].plot(train_acc[::print_steps] + [train_acc[-1]], label='Train')
axes[1].plot(val_acc, label='Validation')
for ax,t in zip(axes, ['Loss', 'Accuracy']): ax.legend(), ax.set_title(t, size=15)# 输出模型的预测结果
output = model(features, adj)
samples = 10
idx_sample = idx_test[torch.randperm(len(idx_test))[:samples]]
# 将样本标签与预测结果进行比较
idx2lbl = {v:k for k,v in lbl2idx.items()}
df = pd.DataFrame({'Real': [idx2lbl[e] for e in labels[idx_sample].tolist()],'Pred': [idx2lbl[e] for e in output[idx_sample].argmax(1).tolist()]})
print(df)
2.7 程序输出汇总
2.7.1 训练过程
2.7.2 训练结果
3 代码汇总
3.1 Cora_GAT.py
from pathlib import Path # 引入提升路径的兼容性
# 引入矩阵运算的相关库
import numpy as np
import pandas as pd
from scipy.sparse import coo_matrix,csr_matrix,diags,eye
# 引入深度学习框架库
import torch
from torch import nn
import torch.nn.functional as F
# 引入绘图库
import matplotlib.pyplot as plt
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"# 1.1 导入基础模块,并设置运行环境
# 输出计算资源情况
device = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu')
print(device) # 输出 cuda# 输出样本路径
path = Path('./data/cora')
print(path) # 输出 cuda# 1.2 读取并解析论文数据
# 读取论文内容数据,将其转化为数据
paper_features_label = np.genfromtxt(path/'cora.content',dtype=np.str_) # 使用Path对象的路径构造,实例化的内容为cora.content。path/'cora.content'表示路径为'data/cora/cora.content'的字符串
print(paper_features_label,np.shape(paper_features_label)) # 打印数据集内容与数据的形状# 取出数据集中的第一列:论文ID
papers = paper_features_label[:,0].astype(np.int32)
print("论文ID序列:",papers) # 输出所有论文ID
# 论文重新编号,并将其映射到论文ID中,实现论文的统一管理
paper2idx = {k:v for v,k in enumerate(papers)}# 将数据中间部分的字标签取出,转化成矩阵
features = csr_matrix(paper_features_label[:,1:-1],dtype=np.float32)
print("字标签矩阵的形状:",np.shape(features)) # 字标签矩阵的形状# 将数据的最后一项的文章分类属性取出,转化为分类的索引
labels = paper_features_label[:,-1]
lbl2idx = { k:v for v,k in enumerate(sorted(np.unique(labels)))}
labels = [lbl2idx[e] for e in labels]
print("论文类别的索引号:",lbl2idx,labels[:5])# 1.3 读取并解析论文关系数据
# 读取论文关系数据,并将其转化为数据
edges = np.genfromtxt(path/'cora.cites',dtype=np.int32) # 将数据集中论文的引用关系以数据的形式读入
print(edges,np.shape(edges))
# 转化为新编号节点间的关系:将数据集中论文ID表示的关系转化为重新编号后的关系
edges = np.asarray([paper2idx[e] for e in edges.flatten()],np.int32).reshape(edges.shape)
print("新编号节点间的对应关系:",edges,edges.shape)
# 计算邻接矩阵,行与列都是论文个数:由论文引用关系所表示的图结构生成邻接矩阵。
adj = coo_matrix((np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),shape=(len(labels), len(labels)), dtype=np.float32)
# 生成无向图对称矩阵:将有向图的邻接矩阵转化为无向图的邻接矩阵。Tip:转化为无向图的原因:主要用于对论文的分类,论文的引用关系主要提供单个特征之间的关联,故更看重是不是有关系,所以无向图即可。
adj_long = adj.multiply(adj.T < adj)
adj = adj_long + adj_long.T# 1.4 加工图结构的矩阵数据
def normalize_adj(mx):rowsum = np.array(mx.sum(1))r_inv = np.power(rowsum,-0.5).flatten()r_inv[np.isinf(r_inv)] = 0.0r_mat_inv = diags(r_inv)return mx.dot(r_mat_inv).transpose().dot(r_mat_inv) # 兑成归一化拉普拉斯矩阵实现邻接矩阵的转化adj = normalize_adj(adj + eye(adj.shape[0])) # 对邻接矩阵进行转化对称归一化拉普拉斯矩阵转化# 1.5 将数据转化为张量,并分配运算资源
adj = torch.FloatTensor(adj.todense()) # 节点间关系 todense()方法将其转换回稠密矩阵。
features = torch.FloatTensor(features.todense()) # 节点自身的特征
labels = torch.LongTensor(labels) # 对每个节点的分类标签# 划分数据集
n_train = 200 # 训练数据集大小
n_val = 300 # 验证数据集大小
n_test = len(features) - n_train - n_val # 测试数据集大小
np.random.seed(34)
idxs = np.random.permutation(len(features)) # 将原有的索引打乱顺序# 计算每个数据集的索引
idx_train = torch.LongTensor(idxs[:n_train]) # 根据指定训练数据集的大小并划分出其对应的训练数据集索引
idx_val = torch.LongTensor(idxs[n_train:n_train+n_val])# 根据指定验证数据集的大小并划分出其对应的验证数据集索引
idx_test = torch.LongTensor(idxs[n_train+n_val:])# 根据指定测试数据集的大小并划分出其对应的测试数据集索引# 分配运算资源
adj = adj.to(device)
features = features.to(device)
labels = labels.to(device)
idx_train = idx_train.to(device)
idx_val = idx_val.to(device)
idx_test = idx_test.to(device)# 1.6 定义Mish激活函数与图注意力层类
def mish(x): # 性能优于RElu函数return x * (torch.tanh(F.softplus(x)))
# 图注意力层类
class GraphAttentionLayer(nn.Module): # 图注意力层# 初始化def __init__(self,in_features,out_features,dropout=0.6):super(GraphAttentionLayer, self).__init__()self.dropout = dropoutself.in_features = in_features # 定义输入特征维度self.out_features = out_features # 定义输出特征维度self.W = nn.Parameter(torch.zeros(size=(in_features,out_features)))nn.init.xavier_uniform_(self.W) # 初始化全连接权重self.a = nn.Parameter(torch.zeros(size=(2 * out_features,1)))nn.init.xavier_uniform_(self.a) # 初始化注意力权重def forward(self,input,adj):h = torch.mm(input,self.W) # 全连接处理N = h.size()[0]# 对全连接后的特征数据分别进行基于批次维度和特征维度的复制,并将复制结果连接在一起。# 这种操作使得顶点中的特征数据进行了充分的排列组合,结果中的每行信息都包含两个顶点特征。接下来的注意力机制便是基于每对顶点特征进行计算的。a_input = torch.cat([h.repeat(1,N).view(N * N ,-1),h.repeat(N,1)],dim=1).view(N,-1,2 * self.out_features) # 主要功能将顶点特征两两搭配,连接在一起,生成数据形状[N,N,2 * self.out_features]e = mish(torch.matmul(a_input,self.a).squeeze(2)) # 计算注意力zero_vec = -9e15 * torch.ones_like(e) # 初始化最小值:该值用于填充被过滤掉的特征对象atenion。如果在过滤时,直接对过滤排的特征赋值为0,那么模型会无法收敛。attention = torch.where(adj>0,e,zero_vec) # 过滤注意力 :按照邻接矩阵中大于0的边对注意力结果进行过滤,使注意力按照图中的顶点配对的范围进行计算。attention = F.softmax(attention,dim=1) # 对注意力分数进行归一化:使用F.Sofmax()函数对最终的注意力机制进行归一化,得到注意力分数(总和为1)。attention = F.dropout(attention,self.dropout,training=self.training)h_prime = torch.matmul(attention,h) # 使用注意力处理特征:将最终的注意力作用到全连接的结果上以完成计算。return mish(h_prime)# 1.7 搭建图注意力模型
class GAT(nn.Module):# 图注意力模型类def __init__(self,nfeat,nclasses,nhid,dropout,nheads): # 图注意力模型类的初始化方法,支持多套注意力机制同时运算,其参数nheads用于指定注意力的计算套数。super(GAT, self).__init__()# 注意力层self.attentions = [GraphAttentionLayer(nfeat,nhid,dropout) for _ in range(nheads)] # 按照指定的注意力套数生成多套注意力层for i , attention in enumerate(self.attentions): # 将注意力层添加到模型self.add_module('attention_{}'.format(i),attention)# 输出层self.out_att = GraphAttentionLayer(nhid * nheads,nclasses,dropout)def forward(self,x,adj): # 定义正向传播方法x = torch.cat([att(x, adj) for att in self.attentions], dim=1)return self.out_att(x, adj)n_labels = labels.max().item() + 1 # 获取分类个数7
n_features = features.shape[1] # 获取节点特征维度 1433
print(n_labels,n_features) # 输出7与1433def accuracy(output,y): # 定义函数计算准确率return (output.argmax(1) == y).type(torch.float32).mean().item()### 定义函数来实现模型的训练过程。与深度学习任务不同,图卷积在训练时需要传入样本间的关系数据。
# 因为该关系数据是与节点数相等的方阵,所以传入的样本数也要与节点数相同,在计算loss值时,可以通过索引从总的运算结果中取出训练集的结果。
def step(): # 定义函数来训练模型 Tip:在图卷积任务中,无论是用模型进行预测还是训练,都需要将全部的图结构方阵输入model.train()optimizer.zero_grad()output = model(features,adj) # 将全部数据载入模型,只用训练数据计算损失loss = F.cross_entropy(output[idx_train],labels[idx_train])acc = accuracy(output[idx_train],labels[idx_train]) # 计算准确率loss.backward()optimizer.step()return loss.item(),accdef evaluate(idx): # 定义函数来评估模型 Tip:在图卷积任务中,无论是用模型进行预测还是训练,都需要将全部的图结构方阵输入model.eval()output = model(features, adj) # 将全部数据载入模型,用指定索引评估模型结果loss = F.cross_entropy(output[idx], labels[idx]).item()return loss, accuracy(output[idx], labels[idx])# 1.8 使用Ranger优化器训练模型并可视化
model = GAT(n_features, n_labels, 16,0.1,8).to(device) # 向GAT传入的后3个参数分别代表输出维度(16)、Dropout的丢弃率(0.1)、注意力的计算套数(8)from tqdm import tqdm
from Cora_ranger import * # 引入Ranger优化器
optimizer = Ranger(model.parameters()) # 使用Ranger优化器# 训练模型
epochs = 1000
print_steps = 50
train_loss, train_acc = [], []
val_loss, val_acc = [], []
for i in tqdm(range(epochs)):tl,ta = step()train_loss = train_loss + [tl]train_acc = train_acc + [ta]if (i+1) % print_steps == 0 or i == 0:tl,ta = evaluate(idx_train)vl,va = evaluate(idx_val)val_loss = val_loss + [vl]val_acc = val_acc + [va]print(f'{i + 1:6d}/{epochs}: train_loss={tl:.4f}, train_acc={ta:.4f}' + f', val_loss={vl:.4f}, val_acc={va:.4f}')# 输出最终结果
final_train, final_val, final_test = evaluate(idx_train), evaluate(idx_val), evaluate(idx_test)
print(f'Train : loss={final_train[0]:.4f}, accuracy={final_train[1]:.4f}')
print(f'Validation: loss={final_val[0]:.4f}, accuracy={final_val[1]:.4f}')
print(f'Test : loss={final_test[0]:.4f}, accuracy={final_test[1]:.4f}')# 可视化训练过程
fig, axes = plt.subplots(1, 2, figsize=(15,5))
ax = axes[0]
axes[0].plot(train_loss[::print_steps] + [train_loss[-1]], label='Train')
axes[0].plot(val_loss, label='Validation')
axes[1].plot(train_acc[::print_steps] + [train_acc[-1]], label='Train')
axes[1].plot(val_acc, label='Validation')
for ax,t in zip(axes, ['Loss', 'Accuracy']): ax.legend(), ax.set_title(t, size=15)# 输出模型的预测结果
output = model(features, adj)
samples = 10
idx_sample = idx_test[torch.randperm(len(idx_test))[:samples]]
# 将样本标签与预测结果进行比较
idx2lbl = {v:k for k,v in lbl2idx.items()}
df = pd.DataFrame({'Real': [idx2lbl[e] for e in labels[idx_sample].tolist()],'Pred': [idx2lbl[e] for e in output[idx_sample].argmax(1).tolist()]})
print(df)
3.2 Cora_ranger.py
#Ranger deep learning optimizer - RAdam + Lookahead combined.
#https://github.com/lessw2020/Ranger-Deep-Learning-Optimizer#Ranger has now been used to capture 12 records on the FastAI leaderboard.#This version = 9.3.19 #Credits:
#RAdam --> https://github.com/LiyuanLucasLiu/RAdam
#Lookahead --> rewritten by lessw2020, but big thanks to Github @LonePatient and @RWightman for ideas from their code.
#Lookahead paper --> MZhang,G Hinton https://arxiv.org/abs/1907.08610#summary of changes:
#full code integration with all updates at param level instead of group, moves slow weights into state dict (from generic weights),
#supports group learning rates (thanks @SHolderbach), fixes sporadic load from saved model issues.
#changes 8/31/19 - fix references to *self*.N_sma_threshold; #changed eps to 1e-5 as better default than 1e-8.import math
import torch
from torch.optim.optimizer import Optimizer, required
import itertools as itclass Ranger(Optimizer):def __init__(self, params, lr=1e-3, alpha=0.5, k=6, N_sma_threshhold=5, betas=(.95,0.999), eps=1e-5, weight_decay=0):#parameter checksif not 0.0 <= alpha <= 1.0:raise ValueError(f'Invalid slow update rate: {alpha}')if not 1 <= k:raise ValueError(f'Invalid lookahead steps: {k}')if not lr > 0:raise ValueError(f'Invalid Learning Rate: {lr}')if not eps > 0:raise ValueError(f'Invalid eps: {eps}')#parameter comments:# beta1 (momentum) of .95 seems to work better than .90...#N_sma_threshold of 5 seems better in testing than 4.#In both cases, worth testing on your dataset (.90 vs .95, 4 vs 5) to make sure which works best for you.#prep defaults and init torch.optim basedefaults = dict(lr=lr, alpha=alpha, k=k, step_counter=0, betas=betas, N_sma_threshhold=N_sma_threshhold, eps=eps, weight_decay=weight_decay)super().__init__(params,defaults)#adjustable thresholdself.N_sma_threshhold = N_sma_threshhold#now we can get to work...#removed as we now use step from RAdam...no need for duplicate step counting#for group in self.param_groups:# group["step_counter"] = 0#print("group step counter init")#look ahead paramsself.alpha = alphaself.k = k #radam buffer for stateself.radam_buffer = [[None,None,None] for ind in range(10)]#self.first_run_check=0#lookahead weights#9/2/19 - lookahead param tensors have been moved to state storage. #This should resolve issues with load/save where weights were left in GPU memory from first load, slowing down future runs.#self.slow_weights = [[p.clone().detach() for p in group['params']]# for group in self.param_groups]#don't use grad for lookahead weights#for w in it.chain(*self.slow_weights):# w.requires_grad = Falsedef __setstate__(self, state):print("set state called")super(Ranger, self).__setstate__(state)def step(self, closure=None):loss = None#note - below is commented out b/c I have other work that passes back the loss as a float, and thus not a callable closure. #Uncomment if you need to use the actual closure...#if closure is not None:#loss = closure()#Evaluate averages and grad, update param tensorsfor group in self.param_groups:for p in group['params']:if p.grad is None:continuegrad = p.grad.data.float()if grad.is_sparse:raise RuntimeError('Ranger optimizer does not support sparse gradients')p_data_fp32 = p.data.float()state = self.state[p] #get state dict for this paramif len(state) == 0: #if first time to run...init dictionary with our desired entries#if self.first_run_check==0:#self.first_run_check=1#print("Initializing slow buffer...should not see this at load from saved model!")state['step'] = 0state['exp_avg'] = torch.zeros_like(p_data_fp32)state['exp_avg_sq'] = torch.zeros_like(p_data_fp32)#look ahead weight storage now in state dict state['slow_buffer'] = torch.empty_like(p.data)state['slow_buffer'].copy_(p.data)else:state['exp_avg'] = state['exp_avg'].type_as(p_data_fp32)state['exp_avg_sq'] = state['exp_avg_sq'].type_as(p_data_fp32)#begin computations exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq']beta1, beta2 = group['betas']#compute variance mov avgexp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad)#compute mean moving avgexp_avg.mul_(beta1).add_(1 - beta1, grad)state['step'] += 1buffered = self.radam_buffer[int(state['step'] % 10)]if state['step'] == buffered[0]:N_sma, step_size = buffered[1], buffered[2]else:buffered[0] = state['step']beta2_t = beta2 ** state['step']N_sma_max = 2 / (1 - beta2) - 1N_sma = N_sma_max - 2 * state['step'] * beta2_t / (1 - beta2_t)buffered[1] = N_smaif N_sma > self.N_sma_threshhold:step_size = math.sqrt((1 - beta2_t) * (N_sma - 4) / (N_sma_max - 4) * (N_sma - 2) / N_sma * N_sma_max / (N_sma_max - 2)) / (1 - beta1 ** state['step'])else:step_size = 1.0 / (1 - beta1 ** state['step'])buffered[2] = step_sizeif group['weight_decay'] != 0:p_data_fp32.add_(-group['weight_decay'] * group['lr'], p_data_fp32)if N_sma > self.N_sma_threshhold:denom = exp_avg_sq.sqrt().add_(group['eps'])p_data_fp32.addcdiv_(-step_size * group['lr'], exp_avg, denom)else:p_data_fp32.add_(-step_size * group['lr'], exp_avg)p.data.copy_(p_data_fp32)#integrated look ahead...#we do it at the param level instead of group levelif state['step'] % group['k'] == 0:slow_p = state['slow_buffer'] #get access to slow param tensorslow_p.add_(self.alpha, p.data - slow_p) #(fast weights - slow weights) * alphap.data.copy_(slow_p) #copy interpolated weights to RAdam param tensorreturn loss