此部分中的主题描述了如何管理模型。
模型管理
1.1 - 更改模型所有权
作为超级用户或模型所有者,您可以使用
ALTER MODEL
重新分配模型所有权,如下所示:
ALTER MODEL model-name OWNER TO owner-name
当模型所有者离职或更改职责时,更改模型所有权很有用。由于您可以更改所有者,因此不需要重写模型。
示例
以下示例演示了如何使用 ALTER_MODEL 更改模型所有者:
-
查找您要更改的模型。作为 dbadmin,您拥有模型。
=> SELECT * FROM V_CATALOG.MODELS WHERE model_name='mykmeansmodel'; -[ RECORD 1 ]--+------------------------------------------ model_id | 45035996273816618 model_name | mykmeansmodel schema_id | 45035996273704978 schema_name | public owner_id | 45035996273704962 owner_name | dbadmin category | VERTICA_MODELS model_type | kmeans is_complete | t create_time | 2017-03-02 11:16:04.990626-05 size | 964
-
将模型所有者从 dbadmin 更改为 user1。
=> ALTER MODEL mykmeansmodel OWNER TO user1; ALTER MODEL
-
查看
V_CATALOG.MODELS
以验证所有者是否已更改。=> SELECT * FROM V_CATALOG.MODELS WHERE model_name='mykmeansmodel';
-[ RECORD 1 ]--+------------------------------------------ model_id | 45035996273816618 model_name | mykmeansmodel schema_id | 45035996273704978 schema_name | public owner_id | 45035996273704962 owner_name | user1 category | VERTICA_MODELS model_type | kmeans is_complete | t create_time | 2017-03-02 11:16:04.990626-05 size | 964
1.2 - 将模型移到另一个架构
您可以使用
ALTER MODEL
将模型从一个架构移动到另一个架构。您可以以超级用户或对当前架构拥有 USAGE
权限且对目标架构拥有 CREATE
权限的用户的身份来移动模型。
示例
以下示例演示了如何使用 ALTER MODEL
更改模型架构:
-
查找您要更改的模型。
=> SELECT * FROM V_CATALOG.MODELS WHERE model_name='mykmeansmodel'; -[ RECORD 1 ]--+------------------------------------------ model_id | 45035996273816618 model_name | mykmeansmodel schema_id | 45035996273704978 schema_name | public owner_id | 45035996273704962 owner_name | dbadmin category | VERTICA_MODELS model_type | kmeans is_complete | t create_time | 2017-03-02 11:16:04.990626-05 size | 964
-
更改模型架构。
=> ALTER MODEL mykmeansmodel SET SCHEMA test; ALTER MODEL
-
查看
V_CATALOG.MODELS
以验证所有者是否已更改。=> SELECT * FROM V_CATALOG.MODELS WHERE model_name='mykmeansmodel';
-[ RECORD 1 ]--+------------------------------------------ model_id | 45035996273816618 model_name | mykmeansmodel schema_id | 45035996273704978 schema_name | test owner_id | 45035996273704962 owner_name | dbadmin category | VERTICA_MODELS model_type | kmeans is_complete | t create_time | 2017-03-02 11:16:04.990626-05 size | 964
1.3 - 重命名模型
ALTER MODEL
允许您重命名模型。例如:
-
查找您要更改的模型。
=> SELECT * FROM V_CATALOG.MODELS WHERE model_name='mymodel'; -[ RECORD 1 ]--+------------------------------------------ model_id | 45035996273816618 model_name | mymodel schema_id | 45035996273704978 schema_name | public owner_id | 45035996273704962 owner_name | dbadmin category | VERTICA_MODELS model_type | kmeans is_complete | t create_time | 2017-03-02 11:16:04.990626-05 size | 964
-
重命名模型。
=> ALTER MODEL mymodel RENAME TO mykmeansmodel; ALTER MODEL
-
查看
V_CATALOG.MODELS
以验证模型名称是否已更改。=> SELECT * FROM V_CATALOG.MODELS WHERE model_name='mykmeansmodel';
-[ RECORD 1 ]--+------------------------------------------ model_id | 45035996273816618 model_name | mykmeansmodel schema_id | 45035996273704978 schema_name | public owner_id | 45035996273704962 owner_name | dbadmin category | VERTICA_MODELS model_type | kmeans is_complete | t create_time | 2017-03-02 11:16:04.990626-05 size | 964
2 - 删除模型
DROP MODEL
从数据库中移除一个或多个模型。例如:
-
查找您要删除的模型。
=> SELECT * FROM V_CATALOG.MODELS WHERE model_name='mySvmClassModel'; -[ RECORD 1 ]--+-------------------------------- model_id | 45035996273765414 model_name | mySvmClassModel schema_id | 45035996273704978 schema_name | public owner_id | 45035996273704962 owner_name | dbadmin category | VERTICA_MODELS model_type | SVM_CLASSIFIER is_complete | t create_time | 2017-02-14 10:30:44.903946-05 size | 525
-
删除模型。
=> DROP MODEL mySvmClassModel; DROP MODEL
-
查看
V_CATALOG.MODELS
以验证模型是否已删除。=> SELECT * FROM V_CATALOG.MODELS WHERE model_name='mySvmClassModel'; (0 rows)
3 - 管理模型安全
您可以使用 GRANT 和 REVOKE 语句来管理模型的安全权限。以下示例展示了如何使用 faithful
表和 linearReg 模型更改 user1 和 user2 的权限。
-
在以下示例中,dbadmin 将 SELECT 权限授予 user1:
=> GRANT SELECT ON TABLE faithful TO user1;
GRANT PRIVILEGE
-
然后,dbadmin 将公共架构的 CREATE 权限授予 user1:
=> GRANT CREATE ON SCHEMA public TO user1; GRANT PRIVILEGE
-
以 user1 身份连接到数据库:
=> \c - user1
-
以 user1 身份构建 linearReg 模型:
=> SELECT LINEAR_REG('linearReg', 'faithful', 'waiting', 'eruptions');
LINEAR_REG
---------------------------
Finished in 1 iterations
(1 row)
- 以 user1 身份将 USAGE 权限授予 user2:
=> GRANT USAGE ON MODEL linearReg TO user2;
GRANT PRIVILEGE
- 以 user2 身份连接到数据库:
=> \c - user2
- 要确认权限已授予 user2,请运行 GET_MODEL_SUMMARY 函数。对模型具有 USAGE 权限的用户可以在该模型上运行 GET_MODEL_SUMMARY:
=> SELECT GET_MODEL_SUMMARY(USING PARAMETERS model_name='linearReg');
=======
details
=======
predictor|coefficient|std_err |t_value |p_value
---------+-----------+--------+--------+--------
Intercept| 33.47440 | 1.15487|28.98533| 0.00000
eruptions| 10.72964 | 0.31475|34.08903| 0.00000
==============
regularization
==============
type| lambda
----+--------
none| 1.00000
===========
call_string
===========
linear_reg('public.linearReg', 'faithful', '"waiting"', 'eruptions'
USING PARAMETERS optimizer='newton', epsilon=1e-06, max_iterations=100, regularization='none', lambda=1)
===============
Additional Info
===============
Name |Value
------------------+-----
iteration_count | 1
rejected_row_count| 0
accepted_row_count| 272
(1 row)
- 以 user1 身份连接到数据库:
=> \c - user1
- 然后,您可以使用 REVOKE 语句撤销 user2 的权限:
=> REVOKE USAGE ON MODEL linearReg FROM user2;
REVOKE PRIVILEGE
- 要确认权限已撤销,请以用户 2 身份连接并运行 GET_MODEL_SUMMARY 函数:
=> \c - user2
=>SELECT GET_MODEL_SUMMARY('linearReg');
ERROR 7523: Problem in get_model_summary.
Detail: Permission denied for model linearReg
另请参阅
4 - 查看模型属性
以下主题介绍了 Vertica 机器学习算法的模型属性。这些属性描述了特定模型的内部结构:
5 - 总结模型
-
查找您要总结的模型。
=> SELECT * FROM v_catalog.models WHERE model_name='svm_class'; model_id | model_name | schema_id | schema_name | owner_id | owner_name | category | model_type | is_complete | create_time | size -------------------+------------+-------------------+-------------+-------------------+------------+-------- --------+----------------+-------------+-------------------------------+------ 45035996273715226 | svm_class | 45035996273704980 | public | 45035996273704962 | dbadmin | VERTICA_MODELS | SVM_CLASSIFIER | t | 2017-08-28 09:49:00.082166-04 | 1427 (1 row)
-
查看模型摘要。
=> SELECT GET_MODEL_SUMMARY(USING PARAMETERS model_name='svm_class'); ------------------------------------------------------------------------ ======= details ======= predictor|coefficient ---------+----------- Intercept| -0.02006 cyl | 0.15367 mpg | 0.15698 wt | -1.78157 hp | 0.00957 =========== call_string =========== SELECT svm_classifier('public.svm_class', 'mtcars_train', '"am"', 'cyl, mpg, wt, hp, gear' USING PARAMETERS exclude_columns='gear', C=1, max_iterations=100, epsilon=0.001); =============== Additional Info =============== Name |Value ------------------+----- accepted_row_count| 20 rejected_row_count| 0 iteration_count | 12 (1 row)
另请参阅
6 - 查看模型
Vertica 将您创建的模型存储在 V_CATALOG.MODELS
系统表中。
您可以查询 V_CATALOG.MODELS
来查看您创建的模型的信息:
=> SELECT * FROM V_CATALOG.MODELS;
-[ RECORD 1 ]--+------------------------------------------
model_id | 45035996273765414
model_name | mySvmClassModel
schema_id | 45035996273704978
schema_name | public
owner_id | 45035996273704962
owner_name | dbadmin
category | VERTICA_MODELS
model_type | SVM_CLASSIFIER
is_complete | t
create_time | 2017-02-14 10:30:44.903946-05
size | 525
-[ RECORD 2 ]--+------------------------------------------
model_id | 45035996273711466
model_name | mtcars_normfit
schema_id | 45035996273704978
schema_name | public
owner_id | 45035996273704962
owner_name | dbadmin
category | VERTICA_MODELS
model_type | SVM_CLASSIFIER
is_complete | t
create_time | 2017-02-06 15:03:05.651941-05
size | 288