图神经网络改进-手把手教你改代码-第2期:双通道图卷积

  本系列项目主攻:代码分享与讲解创新思路解析前沿模块缝合二次创新实现方法
项目主要提供关于:

  • 图神经网络
  • 图对比学习
  • 图结构学习
  • 超图神经网络
  • 超图对比学习
  • 超图结构学习

  这六种方向的通用模型、原创代码以及改进思路,供大家参考学习,后续还会持续更新针对链路预测、节点分类等下游任务上的代码以及改进思路,帮助大家提升代码水平,多发论文。


 希望可以帮助大家快速上手实践图神经网络,实践是最好的入门方式!

  祝大家论文顺利,accept冲冲冲!

第二期:双通道图卷积


详细讲解视频:【图神经网络改进-手把手教你改代码-第2期】

项目Github:图小狮


以下为具体的代码部分:

1.超参数设计,其具体作用设置经验讲解详见:讲解视频

# 命令行参数
def parse_arguments():
    parser = argparse.ArgumentParser(description='Dual-Channels Graph Neural Network')
    parser.add_argument('--dataset', choices=['Cora', 'Citeseer', 'Pubmed'], default='Cora',
                        help="Dataset selection")
    parser.add_argument('--hidden_dim', type=int, default=32, help='Hidden layer dimension')
    parser.add_argument('--dropout_rate', type=float, default=0.5, help='Dropout rate')
    parser.add_argument('--lr', default=0.01, help="Learning Rate selection")
    parser.add_argument('--wd', default=5e-4, help="weight_decay selection")
    parser.add_argument('--epochs', default=200, help="train epochs selection")
    parser.add_argument('--model', choices=['GCN', 'GAT', 'SAGE', 'ChebNet', 'TransformerConv', 'Others'],
                        default='GCN', help="Model selection")
    parser.add_argument('--FusionMethod', choices=['Sum', 'Dot', 'Concat', 'Average', 'Max', 'Attention'],
                        default='Attention', help="Select Fusion Method")
    parser.add_argument('--tsne_drawing', choices=[True, False], default=False,
                        help="Whether to use tsne drawing")
    parser.add_argument('--tsne_colors', default=['#ffc0cb', '#bada55', '#008080', '#420420', '#7fe5f0', '#065535', '#ffd700'], help="colors")
    return parser.parse_args()

2.随机种子,其功能作用详见:讲解视频

def setup_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
    np.random.seed(seed)
    random.seed(seed)

setup_seed(42)

3.模型定义,相较于上一篇“图神经快速实践”文章,为实现双通道功能,对其forward部分进行了修改。

class GNN(torch.nn.Module):
    def __init__(self, in_channels, hidden_channels, out_channels, model_type, dropout_rate):
        super(GNN, self).__init__()
        self.dropout_rate = dropout_rate
        self.lin = torch.nn.Linear(out_channels *2, out_channels)

        if model_type == 'GCN':
            self.conv1 = GCNConv(in_channels, hidden_channels)
            self.conv2 = GCNConv(hidden_channels, out_channels)
        elif model_type == 'GAT':
            self.conv1 = GATConv(in_channels, hidden_channels)
            self.conv2 = GATConv(hidden_channels, out_channels)
        elif model_type == 'SAGE':
            self.conv1 = SAGEConv(in_channels, hidden_channels)
            self.conv2 = SAGEConv(hidden_channels, out_channels)
        elif model_type == 'ChebNet':
            self.conv1 = ChebConv(in_channels, hidden_channels, K=2)
            self.conv2 = ChebConv(hidden_channels, out_channels, K=2)
        elif model_type == 'TransformerConv':
            self.conv1 = TransformerConv(in_channels, hidden_channels)
            self.conv2 = TransformerConv(hidden_channels, out_channels)
        else:
            raise NotImplementedError


    def forward(self, x, edge_index):
        # first channel
        x1 = self.conv1(x, edge_index).relu()
        x1 = F.dropout(x1, p=self.dropout_rate, training=self.training)
        x1 = self.conv2(x1, edge_index)
        # second channel
        x2 = self.conv1(x, edge_index).relu()
        x2 = F.dropout(x2, p=self.dropout_rate, training=self.training)
        x2 = self.conv2(x2, edge_index)
        # Apply fusion method
        if args.FusionMethod == 'Sum':
            x = x1 + x2
        elif args.FusionMethod == 'Dot':
            x = x1 * x2
        elif args.FusionMethod == 'Concat':
            x = torch.cat([x1, x2], dim=1)
            x = self.lin(x)
        elif args.FusionMethod == 'Average':
            x = (x1 + x2) / 2
        elif args.FusionMethod == 'Max':
            x = torch.max(x1, x2)
        elif args.FusionMethod == 'Attention':
            attention_weights = F.softmax(torch.randn(x1.size(0), 1), dim=0).cuda()
            x = attention_weights * x1 + (1 - attention_weights) * x2
        else:
            raise NotImplementedError
        return F.log_softmax(x)

其余的实现部分与“图神经快速实践”基本一致


Powered By 图小狮

希望能够得到大家的喜欢,您的点赞收藏即是对我们最大的支持!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值