DynamicDiff within template_spec help #847
-
Hi, I am struggling to pass on custom vectors to the D() parameter. I have been calculating the first derivate with respect to various variables with this code within the template_spec macro: grad_column = D(f, 1)(x1, x2, x3, x4, x5, x6).x As I know the physical limits of each variable that are possible through domain knowledge, I can pass through a much smaller set of values instead the actual database, I have tried to make a bunch of vectors to pass through instead: Example code for all different combinations for the 6 variables (should be 2,048 long) n = 4 #num steps
min_val = -6 #set example range for standardised data of sd 1, mean 0
max_val = 6
gap = (max_val - min_val) / (n - 1)
vals = [min_val + gap*(i - 1) for i in 1:n] #creates vector [-6, -2, 2, 6]
x6_vals = [0, 1] #binary vector
combinations = collect(Iterators.product(vals, vals, vals, vals, vals, g_vals))
x1_vec = [combo[1] for combo in combinations] #example vectors made
x2_vec = [combo[2] for combo in combinations]
x3_vec = [combo[3] for combo in combinations]
x4_vec = [combo[4] for combo in combinations]
x5_vec = [combo[5] for combo in combinations]
x6_vec = [combo[6] for combo in combinations]
grad_column = D(f,1)(x1_vec, x2_vec, x3_vec, x4_vec, x5_vec, x6_vec) However, this doesn't seem to work. I even tried to make each one a ValidVector, e.g. ERROR: MethodError: no method matching (::ComposableExpression{Float32, DynamicExpressions.NodeModule.Node{…}, @NamedTuple{…}})(::Array{Float32, 6}, ::Array{Float32, 6}, ::Array{Float32, 6}, ::Array{Float32, 6}, ::Array{Float32, 6}, ::Array{Float32, 6}) Thank you in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Could you share the full code for this? Just so I can try to reproduce it. Edit: actually from the error message alone I think I see the issue. It says |
Beta Was this translation helpful? Give feedback.
-
Ok I think the issue is the use of - combinations = collect(Iterators.product(vals, vals, vals, vals, vals, x6_vals))
+ combinations = collect(Iterators.product(vals, vals, vals, vals, vals, x6_vals))[:] And they should be wrapped in |
Beta Was this translation helpful? Give feedback.
-
Thank you, that did the trick! g_vals was a transcription error from other code. function generate_val(min_val, max_val, n) #n=number of steps
gap = (max_val - min_val) / (n - 1)
return [min_val + gap*(i - 1) for i in 1:n]
end
x1_val = generate_val(-8f0, 10f0, 4) #example ranges
x2_val = generate_val(-8f0, 6f0, 4)
x3_val = generate_val(-6f0, 9f0, 4)
x4_val = generate_val(-5f0, 5f0, 3)
x5_val = generate_val(-5f0, 5f0, 3)
x6_val = [0f0,1f0] #binary var
combinations = collect(Iterators.product(x1_val, x2_val, x3_val, x4_val, x5_val, x6_val))[:]
x1_vec = ValidVector([combo[1] for combo in combinations],true)
x2_vec = ValidVector([combo[2] for combo in combinations],true)
x3_vec = ValidVector([combo[3] for combo in combinations],true)
x4_vec = ValidVector([combo[4] for combo in combinations],true)
x5_vec = ValidVector([combo[5] for combo in combinations],true)
x6_vec = ValidVector([combo[6] for combo in combinations],true)
expression_spec = @template_spec(expressions=(f,)) do x1, x2, x3, x4, x5, x6
o = f(x1, x2, x3, x4, x5, x6)
length(x1.x) == 50 && return o #if batch case, do not check monotonicity to speed up search
o.valid || return o
for i in 1:2 #(check monotonicity for first two variables)
grad_column = D(f, i)(x1_vec, x2_vec, x3_vec, x4_vec, x5_vec, x6_vec).x
any(>(0.0f0), grad_column) && return #if monotonicity breached return this
end
return o
end |
Beta Was this translation helpful? Give feedback.
Ok I think the issue is the use of
Iterators.product
. You should be able to fix it by putting a[:]
on the end to just get a single flat list of all combinations:And they should be wrapped in
ValidVector
before input