/
NNParameters.jl
83 lines (59 loc) · 2.98 KB
/
NNParameters.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
80
81
82
83
module NNParameters
export LayerParameters, Conv2DParameters, PoolParameters, ConvolutionLayerParameters, MatrixMultiplicationParameters, SoftmaxParameters, FullyConnectedLayerParameters, NeuralNetParameters, StandardNeuralNetParameters
abstract type LayerParameters end
struct Conv2DParameters{T<:Real, U<:Real} <: LayerParameters
filter::Array{T, 4}
bias::Array{U, 1}
function Conv2DParameters{T, U}(filter::Array{T, 4}, bias::Array{U, 1}) where {T<:Real, U<:Real}
(filter_height, filter_width, filter_in_channels, filter_out_channels) = size(filter)
bias_out_channels = length(bias)
@assert filter_out_channels == bias_out_channels "For the convolution layer, number of output channels in filter, $filter_out_channels, does not match number of output channels in bias, $bias_out_channels."
return new(filter, bias)
end
end
function Conv2DParameters(filter::Array{T, 4}, bias::Array{U, 1}) where {T<:Real, U<:Real}
Conv2DParameters{T, U}(filter, bias)
end
struct PoolParameters{N} <: LayerParameters
strides::NTuple{N, Int}
end
struct ConvolutionLayerParameters{T<:Real, U<:Real} <: LayerParameters
conv2dparams::Conv2DParameters{T, U}
maxpoolparams::PoolParameters{4}
end
function ConvolutionLayerParameters{T<:Real, U<:Real}(filter::Array{T, 4}, bias::Array{U, 1}, strides::NTuple{4, Int})
ConvolutionLayerParameters(Conv2DParameters(filter, bias), PoolParameters(strides))
end
struct MatrixMultiplicationParameters{T<:Real, U<:Real} <: LayerParameters
matrix::Array{T, 2}
bias::Array{U, 1}
function MatrixMultiplicationParameters{T, U}(matrix::Array{T, 2}, bias::Array{U, 1}) where {T<:Real, U<:Real}
(matrix_height, matrix_width) = size(matrix)
bias_height = length(bias)
@assert matrix_height == bias_height "Number of output channels in matrix, $matrix_height, does not match number of output channels in bias, $bias_height."
return new(matrix, bias)
end
end
function MatrixMultiplicationParameters(matrix::Array{T, 2}, bias::Array{U, 1}) where {T<:Real, U<:Real}
MatrixMultiplicationParameters{T, U}(matrix, bias)
end
struct SoftmaxParameters{T<:Real, U<:Real} <: LayerParameters
mmparams::MatrixMultiplicationParameters{T, U}
end
function SoftmaxParameters(matrix::Array{T, 2}, bias::Array{U, 1}) where {T<:Real, U<:Real}
SoftmaxParameters(MatrixMultiplicationParameters(matrix, bias))
end
struct FullyConnectedLayerParameters{T<:Real, U<:Real} <: LayerParameters
mmparams::MatrixMultiplicationParameters{T, U}
end
function FullyConnectedLayerParameters(matrix::Array{T, 2}, bias::Array{U, 1}) where {T<:Real, U<:Real}
FullyConnectedLayerParameters(MatrixMultiplicationParameters(matrix, bias))
end
abstract type NeuralNetParameters end
struct StandardNeuralNetParameters <: NeuralNetParameters
convlayer_params::Array{ConvolutionLayerParameters, 1}
fclayer_params::Array{FullyConnectedLayerParameters, 1}
softmax_params::SoftmaxParameters
UUID::String
end
end