-
Notifications
You must be signed in to change notification settings - Fork 0
/
Networks.jl
executable file
·79 lines (50 loc) · 1.94 KB
/
Networks.jl
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
module Networks
export NeuralNetwork, output
# Simplified, barely optimized implementation
struct NeuralNetwork
layers::Vector{Int64}
shapes::Vector{Tuple{Int64, Int64}}
activation::Function
weights::Vector{Matrix{Float64}}
biases::Vector{Vector{Float64}}
fitness::Ref{Float64}
NeuralNetwork(
layers::Vector{Int64},
activation_function::Function
)::NeuralNetwork =
new(_NeuralNetwork(layers, activation_function)...)
NeuralNetwork(
layers::Vector{Int64},
shapes::Vector{Tuple{Int64, Int64}},
activation::Function,
weights::Vector{Matrix{Float64}},
biases::Vector{Matrix{Float64}}
)::NeuralNetwork =
new(layers, shapes, activation, weights, biases, Ref(0.0))
end
ConstructorReturn::Type = Tuple{Vector{Int64}, Vector{Tuple{Int64, Int64}}, Function, Vector{Matrix{Float64}}, Vector{Vector{Float64}}, Ref{Float64}}
function _NeuralNetwork(layers::Vector{Int64}, activation_function::Function)::ConstructorReturn
_shapes::Vector{Tuple{Int64, Int64}} = shapes(layers)
_weights::Vector{Matrix{Float64}} =
_shapes .|> (shape::Tuple{Int64, Int64} -> randn(shape...))
_biases::Vector{Vector{Float64}} = layers[2:end] .|> randn
return (
layers,
_shapes,
activation_function,
_weights,
_biases,
Ref(0.0)
)
end
function shapes(layers::Vector{Int64})::Vector{Tuple{Int64, Int64}}
return eachindex(layers[1:end-1]) .|> (i -> (layers[i+1], layers[i]))
end
function output(network::NeuralNetwork, input::Vector{Float64})::Vector{Float64}
layer_output::Vector{Float64} = input
for (layer_weights::Matrix{Float64}, layer_biases::Vector{Float64}) in zip(@view(network.weights[:]), @view(network.biases[:]))
layer_output = network.activation.(layer_weights * layer_output + layer_biases)
end
return layer_output
end
end #module