Automatic stabilization of finite-element simulations using neural networks and hierarchical matrices