An analysis of training and generalization errors in shallow and deep networks