put-inference.asciidoc 15 KB

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