themetaschemer / malt

Machine Learning Toolkit accompanying The Little Learner by Daniel P. Friedman and Anurag Mendhekar
MIT License
212 stars 25 forks source link

`class=` in interlude VI doesn't work in neither `malt` nor `malt/learner` mode #18

Closed yangchenyun closed 1 year ago

yangchenyun commented 1 year ago

Use malt:

#lang racket
(require malt)
(require malt/examples/iris)

;; Use argmax to detect class
(define class=-1
  (lambda (t u)
    (cond
      ((= (argmax-1 u) (argmax-1 u)) 1.0)
      (else 0.0))))

(define class= (ext2 class=-1 1 1)) ;; =>  class=-1: arity mismatch; expecte 2, given 1

(define class=
  (ext2-ρ class=-1 1 1)) ;; definition form executed successfully, but
(class= iris-test-ys iris-test-ys) ;; => class=-1: arity mismatch; expecte 2, given 9.
;; 9 is a strange number, I suspect it is given the flat tensor implementation internals instead of the values?

Use malt/learner:

#lang racket
(require malt/learner)
(require malt/examples/iris)

;; Use argmax to detect class
(define class=-1
  (lambda (t u)
    (cond
      ((= (argmax-1 u) (argmax-1 u)) 1.0)
      (else 0.0))))

(define class=
  (ext2 class=-1 1 1)) ;; works as expected

(class= iris-test-ys iris-test-ys) ;; failed

; vector-ref: contract violation
;   expected: vector?
;   given: #<procedure:flat>
; Context (plain; to see better errortrace context, re-run with C-u prefix):
;   /Users/steveyang/Library/Racket/8.8/pkgs/malt/learner/tensors/D-extend.rkt:50:2 body of top-level
;   /Users/steveyang/Library/Racket/8.8/pkgs/malt/learner/tensors/D-extend.rkt:23:4
;   /Applications/Racket v8.8/collects/racket/vector.rkt:62:0 vector-map/update
themetaschemer commented 1 year ago

Thanks for reporting. I'll fix it for flat-tensor. For a temporary workaround, please switch completely to the learner representation.

yangchenyun commented 1 year ago

@themetaschemer thanks for your timely response. I am eventually getting to the part which implements tensor!

I also couldn't find a place where operations like +/-/*... are replaced by the one defined in the malt collection, could you point me to the right place or some Racket documentation to see how this happens?

themetaschemer commented 1 year ago

The operators get renamed at provide. See, for example, flat-tensors.rkt. There is a provide form called rename-out that allows functions to be renamed at export.

themetaschemer commented 1 year ago

@yangchenyun Thanks for your patience. I have just pushed a new commit c8220b5 where I have set the default implementation to be learner, and added some other mechanisms to switch implementations more easily (the README is correspondingly updated). This should avoid future problems with trying to use ext1 and ext2 examples from the book.