Hello everyone,
I am new to Flux, but so far I really like it.
I was trying to train a neural network where I wanted to include the gain of the sigmoidal activation functions in the cost function, to penalise small gains.
To this end I tried to create a struct (which is almost identical to the Dense
layer), but which also holds the gain parameter so that it can be easily differentiated by Flux (this code comes from the code for the Dense
layer):
using Flux
using Flux: @treelike, glorot_uniform
struct GainLayer{F <: Function, S <: AbstractArray, T <: AbstractVector, U <: Number}
W::S
b::T
k::U
σ::F
end
GainLayer(W, b, k) = GainLayer(W, b, k, identity)
@treelike GainLayer
function GainLayer(in::Integer, out::Integer, σ = identity;
initW = glorot_uniform, initb = zeros, initk = 1)
return GainLayer(initW(out, in), initb(out), initk, σ)
end
function (a::GainLayer)(x::AbstractArray)
W, b, k, σ = a.W, a.b, a.k, a.σ
σ.(k .* (W*x .+ b))
end
function activation(x)
0.5 * (tanh(x) + 1)
end
Din = 10
Dhidden = 30
Dout = 5
test_model = Chain(
GainLayer(Din, Dhidden, activation),
GainLayer(Dhidden, Dout, activation)
)
However, when I do Flux.params(test_model)
, it finds no parameters of this net.
Could someone help me figure out what I’m doing wrong? Many thanks!