-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonConcurrent.scala
186 lines (162 loc) · 4.84 KB
/
nonConcurrent.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
package nonConcurrent
import scala.collection.mutable.ArrayBuffer
import scala.math.random
import scala.math.exp
import scala.math.abs
class Neuron(var weights: Array[Double]) {
var last_inputs: Array[Double] = new Array[Double](0)
var output: Double = 0.0
var bias_weight:Double = random * 2 - 1
def getOutput(inputs: Array[Double]): Double = {
last_inputs = inputs
output = 0.0
for( i <- 0 to weights.length - 1 ){
output += weights(i) * inputs(i)
}
output += bias_weight
output = 1.0 / ( 1.0 + exp( -output ) )
output
}
}
class NeuronLayer {
var neurons = new ArrayBuffer[Neuron]()
def add(neuron: Neuron) {
neurons += neuron
}
}
class NeuralNetwork(neurons_per_layer: Array[Int]) {
var inputs = Array[Double]()
var expected_outputs = Array[Double]()
val count_layers = neurons_per_layer.length
var layers = new Array[NeuronLayer](count_layers)
var learning_rate = .3
var error_sum = 0.0
//Build the network
for( i <- 0 to count_layers - 1 ){
layers(i) = new NeuronLayer
val len = if( i > 0 ) layers(i-1).neurons.length else 0
for( j <- 0 to neurons_per_layer(i) - 1 ) {
var weights = Array[Double]()
if( i > 0 ){
weights = new Array[Double](len)
for( k <- 0 to len - 1 ){
weights(k) = random * 2 - 1
}
}
layers(i).add(new Neuron(weights))
}
}
def parse(in: Array[Double], out: Array[Double]) {
inputs = in
expected_outputs = out
parse
}
def get_output(in: Array[Double]): Array[Double] = {
var current_outputs = in
for( layer <- 1 to count_layers - 1 ){
val l = layers(layer).neurons.length
var new_outputs = new Array[Double](l)
for( neuron <- 0 to l - 1 ){
new_outputs(neuron) = layers(layer).neurons(neuron).getOutput(current_outputs)
}
current_outputs = new_outputs
}
current_outputs
}
def parse() {
val current_outputs = get_output( inputs )
error_sum = 0.0
for( i <- 0 to current_outputs.length - 1 ){
val error = expected_outputs(i) - current_outputs(i)
error_sum += error
if( abs(error) > .0005 ) {
for( l <- (1 to count_layers - 1).reverse ){
val neurons = layers(l).neurons
for( n <- 0 to neurons.length - 1 ){
if( !( l == count_layers - 1 && n != i ) ){
val neuron = neurons(n)
for( bond <- 0 to neuron.last_inputs.length - 1 ){
layers(l).neurons(n).weights(bond) += learning_rate * error_sum * neuron.last_inputs(bond) * neuron.output *
( 1 - neuron.output )
}
layers(l).neurons(n).bias_weight += learning_rate * error_sum * neuron.output * ( 1 - neuron.output )
}
}
}
}
}
/*for( i <- 0 to current_outputs.length - 1 ){
error_sum = expected_outputs(i) - current_outputs(i)
}
for( l <- (1 to count_layers - 1).reverse ){
val neurons = layers(l).neurons
for( n <- 0 to neurons.length - 1 ){
val neuron = neurons(n)
for( bond <- 0 to neuron.last_inputs.length - 1 ){
layers(l).neurons(n).weights(bond) += learning_rate * error_sum * neuron.last_inputs(bond) * neuron.output * ( 1 - neuron.output )
}
layers(l).neurons(n).bias_weight += learning_rate * error_sum * neuron.output * ( 1 - neuron.output )
}
}*/
//println( "Error: " + error_sum )
}
def train(times: Int, in_out: Map[Array[Double],Array[Double]]) {
for( i <- 0 to times - 1 ) {
for( (k,v) <- in_out ) {
parse( k, v )
}
}
}
def truth_table() {
val n = neurons_per_layer(0)
val o = neurons_per_layer(neurons_per_layer.length - 1)
println( "_" * (n * 2 + o * 3 + 1) )
print( "|" )
for( i <- 0 to n - 1 ) {
print( i + "|" )
}
for( i <- 0 to o - 1 ) {
print( "O" + i + "|" )
}
print( "\n" )
for( r <- 0 to 2^n - 1 ) {
val bin = (("%" + n + "s") format r.toBinaryString).replace(' ', '0')
var in = new Array[Double](n)
for( i <- 0 to n - 1 ) {
in(i) = Integer.parseInt("" + bin(i),10).toDouble
print( "|" + bin(i) )
}
val out = get_output( in )
for( i <- 0 to out.length - 1 ) {
print( "|" + (("%" + (i.toString.length + 1) + "s") format out( i ).round) )
}
print( "|\n" )
}
println( "-" * (n * 2 + o * 3 + 1) )
}
}
object Main {
def time[A](f: => A) = {
val s = System.nanoTime
val ret = f
println( "Time: " + (System.nanoTime-s)/1e6 + "ms" );
ret
}
/**
* @param args the command line arguments
*/
def main(args: Array[String]): Unit = {
time {
var n_six = new NeuralNetwork( Array( 25, 1000, 1000, 1000, 1 ) )
val zero = Array[Double](0,1,1,1,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,1,1,1,0)
val one = Array[Double](0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0)
val t = Array(1.0)
val f = Array(0.0)
n_six.learning_rate = .5
//n_six.train( 1000, Map( zero->t, one->f ) )
time {
println( n_six.get_output(zero)(0) + " | " + n_six.get_output(one)(0) )
}
}
}
}