Sains Data ITERA · Ganjil 2026/2025
Mahasiswa mampu menjelaskan degradation problem, menurunkan mengapa residual connection memperlancar aliran gradien, serta menghitung secara manual forward, Jacobian, dan jumlah parameter blok residual.
Intuisi: jaringan dalam setidaknya bisa meniru jaringan dangkal (lapisan tambahan cukup menjadi identitas). Kenyataannya (He dkk., 2016a):
| Kedalaman | Plain | ResNet |
|---|---|---|
| 18 lapis | 27,94% | 27,88% |
| 34 lapis | 28,54% (lebih buruk) | 25,03% (lebih baik) |
Karena training error ikut naik, ini bukan overfitting → disebut degradation problem: solver (SGD) kesulitan mengoptimasi jaringan plain yang sangat dalam, bahkan sekadar mempelajari pemetaan identitas.
Alih-alih mempelajari pemetaan yang diinginkan $H(\mathbf{x})$ secara langsung, pelajari residu-nya: \(F(\mathbf{x}) := H(\mathbf{x}) - \mathbf{x} \qquad\Rightarrow\qquad \mathbf{y} = F(\mathbf{x}, \{W_i\}) + \mathbf{x}\)

Blok sederhana (lapisan linear, tanpa bias): $F(\mathbf{x}) = W_2\,\text{ReLU}(W_1\mathbf{x})$, $\;\mathbf{y} = \mathbf{x} + F(\mathbf{x})$ \(\mathbf{x} = \begin{bmatrix}1\\2\end{bmatrix}, \quad W_1 = \begin{bmatrix}1 & -1\\ 0{,}5 & 0{,}5\end{bmatrix}, \quad W_2 = \begin{bmatrix}0{,}5 & 0\\ 0 & -1\end{bmatrix}\)
| Langkah | Perhitungan | Hasil |
|---|---|---|
| $\mathbf{z} = W_1\mathbf{x}$ | $[1 - 2;\; 0{,}5 + 1]$ | $[-1;\; 1{,}5]$ |
| $\mathbf{h} = \text{ReLU}(\mathbf{z})$ | $[\max(0,-1);\; \max(0, 1{,}5)]$ | $[0;\; 1{,}5]$ |
| $F = W_2\mathbf{h}$ | $[0{,}5 \cdot 0;\; -1 \cdot 1{,}5]$ | $[0;\; -1{,}5]$ |
| $\mathbf{y} = \mathbf{x} + F$ | $[1 + 0;\; 2 - 1{,}5]$ | $[1;\; 0{,}5]$ |
Syarat: $F(\mathbf{x})$ dan $\mathbf{x}$ harus berdimensi sama agar dapat dijumlahkan.
Backward dengan gradien dari atas $\mathbf{g} = \partial\mathcal{L}/\partial\mathbf{y} = [1;\, 1]$:
| Rumus | Hasil | |
|---|---|---|
| Dengan skip | $\left(I + W_2DW_1\right)^\top \mathbf{g}$ | $[0{,}5;\; 0{,}5]$ |
| Tanpa skip ($\mathbf{y} = F(\mathbf{x})$) | $\left(W_2DW_1\right)^\top \mathbf{g}$ | $[-0{,}5;\; -0{,}5]$ |
Suku $I$ memastikan gradien $\mathbf{g}$ selalu diteruskan langsung, meskipun cabang $F$ “mati” (mis. unit ReLU pertama, $z_1 = -1$, tidak meneruskan gradien).
Untuk blok tanpa aktivasi setelah penjumlahan, $\mathbf{x}_{l+1} = \mathbf{x}_l + F(\mathbf{x}_l)$. Diulang dari blok $l$ ke $L$: \(\mathbf{x}_L = \mathbf{x}_l + \sum_{i=l}^{L-1} F(\mathbf{x}_i)\)
Turunan loss terhadap lapisan dangkal $\mathbf{x}_l$: \(\frac{\partial \mathcal{L}}{\partial \mathbf{x}_l} = \frac{\partial \mathcal{L}}{\partial \mathbf{x}_L}\left(1 + \frac{\partial}{\partial \mathbf{x}_l}\sum_{i=l}^{L-1} F(\mathbf{x}_i)\right)\)
Model skalar: setiap blok cabang memiliki turunan $a$ (bobot kecil saat inisialisasi, mis. $a = 0{,}01$).
| Turunan per lapis | Gradien ke lapis 1 setelah $L$ lapis | $L = 10$ | $L = 50$ | |
|---|---|---|---|---|
| Plain: $x_{l+1} = a\,x_l$ | $a$ | $a^L$ | $10^{-20}$ | $10^{-100}$ |
| Residual: $x_{l+1} = x_l + a\,x_l$ | $1 + a$ | $(1 + a)^L$ | 1,105 | 1,645 |
Ekspansi produk residual (contoh 3 blok, $a_1 = 0{,}2$, $a_2 = -0{,}1$, $a_3 = 0{,}3$): \(\prod_{l}(1 + a_l) = \underbrace{1}_{\text{jalur langsung}} + \underbrace{\textstyle\sum a_l}_{0{,}4} + \underbrace{\textstyle\sum_{l<m} a_l a_m}_{0{,}01} + \underbrace{a_1a_2a_3}_{-0{,}006} = 1{,}404\)
$1{,}2 \times 0{,}9 \times 1{,}3 = 1{,}404$ ✓. Ada $2^3 = 8$ jalur dengan panjang berbeda → jaringan residual berperilaku seperti ansambel jaringan dangkal (Veit dkk., 2016).
Jika dimensi berubah (jumlah kanal naik atau resolusi turun dengan stride 2), $\mathbf{x}$ tidak bisa langsung dijumlahkan: \(\mathbf{y} = F(\mathbf{x}) + W_s\mathbf{x}\)
| Opsi (He dkk., 2016a) | Cara | Parameter tambahan |
|---|---|---|
| A | Identitas + zero-padding kanal tambahan, subsampling | 0 |
| B | Proyeksi (konvolusi 1×1, stride 2) hanya saat dimensi berubah | Sedikit |
| C | Semua shortcut berupa proyeksi | Lebih banyak; peningkatan marginal |
Contoh proyeksi 64 → 128 kanal: Conv2d(64, 128, kernel_size=1, stride=2, bias=False) → $128 \times 64 = 8.192$ parameter (+ BN).
Kesimpulan paper: proyeksi tidak esensial untuk mengatasi degradation; identitas dipakai di mana pun memungkinkan karena gratis.
Untuk ResNet-50/101/152, blok dua 3×3 diganti 1×1 → 3×3 → 1×1 (tanpa bias):
| Desain | Perhitungan | Parameter |
|---|---|---|
| Blok dasar, 64-d (dua 3×3, 64 → 64) | $2 \times 9 \times 64 \times 64$ | 73.728 |
| Blok bottleneck, 256-d | $256 \cdot 64 + 9 \cdot 64 \cdot 64 + 64 \cdot 256$ | 16.384 + 36.864 + 16.384 = 69.632 |
| Blok dasar, 256-d (dua 3×3, 256 → 256) | $2 \times 9 \times 256 \times 256$ | 1.179.648 |
Memindahkan BN dan ReLU sebelum konvolusi, dan tidak ada aktivasi setelah penjumlahan:
| Original (2016a) | Pre-activation (2016b) | |
|---|---|---|
| Urutan | conv → BN → ReLU → conv → BN → (+) → ReLU | BN → ReLU → conv → BN → ReLU → conv → (+) |
| Jalur shortcut | Melewati ReLU setelah penjumlahan | Identitas murni dari awal sampai akhir |
| Efek | ResNet-1202 di CIFAR-10 dapat dilatih, tetapi test error-nya lebih buruk dari ResNet-110 | ResNet-1001 di CIFAR lebih mudah dilatih dan generalisasinya lebih baik |
Inilah bentuk yang membuat penurunan $\mathbf{x}_L = \mathbf{x}_l + \sum F(\mathbf{x}_i)$ berlaku tepat.
| Arsitektur | Rumus | Catatan |
|---|---|---|
| Highway Network (Srivastava dkk., 2015) | $\mathbf{y} = H(\mathbf{x}) \odot T(\mathbf{x}) + \mathbf{x} \odot (1 - T(\mathbf{x}))$ | Gerbang $T$ dipelajari (mirip LSTM); pendahulu ResNet |
| ResNet (He dkk., 2016a) | $\mathbf{y} = F(\mathbf{x}) + \mathbf{x}$ | Gerbang selalu “terbuka”, tanpa parameter |
| DenseNet (Huang dkk., 2017) | $\mathbf{x}l = H_l([\mathbf{x}_0, \mathbf{x}_1, \ldots, \mathbf{x}{l-1}])$ | Konkatenasi, bukan penjumlahan |
| U-Net (Ronneberger dkk., 2015) | Konkatenasi fitur encoder ke decoder | Segmentasi; memulihkan detail spasial |
DenseNet — hitung kanal: dengan growth rate $k$ dan $k_0$ kanal awal, lapisan ke-$l$ menerima $k_0 + k(l - 1)$ kanal. Contoh $k_0 = 64$, $k = 32$, lapisan ke-4: $64 + 32 \times 3 = 160$ kanal.
| Penjumlahan (ResNet) | Konkatenasi (DenseNet) | |
|---|---|---|
| Dimensi | Tetap | Bertambah setiap lapis |
| Reuse fitur | Implisit | Eksplisit — semua fitur sebelumnya tersedia |
Setiap sub-lapisan (self-attention, feed-forward) dibungkus residual + LayerNorm:
| Varian | Rumus | Dipakai di |
|---|---|---|
| Post-LN (Vaswani dkk., 2017) | $\mathbf{x} \leftarrow \text{LN}\big(\mathbf{x} + \text{Sublayer}(\mathbf{x})\big)$ | Transformer asli, BERT |
| Pre-LN (Xiong dkk., 2020) | $\mathbf{x} \leftarrow \mathbf{x} + \text{Sublayer}\big(\text{LN}(\mathbf{x})\big)$ | GPT-2 dan sebagian besar LLM modern |
Tanpa residual connection, Transformer puluhan hingga ratusan lapis praktis tidak dapat dilatih.
| Sudut pandang | Penjelasan | Rujukan |
|---|---|---|
| Optimisasi | Mempelajari $F \approx 0$ lebih mudah daripada $H \approx I$ | He dkk. (2016a) |
| Aliran gradien | Suku “1” meneruskan gradien langsung | He dkk. (2016b) |
| Ansambel | $2^n$ jalur dengan panjang bervariasi; jalur pendek dominan saat pelatihan | Veit dkk. (2016) |
| Lanskap loss | Skip connection membuat permukaan loss jauh lebih mulus | Li dkk. (2018) |
Trik praktis: inisialisasi $\gamma = 0$ pada BN terakhir setiap blok residual → setiap blok mulai sebagai identitas (Goyal dkk., 2017). Di PyTorch torchvision: resnet50(zero_init_residual=True).
import torch
import torch.nn as nn
class BasicBlock(nn.Module):
def __init__(self, c_in, c_out, stride=1):
super().__init__()
self.conv1 = nn.Conv2d(c_in, c_out, 3, stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(c_out)
self.conv2 = nn.Conv2d(c_out, c_out, 3, 1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(c_out)
self.shortcut = nn.Identity()
if stride != 1 or c_in != c_out: # opsi B: proyeksi 1x1
self.shortcut = nn.Sequential(nn.Conv2d(c_in, c_out, 1, stride, bias=False),
nn.BatchNorm2d(c_out))
def forward(self, x):
out = torch.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
return torch.relu(out + self.shortcut(x)) # y = ReLU(F(x) + x)
# Verifikasi hitung manual (blok linear sederhana)
W1 = torch.tensor([[1., -1.], [0.5, 0.5]]); W2 = torch.tensor([[0.5, 0.], [0., -1.]])
x = torch.tensor([1., 2.], requires_grad=True)
y = x + W2 @ torch.relu(W1 @ x)
print(y) # tensor([1.0000, 0.5000])
y.sum().backward() # g = [1, 1]
print(x.grad) # tensor([0.5000, 0.5000])
| Kesalahan | Akibat / Solusi |
|---|---|
| Dimensi $F(\mathbf{x})$ dan $\mathbf{x}$ berbeda | Error saat penjumlahan → gunakan shortcut proyeksi 1×1 |
Memakai operasi in-place (out += x) pada tensor yang dibutuhkan autograd |
Error backward → gunakan out = out + x |
| Menaruh ReLU di jalur shortcut | Merusak jalur identitas → aktivasi hanya di cabang $F$ |
| Menganggap skip connection = regularisasi | Fungsi utamanya mempermudah optimisasi, bukan mencegah overfitting |
| Lupa bahwa DenseNet menambah kanal | Memori meledak → gunakan transition layer / compression |
Kerjakan latihan dan verifikasi gradien dengan torch.autograd.