Learning with Exact Invariances in Polynomial Time