edge_index:这里可以理解为图的邻接矩阵,但是要注意这里要将邻接矩阵转换成COO格式,shape = [2, 边的数量],type = torch.long。 edge_attr:边的特征矩阵,shape = [边的个数,边的特征数] y:标签,如果任务是图分类,shape = [1, 图的标签数];如果是节点分类,shape = [节点个数,节点的标签数]。(这里注...
Compared to classification, you only need to change the output layer activation function, the loss function, evaluation metric, and obviously the target. Finally, we can predict onedge level. The value of an edge is predicted, or the likelihood of an edge that will appear soon. An example i...
edge_index = torch.LongTensor(np.array([edge for edge in graph.edges()]).T) X = torch.FloatTensor(np.random.uniform(-1, 1, (number_of_nodes, in_channels))) return X, edge_index def create_mock_edge_weight(edge_index): """ Creating a mock edge weight tensor. """ return torch....
optimizer.zerograd() # 清零梯度out = model(data.x, data.edge_index) # 前向传播loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask]) # 计算损失函数值loss.backward() # 反向传播计算梯度值optimizer.step() # 更新权重参数值model.eval() # 设置模型为评估模式 , pred = model(...
x是节点特征,一个具有3个节点、每个节点2个特征的张量;edge_index表示节点之间的连接关系。 3. 定义GCN模型 以下是定义一个简单的GCN模型的代码: importtorch.nnasnnimporttorch.nn.functionalasFfromtorch_geometric.nnimportGCNConvclassGCN(nn.Module):def__init__(self,in_channels,out_channels):super(GCN,se...
可以发现,GCNConv中需要输入的是节点特征矩阵x和邻接关系edge_index,还有一个可选项edge_weight。因此我们首先: 代码语言:javascript 代码运行次数:0 复制 Cloud Studio代码运行 x,edge_index=data.x,data.edge_index x=self.conv1(x,edge_index)x=F.relu(x)x=F.dropout(x,training=self.training) ...
data.edge_index:代表图的连接性,形状是[2, num_edges] data.edge_attr: 边的特征的shape [num_edges, num_edge_features] data.y: 对应的标签,可以是图级别的[1, *],也可以是节点类别的[num_nodes, *] data.pos: 带形状的节点位置矩阵[num_nodes,num_dimensions] [num_nodes,num_dimensions] ...
(edge_index[:,0], edge_index[:,1])), shape=(num_nodes, num_nodes), dtype="float32")returnadjacency@staticmethoddefread_data(path):"""使用不同的方式读取原始数据以进一步处理"""name = osp.basename(path)ifname =="ind.cora.test.index": ...
output = model(x, edge_index) # 打印输出结果 print(output) “` 这是一个简单的GCN模型的实现例子,你可以根据自己的需求进行修改和扩展。值得注意的是,`torch_geometric`包还提供了许多其他的图神经网络模型和功能,你可以根据自己的需要进行学习和使用。
(self, data): # 前向传播x, edge_index, batch = data.x, data.edge_index, data.batch # 赋值# print(batch)# print(x)x = self.conv1(x, edge_index) # 第一层启动运算,输入为节点及特征和边的稀疏矩阵,输出结果是二维度[20张图的所有节点数,16]# print(x.shape)x = F.relu(x) # ...