put-inference.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. =======
  266. //End aggregate output
  267. `classification_labels`::
  268. (Optional, string)
  269. An array of classification labels.
  270. `feature_names`::
  271. (Optional, string)
  272. Features expected by the ensemble, in their expected order.
  273. `target_type`::
  274. (Required, string)
  275. String indicating the model target type; `regression` or `classification.`
  276. `trained_models`::
  277. (Required, object)
  278. An array of `trained_model` objects. Supported trained models are `tree` and
  279. `ensemble`.
  280. ======
  281. //End ensemble
  282. =====
  283. //End trained model
  284. ====
  285. //End definition
  286. `description`::
  287. (Optional, string)
  288. A human-readable description of the {infer} trained model.
  289. //Begin inference_config
  290. `inference_config`::
  291. (Required, object)
  292. The default configuration for inference. This can be either a `regression`
  293. or `classification` configuration. It must match the underlying
  294. `definition.trained_model`'s `target_type`.
  295. +
  296. .Properties of `inference_config`
  297. [%collapsible%open]
  298. ====
  299. `regression`:::
  300. (Optional, object)
  301. Regression configuration for inference.
  302. +
  303. .Properties of regression inference
  304. [%collapsible%open]
  305. =====
  306. `num_top_feature_importance_values`::::
  307. (Optional, integer)
  308. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-regression-num-top-feature-importance-values]
  309. `results_field`::::
  310. (Optional, string)
  311. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-results-field]
  312. =====
  313. `classification`:::
  314. (Optional, object)
  315. Classification configuration for inference.
  316. +
  317. .Properties of classification inference
  318. [%collapsible%open]
  319. =====
  320. `num_top_classes`::::
  321. (Optional, integer)
  322. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-classification-num-top-classes]
  323. `num_top_feature_importance_values`::::
  324. (Optional, integer)
  325. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-classification-num-top-feature-importance-values]
  326. `prediction_field_type`::::
  327. (Optional, string)
  328. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-classification-prediction-field-type]
  329. `results_field`::::
  330. (Optional, string)
  331. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-results-field]
  332. `top_classes_results_field`::::
  333. (Optional, string)
  334. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=inference-config-classification-top-classes-results-field]
  335. =====
  336. ====
  337. //End of inference_config
  338. //Begin input
  339. `input`::
  340. (Required, object)
  341. The input field names for the model definition.
  342. +
  343. .Properties of `input`
  344. [%collapsible%open]
  345. ====
  346. `field_names`:::
  347. (Required, string)
  348. An array of input field names for the model.
  349. ====
  350. //End input
  351. `metadata`::
  352. (Optional, object)
  353. An object map that contains metadata about the model.
  354. `tags`::
  355. (Optional, string)
  356. An array of tags to organize the model.
  357. [[ml-put-inference-example]]
  358. ==== {api-examples-title}
  359. [[ml-put-inference-preprocessor-example]]
  360. ===== Preprocessor examples
  361. The example below shows a `frequency_encoding` preprocessor object:
  362. [source,js]
  363. ----------------------------------
  364. {
  365. "frequency_encoding":{
  366. "field":"FlightDelayType",
  367. "feature_name":"FlightDelayType_frequency",
  368. "frequency_map":{
  369. "Carrier Delay":0.6007414737092798,
  370. "NAS Delay":0.6007414737092798,
  371. "Weather Delay":0.024573576178086153,
  372. "Security Delay":0.02476631010889467,
  373. "No Delay":0.6007414737092798,
  374. "Late Aircraft Delay":0.6007414737092798
  375. }
  376. }
  377. }
  378. ----------------------------------
  379. //NOTCONSOLE
  380. The next example shows a `one_hot_encoding` preprocessor object:
  381. [source,js]
  382. ----------------------------------
  383. {
  384. "one_hot_encoding":{
  385. "field":"FlightDelayType",
  386. "hot_map":{
  387. "Carrier Delay":"FlightDelayType_Carrier Delay",
  388. "NAS Delay":"FlightDelayType_NAS Delay",
  389. "No Delay":"FlightDelayType_No Delay",
  390. "Late Aircraft Delay":"FlightDelayType_Late Aircraft Delay"
  391. }
  392. }
  393. }
  394. ----------------------------------
  395. //NOTCONSOLE
  396. This example shows a `target_mean_encoding` preprocessor object:
  397. [source,js]
  398. ----------------------------------
  399. {
  400. "target_mean_encoding":{
  401. "field":"FlightDelayType",
  402. "feature_name":"FlightDelayType_targetmean",
  403. "target_map":{
  404. "Carrier Delay":39.97465788139886,
  405. "NAS Delay":39.97465788139886,
  406. "Security Delay":203.171206225681,
  407. "Weather Delay":187.64705882352948,
  408. "No Delay":39.97465788139886,
  409. "Late Aircraft Delay":39.97465788139886
  410. },
  411. "default_value":158.17995752420433
  412. }
  413. }
  414. ----------------------------------
  415. //NOTCONSOLE
  416. [[ml-put-inference-model-example]]
  417. ===== Model examples
  418. The first example shows a `trained_model` object:
  419. [source,js]
  420. ----------------------------------
  421. {
  422. "tree":{
  423. "feature_names":[
  424. "DistanceKilometers",
  425. "FlightTimeMin",
  426. "FlightDelayType_NAS Delay",
  427. "Origin_targetmean",
  428. "DestRegion_targetmean",
  429. "DestCityName_targetmean",
  430. "OriginAirportID_targetmean",
  431. "OriginCityName_frequency",
  432. "DistanceMiles",
  433. "FlightDelayType_Late Aircraft Delay"
  434. ],
  435. "tree_structure":[
  436. {
  437. "decision_type":"lt",
  438. "threshold":9069.33437193022,
  439. "split_feature":0,
  440. "split_gain":4112.094574306927,
  441. "node_index":0,
  442. "default_left":true,
  443. "left_child":1,
  444. "right_child":2
  445. },
  446. ...
  447. {
  448. "node_index":9,
  449. "leaf_value":-27.68987349695448
  450. },
  451. ...
  452. ],
  453. "target_type":"regression"
  454. }
  455. }
  456. ----------------------------------
  457. //NOTCONSOLE
  458. The following example shows an `ensemble` model object:
  459. [source,js]
  460. ----------------------------------
  461. "ensemble":{
  462. "feature_names":[
  463. ...
  464. ],
  465. "trained_models":[
  466. {
  467. "tree":{
  468. "feature_names":[],
  469. "tree_structure":[
  470. {
  471. "decision_type":"lte",
  472. "node_index":0,
  473. "leaf_value":47.64069875778043,
  474. "default_left":false
  475. }
  476. ],
  477. "target_type":"regression"
  478. }
  479. },
  480. ...
  481. ],
  482. "aggregate_output":{
  483. "weighted_sum":{
  484. "weights":[
  485. ...
  486. ]
  487. }
  488. },
  489. "target_type":"regression"
  490. }
  491. ----------------------------------
  492. //NOTCONSOLE
  493. [[ml-put-inference-aggregated-output-example]]
  494. ===== Aggregated output example
  495. Example of a `logistic_regression` object:
  496. [source,js]
  497. ----------------------------------
  498. "aggregate_output" : {
  499. "logistic_regression" : {
  500. "weights" : [2.0, 1.0, .5, -1.0, 5.0, 1.0, 1.0]
  501. }
  502. }
  503. ----------------------------------
  504. //NOTCONSOLE
  505. Example of a `weighted_sum` object:
  506. [source,js]
  507. ----------------------------------
  508. "aggregate_output" : {
  509. "weighted_sum" : {
  510. "weights" : [1.0, -1.0, .5, 1.0, 5.0]
  511. }
  512. }
  513. ----------------------------------
  514. //NOTCONSOLE
  515. Example of a `weighted_mode` object:
  516. [source,js]
  517. ----------------------------------
  518. "aggregate_output" : {
  519. "weighted_mode" : {
  520. "weights" : [1.0, 1.0, 1.0, 1.0, 1.0]
  521. }
  522. }
  523. ----------------------------------
  524. //NOTCONSOLE
  525. [[ml-put-inference-json-schema]]
  526. ===== {infer-cap} JSON schema
  527. For the full JSON schema of model {infer},
  528. https://github.com/elastic/ml-json-schemas[click here].