From inverse problems to learning: a statistical mechanics approach