Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

写一个Grpo的训练代码

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-flash-preview-04-17’s answer.
  1. User

    写一个Grpo的训练代码

  2. Assistant

    好的!下面给你一个基于PyTorch实现的GraphSAGE (Graph Sample and AggregATE) 的训练示例代码。GraphSAGE是一种常用的图神经网络(GNN)模型,用于节点分类任务。

    这里用的是PyTorch和PyTorch Geometric(PyG)库,PyG是处理图数据非常方便的库。


    1. 安装依赖

    如果你还没安装PyTorch Geometric,可以用下面命令安装(请根据你的CUDA版本选择对应版本):

    pip install torch torchvision
    pip install torch-geometric
    

    详细安装请参考官方文档:https://pytorch-geometric.readthedocs.io/en/latest/notes/installation.html


    2. GraphSAGE训练示例代码

    import torch
    import torch.nn.functional as F
    from torch_geometric.nn import SAGEConv
    from torch_geometric.datasets import Planetoid
    from torch_geometric.loader import NeighborSampler
    
    # 载入Cora数据集(节点分类任务)
    dataset = Planetoid(root='/tmp/Cora', name='Cora')
    data = dataset[0]
    
    # 定义GraphSAGE模型
    class GraphSAGE(torch.nn.Module):
        def __init__(self, in_channels, hidden_channels, out_channels):
            super(GraphSAGE, self).__init__()
            self.conv1 = SAGEConv(in_channels, hidden_channels)
            self.conv2 = SAGEConv(hidden_channels, out_channels)
    
        def forward(self, x, edge_index):
            x = self.conv1(x, edge_index)
            x = F.relu(x)
            x = F.dropout(x, p=0.5, training=self.training)
            x = self.conv2(x, edge_index)
            return x
    
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model = GraphSAGE(dataset.num_node_features, 64, dataset.num_classes).to(device)
    data = data.to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
    
    def train():
        model.train()
        optimizer.zero_grad()
        out = model(data.x, data.edge_index)
        loss = F.cross_entropy(out[data.train_mask], data.y[data.train_mask])
        loss.backward()
        optimizer.step()
        return loss.item()
    
    @torch.no_grad()
    def test():
        model.eval()
        out = model(data.x, data.edge_index)
        pred = out.argmax(dim=1)
    
        accs = []
        for mask in [data.train_mask, data.val_mask, data.test_mask]:
            correct = pred[mask].eq(data.y[mask]).sum().item()
            acc = correct / mask.sum().item()
            accs.append(acc)
        return accs
    
    for epoch in range(1, 201):
        loss = train()
        train_acc, val_acc, test_acc = test()
        if epoch % 20 == 0:
            print(f'Epoch: {epoch:03d}, Loss: {loss:.4f}, Train Acc: {train_acc:.4f}, Val Acc: {val_acc:.4f}, Test Acc: {test_acc:.4f}')
    

    代码说明:

    • 使用了Cora图数据集,节点分类任务。
    • GraphSAGE模型包含两层SAGEConv卷积层。
    • 训练时只计算训练集上的损失,测试时计算训练/验证/测试集准确率。
    • 训练200轮,每20轮打印一次结果。

    如果你想用自己的图数据,或者想用更复杂的采样策略(比如NeighborSampler),我也可以帮你写示例。你需要告诉我具体需求即可。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.