Created
May 28, 2019 15:03
-
-
Save fmeulen/e6963480fbc7dca378b3d71a2c238a5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function f(x) | |
y = x | |
zz = zeros(typeof(y[1]),10) | |
zz[1] = sum(x) | |
for i in 2:10 | |
zz[i] = zz[i-1] + 0.01 * sum(sin.(y)) | |
end | |
sum(zz)^2 | |
end | |
function f2(x) | |
y = x | |
z = copy(y) | |
A = rand(length(x),length(x)) | |
for i in 2:10 | |
for j in 1:length(z) | |
z[i] = z[i] + sin(dot(@view(A[i,:]),x))/1000 | |
end | |
end | |
sum(z)^2 | |
end | |
function f3(x) | |
y = x | |
z = copy(y) | |
r = 1:length(x) | |
A = rand(length(x),length(x)) | |
for i in 2:10 | |
z = z + sin.(A*x)/1000 | |
end | |
# println("eval f") | |
sum(z)^2 | |
end | |
using LinearAlgebra | |
using ForwardDiff | |
using ReverseDiff | |
N = 1_000 | |
x = rand(N) | |
@time ForwardDiff.gradient(f2,rand(1000)); | |
@time ReverseDiff.gradient(f3,rand(1000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment