put-inference.asciidoc 16 KB

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