Complete the output and calculate the perplexity