You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I created a real valued convolution layer to verify, if the gradients calculated for the complex valued Conv-layer are correct:
using Flux
struct ComplexWeight w_re; w_im end# A 1 dim conv layer, where real and imaginary parts are seperatedeachmat(A) = (view(A, :, :, i) for i inaxes(A, 3))
(cw::ComplexWeight)(A) =reshape(mapreduce(x -> [x[:,1:4] * cw.w_re .- x[:,5:8] * cw.w_im x[:,5:8] * cw.w_re .+ x[:,1:4] * cw.w_im], hcat, eachmat(A)), (size(A,1), 2, 1))
Flux.@functor ComplexWeight
complex_init =randn(ComplexF32, 1, 4, 1)
real_convl =ComplexWeight(real.(vec(complex_init)), imag.(vec(complex_init)))
convl =Conv((1,), 4=>1, identity; pad=SamePad(), init=(dims...) -> complex_init, bias=false)
xs =randn(ComplexF32, 256, 4, 1);
ys =randn(ComplexF32, 256, 1, 1);
to_real(A) =hcat(real.(A), imag.(A))
to_complex(A) =complex.(A[:,1:size(A,2) >>1,:], A[:,size(A,2) >>1+1:end,:])
# Check if layers produce the same output
real_y =real_convl(to_real(xs));
convl(xs) ≈complex.(real_y[:,1], real_y[:,2]) # true# Create loss functions and check if they result in the same outputloss_real(model, xs, ys) = Flux.Losses.mse(to_complex(model(xs)), ys)
loss(model, xs, ys) = Flux.Losses.mse(model(xs), ys)
loss_real(real_convl, to_real(xs), ys) ≈loss(convl, xs, ys) # true# Calculate gradients
params_real = Flux.params(real_convl)
grads_real = Flux.gradient(params_real) doloss_real(real_convl, to_real(xs), ys)
end
params = Flux.params(convl)
grads = Flux.gradient(params) doloss(convl, xs, ys)
endvec(grads[params[1]]) ≈complex.(grads_real[params_real[1]], grads_real[params_real[2]]) # false
The layers and the loss functions produce the same output given the same weights. However, the gradients are different.
I've checked a basic gradient calculation:
I created a real valued convolution layer to verify, if the gradients calculated for the complex valued Conv-layer are correct:
The layers and the loss functions produce the same output given the same weights. However, the gradients are different.
I've checked a basic gradient calculation:
This is correct. My guess is that the error is somewhere in the Conv-layer.
The text was updated successfully, but these errors were encountered: