Public Vars

Back

iterate (clj)

(source)

function

(iterate f x)
Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of side-effects

Examples

originrose/cortex
(ns cortex.optimise.optimisers-test
  (:refer-clojure :exclude [+ - * /])
  (:require [clojure.core.matrix.operators :refer [+ - * /]]
            [clojure.test :refer :all]
            [cortex.optimise.optimisers :refer :all]
            [cortex.optimise.protocols :as cp]
            [cortex.util :refer [approx= def-]]))

(deftest protocol-extension-test
  (testing "map as optimiser"
    (is (= (->> test-optimiser-map
             (iterate #(cp/compute-parameters % [1 2 3] (or (cp/parameters %)
                                                            [0 0 0])))
             (map (juxt cp/parameters cp/get-state))
             (rest)
             (take 3))
           [[[0 0 0] {:velocity [1 2 3]}]
            [[1 2 3] {:velocity [2 4 6]}]
            [[3 6 9] {:velocity [3 6 9]}]])))
  (testing "fn as optimiser"
    (is (= (->> test-optimiser-fn
             (iterate #(cp/compute-parameters % [1 2 3] (or (cp/parameters %)
                                                            [0 0 0])))
             (map (juxt cp/parameters cp/get-state))
             (rest)
             (take 3))
           [[[1 2 3] {}]
            [[2 4 6] {}]
            [[3 6 9] {}]]))))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
  "Type annotations for the base Clojure distribution."
  #?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
  (:require [clojure.core :as cc]
            [typed.clojure :as t]
            #?(:clj [typed.ann-macros.clojure :as macros])
            #?(:clj typed.ann.clojure.jvm) ;; jvm annotations
            #?(:clj clojure.core.typed))
  #?(:clj
     (:import (clojure.lang PersistentHashSet PersistentList
                            APersistentMap #_IPersistentCollection
                            #_ITransientSet
                            IRef)
              (java.util Comparator Collection))))


cc/doall (t/All [[c :< (t/U nil t/AnySeqable)]]
                [(t/? t/AnyInteger) c :-> c])
cc/dorun [(t/? t/AnyInteger) t/AnySeqable :-> nil]
cc/iterate (t/All [x]
                  [[x :-> x] x :-> (t/NonEmptyASeq x)])
cc/memoize (t/All [x y :..]
                  [[y :.. y :-> x] :-> [y :.. y :-> x]])
cloudkj/lambda-ml
(ns lambda-ml.neural-network-test
  (:require [clojure.test :refer :all]
            [clojure.core.matrix :as m]
            [lambda-ml.core :refer :all]
            [lambda-ml.neural-network :refer :all]))

(deftest test-neural-network
  (let [data [[0 0 [0]]
              [0 1 [1]]
              [1 0 [1]]
              [1 1 [0]]]
        model (-> (make-neural-network 0.5 0.0 cross-entropy-cost 54321)
                  (add-neural-network-layer 2 sigmoid)
                  (add-neural-network-layer 3 sigmoid)
                  (add-neural-network-layer 1 sigmoid))
        fit (nth (iterate #(neural-network-fit % data) model) 5000)
        predictions (map first (neural-network-predict fit (map butlast data)))]
    (is (> 0.1 (nth predictions 0)))
    (is (< 0.9 (nth predictions 1)))
    (is (< 0.9 (nth predictions 2)))
    (is (> 0.1 (nth predictions 3)))))
findmyway/reinforcement-learning-an-introduction
;; @@
(ns rl.chapter03.grid-world
  (:require [clojure.core.matrix :as m]))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=

;; @@
(let [N 5
      discount 0.9
      world (m/zero-matrix N N)
      mesh-idx (for [i (range N) j (range N)] [i j])
      actions [[-1 0] [1 0] [0 -1] [0 1]]  ;; [:left :right :down :up]
      prob (zipmap actions (repeat 0.25))
      get-next-states (fn [idx action]
                        (case idx
                          [0 1] [4 1]
                          [0 3] [2 3]
                          (mapv + idx action)))
      get-rewards (fn [idx action]
                    (cond
                      (= [0 1] idx) 10
                      (= [0 3] idx) 5
                      (some #{(mapv + idx action)} mesh-idx) 0
                      :else -1))
      update-w1 (fn [w idx]
                  (apply + (map #(* (prob %)
                                    (+ (get-rewards idx %)
                                       (* discount (get-in w (get-next-states idx %) (get-in w idx)))))
                                actions)))
      update-w2 (fn [w idx]
                  (apply max (map #(+ (get-rewards idx %)
                                      (* discount (get-in w (get-next-states idx %) (get-in w idx))))
                                  actions)))
      iterate-fn (fn [update-f w]
                   (->> (map (partial update-f w) mesh-idx)
                        (partition (count w))
                        (mapv vec)))
      stop? (fn [[a b]] (< (m/abs (- (m/esum a) (m/esum b))) 0.0001))
      converge #(ffirst (filter stop? (partition 2 (iterate (partial iterate-fn %) world))))]
  (m/pm (converge update-w1))
  (m/pm (converge update-w2)))
findmyway/reinforcement-learning-an-introduction
;; @@
(ns rl.chapter04.grid-world
  (:require [clojure.core.matrix :as m])
  (:use [plotly-clj.core]))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=

;; @@
(let [N 4
      reward -1
      world (m/zero-matrix N N)
      states (for [i (range N) j (range N)
                   :when (not (contains? #{[0 0] [(dec N) (dec N)]} [i j]))]
               [i j])
      actions [[-1 0] [1 0] [0 -1] [0 1]]  ;; [:left :right :down :up]
      prob (zipmap actions (repeat 0.25))
      update-v (fn [w idx]
                 (apply + (map #(* (prob %)
                                   (+ reward (get-in w (mapv + % idx) (get-in w idx))))
                               actions)))
      iter-fn #(mapv vec (partition N (concat [0] (map (partial update-v %) states) [0])))
      stop? (fn [[a b]] (< (m/abs (- (m/esum a) (m/esum b))) 0.0001))
      converge #(ffirst (filter stop? (partition 2 %)))]
  (-> (plotly)
      (add-surface :z (converge (iterate iter-fn world)))
      (plot "RL-figure-4-1" :fileopt "overwrite")
      embed-url))
;; @@
;; =>
;;; {"type":"html","content":"<iframe height=\"600\" src=\"//plot.ly/~findmyway/120.embed\" width=\"800\"></iframe>","value":"pr'ed value"}
;; <=