Comments (7)
Your code is probably not generic enough. Read about generic programming in Julia. If you post an example of a function that gives this error I can help.
from reversediff.jl.
If this issue is resolved, please close the issue. If not, please post a minimal example to help.
from reversediff.jl.
Posting a minimal example right now
from reversediff.jl.
Here is the Python code:
import numpy as np
from julia.api import Julia
jl = Julia(compiled_modules=False)
from julia import Main
a = np.random.rand(408);
b = 8
c = 34
d = 41
l = np.random.rand(41,34)
m = np.random.rand(41,34)
p=1
q=1
r = np.random.rand(41,34)
s = np.random.rand(34)
t = np.random.rand(41,8)
u = np.random.rand(41,34)
v = np.random.rand(34)
w = 1
;
Main.a = a
Main.b = b
Main.c = c
Main.d = d
Main.l = l
Main.m = m
Main.p = p
Main.q = q
Main.r = r
Main.s = s
Main.t = t
Main.u = u
Main.v = v
Main.w = w
%%time
jl.eval('include("Dummy Julia v2.jl")')
%%time
jl.eval('calc_sse(a)')
%%time
jl.eval('optimization(a)')
Here is the Julia code:
import Pkg
Pkg.add("Nonconvex")
using AbstractDifferentiation, ReverseDiff
backend = AbstractDifferentiation.ReverseDiffBackend()
using Nonconvex
Nonconvex.@load NLopt
function calc_sse(a)
f = a[1:c]
g= a[c+1:2*c]
h= a[2*c+1:3*c]
k= a[3*c+1:4*c]
n = rand(b,c)
o = zeros((d, c))
mf = 1.0./(1.0 .- (r * f))
fi = ((t.*mf)*n).*u
qt = g
for e in 1:d
for j in 1:c
if l[e,j] != 0.0
o[e,j] = qt[j]+(1-n[j])*(fi[e,j]+v[j])
qt[j] = o[e,j]
end
end
end
diff = sum((o .- l).^2, dims=1) ./transpose(s)
sse = sum(diff)/c
return sse
end
function optimization(a)
model = Model(calc_sse)
addvar!(model, zeros(408), zeros(408).+1000, init=a)
alg = NLoptAlg(Symbol(:LD_MMA))
options = NLoptOptions(ftol_rel = 1.0)
ad_model = abstractdiffy(model,backend)
result = optimize(ad_model, alg, a, options = options)
return result.minimum, result.minimizer, result
end
from reversediff.jl.
Here is the error I am getting:
JuliaError: Exception 'ArgumentError: Converting an instance of ReverseDiff.TrackedReal{Float64, Float64, Nothing} to Float64 is not defined. Please use `ReverseDiff.value` instead.' occurred while calling julia code:
optimization(a)
from reversediff.jl.
o = zeros((d, c))
The above line creates a vector of Float64. You need to do "generic programming" by using the element type of a
as the element type of o
. ReverseDiff doesn't use Float64 but TrackedReal numbers so your code must be generic enough for it to be differentiated by ReverseDiff.
o = zeros(eltype(a), d, c)
from reversediff.jl.
Awesome, my solution to the problem was the following line:
o = zeros(Real,d,c)
Now the code is working
from reversediff.jl.
Related Issues (20)
- [Feature Request] To return function evaluation results along with the computed gradient HOT 3
- Pullback function HOT 2
- Support for sparse TrackedArray
- ChainRulesCore projection & ReverseDiff.TrackedArray
- Interference with Distributed.jl on Windows HOT 23
- Missing Promotion Rule
- Custom `rrule` not working with ReverseDiff HOT 1
- Hypothetical: ReverseDiff or Analytical Derivative
- Get a error when calculating the gradient for LSTM
- Error when using scalar vs. vector to operate on tracked inupt HOT 1
- Record `Broadcast.broadcasted` instead of `Broadcast.broadcast`
- MethodError: ReverseDiff.TrackedReal ... is ambiguous.
- double free crash with multi-threaded code only when using multiple threads
- @grad_from_chainrules macro fails when using multi-output functions HOT 2
- ReverseDiff documentation shows issue that has been fixed? Nested differentiation of a closure? HOT 1
- `MethodError: *(::Diagonal, ::ReverseDiff.TrackedArray)` is ambiguous.
- `@grad_from_chainrules` hygiene: cannot use custom types in method signature HOT 3
- Define `typemin` for tracked reals.
- ReverseDiff defines a huge number of methods. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reversediff.jl.