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

Added kron and swap to Algodiff operations #512

Merged
merged 4 commits into from
Mar 27, 2020
Merged

Conversation

tachukao
Copy link
Member

The main changes in this PR are:

  1. can now do Ndarray transpose in Algodiff, by exposing the ?axis argument
  2. use transpose to implement swap in Algodiff
  3. implement kron product using transpose and matrix product in Algodiff (implementation thanks to @ghennequin )

@ryanrhymes ryanrhymes added this to In Progress in owl development via automation Mar 25, 2020
@jzstark
Copy link
Collaborator

jzstark commented Mar 25, 2020

So I follow the implementation of kron to build a Ndarray version of it:

module N = Dense.Ndarray.S
module M = Dense.Matrix.S

let kron a b =
  let na, ma =
    let s = N.shape a in
    s.(0), s.(1)
  in
  let nb, mb =
    let s = N.shape b in
    s.(0), s.(1)
  in
  let a = N.reshape a [| -1; 1 |] in
  let b = N.reshape a [| 1; -1 |] in
  let c = M.dot a b in
  let c = N.reshape c [| na; ma; nb; mb |] in
  let c = N.transpose ~axis:[| 0; 2; 1; 3 |] c in
  N.reshape c [| Stdlib.(na * nb); Stdlib.(ma * mb) |]

The only difference is that I replace *@ with M.dot. Then I try the test in unit_dense_matrix.ml:

let a = N.sequential [|2;2|];;
let b = Dense.Matrix.S.eye 2;;

Looks like that kron and M.kron give different results:

kron b a ;;

- : N.arr =

   C0 C1 C2 C3
R0  1  0  0  0
R1  0  1  0  0
R2  0  0  1  0
R3  0  0  0  1
M.kron b a 

- : Dense.Matrix.S.mat =

   C0 C1 C2 C3
R0  0  0  1  0
R1  0  0  0  1
R2  2  0  3  0
R3  0  2  0  3

Perhaps need to check the result in Algodiff module to see if I got anything wrong in using the AD implementation to build the Ndarray version kron.

@tachukao
Copy link
Member Author

thanks @jzstark! the latest commit should fix this bug

@jzstark
Copy link
Collaborator

jzstark commented Mar 25, 2020

Cool! Thanks for the quick fix. The rest looks good to me.

@ryanrhymes ryanrhymes merged commit 6a218e9 into owlbarn:master Mar 27, 2020
owl development automation moved this from In Progress to Done (2020) Mar 27, 2020
mseri added a commit to mseri/opam-repository that referenced this pull request Oct 4, 2020
CHANGES:

* various documentation improvements (thanks @pveber, @UnixJunkie, @Fourchaux)
* Fix use of access operators (owlbarn/owl#543)
* Upgrade to ocamlformat 0.15.0 (thanks @gpetiot owlbarn/owl#535)
* keep_dims option (owlbarn/owl#531)
* stats: fix infinite loop in ecdf
* Use Fun.protect to ensure all file descriptors are being closed
* owl_ndarray_maths: improve user experience in case of errors
* owl_io: close file descriptors also in case of errors
* owl_dense_ndarray_generic: fix error on printing 0-ary arrays
* fixed bug in sub forward mode (owlbarn/owl#533)
* Add stack to Algodiff (owlbarn/owl#528)
* added log_sum_exp to Ndarray and Algodiff (owlbarn/owl#527)
* added single-precision and double-precision Bessel functions to Ndarray  (owlbarn/owl#526)
* Fixes owlbarn/owl#518 by introducing another `/` to resolve data directory (@jotterbach owlbarn/owl#519)
* Graph Slice node (resolves owlbarn/owl#483) (@mreppen owlbarn/owl#517)
* Graph subnetwork: Multiple outputs (@mreppen owlbarn/owl#515)
* Added kron and swap to Algodiff operations (owlbarn/owl#512)
* various other small fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
owl development
  
Done (2020)
Development

Successfully merging this pull request may close these issues.

None yet

3 participants