uncomplicate / neanderthal

Fast Clojure Matrix Library
http://neanderthal.uncomplicate.org
Eclipse Public License 1.0
1.07k stars 56 forks source link

Different Outputs Using Let and With-Release #72

Closed CrosleyZack closed 5 years ago

CrosleyZack commented 5 years ago

I am pretty new to the neanderthal library, so I may just be making simple mistakes. I ran across a few things that seem like errors to me while following the Deep Learning in Clojure tutorial (https://dragan.rocks/articles/19/Deep-Learning-in-Clojure-From-Scratch-to-GPU-1-Representing-Layers-and-Connections) and I wanted to ensure were known, assuming they were errors.

  1. let and with-release appear to result in different values, for an identical function.
(defn sample-neuron-layer-with-let
  []
  (let [x  (dv 0.3 0.9)
        w1 (dge 4 2 [0.3 0.6 ; 4 x 2 matrix
                     0.1 2.0
                     0.9 3.7
                     0.0 1.0]
                {:layout :row})
        h1 (dv 4)] ; 4 item vector of zeros
    (mv! w1 x h1))) ; => [ 0.63 1.83 3.60 0.90 ]

(defn sample-neuron-layer-with-release
  []
  (with-release [x  (dv 0.3 0.9)
                 w1 (dge 4 2 [0.3 0.6 ; 4 x 2 matrix
                              0.1 2.0
                              0.9 3.7
                              0.0 1.0]
                         {:layout :row})
                 h1 (dv 4)] ; 4 item vector of zeros
    (mv! w1 feature-vector h1))) ; => [ 0.0 0.0 3.60 0.90 ]
  1. Printing the result of mv! seems to show different values in the vector vs just returning it.
(defn sample-neuron-layer-with-release
  []
  (with-release [x  (dv 0.3 0.9)
                 w1 (dge 4 2 [0.3 0.6 ; 4 x 2 matrix
                              0.1 2.0
                              0.9 3.7
                              0.0 1.0]
                         {:layout :row})
                 h1 (dv 4)] ; 4 item vector of zeros
    (mv! w1 feature-vector h1))) ; => [ 0.0 0.0 3.60 0.90 ]

(defn sample-neuron-layer-with-release-print
  []
  (with-release [x  (dv 0.3 0.9)
                 w1 (dge 4 2 [0.3 0.6 ; 4 x 2 matrix
                              0.1 2.0
                              0.9 3.7
                              0.0 1.0]
                         {:layout :row})
                 h1 (dv 4)] ; 4 item vector of zeros
    (print (mv! w1 feature-vector h1)))) ; => [ 0.63    1.83    3.60    0.90 ]

Again, very new to the library so I apologize if I am misinterpreting intended behavior. I am running Manjaro Linux, Leiningen 2.9.1 on Java 1.8.0_222 OpenJDK 64-Bit Server VM.

blueberry commented 5 years ago

Since the REPL prints the object by default only after they are returned from the let or with-release block, you get the garbled results of the object that has been released. Some numbers may still be correct since everything happens quickly, but assume that the whole structure is erased. When you println explicitly, it is done before the release, so the result is as expected.