PAI-STUDIO在支持OSS数据源的基础上,增加了对MaxCompute表的数据支持。用户可以直接使用PAI-STUDIO的Tensorflow组件读写MaxCompute数据,本教程将提供完整数据和代码供大家测试。

详细流程

为了方便用户快速上手,本文档将以训练iris数据集为例,介绍如何跑通实验。

1.读数据表组件

为了方便大家,我们提供了一份公共读的数据供大家测试,只要拖出读数据表组件,输入:

pai_online_project.iris_data

即可获取数据,

数据格式如图:

2.Tensorflow组件说明

3个输入桩从左到右分别是OSS输入、MaxCompute输入、模型输入。2个输出桩分别是模型输出、MaxCompute输出。如果输入是一个MaxCompute表,输出也是一个MaxCompute表,需要按下图方法连接。

读写MaxCompute表需要配置数据源、代码文件、输出模型路径、建表等操作。

  • Python代码文件:需要把执行代码放到OSS路径下(注意OSS需要与当前项目在同一区域),本文提供的代码可以在下方连接下载(代码需要按照下方代码说明文案调整):http://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/attach/129749/cn_zh/1565333220966/iristest.py?spm=a2c4g.11186623.2.10.50c46b36PlNwcq&file=iristest.py
  • Checkpoint输出目录/模型输入目录:选择自己的OSS路径用来存放模型
  • MaxCompute输出表:写MaxCompute表要求输出表是已经存在的表,并且输出的表名需要跟代码中的输出表名一致。在本案例中需要填写“iris_output”
  • 建表SQL语句:如果代码中的输出表并不存在,可以通过这个输入框输入建表语句自动建表。本案例中建表语句“create table iris_output(f1 DOUBLE,f2 DOUBLE,f3 DOUBLE,f4 DOUBLE,f5 STRING);”

组件PAI命令

PAI -name tensorflow180_ext -project algo_public -Doutputs="odps://${当前项目名}/tables/${输出表名}" -DossHost="${OSS的host}" -Dtables="odps://${当前项目名}/tables/${输入表名}" -DgpuRequired="${GPU卡数}" -Darn="${OSS访问RoleARN}" -Dscript="${执行的代码文件}";

上述命令中的${}需要替换成用户真实数据

3.代码说明

import tensorflow as tf
tf.app.flags.DEFINE_string("tables", "", "tables info") FLAGS = tf.app.flags.FLAGS print("tables:" + FLAGS.tables)
tables = [FLAGS.tables]
filename_queue = tf.train.string_input_producer(tables, num_epochs=1) reader = tf.TableRecordReader()
key, value = reader.read(filename_queue)
record_defaults = [[1.0], [1.0], [1.0], [1.0], ["Iris-virginica"]]
col1, col2, col3, col4, col5 = tf.decode_csv(value, record_defaults = record_defaults)
# line 9 and 10 can be written like below for short. It will be helpful when too many columns exist.
# record_defaults = [[1.0]] * 4 + [["Iris-virginica"]]
# value_list = tf.decode_csv(value, record_defaults = record_defaults) writer = tf.TableRecordWriter("odps://pai_bj_test2/tables/iris_output")
write_to_table = writer.write([0, 1, 2, 3, 4], [col1, col2, col3, col4, col5])
# line 16 can be written like below for short. It will be helpful when too many columns exist.
# write_to_table = writer.write(range(5), value_list) close_table = writer.close() init = tf.global_variables_initializer() with tf.Session() as sess:
sess.run(init)
sess.run(tf.local_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
try:
step = 0
while not coord.should_stop():
step += 1
sess.run(write_to_table)
except tf.errors.OutOfRangeError:
print('%d records copied' % step)
finally:
sess.run(close_table)
coord.request_stop()
coord.join(threads)

  • 读数据表

tables = [FLAGS.tables]
filename_queue = tf.train.string_input_producer(tables, num_epochs=1)
reader = tf.TableRecordReader()
key, value = reader.read(filename_queue)
record_defaults = [[1.0], [1.0], [1.0], [1.0], ["Iris-virginica"]]

其中FLAGS.tables是前端配置的输入表名的传参变量,对应组件的MaxCompute输入桩:

  • 写数据表

writer = tf.TableRecordWriter("odps://pai_bj_test2/tables/iris_output")
write_to_table = writer.write([0, 1, 2, 3, 4], [col1, col2, col3, col4, col5])

TableRecordWriter中的格式为odps://当前项目名/tables/输出表名

本文作者:傲海

原文链接

本文为云栖社区原创内容,未经允许不得转载。

最新文章

  1. ACM/ICPC 之 靠墙走-DFS+BFS(POJ3083)
  2. Cesium原理篇:2最长的一帧之网格划分
  3. PHP太怪了,in_array() ,strpos,
  4. C++ operator 知识点
  5. ED/EP系列2《文件结构》
  6. 使用RemObjects Pascal Script (转)
  7. MVC神韵---你想在哪解脱!(十三)
  8. 循环中continue和break的区别
  9. 利用autoit自动关闭指定标题窗口
  10. IOS 使用IOS6苹果地图
  11. VC socket Connect 超时时间设置
  12. C语言优化实例:为了消除嵌套switch-case聪明的做法
  13. Java数据结构与算法(5) - ch05链表(LinkList)
  14. C++类与对象(05)
  15. 坚持自己的追求,迎来 “中国系统开发网” (CSDN)的专访
  16. Redis数据结构和常用API
  17. 行为驱动开发BDD和Cucunber简介
  18. PyQt5——基本控件
  19. 查看CPU/CACHE的拓扑结构
  20. java程序中中常用到的linux操作

热门文章

  1. 深入浅出 Java Concurrency (29): 线程池 part 2 Executor 以及Executors[转]
  2. PAT甲级——A1105 Spiral Matrix【25】
  3. Windows下运行Tomcat闪退问题
  4. quartz任务调度基础: Job/Trigger/Schedule
  5. log4j的使用及与mybatis应用
  6. 08_springmvc数据回显和@ModelAttribute注解详解
  7. python3-常用模块之sys
  8. vue-admin-template模板添加tagsview
  9. 阿里云“网红"运维工程师白金:做一个平凡的圆梦人
  10. php条件语句(一)