The input dimension of self-attention

Illustration
bin - 2024-10-14T21:20:44+00:00
Question: The input dimension of self-attention

MATLAB currently provides self-attention that can only input one sequence, but how to deal with two-dimensional images, for example, I want to input two-dimensional images composed of two sequences .

Expert Answer

Profile picture of John Williams John Williams answered . 2025-11-20

Hi,
If you want to apply self-attention to two-dimensional images composed of two sequences, you can reshape the image into a single sequence and then apply the self-attention mechanism. Here's a general approach to accomplish this in MATLAB:
  1. Convert the two-dimensional images into sequences: If your two-dimensional images consist of two sequences, you can reshape each image into a single sequence. For example, if the image dimensions are M rows and N columns, you can reshape it into a sequence of length M*N.
  2. Apply self-attention to the reshaped sequences: Once you have reshaped the images into sequences, you can apply the self-attention mechanism. MATLAB does not provide a built-in function specifically for self-attention, but you can implement it using custom code or by utilizing deep learning frameworks like TensorFlow or PyTorch.
Here's a high-level example of how you can implement self-attention for two-dimensional images composed of two sequences using TensorFlow in MATLAB:
 
% Import TensorFlow for MATLAB import tensorflow.* % Reshape the images into sequences sequence1 = reshape(image1, [], 1); sequence2 = reshape(image2, [], 1); % Concatenate the sequences along the feature dimension sequences = cat(2, sequence1, sequence2); % Create a TensorFlow graph graph = tensorflow.Graph; session = tensorflow.Session(graph); % Define the self-attention model with graph.asDefault % Define the inputs input = tensorflow.placeholder(tensorflow.float32, [numFeatures, 2]); % Perform self-attention attention = selfAttention(input); % Run the self-attention operation output = session.run(attention, struct('input', sequences)); % Process the output as needed 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!