import torch
import torch.nn as nn
import torch.optim as optim
import pandas as pd
# 假设输入的矩阵数据为邻接矩阵 A 和特征矩阵 X
# 在这个示例中,我们用随机生成的数据作为示例输入
data=pd.read_csv('datasets/graph.csv')
data=data.values
print(data.shape)
import numpy as np
from scipy.sparse import csr_matrix
# 假设有5个节点,节点对应关系如下(示例数据)
node_relations=[]
for line in data:
my_tuple = (int(line[0]),int(line[1]))
node_relations.append(my_tuple)
# 计算节点的个数
num_nodes = max(max(edge) for edge in node_relations) + 1
# 构建初始邻接矩阵
adj_matrix = np.zeros((num_nodes, num_nodes))
# 填充邻接矩阵
for edge in node_relations:
adj_matrix[edge[0], edge[1]] = 1
adj_matrix[edge[1], edge[0]] = 1 # 如果是无向图,需对称填充
# 将邻接矩阵转换为稀疏矩阵(这里使用 CSR 稀疏格式)
sparse_adj_matrix = csr_matrix(adj_matrix)
print("邻接矩阵:")
print(adj_matrix.shape)
# print("\n稀疏矩阵表示:")
# print(sparse_adj_matrix.shape)
A = torch.Tensor(adj_matrix)# torch.rand((num_nodes, num_nodes)) # 邻接矩阵
print(A.shape)
X = torch.rand((num_nodes, 64)) # 特征矩阵,假设每个节点有10维特征
print(X.shape)
# 定义图卷积层
class GraphConvLayer(nn.Module):
def __init__(self, in_features, out_features):
super(GraphConvLayer, self).__init__()
self.linear = nn.Linear(in_features, out_features)
def forward(self, A, X):
AX = torch.matmul(A, X) # 对特征矩阵和邻接矩阵进行乘积操作
return self.linear(AX) # 返回线性层的输出
# 定义简单的GCN模型
class SimpleGCN(nn.Module):
def __init__(self, in_features, hidden_features, out_features):
super(SimpleGCN, self).__init__()
self.conv1 = GraphConvLayer(in_features, hidden_features)
self.conv2 = GraphConvLayer(hidden_features, out_features)
def forward(self, A, X):
h = torch.relu(self.conv1(A, X)) # 第一个图卷积层
out = self.conv2(A, h) # 第二个图卷积层
return out
# 初始化GCN模型
gcn_model = SimpleGCN(in_features=64, hidden_features=128, out_features=64) # 输入特征为10维,隐藏层特征为16维,输出为8维
# 损失函数和优化器
criterion = nn.MSELoss() # 均方误差损失函数
optimizer = optim.Adam(gcn_model.parameters(), lr=0.01) # Adam优化器
# 训练模型
num_epochs = 1000
for epoch in range(num_epochs):
optimizer.zero_grad()
output = gcn_model(A, X)
loss = criterion(output, torch.zeros_like(output)) # 示范用零向量作为目标值,实际情况需要根据具体任务调整
loss.backward()
optimizer.step()
if (epoch + 1) % 100 == 0:
print(f'Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item()}')
# 得到节点的向量化表示
node_embeddings = gcn_model(A, X)
print("节点的向量化表示:")
print(node_embeddings.shape)
GCN 对矩阵数据的训练和向量化表示
最新推荐文章于 2025-05-07 13:31:33 发布