Learning Counterfactually Invariant Predictors