/
base.clj
166 lines (140 loc) · 6.1 KB
/
base.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
(ns tech.libs.daal.regression.base
(:require [tech.ml.protocols.system :as ml-system]
[tech.ml.registry :as registry]
[tech.ml.dataset :as ds]
[tech.ml.dataset.column :as ds-col]
[tech.ml.dataset.options :as ds-opts]
[tech.ml.model :as ml-model]
[tech.datatype :as dtype]
;;Ensure protocols are loaded
[tech.libs.daal.numeric-table :as numeric-table]
[clojure.core.matrix :as m]
[tech.libs.daal.context :as daal-ctx]
[tech.compute.tensor :as ct])
(:import [com.intel.daal.data_management.data
HomogenNumericTable NumericTable SerializableBase]
[com.intel.daal.services DaalContext]
[com.intel.daal.algorithms Model]
[com.intel.daal.data_management.data DataFeatureUtils$FeatureType]))
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(defn create-homogeneous-table
^HomogenNumericTable [table-data n-rows n-columns metadata-list ^DaalContext daal-context]
(let [n-rows (long n-rows)
n-cols (long n-columns)]
(when-not (= (* n-rows n-cols)
(dtype/ecount table-data))
(throw (ex-info "Table data ecount mismatch"
{:expected-ecount (* n-rows n-cols)
:actual-ecount (dtype/ecount table-data)})))
(let [^HomogenNumericTable retval
(case (dtype/get-datatype table-data)
:float64 (HomogenNumericTable. daal-context ^doubles table-data n-cols n-rows)
:float32 (HomogenNumericTable. daal-context ^floats table-data n-cols n-rows))
table-dict (.getDictionary retval)]
;;It is important for certain algorithms for the input data to be marked as
;;categorical
(doseq [[categorical-idx] (->> metadata-list
(map-indexed vector)
(filter #(contains? (second %) :categorical?))
(map first))]
(.setFeature table-dict (.getNumericType retval) (int categorical-idx)
DataFeatureUtils$FeatureType/DAAL_CATEGORICAL))
retval)))
(defn dataset->metadata-map
[dataset]
(->> (ds/columns dataset)
(map (fn [col]
[(ds-col/column-name col)
(ds-col/metadata col)]))
(into {})))
(defn create-tables
[dataset feature-row-map row-key-seq datatype daal-context]
(let [metadata-map (dataset->metadata-map dataset)
row-major-data (ds/->row-major dataset feature-row-map {:datatype datatype})
first-item (first row-major-data)
[n-cols n-rows] (m/shape dataset)]
(->> row-key-seq
(mapv (fn [row-key]
(let [first-row-data (get first-item row-key)
item-ecount (dtype/ecount first-row-data)
metadata-seq (mapv #(get metadata-map %) (get feature-row-map row-key))]
(-> (dtype/copy-raw->item! (map row-key row-major-data)
(dtype/make-array-of-type datatype (* (long n-rows) item-ecount))
0)
first
(create-homogeneous-table n-rows item-ecount metadata-seq daal-context))))))))
(defonce sub-systems (atom {}))
(defn register-regression-system!
[sub-type gs-opts train-fn predict-fn]
(swap! sub-systems assoc sub-type
{:sub-type sub-type
:gs-opts gs-opts
:train-fn train-fn
:predict-fn predict-fn})
(keys @sub-systems))
(defn get-regression-system
[sub-type]
(if-let [retval (get @sub-systems sub-type)]
retval
(throw (ex-info (format "Failed to find regression subsystem %s" sub-type)
{:sub-type sub-type}))))
(defn options->regression-system
[options]
(-> (ml-model/options->model-type options)
get-regression-system))
(defn pack-model
[^SerializableBase model ^NumericTable model-beta]
(.pack model)
(merge
{:model-bytes (ml-model/model->byte-array model)}
(when model-beta
{:explanatory-variables (dtype/->array-copy model-beta)})))
(defn unpack-model
[^DaalContext daal-context {:keys [model-bytes]}]
(let [^SerializableBase retval (ml-model/byte-array->model model-bytes)]
(.unpack retval daal-context)
retval))
(defn unpack-prediction
[^NumericTable pred]
(let [[n-rows n-cols] (m/shape pred)
table-data (dtype/->array-copy pred)]
(if (= 1 n-cols)
table-data
(ct/in-place-reshape table-data (ct/shape pred)))))
(defrecord DaalSystem []
ml-system/PMLSystem
(system-name [system] :daal.regression)
(gridsearch-options [system options]
(merge (-> (options->regression-system options)
:gs-opts)
options))
(train [system options dataset]
(daal-ctx/with-daal-context
(let [datatype (or (:datatype options) :float64)
[n-cols n-rows] (m/shape dataset)
n-rows (long n-rows)
[feature-table label-table]
(create-tables dataset {:features (ds-opts/feature-column-names options)
:labels (ds-opts/label-column-names options)}
[:features :labels]
datatype
daal-context)
train-fn (-> (options->regression-system options)
:train-fn)]
(train-fn daal-context options feature-table label-table))))
(predict [system options model dataset]
(daal-ctx/with-daal-context
(let [datatype (or (:datatype options) :float64)
[n-cols n-rows] (m/shape dataset)
n-rows (long n-rows)
[feature-table]
(create-tables dataset {:features (ds-opts/feature-column-names options)}
[:features]
datatype
daal-context)
predict-fn (-> (options->regression-system options)
:predict-fn)]
(predict-fn daal-context options model feature-table)))))
(def system (->DaalSystem))
(registry/register-system system)