Giter Site home page Giter Site logo

grad of `convert`? about autograd.jl HOT 4 CLOSED

denizyuret avatar denizyuret commented on May 18, 2024
grad of `convert`?

from autograd.jl.

Comments (4)

ylxdzsw avatar ylxdzsw commented on May 18, 2024

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.

from autograd.jl.

denizyuret avatar denizyuret commented on May 18, 2024

@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.

from autograd.jl.

denizyuret avatar denizyuret commented on May 18, 2024

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.

from autograd.jl.

ylxdzsw avatar ylxdzsw commented on May 18, 2024

Thanks, that solution looks nice.

from autograd.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.