def forward(self, x, edge_index): x = F.relu(self.conv(x, edge_index)) pos_edge_index = edge_index total_edge_index = torch.cat([pos_edge_index, negative_sampling(edge_index, num_neg_samples=pos_edge_index.size(1))], dim=1 edge_features = torch.cat([x[total_edge_index[0]]...
(self, x , edge_index, batch_index): # 1st Conv layer hidden = self.initial_conv(x, edge_index) hidden = torch.tanh(hidden) # Other layers hidden = self.conv1(hidden, edge_index) hidden = torch.tanh(hidden) hidden = self.conv2(hidden, edge_index) hidden = torch.tanh(hidden) ...
dataset=Planetoid(root='/tmp/Cora',name='Cora')classGCN(nn.Module):def__init__(self):super(GCN,self).__init__()self.conv1=GCNConv(dataset.num_node_features,16)self.conv2=GCNConv(16,dataset.num_classes)defforward(self,data):x,edge_index=data.x,data.edge_index x=self.conv1(x,edg...
x (Tensor, optional): 节点属性矩阵,大小为`[num_nodes, num_node_features]` edge_index (LongTensor, optional): 边索引矩阵,大小为`[2, num_edges]`,第0行为尾节点,第1行为头节点,头指向尾 edge_attr (Tensor, optional): 边属性矩阵,大小为`[num_edges, num_edge_features]` y (Tensor, optional...
nn.init.normal_(self.bias, mean=0.0, std=0.1) def forward(self, x, edge_index): ...
edge_attr=torch.zeros((n_node**2,1)) num_edges=0 foriinrange(n_node): forjinrange(n_node): ifself.W[i,j]!=0: edge_index[:,num_edges]=torch.tensor([i,j],dtype=torch.long) edge_attr[num_edges,0]=self.W[i,j] num_edges+=1 ...
self.clf_head=nn.Sequential(nn.Linear(hidden_dim,hidden_dim),nn.Dropout(dropout_pct),nn.Linear(hidden_dim,output_dim))defforward(self,data):x,edge_index,batch=data.x,data.edge_index,data.batchforiinrange(self.num_graph_layers):x=self.convs[i](x,edge_index)x=F.relu(x)x=F.dropout...
MessagePassing.message(...)方法可以接收传递给MessagePassing.propagate(edge_index, size=None, **kwargs)方法的所有参数,我们在message()方法的参数列表里定义要接收的参数,例如我们要接收x,y,z参数,则我们应定义message(x,...
def_create_edges(self, n_node):edge_index=torch.zeros((2, n_node**2),dtype=torch.long)edge_attr=torch.zeros((n_node**2, 1))num_edges=0 foriinrange(n_node): forjinrange(n_node): ifself.W[i, j] !=0: edge_index[:, num_edges] =torch.tensor([i, j],dtype=torch.long) ...
def forward(self, x, edge_index): x = F.relu(self.conv1(x, edge_index)) x = F.dropout(x, training=self.training) x = self.conv2(x, edge_index) return F.log_softmax(x, dim=1) model = Net().to(device) def accuracy(pred_y, y): ...