深度学习笔试选择题精解:代码+公式
·
深度学习笔试选择题解析:题组2
深度学习笔试中的选择题通常涵盖模型架构、优化算法、正则化技术等核心知识点。以下针对典型题目进行技术解析,附代码示例说明。
卷积神经网络(CNN)层数设计
选择题常考察CNN层数对模型性能的影响。深层网络能提取更抽象特征,但可能引发梯度消失问题。ResNet通过残差连接解决该问题。
import torch
import torch.nn as nn
class BasicBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super().__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
self.shortcut = nn.Sequential()
if stride != 1 or in_channels != out_channels:
self.shortcut = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride),
nn.BatchNorm2d(out_channels)
)
def forward(self, x):
out = torch.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out += self.shortcut(x)
return torch.relu(out)
损失函数选择
分类问题常用交叉熵损失,其数学形式为:
$$ \mathcal{L} = -\sum_{i=1}^N y_i \log(p_i) $$
其中$y_i$为真实标签,$p_i$为预测概率。代码实现需注意数值稳定性:
def cross_entropy(y_true, y_pred):
epsilon = 1e-15
y_pred = np.clip(y_pred, epsilon, 1 - epsilon)
return -np.sum(y_true * np.log(y_pred))
批量归一化(BatchNorm)作用
Batch
更多推荐



所有评论(0)