study record-building and deploying applications with tensorflow


目标:1、使用tensorflow创建模型
2、保存模型
3、部署模型到云平台
4、调用云平台模型
5、使用tensorboard监控模型运行情况

tensorboard使用步骤:
1、创建变量用来记录想要展示的执行节点(需在session实例化运行前定义)
2、在模型的session执行过程中将想要展示的节点记录在log中并存在磁盘中
  • 训练集日志和测试集日志保存在一个日志文件夹下,tensorboard会自动展示两组数据.
  • 日志中需要记录对应的需要展示的迭代的次数和该次迭代对应的loss值.(成本函数值)
  • session 执行过程中记录日志
3、程序执行完成之后,在终端中进行以下操作:
  • 切换到对应logs文件夹的上层文件夹位置( 使用cd 命令) 如果不切换的话,容易引起tensorboard展示空白
  • 执行tensorboard --logdir=logs
  • 将第二步执行后生成的链接复制到浏览器中,查看即可
  • 使用完后,在终端使用control+c关闭 tensorboard
4、每次执行前需要把上一次执行的log删除,不然执行日志会存在一份文件中,tensorboard展示会比较乱.
将每次执行的log分别存储在不同文件中操作:
1、定义变量: RUN_NAME= 'run 1 with 50 nodes'
2、存储日志时使用变量:
training_writer = tf.summary.FileWriter( "./logs/{}/training" .format(RUN_NAME), session.graph)
testing_writer = tf.summary.FileWriter( "./logs/{}/testing" .format(RUN_NAME), session.graph)
3、每次执行时使用更改变量“RUN_NAME”的值即可

日志示例:
#定于变量
with tf.variable_scope('logging'):
tf.summary.scalar('current_cost',cost)
summary=tf.summary.merge_all()
#initialize a session so that we can run tensorflow operations
with tf.Session() as session:
#run the global variable initializer to initialize all variables and layers of
session.run(tf.global_variables_initializer())
#create log file writers to record training progress.
#we'll store training and testing log data separately
training_writer=tf.summary.FileWriter("data/logs/training",session.graph) #指定存储训练集的log的位置
testing_writer=tf.summary.FileWriter("data/logs/testing",session.graph)#指定存储测试集的log的位置

for epoch in range(training_epochs): #每次迭代时计算
#feed in the training data and do one step of neural network tranining
session.run(optimizer,feed_dict={X:X_scaled_training,Y:Y_scaled_training})
#every 5 training steps,log our progress 每5次迭代记录一次日志
if epoch % 5 ==0:
training_cost, training_summary=session.run([cost, summary],feed_dict={X:X_scaled_training,Y:Y_scaled_training})
testing_cost, testing_summary=session.run([cost, summary],feed_dict={X:X_scaled_testing,Y:Y_scaled_testing})
#write the current training staus to the log files(which we can visai)
training_writer.add_summary(training_summary,epoch) #记录日志
testing_writer.add_summary(testing_summary,epoch) #记录日志


保存模型步骤:

1、在session实例化,运行前,定义 tf.train.Saver对象. saver= tf.train.Saver()
2、在程序结束/末尾处执行 tf.train.Saver.save()方法保存模型.
save_path=saver.save(session, "logs/trained_model.ckpt" )

使用已保存的模型:
1、 在session实例化,运行前,定义 tf.train.Saver对象. saver= tf.train.Saver()
2、 在session示例时,调用模型tf.train.Saver. restore ()
with tf.Session() as session:
#when loading from a chectpoint,don't initialize the variables!
# run the global variable initializer to initialize all variables and layers of
# session.run(tf.global_variables_initializer())
#insted,load them from disk:
saver.restore(session, "logs/trained_model.ckpt" )
tensorboard 定制化操作:
1、添加图片到tensorboard:
tf.summary.image()
2、添加音频到tensorboard:
tf.summary.audio()
3 、添加直方图到tensorboard:
# create a summary operation to log the progress of the network
with tf.variable_scope( 'logging' ):
tf.summary.scalar( 'current_cost' ,cost)
tf.summary.histogram( "predicted_value" ,prediction) #添加直方图监控执行过程
summary=tf.summary.merge_all()

部署机器学习模型到google服务步骤一:
1、代码末尾添加以下代码:
2、执行后生成的文件夹中,找到"saved_model.pd"
3、登录google cloud账户:
a 创建项目 ,查找项目id
b 去api管理界面,打开机器学习(google cloud machinelearning) 。 点击,启用.
3、去主菜单中,添加结算用的银行卡.
4、安装google cloud sdk,登录: cloud.google.com/sdk/downloads
选择交互式安装选项,下载并安装最新版本.
5、安装 google cloud sdk 后,使用命令行登录激活它.
打开电脑终端,运行: gcloud init 在弹出的页面中选择对应账户确认


model_builder=tf.saved_model.builder.SavedModelBuilder( "export model" )

inputs={
'input' :tf.saved_model.utils.build_tensor_info(X)
}
outputs={
'earnings' :tf.saved_model.utils.build_tensor_info(prediction)
}
signature_def=tf.saved_model.signature_def_utils.build_signature_def(
inputs =inputs,
outputs =outputs,
method_name =tf.saved_model.signature_constants.PREDICT_METHOD_NAME
)

model_builder.add_meta_graph_and_variables(
session,
tags =[tf.saved_model.tag_constants.SERVING],
signature_def_map ={
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY
}
)

model_builder.save()


部署机器学习模型到google服务步骤二:

1、确保步骤一对应的Google 账户已经配置,且模型已生成.
2、整理好需要输入的数据.
3、终端中,使用 cd命令 切换到生成的模型文件夹中.
4、运行命令: gsutil mb -l us-central1 gs://tensorflow-class_1000
gsutil:一个处理大量基本google服务操作的实用程序.(创建新存储桶、移动文件、更改权限等)
mb: make bucket 接下来告诉google 在哪个数据中心创建这个桶
us-central1:创建数据桶的位置.(美国中心)
gs:命名数据桶,名字必须全世界唯一.(tensorflow-class_1000)
5、将数据文件上传到创建的桶模型中:
gsutil cp -R exported_model/* gs://tensorflow-class_1000/earnings_v1/
cp:从本地电脑拷贝文件到云桶中
R:递归复制文件,上传的所有子文件夹都包含其中
exported_model: 上传文件的名称.
gs://tensorflow-class_1000/earnings_v1/ : 上传到云端哪个文件夹中.
6、告诉google 机器学习引擎,我们要创建一个新的模型.
gcloud ml -engine models create earnings --regions us-central1
gcloud ml -engine:调用谷歌云机器学习引擎
models create earnings: 创建模型“earnings”
regions us-central1:模型创建的存储位置,在 us-central1 区域

7、创建调用版本v1
gcloud ml -engine versions create v1 --model= earnings --origin=gs://tensorflow-class_1000/earnings_v1/


使用已经部署在google服务的应用:
1、建立并部署好云服务.
2、登录: console.cloud.google.com, 切换到api管理器页面.--查看凭证
3、创建凭证页面中,选择服务账户的密钥,选择使用服务的账户,点击创建,下载凭证文件.
4、重命名凭证文件,然后把文件复制到需要调用云服务的代码文件夹下.
5、修改调用模型文件,调用云服务.






一、installingmacos--tensorflow+pycharm
python3.0
1、pycharm . 2、anaconda . 3、tensorflow




tensorflow数据结构:
1、 tensor :张量, 多位阵列数据,用于存储需要执行的数据
2、Computational Graph: 计算图 ; 执行数据集计算的操作,确定数据在执行时如何流转.

tesorflow requirements:
1、developement phase
when you are coding and traning a neural network---usually do it in local computer
developement phase . requirements:
a windows,macOS or linux
b can use multiple linux computers (locally or in the cloud) for every large projects


2、runtime(or inference) phase
when you are making predictions with a trained neural network
-- 可以在自己电脑、用户电脑、云服务器、移动设备上完成.



本文是一篇学习笔记,详细图例未贴上,有兴趣的同学,可以留言进行交流.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值