如何解决如何通过 Networkx 图传播神经网络梯度?
我正在尝试训练我的神经网络,使图中每条边的权重都为 10
。
我从生成随机点 (inp
) 开始,并使每 2 个相邻点(使用 idx
)具有 weight = 1
的边缘。然后如果 2 个相邻的点已经有一条边,边的权重就会被发送到神经网络,神经网络输出要添加的额外权重。
import warnings
warnings.filterwarnings("ignore",category=UserWarning)
import torch
import torch.nn as nn
import networkx as nx
import matplotlib.pyplot as plt
import torch.optim as optim
class Net(nn.Module):
def __init__(self):
super(Net,self).__init__()
self.fc1 = nn.Linear(1,3)
self.fc2 = nn.Linear(3,1)
def forward(self,x):
x = self.fc1(x)
x = self.fc2(x)
return x
g = nx.DiGraph()
model = Net()
optimizer = optim.Adam(model.parameters(),lr = 1e-3)
def training(n_iter):
for epoch in range(n_iter):
print(epoch)
inp = torch.randint(0,10,(20,))
idx = 0
while idx < len (inp) - 1:
if g.has_edge(inp[idx].item(),inp[idx+1].item()): #edge exist
edge_weight = g[inp[idx].item()][inp[idx+1].item()]["weight"]
edge_weight_tensor = torch.tensor([edge_weight]).float() #to tensor
added_edge_weight = model(edge_weight_tensor) #value from network
g[inp[idx].item()][inp[idx+1].item()]["weight"] += added_edge_weight
idx +=1
else:
g.add_edge(inp[idx].item(),inp[idx+1].item(),weight = 1)
idx +=1
edges = g.edges()
weights = [g[u][v]['weight'] for u,v in edges]
optimizer.zero_grad()
loss_list = [w for w in weights if not isinstance(w,int)] #only take tensors
try:
loss_tensors = (torch.stack(loss_list,dim=0)-10)
loss_square = torch.square(loss_tensors)
loss = torch.sum(loss_square)
print(loss)
except RuntimeError: #no tensors - hence create a 0 loss
loss = torch.tensor(0.0,requires_grad = True)
loss.backward(retain_graph=True)
optimizer.step()
return weights
weights = training(5)
#plot
plt.figure(figsize=(6,6))
pos = nx.spring_layout(g,k = 0.5)
nx.draw(g,with_labels=True,node_color='skyblue',font_weight='bold',width=weights,pos=pos)
我的问题是,我不确定梯度是否可以通过这种方式传播,而且我似乎无法将 NN 权重添加到边缘的权重中——出现以下错误:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [3,1]],which is output 0 of TBackward,is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that Failed to compute its gradient,with torch.autograd.set_detect_anomaly(True).
解决方法
PyTorch Geometric 专门设计用于在图形对象上实现 PyTorch 方法。具体来说,它的 https://github.com/grails/grails-core/issues/11221 函数允许从 networkx 图转换为可以传播梯度的 PyTorch Geometric 对象。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。