A General Approach to Gradient Based Learning in Multirate Systems and Neural Networks