put-inference.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[put-inference]]
  4. === Create {infer} trained model API
  5. [subs="attributes"]
  6. ++++
  7. <titleabbrev>Create {infer} trained model</titleabbrev>
  8. ++++
  9. Creates an {infer} trained model.
  10. experimental[]
  11. [[ml-put-inference-request]]
  12. ==== {api-request-title}
  13. `PUT _ml/inference/<model_id>`
  14. [[ml-put-inference-prereq]]
  15. ==== {api-prereq-title}
  16. If the {es} {security-features} are enabled, you must have the following
  17. built-in roles and privileges:
  18. * `machine_learning_admin`
  19. For more information, see <<security-privileges>> and <<built-in-roles>>.
  20. [[ml-put-inference-desc]]
  21. ==== {api-description-title}
  22. The create {infer} trained model API enables you to supply a trained model that
  23. is not created by {dfanalytics}.
  24. [[ml-put-inference-path-params]]
  25. ==== {api-path-parms-title}
  26. `<model_id>`::
  27. (Required, string)
  28. include::{docdir}/ml/ml-shared.asciidoc[tag=model-id]
  29. [role="child_attributes"]
  30. [[ml-put-inference-request-body]]
  31. ==== {api-request-body-title}
  32. `compressed_definition`::
  33. (Required, string)
  34. The compressed (GZipped and Base64 encoded) {infer} definition of the model.
  35. If `compressed_definition` is specified, then `definition` cannot be specified.
  36. //Begin definition
  37. `definition`::
  38. (Required, object)
  39. The {infer} definition for the model. If `definition` is specified, then
  40. `compressed_definition` cannot be specified.
  41. +
  42. .Properties of `definition`
  43. [%collapsible%open]
  44. ====
  45. `preprocessors`:::
  46. (Optional, object)
  47. Collection of preprocessors. See <<ml-put-inference-preprocessors>> for the full
  48. list of available preprocessors.
  49. `trained_model`:::
  50. (Required, object)
  51. The definition of the trained model. See <<ml-put-inference-trained-model>> for
  52. details.
  53. ====
  54. //End definition
  55. `description`::
  56. (Optional, string)
  57. A human-readable description of the {infer} trained model.
  58. //Begin inference_config
  59. `inference_config`::
  60. (Required, object)
  61. The default configuration for inference. This can be either a `regression`
  62. or `classification` configuration. It must match the underlying
  63. `definition.trained_model`'s `target_type`.
  64. +
  65. .Properties of `inference_config`
  66. [%collapsible%open]
  67. ====
  68. `regression`:::
  69. (Optional, object)
  70. Regression configuration for inference.
  71. +
  72. .Properties of regression inference
  73. [%collapsible%open]
  74. =====
  75. `num_top_feature_importance_values`::::
  76. (Optional, integer)
  77. include::{docdir}/ml/ml-shared.asciidoc[tag=inference-config-regression-num-top-feature-importance-values]
  78. `results_field`::::
  79. (Optional, string)
  80. include::{docdir}/ml/ml-shared.asciidoc[tag=inference-config-regression-results-field]
  81. =====
  82. `classification`:::
  83. (Optional, object)
  84. Classification configuration for inference.
  85. +
  86. .Properties of classification inference
  87. [%collapsible%open]
  88. =====
  89. `num_top_classes`::::
  90. (Optional, integer)
  91. include::{docdir}/ml/ml-shared.asciidoc[tag=inference-config-classification-num-top-classes]
  92. `num_top_feature_importance_values`::::
  93. (Optional, integer)
  94. include::{docdir}/ml/ml-shared.asciidoc[tag=inference-config-classification-num-top-feature-importance-values]
  95. `results_field`::::
  96. (Optional, string)
  97. include::{docdir}/ml/ml-shared.asciidoc[tag=inference-config-classification-results-field]
  98. `top_classes_results_field`::::
  99. (Optional, string)
  100. include::{docdir}/ml/ml-shared.asciidoc[tag=inference-config-classification-top-classes-results-field]
  101. =====
  102. ====
  103. //End of inference_config
  104. //Begin input
  105. `input`::
  106. (Required, object)
  107. The input field names for the model definition.
  108. +
  109. .Properties of `input`
  110. [%collapsible%open]
  111. ====
  112. `field_names`:::
  113. (Required, string)
  114. An array of input field names for the model.
  115. ====
  116. //End input
  117. `metadata`::
  118. (Optional, object)
  119. An object map that contains metadata about the model.
  120. `tags`::
  121. (Optional, string)
  122. An array of tags to organize the model.
  123. [[ml-put-inference-preprocessors]]
  124. ===== {infer-cap} preprocessor definitions
  125. `frequency_encoding`::
  126. (Required, object)
  127. Defines a frequency encoding for a field.
  128. `frequency_encoding`.`field`:::
  129. (Required, string)
  130. The field name to encode.
  131. `frequency_encoding`.`feature_name`:::
  132. (Required, string)
  133. The name of the resulting feature.
  134. `frequency_encoding`.`frequency_map`:::
  135. (Required, object map of string:double)
  136. Object that maps the field value to the frequency encoded value.
  137. `one_hot_encoding`::
  138. (Required, object)
  139. Defines a one hot encoding map for a field.
  140. `one_hot_encoding`.`field`:::
  141. (Required, string)
  142. The field name to encode.
  143. `one_hot_encoding`.`hot_map`:::
  144. (Required, object map of strings)
  145. String map of "field_value: one_hot_column_name".
  146. `target_mean_encoding`::
  147. (Required, object)
  148. Defines a target mean encoding for a field.
  149. `target_mean_encoding`.`field`:::
  150. (Required, string)
  151. The field name to encode.
  152. `target_mean_encoding`.`feature_name`:::
  153. (Required, string)
  154. The name of the resulting feature.
  155. `target_mean_encoding`.`target_map`:::
  156. (Required, object map of string:double)
  157. Object that maps the field value to the target mean value.
  158. `target_mean_encoding`.`default_value`:::
  159. (Required, double)
  160. The feature value if the field value is not in the `target_map`.
  161. See <<ml-put-inference-preprocessor-example>> for more details.
  162. [[ml-put-inference-trained-model]]
  163. ===== {infer-cap} trained model definitions
  164. `tree`::
  165. (Required, object)
  166. The definition for a binary decision tree.
  167. `tree`.`feature_names`:::
  168. (Required, string)
  169. Features expected by the tree, in their expected order.
  170. `tree`.`tree_structure`:::
  171. (Required, object)
  172. An array of `tree_node` objects. The nodes must be in ordinal order by their
  173. `tree_node.node_index` value.
  174. `tree`.`classification_labels`:::
  175. (Optional, string) An array of classification labels (used for
  176. `classification`).
  177. `tree`.`target_type`:::
  178. (Required, string)
  179. String indicating the model target type; `regression` or `classification`.
  180. There are two major types of nodes: leaf nodes and not-leaf nodes.
  181. * Leaf nodes only need `node_index` and `leaf_value` defined.
  182. * All other nodes need `split_feature`, `left_child`, `right_child`,
  183. `threshold`, `decision_type`, and `default_left` defined.
  184. `tree_node`::
  185. (Required, object)
  186. The definition of a node in a tree.
  187. `tree_node`.`decision_type`:::
  188. (Optional, string)
  189. Indicates the positive value (in other words, when to choose the left node)
  190. decision type. Supported `lt`, `lte`, `gt`, `gte`. Defaults to `lte`.
  191. `tree_node`.`threshold`:::
  192. (Optional, double)
  193. The decision threshold with which to compare the feature value.
  194. `tree_node`.`left_child`:::
  195. (Optional, integer)
  196. The index of the left child.
  197. `tree_node`.`right_child`:::
  198. (Optional, integer)
  199. The index of the right child.
  200. `tree_node`.`default_left`:::
  201. (Optional, boolean)
  202. Indicates whether to default to the left when the feature is missing. Defaults
  203. to `true`.
  204. `tree_node`.`split_feature`:::
  205. (Optional, integer)
  206. The index of the feature value in the feature array.
  207. `tree_node`.`node_index`:::
  208. (Integer)
  209. The index of the current node.
  210. `tree_node`.`split_gain`:::
  211. (Optional, double) The information gain from the split.
  212. `tree_node`.`leaf_value`:::
  213. (Optional, double)
  214. The leaf value of the of the node, if the value is a leaf (in other words, no
  215. children).
  216. `ensemble`::
  217. (Optional, object)
  218. The definition for an ensemble model.
  219. `ensemble`.`feature_names`:::
  220. (Optional, string)
  221. Features expected by the ensemble, in their expected order.
  222. `ensemble`.`trained_models`:::
  223. (Required, object)
  224. An array of `trained_model` objects. Supported trained models are `tree` and
  225. `ensemble`.
  226. `ensemble`.`classification_labels`:::
  227. (Optional, string)
  228. An array of classification labels.
  229. `ensemble`.`target_type`:::
  230. (Required, string)
  231. String indicating the model target type; `regression` or `classification.`
  232. `ensemble`.`aggregate_output`:::
  233. (Required, object)
  234. An aggregated output object that defines how to aggregate the outputs of the
  235. `trained_models`. Supported objects are `weighted_mode`, `weighted_sum`, and
  236. `logistic_regression`.
  237. See <<ml-put-inference-model-example>> for more details.
  238. [[ml-put-inference-aggregated-output]]
  239. ===== Aggregated output types
  240. `logistic_regression`::
  241. (Optional, object)
  242. This `aggregated_output` type works with binary classification (classification
  243. for values [0, 1]). It multiplies the outputs (in the case of the `ensemble`
  244. model, the inference model values) by the supplied `weights`. The resulting
  245. vector is summed and passed to a
  246. https://en.wikipedia.org/wiki/Sigmoid_function[`sigmoid` function]. The result
  247. of the `sigmoid` function is considered the probability of class 1 (`P_1`),
  248. consequently, the probability of class 0 is `1 - P_1`. The class with the
  249. highest probability (either 0 or 1) is then returned. For more information about
  250. logistic regression, see
  251. https://en.wikipedia.org/wiki/Logistic_regression[this wiki article].
  252. `logistic_regression`.`weights`:::
  253. (Required, double)
  254. The weights to multiply by the input values (the inference values of the trained
  255. models).
  256. `weighted_sum`::
  257. (Optional, object)
  258. This `aggregated_output` type works with regression. The weighted sum of the
  259. input values.
  260. `weighted_sum`.`weights`:::
  261. (Required, double)
  262. The weights to multiply by the input values (the inference values of the trained
  263. models).
  264. `weighted_mode`::
  265. (Optional, object)
  266. This `aggregated_output` type works with regression or classification. It takes
  267. a weighted vote of the input values. The most common input value (taking the
  268. weights into account) is returned.
  269. `weighted_mode`.`weights`:::
  270. (Required, double)
  271. The weights to multiply by the input values (the inference values of the trained
  272. models).
  273. See <<ml-put-inference-aggregated-output-example>> for more details.
  274. [[ml-put-inference-example]]
  275. ==== {api-examples-title}
  276. [[ml-put-inference-preprocessor-example]]
  277. ===== Preprocessor examples
  278. The example below shows a `frequency_encoding` preprocessor object:
  279. [source,js]
  280. ----------------------------------
  281. {
  282. "frequency_encoding":{
  283. "field":"FlightDelayType",
  284. "feature_name":"FlightDelayType_frequency",
  285. "frequency_map":{
  286. "Carrier Delay":0.6007414737092798,
  287. "NAS Delay":0.6007414737092798,
  288. "Weather Delay":0.024573576178086153,
  289. "Security Delay":0.02476631010889467,
  290. "No Delay":0.6007414737092798,
  291. "Late Aircraft Delay":0.6007414737092798
  292. }
  293. }
  294. }
  295. ----------------------------------
  296. //NOTCONSOLE
  297. The next example shows a `one_hot_encoding` preprocessor object:
  298. [source,js]
  299. ----------------------------------
  300. {
  301. "one_hot_encoding":{
  302. "field":"FlightDelayType",
  303. "hot_map":{
  304. "Carrier Delay":"FlightDelayType_Carrier Delay",
  305. "NAS Delay":"FlightDelayType_NAS Delay",
  306. "No Delay":"FlightDelayType_No Delay",
  307. "Late Aircraft Delay":"FlightDelayType_Late Aircraft Delay"
  308. }
  309. }
  310. }
  311. ----------------------------------
  312. //NOTCONSOLE
  313. This example shows a `target_mean_encoding` preprocessor object:
  314. [source,js]
  315. ----------------------------------
  316. {
  317. "target_mean_encoding":{
  318. "field":"FlightDelayType",
  319. "feature_name":"FlightDelayType_targetmean",
  320. "target_map":{
  321. "Carrier Delay":39.97465788139886,
  322. "NAS Delay":39.97465788139886,
  323. "Security Delay":203.171206225681,
  324. "Weather Delay":187.64705882352948,
  325. "No Delay":39.97465788139886,
  326. "Late Aircraft Delay":39.97465788139886
  327. },
  328. "default_value":158.17995752420433
  329. }
  330. }
  331. ----------------------------------
  332. //NOTCONSOLE
  333. [[ml-put-inference-model-example]]
  334. ===== Model examples
  335. The first example shows a `trained_model` object:
  336. [source,js]
  337. ----------------------------------
  338. {
  339. "tree":{
  340. "feature_names":[
  341. "DistanceKilometers",
  342. "FlightTimeMin",
  343. "FlightDelayType_NAS Delay",
  344. "Origin_targetmean",
  345. "DestRegion_targetmean",
  346. "DestCityName_targetmean",
  347. "OriginAirportID_targetmean",
  348. "OriginCityName_frequency",
  349. "DistanceMiles",
  350. "FlightDelayType_Late Aircraft Delay"
  351. ],
  352. "tree_structure":[
  353. {
  354. "decision_type":"lt",
  355. "threshold":9069.33437193022,
  356. "split_feature":0,
  357. "split_gain":4112.094574306927,
  358. "node_index":0,
  359. "default_left":true,
  360. "left_child":1,
  361. "right_child":2
  362. },
  363. ...
  364. {
  365. "node_index":9,
  366. "leaf_value":-27.68987349695448
  367. },
  368. ...
  369. ],
  370. "target_type":"regression"
  371. }
  372. }
  373. ----------------------------------
  374. //NOTCONSOLE
  375. The following example shows an `ensemble` model object:
  376. [source,js]
  377. ----------------------------------
  378. "ensemble":{
  379. "feature_names":[
  380. ...
  381. ],
  382. "trained_models":[
  383. {
  384. "tree":{
  385. "feature_names":[],
  386. "tree_structure":[
  387. {
  388. "decision_type":"lte",
  389. "node_index":0,
  390. "leaf_value":47.64069875778043,
  391. "default_left":false
  392. }
  393. ],
  394. "target_type":"regression"
  395. }
  396. },
  397. ...
  398. ],
  399. "aggregate_output":{
  400. "weighted_sum":{
  401. "weights":[
  402. ...
  403. ]
  404. }
  405. },
  406. "target_type":"regression"
  407. }
  408. ----------------------------------
  409. //NOTCONSOLE
  410. [[ml-put-inference-aggregated-output-example]]
  411. ===== Aggregated output example
  412. Example of a `logistic_regression` object:
  413. [source,js]
  414. ----------------------------------
  415. "aggregate_output" : {
  416. "logistic_regression" : {
  417. "weights" : [2.0, 1.0, .5, -1.0, 5.0, 1.0, 1.0]
  418. }
  419. }
  420. ----------------------------------
  421. //NOTCONSOLE
  422. Example of a `weighted_sum` object:
  423. [source,js]
  424. ----------------------------------
  425. "aggregate_output" : {
  426. "weighted_sum" : {
  427. "weights" : [1.0, -1.0, .5, 1.0, 5.0]
  428. }
  429. }
  430. ----------------------------------
  431. //NOTCONSOLE
  432. Example of a `weighted_mode` object:
  433. [source,js]
  434. ----------------------------------
  435. "aggregate_output" : {
  436. "weighted_mode" : {
  437. "weights" : [1.0, 1.0, 1.0, 1.0, 1.0]
  438. }
  439. }
  440. ----------------------------------
  441. //NOTCONSOLE
  442. [[ml-put-inference-json-schema]]
  443. ===== {infer-cap} JSON schema
  444. For the full JSON schema of model {infer},
  445. https://github.com/elastic/ml-json-schemas[click here].