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 .
John Williams answered .
2025-11-20
% 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