Acoustic waves in the Sun are affected by the atmospheric layers, but this region is often ignored in forward models because it increases the computational cost. The purpose of this work is to take the solar atmosphere into account without significantly increasing the computational cost. We solved a scalar-wave equation that describes the propagation of acoustic modes inside the Sun using a finite-element method. The boundary conditions used to truncate the computational domain were learned from the Dirichlet-to-Neumann operator, that is, the relation between the solution and its normal derivative at the computational boundary. These boundary conditions may be applied at any height above which the background medium is assumed to be radially symmetric. We show that learned infinite elements lead to a numerical accuracy similar to the accuracy that is obtained for a traditional radiation boundary condition in a simple atmospheric model. The main advantage of learned infinite elements is that they reproduce the solution for any radially symmetric atmosphere to a very good accuracy at low computational cost. In particular, when the boundary condition is applied directly at the surface instead of at the end of the photosphere, the computational cost is reduced by 20<!PCT!> in 2D and by 60<!PCT!> in 3D. This reduction reaches 70<!PCT!> in 2D and 200<!PCT!> in 3D when the computational domain includes the atmosphere. We emphasize the importance of including atmospheric layers in helioseismology and propose a computationally efficient method to do this.