/
net_components.jl
49 lines (35 loc) · 1.5 KB
/
net_components.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
using JuMP
using AutoHashEquals
export Layer, NeuralNet
JuMPLinearType = Union{JuMP.Variable,JuMP.AffExpr}
JuMPReal = Union{Real,JuMPLinearType}
include("net_components/core_ops.jl")
"""
$(TYPEDEF)
Supertype for all types storing the parameters of each layer. Inherit from this
to specify your own custom type of layer. Each implementation is expected to:
1. Implement a callable specifying the output when any input of type `JuMPReal` is provided.
"""
abstract type Layer end
"""
An array of `Layers` is interpreted as that array of layer being applied
to the input sequentially, starting from the leftmost layer. (In functional programming
terms, this can be thought of as a sort of `fold`).
"""
chain(x::Array{<:JuMPReal}, ps::Array{<:Layer,1}) = length(ps) == 0 ? x : chain(ps[1](x), ps[2:end])
(ps::Array{<:Layer,1})(x::Array{<:JuMPReal}) = chain(x, ps)
function check_size(input::AbstractArray, expected_size::NTuple{N,Int})::Nothing where {N}
input_size = size(input)
@assert input_size == expected_size "Input size $input_size did not match expected size $expected_size."
end
include("net_components/layers.jl")
"""
$(TYPEDEF)
Supertype for all types storing the parameters of a neural net. Inherit from this
to specify your own custom architecture. Each implementation
is expected to:
1. Implement a callable specifying the output when any input of type `JuMPReal` is provided
2. Have a `UUID` field for the name of the neural network.
"""
abstract type NeuralNet end
include("net_components/nets.jl")