Feed Forward Neural Network

Created onDibuat pada
Last updated onTerakhir diperbarui
10 min read10 menit baca
IDFFNNdata-science
ContributorsKontributor
Razi Rachman Widyadhana - @zirachw

Feed Forward Neural Network (FFNN) adalah arsitektur artificial neural network di mana informasi mengalir satu arah dari input ke output tanpa loops.

#Link to this headingIntroduction to ANN

#Link to this headingWhy ANN?

ANN berhasil diterapkan di berbagai sektor: image recognition, NLP, time-series
ANN berhasil diterapkan di berbagai sektor: image recognition, NLP, time-series

ANN terbukti berhasil di berbagai sektor. ANN cocok untuk masalah dengan karakteristik:

  • Instances direpresentasikan oleh banyak attribute-value pairs
  • Target function bisa discrete-valued, real-valued, atau vektor
  • Training examples boleh mengandung noise/errors
  • Waktu training yang lama dapat diterima
  • Evaluasi cepat dari fungsi target yang dipelajari diperlukan
  • Kemampuan manusia memahami hasil pembelajaran tidak penting

#Link to this headingWhat is ANN?

Perceptron (termasuk Adaline): satu unit/neuron, fungsi estimasi.

Artificial Neural Network: banyak computational units yang menjadi intelligent hanya melalui interaksi satu sama lain, terinspirasi dari otak, dan terdiri dari komposisi banyak fungsi berbeda.

#Link to this headingAdaline (ADAptive LInear NEuron)

Single layer Neural Network dengan update rule:

w←w+η(y−y^)⋅xw \leftarrow w + \eta (y - \hat{y}) \cdot x

Error menggunakan Squared Error loss function. Disebut juga Widrow-Hoff rule atau LMS rule, yang merupakan bridge antara Perceptron dan FFNN.

#Link to this headingArsitektur ANN

Arsitektur ANN: explicit style (diagram lengkap) dan compact style (notasi matriks)
Arsitektur ANN: explicit style (diagram lengkap) dan compact style (notasi matriks)

Arsitektur FFNN terdiri dari:

  • Input layer: dd input neurons (x1,x2,…,xdx_1, x_2, \ldots, x_d)
  • Hidden layer(s): satu atau lebih layer tersembunyi
  • Output layer: mm output neurons (o1,…,omo_1, \ldots, o_m)

Setiap layer terhubung sepenuhnya (fully connected) ke layer berikutnya.

#Link to this headingJenis Arsitektur ANN

FFNN: directed acyclic graph; RNN: ada loops untuk sequence processing
FFNN: directed acyclic graph; RNN: ada loops untuk sequence processing
ArsitekturDeskripsi
Feed Forward Neural Network (FFNN)Koneksi hanya satu arah, membentuk directed acyclic graph (DAG). Tidak ada loops.
Multi Layer Perceptron (MLP)FFNN yang fully connected dengan fungsi aktivasi non-linear.
Recurrent Neural Network (RNN)Memiliki loops berupa output intermediate/final di-feed back sebagai input. Dibahas lebih lanjut di bagian RNN.
2-layer MLP: input layer tidak dihitung, hidden + output = 2 layer
2-layer MLP: input layer tidak dihitung, hidden + output = 2 layer

#Link to this headingJumlah Parameter

Jumlah parameter: setiap koneksi + bias adalah parameter yang dapat dilatih
Jumlah parameter: setiap koneksi + bias adalah parameter yang dapat dilatih

Total parameter = jumlah bobot + jumlah bias.

Param per layer=(input units+1)×output units\text{Param per layer} = (\text{input units} + 1) \times \text{output units}

Contoh: Input (8), HL1 (12), HL2 (8), Output (1):

Total=(8+1)×12+(12+1)×8+(8+1)×1=108+104+9=221\text{Total} = (8+1) \times 12 + (12+1) \times 8 + (8+1) \times 1 = 108 + 104 + 9 = \mathbf{221}

Implementasi Keras:

from keras.models import Sequential
from keras.layers import Dense
 
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
 
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=150, batch_size=10)

#Link to this headingForward Propagation

Forward propagation: input mengalir melalui network dari kiri ke kanan
Forward propagation: input mengalir melalui network dari kiri ke kanan

Informasi mengalir melalui network dari input ke output untuk menghitung prediksi.

#Link to this headingFormula Forward Propagation

Untuk input vector x\mathbf{x}, dengan bobot WxhW_{xh} (input ke hidden), bias c\mathbf{c} (hidden), bobot WhyW_{hy} (hidden ke output), dan bias b\mathbf{b} (output):

f(x; Wxh,c,Why,b)=f2 ⁣(WhyT⋅f1 ⁣(WxhT⋅x+c)+b)f(\mathbf{x};\, W_{xh}, \mathbf{c}, W_{hy}, \mathbf{b}) = f_2\!\left(W_{hy}^T \cdot f_1\!\left(W_{xh}^T \cdot \mathbf{x} + \mathbf{c}\right) + \mathbf{b}\right)

Jika bias dimasukkan ke dalam matriks bobot:

f(x; Wxh,Why)=f2 ⁣(WhyT⋅f1 ⁣(WxhT⋅x))f(\mathbf{x};\, W_{xh}, W_{hy}) = f_2\!\left(W_{hy}^T \cdot f_1\!\left(W_{xh}^T \cdot \mathbf{x}\right)\right)

#Link to this headingForward Propagation dengan Mini-batch

Untuk input mini-batch XX (matriks):

f(X; Wxh,c,Why,b)=f2 ⁣(f1(X⋅Wxh+c)⋅Why+b)f(X;\, W_{xh}, \mathbf{c}, W_{hy}, \mathbf{b}) = f_2\!\left(f_1(X \cdot W_{xh} + \mathbf{c}) \cdot W_{hy} + \mathbf{b}\right)

#Link to this headingContoh: XOR (Sigmoid)

XOR dengan sigmoid: 2 hidden units mampu merepresentasikan XOR yang tidak linearly separable
XOR dengan sigmoid: 2 hidden units mampu merepresentasikan XOR yang tidak linearly separable
x1x_1x2x_2yy
000
011
101
110

h1=σ(−10+20x1+20x2),h2=σ(30−20x1−20x2)h_1 = \sigma(-10 + 20x_1 + 20x_2), \quad h_2 = \sigma(30 - 20x_1 - 20x_2)

y=σ(−30+20h1+20h2)y = \sigma(-30 + 20h_1 + 20h_2)

#Link to this headingContoh: XOR (ReLU & Linear)

h1=ReLU(0+x1+x2),h2=ReLU(−1+x1+x2)h_1 = \text{ReLU}(0 + x_1 + x_2), \quad h_2 = \text{ReLU}(-1 + x_1 + x_2)

y=0+h1−2h2y = 0 + h_1 - 2h_2

Verifikasi untuk (x1=1,x2=1)(x_1=1, x_2=1): h1=ReLU(2)=2h_1 = \text{ReLU}(2) = 2, h2=ReLU(1)=1h_2 = \text{ReLU}(1) = 1, y=2−2(1)=0y = 2 - 2(1) = 0 ✓

#Link to this headingFungsi Aktivasi

FungsiFormulaRangeTurunan
Linearf(z)=zf(z) = z(−∞,+∞)(-\infty, +\infty)f′(z)=1f'(z) = 1
Sigmoidf(z)=11+e−zf(z) = \frac{1}{1 + e^{-z}}(0,1)(0, 1)f′(z)=f(z)(1−f(z))f'(z) = f(z)(1 - f(z))
ReLUf(z)=max⁡(0,z)f(z) = \max(0, z)[0,+∞)[0, +\infty)f′(z)=1f'(z) = 1 jika z>0z > 0, 00 jika z≤0z \leq 0

#Link to this headingBackpropagation

Learning procedure: forward prop → compute error → backward prop → update weights
Learning procedure: forward prop → compute error → backward prop → update weights

Backpropagation adalah algoritma untuk melatih FFNN dengan cara menghitung gradient error terhadap setiap bobot menggunakan chain rule.

#Link to this headingProsedur Learning

  1. Forward propagation: hitung output yy untuk input xx
  2. Backward propagation: hitung error term δ\delta untuk setiap neuron
  3. Update setiap bobot berdasarkan error term

#Link to this headingBackpropagation untuk MLP

Backprop MLP: error mengalir mundur dari output layer ke hidden layers
Backprop MLP: error mengalir mundur dari output layer ke hidden layers

#Link to this headingError Term untuk Output Unit

Gradient untuk output unit weights: δ_o = -(t-o)·f'(net)
Gradient untuk output unit weights: δ_o = -(t-o)·f'(net)

Untuk output unit dengan loss function EE (squared error):

δo=∂E∂neto=−(t−o)⋅f′(neto)\delta_o = \frac{\partial E}{\partial \text{net}_o} = -(t - o) \cdot f'(\text{net}_o)

Jika menggunakan aktivasi sigmoid:

δo=−(t−o)⋅o(1−o)\delta_o = -(t - o) \cdot o(1 - o)

#Link to this headingError Term untuk Hidden Unit

Gradient untuk hidden unit weights: δ_h = f'(net_h) · Σ(w_hk · δ_k)
Gradient untuk hidden unit weights: δ_h = f'(net_h) · Σ(w_hk · δ_k)

Untuk hidden unit hh yang terhubung ke beberapa output units:

δh=f′(neth)⋅∑k∈outputswhk⋅δk\delta_h = f'(\text{net}_h) \cdot \sum_{k \in \text{outputs}} w_{hk} \cdot \delta_k

Jika hidden unit menggunakan sigmoid:

δh=oh(1−oh)⋅∑k∈outputswhk⋅δk\delta_h = o_h(1 - o_h) \cdot \sum_{k \in \text{outputs}} w_{hk} \cdot \delta_k

#Link to this headingWeight Update Rule

wijnew=wijold−η⋅∂E∂wij=wijold−η⋅δj⋅xiw_{ij}^{\text{new}} = w_{ij}^{\text{old}} - \eta \cdot \frac{\partial E}{\partial w_{ij}} = w_{ij}^{\text{old}} - \eta \cdot \delta_j \cdot x_i

Karena δ\delta sudah mengandung tanda negatif dari −(t−o)-(t-o), ini ekuivalen dengan:

wijnew=wijold+η⋅(t−o)⋅f′(net)⋅xiw_{ij}^{\text{new}} = w_{ij}^{\text{old}} + \eta \cdot (t - o) \cdot f'(\text{net}) \cdot x_i

Initialize all weights to small random values
Until termination condition is met:
    For each training example <x, t>:
        // Forward pass
        Compute output o for each unit
 
        // Backward pass (output units)
        For each output unit k:
            δ_k = o_k(1 - o_k)(t_k - o_k)
 
        // Backward pass (hidden units, from output back to first hidden)
        For each hidden unit h:
            δ_h = o_h(1 - o_h) * Σ(w_hk * δ_k)
 
        // Weight update
        For each weight w_ij:
            w_ij = w_ij + η * δ_j * x_i

Termination conditions:

  • Jumlah iterasi tetap
  • Error pada training examples << threshold
  • Error pada validation set memenuhi kriteria

#Link to this headingContoh Backpropagation

Backprop contoh: 2 input → 2 hidden (sigmoid) → 2 output (sigmoid), η=0.5
Backprop contoh: 2 input → 2 hidden (sigmoid) → 2 output (sigmoid), η=0.5

Arsitektur: 2 input, 2 hidden (sigmoid), 2 output (sigmoid), η=0.5\eta = 0.5.

Setup:

  • i1=0.05i_1 = 0.05, i2=0.10i_2 = 0.10
  • w1=0.15w_1 = 0.15, w2=0.20w_2 = 0.20, w3=0.25w_3 = 0.25, w4=0.30w_4 = 0.30 (input → hidden)
  • w5=0.40w_5 = 0.40, w6=0.45w_6 = 0.45, w7=0.50w_7 = 0.50, w8=0.55w_8 = 0.55 (hidden → output)
  • b1=0.35b_1 = 0.35 (bias hidden), b2=0.60b_2 = 0.60 (bias output)
  • Target: t1=0.01t_1 = 0.01, t2=0.99t_2 = 0.99

#Link to this headingForward Pass

Hidden layer:

neth1=w1i1+w2i2+b1=0.15(0.05)+0.20(0.10)+0.35=0.3775\text{net}_{h_1} = w_1 i_1 + w_2 i_2 + b_1 = 0.15(0.05) + 0.20(0.10) + 0.35 = 0.3775 oh1=σ(0.3775)=0.5933o_{h_1} = \sigma(0.3775) = 0.5933

neth2=w3i1+w4i2+b1=0.25(0.05)+0.30(0.10)+0.35=0.3925\text{net}_{h_2} = w_3 i_1 + w_4 i_2 + b_1 = 0.25(0.05) + 0.30(0.10) + 0.35 = 0.3925 oh2=σ(0.3925)=0.5969o_{h_2} = \sigma(0.3925) = 0.5969

Output layer:

neto1=w5oh1+w6oh2+b2=0.40(0.5933)+0.45(0.5969)+0.60=1.1059\text{net}_{o_1} = w_5 o_{h_1} + w_6 o_{h_2} + b_2 = 0.40(0.5933) + 0.45(0.5969) + 0.60 = 1.1059 o1=σ(1.1059)=0.7514o_1 = \sigma(1.1059) = 0.7514

neto2=w7oh1+w8oh2+b2=0.50(0.5933)+0.55(0.5969)+0.60=1.2250\text{net}_{o_2} = w_7 o_{h_1} + w_8 o_{h_2} + b_2 = 0.50(0.5933) + 0.55(0.5969) + 0.60 = 1.2250 o2=σ(1.2250)=0.7729o_2 = \sigma(1.2250) = 0.7729

Total Error:

E=12(t1−o1)2+12(t2−o2)2=12(0.01−0.7514)2+12(0.99−0.7729)2=0.2748+0.0236=0.2984E = \frac{1}{2}(t_1 - o_1)^2 + \frac{1}{2}(t_2 - o_2)^2 = \frac{1}{2}(0.01 - 0.7514)^2 + \frac{1}{2}(0.99 - 0.7729)^2 = 0.2748 + 0.0236 = \mathbf{0.2984}

#Link to this headingBackward Pass

Backprop example backward pass: gradient mengalir dari output ke hidden ke input
Backprop example backward pass: gradient mengalir dari output ke hidden ke input

Error term output units (konvensi Mitchell: δ=(t−o)⋅f′\delta = (t-o) \cdot f'):

δo1=o1(1−o1)(t1−o1)=0.7514×0.2486×(−0.7414)=−0.1385\delta_{o_1} = o_1(1 - o_1)(t_1 - o_1) = 0.7514 \times 0.2486 \times (-0.7414) = \mathbf{-0.1385}

δo2=o2(1−o2)(t2−o2)=0.7729×0.2271×0.2171=0.0381\delta_{o_2} = o_2(1 - o_2)(t_2 - o_2) = 0.7729 \times 0.2271 \times 0.2171 = \mathbf{0.0381}

Error term hidden units:

δh1=oh1(1−oh1)⋅(w5δo1+w7δo2)=0.5933×0.4067×(0.40×(−0.1385)+0.50×0.0381)=−0.00876\delta_{h_1} = o_{h_1}(1 - o_{h_1}) \cdot (w_5 \delta_{o_1} + w_7 \delta_{o_2}) = 0.5933 \times 0.4067 \times (0.40 \times (-0.1385) + 0.50 \times 0.0381) = \mathbf{-0.00876}

δh2=oh2(1−oh2)⋅(w6δo1+w8δo2)=0.5969×0.4031×(0.45×(−0.1385)+0.55×0.0381)=−0.00994\delta_{h_2} = o_{h_2}(1 - o_{h_2}) \cdot (w_6 \delta_{o_1} + w_8 \delta_{o_2}) = 0.5969 \times 0.4031 \times (0.45 \times (-0.1385) + 0.55 \times 0.0381) = \mathbf{-0.00994}

#Link to this headingWeight Update

Output layer weights (w=w+η⋅δj⋅xiw = w + \eta \cdot \delta_j \cdot x_i):

BobotUpdateNilai Baru
w5w_50.40+0.5×(−0.1385)×0.59330.40 + 0.5 \times (-0.1385) \times 0.59330.35890.3589
w6w_60.45+0.5×(−0.1385)×0.59690.45 + 0.5 \times (-0.1385) \times 0.59690.40870.4087
w7w_70.50+0.5×0.0381×0.59330.50 + 0.5 \times 0.0381 \times 0.59330.51130.5113
w8w_80.55+0.5×0.0381×0.59690.55 + 0.5 \times 0.0381 \times 0.59690.56140.5614

Hidden layer weights:

BobotUpdateNilai Baru
w1w_10.15+0.5×(−0.00876)×0.050.15 + 0.5 \times (-0.00876) \times 0.050.149780.14978
w2w_20.20+0.5×(−0.00876)×0.100.20 + 0.5 \times (-0.00876) \times 0.100.199560.19956
w3w_30.25+0.5×(−0.00994)×0.050.25 + 0.5 \times (-0.00994) \times 0.050.249750.24975
w4w_40.30+0.5×(−0.00994)×0.100.30 + 0.5 \times (-0.00994) \times 0.100.299500.29950

#Link to this headingArsitektur untuk Klasifikasi Digit

NN untuk digit tulisan tangan: input 784 → hidden layers → output 10 kelas
NN untuk digit tulisan tangan: input 784 → hidden layers → output 10 kelas

Klasifikasi digit tulisan tangan (MNIST 28×28 = 784 piksel) menggunakan FFNN:

Input (784) → Hidden 1 (128, ReLU) → Hidden 2 (64, ReLU) → Output (10, Softmax)

#Link to this headingRingkasan

AspekKeterangan
ArsitekturInput → Hidden Layer(s) → Output
Forward PropagationHitung output dari input ke output
BackpropagationHitung error terms dari output ke input (chain rule)
Weight Update (Mitchell)w=w+η⋅δ⋅xw = w + \eta \cdot \delta \cdot x
Weight Update (Kalkulus)w=w−η⋅δ⋅xw = w - \eta \cdot \delta \cdot x
AktivasiSigmoid, ReLU, Linear
Loss FunctionSquared Error: E=12(t−o)2E = \frac{1}{2}(t-o)^2
Vanishing GradientSigmoid di jaringan dalam → gradient mengecil → gunakan ReLU