Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-sonnet-4-20250514· AnthropicHugging Face LMArena Human Preference 140K

基于1dcnn实现一个ResNext用于对(B,1,28)的数据进行二分类,使用pytorch实现

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with chatgpt-4o-latest-20250326’s answer.
  1. User

    基于1dcnn实现一个ResNext用于对(B,1,28)的数据进行二分类,使用pytorch实现

  2. Claude Assistant

    我来帮你实现一个基于1D CNN的ResNeXt模型,用于对形状为(B,1,28)的数据进行二分类。

    import torch
    import torch.nn as nn
    import torch.nn.functional as F
    
    class ResNeXtBlock1D(nn.Module):
        """1D ResNeXt Block with grouped convolutions"""
        def __init__(self, in_channels, out_channels, cardinality=32, bottleneck_width=4, stride=1):
            super(ResNeXtBlock1D, self).__init__()
            
            # 计算中间层的通道数
            group_width = cardinality * bottleneck_width
            
            # 1x1 conv降维
            self.conv1 = nn.Conv1d(in_channels, group_width, kernel_size=1, bias=False)
            self.bn1 = nn.BatchNorm1d(group_width)
            
            # 3x1 grouped conv
            self.conv2 = nn.Conv1d(group_width, group_width, kernel_size=3, 
                                  stride=stride, padding=1, groups=cardinality, bias=False)
            self.bn2 = nn.BatchNorm1d(group_width)
            
            # 1x1 conv升维
            self.conv3 = nn.Conv1d(group_width, out_channels, kernel_size=1, bias=False)
            self.bn3 = nn.BatchNorm1d(out_channels)
            
            # shortcut连接
            self.shortcut = nn.Sequential()
            if stride != 1 or in_channels != out_channels:
                self.shortcut = nn.Sequential(
                    nn.Conv1d(in_channels, out_channels, kernel_size=1, stride=stride, bias=False),
                    nn.BatchNorm1d(out_channels)
                )
        
        def forward(self, x):
            out = F.relu(self.bn1(self.conv1(x)))
            out = F.relu(self.bn2(self.conv2(out)))
            out = self.bn3(self.conv3(out))
            out += self.shortcut(x)
            out = F.relu(out)
            return out
    
    class ResNeXt1D(nn.Module):
        """1D ResNeXt for binary classification"""
        def __init__(self, input_length=28, num_classes=2, cardinality=8, bottleneck_width=4):
            super(ResNeXt1D, self).__init__()
            
            # 初始卷积层
            self.conv1 = nn.Conv1d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
            self.bn1 = nn.BatchNorm1d(64)
            self.maxpool = nn.MaxPool1d(kernel_size=3, stride=2, padding=1)
            
            # ResNeXt blocks
            self.layer1 = self._make_layer(64, 64, 2, cardinality, bottleneck_width, stride=1)
            self.layer2 = self._make_layer(64, 128, 2, cardinality, bottleneck_width, stride=2)
            self.layer3 = self._make_layer(128, 256, 2, cardinality, bottleneck_width, stride=2)
            
            # 全局平均池化和分类器
            self.avgpool = nn.AdaptiveAvgPool1d(1)
            self.fc = nn.Linear(256, num_classes)
            
            # 权重初始化
            self._initialize_weights()
        
        def _make_layer(self, in_channels, out_channels, num_blocks, cardinality, bottleneck_width, stride):
            layers = []
            # 第一个block可能需要下采样
            layers.append(ResNeXtBlock1D(in_channels, out_channels, cardinality, bottleneck_width, stride))
            # 后续blocks
            for _ in range(1, num_blocks):
                layers.append(ResNeXtBlock1D(out_channels, out_channels, cardinality, bottleneck_width, 1))
            return nn.Sequential(*layers)
        
        def _initialize_weights(self):
            for m in self.modules():
                if isinstance(m, nn.Conv1d):
                    nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
                elif isinstance(m, nn.BatchNorm1d):
                    nn.init.constant_(m.weight, 1)
                    nn.init.constant_(m.bias, 0)
                elif isinstance(m, nn.Linear):
                    nn.init.normal_(m.weight, 0, 0.01)
                    nn.init.constant_(m.bias, 0)
        
        def forward(self, x):
            # 输入: (B, 1, 28)
            x = F.relu(self.bn1(self.conv1(x)))  # (B, 64, 14)
            x = self.maxpool(x)                   # (B, 64, 7)
            
            x = self.layer1(x)                    # (B, 64, 7)
            x = self.layer2(x)                    # (B, 128, 4)
            x = self.layer3(x)                    # (B, 256, 2)
            
            x = self.avgpool(x)                   # (B, 256, 1)
            x = torch.flatten(x, 1)               # (B, 256)
            x = self.fc(x)                        # (B, 2)
            
            return x
    
    # 创建模型实例
    def create_resnext1d(cardinality=8, bottleneck_width=4):
        """创建ResNeXt1D模型"""
        return ResNeXt1D(input_length=28, num_classes=2, 
                         cardinality=cardinality, bottleneck_width=bottleneck_width)
    
    # 使用示例
    if __name__ == "__main__":
        # 创建模型
        model = create_resnext1d(cardinality=8, bottleneck_width=4)
        
        # 测试输入
        batch_size = 32
        input_tensor = torch.randn(batch_size, 1, 28)
        
        # 前向传播
        output = model(input_tensor)
        print(f"Input shape: {input_tensor.shape}")
        print(f"Output shape: {output.shape}")
        print(f"Model parameters: {sum(p.numel() for p in model.parameters()):,}")
        
        # 训练示例
        criterion = nn.CrossEntropyLoss()
        optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
        
        # 模拟训练数据
        labels = torch.randint(0, 2, (batch_size,))
        
        # 前向传播
        outputs = model(input_tensor)
        loss = criterion(outputs, labels)
        
        # 反向传播
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        print(f"Loss: {loss.item():.4f}")
        
        # 预测
        with torch.no_grad():
            predictions = torch.softmax(outputs, dim=1)
            predicted_classes = torch.argmax(predictions, dim=1)
            print(f"Predicted classes: {predicted_classes[:10]}")
    

    这个实现的主要特点:

    ResNeXt核心特性:

    • 使用分组卷积(grouped convolution)实现cardinality概念
    • 采用bottleneck设计减少参数量
    • 保持ResNet的残差连接结构

    网络架构:

    • 初始7x1卷积层进行特征提取
    • 三个ResNeXt层,逐步增加通道数并减少序列长度
    • 全局平均池化避免过拟合
    • 最终全连接层输出二分类结果

    可调参数:

    • cardinality: 分组数量,控制网络宽度
    • bottleneck_width: 瓶颈层宽度,影响计算复杂度

    适用场景: 这个模型特别适合处理一维序列数据的二分类任务,比如时间序列分类、信号分类等。你可以根据具体需求调整cardinality和bottleneck_width参数来平衡模型复杂度和性能。

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