Factor out trainer and support arbitrary number of layers