forked from yusugomori/DeepLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dA.scala
215 lines (171 loc) · 5.25 KB
/
dA.scala
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// $ scalac dA.scala
// $ scala dA
import scala.util.Random
import scala.math
class dA(val N: Int, val n_visible: Int, val n_hidden: Int,
_W: Array[Array[Double]]=null, _hbias: Array[Double]=null, _vbias: Array[Double]=null,
var rng: Random=null) {
var W: Array[Array[Double]] = Array.ofDim[Double](n_hidden, n_visible)
var hbias: Array[Double] = new Array[Double](n_hidden)
var vbias: Array[Double] = new Array[Double](n_visible)
if(rng == null) rng = new Random(1234)
if(_W == null) {
var i: Int = 0
var j: Int = 0
val a: Double = 1 / n_visible
for(i <- 0 until n_hidden)
for(j <- 0 until n_visible)
W(i)(j) = uniform(-a, a)
} else {
W = _W
}
if(_hbias == null) {
var i: Int = 0
for(i <- 0 until n_hidden) hbias(i) = 0
} else {
hbias = _hbias
}
if(_vbias == null) {
var i: Int = 0
for(i <- 0 until n_visible) vbias(i) = 0
} else {
vbias = _vbias
}
def uniform(min: Double, max: Double): Double = rng.nextDouble() * (max - min) + min
def binomial(n: Int, p: Double): Int = {
if(p < 0 || p > 1) return 0
var c: Int = 0
var r: Double = 0
var i: Int = 0
for(i <- 0 until n) {
r = rng.nextDouble()
if(r < p) c += 1
}
c
}
def sigmoid(x: Double): Double = 1.0 / (1.0 + math.pow(math.E, -x))
def get_corrupted_input(x: Array[Int], tilde_x: Array[Int], p: Double) {
var i: Int = 0;
for(i <- 0 until n_visible) {
if(x(i) == 0) {
tilde_x(i) = 0;
} else {
tilde_x(i) = binomial(1, p)
}
}
}
// Encode
def get_hidden_values(x: Array[Int], y: Array[Double]) {
var i: Int = 0
var j: Int = 0
for(i <- 0 until n_hidden) {
y(i) = 0
for(j <- 0 until n_visible) {
y(i) += W(i)(j) * x(j)
}
y(i) += hbias(i)
y(i) = sigmoid(y(i))
}
}
// Decode
def get_reconstructed_input(y: Array[Double], z: Array[Double]) {
var i: Int = 0
var j: Int = 0
for(i <- 0 until n_visible) {
z(i) = 0
for(j <- 0 until n_hidden) {
z(i) += W(j)(i) * y(j)
}
z(i) += vbias(i)
z(i) = sigmoid(z(i))
}
}
def train(x: Array[Int], lr: Double, corruption_level: Double) {
var i: Int = 0
var j: Int = 0
val tilde_x: Array[Int] = new Array[Int](n_visible)
val y: Array[Double] = new Array[Double](n_hidden)
val z: Array[Double] = new Array[Double](n_visible)
val L_vbias: Array[Double] = new Array[Double](n_visible)
val L_hbias: Array[Double] = new Array[Double](n_hidden)
val p: Double = 1 - corruption_level
get_corrupted_input(x, tilde_x, p)
get_hidden_values(tilde_x, y)
get_reconstructed_input(y, z)
// vbias
for(i <- 0 until n_visible) {
L_vbias(i) = x(i) - z(i)
vbias(i) += lr * L_vbias(i) / N
}
// hbias
for(i <- 0 until n_hidden) {
L_hbias(i) = 0
for(j <- 0 until n_visible) {
L_hbias(i) += W(i)(j) * L_vbias(j)
}
L_hbias(i) *= y(i) * (1 - y(i))
hbias(i) += lr * L_hbias(i) / N
}
// W
for(i <- 0 until n_hidden) {
for(j <- 0 until n_visible) {
W(i)(j) += lr * (L_hbias(i) * tilde_x(j) + L_vbias(j) * y(i)) / N
}
}
}
def reconstruct(x: Array[Int], z: Array[Double]) {
val y: Array[Double] = new Array[Double](n_hidden)
get_hidden_values(x, y)
get_reconstructed_input(y, z)
}
}
object dA {
def test_dA() {
val rng: Random = new Random(123)
var learning_rate: Double = 0.1
val corruption_level: Double = 0.3
val training_epochs: Int = 500
val train_N: Int = 10
val test_N: Int = 2
val n_visible: Int = 20
val n_hidden: Int = 5
val train_X: Array[Array[Int]] = Array(
Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Array(1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Array(1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Array(1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Array(0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1),
Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1),
Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1),
Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0)
)
val da: dA = new dA(train_N, n_visible, n_hidden, rng=rng)
var i: Int = 0
var j: Int = 0
// train
var epoch: Int = 0
for(epoch <- 0 until training_epochs) {
for(i <- 0 until train_N) {
da.train(train_X(i), learning_rate, corruption_level)
}
}
// test data
val test_X: Array[Array[Int]] = Array(
Array(1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0)
)
val reconstructed_X: Array[Array[Double]] = Array.ofDim[Double](test_N, n_visible)
for(i <- 0 until test_N) {
da.reconstruct(test_X(i), reconstructed_X(i))
for(j <- 0 until n_visible) {
printf("%.5f ", reconstructed_X(i)(j))
}
println()
}
}
def main(args: Array[String]) {
test_dA()
}
}