Function for creating a neural network with n hidden layers in Flux

You could create the layers first and then stack them, something like this maybe

using Flux

function make_nn(length_in = 80, length_out = 1, nodes = 128, hid_lay = 3, act_fun = relu, act_fun_last = sigmoid)
	first_layer = Dense(length_in, nodes, act_fun)
    intermediate_layers = [Dense(nodes,nodes,act_fun) for _ in 1:hid_lay-1]
    last_layer = Dense(nodes, length_out, act_fun_last)

	return Chain(
        first_layer,
        intermediate_layers...,
        last_layer
    )
end
2 Likes