Giter VIP home page Giter VIP logo

backwardslinalg.jl's Introduction

Backward functions for Linear Algebra

Build Status Codecov

#f03c15 This project is still in progress ...

Backward functions for linear algebras, with GPU support. It is currently ported to Zygote.jl for testing, but these porting codes will be moved to other places (like merging them to Zygote.jl) in the future.

Why we need BackwardsLinalg.jl?

Not only in Julia, but also in well known machine learning packages in python like pytorch, one can hardly find a numerical stable implementations of linear algebra function. This missing piece is crutial to autodiff applications in tensor networks algorithms.

Table of Supported Functions

Note: it will change the default behavior, we are considering not changing the output type (SVD, QR) latter when Zygote is stronger.

  • svd and rsvd (randomized SVD)
  • qr
  • cholesky # Nabla.jl
  • powermethod # we need fixed point methods, trying hard ...
  • eigen # linear BP paper, only symmetric case considered
  • lq # similar to qr
  • pfaffian # find it nowhere, lol

For logdet, det and tr, people can find it in ChainRules.jl and Nabla.jl.

Derivation of adjoint backward functions could be found here.

How to Use

It currently ports into Zygote.jl

using Zygote, BackwardsLinalg

function loss(A)
    M, N = size(A)
    U, S, V = svd(A)
    psi = U[:,1]
    H = randn(ComplexF64, M, M)
    H+=H'
    real(psi'*H*psi)[]
end

a = randn(ComplexF64, 4, 6)
g = loss'(a)

Try something interesting (the backward of TRG code, TensorOperations.jl (as well as patch Jutho/TensorOperations.jl#59) is required.)

julia test/trg.py

backwardslinalg.jl's People

Contributors

giggleliu avatar hamletwanttocode avatar juliatagbot avatar roger-luo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

backwardslinalg.jl's Issues

Merge into ChainRules?

It's great to see someone taking AD + linear algebra seriously. @GiggleLiu would you be open to merging this stuff into ChainRules at some point in the not too distant future? It would be a great help!

Working with QR gradients

Hi @GiggleLiu,

I was wondering about this use case

using Zygote
using BackwardsLinalg
gradient(x -> sum(qr(x).Q), rand(3,3)) 

but this gives an error

julia> gradient(x -> qr(x).Q |> sum, rand(3,3))
ERROR: MethodError: no method matching getindex(::Tuple{Array{Float64,2},Array{Float64,2}}, ::Symbol)
Closest candidates are:
  getindex(::Tuple, ::Int64) at tuple.jl:24
  getindex(::Tuple, ::Real) at tuple.jl:25
  getindex(::Tuple, ::AbstractUnitRange{#s66} where #s66<:Real) at range.jl:288
  ...
Stacktrace:
 [1] adjoint at /home/meach/.julia/packages/Zygote/fuj2C/src/lib/lib.jl:81 [inlined]
 [2] _forward at /home/meach/.julia/packages/Zygote/fuj2C/src/lib/grad.jl:44 [inlined]
 [3] _forward(::Zygote.Context, ::typeof(Zygote.literal_getproperty), ::Tuple{Array{Float64,2},Array{Float64,2}}, ::Val{:Q}) at /home/meach/.julia/packages/Zygote/fuj2C/src/lib/lib.jl:174
 [4] #19 at ./REPL[14]:1 [inlined]
 [5] _forward(::Zygote.Context, ::getfield(Main, Symbol("##19#20")), ::Array{Float64,2}) at /home/meach/.julia/packages/Zygote/fuj2C/src/compiler/interface2.jl:0
 [6] _forward(::Function, ::Array{Float64,2}) at /home/meach/.julia/packages/Zygote/fuj2C/src/compiler/interface.jl:31
 [7] forward(::Function, ::Array{Float64,2}) at /home/meach/.julia/packages/Zygote/fuj2C/src/compiler/interface.jl:37
 [8] gradient(::Function, ::Array{Float64,2}) at /home/meach/.julia/packages/Zygote/fuj2C/src/compiler/interface.jl:46
 [9] top-level scope at REPL[14]:1

Is there any way to fix this?

References

lapack exception (2) backward qr diff

While random matrices work, I get lapack exceptions in trtrs when using it in my application.
fpeps.zip

`using Zygote,BackwardsLinalg,fpeps

width = 4;height = 4;
D = 2;d = 2;chi = 5;

peps = Array{Array{ComplexF64,5},2}(undef,width,height);
for i = 1:width
for j = 1:height
peps[i,j] = rand(ComplexF64,D,D,D,D,d);
end
end

boundaries = fpeps.gen_boundaries(peps,chi);
n1 = boundaries[1][1]; n2 = boundaries[1][2];

fun(a) = real(sum(sum.(fpeps.north_vomps(n1,peps[1,:],a))))
fun'(n2)`

Is there anything I should know about numerical stability problems (assuming the lapack error means that trtrs didn't converge). How can I go about debugging this further? The matrix passed to trtrs did became spectacularly quite ill-conditioned in my example... I can try to reduce this further to a smaller minimal example?

If I set the parameters to make sure that we are effectively working with 1x1 matrices then I get errors later on with zygote (I'm assuming it's unrelated to this package but I'm not sure as the stacktrace effectively says nothing).

QR with pivoting?

Does this support qr! with pivoting for complex, non-square matricies?

using BackwardsLinalg
f(x) = qr!(x, val(true)).Q
A = rand(4,8) .+ 1.5*im
f'(A)

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.