-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNeuralNet_BY_HAND.R
85 lines (64 loc) · 2.34 KB
/
NeuralNet_BY_HAND.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#This code fits a neural network with 2 inputs,
#3 nodes in a single hidden layer, and one output.
x1 = c(0,0,1,1)
x2 = c(0,1,0,1)
y = c(0,1,1,0)
#x1 = c(2,4,1,1)
#x2 = c(3,2,4,1)
#y = c(1,2,3,4)
#activation Function
sigma <- function(x){
out <- 1/(1+exp(-x))
return(out)
}
#Derivative of the activation function
sigma_prime <- function(x){
out <- exp(-x)/(1+exp(-x))^2
return(out)
}
gamma <- 1
#Variable Transofrmations
z1 <- function(x1, x2){
out <- sigma(alpha[1,1] + alpha[1,2]*x1 + alpha[1,3]*x2)
return(out)
}
z2 <- function(x1, x2){
out <- sigma(alpha[2,1] + alpha[2,2]*x1 + alpha[2,3]*x2)
return(out)
}
z3 <- function(x1, x2){
out <- sigma(alpha[3,1] + alpha[3,2]*x1 + alpha[3,3]*x2)
return(out)
}
f <- function(x1, x2){
out <- beta[1] + beta[2]*z1(x1, x2) + beta[3]*z2(x1, x2)
return(out)
}
#Learning rate
g <- 0.1
#partial R wrt beta
#beta1
-2*(y - f(x1,x2))*z1(x1,x2)
#beta2
-2*(y - f(x1,x2))*z2(x1,x2)
#Initial Values for the weights
beta <- c(0,0,0,0)+0.5
alpha <- rbind(c(0,0,0),c(0,0,0),c(0,0,0))+0.5
for (i in 1:1000){
beta[1] <- beta[1] - g*sum(-2*(y - f(x1,x2)))
beta[2] <- beta[2] - g*sum(-2*(y - f(x1,x2))*z1(x1,x2))
beta[3] <- beta[3] - g*sum(-2*(y - f(x1,x2))*z2(x1,x2))
beta[4] <- beta[4] - g*sum(-2*(y - f(x1,x2))*z3(x1,x2))
#partial R wrt alpha
#l = 1
alpha[1,1] <- alpha[1,1]-g*sum(-2*(y - f(x1,x2))*beta[2]*sigma_prime(alpha[1,1] + alpha[1,2]*x1 + alpha[1,3]*x2))
alpha[1,2] <- alpha[1,2]-g*sum(-2*(y - f(x1,x2))*beta[2]*sigma_prime(alpha[1,1] + alpha[1,2]*x1 + alpha[1,3]*x2)*x1)
alpha[1,3] <- alpha[1,3]-g*sum(-2*(y - f(x1,x2))*beta[2]*sigma_prime(alpha[1,1] + alpha[1,2]*x1 + alpha[1,3]*x2)*x2)
alpha[2,1] <- alpha[2,1]-g*sum(-2*(y - f(x1,x2))*beta[3]*sigma_prime(alpha[2,1] + alpha[2,2]*x1 + alpha[2,3]*x2))
alpha[2,2] <- alpha[2,2]-g*sum(-2*(y - f(x1,x2))*beta[3]*sigma_prime(alpha[2,1] + alpha[2,2]*x1 + alpha[2,3]*x2)*x1)
alpha[2,3] <- alpha[2,3]-g*sum(-2*(y - f(x1,x2))*beta[3]*sigma_prime(alpha[2,1] + alpha[2,2]*x1 + alpha[2,3]*x2)*x2)
alpha[3,1] <- alpha[3,1]-g*sum(-2*(y - f(x1,x2))*beta[4]*sigma_prime(alpha[3,1] + alpha[3,2]*x1 + alpha[3,3]*x2))
alpha[3,2] <- alpha[3,2]-g*sum(-2*(y - f(x1,x2))*beta[4]*sigma_prime(alpha[3,1] + alpha[3,2]*x1 + alpha[3,3]*x2)*x1)
alpha[3,3] <- alpha[3,3]-g*sum(-2*(y - f(x1,x2))*beta[4]*sigma_prime(alpha[3,1] + alpha[3,2]*x1 + alpha[3,3]*x2)*x2)
print(f(x1,x2))
}