Speeding up numerical gradient of tensor with FFTs
I have a gradient problem that involves the following function: adding a phase to each column in a matrix and computing the FFT of each column, and aggregating all FFTs into a new matrix.
My brute-force numerical gradient is adding a small phase to each column, iteratively, and then computing the loss by comparing it with some known matrix. currX is the current guess of phases, Ts2pHH is the matrix to whose columns these phases are added, and the function is computing by taking a sum along 2 dimensions of the intensity and then adding it up.
I want to know if this can be done more efficiently, because for my matrix size (~1000×1000) this takes around 2 minutes, which is very slow.
My code is shown below:
for k = 1:length(currX)
currX_perturbed = currX;
currX_perturbed(k) = currX_perturbed(k) + epsilon;
phases_perturbed = exp(1i * [0, currX_perturbed]);
Tcorr_perturbed = Ts2pHH .* phases_perturbed;
TcorrFFT_perturbed = fftshift(fft(fft(fft(fft(reshape(Tcorr_perturbed, [Npx, Npx, Nin, Nin]), [], 3), [], 4), [], 1), [], 2));
inputFreq_perturbed = squeeze(sum(sum(abs(TcorrFFT_perturbed).^2, 1), 2));
gradient(k) = gradient(k) + (-sum(inputFreq_perturbed .* support, ‘all’) – loss) / epsilon;
endI have a gradient problem that involves the following function: adding a phase to each column in a matrix and computing the FFT of each column, and aggregating all FFTs into a new matrix.
My brute-force numerical gradient is adding a small phase to each column, iteratively, and then computing the loss by comparing it with some known matrix. currX is the current guess of phases, Ts2pHH is the matrix to whose columns these phases are added, and the function is computing by taking a sum along 2 dimensions of the intensity and then adding it up.
I want to know if this can be done more efficiently, because for my matrix size (~1000×1000) this takes around 2 minutes, which is very slow.
My code is shown below:
for k = 1:length(currX)
currX_perturbed = currX;
currX_perturbed(k) = currX_perturbed(k) + epsilon;
phases_perturbed = exp(1i * [0, currX_perturbed]);
Tcorr_perturbed = Ts2pHH .* phases_perturbed;
TcorrFFT_perturbed = fftshift(fft(fft(fft(fft(reshape(Tcorr_perturbed, [Npx, Npx, Nin, Nin]), [], 3), [], 4), [], 1), [], 2));
inputFreq_perturbed = squeeze(sum(sum(abs(TcorrFFT_perturbed).^2, 1), 2));
gradient(k) = gradient(k) + (-sum(inputFreq_perturbed .* support, ‘all’) – loss) / epsilon;
end I have a gradient problem that involves the following function: adding a phase to each column in a matrix and computing the FFT of each column, and aggregating all FFTs into a new matrix.
My brute-force numerical gradient is adding a small phase to each column, iteratively, and then computing the loss by comparing it with some known matrix. currX is the current guess of phases, Ts2pHH is the matrix to whose columns these phases are added, and the function is computing by taking a sum along 2 dimensions of the intensity and then adding it up.
I want to know if this can be done more efficiently, because for my matrix size (~1000×1000) this takes around 2 minutes, which is very slow.
My code is shown below:
for k = 1:length(currX)
currX_perturbed = currX;
currX_perturbed(k) = currX_perturbed(k) + epsilon;
phases_perturbed = exp(1i * [0, currX_perturbed]);
Tcorr_perturbed = Ts2pHH .* phases_perturbed;
TcorrFFT_perturbed = fftshift(fft(fft(fft(fft(reshape(Tcorr_perturbed, [Npx, Npx, Nin, Nin]), [], 3), [], 4), [], 1), [], 2));
inputFreq_perturbed = squeeze(sum(sum(abs(TcorrFFT_perturbed).^2, 1), 2));
gradient(k) = gradient(k) + (-sum(inputFreq_perturbed .* support, ‘all’) – loss) / epsilon;
end gradient, optimization, speed, fft, for loop, matrix, loop, iteration MATLAB Answers — New Questions