A mathematical model for automatic differentiation in machine learning