深度学习模型:Pytorch搭建ResNet、DenseNet网络,完成一维数据分类任务
发布人:shili8
发布时间:2025-01-08 06:28
阅读次数:0
**深度学习模型:Pytorch搭建ResNet、DenseNet网络,完成一维数据分类任务**
在本文中,我们将使用Pytorch来搭建两个经典的深度学习模型:ResNet(残差网络)和DenseNet(稠密连接网络)。这些模型都被广泛应用于图像分类等任务,但也可以用于其他类型的数据,如一维数据。我们将在本文中使用Pytorch来搭建这两个模型,并完成一个简单的一维数据分类任务。
**ResNet**
ResNet是由Kaiming He等人提出的,主要解决了深度网络训练难以收敛的问题。它通过引入残差连接(shortcut connection)来实现,这样可以让网络更容易地学习到特征之间的关系。
### ResNet的结构ResNet的基本结构如下:
* **Convolutional Block**:每个块包含一个卷积层、一个批量归一化层和一个激活函数(ReLU)。
* **Shortcut Connection**:用于连接两个相邻的卷积块,实现特征之间的跳跃连接。
* **Pooling Layer**:用于降低空间维度。
### Pytorch代码示例
import torchimport torch.nn as nnclass ResNet(nn.Module): def __init__(self): super(ResNet, self).__init__() # Convolutional Block1 self.conv_block1 = nn.Sequential( nn.Conv1d(in_channels=1, out_channels=64, kernel_size=3), nn.BatchNorm1d(num_features=64), nn.ReLU(), nn.MaxPool1d(kernel_size=2) ) # Convolutional Block2 self.conv_block2 = nn.Sequential( nn.Conv1d(in_channels=64, out_channels=128, kernel_size=3), nn.BatchNorm1d(num_features=128), nn.ReLU(), nn.MaxPool1d(kernel_size=2) ) # Convolutional Block3 self.conv_block3 = nn.Sequential( nn.Conv1d(in_channels=128, out_channels=256, kernel_size=3), nn.BatchNorm1d(num_features=256), nn.ReLU(), nn.MaxPool1d(kernel_size=2) ) # Fully Connected Layer self.fc = nn.Linear(in_features=256, out_features=10) def forward(self, x): x = self.conv_block1(x) x = self.conv_block2(x) x = self.conv_block3(x) x = torch.flatten(x,1) # flatten the output x = self.fc(x) return x# Initialize the modelmodel = ResNet() # Print the model's parametersprint(model.parameters())
**DenseNet**
DenseNet是由Gao Huang等人提出的,主要解决了深度网络训练难以收敛的问题。它通过引入稠密连接(dense connection)来实现,这样可以让网络更容易地学习到特征之间的关系。
### DenseNet的结构DenseNet的基本结构如下:
* **Convolutional Block**:每个块包含一个卷积层、一个批量归一化层和一个激活函数(ReLU)。
* **Dense Connection**:用于连接两个相邻的卷积块,实现特征之间的跳跃连接。
* **Pooling Layer**:用于降低空间维度。
### Pytorch代码示例
import torchimport torch.nn as nnclass DenseNet(nn.Module): def __init__(self): super(DenseNet, self).__init__() # Convolutional Block1 self.conv_block1 = nn.Sequential( nn.Conv1d(in_channels=1, out_channels=64, kernel_size=3), nn.BatchNorm1d(num_features=64), nn.ReLU(), nn.MaxPool1d(kernel_size=2) ) # Convolutional Block2 self.conv_block2 = nn.Sequential( nn.Conv1d(in_channels=64, out_channels=128, kernel_size=3), nn.BatchNorm1d(num_features=128), nn.ReLU(), nn.MaxPool1d(kernel_size=2) ) # Convolutional Block3 self.conv_block3 = nn.Sequential( nn.Conv1d(in_channels=128, out_channels=256, kernel_size=3), nn.BatchNorm1d(num_features=256), nn.ReLU(), nn.MaxPool1d(kernel_size=2) ) # Fully Connected Layer self.fc = nn.Linear(in_features=256, out_features=10) def forward(self, x): x = self.conv_block1(x) x = self.conv_block2(x) x = self.conv_block3(x) x = torch.flatten(x,1) # flatten the output x = self.fc(x) return x# Initialize the modelmodel = DenseNet() # Print the model's parametersprint(model.parameters())
**一维数据分类任务**
在本文中,我们将使用Pytorch来搭建ResNet和DenseNet模型,并完成一个简单的一维数据分类任务。我们将使用MNIST手写数字数据集作为我们的数据源。
### Pytorch代码示例
import torchimport torch.nn as nnfrom torchvision import datasets, transforms# Define the data transformationtransform = transforms.Compose([transforms.ToTensor()]) # Load the MNIST datasettrain_dataset = datasets.MNIST(root='./data', train=True, download=True, transform=transform) test_dataset = datasets.MNIST(root='./data', train=False, download=True, transform=transform) # Create the data loaderbatch_size =64train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False) # Initialize the model and optimizermodel = ResNet() optimizer = torch.optim.Adam(model.parameters(), lr=0.001) # Train the modelnum_epochs =10for epoch in range(num_epochs): for i, (images, labels) in enumerate(train_loader): # Forward pass outputs = model(images) # Calculate the loss loss = nn.CrossEntropyLoss()(outputs, labels) # Backward pass and optimize optimizer.zero_grad() loss.backward() optimizer.step() # Print the epoch number and loss value print(f'Epoch {epoch+1}, Loss: {loss.item()}') # Evaluate the model on the test setmodel.eval() test_loss =0correct =0with torch.no_grad(): for images, labels in test_loader: outputs = model(images) loss = nn.CrossEntropyLoss()(outputs, labels) test_loss += loss.item() _, predicted = torch.max(outputs,1) correct += (predicted == labels).sum().item() # Print the test accuracyaccuracy = correct / len(test_dataset) print(f'Test Accuracy: {accuracy:.2f}')
在本文中,我们使用Pytorch来搭建ResNet和DenseNet模型,并完成一个简单的一维数据分类任务。我们使用MNIST手写数字数据集作为我们的数据源,训练模型并评估其准确率。