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

grad of convert? #7

Closed
ylxdzsw opened this issue Dec 29, 2016 · 4 comments
Closed

grad of convert? #7

ylxdzsw opened this issue Dec 29, 2016 · 4 comments

Comments

@ylxdzsw
Copy link
Contributor

ylxdzsw commented Dec 29, 2016

I need to convert type of some variables, but it crashes AutoGrad.

The code I use is:

function loss(w,x,ygold)
    ypred = predict(w,x)
    ynorm = ypred .- log(sum(exp(ypred),1))
    convert(Float32, -sum(ygold .* ynorm) / size(ygold, 2))
end

I tried @primitive like @primitive convert(T,x),dy zerograd() dy or something similar but never get it work. Is there any way to define it properly, or how to hack AutoGrad so that at least convert works without breaking other functionalities?

@ylxdzsw
Copy link
Contributor Author

ylxdzsw commented Dec 29, 2016

finally I got it work:

function zerograd() 0 end
@zerograd zerograd()

import Base.convert
for t in (Float32, Float64)
    @eval @primitive convert(T::Type{$t},x),dy zerograd() T(dy)
end

Wondering is there better solutions.

@denizyuret
Copy link
Owner

@ylxdzsw Your solution works, here are some suggestions for improvement:

  1. Read the comments in src/core.jl (section 6) to see how gradients are actually defined.
  2. Your case is closest to 6.4.
  3. In your solution, instead of zerograd() you can simply use 0.
  4. Instead of T(dy), using convert(T,dy) may be better.

@denizyuret
Copy link
Owner

I experimented with defining a general convert gradient in AutoGrad. Unfortunately Julia keeps calling convert in all sorts of unexpected places and so far I could not get it to work without breaking AutoGrad. However defining it for specific types like you have done is ok. There is an example for KnetArray / Array conversion in the latest Knet release. It shows an alternative to your solution that does not use the @primitive macro and leaves the gradient wrt first arg undefined.

@ylxdzsw
Copy link
Contributor Author

ylxdzsw commented Feb 24, 2017

Thanks, that solution looks nice.

@ylxdzsw ylxdzsw closed this as completed Mar 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants