Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot take derrivative of a scalar function #410

Closed
ekinakyurek opened this issue Dec 7, 2018 · 2 comments
Closed

cannot take derrivative of a scalar function #410

ekinakyurek opened this issue Dec 7, 2018 · 2 comments
Labels

Comments

@ekinakyurek
Copy link
Collaborator

ekinakyurek commented Dec 7, 2018

I have strange issue, not sure it is a bug. I am sure that loss is a scalar function.

using Knet,LinearAlgebra
h(x) = exp(-x); h′(x,y) = -y
𝓁(x,y) = sum(abs2,x-y)/2
function neural_net(mparams, input; h=h, h′=h′, N=length(mparams))
    δ = [];
    X = Any[input];
    for i=1:N
        x = sum(mparams[i] .* [X[i],1])
        y = h.(x) 
        push!(δ, h′.(x,y))
        push!(X,y)
    end
    return X,δ
end
mparams =[[randn(),randn()] for i=1:3]
P = Param(mparams)
loss(P,x,y)= 𝓁(neural_net(P,x)[1][end],y)
x,y=randn(),randn()
J = @diff loss(P,x,y)
julia> J = @diff loss(P,x,y)
ERROR: Only scalar valued functions supported.
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] #differentiate#1(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::Function) at /kuacc/users/eakyurek13/.julia/packages/AutoGrad/eAmjh/src/core.jl:61
 [3] differentiate(::Function) at /kuacc/users/eakyurek13/.julia/packages/AutoGrad/eAmjh/src/core.jl:45
 [4] top-level scope at none:0
julia> loss(P,x,y)
0.0036112795041667906
@denizyuret
Copy link
Owner

In a @diff context, the return value seems to have multiple embedded Results:
R(R(R(8.982629056671427e32)))
and value(ans) does not strip all of them, therefore is not recognized as a Number. Need to see why this is happening, whether it is a valid problem and if so what is the best way to solve it.

@denizyuret
Copy link
Owner

Since this is an AutoGrad problem, I am moving it to denizyuret/AutoGrad.jl#106 and closing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants