Wake-sleep algorithm