Tensorflow教程笔记

  1. 基础
    TensorFlow 基础
    TensorFlow 模型建立与训练
    基础示例:多层感知机(MLP)
    卷积神经网络(CNN)
    循环神经网络(RNN)
    深度强化学习(DRL)
    Keras Pipeline
    自定义层、损失函数和评估指标
    常用模块 tf.train.Checkpoint :变量的保存与恢复
    常用模块 TensorBoard:训练过程可视化
    常用模块 tf.data :数据集的构建与预处理
    常用模块 TFRecord :TensorFlow 数据集存储格式
    常用模块 tf.function :图执行模式
    常用模块 tf.TensorArray :TensorFlow 动态数组
    常用模块 tf.config:GPU 的使用与分配

  2. 部署
    TensorFlow 模型导出
    TensorFlow Serving
    TensorFlow Lite
    使用 TPU 训练 TensorFlow 模型

  3. 大规模训练与加速
    TensorFlow 分布式训练
    使用 TPU 训练 TensorFlow 模型

  4. 扩展
    TensorFlow Hub 模型复用
    TensorFlow Datasets 数据集载入

  5. 附录
    强化学习基础简介


目录

  • Tensorflow教程笔记
  • TPU 简介
    • 什么是 TPU
    • 为什么使用 TPU
    • TPU 性能
  • TPU 环境配置
    • 免费 TPU:Google Colab
    • Cloud TPU
  • TPU 基础使用

TPU 简介

什么是 TPU

TPU 代表 Tensor Processing Unit (张量处理单元) ,是由谷歌在 2016 年 5 月发布的为机器学习而构建的定制集成电路(ASIC),并为 TensorFlow 量身定制。

早在 2015 年,谷歌大脑团队就成立了第一个 TPU 中心,为 Google Translation,Photos 和 Gmail 等产品提供支持。 为了使所有数据科学家和开发人员能够访问此技术,不久之后就发布了易于使用,可扩展且功能强大的基于云的 TPU,以便在 Google Cloud 上运行 TensorFlow 模型。

TPU 由多个计算核心(Tensor Core)组成,其中包括标量矢量矩阵单元(MXU)。TPU(张量处理单元)与 CPU(中央处理单元)和 GPU(图形处理单元)最重要的区别是:TPU 的硬件专为线性代数而设计,线性代数是深度学习的基石。在过去几年中,Google TPU 已经发布了 v1,v2,v3, v2 Pod, v3 Pod, Edge 等多个版本:

1 Tera: 万亿,10 的 12 次方
1 Peta: 千万亿,10 的 15 次方
1 FLOPS:每秒浮点数计算次数(FLoating-point Operations Per Second)
1 OPS: 每秒位整数计算次数(Integer Operations Per Second)

基于 Google Cloud,TPU 可以方便的进行建立和使用。同时,Google 也推出了专门为边缘计算环境而部署的 Edge TPU。Edge TPU 尺寸小,功耗低,性能高,可以在边缘计算环境中广泛部署高质量的 AI。其作为 Cloud TPU 的补充,可以大大促进 AI 的解决方案在 IoT 环境中的部署。

为什么使用 TPU

通过使用 Cloud TPU ,我们可以大大提升 TensorFlow 进行机器学习训练和预测的性能,并能够灵活的帮助研究人员,开发人员和企业 TensorFlow 计算群集。

根据 Google 提供的数据显示,在 Google Cloud TPU Pod 上可以仅用 8 分钟就能够完成 ResNet-50 模型的训练。

ResNet-50

TPU 性能

根据研究显示,TPU 比现代 GPU 和 CPU 快 15 到 30 倍。同时,TPU 还实现了比传统芯片更好的能耗效率,算力能耗比值提高了 30 倍至 80 倍。

每个周期的操作次数

CPU 10
GPU 10,000
TPU 100,000

TPU 环境配置

免费 TPU:Google Colab

最方便使用 TPU 的方法,就是使用 Google 的 Colab ,不但通过浏览器访问直接可以用,而且还免费。

在 Google Colab 的 Notebook 界面中,打开界面中,打开主菜单 Runtime ,然后选择 Change runtime type,会弹出 Notebook settings 的窗口。选择里面的 Hardware accelerator 为 TPU 就可以了。

为了确认 Colab Notebook 中的确分配了 TPU 资源,我们可以运行以下测试代码。如果输出 ERROR 信息,则表示目前的 Runetime 并没有分配到 TPU;如果输出 TPU 地址及设备列表,则表示 Colab 已经分配了 TPU。

import os
import tensorflow as tfif 'COLAB_TPU_ADDR' not in os.environ:print('ERROR: Not connected to a TPU runtime')
else:tpu_address = 'grpc://' + os.environ['COLAB_TPU_ADDR']print ('TPU address is', tpu_address)

输出信息:

TPU address is grpc://10.49.237.2:8470

看到以上信息(TPU grpc address),既可以确认 Colab 的 TPU 环境设置正常。

Cloud TPU

在 Google Cloud 上,我们可以购买所需的 TPU 资源,用来按需进行机器学习训练。为了使用 Cloud TPU ,需要在 Google Cloud Engine 中启动 VM 并为 VM 请求 Cloud TPU 资源。请求完成后,VM 就可以直接访问分配给它专属的 Cloud TPU 了。

在使用 Cloud TPU 时,为了免除繁琐的驱动安装,我们可以通过直接使用 Google Cloud 提供的 VM 操作系统镜像。

TPU 基础使用

在 TPU 上进行 TensorFlow 分布式训练的核心 API 是 tf.distribute.TPUStrategy ,可以简单几行代码就实现在 TPU 上的分布式训练,同时也可以很容易的迁移到 GPU 单机多卡、多机多卡的环境。以下是如何实例化 TPUStrategy

tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
strategy = tf.distribute.experimental.TPUStrategy(tpu)

在上面的代码中,首先我们实例化 TPUClusterResolver;然后,我们连接 TPU Cluster,并对其进行初始化;最后,完成实例化 TPUStrategy

以下使用 Fashion MNIST 分类任务展示 TPU 的使用方式。本小节的源代码可以在 https://github.com/huan/tensorflow-handbook-tpu 找到。

更方便的是在 Google Colab 上直接打开本例子的 Jupyter 直接运行,地址:https://colab.research.google.com/github/huan/tensorflow-handbook-tpu/blob/master/tensorflow-handbook-tpu-example.ipynb (推荐)

import tensorflow as tf
import numpy as np
import os(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()# add empty color dimension
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)def create_model():model = tf.keras.models.Sequential()model.add(tf.keras.layers.Conv2D(64, (3, 3), input_shape=x_train.shape[1:]))model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2,2)))model.add(tf.keras.layers.Activation('relu'))model.add(tf.keras.layers.Flatten())model.add(tf.keras.layers.Dense(10))model.add(tf.keras.layers.Activation('softmax'))return modeltpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
strategy = tf.distribute.experimental.TPUStrategy(tpu)with strategy.scope():model = create_model()model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),loss=tf.keras.losses.sparse_categorical_crossentropy,metrics=[tf.keras.metrics.sparse_categorical_accuracy])model.fit(x_train.astype(np.float32), y_train.astype(np.float32),epochs=5,steps_per_epoch=60,validation_data=(x_test.astype(np.float32), y_test.astype(np.float32)),validation_freq=5
)

以上程序运行输出为:

Epoch 1/5
60/60 [==========] - 1s 23ms/step - loss: 12.7235 - accuracy: 0.7156
Epoch 2/5
60/60 [==========] - 1s 11ms/step - loss: 0.7600 - accuracy: 0.8598
Epoch 3/5
60/60 [==========] - 1s 11ms/step - loss: 0.4443 - accuracy: 0.8830
Epoch 4/5
60/60 [==========] - 1s 11ms/step - loss: 0.3401 - accuracy: 0.8972
Epoch 5/5
60/60 [==========] - 4s 60ms/step - loss: 0.2867 - accuracy: 0.9072
10/10 [==========] - 2s 158ms/step
10/10 [==========] - 2s 158ms/step
val_loss: 0.3893 - val_sparse_categorical_accuracy: 0.8848

【Tensorflow教程笔记】使用 TPU 训练 TensorFlow 模型相关推荐

  1. 【Tensorflow教程笔记】TensorFlow Datasets 数据集载入

    Tensorflow教程笔记 基础 TensorFlow 基础 TensorFlow 模型建立与训练 基础示例:多层感知机(MLP) 卷积神经网络(CNN) 循环神经网络(RNN) 深度强化学习(DR ...

  2. 【Tensorflow教程笔记】深度强化学习(DRL)

    基础 TensorFlow 基础 TensorFlow 模型建立与训练 基础示例:多层感知机(MLP) 卷积神经网络(CNN) 循环神经网络(RNN) 深度强化学习(DRL) Keras Pipeli ...

  3. 【Tensorflow教程笔记】常用模块 tf.function :图执行模式

    基础 TensorFlow 基础 TensorFlow 模型建立与训练 基础示例:多层感知机(MLP) 卷积神经网络(CNN) 循环神经网络(RNN) 深度强化学习(DRL) Keras Pipeli ...

  4. 【Tensorflow教程笔记】常用模块 tf.train.Checkpoint :变量的保存与恢复

    基础 TensorFlow 基础 TensorFlow 模型建立与训练 基础示例:多层感知机(MLP) 卷积神经网络(CNN) 循环神经网络(RNN) 深度强化学习(DRL) Keras Pipeli ...

  5. Tensorflow学习笔记6:解决tensorflow训练过程中GPU未调用问题

    Tensorflow学习笔记6:解决tensorflow训练过程中GPU未调用问题 参考文章: (1)Tensorflow学习笔记6:解决tensorflow训练过程中GPU未调用问题 (2)http ...

  6. tensorflow学习笔记九:将 TensorFlow 移植到 Android手机,实现物体识别、行人检测和图像风格迁移详细教程

    2017/02/23 更新 贴一个TensorFlow 2017开发者大会的Mobile专题演讲 移动和嵌入式TensorFlow 这里面有重点讲到本文介绍的三个例子,以及其他的移动和嵌入式方面的TF ...

  7. Tensorflow学习笔记4:分布式Tensorflow

    简介 Tensorflow API提供了Cluster.Server以及Supervisor来支持模型的分布式训练. 关于Tensorflow的分布式训练介绍可以参考Distributed Tenso ...

  8. TensorFlow学习笔记(1)--TensorFlow简介,常用基本操作

    要将深度学习更快且更便捷地应用于新的问题中,选择一款深度学习工具是必不可少的步骤. TensorFlow是谷歌于2015年11月9日正式开源的计算框架.TensorFlow计算框架可以很好地支持深度学 ...

  9. linux安装tensorflow教程,Ubuntu 16.04 安装 TensorFlow(GPU支持)

    本文记录Ubuntu 16.04安装Tensorflow步骤,也包括怎么从源码编译安装Tensorflow. 要想安装Tensorflow GPU版本,你需要有一个新一点的Nvidia显卡. Tens ...

  10. 【小白学习tensorflow教程】二、TensorBoard可视化模型训练

    @Author:Runsen 本想在Torch和Keras更新TensorBoard,还是决定扔在了tensorflow. TensorBoard是用于可视化图形和其他工具以理解.调试和优化模型的界面 ...

最新文章

  1. (转载)动态SLAM系统:VDO-SLAM!
  2. response.end
  3. 数据库大型应用解决方案总结(转)
  4. java监听变量的变化_[Java学习小记]使用PropertyChangeSupport来监听变量的变化
  5. Spring Boot 开发web 项目
  6. java switch语句_Java 14:查看更新的switch语句
  7. php5.0相等,关于php:3个相等
  8. 《python透明人士,他是凭什么成为主流编程的宠儿?!》python基础语法
  9. la环球乐园里的机器人_北京环球度假区发布小黄人乐园主题视频,抢鲜感受未来的欢乐体验...
  10. 阿里云 centos mysql 5.6_关于centOS安装配置mysql5.6那点事
  11. 1.0Tensorflow中出现编译问题的解决方案
  12. Java实现非对称加密算法-RSA加解密
  13. 拍人像的时候你喜欢哪种模特?
  14. make条件判断ifeq,ifneq,ifdef,ifndef
  15. 电子/自动化专业常用软件介绍
  16. Lipschitz function 是什么?Lipschitz continuous呢?
  17. Apereo CAS 5.0.X 默认提供的数据库认证的四种方式
  18. dcdc aam模式_AAM的完整形式是什么?
  19. thinkphp6 jwt扩展
  20. 用Python写一个程序,解密游戏内抽奖的秘密

热门文章

  1. excel图片根据表格内容动态变化
  2. java 读取 解析微软Project .mpp 文件
  3. eNSP交换机配置VLAN
  4. 使用el-tree组件当后台返回的数据过多导致页面反应时间过长或页面崩溃的问题及解决方案
  5. 微信第三方平台服务器,EasyWeChat微信开放平台第三方平台接入
  6. 百度 bos php,Thinkphp5对接百度云对象存储 BOS(代码示例)
  7. Python:寻找回文素数
  8. Outlining and Hiding Code----feature
  9. Vue + Spring Boot 项目实战(十四):用户认证方案与完善的访问拦截
  10. html给文字添加阴影效果,如何设置样式之添加文字阴影、边框阴影或者添加自己的CSS...