-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.R
38 lines (25 loc) · 866 Bytes
/
train.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
source('sigmoid.R')
train <- function(Arch, Theta, X, y) {
m <- nrow(y)
# Get Theta1/2 back, use them to initialise error counterpart matrices
t <- splitTheta(Arch, Theta)
Theta1 <- as.matrix(t[[1]])
Theta2 <- as.matrix(t[[2]])
DELTA2 <- matrix(0, nrow(Theta2), ncol(Theta2))
DELTA1 <- matrix(0, nrow(Theta1), ncol(Theta1))
# Forward propagation
# Prepend bias term to each row in the input layer
a1 <- as.matrix(cbind(1, X))
z2 <- a1 %*% Theta1
a2 <- sigmoid(z2)
a2 <- as.matrix(cbind(1, a2))
z3 <- a2 %*% Theta2
a3 <- sigmoid(z3)
# Backpropagation
delta3 <- a3 - y
delta2 <- (delta3 %*% t(Theta2[2:nrow(Theta2),])) * sigmoidGradient(z2)
DELTA2 <- DELTA2 + (t(a2) %*% delta3)
DELTA1 <- DELTA1 + (t(a1) %*% delta2)
grad <- c(DELTA1,DELTA2)
return(list(a3, grad))
}