Explain how self-attention works in Transformer-based architectures and how it differs from recurrent neural networks.