I'm trying to replicate the following example from the mxnet main docs with mxnet.jl in Julia:
A = Variable('A')
B = Variable('B')
C = B * A
D = C + Constant(1)
# get gradient node.
gA, gB = D.grad(wrt=[A, B])
# compiles the gradient function.
f = compile([gA, gB])
grad_a, grad_b = f(A=np.ones(10), B=np.ones(10)*2)
The example shows how to autodiff a symoblic expression and obtain its gradients.
What is the equivalent in mxnet.jl (latest version 2016-03-07)?
Code in MXNet.jl/src/symbolic-node.jl may be helpful for you to find answers.
I am not familiar with this package.
Here is my Guess:
A = mx.Variable("A")
B = mx.Variable("B")
C = B .* A
D = C + 1
mx.normalized_gradient may be the solution to the remaining part if exists.
Related
I want to solve this integration. With the upper limit phi ranging from 1-2.4. I want to receive answers at every increment of phi.
The code that I have pasted below is only outputting the answer when phi=2.4. How can I fix this? I am new to Julia and have basic knowledge of programming which is why i think i am not doing this correctly.
using Ranges
for ϕ in range(start=1, stop=2.4, step=0.2)
using QuadGK
f(ϕ) = ϕ*(cos(ϕ)/sin(ϕ))
a = 0
b = ϕ
I,est = quadgk(f, a, b, rtol=1e-8)
end
println(I, est)
Print should be inside the for loop, so you print all the iterations ...
using Ranges
using QuadGK
f(ϕ) = ϕ*(cos(ϕ)/sin(ϕ))
for ϕ in range(start=1, stop=2.4, step=0.2)
a = 0
b = ϕ
I,est = quadgk(f, a, b, rtol=1e-8)
println(I, est)
end
So I am writing a database that contains the rule waywest(X,Y). waywest takes 2 buildings that are along a street and returns where or not the building X is more than one building west of building Y. I have:
waywest(X,Y) :- not(west(X,Y)).
waywest(X,Y) :- not(west(X,Z)) , waywest(Z,Y).
This is a method that recursively loops through using the fact west(X,Y), where building X is west of building Y. I keep getting this existence error and debugging doesn't work either.
The way you programmed it, you can only use it to disprove facts, according to the documentation of not in swi-prolog:
not(:Goal)
True if Goal cannot be proven. Retained for compatibility only. New code should use +/1.
Moreover I would discourage the use of NOT at all at the beginning of Prolog, because it's not (sic!) working as you might intend/expect in the beginning.
If I understood you problem correctly, this should also do the trick:
west(a,b).
west(b,c).
west(c,d).
west(d,e).
waywest(X,Z) :-
west(X,Y),
west(Y,Z).
waywest(X,Z) :-
west(X,Y),
waywest(Y,Z).
If we now check it we get as expected:
?- waywest(X,Y).
X = a, Y = c ;
X = b, Y = d ;
X = c, Y = e ;
X = a, Y = d ;
X = a, Y = e ;
X = b, Y = e ;
false.
I have a Julia function in a file. Let's say it is the below. Now I want to pass arguments into this function. I tried doing
julia filename.jl randmatstat(5)
but this gives an error that '(' token is unexpected. Not sure what the solution would be. I am also a little torn on if there is a main function / how to write a full solution using Julia. For example what is the starting / entry point of a Julia Program?
function randmatstat(t)
n = 5
v = zeros(t)
w = zeros(t)
for i = 1:t
a = randn(n,n)
b = randn(n,n)
c = randn(n,n)
d = randn(n,n)
P = [a b c d]
Q = [a b; c d]
v[i] = trace((P.'*P)^4)
w[i] = trace((Q.'*Q)^4)
end
std(v)/mean(v), std(w)/mean(w)
end
Julia doesn't have an "entry point" as such.
When you call julia myscript.jl from the terminal, you're essentially asking julia to execute the script and exit. As such, it needs to be a script. If all you have in your script is a function definition, then it won't do much unless you later call that function from your script.
As for arguments, if you call julia myscript.jl 1 2 3 4, all the remaining arguments (i.e. in this case, 1, 2, 3 and 4) become an array of strings with the special name ARGS. You can use this special variable to access the input arguments.
e.g. if you have a julia script which simply says:
# in julia mytest.jl
show(ARGS)
Then calling this from the linux terminal will give this result:
<bashprompt> $ julia mytest.jl 1 two "three and four"
UTF8String["1","two","three and four"]
EDIT: So, from what I understand from your program, you probably want to do something like this (note: in julia, the function needs to be defined before it's called).
# in file myscript.jl
function randmatstat(t)
n = 5
v = zeros(t)
w = zeros(t)
for i = 1:t
a = randn(n,n)
b = randn(n,n)
c = randn(n,n)
d = randn(n,n)
P = [a b c d]
Q = [a b; c d]
v[i] = trace((P.'*P)^4)
w[i] = trace((Q.'*Q)^4)
end
std(v)/mean(v), std(w)/mean(w)
end
t = parse(Int64, ARGS[1])
(a,b) = randmatstat(t)
print("a is $a, and b is $b\n")
And then call this from your linux terminal like so:
julia myscript.jl 5
You can try running like so:
julia -L filename.jl -E 'randmatstat(5)'
Add the following to your Julia file:
### original file
function randmatstat...
...
end
### new stuff
if length(ARGS)>0
ret = eval(parse(join(ARGS," ")))
end
println(ret)
Now, you can run:
julia filename.jl "randmatstat(5)"
As attempted originally. Note the additional quotes added to make sure the parenthesis don't mess up the command.
Explanation: The ARGS variable is defined by Julia to hold the parameters to the command running the file. Since Julia is an interpreter, we can join these parameters to a string, parse it as Julia code, run it and print the result (the code corresponds to this description).
I asked a question a few days ago here and got an answer that seems like it would work- it involves using linsolve to find the solutions to a system of equations that are all modulo p, where p is a non-prime integer.
However, when I try to run the commands from the provided answer, or the linsolve help page, I get an error saying linsolve doesn't support arguments of type 'sym'. Is using linsolve with sym variables only possible in R2013b? I've also tried it with my school's copy, which is R2012b. Here is the code I'm attempting to execute (from the answer at the above link):
A = [0 5 4 1;1 7 0 2;8 1 0 2;10 5 1 0];
b = [2946321;5851213;2563617;10670279];
s = mod(linsolve(sym(A),sym(b)),8)
And the output is:
??? Undefined function or method linsolve' for input arguments of type 'sym'.
I've also tried to use the function solve for this, however even if I construct the equations represented by the matrices A and b above, I'm having issues. Here's what I'm attempting:
syms x y z q;
solve(5*y + 4*z + q == 2946321, x + 7*y + 2*q == 5851213, 8*x + y + 2*q == 2563617, 10*x + 5*y + z == 10670279,x,y,z,q)
And the output is:
??? Error using ==> char
Conversion to char from logical is not possible.
Error in ==> solve>getEqns at 169
vc = char(v);
Error in ==> solve at 67
[eqns,vars] = getEqns(varargin{:});
Am I using solve wrong? Should I just try to execute my code in R2013b to use linsolve with symbolic data types?
The Symbolic Math toolbox math toolbox has changed a lot (for the better) over the years. You might not have sym/linsolve, but does this work?:
s = mod(sym(A)\sym(b),8)
That will basically do the same thing. sym/linsolve just does some extra input checking and and rank calculation to mirror the capabilities of linsolve.
You're using solve correctly for current versions, but it looks like R2010b may not understand the == operator (sym/eq) in this context. You can use the old string format to specify your equations:
eqs = {'5*y + 4*z + q = 2946321',...
'x + 7*y + 2*q = 5851213',...
'8*x + y + 2*q = 2563617',...
'10*x + 5*y + z = 10670279'};
vars = {'x','y','z','q'};
[x,y,z,q] = solve(eqs{:},vars{:})
My highschool years are far behind and I'm having trouble remembering how to isolate the "a" variable in this equation :
ln(20) = ln(a) + 3 * b
b = 0.4605
From this website (last section, at the very bottom http://mathonweb.com/help_ebook/html/expoapps_3.htm) : "Back-substituting b into either of the previous equations gives ln (a) = 4.377, and anti-logging gives a = 79.6"
I know this is fairly simple and I just need a quick help!
Thanks a lot for your time
Joel
Not a programming question, but
log(20) = log(a) + 3
log(20) - log(a) = 3
log(20/a) = 3
20/a = 10^3
20/(10^3) = a
// EDIT
You're right, there was a mistake, it's fixed now. Also, b = - 0.4605
Here's the original method, using e instead of log:
20/(e^(3*(-0.4605))) = 79.6173691