# CREATE MODEL
#### 功能描述
用于训练机器学习模型并保存模型。
#### 注意事项
AI训练时长波动较大，在部分情况下训练运行时间较长，如果设置的GUC参数statement_timeout时长过短会导致训练中断。建议将statement_timeout设置为0，对语句执行时长不做限制。
#### 语法格式
```
CREATE MODEL model_name USING architecture_name
    FEATURES attribute_list[,...]
    [TARGET attribute_name[,...]]
    FROM {[schema.]table_name | (subquery)}
    [WITH {hyper_parameter_name = {hp_value | DEFAULT}}[,...]];
```
![](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/figure/zh-cn_image_0000002764461070.png "点击放大")
#### 参数说明
- **model_name**
  对训练模型进行命名，模型名称需满足唯一性约束。
  取值范围：字符串，要符合[标识符命名规范](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0003.html#ZH-CN_TOPIC_0000002793779057__section1257443811315)。
  
- **architecture_name**
  训练模型的算法类型。
  取值范围：字符串，当前支持logistic_regression、linear_regression、svm_classification以及kmeans。
  
- **attribute_list**
  枚举训练模型的输入列名。
  取值范围：字符串，需要符合数据属性名的命名规范。
  
- **attribute_name**
  在监督学习任务中训练模型的目标列名（可进行简单的表达式处理）。
  取值范围：字符串，需要符合数据属性名的命名规范。
  
- **schema**
  模式名称。
  取值范围：字符串，符合数据库SQL语法。
  
- **table_name**
  训练数据表名称。
  取值范围：字符串，符合数据库SQL语法。
  
- **subquery**
  数据源。
  取值范围：字符串，符合数据库SQL语法。
  
- **hyper_parameter_name**
  机器学习模型的超参名称。
  取值范围：字符串，针对不同算法超参类型范围不同。
  
- **hp_value**
  超参数值。
  取值范围：字符串，针对不同算法范围不同。
  
- **DEFAULT**
  超参默认值，无需设置。不区分大小写。
  当超参未设置具体数值，数据库采用默认值进行模型训练。
  表1超参的默认值以及取值范围 
  | 算子                                                                                                                                                                 | 超参(默认值)                                                 | 取值范围                                                                                                                                                                                                                                                                         | 超参描述                      |
  |:---|:---|:---|:---|
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | optimizer = gd                                           | gd/ngd - gd：梯度下降法。  - ngd：自然梯度下降。   | 优化器。                     |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | verbose = false                                         | T/F                                                                                                                                                                                                                                                                             | 日志显示。                    |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | max_iterations = 100                                    | (0, 10000\]                                                                                                                                                                                                                                                                  | 最大迭代次数。                  |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | max_seconds = 0                                          | \[0,INT_MAX_VALUE\]，其中0表示不对运行时长进行限制。                                                                                                                                                                                                                                            | 运行时长。                      |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | batch_size = 1000                                       | (0, 1048575\]                                                                                                                                                                                                                                                                  | 一次训练所选取的样本数。               |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | learning_rate = 0.8                                    | (0, DOUBLE_MAX_VALUE\]                                                                                                                                                                                                                                                        | 学习率。                        |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | decay = 0.95                                            | (0, DOUBLE_MAX_VALUE\]                                                                                                                                                                                                                                                         | 权值衰减率。                     |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | tolerance = 0.0005                                        | (0, DOUBLE_MAX_VALUE\]                                                                                                                                                                                                                                                           | 公差。                         |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | seed = 0                                              | \[0, INT_MAX_VALUE\]，其中0表示对seed取随机值。                                                                                                                                                                                                                                           | 种子。                       |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | just for linear、SVM：kernel = "linear"                   | linear/gaussian/polynomial                                                                                                                                                                                                                                                    | 核函数。                       |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | just for linear、SVM：components = MAX(2\*features, 128) | \[0, INT_MAX_VALUE\]                                                                                                                                                                                                                                                        | 高维空间维数。                   |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | just for linear、SVM：gamma = 0.5                        | (0, DOUBLE_MAX_VALUE\]                                                                                                                                                                                                                                                      | gaussian核函数参数。            |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | just for linear、SVM：degree = 2                         | \[2, 9\]                                                                                                                                                                                                                                                                     | polynomial核函数参数。           |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | just for linear、SVM：coef0 = 1.0                        | \[0, DOUBLE_MAX_VALUE\]                                                                                                                                                                                                                                                       | polynomial核函数的参数。          |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | just for SVM：lambda = 0.01                                | (0, DOUBLE_MAX_VALUE)                                                                                                                                                                                                                                                         | 正则化参数。                    |
  | GD: logistic_regression、linear_regression、svm_classification、pca                                                     | just for pca： number_components                         | （0，INT_MAX_VALUE\]                                                                                                                                                                                                                                                          | 降维的目标维度。                 |
  | GD: multiclass                                                                                                      | classifier="svm_classification"                          | svm_classification\\logistic_regression                                                                                                                                                                                                                                      | 多分类任务的分类器。                 |
  | Kmeans                                                                                                                                                               | max_iterations = 10                                    | \[1, 10000\]                                                                                                                                                                                                                                                                  | 最大迭代次数。                   |
  | Kmeans                                                                                                                                                               | num_centroids = 10                                      | \[1, 1000000\]                                                                                                                                                                                                                                                               | 簇的数目。                     |
  | Kmeans                                                                                                                                                               | tolerance = 0.00001                                     | (0,1\]                                                                                                                                                                                                                                                                      | 中心点误差。                     |
  | Kmeans                                                                                                                                                               | batch_size = 10                                         | \[1, 1048575\]                                                                                                                                                                                                                                                                | 一次训练所选取的样本数。              |
  | Kmeans                                                                                                                                                               | num_features = 2                                      | \[1, INT_MAX_VALUE\]                                                                                                                                                                                                                                                         | 输入样本特征数。                    |
  | Kmeans                                                                                                                                                               | distance_function = "L2_Squared"                        | L1\\L2\\L2_Squared\\Linf                                                                                                                                                                                                                                                     | 正则化方法。                     |
  | Kmeans                                                                                                                                                               | seeding_function = "Random++"                            | "Random++"\\"KMeans\|\|"                                                                                                                                                                                                                                                     | 初始化种子点方法。                  |
  | Kmeans                                                                                                                                                               | verbose = 0U                                           | { 0, 1, 2 }                                                                                                                                                                                                                                                                   | 冗长模式。                     |
  | Kmeans                                                                                                                                                               | seed = 0U                                               | \[0, INT_MAX_VALUE\]                                                                                                                                                                                                                                                         | 种子。                      |
  | xgboost: xgboost_regression_logistic、xgboost_binary_logistic、xgboost_regression_gamma、xgboost_regression_squarederror | n_iter=10                                              | (0, 10000\]                                                                                                                                                                                                                                                                 | 迭代次数。                    |
  | xgboost: xgboost_regression_logistic、xgboost_binary_logistic、xgboost_regression_gamma、xgboost_regression_squarederror | batch_size=10000                                      | (0, 1048575\]                                                                                                                                                                                                                                                               | 一次训练所选取的样本数。                |
  | xgboost: xgboost_regression_logistic、xgboost_binary_logistic、xgboost_regression_gamma、xgboost_regression_squarederror | booster="gbtree"                                         | gbtree\\gblinear\\dart                                                                                                                                                                                                                                                        | booster种类。               |
  | xgboost: xgboost_regression_logistic、xgboost_binary_logistic、xgboost_regression_gamma、xgboost_regression_squarederror | tree_method="auto"                                      | auto\\exact\\approx\\hist\\gpu_hist 注意：gpu_hist参数需要相应的库是GPU版本，否则DB4AI平台不支持该值。                                                                                                                                                   | 树构建算法。                      |
  | xgboost: xgboost_regression_logistic、xgboost_binary_logistic、xgboost_regression_gamma、xgboost_regression_squarederror | eval_metric="rmse"                                    | rmse\\rmsle\\map\\mae\\auc\\aucpr                                                                                                                                                                                                                                              | 验证数据的评估指标。                  |
  | xgboost: xgboost_regression_logistic、xgboost_binary_logistic、xgboost_regression_gamma、xgboost_regression_squarederror | seed=0                                                  | \[0, INT_MAX_VALUE\]                                                                                                                                                                                                                                                           | 种子。                       |
  | xgboost: xgboost_regression_logistic、xgboost_binary_logistic、xgboost_regression_gamma、xgboost_regression_squarederror | nthread:若未显式指定，则默认采用系统可用的最大线程数。                         | \[0, 100\]                                                                                                                                                                                                                                                                      | 并发量。                         |
  | xgboost: xgboost_regression_logistic、xgboost_binary_logistic、xgboost_regression_gamma、xgboost_regression_squarederror | max_depth=6                                            | \[0, MAX_MEMORY_LIMIT\]                                                                                                                                                                                                                                                     | 树的最大深度，该超参仅对树型booster生效。 |
  | xgboost: xgboost_regression_logistic、xgboost_binary_logistic、xgboost_regression_gamma、xgboost_regression_squarederror | gamma=0.0                                             | \[0, DOUBLE_MAX_VALUE\]                                                                                                                                                                                                                                                      | 叶节点上进行进一步分区所需的最小损失减少。    |
  | xgboost: xgboost_regression_logistic、xgboost_binary_logistic、xgboost_regression_gamma、xgboost_regression_squarederror | eta=0.3                                                | \[0, 1\]                                                                                                                                                                                                                                                                     | 更新中使用的步长收缩，以防止过拟合.       |
  | xgboost: xgboost_regression_logistic、xgboost_binary_logistic、xgboost_regression_gamma、xgboost_regression_squarederror | min_child_weight=1                                      | \[0, INT_MAX_VALUE\]                                                                                                                                                                                                                                                          | 孩子节点中所需的实例权重的最小总和。        |
  | xgboost: xgboost_regression_logistic、xgboost_binary_logistic、xgboost_regression_gamma、xgboost_regression_squarederror | verbosity=1                                          | 0 (silent)\\1 (warning)\\2 (info)\\3 (debug)                                                                                                                                                                                                                                 | 打印信息的详细程度。               |
  | MAX_MEMORY_LIMIT = 最大内存加载的元组数量。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ||||
  | GS_MAX_COLS = 数据库单表最大属性数量。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ||||
     
  ![](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/public_sys-resources/note_3.0-zh-cn.png)
  DB4AI的算法对于支持的数据类型是有限制的。当数据类型不在支持白名单中，会返回ERROR，并提示非法的oid。算子支持的列类型和参数类型参见[表2 算子支持的列类型和参数类型表]。
   表2算子支持的列类型和参数类型表 
  | 算子名称                                                                        | 列类型 | 支持的数据类型                                                                                                                                                                                                   |
  |:---|:---|:---|
  | LOGISTIC_REGRESSION算子、SVM_CLASSIFICATION算子、LINEAR_REGRESSION算子、MULTICLASS算子 | 特征列    | BOOL, INT1, INT2, INT4, INT8, FLOAT4, FLOAT8, NUMERIC, FLOAT8\[\], FLOAT4\[\]。                                                                                                                           |
  | LOGISTIC_REGRESSION算子、SVM_CLASSIFICATION算子、LINEAR_REGRESSION算子、MULTICLASS算子 | 目标列    | BIT, VARBIT, BYTEA, CHAR, RAW, NAME, TEXT, BPCHAR, VARCHAR, NVARCHAR2, INT1, INT2, INT4, INT8, FLOAT4, FLOAT8, NUMERIC, ABSTIME, DATE, TIME, DATEA, TIMESTAMP, TIMESTAMPTZ, TIMETZ, SMALLDATETIME, BOOL。 |
  | PCA算子                                                                         | 特征列    | BOOL, INT1, INT2, INT4, INT8, FLOAT4, FLOAT8, NUMERIC, FLOAT8\[\], FLOAT4\[\]。                                                                                                                          |
  | KMEANS算子                                                                   | 特征列  | FLOAT8\[\]。                                                                                                                                                                                               |
  | XG_REG_LOGISTIC算子、XG_BIN_LOGISTIC算子、XG_REG_SQE算子和XG_REG_GAMMA算子          | 特征列     | BOOL, INT1, INT2, INT4, INT8, FLOAT4, FLOAT8, NUMERIC, TIMESTAMP, TIMESTAMPTZ, TIME, OID。                                                                                                           |
  | XG_REG_LOGISTIC算子、XG_BIN_LOGISTIC算子、XG_REG_SQE算子和XG_REG_GAMMA算子          | 目标列    | BOOL, INT1, INT2, INT4, INT8, FLOAT4, FLOAT8, NUMERIC, TIMESTAMP, TIMESTAMPTZ, TIME, OID。                                                                                                           |
     
  
 
#### 示例
```
--创建数据表。
gaussdb=# CREATE TABLE houses (
id INTEGER,
tax INTEGER,
bedroom INTEGER,
bath DOUBLE PRECISION,
price INTEGER,
size INTEGER,
lot INTEGER,
mark text
);
--插入训练数据。
gaussdb=# INSERT INTO houses(id, tax, bedroom, bath, price, size, lot, mark) VALUES
(1,590,2,1,50000,770,22100,'a+'),
(2,1050,3,2,85000,1410,12000,'a+'),
(3,20,2,1,22500,1060,3500,'a-'),
(4,870,2,2,90000,1300,17500,'a+'),
(5,1320,3,2,133000,1500,30000,'a+'),
(6,1350,2,1,90500,850,25700,'a-'),
(7,2790,3,2.5,260000,2130,25000,'a+'),
(8,680,2,1,142500,1170,22000,'a-'),
(9,1840,3,2,160000,1500,19000,'a+'),
(10,3680,4,2,240000,2790,20000,'a-'),
(11,1660,3,1,87000,1030,17500,'a+'),
(12,1620,3,2,118500,1250,20000,'a-'),
(13,3100,3,2,140000,1760,38000,'a+'),
(14,2090,2,3,148000,1550,14000,'a-'),
(15,650,3,1.5,65000,1450,12000,'a-');
--训练模型。
gaussdb=# CREATE MODEL price_model USING logistic_regression
 FEATURES size, lot
 TARGET mark
 FROM HOUSES
 WITH learning_rate=0.88, max_iterations=default;
--删除模型。
gaussdb=# DROP MODEL price_model;
--删除表。
gaussdb=# DROP TABLE houses;
```
#### 相关链接
[DROP MODEL](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0319.html)，[PREDICT BY](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0376.html)
