[
  {
    "path": ".gitignore",
    "content": "*.aux\r\n*.fdb_latexmk\r\n*.log\r\n*.out\r\n*.synctex.gz\r\n*.toc\r\n*.fls\r\n*.sublime-project\r\n*.sublime-workspace\r\n*.DS_Store\r\n*.idea"
  },
  {
    "path": "README.md",
    "content": "# TensorFlow 官方文档中文版\r\n\r\n![](SOURCE/images/TensorFlow.jpg)   \r\n\r\n### 你正在阅读的项目可能会比 Android 系统更加深远地影响着世界！   \r\n\r\n## 缘起 \r\n\r\n2015年11月9日，Google发布人工智能系统TensorFlow并宣布开源，同日，极客学院组织在线TensorFlow中文文档翻译。\r\n\r\n机器学习作为人工智能的一种类型，可以让软件根据大量的数据来对未来的情况进行阐述或预判。如今，领先的科技巨头无不在机器学习下予以极大投入。Facebook、苹果、微软，甚至国内的百度。Google 自然也在其中。「TensorFlow」是 Google 多年以来内部的机器学习系统。如今，Google 正在将此系统成为开源系统，并将此系统的参数公布给业界工程师、学者和拥有大量编程能力的技术人员，这意味着什么呢？\r\n\r\n打个不太恰当的比喻，如今 Google 对待 TensorFlow 系统，有点类似于该公司对待旗下移动操作系统 Android。如果更多的数据科学家开始使用 Google 的系统来从事机器学习方面的研究，那么这将有利于 Google 对日益发展的机器学习行业拥有更多的主导权。\r\n\r\n为了让国内的技术人员在最短的时间内迅速掌握这一世界领先的 AI 系统，极客学院 Wiki 团队发起对 TensorFlow 官方文档的中文协同翻译，一周之内，全部翻译认领完成，一个月后，全部30章节翻译校对完成，上线极客学院Wiki平台并提供下载。\r\n\r\nGoogle TensorFlow项目负责人Jeff Dean为该中文翻译项目回信称：\"*看到能够将TensorFlow翻译成中文我非常激动，我们将TensorFlow开源的主要原因之一是为了让全世界的人们能够从机器学习与人工智能中获益，类似这样的协作翻译能够让更多的人更容易地接触到TensorFlow项目，很期待接下来该项目在全球范围内的应用!*\"  \r\n\r\nJeff回信原文：\r\n\r\n![jeff](SOURCE/images/jeff.png)\r\n\r\n再次衷心感谢每一位为该翻译项目做出贡献的同学，我们会持续关注TensorFlow、AI领域以及其它最新技术的发展、持续维护该协作翻译、持续提供更多更优质的内容，为广大IT学习者们服务！\r\n\r\n## 内容来源\r\n\r\n英文官方网站：     \r\n<http://tensorflow.org/>\r\n\r\n官方GitHub仓库：   \r\n<https://github.com/tensorflow/tensorflow>\r\n\r\n中文版 GitHub 仓库：  \r\n<https://github.com/jikexueyuanwiki/tensorflow-zh>\r\n\r\n## 参与者（按认领章节排序）\r\n\r\n### 翻译   \r\n\r\n- [@PFZheng](https://github.com/PFZheng)      \r\n- [@Tony Jin](https://github.com/linbojin) \r\n- [@chenweican](https://github.com/chenweican)\r\n- [@bingjin](https://github.com/bingjin)\r\n- [@oskycar](https://github.com/oskycar)\r\n- [@btpeter](https://github.com/btpeter)\r\n- [@Warln](https://github.com/Warln)\r\n- [@ericxk](https://github.com/ericxk)\r\n- [@wangaicc](https://github.com/wangaicc)\r\n- [@Terence Cooper](https://github.com/TerenceCooper)\r\n- [@zhyhooo](https://github.com/zhyhooo)\r\n- [@thylaco1eo](https://github.com/thylaco1eo)\r\n- [@volvet](https://github.com/volvet)\r\n- [@zhangkom](https://github.com/zhangkom)\r\n- [@derekshang](https://github.com/derekshang)\r\n- [@lianghyv](https://github.com/lianghyv)\r\n- [@nb312](https://github.com/nb312)\r\n- [@Jim-Zenn](https://github.com/Jim-Zenn)\r\n- [@andyiac](https://github.com/andyiac)\r\n- [@Terence Cooper](https://github.com/TerenceCooper)\r\n- [@leege100](https://github.com/leege100)\r\n\r\n### 校对\r\n\r\n- [@yangtze](https://github.com/sstruct)\r\n- [@ericxk](https://github.com/ericxk)\r\n- [@HongyangWang](https://github.com/WangHong-yang)\r\n- [@LichAmnesia](https://github.com/LichAmnesia)\r\n- [@zhyhooo](https://github.com/zhyhooo)\r\n- [@waiwaizheng](https://github.com/waiwaizheng)\r\n- [@HongyangWang](https://github.com/WangHong-yang)\r\n- [@tensorfly](https://github.com/tensorfly)\r\n- [@lonlonago](https://github.com/lonlonago)\r\n- [@jishaoming](https://github.com/jishaoming)\r\n- [@lucky521](https://github.com/lucky521)\r\n- [@allensummer](http://github.com/allensummer)\r\n- [@volvet](https://github.com/volvet)\r\n- [@ZHNathanielLee](https://github.com/ZHNathanielLee)\r\n- [@pengfoo](https://github.com/PengFoo)\r\n- [@qiaohaijun](https://github.com/qiaohaijun)\r\n- [@Seika](https://github.com/SeikaScarlet)\r\n\r\n## 进度记录\r\n\r\n- 2015-11-10, 谷歌发布全新人工智能系统TensorFlow并宣布开源, 极客学院Wiki启动协同翻译，创建 GitHub 仓库，制定协同规范 \r\n- 2015-11-18, 所有章节认领完毕，翻译完成18章，校对认领7章，Star数361，fork数100，协同翻译QQ群及技术交流群的TF爱好者将近300人，GitHub搜索TensorFlow排名第二\r\n- 2015-12-10, Star数超过500\r\n- 2015-12-15, 项目正式上线\r\n\r\n## 花絮\r\n\r\n在组织翻译的过程中，有些事情令人印象深刻，记录下来，希望以后来学习文档的同学能够明了到手中这份文档的由来：\r\n\r\n- 参加翻译的有学生，也有老师；有专门研究AI/ML的，也有对此感兴趣的；有国内的，也有远在纽约的；有工程技术人员也有博士、专家\r\n- 其中一位，[恩泽](http://www.longmotto.com)同学，为了翻译一篇文档，在前一天没有睡觉的情况下坚持翻完，20个小时没有合眼\r\n- 还有一位老师，刚从讲台上讲完课，就立即给我们的翻译提修改意见\r\n- 很多同学自发的将搭建环境中遇到的问题总结到FAQ里帮助他人\r\n- 为了一个翻译细节，经常是来回几次，和其他人讨论完善\r\n\r\n## 持续改进\r\n\r\n这样的一个高技术领域的文档，我们在翻译的过程中，难免会有不完善的地方，希望请大家一起帮助我们持续改进文档的翻译质量，帮助更多的人，方法：\r\n\r\n- 在GitHub上提Issue或Pull Request，地址为: [https://github.com/jikexueyuanwiki/tensorflow-zh](https://github.com/jikexueyuanwiki/tensorflow-zh)\r\n- 加入TensorFlow技术交流群，与TensorFlower们一起研究交流技术干货--TensorFlow技术交流群：782484288\r\n- 对翻译感兴趣？加入协同翻译群：248320884，与翻译大神一道研究TensorFlow的本地化\r\n- 给我们写邮件： wiki@jikexueyuan.com\r\n\r\n## 感谢支持\r\n\r\n- [极客学院 Wiki](http://wiki.jikexueyuan.com) 提供图文教程托管服务\r\n\r\n## 离线版本\r\n\r\n目前，离线版本(PDF、ePub)可正常下载、使用\r\n\r\n## Tex-PDF 修订版\r\n\r\n[Tex-PDF 修订版](tex_pdf) 目前正在编订中，欢迎加入进来一起修订。您可以在此查看[预览版](tex_pdf/tensorflow_manual_cn.pdf)目前最新状态。\r\n"
  },
  {
    "path": "README副本.md",
    "content": "# TensorFlow 官方文档中文版\r\n\r\n![](SOURCE/images/tensorflow_logo.png)\r\n\r\n### 你正在翻译的项目可能会比 Android 系统更加深远地影响着世界！\r\n\r\n## 缘起 \r\n\r\n2015年11月9日，Google 官方在其博客上称，Google Research 宣布推出第二代机器学习系统 TensorFlow，针对先前的 DistBelief 的短板有了各方面的加强，更重要的是，它是开源的，任何人都可以用。\r\n\r\n机器学习作为人工智能的一种类型，可以让软件根据大量的数据来对未来的情况进行阐述或预判。如今，领先的科技巨头无不在机器学习下予以极大投入。Facebook、苹果、微软，甚至国内的百度。Google 自然也在其中。「TensorFlow」是 Google 多年以来内部的机器学习系统。如今，Google 正在将此系统成为开源系统，并将此系统的参数公布给业界工程师、学者和拥有大量编程能力的技术人员，这意味着什么呢？\r\n\r\n打个不太恰当的比喻，如今 Google 对待 TensorFlow 系统，有点类似于该公司对待旗下移动操作系统 Android。如果更多的数据科学家开始使用 Google 的系统来从事机器学习方面的研究，那么这将有利于 Google 对日益发展的机器学习行业拥有更多的主导权。\r\n\r\n为了让国内的技术人员在最短的时间内迅速掌握这一世界领先的 AI 系统，极客学院 Wiki 团队发起对 TensorFlow 官方文档的中文协同翻译。\r\n\r\n欢迎各路人工智能及机器学习领域的专家和爱好者参与这一翻译项目，我们会为每位翻译和校对人员署名。\r\n\r\n## 一起来参与\r\n\r\n如果想做出贡献(翻译或者校对)的话，请加QQ群：248320884，谢谢！\r\n\r\nPS: 想探讨TensorFlow技术的可以加\"TensorFlow技术交流群\"：495115006\r\n\r\n## 内容来源\r\n\r\n英文官方网站：     \r\n<http://tensorflow.org/>\r\n\r\n官方GitHub仓库：   \r\n<https://github.com/tensorflow/tensorflow>\r\n\r\n中文版 GitHub 仓库：  \r\n<https://github.com/jikexueyuanwiki/tensorflow-zh>\r\n\r\n## 关于TensorFlow的一些报道   \r\n\r\n[野心：像开放安卓一样，这一次，谷歌决定开放它的机器学习系统——TensorFlow](http://www.huxiu.com/article/130763/1.html?f=wangzhan)   \r\n\r\n[最强大机器学习平台开源 谷歌准备主宰人工智能？](http://m.wallstreetcn.com/node/226012)\r\n\r\n## 参与步骤\r\n\r\n* fork主仓库（<https://github.com/jikexueyuanwiki/tensorflow-zh>）\r\n* 按照章节认领翻译(每次申请一个章节)或者校对(可申请多个章节)在下面这个`README.md`里找还没有被人申请的章节，写上（@你的github号），给主仓库的`master`分支提pull request；\r\n* 提的 pull request 被确认，合并到主仓库后，代表你申请的章节*认领*完成，开始翻译或校对；\r\n* 翻译或校对的文件为README.md或者TOC.md中对应的md文件，请不要翻译/校对单独文件夹中的index.md\r\n* 翻译过程请参照 *翻译协作规范* (见下一节)，完成翻译后提交 pull request 给主仓库的`master`分支；\r\n* 完成校对后同样提交 pull request 给主仓库的`master`分支；\r\n* 全部翻译/校对完成后，我们会生成PDF/ePub文档，放在极客学院Wiki平台发布，并为所有参与者署名！\r\n\r\n## 翻译协作规范   \r\n\r\n为了让大家协作顺畅，需要每一个人遵循如下协作规范~\r\n\r\n- 如果对Markdown和GitHub不了解，请先阅读[如何使用Markdown](markdown.md)以及[如何使用GitHub](learn-github.md)\r\n- 使用Markdown进行翻译，文件名必须使用英文\r\n- 翻译后的文档请放到SOURCE文件夹下的对应章节中，然后pull request即可\r\n- 如遇到文中的图片，请统一放在SOURCE/images目录下\r\n- 原文中的HTML标签及代码请不要修改、翻译\r\n- 有其他任何问题都欢迎发issue，我们看到了会尽快回复\r\n- 翻译人员需将对应的原文地址和翻译人姓名添加到译文末尾，审校人员需要将自己的名字添加到译文末尾，具体格式请参见样例：   \r\n\r\n> 原文：[Color Palettes](http://www.google.com/design/spec/resources/color-palettes.html)  翻译：[iceskysl](https://github.com/iceskysl)  校对：[PoppinLp](https://github.com/poppinlp)   \r\n\r\n## 校对规范\r\n\r\n- 认领校对时请提供机器学习 or AI方面的专业背景说明，保证校对质量\r\n- 文章通顺，适合人类阅读与理解，别让人以为是机翻\r\n- 确保图片都能正常显示，且其相对地址都是SOURCE/images，图片存放正确\r\n- 专有名词符合术语表中的翻译要求，拿不准的新词汇可以使用中文翻译后加括号内英文的形式\r\n- 校对人员需有一定专业背景，保证译文无专业知识方面错误\r\n- 请查看翻译人员是否将原文链接和翻译链接放到译文最下方，审校人员需要将自己的名字添加到译文末尾，具体格式请参见样例：   \r\n\r\n> 原文：[Color Palettes](http://www.google.com/design/spec/resources/color-palettes.html)  翻译：[iceskysl](https://github.com/iceskysl)  校对：[PoppinLp](https://github.com/poppinlp)   \r\n\r\n- 有任何问题请提Issues或到协同翻译群讨论，校对后提PR等待Merge，管理员通过后会在目录后面打√标识完成\r\n\r\n## 参与者（按认领章节排序）\r\n\r\n### 翻译 & 校对\r\n\r\n- 起步\r\n  - [简介](get_started/introduction.md) 翻译：（[@PFZheng](https://github.com/PFZheng)）√ 校对：（[@yangtze](https://github.com/sstruct)）√\r\n  - [下载及安装](get_started/os_setup.md) 翻译：（[@PFZheng](https://github.com/PFZheng)）√ 校对： （[@yangtze](https://github.com/sstruct)）√\r\n  - [基本用法](get_started/basic_usage.md) 翻译：（[@PFZheng](https://github.com/PFZheng)）√ 校对：（[@yangtze](https://github.com/sstruct)）√\r\n- 教程\r\n  - [综述](tutorials/overview.md) 翻译：（[@PFZheng](https://github.com/PFZheng)）√ 校对： ([@ericxk](https://github.com/ericxk)) √\r\n  - [MNIST机器学习入门](tutorials/mnist_beginners.md) 翻译：([@Tony Jin](https://github.com/linbojin))√ 校对： ([@ericxk](https://github.com/ericxk))\r\n  - [深入MNIST](tutorials/mnist_pros.md) 翻译：([@chenweican](https://github.com/chenweican))√  校对： ([@HongyangWang](https://github.com/WangHong-yang)) √\r\n  - [TensorFlow运作方式入门](tutorials/mnist_tf.md) 翻译：([@bingjin](https://github.com/bingjin))√  校对： ([@LichAmnesia](https://github.com/LichAmnesia)) √\r\n  - [卷积神经网络](tutorials/deep_cnn.md) 翻译： ([@oskycar](https://github.com/oskycar))√  校对： ([@zhyhooo](https://github.com/zhyhooo))\r\n  - [Vector Representations of Words](tutorials/word2vec.md)翻译： ([@btpeter](https://github.com/btpeter)) √ 校对： ([@waiwaizheng](https://github.com/waiwaizheng))\r\n  - [递归神经网络](tutorials/recurrent.md) 翻译：([@Warln](https://github.com/Warln))√  校对： ([@HongyangWang](https://github.com/WangHong-yang))\r\n  - [曼德布洛特(Mandelbrot)集合](tutorials/mandelbrot.md) 翻译：([@ericxk](https://github.com/ericxk))√  校对： ([@tensorfly](https://github.com/tensorfly))√\r\n  - [偏微分方程](tutorials/pdes.md) 翻译：([@wangaicc](https://github.com/wangaicc))√  校对： ([@tensorfly](https://github.com/tensorfly))√\r\n  - [MNIST数据下载](tutorials/mnist_download.md) 翻译： ([@btpeter](https://github.com/btpeter)) √ 校对： ([@waiwaizheng](https://github.com/waiwaizheng))\r\n- 运作方式\r\n  - [总览](how_tos/overview.md) 翻译： ([@Terence Cooper](https://github.com/TerenceCooper))√ 校对：\r\n  （[@lonlonago](https://github.com/lonlonago)) √\r\n  - [变量:创建、初始化、保存和加载](how_tos/variables.md) 翻译： ([@zhyhooo](https://github.com/zhyhooo))√ 校对： ([@jishaoming](https://github.com/jishaoming))\r\n  - [TensorBoard:可视化学习](how_tos/summaries_and_tensorboard.md)翻译：([@thylaco1eo](https://github.com/thylaco1eo))√  校对： ([@lucky521](https://github.com/lucky521)) √\r\n  - [TensorBoard:图表可视化](how_tos/graph_viz.md)  翻译：([@Warln](https://github.com/Warln))√  校对： ([@lucky521](https://github.com/lucky521)) √\r\n  - [读取数据](how_tos/reading_data.md)  翻译： ([@volvet](https://github.com/volvet)) 校对： ([@allensummer](http://github.com/allensummer))\r\n  - [线程和队列](how_tos/threading_and_queues.md) 翻译： ([@zhangkom](https://github.com/zhangkom)) √ 校对：（[@volvet](https://github.com/volvet))√\r\n  - [添加新的Op](how_tos/adding_an_op.md) 翻译：（[@PFZheng](https://github.com/PFZheng)）√校对：（[@ZHNathanielLee](https://github.com/ZHNathanielLee)) √\r\n  - [自定义数据读取](how_tos/new_data_formats.md) 翻译：([@derekshang](https://github.com/derekshang))√ 校对：([@pengfoo](https://github.com/PengFoo))\r\n  - [使用gpu](how_tos/using_gpu.md)翻译：([@lianghyv](https://github.com/lianghyv))√ 校对：([@qiaohaijun](https://github.com/qiaohaijun))\r\n  - [共享变量](how_tos/variable_scope.md) 翻译：([@nb312](https://github.com/nb312))√校对：([@pengfoo](https://github.com/PengFoo))\r\n- 资源\r\n  - [总览](resources/overview.md) 翻译：([@Jim-Zenn](https://github.com/Jim-Zenn))√ 校对：（[@lonlonago](https://github.com/lonlonago)) √\r\n  - [BibTex 引用](resources/bib.md) 翻译：([@Jim-Zenn](https://github.com/Jim-Zenn))√ 校对：([@pengfoo](https://github.com/PengFoo)) \r\n  - [使用示例](resources/uses.md) 翻译：([@andyiac](https://github.com/andyiac))√ 校对（[@lonlonago](https://github.com/lonlonago)) √\r\n  - [FAQ](resources/faq.md) 翻译：([@Terence Cooper](https://github.com/TerenceCooper))√ 校对：([@pengfoo](https://github.com/PengFoo))\r\n  - [术语表](resources/glossary.md) 翻译： ([@leege100](https://github.com/leege100))√ 校对：\r\n  （[@lonlonago](https://github.com/lonlonago)) √\r\n  - [Tensor的阶、形状和数据类型](resources/dims_types.md) 翻译：([@nb312](https://github.com/nb312))√ 校对：（[@lonlonago](https://github.com/lonlonago)) √\r\n- 其他\r\n  - [常见问题汇总](faq.md)\r\n\r\n## 进度记录\r\n\r\n- 2015-11-10, 谷歌发布全新人工智能系统TensorFlow并宣布开源, 极客学院Wiki启动协同翻译，创建 GitHub 仓库，制定协同规范 \r\n- 2015-11-18, 所有章节认领完毕，翻译完成18章，校对认领7章，Star数361，fork数100，协同翻译QQ群及技术交流群的TF爱好者将近300人，GitHub搜索TensorFlow排名第二\r\n- 2015-12-10, Star数超过500\r\n\r\n## TensorFlow 相关资源\r\n\r\n- [Google官方Blog宣布TensorFlow开源](https://googleblog.blogspot.com/2015/11/tensorflow-smarter-machine-learning-for.html)\r\n- [TensorFlow WhitePaper(PDF下载)](http://download.tensorflow.org/paper/whitepaper2015.pdf)\r\n- [Jeff Dean 介绍 TensorFlow（视频）](https://www.youtube.com/watch?v=90-S1M7Ny_o&t=21m2s)\r\n- [TensorFlow 简化版接口 Scikit Flow](https://github.com/google/skflow)\r\n- [TensorFlow 使用样例](https://github.com/aymericdamien/TensorFlow-Examples)\r\n- [TensorFlow 与 mxnet, caffe 对比 @chenrudan](http://chenrudan.github.io/blog/2015/11/18/comparethreeopenlib.html)\r\n- [TensorFlow 与 torch7, caffe, mxnet 在内存使用量以及性能对比 @mli](https://github.com/dmlc/mxnet/blob/master/doc/overview_chn.md)\r\n\r\n## 感谢支持\r\n\r\n## 离线版本\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/ClassEnv.md",
    "content": "# Class `tensorflow::Env` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--env-\"></a>\r\n\r\nAn interface used by the tensorflow implementation to access operating system functionality like the filesystem etc.\r\n\r\nCallers may wish to provide a custom Env object to get fine grain control.\r\n\r\nAll Env implementations are safe for concurrent access from multiple threads without any external synchronization.\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::Env::Env()`](#tensorflow_Env_Env)\r\n* [`virtual tensorflow::Env::~Env()`](#virtual_tensorflow_Env_Env)\r\n* [`virtual Status tensorflow::Env::NewRandomAccessFile(const string &fname, RandomAccessFile **result)=0`](#virtual_Status_tensorflow_Env_NewRandomAccessFile)\r\n  * Creates a brand new random access read-only file with the specified name.\r\n* [`virtual Status tensorflow::Env::NewWritableFile(const string &fname, WritableFile **result)=0`](#virtual_Status_tensorflow_Env_NewWritableFile)\r\n  * Creates an object that writes to a new file with the specified name.\r\n* [`virtual Status tensorflow::Env::NewAppendableFile(const string &fname, WritableFile **result)=0`](#virtual_Status_tensorflow_Env_NewAppendableFile)\r\n  * Creates an object that either appends to an existing file, or writes to a new file (if the file does not exist to begin with).\r\n* [`virtual bool tensorflow::Env::FileExists(const string &fname)=0`](#virtual_bool_tensorflow_Env_FileExists)\r\n  * Returns true iff the named file exists.\r\n* [`virtual Status tensorflow::Env::GetChildren(const string &dir, std::vector< string > *result)=0`](#virtual_Status_tensorflow_Env_GetChildren)\r\n  * Stores in *result the names of the children of the specified directory. The names are relative to \"dir\".\r\n* [`virtual Status tensorflow::Env::DeleteFile(const string &fname)=0`](#virtual_Status_tensorflow_Env_DeleteFile)\r\n  * Deletes the named file.\r\n* [`virtual Status tensorflow::Env::CreateDir(const string &dirname)=0`](#virtual_Status_tensorflow_Env_CreateDir)\r\n  * Creates the specified directory.\r\n* [`virtual Status tensorflow::Env::DeleteDir(const string &dirname)=0`](#virtual_Status_tensorflow_Env_DeleteDir)\r\n  * Deletes the specified directory.\r\n* [`virtual Status tensorflow::Env::GetFileSize(const string &fname, uint64 *file_size)=0`](#virtual_Status_tensorflow_Env_GetFileSize)\r\n  * Stores the size of fname in *file_size.\r\n* [`virtual Status tensorflow::Env::RenameFile(const string &src, const string &target)=0`](#virtual_Status_tensorflow_Env_RenameFile)\r\n  * Renames file src to target. If target already exists, it will be replaced.\r\n* [`virtual uint64 tensorflow::Env::NowMicros()=0`](#virtual_uint64_tensorflow_Env_NowMicros)\r\n  * Returns the number of micro-seconds since some fixed point in time. Only useful for computing deltas of time.\r\n* [`virtual void tensorflow::Env::SleepForMicroseconds(int micros)=0`](#virtual_void_tensorflow_Env_SleepForMicroseconds)\r\n  * Sleeps/delays the thread for the prescribed number of micro-seconds.\r\n* [`virtual Thread* tensorflow::Env::StartThread(const ThreadOptions &thread_options, const string &name, std::function< void()> fn) TF_MUST_USE_RESULT=0`](#virtual_Thread_tensorflow_Env_StartThread)\r\n  * Returns a new thread that is running fn() and is identified (for debugging/performance-analysis) by \"name\".\r\n* [`static Env* tensorflow::Env::Default()`](#static_Env_tensorflow_Env_Default)\r\n  * Returns a default environment suitable for the current operating system.\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::Env::Env()` <a class=\"md-anchor\" id=\"tensorflow_Env_Env\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual tensorflow::Env::~Env()` <a class=\"md-anchor\" id=\"virtual_tensorflow_Env_Env\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::Env::NewRandomAccessFile(const string &fname, RandomAccessFile **result)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_NewRandomAccessFile\"></a>\r\n\r\nCreates a brand new random access read-only file with the specified name.\r\n\r\nOn success, stores a pointer to the new file in *result and returns OK. On failure stores NULL in *result and returns non-OK. If the file does not exist, returns a non-OK status.\r\n\r\nThe returned file may be concurrently accessed by multiple threads.\r\n\r\n#### `virtual Status tensorflow::Env::NewWritableFile(const string &fname, WritableFile **result)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_NewWritableFile\"></a>\r\n\r\nCreates an object that writes to a new file with the specified name.\r\n\r\nDeletes any existing file with the same name and creates a new file. On success, stores a pointer to the new file in *result and returns OK. On failure stores NULL in *result and returns non-OK.\r\n\r\nThe returned file will only be accessed by one thread at a time.\r\n\r\n#### `virtual Status tensorflow::Env::NewAppendableFile(const string &fname, WritableFile **result)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_NewAppendableFile\"></a>\r\n\r\nCreates an object that either appends to an existing file, or writes to a new file (if the file does not exist to begin with).\r\n\r\nOn success, stores a pointer to the new file in *result and returns OK. On failure stores NULL in *result and returns non-OK.\r\n\r\nThe returned file will only be accessed by one thread at a time.\r\n\r\n#### `virtual bool tensorflow::Env::FileExists(const string &fname)=0` <a class=\"md-anchor\" id=\"virtual_bool_tensorflow_Env_FileExists\"></a>\r\n\r\nReturns true iff the named file exists.\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::Env::GetChildren(const string &dir, std::vector< string > *result)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_GetChildren\"></a>\r\n\r\nStores in *result the names of the children of the specified directory. The names are relative to \"dir\".\r\n\r\nOriginal contents of *results are dropped.\r\n\r\n#### `virtual Status tensorflow::Env::DeleteFile(const string &fname)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_DeleteFile\"></a>\r\n\r\nDeletes the named file.\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::Env::CreateDir(const string &dirname)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_CreateDir\"></a>\r\n\r\nCreates the specified directory.\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::Env::DeleteDir(const string &dirname)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_DeleteDir\"></a>\r\n\r\nDeletes the specified directory.\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::Env::GetFileSize(const string &fname, uint64 *file_size)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_GetFileSize\"></a>\r\n\r\nStores the size of fname in *file_size.\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::Env::RenameFile(const string &src, const string &target)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_RenameFile\"></a>\r\n\r\nRenames file src to target. If target already exists, it will be replaced.\r\n\r\n\r\n\r\n#### `virtual uint64 tensorflow::Env::NowMicros()=0` <a class=\"md-anchor\" id=\"virtual_uint64_tensorflow_Env_NowMicros\"></a>\r\n\r\nReturns the number of micro-seconds since some fixed point in time. Only useful for computing deltas of time.\r\n\r\n\r\n\r\n#### `virtual void tensorflow::Env::SleepForMicroseconds(int micros)=0` <a class=\"md-anchor\" id=\"virtual_void_tensorflow_Env_SleepForMicroseconds\"></a>\r\n\r\nSleeps/delays the thread for the prescribed number of micro-seconds.\r\n\r\n\r\n\r\n#### `virtual Thread* tensorflow::Env::StartThread(const ThreadOptions &thread_options, const string &name, std::function< void()> fn) TF_MUST_USE_RESULT=0` <a class=\"md-anchor\" id=\"virtual_Thread_tensorflow_Env_StartThread\"></a>\r\n\r\nReturns a new thread that is running fn() and is identified (for debugging/performance-analysis) by \"name\".\r\n\r\nCaller takes ownership of the result and must delete it eventually (the deletion will block until fn() stops running).\r\n\r\n#### `static Env* tensorflow::Env::Default()` <a class=\"md-anchor\" id=\"static_Env_tensorflow_Env_Default\"></a>\r\n\r\nReturns a default environment suitable for the current operating system.\r\n\r\nSophisticated users may wish to provide their own Env implementation instead of relying on this default environment.\r\n\r\nThe result of Default() belongs to this library and must never be deleted.\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/ClassEnvWrapper.md",
    "content": "# Class `tensorflow::EnvWrapper` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--envwrapper-\"></a>\r\n\r\nAn implementation of Env that forwards all calls to another Env .\r\n\r\nMay be useful to clients who wish to override just part of the functionality of another Env .\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::EnvWrapper::EnvWrapper(Env *t)`](#tensorflow_EnvWrapper_EnvWrapper)\r\n  * Initializes an EnvWrapper that delegates all calls to *t.\r\n* [`virtual tensorflow::EnvWrapper::~EnvWrapper()`](#virtual_tensorflow_EnvWrapper_EnvWrapper)\r\n* [`Env* tensorflow::EnvWrapper::target() const`](#Env_tensorflow_EnvWrapper_target)\r\n  * Returns the target to which this Env forwards all calls.\r\n* [`Status tensorflow::EnvWrapper::NewRandomAccessFile(const string &f, RandomAccessFile **r) override`](#Status_tensorflow_EnvWrapper_NewRandomAccessFile)\r\n  * Creates a brand new random access read-only file with the specified name.\r\n* [`Status tensorflow::EnvWrapper::NewWritableFile(const string &f, WritableFile **r) override`](#Status_tensorflow_EnvWrapper_NewWritableFile)\r\n  * Creates an object that writes to a new file with the specified name.\r\n* [`Status tensorflow::EnvWrapper::NewAppendableFile(const string &f, WritableFile **r) override`](#Status_tensorflow_EnvWrapper_NewAppendableFile)\r\n  * Creates an object that either appends to an existing file, or writes to a new file (if the file does not exist to begin with).\r\n* [`bool tensorflow::EnvWrapper::FileExists(const string &f) override`](#bool_tensorflow_EnvWrapper_FileExists)\r\n  * Returns true iff the named file exists.\r\n* [`Status tensorflow::EnvWrapper::GetChildren(const string &dir, std::vector< string > *r) override`](#Status_tensorflow_EnvWrapper_GetChildren)\r\n  * Stores in *result the names of the children of the specified directory. The names are relative to \"dir\".\r\n* [`Status tensorflow::EnvWrapper::DeleteFile(const string &f) override`](#Status_tensorflow_EnvWrapper_DeleteFile)\r\n  * Deletes the named file.\r\n* [`Status tensorflow::EnvWrapper::CreateDir(const string &d) override`](#Status_tensorflow_EnvWrapper_CreateDir)\r\n  * Creates the specified directory.\r\n* [`Status tensorflow::EnvWrapper::DeleteDir(const string &d) override`](#Status_tensorflow_EnvWrapper_DeleteDir)\r\n  * Deletes the specified directory.\r\n* [`Status tensorflow::EnvWrapper::GetFileSize(const string &f, uint64 *s) override`](#Status_tensorflow_EnvWrapper_GetFileSize)\r\n  * Stores the size of fname in *file_size.\r\n* [`Status tensorflow::EnvWrapper::RenameFile(const string &s, const string &t) override`](#Status_tensorflow_EnvWrapper_RenameFile)\r\n  * Renames file src to target. If target already exists, it will be replaced.\r\n* [`uint64 tensorflow::EnvWrapper::NowMicros() override`](#uint64_tensorflow_EnvWrapper_NowMicros)\r\n  * Returns the number of micro-seconds since some fixed point in time. Only useful for computing deltas of time.\r\n* [`void tensorflow::EnvWrapper::SleepForMicroseconds(int micros) override`](#void_tensorflow_EnvWrapper_SleepForMicroseconds)\r\n  * Sleeps/delays the thread for the prescribed number of micro-seconds.\r\n* [`Thread* tensorflow::EnvWrapper::StartThread(const ThreadOptions &thread_options, const string &name, std::function< void()> fn) override`](#Thread_tensorflow_EnvWrapper_StartThread)\r\n  * Returns a new thread that is running fn() and is identified (for debugging/performance-analysis) by \"name\".\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::EnvWrapper::EnvWrapper(Env *t)` <a class=\"md-anchor\" id=\"tensorflow_EnvWrapper_EnvWrapper\"></a>\r\n\r\nInitializes an EnvWrapper that delegates all calls to *t.\r\n\r\n\r\n\r\n#### `virtual tensorflow::EnvWrapper::~EnvWrapper()` <a class=\"md-anchor\" id=\"virtual_tensorflow_EnvWrapper_EnvWrapper\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `Env* tensorflow::EnvWrapper::target() const` <a class=\"md-anchor\" id=\"Env_tensorflow_EnvWrapper_target\"></a>\r\n\r\nReturns the target to which this Env forwards all calls.\r\n\r\n\r\n\r\n#### `Status tensorflow::EnvWrapper::NewRandomAccessFile(const string &f, RandomAccessFile **r) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_NewRandomAccessFile\"></a>\r\n\r\nCreates a brand new random access read-only file with the specified name.\r\n\r\nOn success, stores a pointer to the new file in *result and returns OK. On failure stores NULL in *result and returns non-OK. If the file does not exist, returns a non-OK status.\r\n\r\nThe returned file may be concurrently accessed by multiple threads.\r\n\r\n#### `Status tensorflow::EnvWrapper::NewWritableFile(const string &f, WritableFile **r) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_NewWritableFile\"></a>\r\n\r\nCreates an object that writes to a new file with the specified name.\r\n\r\nDeletes any existing file with the same name and creates a new file. On success, stores a pointer to the new file in *result and returns OK. On failure stores NULL in *result and returns non-OK.\r\n\r\nThe returned file will only be accessed by one thread at a time.\r\n\r\n#### `Status tensorflow::EnvWrapper::NewAppendableFile(const string &f, WritableFile **r) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_NewAppendableFile\"></a>\r\n\r\nCreates an object that either appends to an existing file, or writes to a new file (if the file does not exist to begin with).\r\n\r\nOn success, stores a pointer to the new file in *result and returns OK. On failure stores NULL in *result and returns non-OK.\r\n\r\nThe returned file will only be accessed by one thread at a time.\r\n\r\n#### `bool tensorflow::EnvWrapper::FileExists(const string &f) override` <a class=\"md-anchor\" id=\"bool_tensorflow_EnvWrapper_FileExists\"></a>\r\n\r\nReturns true iff the named file exists.\r\n\r\n\r\n\r\n#### `Status tensorflow::EnvWrapper::GetChildren(const string &dir, std::vector< string > *r) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_GetChildren\"></a>\r\n\r\nStores in *result the names of the children of the specified directory. The names are relative to \"dir\".\r\n\r\nOriginal contents of *results are dropped.\r\n\r\n#### `Status tensorflow::EnvWrapper::DeleteFile(const string &f) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_DeleteFile\"></a>\r\n\r\nDeletes the named file.\r\n\r\n\r\n\r\n#### `Status tensorflow::EnvWrapper::CreateDir(const string &d) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_CreateDir\"></a>\r\n\r\nCreates the specified directory.\r\n\r\n\r\n\r\n#### `Status tensorflow::EnvWrapper::DeleteDir(const string &d) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_DeleteDir\"></a>\r\n\r\nDeletes the specified directory.\r\n\r\n\r\n\r\n#### `Status tensorflow::EnvWrapper::GetFileSize(const string &f, uint64 *s) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_GetFileSize\"></a>\r\n\r\nStores the size of fname in *file_size.\r\n\r\n\r\n\r\n#### `Status tensorflow::EnvWrapper::RenameFile(const string &s, const string &t) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_RenameFile\"></a>\r\n\r\nRenames file src to target. If target already exists, it will be replaced.\r\n\r\n\r\n\r\n#### `uint64 tensorflow::EnvWrapper::NowMicros() override` <a class=\"md-anchor\" id=\"uint64_tensorflow_EnvWrapper_NowMicros\"></a>\r\n\r\nReturns the number of micro-seconds since some fixed point in time. Only useful for computing deltas of time.\r\n\r\n\r\n\r\n#### `void tensorflow::EnvWrapper::SleepForMicroseconds(int micros) override` <a class=\"md-anchor\" id=\"void_tensorflow_EnvWrapper_SleepForMicroseconds\"></a>\r\n\r\nSleeps/delays the thread for the prescribed number of micro-seconds.\r\n\r\n\r\n\r\n#### `Thread* tensorflow::EnvWrapper::StartThread(const ThreadOptions &thread_options, const string &name, std::function< void()> fn) override` <a class=\"md-anchor\" id=\"Thread_tensorflow_EnvWrapper_StartThread\"></a>\r\n\r\nReturns a new thread that is running fn() and is identified (for debugging/performance-analysis) by \"name\".\r\n\r\nCaller takes ownership of the result and must delete it eventually (the deletion will block until fn() stops running).\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/ClassRandomAccessFile.md",
    "content": "# Class `tensorflow::RandomAccessFile` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--randomaccessfile-\"></a>\r\n\r\nA file abstraction for randomly reading the contents of a file.\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::RandomAccessFile::RandomAccessFile()`](#tensorflow_RandomAccessFile_RandomAccessFile)\r\n* [`virtual tensorflow::RandomAccessFile::~RandomAccessFile()`](#virtual_tensorflow_RandomAccessFile_RandomAccessFile)\r\n* [`virtual Status tensorflow::RandomAccessFile::Read(uint64 offset, size_t n, StringPiece *result, char *scratch) const =0`](#virtual_Status_tensorflow_RandomAccessFile_Read)\r\n  * Reads up to \"n\" bytes from the file starting at \"offset\".\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::RandomAccessFile::RandomAccessFile()` <a class=\"md-anchor\" id=\"tensorflow_RandomAccessFile_RandomAccessFile\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual tensorflow::RandomAccessFile::~RandomAccessFile()` <a class=\"md-anchor\" id=\"virtual_tensorflow_RandomAccessFile_RandomAccessFile\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::RandomAccessFile::Read(uint64 offset, size_t n, StringPiece *result, char *scratch) const =0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_RandomAccessFile_Read\"></a>\r\n\r\nReads up to \"n\" bytes from the file starting at \"offset\".\r\n\r\n\"scratch[0..n-1]\" may be written by this routine. Sets \"*result\" to the data that was read (including if fewer than \"n\" bytes were successfully read). May set \"*result\" to point at data in \"scratch[0..n-1]\", so \"scratch[0..n-1]\" must be live when \"*result\" is used.\r\n\r\nOn OK returned status: \"n\" bytes have been stored in \"*result\". On non-OK returned status: [0..n] bytes have been stored in \"*result\".\r\n\r\nReturns `OUT_OF_RANGE` if fewer than n bytes were stored in \"*result\" because of EOF.\r\n\r\nSafe for concurrent use by multiple threads.\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/ClassSession.md",
    "content": "# Class `tensorflow::Session` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--session-\"></a>\r\n\r\nA Session instance lets a caller drive a TensorFlow graph computation.\r\n\r\nWhen a Session is created with a given target, a new Session object is bound to the universe of resources specified by that target. Those resources are available to this session to perform computation described in the GraphDef. After extending the session with a graph, the caller uses the Run() API to perform the computation and potentially fetch outputs as Tensors.\r\n\r\nExample:\r\n\r\n```c++ tensorflow::GraphDef graph;\r\n// ... Create or load graph into \"graph\".\r\n\r\n// This example uses the default options which connects\r\n// to a local runtime.\r\ntensorflow::SessionOptions options;\r\nstd::unique_ptr<tensorflow::Session>\r\nsession(tensorflow::NewSession(options));\r\n\r\n// Create the session with this graph.\r\ntensorflow::Status s = session->Create(graph);\r\nif (!s.ok()) { ... }\r\n\r\n// Run the graph and fetch the first output of the \"output\"\r\n// operation, and also run to but do not return anything\r\n// for the \"update_state\" operation.\r\nstd::vector<tensorflow::Tensor> outputs;\r\ns = session->Run({}, {\"output:0\"}, {\"update_state\"}, &outputs);\r\nif (!s.ok()) { ... }\r\n\r\n// Map the output as a flattened float tensor, and do something\r\n// with it.\r\nauto output_tensor = outputs[0].flat<float>();\r\nif (output_tensor(0) > 0.5) { ... }\r\n\r\n// Close the session to release the resources associated with\r\n// this session.\r\nsession->Close()\r\n\r\n```\r\n\r\nA Session allows concurrent calls to Run() , though a Session must be created / extended by a single thread.\r\n\r\nOnly one thread must call Close() , and Close() must only be called after all other calls to Run() have returned.\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`virtual Status tensorflow::Session::Create(const GraphDef &graph)=0`](#virtual_Status_tensorflow_Session_Create)\r\n  * Create the graph to be used for the session.\r\n* [`virtual Status tensorflow::Session::Extend(const GraphDef &graph)=0`](#virtual_Status_tensorflow_Session_Extend)\r\n  * Adds operations to the graph that is already registered with the Session .\r\n* [`virtual Status tensorflow::Session::Run(const std::vector< std::pair< string, Tensor > > &inputs, const std::vector< string > &output_tensor_names, const std::vector< string > &target_node_names, std::vector< Tensor > *outputs)=0`](#virtual_Status_tensorflow_Session_Run)\r\n  * Runs the graph with the provided input tensors and fills `outputs` for the endpoints specified in `output_tensor_names`. Runs to but does not return Tensors for the nodes in `target_node_names`.\r\n* [`virtual Status tensorflow::Session::Close()=0`](#virtual_Status_tensorflow_Session_Close)\r\n  * Closes this session.\r\n* [`virtual tensorflow::Session::~Session()`](#virtual_tensorflow_Session_Session)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `virtual Status tensorflow::Session::Create(const GraphDef &graph)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Session_Create\"></a>\r\n\r\nCreate the graph to be used for the session.\r\n\r\nReturns an error if this session has already been created with a graph. To re-use the session with a different graph, the caller must Close() the session first.\r\n\r\n#### `virtual Status tensorflow::Session::Extend(const GraphDef &graph)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Session_Extend\"></a>\r\n\r\nAdds operations to the graph that is already registered with the Session .\r\n\r\nThe names of new operations in \"graph\" must not exist in the graph that is already registered.\r\n\r\n#### `virtual Status tensorflow::Session::Run(const std::vector< std::pair< string, Tensor > > &inputs, const std::vector< string > &output_tensor_names, const std::vector< string > &target_node_names, std::vector< Tensor > *outputs)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Session_Run\"></a>\r\n\r\nRuns the graph with the provided input tensors and fills `outputs` for the endpoints specified in `output_tensor_names`. Runs to but does not return Tensors for the nodes in `target_node_names`.\r\n\r\nThe order of tensors in `outputs` will match the order provided by `output_tensor_names`.\r\n\r\nIf `Run` returns `OK()`, then `outputs->size()` will be equal to `output_tensor_names.size()`. If `Run` does not return `OK()`, the state of `outputs` is undefined.\r\n\r\nREQUIRES: The name of each Tensor of the input or output must match a \"Tensor endpoint\" in the `GraphDef` passed to ` Create() `.\r\n\r\nREQUIRES: outputs is not nullptr if `output_tensor_names` is non-empty.\r\n\r\n#### `virtual Status tensorflow::Session::Close()=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Session_Close\"></a>\r\n\r\nCloses this session.\r\n\r\nClosing a session releases the resources used by this session on the TensorFlow runtime (specified during session creation by the ` SessionOptions::target ` field).\r\n\r\n#### `virtual tensorflow::Session::~Session()` <a class=\"md-anchor\" id=\"virtual_tensorflow_Session_Session\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/ClassStatus.md",
    "content": "# Class `tensorflow::Status` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--status-\"></a>\r\n\r\n\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::Status::Status()`](#tensorflow_Status_Status)\r\n  * Create a success status.\r\n* [`tensorflow::Status::~Status()`](#tensorflow_Status_Status)\r\n* [`tensorflow::Status::Status(tensorflow::error::Code code, tensorflow::StringPiece msg)`](#tensorflow_Status_Status)\r\n  * Create a status with the specified error code and msg as a human-readable string containing more detailed information.\r\n* [`tensorflow::Status::Status(const Status &s)`](#tensorflow_Status_Status)\r\n  * Copy the specified status.\r\n* [`void tensorflow::Status::operator=(const Status &s)`](#void_tensorflow_Status_operator_)\r\n* [`bool tensorflow::Status::ok() const`](#bool_tensorflow_Status_ok)\r\n  * Returns true iff the status indicates success.\r\n* [`tensorflow::error::Code tensorflow::Status::code() const`](#tensorflow_error_Code_tensorflow_Status_code)\r\n* [`const string& tensorflow::Status::error_message() const`](#const_string_tensorflow_Status_error_message)\r\n* [`bool tensorflow::Status::operator==(const Status &x) const`](#bool_tensorflow_Status_operator_)\r\n* [`bool tensorflow::Status::operator!=(const Status &x) const`](#bool_tensorflow_Status_operator_)\r\n* [`void tensorflow::Status::Update(const Status &new_status)`](#void_tensorflow_Status_Update)\r\n  * If ` ok() `, stores `new_status` into `*this`. If `!ok()`, preserves the current status, but may augment with additional information about `new_status`.\r\n* [`string tensorflow::Status::ToString() const`](#string_tensorflow_Status_ToString)\r\n  * Return a string representation of this status suitable for printing. Returns the string `\"OK\"` for success.\r\n* [`static Status tensorflow::Status::OK()`](#static_Status_tensorflow_Status_OK)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::Status::Status()` <a class=\"md-anchor\" id=\"tensorflow_Status_Status\"></a>\r\n\r\nCreate a success status.\r\n\r\n\r\n\r\n#### `tensorflow::Status::~Status()` <a class=\"md-anchor\" id=\"tensorflow_Status_Status\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `tensorflow::Status::Status(tensorflow::error::Code code, tensorflow::StringPiece msg)` <a class=\"md-anchor\" id=\"tensorflow_Status_Status\"></a>\r\n\r\nCreate a status with the specified error code and msg as a human-readable string containing more detailed information.\r\n\r\n\r\n\r\n#### `tensorflow::Status::Status(const Status &s)` <a class=\"md-anchor\" id=\"tensorflow_Status_Status\"></a>\r\n\r\nCopy the specified status.\r\n\r\n\r\n\r\n#### `void tensorflow::Status::operator=(const Status &s)` <a class=\"md-anchor\" id=\"void_tensorflow_Status_operator_\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `bool tensorflow::Status::ok() const` <a class=\"md-anchor\" id=\"bool_tensorflow_Status_ok\"></a>\r\n\r\nReturns true iff the status indicates success.\r\n\r\n\r\n\r\n#### `tensorflow::error::Code tensorflow::Status::code() const` <a class=\"md-anchor\" id=\"tensorflow_error_Code_tensorflow_Status_code\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `const string& tensorflow::Status::error_message() const` <a class=\"md-anchor\" id=\"const_string_tensorflow_Status_error_message\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `bool tensorflow::Status::operator==(const Status &x) const` <a class=\"md-anchor\" id=\"bool_tensorflow_Status_operator_\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `bool tensorflow::Status::operator!=(const Status &x) const` <a class=\"md-anchor\" id=\"bool_tensorflow_Status_operator_\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `void tensorflow::Status::Update(const Status &new_status)` <a class=\"md-anchor\" id=\"void_tensorflow_Status_Update\"></a>\r\n\r\nIf ` ok() `, stores `new_status` into `*this`. If `!ok()`, preserves the current status, but may augment with additional information about `new_status`.\r\n\r\nConvenient way of keeping track of the first error encountered. Instead of: `if (overall_status.ok()) overall_status = new_status` Use: `overall_status.Update(new_status);`\r\n\r\n#### `string tensorflow::Status::ToString() const` <a class=\"md-anchor\" id=\"string_tensorflow_Status_ToString\"></a>\r\n\r\nReturn a string representation of this status suitable for printing. Returns the string `\"OK\"` for success.\r\n\r\n\r\n\r\n#### `static Status tensorflow::Status::OK()` <a class=\"md-anchor\" id=\"static_Status_tensorflow_Status_OK\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/ClassTensor.md",
    "content": "# Class `tensorflow::Tensor` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--tensor-\"></a>\r\n\r\nRepresents an n-dimensional array of values.\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::Tensor::Tensor()`](#tensorflow_Tensor_Tensor)\r\n  * Default Tensor constructor. Creates a 1-dimension, 0-element float tensor.\r\n* [`tensorflow::Tensor::Tensor(DataType type, const TensorShape &shape)`](#tensorflow_Tensor_Tensor)\r\n  * Creates a Tensor of the given `type` and `shape`.\r\n* [`tensorflow::Tensor::Tensor(Allocator *a, DataType type, const TensorShape &shape)`](#tensorflow_Tensor_Tensor)\r\n  * Creates a tensor with the input `type` and `shape`, using the allocator `a` to allocate the underlying buffer.\r\n* [`tensorflow::Tensor::Tensor(DataType type)`](#tensorflow_Tensor_Tensor)\r\n  * Creates an uninitialized Tensor of the given data type.\r\n* [`tensorflow::Tensor::Tensor(const Tensor &other)`](#tensorflow_Tensor_Tensor)\r\n* [`tensorflow::Tensor::~Tensor()`](#tensorflow_Tensor_Tensor)\r\n  * Copy constructor.\r\n* [`DataType tensorflow::Tensor::dtype() const`](#DataType_tensorflow_Tensor_dtype)\r\n  * Returns the data type.\r\n* [`const TensorShape& tensorflow::Tensor::shape() const`](#const_TensorShape_tensorflow_Tensor_shape)\r\n  * Returns the shape of the tensor.\r\n* [`int tensorflow::Tensor::dims() const`](#int_tensorflow_Tensor_dims)\r\n  * Convenience accessor for the tensor shape.\r\n* [`int64 tensorflow::Tensor::dim_size(int d) const`](#int64_tensorflow_Tensor_dim_size)\r\n  * Convenience accessor for the tensor shape.\r\n* [`int64 tensorflow::Tensor::NumElements() const`](#int64_tensorflow_Tensor_NumElements)\r\n  * Convenience accessor for the tensor shape.\r\n* [`bool tensorflow::Tensor::IsSameSize(const Tensor &b) const`](#bool_tensorflow_Tensor_IsSameSize)\r\n* [`bool tensorflow::Tensor::IsInitialized() const`](#bool_tensorflow_Tensor_IsInitialized)\r\n  * Has this Tensor been initialized?\r\n* [`size_t tensorflow::Tensor::TotalBytes() const`](#size_t_tensorflow_Tensor_TotalBytes)\r\n  * Returns the estimated memory usage of this tensor.\r\n* [`Tensor& tensorflow::Tensor::operator=(const Tensor &other)`](#Tensor_tensorflow_Tensor_operator_)\r\n  * Assign operator. This tensor shares other&apos;s underlying storage.\r\n* [`bool tensorflow::Tensor::CopyFrom(const Tensor &other, const TensorShape &shape) TF_MUST_USE_RESULT`](#bool_tensorflow_Tensor_CopyFrom)\r\n  * Copy the other tensor into this tensor and reshape it.\r\n* [`Tensor tensorflow::Tensor::Slice(int64 dim0_start, int64 dim0_limit) const`](#Tensor_tensorflow_Tensor_Slice)\r\n  * Slice this tensor along the 1st dimension.\r\n* [`bool tensorflow::Tensor::FromProto(const TensorProto &other) TF_MUST_USE_RESULT`](#bool_tensorflow_Tensor_FromProto)\r\n  * Parse `other` and construct the tensor.\r\n* [`bool tensorflow::Tensor::FromProto(Allocator *a, const TensorProto &other) TF_MUST_USE_RESULT`](#bool_tensorflow_Tensor_FromProto)\r\n* [`void tensorflow::Tensor::AsProtoField(TensorProto *proto) const`](#void_tensorflow_Tensor_AsProtoField)\r\n  * Fills in `proto` with `*this` tensor&apos;s content.\r\n* [`void tensorflow::Tensor::AsProtoTensorContent(TensorProto *proto) const`](#void_tensorflow_Tensor_AsProtoTensorContent)\r\n* [`TTypes<T>::Vec tensorflow::Tensor::vec()`](#TTypes_T_Vec_tensorflow_Tensor_vec)\r\n  * Return the tensor data as an `Eigen::Tensor` with the type and sizes of this ` Tensor `.\r\n* [`TTypes<T>::Matrix tensorflow::Tensor::matrix()`](#TTypes_T_Matrix_tensorflow_Tensor_matrix)\r\n* [`TTypes< T, NDIMS >::Tensor tensorflow::Tensor::tensor()`](#TTypes_T_NDIMS_Tensor_tensorflow_Tensor_tensor)\r\n* [`TTypes<T>::Flat tensorflow::Tensor::flat()`](#TTypes_T_Flat_tensorflow_Tensor_flat)\r\n  * Return the tensor data as an `Eigen::Tensor` of the data type and a specified shape.\r\n* [`TTypes<T>::UnalignedFlat tensorflow::Tensor::unaligned_flat()`](#TTypes_T_UnalignedFlat_tensorflow_Tensor_unaligned_flat)\r\n* [`TTypes<T>::Matrix tensorflow::Tensor::flat_inner_dims()`](#TTypes_T_Matrix_tensorflow_Tensor_flat_inner_dims)\r\n* [`TTypes<T>::Matrix tensorflow::Tensor::flat_outer_dims()`](#TTypes_T_Matrix_tensorflow_Tensor_flat_outer_dims)\r\n* [`TTypes< T, NDIMS >::Tensor tensorflow::Tensor::shaped(gtl::ArraySlice< int64 > new_sizes)`](#TTypes_T_NDIMS_Tensor_tensorflow_Tensor_shaped)\r\n* [`TTypes< T, NDIMS >::UnalignedTensor tensorflow::Tensor::unaligned_shaped(gtl::ArraySlice< int64 > new_sizes)`](#TTypes_T_NDIMS_UnalignedTensor_tensorflow_Tensor_unaligned_shaped)\r\n* [`TTypes< T >::Scalar tensorflow::Tensor::scalar()`](#TTypes_T_Scalar_tensorflow_Tensor_scalar)\r\n  * Return the Tensor data as a `TensorMap` of fixed size 1: `TensorMap<TensorFixedSize<T, 1>>`.\r\n* [`TTypes<T>::ConstVec tensorflow::Tensor::vec() const`](#TTypes_T_ConstVec_tensorflow_Tensor_vec)\r\n  * Const versions of all the methods above.\r\n* [`TTypes<T>::ConstMatrix tensorflow::Tensor::matrix() const`](#TTypes_T_ConstMatrix_tensorflow_Tensor_matrix)\r\n* [`TTypes< T, NDIMS >::ConstTensor tensorflow::Tensor::tensor() const`](#TTypes_T_NDIMS_ConstTensor_tensorflow_Tensor_tensor)\r\n* [`TTypes<T>::ConstFlat tensorflow::Tensor::flat() const`](#TTypes_T_ConstFlat_tensorflow_Tensor_flat)\r\n* [`TTypes<T>::UnalignedConstFlat tensorflow::Tensor::unaligned_flat() const`](#TTypes_T_UnalignedConstFlat_tensorflow_Tensor_unaligned_flat)\r\n* [`TTypes<T>::ConstMatrix tensorflow::Tensor::flat_inner_dims() const`](#TTypes_T_ConstMatrix_tensorflow_Tensor_flat_inner_dims)\r\n* [`TTypes<T>::ConstMatrix tensorflow::Tensor::flat_outer_dims() const`](#TTypes_T_ConstMatrix_tensorflow_Tensor_flat_outer_dims)\r\n* [`TTypes< T, NDIMS >::ConstTensor tensorflow::Tensor::shaped(gtl::ArraySlice< int64 > new_sizes) const`](#TTypes_T_NDIMS_ConstTensor_tensorflow_Tensor_shaped)\r\n* [`TTypes< T, NDIMS >::UnalignedConstTensor tensorflow::Tensor::unaligned_shaped(gtl::ArraySlice< int64 > new_sizes) const`](#TTypes_T_NDIMS_UnalignedConstTensor_tensorflow_Tensor_unaligned_shaped)\r\n* [`TTypes< T >::ConstScalar tensorflow::Tensor::scalar() const`](#TTypes_T_ConstScalar_tensorflow_Tensor_scalar)\r\n* [`string tensorflow::Tensor::SummarizeValue(int64 max_entries) const`](#string_tensorflow_Tensor_SummarizeValue)\r\n  * Render the first `max_entries` values in `*this` into a string.\r\n* [`string tensorflow::Tensor::DebugString() const`](#string_tensorflow_Tensor_DebugString)\r\n  * A human-readable summary of the tensor suitable for debugging.\r\n* [`void tensorflow::Tensor::FillDescription(TensorDescription *description) const`](#void_tensorflow_Tensor_FillDescription)\r\n* [`StringPiece tensorflow::Tensor::tensor_data() const`](#StringPiece_tensorflow_Tensor_tensor_data)\r\n  * Returns a `StringPiece` mapping the current tensor&apos;s buffer.\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::Tensor::Tensor()` <a class=\"md-anchor\" id=\"tensorflow_Tensor_Tensor\"></a>\r\n\r\nDefault Tensor constructor. Creates a 1-dimension, 0-element float tensor.\r\n\r\n\r\n\r\n#### `tensorflow::Tensor::Tensor(DataType type, const TensorShape &shape)` <a class=\"md-anchor\" id=\"tensorflow_Tensor_Tensor\"></a>\r\n\r\nCreates a Tensor of the given `type` and `shape`.\r\n\r\nThe underlying buffer is allocated using a `CPUAllocator`.\r\n\r\n#### `tensorflow::Tensor::Tensor(Allocator *a, DataType type, const TensorShape &shape)` <a class=\"md-anchor\" id=\"tensorflow_Tensor_Tensor\"></a>\r\n\r\nCreates a tensor with the input `type` and `shape`, using the allocator `a` to allocate the underlying buffer.\r\n\r\n`a` must outlive the lifetime of this Tensor .\r\n\r\n#### `tensorflow::Tensor::Tensor(DataType type)` <a class=\"md-anchor\" id=\"tensorflow_Tensor_Tensor\"></a>\r\n\r\nCreates an uninitialized Tensor of the given data type.\r\n\r\n\r\n\r\n#### `tensorflow::Tensor::Tensor(const Tensor &other)` <a class=\"md-anchor\" id=\"tensorflow_Tensor_Tensor\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `tensorflow::Tensor::~Tensor()` <a class=\"md-anchor\" id=\"tensorflow_Tensor_Tensor\"></a>\r\n\r\nCopy constructor.\r\n\r\n\r\n\r\n#### `DataType tensorflow::Tensor::dtype() const` <a class=\"md-anchor\" id=\"DataType_tensorflow_Tensor_dtype\"></a>\r\n\r\nReturns the data type.\r\n\r\n\r\n\r\n#### `const TensorShape& tensorflow::Tensor::shape() const` <a class=\"md-anchor\" id=\"const_TensorShape_tensorflow_Tensor_shape\"></a>\r\n\r\nReturns the shape of the tensor.\r\n\r\n\r\n\r\n#### `int tensorflow::Tensor::dims() const` <a class=\"md-anchor\" id=\"int_tensorflow_Tensor_dims\"></a>\r\n\r\nConvenience accessor for the tensor shape.\r\n\r\nFor all shape accessors, see comments for relevant methods of ` TensorShape ` in ` tensor_shape.h `.\r\n\r\n#### `int64 tensorflow::Tensor::dim_size(int d) const` <a class=\"md-anchor\" id=\"int64_tensorflow_Tensor_dim_size\"></a>\r\n\r\nConvenience accessor for the tensor shape.\r\n\r\n\r\n\r\n#### `int64 tensorflow::Tensor::NumElements() const` <a class=\"md-anchor\" id=\"int64_tensorflow_Tensor_NumElements\"></a>\r\n\r\nConvenience accessor for the tensor shape.\r\n\r\n\r\n\r\n#### `bool tensorflow::Tensor::IsSameSize(const Tensor &b) const` <a class=\"md-anchor\" id=\"bool_tensorflow_Tensor_IsSameSize\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `bool tensorflow::Tensor::IsInitialized() const` <a class=\"md-anchor\" id=\"bool_tensorflow_Tensor_IsInitialized\"></a>\r\n\r\nHas this Tensor been initialized?\r\n\r\n\r\n\r\n#### `size_t tensorflow::Tensor::TotalBytes() const` <a class=\"md-anchor\" id=\"size_t_tensorflow_Tensor_TotalBytes\"></a>\r\n\r\nReturns the estimated memory usage of this tensor.\r\n\r\n\r\n\r\n#### `Tensor& tensorflow::Tensor::operator=(const Tensor &other)` <a class=\"md-anchor\" id=\"Tensor_tensorflow_Tensor_operator_\"></a>\r\n\r\nAssign operator. This tensor shares other&apos;s underlying storage.\r\n\r\n\r\n\r\n#### `bool tensorflow::Tensor::CopyFrom(const Tensor &other, const TensorShape &shape) TF_MUST_USE_RESULT` <a class=\"md-anchor\" id=\"bool_tensorflow_Tensor_CopyFrom\"></a>\r\n\r\nCopy the other tensor into this tensor and reshape it.\r\n\r\nThis tensor shares other&apos;s underlying storage. Returns `true` iff `other.shape()` has the same number of elements of the given `shape`.\r\n\r\n#### `Tensor tensorflow::Tensor::Slice(int64 dim0_start, int64 dim0_limit) const` <a class=\"md-anchor\" id=\"Tensor_tensorflow_Tensor_Slice\"></a>\r\n\r\nSlice this tensor along the 1st dimension.\r\n\r\nI.e., the returned tensor satisifies returned[i, ...] == this[dim0_start + i, ...]. The returned tensor shares the underlying tensor buffer with this tensor.\r\n\r\nNOTE: The returned tensor may not satisfies the same alignment requirement as this tensor depending on the shape. The caller must check the returned tensor&apos;s alignment before calling certain methods that have alignment requirement (e.g., ` flat() `, `tensor()`).\r\n\r\nREQUIRES: ` dims() ` >= 1 REQUIRES: `0 <= dim0_start <= dim0_limit <= dim_size(0)`\r\n\r\n#### `bool tensorflow::Tensor::FromProto(const TensorProto &other) TF_MUST_USE_RESULT` <a class=\"md-anchor\" id=\"bool_tensorflow_Tensor_FromProto\"></a>\r\n\r\nParse `other` and construct the tensor.\r\n\r\nReturns `true` iff the parsing succeeds. If the parsing fails, the state of `*this` is unchanged.\r\n\r\n#### `bool tensorflow::Tensor::FromProto(Allocator *a, const TensorProto &other) TF_MUST_USE_RESULT` <a class=\"md-anchor\" id=\"bool_tensorflow_Tensor_FromProto\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `void tensorflow::Tensor::AsProtoField(TensorProto *proto) const` <a class=\"md-anchor\" id=\"void_tensorflow_Tensor_AsProtoField\"></a>\r\n\r\nFills in `proto` with `*this` tensor&apos;s content.\r\n\r\n` AsProtoField() ` fills in the repeated field for `proto.dtype()`, while `AsProtoTensorContent()` encodes the content in `proto.tensor_content()` in a compact form.\r\n\r\n#### `void tensorflow::Tensor::AsProtoTensorContent(TensorProto *proto) const` <a class=\"md-anchor\" id=\"void_tensorflow_Tensor_AsProtoTensorContent\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes<T>::Vec tensorflow::Tensor::vec()` <a class=\"md-anchor\" id=\"TTypes_T_Vec_tensorflow_Tensor_vec\"></a>\r\n\r\nReturn the tensor data as an `Eigen::Tensor` with the type and sizes of this ` Tensor `.\r\n\r\nUse these methods when you know the data type and the number of dimensions of the Tensor and you want an `Eigen::Tensor` automatically sized to the ` Tensor ` sizes. The implementation check fails if either type or sizes mismatch.\r\n\r\nExample:\r\n\r\n```c++ typedef float T;\r\nTensor my_mat(...built with Shape{rows: 3, cols: 5}...);\r\nauto mat = my_mat.matrix<T>();    // 2D Eigen::Tensor, 3 x 5.\r\nauto mat = my_mat.tensor<T, 2>(); // 2D Eigen::Tensor, 3 x 5.\r\nauto vec = my_mat.vec<T>();       // CHECK fails as my_mat is 2D.\r\nauto vec = my_mat.tensor<T, 3>(); // CHECK fails as my_mat is 2D.\r\nauto mat = my_mat.matrix<int32>();// CHECK fails as type mismatch.\r\n\r\n```\r\n\r\n#### `TTypes<T>::Matrix tensorflow::Tensor::matrix()` <a class=\"md-anchor\" id=\"TTypes_T_Matrix_tensorflow_Tensor_matrix\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes< T, NDIMS >::Tensor tensorflow::Tensor::tensor()` <a class=\"md-anchor\" id=\"TTypes_T_NDIMS_Tensor_tensorflow_Tensor_tensor\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes<T>::Flat tensorflow::Tensor::flat()` <a class=\"md-anchor\" id=\"TTypes_T_Flat_tensorflow_Tensor_flat\"></a>\r\n\r\nReturn the tensor data as an `Eigen::Tensor` of the data type and a specified shape.\r\n\r\nThese methods allow you to access the data with the dimensions and sizes of your choice. You do not need to know the number of dimensions of the Tensor to call them. However, they `CHECK` that the type matches and the dimensions requested creates an `Eigen::Tensor` with the same number of elements as the tensor.\r\n\r\nExample:\r\n\r\n```c++ typedef float T;\r\nTensor my_ten(...built with Shape{planes: 4, rows: 3, cols: 5}...);\r\n// 1D Eigen::Tensor, size 60:\r\nauto flat = my_ten.flat<T>();\r\n// 2D Eigen::Tensor 12 x 5:\r\nauto inner = my_ten.flat_inner_dims<T>();\r\n// 2D Eigen::Tensor 4 x 15:\r\nauto outer = my_ten.shaped<T, 2>({4, 15});\r\n// CHECK fails, bad num elements:\r\nauto outer = my_ten.shaped<T, 2>({4, 8});\r\n// 3D Eigen::Tensor 6 x 5 x 2:\r\nauto weird = my_ten.shaped<T, 3>({6, 5, 2});\r\n// CHECK fails, type mismatch:\r\nauto bad   = my_ten.flat<int32>();\r\n\r\n```\r\n\r\n#### `TTypes<T>::UnalignedFlat tensorflow::Tensor::unaligned_flat()` <a class=\"md-anchor\" id=\"TTypes_T_UnalignedFlat_tensorflow_Tensor_unaligned_flat\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes<T>::Matrix tensorflow::Tensor::flat_inner_dims()` <a class=\"md-anchor\" id=\"TTypes_T_Matrix_tensorflow_Tensor_flat_inner_dims\"></a>\r\n\r\n\r\n\r\nReturns the data as an Eigen::Tensor with 2 dimensions, collapsing all Tensor dimensions but the last one into the first dimension of the result.\r\n\r\n#### `TTypes<T>::Matrix tensorflow::Tensor::flat_outer_dims()` <a class=\"md-anchor\" id=\"TTypes_T_Matrix_tensorflow_Tensor_flat_outer_dims\"></a>\r\n\r\n\r\n\r\nReturns the data as an Eigen::Tensor with 2 dimensions, collapsing all Tensor dimensions but the first one into the last dimension of the result.\r\n\r\n#### `TTypes< T, NDIMS >::Tensor tensorflow::Tensor::shaped(gtl::ArraySlice< int64 > new_sizes)` <a class=\"md-anchor\" id=\"TTypes_T_NDIMS_Tensor_tensorflow_Tensor_shaped\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes< T, NDIMS >::UnalignedTensor tensorflow::Tensor::unaligned_shaped(gtl::ArraySlice< int64 > new_sizes)` <a class=\"md-anchor\" id=\"TTypes_T_NDIMS_UnalignedTensor_tensorflow_Tensor_unaligned_shaped\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes< T >::Scalar tensorflow::Tensor::scalar()` <a class=\"md-anchor\" id=\"TTypes_T_Scalar_tensorflow_Tensor_scalar\"></a>\r\n\r\nReturn the Tensor data as a `TensorMap` of fixed size 1: `TensorMap<TensorFixedSize<T, 1>>`.\r\n\r\nUsing ` scalar() ` allows the compiler to perform optimizations as the size of the tensor is known at compile time.\r\n\r\n#### `TTypes<T>::ConstVec tensorflow::Tensor::vec() const` <a class=\"md-anchor\" id=\"TTypes_T_ConstVec_tensorflow_Tensor_vec\"></a>\r\n\r\nConst versions of all the methods above.\r\n\r\n\r\n\r\n#### `TTypes<T>::ConstMatrix tensorflow::Tensor::matrix() const` <a class=\"md-anchor\" id=\"TTypes_T_ConstMatrix_tensorflow_Tensor_matrix\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes< T, NDIMS >::ConstTensor tensorflow::Tensor::tensor() const` <a class=\"md-anchor\" id=\"TTypes_T_NDIMS_ConstTensor_tensorflow_Tensor_tensor\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes<T>::ConstFlat tensorflow::Tensor::flat() const` <a class=\"md-anchor\" id=\"TTypes_T_ConstFlat_tensorflow_Tensor_flat\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes<T>::UnalignedConstFlat tensorflow::Tensor::unaligned_flat() const` <a class=\"md-anchor\" id=\"TTypes_T_UnalignedConstFlat_tensorflow_Tensor_unaligned_flat\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes<T>::ConstMatrix tensorflow::Tensor::flat_inner_dims() const` <a class=\"md-anchor\" id=\"TTypes_T_ConstMatrix_tensorflow_Tensor_flat_inner_dims\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes<T>::ConstMatrix tensorflow::Tensor::flat_outer_dims() const` <a class=\"md-anchor\" id=\"TTypes_T_ConstMatrix_tensorflow_Tensor_flat_outer_dims\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes< T, NDIMS >::ConstTensor tensorflow::Tensor::shaped(gtl::ArraySlice< int64 > new_sizes) const` <a class=\"md-anchor\" id=\"TTypes_T_NDIMS_ConstTensor_tensorflow_Tensor_shaped\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes< T, NDIMS >::UnalignedConstTensor tensorflow::Tensor::unaligned_shaped(gtl::ArraySlice< int64 > new_sizes) const` <a class=\"md-anchor\" id=\"TTypes_T_NDIMS_UnalignedConstTensor_tensorflow_Tensor_unaligned_shaped\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes< T >::ConstScalar tensorflow::Tensor::scalar() const` <a class=\"md-anchor\" id=\"TTypes_T_ConstScalar_tensorflow_Tensor_scalar\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `string tensorflow::Tensor::SummarizeValue(int64 max_entries) const` <a class=\"md-anchor\" id=\"string_tensorflow_Tensor_SummarizeValue\"></a>\r\n\r\nRender the first `max_entries` values in `*this` into a string.\r\n\r\n\r\n\r\n#### `string tensorflow::Tensor::DebugString() const` <a class=\"md-anchor\" id=\"string_tensorflow_Tensor_DebugString\"></a>\r\n\r\nA human-readable summary of the tensor suitable for debugging.\r\n\r\n\r\n\r\n#### `void tensorflow::Tensor::FillDescription(TensorDescription *description) const` <a class=\"md-anchor\" id=\"void_tensorflow_Tensor_FillDescription\"></a>\r\n\r\n\r\n\r\nFill in the `TensorDescription` proto with metadata about the tensor that is useful for monitoring and debugging.\r\n\r\n#### `StringPiece tensorflow::Tensor::tensor_data() const` <a class=\"md-anchor\" id=\"StringPiece_tensorflow_Tensor_tensor_data\"></a>\r\n\r\nReturns a `StringPiece` mapping the current tensor&apos;s buffer.\r\n\r\nThe returned `StringPiece` may point to memory location on devices that the CPU cannot address directly.\r\n\r\nNOTE: The underlying tensor buffer is refcounted, so the lifetime of the contents mapped by the `StringPiece` matches the lifetime of the buffer; callers should arrange to make sure the buffer does not get destroyed while the `StringPiece` is still used.\r\n\r\nREQUIRES: `DataTypeCanUseMemcpy( dtype() )`.\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/ClassTensorBuffer.md",
    "content": "# Class `tensorflow::TensorBuffer` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--tensorbuffer-\"></a>\r\n\r\n\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::TensorBuffer::~TensorBuffer() override`](#tensorflow_TensorBuffer_TensorBuffer)\r\n* [`virtual void* tensorflow::TensorBuffer::data() const =0`](#virtual_void_tensorflow_TensorBuffer_data)\r\n* [`virtual size_t tensorflow::TensorBuffer::size() const =0`](#virtual_size_t_tensorflow_TensorBuffer_size)\r\n* [`virtual TensorBuffer* tensorflow::TensorBuffer::root_buffer()=0`](#virtual_TensorBuffer_tensorflow_TensorBuffer_root_buffer)\r\n* [`virtual void tensorflow::TensorBuffer::FillAllocationDescription(AllocationDescription *proto) const =0`](#virtual_void_tensorflow_TensorBuffer_FillAllocationDescription)\r\n* [`T* tensorflow::TensorBuffer::base() const`](#T_tensorflow_TensorBuffer_base)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::TensorBuffer::~TensorBuffer() override` <a class=\"md-anchor\" id=\"tensorflow_TensorBuffer_TensorBuffer\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual void* tensorflow::TensorBuffer::data() const =0` <a class=\"md-anchor\" id=\"virtual_void_tensorflow_TensorBuffer_data\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual size_t tensorflow::TensorBuffer::size() const =0` <a class=\"md-anchor\" id=\"virtual_size_t_tensorflow_TensorBuffer_size\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual TensorBuffer* tensorflow::TensorBuffer::root_buffer()=0` <a class=\"md-anchor\" id=\"virtual_TensorBuffer_tensorflow_TensorBuffer_root_buffer\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual void tensorflow::TensorBuffer::FillAllocationDescription(AllocationDescription *proto) const =0` <a class=\"md-anchor\" id=\"virtual_void_tensorflow_TensorBuffer_FillAllocationDescription\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `T* tensorflow::TensorBuffer::base() const` <a class=\"md-anchor\" id=\"T_tensorflow_TensorBuffer_base\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/ClassTensorShape.md",
    "content": "# Class `tensorflow::TensorShape` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--tensorshape-\"></a>\r\n\r\nManages the dimensions of a Tensor and their sizes.\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::TensorShape::TensorShape(gtl::ArraySlice< int64 > dim_sizes)`](#tensorflow_TensorShape_TensorShape)\r\n  * Construct a ` TensorShape ` from the provided sizes. REQUIRES: `dim_sizes[i] >= 0`\r\n* [`tensorflow::TensorShape::TensorShape(std::initializer_list< int64 > dim_sizes)`](#tensorflow_TensorShape_TensorShape)\r\n* [`tensorflow::TensorShape::TensorShape(const TensorShapeProto &proto)`](#tensorflow_TensorShape_TensorShape)\r\n  * REQUIRES: `IsValid(proto)`\r\n* [`tensorflow::TensorShape::TensorShape()`](#tensorflow_TensorShape_TensorShape)\r\n* [`void tensorflow::TensorShape::Clear()`](#void_tensorflow_TensorShape_Clear)\r\n  * Clear a tensor shape.\r\n* [`void tensorflow::TensorShape::AddDim(int64 size)`](#void_tensorflow_TensorShape_AddDim)\r\n  * Add a dimension to the end (\"inner-most\"). REQUIRES: `size >= 0`\r\n* [`void tensorflow::TensorShape::AppendShape(const TensorShape &shape)`](#void_tensorflow_TensorShape_AppendShape)\r\n  * Appends all the dimensions from `shape`.\r\n* [`void tensorflow::TensorShape::InsertDim(int d, int64 size)`](#void_tensorflow_TensorShape_InsertDim)\r\n  * Insert a dimension somewhere in the ` TensorShape `. REQUIRES: `0 <= d <= dims() ` REQUIRES: `size >= 0`\r\n* [`void tensorflow::TensorShape::set_dim(int d, int64 size)`](#void_tensorflow_TensorShape_set_dim)\r\n  * Modifies the size of the dimension `d` to be `size` REQUIRES: `0 <= d < dims() ` REQUIRES: `size >= 0`\r\n* [`void tensorflow::TensorShape::RemoveDim(int d)`](#void_tensorflow_TensorShape_RemoveDim)\r\n  * Removes dimension `d` from the ` TensorShape `. REQUIRES: `0 <= d < dims() `\r\n* [`int tensorflow::TensorShape::dims() const`](#int_tensorflow_TensorShape_dims)\r\n  * Return the number of dimensions in the tensor.\r\n* [`int64 tensorflow::TensorShape::dim_size(int d) const`](#int64_tensorflow_TensorShape_dim_size)\r\n  * Returns the number of elements in dimension `d`. REQUIRES: `0 <= d < dims() `\r\n* [`gtl::ArraySlice<int64> tensorflow::TensorShape::dim_sizes() const`](#gtl_ArraySlice_int64_tensorflow_TensorShape_dim_sizes)\r\n  * Returns sizes of all dimensions.\r\n* [`int64 tensorflow::TensorShape::num_elements() const`](#int64_tensorflow_TensorShape_num_elements)\r\n  * Returns the number of elements in the tensor.\r\n* [`bool tensorflow::TensorShape::IsSameSize(const TensorShape &b) const`](#bool_tensorflow_TensorShape_IsSameSize)\r\n* [`bool tensorflow::TensorShape::operator==(const TensorShape &b) const`](#bool_tensorflow_TensorShape_operator_)\r\n* [`void tensorflow::TensorShape::AsProto(TensorShapeProto *proto) const`](#void_tensorflow_TensorShape_AsProto)\r\n  * Fill `*proto` from `*this`.\r\n* [`Eigen::DSizes< Eigen::DenseIndex, NDIMS > tensorflow::TensorShape::AsEigenDSizes() const`](#Eigen_DSizes_Eigen_DenseIndex_NDIMS_tensorflow_TensorShape_AsEigenDSizes)\r\n  * Fill `*dsizes` from `*this`.\r\n* [`Eigen::DSizes< Eigen::DenseIndex, NDIMS > tensorflow::TensorShape::AsEigenDSizesWithPadding() const`](#Eigen_DSizes_Eigen_DenseIndex_NDIMS_tensorflow_TensorShape_AsEigenDSizesWithPadding)\r\n* [`TensorShapeIter tensorflow::TensorShape::begin() const`](#TensorShapeIter_tensorflow_TensorShape_begin)\r\n  * For iterating through the dimensions.\r\n* [`TensorShapeIter tensorflow::TensorShape::end() const`](#TensorShapeIter_tensorflow_TensorShape_end)\r\n* [`string tensorflow::TensorShape::DebugString() const`](#string_tensorflow_TensorShape_DebugString)\r\n  * For error messages.\r\n* [`string tensorflow::TensorShape::ShortDebugString() const`](#string_tensorflow_TensorShape_ShortDebugString)\r\n* [`static bool tensorflow::TensorShape::IsValid(const TensorShapeProto &proto)`](#static_bool_tensorflow_TensorShape_IsValid)\r\n  * Returns `true` iff `proto` is a valid tensor shape.\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::TensorShape::TensorShape(gtl::ArraySlice< int64 > dim_sizes)` <a class=\"md-anchor\" id=\"tensorflow_TensorShape_TensorShape\"></a>\r\n\r\nConstruct a ` TensorShape ` from the provided sizes. REQUIRES: `dim_sizes[i] >= 0`\r\n\r\n\r\n\r\n#### `tensorflow::TensorShape::TensorShape(std::initializer_list< int64 > dim_sizes)` <a class=\"md-anchor\" id=\"tensorflow_TensorShape_TensorShape\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `tensorflow::TensorShape::TensorShape(const TensorShapeProto &proto)` <a class=\"md-anchor\" id=\"tensorflow_TensorShape_TensorShape\"></a>\r\n\r\nREQUIRES: `IsValid(proto)`\r\n\r\n\r\n\r\n#### `tensorflow::TensorShape::TensorShape()` <a class=\"md-anchor\" id=\"tensorflow_TensorShape_TensorShape\"></a>\r\n\r\n\r\n\r\nCreate a tensor shape with no dimensions and one element, which you can then call ` AddDim() ` on.\r\n\r\n#### `void tensorflow::TensorShape::Clear()` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShape_Clear\"></a>\r\n\r\nClear a tensor shape.\r\n\r\n\r\n\r\n#### `void tensorflow::TensorShape::AddDim(int64 size)` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShape_AddDim\"></a>\r\n\r\nAdd a dimension to the end (\"inner-most\"). REQUIRES: `size >= 0`\r\n\r\n\r\n\r\n#### `void tensorflow::TensorShape::AppendShape(const TensorShape &shape)` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShape_AppendShape\"></a>\r\n\r\nAppends all the dimensions from `shape`.\r\n\r\n\r\n\r\n#### `void tensorflow::TensorShape::InsertDim(int d, int64 size)` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShape_InsertDim\"></a>\r\n\r\nInsert a dimension somewhere in the ` TensorShape `. REQUIRES: `0 <= d <= dims() ` REQUIRES: `size >= 0`\r\n\r\n\r\n\r\n#### `void tensorflow::TensorShape::set_dim(int d, int64 size)` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShape_set_dim\"></a>\r\n\r\nModifies the size of the dimension `d` to be `size` REQUIRES: `0 <= d < dims() ` REQUIRES: `size >= 0`\r\n\r\n\r\n\r\n#### `void tensorflow::TensorShape::RemoveDim(int d)` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShape_RemoveDim\"></a>\r\n\r\nRemoves dimension `d` from the ` TensorShape `. REQUIRES: `0 <= d < dims() `\r\n\r\n\r\n\r\n#### `int tensorflow::TensorShape::dims() const` <a class=\"md-anchor\" id=\"int_tensorflow_TensorShape_dims\"></a>\r\n\r\nReturn the number of dimensions in the tensor.\r\n\r\n\r\n\r\n#### `int64 tensorflow::TensorShape::dim_size(int d) const` <a class=\"md-anchor\" id=\"int64_tensorflow_TensorShape_dim_size\"></a>\r\n\r\nReturns the number of elements in dimension `d`. REQUIRES: `0 <= d < dims() `\r\n\r\n\r\n\r\n#### `gtl::ArraySlice<int64> tensorflow::TensorShape::dim_sizes() const` <a class=\"md-anchor\" id=\"gtl_ArraySlice_int64_tensorflow_TensorShape_dim_sizes\"></a>\r\n\r\nReturns sizes of all dimensions.\r\n\r\n\r\n\r\n#### `int64 tensorflow::TensorShape::num_elements() const` <a class=\"md-anchor\" id=\"int64_tensorflow_TensorShape_num_elements\"></a>\r\n\r\nReturns the number of elements in the tensor.\r\n\r\nWe use `int64` and not `size_t` to be compatible with `Eigen::Tensor` which uses `ptrdiff_t`.\r\n\r\n#### `bool tensorflow::TensorShape::IsSameSize(const TensorShape &b) const` <a class=\"md-anchor\" id=\"bool_tensorflow_TensorShape_IsSameSize\"></a>\r\n\r\n\r\n\r\nReturns true if `*this` and `b` have the same sizes. Ignores dimension names.\r\n\r\n#### `bool tensorflow::TensorShape::operator==(const TensorShape &b) const` <a class=\"md-anchor\" id=\"bool_tensorflow_TensorShape_operator_\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `void tensorflow::TensorShape::AsProto(TensorShapeProto *proto) const` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShape_AsProto\"></a>\r\n\r\nFill `*proto` from `*this`.\r\n\r\n\r\n\r\n#### `Eigen::DSizes< Eigen::DenseIndex, NDIMS > tensorflow::TensorShape::AsEigenDSizes() const` <a class=\"md-anchor\" id=\"Eigen_DSizes_Eigen_DenseIndex_NDIMS_tensorflow_TensorShape_AsEigenDSizes\"></a>\r\n\r\nFill `*dsizes` from `*this`.\r\n\r\n\r\n\r\n#### `Eigen::DSizes< Eigen::DenseIndex, NDIMS > tensorflow::TensorShape::AsEigenDSizesWithPadding() const` <a class=\"md-anchor\" id=\"Eigen_DSizes_Eigen_DenseIndex_NDIMS_tensorflow_TensorShape_AsEigenDSizesWithPadding\"></a>\r\n\r\n\r\n\r\nSame as ` AsEigenDSizes() ` but allows for `NDIMS > dims() ` in which case we pad the rest of the sizes with 1.\r\n\r\n#### `TensorShapeIter tensorflow::TensorShape::begin() const` <a class=\"md-anchor\" id=\"TensorShapeIter_tensorflow_TensorShape_begin\"></a>\r\n\r\nFor iterating through the dimensions.\r\n\r\n\r\n\r\n#### `TensorShapeIter tensorflow::TensorShape::end() const` <a class=\"md-anchor\" id=\"TensorShapeIter_tensorflow_TensorShape_end\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `string tensorflow::TensorShape::DebugString() const` <a class=\"md-anchor\" id=\"string_tensorflow_TensorShape_DebugString\"></a>\r\n\r\nFor error messages.\r\n\r\n\r\n\r\n#### `string tensorflow::TensorShape::ShortDebugString() const` <a class=\"md-anchor\" id=\"string_tensorflow_TensorShape_ShortDebugString\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShape::IsValid(const TensorShapeProto &proto)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShape_IsValid\"></a>\r\n\r\nReturns `true` iff `proto` is a valid tensor shape.\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/ClassTensorShapeIter.md",
    "content": "# Class `tensorflow::TensorShapeIter` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--tensorshapeiter-\"></a>\r\n\r\n\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::TensorShapeIter::TensorShapeIter(const TensorShape *shape, int d)`](#tensorflow_TensorShapeIter_TensorShapeIter)\r\n* [`bool tensorflow::TensorShapeIter::operator==(const TensorShapeIter &rhs)`](#bool_tensorflow_TensorShapeIter_operator_)\r\n* [`bool tensorflow::TensorShapeIter::operator!=(const TensorShapeIter &rhs)`](#bool_tensorflow_TensorShapeIter_operator_)\r\n* [`void tensorflow::TensorShapeIter::operator++()`](#void_tensorflow_TensorShapeIter_operator_)\r\n* [`TensorShapeDim tensorflow::TensorShapeIter::operator*()`](#TensorShapeDim_tensorflow_TensorShapeIter_operator_)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::TensorShapeIter::TensorShapeIter(const TensorShape *shape, int d)` <a class=\"md-anchor\" id=\"tensorflow_TensorShapeIter_TensorShapeIter\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `bool tensorflow::TensorShapeIter::operator==(const TensorShapeIter &rhs)` <a class=\"md-anchor\" id=\"bool_tensorflow_TensorShapeIter_operator_\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `bool tensorflow::TensorShapeIter::operator!=(const TensorShapeIter &rhs)` <a class=\"md-anchor\" id=\"bool_tensorflow_TensorShapeIter_operator_\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `void tensorflow::TensorShapeIter::operator++()` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShapeIter_operator_\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TensorShapeDim tensorflow::TensorShapeIter::operator*()` <a class=\"md-anchor\" id=\"TensorShapeDim_tensorflow_TensorShapeIter_operator_\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/ClassTensorShapeUtils.md",
    "content": "# Class `tensorflow::TensorShapeUtils` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--tensorshapeutils-\"></a>\r\n\r\nStatic helper routines for ` TensorShape `. Includes a few common predicates on a tensor shape.\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`static bool tensorflow::TensorShapeUtils::IsScalar(const TensorShape &shape)`](#static_bool_tensorflow_TensorShapeUtils_IsScalar)\r\n* [`static bool tensorflow::TensorShapeUtils::IsVector(const TensorShape &shape)`](#static_bool_tensorflow_TensorShapeUtils_IsVector)\r\n* [`static bool tensorflow::TensorShapeUtils::IsLegacyScalar(const TensorShape &shape)`](#static_bool_tensorflow_TensorShapeUtils_IsLegacyScalar)\r\n* [`static bool tensorflow::TensorShapeUtils::IsLegacyVector(const TensorShape &shape)`](#static_bool_tensorflow_TensorShapeUtils_IsLegacyVector)\r\n* [`static bool tensorflow::TensorShapeUtils::IsVectorOrHigher(const TensorShape &shape)`](#static_bool_tensorflow_TensorShapeUtils_IsVectorOrHigher)\r\n* [`static bool tensorflow::TensorShapeUtils::IsMatrix(const TensorShape &shape)`](#static_bool_tensorflow_TensorShapeUtils_IsMatrix)\r\n* [`static bool tensorflow::TensorShapeUtils::IsMatrixOrHigher(const TensorShape &shape)`](#static_bool_tensorflow_TensorShapeUtils_IsMatrixOrHigher)\r\n* [`static TensorShape tensorflow::TensorShapeUtils::MakeShape(const T *dims, int n)`](#static_TensorShape_tensorflow_TensorShapeUtils_MakeShape)\r\n  * Returns a ` TensorShape ` whose dimensions are `dims[0]`, `dims[1]`, ..., `dims[n-1]`.\r\n* [`static string tensorflow::TensorShapeUtils::ShapeListString(const gtl::ArraySlice< TensorShape > &shapes)`](#static_string_tensorflow_TensorShapeUtils_ShapeListString)\r\n* [`static bool tensorflow::TensorShapeUtils::StartsWith(const TensorShape &shape0, const TensorShape &shape1)`](#static_bool_tensorflow_TensorShapeUtils_StartsWith)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::IsScalar(const TensorShape &shape)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_IsScalar\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::IsVector(const TensorShape &shape)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_IsVector\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::IsLegacyScalar(const TensorShape &shape)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_IsLegacyScalar\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::IsLegacyVector(const TensorShape &shape)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_IsLegacyVector\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::IsVectorOrHigher(const TensorShape &shape)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_IsVectorOrHigher\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::IsMatrix(const TensorShape &shape)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_IsMatrix\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::IsMatrixOrHigher(const TensorShape &shape)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_IsMatrixOrHigher\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static TensorShape tensorflow::TensorShapeUtils::MakeShape(const T *dims, int n)` <a class=\"md-anchor\" id=\"static_TensorShape_tensorflow_TensorShapeUtils_MakeShape\"></a>\r\n\r\nReturns a ` TensorShape ` whose dimensions are `dims[0]`, `dims[1]`, ..., `dims[n-1]`.\r\n\r\n\r\n\r\n#### `static string tensorflow::TensorShapeUtils::ShapeListString(const gtl::ArraySlice< TensorShape > &shapes)` <a class=\"md-anchor\" id=\"static_string_tensorflow_TensorShapeUtils_ShapeListString\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::StartsWith(const TensorShape &shape0, const TensorShape &shape1)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_StartsWith\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/ClassThread.md",
    "content": "# Class `tensorflow::Thread` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--thread-\"></a>\r\n\r\n\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::Thread::Thread()`](#tensorflow_Thread_Thread)\r\n* [`virtual tensorflow::Thread::~Thread()`](#virtual_tensorflow_Thread_Thread)\r\n  * Blocks until the thread of control stops running.\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::Thread::Thread()` <a class=\"md-anchor\" id=\"tensorflow_Thread_Thread\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual tensorflow::Thread::~Thread()` <a class=\"md-anchor\" id=\"virtual_tensorflow_Thread_Thread\"></a>\r\n\r\nBlocks until the thread of control stops running.\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/ClassWritableFile.md",
    "content": "# Class `tensorflow::WritableFile` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--writablefile-\"></a>\r\n\r\nA file abstraction for sequential writing.\r\n\r\nThe implementation must provide buffering since callers may append small fragments at a time to the file.\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::WritableFile::WritableFile()`](#tensorflow_WritableFile_WritableFile)\r\n* [`virtual tensorflow::WritableFile::~WritableFile()`](#virtual_tensorflow_WritableFile_WritableFile)\r\n* [`virtual Status tensorflow::WritableFile::Append(const StringPiece &data)=0`](#virtual_Status_tensorflow_WritableFile_Append)\r\n* [`virtual Status tensorflow::WritableFile::Close()=0`](#virtual_Status_tensorflow_WritableFile_Close)\r\n* [`virtual Status tensorflow::WritableFile::Flush()=0`](#virtual_Status_tensorflow_WritableFile_Flush)\r\n* [`virtual Status tensorflow::WritableFile::Sync()=0`](#virtual_Status_tensorflow_WritableFile_Sync)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::WritableFile::WritableFile()` <a class=\"md-anchor\" id=\"tensorflow_WritableFile_WritableFile\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual tensorflow::WritableFile::~WritableFile()` <a class=\"md-anchor\" id=\"virtual_tensorflow_WritableFile_WritableFile\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::WritableFile::Append(const StringPiece &data)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_WritableFile_Append\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::WritableFile::Close()=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_WritableFile_Close\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::WritableFile::Flush()=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_WritableFile_Flush\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::WritableFile::Sync()=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_WritableFile_Sync\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/StructSessionOptions.md",
    "content": "# Struct `tensorflow::SessionOptions` <a class=\"md-anchor\" id=\"AUTOGENERATED-struct--tensorflow--sessionoptions-\"></a>\r\n\r\nConfiguration information for a Session .\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`Env* tensorflow::SessionOptions::env`](#Env_tensorflow_SessionOptions_env)\r\n  * The environment to use.\r\n* [`string tensorflow::SessionOptions::target`](#string_tensorflow_SessionOptions_target)\r\n  * The TensorFlow runtime to connect to.\r\n* [`ConfigProto tensorflow::SessionOptions::config`](#ConfigProto_tensorflow_SessionOptions_config)\r\n  * Configuration options.\r\n* [`tensorflow::SessionOptions::SessionOptions()`](#tensorflow_SessionOptions_SessionOptions)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `Env* tensorflow::SessionOptions::env` <a class=\"md-anchor\" id=\"Env_tensorflow_SessionOptions_env\"></a>\r\n\r\nThe environment to use.\r\n\r\n\r\n\r\n#### `string tensorflow::SessionOptions::target` <a class=\"md-anchor\" id=\"string_tensorflow_SessionOptions_target\"></a>\r\n\r\nThe TensorFlow runtime to connect to.\r\n\r\nIf &apos;target&apos; is empty or unspecified, the local TensorFlow runtime implementation will be used. Otherwise, the TensorFlow engine defined by &apos;target&apos; will be used to perform all computations.\r\n\r\n\"target\" can be either a single entry or a comma separated list of entries. Each entry is a resolvable address of the following format: local ip:port host:port ... other system-specific formats to identify tasks and jobs ...\r\n\r\nNOTE: at the moment &apos;local&apos; maps to an in-process service-based runtime.\r\n\r\nUpon creation, a single session affines itself to one of the remote processes, with possible load balancing choices when the \"target\" resolves to a list of possible processes.\r\n\r\nIf the session disconnects from the remote process during its lifetime, session calls may fail immediately.\r\n\r\n#### `ConfigProto tensorflow::SessionOptions::config` <a class=\"md-anchor\" id=\"ConfigProto_tensorflow_SessionOptions_config\"></a>\r\n\r\nConfiguration options.\r\n\r\n\r\n\r\n#### `tensorflow::SessionOptions::SessionOptions()` <a class=\"md-anchor\" id=\"tensorflow_SessionOptions_SessionOptions\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/StructState.md",
    "content": "# Struct `tensorflow::Status::State` <a class=\"md-anchor\" id=\"AUTOGENERATED-struct--tensorflow--status--state-\"></a>\r\n\r\n\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::error::Code tensorflow::Status::State::code`](#tensorflow_error_Code_tensorflow_Status_State_code)\r\n* [`string tensorflow::Status::State::msg`](#string_tensorflow_Status_State_msg)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::error::Code tensorflow::Status::State::code` <a class=\"md-anchor\" id=\"tensorflow_error_Code_tensorflow_Status_State_code\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `string tensorflow::Status::State::msg` <a class=\"md-anchor\" id=\"string_tensorflow_Status_State_msg\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/StructTensorShapeDim.md",
    "content": "# Struct `tensorflow::TensorShapeDim` <a class=\"md-anchor\" id=\"AUTOGENERATED-struct--tensorflow--tensorshapedim-\"></a>\r\n\r\n\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`int tensorflow::TensorShapeDim::size`](#int_tensorflow_TensorShapeDim_size)\r\n* [`tensorflow::TensorShapeDim::TensorShapeDim(int64 s)`](#tensorflow_TensorShapeDim_TensorShapeDim)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `int tensorflow::TensorShapeDim::size` <a class=\"md-anchor\" id=\"int_tensorflow_TensorShapeDim_size\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `tensorflow::TensorShapeDim::TensorShapeDim(int64 s)` <a class=\"md-anchor\" id=\"tensorflow_TensorShapeDim_TensorShapeDim\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/StructThreadOptions.md",
    "content": "# Struct `tensorflow::ThreadOptions` <a class=\"md-anchor\" id=\"AUTOGENERATED-struct--tensorflow--threadoptions-\"></a>\r\n\r\nOptions to configure a Thread .\r\n\r\nNote that the options are all hints, and the underlying implementation may choose to ignore it.\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`size_t tensorflow::ThreadOptions::stack_size`](#size_t_tensorflow_ThreadOptions_stack_size)\r\n  * Thread stack size to use (in bytes).\r\n* [`size_t tensorflow::ThreadOptions::guard_size`](#size_t_tensorflow_ThreadOptions_guard_size)\r\n  * Guard area size to use near thread stacks to use (in bytes)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `size_t tensorflow::ThreadOptions::stack_size` <a class=\"md-anchor\" id=\"size_t_tensorflow_ThreadOptions_stack_size\"></a>\r\n\r\nThread stack size to use (in bytes).\r\n\r\n\r\n\r\n#### `size_t tensorflow::ThreadOptions::guard_size` <a class=\"md-anchor\" id=\"size_t_tensorflow_ThreadOptions_guard_size\"></a>\r\n\r\nGuard area size to use near thread stacks to use (in bytes)\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/cc/index.md",
    "content": "# TensorFlow C++ Session API reference documentation <a class=\"md-anchor\" id=\"AUTOGENERATED-tensorflow-c---session-api-reference-documentation\"></a>\r\n\r\nTensorFlow's public C++ API includes only the API for executing graphs, as of\r\nversion 0.5. To control the execution of a graph from C++:\r\n\r\n1. Build the computation graph using the [Python API](../../api_docs/python/).\r\n1. Use [tf.train.write_graph()](../../api_docs/python/train.md#write_graph) to\r\nwrite the graph to a file.\r\n1. Load the graph using the C++ Session API. For example:\r\n\r\n  ```c++\r\n  // Reads a model graph definition from disk, and creates a session object you\r\n  // can use to run it.\r\n  Status LoadGraph(string graph_file_name, Session** session) {\r\n    GraphDef graph_def;\r\n    TF_RETURN_IF_ERROR(\r\n        ReadBinaryProto(Env::Default(), graph_file_name, &graph_def));\r\n    TF_RETURN_IF_ERROR(NewSession(SessionOptions(), session));\r\n    TF_RETURN_IF_ERROR((*session)->Create(graph_def));\r\n    return Status::OK();\r\n  }\r\n```\r\n\r\n1. Run the graph with a call to `session->Run()`\r\n\r\n\r\n##Classes <a class=\"md-anchor\" id=\"AUTOGENERATED-classes\"></a>\r\n\r\n* [tensorflow::Env](../../api_docs/cc/ClassEnv.md)\r\n* [tensorflow::EnvWrapper](../../api_docs/cc/ClassEnvWrapper.md)\r\n* [tensorflow::RandomAccessFile](../../api_docs/cc/ClassRandomAccessFile.md)\r\n* [tensorflow::Session](../../api_docs/cc/ClassSession.md)\r\n* [tensorflow::Status](../../api_docs/cc/ClassStatus.md)\r\n* [tensorflow::Tensor](../../api_docs/cc/ClassTensor.md)\r\n* [tensorflow::TensorBuffer](../../api_docs/cc/ClassTensorBuffer.md)\r\n* [tensorflow::TensorShape](../../api_docs/cc/ClassTensorShape.md)\r\n* [tensorflow::TensorShapeIter](../../api_docs/cc/ClassTensorShapeIter.md)\r\n* [tensorflow::TensorShapeUtils](../../api_docs/cc/ClassTensorShapeUtils.md)\r\n* [tensorflow::Thread](../../api_docs/cc/ClassThread.md)\r\n* [tensorflow::WritableFile](../../api_docs/cc/ClassWritableFile.md)\r\n\r\n##Structs <a class=\"md-anchor\" id=\"AUTOGENERATED-structs\"></a>\r\n\r\n* [tensorflow::SessionOptions](../../api_docs/cc/StructSessionOptions.md)\r\n* [tensorflow::Status::State](../../api_docs/cc/StructState.md)\r\n* [tensorflow::TensorShapeDim](../../api_docs/cc/StructTensorShapeDim.md)\r\n* [tensorflow::ThreadOptions](../../api_docs/cc/StructThreadOptions.md)\r\n\r\n\r\n<div class='sections-order' style=\"display: none;\">\r\n<!--\r\n<!-- ClassEnv.md -->\r\n<!-- ClassEnvWrapper.md -->\r\n<!-- ClassRandomAccessFile.md -->\r\n<!-- ClassSession.md -->\r\n<!-- ClassStatus.md -->\r\n<!-- ClassTensor.md -->\r\n<!-- ClassTensorBuffer.md -->\r\n<!-- ClassTensorShape.md -->\r\n<!-- ClassTensorShapeIter.md -->\r\n<!-- ClassTensorShapeUtils.md -->\r\n<!-- ClassThread.md -->\r\n<!-- ClassWritableFile.md -->\r\n<!-- StructSessionOptions.md -->\r\n<!-- StructState.md -->\r\n<!-- StructTensorShapeDim.md -->\r\n<!-- StructThreadOptions.md -->\r\n-->\r\n</div>\r\n"
  },
  {
    "path": "SOURCE/api_docs/index.md",
    "content": "# Overview <a class=\"md-anchor\" id=\"AUTOGENERATED-overview\"></a>\r\n\r\nTensorFlow has APIs available in several languages both for constructing and\r\nexecuting a TensorFlow graph.  The Python API is at present the most complete\r\nand the easiest to use, but the C++ API may offer some performance advantages\r\nin graph execution, and supports deployment to small devices such as Android.\r\n\r\nOver time, we hope that the TensorFlow community will develop front ends for\r\nlanguages like Go, Java, JavaScript, Lua R, and perhaps others. With\r\n[SWIG](http://swig.org), it's relatively easy to develop a TensorFlow interface\r\nfor your favorite language.\r\n\r\nNote: Many practical aspects of usage are covered in the Mechanics tab, and\r\nsome additional documentation not specific to any particular language API is\r\navailable in the Resources tab.\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/python/array_ops.md",
    "content": "<!-- This file is machine generated: DO NOT EDIT! -->\r\n\r\n# Tensor Transformations <a class=\"md-anchor\" id=\"AUTOGENERATED-tensor-transformations\"></a>\r\n\r\nNote: Functions taking `Tensor` arguments can also take anything accepted by\r\n[`tf.convert_to_tensor`](../../api_docs/python/framework.md#convert_to_tensor).\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Tensor Transformations](#AUTOGENERATED-tensor-transformations)\r\n* [Casting](#AUTOGENERATED-casting)\r\n  * [`tf.string_to_number(string_tensor, out_type=None, name=None)`](#string_to_number)\r\n  * [`tf.to_double(x, name='ToDouble')`](#to_double)\r\n  * [`tf.to_float(x, name='ToFloat')`](#to_float)\r\n  * [`tf.to_bfloat16(x, name='ToBFloat16')`](#to_bfloat16)\r\n  * [`tf.to_int32(x, name='ToInt32')`](#to_int32)\r\n  * [`tf.to_int64(x, name='ToInt64')`](#to_int64)\r\n  * [`tf.cast(x, dtype, name=None)`](#cast)\r\n* [Shapes and Shaping](#AUTOGENERATED-shapes-and-shaping)\r\n  * [`tf.shape(input, name=None)`](#shape)\r\n  * [`tf.size(input, name=None)`](#size)\r\n  * [`tf.rank(input, name=None)`](#rank)\r\n  * [`tf.reshape(tensor, shape, name=None)`](#reshape)\r\n  * [`tf.squeeze(input, squeeze_dims=None, name=None)`](#squeeze)\r\n  * [`tf.expand_dims(input, dim, name=None)`](#expand_dims)\r\n* [Slicing and Joining](#AUTOGENERATED-slicing-and-joining)\r\n  * [`tf.slice(input_, begin, size, name=None)`](#slice)\r\n  * [`tf.split(split_dim, num_split, value, name='split')`](#split)\r\n  * [`tf.tile(input, multiples, name=None)`](#tile)\r\n  * [`tf.pad(input, paddings, name=None)`](#pad)\r\n  * [`tf.concat(concat_dim, values, name='concat')`](#concat)\r\n  * [`tf.pack(values, name='pack')`](#pack)\r\n  * [`tf.unpack(value, num=None, name='unpack')`](#unpack)\r\n  * [`tf.reverse_sequence(input, seq_lengths, seq_dim, name=None)`](#reverse_sequence)\r\n  * [`tf.reverse(tensor, dims, name=None)`](#reverse)\r\n  * [`tf.transpose(a, perm=None, name='transpose')`](#transpose)\r\n  * [`tf.gather(params, indices, name=None)`](#gather)\r\n  * [`tf.dynamic_partition(data, partitions, num_partitions, name=None)`](#dynamic_partition)\r\n  * [`tf.dynamic_stitch(indices, data, name=None)`](#dynamic_stitch)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## Casting <a class=\"md-anchor\" id=\"AUTOGENERATED-casting\"></a>\r\n\r\nTensorFlow provides several operations that you can use to cast tensor data\r\ntypes in your graph.\r\n\r\n- - -\r\n\r\n### `tf.string_to_number(string_tensor, out_type=None, name=None)` <a class=\"md-anchor\" id=\"string_to_number\"></a>\r\n\r\nConverts each string in the input Tensor to the specified numeric type.\r\n\r\n(Note that int32 overflow results in an error while float overflow\r\nresults in a rounded value.)\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`string_tensor`</b>: A `Tensor` of type `string`.\r\n*  <b>`out_type`</b>: An optional `tf.DType` from: `tf.float32, tf.int32`. Defaults to `tf.float32`.\r\n    The numeric type to interpret each string in string_tensor as.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `out_type`.\r\n  A Tensor of the same shape as the input string_tensor.\r\n\r\n\r\n- - -\r\n\r\n### `tf.to_double(x, name='ToDouble')` <a class=\"md-anchor\" id=\"to_double\"></a>\r\n\r\nCasts a tensor to type `float64`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor` or `SparseTensor`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` or `SparseTensor` with same shape as `x` with type `float64`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `x` cannot be cast to the `float64`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.to_float(x, name='ToFloat')` <a class=\"md-anchor\" id=\"to_float\"></a>\r\n\r\nCasts a tensor to type `float32`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor` or `SparseTensor`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` or `SparseTensor` with same shape as `x` with type `float32`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `x` cannot be cast to the `float32`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.to_bfloat16(x, name='ToBFloat16')` <a class=\"md-anchor\" id=\"to_bfloat16\"></a>\r\n\r\nCasts a tensor to type `bfloat16`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor` or `SparseTensor`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` or `SparseTensor` with same shape as `x` with type `bfloat16`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `x` cannot be cast to the `bfloat16`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.to_int32(x, name='ToInt32')` <a class=\"md-anchor\" id=\"to_int32\"></a>\r\n\r\nCasts a tensor to type `int32`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor` or `SparseTensor`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` or `SparseTensor` with same shape as `x` with type `int32`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `x` cannot be cast to the `int32`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.to_int64(x, name='ToInt64')` <a class=\"md-anchor\" id=\"to_int64\"></a>\r\n\r\nCasts a tensor to type `int64`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor` or `SparseTensor`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` or `SparseTensor` with same shape as `x` with type `int64`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `x` cannot be cast to the `int64`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.cast(x, dtype, name=None)` <a class=\"md-anchor\" id=\"cast\"></a>\r\n\r\nCasts a tensor to a new type.\r\n\r\nThe operation casts `x` (in case of `Tensor`) or `x.values`\r\n(in case of `SparseTensor`) to `dtype`.\r\n\r\nFor example:\r\n\r\n```python\r\n# tensor `a` is [1.8, 2.2], dtype=tf.float\r\ntf.cast(a, tf.int32) ==> [1, 2]  # dtype=tf.int32\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor` or `SparseTensor`.\r\n*  <b>`dtype`</b>: The destination type.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` or `SparseTensor` with same shape as `x`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `x` cannot be cast to the `dtype`.\r\n\r\n\r\n\r\n## Shapes and Shaping <a class=\"md-anchor\" id=\"AUTOGENERATED-shapes-and-shaping\"></a>\r\n\r\nTensorFlow provides several operations that you can use to determine the shape\r\nof a tensor and change the shape of a tensor.\r\n\r\n- - -\r\n\r\n### `tf.shape(input, name=None)` <a class=\"md-anchor\" id=\"shape\"></a>\r\n\r\nReturns the shape of a tensor.\r\n\r\nThis operation returns a 1-D integer tensor representing the shape of `input`.\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# 't' is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]\r\nshape(t) ==> [2, 2, 3]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `int32`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.size(input, name=None)` <a class=\"md-anchor\" id=\"size\"></a>\r\n\r\nReturns the size of a tensor.\r\n\r\nThis operation returns an integer representing the number of elements in\r\n`input`.\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# 't' is [[[1, 1,, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]]\r\nsize(t) ==> 12\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `int32`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.rank(input, name=None)` <a class=\"md-anchor\" id=\"rank\"></a>\r\n\r\nReturns the rank of a tensor.\r\n\r\nThis operation returns an integer representing the rank of `input`.\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# 't' is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]\r\n# shape of tensor 't' is [2, 2, 3]\r\nrank(t) ==> 3\r\n```\r\n\r\n**Note**: The rank of a tensor is not the same as the rank of a matrix. The rank\r\nof a tensor is the number of indices required to uniquely select each element\r\nof the tensor. Rank is also known as \"order\", \"degree\", or \"ndims.\"\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `int32`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.reshape(tensor, shape, name=None)` <a class=\"md-anchor\" id=\"reshape\"></a>\r\n\r\nReshapes a tensor.\r\n\r\nGiven `tensor`, this operation returns a tensor that has the same values\r\nas `tensor` with shape `shape`.\r\n\r\nIf `shape` is the special value `[-1]`, then `tensor` is flattened and the\r\noperation outputs a 1-D tensor with all elements of `tensor`.\r\n\r\nIf `shape` is 1-D or higher, then the operation returns a tensor with shape\r\n`shape` filled with the values of `tensor`. In this case, the number of elements\r\nimplied by `shape` must be the same as the number of elements in `tensor`.\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]\r\n# tensor 't' has shape [9]\r\nreshape(t, [3, 3]) ==> [[1, 2, 3]\r\n                        [4, 5, 6]\r\n                        [7, 8, 9]]\r\n\r\n# tensor 't' is [[[1, 1], [2, 2]]\r\n#                [[3, 3], [4, 4]]]\r\n# tensor 't' has shape [2, 2]\r\nreshape(t, [2, 4]) ==> [[1, 1, 2, 2]\r\n                        [3, 3, 4, 4]]\r\n\r\n# tensor 't' is [[[1, 1, 1],\r\n#                 [2, 2, 2]],\r\n#                [[3, 3, 3],\r\n#                 [4, 4, 4]],\r\n#                [[5, 5, 5],\r\n#                 [6, 6, 6]]]\r\n# tensor 't' has shape [3, 2, 3]\r\n# pass '[-1]' to flatten 't'\r\nreshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tensor`</b>: A `Tensor`.\r\n*  <b>`shape`</b>: A `Tensor` of type `int32`. Defines the shape of the output tensor.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `tensor`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.squeeze(input, squeeze_dims=None, name=None)` <a class=\"md-anchor\" id=\"squeeze\"></a>\r\n\r\nRemoves dimensions of size 1 from the shape of a tensor.\r\n\r\nGiven a tensor `input`, this operation returns a tensor of the same type with\r\nall dimensions of size 1 removed. If you don't want to remove all size 1\r\ndimensions, you can remove specific size 1 dimensions by specifying\r\n`squeeze_dims`.\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]\r\nshape(squeeze(t)) ==> [2, 3]\r\n```\r\n\r\nOr, to remove specific size 1 dimensions:\r\n\r\n```prettyprint\r\n# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]\r\nshape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`. The `input` to squeeze.\r\n*  <b>`squeeze_dims`</b>: An optional list of `ints`. Defaults to `[]`.\r\n    If specified, only squeezes the dimensions listed. The dimension\r\n    index starts at 0. It is an error to squeeze a dimension that is not 1.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `input`.\r\n  Contains the same data as `input`, but has one or more dimensions of\r\n  size 1 removed.\r\n\r\n\r\n- - -\r\n\r\n### `tf.expand_dims(input, dim, name=None)` <a class=\"md-anchor\" id=\"expand_dims\"></a>\r\n\r\nInserts a dimension of 1 into a tensor's shape.\r\n\r\nGiven a tensor `input`, this operation inserts a dimension of 1 at the\r\ndimension index `dim` of `input`'s shape. The dimension index `dim` starts at\r\nzero; if you specify a negative number for `dim` it is counted backward from\r\nthe end.\r\n\r\nThis operation is useful if you want to add a batch dimension to a single\r\nelement. For example, if you have a single image of shape `[height, width,\r\nchannels]`, you can make it a batch of 1 image with `expand_dims(image, 0)`,\r\nwhich will make the shape `[1, height, width, channels]`.\r\n\r\nOther examples:\r\n\r\n```prettyprint\r\n# 't' is a tensor of shape [2]\r\nshape(expand_dims(t, 0)) ==> [1, 2]\r\nshape(expand_dims(t, 1)) ==> [2, 1]\r\nshape(expand_dims(t, -1)) ==> [2, 1]\r\n\r\n# 't2' is a tensor of shape [2, 3, 5]\r\nshape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]\r\nshape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]\r\nshape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]\r\n```\r\n\r\nThis operation requires that:\r\n\r\n`-1-input.dims() <= dim <= input.dims()`\r\n\r\nThis operation is related to `squeeze()`, which removes dimensions of\r\nsize 1.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`.\r\n*  <b>`dim`</b>: A `Tensor` of type `int32`.\r\n    0-D (scalar). Specifies the dimension index at which to\r\n    expand the shape of `input`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `input`.\r\n  Contains the same data as `input`, but its shape has an additional\r\n  dimension of size 1 added.\r\n\r\n\r\n\r\n## Slicing and Joining <a class=\"md-anchor\" id=\"AUTOGENERATED-slicing-and-joining\"></a>\r\n\r\nTensorFlow provides several operations to slice or extract parts of a tensor,\r\nor join multiple tensors together.\r\n\r\n- - -\r\n\r\n### `tf.slice(input_, begin, size, name=None)` <a class=\"md-anchor\" id=\"slice\"></a>\r\n\r\nExtracts a slice from a tensor.\r\n\r\nThis operation extracts a slice of size `size` from a tensor `input` starting\r\nat the location specified by `begin`. The slice `size` is represented as a\r\ntensor shape, where `size[i]` is the number of elements of the 'i'th dimension\r\nof `input` that you want to slice. The starting location (`begin`) for the\r\nslice is represented as an offset in each dimension of `input`. In other\r\nwords, `begin[i]` is the offset into the 'i'th dimension of `input` that you\r\nwant to slice from.\r\n\r\n`begin` is zero-based; `size` is one-based. If `size[i]` is -1,\r\nall remaining elements in dimension i are included in the\r\nslice. In other words, this is equivalent to setting:\r\n\r\n`size[i] = input.dim_size(i) - begin[i]`\r\n\r\nThis operation requires that:\r\n\r\n`0 <= begin[i] <= begin[i] + size[i] <= Di  for i in [0, n]`\r\n\r\nFor example:\r\n\r\n```\r\n# 'input' is [[[1, 1, 1], [2, 2, 2]],\r\n#             [[3, 3, 3], [4, 4, 4]],\r\n#             [[5, 5, 5], [6, 6, 6]]]\r\ntf.slice(input, [1, 0, 0], [1, 1, 3]) ==> [[[3, 3, 3]]]\r\ntf.slice(input, [1, 0, 0], [1, 2, 3]) ==> [[[3, 3, 3],\r\n                                            [4, 4, 4]]]\r\ntf.slice(input, [1, 0, 0], [2, 1, 3]) ==> [[[3, 3, 3]],\r\n                                           [[5, 5, 5]]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input_`</b>: A `Tensor`.\r\n*  <b>`begin`</b>: An `int32` or `int64` `Tensor`.\r\n*  <b>`size`</b>: An `int32` or `int64` `Tensor`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` the same type as `input`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.split(split_dim, num_split, value, name='split')` <a class=\"md-anchor\" id=\"split\"></a>\r\n\r\nSplits a tensor into `num_split` tensors along one dimension.\r\n\r\nSplits `value` along dimension `split_dim` into `num_split` smaller tensors.\r\nRequires that `num_split` evenly divide `value.shape[split_dim]`.\r\n\r\nFor example:\r\n\r\n```python\r\n# 'value' is a tensor with shape [5, 30]\r\n# Split 'value' into 3 tensors along dimension 1\r\nsplit0, split1, split2 = tf.split(1, 3, value)\r\ntf.shape(split0) ==> [5, 10]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`split_dim`</b>: A 0-D `int32` `Tensor`. The dimension along which to split.\r\n    Must be in the range `[0, rank(value))`.\r\n*  <b>`num_split`</b>: A 0-D `int32` `Tensor`. The number of ways to split.\r\n*  <b>`value`</b>: The `Tensor` to split.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  `num_split` `Tensor` objects resulting from splitting `value`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.tile(input, multiples, name=None)` <a class=\"md-anchor\" id=\"tile\"></a>\r\n\r\nConstructs a tensor by tiling a given tensor.\r\n\r\nThis operation creates a new tensor by replicating `input` `multiples` times.\r\nThe output tensor's i'th dimension has `input.dims(i) * multiples[i]` elements,\r\nand the values of `input` are replicated `multiples[i]` times along the 'i'th\r\ndimension. For example, tiling `[a b c d]` by `[2]` produces\r\n`[a b c d a b c d]`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`. 1-D or higher.\r\n*  <b>`multiples`</b>: A `Tensor` of type `int32`.\r\n    1-D. Length must be the same as the number of dimensions in `input`\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `input`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.pad(input, paddings, name=None)` <a class=\"md-anchor\" id=\"pad\"></a>\r\n\r\nPads a tensor with zeros.\r\n\r\nThis operation pads a `input` with zeros according to the `paddings` you\r\nspecify. `paddings` is an integer tensor with shape `[Dn, 2]`, where n is the\r\nrank of `input`. For each dimension D of `input`, `paddings[D, 0]` indicates\r\nhow many zeros to add before the contents of `input` in that dimension, and\r\n`paddings[D, 1]` indicates how many zeros to add after the contents of `input`\r\nin that dimension.\r\n\r\nThe padded size of each dimension D of the output is:\r\n\r\n`paddings(D, 0) + input.dim_size(D) + paddings(D, 1)`\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# 't' is [[1, 1], [2, 2]]\r\n# 'paddings' is [[1, 1]], [2, 2]]\r\n# rank of 't' is 2\r\npad(t, paddings) ==> [[0, 0, 0, 0, 0]\r\n                      [0, 0, 0, 0, 0]\r\n                      [0, 1, 1, 0, 0]\r\n                     [[0, 2, 2, 0, 0]\r\n                      [0, 0, 0, 0, 0]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`.\r\n*  <b>`paddings`</b>: A `Tensor` of type `int32`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `input`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.concat(concat_dim, values, name='concat')` <a class=\"md-anchor\" id=\"concat\"></a>\r\n\r\nConcatenates tensors along one dimension.\r\n\r\nConcatenates the list of tensors `values` along dimension `concat_dim`.  If\r\n`values[i].shape = [D0, D1, ... Dconcat_dim(i), ...Dn]`, the concatenated\r\nresult has shape\r\n\r\n    [D0, D1, ... Rconcat_dim, ...Dn]\r\n\r\nwhere\r\n\r\n    Rconcat_dim = sum(Dconcat_dim(i))\r\n\r\nThat is, the data from the input tensors is joined along the `concat_dim`\r\ndimension.\r\n\r\nThe number of dimensions of the input tensors must match, and all dimensions\r\nexcept `concat_dim` must be equal.\r\n\r\nFor example:\r\n\r\n```python\r\nt1 = [[1, 2, 3], [4, 5, 6]]\r\nt2 = [[7, 8, 9], [10, 11, 12]]\r\ntf.concat(0, [t1, t2]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]\r\ntf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]\r\n\r\n# tensor t3 with shape [2, 3]\r\n# tensor t4 with shape [2, 3]\r\ntf.shape(tf.concat(0, [t3, t4])) ==> [4, 3]\r\ntf.shape(tf.concat(1, [t3, t4])) ==> [2, 6]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`concat_dim`</b>: 0-D `int32` `Tensor`.  Dimension along which to concatenate.\r\n*  <b>`values`</b>: A list of `Tensor` objects or a single `Tensor`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` resulting from concatenation of the input tensors.\r\n\r\n\r\n- - -\r\n\r\n### `tf.pack(values, name='pack')` <a class=\"md-anchor\" id=\"pack\"></a>\r\n\r\nPacks a list of rank-`R` tensors into one rank-`(R+1)` tensor.\r\n\r\nPacks tensors in `values` into a tensor with rank one higher than each tensor\r\nin `values` and shape `[len(values)] + values[0].shape`. The output satisfies\r\n`output[i, ...] = values[i][...]`.\r\n\r\nThis is the opposite of unpack.  The numpy equivalent is\r\n\r\n    tf.pack([x, y, z]) = np.asarray([x, y, z])\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`values`</b>: A list of `Tensor` objects with the same shape and type.\r\n*  <b>`name`</b>: A name for this operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n\r\n*  <b>`output`</b>: A packed `Tensor` with the same type as `values`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.unpack(value, num=None, name='unpack')` <a class=\"md-anchor\" id=\"unpack\"></a>\r\n\r\nUnpacks the outer dimension of a rank-`R` tensor into rank-`(R-1)` tensors.\r\n\r\nUnpacks `num` tensors from `value` along the first dimension.\r\nIf `num` is not specified (the default), it is inferred from `value`'s shape.\r\nIf `value.shape[0]` is not known, `ValueError` is raised.\r\n\r\nThe ith tensor in `output` is the slice `value[i, ...]`. Each tensor in\r\n`output` has shape `value.shape[1:]`.\r\n\r\nThis is the opposite of pack.  The numpy equivalent is\r\n\r\n    tf.unpack(x, n) = list(x)\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`value`</b>: A rank `R > 0` `Tensor` to be unpacked.\r\n*  <b>`num`</b>: An `int`. The first dimension of value. Automatically inferred if\r\n    `None` (the default).\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The list of `Tensor` objects unpacked from `value`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `num` is unspecified and cannot be inferred.\r\n\r\n\r\n- - -\r\n\r\n### `tf.reverse_sequence(input, seq_lengths, seq_dim, name=None)` <a class=\"md-anchor\" id=\"reverse_sequence\"></a>\r\n\r\nReverses variable length slices in dimension `seq_dim`.\r\n\r\nThis op first slices `input` along the first dimension, and for each slice `i`,\r\nreverses the first `seq_lengths[i]` elements along the dimension `seq_dim`.\r\n\r\nThe elements of `seq_lengths` must obey `seq_lengths[i] < input.dims[seq_dim]`,\r\nand `seq_lengths` must be a vector of length `input.dims(0)`.\r\n\r\nThe output slice `i` along dimension 0 is then given by input slice `i`, with\r\nthe first `seq_lengths[i]` slices along dimension `seq_dim` reversed.\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# Given this:\r\nseq_dim = 1\r\ninput.dims = (4, ...)\r\nseq_lengths = [7, 2, 3, 5]\r\n\r\n# then slices of input are reversed on seq_dim, but only up to seq_lengths:\r\noutput[0, 0:7, :, ...] = input[0, 7:0:-1, :, ...]\r\noutput[1, 0:2, :, ...] = input[1, 2:0:-1, :, ...]\r\noutput[2, 0:3, :, ...] = input[2, 3:0:-1, :, ...]\r\noutput[3, 0:5, :, ...] = input[3, 5:0:-1, :, ...]\r\n\r\n# while entries past seq_lens are copied through:\r\noutput[0, 7:, :, ...] = input[0, 7:, :, ...]\r\noutput[1, 2:, :, ...] = input[1, 2:, :, ...]\r\noutput[2, 3:, :, ...] = input[2, 3:, :, ...]\r\noutput[3, 2:, :, ...] = input[3, 2:, :, ...]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`. The input to reverse.\r\n*  <b>`seq_lengths`</b>: A `Tensor` of type `int64`.\r\n    1-D with length `input.dims(0)` and\r\n    `max(seq_lengths) < input.dims(seq_dim)`\r\n*  <b>`seq_dim`</b>: An `int`. The dimension which is partially reversed.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `input`.\r\n  The partially reversed input. It has the same shape as `input`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.reverse(tensor, dims, name=None)` <a class=\"md-anchor\" id=\"reverse\"></a>\r\n\r\nReverses specific dimensions of a tensor.\r\n\r\nGiven a `tensor`, and a `bool` tensor `dims` representing the dimensions\r\nof `tensor`, this operation reverses each dimension i of `tensor` where\r\n`dims[i]` is `True`.\r\n\r\n`tensor` can have up to 8 dimensions. The number of dimensions\r\nof `tensor` must equal the number of elements in `dims`. In other words:\r\n\r\n`rank(tensor) = size(dims)`\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# tensor 't' is [[[[ 0,  1,  2,  3],\r\n#                  [ 4,  5,  6,  7],\r\n#                  [ 8,  9, 10, 11]],\r\n#                 [[12, 13, 14, 15],\r\n#                  [16, 17, 18, 19],\r\n#                  [20, 21, 22, 23]]]]\r\n# tensor 't' shape is [1, 2, 3, 4]\r\n\r\n# 'dims' is [False, False, False, True]\r\nreverse(t, dims) ==> [[[[ 3,  2,  1,  0],\r\n                        [ 7,  6,  5,  4],\r\n                        [ 11, 10, 9, 8]],\r\n                       [[15, 14, 13, 12],\r\n                        [19, 18, 17, 16],\r\n                        [23, 22, 21, 20]]]]\r\n\r\n# 'dims' is [False, True, False, False]\r\nreverse(t, dims) ==> [[[[12, 13, 14, 15],\r\n                        [16, 17, 18, 19],\r\n                        [20, 21, 22, 23]\r\n                       [[ 0,  1,  2,  3],\r\n                        [ 4,  5,  6,  7],\r\n                        [ 8,  9, 10, 11]]]]\r\n\r\n# 'dims' is [False, False, True, False]\r\nreverse(t, dims) ==> [[[[8, 9, 10, 11],\r\n                        [4, 5, 6, 7],\r\n                        [0, 1, 2, 3]]\r\n                       [[20, 21, 22, 23],\r\n                        [16, 17, 18, 19],\r\n                        [12, 13, 14, 15]]]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tensor`</b>: A `Tensor`. Must be one of the following types: `uint8`, `int8`, `int32`, `bool`, `float32`, `float64`.\r\n    Up to 8-D.\r\n*  <b>`dims`</b>: A `Tensor` of type `bool`. 1-D. The dimensions to reverse.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `tensor`. The same shape as `tensor`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.transpose(a, perm=None, name='transpose')` <a class=\"md-anchor\" id=\"transpose\"></a>\r\n\r\nTransposes `a`. Permutes the dimensions according to `perm`.\r\n\r\nThe returned tensor's dimension i will correspond to the input dimension\r\n`perm[i]`. If `perm` is not given, it is set to (n-1...0), where n is\r\nthe rank of the input tensor. Hence by default, this operation performs a\r\nregular matrix transpose on 2-D input Tensors.\r\n\r\nFor example:\r\n\r\n```python\r\n# 'x' is [[1 2 3]\r\n#         [4 5 6]]\r\ntf.transpose(x) ==> [[1 4]\r\n                     [2 5]\r\n                     [3 6]]\r\n\r\n# Equivalently\r\ntf.transpose(x perm=[0, 1]) ==> [[1 4]\r\n                                 [2 5]\r\n                                 [3 6]]\r\n\r\n# 'perm' is more useful for n-dimensional tensors, for n > 2\r\n# 'x' is   [[[1  2  3]\r\n#            [4  5  6]]\r\n#           [[7  8  9]\r\n#            [10 11 12]]]\r\n# Take the transpose of the matrices in dimension-0\r\ntf.transpose(b, perm=[0, 2, 1]) ==> [[[1  4]\r\n                                      [2  5]\r\n                                      [3  6]]\r\n\r\n                                     [[7 10]\r\n                                      [8 11]\r\n                                      [9 12]]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`a`</b>: A `Tensor`.\r\n*  <b>`perm`</b>: A permutation of the dimensions of `a`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A transposed `Tensor`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.gather(params, indices, name=None)` <a class=\"md-anchor\" id=\"gather\"></a>\r\n\r\nGather slices from `params` according to `indices`.\r\n\r\n`indices` must be an integer tensor of any dimension (usually 0-D or 1-D).\r\nProduces an output tensor with shape `indices.shape + params.shape[1:]` where:\r\n\r\n    # Scalar indices\r\n    output[:, ..., :] = params[indices, :, ... :]\r\n\r\n    # Vector indices\r\n    output[i, :, ..., :] = params[indices[i], :, ... :]\r\n\r\n    # Higher rank indices\r\n    output[i, ..., j, :, ... :] = params[indices[i, ..., j], :, ..., :]\r\n\r\nIf `indices` is a permutation and `len(indices) == params.shape[0]` then\r\nthis operation will permute `params` accordingly.\r\n\r\n<div style=\"width:70%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/Gather.png\" alt>\r\n</div>\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`params`</b>: A `Tensor`.\r\n*  <b>`indices`</b>: A `Tensor`. Must be one of the following types: `int32`, `int64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `params`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.dynamic_partition(data, partitions, num_partitions, name=None)` <a class=\"md-anchor\" id=\"dynamic_partition\"></a>\r\n\r\nPartitions `data` into `num_partitions` tensors using indices from `partitions`.\r\n\r\nFor each index tuple `js` of size `partitions.ndim`, the slice `data[js, ...]`\r\nbecomes part of `outputs[partitions[js]]`.  The slices with `partitions[js] = i`\r\nare placed in `outputs[i]` in lexicographic order of `js`, and the first\r\ndimension of `outputs[i]` is the number of entries in `partitions` equal to `i`.\r\nIn detail,\r\n\r\n    outputs[i].shape = [sum(partitions == i)] + data.shape[partitions.ndim:]\r\n\r\n    outputs[i] = pack([data[js, ...] for js if partitions[js] == i])\r\n\r\n`data.shape` must start with `partitions.shape`.\r\n\r\nFor example:\r\n\r\n    # Scalar partitions\r\n    partitions = 1\r\n    num_partitions = 2\r\n    data = [10, 20]\r\n    outputs[0] = []  # Empty with shape [0, 2]\r\n    outputs[1] = [[10, 20]]\r\n\r\n    # Vector partitions\r\n    partitions = [0, 0, 1, 1, 0]\r\n    num_partitions = 2\r\n    data = [10, 20, 30, 40, 50]\r\n    outputs[0] = [10, 20, 50]\r\n    outputs[1] = [30, 40]\r\n\r\n<div style=\"width:70%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/DynamicPartition.png\" alt>\r\n</div>\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`data`</b>: A `Tensor`.\r\n*  <b>`partitions`</b>: A `Tensor` of type `int32`.\r\n    Any shape.  Indices in the range `[0, num_partitions)`.\r\n*  <b>`num_partitions`</b>: An `int` that is `>= 1`.\r\n    The number of partitions to output.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of `num_partitions` `Tensor` objects of the same type as data.\r\n\r\n\r\n- - -\r\n\r\n### `tf.dynamic_stitch(indices, data, name=None)` <a class=\"md-anchor\" id=\"dynamic_stitch\"></a>\r\n\r\nInterleave the values from the `data` tensors into a single tensor.\r\n\r\nBuilds a merged tensor such that\r\n\r\n    merged[indices[m][i, ..., j], ...] = data[m][i, ..., j, ...]\r\n\r\nFor example, if each `indices[m]` is scalar or vector, we have\r\n\r\n    # Scalar indices\r\n    merged[indices[m], ...] = data[m][...]\r\n\r\n    # Vector indices\r\n    merged[indices[m][i], ...] = data[m][i, ...]\r\n\r\nEach `data[i].shape` must start with the corresponding `indices[i].shape`,\r\nand the rest of `data[i].shape` must be constant w.r.t. `i`.  That is, we\r\nmust have `data[i].shape = indices[i].shape + constant`.  In terms of this\r\n`constant`, the output shape is\r\n\r\n    merged.shape = [max(indices)] + constant\r\n\r\nValues are merged in order, so if an index appears in both `indices[m][i]` and\r\n`indices[n][j]` for `(m,i) < (n,j)` the slice `data[n][j]` will appear in the\r\nmerged result.\r\n\r\nFor example:\r\n\r\n    indices[0] = 6\r\n    indices[1] = [4, 1]\r\n    indices[2] = [[5, 2], [0, 3]]\r\n    data[0] = [61, 62]\r\n    data[1] = [[41, 42], [11, 12]]\r\n    data[2] = [[[51, 52], [21, 22]], [[1, 2], [31, 32]]]\r\n    merged = [[1, 2], [11, 12], [21, 22], [31, 32], [41, 42],\r\n              [51, 52], [61, 62]]\r\n\r\n<div style=\"width:70%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/DynamicStitch.png\" alt>\r\n</div>\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`indices`</b>: A list of at least 2 `Tensor` objects of type `int32`.\r\n*  <b>`data`</b>: A list with the same number of `Tensor` objects as `indices` of `Tensor` objects of the same type.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `data`.\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/python/client.md",
    "content": "<!-- This file is machine generated: DO NOT EDIT! -->\r\n\r\n# Running Graphs <a class=\"md-anchor\" id=\"AUTOGENERATED-running-graphs\"></a>\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Running Graphs](#AUTOGENERATED-running-graphs)\r\n* [Session management](#AUTOGENERATED-session-management)\r\n  * [`class tf.Session`](#Session)\r\n  * [`class tf.InteractiveSession`](#InteractiveSession)\r\n  * [`tf.get_default_session()`](#get_default_session)\r\n* [Error classes](#AUTOGENERATED-error-classes)\r\n  * [`class tf.OpError`](#OpError)\r\n  * [`class tf.errors.CancelledError`](#CancelledError)\r\n  * [`class tf.errors.UnknownError`](#UnknownError)\r\n  * [`class tf.errors.InvalidArgumentError`](#InvalidArgumentError)\r\n  * [`class tf.errors.DeadlineExceededError`](#DeadlineExceededError)\r\n  * [`class tf.errors.NotFoundError`](#NotFoundError)\r\n  * [`class tf.errors.AlreadyExistsError`](#AlreadyExistsError)\r\n  * [`class tf.errors.PermissionDeniedError`](#PermissionDeniedError)\r\n  * [`class tf.errors.UnauthenticatedError`](#UnauthenticatedError)\r\n  * [`class tf.errors.ResourceExhaustedError`](#ResourceExhaustedError)\r\n  * [`class tf.errors.FailedPreconditionError`](#FailedPreconditionError)\r\n  * [`class tf.errors.AbortedError`](#AbortedError)\r\n  * [`class tf.errors.OutOfRangeError`](#OutOfRangeError)\r\n  * [`class tf.errors.UnimplementedError`](#UnimplementedError)\r\n  * [`class tf.errors.InternalError`](#InternalError)\r\n  * [`class tf.errors.UnavailableError`](#UnavailableError)\r\n  * [`class tf.errors.DataLossError`](#DataLossError)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\nThis library contains classes for launching graphs and executing operations.\r\n\r\nThe [basic usage](../../get_started/introduction.md#basic-usage) guide has\r\nexamples of how a graph is launched in a [`tf.Session`](#Session).\r\n\r\n## Session management <a class=\"md-anchor\" id=\"AUTOGENERATED-session-management\"></a>\r\n\r\n- - -\r\n\r\n### `class tf.Session` <a class=\"md-anchor\" id=\"Session\"></a>\r\n\r\nA class for running TensorFlow operations.\r\n\r\nA `Session` object encapsulates the environment in which `Operation`\r\nobjects are executed, and `Tensor` objects are evaluated. For\r\nexample:\r\n\r\n```python\r\n# Build a graph.\r\na = tf.constant(5.0)\r\nb = tf.constant(6.0)\r\nc = a * b\r\n\r\n# Launch the graph in a session.\r\nsess = tf.Session()\r\n\r\n# Evaluate the tensor `c`.\r\nprint sess.run(c)\r\n```\r\n\r\nA session may own resources, such as\r\n[variables](../../api_docs/python/state_ops.md#Variable), [queues](../../api_docs/python/io_ops.md#QueueBase),\r\nand [readers](../../api_docs/python/io_ops.md#ReaderBase). It is important to release\r\nthese resources when they are no longer required. To do this, either\r\ninvoke the [`close()`](#Session.close) method on the session, or use\r\nthe session as a context manager. The following two examples are\r\nequivalent:\r\n\r\n```python\r\n# Using the `close()` method.\r\nsess = tf.Session()\r\nsess.run(...)\r\nsess.close()\r\n\r\n# Using the context manager.\r\nwith tf.Session() as sess:\r\n  sess.run(...)\r\n```\r\n\r\nThe [`ConfigProto`]\r\n(https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/config.proto)\r\nprotocol buffer exposes various configuration options for a\r\nsession. For example, to create a session that uses soft constraints\r\nfor device placement, and log the resulting placement decisions,\r\ncreate a session as follows:\r\n\r\n```python\r\n# Launch the graph in a session that allows soft device placement and\r\n# logs the placement decisions.\r\nsess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,\r\n                                        log_device_placement=True))\r\n```\r\n\r\n- - -\r\n\r\n#### `tf.Session.__init__(target='', graph=None, config=None)` <a class=\"md-anchor\" id=\"Session.__init__\"></a>\r\n\r\nCreates a new TensorFlow session.\r\n\r\nIf no `graph` argument is specified when constructing the session,\r\nthe default graph will be launched in the session. If you are\r\nusing more than one graph (created with `tf.Graph()` in the same\r\nprocess, you will have to use different sessions for each graph,\r\nbut each graph can be used in multiple sessions. In this case, it\r\nis often clearer to pass the graph to be launched explicitly to\r\nthe session constructor.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`target`</b>: (Optional.) The execution engine to connect to.\r\n    Defaults to using an in-process engine. At present, no value\r\n    other than the empty string is supported.\r\n*  <b>`graph`</b>: (Optional.) The `Graph` to be launched (described above).\r\n*  <b>`config`</b>: (Optional.) A [`ConfigProto`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/config.proto)\r\n    protocol buffer with configuration options for the session.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Session.run(fetches, feed_dict=None)` <a class=\"md-anchor\" id=\"Session.run\"></a>\r\n\r\nRuns the operations and evaluates the tensors in `fetches`.\r\n\r\nThis method runs one \"step\" of TensorFlow computation, by\r\nrunning the necessary graph fragment to execute every `Operation`\r\nand evaluate every `Tensor` in `fetches`, substituting the values in\r\n`feed_dict` for the corresponding input values.\r\n\r\nThe `fetches` argument may be a list of graph elements or a single\r\ngraph element, and these determine the return value of this\r\nmethod. A graph element can be one of the following types:\r\n\r\n* If the *i*th element of `fetches` is an\r\n  [`Operation`](../../api_docs/python/framework.md#Operation), the *i*th\r\n  return value will be `None`.\r\n* If the *i*th element of `fetches` is a\r\n  [`Tensor`](../../api_docs/python/framework.md#Tensor), the *i*th return\r\n  value will be a numpy ndarray containing the value of that tensor.\r\n* If the *i*th element of `fetches` is a\r\n  [`SparseTensor`](../../api_docs/python/sparse_ops.md#SparseTensor),\r\n  the *i*th return value will be a\r\n  [`SparseTensorValue`](../../api_docs/python/sparse_ops.md#SparseTensorValue)\r\n  containing the value of that sparse tensor.\r\n\r\nThe optional `feed_dict` argument allows the caller to override\r\nthe value of tensors in the graph. Each key in `feed_dict` can be\r\none of the following types:\r\n\r\n* If the key is a [`Tensor`](../../api_docs/python/framework.md#Tensor), the\r\n  value may be a Python scalar, string, list, or numpy ndarray\r\n  that can be converted to the same `dtype` as that\r\n  tensor. Additionally, if the key is a\r\n  [placeholder](../../api_docs/python/io_ops.md#placeholder), the shape of\r\n  the value will be checked for compatibility with the placeholder.\r\n* If the key is a\r\n  [`SparseTensor`](../../api_docs/python/sparse_ops.md#SparseTensor),\r\n  the value should be a\r\n  [`SparseTensorValue`](../../api_docs/python/sparse_ops.md#SparseTensorValue).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`fetches`</b>: A single graph element, or a list of graph elements\r\n    (described above).\r\n*  <b>`feed_dict`</b>: A dictionary that maps graph elements to values\r\n    (described above).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  Either a single value if `fetches` is a single graph element, or\r\n  a list of values if `fetches` is a list (described above).\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`RuntimeError`</b>: If this `Session` is in an invalid state (e.g. has been\r\n    closed).\r\n*  <b>`TypeError`</b>: If `fetches` or `feed_dict` keys are of an inappropriate type.\r\n*  <b>`ValueError`</b>: If `fetches` or `feed_dict` keys are invalid or refer to a\r\n    `Tensor` that doesn't exist.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Session.close()` <a class=\"md-anchor\" id=\"Session.close\"></a>\r\n\r\nCloses this session.\r\n\r\nCalling this method frees all resources associated with the session.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`RuntimeError`</b>: If an error occurs while closing the session.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Session.graph` <a class=\"md-anchor\" id=\"Session.graph\"></a>\r\n\r\nThe graph that was launched in this session.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Session.as_default()` <a class=\"md-anchor\" id=\"Session.as_default\"></a>\r\n\r\nReturns a context manager that makes this object the default session.\r\n\r\nUse with the `with` keyword to specify that calls to\r\n[`Operation.run()`](../../api_docs/python/framework.md#Operation.run) or\r\n[`Tensor.run()`](../../api_docs/python/framework.md#Tensor.run) should be\r\nexecuted in this session.\r\n\r\n```python\r\nc = tf.constant(..)\r\nsess = tf.Session()\r\n\r\nwith sess.as_default():\r\n  assert tf.get_default_session() is sess\r\n  print c.eval()\r\n```\r\n\r\nTo get the current default session, use\r\n[`tf.get_default_session()`](#get_default_session).\r\n\r\n\r\n*N.B.* The `as_default` context manager *does not* close the\r\nsession when you exit the context, and you must close the session\r\nexplicitly.\r\n\r\n```python\r\nc = tf.constant(...)\r\nsess = tf.Session()\r\nwith sess.as_default():\r\n  print c.eval()\r\n# ...\r\nwith sess.as_default():\r\n  print c.eval()\r\n\r\nsess.close()\r\n```\r\n\r\nAlternatively, you can use `with tf.Session():` to create a\r\nsession that is automatically closed on exiting the context,\r\nincluding when an uncaught exception is raised.\r\n\r\n*N.B.* The default graph is a property of the current thread. If you\r\ncreate a new thread, and wish to use the default session in that\r\nthread, you must explicitly add a `with sess.as_default():` in that\r\nthread's function.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A context manager using this session as the default session.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.InteractiveSession` <a class=\"md-anchor\" id=\"InteractiveSession\"></a>\r\n\r\nA TensorFlow `Session` for use in interactive contexts, such as a shell.\r\n\r\nThe only difference with a regular `Session` is that an `InteractiveSession`\r\ninstalls itself as the default session on construction.\r\nThe methods [`Tensor.eval()`](../../api_docs/python/framework.md#Tensor.eval)\r\nand [`Operation.run()`](../../api_docs/python/framework.md#Operation.run)\r\nwill use that session to run ops.\r\n\r\nThis is convenient in interactive shells and [IPython\r\nnotebooks](http://ipython.org), as it avoids having to pass an explicit\r\n`Session` object to run ops.\r\n\r\nFor example:\r\n\r\n```python\r\nsess = tf.InteractiveSession()\r\na = tf.constant(5.0)\r\nb = tf.constant(6.0)\r\nc = a * b\r\n# We can just use 'c.eval()' without passing 'sess'\r\nprint c.eval()\r\nsess.close()\r\n```\r\n\r\nNote that a regular session installs itself as the default session when it\r\nis created in a `with` statement.  The common usage in non-interactive\r\nprograms is to follow that pattern:\r\n\r\n```python\r\na = tf.constant(5.0)\r\nb = tf.constant(6.0)\r\nc = a * b\r\nwith tf.Session():\r\n  # We can also use 'c.eval()' here.\r\n  print c.eval()\r\n```\r\n\r\n- - -\r\n\r\n#### `tf.InteractiveSession.__init__(target='', graph=None)` <a class=\"md-anchor\" id=\"InteractiveSession.__init__\"></a>\r\n\r\nCreates a new interactive TensorFlow session.\r\n\r\nIf no `graph` argument is specified when constructing the session,\r\nthe default graph will be launched in the session. If you are\r\nusing more than one graph (created with `tf.Graph()` in the same\r\nprocess, you will have to use different sessions for each graph,\r\nbut each graph can be used in multiple sessions. In this case, it\r\nis often clearer to pass the graph to be launched explicitly to\r\nthe session constructor.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`target`</b>: (Optional.) The execution engine to connect to.\r\n    Defaults to using an in-process engine. At present, no value\r\n    other than the empty string is supported.\r\n*  <b>`graph`</b>: (Optional.) The `Graph` to be launched (described above).\r\n\r\n\r\n- - -\r\n\r\n#### `tf.InteractiveSession.close()` <a class=\"md-anchor\" id=\"InteractiveSession.close\"></a>\r\n\r\nCloses an `InteractiveSession`.\r\n\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.get_default_session()` <a class=\"md-anchor\" id=\"get_default_session\"></a>\r\n\r\nReturns the default session for the current thread.\r\n\r\nThe returned `Session` will be the innermost session on which a\r\n`Session` or `Session.as_default()` context has been entered.\r\n\r\n*N.B.* The default session is a property of the current thread. If you\r\ncreate a new thread, and wish to use the default session in that\r\nthread, you must explicitly add a `with sess.as_default():` in that\r\nthread's function.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The default `Session` being used in the current thread.\r\n\r\n\r\n\r\n## Error classes <a class=\"md-anchor\" id=\"AUTOGENERATED-error-classes\"></a>\r\n\r\n- - -\r\n\r\n### `class tf.OpError` <a class=\"md-anchor\" id=\"OpError\"></a>\r\n\r\nA generic error that is raised when TensorFlow execution fails.\r\n\r\nWhenever possible, the session will raise a more specific subclass\r\nof `OpError` from the `tf.errors` module.\r\n\r\n- - -\r\n\r\n#### `tf.OpError.op` <a class=\"md-anchor\" id=\"OpError.op\"></a>\r\n\r\nThe operation that failed, if known.\r\n\r\n*N.B.* If the failed op was synthesized at runtime, e.g. a `Send`\r\nor `Recv` op, there will be no corresponding\r\n[`Operation`](../../api_docs/python/framework.md#Operation) object.  In that case, this\r\nwill return `None`, and you should instead use the\r\n[`OpError.node_def`](#OpError.node_def) to discover information about the\r\nop.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The `Operation` that failed, or None.\r\n\r\n- - -\r\n\r\n#### `tf.OpError.node_def` <a class=\"md-anchor\" id=\"OpError.node_def\"></a>\r\n\r\nThe `NodeDef` proto representing the op that failed.\r\n\r\n\r\n#### Other Methods <a class=\"md-anchor\" id=\"AUTOGENERATED-other-methods\"></a>\r\n- - -\r\n\r\n#### `tf.OpError.__init__(node_def, op, message, error_code)` <a class=\"md-anchor\" id=\"OpError.__init__\"></a>\r\n\r\nCreates a new OpError indicating that a particular op failed.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`node_def`</b>: The graph_pb2.NodeDef proto representing the op that failed.\r\n*  <b>`op`</b>: The ops.Operation that failed, if known; otherwise None.\r\n*  <b>`message`</b>: The message string describing the failure.\r\n*  <b>`error_code`</b>: The error_codes_pb2.Code describing the error.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.OpError.error_code` <a class=\"md-anchor\" id=\"OpError.error_code\"></a>\r\n\r\nThe integer error code that describes the error.\r\n\r\n- - -\r\n\r\n#### `tf.OpError.message` <a class=\"md-anchor\" id=\"OpError.message\"></a>\r\n\r\nThe error message that describes the error.\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.CancelledError` <a class=\"md-anchor\" id=\"CancelledError\"></a>\r\n\r\nRaised when an operation or step is cancelled.\r\n\r\nFor example, a long-running operation (e.g.\r\n[`queue.enqueue()`](../../api_docs/python/io_ops.md#QueueBase.enqueue) may be\r\ncancelled by running another operation (e.g.\r\n[`queue.close(cancel_pending_enqueues=True)`](../../api_docs/python/io_ops.md#QueueBase.close),\r\nor by [closing the session](../../api_docs/python/client.md#Session.close).\r\nA step that is running such a long-running operation will fail by raising\r\n`CancelledError`.\r\n\r\n- - -\r\n\r\n#### `tf.errors.CancelledError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"CancelledError.__init__\"></a>\r\n\r\nCreates a `CancelledError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.UnknownError` <a class=\"md-anchor\" id=\"UnknownError\"></a>\r\n\r\nUnknown error.\r\n\r\nAn example of where this error may be returned is if a Status value\r\nreceived from another address space belongs to an error-space that\r\nis not known to this address space. Also errors raised by APIs that\r\ndo not return enough error information may be converted to this\r\nerror.\r\n\r\n- - -\r\n\r\n#### `tf.errors.UnknownError.__init__(node_def, op, message, error_code=2)` <a class=\"md-anchor\" id=\"UnknownError.__init__\"></a>\r\n\r\nCreates an `UnknownError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.InvalidArgumentError` <a class=\"md-anchor\" id=\"InvalidArgumentError\"></a>\r\n\r\nRaised when an operation receives an invalid argument.\r\n\r\nThis may occur, for example, if an operation is receives an input\r\ntensor that has an invalid value or shape. For example, the\r\n[`tf.matmul()`](../../api_docs/python/math_ops.md#matmul) op will raise this\r\nerror if it receives an input that is not a matrix, and the\r\n[`tf.reshape()`](../../api_docs/python/array_ops.md#reshape) op will raise\r\nthis error if the new shape does not match the number of elements in the input\r\ntensor.\r\n\r\n- - -\r\n\r\n#### `tf.errors.InvalidArgumentError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"InvalidArgumentError.__init__\"></a>\r\n\r\nCreates an `InvalidArgumentError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.DeadlineExceededError` <a class=\"md-anchor\" id=\"DeadlineExceededError\"></a>\r\n\r\nRaised when a deadline expires before an operation could complete.\r\n\r\nThis exception is not currently used.\r\n\r\n- - -\r\n\r\n#### `tf.errors.DeadlineExceededError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"DeadlineExceededError.__init__\"></a>\r\n\r\nCreates a `DeadlineExceededError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.NotFoundError` <a class=\"md-anchor\" id=\"NotFoundError\"></a>\r\n\r\nRaised when a requested entity (e.g., a file or directory) was not found.\r\n\r\nFor example, running the\r\n[`tf.WholeFileReader.read()`](../../api_docs/python/io_ops.md#WholeFileReader)\r\noperation could raise `NotFoundError` if it receives the name of a file that\r\ndoes not exist.\r\n\r\n- - -\r\n\r\n#### `tf.errors.NotFoundError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"NotFoundError.__init__\"></a>\r\n\r\nCreates a `NotFoundError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.AlreadyExistsError` <a class=\"md-anchor\" id=\"AlreadyExistsError\"></a>\r\n\r\nRaised when an entity that we attempted to create already exists.\r\n\r\nFor example, running an operation that saves a file\r\n(e.g. [`tf.train.Saver.save()`](../../api_docs/python/train.md#Saver.save))\r\ncould potentially raise this exception if an explicit filename for an\r\nexisting file was passed.\r\n\r\n- - -\r\n\r\n#### `tf.errors.AlreadyExistsError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"AlreadyExistsError.__init__\"></a>\r\n\r\nCreates an `AlreadyExistsError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.PermissionDeniedError` <a class=\"md-anchor\" id=\"PermissionDeniedError\"></a>\r\n\r\nRaised when the caller does not have permission to run an operation.\r\n\r\nFor example, running the\r\n[`tf.WholeFileReader.read()`](../../api_docs/python/io_ops.md#WholeFileReader)\r\noperation could raise `PermissionDeniedError` if it receives the name of a\r\nfile for which the user does not have the read file permission.\r\n\r\n- - -\r\n\r\n#### `tf.errors.PermissionDeniedError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"PermissionDeniedError.__init__\"></a>\r\n\r\nCreates a `PermissionDeniedError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.UnauthenticatedError` <a class=\"md-anchor\" id=\"UnauthenticatedError\"></a>\r\n\r\nThe request does not have valid authentication credentials.\r\n\r\nThis exception is not currently used.\r\n\r\n- - -\r\n\r\n#### `tf.errors.UnauthenticatedError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"UnauthenticatedError.__init__\"></a>\r\n\r\nCreates an `UnauthenticatedError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.ResourceExhaustedError` <a class=\"md-anchor\" id=\"ResourceExhaustedError\"></a>\r\n\r\nSome resource has been exhausted.\r\n\r\nFor example, this error might be raised if a per-user quota is\r\nexhausted, or perhaps the entire file system is out of space.\r\n\r\n- - -\r\n\r\n#### `tf.errors.ResourceExhaustedError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"ResourceExhaustedError.__init__\"></a>\r\n\r\nCreates a `ResourceExhaustedError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.FailedPreconditionError` <a class=\"md-anchor\" id=\"FailedPreconditionError\"></a>\r\n\r\nOperation was rejected because the system is not in a state to execute it.\r\n\r\nThis exception is most commonly raised when running an operation\r\nthat reads a [`tf.Variable`](../../api_docs/python/state_ops.md#Variable)\r\nbefore it has been initialized.\r\n\r\n- - -\r\n\r\n#### `tf.errors.FailedPreconditionError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"FailedPreconditionError.__init__\"></a>\r\n\r\nCreates a `FailedPreconditionError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.AbortedError` <a class=\"md-anchor\" id=\"AbortedError\"></a>\r\n\r\nThe operation was aborted, typically due to a concurrent action.\r\n\r\nFor example, running a\r\n[`queue.enqueue()`](../../api_docs/python/io_ops.md#QueueBase.enqueue)\r\noperation may raise `AbortedError` if a\r\n[`queue.close()`](../../api_docs/python/io_ops.md#QueueBase.close) operation\r\npreviously ran.\r\n\r\n- - -\r\n\r\n#### `tf.errors.AbortedError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"AbortedError.__init__\"></a>\r\n\r\nCreates an `AbortedError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.OutOfRangeError` <a class=\"md-anchor\" id=\"OutOfRangeError\"></a>\r\n\r\nRaised when an operation executed past the valid range.\r\n\r\nThis exception is raised in \"end-of-file\" conditions, such as when a\r\n[`queue.dequeue()`](../../api_docs/python/io_ops.md#QueueBase.dequeue)\r\noperation is blocked on an empty queue, and a\r\n[`queue.close()`](../../api_docs/python/io_ops.md#QueueBase.close)\r\noperation executes.\r\n\r\n- - -\r\n\r\n#### `tf.errors.OutOfRangeError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"OutOfRangeError.__init__\"></a>\r\n\r\nCreates an `OutOfRangeError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.UnimplementedError` <a class=\"md-anchor\" id=\"UnimplementedError\"></a>\r\n\r\nRaised when an operation has not been implemented.\r\n\r\nSome operations may raise this error when passed otherwise-valid\r\narguments that it does not currently support. For example, running\r\nthe [`tf.nn.max_pool()`](../../api_docs/python/nn.md#max_pool) operation\r\nwould raise this error if pooling was requested on the batch dimension,\r\nbecause this is not yet supported.\r\n\r\n- - -\r\n\r\n#### `tf.errors.UnimplementedError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"UnimplementedError.__init__\"></a>\r\n\r\nCreates an `UnimplementedError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.InternalError` <a class=\"md-anchor\" id=\"InternalError\"></a>\r\n\r\nRaised when the system experiences an internal error.\r\n\r\nThis exception is raised when some invariant expected by the runtime\r\nhas been broken. Catching this exception is not recommended.\r\n\r\n- - -\r\n\r\n#### `tf.errors.InternalError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"InternalError.__init__\"></a>\r\n\r\nCreates an `InternalError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.UnavailableError` <a class=\"md-anchor\" id=\"UnavailableError\"></a>\r\n\r\nRaised when the runtime is currently unavailable.\r\n\r\nThis exception is not currently used.\r\n\r\n- - -\r\n\r\n#### `tf.errors.UnavailableError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"UnavailableError.__init__\"></a>\r\n\r\nCreates an `UnavailableError`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.errors.DataLossError` <a class=\"md-anchor\" id=\"DataLossError\"></a>\r\n\r\nRaised when unrecoverable data loss or corruption is encountered.\r\n\r\nFor example, this may be raised by running a\r\n[`tf.WholeFileReader.read()`](../../api_docs/python/io_ops.md#WholeFileReader)\r\noperation, if the file is truncated while it is being read.\r\n\r\n- - -\r\n\r\n#### `tf.errors.DataLossError.__init__(node_def, op, message)` <a class=\"md-anchor\" id=\"DataLossError.__init__\"></a>\r\n\r\nCreates a `DataLossError`.\r\n\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/python/constant_op.md",
    "content": "<!-- This file is machine generated: DO NOT EDIT! -->\r\n\r\n# Constants, Sequences, and Random Values <a class=\"md-anchor\" id=\"AUTOGENERATED-constants--sequences--and-random-values\"></a>\r\n\r\nNote: Functions taking `Tensor` arguments can also take anything accepted by\r\n[`tf.convert_to_tensor`](../../api_docs/python/framework.md#convert_to_tensor).\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Constants, Sequences, and Random Values](#AUTOGENERATED-constants--sequences--and-random-values)\r\n* [Constant Value Tensors](#AUTOGENERATED-constant-value-tensors)\r\n  * [`tf.zeros(shape, dtype=tf.float32, name=None)`](#zeros)\r\n  * [`tf.zeros_like(tensor, dtype=None, name=None)`](#zeros_like)\r\n  * [`tf.ones(shape, dtype=tf.float32, name=None)`](#ones)\r\n  * [`tf.ones_like(tensor, dtype=None, name=None)`](#ones_like)\r\n  * [`tf.fill(dims, value, name=None)`](#fill)\r\n  * [`tf.constant(value, dtype=None, shape=None, name='Const')`](#constant)\r\n* [Sequences](#AUTOGENERATED-sequences)\r\n  * [`tf.linspace(start, stop, num, name=None)`](#linspace)\r\n  * [`tf.range(start, limit, delta=1, name='range')`](#range)\r\n* [Random Tensors](#AUTOGENERATED-random-tensors)\r\n  * [Examples:](#AUTOGENERATED-examples-)\r\n  * [`tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)`](#random_normal)\r\n  * [`tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)`](#truncated_normal)\r\n  * [`tf.random_uniform(shape, minval=0.0, maxval=1.0, dtype=tf.float32, seed=None, name=None)`](#random_uniform)\r\n  * [`tf.random_shuffle(value, seed=None, name=None)`](#random_shuffle)\r\n  * [`tf.set_random_seed(seed)`](#set_random_seed)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## Constant Value Tensors <a class=\"md-anchor\" id=\"AUTOGENERATED-constant-value-tensors\"></a>\r\n\r\nTensorFlow provides several operations that you can use to generate constants.\r\n\r\n- - -\r\n\r\n### `tf.zeros(shape, dtype=tf.float32, name=None)` <a class=\"md-anchor\" id=\"zeros\"></a>\r\n\r\nCreates a tensor with all elements set to zero.\r\n\r\nThis operation returns a tensor of type `dtype` with shape `shape` and\r\nall elements set to zero.\r\n\r\nFor example:\r\n\r\n```python\r\ntf.zeros([3, 4], int32) ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`shape`</b>: Either a list of integers, or a 1-D `Tensor` of type `int32`.\r\n*  <b>`dtype`</b>: The type of an element in the resulting `Tensor`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` with all elements set to zero.\r\n\r\n\r\n- - -\r\n\r\n### `tf.zeros_like(tensor, dtype=None, name=None)` <a class=\"md-anchor\" id=\"zeros_like\"></a>\r\n\r\nCreates a tensor with all elements set to zero.\r\n\r\nGiven a single tensor (`tensor`), this operation returns a tensor of the\r\nsame type and shape as `tensor` with all elements set to zero. Optionally,\r\nyou can use `dtype` to specify a new type for the returned tensor.\r\n\r\nFor example:\r\n\r\n```python\r\n# 'tensor' is [[1, 2, 3], [4, 5, 6]]\r\ntf.zeros_like(tensor) ==> [[0, 0, 0], [0, 0, 0]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tensor`</b>: A `Tensor`.\r\n*  <b>`dtype`</b>: A type for the returned `Tensor`. Must be `float32`, `float64`,\r\n  `int8`, `int16`, `int32`, `int64`, `uint8`, or `complex64`.\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` with all elements set to zero.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.ones(shape, dtype=tf.float32, name=None)` <a class=\"md-anchor\" id=\"ones\"></a>\r\n\r\nCreates a tensor with all elements set to 1.\r\n\r\nThis operation returns a tensor of type `dtype` with shape `shape` and all\r\nelements set to 1.\r\n\r\nFor example:\r\n\r\n```python\r\ntf.ones([2, 3], int32) ==> [[1, 1, 1], [1, 1, 1]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`shape`</b>: Either a list of integers, or a 1-D `Tensor` of type `int32`.\r\n*  <b>`dtype`</b>: The type of an element in the resulting `Tensor`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` with all elements set to 1.\r\n\r\n\r\n- - -\r\n\r\n### `tf.ones_like(tensor, dtype=None, name=None)` <a class=\"md-anchor\" id=\"ones_like\"></a>\r\n\r\nCreates a tensor with all elements set to 1.\r\n\r\nGiven a single tensor (`tensor`), this operation returns a tensor of the same\r\ntype and shape as `tensor` with all elements set to 1. Optionally, you can\r\nspecify a new type (`dtype`) for the returned tensor.\r\n\r\nFor example:\r\n\r\n```python\r\n# 'tensor' is [[1, 2, 3], [4, 5, 6]]\r\ntf.ones_like(tensor) ==> [[1, 1, 1], [1, 1, 1]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tensor`</b>: A `Tensor`.\r\n*  <b>`dtype`</b>: A type for the returned `Tensor`. Must be `float32`, `float64`,\r\n  `int8`, `int16`, `int32`, `int64`, `uint8`, or `complex64`.\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` with all elements set to 1.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.fill(dims, value, name=None)` <a class=\"md-anchor\" id=\"fill\"></a>\r\n\r\nCreates a tensor filled with a scalar value.\r\n\r\nThis operation creates a tensor of shape `dims` and fills it with `value`.\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# output tensor shape needs to be [2, 3]\r\n# so 'dims' is [2, 3]\r\nfill(dims, 9) ==> [[9, 9, 9]\r\n                   [9, 9, 9]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`dims`</b>: A `Tensor` of type `int32`.\r\n    1-D. Represents the shape of the output tensor.\r\n*  <b>`value`</b>: A `Tensor`. 0-D (scalar). Value to fill the returned tensor.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `value`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.constant(value, dtype=None, shape=None, name='Const')` <a class=\"md-anchor\" id=\"constant\"></a>\r\n\r\nCreates a constant tensor.\r\n\r\n The resulting tensor is populated with values of type `dtype`, as\r\n specified by arguments `value` and (optionally) `shape` (see examples\r\n below).\r\n\r\n The argument `value` can be a constant value, or a list of values of type\r\n `dtype`. If `value` is a list, then the length of the list must be less\r\n than or equal to the number of elements implied by the `shape` argument (if\r\n specified). In the case where the list length is less than the number of\r\n elements specified by `shape`, the last element in the list will be used\r\n to fill the remaining entries.\r\n\r\n The argument `shape` is optional. If present, it specifies the dimensions\r\n of the resulting tensor. If not present, then the tensor is a scalar (0-D)\r\n if `value` is a scalar, or 1-D otherwise.\r\n\r\n If the argument `dtype` is not specified, then the type is inferred from\r\n the type of `value`.\r\n\r\n For example:\r\n\r\n ```python\r\n # Constant 1-D Tensor populated with value list.\r\n tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]\r\n\r\n # Constant 2-D tensor populated with scalar value -1.\r\n tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.]\r\n                                              [-1. -1. -1.]]\r\n ```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`value`</b>: A constant value (or list) of output type `dtype`.\r\n\r\n\r\n*  <b>`dtype`</b>: The type of the elements of the resulting tensor.\r\n\r\n\r\n*  <b>`shape`</b>: Optional dimensions of resulting tensor.\r\n\r\n\r\n*  <b>`name`</b>: Optional name for the tensor.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A Constant Tensor.\r\n\r\n\r\n\r\n## Sequences <a class=\"md-anchor\" id=\"AUTOGENERATED-sequences\"></a>\r\n\r\n- - -\r\n\r\n### `tf.linspace(start, stop, num, name=None)` <a class=\"md-anchor\" id=\"linspace\"></a>\r\n\r\nGenerates values in an interval.\r\n\r\nA sequence of `num` evenly-spaced values are generated beginning at `start`.\r\nIf `num > 1`, the values in the sequence increase by `stop - start / num - 1`,\r\nso that the last one is exactly `stop`.\r\n\r\nFor example:\r\n\r\n```\r\ntf.linspace(10.0, 12.0, 3, name=\"linspace\") => [ 10.0  11.0  12.0]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`start`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.\r\n    First entry in the range.\r\n*  <b>`stop`</b>: A `Tensor`. Must have the same type as `start`.\r\n    Last entry in the range.\r\n*  <b>`num`</b>: A `Tensor` of type `int32`. Number of values to generate.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `start`. 1-D. The generated values.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.range(start, limit, delta=1, name='range')` <a class=\"md-anchor\" id=\"range\"></a>\r\n\r\nCreates a sequence of integers.\r\n\r\nThis operation creates a sequence of integers that begins at `start` and\r\nextends by increments of `delta` up to but not including `limit`.\r\n\r\nFor example:\r\n\r\n```\r\n# 'start' is 3\r\n# 'limit' is 18\r\n# 'delta' is 3\r\ntf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`start`</b>: A 0-D (scalar) of type `int32`. First entry in sequence.\r\n*  <b>`limit`</b>: A 0-D (scalar) of type `int32`. Upper limit of sequence,\r\n    exclusive.\r\n*  <b>`delta`</b>: A 0-D `Tensor` (scalar) of type `int32`. Optional. Default is 1.\r\n    Number that increments `start`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An 1-D `int32` `Tensor`.\r\n\r\n\r\n\r\n## Random Tensors <a class=\"md-anchor\" id=\"AUTOGENERATED-random-tensors\"></a>\r\n\r\nTensorFlow has several ops that create random tensors with different\r\ndistributions.  The random ops are stateful, and create new random values each\r\ntime they are evaluated.\r\n\r\nThe `seed` keyword argument in these functions acts in conjunction with\r\nthe graph-level random seed. Changing either the graph-level seed using\r\n[`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed) or the\r\nop-level seed will change the underlying seed of these operations. Setting\r\nneither graph-level nor op-level seed, results in a random seed for all\r\noperations.\r\nSee [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\nfor details on the interaction between operation-level and graph-level random\r\nseeds.\r\n\r\n### Examples: <a class=\"md-anchor\" id=\"AUTOGENERATED-examples-\"></a>\r\n\r\n```python\r\n# Create a tensor of shape [2, 3] consisting of random normal values, with mean\r\n# -1 and standard deviation 4.\r\nnorm = tf.random_normal([2, 3], mean=-1, stddev=4)\r\n\r\n# Shuffle the first dimension of a tensor\r\nc = tf.constant([[1, 2], [3, 4], [5, 6]])\r\nshuff = tf.random_shuffle(c)\r\n\r\n# Each time we run these ops, different results are generated\r\nsess = tf.Session()\r\nprint sess.run(norm)\r\nprint sess.run(norm)\r\n\r\n# Set an op-level seed to generate repeatable sequences across sessions.\r\nc = tf.constant([[1, 2], [3, 4], [5, 6]])\r\nsess = tf.Session()\r\nnorm = tf.random_normal(c, seed=1234)\r\nprint sess.run(norm)\r\nprint sess.run(norm)\r\n```\r\n\r\nAnother common use of random values is the intialization of variables. Also see\r\nthe [Variables How To](../../how_tos/variables/index.md).\r\n\r\n```python\r\n# Use random uniform values in [0, 1) as the initializer for a variable of shape\r\n# [2, 3]. The default type is float32.\r\nvar = tf.Variable(tf.random_uniform([2, 3]), name=\"var\")\r\ninit = tf.initialize_all_variables()\r\n\r\nsess = tf.Session()\r\nsess.run(init)\r\nprint sess.run(var)\r\n```\r\n\r\n- - -\r\n\r\n### `tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)` <a class=\"md-anchor\" id=\"random_normal\"></a>\r\n\r\nOutputs random values from a normal distribution.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`shape`</b>: A 1-D integer Tensor or Python array. The shape of the output tensor.\r\n*  <b>`mean`</b>: A 0-D Tensor or Python value of type `dtype`. The mean of the normal\r\n    distribution.\r\n*  <b>`stddev`</b>: A 0-D Tensor or Python value of type `dtype`. The standard deviation\r\n    of the normal distribution.\r\n*  <b>`dtype`</b>: The type of the output.\r\n*  <b>`seed`</b>: A Python integer. Used to create a random seed for the distribution.\r\n    See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tensor of the specified shape filled with random normal values.\r\n\r\n\r\n- - -\r\n\r\n### `tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)` <a class=\"md-anchor\" id=\"truncated_normal\"></a>\r\n\r\nOutputs random values from a truncated normal distribution.\r\n\r\nThe generated values follow a normal distribution with specified mean and\r\nstandard deviation, except that values whose magnitude is more than 2 standard\r\ndeviations from the mean are dropped and re-picked.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`shape`</b>: A 1-D integer Tensor or Python array. The shape of the output tensor.\r\n*  <b>`mean`</b>: A 0-D Tensor or Python value of type `dtype`. The mean of the\r\n    truncated normal distribution.\r\n*  <b>`stddev`</b>: A 0-D Tensor or Python value of type `dtype`. The standard deviation\r\n    of the truncated normal distribution.\r\n*  <b>`dtype`</b>: The type of the output.\r\n*  <b>`seed`</b>: A Python integer. Used to create a random seed for the distribution.\r\n    See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tensor of the specified shape filled with random truncated normal values.\r\n\r\n\r\n- - -\r\n\r\n### `tf.random_uniform(shape, minval=0.0, maxval=1.0, dtype=tf.float32, seed=None, name=None)` <a class=\"md-anchor\" id=\"random_uniform\"></a>\r\n\r\nOutputs random values from a uniform distribution.\r\n\r\nThe generated values follow a uniform distribution in the range\r\n`[minval, maxval)`. The lower bound `minval` is included in the range, while\r\nthe upper bound `maxval` is excluded.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`shape`</b>: A 1-D integer Tensor or Python array. The shape of the output tensor.\r\n*  <b>`minval`</b>: A 0-D Tensor or Python value of type `dtype`. The lower bound on the\r\n    range of random values to generate.\r\n*  <b>`maxval`</b>: A 0-D Tensor or Python value of type `dtype`. The upper bound on\r\n    the range of random values to generate.\r\n*  <b>`dtype`</b>: The type of the output.\r\n*  <b>`seed`</b>: A Python integer. Used to create a random seed for the distribution.\r\n    See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tensor of the specified shape filled with random uniform values.\r\n\r\n\r\n- - -\r\n\r\n### `tf.random_shuffle(value, seed=None, name=None)` <a class=\"md-anchor\" id=\"random_shuffle\"></a>\r\n\r\nRandomly shuffles a tensor along its first dimension.\r\n\r\nThe tensor is shuffled along dimension 0, such that each `value[j]` is mapped\r\nto one and only one `output[i]`. For example, a mapping that might occur for a\r\n3x2 tensor is:\r\n\r\n```python\r\n[[1, 2],       [[5, 6],\r\n [3, 4],  ==>   [1, 2],\r\n [5, 6]]        [3, 4]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`value`</b>: A Tensor to be shuffled.\r\n*  <b>`seed`</b>: A Python integer. Used to create a random seed for the distribution.\r\n    See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tensor of same shape and type as `value`, shuffled along its first\r\n  dimension.\r\n\r\n\r\n- - -\r\n\r\n### `tf.set_random_seed(seed)` <a class=\"md-anchor\" id=\"set_random_seed\"></a>\r\n\r\nSets the graph-level random seed.\r\n\r\nOperations that rely on a random seed actually derive it from two seeds:\r\nthe graph-level and operation-level seeds. This sets the graph-level seed.\r\n\r\nIts interactions with operation-level seeds is as follows:\r\n\r\n  1. If neither the graph-level nor the operation seed is set:\r\n    A random seed is used for this op.\r\n  2. If the graph-level seed is set, but the operation seed is not:\r\n    The system deterministically picks an operation seed in conjunction\r\n    with the graph-level seed so that it gets a unique random sequence.\r\n  3. If the graph-level seed is not set, but the operation seed is set:\r\n    A default graph-level seed and the specified operation seed are used to\r\n    determine the random sequence.\r\n  4. If both the graph-level and the operation seed are set:\r\n    Both seeds are used in conjunction to determine the random sequence.\r\n\r\nTo illustrate the user-visible effects, consider these examples:\r\n\r\nTo generate different sequences across sessions, set neither\r\ngraph-level nor op-level seeds:\r\n\r\n```python\r\na = tf.random_uniform([1])\r\nb = tf.random_normal([1])\r\n\r\nprint \"Session 1\"\r\nwith tf.Session() as sess1:\r\n  print sess1.run(a)  # generates 'A1'\r\n  print sess1.run(a)  # generates 'A2'\r\n  print sess1.run(b)  # generates 'B1'\r\n  print sess1.run(b)  # generates 'B2'\r\n\r\nprint \"Session 2\"\r\nwith tf.Session() as sess2:\r\n  print sess2.run(a)  # generates 'A3'\r\n  print sess2.run(a)  # generates 'A4'\r\n  print sess2.run(b)  # generates 'B3'\r\n  print sess2.run(b)  # generates 'B4'\r\n```\r\n\r\nTo generate the same repeatable sequence for an op across sessions, set the\r\nseed for the op:\r\n\r\n```python\r\na = tf.random_uniform([1], seed=1)\r\nb = tf.random_normal([1])\r\n\r\n# Repeatedly running this block with the same graph will generate the same\r\n# sequence of values for 'a', but different sequences of values for 'b'.\r\nprint \"Session 1\"\r\nwith tf.Session() as sess1:\r\n  print sess1.run(a)  # generates 'A1'\r\n  print sess1.run(a)  # generates 'A2'\r\n  print sess1.run(b)  # generates 'B1'\r\n  print sess1.run(b)  # generates 'B2'\r\n\r\nprint \"Session 2\"\r\nwith tf.Session() as sess2:\r\n  print sess2.run(a)  # generates 'A1'\r\n  print sess2.run(a)  # generates 'A2'\r\n  print sess2.run(b)  # generates 'B3'\r\n  print sess2.run(b)  # generates 'B4'\r\n```\r\n\r\nTo make the random sequences generated by all ops be repeatable across\r\nsessions, set a graph-level seed:\r\n\r\n```python\r\ntf.set_random_seed(1234)\r\na = tf.random_uniform([1])\r\nb = tf.random_normal([1])\r\n\r\n# Repeatedly running this block with the same graph will generate different\r\n# sequences of 'a' and 'b'.\r\nprint \"Session 1\"\r\nwith tf.Session() as sess1:\r\n  print sess1.run(a)  # generates 'A1'\r\n  print sess1.run(a)  # generates 'A2'\r\n  print sess1.run(b)  # generates 'B1'\r\n  print sess1.run(b)  # generates 'B2'\r\n\r\nprint \"Session 2\"\r\nwith tf.Session() as sess2:\r\n  print sess2.run(a)  # generates 'A1'\r\n  print sess2.run(a)  # generates 'A2'\r\n  print sess2.run(b)  # generates 'B1'\r\n  print sess2.run(b)  # generates 'B2'\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`seed`</b>: integer.\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/python/control_flow_ops.md",
    "content": "<!-- This file is machine generated: DO NOT EDIT! -->\r\n\r\n# Control Flow <a class=\"md-anchor\" id=\"AUTOGENERATED-control-flow\"></a>\r\n\r\nNote: Functions taking `Tensor` arguments can also take anything accepted by\r\n[`tf.convert_to_tensor`](../../api_docs/python/framework.md#convert_to_tensor).\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Control Flow](#AUTOGENERATED-control-flow)\r\n* [Control Flow Operations](#AUTOGENERATED-control-flow-operations)\r\n  * [`tf.identity(input, name=None)`](#identity)\r\n  * [`tf.tuple(tensors, name=None, control_inputs=None)`](#tuple)\r\n  * [`tf.group(*inputs, **kwargs)`](#group)\r\n  * [`tf.no_op(name=None)`](#no_op)\r\n  * [`tf.count_up_to(ref, limit, name=None)`](#count_up_to)\r\n* [Logical Operators](#AUTOGENERATED-logical-operators)\r\n  * [`tf.logical_and(x, y, name=None)`](#logical_and)\r\n  * [`tf.logical_not(x, name=None)`](#logical_not)\r\n  * [`tf.logical_or(x, y, name=None)`](#logical_or)\r\n  * [`tf.logical_xor(x, y, name='LogicalXor')`](#logical_xor)\r\n* [Comparison Operators](#AUTOGENERATED-comparison-operators)\r\n  * [`tf.equal(x, y, name=None)`](#equal)\r\n  * [`tf.not_equal(x, y, name=None)`](#not_equal)\r\n  * [`tf.less(x, y, name=None)`](#less)\r\n  * [`tf.less_equal(x, y, name=None)`](#less_equal)\r\n  * [`tf.greater(x, y, name=None)`](#greater)\r\n  * [`tf.greater_equal(x, y, name=None)`](#greater_equal)\r\n  * [`tf.select(condition, t, e, name=None)`](#select)\r\n  * [`tf.where(input, name=None)`](#where)\r\n* [Debugging Operations](#AUTOGENERATED-debugging-operations)\r\n  * [`tf.is_finite(x, name=None)`](#is_finite)\r\n  * [`tf.is_inf(x, name=None)`](#is_inf)\r\n  * [`tf.is_nan(x, name=None)`](#is_nan)\r\n  * [`tf.verify_tensor_all_finite(t, msg, name=None)`](#verify_tensor_all_finite)\r\n  * [`tf.check_numerics(tensor, message, name=None)`](#check_numerics)\r\n  * [`tf.add_check_numerics_ops()`](#add_check_numerics_ops)\r\n  * [`tf.Assert(condition, data, summarize=None, name=None)`](#Assert)\r\n  * [`tf.Print(input_, data, message=None, first_n=None, summarize=None, name=None)`](#Print)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## Control Flow Operations <a class=\"md-anchor\" id=\"AUTOGENERATED-control-flow-operations\"></a>\r\n\r\nTensorFlow provides several operations and classes that you can use to control\r\nthe execution of operations and add conditional dependencies to your graph.\r\n\r\n- - -\r\n\r\n### `tf.identity(input, name=None)` <a class=\"md-anchor\" id=\"identity\"></a>\r\n\r\nReturn a tensor with the same shape and contents as the input tensor or value.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `input`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.tuple(tensors, name=None, control_inputs=None)` <a class=\"md-anchor\" id=\"tuple\"></a>\r\n\r\nGroup tensors together.\r\n\r\nThis creates a tuple of tensors with the same values as the `tensors`\r\nargument, except that the value of each tensor is only returned after the\r\nvalues of all tensors have been computed.\r\n\r\n`control_inputs` contains additional ops that have to finish before this op\r\nfinishes, but whose outputs are not returned.\r\n\r\nThis can be used as a \"join\" mechanism for parallel computations: all the\r\nargument tensors can be computed in parallel, but the values of any tensor\r\nreturned by `tuple` are only available after all the parallel computations\r\nare done.\r\n\r\nSee also `group` and `with_dependencies`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tensors`</b>: A list of `Tensor`s or `IndexedSlices`, some entries can be `None`.\r\n*  <b>`name`</b>: (optional) A name to use as a `name_scope` for the operation.\r\n*  <b>`control_inputs`</b>: List of additional ops to finish before returning.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  Same as `tensors`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `tensors` does not contain any `Tensor` or `IndexedSlices`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.group(*inputs, **kwargs)` <a class=\"md-anchor\" id=\"group\"></a>\r\n\r\nCreate an op that groups multiple operations.\r\n\r\nWhen this op finishes, all ops in `input` have finished. This op has no\r\noutput.\r\n\r\nSee also `tuple` and `with_dependencies`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`*inputs`</b>: One or more tensors to group.\r\n*  <b>`**kwargs`</b>: Optional parameters to pass when constructing the NodeDef.\r\n*  <b>`name`</b>: A name for this operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An Operation that executes all its inputs.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If an unknown keyword argument is provided, or if there are\r\n              no inputs.\r\n\r\n\r\n- - -\r\n\r\n### `tf.no_op(name=None)` <a class=\"md-anchor\" id=\"no_op\"></a>\r\n\r\nDoes nothing. Only useful as a placeholder for control edges.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The created Operation.\r\n\r\n\r\n- - -\r\n\r\n### `tf.count_up_to(ref, limit, name=None)` <a class=\"md-anchor\" id=\"count_up_to\"></a>\r\n\r\nIncrements 'ref' until it reaches 'limit'.\r\n\r\nThis operation outputs \"ref\" after the update is done.  This makes it\r\neasier to chain operations that need to use the updated value.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`ref`</b>: A mutable `Tensor`. Must be one of the following types: `int32`, `int64`.\r\n    Should be from a scalar `Variable` node.\r\n*  <b>`limit`</b>: An `int`.\r\n    If incrementing ref would bring it above limit, instead generates an\r\n    'OutOfRange' error.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `ref`.\r\n  A copy of the input before increment. If nothing else modifies the\r\n  input, the values produced will all be distinct.\r\n\r\n\r\n\r\n## Logical Operators <a class=\"md-anchor\" id=\"AUTOGENERATED-logical-operators\"></a>\r\n\r\nTensorFlow provides several operations that you can use to add logical operators\r\nto your graph.\r\n\r\n- - -\r\n\r\n### `tf.logical_and(x, y, name=None)` <a class=\"md-anchor\" id=\"logical_and\"></a>\r\n\r\nReturns the truth value of x AND y element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor` of type `bool`.\r\n*  <b>`y`</b>: A `Tensor` of type `bool`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `bool`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.logical_not(x, name=None)` <a class=\"md-anchor\" id=\"logical_not\"></a>\r\n\r\nReturns the truth value of NOT x element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor` of type `bool`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `bool`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.logical_or(x, y, name=None)` <a class=\"md-anchor\" id=\"logical_or\"></a>\r\n\r\nReturns the truth value of x OR y element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor` of type `bool`.\r\n*  <b>`y`</b>: A `Tensor` of type `bool`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `bool`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.logical_xor(x, y, name='LogicalXor')` <a class=\"md-anchor\" id=\"logical_xor\"></a>\r\n\r\nx ^ y = (x | y) & ~(x & y).\r\n\r\n\r\n\r\n## Comparison Operators <a class=\"md-anchor\" id=\"AUTOGENERATED-comparison-operators\"></a>\r\n\r\nTensorFlow provides several operations that you can use to add comparison\r\noperators to your graph.\r\n\r\n- - -\r\n\r\n### `tf.equal(x, y, name=None)` <a class=\"md-anchor\" id=\"equal\"></a>\r\n\r\nReturns the truth value of (x == y) element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`, `complex64`, `quint8`, `qint8`, `qint32`.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `bool`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.not_equal(x, y, name=None)` <a class=\"md-anchor\" id=\"not_equal\"></a>\r\n\r\nReturns the truth value of (x != y) element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`, `complex64`, `quint8`, `qint8`, `qint32`.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `bool`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.less(x, y, name=None)` <a class=\"md-anchor\" id=\"less\"></a>\r\n\r\nReturns the truth value of (x < y) element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `bool`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.less_equal(x, y, name=None)` <a class=\"md-anchor\" id=\"less_equal\"></a>\r\n\r\nReturns the truth value of (x <= y) element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `bool`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.greater(x, y, name=None)` <a class=\"md-anchor\" id=\"greater\"></a>\r\n\r\nReturns the truth value of (x > y) element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `bool`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.greater_equal(x, y, name=None)` <a class=\"md-anchor\" id=\"greater_equal\"></a>\r\n\r\nReturns the truth value of (x >= y) element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `bool`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.select(condition, t, e, name=None)` <a class=\"md-anchor\" id=\"select\"></a>\r\n\r\nSelects elements from `t` or `e`, depending on `condition`.\r\n\r\nThe `condition`, `t`, and `e` tensors must all have the same shape,\r\nand the output will also have that shape. The `condition` tensor acts\r\nas an element-wise mask that chooses, based on the value at each\r\nelement, whether the corresponding element in the output should be\r\ntaken from `t` (if true) or `e` (if false). For example:\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# 'condition' tensor is [[True, False]\r\n#                        [True, False]]\r\n# 't' is [[1, 1],\r\n#         [1, 1]]\r\n# 'e' is [[2, 2],\r\n#         [2, 2]]\r\nselect(condition, t, e) ==> [[1, 2],\r\n                             [1, 2]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`condition`</b>: A `Tensor` of type `bool`.\r\n*  <b>`t`</b>: A `Tensor` with the same shape as `condition`.\r\n*  <b>`e`</b>: A `Tensor` with the same type and shape as `t`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` with the same type and shape as `t` and `e`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.where(input, name=None)` <a class=\"md-anchor\" id=\"where\"></a>\r\n\r\nReturns locations of true values in a boolean tensor.\r\n\r\nThis operation returns the coordinates of true elements in `input`. The\r\ncoordinates are returned in a 2-D tensor where the first dimension (rows)\r\nrepresents the number of true elements, and the second dimension (columns)\r\nrepresents the coordinates of the true elements. Keep in mind, the shape of\r\nthe output tensor can vary depending on how many true values there are in\r\n`input`. Indices are output in row-major order.\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# 'input' tensor is [[True, False]\r\n#                    [True, False]]\r\n# 'input' has two true values, so output has two coordinates.\r\n# 'input' has rank of 2, so coordinates have two indices.\r\nwhere(input) ==> [[0, 0],\r\n                  [1, 0]]\r\n\r\n# `input` tensor is [[[True, False]\r\n#                     [True, False]]\r\n#                    [[False, True]\r\n#                     [False, True]]\r\n#                    [[False, False]\r\n#                     [False, True]]]\r\n# 'input' has 5 true values, so output has 5 coordinates.\r\n# 'input' has rank of 3, so coordinates have three indices.\r\nwhere(input) ==> [[0, 0, 0],\r\n                  [0, 1, 0],\r\n                  [1, 0, 1],\r\n                  [1, 1, 1],\r\n                  [2, 1, 1]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor` of type `bool`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `int64`.\r\n\r\n\r\n\r\n## Debugging Operations <a class=\"md-anchor\" id=\"AUTOGENERATED-debugging-operations\"></a>\r\n\r\nTensorFlow provides several operations that you can use to validate values and\r\ndebug your graph.\r\n\r\n- - -\r\n\r\n### `tf.is_finite(x, name=None)` <a class=\"md-anchor\" id=\"is_finite\"></a>\r\n\r\nReturns which elements of x are finite.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `bool`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.is_inf(x, name=None)` <a class=\"md-anchor\" id=\"is_inf\"></a>\r\n\r\nReturns which elements of x are Inf.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `bool`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.is_nan(x, name=None)` <a class=\"md-anchor\" id=\"is_nan\"></a>\r\n\r\nReturns which elements of x are NaN.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `bool`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.verify_tensor_all_finite(t, msg, name=None)` <a class=\"md-anchor\" id=\"verify_tensor_all_finite\"></a>\r\n\r\nAssert that the tensor does not contain any NaN's or Inf's.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`t`</b>: Tensor to check.\r\n*  <b>`msg`</b>: Message to log on failure.\r\n*  <b>`name`</b>: A name for this operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  Same tensor as `t`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.check_numerics(tensor, message, name=None)` <a class=\"md-anchor\" id=\"check_numerics\"></a>\r\n\r\nChecks a tensor for NaN and Inf values.\r\n\r\nWhen run, reports an `InvalidArgument` error if `tensor` has any values\r\nthat are not a number (NaN) or infinity (Inf). Otherwise, passes `tensor` as-is.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tensor`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.\r\n*  <b>`message`</b>: A `string`. Prefix of the error message.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `tensor`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.add_check_numerics_ops()` <a class=\"md-anchor\" id=\"add_check_numerics_ops\"></a>\r\n\r\nConnect a check_numerics to every floating point tensor.\r\n\r\n`check_numerics` operations themselves are added for each `float` or `double`\r\ntensor in the graph. For all ops in the graph, the `check_numerics` op for\r\nall of its (`float` or `double`) inputs is guaranteed to run before the\r\n`check_numerics` op on any of its outputs.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `group` op depending on all `check_numerics` ops added.\r\n\r\n\r\n- - -\r\n\r\n### `tf.Assert(condition, data, summarize=None, name=None)` <a class=\"md-anchor\" id=\"Assert\"></a>\r\n\r\nAsserts that the given condition is true.\r\n\r\nIf `condition` evaluates to false, print the list of tensors in `data`.\r\n`summarize` determines how many entries of the tensors to print.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`condition`</b>: The condition to evaluate.\r\n*  <b>`data`</b>: The tensors to print out when condition is false.\r\n*  <b>`summarize`</b>: Print this many entries of each tensor.\r\n*  <b>`name`</b>: A name for this operation (optional).\r\n\r\n\r\n- - -\r\n\r\n### `tf.Print(input_, data, message=None, first_n=None, summarize=None, name=None)` <a class=\"md-anchor\" id=\"Print\"></a>\r\n\r\nPrints a list of tensors.\r\n\r\nThis is an identity op with the side effect of printing `data` when\r\nevaluating.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input_`</b>: A tensor passed through this op.\r\n*  <b>`data`</b>: A list of tensors to print out when op is evaluated.\r\n*  <b>`message`</b>: A string, prefix of the error message.\r\n*  <b>`first_n`</b>: Only log `first_n` number of times. Negative numbers log always;\r\n           this is the default.\r\n*  <b>`summarize`</b>: Only print this many entries of each tensor.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  Same tensor as `input_`.\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/python/framework.md",
    "content": "<!-- This file is machine generated: DO NOT EDIT! -->\r\n\r\n# Building Graphs <a class=\"md-anchor\" id=\"AUTOGENERATED-building-graphs\"></a>\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Building Graphs](#AUTOGENERATED-building-graphs)\r\n* [Core graph data structures](#AUTOGENERATED-core-graph-data-structures)\r\n  * [`class tf.Graph`](#Graph)\r\n  * [`class tf.Operation`](#Operation)\r\n  * [`class tf.Tensor`](#Tensor)\r\n* [Tensor types](#AUTOGENERATED-tensor-types)\r\n  * [`class tf.DType`](#DType)\r\n  * [`tf.as_dtype(type_value)`](#as_dtype)\r\n* [Utility functions](#AUTOGENERATED-utility-functions)\r\n  * [`tf.device(dev)`](#device)\r\n  * [`tf.name_scope(name)`](#name_scope)\r\n  * [`tf.control_dependencies(control_inputs)`](#control_dependencies)\r\n  * [`tf.convert_to_tensor(value, dtype=None, name=None)`](#convert_to_tensor)\r\n  * [`tf.get_default_graph()`](#get_default_graph)\r\n  * [`tf.import_graph_def(graph_def, input_map=None, return_elements=None, name=None, op_dict=None)`](#import_graph_def)\r\n* [Graph collections](#AUTOGENERATED-graph-collections)\r\n  * [`tf.add_to_collection(name, value)`](#add_to_collection)\r\n  * [`tf.get_collection(key, scope=None)`](#get_collection)\r\n  * [`class tf.GraphKeys`](#GraphKeys)\r\n* [Defining new operations](#AUTOGENERATED-defining-new-operations)\r\n  * [`class tf.RegisterGradient`](#RegisterGradient)\r\n  * [`tf.NoGradient(op_type)`](#NoGradient)\r\n  * [`class tf.RegisterShape`](#RegisterShape)\r\n  * [`class tf.TensorShape`](#TensorShape)\r\n  * [`class tf.Dimension`](#Dimension)\r\n  * [`tf.op_scope(values, name, default_name)`](#op_scope)\r\n  * [`tf.get_seed(op_seed)`](#get_seed)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\nClasses and functions for building TensorFlow graphs.\r\n\r\n## Core graph data structures <a class=\"md-anchor\" id=\"AUTOGENERATED-core-graph-data-structures\"></a>\r\n\r\n- - -\r\n\r\n### `class tf.Graph` <a class=\"md-anchor\" id=\"Graph\"></a>\r\n\r\nA TensorFlow computation, represented as a dataflow graph.\r\n\r\nA `Graph` contains a set of\r\n[`Operation`](../../api_docs/python/framework.md#Operation) objects,\r\nwhich represent units of computation; and\r\n[`Tensor`](../../api_docs/python/framework.md#Tensor) objects, which represent\r\nthe units of data that flow between operations.\r\n\r\nA default `Graph` is always registered, and accessible by calling\r\n[`tf.get_default_graph()`](../../api_docs/python/framework.md#get_default_graph).\r\nTo add an operation to the default graph, simply call one of the functions\r\nthat defines a new `Operation`:\r\n\r\n```\r\nc = tf.constant(4.0)\r\nassert c.graph is tf.get_default_graph()\r\n```\r\n\r\nAnother typical usage involves the\r\n[`Graph.as_default()`](../../api_docs/python/framework.md#Graph.as_default)\r\ncontext manager, which overrides the current default graph for the\r\nlifetime of the context:\r\n\r\n```python\r\ng = tf.Graph()\r\nwith g.as_default():\r\n  # Define operations and tensors in `g`.\r\n  c = tf.constant(30.0)\r\n  assert c.graph is g\r\n```\r\n\r\nImportant note: This class *is not* thread-safe for graph construction. All\r\noperations should be created from a single thread, or external\r\nsynchronization must be provided. Unless otherwise specified, all methods\r\nare not thread-safe.\r\n\r\n- - -\r\n\r\n#### `tf.Graph.__init__()` <a class=\"md-anchor\" id=\"Graph.__init__\"></a>\r\n\r\nCreates a new, empty Graph.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.as_default()` <a class=\"md-anchor\" id=\"Graph.as_default\"></a>\r\n\r\nReturns a context manager that makes this `Graph` the default graph.\r\n\r\nThis method should be used if you want to create multiple graphs\r\nin the same process. For convenience, a global default graph is\r\nprovided, and all ops will be added to this graph if you do not\r\ncreate a new graph explicitly. Use this method the `with` keyword\r\nto specify that ops created within the scope of a block should be\r\nadded to this graph.\r\n\r\nThe default graph is a property of the current thread. If you\r\ncreate a new thread, and wish to use the default graph in that\r\nthread, you must explicitly add a `with g.as_default():` in that\r\nthread's function.\r\n\r\nThe following code examples are equivalent:\r\n\r\n```python\r\n# 1. Using Graph.as_default():\r\ng = tf.Graph()\r\nwith g.as_default():\r\n  c = tf.constant(5.0)\r\n  assert c.graph is g\r\n\r\n# 2. Constructing and making default:\r\nwith tf.Graph().as_default() as g:\r\n  c = tf.constant(5.0)\r\n  assert c.graph is g\r\n```\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A context manager for using this graph as the default graph.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.as_graph_def(from_version=None)` <a class=\"md-anchor\" id=\"Graph.as_graph_def\"></a>\r\n\r\nReturns a serialized `GraphDef` representation of this graph.\r\n\r\nThe serialized `GraphDef` can be imported into another `Graph`\r\n(using [`import_graph_def()`](#import_graph_def)) or used with the\r\n[C++ Session API](../../api_docs/cc/index.md).\r\n\r\nThis method is thread-safe.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`from_version`</b>: Optional.  If this is set, returns a `GraphDef`\r\n    containing only the nodes that were added to this graph since\r\n    its `version` property had the given value.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A [`GraphDef`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/graph.proto)\r\n  protocol buffer.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.finalize()` <a class=\"md-anchor\" id=\"Graph.finalize\"></a>\r\n\r\nFinalizes this graph, making it read-only.\r\n\r\nAfter calling `g.finalize()`, no new operations can be added to\r\n`g`.  This method is used to ensure that no operations are added\r\nto a graph when it is shared between multiple threads, for example\r\nwhen using a [`QueueRunner`](../../api_docs/python/train.md#QueueRunner).\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.finalized` <a class=\"md-anchor\" id=\"Graph.finalized\"></a>\r\n\r\nTrue if this graph has been finalized.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.control_dependencies(control_inputs)` <a class=\"md-anchor\" id=\"Graph.control_dependencies\"></a>\r\n\r\nReturns a context manager that specifies control dependencies.\r\n\r\nUse with the `with` keyword to specify that all operations constructed\r\nwithin the context should have control dependencies on\r\n`control_inputs`. For example:\r\n\r\n```python\r\nwith g.control_dependencies([a, b, c]):\r\n  # `d` and `e` will only run after `a`, `b`, and `c` have executed.\r\n  d = ...\r\n  e = ...\r\n```\r\n\r\nMultiple calls to `control_dependencies()` can be nested, and in\r\nthat case a new `Operation` will have control dependencies on the union\r\nof `control_inputs` from all active contexts.\r\n\r\n```python\r\nwith g.control_dependencies([a, b]):\r\n  # Ops declared here run after `a` and `b`.\r\n  with g.control_dependencies([c, d]):\r\n    # Ops declared here run after `a`, `b`, `c`, and `d`.\r\n```\r\n\r\n*N.B.* The control dependencies context applies *only* to ops that\r\nare constructed within the context. Merely using an op or tensor\r\nin the context does not add a control dependency. The following\r\nexample illustrates this point:\r\n\r\n```python\r\n# WRONG\r\ndef my_func(pred, tensor):\r\n  t = tf.matmul(tensor, tensor)\r\n  with tf.control_dependencies([pred]):\r\n    # The matmul op is created outside the context, so no control\r\n    # dependency will be added.\r\n    return t\r\n\r\n# RIGHT\r\ndef my_func(pred, tensor):\r\n  with tf.control_dependencies([pred]):\r\n    # The matmul op is created in the context, so a control dependency\r\n    # will be added.\r\n    return tf.matmul(tensor, tensor)\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`control_inputs`</b>: A list of `Operation` or `Tensor` objects, which\r\n    must be executed or computed before running the operations\r\n    defined in the context.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n A context manager that specifies control dependencies for all\r\n operations constructed within the context.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `control_inputs` is not a list of `Operation` or\r\n    `Tensor` objects.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.device(device_name_or_function)` <a class=\"md-anchor\" id=\"Graph.device\"></a>\r\n\r\nReturns a context manager that specifies the default device to use.\r\n\r\nThe `device_name_or_function` argument may either be a device name\r\nstring, a device function, or None:\r\n\r\n* If it is a device name string, all operations constructed in\r\n  this context will be assigned to the device with that name.\r\n* If it is a function, it will be treated as function from\r\n  Operation objects to device name strings, and invoked each time\r\n  a new Operation is created. The Operation will be assigned to\r\n  the device with the returned name.\r\n* If it is None, the default device will be cleared.\r\n\r\nFor example:\r\n\r\n```python\r\nwith g.device('/gpu:0'):\r\n  # All operations constructed in this context will be placed\r\n  # on GPU 0.\r\n  with g.device(None):\r\n    # All operations constructed in this context will have no\r\n    # assigned device.\r\n\r\n# Defines a function from `Operation` to device string.\r\ndef matmul_on_gpu(n):\r\n  if n.type == \"MatMul\":\r\n    return \"/gpu:0\"\r\n  else:\r\n    return \"/cpu:0\"\r\n\r\nwith g.device(matmul_on_gpu):\r\n  # All operations of type \"MatMul\" constructed in this context\r\n  # will be placed on GPU 0; all other operations will be placed\r\n  # on CPU 0.\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`device_name_or_function`</b>: The device name or function to use in\r\n    the context.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A context manager that specifies the default device to use for newly\r\n  created ops.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.name_scope(name)` <a class=\"md-anchor\" id=\"Graph.name_scope\"></a>\r\n\r\nReturns a context manager that creates hierarchical names for operations.\r\n\r\nA graph maintains a stack of name scopes. A `with name_scope(...):`\r\nstatement pushes a new name onto the stack for the lifetime of the context.\r\n\r\nThe `name` argument will be interpreted as follows:\r\n\r\n* A string (not ending with '/') will create a new name scope, in which\r\n  `name` is appended to the prefix of all operations created in the\r\n  context. If `name` has been used before, it will be made unique by\r\n  calling `self.unique_name(name)`.\r\n* A scope previously captured from a `with g.name_scope(...) as\r\n  scope:` statement will be treated as an \"absolute\" name scope, which\r\n  makes it possible to re-enter existing scopes.\r\n* A value of `None` or the empty string will reset the current name scope\r\n  to the top-level (empty) name scope.\r\n\r\nFor example:\r\n\r\n```python\r\nwith tf.Graph().as_default() as g:\r\n  c = tf.constant(5.0, name=\"c\")\r\n  assert c_1.name == \"c\"\r\n  c_1 = tf.constant(6.0, name=\"c\")\r\n  assert c_1.name == \"c_1\"\r\n\r\n  # Creates a scope called \"nested\"\r\n  with g.name_scope(\"nested\") as scope:\r\n    nested_c = tf.constant(10.0, name=\"c\")\r\n    assert nested_c.name == \"nested/c\"\r\n\r\n    # Creates a nested scope called \"inner\".\r\n    with g.name_scope(\"inner\"):\r\n      nested_inner_c = tf.constant(20.0, name=\"c\")\r\n      assert nested_inner_c.name == \"nested/inner/c\"\r\n\r\n    # Create a nested scope called \"inner_1\".\r\n    with g.name_scope(\"inner\"):\r\n      nested_inner_1_c = tf.constant(30.0, name=\"c\")\r\n      assert nested_inner_1_c.name == \"nested/inner_1/c\"\r\n\r\n      # Treats `scope` as an absolute name scope, and\r\n      # switches to the \"nested/\" scope.\r\n      with g.name_scope(scope):\r\n        nested_d = tf.constant(40.0, name=\"d\")\r\n        assert nested_d.name == \"nested/d\"\r\n\r\n        with g.name_scope(\"\"):\r\n          e = tf.constant(50.0, name=\"e\")\r\n          assert e.name == \"e\"\r\n```\r\n\r\nThe name of the scope itself can be captured by `with\r\ng.name_scope(...) as scope:`, which stores the name of the scope\r\nin the variable `scope`. This value can be used to name an\r\noperation that represents the overall result of executing the ops\r\nin a scope. For example:\r\n\r\n```python\r\ninputs = tf.constant(...)\r\nwith g.name_scope('my_layer') as scope:\r\n  weights = tf.Variable(..., name=\"weights\")\r\n  biases = tf.Variable(..., name=\"biases\")\r\n  affine = tf.matmul(inputs, weights) + biases\r\n  output = tf.nn.relu(affine, name=scope)\r\n```\r\n\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the scope.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A context manager that installs `name` as a new name scope.\r\n\r\n\r\n\r\nA `Graph` instance supports an arbitrary number of \"collections\"\r\nthat are identified by name. For convenience when building a large\r\ngraph, collections can store groups of related objects: for\r\nexample, the `tf.Variable` uses a collection (named\r\n[`tf.GraphKeys.VARIABLES`](../../api_docs/python/framework.md#GraphKeys)) for\r\nall variables that are created during the construction of a graph. The caller\r\nmay define additional collections by specifying a new name.\r\n\r\n- - -\r\n\r\n#### `tf.Graph.add_to_collection(name, value)` <a class=\"md-anchor\" id=\"Graph.add_to_collection\"></a>\r\n\r\nStores `value` in the collection with the given `name`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: The key for the collection. For example, the `GraphKeys` class\r\n    contains many standard names for collections.\r\n*  <b>`value`</b>: The value to add to the collection.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.get_collection(name, scope=None)` <a class=\"md-anchor\" id=\"Graph.get_collection\"></a>\r\n\r\nReturns a list of values in the collection with the given `name`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`key`</b>: The key for the collection. For example, the `GraphKeys` class\r\n    contains many standard names for collections.\r\n*  <b>`scope`</b>: (Optional.) If supplied, the resulting list is filtered to include\r\n    only items whose name begins with this string.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The list of values in the collection with the given `name`, or\r\n  an empty list if no value has been added to that collection. The\r\n  list contains the values in the order under which they were\r\n  collected.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.as_graph_element(obj, allow_tensor=True, allow_operation=True)` <a class=\"md-anchor\" id=\"Graph.as_graph_element\"></a>\r\n\r\nReturns the object referred to by `obj`, as an `Operation` or `Tensor`.\r\n\r\nThis function validates that `obj` represents an element of this\r\ngraph, and gives an informative error message if it is not.\r\n\r\nThis function is the canonical way to get/validate an object of\r\none of the allowed types from an external argument reference in the\r\nSession API.\r\n\r\nThis method may be called concurrently from multiple threads.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`obj`</b>: A `Tensor`, an `Operation`, or the name of a tensor or operation.\r\n    Can also be any object with an `_as_graph_element()` method that returns\r\n    a value of one of these types.\r\n*  <b>`allow_tensor`</b>: If true, `obj` may refer to a `Tensor`.\r\n*  <b>`allow_operation`</b>: If true, `obj` may refer to an `Operation`.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The `Tensor` or `Operation` in the Graph corresponding to `obj`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `obj` is not a type we support attempting to convert\r\n    to types.\r\n*  <b>`ValueError`</b>: If `obj` is of an appropriate type but invalid. For\r\n    example, an invalid string.\r\n*  <b>`KeyError`</b>: If `obj` is not an object in the graph.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.get_operation_by_name(name)` <a class=\"md-anchor\" id=\"Graph.get_operation_by_name\"></a>\r\n\r\nReturns the `Operation` with the given `name`.\r\n\r\nThis method may be called concurrently from multiple threads.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: The name of the `Operation` to return.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The `Operation` with the given `name`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `name` is not a string.\r\n*  <b>`KeyError`</b>: If `name` does not correspond to an operation in this graph.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.get_tensor_by_name(name)` <a class=\"md-anchor\" id=\"Graph.get_tensor_by_name\"></a>\r\n\r\nReturns the `Tensor` with the given `name`.\r\n\r\nThis method may be called concurrently from multiple threads.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: The name of the `Tensor` to return.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The `Tensor` with the given `name`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `name` is not a string.\r\n*  <b>`KeyError`</b>: If `name` does not correspond to a tensor in this graph.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.get_operations()` <a class=\"md-anchor\" id=\"Graph.get_operations\"></a>\r\n\r\nReturn the list of operations in the graph.\r\n\r\nYou can modify the operations in place, but modifications\r\nto the list such as inserts/delete have no effect on the\r\nlist of operations known to the graph.\r\n\r\nThis method may be called concurrently from multiple threads.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of Operations.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.get_default_device()` <a class=\"md-anchor\" id=\"Graph.get_default_device\"></a>\r\n\r\nReturns the default device.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A string.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.seed` <a class=\"md-anchor\" id=\"Graph.seed\"></a>\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.unique_name(name)` <a class=\"md-anchor\" id=\"Graph.unique_name\"></a>\r\n\r\nReturn a unique Operation name for \"name\".\r\n\r\nNote: You rarely need to call unique_name() directly.  Most of the time you\r\njust need to create \"with g.name_scope()\" blocks to generate structured\r\nnames.\r\n\r\n`unique_name` is used to generate structured names, separated by \"/\",\r\nto help identify Operations when debugging a Graph.  Operation names\r\nare displayed in error messages reported by the TensorFlow runtime,\r\nand in various visualization tools such as TensorBoard.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: The name for an `Operation`.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A string to be passed to `create_op()` that will be used\r\n  to name the operation being created.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.version` <a class=\"md-anchor\" id=\"Graph.version\"></a>\r\n\r\nReturns a version number that increases as ops are added to the graph.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.create_op(op_type, inputs, dtypes, input_types=None, name=None, attrs=None, op_def=None, compute_shapes=True)` <a class=\"md-anchor\" id=\"Graph.create_op\"></a>\r\n\r\nCreates an `Operation` in this graph.\r\n\r\nThis is a low-level interface for creating an `Operation`. Most\r\nprograms will not call this method directly, and instead use the\r\nPython op constructors, such as `tf.constant()`, which add ops to\r\nthe default graph.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`op_type`</b>: The `Operation` type to create. This corresponds to the\r\n    `OpDef.name` field for the proto that defines the operation.\r\n*  <b>`inputs`</b>: A list of `Tensor` objects that will be inputs to the `Operation`.\r\n*  <b>`dtypes`</b>: A list of `DType` objects that will be the types of the tensors\r\n    that the operation produces.\r\n*  <b>`input_types`</b>: (Optional.) A list of `DType`s that will be the types of\r\n    the tensors that the operation consumes. By default, uses the base\r\n    `DType` of each input in `inputs`. Operations that expect\r\n    reference-typed inputs must specify `input_types` explicitly.\r\n*  <b>`name`</b>: (Optional.) A string name for the operation. If not specified, a\r\n    name is generated based on `op_type`.\r\n*  <b>`attrs`</b>: (Optional.) A list of `AttrValue` protos for the `attr` field of\r\n    the `NodeDef` proto that will represent the operation.\r\n*  <b>`op_def`</b>: (Optional.) The `OpDef` proto that describes the `op_type` that\r\n    the operation will have.\r\n*  <b>`compute_shapes`</b>: (Optional.) If True, shape inference will be performed\r\n    to compute the shapes of the outputs.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: if any of the inputs is not a `Tensor`.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An `Operation` object.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Graph.gradient_override_map(op_type_map)` <a class=\"md-anchor\" id=\"Graph.gradient_override_map\"></a>\r\n\r\nEXPERIMENTAL: A context manager for overriding gradient functions.\r\n\r\nThis context manager can be used to override the gradient function\r\nthat will be used for ops within the scope of the context.\r\n\r\nFor example:\r\n\r\n```python\r\n@tf.RegisterGradient(\"CustomSquare\")\r\ndef _custom_square_grad(op, inputs):\r\n  # ...\r\n\r\nwith tf.Graph().as_default() as g:\r\n  c = tf.constant(5.0)\r\n  s_1 = tf.square(c)  # Uses the default gradient for tf.square.\r\n  with g.gradient_override_map({\"Square\": \"CustomSquare\"}):\r\n    s_2 = tf.square(s_2)  # Uses _custom_square_grad to compute the\r\n                          # gradient of s_2.\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`op_type_map`</b>: A dictionary mapping op type strings to alternative op\r\n    type strings.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A context manager that sets the alternative op type to be used for one\r\n  or more ops created in that context.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `op_type_map` is not a dictionary mapping strings to\r\n    strings.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.Operation` <a class=\"md-anchor\" id=\"Operation\"></a>\r\n\r\nRepresents a graph node that performs computation on tensors.\r\n\r\nAn `Operation` is a node in a TensorFlow `Graph` that takes zero or\r\nmore `Tensor` objects as input, and produces zero or more `Tensor`\r\nobjects as output. Objects of type `Operation` are created by\r\ncalling a Python op constructor (such as\r\n[`tf.matmul()`](../../api_docs/python/math_ops.md#matmul))\r\nor [`Graph.create_op()`](../../api_docs/python/framework.md#Graph.create_op).\r\n\r\nFor example `c = tf.matmul(a, b)` creates an `Operation` of type\r\n\"MatMul\" that takes tensors `a` and `b` as input, and produces `c`\r\nas output.\r\n\r\nAfter the graph has been launched in a session, an `Operation` can\r\nbe executed by passing it to\r\n[`Session.run()`](../../api_docs/python/client.md#Session.run).\r\n`op.run()` is a shortcut for calling `tf.get_default_session().run(op)`.\r\n\r\n- - -\r\n\r\n#### `tf.Operation.name` <a class=\"md-anchor\" id=\"Operation.name\"></a>\r\n\r\nThe full name of this operation.\r\n\r\n- - -\r\n\r\n#### `tf.Operation.type` <a class=\"md-anchor\" id=\"Operation.type\"></a>\r\n\r\nThe type of the op (e.g. `\"MatMul\"`).\r\n\r\n- - -\r\n\r\n#### `tf.Operation.inputs` <a class=\"md-anchor\" id=\"Operation.inputs\"></a>\r\n\r\nThe list of `Tensor` objects representing the data inputs of this op.\r\n\r\n- - -\r\n\r\n#### `tf.Operation.control_inputs` <a class=\"md-anchor\" id=\"Operation.control_inputs\"></a>\r\n\r\nThe `Operation` objects on which this op has a control dependency.\r\n\r\nBefore this op is executed, TensorFlow will ensure that the\r\noperations in `self.control_inputs` have finished executing. This\r\nmechanism can be used to run ops sequentially for performance\r\nreasons, or to ensure that the side effects of an op are observed\r\nin the correct order.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of `Operation` objects.\r\n\r\n- - -\r\n\r\n#### `tf.Operation.outputs` <a class=\"md-anchor\" id=\"Operation.outputs\"></a>\r\n\r\nThe list of `Tensor` objects representing the outputs of this op.\r\n\r\n- - -\r\n\r\n#### `tf.Operation.device` <a class=\"md-anchor\" id=\"Operation.device\"></a>\r\n\r\nThe name of the device to which this op has been assigned, if any.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The string name of the device to which this op has been\r\n  assigned, or None if it has not been assigned to a device.\r\n\r\n- - -\r\n\r\n#### `tf.Operation.graph` <a class=\"md-anchor\" id=\"Operation.graph\"></a>\r\n\r\nThe `Graph` that contains this operation.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Operation.run(feed_dict=None, session=None)` <a class=\"md-anchor\" id=\"Operation.run\"></a>\r\n\r\nRuns this operation in a `Session`.\r\n\r\nCalling this method will execute all preceding operations that\r\nproduce the inputs needed for this operation.\r\n\r\n*N.B.* Before invoking `Operation.run()`, its graph must have been\r\nlaunched in a session, and either a default session must be\r\navailable, or `session` must be specified explicitly.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`feed_dict`</b>: A dictionary that maps `Tensor` objects to feed values.\r\n    See [`Session.run()`](../../api_docs/python/client.md#Session.run)\r\n    for a description of the valid feed values.\r\n*  <b>`session`</b>: (Optional.) The `Session` to be used to run to this operation. If\r\n    none, the default session will be used.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Operation.get_attr(name)` <a class=\"md-anchor\" id=\"Operation.get_attr\"></a>\r\n\r\nReturns the value of the attr of this op with the given `name`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: The name of the attr to fetch.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The value of the attr, as a Python object.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If this op does not have an attr with the given `name`.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Operation.traceback` <a class=\"md-anchor\" id=\"Operation.traceback\"></a>\r\n\r\nReturns the call stack from when this operation was constructed.\r\n\r\n\r\n#### Other Methods <a class=\"md-anchor\" id=\"AUTOGENERATED-other-methods\"></a>\r\n- - -\r\n\r\n#### `tf.Operation.__init__(node_def, g, inputs=None, output_types=None, control_inputs=None, input_types=None, original_op=None, op_def=None)` <a class=\"md-anchor\" id=\"Operation.__init__\"></a>\r\n\r\nCreates an `Operation`.\r\n\r\nNOTE: This constructor validates the name of the Operation (passed\r\nas \"node_def.name\"). Valid Operation names match the following\r\nregular expression:\r\n\r\n  [A-Za-z0-9.][A-Za-z0-9_.\\-/]*\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`node_def`</b>: graph_pb2.NodeDef.  NodeDef for the Operation.\r\n    Used for attributes of graph_pb2.NodeDef, typically \"name\",\r\n    \"op\", and \"device\".  The \"input\" attribute is irrelevant here\r\n    as it will be computed when generating the model.\r\n*  <b>`g`</b>: Graph. The parent graph.\r\n*  <b>`inputs`</b>: list of Tensor objects. The inputs to this Operation.\r\n*  <b>`output_types`</b>: list of types_pb2.DataType.  List of the types of the\r\n    Tensors computed by this operation.  The length of this list indicates\r\n    the number of output endpoints of the Operation.\r\n*  <b>`control_inputs`</b>: list of operations or tensors from which to have a\r\n    control dependency.\r\n*  <b>`input_types`</b>: List of types_pb2.DataType representing the\r\n    types of the Tensors accepted by the Operation.  By default\r\n    uses [x.dtype.base_dtype for x in inputs].  Operations that expect\r\n    reference-typed inputs must specify these explicitly.\r\n*  <b>`original_op`</b>: Optional. Used to associate the new Operation with an\r\n    existing Operation (for example, a replica with the op that was\r\n    replicated).\r\n*  <b>`op_def`</b>: Optional. The op_def_pb2.OpDef proto that describes the\r\n    op type that this Operation represents.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: if control inputs are not Operations or Tensors,\r\n    or if node_def is not a NodeDef,\r\n    or if g is not a Graph,\r\n    or if inputs are not Tensors,\r\n    or if inputs and input_types are incompatible.\r\n*  <b>`ValueError`</b>: if the node_def name is not valid.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Operation.node_def` <a class=\"md-anchor\" id=\"Operation.node_def\"></a>\r\n\r\nReturns a serialized `NodeDef` representation of this operation.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A\r\n  [`NodeDef`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/graph.proto)\r\n  protocol buffer.\r\n\r\n- - -\r\n\r\n#### `tf.Operation.op_def` <a class=\"md-anchor\" id=\"Operation.op_def\"></a>\r\n\r\nReturns the `OpDef` proto that represents the type of this op.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An\r\n  [`OpDef`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op_def.proto)\r\n  protocol buffer.\r\n\r\n- - -\r\n\r\n#### `tf.Operation.values()` <a class=\"md-anchor\" id=\"Operation.values\"></a>\r\n\r\nDEPRECATED: Use outputs.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.Tensor` <a class=\"md-anchor\" id=\"Tensor\"></a>\r\n\r\nRepresents a value produced by an `Operation`.\r\n\r\nA `Tensor` is a symbolic handle to one of the outputs of an\r\n`Operation`. It does not hold the values of that operation's output,\r\nbut instead provides a means of computing those values in a\r\nTensorFlow [`Session`](../../api_docs/python/client.md#Session).\r\n\r\nThis class has two primary purposes:\r\n\r\n1. A `Tensor` can be passed as an input to another `Operation`.\r\n   This builds a dataflow connection between operations, which\r\n   enables TensorFlow to execute an entire `Graph` that represents a\r\n   large, multi-step computation.\r\n\r\n2. After the graph has been launched in a session, the value of the\r\n   `Tensor` can be computed by passing it to\r\n   [`Session.run()`](../../api_docs/python/client.md#Session.run).\r\n   `t.eval()` is a shortcut for calling\r\n   `tf.get_default_session().run(t)`.\r\n\r\nIn the following example, `c`, `d`, and `e` are symbolic `Tensor`\r\nobjects, whereas `result` is a numpy array that stores a concrete\r\nvalue:\r\n\r\n```python\r\n# Build a dataflow graph.\r\nc = tf.constant([[1.0, 2.0], [3.0, 4.0]])\r\nd = tf.constant([[1.0, 1.0], [0.0, 1.0]])\r\ne = tf.matmul(c, d)\r\n\r\n# Construct a `Session` to execut the graph.\r\nsess = tf.Session()\r\n\r\n# Execute the graph and store the value that `e` represents in `result`.\r\nresult = sess.run(e)\r\n```\r\n\r\n- - -\r\n\r\n#### `tf.Tensor.dtype` <a class=\"md-anchor\" id=\"Tensor.dtype\"></a>\r\n\r\nThe `DType` of elements in this tensor.\r\n\r\n- - -\r\n\r\n#### `tf.Tensor.name` <a class=\"md-anchor\" id=\"Tensor.name\"></a>\r\n\r\nThe string name of this tensor.\r\n\r\n- - -\r\n\r\n#### `tf.Tensor.value_index` <a class=\"md-anchor\" id=\"Tensor.value_index\"></a>\r\n\r\nThe index of this tensor in the outputs of its `Operation`.\r\n\r\n- - -\r\n\r\n#### `tf.Tensor.graph` <a class=\"md-anchor\" id=\"Tensor.graph\"></a>\r\n\r\nThe `Graph` that contains this tensor.\r\n\r\n- - -\r\n\r\n#### `tf.Tensor.op` <a class=\"md-anchor\" id=\"Tensor.op\"></a>\r\n\r\nThe `Operation` that produces this tensor as an output.\r\n\r\n- - -\r\n\r\n#### `tf.Tensor.consumers()` <a class=\"md-anchor\" id=\"Tensor.consumers\"></a>\r\n\r\nReturns a list of `Operation`s that consume this tensor.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of `Operation`s.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Tensor.eval(feed_dict=None, session=None)` <a class=\"md-anchor\" id=\"Tensor.eval\"></a>\r\n\r\nEvaluates this tensor in a `Session`.\r\n\r\nCalling this method will execute all preceding operations that\r\nproduce the inputs needed for the operation that produces this\r\ntensor.\r\n\r\n*N.B.* Before invoking `Tensor.eval()`, its graph must have been\r\nlaunched in a session, and either a default session must be\r\navailable, or `session` must be specified explicitly.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`feed_dict`</b>: A dictionary that maps `Tensor` objects to feed values.\r\n    See [`Session.run()`](../../api_docs/python/client.md#Session.run) for a\r\n    description of the valid feed values.\r\n*  <b>`session`</b>: (Optional.) The `Session` to be used to evaluate this tensor. If\r\n    none, the default session will be used.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A numpy array corresponding to the value of this tensor.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Tensor.get_shape()` <a class=\"md-anchor\" id=\"Tensor.get_shape\"></a>\r\n\r\nReturns the `TensorShape` that represents the shape of this tensor.\r\n\r\nThe shape is computed using shape inference functions that are\r\nregistered for each `Operation` type using `tf.RegisterShape`.\r\nSee [`TensorShape`](../../api_docs/python/framework.md#TensorShape) for more\r\ndetails of what a shape represents.\r\n\r\nThe inferred shape of a tensor is used to provide shape\r\ninformation without having to launch the graph in a session. This\r\ncan be used for debugging, and providing early error messages. For\r\nexample:\r\n\r\n```python\r\nc = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])\r\n\r\nprint c.get_shape()\r\n==> TensorShape([Dimension(2), Dimension(3)])\r\n\r\nd = tf.constant([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 1.0]])\r\n\r\nprint d.get_shape()\r\n==> TensorShape([Dimension(4), Dimension(2)])\r\n\r\n# Raises a ValueError, because `c` and `d` do not have compatible\r\n# inner dimensions.\r\ne = tf.matmul(c, d)\r\n\r\nf = tf.matmul(c, d, transpose_a=True, transpose_b=True)\r\n\r\nprint f.get_shape()\r\n==> TensorShape([Dimension(3), Dimension(4)])\r\n```\r\n\r\nIn some cases, the inferred shape may have unknown dimensions. If\r\nthe caller has additional information about the values of these\r\ndimensions, `Tensor.set_shape()` can be used to augment the\r\ninferred shape.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `TensorShape` representing the shape of this tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Tensor.set_shape(shape)` <a class=\"md-anchor\" id=\"Tensor.set_shape\"></a>\r\n\r\nUpdates the shape of this tensor.\r\n\r\nThis method can be called multiple times, and will merge the given\r\n`shape` with the current shape of this tensor. It can be used to\r\nprovide additional information about the shape of this tensor that\r\ncannot be inferred from the graph alone. For example, this can be used\r\nto provide additional information about the shapes of images:\r\n\r\n```python\r\n_, image_data = tf.TFRecordReader(...).read(...)\r\nimage = tf.image.decode_png(image_data, channels=3)\r\n\r\n# The height and width dimensions of `image` are data dependent, and\r\n# cannot be computed without executing the op.\r\nprint image.get_shape()\r\n==> TensorShape([Dimension(None), Dimension(None), Dimension(3)])\r\n\r\n# We know that each image in this dataset is 28 x 28 pixels.\r\nimage.set_shape([28, 28, 3])\r\nprint image.get_shape()\r\n==> TensorShape([Dimension(28), Dimension(28), Dimension(3)])\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`shape`</b>: A `TensorShape` representing the shape of this tensor.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `shape` is not compatible with the current shape of\r\n    this tensor.\r\n\r\n\r\n\r\n#### Other Methods <a class=\"md-anchor\" id=\"AUTOGENERATED-other-methods\"></a>\r\n- - -\r\n\r\n#### `tf.Tensor.__init__(op, value_index, dtype)` <a class=\"md-anchor\" id=\"Tensor.__init__\"></a>\r\n\r\nCreates a new `Tensor`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`op`</b>: An `Operation`. `Operation` that computes this tensor.\r\n*  <b>`value_index`</b>: An `int`. Index of the operation's endpoint that produces\r\n    this tensor.\r\n*  <b>`dtype`</b>: A `types.DType`. Type of data stored in this tensor.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If the op is not an `Operation`.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Tensor.device` <a class=\"md-anchor\" id=\"Tensor.device\"></a>\r\n\r\nThe name of the device on which this tensor will be produced, or None.\r\n\r\n\r\n\r\n## Tensor types <a class=\"md-anchor\" id=\"AUTOGENERATED-tensor-types\"></a>\r\n\r\n- - -\r\n\r\n### `class tf.DType` <a class=\"md-anchor\" id=\"DType\"></a>\r\n\r\nRepresents the type of the elements in a `Tensor`.\r\n\r\nThe following `DType` objects are defined:\r\n\r\n* `tf.float32`: 32-bit single-precision floating-point.\r\n* `tf.float64`: 64-bit double-precision floating-point.\r\n* `tf.bfloat16`: 16-bit truncated floating-point.\r\n* `tf.complex64`: 64-bit single-precision complex.\r\n\r\n* `tf.int8`: 8-bit signed integer.\r\n* `tf.uint8`: 8-bit unsigned integer.\r\n* `tf.int32`: 32-bit signed integer.\r\n* `tf.int64`: 64-bit signed integer.\r\n\r\n* `tf.bool`: Boolean.\r\n\r\n* `tf.string`: String.\r\n\r\n* `tf.qint8`: Quantized 8-bit signed integer.\r\n* `tf.quint8`: Quantized 8-bit unsigned integer.\r\n* `tf.qint32`: Quantized 32-bit signed integer.\r\n\r\nIn addition, variants of these types with the `_ref` suffix are\r\ndefined for reference-typed tensors.\r\n\r\nThe `tf.as_dtype()` function converts numpy types and string type\r\nnames to a `DType` object.\r\n\r\n- - -\r\n\r\n#### `tf.DType.is_compatible_with(other)` <a class=\"md-anchor\" id=\"DType.is_compatible_with\"></a>\r\n\r\nReturns True if the `other` DType will be converted to this DType.\r\n\r\nThe conversion rules are as follows:\r\n\r\n```\r\nDType(T)       .is_compatible_with(DType(T))        == True\r\nDType(T)       .is_compatible_with(DType(T).as_ref) == True\r\nDType(T).as_ref.is_compatible_with(DType(T))        == False\r\nDType(T).as_ref.is_compatible_with(DType(T).as_ref) == True\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`other`</b>: A `DType` (or object that may be converted to a `DType`).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  True if a Tensor of the `other` `DType` will be implicitly converted to\r\n  this `DType`.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.DType.name` <a class=\"md-anchor\" id=\"DType.name\"></a>\r\n\r\nReturns the string name for this `DType`.\r\n\r\n- - -\r\n\r\n#### `tf.DType.base_dtype` <a class=\"md-anchor\" id=\"DType.base_dtype\"></a>\r\n\r\nReturns a non-reference `DType` based on this `DType`.\r\n\r\n- - -\r\n\r\n#### `tf.DType.is_ref_dtype` <a class=\"md-anchor\" id=\"DType.is_ref_dtype\"></a>\r\n\r\nReturns `True` if this `DType` represents a reference type.\r\n\r\n- - -\r\n\r\n#### `tf.DType.as_ref` <a class=\"md-anchor\" id=\"DType.as_ref\"></a>\r\n\r\nReturns a reference `DType` based on this `DType`.\r\n\r\n- - -\r\n\r\n#### `tf.DType.is_integer` <a class=\"md-anchor\" id=\"DType.is_integer\"></a>\r\n\r\nReturns whether this is a (non-quantized) integer type.\r\n\r\n- - -\r\n\r\n#### `tf.DType.is_quantized` <a class=\"md-anchor\" id=\"DType.is_quantized\"></a>\r\n\r\nReturns whether this is a quantized data type.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.DType.as_numpy_dtype` <a class=\"md-anchor\" id=\"DType.as_numpy_dtype\"></a>\r\n\r\nReturns a `numpy.dtype` based on this `DType`.\r\n\r\n- - -\r\n\r\n#### `tf.DType.as_datatype_enum` <a class=\"md-anchor\" id=\"DType.as_datatype_enum\"></a>\r\n\r\nReturns a `types_pb2.DataType` enum value based on this `DType`.\r\n\r\n\r\n#### Other Methods <a class=\"md-anchor\" id=\"AUTOGENERATED-other-methods\"></a>\r\n- - -\r\n\r\n#### `tf.DType.__init__(type_enum)` <a class=\"md-anchor\" id=\"DType.__init__\"></a>\r\n\r\nCreates a new `DataType`.\r\n\r\nNOTE(mrry): In normal circumstances, you should not need to\r\nconstruct a DataType object directly. Instead, use the\r\ntypes.as_dtype() function.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`type_enum`</b>: A `types_pb2.DataType` enum value.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `type_enum` is not a value `types_pb2.DataType`.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.DType.max` <a class=\"md-anchor\" id=\"DType.max\"></a>\r\n\r\nReturns the maximum representable value in this data type.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: if this is a non-numeric, unordered, or quantized type.\r\n\r\n- - -\r\n\r\n#### `tf.DType.min` <a class=\"md-anchor\" id=\"DType.min\"></a>\r\n\r\nReturns the minimum representable value in this data type.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: if this is a non-numeric, unordered, or quantized type.\r\n\r\n\r\n- - -\r\n\r\n### `tf.as_dtype(type_value)` <a class=\"md-anchor\" id=\"as_dtype\"></a>\r\n\r\nConverts the given `type_value` to a `DType`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`type_value`</b>: A value that can be converted to a `tf.DType`\r\n    object. This may currently be a `tf.DType` object, a\r\n    [`DataType` enum](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/types.proto),\r\n    a string type name, or a `numpy.dtype`.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `DType` corresponding to `type_value`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `type_value` cannot be converted to a `DType`.\r\n\r\n\r\n\r\n## Utility functions <a class=\"md-anchor\" id=\"AUTOGENERATED-utility-functions\"></a>\r\n\r\n- - -\r\n\r\n### `tf.device(dev)` <a class=\"md-anchor\" id=\"device\"></a>\r\n\r\nWrapper for `Graph.device()` using the default graph.\r\n\r\nSee\r\n[`Graph.name_scope()`](../../api_docs/python/framework.md#Graph.name_scope)\r\nfor more details.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`device_name_or_function`</b>: The device name or function to use in\r\n    the context.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A context manager that specifies the default device to use for newly\r\n  created ops.\r\n\r\n\r\n- - -\r\n\r\n### `tf.name_scope(name)` <a class=\"md-anchor\" id=\"name_scope\"></a>\r\n\r\nWrapper for `Graph.name_scope()` using the default graph.\r\n\r\nSee\r\n[`Graph.name_scope()`](../../api_docs/python/framework.md#Graph.name_scope)\r\nfor more details.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the scope.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A context manager that installs `name` as a new name scope in the\r\n  default graph.\r\n\r\n\r\n- - -\r\n\r\n### `tf.control_dependencies(control_inputs)` <a class=\"md-anchor\" id=\"control_dependencies\"></a>\r\n\r\nWrapper for `Graph.control_dependencies()` using the default graph.\r\n\r\nSee [`Graph.control_dependencies()`](../../api_docs/python/framework.md#Graph.control_dependencies)\r\nfor more details.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`control_inputs`</b>: A list of `Operation` or `Tensor` objects, which\r\n    must be executed or computed before running the operations\r\n    defined in the context.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n A context manager that specifies control dependencies for all\r\n operations constructed within the context.\r\n\r\n\r\n- - -\r\n\r\n### `tf.convert_to_tensor(value, dtype=None, name=None)` <a class=\"md-anchor\" id=\"convert_to_tensor\"></a>\r\n\r\nConverts the given `value` to a `Tensor`.\r\n\r\nThis function converts Python objects of various types to `Tensor`\r\nobjects. It accepts `Tensor` objects, numpy arrays, Python lists,\r\nand Python scalars. For example:\r\n\r\n```python\r\nimport numpy as np\r\narray = np.random.rand((32, 100, 100))\r\n\r\ndef my_func(arg):\r\n  arg = tf.convert_to_tensor(arg, dtype=tf.float32)\r\n  return tf.matmul(arg, arg) + arg\r\n\r\n# The following calls are equivalent.\r\nvalue_1 = my_func(tf.constant([[1.0, 2.0], [3.0, 4.0]]))\r\nvalue_2 = my_func([[1.0, 2.0], [3.0, 4.0]])\r\nvalue_3 = my_func(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32))\r\n```\r\n\r\nThis function can be useful when composing a new operation in Python\r\n(such as `my_func` in the example above). All standard Python op\r\nconstructors apply this function to each of their Tensor-valued\r\ninputs, which allows those ops to accept numpy arrays, Python lists,\r\nand scalars in addition to `Tensor` objects.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`value`</b>: An object whose type has a registered `Tensor` conversion function.\r\n*  <b>`dtype`</b>: Optional element type for the returned tensor. If missing, the\r\n    type is inferred from the type of `value`.\r\n*  <b>`name`</b>: Optional name to use if a new `Tensor` is created.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` based on `value`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If no conversion function is registered for `value`.\r\n*  <b>`RuntimeError`</b>: If a registered conversion function returns an invalid value.\r\n\r\n\r\n- - -\r\n\r\n### `tf.get_default_graph()` <a class=\"md-anchor\" id=\"get_default_graph\"></a>\r\n\r\nReturns the default graph for the current thread.\r\n\r\nThe returned graph will be the innermost graph on which a\r\n`Graph.as_default()` context has been entered, or a global default\r\ngraph if none has been explicitly created.\r\n\r\n*N.B.* The default graph is a property of the current thread. If you\r\ncreate a new thread, and wish to use the default graph in that\r\nthread, you must explicitly add a `with g.as_default():` in that\r\nthread's function.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The default `Graph` being used in the current thread.\r\n\r\n\r\n- - -\r\n\r\n### `tf.import_graph_def(graph_def, input_map=None, return_elements=None, name=None, op_dict=None)` <a class=\"md-anchor\" id=\"import_graph_def\"></a>\r\n\r\nImports the TensorFlow graph in `graph_def` into the Python `Graph`.\r\n\r\nThis function provides a way to import a serialized TensorFlow\r\n[`GraphDef`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/graph.proto)\r\nprotocol buffer, and extract individual objects in the `GraphDef` as\r\n[`Tensor`](#Tensor) and [`Operation`](#Operation) objects. See\r\n[`Graph.as_graph_def()`](#Graph.as_graph_def) for a way to create a\r\n`GraphDef` proto.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`graph_def`</b>: A `GraphDef` proto containing operations to be imported into\r\n    the default graph.\r\n*  <b>`input_map`</b>: A dictionary mapping input names (as strings) in `graph_def`\r\n    to `Tensor` objects. The values of the named input tensors in the\r\n    imported graph will be re-mapped to the respective `Tensor` values.\r\n*  <b>`return_elements`</b>: A list of strings containing operation names in\r\n    `graph_def` that will be returned as `Operation` objects; and/or\r\n    tensor names in `graph_def` that will be returned as `Tensor` objects.\r\n*  <b>`name`</b>: (Optional.) A prefix that will be prepended to the names in\r\n    `graph_def`. Defaults to `\"import\"`.\r\n*  <b>`op_dict`</b>: (Optional.) A dictionary mapping op type names to `OpDef` protos.\r\n    Must contain an `OpDef` proto for each op type named in `graph_def`.\r\n    If omitted, uses the `OpDef` protos registered in the global registry.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of `Operation` and/or `Tensor` objects from the imported graph,\r\n  corresponding to the names in `return_elements'.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `graph_def` is not a `GraphDef` proto,\r\n    `input_map' is not a dictionary mapping strings to `Tensor` objects,\r\n    or `return_elements` is not a list of strings.\r\n*  <b>`ValueError`</b>: If `input_map`, or `return_elements` contains names that\r\n    do not appear in `graph_def`, or `graph_def` is not well-formed (e.g.\r\n    it refers to an unknown tensor).\r\n\r\n\r\n\r\n## Graph collections <a class=\"md-anchor\" id=\"AUTOGENERATED-graph-collections\"></a>\r\n\r\n- - -\r\n\r\n### `tf.add_to_collection(name, value)` <a class=\"md-anchor\" id=\"add_to_collection\"></a>\r\n\r\nWrapper for `Graph.add_to_collection()` using the default graph.\r\n\r\nSee [`Graph.add_to_collection()`](../../api_docs/python/framework.md#Graph.add_to_collection)\r\nfor more details.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: The key for the collection. For example, the `GraphKeys` class\r\n    contains many standard names for collections.\r\n*  <b>`value`</b>: The value to add to the collection.\r\n\r\n\r\n- - -\r\n\r\n### `tf.get_collection(key, scope=None)` <a class=\"md-anchor\" id=\"get_collection\"></a>\r\n\r\nWrapper for `Graph.get_collection()` using the default graph.\r\n\r\nSee [`Graph.get_collection()`](../../api_docs/python/framework.md#Graph.get_collection)\r\nfor more details.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`key`</b>: The key for the collection. For example, the `GraphKeys` class\r\n    contains many standard names for collections.\r\n*  <b>`scope`</b>: (Optional.) If supplied, the resulting list is filtered to include\r\n    only items whose name begins with this string.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The list of values in the collection with the given `name`, or\r\n  an empty list if no value has been added to that collection. The\r\n  list contains the values in the order under which they were\r\n  collected.\r\n\r\n\r\n- - -\r\n\r\n### `class tf.GraphKeys` <a class=\"md-anchor\" id=\"GraphKeys\"></a>\r\n\r\nStandard names to use for graph collections.\r\n\r\nThe standard library uses various well-known names to collect and\r\nretrieve values associated with a graph. For example, the\r\n`tf.Optimizer` subclasses default to optimizing the variables\r\ncollected under `tf.GraphKeys.TRAINABLE_VARIABLES` if none is\r\nspecified, but it is also possible to pass an explicit list of\r\nvariables.\r\n\r\nThe following standard keys are defined:\r\n\r\n* `VARIABLES`: the `Variable` objects that comprise a model, and\r\n  must be saved and restored together. See\r\n  [`tf.all_variables()`](../../api_docs/python/state_ops.md#all_variables)\r\n  for more details.\r\n* `TRAINABLE_VARIABLES`: the subset of `Variable` objects that will\r\n  be trained by an optimizer. See\r\n  [`tf.trainable_variables()`](../../api_docs/python/state_ops.md#trainable_variables)\r\n  for more details.\r\n* `SUMMARIES`: the summary `Tensor` objects that have been created in the\r\n  graph. See\r\n  [`tf.merge_all_summaries()`](../../api_docs/python/train.md#merge_all_summaries)\r\n  for more details.\r\n* `QUEUE_RUNNERS`: the `QueueRunner` objects that are used to\r\n  produce input for a computation. See\r\n  [`tf.start_queue_runners()`](../../api_docs/python/train.md#start_queue_runners)\r\n  for more details.\r\n\r\n\r\n## Defining new operations <a class=\"md-anchor\" id=\"AUTOGENERATED-defining-new-operations\"></a>\r\n\r\n- - -\r\n\r\n### `class tf.RegisterGradient` <a class=\"md-anchor\" id=\"RegisterGradient\"></a>\r\n\r\nA decorator for registering the gradient function for an op type.\r\n\r\nThis decorator is only used when defining a new op type. For an op\r\nwith `m` inputs and `n` inputs, the gradient function is a function\r\nthat takes the original `Operation` and `n` `Tensor` objects\r\n(representing the gradients with respect to each output of the op),\r\nand returns `m` `Tensor` objects (representing the partial gradients\r\nwith respect to each input of the op).\r\n\r\nFor example, assuming that operations of type `\"Sub\"` take two\r\ninputs `x` and `y`, and return a single output `x - y`, the\r\nfollowing gradient function would be registered:\r\n\r\n```python\r\n@tf.RegisterGradient(\"Sub\")\r\ndef _sub_grad(unused_op, grad):\r\n  return grad, tf.Neg(grad)\r\n```\r\n\r\nThe decorator argument `op_type` is the string type of an\r\noperation. This corresponds to the `OpDef.name` field for the proto\r\nthat defines the operation.\r\n\r\n- - -\r\n\r\n#### `tf.RegisterGradient.__init__(op_type)` <a class=\"md-anchor\" id=\"RegisterGradient.__init__\"></a>\r\n\r\nCreates a new decorator with `op_type` as the Operation type.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`op_type`</b>: The string type of an operation. This corresponds to the\r\n    `OpDef.name` field for the proto that defines the operation.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.NoGradient(op_type)` <a class=\"md-anchor\" id=\"NoGradient\"></a>\r\n\r\nSpecifies that ops of type `op_type` do not have a defined gradient.\r\n\r\nThis function is only used when defining a new op type. It may be\r\nused for ops such as `tf.size()` that are not differentiable.  For\r\nexample:\r\n\r\n```python\r\ntf.NoGradient(\"Size\")\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`op_type`</b>: The string type of an operation. This corresponds to the\r\n    `OpDef.name` field for the proto that defines the operation.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `op_type` is not a string.\r\n\r\n\r\n- - -\r\n\r\n### `class tf.RegisterShape` <a class=\"md-anchor\" id=\"RegisterShape\"></a>\r\n\r\nA decorator for registering the shape function for an op type.\r\n\r\nThis decorator is only used when defining a new op type. A shape\r\nfunction is a function from an `Operation` object to a list of\r\n`TensorShape` objects, with one `TensorShape` for each output of the\r\noperation.\r\n\r\nFor example, assuming that operations of type `\"Sub\"` take two\r\ninputs `x` and `y`, and return a single output `x - y`, all with the\r\nsame shape, the following shape function would be registered:\r\n\r\n```python\r\n@tf.RegisterShape(\"Sub\")\r\ndef _sub_shape(op):\r\n  return [op.inputs[0].get_shape().merge_with(op.inputs[1].get_shape())]\r\n```\r\n\r\nThe decorator argument `op_type` is the string type of an\r\noperation. This corresponds to the `OpDef.name` field for the proto\r\nthat defines the operation.\r\n- - -\r\n\r\n#### `tf.RegisterShape.__init__(op_type)` <a class=\"md-anchor\" id=\"RegisterShape.__init__\"></a>\r\n\r\nSaves the \"op_type\" as the Operation type.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.TensorShape` <a class=\"md-anchor\" id=\"TensorShape\"></a>\r\n\r\nRepresents the shape of a `Tensor`.\r\n\r\nA `TensorShape` represents a possibly-partial shape specification for a\r\n`Tensor`. It may be one of the following:\r\n\r\n* *Fully-known shape:* has a known number of dimensions and a known size\r\n  for each dimension.\r\n* *Partially-known shape:* has a known number of dimensions, and an unknown\r\n  size for one or more dimension.\r\n* *Unknown shape:* has an unknown number of dimensions, and an unknown\r\n  size in all dimensions.\r\n\r\nIf a tensor is produced by an operation of type `\"Foo\"`, its shape\r\nmay be inferred if there is a registered shape function for\r\n`\"Foo\"`. See [`tf.RegisterShape()`](../../api_docs/python/framework.md#RegisterShape)\r\nfor details of shape\r\nfunctions and how to register them. Alternatively, the shape may be set\r\nexplicitly using [`Tensor.set_shape()`](../../api_docs/python/framework.md#Tensor.set_shape).\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.merge_with(other)` <a class=\"md-anchor\" id=\"TensorShape.merge_with\"></a>\r\n\r\nReturns a `TensorShape` combining the information in `self` and `other`.\r\n\r\nThe dimensions in `self` and `other` are merged elementwise,\r\naccording to the rules defined for `Dimension.merge_with()`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`other`</b>: Another `TensorShape`.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `TensorShape` containing the combined information of `self` and\r\n  `other`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `self` and `other` are not compatible.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.concatenate(other)` <a class=\"md-anchor\" id=\"TensorShape.concatenate\"></a>\r\n\r\nReturns the concatenation of the dimension in `self` and `other`.\r\n\r\n*N.B.* If either `self` or `other` is completely unknown,\r\nconcatenation will discard information about the other shape. In\r\nfuture, we might support concatenation that preserves this\r\ninformation for use with slicing.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`other`</b>: Another `TensorShape`.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `TensorShape` whose dimensions are the concatenation of the\r\n  dimensions in `self` and `other`.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.ndims` <a class=\"md-anchor\" id=\"TensorShape.ndims\"></a>\r\n\r\nReturns the rank of this shape, or None if it is unspecified.\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.dims` <a class=\"md-anchor\" id=\"TensorShape.dims\"></a>\r\n\r\nReturns a list of Dimensions, or None if the shape is unspecified.\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.as_list()` <a class=\"md-anchor\" id=\"TensorShape.as_list\"></a>\r\n\r\nReturns a list of integers or None for each dimension.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.is_compatible_with(other)` <a class=\"md-anchor\" id=\"TensorShape.is_compatible_with\"></a>\r\n\r\nReturns True iff `self` is compatible with `other`.\r\n\r\nTwo possibly-partially-defined shapes are compatible if there\r\nexists a fully-defined shape that both shapes can represent. Thus,\r\ncompatibility allows the shape inference code to reason about\r\npartially-defined shapes. For example:\r\n\r\n* TensorShape(None) is compatible with all shapes.\r\n\r\n* TensorShape([None, None]) is compatible with all two-dimensional\r\n  shapes, such as TensorShape([32, 784]), and also TensorShape(None). It is\r\n  not compatible with, for example, TensorShape([None]) or\r\n  TensorShape([None, None, None]).\r\n\r\n* TensorShape([32, None]) is compatible with all two-dimensional shapes\r\n  with size 32 in the 0th dimension, and also TensorShape([None, None])\r\n  and TensorShape(None). It is not compatible with, for example,\r\n  TensorShape([32]), TensorShape([32, None, 1]) or TensorShape([64, None]).\r\n\r\n* TensorShape([32, 784]) is compatible with itself, and also\r\n  TensorShape([32, None]), TensorShape([None, 784]), TensorShape([None,\r\n  None]) and TensorShape(None). It is not compatible with, for example,\r\n  TensorShape([32, 1, 784]) or TensorShape([None]).\r\n\r\nThe compatibility relation is reflexive and symmetric, but not\r\ntransitive. For example, TensorShape([32, 784]) is compatible with\r\nTensorShape(None), and TensorShape(None) is compatible with\r\nTensorShape([4, 4]), but TensorShape([32, 784]) is not compatible with\r\nTensorShape([4, 4]).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`other`</b>: Another TensorShape.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  True iff `self` is compatible with `other`.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.is_fully_defined()` <a class=\"md-anchor\" id=\"TensorShape.is_fully_defined\"></a>\r\n\r\nReturns True iff `self` is fully defined in every dimension.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.with_rank(rank)` <a class=\"md-anchor\" id=\"TensorShape.with_rank\"></a>\r\n\r\nReturns a shape based on `self` with the given rank.\r\n\r\nThis method promotes a completely unknown shape to one with a\r\nknown rank.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`rank`</b>: An integer.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A shape that is at least as specific as `self` with the given rank.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `self` does not represent a shape with the given `rank`.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.with_rank_at_least(rank)` <a class=\"md-anchor\" id=\"TensorShape.with_rank_at_least\"></a>\r\n\r\nReturns a shape based on `self` with at least the given rank.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`rank`</b>: An integer.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A shape that is at least as specific as `self` with at least the given\r\n  rank.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `self` does not represent a shape with at least the given\r\n    `rank`.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.with_rank_at_most(rank)` <a class=\"md-anchor\" id=\"TensorShape.with_rank_at_most\"></a>\r\n\r\nReturns a shape based on `self` with at most the given rank.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`rank`</b>: An integer.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A shape that is at least as specific as `self` with at most the given\r\n  rank.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `self` does not represent a shape with at most the given\r\n    `rank`.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.assert_has_rank(rank)` <a class=\"md-anchor\" id=\"TensorShape.assert_has_rank\"></a>\r\n\r\nRaises an exception if `self` is not compatible with the given `rank`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`rank`</b>: An integer.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `self` does not represent a shape with the given `rank`.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.assert_same_rank(other)` <a class=\"md-anchor\" id=\"TensorShape.assert_same_rank\"></a>\r\n\r\nRaises an exception if `self` and `other` do not have compatible ranks.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`other`</b>: Another `TensorShape`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `self` and `other` do not represent shapes with the\r\n    same rank.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.assert_is_compatible_with(other)` <a class=\"md-anchor\" id=\"TensorShape.assert_is_compatible_with\"></a>\r\n\r\nRaises exception if `self` and `other` do not represent the same shape.\r\n\r\nThis method can be used to assert that there exists a shape that both\r\n`self` and `other` represent.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`other`</b>: Another TensorShape.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `self` and `other` do not represent the same shape.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.assert_is_fully_defined()` <a class=\"md-anchor\" id=\"TensorShape.assert_is_fully_defined\"></a>\r\n\r\nRaises an exception if `self` is not fully defined in every dimension.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `self` does not have a known value for every dimension.\r\n\r\n\r\n\r\n#### Other Methods <a class=\"md-anchor\" id=\"AUTOGENERATED-other-methods\"></a>\r\n- - -\r\n\r\n#### `tf.TensorShape.__init__(dims)` <a class=\"md-anchor\" id=\"TensorShape.__init__\"></a>\r\n\r\nCreates a new TensorShape with the given dimensions.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`dims`</b>: A list of Dimensions, or None if the shape is unspecified.\r\n*  <b>`DEPRECATED`</b>: A single integer is treated as a singleton list.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.as_dimension_list()` <a class=\"md-anchor\" id=\"TensorShape.as_dimension_list\"></a>\r\n\r\nDEPRECATED: use as_list().\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TensorShape.num_elements()` <a class=\"md-anchor\" id=\"TensorShape.num_elements\"></a>\r\n\r\nReturns the total number of elements, or none for incomplete shapes.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.Dimension` <a class=\"md-anchor\" id=\"Dimension\"></a>\r\n\r\nRepresents the value of one dimension in a TensorShape.\r\n- - -\r\n\r\n#### `tf.Dimension.__init__(value)` <a class=\"md-anchor\" id=\"Dimension.__init__\"></a>\r\n\r\nCreates a new Dimension with the given value.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Dimension.assert_is_compatible_with(other)` <a class=\"md-anchor\" id=\"Dimension.assert_is_compatible_with\"></a>\r\n\r\nRaises an exception if `other` is not compatible with this Dimension.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`other`</b>: Another Dimension.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `self` and `other` are not compatible (see\r\n    is_compatible_with).\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Dimension.is_compatible_with(other)` <a class=\"md-anchor\" id=\"Dimension.is_compatible_with\"></a>\r\n\r\nReturns true if `other` is compatible with this Dimension.\r\n\r\nTwo known Dimensions are compatible if they have the same value.\r\nAn unknown Dimension is compatible with all other Dimensions.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`other`</b>: Another Dimension.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  True if this Dimension and `other` are compatible.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Dimension.merge_with(other)` <a class=\"md-anchor\" id=\"Dimension.merge_with\"></a>\r\n\r\nReturns a Dimension that combines the information in `self` and `other`.\r\n\r\nDimensions are combined as follows:\r\n\r\n  Dimension(n)   .merge_with(Dimension(n))    == Dimension(n)\r\n  Dimension(n)   .merge_with(Dimension(None)) == Dimension(n)\r\n  Dimension(None).merge_with(Dimension(n))    == Dimension(n)\r\n  Dimension(None).merge_with(Dimension(None)) == Dimension(None)\r\n  Dimension(n)   .merge_with(Dimension(m)) raises ValueError for n != m\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`other`</b>: Another Dimension.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A Dimension containing the combined information of `self` and\r\n  `other`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `self` and `other` are not compatible (see\r\n    is_compatible_with).\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Dimension.value` <a class=\"md-anchor\" id=\"Dimension.value\"></a>\r\n\r\nThe value of this dimension, or None if it is unknown.\r\n\r\n\r\n- - -\r\n\r\n### `tf.op_scope(values, name, default_name)` <a class=\"md-anchor\" id=\"op_scope\"></a>\r\n\r\nReturns a context manager for use when defining a Python op.\r\n\r\nThis context manager validates that the given `values` are from the\r\nsame graph, ensures that that graph is the default graph, and pushes a\r\nname scope.\r\n\r\nFor example, to define a new Python op called `my_op`:\r\n\r\n```python\r\ndef my_op(a, b, c, name=None):\r\n  with tf.op_scope([a, b, c], name, \"MyOp\") as scope:\r\n    a = tf.convert_to_tensor(a, name=\"a\")\r\n    b = tf.convert_to_tensor(b, name=\"b\")\r\n    c = tf.convert_to_tensor(c, name=\"c\")\r\n    # Define some computation that uses `a`, `b`, and `c`.\r\n    return foo_op(..., name=scope)\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`values`</b>: The list of `Tensor` arguments that are passed to the op function.\r\n*  <b>`name`</b>: The name argument that is passed to the op function.\r\n*  <b>`default_name`</b>: The default name to use if the `name` argument is `None`.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A context manager for use in defining a Python op.\r\n\r\n\r\n- - -\r\n\r\n### `tf.get_seed(op_seed)` <a class=\"md-anchor\" id=\"get_seed\"></a>\r\n\r\nReturns the local seeds an operation should use given an op-specific seed.\r\n\r\nGiven operation-specific seed, `op_seed`, this helper function returns two\r\nseeds derived from graph-level and op-level seeds. Many random operations\r\ninternally use the two seeds to allow user to change the seed globally for a\r\ngraph, or for only specific operations.\r\n\r\nFor details on how the graph-level seed interacts with op seeds, see\r\n[`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`op_seed`</b>: integer.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tuple of two integers that should be used for the local seed of this\r\n  operation.\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/python/image.md",
    "content": "<!-- This file is machine generated: DO NOT EDIT! -->\r\n\r\n# Images <a class=\"md-anchor\" id=\"AUTOGENERATED-images\"></a>\r\n\r\nNote: Functions taking `Tensor` arguments can also take anything accepted by\r\n[`tf.convert_to_tensor`](../../api_docs/python/framework.md#convert_to_tensor).\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Images](#AUTOGENERATED-images)\r\n* [Encoding and Decoding](#AUTOGENERATED-encoding-and-decoding)\r\n  * [`tf.image.decode_jpeg(contents, channels=None, ratio=None, fancy_upscaling=None, try_recover_truncated=None, acceptable_fraction=None, name=None)`](#decode_jpeg)\r\n  * [`tf.image.encode_jpeg(image, format=None, quality=None, progressive=None, optimize_size=None, chroma_downsampling=None, density_unit=None, x_density=None, y_density=None, xmp_metadata=None, name=None)`](#encode_jpeg)\r\n  * [`tf.image.decode_png(contents, channels=None, name=None)`](#decode_png)\r\n  * [`tf.image.encode_png(image, compression=None, name=None)`](#encode_png)\r\n* [Resizing](#AUTOGENERATED-resizing)\r\n  * [`tf.image.resize_images(images, new_height, new_width, method=0)`](#resize_images)\r\n  * [`tf.image.resize_area(images, size, name=None)`](#resize_area)\r\n  * [`tf.image.resize_bicubic(images, size, name=None)`](#resize_bicubic)\r\n  * [`tf.image.resize_bilinear(images, size, name=None)`](#resize_bilinear)\r\n  * [`tf.image.resize_nearest_neighbor(images, size, name=None)`](#resize_nearest_neighbor)\r\n* [Cropping](#AUTOGENERATED-cropping)\r\n  * [`tf.image.resize_image_with_crop_or_pad(image, target_height, target_width)`](#resize_image_with_crop_or_pad)\r\n  * [`tf.image.pad_to_bounding_box(image, offset_height, offset_width, target_height, target_width)`](#pad_to_bounding_box)\r\n  * [`tf.image.crop_to_bounding_box(image, offset_height, offset_width, target_height, target_width)`](#crop_to_bounding_box)\r\n  * [`tf.image.random_crop(image, size, seed=None, name=None)`](#random_crop)\r\n  * [`tf.image.extract_glimpse(input, size, offsets, centered=None, normalized=None, uniform_noise=None, name=None)`](#extract_glimpse)\r\n* [Flipping and Transposing](#AUTOGENERATED-flipping-and-transposing)\r\n  * [`tf.image.flip_up_down(image)`](#flip_up_down)\r\n  * [`tf.image.random_flip_up_down(image, seed=None)`](#random_flip_up_down)\r\n  * [`tf.image.flip_left_right(image)`](#flip_left_right)\r\n  * [`tf.image.random_flip_left_right(image, seed=None)`](#random_flip_left_right)\r\n  * [`tf.image.transpose_image(image)`](#transpose_image)\r\n* [Image Adjustments](#AUTOGENERATED-image-adjustments)\r\n  * [`tf.image.adjust_brightness(image, delta, min_value=None, max_value=None)`](#adjust_brightness)\r\n  * [`tf.image.random_brightness(image, max_delta, seed=None)`](#random_brightness)\r\n  * [`tf.image.adjust_contrast(images, contrast_factor, min_value=None, max_value=None)`](#adjust_contrast)\r\n  * [`tf.image.random_contrast(image, lower, upper, seed=None)`](#random_contrast)\r\n  * [`tf.image.per_image_whitening(image)`](#per_image_whitening)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## Encoding and Decoding <a class=\"md-anchor\" id=\"AUTOGENERATED-encoding-and-decoding\"></a>\r\n\r\nTensorFlow provides Ops to decode and encode JPEG and PNG formats.  Encoded\r\nimages are represented by scalar string Tensors, decoded images by 3-D uint8\r\ntensors of shape `[height, width, channels]`.\r\n\r\nThe encode and decode Ops apply to one image at a time.  Their input and output\r\nare all of variable size.  If you need fixed size images, pass the output of\r\nthe decode Ops to one of the cropping and resizing Ops.\r\n\r\nNote: The PNG encode and decode Ops support RGBA, but the conversions Ops\r\npresently only support RGB, HSV, and GrayScale.\r\n\r\n- - -\r\n\r\n### `tf.image.decode_jpeg(contents, channels=None, ratio=None, fancy_upscaling=None, try_recover_truncated=None, acceptable_fraction=None, name=None)` <a class=\"md-anchor\" id=\"decode_jpeg\"></a>\r\n\r\nDecode a JPEG-encoded image to a uint8 tensor.\r\n\r\nThe attr `channels` indicates the desired number of color channels for the\r\ndecoded image.\r\n\r\nAccepted values are:\r\n\r\n*   0: Use the number of channels in the JPEG-encoded image.\r\n*   1: output a grayscale image.\r\n*   3: output an RGB image.\r\n\r\nIf needed, the JPEG-encoded image is transformed to match the requested number\r\nof color channels.\r\n\r\nThe attr `ratio` allows downscaling the image by an integer factor during\r\ndecoding.  Allowed values are: 1, 2, 4, and 8.  This is much faster than\r\ndownscaling the image later.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`contents`</b>: A `Tensor` of type `string`. 0-D.  The JPEG-encoded image.\r\n*  <b>`channels`</b>: An optional `int`. Defaults to `0`.\r\n    Number of color channels for the decoded image.\r\n*  <b>`ratio`</b>: An optional `int`. Defaults to `1`. Downscaling ratio.\r\n*  <b>`fancy_upscaling`</b>: An optional `bool`. Defaults to `True`.\r\n    If true use a slower but nicer upscaling of the\r\n    chroma planes (yuv420/422 only).\r\n*  <b>`try_recover_truncated`</b>: An optional `bool`. Defaults to `False`.\r\n    If true try to recover an image from truncated input.\r\n*  <b>`acceptable_fraction`</b>: An optional `float`. Defaults to `1`.\r\n    The minimum required fraction of lines before a truncated\r\n    input is accepted.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `uint8`. 3-D with shape `[height, width, channels]`..\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.encode_jpeg(image, format=None, quality=None, progressive=None, optimize_size=None, chroma_downsampling=None, density_unit=None, x_density=None, y_density=None, xmp_metadata=None, name=None)` <a class=\"md-anchor\" id=\"encode_jpeg\"></a>\r\n\r\nJPEG-encode an image.\r\n\r\n`image` is a 3-D uint8 Tensor of shape `[height, width, channels]`.\r\n\r\nThe attr `format` can be used to override the color format of the encoded\r\noutput.  Values can be:\r\n\r\n*   `''`: Use a default format based on the number of channels in the image.\r\n*   `grayscale`: Output a grayscale JPEG image.  The `channels` dimension\r\n    of `image` must be 1.\r\n*   `rgb`: Output an RGB JPEG image. The `channels` dimension\r\n    of `image` must be 3.\r\n\r\nIf `format` is not specified or is the empty string, a default format is picked\r\nin function of the number of channels in `image`:\r\n\r\n*   1: Output a grayscale image.\r\n*   3: Output an RGB image.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: A `Tensor` of type `uint8`.\r\n    3-D with shape `[height, width, channels]`.\r\n*  <b>`format`</b>: An optional `string` from: `\"\", \"grayscale\", \"rgb\"`. Defaults to `\"\"`.\r\n    Per pixel image format.\r\n*  <b>`quality`</b>: An optional `int`. Defaults to `95`.\r\n    Quality of the compression from 0 to 100 (higher is better and slower).\r\n*  <b>`progressive`</b>: An optional `bool`. Defaults to `False`.\r\n    If True, create a JPEG that loads progressively (coarse to fine).\r\n*  <b>`optimize_size`</b>: An optional `bool`. Defaults to `False`.\r\n    If True, spend CPU/RAM to reduce size with no quality change.\r\n*  <b>`chroma_downsampling`</b>: An optional `bool`. Defaults to `True`.\r\n    See http://en.wikipedia.org/wiki/Chroma_subsampling.\r\n*  <b>`density_unit`</b>: An optional `string` from: `\"in\", \"cm\"`. Defaults to `\"in\"`.\r\n    Unit used to specify `x_density` and `y_density`:\r\n    pixels per inch (`'in'`) or centimeter (`'cm'`).\r\n*  <b>`x_density`</b>: An optional `int`. Defaults to `300`.\r\n    Horizontal pixels per density unit.\r\n*  <b>`y_density`</b>: An optional `int`. Defaults to `300`.\r\n    Vertical pixels per density unit.\r\n*  <b>`xmp_metadata`</b>: An optional `string`. Defaults to `\"\"`.\r\n    If not empty, embed this XMP metadata in the image header.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `string`. 0-D. JPEG-encoded image.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.decode_png(contents, channels=None, name=None)` <a class=\"md-anchor\" id=\"decode_png\"></a>\r\n\r\nDecode a PNG-encoded image to a uint8 tensor.\r\n\r\nThe attr `channels` indicates the desired number of color channels for the\r\ndecoded image.\r\n\r\nAccepted values are:\r\n\r\n*   0: Use the number of channels in the PNG-encoded image.\r\n*   1: output a grayscale image.\r\n*   3: output an RGB image.\r\n*   4: output an RGBA image.\r\n\r\nIf needed, the PNG-encoded image is transformed to match the requested number\r\nof color channels.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`contents`</b>: A `Tensor` of type `string`. 0-D.  The PNG-encoded image.\r\n*  <b>`channels`</b>: An optional `int`. Defaults to `0`.\r\n    Number of color channels for the decoded image.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `uint8`. 3-D with shape `[height, width, channels]`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.encode_png(image, compression=None, name=None)` <a class=\"md-anchor\" id=\"encode_png\"></a>\r\n\r\nPNG-encode an image.\r\n\r\n`image` is a 3-D uint8 Tensor of shape `[height, width, channels]` where\r\n`channels` is:\r\n\r\n*   1: for grayscale.\r\n*   3: for RGB.\r\n*   4: for RGBA.\r\n\r\nThe ZLIB compression level, `compression`, can be -1 for the PNG-encoder\r\ndefault or a value from 0 to 9.  9 is the highest compression level, generating\r\nthe smallest output, but is slower.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: A `Tensor` of type `uint8`.\r\n    3-D with shape `[height, width, channels]`.\r\n*  <b>`compression`</b>: An optional `int`. Defaults to `-1`. Compression level.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `string`. 0-D. PNG-encoded image.\r\n\r\n\r\n\r\n## Resizing <a class=\"md-anchor\" id=\"AUTOGENERATED-resizing\"></a>\r\n\r\nThe resizing Ops accept input images as tensors of several types.  They always\r\noutput resized images as float32 tensors.\r\n\r\nThe convenience function [resize_images()](#resize_images) supports both 4-D\r\nand 3-D tensors as input and output.  4-D tensors are for batches of images,\r\n3-D tensors for individual images.\r\n\r\nOther resizing Ops only support 3-D individual images as input:\r\n[resize_area](#resize_area), [resize_bicubic](#resize_bicubic),\r\n[resize_bilinear](#resize_bilinear),\r\n[resize_nearest_neighbor](#resize_nearest_neighbor).\r\n\r\nExample:\r\n\r\n```python\r\n# Decode a JPG image and resize it to 299 by 299.\r\nimage = tf.image.decode_jpeg(...)\r\nresized_image = tf.image.resize_bilinear(image, [299, 299])\r\n```\r\n\r\n<i>Maybe refer to the Queue examples that show how to add images to a Queue\r\nafter resizing them to a fixed size, and how to dequeue batches of resized\r\nimages from the Queue.</i>\r\n\r\n- - -\r\n\r\n### `tf.image.resize_images(images, new_height, new_width, method=0)` <a class=\"md-anchor\" id=\"resize_images\"></a>\r\n\r\nResize `images` to `new_width`, `new_height` using the specified `method`.\r\n\r\nResized images will be distorted if their original aspect ratio is not\r\nthe same as `new_width`, `new_height`.  To avoid distortions see\r\n[resize_image_with_crop_or_pad](#resize_image_with_crop_or_pad).\r\n\r\n`method` can be one of:\r\n\r\n*   <b>ResizeMethod.BILINEAR</b>: [Bilinear interpolation.]\r\n    (https://en.wikipedia.org/wiki/Bilinear_interpolation)\r\n*   <b>ResizeMethod.NEAREST_NEIGHBOR</b>: [Nearest neighbor interpolation.]\r\n    (https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation)\r\n*   <b>ResizeMethod.BICUBIC</b>: [Bicubic interpolation.]\r\n    (https://en.wikipedia.org/wiki/Bicubic_interpolation)\r\n*   <b>ResizeMethod.AREA</b>: Area interpolation.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`images`</b>: 4-D Tensor of shape `[batch, height, width, channels]` or\r\n          3-D Tensor of shape `[height, width, channels]`.\r\n*  <b>`new_height`</b>: integer.\r\n*  <b>`new_width`</b>: integer.\r\n*  <b>`method`</b>: ResizeMethod.  Defaults to `ResizeMethod.BILINEAR`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if the shape of `images` is incompatible with the\r\n    shape arguments to this function\r\n*  <b>`ValueError`</b>: if an unsupported resize method is specified.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  If `images` was 4-D, a 4-D float Tensor of shape\r\n  `[batch, new_height, new_width, channels]`.\r\n  If `images` was 3-D, a 3-D float Tensor of shape\r\n  `[new_height, new_width, channels]`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.resize_area(images, size, name=None)` <a class=\"md-anchor\" id=\"resize_area\"></a>\r\n\r\nResize `images` to `size` using area interpolation.\r\n\r\nInput images can be of different types but output images are always float.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`images`</b>: A `Tensor`. Must be one of the following types: `uint8`, `int8`, `int32`, `float32`, `float64`.\r\n    4-D with shape `[batch, height, width, channels]`.\r\n*  <b>`size`</b>: A 1-D int32 Tensor of 2 elements: `new_height, new_width`.  The\r\n    new size for the images.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `float32`. 4-D with shape\r\n  `[batch, new_height, new_width, channels]`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.resize_bicubic(images, size, name=None)` <a class=\"md-anchor\" id=\"resize_bicubic\"></a>\r\n\r\nResize `images` to `size` using bicubic interpolation.\r\n\r\nInput images can be of different types but output images are always float.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`images`</b>: A `Tensor`. Must be one of the following types: `uint8`, `int8`, `int32`, `float32`, `float64`.\r\n    4-D with shape `[batch, height, width, channels]`.\r\n*  <b>`size`</b>: A 1-D int32 Tensor of 2 elements: `new_height, new_width`.  The\r\n    new size for the images.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `float32`. 4-D with shape\r\n  `[batch, new_height, new_width, channels]`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.resize_bilinear(images, size, name=None)` <a class=\"md-anchor\" id=\"resize_bilinear\"></a>\r\n\r\nResize `images` to `size` using bilinear interpolation.\r\n\r\nInput images can be of different types but output images are always float.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`images`</b>: A `Tensor`. Must be one of the following types: `uint8`, `int8`, `int32`, `float32`, `float64`.\r\n    4-D with shape `[batch, height, width, channels]`.\r\n*  <b>`size`</b>: A 1-D int32 Tensor of 2 elements: `new_height, new_width`.  The\r\n    new size for the images.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `float32`. 4-D with shape\r\n  `[batch, new_height, new_width, channels]`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.resize_nearest_neighbor(images, size, name=None)` <a class=\"md-anchor\" id=\"resize_nearest_neighbor\"></a>\r\n\r\nResize `images` to `size` using nearest neighbor interpolation.\r\n\r\nInput images can be of different types but output images are always float.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`images`</b>: A `Tensor`. Must be one of the following types: `uint8`, `int8`, `int32`, `float32`, `float64`.\r\n    4-D with shape `[batch, height, width, channels]`.\r\n*  <b>`size`</b>: A 1-D int32 Tensor of 2 elements: `new_height, new_width`.  The\r\n    new size for the images.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `images`. 4-D with shape\r\n  `[batch, new_height, new_width, channels]`.\r\n\r\n\r\n\r\n\r\n## Cropping <a class=\"md-anchor\" id=\"AUTOGENERATED-cropping\"></a>\r\n\r\n- - -\r\n\r\n### `tf.image.resize_image_with_crop_or_pad(image, target_height, target_width)` <a class=\"md-anchor\" id=\"resize_image_with_crop_or_pad\"></a>\r\n\r\nCrops and/or pads an image to a target width and height.\r\n\r\nResizes an image to a target width and height by either centrally\r\ncropping the image or padding it evenly with zeros.\r\n\r\nIf `width` or `height` is greater than the specified `target_width` or\r\n`target_height` respectively, this op centrally crops along that dimension.\r\nIf `width` or `height` is smaller than the specified `target_width` or\r\n`target_height` respectively, this op centrally pads with 0 along that\r\ndimension.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: 3-D tensor of shape [height, width, channels]\r\n*  <b>`target_height`</b>: Target height.\r\n*  <b>`target_width`</b>: Target width.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if `target_height` or `target_width` are zero or negative.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  Cropped and/or padded image of shape\r\n  `[target_height, target_width, channels]`\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.pad_to_bounding_box(image, offset_height, offset_width, target_height, target_width)` <a class=\"md-anchor\" id=\"pad_to_bounding_box\"></a>\r\n\r\nPad `image` with zeros to the specified `height` and `width`.\r\n\r\nAdds `offset_height` rows of zeros on top, `offset_width` columns of\r\nzeros on the left, and then pads the image on the bottom and right\r\nwith zeros until it has dimensions `target_height`, `target_width`.\r\n\r\nThis op does nothing if `offset_*` is zero and the image already has size\r\n`target_height` by `target_width`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: 3-D tensor with shape `[height, width, channels]`\r\n*  <b>`offset_height`</b>: Number of rows of zeros to add on top.\r\n*  <b>`offset_width`</b>: Number of columns of zeros to add on the left.\r\n*  <b>`target_height`</b>: Height of output image.\r\n*  <b>`target_width`</b>: Width of output image.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  3-D tensor of shape `[target_height, target_width, channels]`\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If the shape of `image` is incompatible with the `offset_*` or\r\n    `target_*` arguments\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.crop_to_bounding_box(image, offset_height, offset_width, target_height, target_width)` <a class=\"md-anchor\" id=\"crop_to_bounding_box\"></a>\r\n\r\nCrops an image to a specified bounding box.\r\n\r\nThis op cuts a rectangular part out of `image`. The top-left corner of the\r\nreturned image is at `offset_height, offset_width` in `image`, and its\r\nlower-right corner is at\r\n`offset_height + target_height, offset_width + target_width'.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: 3-D tensor with shape `[height, width, channels]`\r\n*  <b>`offset_height`</b>: Vertical coordinate of the top-left corner of the result in\r\n                 the input.\r\n*  <b>`offset_width`</b>: Horizontal coordinate of the top-left corner of the result in\r\n                the input.\r\n*  <b>`target_height`</b>: Height of the result.\r\n*  <b>`target_width`</b>: Width of the result.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  3-D tensor of image with shape `[target_height, target_width, channels]`\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If the shape of `image` is incompatible with the `offset_*` or\r\n  `target_*` arguments\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.random_crop(image, size, seed=None, name=None)` <a class=\"md-anchor\" id=\"random_crop\"></a>\r\n\r\nRandomly crops `image` to size `[target_height, target_width]`.\r\n\r\nThe offset of the output within `image` is uniformly random. `image` always\r\nfully contains the result.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: 3-D tensor of shape `[height, width, channels]`\r\n*  <b>`size`</b>: 1-D tensor with two elements, specifying target `[height, width]`\r\n*  <b>`seed`</b>: A Python integer. Used to create a random seed. See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n*  <b>`name`</b>: A name for this operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A cropped 3-D tensor of shape `[target_height, target_width, channels]`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.extract_glimpse(input, size, offsets, centered=None, normalized=None, uniform_noise=None, name=None)` <a class=\"md-anchor\" id=\"extract_glimpse\"></a>\r\n\r\nExtracts a glimpse from the input tensor.\r\n\r\nReturns a set of windows called glimpses extracted at location `offsets`\r\nfrom the input tensor. If the windows only partially overlaps the inputs, the\r\nnon overlapping areas will be filled with random noise.\r\n\r\nThe result is a 4-D tensor of shape `[batch_size, glimpse_height,\r\nglimpse_width, channels]`. The channels and batch dimensions are the same as that\r\nof the input tensor. The height and width of the output windows are\r\nspecified in the `size` parameter.\r\n\r\nThe argument `normalized` and `centered` controls how the windows are built:\r\n* If the coordinates are normalized but not centered, 0.0 and 1.0\r\n  correspond to the minimum and maximum of each height and width dimension.\r\n* If the coordinates are both normalized and centered, they range from -1.0 to\r\n  1.0. The coordinates (-1.0, -1.0) correspond to the upper left corner, the\r\n  lower right corner is located at  (1.0, 1.0) and the center is at (0, 0).\r\n* If the coordinates are not normalized they are interpreted as numbers of pixels.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor` of type `float32`.\r\n    A 4-D float tensor of shape `[batch_size, height, width, channels]`.\r\n*  <b>`size`</b>: A `Tensor` of type `int32`.\r\n    A 1-D tensor of 2 elements containing the size of the glimpses to extract.\r\n    The glimpse height must be specified first, following by the glimpse width.\r\n*  <b>`offsets`</b>: A `Tensor` of type `float32`.\r\n    A 2-D integer tensor of shape `[batch_size, 2]` containing the x, y\r\n    locations of the center of each window.\r\n*  <b>`centered`</b>: An optional `bool`. Defaults to `True`.\r\n    indicates if the offset coordinates are centered relative to\r\n    the image, in which case the (0, 0) offset is relative to the center of the\r\n    input images. If false, the (0,0) offset corresponds to the upper left corner\r\n    of the input images.\r\n*  <b>`normalized`</b>: An optional `bool`. Defaults to `True`.\r\n    indicates if the offset coordinates are normalized.\r\n*  <b>`uniform_noise`</b>: An optional `bool`. Defaults to `True`.\r\n    indicates if the noise should be generated using a\r\n    uniform distribution or a gaussian distribution.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `float32`.\r\n  A tensor representing the glimpses `[batch_size, glimpse_height,\r\n  glimpse_width, channels]`.\r\n\r\n\r\n\r\n## Flipping and Transposing <a class=\"md-anchor\" id=\"AUTOGENERATED-flipping-and-transposing\"></a>\r\n\r\n- - -\r\n\r\n### `tf.image.flip_up_down(image)` <a class=\"md-anchor\" id=\"flip_up_down\"></a>\r\n\r\nFlip an image horizontally (upside down).\r\n\r\nOutputs the contents of `image` flipped along the first dimension, which is\r\n`height`.\r\n\r\nSee also `reverse()`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: A 3-D tensor of shape `[height, width, channels].`\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A 3-D tensor of the same type and shape as `image`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if the shape of `image` not supported.\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.random_flip_up_down(image, seed=None)` <a class=\"md-anchor\" id=\"random_flip_up_down\"></a>\r\n\r\nRandomly flips an image vertically (upside down).\r\n\r\nWith a 1 in 2 chance, outputs the contents of `image` flipped along the first\r\ndimension, which is `height`.  Otherwise output the image as-is.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: A 3-D tensor of shape `[height, width, channels].`\r\n*  <b>`seed`</b>: A Python integer. Used to create a random seed. See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A 3-D tensor of the same type and shape as `image`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if the shape of `image` not supported.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.flip_left_right(image)` <a class=\"md-anchor\" id=\"flip_left_right\"></a>\r\n\r\nFlip an image horizontally (left to right).\r\n\r\nOutputs the contents of `image` flipped along the second dimension, which is\r\n`width`.\r\n\r\nSee also `reverse()`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: A 3-D tensor of shape `[height, width, channels].`\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A 3-D tensor of the same type and shape as `image`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if the shape of `image` not supported.\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.random_flip_left_right(image, seed=None)` <a class=\"md-anchor\" id=\"random_flip_left_right\"></a>\r\n\r\nRandomly flip an image horizontally (left to right).\r\n\r\nWith a 1 in 2 chance, outputs the contents of `image` flipped along the\r\nsecond dimension, which is `width`.  Otherwise output the image as-is.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: A 3-D tensor of shape `[height, width, channels].`\r\n*  <b>`seed`</b>: A Python integer. Used to create a random seed. See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A 3-D tensor of the same type and shape as `image`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if the shape of `image` not supported.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.transpose_image(image)` <a class=\"md-anchor\" id=\"transpose_image\"></a>\r\n\r\nTranspose an image by swapping the first and second dimension.\r\n\r\nSee also `transpose()`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: 3-D tensor of shape `[height, width, channels]`\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A 3-D tensor of shape `[width, height, channels]`\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if the shape of `image` not supported.\r\n\r\n\r\n\r\n## Image Adjustments <a class=\"md-anchor\" id=\"AUTOGENERATED-image-adjustments\"></a>\r\n\r\nTensorFlow provides functions to adjust images in various ways: brightness,\r\ncontrast, hue, and saturation.  Each adjustment can be done with predefined\r\nparameters or with random parameters picked from predefined intervals.  Random\r\nadjustments are often useful to expand a training set and reduce overfitting.\r\n\r\n- - -\r\n\r\n### `tf.image.adjust_brightness(image, delta, min_value=None, max_value=None)` <a class=\"md-anchor\" id=\"adjust_brightness\"></a>\r\n\r\nAdjust the brightness of RGB or Grayscale images.\r\n\r\nThe value `delta` is added to all components of the tensor `image`. `image`\r\nand `delta` are cast to `float` before adding, and the resulting values are\r\nclamped to `[min_value, max_value]`. Finally, the result is cast back to\r\n`images.dtype`.\r\n\r\nIf `min_value` or `max_value` are not given, they are set to the minimum and\r\nmaximum allowed values for `image.dtype` respectively.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: A tensor.\r\n*  <b>`delta`</b>: A scalar. Amount to add to the pixel values.\r\n*  <b>`min_value`</b>: Minimum value for output.\r\n*  <b>`max_value`</b>: Maximum value for output.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tensor of the same shape and type as `image`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.random_brightness(image, max_delta, seed=None)` <a class=\"md-anchor\" id=\"random_brightness\"></a>\r\n\r\nAdjust the brightness of images by a random factor.\r\n\r\nEquivalent to `adjust_brightness()` using a `delta` randomly picked in the\r\ninterval `[-max_delta, max_delta)`.\r\n\r\nNote that `delta` is picked as a float. Because for integer type images,\r\nthe brightness adjusted result is rounded before casting, integer images may\r\nhave modifications in the range `[-max_delta,max_delta]`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: 3-D tensor of shape `[height, width, channels]`.\r\n*  <b>`max_delta`</b>: float, must be non-negative.\r\n*  <b>`seed`</b>: A Python integer. Used to create a random seed. See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  3-D tensor of images of shape `[height, width, channels]`\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if max_delta is negative.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.adjust_contrast(images, contrast_factor, min_value=None, max_value=None)` <a class=\"md-anchor\" id=\"adjust_contrast\"></a>\r\n\r\nAdjust contrast of RGB or grayscale images.\r\n\r\n`images` is a tensor of at least 3 dimensions.  The last 3 dimensions are\r\ninterpreted as `[height, width, channels]`.  The other dimensions only\r\nrepresent a collection of images, such as `[batch, height, width, channels].`\r\n\r\nContrast is adjusted independently for each channel of each image.\r\n\r\nFor each channel, this Op first computes the mean of the image pixels in the\r\nchannel and then adjusts each component `x` of each pixel to\r\n`(x - mean) * contrast_factor + mean`.\r\n\r\nThe adjusted values are then clipped to fit in the `[min_value, max_value]`\r\ninterval. If `min_value` or `max_value` is not given, it is replaced with the\r\nminimum and maximum values for the data type of `images` respectively.\r\n\r\nThe contrast-adjusted image is always computed as `float`, and it is\r\ncast back to its original type after clipping.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`images`</b>: Images to adjust.  At least 3-D.\r\n*  <b>`contrast_factor`</b>: A float multiplier for adjusting contrast.\r\n*  <b>`min_value`</b>: Minimum value for clipping the adjusted pixels.\r\n*  <b>`max_value`</b>: Maximum value for clipping the adjusted pixels.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The constrast-adjusted image or images.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if the arguments are invalid.\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.random_contrast(image, lower, upper, seed=None)` <a class=\"md-anchor\" id=\"random_contrast\"></a>\r\n\r\nAdjust the contrase of an image by a random factor.\r\n\r\nEquivalent to `adjust_constrast()` but uses a `contrast_factor` randomly\r\npicked in the interval `[lower, upper]`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: 3-D tensor of shape `[height, width, channels]`.\r\n*  <b>`lower`</b>: float.  Lower bound for the random contrast factor.\r\n*  <b>`upper`</b>: float.  Upper bound for the random contrast factor.\r\n*  <b>`seed`</b>: A Python integer. Used to create a random seed. See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  3-D tensor of shape `[height, width, channels]`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if `upper <= lower` or if `lower < 0`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.image.per_image_whitening(image)` <a class=\"md-anchor\" id=\"per_image_whitening\"></a>\r\n\r\nLinearly scales `image` to have zero mean and unit norm.\r\n\r\nThis op computes `(x - mean) / adjusted_stddev`, where `mean` is the average\r\nof all values in image, and\r\n`adjusted_stddev = max(stddev, 1.0/srqt(image.NumElements()))`.\r\n\r\n`stddev` is the standard deviation of all values in `image`. It is capped\r\naway from zero to protect against division by 0 when handling uniform images.\r\n\r\nNote that this implementation is limited:\r\n*  It only whitens based on the statistics of an individual image.\r\n*  It does not take into account the covariance structure.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`image`</b>: 3-D tensor of shape `[height, width, channels]`.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The whitened image with same shape as `image`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if the shape of 'image' is incompatible with this function.\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/python/index.md",
    "content": "<!-- This file is machine generated: DO NOT EDIT! -->\r\n\r\n# TensorFlow Python reference documentation <a class=\"md-anchor\" id=\"AUTOGENERATED-tensorflow-python-reference-documentation\"></a>\r\n\r\n* **[Building Graphs](../../api_docs/python/framework.md)**:\r\n  * [`add_to_collection`](../../api_docs/python/framework.md#add_to_collection)\r\n  * [`as_dtype`](../../api_docs/python/framework.md#as_dtype)\r\n  * [`control_dependencies`](../../api_docs/python/framework.md#control_dependencies)\r\n  * [`convert_to_tensor`](../../api_docs/python/framework.md#convert_to_tensor)\r\n  * [`device`](../../api_docs/python/framework.md#device)\r\n  * [`Dimension`](../../api_docs/python/framework.md#Dimension)\r\n  * [`DType`](../../api_docs/python/framework.md#DType)\r\n  * [`get_collection`](../../api_docs/python/framework.md#get_collection)\r\n  * [`get_default_graph`](../../api_docs/python/framework.md#get_default_graph)\r\n  * [`get_seed`](../../api_docs/python/framework.md#get_seed)\r\n  * [`Graph`](../../api_docs/python/framework.md#Graph)\r\n  * [`GraphKeys`](../../api_docs/python/framework.md#GraphKeys)\r\n  * [`import_graph_def`](../../api_docs/python/framework.md#import_graph_def)\r\n  * [`name_scope`](../../api_docs/python/framework.md#name_scope)\r\n  * [`NoGradient`](../../api_docs/python/framework.md#NoGradient)\r\n  * [`op_scope`](../../api_docs/python/framework.md#op_scope)\r\n  * [`Operation`](../../api_docs/python/framework.md#Operation)\r\n  * [`RegisterGradient`](../../api_docs/python/framework.md#RegisterGradient)\r\n  * [`RegisterShape`](../../api_docs/python/framework.md#RegisterShape)\r\n  * [`Tensor`](../../api_docs/python/framework.md#Tensor)\r\n  * [`TensorShape`](../../api_docs/python/framework.md#TensorShape)\r\n\r\n* **[Constants, Sequences, and Random Values](../../api_docs/python/constant_op.md)**:\r\n  * [`constant`](../../api_docs/python/constant_op.md#constant)\r\n  * [`fill`](../../api_docs/python/constant_op.md#fill)\r\n  * [`linspace`](../../api_docs/python/constant_op.md#linspace)\r\n  * [`ones`](../../api_docs/python/constant_op.md#ones)\r\n  * [`ones_like`](../../api_docs/python/constant_op.md#ones_like)\r\n  * [`random_normal`](../../api_docs/python/constant_op.md#random_normal)\r\n  * [`random_shuffle`](../../api_docs/python/constant_op.md#random_shuffle)\r\n  * [`random_uniform`](../../api_docs/python/constant_op.md#random_uniform)\r\n  * [`range`](../../api_docs/python/constant_op.md#range)\r\n  * [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n  * [`truncated_normal`](../../api_docs/python/constant_op.md#truncated_normal)\r\n  * [`zeros`](../../api_docs/python/constant_op.md#zeros)\r\n  * [`zeros_like`](../../api_docs/python/constant_op.md#zeros_like)\r\n\r\n* **[Variables](../../api_docs/python/state_ops.md)**:\r\n  * [`all_variables`](../../api_docs/python/state_ops.md#all_variables)\r\n  * [`assert_variables_initialized`](../../api_docs/python/state_ops.md#assert_variables_initialized)\r\n  * [`assign`](../../api_docs/python/state_ops.md#assign)\r\n  * [`assign_add`](../../api_docs/python/state_ops.md#assign_add)\r\n  * [`assign_sub`](../../api_docs/python/state_ops.md#assign_sub)\r\n  * [`constant_initializer`](../../api_docs/python/state_ops.md#constant_initializer)\r\n  * [`count_up_to`](../../api_docs/python/state_ops.md#count_up_to)\r\n  * [`device`](../../api_docs/python/state_ops.md#device)\r\n  * [`get_checkpoint_state`](../../api_docs/python/state_ops.md#get_checkpoint_state)\r\n  * [`get_variable`](../../api_docs/python/state_ops.md#get_variable)\r\n  * [`get_variable_scope`](../../api_docs/python/state_ops.md#get_variable_scope)\r\n  * [`IndexedSlices`](../../api_docs/python/state_ops.md#IndexedSlices)\r\n  * [`initialize_all_variables`](../../api_docs/python/state_ops.md#initialize_all_variables)\r\n  * [`initialize_variables`](../../api_docs/python/state_ops.md#initialize_variables)\r\n  * [`latest_checkpoint`](../../api_docs/python/state_ops.md#latest_checkpoint)\r\n  * [`random_normal_initializer`](../../api_docs/python/state_ops.md#random_normal_initializer)\r\n  * [`random_uniform_initializer`](../../api_docs/python/state_ops.md#random_uniform_initializer)\r\n  * [`Saver`](../../api_docs/python/state_ops.md#Saver)\r\n  * [`scatter_add`](../../api_docs/python/state_ops.md#scatter_add)\r\n  * [`scatter_sub`](../../api_docs/python/state_ops.md#scatter_sub)\r\n  * [`scatter_update`](../../api_docs/python/state_ops.md#scatter_update)\r\n  * [`sparse_mask`](../../api_docs/python/state_ops.md#sparse_mask)\r\n  * [`trainable_variables`](../../api_docs/python/state_ops.md#trainable_variables)\r\n  * [`truncated_normal_initializer`](../../api_docs/python/state_ops.md#truncated_normal_initializer)\r\n  * [`uniform_unit_scaling_initializer`](../../api_docs/python/state_ops.md#uniform_unit_scaling_initializer)\r\n  * [`update_checkpoint_state`](../../api_docs/python/state_ops.md#update_checkpoint_state)\r\n  * [`Variable`](../../api_docs/python/state_ops.md#Variable)\r\n  * [`variable_scope`](../../api_docs/python/state_ops.md#variable_scope)\r\n  * [`zeros_initializer`](../../api_docs/python/state_ops.md#zeros_initializer)\r\n\r\n* **[Tensor Transformations](../../api_docs/python/array_ops.md)**:\r\n  * [`cast`](../../api_docs/python/array_ops.md#cast)\r\n  * [`concat`](../../api_docs/python/array_ops.md#concat)\r\n  * [`dynamic_partition`](../../api_docs/python/array_ops.md#dynamic_partition)\r\n  * [`dynamic_stitch`](../../api_docs/python/array_ops.md#dynamic_stitch)\r\n  * [`expand_dims`](../../api_docs/python/array_ops.md#expand_dims)\r\n  * [`gather`](../../api_docs/python/array_ops.md#gather)\r\n  * [`pack`](../../api_docs/python/array_ops.md#pack)\r\n  * [`pad`](../../api_docs/python/array_ops.md#pad)\r\n  * [`rank`](../../api_docs/python/array_ops.md#rank)\r\n  * [`reshape`](../../api_docs/python/array_ops.md#reshape)\r\n  * [`reverse`](../../api_docs/python/array_ops.md#reverse)\r\n  * [`reverse_sequence`](../../api_docs/python/array_ops.md#reverse_sequence)\r\n  * [`shape`](../../api_docs/python/array_ops.md#shape)\r\n  * [`size`](../../api_docs/python/array_ops.md#size)\r\n  * [`slice`](../../api_docs/python/array_ops.md#slice)\r\n  * [`split`](../../api_docs/python/array_ops.md#split)\r\n  * [`squeeze`](../../api_docs/python/array_ops.md#squeeze)\r\n  * [`string_to_number`](../../api_docs/python/array_ops.md#string_to_number)\r\n  * [`tile`](../../api_docs/python/array_ops.md#tile)\r\n  * [`to_bfloat16`](../../api_docs/python/array_ops.md#to_bfloat16)\r\n  * [`to_double`](../../api_docs/python/array_ops.md#to_double)\r\n  * [`to_float`](../../api_docs/python/array_ops.md#to_float)\r\n  * [`to_int32`](../../api_docs/python/array_ops.md#to_int32)\r\n  * [`to_int64`](../../api_docs/python/array_ops.md#to_int64)\r\n  * [`transpose`](../../api_docs/python/array_ops.md#transpose)\r\n  * [`unpack`](../../api_docs/python/array_ops.md#unpack)\r\n\r\n* **[Math](../../api_docs/python/math_ops.md)**:\r\n  * [`abs`](../../api_docs/python/math_ops.md#abs)\r\n  * [`accumulate_n`](../../api_docs/python/math_ops.md#accumulate_n)\r\n  * [`add`](../../api_docs/python/math_ops.md#add)\r\n  * [`add_n`](../../api_docs/python/math_ops.md#add_n)\r\n  * [`argmax`](../../api_docs/python/math_ops.md#argmax)\r\n  * [`argmin`](../../api_docs/python/math_ops.md#argmin)\r\n  * [`batch_cholesky`](../../api_docs/python/math_ops.md#batch_cholesky)\r\n  * [`batch_matmul`](../../api_docs/python/math_ops.md#batch_matmul)\r\n  * [`batch_matrix_determinant`](../../api_docs/python/math_ops.md#batch_matrix_determinant)\r\n  * [`batch_matrix_inverse`](../../api_docs/python/math_ops.md#batch_matrix_inverse)\r\n  * [`ceil`](../../api_docs/python/math_ops.md#ceil)\r\n  * [`cholesky`](../../api_docs/python/math_ops.md#cholesky)\r\n  * [`complex`](../../api_docs/python/math_ops.md#complex)\r\n  * [`complex_abs`](../../api_docs/python/math_ops.md#complex_abs)\r\n  * [`conj`](../../api_docs/python/math_ops.md#conj)\r\n  * [`cos`](../../api_docs/python/math_ops.md#cos)\r\n  * [`diag`](../../api_docs/python/math_ops.md#diag)\r\n  * [`div`](../../api_docs/python/math_ops.md#div)\r\n  * [`edit_distance`](../../api_docs/python/math_ops.md#edit_distance)\r\n  * [`exp`](../../api_docs/python/math_ops.md#exp)\r\n  * [`floor`](../../api_docs/python/math_ops.md#floor)\r\n  * [`imag`](../../api_docs/python/math_ops.md#imag)\r\n  * [`inv`](../../api_docs/python/math_ops.md#inv)\r\n  * [`invert_permutation`](../../api_docs/python/math_ops.md#invert_permutation)\r\n  * [`listdiff`](../../api_docs/python/math_ops.md#listdiff)\r\n  * [`log`](../../api_docs/python/math_ops.md#log)\r\n  * [`matmul`](../../api_docs/python/math_ops.md#matmul)\r\n  * [`matrix_determinant`](../../api_docs/python/math_ops.md#matrix_determinant)\r\n  * [`matrix_inverse`](../../api_docs/python/math_ops.md#matrix_inverse)\r\n  * [`maximum`](../../api_docs/python/math_ops.md#maximum)\r\n  * [`minimum`](../../api_docs/python/math_ops.md#minimum)\r\n  * [`mod`](../../api_docs/python/math_ops.md#mod)\r\n  * [`mul`](../../api_docs/python/math_ops.md#mul)\r\n  * [`neg`](../../api_docs/python/math_ops.md#neg)\r\n  * [`pow`](../../api_docs/python/math_ops.md#pow)\r\n  * [`real`](../../api_docs/python/math_ops.md#real)\r\n  * [`reduce_all`](../../api_docs/python/math_ops.md#reduce_all)\r\n  * [`reduce_any`](../../api_docs/python/math_ops.md#reduce_any)\r\n  * [`reduce_max`](../../api_docs/python/math_ops.md#reduce_max)\r\n  * [`reduce_mean`](../../api_docs/python/math_ops.md#reduce_mean)\r\n  * [`reduce_min`](../../api_docs/python/math_ops.md#reduce_min)\r\n  * [`reduce_prod`](../../api_docs/python/math_ops.md#reduce_prod)\r\n  * [`reduce_sum`](../../api_docs/python/math_ops.md#reduce_sum)\r\n  * [`round`](../../api_docs/python/math_ops.md#round)\r\n  * [`rsqrt`](../../api_docs/python/math_ops.md#rsqrt)\r\n  * [`segment_max`](../../api_docs/python/math_ops.md#segment_max)\r\n  * [`segment_mean`](../../api_docs/python/math_ops.md#segment_mean)\r\n  * [`segment_min`](../../api_docs/python/math_ops.md#segment_min)\r\n  * [`segment_prod`](../../api_docs/python/math_ops.md#segment_prod)\r\n  * [`segment_sum`](../../api_docs/python/math_ops.md#segment_sum)\r\n  * [`sign`](../../api_docs/python/math_ops.md#sign)\r\n  * [`sin`](../../api_docs/python/math_ops.md#sin)\r\n  * [`sparse_segment_mean`](../../api_docs/python/math_ops.md#sparse_segment_mean)\r\n  * [`sparse_segment_sum`](../../api_docs/python/math_ops.md#sparse_segment_sum)\r\n  * [`sqrt`](../../api_docs/python/math_ops.md#sqrt)\r\n  * [`square`](../../api_docs/python/math_ops.md#square)\r\n  * [`sub`](../../api_docs/python/math_ops.md#sub)\r\n  * [`transpose`](../../api_docs/python/math_ops.md#transpose)\r\n  * [`unique`](../../api_docs/python/math_ops.md#unique)\r\n  * [`unsorted_segment_sum`](../../api_docs/python/math_ops.md#unsorted_segment_sum)\r\n  * [`where`](../../api_docs/python/math_ops.md#where)\r\n\r\n* **[Control Flow](../../api_docs/python/control_flow_ops.md)**:\r\n  * [`add_check_numerics_ops`](../../api_docs/python/control_flow_ops.md#add_check_numerics_ops)\r\n  * [`Assert`](../../api_docs/python/control_flow_ops.md#Assert)\r\n  * [`check_numerics`](../../api_docs/python/control_flow_ops.md#check_numerics)\r\n  * [`count_up_to`](../../api_docs/python/control_flow_ops.md#count_up_to)\r\n  * [`equal`](../../api_docs/python/control_flow_ops.md#equal)\r\n  * [`greater`](../../api_docs/python/control_flow_ops.md#greater)\r\n  * [`greater_equal`](../../api_docs/python/control_flow_ops.md#greater_equal)\r\n  * [`group`](../../api_docs/python/control_flow_ops.md#group)\r\n  * [`identity`](../../api_docs/python/control_flow_ops.md#identity)\r\n  * [`is_finite`](../../api_docs/python/control_flow_ops.md#is_finite)\r\n  * [`is_inf`](../../api_docs/python/control_flow_ops.md#is_inf)\r\n  * [`is_nan`](../../api_docs/python/control_flow_ops.md#is_nan)\r\n  * [`less`](../../api_docs/python/control_flow_ops.md#less)\r\n  * [`less_equal`](../../api_docs/python/control_flow_ops.md#less_equal)\r\n  * [`logical_and`](../../api_docs/python/control_flow_ops.md#logical_and)\r\n  * [`logical_not`](../../api_docs/python/control_flow_ops.md#logical_not)\r\n  * [`logical_or`](../../api_docs/python/control_flow_ops.md#logical_or)\r\n  * [`logical_xor`](../../api_docs/python/control_flow_ops.md#logical_xor)\r\n  * [`no_op`](../../api_docs/python/control_flow_ops.md#no_op)\r\n  * [`not_equal`](../../api_docs/python/control_flow_ops.md#not_equal)\r\n  * [`Print`](../../api_docs/python/control_flow_ops.md#Print)\r\n  * [`select`](../../api_docs/python/control_flow_ops.md#select)\r\n  * [`tuple`](../../api_docs/python/control_flow_ops.md#tuple)\r\n  * [`verify_tensor_all_finite`](../../api_docs/python/control_flow_ops.md#verify_tensor_all_finite)\r\n  * [`where`](../../api_docs/python/control_flow_ops.md#where)\r\n\r\n* **[Images](../../api_docs/python/image.md)**:\r\n  * [`adjust_brightness`](../../api_docs/python/image.md#adjust_brightness)\r\n  * [`adjust_contrast`](../../api_docs/python/image.md#adjust_contrast)\r\n  * [`crop_to_bounding_box`](../../api_docs/python/image.md#crop_to_bounding_box)\r\n  * [`decode_jpeg`](../../api_docs/python/image.md#decode_jpeg)\r\n  * [`decode_png`](../../api_docs/python/image.md#decode_png)\r\n  * [`encode_jpeg`](../../api_docs/python/image.md#encode_jpeg)\r\n  * [`encode_png`](../../api_docs/python/image.md#encode_png)\r\n  * [`extract_glimpse`](../../api_docs/python/image.md#extract_glimpse)\r\n  * [`flip_left_right`](../../api_docs/python/image.md#flip_left_right)\r\n  * [`flip_up_down`](../../api_docs/python/image.md#flip_up_down)\r\n  * [`pad_to_bounding_box`](../../api_docs/python/image.md#pad_to_bounding_box)\r\n  * [`per_image_whitening`](../../api_docs/python/image.md#per_image_whitening)\r\n  * [`random_brightness`](../../api_docs/python/image.md#random_brightness)\r\n  * [`random_contrast`](../../api_docs/python/image.md#random_contrast)\r\n  * [`random_crop`](../../api_docs/python/image.md#random_crop)\r\n  * [`random_flip_left_right`](../../api_docs/python/image.md#random_flip_left_right)\r\n  * [`random_flip_up_down`](../../api_docs/python/image.md#random_flip_up_down)\r\n  * [`resize_area`](../../api_docs/python/image.md#resize_area)\r\n  * [`resize_bicubic`](../../api_docs/python/image.md#resize_bicubic)\r\n  * [`resize_bilinear`](../../api_docs/python/image.md#resize_bilinear)\r\n  * [`resize_image_with_crop_or_pad`](../../api_docs/python/image.md#resize_image_with_crop_or_pad)\r\n  * [`resize_images`](../../api_docs/python/image.md#resize_images)\r\n  * [`resize_nearest_neighbor`](../../api_docs/python/image.md#resize_nearest_neighbor)\r\n  * [`transpose_image`](../../api_docs/python/image.md#transpose_image)\r\n\r\n* **[Sparse Tensors](../../api_docs/python/sparse_ops.md)**:\r\n  * [`shape`](../../api_docs/python/sparse_ops.md#shape)\r\n  * [`sparse_concat`](../../api_docs/python/sparse_ops.md#sparse_concat)\r\n  * [`sparse_fill_empty_rows`](../../api_docs/python/sparse_ops.md#sparse_fill_empty_rows)\r\n  * [`sparse_reorder`](../../api_docs/python/sparse_ops.md#sparse_reorder)\r\n  * [`sparse_retain`](../../api_docs/python/sparse_ops.md#sparse_retain)\r\n  * [`sparse_tensor_to_dense`](../../api_docs/python/sparse_ops.md#sparse_tensor_to_dense)\r\n  * [`sparse_to_dense`](../../api_docs/python/sparse_ops.md#sparse_to_dense)\r\n  * [`sparse_to_indicator`](../../api_docs/python/sparse_ops.md#sparse_to_indicator)\r\n  * [`SparseTensor`](../../api_docs/python/sparse_ops.md#SparseTensor)\r\n  * [`SparseTensorValue`](../../api_docs/python/sparse_ops.md#SparseTensorValue)\r\n\r\n* **[Inputs and Readers](../../api_docs/python/io_ops.md)**:\r\n  * [`batch`](../../api_docs/python/io_ops.md#batch)\r\n  * [`batch_join`](../../api_docs/python/io_ops.md#batch_join)\r\n  * [`decode_csv`](../../api_docs/python/io_ops.md#decode_csv)\r\n  * [`decode_raw`](../../api_docs/python/io_ops.md#decode_raw)\r\n  * [`FIFOQueue`](../../api_docs/python/io_ops.md#FIFOQueue)\r\n  * [`FixedLengthRecordReader`](../../api_docs/python/io_ops.md#FixedLengthRecordReader)\r\n  * [`IdentityReader`](../../api_docs/python/io_ops.md#IdentityReader)\r\n  * [`limit_epochs`](../../api_docs/python/io_ops.md#limit_epochs)\r\n  * [`match_filenames_once`](../../api_docs/python/io_ops.md#match_filenames_once)\r\n  * [`matching_files`](../../api_docs/python/io_ops.md#matching_files)\r\n  * [`parse_example`](../../api_docs/python/io_ops.md#parse_example)\r\n  * [`parse_single_example`](../../api_docs/python/io_ops.md#parse_single_example)\r\n  * [`placeholder`](../../api_docs/python/io_ops.md#placeholder)\r\n  * [`QueueBase`](../../api_docs/python/io_ops.md#QueueBase)\r\n  * [`RandomShuffleQueue`](../../api_docs/python/io_ops.md#RandomShuffleQueue)\r\n  * [`range_input_producer`](../../api_docs/python/io_ops.md#range_input_producer)\r\n  * [`read_file`](../../api_docs/python/io_ops.md#read_file)\r\n  * [`ReaderBase`](../../api_docs/python/io_ops.md#ReaderBase)\r\n  * [`shuffle_batch`](../../api_docs/python/io_ops.md#shuffle_batch)\r\n  * [`shuffle_batch_join`](../../api_docs/python/io_ops.md#shuffle_batch_join)\r\n  * [`size`](../../api_docs/python/io_ops.md#size)\r\n  * [`slice_input_producer`](../../api_docs/python/io_ops.md#slice_input_producer)\r\n  * [`string_input_producer`](../../api_docs/python/io_ops.md#string_input_producer)\r\n  * [`TextLineReader`](../../api_docs/python/io_ops.md#TextLineReader)\r\n  * [`TFRecordReader`](../../api_docs/python/io_ops.md#TFRecordReader)\r\n  * [`WholeFileReader`](../../api_docs/python/io_ops.md#WholeFileReader)\r\n\r\n* **[Data IO (Python functions)](../../api_docs/python/python_io.md)**:\r\n  * [`tf_record_iterator`](../../api_docs/python/python_io.md#tf_record_iterator)\r\n  * [`TFRecordWriter`](../../api_docs/python/python_io.md#TFRecordWriter)\r\n\r\n* **[Neural Network](../../api_docs/python/nn.md)**:\r\n  * [`avg_pool`](../../api_docs/python/nn.md#avg_pool)\r\n  * [`bias_add`](../../api_docs/python/nn.md#bias_add)\r\n  * [`compute_accidental_hits`](../../api_docs/python/nn.md#compute_accidental_hits)\r\n  * [`conv2d`](../../api_docs/python/nn.md#conv2d)\r\n  * [`depthwise_conv2d`](../../api_docs/python/nn.md#depthwise_conv2d)\r\n  * [`dropout`](../../api_docs/python/nn.md#dropout)\r\n  * [`embedding_lookup`](../../api_docs/python/nn.md#embedding_lookup)\r\n  * [`fixed_unigram_candidate_sampler`](../../api_docs/python/nn.md#fixed_unigram_candidate_sampler)\r\n  * [`in_top_k`](../../api_docs/python/nn.md#in_top_k)\r\n  * [`l2_loss`](../../api_docs/python/nn.md#l2_loss)\r\n  * [`l2_normalize`](../../api_docs/python/nn.md#l2_normalize)\r\n  * [`learned_unigram_candidate_sampler`](../../api_docs/python/nn.md#learned_unigram_candidate_sampler)\r\n  * [`local_response_normalization`](../../api_docs/python/nn.md#local_response_normalization)\r\n  * [`log_uniform_candidate_sampler`](../../api_docs/python/nn.md#log_uniform_candidate_sampler)\r\n  * [`max_pool`](../../api_docs/python/nn.md#max_pool)\r\n  * [`max_pool_with_argmax`](../../api_docs/python/nn.md#max_pool_with_argmax)\r\n  * [`moments`](../../api_docs/python/nn.md#moments)\r\n  * [`nce_loss`](../../api_docs/python/nn.md#nce_loss)\r\n  * [`relu`](../../api_docs/python/nn.md#relu)\r\n  * [`relu6`](../../api_docs/python/nn.md#relu6)\r\n  * [`sampled_softmax_loss`](../../api_docs/python/nn.md#sampled_softmax_loss)\r\n  * [`separable_conv2d`](../../api_docs/python/nn.md#separable_conv2d)\r\n  * [`sigmoid`](../../api_docs/python/nn.md#sigmoid)\r\n  * [`sigmoid_cross_entropy_with_logits`](../../api_docs/python/nn.md#sigmoid_cross_entropy_with_logits)\r\n  * [`softmax`](../../api_docs/python/nn.md#softmax)\r\n  * [`softmax_cross_entropy_with_logits`](../../api_docs/python/nn.md#softmax_cross_entropy_with_logits)\r\n  * [`softplus`](../../api_docs/python/nn.md#softplus)\r\n  * [`tanh`](../../api_docs/python/nn.md#tanh)\r\n  * [`top_k`](../../api_docs/python/nn.md#top_k)\r\n  * [`uniform_candidate_sampler`](../../api_docs/python/nn.md#uniform_candidate_sampler)\r\n\r\n* **[Running Graphs](../../api_docs/python/client.md)**:\r\n  * [`AbortedError`](../../api_docs/python/client.md#AbortedError)\r\n  * [`AlreadyExistsError`](../../api_docs/python/client.md#AlreadyExistsError)\r\n  * [`CancelledError`](../../api_docs/python/client.md#CancelledError)\r\n  * [`DataLossError`](../../api_docs/python/client.md#DataLossError)\r\n  * [`DeadlineExceededError`](../../api_docs/python/client.md#DeadlineExceededError)\r\n  * [`FailedPreconditionError`](../../api_docs/python/client.md#FailedPreconditionError)\r\n  * [`get_default_session`](../../api_docs/python/client.md#get_default_session)\r\n  * [`InteractiveSession`](../../api_docs/python/client.md#InteractiveSession)\r\n  * [`InternalError`](../../api_docs/python/client.md#InternalError)\r\n  * [`InvalidArgumentError`](../../api_docs/python/client.md#InvalidArgumentError)\r\n  * [`NotFoundError`](../../api_docs/python/client.md#NotFoundError)\r\n  * [`OpError`](../../api_docs/python/client.md#OpError)\r\n  * [`OutOfRangeError`](../../api_docs/python/client.md#OutOfRangeError)\r\n  * [`PermissionDeniedError`](../../api_docs/python/client.md#PermissionDeniedError)\r\n  * [`ResourceExhaustedError`](../../api_docs/python/client.md#ResourceExhaustedError)\r\n  * [`Session`](../../api_docs/python/client.md#Session)\r\n  * [`UnauthenticatedError`](../../api_docs/python/client.md#UnauthenticatedError)\r\n  * [`UnavailableError`](../../api_docs/python/client.md#UnavailableError)\r\n  * [`UnimplementedError`](../../api_docs/python/client.md#UnimplementedError)\r\n  * [`UnknownError`](../../api_docs/python/client.md#UnknownError)\r\n\r\n* **[Training](../../api_docs/python/train.md)**:\r\n  * [`AdagradOptimizer`](../../api_docs/python/train.md#AdagradOptimizer)\r\n  * [`AdamOptimizer`](../../api_docs/python/train.md#AdamOptimizer)\r\n  * [`add_queue_runner`](../../api_docs/python/train.md#add_queue_runner)\r\n  * [`AggregationMethod`](../../api_docs/python/train.md#AggregationMethod)\r\n  * [`clip_by_average_norm`](../../api_docs/python/train.md#clip_by_average_norm)\r\n  * [`clip_by_global_norm`](../../api_docs/python/train.md#clip_by_global_norm)\r\n  * [`clip_by_norm`](../../api_docs/python/train.md#clip_by_norm)\r\n  * [`clip_by_value`](../../api_docs/python/train.md#clip_by_value)\r\n  * [`Coordinator`](../../api_docs/python/train.md#Coordinator)\r\n  * [`exponential_decay`](../../api_docs/python/train.md#exponential_decay)\r\n  * [`ExponentialMovingAverage`](../../api_docs/python/train.md#ExponentialMovingAverage)\r\n  * [`FtrlOptimizer`](../../api_docs/python/train.md#FtrlOptimizer)\r\n  * [`global_norm`](../../api_docs/python/train.md#global_norm)\r\n  * [`global_step`](../../api_docs/python/train.md#global_step)\r\n  * [`GradientDescentOptimizer`](../../api_docs/python/train.md#GradientDescentOptimizer)\r\n  * [`gradients`](../../api_docs/python/train.md#gradients)\r\n  * [`histogram_summary`](../../api_docs/python/train.md#histogram_summary)\r\n  * [`image_summary`](../../api_docs/python/train.md#image_summary)\r\n  * [`merge_all_summaries`](../../api_docs/python/train.md#merge_all_summaries)\r\n  * [`merge_summary`](../../api_docs/python/train.md#merge_summary)\r\n  * [`MomentumOptimizer`](../../api_docs/python/train.md#MomentumOptimizer)\r\n  * [`Optimizer`](../../api_docs/python/train.md#Optimizer)\r\n  * [`QueueRunner`](../../api_docs/python/train.md#QueueRunner)\r\n  * [`RMSPropOptimizer`](../../api_docs/python/train.md#RMSPropOptimizer)\r\n  * [`scalar_summary`](../../api_docs/python/train.md#scalar_summary)\r\n  * [`start_queue_runners`](../../api_docs/python/train.md#start_queue_runners)\r\n  * [`stop_gradient`](../../api_docs/python/train.md#stop_gradient)\r\n  * [`summary_iterator`](../../api_docs/python/train.md#summary_iterator)\r\n  * [`SummaryWriter`](../../api_docs/python/train.md#SummaryWriter)\r\n  * [`write_graph`](../../api_docs/python/train.md#write_graph)\r\n  * [`zero_fraction`](../../api_docs/python/train.md#zero_fraction)\r\n\r\n<div class=\"sections-order\" style=\"display: none;\">\r\n<!--\r\n<!-- framework.md -->\r\n<!-- constant_op.md -->\r\n<!-- state_ops.md -->\r\n<!-- array_ops.md -->\r\n<!-- math_ops.md -->\r\n<!-- control_flow_ops.md -->\r\n<!-- image.md -->\r\n<!-- sparse_ops.md -->\r\n<!-- io_ops.md -->\r\n<!-- python_io.md -->\r\n<!-- nn.md -->\r\n<!-- client.md -->\r\n<!-- train.md -->\r\n-->\r\n</div>\r\n"
  },
  {
    "path": "SOURCE/api_docs/python/io_ops.md",
    "content": "<!-- This file is machine generated: DO NOT EDIT! -->\r\n\r\n# Inputs and Readers <a class=\"md-anchor\" id=\"AUTOGENERATED-inputs-and-readers\"></a>\r\n\r\nNote: Functions taking `Tensor` arguments can also take anything accepted by\r\n[`tf.convert_to_tensor`](../../api_docs/python/framework.md#convert_to_tensor).\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Inputs and Readers](#AUTOGENERATED-inputs-and-readers)\r\n* [Placeholders](#AUTOGENERATED-placeholders)\r\n  * [`tf.placeholder(dtype, shape=None, name=None)`](#placeholder)\r\n* [Readers](#AUTOGENERATED-readers)\r\n  * [`class tf.ReaderBase`](#ReaderBase)\r\n  * [`class tf.TextLineReader`](#TextLineReader)\r\n  * [`class tf.WholeFileReader`](#WholeFileReader)\r\n  * [`class tf.IdentityReader`](#IdentityReader)\r\n  * [`class tf.TFRecordReader`](#TFRecordReader)\r\n  * [`class tf.FixedLengthRecordReader`](#FixedLengthRecordReader)\r\n* [Converting](#AUTOGENERATED-converting)\r\n  * [`tf.decode_csv(records, record_defaults, field_delim=None, name=None)`](#decode_csv)\r\n  * [`tf.decode_raw(bytes, out_type, little_endian=None, name=None)`](#decode_raw)\r\n  * [Example protocol buffer](#AUTOGENERATED-example-protocol-buffer)\r\n  * [`tf.parse_example(serialized, names=None, sparse_keys=None, sparse_types=None, dense_keys=None, dense_types=None, dense_defaults=None, dense_shapes=None, name='ParseExample')`](#parse_example)\r\n  * [`tf.parse_single_example(serialized, names=None, sparse_keys=None, sparse_types=None, dense_keys=None, dense_types=None, dense_defaults=None, dense_shapes=None, name='ParseSingleExample')`](#parse_single_example)\r\n* [Queues](#AUTOGENERATED-queues)\r\n  * [`class tf.QueueBase`](#QueueBase)\r\n  * [`class tf.FIFOQueue`](#FIFOQueue)\r\n  * [`class tf.RandomShuffleQueue`](#RandomShuffleQueue)\r\n* [Dealing with the filesystem](#AUTOGENERATED-dealing-with-the-filesystem)\r\n  * [`tf.matching_files(pattern, name=None)`](#matching_files)\r\n  * [`tf.read_file(filename, name=None)`](#read_file)\r\n* [Input pipeline](#AUTOGENERATED-input-pipeline)\r\n  * [Beginning of an input pipeline](#AUTOGENERATED-beginning-of-an-input-pipeline)\r\n  * [`tf.train.match_filenames_once(pattern, name=None)`](#match_filenames_once)\r\n  * [`tf.train.limit_epochs(tensor, num_epochs=None, name=None)`](#limit_epochs)\r\n  * [`tf.train.range_input_producer(limit, num_epochs=None, shuffle=True, seed=None, capacity=32, name=None)`](#range_input_producer)\r\n  * [`tf.train.slice_input_producer(tensor_list, num_epochs=None, shuffle=True, seed=None, capacity=32, name=None)`](#slice_input_producer)\r\n  * [`tf.train.string_input_producer(string_tensor, num_epochs=None, shuffle=True, seed=None, capacity=32, name=None)`](#string_input_producer)\r\n  * [Batching at the end of an input pipeline](#AUTOGENERATED-batching-at-the-end-of-an-input-pipeline)\r\n  * [`tf.train.batch(tensor_list, batch_size, num_threads=1, capacity=32, enqueue_many=False, shapes=None, name=None)`](#batch)\r\n  * [`tf.train.batch_join(tensor_list_list, batch_size, capacity=32, enqueue_many=False, shapes=None, name=None)`](#batch_join)\r\n  * [`tf.train.shuffle_batch(tensor_list, batch_size, capacity, min_after_dequeue, num_threads=1, seed=None, enqueue_many=False, shapes=None, name=None)`](#shuffle_batch)\r\n  * [`tf.train.shuffle_batch_join(tensor_list_list, batch_size, capacity, min_after_dequeue, seed=None, enqueue_many=False, shapes=None, name=None)`](#shuffle_batch_join)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## Placeholders <a class=\"md-anchor\" id=\"AUTOGENERATED-placeholders\"></a>\r\n\r\nTensorFlow provides a placeholder operation that must be fed with data\r\non execution.  For more info, see the section on [Feeding\r\ndata](../../how_tos/reading_data/index.md#feeding).\r\n\r\n- - -\r\n\r\n### `tf.placeholder(dtype, shape=None, name=None)` <a class=\"md-anchor\" id=\"placeholder\"></a>\r\n\r\nInserts a placeholder for a tensor that will be always fed.\r\n\r\n**Important**: This tensor will produce an error if evaluated. Its value must\r\nbe fed using the `feed_dict` optional argument to `Session.run()`,\r\n`Tensor.eval()`, or `Operation.run()`.\r\n\r\nFor example:\r\n\r\n```python\r\nx = tf.placeholder(float, shape=(1024, 1024))\r\ny = tf.matmul(x, x)\r\n\r\nwith tf.Session() as sess:\r\n  print sess.run(y)  # ERROR: will fail because x was not fed.\r\n\r\n  rand_array = np.random.rand(1024, 1024)\r\n  print sess.run(y, feed_dict={x: rand_array})  # Will succeed.\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`dtype`</b>: The type of elements in the tensor to be fed.\r\n*  <b>`shape`</b>: The shape of the tensor to be fed (optional). If the shape is not\r\n    specified, you can feed a tensor of any shape.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` that may be used as a handle for feeding a value, but not\r\n  evaluated directly.\r\n\r\n\r\n\r\n## Readers <a class=\"md-anchor\" id=\"AUTOGENERATED-readers\"></a>\r\n\r\nTensorFlow provides a set of Reader classes for reading data formats.\r\nFor more information on inputs and readers, see [Reading\r\ndata](../../how_tos/reading_data/index.md).\r\n\r\n- - -\r\n\r\n### `class tf.ReaderBase` <a class=\"md-anchor\" id=\"ReaderBase\"></a>\r\n\r\nBase class for different Reader types, that produce a record every step.\r\n\r\nConceptually, Readers convert string 'work units' into records (key,\r\nvalue pairs).  Typically the 'work units' are filenames and the\r\nrecords are extracted from the contents of those files.  We want a\r\nsingle record produced per step, but a work unit can correspond to\r\nmany records.\r\n\r\nTherefore we introduce some decoupling using a queue.  The queue\r\ncontains the work units and the Reader dequeues from the queue when\r\nit is asked to produce a record (via Read()) but it has finished the\r\nlast work unit.\r\n- - -\r\n\r\n#### `tf.ReaderBase.__init__(reader_ref, supports_serialize=False)` <a class=\"md-anchor\" id=\"ReaderBase.__init__\"></a>\r\n\r\nCreates a new ReaderBase.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`reader_ref`</b>: The operation that implements the reader.\r\n*  <b>`supports_serialize`</b>: True if the reader implementation can\r\n    serialize its state.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.ReaderBase.num_records_produced(name=None)` <a class=\"md-anchor\" id=\"ReaderBase.num_records_produced\"></a>\r\n\r\nReturns the number of records this reader has produced.\r\n\r\nThis is the same as the number of Read executions that have\r\nsucceeded.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An int64 Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.ReaderBase.num_work_units_completed(name=None)` <a class=\"md-anchor\" id=\"ReaderBase.num_work_units_completed\"></a>\r\n\r\nReturns the number of work units this reader has finished processing.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An int64 Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.ReaderBase.read(queue, name=None)` <a class=\"md-anchor\" id=\"ReaderBase.read\"></a>\r\n\r\nReturns the next record (key, value pair) produced by a reader.\r\n\r\nWill dequeue a work unit from queue if necessary (e.g. when the\r\nReader needs to start reading from a new file since it has\r\nfinished with the previous file).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`queue`</b>: A Queue or a mutable string Tensor representing a handle\r\n    to a Queue, with string work items.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tuple of Tensors (key, value).\r\n\r\n*  <b>`key`</b>: A string scalar Tensor.\r\n*  <b>`value`</b>: A string scalar Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.ReaderBase.reader_ref` <a class=\"md-anchor\" id=\"ReaderBase.reader_ref\"></a>\r\n\r\nOp that implements the reader.\r\n\r\n- - -\r\n\r\n#### `tf.ReaderBase.reset(name=None)` <a class=\"md-anchor\" id=\"ReaderBase.reset\"></a>\r\n\r\nRestore a reader to its initial clean state.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The created Operation.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.ReaderBase.restore_state(state, name=None)` <a class=\"md-anchor\" id=\"ReaderBase.restore_state\"></a>\r\n\r\nRestore a reader to a previously saved state.\r\n\r\nNot all Readers support being restored, so this can produce an\r\nUnimplemented error.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`state`</b>: A string Tensor.\r\n    Result of a SerializeState of a Reader with matching type.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The created Operation.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.ReaderBase.serialize_state(name=None)` <a class=\"md-anchor\" id=\"ReaderBase.serialize_state\"></a>\r\n\r\nProduce a string tensor that encodes the state of a reader.\r\n\r\nNot all Readers support being serialized, so this can produce an\r\nUnimplemented error.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A string Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.ReaderBase.supports_serialize` <a class=\"md-anchor\" id=\"ReaderBase.supports_serialize\"></a>\r\n\r\nWhether the Reader implementation can serialize its state.\r\n\r\n\r\n- - -\r\n\r\n### `class tf.TextLineReader` <a class=\"md-anchor\" id=\"TextLineReader\"></a>\r\n\r\nA Reader that outputs the lines of a file delimited by newlines.\r\n\r\nNewlines are stripped from the output.\r\nSee ReaderBase for supported methods.\r\n- - -\r\n\r\n#### `tf.TextLineReader.__init__(skip_header_lines=None, name=None)` <a class=\"md-anchor\" id=\"TextLineReader.__init__\"></a>\r\n\r\nCreate a TextLineReader.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`skip_header_lines`</b>: An optional int. Defaults to 0.  Number of lines\r\n    to skip from the beginning of every file.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TextLineReader.num_records_produced(name=None)` <a class=\"md-anchor\" id=\"TextLineReader.num_records_produced\"></a>\r\n\r\nReturns the number of records this reader has produced.\r\n\r\nThis is the same as the number of Read executions that have\r\nsucceeded.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An int64 Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TextLineReader.num_work_units_completed(name=None)` <a class=\"md-anchor\" id=\"TextLineReader.num_work_units_completed\"></a>\r\n\r\nReturns the number of work units this reader has finished processing.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An int64 Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TextLineReader.read(queue, name=None)` <a class=\"md-anchor\" id=\"TextLineReader.read\"></a>\r\n\r\nReturns the next record (key, value pair) produced by a reader.\r\n\r\nWill dequeue a work unit from queue if necessary (e.g. when the\r\nReader needs to start reading from a new file since it has\r\nfinished with the previous file).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`queue`</b>: A Queue or a mutable string Tensor representing a handle\r\n    to a Queue, with string work items.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tuple of Tensors (key, value).\r\n\r\n*  <b>`key`</b>: A string scalar Tensor.\r\n*  <b>`value`</b>: A string scalar Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TextLineReader.reader_ref` <a class=\"md-anchor\" id=\"TextLineReader.reader_ref\"></a>\r\n\r\nOp that implements the reader.\r\n\r\n- - -\r\n\r\n#### `tf.TextLineReader.reset(name=None)` <a class=\"md-anchor\" id=\"TextLineReader.reset\"></a>\r\n\r\nRestore a reader to its initial clean state.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The created Operation.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TextLineReader.restore_state(state, name=None)` <a class=\"md-anchor\" id=\"TextLineReader.restore_state\"></a>\r\n\r\nRestore a reader to a previously saved state.\r\n\r\nNot all Readers support being restored, so this can produce an\r\nUnimplemented error.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`state`</b>: A string Tensor.\r\n    Result of a SerializeState of a Reader with matching type.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The created Operation.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TextLineReader.serialize_state(name=None)` <a class=\"md-anchor\" id=\"TextLineReader.serialize_state\"></a>\r\n\r\nProduce a string tensor that encodes the state of a reader.\r\n\r\nNot all Readers support being serialized, so this can produce an\r\nUnimplemented error.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A string Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TextLineReader.supports_serialize` <a class=\"md-anchor\" id=\"TextLineReader.supports_serialize\"></a>\r\n\r\nWhether the Reader implementation can serialize its state.\r\n\r\n\r\n- - -\r\n\r\n### `class tf.WholeFileReader` <a class=\"md-anchor\" id=\"WholeFileReader\"></a>\r\n\r\nA Reader that outputs the entire contents of a file as a value.\r\n\r\nTo use, enqueue filenames in a Queue.  The output of Read will\r\nbe a filename (key) and the contents of that file (value).\r\n\r\nSee ReaderBase for supported methods.\r\n- - -\r\n\r\n#### `tf.WholeFileReader.__init__(name=None)` <a class=\"md-anchor\" id=\"WholeFileReader.__init__\"></a>\r\n\r\nCreate a WholeFileReader.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n\r\n- - -\r\n\r\n#### `tf.WholeFileReader.num_records_produced(name=None)` <a class=\"md-anchor\" id=\"WholeFileReader.num_records_produced\"></a>\r\n\r\nReturns the number of records this reader has produced.\r\n\r\nThis is the same as the number of Read executions that have\r\nsucceeded.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An int64 Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.WholeFileReader.num_work_units_completed(name=None)` <a class=\"md-anchor\" id=\"WholeFileReader.num_work_units_completed\"></a>\r\n\r\nReturns the number of work units this reader has finished processing.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An int64 Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.WholeFileReader.read(queue, name=None)` <a class=\"md-anchor\" id=\"WholeFileReader.read\"></a>\r\n\r\nReturns the next record (key, value pair) produced by a reader.\r\n\r\nWill dequeue a work unit from queue if necessary (e.g. when the\r\nReader needs to start reading from a new file since it has\r\nfinished with the previous file).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`queue`</b>: A Queue or a mutable string Tensor representing a handle\r\n    to a Queue, with string work items.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tuple of Tensors (key, value).\r\n\r\n*  <b>`key`</b>: A string scalar Tensor.\r\n*  <b>`value`</b>: A string scalar Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.WholeFileReader.reader_ref` <a class=\"md-anchor\" id=\"WholeFileReader.reader_ref\"></a>\r\n\r\nOp that implements the reader.\r\n\r\n- - -\r\n\r\n#### `tf.WholeFileReader.reset(name=None)` <a class=\"md-anchor\" id=\"WholeFileReader.reset\"></a>\r\n\r\nRestore a reader to its initial clean state.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The created Operation.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.WholeFileReader.restore_state(state, name=None)` <a class=\"md-anchor\" id=\"WholeFileReader.restore_state\"></a>\r\n\r\nRestore a reader to a previously saved state.\r\n\r\nNot all Readers support being restored, so this can produce an\r\nUnimplemented error.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`state`</b>: A string Tensor.\r\n    Result of a SerializeState of a Reader with matching type.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The created Operation.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.WholeFileReader.serialize_state(name=None)` <a class=\"md-anchor\" id=\"WholeFileReader.serialize_state\"></a>\r\n\r\nProduce a string tensor that encodes the state of a reader.\r\n\r\nNot all Readers support being serialized, so this can produce an\r\nUnimplemented error.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A string Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.WholeFileReader.supports_serialize` <a class=\"md-anchor\" id=\"WholeFileReader.supports_serialize\"></a>\r\n\r\nWhether the Reader implementation can serialize its state.\r\n\r\n\r\n- - -\r\n\r\n### `class tf.IdentityReader` <a class=\"md-anchor\" id=\"IdentityReader\"></a>\r\n\r\nA Reader that outputs the queued work as both the key and value.\r\n\r\nTo use, enqueue strings in a Queue.  Read will take the front\r\nwork string and output (work, work).\r\n\r\nSee ReaderBase for supported methods.\r\n- - -\r\n\r\n#### `tf.IdentityReader.__init__(name=None)` <a class=\"md-anchor\" id=\"IdentityReader.__init__\"></a>\r\n\r\nCreate a IdentityReader.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n\r\n- - -\r\n\r\n#### `tf.IdentityReader.num_records_produced(name=None)` <a class=\"md-anchor\" id=\"IdentityReader.num_records_produced\"></a>\r\n\r\nReturns the number of records this reader has produced.\r\n\r\nThis is the same as the number of Read executions that have\r\nsucceeded.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An int64 Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.IdentityReader.num_work_units_completed(name=None)` <a class=\"md-anchor\" id=\"IdentityReader.num_work_units_completed\"></a>\r\n\r\nReturns the number of work units this reader has finished processing.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An int64 Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.IdentityReader.read(queue, name=None)` <a class=\"md-anchor\" id=\"IdentityReader.read\"></a>\r\n\r\nReturns the next record (key, value pair) produced by a reader.\r\n\r\nWill dequeue a work unit from queue if necessary (e.g. when the\r\nReader needs to start reading from a new file since it has\r\nfinished with the previous file).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`queue`</b>: A Queue or a mutable string Tensor representing a handle\r\n    to a Queue, with string work items.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tuple of Tensors (key, value).\r\n\r\n*  <b>`key`</b>: A string scalar Tensor.\r\n*  <b>`value`</b>: A string scalar Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.IdentityReader.reader_ref` <a class=\"md-anchor\" id=\"IdentityReader.reader_ref\"></a>\r\n\r\nOp that implements the reader.\r\n\r\n- - -\r\n\r\n#### `tf.IdentityReader.reset(name=None)` <a class=\"md-anchor\" id=\"IdentityReader.reset\"></a>\r\n\r\nRestore a reader to its initial clean state.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The created Operation.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.IdentityReader.restore_state(state, name=None)` <a class=\"md-anchor\" id=\"IdentityReader.restore_state\"></a>\r\n\r\nRestore a reader to a previously saved state.\r\n\r\nNot all Readers support being restored, so this can produce an\r\nUnimplemented error.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`state`</b>: A string Tensor.\r\n    Result of a SerializeState of a Reader with matching type.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The created Operation.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.IdentityReader.serialize_state(name=None)` <a class=\"md-anchor\" id=\"IdentityReader.serialize_state\"></a>\r\n\r\nProduce a string tensor that encodes the state of a reader.\r\n\r\nNot all Readers support being serialized, so this can produce an\r\nUnimplemented error.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A string Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.IdentityReader.supports_serialize` <a class=\"md-anchor\" id=\"IdentityReader.supports_serialize\"></a>\r\n\r\nWhether the Reader implementation can serialize its state.\r\n\r\n\r\n- - -\r\n\r\n### `class tf.TFRecordReader` <a class=\"md-anchor\" id=\"TFRecordReader\"></a>\r\n\r\nA Reader that outputs the records from a TFRecords file.\r\n\r\nSee ReaderBase for supported methods.\r\n- - -\r\n\r\n#### `tf.TFRecordReader.__init__(name=None)` <a class=\"md-anchor\" id=\"TFRecordReader.__init__\"></a>\r\n\r\nCreate a TFRecordReader.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TFRecordReader.num_records_produced(name=None)` <a class=\"md-anchor\" id=\"TFRecordReader.num_records_produced\"></a>\r\n\r\nReturns the number of records this reader has produced.\r\n\r\nThis is the same as the number of Read executions that have\r\nsucceeded.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An int64 Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TFRecordReader.num_work_units_completed(name=None)` <a class=\"md-anchor\" id=\"TFRecordReader.num_work_units_completed\"></a>\r\n\r\nReturns the number of work units this reader has finished processing.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An int64 Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TFRecordReader.read(queue, name=None)` <a class=\"md-anchor\" id=\"TFRecordReader.read\"></a>\r\n\r\nReturns the next record (key, value pair) produced by a reader.\r\n\r\nWill dequeue a work unit from queue if necessary (e.g. when the\r\nReader needs to start reading from a new file since it has\r\nfinished with the previous file).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`queue`</b>: A Queue or a mutable string Tensor representing a handle\r\n    to a Queue, with string work items.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tuple of Tensors (key, value).\r\n\r\n*  <b>`key`</b>: A string scalar Tensor.\r\n*  <b>`value`</b>: A string scalar Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TFRecordReader.reader_ref` <a class=\"md-anchor\" id=\"TFRecordReader.reader_ref\"></a>\r\n\r\nOp that implements the reader.\r\n\r\n- - -\r\n\r\n#### `tf.TFRecordReader.reset(name=None)` <a class=\"md-anchor\" id=\"TFRecordReader.reset\"></a>\r\n\r\nRestore a reader to its initial clean state.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The created Operation.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TFRecordReader.restore_state(state, name=None)` <a class=\"md-anchor\" id=\"TFRecordReader.restore_state\"></a>\r\n\r\nRestore a reader to a previously saved state.\r\n\r\nNot all Readers support being restored, so this can produce an\r\nUnimplemented error.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`state`</b>: A string Tensor.\r\n    Result of a SerializeState of a Reader with matching type.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The created Operation.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TFRecordReader.serialize_state(name=None)` <a class=\"md-anchor\" id=\"TFRecordReader.serialize_state\"></a>\r\n\r\nProduce a string tensor that encodes the state of a reader.\r\n\r\nNot all Readers support being serialized, so this can produce an\r\nUnimplemented error.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A string Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.TFRecordReader.supports_serialize` <a class=\"md-anchor\" id=\"TFRecordReader.supports_serialize\"></a>\r\n\r\nWhether the Reader implementation can serialize its state.\r\n\r\n\r\n- - -\r\n\r\n### `class tf.FixedLengthRecordReader` <a class=\"md-anchor\" id=\"FixedLengthRecordReader\"></a>\r\n\r\nA Reader that outputs fixed-length records from a file.\r\n\r\nSee ReaderBase for supported methods.\r\n- - -\r\n\r\n#### `tf.FixedLengthRecordReader.__init__(record_bytes, header_bytes=None, footer_bytes=None, name=None)` <a class=\"md-anchor\" id=\"FixedLengthRecordReader.__init__\"></a>\r\n\r\nCreate a FixedLengthRecordReader.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`record_bytes`</b>: An int.\r\n*  <b>`header_bytes`</b>: An optional int. Defaults to 0.\r\n*  <b>`footer_bytes`</b>: An optional int. Defaults to 0.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n\r\n- - -\r\n\r\n#### `tf.FixedLengthRecordReader.num_records_produced(name=None)` <a class=\"md-anchor\" id=\"FixedLengthRecordReader.num_records_produced\"></a>\r\n\r\nReturns the number of records this reader has produced.\r\n\r\nThis is the same as the number of Read executions that have\r\nsucceeded.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An int64 Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.FixedLengthRecordReader.num_work_units_completed(name=None)` <a class=\"md-anchor\" id=\"FixedLengthRecordReader.num_work_units_completed\"></a>\r\n\r\nReturns the number of work units this reader has finished processing.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An int64 Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.FixedLengthRecordReader.read(queue, name=None)` <a class=\"md-anchor\" id=\"FixedLengthRecordReader.read\"></a>\r\n\r\nReturns the next record (key, value pair) produced by a reader.\r\n\r\nWill dequeue a work unit from queue if necessary (e.g. when the\r\nReader needs to start reading from a new file since it has\r\nfinished with the previous file).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`queue`</b>: A Queue or a mutable string Tensor representing a handle\r\n    to a Queue, with string work items.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tuple of Tensors (key, value).\r\n\r\n*  <b>`key`</b>: A string scalar Tensor.\r\n*  <b>`value`</b>: A string scalar Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.FixedLengthRecordReader.reader_ref` <a class=\"md-anchor\" id=\"FixedLengthRecordReader.reader_ref\"></a>\r\n\r\nOp that implements the reader.\r\n\r\n- - -\r\n\r\n#### `tf.FixedLengthRecordReader.reset(name=None)` <a class=\"md-anchor\" id=\"FixedLengthRecordReader.reset\"></a>\r\n\r\nRestore a reader to its initial clean state.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The created Operation.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.FixedLengthRecordReader.restore_state(state, name=None)` <a class=\"md-anchor\" id=\"FixedLengthRecordReader.restore_state\"></a>\r\n\r\nRestore a reader to a previously saved state.\r\n\r\nNot all Readers support being restored, so this can produce an\r\nUnimplemented error.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`state`</b>: A string Tensor.\r\n    Result of a SerializeState of a Reader with matching type.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The created Operation.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.FixedLengthRecordReader.serialize_state(name=None)` <a class=\"md-anchor\" id=\"FixedLengthRecordReader.serialize_state\"></a>\r\n\r\nProduce a string tensor that encodes the state of a reader.\r\n\r\nNot all Readers support being serialized, so this can produce an\r\nUnimplemented error.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A string Tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.FixedLengthRecordReader.supports_serialize` <a class=\"md-anchor\" id=\"FixedLengthRecordReader.supports_serialize\"></a>\r\n\r\nWhether the Reader implementation can serialize its state.\r\n\r\n\r\n\r\n## Converting <a class=\"md-anchor\" id=\"AUTOGENERATED-converting\"></a>\r\n\r\nTensorFlow provides several operations that you can use to convert various data\r\nformats into tensors.\r\n\r\n- - -\r\n\r\n### `tf.decode_csv(records, record_defaults, field_delim=None, name=None)` <a class=\"md-anchor\" id=\"decode_csv\"></a>\r\n\r\nConvert CSV records to tensors. Each column maps to one tensor.\r\n\r\nRFC 4180 format is expected for the CSV records.\r\n(https://tools.ietf.org/html/rfc4180)\r\nNote that we allow leading and trailing spaces with int or float field.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`records`</b>: A `Tensor` of type `string`.\r\n    Each string is a record/row in the csv and all records should have\r\n    the same format.\r\n*  <b>`record_defaults`</b>: A list of `Tensor` objects with types from: `float32`, `int32`, `int64`, `string`.\r\n    One tensor per column of the input record, with either a\r\n    scalar default value for that column or empty if the column is required.\r\n*  <b>`field_delim`</b>: An optional `string`. Defaults to `\",\"`.\r\n    delimiter to separate fields in a record.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of `Tensor` objects. Has the same type as `record_defaults`.\r\n  Each tensor will have the same shape as records.\r\n\r\n\r\n- - -\r\n\r\n### `tf.decode_raw(bytes, out_type, little_endian=None, name=None)` <a class=\"md-anchor\" id=\"decode_raw\"></a>\r\n\r\nReinterpret the bytes of a string as a vector of numbers.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`bytes`</b>: A `Tensor` of type `string`.\r\n    All the elements must have the same length.\r\n*  <b>`out_type`</b>: A `tf.DType` from: `tf.float32, tf.float64, tf.int32, tf.uint8, tf.int16, tf.int8, tf.int64`.\r\n*  <b>`little_endian`</b>: An optional `bool`. Defaults to `True`.\r\n    Whether the input bytes are in little-endian order.\r\n    Ignored for out_types that are stored in a single byte like uint8.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `out_type`.\r\n  A Tensor with one more dimension than the input bytes.  The\r\n  added dimension will have size equal to the length of the elements\r\n  of bytes divided by the number of bytes to represent out_type.\r\n\r\n\r\n\r\n- - -\r\n\r\n### Example protocol buffer <a class=\"md-anchor\" id=\"AUTOGENERATED-example-protocol-buffer\"></a>\r\n\r\nTensorFlow's [recommended format for training\r\nexamples](../../how_tos/reading_data/index.md#standard-tensorflow-format)\r\nis serialized `Example` protocol buffers, [described\r\nhere](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/example.proto).\r\nThey contain `Features`, [described\r\nhere](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/feature.proto).\r\n\r\n- - -\r\n\r\n### `tf.parse_example(serialized, names=None, sparse_keys=None, sparse_types=None, dense_keys=None, dense_types=None, dense_defaults=None, dense_shapes=None, name='ParseExample')` <a class=\"md-anchor\" id=\"parse_example\"></a>\r\n\r\nParses `Example` protos.\r\n\r\nParses a number of serialized [`Example`]\r\n(https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/example.proto)\r\nprotos given in `serialized`.\r\n\r\n`names` may contain descriptive names for the corresponding serialized protos.\r\nThese may be useful for debugging purposes, but they have no effect on the\r\noutput. If not `None`, `names` must be the same length as `serialized`.\r\n\r\nThis op parses serialized examples into a dictionary mapping keys to `Tensor`\r\nand `SparseTensor` objects respectively, depending on whether the keys appear\r\nin `sparse_keys` or `dense_keys`.\r\n\r\nThe key `dense_keys[j]` is mapped to a `Tensor` of type `dense_types[j]` and\r\nof shape `(serialized.size(),) + dense_shapes[j]`.\r\n\r\n`dense_defaults` provides defaults for values referenced using `dense_keys`.\r\nIf a key is not present in this dictionary, the corresponding dense `Feature`\r\nis required in all elements of `serialized`.\r\n\r\n`dense_shapes[j]` provides the shape of each `Feature` entry referenced by\r\n`dense_keys[j]`. The number of elements in the `Feature` corresponding to\r\n`dense_key[j]` must always have `np.prod(dense_shapes[j])` entries. The\r\nreturned `Tensor` for `dense_key[j]` has shape `[N] + dense_shape[j]`, where\r\n`N` is the number of `Example`s in `serialized`.\r\n\r\nThe key `sparse_keys[j]` is mapped to a `SparseTensor` of type\r\n`sparse_types[j]`. The `SparseTensor` represents a ragged matrix.\r\nIts indices are `[batch, index]` where `batch` is the batch entry the value\r\nis from, and `index` is the value's index in the list of values associated\r\nwith that feature and example.\r\n\r\nExamples:\r\n\r\nFor example, if one expects a `tf.float32` sparse feature `ft` and three\r\nserialized `Example`s are provided:\r\n\r\n```\r\nserialized = [\r\n  features:\r\n    { feature: [ key: { \"ft\" value: float_list: { value: [1.0, 2.0] } } ] },\r\n  features:\r\n    { feature: [] },\r\n  features:\r\n    { feature: [ key: { \"ft\" value: float_list: { value: [3.0] } } ] }\r\n]\r\n```\r\n\r\nthen the output will look like:\r\n\r\n```\r\n{\"ft\": SparseTensor(indices=[[0, 0], [0, 1], [2, 0]],\r\n                    values=[1.0, 2.0, 3.0],\r\n                    shape=(3, 2)) }\r\n```\r\n\r\nGiven two `Example` input protos in `serialized`:\r\n\r\n```\r\n[\r\n  features: {\r\n    feature: { key: \"kw\" value: { bytes_list: { value: [ \"knit\", \"big\" ] } } }\r\n    feature: { key: \"gps\" value: { float_list: { value: [] } } }\r\n  },\r\n  features: {\r\n    feature: { key: \"kw\" value: { bytes_list: { value: [ \"emmy\" ] } } }\r\n    feature: { key: \"dank\" value: { int64_list: { value: [ 42 ] } } }\r\n    feature: { key: \"gps\" value: { } }\r\n  }\r\n]\r\n```\r\n\r\nAnd arguments\r\n\r\n```\r\n  names: [\"input0\", \"input1\"],\r\n  sparse_keys: [\"kw\", \"dank\", \"gps\"]\r\n  sparse_types: [DT_STRING, DT_INT64, DT_FLOAT]\r\n```\r\n\r\nThen the output is a dictionary:\r\n\r\n```python\r\n{\r\n  \"kw\": SparseTensor(\r\n      indices=[[0, 0], [0, 1], [1, 0]],\r\n      values=[\"knit\", \"big\", \"emmy\"]\r\n      shape=[2, 2]),\r\n  \"dank\": SparseTensor(\r\n      indices=[[1, 0]],\r\n      values=[42],\r\n      shape=[2, 1]),\r\n  \"gps\": SparseTensor(\r\n      indices=[],\r\n      values=[],\r\n      shape=[2, 0]),\r\n}\r\n```\r\n\r\nFor dense results in two serialized `Example`s:\r\n\r\n```\r\n[\r\n  features: {\r\n    feature: { key: \"age\" value: { int64_list: { value: [ 0 ] } } }\r\n    feature: { key: \"gender\" value: { bytes_list: { value: [ \"f\" ] } } }\r\n   },\r\n   features: {\r\n    feature: { key: \"age\" value: { int64_list: { value: [] } } }\r\n    feature: { key: \"gender\" value: { bytes_list: { value: [ \"f\" ] } } }\r\n  }\r\n]\r\n```\r\n\r\nWe can use arguments:\r\n\r\n```\r\nnames: [\"input0\", \"input1\"],\r\ndense_keys: np.array([\"age\", \"gender\"]),\r\ndense_types: [tf.int64, tf.string],\r\ndense_defaults: {\r\n  \"age\": -1  # \"age\" defaults to -1 if missing\r\n             # \"gender\" has no specified default so it's required\r\n}\r\ndense_shapes: [(1,), (1,)],  # age, gender, label, weight\r\n```\r\n\r\nAnd the expected output is:\r\n\r\n```python\r\n{\r\n  \"age\": [[0], [-1]],\r\n  \"gender\": [[\"f\"], [\"f\"]],\r\n}\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`serialized`</b>: A list of strings, a batch of binary serialized `Example`\r\n    protos.\r\n*  <b>`names`</b>: A list of strings, the names of the serialized protos.\r\n*  <b>`sparse_keys`</b>: A list of string keys in the examples' features.\r\n    The results for these keys will be returned as `SparseTensor` objects.\r\n*  <b>`sparse_types`</b>: A list of `DTypes` of the same length as `sparse_keys`.\r\n    Only `tf.float32` (`FloatList`), `tf.int64` (`Int64List`),\r\n    and `tf.string` (`BytesList`) are supported.\r\n*  <b>`dense_keys`</b>: A list of string keys in the examples' features.\r\n    The results for these keys will be returned as `Tensor`s\r\n*  <b>`dense_types`</b>: A list of DTypes of the same length as `dense_keys`.\r\n    Only `tf.float32` (`FloatList`), `tf.int64` (`Int64List`),\r\n    and `tf.string` (`BytesList`) are supported.\r\n*  <b>`dense_defaults`</b>: A dict mapping string keys to `Tensor`s.\r\n    The keys of the dict must match the dense_keys of the feature.\r\n*  <b>`dense_shapes`</b>: A list of tuples with the same length as `dense_keys`.\r\n    The shape of the data for each dense feature referenced by `dense_keys`.\r\n*  <b>`name`</b>: A name for this operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `dict` mapping keys to `Tensor`s and `SparseTensor`s.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If sparse and dense key sets intersect, or input lengths do not\r\n    match up.\r\n\r\n\r\n- - -\r\n\r\n### `tf.parse_single_example(serialized, names=None, sparse_keys=None, sparse_types=None, dense_keys=None, dense_types=None, dense_defaults=None, dense_shapes=None, name='ParseSingleExample')` <a class=\"md-anchor\" id=\"parse_single_example\"></a>\r\n\r\nParses a single `Example` proto.\r\n\r\nSimilar to `parse_example`, except:\r\n\r\nFor dense tensors, the returned `Tensor` is identical to the output of\r\n`parse_example`, except there is no batch dimension, the output shape is the\r\nsame as the shape given in `dense_shape`.\r\n\r\nFor `SparseTensor`s, the first (batch) column of the indices matrix is removed\r\n(the indices matrix is a column vector), the values vector is unchanged, and\r\nthe first (batch_size) entry of the shape vector is removed (it is now a\r\nsingle element vector).\r\n\r\nSee also `parse_example`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`serialized`</b>: A scalar string, a single serialized Example.\r\n    See parse_example documentation for more details.\r\n*  <b>`names`</b>: (Optional) A scalar string, the associated name.\r\n    See parse_example documentation for more details.\r\n*  <b>`sparse_keys`</b>: See parse_example documentation for more details.\r\n*  <b>`sparse_types`</b>: See parse_example documentation for more details.\r\n*  <b>`dense_keys`</b>: See parse_example documentation for more details.\r\n*  <b>`dense_types`</b>: See parse_example documentation for more details.\r\n*  <b>`dense_defaults`</b>: See parse_example documentation for more details.\r\n*  <b>`dense_shapes`</b>: See parse_example documentation for more details.\r\n*  <b>`name`</b>: A name for this operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A dictionary mapping keys to Tensors and SparseTensors.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if \"scalar\" or \"names\" have known shapes, and are not scalars.\r\n\r\n\r\n\r\n## Queues <a class=\"md-anchor\" id=\"AUTOGENERATED-queues\"></a>\r\n\r\nTensorFlow provides several implementations of 'Queues', which are\r\nstructures within the TensorFlow computation graph to stage pipelines\r\nof tensors together. The following describe the basic Queue interface\r\nand some implementations.  To see an example use, see [Threading and\r\nQueues](../../how_tos/threading_and_queues/index.md).\r\n\r\n- - -\r\n\r\n### `class tf.QueueBase` <a class=\"md-anchor\" id=\"QueueBase\"></a>\r\n\r\nBase class for queue implementations.\r\n\r\nA queue is a TensorFlow data structure that stores tensors across\r\nmultiple steps, and exposes operations that enqueue and dequeue\r\ntensors.\r\n\r\nEach queue element is a tuple of one or more tensors, where each\r\ntuple component has a static dtype, and may have a static shape. The\r\nqueue implementations support versions of enqueue and dequeue that\r\nhandle single elements, versions that support enqueuing and\r\ndequeuing a batch of elements at once.\r\n\r\nSee [`tf.FIFOQueue`](#FIFOQueue) and\r\n[`tf.RandomShuffleQueue`](#RandomShuffleQueue) for concrete\r\nimplementations of this class, and instructions on how to create\r\nthem.\r\n\r\n- - -\r\n\r\n#### `tf.QueueBase.enqueue(vals, name=None)` <a class=\"md-anchor\" id=\"QueueBase.enqueue\"></a>\r\n\r\nEnqueues one element to this queue.\r\n\r\nIf the queue is full when this operation executes, it will block\r\nuntil the element has been enqueued.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`vals`</b>: The tuple of `Tensor` objects to be enqueued.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The operation that enqueues a new tuple of tensors to the queue.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.QueueBase.enqueue_many(vals, name=None)` <a class=\"md-anchor\" id=\"QueueBase.enqueue_many\"></a>\r\n\r\nEnqueues zero or elements to this queue.\r\n\r\nThis operation slices each component tensor along the 0th dimension to\r\nmake multiple queue elements. All of the tensors in `vals` must have the\r\nsame size in the 0th dimension.\r\n\r\nIf the queue is full when this operation executes, it will block\r\nuntil all of the elements have been enqueued.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`vals`</b>: The tensor or tuple of tensors from which the queue elements\r\n    are taken.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The operation that enqueues a batch of tuples of tensors to the queue.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.QueueBase.dequeue(name=None)` <a class=\"md-anchor\" id=\"QueueBase.dequeue\"></a>\r\n\r\nDequeues one element from this queue.\r\n\r\nIf the queue is empty when this operation executes, it will block\r\nuntil there is an element to dequeue.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The tuple of tensors that was dequeued.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.QueueBase.dequeue_many(n, name=None)` <a class=\"md-anchor\" id=\"QueueBase.dequeue_many\"></a>\r\n\r\nDequeues and concatenates `n` elements from this queue.\r\n\r\nThis operation concatenates queue-element component tensors along\r\nthe 0th dimension to make a single component tensor.  All of the\r\ncomponents in the dequeued tuple will have size `n` in the 0th dimension.\r\n\r\nIf the queue contains fewer than `n` elements when this operation\r\nexecutes, it will block until `n` elements have been dequeued.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`n`</b>: A scalar `Tensor` containing the number of elements to dequeue.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The tuple of concatenated tensors that was dequeued.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.QueueBase.size(name=None)` <a class=\"md-anchor\" id=\"QueueBase.size\"></a>\r\n\r\nCompute the number of elements in this queue.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A scalar tensor containing the number of elements in this queue.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.QueueBase.close(cancel_pending_enqueues=False, name=None)` <a class=\"md-anchor\" id=\"QueueBase.close\"></a>\r\n\r\nCloses this queue.\r\n\r\nThis operation signals that no more elements will be enqueued in\r\nthe given queue. Subsequent `enqueue` and `enqueue_many`\r\noperations will fail. Subsequent `dequeue` and `dequeue_many`\r\noperations will continue to succeed if sufficient elements remain\r\nin the queue. Subsequent `dequeue` and `dequeue_many` operations\r\nthat would block will fail immediately.\r\n\r\nIf `cancel_pending_enqueues` is `True`, all pending requests will also\r\nbe cancelled.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`cancel_pending_enqueues`</b>: (Optional.) A boolean, defaulting to\r\n    `False` (described above).\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The operation that closes the queue.\r\n\r\n\r\n\r\n#### Other Methods <a class=\"md-anchor\" id=\"AUTOGENERATED-other-methods\"></a>\r\n- - -\r\n\r\n#### `tf.QueueBase.__init__(dtypes, shapes, queue_ref)` <a class=\"md-anchor\" id=\"QueueBase.__init__\"></a>\r\n\r\nConstructs a queue object from a queue reference.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`dtypes`</b>: A list of types.  The length of dtypes must equal the number\r\n    of tensors in each element.\r\n*  <b>`shapes`</b>: Constraints on the shapes of tensors in an element:\r\n    A list of shape tuples or None. This list is the same length\r\n    as dtypes.  If the shape of any tensors in the element are constrained,\r\n    all must be; shapes can be None if the shapes should not be constrained.\r\n*  <b>`queue_ref`</b>: The queue reference, i.e. the output of the queue op.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.QueueBase.dtypes` <a class=\"md-anchor\" id=\"QueueBase.dtypes\"></a>\r\n\r\nThe list of dtypes for each component of a queue element.\r\n\r\n- - -\r\n\r\n#### `tf.QueueBase.name` <a class=\"md-anchor\" id=\"QueueBase.name\"></a>\r\n\r\nThe name of the underlying queue.\r\n\r\n- - -\r\n\r\n#### `tf.QueueBase.queue_ref` <a class=\"md-anchor\" id=\"QueueBase.queue_ref\"></a>\r\n\r\nThe underlying queue reference.\r\n\r\n\r\n- - -\r\n\r\n### `class tf.FIFOQueue` <a class=\"md-anchor\" id=\"FIFOQueue\"></a>\r\n\r\nA queue implementation that dequeues elements in first-in-first out order.\r\n\r\nSee [`tf.QueueBase`](#QueueBase) for a description of the methods on\r\nthis class.\r\n\r\n- - -\r\n\r\n#### `tf.FIFOQueue.__init__(capacity, dtypes, shapes=None, shared_name=None, name='fifo_queue')` <a class=\"md-anchor\" id=\"FIFOQueue.__init__\"></a>\r\n\r\nCreates a queue that dequeues elements in a first-in first-out order.\r\n\r\nA `FIFOQueue` has bounded capacity; supports multiple concurrent\r\nproducers and consumers; and provides exactly-once delivery.\r\n\r\nA `FIFOQueue` holds a list of up to `capacity` elements. Each\r\nelement is a fixed-length tuple of tensors whose dtypes are\r\ndescribed by `dtypes`, and whose shapes are optionally described\r\nby the `shapes` argument.\r\n\r\nIf the `shapes` argument is specified, each component of a queue\r\nelement must have the respective fixed shape. If it is\r\nunspecified, different queue elements may have different shapes,\r\nbut the use of `dequeue_many` is disallowed.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`capacity`</b>: An integer. The upper bound on the number of elements\r\n    that may be stored in this queue.\r\n*  <b>`dtypes`</b>: A list of `DType` objects. The length of `dtypes` must equal\r\n    the number of tensors in each queue element.\r\n*  <b>`shapes`</b>: (Optional.) A list of fully-defined `TensorShape` objects,\r\n    with the same length as `dtypes` or `None`.\r\n*  <b>`shared_name`</b>: (Optional.) If non-empty, this queue will be shared under\r\n    the given name across multiple sessions.\r\n*  <b>`name`</b>: Optional name for the queue operation.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.RandomShuffleQueue` <a class=\"md-anchor\" id=\"RandomShuffleQueue\"></a>\r\n\r\nA queue implementation that dequeues elements in a random order.\r\n\r\nSee [`tf.QueueBase`](#QueueBase) for a description of the methods on\r\nthis class.\r\n\r\n- - -\r\n\r\n#### `tf.RandomShuffleQueue.__init__(capacity, min_after_dequeue, dtypes, shapes=None, seed=None, shared_name=None, name='random_shuffle_queue')` <a class=\"md-anchor\" id=\"RandomShuffleQueue.__init__\"></a>\r\n\r\nCreate a queue that dequeues elements in a random order.\r\n\r\nA `RandomShuffleQueue` has bounded capacity; supports multiple\r\nconcurrent producers and consumers; and provides exactly-once\r\ndelivery.\r\n\r\nA `RandomShuffleQueue` holds a list of up to `capacity`\r\nelements. Each element is a fixed-length tuple of tensors whose\r\ndtypes are described by `dtypes`, and whose shapes are optionally\r\ndescribed by the `shapes` argument.\r\n\r\nIf the `shapes` argument is specified, each component of a queue\r\nelement must have the respective fixed shape. If it is\r\nunspecified, different queue elements may have different shapes,\r\nbut the use of `dequeue_many` is disallowed.\r\n\r\nThe `min_after_dequeue` argument allows the caller to specify a\r\nminimum number of elements that will remain in the queue after a\r\n`dequeue` or `dequeue_many` operation completes, to ensure a\r\nminimum level of mixing of elements. This invariant is maintained\r\nby blocking those operations until sufficient elements have been\r\nenqueued. The `min_after_dequeue` argument is ignored after the\r\nqueue has been closed.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`capacity`</b>: An integer. The upper bound on the number of elements\r\n    that may be stored in this queue.\r\n*  <b>`min_after_dequeue`</b>: An integer (described above).\r\n*  <b>`dtypes`</b>: A list of `DType` objects. The length of `dtypes` must equal\r\n    the number of tensors in each queue element.\r\n*  <b>`shapes`</b>: (Optional.) A list of fully-defined `TensorShape` objects,\r\n    with the same length as `dtypes` or `None`.\r\n*  <b>`seed`</b>: A Python integer. Used to create a random seed. See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n*  <b>`shared_name`</b>: (Optional.) If non-empty, this queue will be shared under\r\n    the given name across multiple sessions.\r\n*  <b>`name`</b>: Optional name for the queue operation.\r\n\r\n\r\n\r\n\r\n## Dealing with the filesystem <a class=\"md-anchor\" id=\"AUTOGENERATED-dealing-with-the-filesystem\"></a>\r\n\r\n- - -\r\n\r\n### `tf.matching_files(pattern, name=None)` <a class=\"md-anchor\" id=\"matching_files\"></a>\r\n\r\nReturns the set of files matching a pattern.\r\n\r\nNote that this routine only supports wildcard characters in the\r\nbasename portion of the pattern, not in the directory portion.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`pattern`</b>: A `Tensor` of type `string`. A (scalar) shell wildcard pattern.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `string`. A vector of matching filenames.\r\n\r\n\r\n- - -\r\n\r\n### `tf.read_file(filename, name=None)` <a class=\"md-anchor\" id=\"read_file\"></a>\r\n\r\nReads and outputs the entire contents of the input filename.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`filename`</b>: A `Tensor` of type `string`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `string`.\r\n\r\n\r\n\r\n## Input pipeline <a class=\"md-anchor\" id=\"AUTOGENERATED-input-pipeline\"></a>\r\n\r\nTensorFlow functions for setting up an input-prefetching pipeline.\r\nPlease see the [reading data how-to](../../how_tos/reading_data/index.md)\r\nfor context.\r\n\r\n### Beginning of an input pipeline <a class=\"md-anchor\" id=\"AUTOGENERATED-beginning-of-an-input-pipeline\"></a>\r\n\r\nThe \"producer\" functions add a queue to the graph and a corresponding\r\n`QueueRunner` for running the subgraph that fills that queue.\r\n\r\n- - -\r\n\r\n### `tf.train.match_filenames_once(pattern, name=None)` <a class=\"md-anchor\" id=\"match_filenames_once\"></a>\r\n\r\nSave the list of files matching pattern, so it is only computed once.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`pattern`</b>: A file pattern (glob).\r\n*  <b>`name`</b>: A name for the operations (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A variable that is initialized to the list of files matching pattern.\r\n\r\n\r\n- - -\r\n\r\n### `tf.train.limit_epochs(tensor, num_epochs=None, name=None)` <a class=\"md-anchor\" id=\"limit_epochs\"></a>\r\n\r\nReturns tensor num_epochs times and then raises an OutOfRange error.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tensor`</b>: Any Tensor.\r\n*  <b>`num_epochs`</b>: An integer (optional).  If specified, limits the number\r\n    of steps the output tensor may be evaluated.\r\n*  <b>`name`</b>: A name for the operations (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  tensor or OutOfRange.\r\n\r\n\r\n- - -\r\n\r\n### `tf.train.range_input_producer(limit, num_epochs=None, shuffle=True, seed=None, capacity=32, name=None)` <a class=\"md-anchor\" id=\"range_input_producer\"></a>\r\n\r\nProduces the integers from 0 to limit-1 in a queue.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`limit`</b>: An int32 scalar tensor.\r\n*  <b>`num_epochs`</b>: An integer (optional). If specified, `range_input_producer`\r\n    produces each integer `num_epochs` times before generating an\r\n    OutOfRange error. If not specified, `range_input_producer` can cycle\r\n    through the integers an unlimited number of times.\r\n*  <b>`shuffle`</b>: Boolean. If true, the integers are randomly shuffled within each\r\n    epoch.\r\n*  <b>`seed`</b>: An integer (optional). Seed used if shuffle == True.\r\n*  <b>`capacity`</b>: An integer. Sets the queue capacity.\r\n*  <b>`name`</b>: A name for the operations (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A Queue with the output integers.  A QueueRunner for the Queue\r\n  is added to the current Graph's QUEUE_RUNNER collection.\r\n\r\n\r\n- - -\r\n\r\n### `tf.train.slice_input_producer(tensor_list, num_epochs=None, shuffle=True, seed=None, capacity=32, name=None)` <a class=\"md-anchor\" id=\"slice_input_producer\"></a>\r\n\r\nProduces a slice of each Tensor in tensor_list.\r\n\r\nImplemented using a Queue -- a QueueRunner for the Queue\r\nis added to the current Graph's QUEUE_RUNNER collection.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tensor_list`</b>: A list of Tensors. Every Tensor in tensor_list must\r\n    have the same size in the first dimension.\r\n*  <b>`num_epochs`</b>: An integer (optional). If specified, `slice_input_producer`\r\n    produces each slice `num_epochs` times before generating\r\n    an OutOfRange error. If not specified, `slice_input_producer` can cycle\r\n    through the slices an unlimited number of times.\r\n*  <b>`seed`</b>: An integer (optional). Seed used if shuffle == True.\r\n*  <b>`capacity`</b>: An integer. Sets the queue capacity.\r\n*  <b>`name`</b>: A name for the operations (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of tensors, one for each element of tensor_list.  If the tensor\r\n  in tensor_list has shape [N, a, b, .., z], then the corresponding output\r\n  tensor will have shape [a, b, ..., z].\r\n\r\n\r\n- - -\r\n\r\n### `tf.train.string_input_producer(string_tensor, num_epochs=None, shuffle=True, seed=None, capacity=32, name=None)` <a class=\"md-anchor\" id=\"string_input_producer\"></a>\r\n\r\nOutput strings (e.g. filenames) to a queue for an input pipeline.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`string_tensor`</b>: A 1-D string tensor with the strings to produce.\r\n*  <b>`num_epochs`</b>: An integer (optional). If specified, `string_input_producer`\r\n    produces each string from `string_tensor` `num_epochs` times before\r\n    generating an OutOfRange error. If not specified, `string_input_producer`\r\n    can cycle through the strings in `string_tensor` an unlimited number of\r\n    times.\r\n*  <b>`shuffle`</b>: Boolean. If true, the strings are randomly shuffled within each\r\n    epoch.\r\n*  <b>`seed`</b>: An integer (optional). Seed used if shuffle == True.\r\n*  <b>`capacity`</b>: An integer. Sets the queue capacity.\r\n*  <b>`name`</b>: A name for the operations (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A queue with the output strings.  A QueueRunner for the Queue\r\n  is added to the current Graph's QUEUE_RUNNER collection.\r\n\r\n\r\n\r\n### Batching at the end of an input pipeline <a class=\"md-anchor\" id=\"AUTOGENERATED-batching-at-the-end-of-an-input-pipeline\"></a>\r\n\r\nThese functions add a queue to the graph to assemble a batch of examples, with\r\npossible shuffling.  They also add a `QueueRunner` for running the subgraph\r\nthat fills that queue.\r\n\r\nUse [batch](#batch) or [batch_join](#batch_join) for batching examples that have\r\nalready been well shuffled.  Use [shuffle_batch](#shuffle_batch) or\r\n[shuffle_batch_join](#shuffle_batch_join) for examples that\r\nwould benefit from additional shuffling.\r\n\r\nUse [batch](#batch) or [shuffle_batch](#shuffle_batch) if you want a\r\nsingle thread producing examples to batch, or if you have a\r\nsingle subgraph producing examples but you want to run it in N threads\r\n(where you increase N until it can keep the queue full).  Use\r\n[batch_join](#batch_join) or [shuffle_batch_join](#shuffle_batch_join)\r\nif you have N different subgraphs producing examples to batch and you\r\nwant them run by N threads.\r\n\r\n- - -\r\n\r\n### `tf.train.batch(tensor_list, batch_size, num_threads=1, capacity=32, enqueue_many=False, shapes=None, name=None)` <a class=\"md-anchor\" id=\"batch\"></a>\r\n\r\nCreates batches of tensors in `tensor_list`.\r\n\r\nThis function is implemented using a queue. A `QueueRunner` for the\r\nqueue is added to the current `Graph`'s `QUEUE_RUNNER` collection.\r\n\r\nIf `enqueue_many` is `False`, `tensor_list` is assumed to represent a\r\nsingle example.  An input tensor with shape `[x, y, z]` will be output\r\nas a tensor with shape `[batch_size, x, y, z]`.\r\n\r\nIf `enqueue_many` is `True`, `tensor_list` is assumed to represent a\r\nbatch of examples, where the first dimension is indexed by example,\r\nand all members of `tensor_list` should have the same size in the\r\nfirst dimension.  If an input tensor has shape `[*, x, y, z]`, the\r\noutput will have shape `[batch_size, x, y, z]`.  The `capacity` argument\r\ncontrols the how long the prefetching is allowed to grow the queues.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tensor_list`</b>: The list of tensors to enqueue.\r\n*  <b>`batch_size`</b>: The new batch size pulled from the queue.\r\n*  <b>`num_threads`</b>: The number of threads enqueuing `tensor_list`.\r\n*  <b>`capacity`</b>: An integer. The maximum number of elements in the queue.\r\n*  <b>`enqueue_many`</b>: Whether each tensor in `tensor_list` is a single example.\r\n*  <b>`shapes`</b>: (Optional) The shapes for each example.  Defaults to the\r\n    inferred shapes for `tensor_list`.\r\n*  <b>`name`</b>: (Optional) A name for the operations.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of tensors with the same number and types as `tensor_list`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.train.batch_join(tensor_list_list, batch_size, capacity=32, enqueue_many=False, shapes=None, name=None)` <a class=\"md-anchor\" id=\"batch_join\"></a>\r\n\r\nRuns a list of tensors to fill a queue to create batches of examples.\r\n\r\nEnqueues a different list of tensors in different threads.\r\nImplemented using a queue -- a `QueueRunner` for the queue\r\nis added to the current `Graph`'s `QUEUE_RUNNER` collection.\r\n\r\n`len(tensor_list_list)` threads will be started,\r\nwith thread `i` enqueuing the tensors from\r\n`tensor_list_list[i]`. `tensor_list_list[i1][j]` must match\r\n`tensor_list_list[i2][j]` in type and shape, except in the first\r\ndimension if `enqueue_many` is true.\r\n\r\nIf `enqueue_many` is `False`, each `tensor_list_list[i]` is assumed\r\nto represent a single example. An input tensor `x` will be output as a\r\ntensor with shape `[batch_size] + x.shape`.\r\n\r\nIf `enqueue_many` is `True`, `tensor_list_list[i]` is assumed to\r\nrepresent a batch of examples, where the first dimension is indexed\r\nby example, and all members of `tensor_list_list[i]` should have the\r\nsame size in the first dimension.  The slices of any input tensor\r\n`x` are treated as examples, and the output tensors will have shape\r\n`[batch_size] + x.shape[1:]`.\r\n\r\nThe `capacity` argument controls the how long the prefetching is allowed to\r\ngrow the queues.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tensor_list_list`</b>: A list of tuples of tensors to enqueue.\r\n*  <b>`batch_size`</b>: An integer. The new batch size pulled from the queue.\r\n*  <b>`capacity`</b>: An integer. The maximum number of elements in the queue.\r\n*  <b>`enqueue_many`</b>: Whether each tensor in `tensor_list_list` is a single\r\n    example.\r\n*  <b>`shapes`</b>: (Optional) The shapes for each example.  Defaults to the\r\n    inferred shapes for `tensor_list_list[i]`.\r\n*  <b>`name`</b>: (Optional) A name for the operations.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of tensors with the same number and types as\r\n  `tensor_list_list[i]`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.train.shuffle_batch(tensor_list, batch_size, capacity, min_after_dequeue, num_threads=1, seed=None, enqueue_many=False, shapes=None, name=None)` <a class=\"md-anchor\" id=\"shuffle_batch\"></a>\r\n\r\nCreates batches by randomly shuffling tensors.\r\n\r\nThis function adds the following to the current `Graph`:\r\n\r\n* A shuffling queue into which tensors from `tensor_list` are enqueued.\r\n* A `dequeue_many` operation to create batches from the queue.\r\n* A `QueueRunner` to `QUEUE_RUNNER` collection, to enqueue the tensors\r\n  from `tensor_list`.\r\n\r\nIf `enqueue_many` is `False`, `tensor_list` is assumed to represent a\r\nsingle example.  An input tensor with shape `[x, y, z]` will be output\r\nas a tensor with shape `[batch_size, x, y, z]`.\r\n\r\nIf `enqueue_many` is `True`, `tensor_list` is assumed to represent a\r\nbatch of examples, where the first dimension is indexed by example,\r\nand all members of `tensor_list` should have the same size in the\r\nfirst dimension.  If an input tensor has shape `[*, x, y, z]`, the\r\noutput will have shape `[batch_size, x, y, z]`.\r\n\r\nThe `capacity` argument controls the how long the prefetching is allowed to\r\ngrow the queues.\r\n\r\nFor example:\r\n\r\n```python\r\n# Creates batches of 32 images and 32 labels.\r\nimage_batch, label_batch = tf.train.shuffle_batch(\r\n      [single_image, single_label],\r\n      batch_size=32,\r\n      num_threads=4,\r\n      capacity=50000,\r\n      min_after_dequeue=10000)\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tensor_list`</b>: The list of tensors to enqueue.\r\n*  <b>`batch_size`</b>: The new batch size pulled from the queue.\r\n*  <b>`capacity`</b>: An integer. The maximum number of elements in the queue.\r\n*  <b>`min_after_dequeue`</b>: Minimum number elements in the queue after a\r\n    dequeue, used to ensure a level of mixing of elements.\r\n*  <b>`num_threads`</b>: The number of threads enqueuing `tensor_list`.\r\n*  <b>`seed`</b>: Seed for the random shuffling within the queue.\r\n*  <b>`enqueue_many`</b>: Whether each tensor in `tensor_list` is a single example.\r\n*  <b>`shapes`</b>: (Optional) The shapes for each example.  Defaults to the\r\n    inferred shapes for `tensor_list`.\r\n*  <b>`name`</b>: (Optional) A name for the operations.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of tensors with the same number and types as `tensor_list`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.train.shuffle_batch_join(tensor_list_list, batch_size, capacity, min_after_dequeue, seed=None, enqueue_many=False, shapes=None, name=None)` <a class=\"md-anchor\" id=\"shuffle_batch_join\"></a>\r\n\r\nCreate batches by randomly shuffling tensors.\r\n\r\nThis version enqueues a different list of tensors in different threads.\r\nIt adds the following to the current `Graph`:\r\n\r\n* A shuffling queue into which tensors from `tensor_list_list` are enqueued.\r\n* A `dequeue_many` operation to create batches from the queue.\r\n* A `QueueRunner` to `QUEUE_RUNNER` collection, to enqueue the tensors\r\n  from `tensor_list_list`.\r\n\r\n`len(tensor_list_list)` threads will be started, with thread `i` enqueuing\r\nthe tensors from `tensor_list_list[i]`. `tensor_list_list[i1][j]` must match\r\n`tensor_list_list[i2][j]` in type and shape, except in the first dimension if\r\n`enqueue_many` is true.\r\n\r\nIf `enqueue_many` is `False`, each `tensor_list_list[i]` is assumed\r\nto represent a single example.  An input tensor with shape `[x, y,\r\nz]` will be output as a tensor with shape `[batch_size, x, y, z]`.\r\n\r\nIf `enqueue_many` is `True`, `tensor_list_list[i]` is assumed to\r\nrepresent a batch of examples, where the first dimension is indexed\r\nby example, and all members of `tensor_list_list[i]` should have the\r\nsame size in the first dimension.  If an input tensor has shape `[*, x,\r\ny, z]`, the output will have shape `[batch_size, x, y, z]`.\r\n\r\nThe `capacity` argument controls the how long the prefetching is allowed to\r\ngrow the queues.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tensor_list_list`</b>: A list of tuples of tensors to enqueue.\r\n*  <b>`batch_size`</b>: An integer. The new batch size pulled from the queue.\r\n*  <b>`capacity`</b>: An integer. The maximum number of elements in the queue.\r\n*  <b>`min_after_dequeue`</b>: Minimum number elements in the queue after a\r\n    dequeue, used to ensure a level of mixing of elements.\r\n*  <b>`seed`</b>: Seed for the random shuffling within the queue.\r\n*  <b>`enqueue_many`</b>: Whether each tensor in `tensor_list_list` is a single\r\n    example.\r\n*  <b>`shapes`</b>: (Optional) The shapes for each example.  Defaults to the\r\n    inferred shapes for `tensor_list_list[i]`.\r\n*  <b>`name`</b>: (Optional) A name for the operations.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of tensors with the same number and types as `tensor_list_list[i]`.\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/python/math_ops.md",
    "content": "<!-- This file is machine generated: DO NOT EDIT! -->\r\n\r\n# Math <a class=\"md-anchor\" id=\"AUTOGENERATED-math\"></a>\r\n\r\nNote: Functions taking `Tensor` arguments can also take anything accepted by\r\n[`tf.convert_to_tensor`](../../api_docs/python/framework.md#convert_to_tensor).\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Math](#AUTOGENERATED-math)\r\n* [Arithmetic Operators](#AUTOGENERATED-arithmetic-operators)\r\n  * [`tf.add(x, y, name=None)`](#add)\r\n  * [`tf.sub(x, y, name=None)`](#sub)\r\n  * [`tf.mul(x, y, name=None)`](#mul)\r\n  * [`tf.div(x, y, name=None)`](#div)\r\n  * [`tf.mod(x, y, name=None)`](#mod)\r\n* [Basic Math Functions](#AUTOGENERATED-basic-math-functions)\r\n  * [`tf.add_n(inputs, name=None)`](#add_n)\r\n  * [`tf.abs(x, name=None)`](#abs)\r\n  * [`tf.neg(x, name=None)`](#neg)\r\n  * [`tf.sign(x, name=None)`](#sign)\r\n  * [`tf.inv(x, name=None)`](#inv)\r\n  * [`tf.square(x, name=None)`](#square)\r\n  * [`tf.round(x, name=None)`](#round)\r\n  * [`tf.sqrt(x, name=None)`](#sqrt)\r\n  * [`tf.rsqrt(x, name=None)`](#rsqrt)\r\n  * [`tf.pow(x, y, name=None)`](#pow)\r\n  * [`tf.exp(x, name=None)`](#exp)\r\n  * [`tf.log(x, name=None)`](#log)\r\n  * [`tf.ceil(x, name=None)`](#ceil)\r\n  * [`tf.floor(x, name=None)`](#floor)\r\n  * [`tf.maximum(x, y, name=None)`](#maximum)\r\n  * [`tf.minimum(x, y, name=None)`](#minimum)\r\n  * [`tf.cos(x, name=None)`](#cos)\r\n  * [`tf.sin(x, name=None)`](#sin)\r\n* [Matrix Math Functions](#AUTOGENERATED-matrix-math-functions)\r\n  * [`tf.diag(diagonal, name=None)`](#diag)\r\n  * [`tf.transpose(a, perm=None, name='transpose')`](#transpose)\r\n  * [`tf.matmul(a, b, transpose_a=False, transpose_b=False, a_is_sparse=False, b_is_sparse=False, name=None)`](#matmul)\r\n  * [`tf.batch_matmul(x, y, adj_x=None, adj_y=None, name=None)`](#batch_matmul)\r\n  * [`tf.matrix_determinant(input, name=None)`](#matrix_determinant)\r\n  * [`tf.batch_matrix_determinant(input, name=None)`](#batch_matrix_determinant)\r\n  * [`tf.matrix_inverse(input, name=None)`](#matrix_inverse)\r\n  * [`tf.batch_matrix_inverse(input, name=None)`](#batch_matrix_inverse)\r\n  * [`tf.cholesky(input, name=None)`](#cholesky)\r\n  * [`tf.batch_cholesky(input, name=None)`](#batch_cholesky)\r\n* [Complex Number Functions](#AUTOGENERATED-complex-number-functions)\r\n  * [`tf.complex(real, imag, name=None)`](#complex)\r\n  * [`tf.complex_abs(x, name=None)`](#complex_abs)\r\n  * [`tf.conj(in_, name=None)`](#conj)\r\n  * [`tf.imag(in_, name=None)`](#imag)\r\n  * [`tf.real(in_, name=None)`](#real)\r\n* [Reduction](#AUTOGENERATED-reduction)\r\n  * [`tf.reduce_sum(input_tensor, reduction_indices=None, keep_dims=False, name=None)`](#reduce_sum)\r\n  * [`tf.reduce_prod(input_tensor, reduction_indices=None, keep_dims=False, name=None)`](#reduce_prod)\r\n  * [`tf.reduce_min(input_tensor, reduction_indices=None, keep_dims=False, name=None)`](#reduce_min)\r\n  * [`tf.reduce_max(input_tensor, reduction_indices=None, keep_dims=False, name=None)`](#reduce_max)\r\n  * [`tf.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None)`](#reduce_mean)\r\n  * [`tf.reduce_all(input_tensor, reduction_indices=None, keep_dims=False, name=None)`](#reduce_all)\r\n  * [`tf.reduce_any(input_tensor, reduction_indices=None, keep_dims=False, name=None)`](#reduce_any)\r\n  * [`tf.accumulate_n(inputs, shape=None, tensor_dtype=None, name=None)`](#accumulate_n)\r\n* [Segmentation](#AUTOGENERATED-segmentation)\r\n  * [`tf.segment_sum(data, segment_ids, name=None)`](#segment_sum)\r\n  * [`tf.segment_prod(data, segment_ids, name=None)`](#segment_prod)\r\n  * [`tf.segment_min(data, segment_ids, name=None)`](#segment_min)\r\n  * [`tf.segment_max(data, segment_ids, name=None)`](#segment_max)\r\n  * [`tf.segment_mean(data, segment_ids, name=None)`](#segment_mean)\r\n  * [`tf.unsorted_segment_sum(data, segment_ids, num_segments, name=None)`](#unsorted_segment_sum)\r\n  * [`tf.sparse_segment_sum(data, indices, segment_ids, name=None)`](#sparse_segment_sum)\r\n  * [`tf.sparse_segment_mean(data, indices, segment_ids, name=None)`](#sparse_segment_mean)\r\n* [Sequence Comparison and Indexing](#AUTOGENERATED-sequence-comparison-and-indexing)\r\n  * [`tf.argmin(input, dimension, name=None)`](#argmin)\r\n  * [`tf.argmax(input, dimension, name=None)`](#argmax)\r\n  * [`tf.listdiff(x, y, name=None)`](#listdiff)\r\n  * [`tf.where(input, name=None)`](#where)\r\n  * [`tf.unique(x, name=None)`](#unique)\r\n  * [`tf.edit_distance(hypothesis, truth, normalize=True, name='edit_distance')`](#edit_distance)\r\n  * [`tf.invert_permutation(x, name=None)`](#invert_permutation)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## Arithmetic Operators <a class=\"md-anchor\" id=\"AUTOGENERATED-arithmetic-operators\"></a>\r\n\r\nTensorFlow provides several operations that you can use to add basic arithmetic\r\noperators to your graph.\r\n\r\n- - -\r\n\r\n### `tf.add(x, y, name=None)` <a class=\"md-anchor\" id=\"add\"></a>\r\n\r\nReturns x + y element-wise.\r\n\r\n*NOTE*: Add supports broadcasting. AddN does not.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int8`, `int16`, `int32`, `complex64`, `int64`.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.sub(x, y, name=None)` <a class=\"md-anchor\" id=\"sub\"></a>\r\n\r\nReturns x - y element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `complex64`, `int64`.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.mul(x, y, name=None)` <a class=\"md-anchor\" id=\"mul\"></a>\r\n\r\nReturns x * y element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int8`, `int16`, `int32`, `complex64`, `int64`.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.div(x, y, name=None)` <a class=\"md-anchor\" id=\"div\"></a>\r\n\r\nReturns x / y element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `complex64`, `int64`.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.mod(x, y, name=None)` <a class=\"md-anchor\" id=\"mod\"></a>\r\n\r\nReturns element-wise remainder of division.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `int32`, `int64`, `float32`, `float64`.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n\r\n## Basic Math Functions <a class=\"md-anchor\" id=\"AUTOGENERATED-basic-math-functions\"></a>\r\n\r\nTensorFlow provides several operations that you can use to add basic\r\nmathematical functions to your graph.\r\n\r\n- - -\r\n\r\n### `tf.add_n(inputs, name=None)` <a class=\"md-anchor\" id=\"add_n\"></a>\r\n\r\nAdd all input tensors element wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`inputs`</b>: A list of at least 1 `Tensor` objects of the same type in: `float32`, `float64`, `int64`, `int32`, `uint8`, `int16`, `int8`, `complex64`, `qint8`, `quint8`, `qint32`.\r\n    Must all be the same size and shape.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `inputs`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.abs(x, name=None)` <a class=\"md-anchor\" id=\"abs\"></a>\r\n\r\nComputes the absolute value of a tensor.\r\n\r\nGiven a tensor of real numbers `x`, this operation returns a tensor\r\ncontaining the absolute value of each element in `x`. For example, if x is\r\nan input element and y is an output element, this operation computes\r\n\\\\(y = |x|\\\\).\r\n\r\nSee [`tf.complex_abs()`](#tf_complex_abs) to compute the absolute value of a complex\r\nnumber.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor` of type `float`, `double`, `int32`, or `int64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n   A `Tensor` the same size and type as `x` with absolute values.\r\n\r\n\r\n- - -\r\n\r\n### `tf.neg(x, name=None)` <a class=\"md-anchor\" id=\"neg\"></a>\r\n\r\nComputes numerical negative value element-wise.\r\n\r\nI.e., \\\\(y = -x\\\\).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `complex64`, `int64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.sign(x, name=None)` <a class=\"md-anchor\" id=\"sign\"></a>\r\n\r\nReturns an element-wise indication of the sign of a number.\r\n\r\ny = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.inv(x, name=None)` <a class=\"md-anchor\" id=\"inv\"></a>\r\n\r\nComputes the reciprocal of x element-wise.\r\n\r\nI.e., \\\\(y = 1 / x\\\\).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `complex64`, `int64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.square(x, name=None)` <a class=\"md-anchor\" id=\"square\"></a>\r\n\r\nComputes square of x element-wise.\r\n\r\nI.e., \\\\(y = x * x = x^2\\\\).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `complex64`, `int64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.round(x, name=None)` <a class=\"md-anchor\" id=\"round\"></a>\r\n\r\nRounds the values of a tensor to the nearest integer, element-wise.\r\n\r\nFor example:\r\n\r\n```python\r\n# 'a' is [0.9, 2.5, 2.3, -4.4]\r\ntf.round(a) ==> [ 1.0, 3.0, 2.0, -4.0 ]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor` of type `float` or `double`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of same shape and type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.sqrt(x, name=None)` <a class=\"md-anchor\" id=\"sqrt\"></a>\r\n\r\nComputes square root of x element-wise.\r\n\r\nI.e., \\\\(y = \\sqrt{x} = x^{1/2}\\\\).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `complex64`, `int64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.rsqrt(x, name=None)` <a class=\"md-anchor\" id=\"rsqrt\"></a>\r\n\r\nComputes reciprocal of square root of x element-wise.\r\n\r\nI.e., \\\\(y = 1 / \\sqrt{x}\\\\).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `complex64`, `int64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.pow(x, y, name=None)` <a class=\"md-anchor\" id=\"pow\"></a>\r\n\r\nComputes the power of one value to another.\r\n\r\nGiven a tensor `x` and a tensor `y`, this operation computes \\\\(x^y\\\\) for\r\ncorresponding elements in `x` and `y`. For example:\r\n\r\n```\r\n# tensor 'x' is [[2, 2]], [3, 3]]\r\n# tensor 'y' is [[8, 16], [2, 3]]\r\ntf.pow(x, y) ==> [[256, 65536], [9, 27]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor` of type `float`, `double`, `int32`, `complex64`, or `int64`.\r\n*  <b>`y`</b>: A `Tensor` of type `float`, `double`, `int32`, `complex64`, or `int64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.exp(x, name=None)` <a class=\"md-anchor\" id=\"exp\"></a>\r\n\r\nComputes exponential of x element-wise.  \\\\(y = e^x\\\\).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `complex64`, `int64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.log(x, name=None)` <a class=\"md-anchor\" id=\"log\"></a>\r\n\r\nComputes natural logrithm of x element-wise.\r\n\r\nI.e., \\\\(y = \\log_e x\\\\).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `complex64`, `int64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.ceil(x, name=None)` <a class=\"md-anchor\" id=\"ceil\"></a>\r\n\r\nReturns element-wise smallest integer in not less than x.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.floor(x, name=None)` <a class=\"md-anchor\" id=\"floor\"></a>\r\n\r\nReturns element-wise largest integer not greater than x.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.maximum(x, y, name=None)` <a class=\"md-anchor\" id=\"maximum\"></a>\r\n\r\nReturns the max of x and y (i.e. x > y ? x : y) element-wise, broadcasts.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.minimum(x, y, name=None)` <a class=\"md-anchor\" id=\"minimum\"></a>\r\n\r\nReturns the min of x and y (i.e. x < y ? x : y) element-wise, broadcasts.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.cos(x, name=None)` <a class=\"md-anchor\" id=\"cos\"></a>\r\n\r\nComputes cos of x element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `complex64`, `int64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.sin(x, name=None)` <a class=\"md-anchor\" id=\"sin\"></a>\r\n\r\nComputes sin of x element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `complex64`, `int64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n\r\n\r\n\r\n## Matrix Math Functions <a class=\"md-anchor\" id=\"AUTOGENERATED-matrix-math-functions\"></a>\r\n\r\nTensorFlow provides several operations that you can use to add basic\r\nmathematical functions for matrices to your graph.\r\n\r\n- - -\r\n\r\n### `tf.diag(diagonal, name=None)` <a class=\"md-anchor\" id=\"diag\"></a>\r\n\r\nReturns a diagonal tensor with a given diagonal values.\r\n\r\nGiven a `diagonal`, this operation returns a tensor with the `diagonal` and\r\neverything else padded with zeros. The diagonal is computed as follows:\r\n\r\nAssume `diagonal` has dimensions [D1,..., Dk], then the output is a tensor of\r\nrank 2k with dimensions [D1,..., Dk, D1,..., Dk] where:\r\n\r\n`output[i1,..., ik, i1,..., ik] = diagonal[i1, ..., ik]` and 0 everywhere else.\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# 'diagonal' is [1, 2, 3, 4]\r\ntf.diag(diagonal) ==> [[1, 0, 0, 0]\r\n                       [0, 2, 0, 0]\r\n                       [0, 0, 3, 0]\r\n                       [0, 0, 0, 4]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`diagonal`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`.\r\n    Rank k tensor where k is at most 3.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `diagonal`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.transpose(a, perm=None, name='transpose')` <a class=\"md-anchor\" id=\"transpose\"></a>\r\n\r\nTransposes `a`. Permutes the dimensions according to `perm`.\r\n\r\nThe returned tensor's dimension i will correspond to the input dimension\r\n`perm[i]`. If `perm` is not given, it is set to (n-1...0), where n is\r\nthe rank of the input tensor. Hence by default, this operation performs a\r\nregular matrix transpose on 2-D input Tensors.\r\n\r\nFor example:\r\n\r\n```python\r\n# 'x' is [[1 2 3]\r\n#         [4 5 6]]\r\ntf.transpose(x) ==> [[1 4]\r\n                     [2 5]\r\n                     [3 6]]\r\n\r\n# Equivalently\r\ntf.transpose(x perm=[0, 1]) ==> [[1 4]\r\n                                 [2 5]\r\n                                 [3 6]]\r\n\r\n# 'perm' is more useful for n-dimensional tensors, for n > 2\r\n# 'x' is   [[[1  2  3]\r\n#            [4  5  6]]\r\n#           [[7  8  9]\r\n#            [10 11 12]]]\r\n# Take the transpose of the matrices in dimension-0\r\ntf.transpose(b, perm=[0, 2, 1]) ==> [[[1  4]\r\n                                      [2  5]\r\n                                      [3  6]]\r\n\r\n                                     [[7 10]\r\n                                      [8 11]\r\n                                      [9 12]]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`a`</b>: A `Tensor`.\r\n*  <b>`perm`</b>: A permutation of the dimensions of `a`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A transposed `Tensor`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.matmul(a, b, transpose_a=False, transpose_b=False, a_is_sparse=False, b_is_sparse=False, name=None)` <a class=\"md-anchor\" id=\"matmul\"></a>\r\n\r\nMultiplies matrix `a` by matrix `b`, producing `a` * `b`.\r\n\r\nThe inputs must be two-dimensional matrices, with matching inner dimensions,\r\npossibly after transposition.\r\n\r\nBoth matrices must be of the same type. The supported types are:\r\n`float`, `double`, `int32`, `complex64`.\r\n\r\nEither matrix can be transposed on the fly by setting the corresponding flag\r\nto `True`. This is `False` by default.\r\n\r\nIf one or both of the matrices contain a lot of zeros, a more efficient\r\nmultiplication algorithm can be used by setting the corresponding\r\n`a_is_sparse` or `b_is_sparse` flag to `True`. These are `False` by default.\r\n\r\nFor example:\r\n\r\n```python\r\n# 2-D tensor `a`\r\na = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3]) => [[1. 2. 3.]\r\n                                                      [4. 5. 6.]]\r\n# 2-D tensor `b`\r\nb = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2]) => [[7. 8.]\r\n                                                         [9. 10.]\r\n                                                         [11. 12.]]\r\nc = tf.matmul(a, b) => [[58 64]\r\n                        [139 154]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`a`</b>: `Tensor` of type `float`, `double`, `int32` or `complex64`.\r\n*  <b>`b`</b>: `Tensor` with same type as `a`.\r\n*  <b>`transpose_a`</b>: If `True`, `a` is transposed before multiplication.\r\n*  <b>`transpose_b`</b>: If `True`, `b` is transposed before multiplication.\r\n*  <b>`a_is_sparse`</b>: If `True`, `a` is treated as a sparse matrix.\r\n*  <b>`b_is_sparse`</b>: If `True`, `b` is treated as a sparse matrix.\r\n*  <b>`name`</b>: Name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of the same type as `a`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.batch_matmul(x, y, adj_x=None, adj_y=None, name=None)` <a class=\"md-anchor\" id=\"batch_matmul\"></a>\r\n\r\nMultiplies slices of two tensors in batches.\r\n\r\nMultiplies all slices of `Tensor` `x` and `y` (each slice can be\r\nviewed as an element of a batch), and arranges the individual results\r\nin a single output tensor of the same batch size. Each of the\r\nindividual slices can optionally be adjointed (to adjoint a matrix\r\nmeans to transpose and conjugate it) before multiplication by setting\r\nthe `adj_x` or `adj_y` flag to `True`, which are by default `False`.\r\n\r\nThe input tensors `x` and `y` are 3-D or higher with shape `[..., r_x, c_x]`\r\nand `[..., r_y, c_y]`.\r\n\r\nThe output tensor is 3-D or higher with shape `[..., r_o, c_o]`, where:\r\n\r\n    r_o = c_x if adj_x else r_x\r\n    c_o = r_y if adj_y else c_y\r\n\r\nIt is computed as:\r\n\r\n    out[..., :, :] = matrix(x[..., :, :]) * matrix(y[..., :, :])\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `complex64`.\r\n    3-D or higher with shape `[..., r_x, c_x]`.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.\r\n    3-D or higher with shape `[..., r_y, c_y]`.\r\n*  <b>`adj_x`</b>: An optional `bool`. Defaults to `False`.\r\n    If `True`, adjoint the slices of `x`. Defaults to `False`.\r\n*  <b>`adj_y`</b>: An optional `bool`. Defaults to `False`.\r\n    If `True`, adjoint the slices of `y`. Defaults to `False`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `x`.\r\n  3-D or higher with shape `[..., r_o, c_o]`\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.matrix_determinant(input, name=None)` <a class=\"md-anchor\" id=\"matrix_determinant\"></a>\r\n\r\nCalculates the determinant of a square matrix.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.\r\n    A tensor of shape `[M, M]`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `input`.\r\n  A scalar, equal to the determinant of the input.\r\n\r\n\r\n- - -\r\n\r\n### `tf.batch_matrix_determinant(input, name=None)` <a class=\"md-anchor\" id=\"batch_matrix_determinant\"></a>\r\n\r\nCalculates the determinants for a batch of square matrices.\r\n\r\nThe input is a tensor of shape `[..., M, M]` whose inner-most 2 dimensions\r\nform square matrices. The output is a 1-D tensor containing the determinants\r\nfor all input submatrices `[..., :, :]`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.\r\n    Shape is `[..., M, M]`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `input`. Shape is `[...]`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.matrix_inverse(input, name=None)` <a class=\"md-anchor\" id=\"matrix_inverse\"></a>\r\n\r\nCalculates the inverse of a square invertible matrix. Checks for invertibility.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.\r\n    Shape is `[M, M]`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `input`.\r\n  Shape is `[M, M]` containing the matrix inverse of the input.\r\n\r\n\r\n- - -\r\n\r\n### `tf.batch_matrix_inverse(input, name=None)` <a class=\"md-anchor\" id=\"batch_matrix_inverse\"></a>\r\n\r\nCalculates the inverse of square invertible matrices. Checks for invertibility.\r\n\r\nThe input is a tensor of shape `[..., M, M]` whose inner-most 2 dimensions\r\nform square matrices. The output is a tensor of the same shape as the input\r\ncontaining the inverse for all input submatrices `[..., :, :]`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.\r\n    Shape is `[..., M, M]`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `input`. Shape is `[..., M, M]`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.cholesky(input, name=None)` <a class=\"md-anchor\" id=\"cholesky\"></a>\r\n\r\nCalculates the Cholesky decomposition of a square matrix.\r\n\r\nThe input has to be symmetric and positive definite. Only the lower-triangular\r\npart of the input will be used for this operation. The upper-triangular part\r\nwill not be read.\r\n\r\nThe result is the lower-triangular matrix of the Cholesky decomposition of the\r\ninput.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`. Must be one of the following types: `float64`, `float32`.\r\n    Shape is `[M, M]`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `input`. Shape is `[M, M]`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.batch_cholesky(input, name=None)` <a class=\"md-anchor\" id=\"batch_cholesky\"></a>\r\n\r\nCalculates the Cholesky decomposition of a batch of square matrices.\r\n\r\nThe input is a tensor of shape `[..., M, M]` whose inner-most 2 dimensions\r\nform square matrices, with the same constraints as the single matrix Cholesky\r\ndecomposition above. The output is a tensor of the same shape as the input\r\ncontaining the Cholesky decompositions for all input submatrices `[..., :, :]`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`. Must be one of the following types: `float64`, `float32`.\r\n    Shape is `[..., M, M]`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `input`. Shape is `[..., M, M]`.\r\n\r\n\r\n\r\n## Complex Number Functions <a class=\"md-anchor\" id=\"AUTOGENERATED-complex-number-functions\"></a>\r\n\r\nTensorFlow provides several operations that you can use to add complex number\r\nfunctions to your graph.\r\n\r\n- - -\r\n\r\n### `tf.complex(real, imag, name=None)` <a class=\"md-anchor\" id=\"complex\"></a>\r\n\r\nConverts two real numbers to a complex number.\r\n\r\nGiven a tensor `real` representing the real part of a complex number, and a\r\ntensor `imag` representing the imaginary part of a complex number, this\r\noperation computes complex numbers elementwise of the form \\\\(a + bj\\\\),\r\nwhere *a* represents the `real` part and *b* represents the `imag` part.\r\n\r\nThe input tensors `real` and `imag` must be the same shape.\r\n\r\nFor example:\r\n\r\n```\r\n# tensor 'real' is [2.25, 3.25]\r\n# tensor `imag` is [4.75, 5.75]\r\ntf.complex(real, imag) ==> [[2.25 + 4.74j], [3.25 + 5.75j]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`real`</b>: A `Tensor` of type `float`.\r\n*  <b>`imag`</b>: A `Tensor` of type `float`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `complex64`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.complex_abs(x, name=None)` <a class=\"md-anchor\" id=\"complex_abs\"></a>\r\n\r\nComputes the complex absolute value of a tensor.\r\n\r\nGiven a tensor `x` of complex numbers, this operation returns a tensor of type\r\n`float` that is the absolute value of each element in `x`. All elements in `x`\r\nmust be complex numbers of the form \\\\(a + bj\\\\). The absolute value is\r\ncomputed as \\\\( \\sqrt{a^2 + b^2}\\\\).\r\n\r\nFor example:\r\n\r\n```\r\n# tensor 'x' is [[-2.25 + 4.75j], [-3.25 + 5.75j]]\r\ntf.complex_abs(x) ==> [5.25594902, 6.60492229]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor` of type `complex64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `float32`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.conj(in_, name=None)` <a class=\"md-anchor\" id=\"conj\"></a>\r\n\r\nReturns the complex conjugate of a complex number.\r\n\r\nGiven a tensor `in` of complex numbers, this operation returns a tensor of\r\ncomplex numbers that are the complex conjugate of each element in `in`. The\r\ncomplex numbers in `in` must be of the form \\\\(a + bj\\\\), where *a* is the real\r\npart and *b* is the imaginary part.\r\n\r\nThe complex conjugate returned by this operation is of the form \\\\(a - bj\\\\).\r\n\r\nFor example:\r\n\r\n```\r\n# tensor 'in' is [-2.25 + 4.75j, 3.25 + 5.75j]\r\ntf.conj(in) ==> [-2.25 - 4.75j, 3.25 - 5.75j]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`in_`</b>: A `Tensor` of type `complex64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `complex64`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.imag(in_, name=None)` <a class=\"md-anchor\" id=\"imag\"></a>\r\n\r\nReturns the imaginary part of a complex number.\r\n\r\nGiven a tensor `in` of complex numbers, this operation returns a tensor of type\r\n`float` that is the imaginary part of each element in `in`. All elements in `in`\r\nmust be complex numbers of the form \\\\(a + bj\\\\), where *a* is the real part\r\nand *b* is the imaginary part returned by this operation.\r\n\r\nFor example:\r\n\r\n```\r\n# tensor 'in' is [-2.25 + 4.75j, 3.25 + 5.75j]\r\ntf.imag(in) ==> [4.75, 5.75]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`in_`</b>: A `Tensor` of type `complex64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `float32`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.real(in_, name=None)` <a class=\"md-anchor\" id=\"real\"></a>\r\n\r\nReturns the real part of a complex number.\r\n\r\nGiven a tensor `in` of complex numbers, this operation returns a tensor of type\r\n`float` that is the real part of each element in `in`. All elements in `in`\r\nmust be complex numbers of the form \\\\(a + bj\\\\), where *a* is the real part\r\nreturned by this operation and *b* is the imaginary part.\r\n\r\nFor example:\r\n\r\n```\r\n# tensor 'in' is [-2.25 + 4.75j, 3.25 + 5.75j]\r\ntf.real(in) ==> [-2.25, 3.25]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`in_`</b>: A `Tensor` of type `complex64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `float32`.\r\n\r\n\r\n\r\n## Reduction <a class=\"md-anchor\" id=\"AUTOGENERATED-reduction\"></a>\r\n\r\nTensorFlow provides several operations that you can use to perform\r\ncommon math computations that reduce various dimensions of a tensor.\r\n\r\n- - -\r\n\r\n### `tf.reduce_sum(input_tensor, reduction_indices=None, keep_dims=False, name=None)` <a class=\"md-anchor\" id=\"reduce_sum\"></a>\r\n\r\nComputes the sum of elements across dimensions of a tensor.\r\n\r\nReduces `input_tensor` along the dimensions given in `reduction_indices`.\r\nUnless `keep_dims` is true, the rank of the tensor is reduced by 1 for each\r\nentry in `reduction_indices`. If `keep_dims` is true, the reduced dimensions\r\nare retained with length 1.\r\n\r\nIf `reduction_indices` has no entries, all dimensions are reduced, and a\r\ntensor with a single element is returned.\r\n\r\nFor example:\r\n\r\n```python\r\n# 'x' is [[1, 1, 1]]\r\n#         [1, 1, 1]]\r\ntf.reduce_sum(x) ==> 6\r\ntf.reduce_sum(x, 0) ==> [2, 2, 2]\r\ntf.reduce_sum(x, 1) ==> [3, 3]\r\ntf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]]\r\ntf.reduce_sum(x, [0, 1]) ==> 6\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input_tensor`</b>: The tensor to reduce. Should have numeric type.\r\n*  <b>`reduction_indices`</b>: The dimensions to reduce. If `None` (the defaut),\r\n    reduces all dimensions.\r\n*  <b>`keep_dims`</b>: If true, retains reduced dimensions with length 1.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The reduced tensor.\r\n\r\n\r\n- - -\r\n\r\n### `tf.reduce_prod(input_tensor, reduction_indices=None, keep_dims=False, name=None)` <a class=\"md-anchor\" id=\"reduce_prod\"></a>\r\n\r\nComputes the product of elements across dimensions of a tensor.\r\n\r\nReduces `input_tensor` along the dimensions given in `reduction_indices`.\r\nUnless `keep_dims` is true, the rank of the tensor is reduced by 1 for each\r\nentry in `reduction_indices`. If `keep_dims` is true, the reduced dimensions\r\nare retained with length 1.\r\n\r\nIf `reduction_indices` has no entries, all dimensions are reduced, and a\r\ntensor with a single element is returned.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input_tensor`</b>: The tensor to reduce. Should have numeric type.\r\n*  <b>`reduction_indices`</b>: The dimensions to reduce. If `None` (the defaut),\r\n    reduces all dimensions.\r\n*  <b>`keep_dims`</b>: If true, retains reduced dimensions with length 1.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The reduced tensor.\r\n\r\n\r\n- - -\r\n\r\n### `tf.reduce_min(input_tensor, reduction_indices=None, keep_dims=False, name=None)` <a class=\"md-anchor\" id=\"reduce_min\"></a>\r\n\r\nComputes the minimum of elements across dimensions of a tensor.\r\n\r\nReduces `input_tensor` along the dimensions given in `reduction_indices`.\r\nUnless `keep_dims` is true, the rank of the tensor is reduced by 1 for each\r\nentry in `reduction_indices`. If `keep_dims` is true, the reduced dimensions\r\nare retained with length 1.\r\n\r\nIf `reduction_indices` has no entries, all dimensions are reduced, and a\r\ntensor with a single element is returned.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input_tensor`</b>: The tensor to reduce. Should have numeric type.\r\n*  <b>`reduction_indices`</b>: The dimensions to reduce. If `None` (the defaut),\r\n    reduces all dimensions.\r\n*  <b>`keep_dims`</b>: If true, retains reduced dimensions with length 1.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The reduced tensor.\r\n\r\n\r\n- - -\r\n\r\n### `tf.reduce_max(input_tensor, reduction_indices=None, keep_dims=False, name=None)` <a class=\"md-anchor\" id=\"reduce_max\"></a>\r\n\r\nComputes the maximum of elements across dimensions of a tensor.\r\n\r\nReduces `input_tensor` along the dimensions given in `reduction_indices`.\r\nUnless `keep_dims` is true, the rank of the tensor is reduced by 1 for each\r\nentry in `reduction_indices`. If `keep_dims` is true, the reduced dimensions\r\nare retained with length 1.\r\n\r\nIf `reduction_indices` has no entries, all dimensions are reduced, and a\r\ntensor with a single element is returned.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input_tensor`</b>: The tensor to reduce. Should have numeric type.\r\n*  <b>`reduction_indices`</b>: The dimensions to reduce. If `None` (the defaut),\r\n    reduces all dimensions.\r\n*  <b>`keep_dims`</b>: If true, retains reduced dimensions with length 1.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The reduced tensor.\r\n\r\n\r\n- - -\r\n\r\n### `tf.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None)` <a class=\"md-anchor\" id=\"reduce_mean\"></a>\r\n\r\nComputes the mean of elements across dimensions of a tensor.\r\n\r\nReduces `input_tensor` along the dimensions given in `reduction_indices`.\r\nUnless `keep_dims` is true, the rank of the tensor is reduced by 1 for each\r\nentry in `reduction_indices`. If `keep_dims` is true, the reduced dimensions\r\nare retained with length 1.\r\n\r\nIf `reduction_indices` has no entries, all dimensions are reduced, and a\r\ntensor with a single element is returned.\r\n\r\nFor example:\r\n\r\n```python\r\n# 'x' is [[1., 1. ]]\r\n#         [2., 2.]]\r\ntf.reduce_mean(x) ==> 1.5\r\ntf.reduce_mean(x, 0) ==> [1.5, 1.5]\r\ntf.reduce_mean(x, 1) ==> [1.,  2.]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input_tensor`</b>: The tensor to reduce. Should have numeric type.\r\n*  <b>`reduction_indices`</b>: The dimensions to reduce. If `None` (the defaut),\r\n    reduces all dimensions.\r\n*  <b>`keep_dims`</b>: If true, retains reduced dimensions with length 1.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The reduced tensor.\r\n\r\n\r\n- - -\r\n\r\n### `tf.reduce_all(input_tensor, reduction_indices=None, keep_dims=False, name=None)` <a class=\"md-anchor\" id=\"reduce_all\"></a>\r\n\r\nComputes the \"logical and\" of elements across dimensions of a tensor.\r\n\r\nReduces `input_tensor` along the dimensions given in `reduction_indices`.\r\nUnless `keep_dims` is true, the rank of the tensor is reduced by 1 for each\r\nentry in `reduction_indices`. If `keep_dims` is true, the reduced dimensions\r\nare retained with length 1.\r\n\r\nIf `reduction_indices` has no entries, all dimensions are reduced, and a\r\ntensor with a single element is returned.\r\n\r\nFor example:\r\n\r\n```python\r\n# 'x' is [[True,  True]]\r\n#         [False, False]]\r\ntf.reduce_all(x) ==> False\r\ntf.reduce_all(x, 0) ==> [False, False]\r\ntf.reduce_all(x, 1) ==> [True, False]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input_tensor`</b>: The boolean tensor to reduce.\r\n*  <b>`reduction_indices`</b>: The dimensions to reduce. If `None` (the defaut),\r\n    reduces all dimensions.\r\n*  <b>`keep_dims`</b>: If true, retains reduced dimensions with length 1.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The reduced tensor.\r\n\r\n\r\n- - -\r\n\r\n### `tf.reduce_any(input_tensor, reduction_indices=None, keep_dims=False, name=None)` <a class=\"md-anchor\" id=\"reduce_any\"></a>\r\n\r\nComputes the \"logical or\" of elements across dimensions of a tensor.\r\n\r\nReduces `input_tensor` along the dimensions given in `reduction_indices`.\r\nUnless `keep_dims` is true, the rank of the tensor is reduced by 1 for each\r\nentry in `reduction_indices`. If `keep_dims` is true, the reduced dimensions\r\nare retained with length 1.\r\n\r\nIf `reduction_indices` has no entries, all dimensions are reduced, and a\r\ntensor with a single element is returned.\r\n\r\nFor example:\r\n\r\n```python\r\n# 'x' is [[True,  True]]\r\n#         [False, False]]\r\ntf.reduce_any(x) ==> True\r\ntf.reduce_any(x, 0) ==> [True, True]\r\ntf.reduce_any(x, 1) ==> [True, False]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input_tensor`</b>: The boolean tensor to reduce.\r\n*  <b>`reduction_indices`</b>: The dimensions to reduce. If `None` (the defaut),\r\n    reduces all dimensions.\r\n*  <b>`keep_dims`</b>: If true, retains reduced dimensions with length 1.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The reduced tensor.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.accumulate_n(inputs, shape=None, tensor_dtype=None, name=None)` <a class=\"md-anchor\" id=\"accumulate_n\"></a>\r\n\r\nReturns the element-wise sum of a list of tensors.\r\n\r\nOptionally, pass `shape` and `tensor_dtype` for shape and type checking,\r\notherwise, these are inferred.\r\n\r\nFor example:\r\n\r\n```python\r\n# tensor 'a' is [[1, 2], [3, 4]\r\n# tensor `b` is [[5, 0], [0, 6]]\r\ntf.accumulate_n([a, b, a]) ==> [[7, 4], [6, 14]]\r\n\r\n# Explicitly pass shape and type\r\ntf.accumulate_n([a, b, a], shape=[2, 2], tensor_dtype=tf.int32)\r\n  ==> [[7, 4], [6, 14]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`inputs`</b>: A list of `Tensor` objects, each with same shape and type.\r\n*  <b>`shape`</b>: Shape of elements of `inputs`.\r\n*  <b>`tensor_dtype`</b>: The type of `inputs`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of same shape and type as the elements of `inputs`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `inputs` don't all have same shape and dtype or the shape\r\n  cannot be inferred.\r\n\r\n\r\n\r\n## Segmentation <a class=\"md-anchor\" id=\"AUTOGENERATED-segmentation\"></a>\r\n\r\nTensorFlow provides several operations that you can use to perform common\r\nmath computations on tensor segments.\r\nHere a segmentation is a partitioning of a tensor along\r\nthe first dimension, i.e. it  defines a mapping from the first dimension onto\r\n`segment_ids`. The `segment_ids` tensor should be the size of\r\nthe first dimension, `d0`, with consecutive IDs in the range `0` to `k`,\r\nwhere `k<d0`.\r\nIn particular, a segmentation of a matrix tensor is a mapping of rows to\r\nsegments.\r\n\r\nFor example:\r\n\r\n```python\r\nc = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])\r\ntf.segment_sum(c, tf.constant([0, 0, 1]))\r\n  ==>  [[0 0 0 0]\r\n        [5 6 7 8]]\r\n```\r\n\r\n- - -\r\n\r\n### `tf.segment_sum(data, segment_ids, name=None)` <a class=\"md-anchor\" id=\"segment_sum\"></a>\r\n\r\nComputes the sum along segments of a tensor.\r\n\r\nRead [the section on Segmentation](../../api_docs/python/math_ops.md#segmentation)\r\nfor an explanation of segments.\r\n\r\nComputes a tensor such that\r\n\\\\(output_i = \\sum_j data_j\\\\) where sum is over `j` such\r\nthat `segment_ids[j] == i`.\r\n\r\n<div style=\"width:70%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/SegmentSum.png\" alt>\r\n</div>\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`data`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`, `uint8`, `int16`, `int8`.\r\n*  <b>`segment_ids`</b>: A `Tensor`. Must be one of the following types: `int32`, `int64`.\r\n    A 1-D tensor whose rank is equal to the rank of `data`'s\r\n    first dimension.  Values should be sorted and can be repeated.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `data`.\r\n  Has same shape as data, except for dimension_0 which\r\n  has size `k`, the number of segments.\r\n\r\n\r\n- - -\r\n\r\n### `tf.segment_prod(data, segment_ids, name=None)` <a class=\"md-anchor\" id=\"segment_prod\"></a>\r\n\r\nComputes the product along segments of a tensor.\r\n\r\nRead [the section on\r\nSegmentation](../../api_docs/python/math_ops.md#segmentation) for an explanation\r\nof segments.\r\n\r\nComputes a tensor such that\r\n\\\\(output_i = \\prod_j data_j\\\\) where the product is over `j` such\r\nthat `segment_ids[j] == i`.\r\n\r\n<div style=\"width:70%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/SegmentProd.png\" alt>\r\n</div>\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`data`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`, `uint8`, `int16`, `int8`.\r\n*  <b>`segment_ids`</b>: A `Tensor`. Must be one of the following types: `int32`, `int64`.\r\n    A 1-D tensor whose rank is equal to the rank of `data`'s\r\n    first dimension.  Values should be sorted and can be repeated.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `data`.\r\n  Has same shape as data, except for dimension_0 which\r\n  has size `k`, the number of segments.\r\n\r\n\r\n- - -\r\n\r\n### `tf.segment_min(data, segment_ids, name=None)` <a class=\"md-anchor\" id=\"segment_min\"></a>\r\n\r\nComputes the minimum along segments of a tensor.\r\n\r\nRead [the section on\r\nSegmentation](../../api_docs/python/math_ops.md#segmentation) for an explanation\r\nof segments.\r\n\r\nComputes a tensor such that\r\n\\\\(output_i = \\min_j(data_j)\\\\) where `min` is over `j` such\r\nthat `segment_ids[j] == i`.\r\n\r\n<div style=\"width:70%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/SegmentMin.png\" alt>\r\n</div>\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`data`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`, `uint8`, `int16`, `int8`.\r\n*  <b>`segment_ids`</b>: A `Tensor`. Must be one of the following types: `int32`, `int64`.\r\n    A 1-D tensor whose rank is equal to the rank of `data`'s\r\n    first dimension.  Values should be sorted and can be repeated.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `data`.\r\n  Has same shape as data, except for dimension_0 which\r\n  has size `k`, the number of segments.\r\n\r\n\r\n- - -\r\n\r\n### `tf.segment_max(data, segment_ids, name=None)` <a class=\"md-anchor\" id=\"segment_max\"></a>\r\n\r\nComputes the maximum along segments of a tensor.\r\n\r\nRead [the section on Segmentation](../../api_docs/python/math_ops.md#segmentation)\r\nfor an explanation of segments.\r\n\r\nComputes a tensor such that\r\n\\\\(output_i = \\max_j(data_j)\\\\) where `max` is over `j` such\r\nthat `segment_ids[j] == i`.\r\n\r\n<div style=\"width:70%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/SegmentMax.png\" alt>\r\n</div>\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`data`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`, `uint8`, `int16`, `int8`.\r\n*  <b>`segment_ids`</b>: A `Tensor`. Must be one of the following types: `int32`, `int64`.\r\n    A 1-D tensor whose rank is equal to the rank of `data`'s\r\n    first dimension.  Values should be sorted and can be repeated.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `data`.\r\n  Has same shape as data, except for dimension_0 which\r\n  has size `k`, the number of segments.\r\n\r\n\r\n- - -\r\n\r\n### `tf.segment_mean(data, segment_ids, name=None)` <a class=\"md-anchor\" id=\"segment_mean\"></a>\r\n\r\nComputes the mean along segments of a tensor.\r\n\r\nRead [the section on\r\nSegmentation](../../api_docs/python/math_ops.md#segmentation) for an explanation\r\nof segments.\r\n\r\nComputes a tensor such that\r\n\\\\(output_i = \\frac{\\sum_j data_j}{N}\\\\) where `mean` is\r\nover `j` such that `segment_ids[j] == i` and `N` is the total number of\r\nvalues summed.\r\n\r\n<div style=\"width:70%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/SegmentMean.png\" alt>\r\n</div>\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`data`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`, `uint8`, `int16`, `int8`.\r\n*  <b>`segment_ids`</b>: A `Tensor`. Must be one of the following types: `int32`, `int64`.\r\n    A 1-D tensor whose rank is equal to the rank of `data`'s\r\n    first dimension.  Values should be sorted and can be repeated.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `data`.\r\n  Has same shape as data, except for dimension_0 which\r\n  has size `k`, the number of segments.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.unsorted_segment_sum(data, segment_ids, num_segments, name=None)` <a class=\"md-anchor\" id=\"unsorted_segment_sum\"></a>\r\n\r\nComputes the sum along segments of a tensor.\r\n\r\nRead [the section on\r\nSegmentation](../../api_docs/python/math_ops.md#segmentation) for an explanation\r\nof segments.\r\n\r\nComputes a tensor such that\r\n\\\\(output_i = \\sum_j data_j\\\\) where sum is over `j` such\r\nthat `segment_ids[j] == i`. Unlike `SegmentSum`, `segment_ids`\r\nneed not be sorted and need not cover all values in the full\r\n  range of valid values.\r\n\r\nIf the sum is empty for a given segment ID `i`, `output[i] = 0`.\r\n\r\n`num_segments` should equal the number of distinct segment IDs.\r\n\r\n<div style=\"width:70%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/UnsortedSegmentSum.png\" alt>\r\n</div>\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`data`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`, `uint8`, `int16`, `int8`.\r\n*  <b>`segment_ids`</b>: A `Tensor`. Must be one of the following types: `int32`, `int64`.\r\n    A 1-D tensor whose rank is equal to the rank of `data`'s\r\n    first dimension.\r\n*  <b>`num_segments`</b>: A `Tensor` of type `int32`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `data`.\r\n  Has same shape as data, except for dimension_0 which\r\n  has size `num_segments`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.sparse_segment_sum(data, indices, segment_ids, name=None)` <a class=\"md-anchor\" id=\"sparse_segment_sum\"></a>\r\n\r\nComputes the sum along sparse segments of a tensor.\r\n\r\nRead [the section on\r\nSegmentation](../../api_docs/python/math_ops.md#segmentation) for an explanation\r\nof segments.\r\n\r\nLike `SegmentSum`, but `segment_ids` can have rank less than `data`'s first\r\ndimension, selecting a subset of dimension_0, specified by `indices`.\r\n\r\nFor example:\r\n\r\n```prettyprint\r\nc = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])\r\n\r\n# Select two rows, one segment.\r\ntf.sparse_segment_sum(c, tf.constant([0, 1]), tf.constant([0, 0]))\r\n  ==> [[0 0 0 0]]\r\n\r\n# Select two rows, two segment.\r\ntf.sparse_segment_sum(c, tf.constant([0, 1]), tf.constant([0, 1]))\r\n  ==> [[ 1  2  3  4]\r\n       [-1 -2 -3 -4]]\r\n\r\n# Select all rows, two segments.\r\ntf.sparse_segment_sum(c, tf.constant([0, 1, 2]), tf.constant([0, 0, 1]))\r\n  ==> [[0 0 0 0]\r\n       [5 6 7 8]]\r\n\r\n# Which is equivalent to:\r\ntf.segment_sum(c, tf.constant([0, 0, 1]))\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`data`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`, `uint8`, `int16`, `int8`.\r\n*  <b>`indices`</b>: A `Tensor` of type `int32`.\r\n    A 1-D tensor. Has same rank as `segment_ids`.\r\n*  <b>`segment_ids`</b>: A `Tensor` of type `int32`.\r\n    A 1-D tensor. Values should be sorted and can be repeated.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `data`.\r\n  Has same shape as data, except for dimension_0 which\r\n  has size `k`, the number of segments.\r\n\r\n\r\n- - -\r\n\r\n### `tf.sparse_segment_mean(data, indices, segment_ids, name=None)` <a class=\"md-anchor\" id=\"sparse_segment_mean\"></a>\r\n\r\nComputes the mean along sparse segments of a tensor.\r\n\r\nRead [the section on\r\nSegmentation](../../api_docs/python/math_ops.md#segmentation) for an explanation\r\nof segments.\r\n\r\nLike `SegmentMean`, but `segment_ids` can have rank less than `data`'s first\r\ndimension, selecting a subset of dimension_0, specified by `indices`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`data`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.\r\n*  <b>`indices`</b>: A `Tensor` of type `int32`.\r\n    A 1-D tensor. Has same rank as `segment_ids`.\r\n*  <b>`segment_ids`</b>: A `Tensor` of type `int32`.\r\n    A 1-D tensor. Values should be sorted and can be repeated.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `data`.\r\n  Has same shape as data, except for dimension_0 which\r\n  has size `k`, the number of segments.\r\n\r\n\r\n\r\n\r\n## Sequence Comparison and Indexing <a class=\"md-anchor\" id=\"AUTOGENERATED-sequence-comparison-and-indexing\"></a>\r\n\r\nTensorFlow provides several operations that you can use to add sequence\r\ncomparison and index extraction to your graph. You can use these operations to\r\ndetermine sequence differences and determine the indexes of specific values in\r\na tensor.\r\n\r\n- - -\r\n\r\n### `tf.argmin(input, dimension, name=None)` <a class=\"md-anchor\" id=\"argmin\"></a>\r\n\r\nReturns the index with the smallest value across dimensions of a tensor.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int64`, `int32`, `uint8`, `int16`, `int8`, `complex64`, `qint8`, `quint8`, `qint32`.\r\n*  <b>`dimension`</b>: A `Tensor` of type `int32`.\r\n    int32, 0 <= dimension < rank(input).  Describes which dimension\r\n    of the input Tensor to reduce across. For vectors, use dimension = 0.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `int64`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.argmax(input, dimension, name=None)` <a class=\"md-anchor\" id=\"argmax\"></a>\r\n\r\nReturns the index with the largest value across dimensions of a tensor.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int64`, `int32`, `uint8`, `int16`, `int8`, `complex64`, `qint8`, `quint8`, `qint32`.\r\n*  <b>`dimension`</b>: A `Tensor` of type `int32`.\r\n    int32, 0 <= dimension < rank(input).  Describes which dimension\r\n    of the input Tensor to reduce across. For vectors, use dimension = 0.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `int64`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.listdiff(x, y, name=None)` <a class=\"md-anchor\" id=\"listdiff\"></a>\r\n\r\nComputes the difference between two lists of numbers.\r\n\r\nGiven a list `x` and a list `y`, this operation returns a list `out` that\r\nrepresents all numbers that are in `x` but not in `y`. The returned list `out`\r\nis sorted in the same order that the numbers appear in `x` (duplicates are\r\npreserved). This operation also returns a list `idx` that represents the\r\nposition of each `out` element in `x`. In other words:\r\n\r\n`out[i] = x[idx[i]] for i in [0, 1, ..., len(out) - 1]`\r\n\r\nFor example, given this input:\r\n\r\n```prettyprint\r\nx = [1, 2, 3, 4, 5, 6]\r\ny = [1, 3, 5]\r\n```\r\n\r\nThis operation would return:\r\n\r\n```prettyprint\r\nout ==> [2, 4, 6]\r\nidx ==> [1, 3, 5]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. 1-D. Values to keep.\r\n*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`. 1-D. Values to remove.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tuple of `Tensor` objects (out, idx).\r\n\r\n*  <b>`out`</b>: A `Tensor`. Has the same type as `x`. 1-D. Values present in `x` but not in `y`.\r\n*  <b>`idx`</b>: A `Tensor` of type `int32`. 1-D. Positions of `x` values preserved in `out`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.where(input, name=None)` <a class=\"md-anchor\" id=\"where\"></a>\r\n\r\nReturns locations of true values in a boolean tensor.\r\n\r\nThis operation returns the coordinates of true elements in `input`. The\r\ncoordinates are returned in a 2-D tensor where the first dimension (rows)\r\nrepresents the number of true elements, and the second dimension (columns)\r\nrepresents the coordinates of the true elements. Keep in mind, the shape of\r\nthe output tensor can vary depending on how many true values there are in\r\n`input`. Indices are output in row-major order.\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# 'input' tensor is [[True, False]\r\n#                    [True, False]]\r\n# 'input' has two true values, so output has two coordinates.\r\n# 'input' has rank of 2, so coordinates have two indices.\r\nwhere(input) ==> [[0, 0],\r\n                  [1, 0]]\r\n\r\n# `input` tensor is [[[True, False]\r\n#                     [True, False]]\r\n#                    [[False, True]\r\n#                     [False, True]]\r\n#                    [[False, False]\r\n#                     [False, True]]]\r\n# 'input' has 5 true values, so output has 5 coordinates.\r\n# 'input' has rank of 3, so coordinates have three indices.\r\nwhere(input) ==> [[0, 0, 0],\r\n                  [0, 1, 0],\r\n                  [1, 0, 1],\r\n                  [1, 1, 1],\r\n                  [2, 1, 1]]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor` of type `bool`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `int64`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.unique(x, name=None)` <a class=\"md-anchor\" id=\"unique\"></a>\r\n\r\nFinds unique elements in a 1-D tensor.\r\n\r\nThis operation returns a tensor `y` containing all of the unique elements of `x`\r\nsorted in the same order that they occur in `x`. This operation also returns a\r\ntensor `idx` the same size as `x` that contains the index of each value of `x`\r\nin the unique output `y`. In other words:\r\n\r\n`y[idx[i]] = x[i] for i in [0, 1,...,rank(x) - 1]`\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# tensor 'x' is [1, 1, 2, 4, 4, 4, 7, 8, 8]\r\ny, idx = unique(x)\r\ny ==> [1, 2, 4, 7, 8]\r\nidx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`. 1-D.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tuple of `Tensor` objects (y, idx).\r\n\r\n*  <b>`y`</b>: A `Tensor`. Has the same type as `x`. 1-D.\r\n*  <b>`idx`</b>: A `Tensor` of type `int32`. 1-D.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.edit_distance(hypothesis, truth, normalize=True, name='edit_distance')` <a class=\"md-anchor\" id=\"edit_distance\"></a>\r\n\r\nComputes the Levenshtein distance between sequences.\r\n\r\nThis operation takes variable-length sequences (`hypothesis` and `truth`),\r\neach provided as a `SparseTensor`, and computes the Levenshtein distance.\r\nYou can normalize the edit distance by length of `truth` by setting\r\n`normalize` to true.\r\n\r\nFor example, given the following input:\r\n\r\n```python\r\n# 'hypothesis' is a tensor of shape `[2, 1]` with variable-length values:\r\n#   (0,0) = [\"a\"]\r\n#   (1,0) = [\"b\"]\r\nhypothesis = tf.SparseTensor(\r\n    [[0, 0, 0],\r\n     [1, 0, 0]],\r\n    [\"a\", \"b\"]\r\n    (2, 1, 1))\r\n\r\n# 'truth' is a tensor of shape `[2, 2]` with variable-length values:\r\n#   (0,0) = []\r\n#   (0,1) = [\"a\"]\r\n#   (1,0) = [\"b\", \"c\"]\r\n#   (1,1) = [\"a\"]\r\ntruth = tf.SparseTensor(\r\n    [[0, 1, 0],\r\n     [1, 0, 0],\r\n     [1, 0, 1],\r\n     [1, 1, 0]]\r\n    [\"a\", \"b\", \"c\", \"a\"],\r\n    (2, 2, 2))\r\n\r\nnormalize = True\r\n```\r\n\r\nThis operation would return the following:\r\n\r\n```python\r\n# 'output' is a tensor of shape `[2, 2]` with edit distances normalized\r\n# by 'truth' lengths.\r\noutput ==> [[inf, 1.0],  # (0,0): no truth, (0,1): no hypothesis\r\n           [0.5, 1.0]]  # (1,0): addition, (1,1): no hypothesis\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`hypothesis`</b>: A `SparseTensor` containing hypothesis sequences.\r\n*  <b>`truth`</b>: A `SparseTensor` containing truth sequences.\r\n*  <b>`normalize`</b>: A `bool`. If `True`, normalizes the Levenshtein distance by\r\n    length of `truth.`\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A dense `Tensor` with rank `R - 1`, where R is the rank of the\r\n  `SparseTensor` inputs `hypothesis` and `truth`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If either `hypothesis` or `truth` are not a `SparseTensor`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.invert_permutation(x, name=None)` <a class=\"md-anchor\" id=\"invert_permutation\"></a>\r\n\r\nComputes the inverse permutation of a tensor.\r\n\r\nThis operation computes the inverse of an index permutation. It takes a 1-D\r\ninteger tensor `x`, which represents the indices of a zero-based array, and\r\nswaps each value with its index position. In other words, for an ouput tensor\r\n`y` and an input tensor `x`, this operation computes the following:\r\n\r\n`y[x[i]] = i for i in [0, 1, ..., len(x) - 1]`\r\n\r\nThe values must include 0. There can be no duplicate values or negative values.\r\n\r\nFor example:\r\n\r\n```prettyprint\r\n# tensor `x` is [3, 4, 0, 2, 1]\r\ninvert_permutation(x) ==> [2, 4, 3, 0, 1]\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor` of type `int32`. 1-D.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `int32`. 1-D.\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/python/nn.md",
    "content": "<!-- This file is machine generated: DO NOT EDIT! -->\r\n\r\n# Neural Network <a class=\"md-anchor\" id=\"AUTOGENERATED-neural-network\"></a>\r\n\r\nNote: Functions taking `Tensor` arguments can also take anything accepted by\r\n[`tf.convert_to_tensor`](../../api_docs/python/framework.md#convert_to_tensor).\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Neural Network](#AUTOGENERATED-neural-network)\r\n* [Activation Functions](#AUTOGENERATED-activation-functions)\r\n  * [`tf.nn.relu(features, name=None)`](#relu)\r\n  * [`tf.nn.relu6(features, name=None)`](#relu6)\r\n  * [`tf.nn.softplus(features, name=None)`](#softplus)\r\n  * [`tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None)`](#dropout)\r\n  * [`tf.nn.bias_add(value, bias, name=None)`](#bias_add)\r\n  * [`tf.sigmoid(x, name=None)`](#sigmoid)\r\n  * [`tf.tanh(x, name=None)`](#tanh)\r\n* [Convolution](#AUTOGENERATED-convolution)\r\n  * [`tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)`](#conv2d)\r\n  * [`tf.nn.depthwise_conv2d(input, filter, strides, padding, name=None)`](#depthwise_conv2d)\r\n  * [`tf.nn.separable_conv2d(input, depthwise_filter, pointwise_filter, strides, padding, name=None)`](#separable_conv2d)\r\n* [Pooling](#AUTOGENERATED-pooling)\r\n  * [`tf.nn.avg_pool(value, ksize, strides, padding, name=None)`](#avg_pool)\r\n  * [`tf.nn.max_pool(value, ksize, strides, padding, name=None)`](#max_pool)\r\n  * [`tf.nn.max_pool_with_argmax(input, ksize, strides, padding, Targmax=None, name=None)`](#max_pool_with_argmax)\r\n* [Normalization](#AUTOGENERATED-normalization)\r\n  * [`tf.nn.l2_normalize(x, dim, epsilon=1e-12, name=None)`](#l2_normalize)\r\n  * [`tf.nn.local_response_normalization(input, depth_radius=None, bias=None, alpha=None, beta=None, name=None)`](#local_response_normalization)\r\n  * [`tf.nn.moments(x, axes, name=None)`](#moments)\r\n* [Losses](#AUTOGENERATED-losses)\r\n  * [`tf.nn.l2_loss(t, name=None)`](#l2_loss)\r\n* [Classification](#AUTOGENERATED-classification)\r\n  * [`tf.nn.sigmoid_cross_entropy_with_logits(logits, targets, name=None)`](#sigmoid_cross_entropy_with_logits)\r\n  * [`tf.nn.softmax(logits, name=None)`](#softmax)\r\n  * [`tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None)`](#softmax_cross_entropy_with_logits)\r\n* [Embeddings](#AUTOGENERATED-embeddings)\r\n  * [`tf.nn.embedding_lookup(params, ids, name=None)`](#embedding_lookup)\r\n* [Evaluation](#AUTOGENERATED-evaluation)\r\n  * [`tf.nn.top_k(input, k, name=None)`](#top_k)\r\n  * [`tf.nn.in_top_k(predictions, targets, k, name=None)`](#in_top_k)\r\n* [Candidate Sampling](#AUTOGENERATED-candidate-sampling)\r\n  * [Sampled Loss Functions](#AUTOGENERATED-sampled-loss-functions)\r\n  * [`tf.nn.nce_loss(weights, biases, inputs, labels, num_sampled, num_classes, num_true=1, sampled_values=None, remove_accidental_hits=False, name='nce_loss')`](#nce_loss)\r\n  * [`tf.nn.sampled_softmax_loss(weights, biases, inputs, labels, num_sampled, num_classes, num_true=1, sampled_values=None, remove_accidental_hits=True, name='sampled_softmax_loss')`](#sampled_softmax_loss)\r\n  * [Candidate Samplers](#AUTOGENERATED-candidate-samplers)\r\n  * [`tf.nn.uniform_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, seed=None, name=None)`](#uniform_candidate_sampler)\r\n  * [`tf.nn.log_uniform_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, seed=None, name=None)`](#log_uniform_candidate_sampler)\r\n  * [`tf.nn.learned_unigram_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, seed=None, name=None)`](#learned_unigram_candidate_sampler)\r\n  * [`tf.nn.fixed_unigram_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, vocab_file='', distortion=0.0, num_reserved_ids=0, num_shards=1, shard=0, unigrams=[], seed=None, name=None)`](#fixed_unigram_candidate_sampler)\r\n  * [Miscellaneous candidate sampling utilities](#AUTOGENERATED-miscellaneous-candidate-sampling-utilities)\r\n  * [`tf.nn.compute_accidental_hits(true_classes, sampled_candidates, num_true, seed=None, name=None)`](#compute_accidental_hits)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## Activation Functions <a class=\"md-anchor\" id=\"AUTOGENERATED-activation-functions\"></a>\r\n\r\nThe activation ops provide different types of nonlinearities for use in\r\nneural networks.  These include smooth nonlinearities (`sigmoid`,\r\n`tanh`, and `softplus`), continuous but not everywhere differentiable\r\nfunctions (`relu`, `relu6`, and `relu_x`), and random regularization\r\n(`dropout`).\r\n\r\nAll activation ops apply componentwise, and produce a tensor of the same\r\nshape as the input tensor.\r\n\r\n- - -\r\n\r\n### `tf.nn.relu(features, name=None)` <a class=\"md-anchor\" id=\"relu\"></a>\r\n\r\nComputes rectified linear: `max(features, 0)`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`features`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`, `uint8`, `int16`, `int8`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `features`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.relu6(features, name=None)` <a class=\"md-anchor\" id=\"relu6\"></a>\r\n\r\nComputes Rectified Linear 6: `min(max(features, 0), 6)`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`features`</b>: A `Tensor` with type `float`, `double`, `int32`, `int64`, `uint8`,\r\n    `int16`, or `int8`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` with the same type as `features`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.softplus(features, name=None)` <a class=\"md-anchor\" id=\"softplus\"></a>\r\n\r\nComputes softplus: `log(exp(features) + 1)`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`features`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`, `uint8`, `int16`, `int8`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `features`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None)` <a class=\"md-anchor\" id=\"dropout\"></a>\r\n\r\nComputes dropout.\r\n\r\nWith probability `keep_prob`, outputs the input element scaled up by\r\n`1 / keep_prob`, otherwise outputs `0`.  The scaling is so that the expected\r\nsum is unchanged.\r\n\r\nBy default, each element is kept or dropped independently.  If `noise_shape`\r\nis specified, it must be\r\n[broadcastable](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)\r\nto the shape of `x`, and only dimensions with `noise_shape[i] == shape(x)[i]`\r\nwill make independent decisions.  For example, if `shape(x) = [k, l, m, n]`\r\nand `noise_shape = [k, 1, 1, n]`, each batch and channel component will be\r\nkept independently and each row and column will be kept or not kept together.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A tensor.\r\n*  <b>`keep_prob`</b>: A Python float. The probability that each element is kept.\r\n*  <b>`noise_shape`</b>: A 1-D `Tensor` of type `int32`, representing the\r\n    shape for randomly generated keep/drop flags.\r\n*  <b>`seed`</b>: A Python integer. Used to create random seeds. See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n*  <b>`name`</b>: A name for this operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A Tensor of the same shape of `x`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `keep_prob` is not in `(0, 1]`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.bias_add(value, bias, name=None)` <a class=\"md-anchor\" id=\"bias_add\"></a>\r\n\r\nAdds `bias` to `value`.\r\n\r\nThis is (mostly) a special case of `tf.add` where `bias` is restricted to 1-D.\r\nBroadcasting is supported, so `value` may have any number of dimensions.\r\nUnlike `tf.add`, the type of `bias` is allowed to differ from `value` in the\r\ncase where both types are quantized.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`value`</b>: A `Tensor` with type `float`, `double`, `int64`, `int32`, `uint8`,\r\n    `int16`, `int8`, or `complex64`.\r\n*  <b>`bias`</b>: A 1-D `Tensor` with size matching the last dimension of `value`.\r\n    Must be the same type as `value` unless `value` is a quantized type,\r\n    in which case a different quantized type may be used.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` with the same type as `value`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.sigmoid(x, name=None)` <a class=\"md-anchor\" id=\"sigmoid\"></a>\r\n\r\nComputes sigmoid of `x` element-wise.\r\n\r\nSpecifically, `y = 1 / (1 + exp(-x))`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A Tensor with type `float`, `double`, `int32`, `complex64`, `int64`,\r\n    or `qint32`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A Tensor with the same type as `x` if `x.dtype != qint32`\r\n    otherwise the return type is `quint8`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.tanh(x, name=None)` <a class=\"md-anchor\" id=\"tanh\"></a>\r\n\r\nComputes hyperbolic tangent of `x` element-wise.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A Tensor with type `float`, `double`, `int32`, `complex64`, `int64`,\r\n    or `qint32`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A Tensor with the same type as `x` if `x.dtype != qint32` otherwise\r\n    the return type is `quint8`.\r\n\r\n\r\n\r\n## Convolution <a class=\"md-anchor\" id=\"AUTOGENERATED-convolution\"></a>\r\n\r\nThe convolution ops sweep a 2-D filter over a batch of images, applying the\r\nfilter to each window of each image of the appropriate size.  The different\r\nops trade off between generic vs. specific filters:\r\n\r\n* `conv2d`: Arbitrary filters that can mix channels together.\r\n* `depthwise_conv2d`: Filters that operate on each channel independently.\r\n* `separable_conv2d`: A depthwise spatial filter followed by a pointwise filter.\r\n\r\nNote that although these ops are called \"convolution\", they are strictly\r\nspeaking \"cross-correlation\" since the filter is combined with an input window\r\nwithout reversing the filter.  For details, see [the properties of\r\ncross-correlation](https://en.wikipedia.org/wiki/Cross-correlation#Properties).\r\n\r\nThe filter is applied to image patches of the same size as the filter and\r\nstrided according to the `strides` argument.  `strides = [1, 1, 1, 1]` applies\r\nthe filter to a patch at every offset, `strides = [1, 2, 2, 1]` applies the\r\nfilter to every other image patch in each dimension, etc.\r\n\r\nIgnoring channels for the moment, the spatial semantics of the convolution ops\r\nare as follows.  If the 4-D `input` has shape\r\n`[batch, in_height, in_width, ...]` and the 4-D `filter` has shape\r\n`[filter_height, filter_width, ...]`, then\r\n\r\n    shape(output) = [batch,\r\n                     (in_height - filter_height + 1) / strides[1],\r\n                     (in_width - filter_width + 1) / strides[2],\r\n                     ...]\r\n\r\n    output[b, i, j, :] =\r\n        sum_{di, dj} input[b, strides[1] * i + di, strides[2] * j + dj, ...] *\r\n                     filter[di, dj, ...]\r\n\r\nSince `input` is 4-D, each `input[b, i, j, :]` is a vector.  For `conv2d`, these\r\nvectors are multiplied by the `filter[di, dj, :, :]` matrices to produce new\r\nvectors.  For `depthwise_conv_2d`, each scalar component `input[b, i, j, k]`\r\nis multiplied by a vector `filter[di, dj, k]`, and all the vectors are\r\nconcatenated.\r\n\r\nIn the formula for `shape(output)`, the rounding direction depends on padding:\r\n\r\n* `padding = 'SAME'`: Round down (only full size windows are considered).\r\n* `padding = 'VALID'`: Round up (partial windows are included).\r\n\r\n- - -\r\n\r\n### `tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)` <a class=\"md-anchor\" id=\"conv2d\"></a>\r\n\r\nComputes a 2-D convolution given 4-D `input` and `filter` tensors.\r\n\r\nGiven an input tensor of shape `[batch, in_height, in_width, in_channels]`\r\nand a filter / kernel tensor of shape\r\n`[filter_height, filter_width, in_channels, out_channels]`, this op\r\nperforms the following:\r\n\r\n1. Flattens the filter to a 2-D matrix with shape\r\n   `[filter_height * filter_width * in_channels, output_channels]`.\r\n2. Extracts image patches from the the input tensor to form a *virtual*\r\n   tensor of shape `[batch, out_height, out_width,\r\n   filter_height * filter_width * in_channels]`.\r\n3. For each patch, right-multiplies the filter matrix and the image patch\r\n   vector.\r\n\r\nIn detail,\r\n\r\n    output[b, i, j, k] =\r\n        sum_{di, dj, q} input[b, strides[1] * i + di, strides[2] * j + dj, q] *\r\n                        filter[di, dj, q, k]\r\n\r\nMust have `strides[0] = strides[3] = 1`.  For the most common case of the same\r\nhorizontal and vertices strides, `strides = [1, stride, stride, 1]`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.\r\n*  <b>`filter`</b>: A `Tensor`. Must have the same type as `input`.\r\n*  <b>`strides`</b>: A list of `ints`.\r\n    1-D of length 4.  The stride of the sliding window for each dimension\r\n    of `input`.\r\n*  <b>`padding`</b>: A `string` from: `\"SAME\", \"VALID\"`.\r\n    The type of padding algorithm to use.\r\n*  <b>`use_cudnn_on_gpu`</b>: An optional `bool`. Defaults to `True`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `input`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.depthwise_conv2d(input, filter, strides, padding, name=None)` <a class=\"md-anchor\" id=\"depthwise_conv2d\"></a>\r\n\r\nDepthwise 2-D convolution.\r\n\r\nGiven an input tensor of shape `[batch, in_height, in_width, in_channels]`\r\nand a filter tensor of shape\r\n`[filter_height, filter_width, in_channels, channel_multiplier]`\r\ncontaining `in_channels` convolutional filters of depth 1, `depthwise_conv2d`\r\napplies a different filter to each input channel (expanding from 1 channel\r\nto `channel_multiplier` channels for each), then concatenates the results\r\ntogether.  The output has `in_channels * channel_multiplier` channels.\r\n\r\nIn detail,\r\n\r\n    output[b, i, j, k * channel_multiplier + q] =\r\n        sum_{di, dj} input[b, strides[1] * i + di, strides[2] * j + dj, k] *\r\n                     filter[di, dj, k, q]\r\n\r\nMust have `strides[0] = strides[3] = 1`.  For the most common case of the\r\nsame horizontal and vertical strides, `strides = [1, stride, stride, 1]`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: 4-D with shape `[batch, in_height, in_width, in_channels]`.\r\n*  <b>`filter`</b>: 4-D with shape\r\n    `[filter_height, filter_width, in_channels, channel_multiplier]`.\r\n*  <b>`strides`</b>: 1-D of size 4.  The stride of the sliding window for each\r\n    dimension of `input`.\r\n*  <b>`padding`</b>: A string, either `'VALID'` or `'SAME'`.  The padding algorithm.\r\n*  <b>`name`</b>: A name for this operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A 4-D `Tensor` of shape\r\n  `[batch, out_height, out_width, in_channels * channel_multiplier].`\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.separable_conv2d(input, depthwise_filter, pointwise_filter, strides, padding, name=None)` <a class=\"md-anchor\" id=\"separable_conv2d\"></a>\r\n\r\n2-D convolution with separable filters.\r\n\r\nPerforms a depthwise convolution that acts separately on channels followed by\r\na pointwise convolution that mixes channels.  Note that this is separability\r\nbetween dimensions `[1, 2]` and `3`, not spatial separability between\r\ndimensions `1` and `2`.\r\n\r\nIn detail,\r\n\r\n    output[b, i, j, k] = sum_{di, dj, q, r]\r\n        input[b, strides[1] * i + di, strides[2] * j + dj, q] *\r\n        depthwise_filter[di, dj, q, r] *\r\n        pointwise_filter[0, 0, q * channel_multiplier + r, k]\r\n\r\n`strides` controls the strides for the depthwise convolution only, since\r\nthe pointwise convolution has implicit strides of `[1, 1, 1, 1]`.  Must have\r\n`strides[0] = strides[3] = 1`.  For the most common case of the same\r\nhorizontal and vertical strides, `strides = [1, stride, stride, 1]`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: 4-D `Tensor` with shape `[batch, in_height, in_width, in_channels]`.\r\n*  <b>`depthwise_filter`</b>: 4-D `Tensor` with shape\r\n    `[filter_height, filter_width, in_channels, channel_multiplier]`.\r\n    Contains `in_channels` convolutional filters of depth 1.\r\n*  <b>`pointwise_filter`</b>: 4-D `Tensor` with shape\r\n    `[1, 1, channel_multiplier * in_channels, out_channels]`.  Pointwise\r\n    filter to mix channels after `depthwise_filter` has convolved spatially.\r\n*  <b>`strides`</b>: 1-D of size 4.  The strides for the depthwise convolution for\r\n    each dimension of `input`.\r\n*  <b>`padding`</b>: A string, either `'VALID'` or `'SAME'`.  The padding algorithm.\r\n*  <b>`name`</b>: A name for this operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A 4-D `Tensor` of shape `[batch, out_height, out_width, out_channels]`.\r\n\r\n\r\n\r\n## Pooling <a class=\"md-anchor\" id=\"AUTOGENERATED-pooling\"></a>\r\n\r\nThe pooling ops sweep a rectangular window over the input tensor, computing a\r\nreduction operation for each window (average, max, or max with argmax).  Each\r\npooling op uses rectangular windows of size `ksize` separated by offset\r\n`strides`.  For example, if `strides` is all ones every window is used, if\r\n`strides` is all twos every other window is used in each dimension, etc.\r\n\r\nIn detail, the output is\r\n\r\n    output[i] = reduce(value[strides * i:strides * i + ksize])\r\n\r\nfor each tuple of indices `i`.  The output shape is\r\n\r\n    shape(output) = (shape(value) - ksize + 1) / strides\r\n\r\nwhere the rounding direction depends on padding:\r\n\r\n* `padding = 'SAME'`: Round down (only full size windows are considered).\r\n* `padding = 'VALID'`: Round up (partial windows are included).\r\n\r\n- - -\r\n\r\n### `tf.nn.avg_pool(value, ksize, strides, padding, name=None)` <a class=\"md-anchor\" id=\"avg_pool\"></a>\r\n\r\nPerforms the average pooling on the input.\r\n\r\nEach entry in `output` is the mean of the corresponding size `ksize`\r\nwindow in `value`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`value`</b>: A 4-D `Tensor` of shape `[batch, height, width, channels]` and type\r\n    `float32`, `float64`, `qint8`, `quint8`, or `qint32`.\r\n*  <b>`ksize`</b>: A list of ints that has length >= 4.\r\n    The size of the window for each dimension of the input tensor.\r\n*  <b>`strides`</b>: A list of ints that has length >= 4.\r\n    The stride of the sliding window for each dimension of the\r\n    input tensor.\r\n*  <b>`padding`</b>: A string, either `'VALID'` or `'SAME'`. The padding algorithm.\r\n*  <b>`name`</b>: Optional name for the operation.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` with the same type as `value`.  The average pooled output tensor.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.max_pool(value, ksize, strides, padding, name=None)` <a class=\"md-anchor\" id=\"max_pool\"></a>\r\n\r\nPerforms the max pooling on the input.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`value`</b>: A 4-D `Tensor` with shape `[batch, height, width, channels]` and\r\n    type `float32`, `float64`, `qint8`, `quint8`, `qint32`.\r\n*  <b>`ksize`</b>: A list of ints that has length >= 4.  The size of the window for\r\n    each dimension of the input tensor.\r\n*  <b>`strides`</b>: A list of ints that has length >= 4.  The stride of the sliding\r\n    window for each dimension of the input tensor.\r\n*  <b>`padding`</b>: A string, either `'VALID'` or `'SAME'`. The padding algorithm.\r\n*  <b>`name`</b>: Optional name for the operation.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` with the same type as `value`.  The max pooled output tensor.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.max_pool_with_argmax(input, ksize, strides, padding, Targmax=None, name=None)` <a class=\"md-anchor\" id=\"max_pool_with_argmax\"></a>\r\n\r\nPerforms max pooling on the input and outputs both max values and indices.\r\n\r\nThe indices in `argmax` are flattened, so that a maximum value at position\r\n`[b, y, x, c]` becomes flattened index\r\n`((b * height + y) * width + x) * channels + c`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor` of type `float32`.\r\n    4-D with shape `[batch, height, width, channels]`.  Input to pool over.\r\n*  <b>`ksize`</b>: A list of `ints` that has length `>= 4`.\r\n    The size of the window for each dimension of the input tensor.\r\n*  <b>`strides`</b>: A list of `ints` that has length `>= 4`.\r\n    The stride of the sliding window for each dimension of the\r\n    input tensor.\r\n*  <b>`padding`</b>: A `string` from: `\"SAME\", \"VALID\"`.\r\n    The type of padding algorithm to use.\r\n*  <b>`Targmax`</b>: An optional `tf.DType` from: `tf.int32, tf.int64`. Defaults to `tf.int64`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tuple of `Tensor` objects (output, argmax).\r\n\r\n*  <b>`output`</b>: A `Tensor` of type `float32`. The max pooled output tensor.\r\n*  <b>`argmax`</b>: A `Tensor` of type `Targmax`. 4-D.  The flattened indices of the max values chosen for each output.\r\n\r\n\r\n\r\n## Normalization <a class=\"md-anchor\" id=\"AUTOGENERATED-normalization\"></a>\r\n\r\nNormalization is useful to prevent neurons from saturating when inputs may\r\nhave varying scale, and to aid generalization.\r\n\r\n- - -\r\n\r\n### `tf.nn.l2_normalize(x, dim, epsilon=1e-12, name=None)` <a class=\"md-anchor\" id=\"l2_normalize\"></a>\r\n\r\nNormalizes along dimension `dim` using an L2 norm.\r\n\r\nFor a 1-D tensor with `dim = 0`, computes\r\n\r\n    output = x / sqrt(max(sum(x**2), epsilon))\r\n\r\nFor `x` with more dimensions, independently normalizes each 1-D slice along\r\ndimension `dim`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`.\r\n*  <b>`dim`</b>: Dimension along which to normalize.\r\n*  <b>`epsilon`</b>: A lower bound value for the norm. Will use `sqrt(epsilon)` as the\r\n    divisor if `norm < sqrt(epsilon)`.\r\n*  <b>`name`</b>: A name for this operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` with the same shape as `x`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.local_response_normalization(input, depth_radius=None, bias=None, alpha=None, beta=None, name=None)` <a class=\"md-anchor\" id=\"local_response_normalization\"></a>\r\n\r\nLocal Response Normalization.\r\n\r\nThe 4-D `input` tensor is treated as a 3-D array of 1-D vectors (along the last\r\ndimension), and each vector is normalized independently.  Within a given vector,\r\neach component is divided by the weighted, squared sum of inputs within\r\n`depth_radius`.  In detail,\r\n\r\n    sqr_sum[a, b, c, d] =\r\n        sum(input[a, b, c, d - depth_radius : d + depth_radius + 1] ** 2)\r\n    output = input / (bias + alpha * sqr_sum ** beta)\r\n\r\nFor details, see [Krizhevsky et al., ImageNet classification with deep\r\nconvolutional neural networks (NIPS 2012)]\r\n(http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor` of type `float32`. 4-D.\r\n*  <b>`depth_radius`</b>: An optional `int`. Defaults to `5`.\r\n    0-D.  Half-width of the 1-D normalization window.\r\n*  <b>`bias`</b>: An optional `float`. Defaults to `1`.\r\n    An offset (usually positive to avoid dividing by 0).\r\n*  <b>`alpha`</b>: An optional `float`. Defaults to `1`.\r\n    A scale factor, usually positive.\r\n*  <b>`beta`</b>: An optional `float`. Defaults to `0.5`. An exponent.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `float32`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.moments(x, axes, name=None)` <a class=\"md-anchor\" id=\"moments\"></a>\r\n\r\nCalculate the mean and variance of `x`.\r\n\r\nThe mean and variance are calculated by aggregating the contents of `x`\r\nacross `axes`.  If `x` is 1-D and `axes = [0]` this is just the mean\r\nand variance of a vector.\r\n\r\nFor so-called \"global normalization\" needed for convolutional filters pass\r\n`axes=[0, 1, 2]` (batch, height, width).  For batch normalization pass\r\n`axes=[0]` (batch).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`x`</b>: A `Tensor`.\r\n*  <b>`axes`</b>: array of ints.  Axes along which to compute mean and\r\n    variance.\r\n*  <b>`name`</b>: Name used to scope the operations that compute the moments.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  Two `Tensors`: `mean` and `variance`.\r\n\r\n\r\n\r\n## Losses <a class=\"md-anchor\" id=\"AUTOGENERATED-losses\"></a>\r\n\r\nThe loss ops measure error between two tensors, or between a tensor and zero.\r\nThese can be used for measuring accuracy of a network in a regression task\r\nor for regularization purposes (weight decay).\r\n\r\n- - -\r\n\r\n### `tf.nn.l2_loss(t, name=None)` <a class=\"md-anchor\" id=\"l2_loss\"></a>\r\n\r\nL2 Loss.\r\n\r\nComputes half the L2 norm of a tensor without the `sqrt`:\r\n\r\n    output = sum(t ** 2) / 2\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`t`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int64`, `int32`, `uint8`, `int16`, `int8`, `complex64`, `qint8`, `quint8`, `qint32`.\r\n    Typically 2-D, but may have any dimensions.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `t`. 0-D.\r\n\r\n\r\n\r\n## Classification <a class=\"md-anchor\" id=\"AUTOGENERATED-classification\"></a>\r\n\r\nTensorFlow provides several operations that help you perform classification.\r\n\r\n- - -\r\n\r\n### `tf.nn.sigmoid_cross_entropy_with_logits(logits, targets, name=None)` <a class=\"md-anchor\" id=\"sigmoid_cross_entropy_with_logits\"></a>\r\n\r\nComputes sigmoid cross entropy given `logits`.\r\n\r\nMeasures the probability error in discrete classification tasks in which each\r\nclass is independent and not mutually exclusive.  For instance, one could\r\nperform multilabel classification where a picture can contain both an elephant\r\nand a dog at the same time.\r\n\r\nFor brevity, let `x = logits`, `z = targets`.  The logistic loss is\r\n\r\n    x - x * z + log(1 + exp(-x))\r\n\r\nTo ensure stability and avoid overflow, the implementation uses\r\n\r\n    max(x, 0) - x * z + log(1 + exp(-abs(x)))\r\n\r\n`logits` and `targets` must have the same type and shape.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`logits`</b>: A `Tensor` of type `float32` or `float64`.\r\n*  <b>`targets`</b>: A `Tensor` of the same type and shape as `logits`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of the same shape as `logits` with the componentwise\r\n  logistic losses.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.softmax(logits, name=None)` <a class=\"md-anchor\" id=\"softmax\"></a>\r\n\r\nComputes softmax activations.\r\n\r\nFor each batch `i` and class `j` we have\r\n\r\n    softmax[i, j] = exp(logits[i, j]) / sum(exp(logits[i]))\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`logits`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.\r\n    2-D with shape `[batch_size, num_classes]`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `logits`. Same shape as `logits`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None)` <a class=\"md-anchor\" id=\"softmax_cross_entropy_with_logits\"></a>\r\n\r\nComputes softmax cross entropy between `logits` and `labels`.\r\n\r\nMeasures the probability error in discrete classification tasks in which the\r\nclasses are mutually exclusive (each entry is in exactly one class).  For\r\nexample, each CIFAR-10 image is labeled with one and only one label: an image\r\ncan be a dog or a truck, but not both.\r\n\r\n**WARNING:** This op expects unscaled logits, since it performs a `softmax`\r\non `logits` internally for efficiency.  Do not call this op with the\r\noutput of `softmax`, as it will produce incorrect results.\r\n\r\n`logits` and `labels` must have the same shape `[batch_size, num_classes]`\r\nand the same dtype (either `float32` or `float64`).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`logits`</b>: Unscaled log probabilities.\r\n*  <b>`labels`</b>: Each row `labels[i]` must be a valid probability distribution.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A 1-D `Tensor` of length `batch_size` of the same type as `logits` with the\r\n  softmax cross entropy loss.\r\n\r\n\r\n\r\n## Embeddings <a class=\"md-anchor\" id=\"AUTOGENERATED-embeddings\"></a>\r\n\r\nTensorFlow provides library support for looking up values in embedding\r\ntensors.\r\n\r\n- - -\r\n\r\n### `tf.nn.embedding_lookup(params, ids, name=None)` <a class=\"md-anchor\" id=\"embedding_lookup\"></a>\r\n\r\nLooks up `ids` in a list of embedding tensors.\r\n\r\nThis function is used to perform parallel lookups on the list of\r\ntensors in `params`.  It is a generalization of\r\n[`tf.gather()`](../../api_docs/python/array_ops.md#gather), where `params` is\r\ninterpreted as a partition of a larger embedding tensor.\r\n\r\nIf `len(params) > 1`, each element `id` of `ids` is partitioned between\r\nthe elements of `params` by computing `p = id % len(params)`, and is\r\nthen used to look up the slice `params[p][id // len(params), ...]`.\r\n\r\nThe results of the lookup are then concatenated into a dense\r\ntensor. The returned tensor has shape `shape(ids) + shape(params)[1:]`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`params`</b>: A list of tensors with the same shape and type.\r\n*  <b>`ids`</b>: A `Tensor` with type `int32` containing the ids to be looked\r\n    up in `params`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` with the same type as the tensors in `params`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If `params` is empty.\r\n\r\n\r\n\r\n## Evaluation <a class=\"md-anchor\" id=\"AUTOGENERATED-evaluation\"></a>\r\n\r\nThe evaluation ops are useful for measuring the performance of a network.\r\nSince they are nondifferentiable, they are typically used at evaluation time.\r\n\r\n- - -\r\n\r\n### `tf.nn.top_k(input, k, name=None)` <a class=\"md-anchor\" id=\"top_k\"></a>\r\n\r\nReturns the values and indices of the k largest elements for each row.\r\n\r\n\\\\(values_{i, j}\\\\) represents the j-th largest element in \\\\(input_i\\\\).\r\n\r\n\\\\(indices_{i, j}\\\\) gives the column index of the corresponding element,\r\nsuch that \\\\(input_{i, indices_{i, j}} = values_{i, j}\\\\). If two\r\nelements are equal, the lower-index element appears first.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`, `uint8`, `int16`, `int8`.\r\n    A batch_size x classes tensor\r\n*  <b>`k`</b>: An `int` that is `>= 1`.\r\n    Number of top elements to look for within each row\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A tuple of `Tensor` objects (values, indices).\r\n\r\n*  <b>`values`</b>: A `Tensor`. Has the same type as `input`. A batch_size x k tensor with the k largest elements for each row,\r\n    sorted in descending order\r\n*  <b>`indices`</b>: A `Tensor` of type `int32`. A batch_size x k tensor with the index of each value within each row\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.in_top_k(predictions, targets, k, name=None)` <a class=\"md-anchor\" id=\"in_top_k\"></a>\r\n\r\nSays whether the targets are in the top K predictions.\r\n\r\nThis outputs a batch_size bool array, an entry out[i] is true if the\r\nprediction for the target class is among the top k predictions among\r\nall predictions for example i. Note that the behavior of InTopK differs\r\nfrom the TopK op in its handling of ties; if multiple classes have the\r\nsame prediction value and straddle the top-k boundary, all of those\r\nclasses are considered to be in the top k.\r\n\r\nMore formally, let\r\n\r\n  \\\\(predictions_i\\\\) be the predictions for all classes for example i,\r\n  \\\\(targets_i\\\\) be the target class for example i,\r\n  \\\\(out_i\\\\) be the output for example i,\r\n\r\n$$out_i = predictions_{i, targets_i} \\in TopKIncludingTies(predictions_i)$$\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`predictions`</b>: A `Tensor` of type `float32`. A batch_size x classes tensor\r\n*  <b>`targets`</b>: A `Tensor` of type `int32`. A batch_size vector of class ids\r\n*  <b>`k`</b>: An `int`. Number of top elements to look at for computing precision\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` of type `bool`. Computed Precision at k as a bool Tensor\r\n\r\n\r\n\r\n## Candidate Sampling <a class=\"md-anchor\" id=\"AUTOGENERATED-candidate-sampling\"></a>\r\n\r\nDo you want to train a multiclass or multilabel model with thousands\r\nor millions of output classes (for example, a language model with a\r\nlarge vocabulary)?  Training with a full Softmax is slow in this case,\r\nsince all of the classes are evaluated for every training example.\r\nCandidate Sampling training algorithms can speed up your step times by\r\nonly considering a small randomly-chosen subset of contrastive classes\r\n(called candidates) for each batch of training examples.\r\n\r\nSee our [Candidate Sampling Algorithms Reference]\r\n(../../extras/candidate_sampling.pdf)\r\n\r\n### Sampled Loss Functions <a class=\"md-anchor\" id=\"AUTOGENERATED-sampled-loss-functions\"></a>\r\n\r\nTensorFlow provides the following sampled loss functions for faster training.\r\n\r\n- - -\r\n\r\n### `tf.nn.nce_loss(weights, biases, inputs, labels, num_sampled, num_classes, num_true=1, sampled_values=None, remove_accidental_hits=False, name='nce_loss')` <a class=\"md-anchor\" id=\"nce_loss\"></a>\r\n\r\nComputes and returns the noise-contrastive estimation training loss.\r\n\r\nSee [Noise-contrastive estimation: A new estimation principle for\r\nunnormalized statistical models]\r\n(http://www.jmlr.org/proceedings/papers/v9/gutmann10a/gutmann10a.pdf).\r\nAlso see our [Candidate Sampling Algorithms Reference]\r\n(http://www.tensorflow.org/extras/candidate_sampling.pdf)\r\n\r\nNote: In the case where num_true > 1, we assign to each target class\r\nthe target probability 1 / num_true so that the target probabilities\r\nsum to 1 per-example.\r\n\r\nNote: It would be useful to allow a variable number of target classes per\r\nexample.  We hope to provide this functionality in a future release.\r\nFor now, if you have a variable number of target classes, you can pad them\r\nout to a constant number by either repeating them or by padding\r\nwith an otherwise unused class.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`weights`</b>: A `Tensor` of shape [num_classes, dim].  The class embeddings.\r\n*  <b>`biases`</b>: A `Tensor` of shape [num_classes].  The class biases.\r\n*  <b>`inputs`</b>: A `Tensor` of shape [batch_size, dim].  The forward\r\n      activations of the input network.\r\n*  <b>`labels`</b>: A `Tensor` of type `int64` and shape `[batch_size,\r\n    num_true]`. The target classes.\r\n*  <b>`num_sampled`</b>: An `int`.  The number of classes to randomly sample per batch.\r\n*  <b>`num_classes`</b>: An `int`. The number of possible classes.\r\n*  <b>`num_true`</b>: An `int`.  The number of target classes per training example.\r\n*  <b>`sampled_values`</b>: a tuple of `(sampled_candidates, true_expected_count,\r\n      sampled_expected_count)` returned by a *_candidate_sampler function.\r\n      (if None, we default to LogUniformCandidateSampler)\r\n*  <b>`remove_accidental_hits`</b>: A `bool`.  Whether to remove \"accidental hits\"\r\n      where a sampled class equals one of the target classes.  If set to\r\n      `True`, this is a \"Sampled Logistic\" loss instead of NCE, and we are\r\n      learning to generate log-odds instead of log probabilities.  See\r\n      our [Candidate Sampling Algorithms Reference]\r\n      (http://www.tensorflow.org/extras/candidate_sampling.pdf).\r\n      Default is False.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A batch_size 1-D tensor of per-example NCE losses.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.sampled_softmax_loss(weights, biases, inputs, labels, num_sampled, num_classes, num_true=1, sampled_values=None, remove_accidental_hits=True, name='sampled_softmax_loss')` <a class=\"md-anchor\" id=\"sampled_softmax_loss\"></a>\r\n\r\nComputes and returns the sampled softmax training loss.\r\n\r\nThis is a faster way to train a softmax classifier over a huge number of\r\nclasses.\r\n\r\nThis operation is for training only.  It is generally an underestimate of\r\nthe full softmax loss.\r\n\r\nAt inference time, you can compute full softmax probabilities with the\r\nexpression `tf.nn.softmax(tf.matmul(inputs, weights) + biases)`.\r\n\r\nSee our [Candidate Sampling Algorithms Reference]\r\n(http://www.tensorflow.org/extras/candidate_sampling.pdf)\r\n\r\nAlso see Section 3 of http://arxiv.org/abs/1412.2007 for the math.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`weights`</b>: A `Tensor` of shape [num_classes, dim].  The class embeddings.\r\n*  <b>`biases`</b>: A `Tensor` of shape [num_classes].  The class biases.\r\n*  <b>`inputs`</b>: A `Tensor` of shape [batch_size, dim].  The forward\r\n      activations of the input network.\r\n*  <b>`labels`</b>: A `Tensor` of type `int64` and shape `[batch_size,\r\n    num_true]`. The target classes.  Note that this format differs from\r\n    the `labels` argument of `nn.softmax_cross_entropy_with_logits`.\r\n*  <b>`num_sampled`</b>: An `int`.  The number of classes to randomly sample per batch.\r\n*  <b>`num_classes`</b>: An `int`. The number of possible classes.\r\n*  <b>`num_true`</b>: An `int`.  The number of target classes per training example.\r\n*  <b>`sampled_values`</b>: a tuple of `(sampled_candidates, true_expected_count,\r\n      sampled_expected_count)` returned by a *_candidate_sampler function.\r\n      (if None, we default to LogUniformCandidateSampler)\r\n*  <b>`remove_accidental_hits`</b>: A `bool`.  whether to remove \"accidental hits\"\r\n      where a sampled class equals one of the target classes.  Default is\r\n      True.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A batch_size 1-D tensor of per-example sampled softmax losses.\r\n\r\n\r\n\r\n### Candidate Samplers <a class=\"md-anchor\" id=\"AUTOGENERATED-candidate-samplers\"></a>\r\n\r\nTensorFlow provides the following samplers for randomly sampling candidate\r\nclasses when using one of the sampled loss functions above.\r\n\r\n- - -\r\n\r\n### `tf.nn.uniform_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, seed=None, name=None)` <a class=\"md-anchor\" id=\"uniform_candidate_sampler\"></a>\r\n\r\nSamples a set of classes using a uniform base distribution.\r\n\r\nThis operation randomly samples a tensor of sampled classes\r\n(`sampled_candidates`) from the range of integers `[0, range_max]`.\r\n\r\nThe elements of `sampled_candidates` are drawn without replacement\r\n(if `unique=True`) or with replacement (if `unique=False`) from\r\nthe base distribution.\r\n\r\nThe base distribution for this operation is the uniform distribution\r\nover the range of integers `[0, range_max]`.\r\n\r\nIn addition, this operation returns tensors `true_expected_count`\r\nand `sampled_expected_count` representing the number of times each\r\nof the target classes (`true_classes`) and the sampled\r\nclasses (`sampled_candidates`) is expected to occur in an average\r\ntensor of sampled classes.  These values correspond to `Q(y|x)`\r\ndefined in [this\r\ndocument](http://www.tensorflow.org/extras/candidate_sampling.pdf).\r\nIf `unique=True`, then these are post-rejection probabilities and we\r\ncompute them approximately.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`true_classes`</b>: A `Tensor` of type `int64` and shape `[batch_size,\r\n    num_true]`. The target classes.\r\n*  <b>`num_true`</b>: An `int`.  The number of target classes per training example.\r\n*  <b>`num_sampled`</b>: An `int`.  The number of classes to randomly sample per batch.\r\n*  <b>`unique`</b>: A `bool`. Determines whether all sampled classes in a batch are\r\n    unique.\r\n*  <b>`range_max`</b>: An `int`. The number of possible classes.\r\n*  <b>`seed`</b>: An `int`. An operation-specific seed. Default is 0.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n\r\n*  <b>`sampled_candidates`</b>: A tensor of type `int64` and shape `[num_sampled]`.\r\n    The sampled classes.\r\n*  <b>`true_expected_count`</b>: A tensor of type `float`.  Same shape as\r\n    `true_classes`. The expected counts under the sampling distribution\r\n    of each of `true_classes`.\r\n*  <b>`sampled_expected_count`</b>: A tensor of type `float`. Same shape as\r\n    `sampled_candidates`. The expected counts under the sampling distribution\r\n    of each of `sampled_candidates`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.log_uniform_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, seed=None, name=None)` <a class=\"md-anchor\" id=\"log_uniform_candidate_sampler\"></a>\r\n\r\nSamples a set of classes using a log-uniform (Zipfian) base distribution.\r\n\r\nThis operation randomly samples a tensor of sampled classes\r\n(`sampled_candidates`) from the range of integers `[0, range_max]`.\r\n\r\nThe elements of `sampled_candidates` are drawn without replacement\r\n(if `unique=True`) or with replacement (if `unique=False`) from\r\nthe base distribution.\r\n\r\nThe base distribution for this operation is an approximately log-uniform\r\nor Zipfian distribution:\r\n\r\n`P(class) = (log(class + 2) - log(class + 1)) / log(range_max + 1)`\r\n\r\nThis sampler is useful when the target classes approximately follow such\r\na distribution - for example, if the classes represent words in a lexicon\r\nsorted in decreasing order of frequency. If your classes are not ordered by\r\ndecreasing frequency, do not use this op.\r\n\r\nIn addition, this operation returns tensors `true_expected_count`\r\nand `sampled_expected_count` representing the number of times each\r\nof the target classes (`true_classes`) and the sampled\r\nclasses (`sampled_candidates`) is expected to occur in an average\r\ntensor of sampled classes.  These values correspond to `Q(y|x)`\r\ndefined in [this\r\ndocument](http://www.tensorflow.org/extras/candidate_sampling.pdf).\r\nIf `unique=True`, then these are post-rejection probabilities and we\r\ncompute them approximately.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`true_classes`</b>: A `Tensor` of type `int64` and shape `[batch_size,\r\n    num_true]`. The target classes.\r\n*  <b>`num_true`</b>: An `int`.  The number of target classes per training example.\r\n*  <b>`num_sampled`</b>: An `int`.  The number of classes to randomly sample per batch.\r\n*  <b>`unique`</b>: A `bool`. Determines whether all sampled classes in a batch are\r\n    unique.\r\n*  <b>`range_max`</b>: An `int`. The number of possible classes.\r\n*  <b>`seed`</b>: An `int`. An operation-specific seed. Default is 0.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n\r\n*  <b>`sampled_candidates`</b>: A tensor of type `int64` and shape `[num_sampled]`.\r\n    The sampled classes.\r\n*  <b>`true_expected_count`</b>: A tensor of type `float`.  Same shape as\r\n    `true_classes`. The expected counts under the sampling distribution\r\n    of each of `true_classes`.\r\n*  <b>`sampled_expected_count`</b>: A tensor of type `float`. Same shape as\r\n    `sampled_candidates`. The expected counts under the sampling distribution\r\n    of each of `sampled_candidates`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.learned_unigram_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, seed=None, name=None)` <a class=\"md-anchor\" id=\"learned_unigram_candidate_sampler\"></a>\r\n\r\nSamples a set of classes from a distribution learned during training.\r\n\r\nThis operation randomly samples a tensor of sampled classes\r\n(`sampled_candidates`) from the range of integers `[0, range_max]`.\r\n\r\nThe elements of `sampled_candidates` are drawn without replacement\r\n(if `unique=True`) or with replacement (if `unique=False`) from\r\nthe base distribution.\r\n\r\nThe base distribution for this operation is constructed on the fly\r\nduring training.  It is a unigram distribution over the target\r\nclasses seen so far during training.  Every integer in `[0, range_max]`\r\nbegins with a weight of 1, and is incremented by 1 each time it is\r\nseen as a target class.  The base distribution is not saved to checkpoints,\r\nso it is reset when the model is reloaded.\r\n\r\nIn addition, this operation returns tensors `true_expected_count`\r\nand `sampled_expected_count` representing the number of times each\r\nof the target classes (`true_classes`) and the sampled\r\nclasses (`sampled_candidates`) is expected to occur in an average\r\ntensor of sampled classes.  These values correspond to `Q(y|x)`\r\ndefined in [this\r\ndocument](http://www.tensorflow.org/extras/candidate_sampling.pdf).\r\nIf `unique=True`, then these are post-rejection probabilities and we\r\ncompute them approximately.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`true_classes`</b>: A `Tensor` of type `int64` and shape `[batch_size,\r\n    num_true]`. The target classes.\r\n*  <b>`num_true`</b>: An `int`.  The number of target classes per training example.\r\n*  <b>`num_sampled`</b>: An `int`.  The number of classes to randomly sample per batch.\r\n*  <b>`unique`</b>: A `bool`. Determines whether all sampled classes in a batch are\r\n    unique.\r\n*  <b>`range_max`</b>: An `int`. The number of possible classes.\r\n*  <b>`seed`</b>: An `int`. An operation-specific seed. Default is 0.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n\r\n*  <b>`sampled_candidates`</b>: A tensor of type `int64` and shape `[num_sampled]`.\r\n    The sampled classes.\r\n*  <b>`true_expected_count`</b>: A tensor of type `float`.  Same shape as\r\n    `true_classes`. The expected counts under the sampling distribution\r\n    of each of `true_classes`.\r\n*  <b>`sampled_expected_count`</b>: A tensor of type `float`. Same shape as\r\n    `sampled_candidates`. The expected counts under the sampling distribution\r\n    of each of `sampled_candidates`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.fixed_unigram_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, vocab_file='', distortion=0.0, num_reserved_ids=0, num_shards=1, shard=0, unigrams=[], seed=None, name=None)` <a class=\"md-anchor\" id=\"fixed_unigram_candidate_sampler\"></a>\r\n\r\nSamples a set of classes using the provided (fixed) base distribution.\r\n\r\nThis operation randomly samples a tensor of sampled classes\r\n(`sampled_candidates`) from the range of integers `[0, range_max]`.\r\n\r\nThe elements of `sampled_candidates` are drawn without replacement\r\n(if `unique=True`) or with replacement (if `unique=False`) from\r\nthe base distribution.\r\n\r\nThe base distribution is read from a file or passed in as an\r\nin-memory array. There is also an option to skew the distribution by\r\napplying a distortion power to the weights.\r\n\r\nIn addition, this operation returns tensors `true_expected_count`\r\nand `sampled_expected_count` representing the number of times each\r\nof the target classes (`true_classes`) and the sampled\r\nclasses (`sampled_candidates`) is expected to occur in an average\r\ntensor of sampled classes.  These values correspond to `Q(y|x)`\r\ndefined in [this\r\ndocument](http://www.tensorflow.org/extras/candidate_sampling.pdf).\r\nIf `unique=True`, then these are post-rejection probabilities and we\r\ncompute them approximately.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`true_classes`</b>: A `Tensor` of type `int64` and shape `[batch_size,\r\n    num_true]`. The target classes.\r\n*  <b>`num_true`</b>: An `int`.  The number of target classes per training example.\r\n*  <b>`num_sampled`</b>: An `int`.  The number of classes to randomly sample per batch.\r\n*  <b>`unique`</b>: A `bool`. Determines whether all sampled classes in a batch are\r\n    unique.\r\n*  <b>`range_max`</b>: An `int`. The number of possible classes.\r\n*  <b>`vocab_file`</b>: Each valid line in this file (which should have a CSV-like\r\n    format) corresponds to a valid word ID. IDs are in sequential order,\r\n    starting from num_reserved_ids. The last entry in each line is expected\r\n    to be a value corresponding to the count or relative probability. Exactly\r\n    one of `vocab_file` and `unigrams` needs to be passed to this operation.\r\n*  <b>`distortion`</b>: The distortion is used to skew the unigram probability\r\n    distribution.  Each weight is first raised to the distortion's power\r\n    before adding to the internal unigram distribution. As a result,\r\n    `distortion = 1.0` gives regular unigram sampling (as defined by the vocab\r\n    file), and `distortion = 0.0` gives a uniform distribution.\r\n*  <b>`num_reserved_ids`</b>: Optionally some reserved IDs can be added in the range\r\n    `[0, num_reserved_ids]` by the users. One use case is that a special\r\n    unknown word token is used as ID 0. These IDs will have a sampling\r\n    probability of 0.\r\n*  <b>`num_shards`</b>: A sampler can be used to sample from a subset of the original\r\n    range in order to speed up the whole computation through parallelism. This\r\n    parameter (together with `shard`) indicates the number of partitions that\r\n    are being used in the overall computation.\r\n*  <b>`shard`</b>: A sampler can be used to sample from a subset of the original range\r\n    in order to speed up the whole computation through parallelism. This\r\n    parameter (together with `num_shards`) indicates the particular partition\r\n    number of the operation, when partitioning is being used.\r\n*  <b>`unigrams`</b>: A list of unigram counts or probabilities, one per ID in\r\n    sequential order. Exactly one of `vocab_file` and `unigrams` should be\r\n    passed to this operation.\r\n*  <b>`seed`</b>: An `int`. An operation-specific seed. Default is 0.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n\r\n*  <b>`sampled_candidates`</b>: A tensor of type `int64` and shape `[num_sampled]`.\r\n    The sampled classes.\r\n*  <b>`true_expected_count`</b>: A tensor of type `float`.  Same shape as\r\n    `true_classes`. The expected counts under the sampling distribution\r\n    of each of `true_classes`.\r\n*  <b>`sampled_expected_count`</b>: A tensor of type `float`. Same shape as\r\n    `sampled_candidates`. The expected counts under the sampling distribution\r\n    of each of `sampled_candidates`.\r\n\r\n\r\n\r\n### Miscellaneous candidate sampling utilities <a class=\"md-anchor\" id=\"AUTOGENERATED-miscellaneous-candidate-sampling-utilities\"></a>\r\n\r\n- - -\r\n\r\n### `tf.nn.compute_accidental_hits(true_classes, sampled_candidates, num_true, seed=None, name=None)` <a class=\"md-anchor\" id=\"compute_accidental_hits\"></a>\r\n\r\nCompute the ids of positions in sampled_candidates matching true_classes.\r\n\r\nIn Candidate Sampling, this operation facilitates virtually removing\r\nsampled classes which happen to match target classes.  This is done\r\nin Sampled Softmax and Sampled Logistic.\r\n\r\nSee our [Candidate Sampling Algorithms\r\nReference](http://www.tensorflow.org/extras/candidate_sampling.pdf).\r\n\r\nWe presuppose that the `sampled_candidates` are unique.\r\n\r\nWe call it an 'accidental hit' when one of the target classes\r\nmatches one of the sampled classes.  This operation reports\r\naccidental hits as triples `(index, id, weight)`, where `index`\r\nrepresents the row number in `true_classes`, `id` represents the\r\nposition in `sampled_candidates`, and weight is `-FLOAT_MAX`.\r\n\r\nThe result of this op should be passed through a `sparse_to_dense`\r\noperation, then added to the logits of the sampled classes. This\r\nremoves the contradictory effect of accidentally sampling the true\r\ntarget classes as noise classes for the same example.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`true_classes`</b>: A `Tensor` of type `int64` and shape `[batch_size,\r\n    num_true]`. The target classes.\r\n*  <b>`sampled_candidates`</b>: A tensor of type `int64` and shape `[num_sampled]`.\r\n    The sampled_candidates output of CandidateSampler.\r\n*  <b>`num_true`</b>: An `int`.  The number of target classes per training example.\r\n*  <b>`seed`</b>: An `int`. An operation-specific seed. Default is 0.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n\r\n*  <b>`indices`</b>: A `Tensor` of type `int32` and shape `[num_accidental_hits]`.\r\n    Values indicate rows in `true_classes`.\r\n*  <b>`ids`</b>: A `Tensor` of type `int64` and shape `[num_accidental_hits]`.\r\n    Values indicate positions in `sampled_candidates`.\r\n*  <b>`weights`</b>: A `Tensor` of type `float` and shape `[num_accidental_hits]`.\r\n    Each value is `-FLOAT_MAX`.\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/python/python_io.md",
    "content": "<!-- This file is machine generated: DO NOT EDIT! -->\r\n\r\n# Data IO (Python functions) <a class=\"md-anchor\" id=\"AUTOGENERATED-data-io--python-functions-\"></a>\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Data IO (Python functions)](#AUTOGENERATED-data-io--python-functions-)\r\n* [Data IO (Python Functions)](#AUTOGENERATED-data-io--python-functions-)\r\n  * [`class tf.python_io.TFRecordWriter`](#TFRecordWriter)\r\n  * [`tf.python_io.tf_record_iterator(path)`](#tf_record_iterator)\r\n  * [TFRecords Format Details](#AUTOGENERATED-tfrecords-format-details)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## Data IO (Python Functions) <a class=\"md-anchor\" id=\"AUTOGENERATED-data-io--python-functions-\"></a>\r\n\r\nA TFRecords file represents a sequence of (binary) strings.  The format is not\r\nrandom access, so it is suitable for streaming large amounts of data but not\r\nsuitable if fast sharding or other non-sequential access is desired.\r\n\r\n- - -\r\n\r\n### `class tf.python_io.TFRecordWriter` <a class=\"md-anchor\" id=\"TFRecordWriter\"></a>\r\n\r\nA class to write records to a TFRecords file.\r\n\r\nThis class implements `__enter__` and `__exit__`, and can be used\r\nin `with` blocks like a normal file.\r\n\r\n- - -\r\n\r\n#### `tf.python_io.TFRecordWriter.__init__(path)` <a class=\"md-anchor\" id=\"TFRecordWriter.__init__\"></a>\r\n\r\nOpens file `path` and creates a `TFRecordWriter` writing to it.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`path`</b>: The path to the TFRecords file.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`IOError`</b>: If `path` cannot be opened for writing.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.python_io.TFRecordWriter.write(record)` <a class=\"md-anchor\" id=\"TFRecordWriter.write\"></a>\r\n\r\nWrite a string record to the file.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`record`</b>: str\r\n\r\n\r\n- - -\r\n\r\n#### `tf.python_io.TFRecordWriter.close()` <a class=\"md-anchor\" id=\"TFRecordWriter.close\"></a>\r\n\r\nClose the file.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.python_io.tf_record_iterator(path)` <a class=\"md-anchor\" id=\"tf_record_iterator\"></a>\r\n\r\nAn iterator that read the records from a TFRecords file.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`path`</b>: The path to the TFRecords file.\r\n\r\n##### Yields: <a class=\"md-anchor\" id=\"AUTOGENERATED-yields-\"></a>\r\n\r\n  Strings.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`IOError`</b>: If `path` cannot be opened for reading.\r\n\r\n\r\n\r\n- - -\r\n\r\n### TFRecords Format Details <a class=\"md-anchor\" id=\"AUTOGENERATED-tfrecords-format-details\"></a>\r\n\r\nA TFRecords file contains a sequence of strings with CRC hashes.  Each record\r\nhas the format\r\n\r\n    uint64 length\r\n    uint32 masked_crc32_of_length\r\n    byte   data[length]\r\n    uint32 masked_crc32_of_data\r\n\r\nand the records are concatenated together to produce the file.  The CRC32s\r\nare [described here](https://en.wikipedia.org/wiki/Cyclic_redundancy_check),\r\nand the mask of a CRC is\r\n\r\n    masked_crc = ((crc >> 15) | (crc << 17)) + 0xa282ead8ul\r\n"
  },
  {
    "path": "SOURCE/api_docs/python/sparse_ops.md",
    "content": "<!-- This file is machine generated: DO NOT EDIT! -->\r\n\r\n# Sparse Tensors <a class=\"md-anchor\" id=\"AUTOGENERATED-sparse-tensors\"></a>\r\n\r\nNote: Functions taking `Tensor` arguments can also take anything accepted by\r\n[`tf.convert_to_tensor`](../../api_docs/python/framework.md#convert_to_tensor).\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Sparse Tensors](#AUTOGENERATED-sparse-tensors)\r\n* [Sparse Tensor Representation](#AUTOGENERATED-sparse-tensor-representation)\r\n  * [`class tf.SparseTensor`](#SparseTensor)\r\n  * [`class tf.SparseTensorValue`](#SparseTensorValue)\r\n* [Sparse to Dense Conversion](#AUTOGENERATED-sparse-to-dense-conversion)\r\n  * [`tf.sparse_to_dense(sparse_indices, output_shape, sparse_values, default_value, name=None)`](#sparse_to_dense)\r\n  * [`tf.sparse_tensor_to_dense(sp_input, default_value, name=None)`](#sparse_tensor_to_dense)\r\n  * [`tf.sparse_to_indicator(sp_input, vocab_size, name=None)`](#sparse_to_indicator)\r\n* [Manipulation](#AUTOGENERATED-manipulation)\r\n  * [`tf.sparse_concat(concat_dim, sp_inputs, name=None)`](#sparse_concat)\r\n  * [`tf.sparse_reorder(sp_input, name=None)`](#sparse_reorder)\r\n  * [`tf.sparse_retain(sp_input, to_retain)`](#sparse_retain)\r\n  * [`tf.sparse_fill_empty_rows(sp_input, default_value, name=None)`](#sparse_fill_empty_rows)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## Sparse Tensor Representation <a class=\"md-anchor\" id=\"AUTOGENERATED-sparse-tensor-representation\"></a>\r\n\r\nTensorflow supports a `SparseTensor` representation for data that is sparse\r\nin multiple dimensions. Contrast this representation with `IndexedSlices`,\r\nwhich is efficient for representing tensors that are sparse in their first\r\ndimension, and dense along all other dimensions.\r\n\r\n- - -\r\n\r\n### `class tf.SparseTensor` <a class=\"md-anchor\" id=\"SparseTensor\"></a>\r\n\r\nRepresents a sparse tensor.\r\n\r\nTensorflow represents a sparse tensor as three separate dense tensors:\r\n`indices`, `values`, and `dense_shape`.  In Python, the three tensors are\r\ncollected into a `SparseTensor` class for ease of use.  If you have separate\r\n`indices`, `values`, and `dense_shape` tensors, wrap them in a `SparseTensor`\r\nobject before passing to the Ops below.\r\n\r\nConcretely, the sparse tensor `SparseTensor(values, indices, dense_shape)` is\r\n\r\n* `indices`: A 2-D int64 tensor of shape `[N, ndims]`.\r\n* `values`: A 1-D tensor of any type and shape `[N]`.\r\n* `dense_shape`: A 1-D int64 tensor of shape `[ndims]`.\r\n\r\nwhere `N` and `ndims` are the number of values, and number of dimensions in\r\nthe `SparseTensor` respectively.\r\n\r\nThe corresponding dense tensor satisfies\r\n\r\n```python\r\ndense.shape = dense_shape\r\ndense[tuple(indices[i])] = values[i]\r\n```\r\n\r\nBy convention, `indices` should be sorted in row-major order (or equivalently\r\nlexigraphic order on the tuples `indices[i]`).  This is not enforced when\r\n`SparseTensor` objects are constructed, but most Ops assume correct ordering.\r\nIf the ordering is wrong, it can be fixed by calling `sparse_reorder` on the\r\nmisordered `SparseTensor`.\r\n\r\nExample: The sparse tensor\r\n\r\n```python\r\n  SparseTensor(values=[1, 2], indices=[[0, 0], [1, 2]], shape=[3, 4])\r\n```\r\n\r\nrepresents the dense tensor\r\n\r\n```python\r\n  [[1, 0, 0, 0]\r\n   [0, 0, 2, 0]\r\n   [0, 0, 0, 0]]\r\n```\r\n\r\n- - -\r\n\r\n#### `tf.SparseTensor.__init__(indices, values, shape)` <a class=\"md-anchor\" id=\"SparseTensor.__init__\"></a>\r\n\r\nCreates a `SparseTensor`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`indices`</b>: A 2-D int64 tensor of shape `[N, ndims]`.\r\n*  <b>`values`</b>: A 1-D tensor of any type and shape `[N]`.\r\n*  <b>`dense_shape`</b>: A 1-D int64 tensor of shape `[ndims]`.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `SparseTensor`\r\n\r\n\r\n- - -\r\n\r\n#### `tf.SparseTensor.indices` <a class=\"md-anchor\" id=\"SparseTensor.indices\"></a>\r\n\r\nThe indices of non-zero values in the represented dense tensor.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A 2-D Tensor of int64 with shape `[N, ndims]`, where `N` is the\r\n    number of non-zero values in the tensor, and `ndims` is the rank.\r\n\r\n- - -\r\n\r\n#### `tf.SparseTensor.values` <a class=\"md-anchor\" id=\"SparseTensor.values\"></a>\r\n\r\nThe non-zero values in the represented dense tensor.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A 1-D Tensor of any data type.\r\n\r\n- - -\r\n\r\n#### `tf.SparseTensor.dtype` <a class=\"md-anchor\" id=\"SparseTensor.dtype\"></a>\r\n\r\nThe `DType` of elements in this tensor.\r\n\r\n- - -\r\n\r\n#### `tf.SparseTensor.shape` <a class=\"md-anchor\" id=\"SparseTensor.shape\"></a>\r\n\r\nA 1-D Tensor of int64 representing the shape of the dense tensor.\r\n\r\n- - -\r\n\r\n#### `tf.SparseTensor.graph` <a class=\"md-anchor\" id=\"SparseTensor.graph\"></a>\r\n\r\nThe `Graph` that contains the index, value, and shape tensors.\r\n\r\n\r\n- - -\r\n\r\n### `class tf.SparseTensorValue` <a class=\"md-anchor\" id=\"SparseTensorValue\"></a>\r\n\r\nSparseTensorValue(indices, values, shape)\r\n- - -\r\n\r\n#### `tf.SparseTensorValue.indices` <a class=\"md-anchor\" id=\"SparseTensorValue.indices\"></a>\r\n\r\nAlias for field number 0\r\n\r\n- - -\r\n\r\n#### `tf.SparseTensorValue.shape` <a class=\"md-anchor\" id=\"SparseTensorValue.shape\"></a>\r\n\r\nAlias for field number 2\r\n\r\n- - -\r\n\r\n#### `tf.SparseTensorValue.values` <a class=\"md-anchor\" id=\"SparseTensorValue.values\"></a>\r\n\r\nAlias for field number 1\r\n\r\n\r\n\r\n## Sparse to Dense Conversion <a class=\"md-anchor\" id=\"AUTOGENERATED-sparse-to-dense-conversion\"></a>\r\n\r\n- - -\r\n\r\n### `tf.sparse_to_dense(sparse_indices, output_shape, sparse_values, default_value, name=None)` <a class=\"md-anchor\" id=\"sparse_to_dense\"></a>\r\n\r\nConverts a sparse representation into a dense tensor.\r\n\r\nBuilds an array `dense` with shape `output_shape` such that\r\n\r\n```prettyprint\r\n# If sparse_indices is scalar\r\ndense[i] = (i == sparse_indices ? sparse_values : default_value)\r\n\r\n# If sparse_indices is a vector, then for each i\r\ndense[sparse_indices[i]] = sparse_values[i]\r\n\r\n# If sparse_indices is an n by d matrix, then for each i in [0, n)\r\ndense[sparse_indices[i][0], ..., sparse_indices[i][d-1]] = sparse_values[i]\r\n```\r\n\r\nAll other values in `dense` are set to `default_value`.  If `sparse_values` is a\r\nscalar, all sparse indices are set to this single value.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`sparse_indices`</b>: A `Tensor`. Must be one of the following types: `int32`, `int64`.\r\n    0-D, 1-D, or 2-D.  `sparse_indices[i]` contains the complete\r\n    index where `sparse_values[i]` will be placed.\r\n*  <b>`output_shape`</b>: A `Tensor`. Must have the same type as `sparse_indices`.\r\n    1-D.  Shape of the dense output tensor.\r\n*  <b>`sparse_values`</b>: A `Tensor`.\r\n    1-D.  Values corresponding to each row of `sparse_indices`,\r\n    or a scalar value to be used for all sparse indices.\r\n*  <b>`default_value`</b>: A `Tensor`. Must have the same type as `sparse_values`.\r\n    Scalar value to set for indices not specified in\r\n    `sparse_indices`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `sparse_values`.\r\n  Dense output tensor of shape `output_shape`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.sparse_tensor_to_dense(sp_input, default_value, name=None)` <a class=\"md-anchor\" id=\"sparse_tensor_to_dense\"></a>\r\n\r\nConverts a `SparseTensor` into a dense tensor.\r\n\r\nThis op is a convenience wrapper around `sparse_to_dense` for `SparseTensor`s.\r\n\r\nFor example, if `sp_input` has shape `[3, 5]` and non-empty string values:\r\n\r\n    [0, 1]: a\r\n    [0, 3]: b\r\n    [2, 0]: c\r\n\r\nand `default_value` is `x`, then the output will be a dense `[3, 5]`\r\nstring tensor with values:\r\n\r\n    [[x a x b x]\r\n     [x x x x x]\r\n     [c x x x x]]\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`sp_input`</b>: The input `SparseTensor`.\r\n*  <b>`default_value`</b>: Scalar value to set for indices not specified in\r\n    `sp_input`.\r\n*  <b>`name`</b>: A name prefix for the returned tensors (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A dense tensor with shape `sp_input.shape` and values specified by\r\n  the non-empty values in `sp_input`. Indices not in `sp_input` are assigned\r\n  `default_value`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `sp_input` is not a `SparseTensor`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.sparse_to_indicator(sp_input, vocab_size, name=None)` <a class=\"md-anchor\" id=\"sparse_to_indicator\"></a>\r\n\r\nConverts a `SparseTensor` of ids into a dense bool indicator tensor.\r\n\r\nThe last dimension of `sp_input` is discarded and replaced with the values of\r\n`sp_input`.  If `sp_input.shape = [D0, D1, ..., Dn, K]`, then\r\n`output.shape = [D0, D1, ..., Dn, vocab_size]`, where\r\n\r\n    output[d_0, d_1, ..., d_n, sp_input[d_0, d_1, ..., d_n, k]] = True\r\n\r\nand False elsewhere in `output`.\r\n\r\nFor example, if `sp_input.shape = [2, 3, 4]` with non-empty values:\r\n\r\n    [0, 0, 0]: 0\r\n    [0, 1, 0]: 10\r\n    [1, 0, 3]: 103\r\n    [1, 1, 2]: 112\r\n    [1, 1, 3]: 113\r\n    [1, 2, 1]: 121\r\n\r\nand `vocab_size = 200`, then the output will be a `[2, 3, 200]` dense bool\r\ntensor with False everywhere except at positions\r\n\r\n    (0, 0, 0), (0, 1, 10), (1, 0, 103), (1, 1, 112), (1, 1, 113), (1, 2, 121).\r\n\r\nThis op is useful for converting `SparseTensor`s into dense formats for\r\ncompatibility with ops that expect dense tensors.\r\n\r\nThe input `SparseTensor` must be in row-major order.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`sp_input`</b>: A `SparseTensor` of type `int32` or `int64`.\r\n*  <b>`vocab_size`</b>: The new size of the last dimension, with\r\n    `all(0 <= sp_input.values < vocab_size)`.\r\n*  <b>`name`</b>: A name prefix for the returned tensors (optional)\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A dense bool indicator tensor representing the indices with specified value.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `sp_input` is not a `SparseTensor`.\r\n\r\n\r\n\r\n## Manipulation <a class=\"md-anchor\" id=\"AUTOGENERATED-manipulation\"></a>\r\n\r\n- - -\r\n\r\n### `tf.sparse_concat(concat_dim, sp_inputs, name=None)` <a class=\"md-anchor\" id=\"sparse_concat\"></a>\r\n\r\nConcatenates a list of `SparseTensor` along the specified dimension.\r\n\r\nConcatenation is with respect to the dense versions of each sparse input.\r\nIt is assumed that each inputs is a `SparseTensor` whose elements are ordered\r\nalong increasing dimension number.\r\n\r\nAll inputs' shapes must match, except for the concat dimension.  The\r\n`indices`, `values`, and `shapes` lists must have the same length.\r\n\r\nThe output shape is identical to the inputs', except along the concat\r\ndimension, where it is the sum of the inputs' sizes along that dimension.\r\n\r\nThe output elements will be resorted to preserve the sort order along\r\nincreasing dimension number.\r\n\r\nThis op runs in `O(M log M)` time, where `M` is the total number of non-empty\r\nvalues across all inputs. This is due to the need for an internal sort in\r\norder to concatenate efficiently across an arbitrary dimension.\r\n\r\nFor example, if `concat_dim = 1` and the inputs are\r\n\r\n    sp_inputs[0]: shape = [2, 3]\r\n    [0, 2]: \"a\"\r\n    [1, 0]: \"b\"\r\n    [1, 1]: \"c\"\r\n\r\n    sp_inputs[1]: shape = [2, 4]\r\n    [0, 1]: \"d\"\r\n    [0, 2]: \"e\"\r\n\r\nthen the output will be\r\n\r\n    shape = [2, 7]\r\n    [0, 2]: \"a\"\r\n    [0, 4]: \"d\"\r\n    [0, 5]: \"e\"\r\n    [1, 0]: \"b\"\r\n    [1, 1]: \"c\"\r\n\r\nGraphically this is equivalent to doing\r\n\r\n    [    a] concat [  d e  ] = [    a   d e  ]\r\n    [b c  ]        [       ]   [b c          ]\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`concat_dim`</b>: Dimension to concatenate along.\r\n*  <b>`sp_inputs`</b>: List of `SparseTensor` to concatenate.\r\n*  <b>`name`</b>: A name prefix for the returned tensors (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `SparseTensor` with the concatenated output.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `sp_inputs` is not a list of `SparseTensor`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.sparse_reorder(sp_input, name=None)` <a class=\"md-anchor\" id=\"sparse_reorder\"></a>\r\n\r\nReorders a `SparseTensor` into the canonical, row-major ordering.\r\n\r\nNote that by convention, all sparse ops preserve the canonical ordering\r\nalong increasing dimension number. The only time ordering can be violated\r\nis during manual manipulation of the indices and values to add entries.\r\n\r\nReordering does not affect the shape of the `SparseTensor`.\r\n\r\nFor example, if sp_input has shape `[4, 5]` and `indices` / `values`:\r\n\r\n    [0, 3]: b\r\n    [0, 1]: a\r\n    [3, 1]: d\r\n    [2, 0]: c\r\n\r\nthen the output will be a `SparseTensor` of shape `[4, 5]` and\r\n`indices` / `values`:\r\n\r\n    [0, 1]: a\r\n    [0, 3]: b\r\n    [2, 0]: c\r\n    [3, 1]: d\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`sp_input`</b>: The input `SparseTensor`.\r\n*  <b>`name`</b>: A name prefix for the returned tensors (optional)\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `SparseTensor` with the same shape and non-empty values, but in\r\n  canonical ordering.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `sp_input` is not a `SparseTensor`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.sparse_retain(sp_input, to_retain)` <a class=\"md-anchor\" id=\"sparse_retain\"></a>\r\n\r\nRetains specified non-empty values within a `SparseTensor`.\r\n\r\nFor example, if `sp_input` has shape `[4, 5]` and 4 non-empty string values:\r\n\r\n    [0, 1]: a\r\n    [0, 3]: b\r\n    [2, 0]: c\r\n    [3, 1]: d\r\n\r\nand `to_retain = [True, False, False, True]`, then the output will\r\nbe a `SparseTensor` of shape `[4, 5]` with 2 non-empty values:\r\n\r\n    [0, 1]: a\r\n    [3, 1]: d\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`sp_input`</b>: The input `SparseTensor` with `N` non-empty elements.\r\n*  <b>`to_retain`</b>: A bool vector of length `N` with `M` true values.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `SparseTensor` with the same shape as the input and `M` non-empty\r\n  elements corresponding to the true positions in `to_retain`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `sp_input` is not a `SparseTensor`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.sparse_fill_empty_rows(sp_input, default_value, name=None)` <a class=\"md-anchor\" id=\"sparse_fill_empty_rows\"></a>\r\n\r\nFills empty rows in the input 2-D `SparseTensor` with a default value.\r\n\r\nThis op adds entries with the specified `default_value` at index\r\n`[row, 0]` for any row in the input that does not already have a value.\r\n\r\nFor example, suppose `sp_input` has shape `[5, 6]` and non-empty values:\r\n\r\n    [0, 1]: a\r\n    [0, 3]: b\r\n    [2, 0]: c\r\n    [3, 1]: d\r\n\r\nRows 1 and 4 are empty, so the output will be of shape `[5, 6]` with values:\r\n\r\n    [0, 1]: a\r\n    [0, 3]: b\r\n    [1, 0]: default_value\r\n    [2, 0]: c\r\n    [3, 1]: d\r\n    [4, 0]: default_value\r\n\r\nNote that the input may have empty columns at the end, with no effect on\r\nthis op.\r\n\r\nThe output `SparseTensor` will be in row-major order and will have the\r\nsame shape as the input.\r\n\r\nThis op also returns an indicator vector such that\r\n\r\n    empty_row_indicator[i] = True iff row i was an empty row.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`sp_input`</b>: A `SparseTensor` with shape `[N, M]`.\r\n*  <b>`default_value`</b>: The value to fill for empty rows, with the same type as\r\n    `sp_input.`\r\n*  <b>`name`</b>: A name prefix for the returned tensors (optional)\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n\r\n*  <b>`sp_ordered_output`</b>: A `SparseTensor` with shape `[N, M]`, and with all empty\r\n    rows filled in with `default_value`.\r\n*  <b>`empty_row_indicator`</b>: A bool vector of length `N` indicating whether each\r\n    input row was empty.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `sp_input` is not a `SparseTensor`.\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/python/state_ops.md",
    "content": "<!-- This file is machine generated: DO NOT EDIT! -->\r\n\r\n# Variables <a class=\"md-anchor\" id=\"AUTOGENERATED-variables\"></a>\r\n\r\nNote: Functions taking `Tensor` arguments can also take anything accepted by\r\n[`tf.convert_to_tensor`](../../api_docs/python/framework.md#convert_to_tensor).\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Variables](#AUTOGENERATED-variables)\r\n* [Variables](#AUTOGENERATED-variables)\r\n  * [`class tf.Variable`](#Variable)\r\n* [Variable helper functions](#AUTOGENERATED-variable-helper-functions)\r\n  * [`tf.all_variables()`](#all_variables)\r\n  * [`tf.trainable_variables()`](#trainable_variables)\r\n  * [`tf.initialize_all_variables()`](#initialize_all_variables)\r\n  * [`tf.initialize_variables(var_list, name='init')`](#initialize_variables)\r\n  * [`tf.assert_variables_initialized(var_list=None)`](#assert_variables_initialized)\r\n* [Saving and Restoring Variables](#AUTOGENERATED-saving-and-restoring-variables)\r\n  * [`class tf.train.Saver`](#Saver)\r\n  * [`tf.train.latest_checkpoint(checkpoint_dir, latest_filename=None)`](#latest_checkpoint)\r\n  * [`tf.train.get_checkpoint_state(checkpoint_dir, latest_filename=None)`](#get_checkpoint_state)\r\n  * [`tf.train.update_checkpoint_state(save_dir, model_checkpoint_path, all_model_checkpoint_paths=None, latest_filename=None)`](#update_checkpoint_state)\r\n* [Sharing Variables](#AUTOGENERATED-sharing-variables)\r\n  * [`tf.get_variable(name, shape=None, dtype=tf.float32, initializer=None, trainable=True, collections=None)`](#get_variable)\r\n  * [`tf.get_variable_scope()`](#get_variable_scope)\r\n  * [`tf.variable_scope(name_or_scope, reuse=None, initializer=None)`](#variable_scope)\r\n  * [`tf.constant_initializer(value=0.0)`](#constant_initializer)\r\n  * [`tf.random_normal_initializer(mean=0.0, stddev=1.0, seed=None)`](#random_normal_initializer)\r\n  * [`tf.truncated_normal_initializer(mean=0.0, stddev=1.0, seed=None)`](#truncated_normal_initializer)\r\n  * [`tf.random_uniform_initializer(minval=0.0, maxval=1.0, seed=None)`](#random_uniform_initializer)\r\n  * [`tf.uniform_unit_scaling_initializer(factor=1.0, seed=None)`](#uniform_unit_scaling_initializer)\r\n  * [`tf.zeros_initializer(shape, dtype=tf.float32)`](#zeros_initializer)\r\n* [Sparse Variable Updates](#AUTOGENERATED-sparse-variable-updates)\r\n  * [`tf.scatter_update(ref, indices, updates, use_locking=None, name=None)`](#scatter_update)\r\n  * [`tf.scatter_add(ref, indices, updates, use_locking=None, name=None)`](#scatter_add)\r\n  * [`tf.scatter_sub(ref, indices, updates, use_locking=None, name=None)`](#scatter_sub)\r\n  * [`tf.sparse_mask(a, mask_indices, name=None)`](#sparse_mask)\r\n  * [`class tf.IndexedSlices`](#IndexedSlices)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## Variables <a class=\"md-anchor\" id=\"AUTOGENERATED-variables\"></a>\r\n\r\n- - -\r\n\r\n### `class tf.Variable` <a class=\"md-anchor\" id=\"Variable\"></a>\r\n\r\nSee the [Variables How To](../../how_tos/variables/index.md) for a high\r\nlevel overview.\r\n\r\nA variable maintains state in the graph across calls to `run()`. You add a\r\nvariable to the graph by constructing an instance of the class `Variable`.\r\n\r\nThe `Variable()` constructor requires an initial value for the variable,\r\nwhich can be a `Tensor` of any type and shape. The initial value defines the\r\ntype and shape of the variable. After construction, the type and shape of\r\nthe variable are fixed. The value can be changed using one of the assign\r\nmethods.\r\n\r\nIf you want to change the shape of a variable later you have to use an\r\n`assign` Op with `validate_shape=False`.\r\n\r\nJust like any `Tensor`, variables created with `Variable()` can be used as\r\ninputs for other Ops in the graph. Additionally, all the operators\r\noverloaded for the `Tensor` class are carried over to variables, so you can\r\nalso add nodes to the graph by just doing arithmetic on variables.\r\n\r\n```python\r\nimport tensorflow as tf\r\n\r\n# Create a variable.\r\nw = tf.Variable(<initial-value>, name=<optional-name>)\r\n\r\n# Use the variable in the graph like any Tensor.\r\ny = tf.matmul(w, ...another variable or tensor...)\r\n\r\n# The overloaded operators are available too.\r\nz = tf.sigmoid(w + b)\r\n\r\n# Assign a new value to the variable with `assign()` or a related method.\r\nw.assign(w + 1.0)\r\nw.assign_add(1.0)\r\n```\r\n\r\nWhen you launch the graph, variables have to be explicitly initialized before\r\nyou can run Ops that use their value. You can initialize a variable by\r\nrunning its *initializer op*, restoring the variable from a save file, or\r\nsimply running an `assign` Op that assigns a value to the variable. In fact,\r\nthe variable *initializer op* is just an `assign` Op that assigns the\r\nvariable's initial value to the variable itself.\r\n\r\n```python\r\n# Launch the graph in a session.\r\nwith tf.Session() as sess:\r\n    # Run the variable initializer.\r\n    sess.run(w.initializer)\r\n    # ...you now can run ops that use the value of 'w'...\r\n```\r\n\r\nThe most common initialization pattern is to use the convenience function\r\n`initialize_all_variables()` to add an Op to the graph that initializes\r\nall the variables. You then run that Op after launching the graph.\r\n\r\n```python\r\n# Add an Op to initialize all variables.\r\ninit_op = tf.initialize_all_variables()\r\n\r\n# Launch the graph in a session.\r\nwith tf.Session() as sess:\r\n    # Run the Op that initializes all variables.\r\n    sess.run(init_op)\r\n    # ...you can now run any Op that uses variable values...\r\n```\r\n\r\nIf you need to create a variable with an initial value dependent on another\r\nvariable, use the other variable's `initialized_value()`. This ensures that\r\nvariables are initialized in the right order.\r\n\r\nAll variables are automatically collected in the graph where they are\r\ncreated. By default, the constructor adds the new variable to the graph\r\ncollection `GraphKeys.VARIABLES`. The convenience function\r\n`all_variables()` returns the contents of that collection.\r\n\r\nWhen building a machine learning model it is often convenient to distinguish\r\nbetwen variables holding the trainable model parameters and other variables\r\nsuch as a `global step` variable used to count training steps. To make this\r\neasier, the variable constructor supports a `trainable=<bool>` parameter. If\r\n`True`, the new variable is also added to the graph collection\r\n`GraphKeys.TRAINABLE_VARIABLES`. The convenience function\r\n`trainable_variables()` returns the contents of this collection. The\r\nvarious `Optimizer` classes use this collection as the default list of\r\nvariables to optimize.\r\n\r\n\r\nCreating a variable.\r\n\r\n- - -\r\n\r\n#### `tf.Variable.__init__(initial_value, trainable=True, collections=None, validate_shape=True, name=None)` <a class=\"md-anchor\" id=\"Variable.__init__\"></a>\r\n\r\nCreates a new variable with value `initial_value`.\r\n\r\nThe new variable is added to the graph collections listed in `collections`,\r\nwhich defaults to `[GraphKeys.VARIABLES]`.\r\n\r\nIf `trainable` is `True` the variable is also added to the graph collection\r\n`GraphKeys.TRAINABLE_VARIABLES`.\r\n\r\nThis constructor creates both a `variable` Op and an `assign` Op to set the\r\nvariable to its initial value.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`initial_value`</b>: A `Tensor`, or Python object convertible to a `Tensor`.\r\n    The initial value for the Variable. Must have a shape specified unless\r\n    `validate_shape` is set to False.\r\n*  <b>`trainable`</b>: If `True`, the default, also adds the variable to the graph\r\n    collection `GraphKeys.TRAINABLE_VARIABLES`. This collection is used as\r\n    the default list of variables to use by the `Optimizer` classes.\r\n*  <b>`collections`</b>: List of graph collections keys. The new variable is added to\r\n    these collections. Defaults to `[GraphKeys.VARIABLES]`.\r\n*  <b>`validate_shape`</b>: If `False`, allows the variable to be initialized with a\r\n    value of unknown shape. If `True`, the default, the shape of\r\n    `initial_value` must be known.\r\n*  <b>`name`</b>: Optional name for the variable. Defaults to `'Variable'` and gets\r\n    uniquified automatically.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A Variable.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If the initial value does not have a shape and\r\n    `validate_shape` is `True`.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Variable.initialized_value()` <a class=\"md-anchor\" id=\"Variable.initialized_value\"></a>\r\n\r\nReturns the value of the initialized variable.\r\n\r\nYou should use this instead of the variable itself to initialize another\r\nvariable with a value that depends on the value of this variable.\r\n\r\n```python\r\n# Initialize 'v' with a random tensor.\r\nv = tf.Variable(tf.truncated_normal([10, 40]))\r\n# Use `initialized_value` to guarantee that `v` has been\r\n# initialized before its value is used to initialize `w`.\r\n# The random values are picked only once.\r\nw = tf.Variable(v.initialized_value() * 2.0)\r\n```\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` holding the value of this variable after its initializer\r\n  has run.\r\n\r\n\r\n\r\nChanging a variable value.\r\n\r\n- - -\r\n\r\n#### `tf.Variable.assign(value, use_locking=False)` <a class=\"md-anchor\" id=\"Variable.assign\"></a>\r\n\r\nAssigns a new value to the variable.\r\n\r\nThis is essentially a shortcut for `assign(self, value)`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`value`</b>: A `Tensor`. The new value for this variable.\r\n*  <b>`use_locking`</b>: If `True`, use locking during the assignment.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` that will hold the new value of this variable after\r\n  the assignment has completed.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Variable.assign_add(delta, use_locking=False)` <a class=\"md-anchor\" id=\"Variable.assign_add\"></a>\r\n\r\nAdds a value to this variable.\r\n\r\n This is essentially a shortcut for `assign_add(self, delta)`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`delta`</b>: A `Tensor`. The value to add to this variable.\r\n*  <b>`use_locking`</b>: If `True`, use locking during the operation.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` that will hold the new value of this variable after\r\n  the addition has completed.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Variable.assign_sub(delta, use_locking=False)` <a class=\"md-anchor\" id=\"Variable.assign_sub\"></a>\r\n\r\nSubtracts a value from this variable.\r\n\r\nThis is essentially a shortcut for `assign_sub(self, delta)`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`delta`</b>: A `Tensor`. The value to subtract from this variable.\r\n*  <b>`use_locking`</b>: If `True`, use locking during the operation.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` that will hold the new value of this variable after\r\n  the subtraction has completed.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Variable.scatter_sub(sparse_delta, use_locking=False)` <a class=\"md-anchor\" id=\"Variable.scatter_sub\"></a>\r\n\r\nSubtracts `IndexedSlices` from this variable.\r\n\r\nThis is essentially a shortcut for `scatter_sub(self, sparse_delta.indices,\r\nsparse_delta.values)`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`sparse_delta`</b>: `IndexedSlices` to be subtracted from this variable.\r\n*  <b>`use_locking`</b>: If `True`, use locking during the operation.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` that will hold the new value of this variable after\r\n  the scattered subtraction has completed.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if `sparse_delta` is not an `IndexedSlices`.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Variable.count_up_to(limit)` <a class=\"md-anchor\" id=\"Variable.count_up_to\"></a>\r\n\r\nIncrements this variable until it reaches `limit`.\r\n\r\nWhen that Op is run it tries to increment the variable by `1`. If\r\nincrementing the variable would bring it above `limit` then the Op raises\r\nthe exception `OutOfRangeError`.\r\n\r\nIf no error is raised, the Op outputs the value of the variable before\r\nthe increment.\r\n\r\nThis is essentially a shortcut for `count_up_to(self, limit)`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`limit`</b>: value at which incrementing the variable raises an error.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor` that will hold the variable value before the increment. If no\r\n  other Op modifies this variable, the values produced will all be\r\n  distinct.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Variable.eval(session=None)` <a class=\"md-anchor\" id=\"Variable.eval\"></a>\r\n\r\nIn a session, computes and returns the value of this variable.\r\n\r\nThis is not a graph construction method, it does not add ops to the graph.\r\n\r\nThis convenience method requires a session where the graph containing this\r\nvariable has been launched. If no session is passed, the default session is\r\nused.  See the [Session class](../../api_docs/python/client.md#Session) for more information on\r\nlaunching a graph and on sessions.\r\n\r\n```python\r\nv = tf.Variable([1, 2])\r\ninit = tf.initialize_all_variables()\r\n\r\nwith tf.Session() as sess:\r\n    sess.run(init)\r\n    # Usage passing the session explicitly.\r\n    print v.eval(sess)\r\n    # Usage with the default session.  The 'with' block\r\n    # above makes 'sess' the default session.\r\n    print v.eval()\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`session`</b>: The session to use to evaluate this variable. If\r\n    none, the default session is used.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A numpy `ndarray` with a copy of the value of this variable.\r\n\r\n\r\n\r\nProperties.\r\n\r\n- - -\r\n\r\n#### `tf.Variable.name` <a class=\"md-anchor\" id=\"Variable.name\"></a>\r\n\r\nThe name of this variable.\r\n\r\n- - -\r\n\r\n#### `tf.Variable.dtype` <a class=\"md-anchor\" id=\"Variable.dtype\"></a>\r\n\r\nThe `DType` of this variable.\r\n\r\n- - -\r\n\r\n#### `tf.Variable.get_shape()` <a class=\"md-anchor\" id=\"Variable.get_shape\"></a>\r\n\r\nThe `TensorShape` of this variable.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `TensorShape`.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.Variable.device` <a class=\"md-anchor\" id=\"Variable.device\"></a>\r\n\r\nThe device of this variable.\r\n\r\n- - -\r\n\r\n#### `tf.Variable.initializer` <a class=\"md-anchor\" id=\"Variable.initializer\"></a>\r\n\r\nThe initializer operation for this variable.\r\n\r\n- - -\r\n\r\n#### `tf.Variable.graph` <a class=\"md-anchor\" id=\"Variable.graph\"></a>\r\n\r\nThe `Graph` of this variable.\r\n\r\n- - -\r\n\r\n#### `tf.Variable.op` <a class=\"md-anchor\" id=\"Variable.op\"></a>\r\n\r\nThe `Operation` of this variable.\r\n\r\n\r\n\r\n## Variable helper functions <a class=\"md-anchor\" id=\"AUTOGENERATED-variable-helper-functions\"></a>\r\n\r\nTensorFlow provides a set of functions to help manage the set of variables\r\ncollected in the graph.\r\n\r\n- - -\r\n\r\n### `tf.all_variables()` <a class=\"md-anchor\" id=\"all_variables\"></a>\r\n\r\nReturns all variables collected in the graph.\r\n\r\nThe `Variable()` constructor automatically adds new variables to the graph\r\ncollection `GraphKeys.VARIABLES`. This convenience function returns the\r\ncontents of that collection.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of `Variable` objects.\r\n\r\n\r\n- - -\r\n\r\n### `tf.trainable_variables()` <a class=\"md-anchor\" id=\"trainable_variables\"></a>\r\n\r\nReturns all variables created with `trainable=True`.\r\n\r\nWhen passed `trainable=True`, the `Variable()` constructor automatically\r\nadds new variables to the graph collection\r\n`GraphKeys.TRAINABLE_VARIABLES`. This convenience function returns the\r\ncontents of that collection.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of Variable objects.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.initialize_all_variables()` <a class=\"md-anchor\" id=\"initialize_all_variables\"></a>\r\n\r\nReturns an Op that initializes all variables.\r\n\r\nThis is just a shortcut for `initialize_variables(all_variables())`\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An Op that initializes all variables in the graph.\r\n\r\n\r\n- - -\r\n\r\n### `tf.initialize_variables(var_list, name='init')` <a class=\"md-anchor\" id=\"initialize_variables\"></a>\r\n\r\nReturns an Op that initializes a list of variables.\r\n\r\nAfter you launch the graph in a session, you can run the returned Op to\r\ninitialize all the variables in `var_list`. This Op runs all the\r\ninitializers of the variables in `var_list` in parallel.\r\n\r\nCalling `initialize_variables()` is equivalent to passing the list of\r\ninitializers to `Group()`.\r\n\r\nIf `var_list` is empty, however, the function still returns an Op that can\r\nbe run. That Op just has no effect.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`var_list`</b>: List of `Variable` objects to initialize.\r\n*  <b>`name`</b>: Optional name for the returned operation.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An Op that run the initializers of all the specified variables.\r\n\r\n\r\n- - -\r\n\r\n### `tf.assert_variables_initialized(var_list=None)` <a class=\"md-anchor\" id=\"assert_variables_initialized\"></a>\r\n\r\nReturns an Op to check if variables are initialized.\r\n\r\nWhen run, the returned Op will raise the exception `FailedPreconditionError`\r\nif any of the variables has not yet been initialized.\r\n\r\nNote: This function is implemented by trying to fetch the values of the\r\nvariables. If one of the variables is not initialized a message may be\r\nlogged by the C++ runtime. This is expected.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`var_list`</b>: List of `Variable` objects to check. Defaults to the\r\n    value of `all_variables().`\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An Op, or None if there are no variables.\r\n\r\n\r\n\r\n## Saving and Restoring Variables <a class=\"md-anchor\" id=\"AUTOGENERATED-saving-and-restoring-variables\"></a>\r\n\r\n- - -\r\n\r\n### `class tf.train.Saver` <a class=\"md-anchor\" id=\"Saver\"></a>\r\n\r\nSaves and restores variables.\r\n\r\nSee [Variables](../../how_tos/variables/index.md)\r\nfor an overview of variables, saving and restoring.\r\n\r\nThe `Saver` class adds ops to save and restore variables to and from\r\n*checkpoints*.  It also provides convenience methods to run these ops.\r\n\r\nCheckpoints are binary files in a proprietary format which map variable names\r\nto tensor values.  The best way to examine the contents of a checkpoint is to\r\nload it using a `Saver`.\r\n\r\nSavers can automatically number checkpoint filenames with a provided counter.\r\nThis lets you keep multiple checkpoints at different steps while training a\r\nmodel.  For example you can number the checkpoint filenames with the training\r\nstep number.  To avoid filling up disks, savers manage checkpoint files\r\nautomatically. For example, they can keep only the N most recent files, or\r\none checkpoint for every N hours of training.\r\n\r\nYou number checkpoint filenames by passing a value to the optional\r\n`global_step` argument to `save()`:\r\n\r\n```python\r\nsaver.save(sess, 'my-model', global_step=0) ==> filename: 'my-model-0'\r\n...\r\nsaver.save(sess, 'my-model', global_step=1000) ==> filename: 'my-model-1000'\r\n```\r\n\r\nAdditionally, optional arguments to the `Saver()` constructor let you control\r\nthe proliferation of checkpoint files on disk:\r\n\r\n* `max_to_keep` indicates the maximum number of recent checkpoint files to\r\n  keep.  As new files are created, older files are deleted.  If None or 0,\r\n  all checkpoint files are kept.  Defaults to 5 (that is, the 5 most recent\r\n  checkpoint files are kept.)\r\n\r\n* `keep_checkpoint_every_n_hours`: In addition to keeping the most recent\r\n  `max_to_keep` checkpoint files, you might want to keep one checkpoint file\r\n  for every N hours of training.  This can be useful if you want to later\r\n  analyze how a model progressed during a long training session.  For\r\n  example, passing `keep_checkpoint_every_n_hours=2` ensures that you keep\r\n  one checkpoint file for every 2 hours of training.  The default value of\r\n  10,000 hours effectively disables the feature.\r\n\r\nNote that you still have to call the `save()` method to save the model.\r\nPassing these arguments to the constructor will not save variables\r\nautomatically for you.\r\n\r\nA training program that saves regularly looks like:\r\n\r\n```python\r\n...\r\n# Create a saver.\r\nsaver = tf.train.Saver(...variables...)\r\n# Launch the graph and train, saving the model every 1,000 steps.\r\nsess = tf.Session()\r\nfor step in xrange(1000000):\r\n    sess.run(..training_op..)\r\n    if step % 1000 == 0:\r\n        # Append the step number to the checkpoint name:\r\n        saver.save(sess, 'my-model', global_step=step)\r\n```\r\n\r\nIn addition to checkpoint files, savers keep a protocol buffer on disk with\r\nthe list of recent checkpoints. This is used to manage numbered checkpoint\r\nfiles and by `latest_checkpoint()`, which makes it easy to discover the path\r\nto the most recent checkpoint. That protocol buffer is stored in a file named\r\n'checkpoint' next to the checkpoint files.\r\n\r\nIf you create several savers, you can specify a different filename for the\r\nprotocol buffer file in the call to `save()`.\r\n\r\n- - -\r\n\r\n#### `tf.train.Saver.__init__(var_list=None, reshape=False, sharded=False, max_to_keep=5, keep_checkpoint_every_n_hours=10000.0, name=None, restore_sequentially=False, saver_def=None, builder=None)` <a class=\"md-anchor\" id=\"Saver.__init__\"></a>\r\n\r\nCreates a `Saver`.\r\n\r\nThe constructor adds ops to save and restore variables.\r\n\r\n`var_list` specifies the variables that will be saved and restored. It can\r\nbe passed as a `dict` or a list:\r\n\r\n* A `dict` of names to variables: The keys are the names that will be\r\n  used to save or restore the variables in the checkpoint files.\r\n* A list of variables: The variables will be keyed with their op name in\r\n  the checkpoint files.\r\n\r\nFor example:\r\n\r\n```python\r\nv1 = tf.Variable(..., name='v1')\r\nv2 = tf.Variable(..., name='v2')\r\n\r\n# Pass the variables as a dict:\r\nsaver = tf.train.Saver({'v1': v1, 'v2': v2})\r\n\r\n# Or pass them as a list.\r\nsaver = tf.train.Saver([v1, v2])\r\n# Passing a list is equivalent to passing a dict with the variable op names\r\n# as keys:\r\nsaver = tf.train.Saver({v.op.name: v for v in [v1, v2]})\r\n```\r\n\r\nThe optional `reshape` argument, if True, allows restoring a variable from\r\na save file where the variable had a different shape, but the same number\r\nof elements and type.  This is useful if you have reshaped a variable and\r\nwant to reload it from an older checkpoint.\r\n\r\nThe optional `sharded` argument, if True, instructs the saver to shard\r\ncheckpoints per device.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`var_list`</b>: A list of Variables or a dictionary mapping names to\r\n    Variables.  If None, defaults to the list of all variables.\r\n*  <b>`reshape`</b>: If True, allows restoring parameters from a checkpoint\r\n    where the variables have a different shape.\r\n*  <b>`sharded`</b>: If True, shard the checkpoints, one per device.\r\n*  <b>`max_to_keep`</b>: maximum number of recent checkpoints to keep.\r\n    Defaults to 10,000 hours.\r\n*  <b>`keep_checkpoint_every_n_hours`</b>: How often to keep checkpoints.\r\n    Defaults to 10,000 hours.\r\n*  <b>`name`</b>: string.  Optional name to use as a prefix when adding operations.\r\n*  <b>`restore_sequentially`</b>: A Bool, which if true, causes restore of different\r\n    variables to happen sequentially within each device.  This can lower\r\n    memory usage when restoring very large models.\r\n*  <b>`saver_def`</b>: Optional SaverDef proto to use instead of running the builder.\r\n    This is only useful for specialty code that wants to recreate a Saver\r\n    object for a previously built Graph that had a Saver.  The saver_def\r\n    proto should be the one returned by the as_saver_def() call of the\r\n    Saver that was created for that Graph.\r\n*  <b>`builder`</b>: Optional SaverBuilder to use if a saver_def was not provided.\r\n    Defaults to BaseSaverBuilder().\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `var_list` is invalid.\r\n*  <b>`ValueError`</b>: If any of the keys or values in `var_list` is not unique.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.Saver.save(sess, save_path, global_step=None, latest_filename=None)` <a class=\"md-anchor\" id=\"Saver.save\"></a>\r\n\r\nSaves variables.\r\n\r\nThis method runs the ops added by the constructor for saving variables.\r\nIt requires a session in which the graph was launched.  The variables to\r\nsave must also have been initialized.\r\n\r\nThe method returns the path of the newly created checkpoint file.  This\r\npath can be passed directly to a call to `restore()`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`sess`</b>: A Session to use to save the variables.\r\n*  <b>`save_path`</b>: string.  Path to the checkpoint filename.  If the saver is\r\n    `sharded`, this is the prefix of the sharded checkpoint filename.\r\n*  <b>`global_step`</b>: If provided the global step number is appended to\r\n    `save_path` to create the checkpoint filename. The optional argument\r\n    can be a Tensor, a Tensor name or an integer.\r\n*  <b>`latest_filename`</b>: Optional name for the protocol buffer file that will\r\n    contains the list of most recent checkpoint filenames.  That file,\r\n    kept in the same directory as the checkpoint files, is automatically\r\n    managed by the saver to keep track of recent checkpoints.  Defaults to\r\n    'checkpoint'.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A string: path at which the variables were saved.  If the saver is\r\n    sharded, this string ends with: '-?????-of-nnnnn' where 'nnnnn'\r\n    is the number of shards created.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `sess` is not a Session.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.Saver.restore(sess, save_path)` <a class=\"md-anchor\" id=\"Saver.restore\"></a>\r\n\r\nRestores previously saved variables.\r\n\r\nThis method runs the ops added by the constructor for restoring variables.\r\nIt requires a session in which the graph was launched.  The variables to\r\nrestore do not have to have been initialized, as restoring is itself a way\r\nto initialize variables.\r\n\r\nThe `save_path` argument is typically a value previously returned from a\r\n`save()` call, or a call to `latest_checkpoint()`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`sess`</b>: A Session to use to restore the parameters.\r\n*  <b>`save_path`</b>: Path where parameters were previously saved.\r\n\r\n\r\n\r\nOther utility methods.\r\n\r\n- - -\r\n\r\n#### `tf.train.Saver.last_checkpoints` <a class=\"md-anchor\" id=\"Saver.last_checkpoints\"></a>\r\n\r\nList of not-yet-deleted checkpoint filenames.\r\n\r\nYou can pass any of the returned values to `restore()`.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of checkpoint filenames, sorted from oldest to newest.\r\n\r\n- - -\r\n\r\n#### `tf.train.Saver.set_last_checkpoints(last_checkpoints)` <a class=\"md-anchor\" id=\"Saver.set_last_checkpoints\"></a>\r\n\r\nSets the list of not-yet-deleted checkpoint filenames.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`last_checkpoints`</b>: a list of checkpoint filenames.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`AssertionError`</b>: if the list of checkpoint filenames has already been set.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.Saver.as_saver_def()` <a class=\"md-anchor\" id=\"Saver.as_saver_def\"></a>\r\n\r\nGenerates a `SaverDef` representation of this saver.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `SaverDef` proto.\r\n\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.train.latest_checkpoint(checkpoint_dir, latest_filename=None)` <a class=\"md-anchor\" id=\"latest_checkpoint\"></a>\r\n\r\nFinds the filename of latest saved checkpoint file.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`checkpoint_dir`</b>: Directory where the variables were saved.\r\n*  <b>`latest_filename`</b>: Optional name for the protocol buffer file that\r\n    contains the list of most recent checkpoint filenames.\r\n    See the corresponding argument to `Saver.save()`.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The full path to the latest checkpoint or None if no checkpoint was found.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.train.get_checkpoint_state(checkpoint_dir, latest_filename=None)` <a class=\"md-anchor\" id=\"get_checkpoint_state\"></a>\r\n\r\nReturns CheckpointState proto from the \"checkpoint\" file.\r\n\r\nIf the \"checkpoint\" file contains a valid CheckpointState\r\nproto, returns it.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`checkpoint_dir`</b>: The directory of checkpoints.\r\n*  <b>`latest_filename`</b>: Optional name of the checkpoint file.  Default to\r\n    'checkpoint'.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A CheckpointState if the state was available, None\r\n  otherwise.\r\n\r\n\r\n- - -\r\n\r\n### `tf.train.update_checkpoint_state(save_dir, model_checkpoint_path, all_model_checkpoint_paths=None, latest_filename=None)` <a class=\"md-anchor\" id=\"update_checkpoint_state\"></a>\r\n\r\nUpdates the content of the 'checkpoint' file.\r\n\r\nThis updates the checkpoint file containing a CheckpointState\r\nproto.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`save_dir`</b>: Directory where the model was saved.\r\n*  <b>`model_checkpoint_path`</b>: The checkpoint file.\r\n*  <b>`all_model_checkpoint_paths`</b>: list of strings.  Paths to all not-yet-deleted\r\n    checkpoints, sorted from oldest to newest.  If this is a non-empty list,\r\n    the last element must be equal to model_checkpoint_path.  These paths\r\n    are also saved in the CheckpointState proto.\r\n*  <b>`latest_filename`</b>: Optional name of the checkpoint file.  Default to\r\n    'checkpoint'.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`RuntimeError`</b>: If the save paths conflict.\r\n\r\n\r\n\r\n## Sharing Variables <a class=\"md-anchor\" id=\"AUTOGENERATED-sharing-variables\"></a>\r\n\r\nTensorFlow provides several classes and operations that you can use to\r\ncreate variables contingent on certain conditions.\r\n\r\n- - -\r\n\r\n### `tf.get_variable(name, shape=None, dtype=tf.float32, initializer=None, trainable=True, collections=None)` <a class=\"md-anchor\" id=\"get_variable\"></a>\r\n\r\nGets an existing variable with these parameters or create a new one.\r\n\r\nThis function prefixes the name with the current variable scope\r\nand performs reuse checks. See the\r\n[Variable Scope How To](../../how_tos/variable_scope/index.md)\r\nfor an extensive description of how reusing works. Here is a basic example:\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    v = get_variable(\"v\", [1])  # v.name == \"foo/v:0\"\r\n    w = get_variable(\"w\", [1])  # w.name == \"foo/w:0\"\r\nwith tf.variable_scope(\"foo\", reuse=True)\r\n    v1 = get_variable(\"v\")  # The same as v above.\r\n```\r\n\r\nIf initializer is `None` (the default), the default initializer passed in\r\nthe constructor is used. If that one is `None` too, a\r\n`UniformUnitScalingInitializer` will be used.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name`</b>: the name of the new or existing variable.\r\n*  <b>`shape`</b>: shape of the new or existing variable.\r\n*  <b>`dtype`</b>: type of the new or existing variable (defaults to `DT_FLOAT`).\r\n*  <b>`initializer`</b>: initializer for the variable if one is created.\r\n*  <b>`trainable`</b>: If `True` also add the variable to the graph collection\r\n    `GraphKeys.TRAINABLE_VARIABLES` (see variables.Variable).\r\n*  <b>`collections`</b>: List of graph collections keys to add the Variable to.\r\n    Defaults to `[GraphKeys.VARIABLES]` (see variables.Variable).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The created or existing variable.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: when creating a new variable and shape is not declared,\r\n    or when violating reuse during variable creation. Reuse is set inside\r\n    `variable_scope`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.get_variable_scope()` <a class=\"md-anchor\" id=\"get_variable_scope\"></a>\r\n\r\nReturns the current variable scope.\r\n\r\n\r\n- - -\r\n\r\n### `tf.variable_scope(name_or_scope, reuse=None, initializer=None)` <a class=\"md-anchor\" id=\"variable_scope\"></a>\r\n\r\nReturns a context for variable scope.\r\n\r\nVariable scope allows to create new variables and to share already created\r\nones while providing checks to not create or share by accident. For details,\r\nsee the [Variable Scope How To](../../how_tos/variable_scope/index.md),\r\nhere we present only a few basic examples.\r\n\r\nSimple example of how to create a new variable:\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    with tf.variable_scope(\"bar\"):\r\n        v = tf.get_variable(\"v\", [1])\r\n        assert v.name == \"foo/bar/v:0\"\r\n```\r\n\r\nBasic example of sharing a variable:\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    v = get_variable(\"v\", [1])\r\nwith tf.variable_scope(\"foo\", reuse=True):\r\n    v1 = tf.get_variable(\"v\", [1])\r\nassert v1 == v\r\n```\r\n\r\nSharing a variable by capturing a scope and setting reuse:\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\") as scope.\r\n    v = get_variable(\"v\", [1])\r\n    scope.reuse_variables()\r\n    v1 = tf.get_variable(\"v\", [1])\r\nassert v1 == v\r\n```\r\n\r\nTo prevent accidental sharing of variables, we raise an exception when\r\ngetting an existing variable in a non-reusing scope.\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\") as scope.\r\n    v = get_variable(\"v\", [1])\r\n    v1 = tf.get_variable(\"v\", [1])\r\n    #  Raises ValueError(\"... v already exists ...\").\r\n```\r\n\r\nSimilarly, we raise an exception when trying to get a variable that\r\ndoes not exist in reuse mode.\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\", reuse=True):\r\n    v = get_variable(\"v\", [1])\r\n    #  Raises ValueError(\"... v does not exists ...\").\r\n```\r\n\r\nNote that the `reuse` flag is inherited: if we open a reusing scope,\r\nthen all its sub-scopes become reusing as well.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`name_or_scope`</b>: `string` or `VariableScope`: the scope to open.\r\n*  <b>`reuse`</b>: `True` or `None`; if `True`, we go into reuse mode for this scope as\r\n    well as all sub-scopes; if `None`, we just inherit the parent scope reuse.\r\n*  <b>`initializer`</b>: default initializer for variables within this scope.\r\n\r\n##### Yields: <a class=\"md-anchor\" id=\"AUTOGENERATED-yields-\"></a>\r\n\r\n  A scope that can be to captured and reused.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: when trying to reuse within a create scope, or create within\r\n    a reuse scope, or if reuse is not `None` or `True`.\r\n*  <b>`TypeError`</b>: when the types of some arguments are not appropriate.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.constant_initializer(value=0.0)` <a class=\"md-anchor\" id=\"constant_initializer\"></a>\r\n\r\nReturns an initializer that generates Tensors with a single value.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`value`</b>: A Python scalar. All elements of the initialized variable\r\n    will be set to this value.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An initializer that generates Tensors with a single value.\r\n\r\n\r\n- - -\r\n\r\n### `tf.random_normal_initializer(mean=0.0, stddev=1.0, seed=None)` <a class=\"md-anchor\" id=\"random_normal_initializer\"></a>\r\n\r\nReturns an initializer that generates Tensors with a normal distribution.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`mean`</b>: a python scalar or a scalar tensor. Mean of the random values\r\n    to generate.\r\n*  <b>`stddev`</b>: a python scalar or a scalar tensor. Standard deviation of the\r\n    random values to generate.\r\n*  <b>`seed`</b>: A Python integer. Used to create random seeds. See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An initializer that generates Tensors with a normal distribution.\r\n\r\n\r\n- - -\r\n\r\n### `tf.truncated_normal_initializer(mean=0.0, stddev=1.0, seed=None)` <a class=\"md-anchor\" id=\"truncated_normal_initializer\"></a>\r\n\r\nReturns an initializer that generates a truncated normal distribution.\r\n\r\nThese values are similar to values from a random_normal_initializer\r\nexcept that values more than two standard deviations from the mean\r\nare discarded and re-drawn. This is the recommended initializer for\r\nneural network weights and filters.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`mean`</b>: a python scalar or a scalar tensor. Mean of the random values\r\n    to generate.\r\n*  <b>`stddev`</b>: a python scalar or a scalar tensor. Standard deviation of the\r\n    random values to generate.\r\n*  <b>`seed`</b>: A Python integer. Used to create random seeds. See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An initializer that generates Tensors with a truncated normal\r\n  distribution.\r\n\r\n\r\n- - -\r\n\r\n### `tf.random_uniform_initializer(minval=0.0, maxval=1.0, seed=None)` <a class=\"md-anchor\" id=\"random_uniform_initializer\"></a>\r\n\r\nReturns an initializer that generates Tensors with a uniform distribution.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`minval`</b>: a python scalar or a scalar tensor. lower bound of the range\r\n    of random values to generate.\r\n*  <b>`maxval`</b>: a python scalar or a scalar tensor. upper bound of the range\r\n    of random values to generate.\r\n*  <b>`seed`</b>: A Python integer. Used to create random seeds. See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An initializer that generates Tensors with a uniform distribution.\r\n\r\n\r\n- - -\r\n\r\n### `tf.uniform_unit_scaling_initializer(factor=1.0, seed=None)` <a class=\"md-anchor\" id=\"uniform_unit_scaling_initializer\"></a>\r\n\r\nReturns an initializer that generates tensors without scaling variance.\r\n\r\nWhen initializing a deep network, it is in principle advantageous to keep\r\nthe scale of the input variance constant, so it does not explode or diminish\r\nby reaching the final layer. If the input is `x` and the operation `x * W`,\r\nand we want to initialize `W` uniformly at random, we need to pick `W` from\r\n\r\n    [-sqrt(3) / sqrt(dim), sqrt(3) / sqrt(dim)]\r\n\r\nto keep the scale intact, where `dim = W.shape[0]` (the size of the input).\r\nA similar calculation for convolutional networks gives an analogous result\r\nwith `dim` equal to the product of the first 3 dimensions.  When\r\nnonlinearities are present, we need to multiply this by a constant `factor`.\r\nSee <https://arxiv.org/pdf/1412.6558v3.pdf> for deeper motivation, experiments\r\nand the calculation of constants. In section 2.3 there, the constants were\r\nnumerically computed: for a linear layer it's 1.0, relu: ~1.43, tanh: ~1.15.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`factor`</b>: Float.  A multiplicative factor by which the values will be scaled.\r\n*  <b>`seed`</b>: A Python integer. Used to create random seeds. See\r\n    [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)\r\n    for behavior.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An initializer that generates tensors with unit variance.\r\n\r\n\r\n- - -\r\n\r\n### `tf.zeros_initializer(shape, dtype=tf.float32)` <a class=\"md-anchor\" id=\"zeros_initializer\"></a>\r\n\r\nAn adaptor for zeros() to match the Initializer spec.\r\n\r\n\r\n\r\n## Sparse Variable Updates <a class=\"md-anchor\" id=\"AUTOGENERATED-sparse-variable-updates\"></a>\r\n\r\nThe sparse update ops modify a subset of the entries in a dense `Variable`,\r\neither overwriting the entries or adding / subtracting a delta.  These are\r\nuseful for training embedding models and similar lookup-based networks, since\r\nonly a small subset of embedding vectors change in any given step.\r\n\r\nSince a sparse update of a large tensor may be generated automatically during\r\ngradient computation (as in the gradient of\r\n[`tf.gather`](../../api_docs/python/array_ops.md#gather)),\r\nan [`IndexedSlices`](#IndexedSlices) class is provided that encapsulates a set\r\nof sparse indices and values.  `IndexedSlices` objects are detected and handled\r\nautomatically by the optimizers in most cases.\r\n\r\n- - -\r\n\r\n### `tf.scatter_update(ref, indices, updates, use_locking=None, name=None)` <a class=\"md-anchor\" id=\"scatter_update\"></a>\r\n\r\nApplies sparse updates to a variable reference.\r\n\r\nThis operation computes\r\n\r\n    # Scalar indices\r\n    ref[indices, ...] = updates[...]\r\n\r\n    # Vector indices (for each i)\r\n    ref[indices[i], ...] = updates[i, ...]\r\n\r\n    # High rank indices (for each i, ..., j)\r\n    ref[indices[i, ..., j], ...] = updates[i, ..., j, ...]\r\n\r\nThis operation outputs `ref` after the update is done.\r\nThis makes it easier to chain operations that need to use the reset value.\r\n\r\nIf `indices` contains duplicate entries, lexicographically later entries\r\noverride earlier entries.\r\n\r\nRequires `updates.shape = indices.shape + ref.shape[1:]`.\r\n\r\n<div style=\"width:70%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/ScatterUpdate.png\" alt>\r\n</div>\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`ref`</b>: A mutable `Tensor`. Should be from a `Variable` node.\r\n*  <b>`indices`</b>: A `Tensor`. Must be one of the following types: `int32`, `int64`.\r\n    A tensor of indices into the first dimension of `ref`.\r\n*  <b>`updates`</b>: A `Tensor`. Must have the same type as `ref`.\r\n    A tensor of updated values to store in `ref`.\r\n*  <b>`use_locking`</b>: An optional `bool`. Defaults to `True`.\r\n    If True, the assignment will be protected by a lock;\r\n    otherwise the behavior is undefined, but may exhibit less contention.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  Same as `ref`.  Returned as a convenience for operations that want\r\n  to use the updated values after the update is done.\r\n\r\n\r\n- - -\r\n\r\n### `tf.scatter_add(ref, indices, updates, use_locking=None, name=None)` <a class=\"md-anchor\" id=\"scatter_add\"></a>\r\n\r\nAdds sparse updates to a variable reference.\r\n\r\nThis operation computes\r\n\r\n    # Scalar indices\r\n    ref[indices, ...] += updates[...]\r\n\r\n    # Vector indices (for each i)\r\n    ref[indices[i], ...] += updates[i, ...]\r\n\r\n    # High rank indices (for each i, ..., j)\r\n    ref[indices[i, ..., j], ...] += updates[i, ..., j, ...]\r\n\r\nThis operation outputs `ref` after the update is done.\r\nThis makes it easier to chain operations that need to use the reset value.\r\n\r\nDuplicate entries are handled correctly: if multiple `indices` reference\r\nthe same location, their contributions add.\r\n\r\nRequires `updates.shape = indices.shape + ref.shape[1:]`.\r\n\r\n<div style=\"width:70%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/ScatterAdd.png\" alt>\r\n</div>\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`ref`</b>: A mutable `Tensor`. Must be one of the following types: `float32`, `float64`, `int64`, `int32`, `uint8`, `int16`, `int8`, `complex64`, `qint8`, `quint8`, `qint32`.\r\n    Should be from a `Variable` node.\r\n*  <b>`indices`</b>: A `Tensor`. Must be one of the following types: `int32`, `int64`.\r\n    A tensor of indices into the first dimension of `ref`.\r\n*  <b>`updates`</b>: A `Tensor`. Must have the same type as `ref`.\r\n    A tensor of updated values to add to `ref`.\r\n*  <b>`use_locking`</b>: An optional `bool`. Defaults to `False`.\r\n    If True, the addition will be protected by a lock;\r\n    otherwise the behavior is undefined, but may exhibit less contention.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  Same as `ref`.  Returned as a convenience for operations that want\r\n  to use the updated values after the update is done.\r\n\r\n\r\n- - -\r\n\r\n### `tf.scatter_sub(ref, indices, updates, use_locking=None, name=None)` <a class=\"md-anchor\" id=\"scatter_sub\"></a>\r\n\r\nSubtracts sparse updates to a variable reference.\r\n\r\n    # Scalar indices\r\n    ref[indices, ...] -= updates[...]\r\n\r\n    # Vector indices (for each i)\r\n    ref[indices[i], ...] -= updates[i, ...]\r\n\r\n    # High rank indices (for each i, ..., j)\r\n    ref[indices[i, ..., j], ...] -= updates[i, ..., j, ...]\r\n\r\nThis operation outputs `ref` after the update is done.\r\nThis makes it easier to chain operations that need to use the reset value.\r\n\r\nDuplicate entries are handled correctly: if multiple `indices` reference\r\nthe same location, their (negated) contributions add.\r\n\r\nRequires `updates.shape = indices.shape + ref.shape[1:]`.\r\n\r\n<div style=\"width:70%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/ScatterSub.png\" alt>\r\n</div>\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`ref`</b>: A mutable `Tensor`. Must be one of the following types: `float32`, `float64`, `int64`, `int32`, `uint8`, `int16`, `int8`, `complex64`, `qint8`, `quint8`, `qint32`.\r\n    Should be from a `Variable` node.\r\n*  <b>`indices`</b>: A `Tensor`. Must be one of the following types: `int32`, `int64`.\r\n    A tensor of indices into the first dimension of `ref`.\r\n*  <b>`updates`</b>: A `Tensor`. Must have the same type as `ref`.\r\n    A tensor of updated values to subtract from `ref`.\r\n*  <b>`use_locking`</b>: An optional `bool`. Defaults to `False`.\r\n    If True, the subtraction will be protected by a lock;\r\n    otherwise the behavior is undefined, but may exhibit less contention.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  Same as `ref`.  Returned as a convenience for operations that want\r\n  to use the updated values after the update is done.\r\n\r\n\r\n- - -\r\n\r\n### `tf.sparse_mask(a, mask_indices, name=None)` <a class=\"md-anchor\" id=\"sparse_mask\"></a>\r\n\r\nMasks elements of `IndexedSlices`.\r\n\r\nGiven an `IndexedSlices` instance `a`, returns another `IndexedSlices` that\r\ncontains a subset of the slices of `a`. Only the slices at indices specified\r\nin `mask_indices` are returned.\r\n\r\nThis is useful when you need to extract a subset of slices in an\r\n`IndexedSlices` object.\r\n\r\nFor example:\r\n\r\n```python\r\n# `a` contains slices at indices [12, 26, 37, 45] from a large tensor\r\n# with shape [1000, 10]\r\na.indices => [12, 26, 37, 45]\r\ntf.shape(a.values) => [4, 10]\r\n\r\n# `b` will be the subset of `a` slices at its second and third indices, so\r\n# we want to mask of its first and last indices (which are at absolute\r\n# indices 12, 45)\r\nb = tf.sparse_mask(a, [12, 45])\r\n\r\nb.indices => [26, 37]\r\ntf.shape(b.values) => [2, 10]\r\n\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n  * `a`: An `IndexedSlices` instance.\r\n  * `mask_indices`: Indices of elements to mask.\r\n  * `name`: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The masked `IndexedSlices` instance.\r\n\r\n\r\n- - -\r\n\r\n### `class tf.IndexedSlices` <a class=\"md-anchor\" id=\"IndexedSlices\"></a>\r\n\r\nA sparse representation of a set of tensor slices at given indices.\r\n\r\nThis class is a simple wrapper for a pair of `Tensor` objects:\r\n\r\n* `values`: A `Tensor` of any dtype with shape `[D0, D1, ..., Dn]`.\r\n* `indices`: A 1-D integer `Tensor` with shape `[D0]`.\r\n\r\nAn `IndexedSlices` is typically used to represent a subset of a larger\r\ntensor `dense` of shape `[LARGE0, D1, .. , DN]` where `LARGE0 >> D0`.\r\nThe values in `indices` are the indices in the first dimension of\r\nthe slices that have been extracted from the larger tensor.\r\n\r\nThe dense tensor `dense` represented by an `IndexedSlices` `slices` has\r\n\r\n```python\r\ndense[slices.indices[i], :, :, :, ...] = slices.values[i, :, :, :, ...]\r\n```\r\n\r\nThe `IndexedSlices` class is used principally in the definition of\r\ngradients for operations that have sparse gradients\r\n(e.g. [`tf.gather`](../../api_docs/python/array_ops.md#gather)).\r\n\r\nContrast this representation with\r\n[`SparseTensor`](../../api_docs/python/sparse_ops.md#SparseTensor),\r\nwhich uses multi-dimensional indices and scalar values.\r\n\r\n- - -\r\n\r\n#### `tf.IndexedSlices.__init__(values, indices, dense_shape=None)` <a class=\"md-anchor\" id=\"IndexedSlices.__init__\"></a>\r\n\r\nCreates an `IndexedSlices`.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.IndexedSlices.values` <a class=\"md-anchor\" id=\"IndexedSlices.values\"></a>\r\n\r\nA `Tensor` containing the values of the slices.\r\n\r\n- - -\r\n\r\n#### `tf.IndexedSlices.indices` <a class=\"md-anchor\" id=\"IndexedSlices.indices\"></a>\r\n\r\nA 1-D `Tensor` containing the indices of the slices.\r\n\r\n- - -\r\n\r\n#### `tf.IndexedSlices.dense_shape` <a class=\"md-anchor\" id=\"IndexedSlices.dense_shape\"></a>\r\n\r\nA 1-D `Tensor` containing the shape of the corresponding dense tensor.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.IndexedSlices.name` <a class=\"md-anchor\" id=\"IndexedSlices.name\"></a>\r\n\r\nThe name of this `IndexedSlices`.\r\n\r\n- - -\r\n\r\n#### `tf.IndexedSlices.dtype` <a class=\"md-anchor\" id=\"IndexedSlices.dtype\"></a>\r\n\r\nThe `DType` of elements in this tensor.\r\n\r\n- - -\r\n\r\n#### `tf.IndexedSlices.device` <a class=\"md-anchor\" id=\"IndexedSlices.device\"></a>\r\n\r\nThe name of the device on which `values` will be produced, or `None`.\r\n\r\n- - -\r\n\r\n#### `tf.IndexedSlices.op` <a class=\"md-anchor\" id=\"IndexedSlices.op\"></a>\r\n\r\nThe `Operation` that produces `values` as an output.\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/api_docs/python/train.md",
    "content": "<!-- This file is machine generated: DO NOT EDIT! -->\r\n\r\n# Training <a class=\"md-anchor\" id=\"AUTOGENERATED-training\"></a>\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Training](#AUTOGENERATED-training)\r\n* [Optimizers](#AUTOGENERATED-optimizers)\r\n  * [`class tf.train.Optimizer`](#Optimizer)\r\n  * [Usage](#AUTOGENERATED-usage)\r\n  * [Processing gradients before applying them.](#AUTOGENERATED-processing-gradients-before-applying-them.)\r\n  * [Gating Gradients](#AUTOGENERATED-gating-gradients)\r\n  * [Slots](#AUTOGENERATED-slots)\r\n  * [`class tf.train.GradientDescentOptimizer`](#GradientDescentOptimizer)\r\n  * [`class tf.train.AdagradOptimizer`](#AdagradOptimizer)\r\n  * [`class tf.train.MomentumOptimizer`](#MomentumOptimizer)\r\n  * [`class tf.train.AdamOptimizer`](#AdamOptimizer)\r\n  * [`class tf.train.FtrlOptimizer`](#FtrlOptimizer)\r\n  * [`class tf.train.RMSPropOptimizer`](#RMSPropOptimizer)\r\n* [Gradient Computation](#AUTOGENERATED-gradient-computation)\r\n  * [`tf.gradients(ys, xs, grad_ys=None, name='gradients', colocate_gradients_with_ops=False, gate_gradients=False, aggregation_method=None)`](#gradients)\r\n  * [`class tf.AggregationMethod`](#AggregationMethod)\r\n  * [`tf.stop_gradient(input, name=None)`](#stop_gradient)\r\n* [Gradient Clipping](#AUTOGENERATED-gradient-clipping)\r\n  * [`tf.clip_by_value(t, clip_value_min, clip_value_max, name=None)`](#clip_by_value)\r\n  * [`tf.clip_by_norm(t, clip_norm, name=None)`](#clip_by_norm)\r\n  * [`tf.clip_by_average_norm(t, clip_norm, name=None)`](#clip_by_average_norm)\r\n  * [`tf.clip_by_global_norm(t_list, clip_norm, use_norm=None, name=None)`](#clip_by_global_norm)\r\n  * [`tf.global_norm(t_list, name=None)`](#global_norm)\r\n* [Decaying the learning rate](#AUTOGENERATED-decaying-the-learning-rate)\r\n  * [`tf.train.exponential_decay(learning_rate, global_step, decay_steps, decay_rate, staircase=False, name=None)`](#exponential_decay)\r\n* [Moving Averages](#AUTOGENERATED-moving-averages)\r\n  * [`class tf.train.ExponentialMovingAverage`](#ExponentialMovingAverage)\r\n* [Coordinator and QueueRunner](#AUTOGENERATED-coordinator-and-queuerunner)\r\n  * [`class tf.train.Coordinator`](#Coordinator)\r\n  * [`class tf.train.QueueRunner`](#QueueRunner)\r\n  * [`tf.train.add_queue_runner(qr, collection='queue_runners')`](#add_queue_runner)\r\n  * [`tf.train.start_queue_runners(sess=None, coord=None, daemon=True, start=True, collection='queue_runners')`](#start_queue_runners)\r\n* [Summary Operations](#AUTOGENERATED-summary-operations)\r\n  * [`tf.scalar_summary(tags, values, collections=None, name=None)`](#scalar_summary)\r\n  * [`tf.image_summary(tag, tensor, max_images=None, collections=None, name=None)`](#image_summary)\r\n  * [`tf.histogram_summary(tag, values, collections=None, name=None)`](#histogram_summary)\r\n  * [`tf.nn.zero_fraction(value, name=None)`](#zero_fraction)\r\n  * [`tf.merge_summary(inputs, collections=None, name=None)`](#merge_summary)\r\n  * [`tf.merge_all_summaries(key='summaries')`](#merge_all_summaries)\r\n* [Adding Summaries to Event Files](#AUTOGENERATED-adding-summaries-to-event-files)\r\n  * [`class tf.train.SummaryWriter`](#SummaryWriter)\r\n  * [`tf.train.summary_iterator(path)`](#summary_iterator)\r\n* [Training utilities](#AUTOGENERATED-training-utilities)\r\n  * [`tf.train.global_step(sess, global_step_tensor)`](#global_step)\r\n  * [`tf.train.write_graph(graph_def, logdir, name, as_text=True)`](#write_graph)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\nThis library provides a set of classes and functions that helps train models.\r\n\r\n## Optimizers <a class=\"md-anchor\" id=\"AUTOGENERATED-optimizers\"></a>\r\n\r\nThe Optimizer base class provides methods to compute gradients for a loss and\r\napply gradients to variables.  A collection of subclasses implement classic\r\noptimization algorithms such as GradientDescent and Adagrad.\r\n\r\nYou never instantiate the Optimizer class itself, but instead instantiate one\r\nof the subclasses.\r\n\r\n- - -\r\n\r\n### `class tf.train.Optimizer` <a class=\"md-anchor\" id=\"Optimizer\"></a>\r\n\r\nBase class for optimizers.\r\n\r\nThis class defines the API to add Ops to train a model.  You never use this\r\nclass directly, but instead instantiate one of its subclasses such as\r\n`GradientDescentOptimizer`, `AdagradOptimizer`, or `MomentumOptimizer`.\r\n\r\n### Usage <a class=\"md-anchor\" id=\"AUTOGENERATED-usage\"></a>\r\n\r\n```\r\n# Create an optimizer with the desired parameters.\r\nopt = GradientDescentOptimizer(learning_rate=0.1)\r\n# Add Ops to the graph to minimize a cost by updating a list of variables.\r\n# \"cost\" is a Tensor, and the list of variables contains variables.Variable\r\n# objects.\r\nopt_op = opt.minimize(cost, <list of variables>)\r\n```\r\n\r\nIn the training program you will just have to run the returned Op.\r\n\r\n```\r\n# Execute opt_op to do one step of training:\r\nopt_op.run()\r\n```\r\n\r\n### Processing gradients before applying them. <a class=\"md-anchor\" id=\"AUTOGENERATED-processing-gradients-before-applying-them.\"></a>\r\n\r\nCalling `minimize()` takes care of both computing the gradients and\r\napplying them to the variables.  If you want to process the gradients\r\nbefore applying them you can instead use the optimizer in three steps:\r\n\r\n1.  Compute the gradients with `compute_gradients()`.\r\n2.  Process the gradients as you wish.\r\n3.  Apply the processed gradients with `apply_gradients()`.\r\n\r\nExample:\r\n\r\n```\r\n# Create an optimizer.\r\nopt = GradientDescentOptimizer(learning_rate=0.1)\r\n\r\n# Compute the gradients for a list of variables.\r\ngrads_and_vars = opt.compute_gradients(loss, <list of variables>)\r\n\r\n# grads_and_vars is a list of tuples (gradient, variable).  Do whatever you\r\n# need to the 'gradient' part, for example cap them, etc.\r\ncapped_grads_and_vars = [(MyCapper(gv[0]), gv[1])) for gv in grads_and_vars]\r\n\r\n# Ask the optimizer to apply the capped gradients.\r\nopt.apply_gradients(capped_grads_and_vars)\r\n```\r\n\r\n- - -\r\n\r\n#### `tf.train.Optimizer.__init__(use_locking, name)` <a class=\"md-anchor\" id=\"Optimizer.__init__\"></a>\r\n\r\nCreate a new Optimizer.\r\n\r\nThis must be called by the constructors of subclasses.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`use_locking`</b>: Bool. If True apply use locks to prevent concurrent updates\r\n    to variables.\r\n*  <b>`name`</b>: A non-empty string.  The name to use for accumulators created\r\n    for the optimizer.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if name is malformed.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.Optimizer.minimize(loss, global_step=None, var_list=None, gate_gradients=1, name=None)` <a class=\"md-anchor\" id=\"Optimizer.minimize\"></a>\r\n\r\nAdd operations to minimize 'loss' by updating 'var_list'.\r\n\r\nThis method simply combines calls compute_gradients() and\r\napply_gradients(). If you want to process the gradient before applying them\r\ncall compute_gradients() and apply_gradients() explicitly instead of using\r\nthis function.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`loss`</b>: A Tensor containing the value to minimize.\r\n*  <b>`global_step`</b>: Optional Variable to increment by one after the\r\n    variables have been updated.\r\n*  <b>`var_list`</b>: Optional list of variables.Variable to update to minimize\r\n    'loss'.  Defaults to the list of variables collected in the graph\r\n    under the key GraphKeys.TRAINABLE_VARIABLES.\r\n*  <b>`gate_gradients`</b>: How to gate the computation of gradients.  Can be\r\n    GATE_NONE, GATE_OP, or  GATE_GRAPH.\r\n*  <b>`name`</b>: Optional name for the returned operation.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An Operation that updates the variables in 'var_list'.  If 'global_step'\r\n  was not None, that operation also increments global_step.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if some of the variables are not variables.Variable objects.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.Optimizer.compute_gradients(loss, var_list=None, gate_gradients=1)` <a class=\"md-anchor\" id=\"Optimizer.compute_gradients\"></a>\r\n\r\nCompute gradients of \"loss\" for the variables in \"var_list\".\r\n\r\nThis is the first part of minimize().  It returns a list\r\nof (gradient, variable) pairs where \"gradient\" is the gradient\r\nfor \"variable\".  Note that \"gradient\" can be a Tensor, a\r\nIndexedSlices, or None if there is no gradient for the\r\ngiven variable.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`loss`</b>: A Tensor containing the value to minimize.\r\n*  <b>`var_list`</b>: Optional list of variables.Variable to update to minimize\r\n    \"loss\".  Defaults to the list of variables collected in the graph\r\n    under the key GraphKey.TRAINABLE_VARIABLES.\r\n*  <b>`gate_gradients`</b>: How to gate the computation of gradients.  Can be\r\n    GATE_NONE, GATE_OP, or  GATE_GRAPH.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of (gradient, variable) pairs.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If var_list contains anything else than variables.Variable.\r\n*  <b>`ValueError`</b>: If some arguments are invalid.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.Optimizer.apply_gradients(grads_and_vars, global_step=None, name=None)` <a class=\"md-anchor\" id=\"Optimizer.apply_gradients\"></a>\r\n\r\nApply gradients to variables.\r\n\r\nThis is the second part of minimize(). It returns an Operation that\r\napplies gradients.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`grads_and_vars`</b>: List of (gradient, variable) pairs as returned by\r\n    compute_gradients().\r\n*  <b>`global_step`</b>: Optional Variable to increment by one after the\r\n    variables have been updated.\r\n*  <b>`name`</b>: Optional name for the returned operation.  Default to the\r\n    name passed to the Optimizer constructor.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An Operation that applies the specified gradients. If 'global_step'\r\n  was not None, that operation also increments global_step.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: if grads_and_vars is malformed.\r\n\r\n\r\n\r\n### Gating Gradients <a class=\"md-anchor\" id=\"AUTOGENERATED-gating-gradients\"></a>\r\n\r\nBoth `minimize()` and `compute_gradients()` accept a `gate_gradient` argument\r\nthat controls the degree of parallelism during the application of the\r\ngradients.\r\n\r\nThe possible values are: `GATE_NONE`, `GATE_OP`, and `GATE_GRAPH`.\r\n\r\n<b>GATE_NONE</b>: Compute and apply gradients in parallel.  This provides the\r\nmaximum parallelism in execution, at the cost of some non-reproducibility in\r\nthe results.  For example the two gradients of MatMul depend on the input\r\nvalues: With `GATE_NONE` one of the gradients could be applied to one of the\r\ninputs _before_ the other gradient is computed resulting in non-reproducible\r\nresults.\r\n\r\n<b>GATE_OP</b>: For each Op, make sure all gradients are computed before they\r\nare used.  This prevents race conditions for Ops that generate gradients for\r\nmultiple inputs where the gradients depend on the inputs.\r\n\r\n<b>GATE_GRAPH</b>: Make sure all gradients for all variables are computed\r\nbefore any one of them is used.  This provides the least parallelism but can\r\nbe useful if you want to process all gradients before applying any of them.\r\n\r\n### Slots <a class=\"md-anchor\" id=\"AUTOGENERATED-slots\"></a>\r\n\r\nSome optimizer subclasses, such as `MomentumOptimizer` and `AdagradOptimizer`\r\nallocate and manage additional variables associated with the variables to\r\ntrain.  These are called <i>Slots</i>.  Slots have names and you can ask the\r\noptimizer for the names of the slots that it uses.  Once you have a slot name\r\nyou can ask the optimizer for the variable it created to hold the slot value.\r\n\r\nThis can be useful if you want to log debug a training algorithm, report stats\r\nabout the slots, etc.\r\n\r\n- - -\r\n\r\n#### `tf.train.Optimizer.get_slot_names()` <a class=\"md-anchor\" id=\"Optimizer.get_slot_names\"></a>\r\n\r\nReturn a list of the names of slots created by the Optimizer.\r\n\r\nSee get_slot().\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of strings.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.Optimizer.get_slot(var, name)` <a class=\"md-anchor\" id=\"Optimizer.get_slot\"></a>\r\n\r\nReturn a slot named \"name\" created for \"var\" by the Optimizer.\r\n\r\nSome Optimizer subclasses use additional variables.  For example\r\nMomentum and Adagrad use variables to accumulate updates.  This method\r\ngives access to these Variables if for some reason you need them.\r\n\r\nUse get_slot_names() to get the list of slot names created by the Optimizer.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`var`</b>: A variable passed to minimize() or apply_gradients().\r\n*  <b>`name`</b>: A string.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The Variable for the slot if it was created, None otherwise.\r\n\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.train.GradientDescentOptimizer` <a class=\"md-anchor\" id=\"GradientDescentOptimizer\"></a>\r\n\r\nOptimizer that implements the gradient descent algorithm.\r\n\r\n- - -\r\n\r\n#### `tf.train.GradientDescentOptimizer.__init__(learning_rate, use_locking=False, name='GradientDescent')` <a class=\"md-anchor\" id=\"GradientDescentOptimizer.__init__\"></a>\r\n\r\nConstruct a new gradient descent optimizer.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`learning_rate`</b>: A Tensor or a floating point value.  The learning\r\n    rate to use.\r\n*  <b>`use_locking`</b>: If True use locks for update operation.s\r\n*  <b>`name`</b>: Optional name prefix for the operations created when applying\r\n    gradients. Defaults to \"GradientDescent\".\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.train.AdagradOptimizer` <a class=\"md-anchor\" id=\"AdagradOptimizer\"></a>\r\n\r\nOptimizer that implements the Adagrad algorithm.\r\n\r\n- - -\r\n\r\n#### `tf.train.AdagradOptimizer.__init__(learning_rate, initial_accumulator_value=0.1, use_locking=False, name='Adagrad')` <a class=\"md-anchor\" id=\"AdagradOptimizer.__init__\"></a>\r\n\r\nConstruct a new Adagrad optimizer.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`learning_rate`</b>: A `Tensor` or a floating point value.  The learning rate.\r\n*  <b>`initial_accumulator_value`</b>: A floating point value.\r\n    Starting value for the accumulators, must be positive.\r\n*  <b>`use_locking`</b>: If `True` use locks for update operations.\r\n*  <b>`name`</b>: Optional name prefix for the operations created when applying\r\n    gradients.  Defaults to \"Adagrad\".\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: If the initial_accumulator_value is invalid.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.train.MomentumOptimizer` <a class=\"md-anchor\" id=\"MomentumOptimizer\"></a>\r\n\r\nOptimizer that implements the Momentum algorithm.\r\n\r\n- - -\r\n\r\n#### `tf.train.MomentumOptimizer.__init__(learning_rate, momentum, use_locking=False, name='Momentum')` <a class=\"md-anchor\" id=\"MomentumOptimizer.__init__\"></a>\r\n\r\nConstruct a new Momentum optimizer.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`learning_rate`</b>: A `Tensor` or a floating point value.  The learning rate.\r\n*  <b>`momentum`</b>: A `Tensor` or a floating point value.  The momentum.\r\n*  <b>`use_locking`</b>: If `True` use locks for update operations.\r\n*  <b>`name`</b>: Optional name prefix for the operations created when applying\r\n    gradients.  Defaults to \"Momentum\".\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.train.AdamOptimizer` <a class=\"md-anchor\" id=\"AdamOptimizer\"></a>\r\n\r\nOptimizer that implements the Adam algorithm.\r\n\r\n- - -\r\n\r\n#### `tf.train.AdamOptimizer.__init__(learning_rate=0.001, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False, name='Adam')` <a class=\"md-anchor\" id=\"AdamOptimizer.__init__\"></a>\r\n\r\nConstruct a new Adam optimizer.\r\n\r\nImplementation is based on: http://arxiv.org/pdf/1412.6980v7.pdf\r\n\r\nInitialization:\r\n\r\n```\r\nm_0 <- 0 (Initialize initial 1st moment vector)\r\nv_0 <- 0 (Initialize initial 2nd moment vector)\r\nt <- 0 (Initialize timestep)\r\n```\r\n\r\nThe update rule for `variable` with gradient `g` uses an optimization\r\ndescribed at the end of section2 of the paper:\r\n\r\n```\r\nt <- t + 1\r\nlr_t <- learning_rate * sqrt(1 - beta2^t) / (1 - beta1^t)\r\n\r\nm_t <- beta1 * m_{t-1} + (1 - beta1) * g\r\nv_t <- beta2 * v_{t-1} + (1 - beta2) * g * g\r\nvariable <- variable - lr_t * m_t / (sqrt(v_t) + epsilon)\r\n```\r\n\r\nThe default value of 1e-8 for epsilon might not be a good default in\r\ngeneral. For example, when training an Inception network on ImageNet a\r\ncurrent good choice is 1.0 or 0.1.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`learning_rate`</b>: A Tensor or a floating point value.  The learning rate.\r\n*  <b>`beta1`</b>: A float value or a constant float tensor.\r\n    The exponential decay rate for the 1st moment estimates.\r\n*  <b>`beta2`</b>: A float value or a constant float tensor.\r\n    The exponential decay rate for the 2st moment estimates.\r\n*  <b>`epsilon`</b>: A small constant for numerical stability.\r\n*  <b>`use_locking`</b>: If True use locks for update operation.s\r\n*  <b>`name`</b>: Optional name for the operations created when applying gradients.\r\n    Defaults to \"Adam\".\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.train.FtrlOptimizer` <a class=\"md-anchor\" id=\"FtrlOptimizer\"></a>\r\n\r\nOptimizer that implements the FTRL algorithm.\r\n\r\n- - -\r\n\r\n#### `tf.train.FtrlOptimizer.__init__(learning_rate, learning_rate_power=-0.5, initial_accumulator_value=0.1, l1_regularization_strength=0.0, l2_regularization_strength=0.0, use_locking=False, name='Ftrl')` <a class=\"md-anchor\" id=\"FtrlOptimizer.__init__\"></a>\r\n\r\nConstruct a new FTRL optimizer.\r\n\r\nThe Ftrl-proximal algorithm, abbreviated for Follow-the-regularized-leader,\r\nis described in the paper [Ad Click Prediction: a View from the Trenches](\r\nhttps://www.eecs.tufts.edu/~dsculley/papers/ad-click-prediction.pdf).\r\n\r\nIt can give a good performance vs. sparsity tradeoff.\r\n\r\nFtrl-proximal uses its own global base learning rate and can behave like\r\nAdagrad with `learning_rate_power=-0.5`, or like gradient descent with\r\n`learning_rate_power=0.0`.\r\n\r\nThe effective learning rate is adjusted per parameter, relative to this\r\nbase learning rate as:\r\n\r\n```\r\neffective_learning_rate_i = (learning_rate /\r\n    pow(k + summed_squared_gradients_for_i, learning_rate_power));\r\n```\r\n\r\nwhere k is the small constant `initial_accumulator_value`.\r\n\r\nNote that the real regularization coefficient of `|w|^2` for objective\r\nfunction is `1 / lambda_2` if specifying `l2 = lambda_2` as argument when\r\nusing this function.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`learning_rate`</b>: A float value or a constant float `Tensor`.\r\n*  <b>`learning_rate_power`</b>: A float value, must be less or equal to zero.\r\n*  <b>`initial_accumulator_value`</b>: The starting value for accumulators.\r\n    Only positive values are allowed.\r\n*  <b>`l1_regularization_strength`</b>: A float value, must be greater than or\r\n    equal to zero.\r\n*  <b>`l2_regularization_strength`</b>: A float value, must be greater than or\r\n    equal to zero.\r\n*  <b>`use_locking`</b>: If `True` use locks for update operations.\r\n*  <b>`name`</b>: Optional name prefix for the operations created when applying\r\n    gradients.  Defaults to \"Ftrl\".\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`ValueError`</b>: if one of the arguments is invalid.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.train.RMSPropOptimizer` <a class=\"md-anchor\" id=\"RMSPropOptimizer\"></a>\r\n\r\nOptimizer that implements the RMSProp algorithm.\r\n\r\n- - -\r\n\r\n#### `tf.train.RMSPropOptimizer.__init__(learning_rate, decay, momentum=0.0, epsilon=1e-10, use_locking=False, name='RMSProp')` <a class=\"md-anchor\" id=\"RMSPropOptimizer.__init__\"></a>\r\n\r\nConstruct a new RMSProp optimizer.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`learning_rate`</b>: A Tensor or a floating point value.  The learning rate.\r\n*  <b>`decay`</b>: discounting factor for the history/coming gradient\r\n*  <b>`momentum`</b>: a scalar tensor.\r\n*  <b>`epsilon`</b>: small value to avoid zero denominator.\r\n*  <b>`use_locking`</b>: If True use locks for update operation.\r\n*  <b>`name`</b>: Optional name prefic for the operations created when applying\r\n    gradients. Defaults to \"RMSProp\".\r\n\r\n\r\n\r\n\r\n## Gradient Computation <a class=\"md-anchor\" id=\"AUTOGENERATED-gradient-computation\"></a>\r\n\r\nTensorFlow provides functions to compute the derivatives for a given\r\nTensorFlow computation graph, adding operations to the graph. The\r\noptimizer classes automatically compute derivatives on your graph, but\r\ncreators of new Optimizers or expert users can call the lower-level\r\nfunctions below.\r\n\r\n- - -\r\n\r\n### `tf.gradients(ys, xs, grad_ys=None, name='gradients', colocate_gradients_with_ops=False, gate_gradients=False, aggregation_method=None)` <a class=\"md-anchor\" id=\"gradients\"></a>\r\n\r\nConstructs symbolic partial derivatives of `ys` w.r.t. x in `xs`.\r\n\r\n`ys` and `xs` are each a `Tensor` or a list of tensors.  `grad_ys`\r\nis a list of `Tensor`, holding the gradients received by the\r\n`ys`. The list must be the same length as `ys`.\r\n\r\n`gradients()` adds ops to the graph to output the partial\r\nderivatives of `ys` with respect to `xs`.  It returns a list of\r\n`Tensor` of length `len(xs)` where each tensor is the `sum(dy/dx)`\r\nfor y in `ys`.\r\n\r\n`grad_ys` is a list of tensors of the same length as `ys` that holds\r\nthe initial gradients for each y in `ys`.  When `grad_ys` is None,\r\nwe fill in a tensor of '1's of the shape of y for each y in `ys`.  A\r\nuser can provide their own initial 'grad_ys` to compute the\r\nderivatives using a different initial gradient for each y (e.g., if\r\none wanted to weight the gradient differently for each value in\r\neach y).\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`ys`</b>: A `Tensor` or list of tensors to be differentiated.\r\n*  <b>`xs`</b>: A `Tensor` or list of tensors to be used for differentiation.\r\n*  <b>`grad_ys`</b>: Optional. A `Tensor` or list of tensors the same size as\r\n    `ys` and holding the gradients computed for each y in `ys`.\r\n*  <b>`name`</b>: Optional name to use for grouping all the gradient ops together.\r\n    defaults to 'gradients'.\r\n*  <b>`colocate_gradients_with_ops`</b>: If True, try colocating gradients with\r\n    the corresponding op.\r\n*  <b>`gate_gradients`</b>: If True, add a tuple around the gradients returned\r\n    for an operations.  This avoids some race conditions.\r\n*  <b>`aggregation_method`</b>: Specifies the method used to combine gradient terms.\r\n    Accepted values are constants defined in the class `AggregationMethod`.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of `sum(dy/dx)` for each x in `xs`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`LookupError`</b>: if one of the operations between `x` and `y` does not\r\n    have a registered gradient function.\r\n*  <b>`ValueError`</b>: if the arguments are invalid.\r\n\r\n\r\n- - -\r\n\r\n### `class tf.AggregationMethod` <a class=\"md-anchor\" id=\"AggregationMethod\"></a>\r\n\r\nA class listing aggregation methods used to combine gradients.\r\n\r\nComputing partial derivatives can require aggregating gradient\r\ncontributions. This class lists the various methods that can\r\nbe used to combine gradients in the graph:\r\n\r\n*  `ADD_N`: All of the gradient terms are summed as part of one\r\n   operation using the \"AddN\" op. It has the property that all\r\n   gradients must be ready before any aggregation is performed.\r\n*  `DEFAULT`: The system-chosen default aggregation method.\r\n\r\n\r\n- - -\r\n\r\n### `tf.stop_gradient(input, name=None)` <a class=\"md-anchor\" id=\"stop_gradient\"></a>\r\n\r\nStops gradient computation.\r\n\r\nWhen executed in a graph, this op outputs its input tensor as-is.\r\n\r\nWhen building ops to compute gradients, this op prevents the contribution of\r\nits inputs to be taken into account.  Normally, the gradient generator adds ops\r\nto a graph to compute the derivatives of a specified 'loss' by recursively\r\nfinding out inputs that contributed to its computation.  If you insert this op\r\nin the graph it inputs are masked from the gradient generator.  They are not\r\ntaken into account for computing gradients.\r\n\r\nThis is useful any time you want to compute a value with TensorFlow but need\r\nto pretend that the value was a constant. Some examples include:\r\n\r\n*  The *EM* algorithm where the *M-step* should not involve backpropagation\r\n   through the output of the *E-step*.\r\n*  Contrastive divergence training of Boltzmann machines where, when\r\n   differentiating the energy function, the training must not backpropagate\r\n   through the graph that generated the samples from the model.\r\n*  Adversarial training, where no backprop should happen through the adversarial\r\n   example generation process.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`input`</b>: A `Tensor`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Tensor`. Has the same type as `input`.\r\n\r\n\r\n\r\n\r\n## Gradient Clipping <a class=\"md-anchor\" id=\"AUTOGENERATED-gradient-clipping\"></a>\r\n\r\nTensorFlow provides several operations that you can use to add clipping\r\nfunctions to your graph. You can use these functions to perform general data\r\nclipping, but they're particularly useful for handling exploding or vanishing\r\ngradients.\r\n\r\n- - -\r\n\r\n### `tf.clip_by_value(t, clip_value_min, clip_value_max, name=None)` <a class=\"md-anchor\" id=\"clip_by_value\"></a>\r\n\r\nClips tensor values to a specified min and max.\r\n\r\nGiven a tensor `t`, this operation returns a tensor of the same type and\r\nshape as `t` with its values clipped to `clip_value_min` and `clip_value_max`.\r\nAny values less than `clip_value_min` are set to `clip_value_min`. Any values\r\ngreater than `clip_value_max` are set to `clip_value_max`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`t`</b>: A `Tensor`.\r\n*  <b>`clip_value_min`</b>: A 0-D (scalar) `Tensor`. The minimum value to clip by.\r\n*  <b>`clip_value_max`</b>: A 0-D (scalar) `Tensor`. The maximum value to clip by.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A clipped `Tensor`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.clip_by_norm(t, clip_norm, name=None)` <a class=\"md-anchor\" id=\"clip_by_norm\"></a>\r\n\r\nClips tensor values to a maximum L2-norm.\r\n\r\nGiven a tensor `t`, and a maximum clip value `clip_norm`, this operation\r\nnormalizes `t` so that its L2-norm is less than or equal to `clip_norm'.\r\nSpecifically, if the L2-norm is already less than or equal to `clip_norm`,\r\nthen `t` is not modified. If the L2-norm is greater than `clip_norm`, then\r\nthis operation returns a tensor of the same type and shape as `t` with its\r\nvalues set to:\r\n\r\n`t * clip_norm / l2norm(t)`\r\n\r\nIn this case, the L2-norm of the output tensor is `clip_norm`.\r\n\r\nThis operation is typically used to clip gradients before applying them with\r\nan optimizer.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`t`</b>: A `Tensor`.\r\n*  <b>`clip_norm`</b>: A 0-D (scalar) `Tensor` > 0. A maximum clipping value.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A clipped `Tensor`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.clip_by_average_norm(t, clip_norm, name=None)` <a class=\"md-anchor\" id=\"clip_by_average_norm\"></a>\r\n\r\nClips tensor values to a maximum average L2-norm.\r\n\r\nGiven a tensor `t`, and a maximum clip value `clip_norm`, this operation\r\nnormalizes `t` so that its average L2-norm is less than or equal to\r\n`clip_norm'. Specifically, if the average L2-norm is already less than or\r\nequal to `clip_norm`, then `t` is not modified. If the average L2-norm is\r\ngreater than `clip_norm`, then this operation returns a tensor of the same\r\ntype and shape as `t` with its values set to:\r\n\r\n`t * clip_norm / l2norm_avg(t)`\r\n\r\nIn this case, the average L2-norm of the output tensor is `clip_norm`.\r\n\r\nThis operation is typically used to clip gradients before applying them with\r\nan optimizer.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`t`</b>: A `Tensor`.\r\n*  <b>`clip_norm`</b>: A 0-D (scalar) `Tensor` > 0. A maximum clipping value.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A clipped `Tensor`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.clip_by_global_norm(t_list, clip_norm, use_norm=None, name=None)` <a class=\"md-anchor\" id=\"clip_by_global_norm\"></a>\r\n\r\nClips values of multiple tensors by the ratio of the sum of their norms.\r\n\r\nGiven a tuple or list of tensors `t_list`, and a clipping ratio `clip_norm`,\r\nthis operation returns a list of clipped tensors `list_clipped`\r\nand the global norm (`global_norm`) of all tensors in `t_list`. Optionally,\r\nif you've already computed the global norm for `t_list`, you can specify\r\nthe global norm with `use_norm`.\r\n\r\nTo perform the clipping, the values t_list[i] are set to:\r\n\r\n`t_list[i] * clip_norm / max(global_norm, clip_norm)`\r\n\r\nwhere:\r\n\r\n`global_norm = sqrt(sum([l2norm(t)**2 for t in t_list]))`\r\n\r\nIf `clip_norm > global_norm` then the entries in `t_list` remain as they are,\r\notherwise they're all shrunk by the global ratio.\r\n\r\nAny of the entries of `t_list` that are of type None are ignored.\r\n\r\nThis is the correct way to perform gradient clipping (for example, see\r\nR. Pascanu, T. Mikolov, and Y. Bengio, \"On the difficulty of training\r\nRecurrent Neural Networks\".  http://arxiv.org/abs/1211.5063)\r\n\r\nHowever, it is slower than `clip_by_norm()` because all the parameters must be\r\nready before the clipping operation can be performed.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`t_list`</b>: A tuple or list of mixed `Tensors`, `IndexedSlices`, or None.\r\n*  <b>`clip_norm`</b>: A 0-D (scalar) `Tensor` > 0. The clipping ratio.\r\n*  <b>`use_norm`</b>: A 0-D (scalar) `Tensor` of type `float` (optional). The global\r\n    norm to use. If not provided, `global_norm()` is used to compute the norm.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n\r\n*  <b>`list_clipped`</b>: A list of `Tensors` of the same type as `list_t`.\r\n*  <b>`global_norm`</b>: A 0-D (scalar) `Tensor` representing the global norm.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `t_list` is not a sequence.\r\n\r\n\r\n- - -\r\n\r\n### `tf.global_norm(t_list, name=None)` <a class=\"md-anchor\" id=\"global_norm\"></a>\r\n\r\nComputes the global norm of multiple tensors.\r\n\r\nGiven a tuple or list of tensors `t_list`, this operation returns the\r\nglobal norm of the elements in all tensors in `t_list`. The global norm is\r\ncomputed as:\r\n\r\n`global_norm = sqrt(sum([l2norm(t)**2 for t in t_list]))`\r\n\r\nAny entries in `t_list` that are of type None are ignored.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`t_list`</b>: A tuple or list of mixed `Tensors`, `IndexedSlices`, or None.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A 0-D (scalar) `Tensor` of type `float`.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If `t_list` is not a sequence.\r\n\r\n\r\n\r\n## Decaying the learning rate <a class=\"md-anchor\" id=\"AUTOGENERATED-decaying-the-learning-rate\"></a>\r\n- - -\r\n\r\n### `tf.train.exponential_decay(learning_rate, global_step, decay_steps, decay_rate, staircase=False, name=None)` <a class=\"md-anchor\" id=\"exponential_decay\"></a>\r\n\r\nApplies exponential decay to the learning rate.\r\n\r\nWhen training a model, it is often recommended to lower the learning rate as\r\nthe training progresses.  This function applies an exponential decay function\r\nto a provided initial learning rate.  It requires a `global_step` value to\r\ncompute the decayed learning rate.  You can just pass a TensorFlow variable\r\nthat you increment at each training step.\r\n\r\nThe function returns the decayed learning rate.  It is computed as:\r\n\r\n```python\r\ndecayed_learning_rate = learning_rate *\r\n                        decay_rate ^ (global_step / decay_steps)\r\n```\r\n\r\nIf the argument `staircase` is `True`, then `global_step /decay_steps` is an\r\ninteger division and the decayed learning rate follows a staircase function.\r\n\r\nExample: decay every 100000 steps with a base of 0.96:\r\n\r\n```python\r\n...\r\nglobal_step = tf.Variable(0, trainable=False)\r\nstarter_learning_rate = 0.1\r\nlearning_rate = tf.exponential_decay(starter_learning_rate, global_step,\r\n                                     100000, 0.96, staircase=True)\r\noptimizer = tf.GradientDescent(learning_rate)\r\n# Passing global_step to minimize() will increment it at each step.\r\noptimizer.minimize(...my loss..., global_step=global_step)\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`learning_rate`</b>: A scalar `float32` or `float64` `Tensor` or a\r\n    Python number.  The initial learning rate.\r\n*  <b>`global_step`</b>: A scalar `int32` or `int64` `Tensor` or a Python number.\r\n    Global step to use for the decay computation.  Must not be negative.\r\n*  <b>`decay_steps`</b>: A scalar `int32` or `int64` `Tensor` or a Python number.\r\n    Must be positive.  See the decay computation above.\r\n*  <b>`decay_rate`</b>: A scalar `float32` or `float64` `Tensor` or a\r\n    Python number.  The decay rate.\r\n*  <b>`staircase`</b>: Boolean.  It `True` decay the learning rate at discrete intervals.\r\n*  <b>`name`</b>: string.  Optional name of the operation.  Defaults to 'ExponentialDecay'\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A scalar `Tensor` of the same type as `learning_rate`.  The decayed\r\n  learning rate.\r\n\r\n\r\n\r\n## Moving Averages <a class=\"md-anchor\" id=\"AUTOGENERATED-moving-averages\"></a>\r\n\r\nSome training algorithms, such as GradientDescent and Momentum often benefit\r\nfrom maintaining a moving average of variables during optimization.  Using the\r\nmoving averages for evaluations often improve results significantly.\r\n\r\n- - -\r\n\r\n### `class tf.train.ExponentialMovingAverage` <a class=\"md-anchor\" id=\"ExponentialMovingAverage\"></a>\r\n\r\nMaintains moving averages of variables by employing and exponential decay.\r\n\r\nWhen training a model, it is often beneficial to maintain moving averages of\r\nthe trained parameters.  Evaluations that use averaged parameters sometimes\r\nproduce significantly better results than the final trained values.\r\n\r\nThe `apply()` method adds shadow copies of trained variables and add ops that\r\nmaintain a moving average of the trained variables in their shadow copies.\r\nIt is used when building the training model.  The ops that maintain moving\r\naverages are typically run after each training step.\r\nThe `average()` and `average_name()` methods give access to the shadow\r\nvariables and their names.  They are useful when building an evaluation\r\nmodel, or when restoring a model from a checkpoint file.  They help use the\r\nmoving averages in place of the last trained values for evaluations.\r\n\r\nThe moving averages are computed using exponential decay.  You specify the\r\ndecay value when creating the `ExponentialMovingAverage` object.  The shadow\r\nvariables are initialized with the same initial values as the trained\r\nvariables.  When you run the ops to maintain the moving averages, each\r\nshadow variable is updated with the formula:\r\n\r\n  `shadow_variable -= (1 - decay) * (shadow_variable - variable)`\r\n\r\nThis is mathematically equivalent to the classic formula below, but the use\r\nof an `assign_sub` op (the `\"-=\"` in the formula) allows concurrent lockless\r\nupdates to the variables:\r\n\r\n  `shadow_variable = decay * shadow_variable + (1 - decay) * variable`\r\n\r\nReasonable values for `decay` are close to 1.0, typically in the\r\nmultiple-nines range: 0.999, 0.9999, etc.\r\n\r\nExample usage when creating a training model:\r\n\r\n```python\r\n# Create variables.\r\nvar0 = tf.Variable(...)\r\nvar1 = tf.Variable(...)\r\n# ... use the variables to build a training model...\r\n...\r\n# Create an op that applies the optimizer.  This is what we usually\r\n# would use as a training op.\r\nopt_op = opt.minimize(my_loss, [var0, var1])\r\n\r\n# Create an ExponentialMovingAverage object\r\nema = tf.train.ExponentialMovingAverage(decay=0.9999)\r\n\r\n# Create the shadow variables, and add ops to maintain moving averages\r\n# of var0 and var1.\r\nmaintain_averages_op = ema.apply([var0, var1])\r\n\r\n# Create an op that will update the moving averages after each training\r\n# step.  This is what we will use in place of the usuall trainig op.\r\nwith tf.control_dependencies([opt_op]):\r\n    training_op = tf.group(maintain_averages_op)\r\n\r\n...train the model by running training_op...\r\n```\r\n\r\nThere are two ways to use the moving averages for evaluations:\r\n\r\n*  Build a model that uses the shadow variables instead of the variables.\r\n   For this, use the `average()` method which returns the shadow variable\r\n   for a given variable.\r\n*  Build a model normally but load the checkpoint files to evaluate by using\r\n   the shadow variable names.  For this use the `average_name()` method.  See\r\n   the [Saver class](../../api_docs/python/train.md#Saver) for more\r\n   information on restoring saved variables.\r\n\r\nExample of restoring the shadow variable values:\r\n\r\n```python\r\n# Create a Saver that loads variables from their saved shadow values.\r\nshadow_var0_name = ema.average_name(var0)\r\nshadow_var1_name = ema.average_name(var1)\r\nsaver = tf.train.Saver({shadow_var0_name: var0, shadow_var1_name: var1})\r\nsaver.restore(...checkpoint filename...)\r\n# var0 and var1 now hold the moving average values\r\n```\r\n\r\n- - -\r\n\r\n#### `tf.train.ExponentialMovingAverage.__init__(decay, num_updates=None, name='ExponentialMovingAverage')` <a class=\"md-anchor\" id=\"ExponentialMovingAverage.__init__\"></a>\r\n\r\nCreates a new ExponentialMovingAverage object.\r\n\r\nThe `Apply()` method has to be called to create shadow variables and add\r\nops to maintain moving averages.\r\n\r\nThe optional `num_updates` parameter allows one to tweak the decay rate\r\ndynamically. .  It is typical to pass the count of training steps, usually\r\nkept in a variable that is incremented at each step, in which case the\r\ndecay rate is lower at the start of training.  This makes moving averages\r\nmove faster.  If passed, the actual decay rate used is:\r\n\r\n  `min(decay, (1 + num_updates) / (10 + num_updates))`\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`decay`</b>: Float.  The decay to use.\r\n*  <b>`num_updates`</b>: Optional count of number of updates applied to variables.\r\n*  <b>`name`</b>: String. Optional prefix name to use for the name of ops added in\r\n    `Apply()`.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.ExponentialMovingAverage.apply(var_list=None)` <a class=\"md-anchor\" id=\"ExponentialMovingAverage.apply\"></a>\r\n\r\nMaintains moving averages of variables.\r\n\r\n`var_list` must be a list of `Variable` or `Tensor` objects.  This method\r\ncreates shadow variables for all elements of `var_list`.  Shadow variables\r\nfor `Variable` objects are initialized to the variable's initial value.\r\nFor `Tensor` objects, the shadow variables are initialized to 0.\r\n\r\nshadow variables are created with `trainable=False` and added to the\r\n`GraphKeys.ALL_VARIABLES` collection.  They will be returned by calls to\r\n`tf.all_variables()`.\r\n\r\nReturns an op that updates all shadow variables as described above.\r\n\r\nNote that `apply()` can be called multiple times with different lists of\r\nvariables.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`var_list`</b>: A list of Variable or Tensor objects. The variables\r\n    and Tensors must be of types float32 or float64.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  An Operation that updates the moving averages.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`TypeError`</b>: If the arguments are not all float32 or float64.\r\n*  <b>`ValueError`</b>: If the moving average of one of the variables is already\r\n    being computed.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.ExponentialMovingAverage.average_name(var)` <a class=\"md-anchor\" id=\"ExponentialMovingAverage.average_name\"></a>\r\n\r\nReturns the name of the `Variable` holding the average for `var`.\r\n\r\nThe typical scenario for `ExponentialMovingAverage` is to compute moving\r\naverages of variables during training, and restore the variables from the\r\ncomputed moving averages during evaluations.\r\n\r\nTo restore variables, you have to know the name of the shadow variables.\r\nThat name and the original variable can then be passed to a `Saver()` object\r\nto restore the variable from the moving average value with:\r\n  `saver = tf.train.Saver({ema.average_name(var): var})`\r\n\r\n`average_name()` can be called whether or not `apply()` has been called.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`var`</b>: A `Variable` object.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A string: the name of the variable that will be used or was used\r\n  by the `ExponentialMovingAverage class` to hold the moving average of\r\n  `var`.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.ExponentialMovingAverage.average(var)` <a class=\"md-anchor\" id=\"ExponentialMovingAverage.average\"></a>\r\n\r\nReturns the `Variable` holding the average of `var`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`var`</b>: A `Variable` object.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A `Variable` object or `None` if the moving average of `var`\r\n  is not maintained..\r\n\r\n\r\n\r\n\r\n## Coordinator and QueueRunner <a class=\"md-anchor\" id=\"AUTOGENERATED-coordinator-and-queuerunner\"></a>\r\n\r\nSee [Threading and Queues](../../how_tos/threading_and_queues/index.md)\r\nfor how to use threads and queues.  For documentation on the Queue API,\r\nsee [Queues](../../api_docs/python/io_ops.md#queues).\r\n\r\n- - -\r\n\r\n### `class tf.train.Coordinator` <a class=\"md-anchor\" id=\"Coordinator\"></a>\r\n\r\nA coordinator for threads.\r\n\r\nThis class implements a simple mechanism to coordinate the termination of a\r\nset of threads.\r\n\r\n#### Usage: <a class=\"md-anchor\" id=\"AUTOGENERATED-usage-\"></a>\r\n\r\n```python\r\n# Create a coordinator.\r\ncoord = Coordinator()\r\n# Start a number of threads, passing the coordinator to each of them.\r\n...start thread 1...(coord, ...)\r\n...start thread N...(coord, ...)\r\n# Wait for all the threads to terminate.\r\ncoord.join(threads)\r\n```\r\n\r\nAny of the threads can call `coord.request_stop()` to ask for all the threads\r\nto stop.  To cooperate with the requests, each thread must check for\r\n`coord.should_stop()` on a regular basis.  `coord.should_stop()` returns\r\n`True` as soon as `coord.request_stop()` has been called.\r\n\r\nA typical thread running with a Coordinator will do something like:\r\n\r\n```python\r\nwhile not coord.should_stop():\r\n   ...do some work...\r\n```\r\n\r\n#### Exception handling: <a class=\"md-anchor\" id=\"AUTOGENERATED-exception-handling-\"></a>\r\n\r\nA thread can report an exception to the Coordinator as part of the\r\n`should_stop()` call.  The exception will be re-raised from the\r\n`coord.join()` call.\r\n\r\nThread code:\r\n\r\n```python\r\ntry:\r\n  while not coord.should_stop():\r\n    ...do some work...\r\nexcept Exception, e:\r\n  coord.request_stop(e)\r\n```\r\n\r\nMain code:\r\n\r\n```python\r\ntry:\r\n  ...\r\n  coord = Coordinator()\r\n  # Start a number of threads, passing the coordinator to each of them.\r\n  ...start thread 1...(coord, ...)\r\n  ...start thread N...(coord, ...)\r\n  # Wait for all the threads to terminate.\r\n  coord.join(threads)\r\nexcept Exception, e:\r\n  ...exception that was passed to coord.request_stop()\r\n```\r\n\r\n#### Grace period for stopping: <a class=\"md-anchor\" id=\"AUTOGENERATED-grace-period-for-stopping-\"></a>\r\n\r\nAfter a thread has called `coord.request_stop()` the other threads have a\r\nfixed time to stop, this is called the 'stop grace period' and defaults to 2\r\nminutes.  If any of the threads is still alive after the grace period expires\r\n`coord.join()` raises a RuntimeException reporting the laggards.\r\n\r\n```\r\ntry:\r\n  ...\r\n  coord = Coordinator()\r\n  # Start a number of threads, passing the coordinator to each of them.\r\n  ...start thread 1...(coord, ...)\r\n  ...start thread N...(coord, ...)\r\n  # Wait for all the threads to terminate, give them 10s grace period\r\n  coord.join(threads, stop_grace_period_secs=10)\r\nexcept RuntimeException:\r\n  ...one of the threads took more than 10s to stop after request_stop()\r\n  ...was called.\r\nexcept Exception:\r\n  ...exception that was passed to coord.request_stop()\r\n```\r\n- - -\r\n\r\n#### `tf.train.Coordinator.__init__()` <a class=\"md-anchor\" id=\"Coordinator.__init__\"></a>\r\n\r\nCreate a new Coordinator.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.Coordinator.join(threads, stop_grace_period_secs=120)` <a class=\"md-anchor\" id=\"Coordinator.join\"></a>\r\n\r\nWait for threads to terminate.\r\n\r\nBlocks until all 'threads' have terminated or request_stop() is called.\r\n\r\nAfter the threads stop, if an 'exc_info' was passed to request_stop, that\r\nexception is re-reaised.\r\n\r\nGrace period handling: When request_stop() is called, threads are given\r\n'stop_grace_period_secs' seconds to terminate.  If any of them is still\r\nalive after that period expires, a RuntimeError is raised.  Note that if\r\nan 'exc_info' was passed to request_stop() then it is raised instead of\r\nthat RuntimeError.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`threads`</b>: List threading.Threads. The started threads to join.\r\n*  <b>`stop_grace_period_secs`</b>: Number of seconds given to threads to stop after\r\n    request_stop() has been called.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`RuntimeError`</b>: If any thread is still alive after request_stop()\r\n    is called and the grace period expires.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.Coordinator.request_stop(ex=None)` <a class=\"md-anchor\" id=\"Coordinator.request_stop\"></a>\r\n\r\nRequest that the threads stop.\r\n\r\nAfter this is called, calls to should_stop() will return True.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`ex`</b>: Optional Exception, or Python 'exc_info' tuple as returned by\r\n    sys.exc_info().  If this is the first call to request_stop() the\r\n    corresponding exception is recorded and re-raised from join().\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.Coordinator.should_stop()` <a class=\"md-anchor\" id=\"Coordinator.should_stop\"></a>\r\n\r\nCheck if stop was requested.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  True if a stop was requested.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.Coordinator.wait_for_stop(timeout=None)` <a class=\"md-anchor\" id=\"Coordinator.wait_for_stop\"></a>\r\n\r\nWait till the Coordinator is told to stop.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`timeout`</b>: float.  Sleep for up to that many seconds waiting for\r\n    should_stop() to become True.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  True if the Coordinator is told stop, False if the timeout expired.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `class tf.train.QueueRunner` <a class=\"md-anchor\" id=\"QueueRunner\"></a>\r\n\r\nHolds a list of enqueue operations for a queue, each to be run in a thread.\r\n\r\nQueues are a convenient TensorFlow mechanism to compute tensors\r\nasynchronously using multiple threads. For example in the canonical 'Input\r\nReader' setup one set of threads generates filenames in a queue; a second set\r\nof threads read records from the files, processes them, and enqueues tensors\r\non a second queue; a third set of threads dequeues these input records to\r\nconstruct batches and runs them through training operations.\r\n\r\nThere are several delicate issues when running multiple threads that way:\r\nclosing the queues in sequence as the input is exhausted, correctly catching\r\nand reporting exceptions, etc.\r\n\r\nThe `QueueRunner`, combined with the `Coordinator`, helps handle these issues.\r\n- - -\r\n\r\n#### `tf.train.QueueRunner.__init__(queue, enqueue_ops)` <a class=\"md-anchor\" id=\"QueueRunner.__init__\"></a>\r\n\r\nCreate a QueueRunner.\r\n\r\nOn construction the `QueueRunner` adds an op to close the queue.  That op\r\nwill be run if the enqueue ops raise exceptions.\r\n\r\nWhen you later call the `create_threads()` method, the `QueueRunner` will\r\ncreate one thread for each op in `enqueue_ops`.  Each thread will run its\r\nenqueue op in parallel with the other threads.  The enqueue ops do not have\r\nto all be the same op, but it is expected that they all enqueue tensors in\r\n`queue`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`queue`</b>: A `Queue`.\r\n*  <b>`enqueue_ops`</b>: List of enqueue ops to run in threads later.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.QueueRunner.create_threads(sess, coord=None, daemon=False, start=False)` <a class=\"md-anchor\" id=\"QueueRunner.create_threads\"></a>\r\n\r\nCreate threads to run the enqueue ops.\r\n\r\nThis method requires a session in which the graph was launched.  It creates\r\na list of threads, optionally starting them.  There is one thread for each\r\nop passed in `enqueue_ops`.\r\n\r\nThe `coord` argument is an optional coordinator, that the threads will use\r\nto terminate together and report exceptions.  If a coordinator is given,\r\nthis method starts an additional thread to close the queue when the\r\ncoordinator requests a stop.\r\n\r\nThis method may be called again as long as all threads from a previous call\r\nhave stopped.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`sess`</b>: A `Session`.\r\n*  <b>`coord`</b>: Optional `Coordinator` object for reporting errors and checking\r\n    stop conditions.\r\n*  <b>`daemon`</b>: Boolean.  If `True` make the threads daemon threads.\r\n*  <b>`start`</b>: Boolean.  If `True` starts the threads.  If `False` the\r\n    caller must call the `start()` method of the returned threads.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of threads.\r\n\r\n##### Raises: <a class=\"md-anchor\" id=\"AUTOGENERATED-raises-\"></a>\r\n\r\n\r\n*  <b>`RuntimeError`</b>: If threads from a previous call to `create_threads()` are\r\n  still running.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.QueueRunner.exceptions_raised` <a class=\"md-anchor\" id=\"QueueRunner.exceptions_raised\"></a>\r\n\r\nExceptions raised but not handled by the `QueueRunner` threads.\r\n\r\nExceptions raised in queue runner threads are handled in one of two ways\r\ndepending on whether or not a `Coordinator` was passed to\r\n`create_threads()`:\r\n\r\n* With a `Coordinator`, exceptions are reported to the coordinator and\r\n  forgotten by the `QueueRunner`.\r\n* Without a `Coordinator`, exceptions are captured by the `QueueRunner` and\r\n  made available in this `exceptions_raised` property.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of Python `Exception` objects.  The list is empty if no exception\r\n  was captured.  (No exceptions are captured when using a Coordinator.)\r\n\r\n\r\n- - -\r\n\r\n### `tf.train.add_queue_runner(qr, collection='queue_runners')` <a class=\"md-anchor\" id=\"add_queue_runner\"></a>\r\n\r\nAdds a `QueueRunner` to a collection in the graph.\r\n\r\nWhen building a complex model that uses many queues it is often difficult to\r\ngather all the queue runners that need to be run.  This convenience function\r\nallows you to add a queue runner to a well known collection in the graph.\r\n\r\nThe companion method `start_queue_runners()` can be used to start threads for\r\nall the collected queue runners.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`qr`</b>: A `QueueRunner`.\r\n*  <b>`collection`</b>: A `GraphKey` specifying the graph collection to add\r\n    the queue runner to.  Defaults to `GraphKeys.QUEUE_RUNNERS`.\r\n\r\n\r\n- - -\r\n\r\n### `tf.train.start_queue_runners(sess=None, coord=None, daemon=True, start=True, collection='queue_runners')` <a class=\"md-anchor\" id=\"start_queue_runners\"></a>\r\n\r\nStarts all queue runners collected in the graph.\r\n\r\nThis is a companion method to `add_queue_runner()`.  It just starts\r\nthreads for all queue runners collected in the graph.  It returns\r\nthe list of all threads.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`sess`</b>: `Session` used to run the queue ops.  Defaults to the\r\n    default session.\r\n*  <b>`coord`</b>: Optional `Coordinator` for coordinating the started threads.\r\n*  <b>`daemon`</b>: Whether the threads should be marked as `daemons`, meaning\r\n    they don't block program exit.\r\n*  <b>`start`</b>: Set to `False` to only create the threads, not start them.\r\n*  <b>`collection`</b>: A `GraphKey` specifying the graph collection to\r\n    get the queue runners from.  Defaults to `GraphKeys.QUEUE_RUNNERS`.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A list of threads.\r\n\r\n\r\n\r\n## Summary Operations <a class=\"md-anchor\" id=\"AUTOGENERATED-summary-operations\"></a>\r\n\r\nThe following ops output\r\n[`Summary`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/summary.proto)\r\nprotocol buffers as serialized string tensors.\r\n\r\nYou can fetch the output of a summary op in a session, and pass it to\r\na [SummaryWriter](../../api_docs/python/train.md#SummaryWriter) to append it\r\nto an event file.  Event files contain\r\n[`Event`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/util/event.proto)\r\nprotos that can contain `Summary` protos along with the timestamp and\r\nstep.  You can then use TensorBoard to visualize the contents of the\r\nevent files.  See [TensorBoard and\r\nSummaries](../../how_tos/summaries_and_tensorboard/index.md) for more\r\ndetails.\r\n\r\n- - -\r\n\r\n### `tf.scalar_summary(tags, values, collections=None, name=None)` <a class=\"md-anchor\" id=\"scalar_summary\"></a>\r\n\r\nOutputs a `Summary` protocol buffer with scalar values.\r\n\r\nThe input `tags` and `values` must have the same shape.  The generated\r\nsummary has a summary value for each tag-value pair in `tags` and `values`.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tags`</b>: A 1-D `string` `Tensor`.  Tags for the summaries.\r\n*  <b>`values`</b>: A 1-D `float32` or `float64` Tensor.  Values for the summaries.\r\n*  <b>`collections`</b>: Optional list of graph collections keys. The new summary op is\r\n    added to these collections. Defaults to `[GraphKeys.SUMMARIES]`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A scalar `Tensor` of type `string`. The serialized `Summary` protocol\r\n  buffer.\r\n\r\n\r\n- - -\r\n\r\n### `tf.image_summary(tag, tensor, max_images=None, collections=None, name=None)` <a class=\"md-anchor\" id=\"image_summary\"></a>\r\n\r\nOutputs a `Summary` protocol buffer with images.\r\n\r\nThe summary has up to `max_images` summary values containing images. The\r\nimages are built from `tensor` which must be 4-D with shape `[batch_size,\r\nheight, width, channels]` and where `channels` can be:\r\n\r\n*  1: `tensor` is interpreted as Grayscale.\r\n*  3: `tensor` is interpreted as RGB.\r\n*  4: `tensor` is interpreted as RGBA.\r\n\r\nThe images have the same number of channels as the input tensor. Their values\r\nare normalized, one image at a time, to fit in the range `[0, 255]`.  The\r\nop uses two different normalization algorithms:\r\n\r\n*  If the input values are all positive, they are rescaled so the largest one\r\n   is 255.\r\n\r\n*  If any input value is negative, the values are shifted so input value 0.0\r\n   is at 127.  They are then rescaled so that either the smallest value is 0,\r\n   or the largest one is 255.\r\n\r\nThe `tag` argument is a scalar `Tensor` of type `string`.  It is used to\r\nbuild the `tag` of the summary values:\r\n\r\n*  If `max_images` is 1, the summary value tag is '*tag*/image'.\r\n*  If `max_images` is greater than 1, the summary value tags are\r\n   generated sequentially as '*tag*/image/0', '*tag*/image/1', etc.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tag`</b>: A scalar `Tensor` of type `string`. Used to build the `tag`\r\n    of the summary values.\r\n*  <b>`tensor`</b>: A 4-D `float32` `Tensor` of shape `[batch_size, height, width,\r\n   channels]` where `channels` is 1, 3, or 4.\r\n*  <b>`max_images`</b>: Max number of batch elements to generate images for.\r\n*  <b>`collections`</b>: Optional list of ops.GraphKeys.  The collections to add the\r\n    summary to.  Defaults to [ops.GraphKeys.SUMMARIES]\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A scalar `Tensor` of type `string`. The serialized `Summary` protocol\r\n  buffer.\r\n\r\n\r\n- - -\r\n\r\n### `tf.histogram_summary(tag, values, collections=None, name=None)` <a class=\"md-anchor\" id=\"histogram_summary\"></a>\r\n\r\nOutputs a `Summary` protocol buffer with a histogram.\r\n\r\nThe generated\r\n[`Summary`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/summary.proto)\r\nhas one summary value containing a histogram for `values`.\r\n\r\nThis op reports an `OutOfRange` error if any value is not finite.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`tag`</b>: A `string` `Tensor`. 0-D.  Tag to use for the summary value.\r\n*  <b>`values`</b>: A `float32` `Tensor`. Any shape. Values to use to build the\r\n    histogram.\r\n*  <b>`collections`</b>: Optional list of graph collections keys. The new summary op is\r\n    added to these collections. Defaults to `[GraphKeys.SUMMARIES]`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A scalar `Tensor` of type `string`. The serialized `Summary` protocol\r\n  buffer.\r\n\r\n\r\n- - -\r\n\r\n### `tf.nn.zero_fraction(value, name=None)` <a class=\"md-anchor\" id=\"zero_fraction\"></a>\r\n\r\nReturns the fraction of zeros in `value`.\r\n\r\nIf `value` is empty, the result is `nan`.\r\n\r\nThis is useful in summaries to measure and report sparsity.  For example,\r\n\r\n    z = tf.Relu(...)\r\n    summ = tf.scalar_summary('sparsity', tf.zero_fraction(z))\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`value`</b>: A tensor of numeric type.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The fraction of zeros in `value`, with type `float32`.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.merge_summary(inputs, collections=None, name=None)` <a class=\"md-anchor\" id=\"merge_summary\"></a>\r\n\r\nMerges summaries.\r\n\r\nThis op creates a\r\n[`Summary`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/summary.proto)\r\nprotocol buffer that contains the union of all the values in the input\r\nsummaries.\r\n\r\nWhen the Op is run, it reports an `InvalidArgument` error if multiple values\r\nin the summaries to merge use the same tag.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`inputs`</b>: A list of `string` `Tensor` objects containing serialized `Summary`\r\n    protocol buffers.\r\n*  <b>`collections`</b>: Optional list of graph collections keys. The new summary op is\r\n    added to these collections. Defaults to `[GraphKeys.SUMMARIES]`.\r\n*  <b>`name`</b>: A name for the operation (optional).\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  A scalar `Tensor` of type `string`. The serialized `Summary` protocol\r\n  buffer resulting from the merging.\r\n\r\n\r\n- - -\r\n\r\n### `tf.merge_all_summaries(key='summaries')` <a class=\"md-anchor\" id=\"merge_all_summaries\"></a>\r\n\r\nMerges all summaries collected in the default graph.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`key`</b>: `GraphKey` used to collect the summaries.  Defaults to\r\n    `GraphKeys.SUMMARIES`.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  If no summaries were collected, returns None.  Otherwise returns a scalar\r\n  `Tensor` of type`string` containing the serialized `Summary` protocol\r\n  buffer resulting from the merging.\r\n\r\n\r\n\r\n## Adding Summaries to Event Files <a class=\"md-anchor\" id=\"AUTOGENERATED-adding-summaries-to-event-files\"></a>\r\n\r\nSee [Summaries and\r\nTensorBoard](../../how_tos/summaries_and_tensorboard/index.md) for an\r\noverview of summaries, event files, and visualization in TensorBoard.\r\n\r\n- - -\r\n\r\n### `class tf.train.SummaryWriter` <a class=\"md-anchor\" id=\"SummaryWriter\"></a>\r\n\r\nWrites `Summary` protocol buffers to event files.\r\n\r\nThe `SummaryWriter` class provides a mechanism to create an event file in a\r\ngiven directory and add summaries and events to it. The class updates the\r\nfile contents asynchronously. This allows a training program to call methods\r\nto add data to the file directly from the training loop, without slowing down\r\ntraining.\r\n\r\n- - -\r\n\r\n#### `tf.train.SummaryWriter.__init__(logdir, graph_def=None, max_queue=10, flush_secs=120)` <a class=\"md-anchor\" id=\"SummaryWriter.__init__\"></a>\r\n\r\nCreates a `SummaryWriter` and an event file.\r\n\r\nOn construction the summary writer creates a new event file in `logdir`.\r\nThis event file will contain `Event` protocol buffers constructed when you\r\ncall one of the following functions: `add_summary()`, `add_event()`, or\r\n`add_graph()`.\r\n\r\nIf you pass a `graph_def` protocol buffer to the constructor it is added to\r\nthe event file. (This is equivalent to calling `add_graph()` later).\r\n\r\nTensorBoard will pick the graph from the file and display it graphically so\r\nyou can interactively explore the graph you built. You will usually pass\r\nthe graph from the session in which you launched it:\r\n\r\n```python\r\n...create a graph...\r\n# Launch the graph in a session.\r\nsess = tf.Session()\r\n# Create a summary writer, add the 'graph_def' to the event file.\r\nwriter = tf.train.SummaryWriter(<some-directory>, sess.graph_def)\r\n```\r\n\r\nThe other arguments to the constructor control the asynchronous writes to\r\nthe event file:\r\n\r\n*  `flush_secs`: How often, in seconds, to flush the added summaries\r\n   and events to disk.\r\n*  `max_queue`: Maximum number of summaries or events pending to be\r\n   written to disk before one of the 'add' calls block.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`logdir`</b>: A string. Directory where event file will be written.\r\n*  <b>`graph_def`</b>: A `GraphDef` protocol buffer.\r\n*  <b>`max_queue`</b>: Integer. Size of the queue for pending events and summaries.\r\n*  <b>`flush_secs`</b>: Number. How often, in seconds, to flush the\r\n    pending events and summaries to disk.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.SummaryWriter.add_summary(summary, global_step=None)` <a class=\"md-anchor\" id=\"SummaryWriter.add_summary\"></a>\r\n\r\nAdds a `Summary` protocol buffer to the event file.\r\n\r\nThis method wraps the provided summary in an `Event` procotol buffer\r\nand adds it to the event file.\r\n\r\nYou can pass the output of any summary op, as-is, to this function. You\r\ncan also pass a `Summary` procotol buffer that you manufacture with your\r\nown data. This is commonly done to report evaluation results in event\r\nfiles.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`summary`</b>: A `Summary` protocol buffer, optionally serialized as a string.\r\n*  <b>`global_step`</b>: Number. Optional global step value to record with the\r\n    summary.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.SummaryWriter.add_event(event)` <a class=\"md-anchor\" id=\"SummaryWriter.add_event\"></a>\r\n\r\nAdds an event to the event file.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`event`</b>: An `Event` protocol buffer.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.SummaryWriter.add_graph(graph_def, global_step=None)` <a class=\"md-anchor\" id=\"SummaryWriter.add_graph\"></a>\r\n\r\nAdds a `GraphDef` protocol buffer to the event file.\r\n\r\nThe graph described by the protocol buffer will be displayed by\r\nTensorBoard. Most users pass a graph in the constructor instead.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`graph_def`</b>: A `GraphDef` protocol buffer.\r\n*  <b>`global_step`</b>: Number. Optional global step counter to record with the\r\n    graph.\r\n\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.SummaryWriter.flush()` <a class=\"md-anchor\" id=\"SummaryWriter.flush\"></a>\r\n\r\nFlushes the event file to disk.\r\n\r\nCall this method to make sure that all pending events have been written to\r\ndisk.\r\n\r\n\r\n- - -\r\n\r\n#### `tf.train.SummaryWriter.close()` <a class=\"md-anchor\" id=\"SummaryWriter.close\"></a>\r\n\r\nFlushes the event file to disk and close the file.\r\n\r\nCall this method when you do not need the summary writer anymore.\r\n\r\n\r\n\r\n- - -\r\n\r\n### `tf.train.summary_iterator(path)` <a class=\"md-anchor\" id=\"summary_iterator\"></a>\r\n\r\nAn iterator for reading `Event` protocol buffers from an event file.\r\n\r\nYou can use this function to read events written to an event file. It returns\r\na Python iterator that yields `Event` protocol buffers.\r\n\r\nExample: Print the contents of an events file.\r\n\r\n```python\r\nfor e in tf.summary_iterator(path to events file):\r\n    print e\r\n```\r\n\r\nExample: Print selected summary values.\r\n\r\n```python\r\n# This example supposes that the events file contains summaries with a\r\n# summary value tag 'loss'.  These could have been added by calling\r\n# `add_summary()`, passing the output of a scalar summary op created with\r\n# with: `tf.scalar_summary(['loss'], loss_tensor)`.\r\nfor e in tf.summary_iterator(path to events file):\r\n    for v in e.summary.value:\r\n        if v.tag == 'loss':\r\n            print v.simple_value\r\n```\r\n\r\nSee the protocol buffer definitions of\r\n[Event](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/util/event.proto)\r\nand\r\n[Summary](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/summary.proto)\r\nfor more information about their attributes.\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`path`</b>: The path to an event file created by a `SummaryWriter`.\r\n\r\n##### Yields: <a class=\"md-anchor\" id=\"AUTOGENERATED-yields-\"></a>\r\n\r\n  `Event` protocol buffers.\r\n\r\n\r\n\r\n## Training utilities <a class=\"md-anchor\" id=\"AUTOGENERATED-training-utilities\"></a>\r\n\r\n- - -\r\n\r\n### `tf.train.global_step(sess, global_step_tensor)` <a class=\"md-anchor\" id=\"global_step\"></a>\r\n\r\nSmall helper to get the global step.\r\n\r\n```python\r\n# Creates a variable to hold the global_step.\r\nglobal_step_tensor = tf.Variable(10, trainable=False, name='global_step')\r\n# Creates a session.\r\nsess = tf.Session()\r\n# Initializes the variable.\r\nsess.run(global_step_tensor.initializer)\r\nprint 'global_step:', tf.train.global_step(sess, global_step_tensor)\r\n\r\nglobal_step: 10\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`sess`</b>: A brain `Session` object.\r\n*  <b>`global_step_tensor`</b>: `Tensor` or the `name` of the operation that contains\r\n    the global step.\r\n\r\n##### Returns: <a class=\"md-anchor\" id=\"AUTOGENERATED-returns-\"></a>\r\n\r\n  The global step value.\r\n\r\n\r\n- - -\r\n\r\n### `tf.train.write_graph(graph_def, logdir, name, as_text=True)` <a class=\"md-anchor\" id=\"write_graph\"></a>\r\n\r\nWrites a graph proto on disk.\r\n\r\nThe graph is written as a binary proto unless as_text is `True`.\r\n\r\n```python\r\nv = tf.Variable(0, name='my_variable')\r\nsess = tf.Session()\r\ntf.train.write_graph(sess.graph_def, '/tmp/my-model', 'train.pbtxt')\r\n```\r\n\r\n##### Args: <a class=\"md-anchor\" id=\"AUTOGENERATED-args-\"></a>\r\n\r\n\r\n*  <b>`graph_def`</b>: A `GraphDef` protocol buffer.\r\n*  <b>`logdir`</b>: Directory where to write the graph.\r\n*  <b>`name`</b>: Filename for the graph.\r\n*  <b>`as_text`</b>: If `True`, writes the graph as an ASCII proto.\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/extras/README.txt",
    "content": "This directory holds extra files we'd like to be able\r\nto link to and serve from within tensorflow.org\r\n"
  },
  {
    "path": "SOURCE/faq.md",
    "content": "# 常见问题汇总\r\n\r\n## 说明\r\n\r\n本章非官方文档翻译，是由众多TensorFlow爱好者将安装和使用TF过程中的问题总结而成的。\r\n\r\n## 常见问题及解答\r\n\r\n### (示例)官网地址是哪里？\r\n\r\n[http://www.tensorflow.org/](http://www.tensorflow.org/)\r\n\r\n### 如何安装 pip ？\r\n\r\n * Ubuntu (14.04)\r\n\r\n        sudo apt-get update && sudo apt-get install -y python python-dev python-pip\r\n\r\n * CentOS 7\r\n\r\n        yum update -y && yum install -y python python-devel epel-release.noarch python-pip\r\n\r\n * MACOS\r\n\r\n        sudo easy_install pip\r\n\r\n### docker run -it b.gcr.io/tensorflow/tensorflow 失败\r\n\r\n该镜像所在仓库被墙，需要梯子。\r\n\r\n[这里](http://pan.baidu.com/s/1bnyVrMR)（密码：v9ts）有镜像的导出包。\r\n使用方法\r\n\r\n    docker load < sensorflow.tar.gz\r\n\r\n### SSLError: SSL_VERIFY_FAILED\r\n\r\n在用url通过pip的方式安装的时候，可能会遇到的错误如下：\r\n\r\n        SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)\r\n\r\n解决办法:先下载wheel到本地，之后本地pip install\r\n"
  },
  {
    "path": "SOURCE/get_started/basic_usage.md",
    "content": "# 基本使用 <a class=\"md-anchor\" id=\"AUTOGENERATED-basic-usage\"></a>\r\n\r\n使用 TensorFlow, 你必须明白 TensorFlow:\r\n\r\n* 使用图 (graph) 来表示计算任务.\r\n* 在被称之为 `会话 (Session)` 的上下文 (context) 中执行图.\r\n* 使用 tensor 表示数据.\r\n* 通过 `变量 (Variable)` 维护状态.\r\n* 使用 feed 和 fetch 可以为任意的操作(arbitrary operation) 赋值或者从其中获取数据.\r\n\r\n## 综述 <a class=\"md-anchor\" id=\"AUTOGENERATED-overview\"></a>\r\n\r\nTensorFlow 是一个编程系统, 使用图来表示计算任务. 图中的节点被称之为 *op* \r\n(operation 的缩写). 一个 op 获得 0 个或多个 `Tensor`, 执行计算,\r\n产生 0 个或多个 `Tensor `. 每个 Tensor 是一个类型化的多维数组. \r\n例如, 你可以将一小组图像集表示为一个四维浮点数数组, \r\n这四个维度分别是 `[batch, height, width, channels]`.\r\n\r\n一个 TensorFlow 图*描述*了计算的过程. 为了进行计算, 图必须在 `会话` 里被启动.\r\n`会话` 将图的 op 分发到诸如 CPU 或 GPU 之类的 `设备` 上, 同时提供执行 op 的方法.\r\n这些方法执行后, 将产生的 tensor 返回. 在 Python 语言中, 返回的 tensor 是\r\n[numpy](http://www.numpy.org) `ndarray` 对象; 在 C 和 C++ 语言中, 返回的 tensor 是\r\n `tensorflow::Tensor` 实例.\r\n\r\n## 计算图 <a class=\"md-anchor\" id=\"AUTOGENERATED-the-computation-graph\"></a>\r\n\r\nTensorFlow 程序通常被组织成一个构建阶段和一个执行阶段. 在构建阶段, op 的执行步骤\r\n被描述成一个图. 在执行阶段, 使用会话执行执行图中的 op.\r\n\r\n例如, 通常在构建阶段创建一个图来表示和训练神经网络, 然后在执行阶段反复执行图中的训练 op.\r\n\r\nTensorFlow 支持 C, C++, Python 编程语言. 目前, TensorFlow 的 Python 库更加易用,\r\n它提供了大量的辅助函数来简化构建图的工作, 这些函数尚未被 C 和 C++ 库支持.\r\n\r\n三种语言的会话库 (session libraries) 是一致的.\r\n\r\n### 构建图 <a class=\"md-anchor\" id=\"AUTOGENERATED-building-the-graph\"></a>\r\n\r\n构建图的第一步, 是创建源 op (source op). 源 op 不需要任何输入, 例如 `常量 (Constant)`. 源 op 的输出被传递给其它 op 做运算.\r\n\r\nPython 库中, op 构造器的返回值代表被构造出的 op 的输出, 这些返回值可以传递给其它\r\nop 构造器作为输入.\r\n\r\nTensorFlow Python 库有一个*默认图 (default graph)*, op 构造器可以为其增加节点. 这个默认图对\r\n许多程序来说已经足够用了. 阅读 [Graph 类](../api_docs/python/framework.md#Graph) 文档\r\n来了解如何管理多个图.\r\n\r\n```python\r\nimport tensorflow as tf\r\n\r\n# 创建一个常量 op, 产生一个 1x2 矩阵. 这个 op 被作为一个节点\r\n# 加到默认图中.\r\n#\r\n# 构造器的返回值代表该常量 op 的返回值.\r\nmatrix1 = tf.constant([[3., 3.]])\r\n\r\n# 创建另外一个常量 op, 产生一个 2x1 矩阵.\r\nmatrix2 = tf.constant([[2.],[2.]])\r\n\r\n# 创建一个矩阵乘法 matmul op , 把 'matrix1' 和 'matrix2' 作为输入.\r\n# 返回值 'product' 代表矩阵乘法的结果.\r\nproduct = tf.matmul(matrix1, matrix2)\r\n```\r\n\r\n默认图现在有三个节点, 两个 `constant()` op, 和一个`matmul()` op. 为了真正进行矩阵相乘运算, 并得到矩阵乘法的\r\n结果, 你必须在会话里启动这个图. \r\n\r\n### 在一个会话中启动图 <a class=\"md-anchor\" id=\"AUTOGENERATED-launching-the-graph-in-a-session\"></a>\r\n\r\n构造阶段完成后, 才能启动图. 启动图的第一步是创建一个 `Session` 对象, 如果无任何创建参数, \r\n会话构造器将启动默认图.\r\n\r\n欲了解完整的会话 API, 请阅读[Session 类](../api_docs/python/client.md#session-management).\r\n\r\n```python\r\n# 启动默认图.\r\nsess = tf.Session()\r\n\r\n# 调用 sess 的 'run()' 方法来执行矩阵乘法 op, 传入 'product' 作为该方法的参数. \r\n# 上面提到, 'product' 代表了矩阵乘法 op 的输出, 传入它是向方法表明, 我们希望取回\r\n# 矩阵乘法 op 的输出.\r\n#\r\n# 整个执行过程是自动化的, 会话负责传递 op 所需的全部输入. op 通常是并发执行的.\r\n# \r\n# 函数调用 'run(product)' 触发了图中三个 op (两个常量 op 和一个矩阵乘法 op) 的执行.\r\n#\r\n# 返回值 'result' 是一个 numpy `ndarray` 对象.\r\nresult = sess.run(product)\r\nprint result\r\n# ==> [[ 12.]]\r\n\r\n# 任务完成, 关闭会话.\r\nsess.close()\r\n```\r\n\r\n`Session` 对象在使用完后需要关闭以释放资源. 除了显式调用 close 外, 也可以使用 \"with\" 代码块\r\n来自动完成关闭动作.\r\n\r\n```python\r\nwith tf.Session() as sess:\r\n  result = sess.run([product])\r\n  print result\r\n```\r\n\r\n在实现上, TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU\r\n或 GPU). 一般你不需要显式指定使用 CPU 还是 GPU, TensorFlow 能自动检测. 如果检测到 GPU, TensorFlow \r\n会尽可能地利用找到的第一个 GPU 来执行操作.\r\n\r\n如果机器上有超过一个可用的 GPU, 除第一个外的其它 GPU 默认是不参与计算的. 为了让 TensorFlow \r\n使用这些 GPU, 你必须将 op 明确指派给它们执行. `with...Device` 语句用来指派特定的 CPU 或 GPU\r\n执行操作:\r\n\r\n```python\r\nwith tf.Session() as sess:\r\n  with tf.device(\"/gpu:1\"):\r\n    matrix1 = tf.constant([[3., 3.]])\r\n    matrix2 = tf.constant([[2.],[2.]])\r\n    product = tf.matmul(matrix1, matrix2)\r\n    ...\r\n```\r\n\r\n设备用字符串进行标识. 目前支持的设备包括:\r\n\r\n*  `\"/cpu:0\"`: 机器的 CPU.\r\n*  `\"/gpu:0\"`: 机器的第一个 GPU, 如果有的话.\r\n*  `\"/gpu:1\"`: 机器的第二个 GPU, 以此类推.\r\n\r\n阅读[使用GPU](tensorflow-zh/SOURCE/how_tos/using_gpu.md)章节, 了解 TensorFlow GPU 使用的更多信息. \r\n\r\n## 交互式使用 <a class=\"md-anchor\" id=\"AUTOGENERATED-interactive-usage\"></a>\r\n\r\n文档中的 Python 示例使用一个会话 [`Session`](../api_docs/python/client.md#Session) 来\r\n启动图, 并调用 [`Session.run()`](../api_docs/python/client.md#Session.run) 方法执行操作. \r\n\r\n为了便于使用诸如 [IPython](http://ipython.org) 之类的 Python 交互环境, 可以使用\r\n[`InteractiveSession`](../api_docs/python/client.md#InteractiveSession) 代替\r\n`Session` 类, 使用 [`Tensor.eval()`](../api_docs/python/framework.md#Tensor.eval)\r\n和 [`Operation.run()`](../api_docs/python/framework.md#Operation.run) 方法代替\r\n`Session.run()`. 这样可以避免使用一个变量来持有会话.\r\n\r\n```python\r\n# 进入一个交互式 TensorFlow 会话.\r\nimport tensorflow as tf\r\nsess = tf.InteractiveSession()\r\n\r\nx = tf.Variable([1.0, 2.0])\r\na = tf.constant([3.0, 3.0])\r\n\r\n# 使用初始化器 initializer op 的 run() 方法初始化 'x' \r\nx.initializer.run()\r\n\r\n# 增加一个减法 sub op, 从 'x' 减去 'a'. 运行减法 op, 输出结果 \r\nsub = tf.sub(x, a)\r\nprint sub.eval()\r\n# ==> [-2. -1.]\r\n```\r\n\r\n## Tensor <a class=\"md-anchor\" id=\"AUTOGENERATED-tensors\"></a>\r\n\r\nTensorFlow 程序使用 tensor 数据结构来代表所有的数据, 计算图中, 操作间传递的数据都是 tensor.\r\n你可以把 TensorFlow tensor 看作是一个 n 维的数组或列表. 一个 tensor 包含一个静态类型 rank, 和\r\n一个 shape. 想了解 TensorFlow 是如何处理这些概念的, 参见\r\n[Rank, Shape, 和 Type](../resources/dims_types.md).\r\n\r\n## 变量 <a class=\"md-anchor\" id=\"AUTOGENERATED-variables\"></a>\r\n\r\n[Variables](../how_tos/variables/index.md) for more details.\r\n变量维护图执行过程中的状态信息. 下面的例子演示了如何使用变量实现一个简单的计数器. 参见\r\n[变量](tensorflow-zh/how_tos/variables.md) 章节了解更多细节.\r\n\r\n```python\r\n# 创建一个变量, 初始化为标量 0.\r\nstate = tf.Variable(0, name=\"counter\")\r\n\r\n# 创建一个 op, 其作用是使 state 增加 1\r\n\r\none = tf.constant(1)\r\nnew_value = tf.add(state, one)\r\nupdate = tf.assign(state, new_value)\r\n\r\n# 启动图后, 变量必须先经过`初始化` (init) op 初始化,\r\n# 首先必须增加一个`初始化` op 到图中.\r\ninit_op = tf.initialize_all_variables()\r\n\r\n# 启动图, 运行 op\r\nwith tf.Session() as sess:\r\n  # 运行 'init' op\r\n  sess.run(init_op)\r\n  # 打印 'state' 的初始值\r\n  print sess.run(state)\r\n  # 运行 op, 更新 'state', 并打印 'state'\r\n  for _ in range(3):\r\n    sess.run(update)\r\n    print sess.run(state)\r\n\r\n# 输出:\r\n\r\n# 0\r\n# 1\r\n# 2\r\n# 3\r\n```\r\n\r\n代码中 `assign()` 操作是图所描绘的表达式的一部分, 正如 `add()` 操作一样. 所以在调用 `run()` \r\n执行表达式之前, 它并不会真正执行赋值操作.\r\n\r\n通常会将一个统计模型中的参数表示为一组变量. 例如, 你可以将一个神经网络的权重作为某个变量存储在一个 tensor 中.\r\n在训练过程中, 通过重复运行训练图, 更新这个 tensor.\r\n\r\n## Fetch <a class=\"md-anchor\" id=\"AUTOGENERATED-fetches\"></a>\r\n\r\n为了取回操作的输出内容, 可以在使用 `Session` 对象的 `run()` 调用 执行图时, 传入一些 tensor,\r\n这些 tensor 会帮助你取回结果. 在之前的例子里, 我们只取回了单个节点 `state`, 但是你也可以取回多个\r\ntensor:\r\n\r\n\r\n```python\r\ninput1 = tf.constant(3.0)\r\ninput2 = tf.constant(2.0)\r\ninput3 = tf.constant(5.0)\r\nintermed = tf.add(input2, input3)\r\nmul = tf.mul(input1, intermed)\r\n\r\nwith tf.Session() as sess:\r\n  result = sess.run([mul, intermed])\r\n  print result\r\n\r\n# 输出:\r\n# [array([ 21.], dtype=float32), array([ 7.], dtype=float32)]\r\n```\r\n\r\n需要获取的多个 tensor 值，在 op 的一次运行中一起获得（而不是逐个去获取 tensor）。\r\n\r\n## Feed <a class=\"md-anchor\" id=\"AUTOGENERATED-feeds\"></a>\r\n\r\n上述示例在计算图中引入了 tensor, 以常量或变量的形式存储. TensorFlow 还提供了 feed 机制, 该机制\r\n可以临时替代图中的任意操作中的 tensor\t可以对图中任何操作提交补丁, 直接插入一个 tensor.\r\n\r\nfeed 使用一个 tensor 值临时替换一个操作的输出结果. 你可以提供 feed 数据作为 `run()` 调用的参数.\r\nfeed 只在调用它的方法内有效, 方法结束, feed 就会消失. 最常见的用例是将某些特殊的操作指定为 \"feed\" 操作,\r\n标记的方法是使用 tf.placeholder() 为这些操作创建占位符. \r\n\r\n```python\r\n\r\ninput1 = tf.placeholder(tf.float32)\r\ninput2 = tf.placeholder(tf.float32)\r\noutput = tf.mul(input1, input2)\r\n\r\nwith tf.Session() as sess:\r\n  print sess.run([output], feed_dict={input1:[7.], input2:[2.]})\r\n\r\n# 输出:\r\n# [array([ 14.], dtype=float32)]\r\n```\r\n\r\nfor a larger-scale example of feeds.\r\n如果没有正确提供 feed, `placeholder()` 操作将会产生错误.\r\nMNIST 全连通 [feed 教程](http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_tf.html)\r\n([source code](https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/tutorials/mnist/fully_connected_feed.py))\r\n给出了一个更大规模的使用 feed 的例子.\r\n\r\n> 原文：[Basic Usage](http://tensorflow.org/get_started/basic_usage.md)  翻译：[@doc001](https://github.com/PFZheng)  校对：[@yangtze](https://github.com/sstruct)\r\n\r\n"
  },
  {
    "path": "SOURCE/get_started/introduction.md",
    "content": "# 简介 <a class=\"md-anchor\" id=\"AUTOGENERATED-introduction\"></a>\r\n\r\n本章的目的是让你了解和运行 TensorFlow!\r\n\r\n在开始之前, 让我们先看一段使用 Python API 撰写的 TensorFlow 示例代码, \r\n让你对将要学习的内容有初步的印象.\r\n\r\n这段很短的 Python 程序生成了一些三维数据, 然后用一个平面拟合它.\r\n\r\n```python\r\nimport tensorflow as tf\r\nimport numpy as np\r\n\r\n# 使用 NumPy 生成假数据(phony data), 总共 100 个点.\r\nx_data = np.float32(np.random.rand(2, 100)) # 随机输入\r\ny_data = np.dot([0.100, 0.200], x_data) + 0.300\r\n\r\n# 构造一个线性模型\r\n# \r\nb = tf.Variable(tf.zeros([1]))\r\nW = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))\r\ny = tf.matmul(W, x_data) + b\r\n\r\n# 最小化方差\r\nloss = tf.reduce_mean(tf.square(y - y_data))\r\noptimizer = tf.train.GradientDescentOptimizer(0.5)\r\ntrain = optimizer.minimize(loss)\r\n\r\n# 初始化变量\r\ninit = tf.initialize_all_variables()\r\n\r\n# 启动图 (graph)\r\nsess = tf.Session()\r\nsess.run(init)\r\n\r\n# 拟合平面\r\nfor step in xrange(0, 201):\r\n    sess.run(train)\r\n    if step % 20 == 0:\r\n        print step, sess.run(W), sess.run(b)\r\n\r\n# 得到最佳拟合结果 W: [[0.100  0.200]], b: [0.300]\r\n```\r\n\r\n为了进一步激发你的学习欲望, 我们想让你先看一下 TensorFlow 是如何解决一个经典的机器\r\n学习问题的. 在神经网络领域, 最为经典的问题莫过于 MNIST 手写数字分类问题. 我们准备了\r\n两篇不同的教程, 分别面向机器学习领域的初学者和专家. 如果你已经使用其它软件训练过许多\r\nMNIST 模型, 请阅读高级教程 (红色药丸链接). 如果你以前从未听说过 MNIST, 请阅读初级教程\r\n(蓝色药丸链接). 如果你的水平介于这两类人之间, 我们建议你先快速浏览初级教程, 然后再阅读高级教程.\r\n\r\n<div style=\"width:100%; margin:auto; margin-bottom:10px; margin-top:20px; display: flex; flex-direction: row\">\r\n <a href=\"http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_beginners.html\" title=\"面向机器学习初学者的 MNIST 初级教程\">\r\n   <img style=\"flex-grow:1; flex-shrink:1; border: 1px solid black;\" src=\"../images/blue_pill.png\" alt=\"面向机器学习初学者的 MNIST 初级教程\" />\r\n </a>\r\n <a href=\"http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_pros.html\" title=\"面向机器学习专家的 MNIST 高级教程\">\r\n   <img style=\"flex-grow:1; flex-shrink:1; border: 1px solid black;\" src=\"../images/red_pill.png\" alt=\"面向机器学习专家的 MNIST 高级教程\" />\r\n </a>\r\n</div>\r\n<p style=\"font-size:10px;\">图片由 CC BY-SA 4.0 授权; 原作者 W. Carter</p>\r\n\r\n如果你已经下定决心, 准备学习和安装 TensorFlow, 你可以略过这些文字, 直接阅读\r\n后面的章节. 不用担心, 你仍然会看到 MNIST -- 在阐述 TensorFlow 的特性时,\r\n我们还会使用 MNIST 作为一个样例.\r\n\r\n## 推荐随后阅读: <a class=\"md-anchor\" id=\"AUTOGENERATED-recommended-next-steps-\"></a>\r\n\r\n* [下载与安装](../get_started/os_setup.md)\r\n* [基本使用](../get_started/basic_usage.md)\r\n* [TensorFlow 技术指南](../tutorials/mnist/tf/index.md)\r\n\r\n<div class='sections-order' style=\"display: none;\">\r\n<!-- os_setup.md -->\r\n<!-- basic_usage.md -->\r\n</div>\r\n\r\n> 原文：[Introduction](http://tensorflow.org/get_started)  翻译：[@doc001](https://github.com/PFZheng)  校对：[@yangtze](https://github.com/sstruct)\r\n"
  },
  {
    "path": "SOURCE/get_started/os_setup.md",
    "content": "# 下载与安装 <a class=\"md-anchor\" id=\"AUTOGENERATED-download-and-setup\"></a>\n\n你可以使用我们提供的 Pip, Docker, Virtualenv, Anaconda 或 源码编译的方法安装 TensorFlow.\n\n## Pip 安装 <a class=\"md-anchor\" id=\"AUTOGENERATED-binary-installation\"></a>\n\n[Pip](https://en.wikipedia.org/wiki/Pip_(package_manager)) 是一个 Python 的软件包安装与管理工具.\n\n在安装 TensorFlow 过程中要涉及安装或升级的包详见 [列表](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/pip_package/setup.py)\n\n首先安装 pip (或 Python3 的 pip3 ):\n\n```bash\n# Ubuntu/Linux 64-bit\n$ sudo apt-get install python-pip python-dev\n\n# Mac OS X\n$ sudo easy_install pip\n```\n\n安装 TensorFlow :\n\n```bash\n# Ubuntu/Linux 64-bit, CPU only, Python 2.7:\n$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl\n\n# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7. Requires CUDA toolkit 7.5 and CuDNN v4.\n# For other versions, see \"Install from sources\" below.\n$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl\n\n# Mac OS X, CPU only:\n$ sudo easy_install --upgrade six\n$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl\n```\n\n如果是 Python3 :\n\n```bash\n# Ubuntu/Linux 64-bit, CPU only, Python 3.4:\n$ sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp34-cp34m-linux_x86_64.whl\n\n# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4. Requires CUDA toolkit 7.5 and CuDNN v4.\n# For other versions, see \"Install from sources\" below.\n$ sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0-cp34-cp34m-linux_x86_64.whl\n\n# Mac OS X, CPU only:\n$ sudo easy_install --upgrade six\n$ sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py3-none-any.whl\n```\n\n备注：如果之前安装过 TensorFlow < 0.7.1 的版本,应该先使用 `pip uninstall` 卸载 TensorFlow 和 protobuf ,保证获取的是一个最新 protobuf 依赖下的安装包.\n\n之后可以[测试](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md#test-the-tensorflow-installation)一下.\n\n## 基于 Docker 的安装 <a class=\"md-anchor\" id=\"AUTOGENERATED-docker-based-installation\"></a>\n\n我们也支持通过 [Docker](http://docker.com/) 运行 TensorFlow. \n该方式的优点是不用操心软件依赖问题.\n\n首先, [安装 Docker](http://docs.docker.com/engine/installation/). 一旦 Docker\n已经启动运行, 可以通过命令启动一个容器:\n\n```bash\n$ docker run -it b.gcr.io/tensorflow/tensorflow\n```\n\n该命令将启动一个已经安装好 TensorFlow 及相关依赖的容器.\n\n### 其它镜像 <a class=\"md-anchor\" id=\"AUTOGENERATED-additional-images\"></a>\n\n默认的 Docker 镜像只包含启动和运行 TensorFlow 所需依赖库的一个最小集. 我们额外提供了\n下面的容器, 该容器同样可以通过上述 `docker run` 命令安装:\n\n* `b.gcr.io/tensorflow/tensorflow-full`: 镜像中的 TensorFlow 是从源代码完整安装的,\n  包含了编译和运行 TensorFlow 所需的全部工具. 在该镜像上, 可以直接使用源代码进行实验,\n  而不需要再安装上述的任何依赖.\n \n## 基于 VirtualEnv 的安装 <a class=\"md-anchor\" id=\"virtualenv_install\"></a>\n\n我们推荐使用 [virtualenv](https://pypi.python.org/pypi/virtualenv) 创建一个隔离的容器, 来安装 TensorFlow. 这是可选的, 但是这样做能使排查安装问题变得更容易.\n\n首先, 安装所有必备工具:\n\n```bash\n# 在 Linux 上:\n$ sudo apt-get install python-pip python-dev python-virtualenv\n\n# 在 Mac 上:\n$ sudo easy_install pip  # 如果还没有安装 pip\n$ sudo pip install --upgrade virtualenv\n```\n\n接下来, 建立一个全新的 virtualenv 环境. 为了将环境建在 `~/tensorflow`\n目录下, 执行:\n\n```bash\n$ virtualenv --system-site-packages ~/tensorflow\n$ cd ~/tensorflow\n```\n\n然后, 激活 virtualenv:\n\n```bash\n$ source bin/activate  # 如果使用 bash\n$ source bin/activate.csh  # 如果使用 csh\n(tensorflow)$  # 终端提示符应该发生变化\n```\n\n在 virtualenv 内, 安装 TensorFlow:\n\n```bash\n(tensorflow)$ pip install --upgrade <$url_to_binary.whl>\n```\n\n接下来, 使用类似命令运行 TensorFlow 程序:\n\n```bash\n(tensorflow)$ cd tensorflow/models/image/mnist\n(tensorflow)$ python convolutional.py\n\n# 当使用完 TensorFlow\n(tensorflow)$ deactivate  # 停用 virtualenv\n\n$  # 你的命令提示符会恢复原样\n```\n\n## 基于 Anaconda 的安装 <a class=\"md-anchor\" id=\"anaconda_install\"></a>\n\n[Anaconda](https://www.continuum.io/why-anaconda) 是一个集成许多第三方科学计算库的 Python 科学计算环境,Anaconda 使用 conda 作为自己的包管理工具,同时具有自己的[计算环境](http://conda.pydata.org/docs/using/envs.html),类似 Virtualenv.\n\n和 Virtualenv 一样,不同 Python 工程需要的依赖包,conda 将他们存储在不同的地方。 TensorFlow 上安装的 Anaconda 不会对之前安装的 Python 包进行覆盖.\n\n* 安装 Anaconda\n* 建立一个 conda 计算环境\n* 激活环境,使用 conda 安装 TensorFlow\n* 安装成功后,每次使用 TensorFlow 的时候需要激活 conda 环境\n\n安装 Anaconda :\n\n参考 Anaconda 的下载页面的[指导](https://www.continuum.io/downloads)\n\n建立一个 conda 计算环境名字叫`tensorflow`:\n\n```bash\n# Python 2.7\n$ conda create -n tensorflow python=2.7\n\n# Python 3.4\n$ conda create -n tensorflow python=3.4\n```\n\n激活`tensorflow`环境,然后使用其中的 pip 安装 TensorFlow. 当使用`easy_install`使用`--ignore-installed`标记防止错误的产生。\n\n```bash\n$ source activate tensorflow\n(tensorflow)$  # Your prompt should change\n\n# Ubuntu/Linux 64-bit, CPU only, Python 2.7:\n(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl\n\n# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7. Requires CUDA toolkit 7.5 and CuDNN v4.\n# For other versions, see \"Install from sources\" below.\n(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl\n\n# Mac OS X, CPU only:\n(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0rc0-py2-none-any.whl\n```\n\n对于 Python 3.x :\n\n```bash\n$ source activate tensorflow\n(tensorflow)$  # Your prompt should change\n\n# Ubuntu/Linux 64-bit, CPU only, Python 3.4:\n(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp34-cp34m-linux_x86_64.whl\n\n# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4. Requires CUDA toolkit 7.5 and CuDNN v4.\n# For other versions, see \"Install from sources\" below.\n(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0rc0-cp34-cp34m-linux_x86_64.whl\n\n# Mac OS X, CPU only:\n(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0rc0-py3-none-any.whl\n```\n\nconda 环境激活后,你可以[测试](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md#test-the-tensorflow-installation)\n\n当你不用 TensorFlow 的时候,关闭环境:\n\n```bash\n(tensorflow)$ source deactivate\n\n$  # Your prompt should change back\n```\n\n再次使用的时候再激活 :-)\n\n```bash\n$ source activate tensorflow\n(tensorflow)$  # Your prompt should change.\n# Run Python programs that use TensorFlow.\n...\n# When you are done using TensorFlow, deactivate the environment.\n(tensorflow)$ source deactivate\n```\n\n\n\n## 尝试你的第一个 TensorFlow 程序 <a class=\"md-anchor\" id=\"AUTOGENERATED-try-your-first-tensorflow-program\"></a>\n\n### (可选) 启用 GPU 支持 <a class=\"md-anchor\" id=\"AUTOGENERATED--optional--enable-gpu-support\"></a>\n\n如果你使用 pip 二进制包安装了开启 GPU 支持的 TensorFlow, 你必须确保\n系统里安装了正确的 CUDA sdk 和 CUDNN 版本. 请参间  [CUDA 安装教程](#install_cuda)\n\n你还需要设置 `LD_LIBRARY_PATH` 和 `CUDA_HOME` 环境变量. 可以考虑将下面的命令\n添加到 `~/.bash_profile` 文件中, 这样每次登陆后自动生效. 注意, 下面的命令\n假定 CUDA 安装目录为 `/usr/local/cuda`:\n\n```bash\nexport LD_LIBRARY_PATH=\"$LD_LIBRARY_PATH:/usr/local/cuda/lib64\"\nexport CUDA_HOME=/usr/local/cuda\n```\n\n### 运行 TensorFlow <a class=\"md-anchor\" id=\"AUTOGENERATED-run-tensorflow\"></a>\n\n打开一个 python 终端:\n\n```bash\n$ python\n\n>>> import tensorflow as tf\n>>> hello = tf.constant('Hello, TensorFlow!')\n>>> sess = tf.Session()\n>>> print sess.run(hello)\nHello, TensorFlow!\n>>> a = tf.constant(10)\n>>> b = tf.constant(32)\n>>> print sess.run(a+b)\n42\n>>>\n\n```\n\n## 从源码安装 <a class=\"md-anchor\" id=\"source\"></a>\n\n### 克隆 TensorFlow 仓库 <a class=\"md-anchor\" id=\"AUTOGENERATED-clone-the-tensorflow-repository\"></a>\n\n```bash\n$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow\n```\n\n`--recurse-submodules` 参数是必须得, 用于获取 TesorFlow 依赖的 protobuf 库.\n\n### Linux 安装 <a class=\"md-anchor\" id=\"AUTOGENERATED-installation-for-linux\"></a>\n\n#### 安装 Bazel <a class=\"md-anchor\" id=\"AUTOGENERATED-install-bazel\"></a>\n\n首先依照 [教程](http://bazel.io/docs/install.html) 安装 Bazel 的依赖.\n然后在 [链接](https://github.com/bazelbuild/bazel/releases) 中下载适合你的操作系统的最新稳定版,\n最后按照下面脚本执行：\n\n```bash\n$ chmod +x PATH_TO_INSTALL.SH\n$ ./PATH_TO_INSTALL.SH --user\n```\n\n注意把 `PATH_TO_INSTALL.SH` 替换为你下载的安装包的文件路径.\n\n将执行路径 `output/bazel` 添加到 `$PATH` 环境变量中.\n\n#### 安装其他依赖 <a class=\"md-anchor\" id=\"AUTOGENERATED-install-other-dependencies\"></a>\n\n```bash\n# For Python 2.7:\n$ sudo apt-get install python-numpy swig python-dev python-wheel\n# For Python 3.x:\n$ sudo apt-get install python3-numpy swig python3-dev python3-wheel\n```\n\n#### 可选: 安装 CUDA (在 Linux 上开启 GPU 支持) <a class=\"md-anchor\" id=\"install_cuda\"></a>\n\n为了编译并运行能够使用 GPU 的 TensorFlow, 需要先安装 NVIDIA 提供的 Cuda Toolkit 7.0\n和 CUDNN 6.5 V2.\n\nTensorFlow 的 GPU 特性只支持 NVidia Compute Capability >= 3.5 的显卡. 被支持的显卡\n包括但不限于:\n\n* NVidia Titan\n* NVidia Titan X\n* NVidia K20\n* NVidia K40\n\n##### 下载并安装 Cuda Toolkit 7.0 <a class=\"md-anchor\" id=\"AUTOGENERATED-download-and-install-cuda-toolkit-7.0\"></a>\n\n[下载地址](https://developer.nvidia.com/cuda-toolkit-70)\n\n将工具安装到诸如 `/usr/local/cuda` 之类的路径.\n\n##### 下载并安装 CUDNN Toolkit 6.5 <a class=\"md-anchor\" id=\"AUTOGENERATED-download-and-install-cudnn-toolkit-6.5\"></a>\n\n[下载地址](https://developer.nvidia.com/rdp/cudnn-archive)\n\n解压并拷贝 CUDNN 文件到 Cuda Toolkit 7.0 安装路径下. 假设 Cuda Toolkit 7.0 安装\n在 `/usr/local/cuda`, 执行以下命令:\n\n``` bash\ntar xvzf cudnn-6.5-linux-x64-v2.tgz\nsudo cp cudnn-6.5-linux-x64-v2/cudnn.h /usr/local/cuda/include\nsudo cp cudnn-6.5-linux-x64-v2/libcudnn* /usr/local/cuda/lib64\n```\n\n##### 配置 TensorFlow 的 Cuda 选项 <a class=\"md-anchor\" id=\"AUTOGENERATED-configure-tensorflow-s-canonical-view-of-cuda-libraries\"></a>\n\n从源码树的根路径执行:\n\n``` bash\n$ ./configure\nDo you wish to bulid TensorFlow with GPU support? [y/n] y\nGPU support will be enabled for TensorFlow\n\nPlease specify the location where CUDA 7.0 toolkit is installed. Refer to\nREADME.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda\n\nPlease specify the location where CUDNN 6.5 V2 library is installed. Refer to\nREADME.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda\n\nSetting up Cuda include\nSetting up Cuda lib64\nSetting up Cuda bin\nSetting up Cuda nvvm\nConfiguration finished\n```\n\n这些配置将建立到系统 Cuda 库的符号链接. 每当 Cuda 库的路径发生变更时, 必须重新执行上述\n步骤, 否则无法调用 bazel 编译命令.\n\n##### 编译目标程序, 开启 GPU 支持 <a class=\"md-anchor\" id=\"AUTOGENERATED-build-your-target-with-gpu-support.\"></a>\n\n从源码树的根路径执行:\n\n```bash\n$ bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer\n\n$ bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu\n# 大量的输出信息. 这个例子用 GPU 迭代计算一个 2x2 矩阵的主特征值 (major eigenvalue).\n# 最后几行输出和下面的信息类似.\n000009/000005 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]\n000006/000001 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]\n000009/000009 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]\n```\n\n注意, GPU 支持需通过编译选项 \"--config=cuda\" 开启.\n\n##### 已知问题 <a class=\"md-anchor\" id=\"AUTOGENERATED-known-issues\"></a>\n\n* 尽管可以在同一个源码树下编译开启 Cuda 支持和禁用 Cuda 支持的版本, 我们还是推荐在\n在切换这两种不同的编译配置时, 使用 \"bazel clean\" 清理环境.\n\n* 在执行 bazel 编译前必须先运行 configure, 否则编译会失败并提示错误信息. 未来, \n我们可能考虑将 configure 步骤包含在编译过程中, 以简化整个过程, 前提是 bazel 能够提供新的特性支持这样. \n\n### Mac OS X 安装 <a class=\"md-anchor\" id=\"AUTOGENERATED-installation-for-mac-os-x\"></a>\n\nMac 和 Linux 需要的软件依赖完全一样, 但是安装过程区别很大. 以下链接用于帮助你\n在 Mac OS X 上安装这些依赖:\n\n#### Bazel <a class=\"md-anchor\" id=\"AUTOGENERATED-bazel\"></a>\n\n参见[本网页](http://bazel.io/docs/install.html)的 Mac OS X 安装指南.\n\n#### SWIG <a class=\"md-anchor\" id=\"AUTOGENERATED-swig\"></a>\n\n[Mac OS X 安装教程](http://www.swig.org/Doc3.0/Preface.html#Preface_osx_installation).\n\n注意: 你需要安装[PCRE](ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/),\n而*不是* PCRE2.\n\n#### Numpy <a class=\"md-anchor\" id=\"AUTOGENERATED-numpy\"></a>\n\n参见[安装教程](http://docs.scipy.org/doc/numpy/user/install.html).\n\n### 创建 pip 包并安装 <a class=\"md-anchor\" id=\"create-pip\"></a>\n\n```bash\n$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package\n\n$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg\n\n# .whl 文件的实际名字与你所使用的平台有关\n$ pip install /tmp/tensorflow_pkg/tensorflow-0.5.0-cp27-none-linux_x86_64.whl\n```\n\n## 训练你的第一个 TensorFlow 神经网络模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-train-your-first-tensorflow-neural-net-model\"></a>\n\n从源代码树的根路径执行:\n\n```python\n$ cd tensorflow/models/image/mnist\n$ python convolutional.py\nSuccesfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.\nSuccesfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.\nSuccesfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.\nSuccesfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.\nExtracting data/train-images-idx3-ubyte.gz\nExtracting data/train-labels-idx1-ubyte.gz\nExtracting data/t10k-images-idx3-ubyte.gz\nExtracting data/t10k-labels-idx1-ubyte.gz\nInitialized!\nEpoch 0.00\nMinibatch loss: 12.054, learning rate: 0.010000\nMinibatch error: 90.6%\nValidation error: 84.6%\nEpoch 0.12\nMinibatch loss: 3.285, learning rate: 0.010000\nMinibatch error: 6.2%\nValidation error: 7.0%\n...\n...\n```\n\n## 常见问题 <a class=\"md-anchor\" id=\"common_install_problems\"></a>\n\n### GPU 相关问题 <a class=\"md-anchor\" id=\"AUTOGENERATED-gpu-related-issues\"></a>\n\n如果在尝试运行一个 TensorFlow 程序时出现以下错误:\n\n```python\nImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory\n```\n\n请确认你正确安装了 GPU 支持, 参见 [相关章节](#install_cuda).\n\n### 在 Linux 上 <a class=\"md-anchor\" id=\"AUTOGENERATED-on-linux\"></a>\n\n如果出现错误:\n\n```python\n...\n \"__add__\", \"__radd__\",\n             ^\nSyntaxError: invalid syntax\n```\n\n解决方案: 确认正在使用的 Python 版本为 Python 2.7.\n\n### 在 Mac OS X 上 <a class=\"md-anchor\" id=\"AUTOGENERATED-on-macosx\"></a>\n\n如果出现错误:\n\n```python\nimport six.moves.copyreg as copyreg\n\nImportError: No module named copyreg\n```\n\n解决方案: TensorFlow 使用的 protobuf 依赖 `six-1.10.0`. 但是, Apple 的默认 python 环境\n已经安装了 `six-1.4.1`, 该版本可能很难升级. 这里提供几种方法来解决该问题:\n\n1. 升级全系统的 `six`:\n\n    ```bash\n    sudo easy_install -U six\n    ```\n\n2. 通过 homebrew 安装一个隔离的 python 副本:\n\n    ```bash\n    brew install python\n    ```\n\n3. 在[`virtualenv`](#virtualenv_install) 内编译或使用 TensorFlow.\n\n\n如果出现错误:\n\n```\n>>> import tensorflow as tf\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\n  File \"/usr/local/lib/python2.7/site-packages/tensorflow/__init__.py\", line 4, in <module>\n    from tensorflow.python import *\n  File \"/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py\", line 13, in <module>\n    from tensorflow.core.framework.graph_pb2 import *\n...\n  File \"/usr/local/lib/python2.7/site-packages/tensorflow/core/framework/tensor_shape_pb2.py\", line 22, in <module>\n    serialized_pb=_b('\\n,tensorflow/core/framework/tensor_shape.proto\\x12\\ntensorflow\\\"d\\n\\x10TensorShapeProto\\x12-\\n\\x03\\x64im\\x18\\x02 \\x03(\\x0b\\x32 .tensorflow.TensorShapeProto.Dim\\x1a!\\n\\x03\\x44im\\x12\\x0c\\n\\x04size\\x18\\x01 \\x01(\\x03\\x12\\x0c\\n\\x04name\\x18\\x02 \\x01(\\tb\\x06proto3')\nTypeError: __init__() got an unexpected keyword argument 'syntax'\n```\n\n这是由于安装了冲突的 protobuf 版本引起的, TensorFlow 需要的是 protobuf 3.0.0. 当前\n最好的解决方案是确保没有安装旧版本的 protobuf, 可以使用以下命令重新安装 protobuf 来解决\n冲突: \n\n```bash\nbrew reinstall --devel protobuf\n```\n\n> 原文：[Download and Setup](http://tensorflow.org/get_started/os_setup.md)  翻译：[@doc001](https://github.com/PFZheng)  校对：[@yangtze](https://github.com/sstruct)"
  },
  {
    "path": "SOURCE/how_tos/__init__.py",
    "content": ""
  },
  {
    "path": "SOURCE/how_tos/adding_an_op/__init__.py",
    "content": ""
  },
  {
    "path": "SOURCE/how_tos/adding_an_op/attr_examples.cc",
    "content": "#include <stdio.h>\r\n#include \"tensorflow/core/framework/op.h\"\r\n\r\nREGISTER_OP(\"RestrictedTypeExample\").Attr(\"t: {int32, float, bool}\");\r\n\r\nREGISTER_OP(\"NumberType\").Attr(\"t: numbertype\");\r\n\r\nREGISTER_OP(\"EnumExample\").Attr(\"e: {'apple', 'orange'}\");\r\n\r\nREGISTER_OP(\"MinIntExample\").Attr(\"a: int >= 2\");\r\n\r\nREGISTER_OP(\"TypeListExample\").Attr(\"a: list({int32, float}) >= 3\");\r\n\r\nREGISTER_OP(\"AttrDefaultExample\").Attr(\"i: int = 0\");\r\n\r\nREGISTER_OP(\"AttrDefaultExampleForAllTypes\")\r\n    .Attr(\"s: string = 'foo'\")\r\n    .Attr(\"i: int = 0\")\r\n    .Attr(\"f: float = 1.0\")\r\n    .Attr(\"b: bool = true\")\r\n    .Attr(\"ty: type = DT_INT32\")\r\n    .Attr(\"sh: shape = { dim { size: 1 } dim { size: 2 } }\")\r\n    .Attr(\"te: tensor = { dtype: DT_INT32 int_val: 5 }\")\r\n    .Attr(\"l_empty: list(int) = []\")\r\n    .Attr(\"l_int: list(int) = [2, 3, 5, 7]\");\r\n\r\nint main(int argc, char* argv[]) {\r\n  printf(\"All registered ops:\\n%s\\n\",\r\n         tensorflow::OpRegistry::Global()->DebugString(false).c_str());\r\n  return 0;\r\n}\r\n"
  },
  {
    "path": "SOURCE/how_tos/adding_an_op/fact_test.py",
    "content": "\"\"\"Test that user ops can be used as expected.\"\"\"\r\nfrom __future__ import print_function\r\n\r\nimport tensorflow.python.platform\r\n\r\nimport tensorflow as tf\r\n\r\n\r\nclass FactTest(tf.test.TestCase):\r\n\r\n  def test(self):\r\n    with self.test_session():\r\n      print(tf.user_ops.my_fact().eval())\r\n\r\n\r\nif __name__ == '__main__':\r\n  tf.test.main()\r\n"
  },
  {
    "path": "SOURCE/how_tos/adding_an_op/index.md",
    "content": "# Adding a New Op <a class=\"md-anchor\" id=\"AUTOGENERATED-adding-a-new-op\"></a>\r\n\r\nPREREQUISITES:\r\n\r\n* Some familiarity with C++.\r\n* Must have [downloaded TensorFlow source](../../get_started/introduction.md),\r\n  and be able to build it.\r\n\r\nIf you'd like to incorporate an operation that isn't covered by the existing\r\nlibrary, you can create a custom Op. To incorporate your custom Op, you'll need\r\nto:\r\n\r\n* Register the new Op in a C++ file. The Op registration is independent of the\r\n  implementation, and describes the semantics of how the Op is invoked. For\r\n  example, it defines the Op name, and specifies its inputs and outputs.\r\n* Implement the Op in C++. This implementation is called a \"kernel\", and there\r\n  can be multiple kernels for different architectures (e.g. CPUs, GPUs) or\r\n  input / output types.\r\n* Create a Python wrapper. This wrapper is the public API to create the Op. A\r\n  default wrapper is generated from the Op registration, which can be used\r\n  directly or added to.\r\n* Optionally, write a function to compute gradients for the Op.\r\n* Optionally, write a function that describes the input and output shapes\r\n  for the Op.  This allows shape inference to work with your Op.\r\n* Test the Op, typically in Python.\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Adding a New Op](#AUTOGENERATED-adding-a-new-op)\r\n* [Define the Op's interface](#define_interface)\r\n* [Implement the kernel for the Op](#AUTOGENERATED-implement-the-kernel-for-the-op)\r\n* [Generate the client wrapper](#AUTOGENERATED-generate-the-client-wrapper)\r\n  * [The Python Op wrapper](#AUTOGENERATED-the-python-op-wrapper)\r\n  * [The C++ Op wrapper](#AUTOGENERATED-the-c---op-wrapper)\r\n* [Verify it works](#AUTOGENERATED-verify-it-works)\r\n* [Validation](#Validation)\r\n* [Op registration](#AUTOGENERATED-op-registration)\r\n  * [Attrs](#Attrs)\r\n  * [Attr types](#AUTOGENERATED-attr-types)\r\n  * [Polymorphism](#Polymorphism)\r\n  * [Inputs and Outputs](#AUTOGENERATED-inputs-and-outputs)\r\n  * [Backwards compatibility](#AUTOGENERATED-backwards-compatibility)\r\n* [GPU Support](#mult-archs)\r\n* [Implement the gradient in Python](#AUTOGENERATED-implement-the-gradient-in-python)\r\n* [Implement a shape function in Python](#AUTOGENERATED-implement-a-shape-function-in-python)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## Define the Op's interface <a class=\"md-anchor\" id=\"define_interface\"></a>\r\n\r\nYou define the interface of an Op by registering it with the TensorFlow system.\r\nIn the registration, you specify the name of your Op, its inputs (types and\r\nnames) and outputs (types and names), as well as docstrings and\r\nany [attrs](#Attrs) the Op might require.\r\n\r\nTo see how this works, suppose you'd like to create an Op that takes a tensor of\r\n`int32`s and outputs a copy of the tensor, with all but the first element set to\r\nzero. Create file [`tensorflow/core/user_ops`][user_ops]`/zero_out.cc` and\r\nadd a call to the `REGISTER_OP` macro that defines the interface for such an Op:\r\n\r\n```c++\r\n#include \"tensorflow/core/framework/op.h\"\r\n\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Input(\"to_zero: int32\")\r\n    .Output(\"zeroed: int32\");\r\n```\r\n\r\nThis `ZeroOut` Op takes one tensor `to_zero` of 32-bit integers as input, and\r\noutputs a tensor `zeroed` of 32-bit integers.\r\n\r\n> A note on naming: The name of the Op should be unique and CamelCase.  Names\r\n> starting with an underscore (`_`) are reserved for internal use.\r\n\r\n## Implement the kernel for the Op <a class=\"md-anchor\" id=\"AUTOGENERATED-implement-the-kernel-for-the-op\"></a>\r\n\r\nAfter you define the interface, provide one or more implementations of the Op.\r\nTo create one of these kernels, create a class that extends `OpKernel` and\r\noverrides the `Compute` method. The `Compute` method provides one `context`\r\nargument of type `OpKernelContext*`, from which you can access useful things\r\nlike the input and output tensors.\r\n\r\nAdd your kernel to the file you created above. The kernel might look something\r\nlike this:\r\n\r\n```c++\r\n#include \"tensorflow/core/framework/op_kernel.h\"\r\n\r\nusing namespace tensorflow;\r\n\r\nclass ZeroOutOp : public OpKernel {\r\n public:\r\n  explicit ZeroOutOp(OpKernelConstruction* context) : OpKernel(context) {}\r\n\r\n  void Compute(OpKernelContext* context) override {\r\n    // Grab the input tensor\r\n    const Tensor& input_tensor = context->input(0);\r\n    auto input = input_tensor.flat<int32>();\r\n\r\n    // Create an output tensor\r\n    Tensor* output_tensor = NULL;\r\n    OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),\r\n                                                     &output_tensor));\r\n    auto output = output_tensor->template flat<int32>();\r\n\r\n    // Set all but the first element of the output tensor to 0.\r\n    const int N = input.size();\r\n    for (int i = 1; i < N; i++) {\r\n      output(i) = 0;\r\n    }\r\n\r\n    // Preserve the first input value if possible.\r\n    if (N > 0) output(0) = input(0);\r\n  }\r\n};\r\n```\r\n\r\nAfter implementing your kernel, you register it with the TensorFlow system. In\r\nthe registration, you specify different constraints under which this kernel\r\nwill run. For example, you might have one kernel made for CPUs, and a separate\r\none for GPUs.\r\n\r\nTo do this for the `ZeroOut` op, add the following to `zero_out.cc`:\r\n\r\n```c++\r\nREGISTER_KERNEL_BUILDER(Name(\"ZeroOut\").Device(DEVICE_CPU), ZeroOutOp);\r\n```\r\n\r\nOnce you\r\n[build and reinstall TensorFlow](../../get_started/os_setup.md#create-pip), the\r\nTensorflow system can reference and use the Op when requested.\r\n\r\n## Generate the client wrapper <a class=\"md-anchor\" id=\"AUTOGENERATED-generate-the-client-wrapper\"></a>\r\n### The Python Op wrapper <a class=\"md-anchor\" id=\"AUTOGENERATED-the-python-op-wrapper\"></a>\r\n\r\nPython op wrappers are created automatically in\r\n`bazel-genfiles/tensorflow/python/ops/gen_user_ops.py` for all ops placed in the\r\n[`tensorflow/core/user_ops`][user_ops] directory when you build Tensorflow.\r\nThose ops are imported into\r\n[`tensorflow/python/user_ops/user_ops.py`][python-user_ops] with the statement:\r\n\r\n```python\r\nfrom tensorflow.python.ops.gen_user_ops import *\r\n```\r\n\r\nYou may optionally use your own function instead.  To do this, you first hide\r\nthe generated code for that op by adding its name to the `hidden` list in the\r\n`\"user_ops\"` rule in\r\n[`tensorflow/python/BUILD`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/BUILD):\r\n\r\n```python\r\ntf_gen_op_wrapper_py(\r\n    name = \"user_ops\",\r\n    hidden = [\r\n        \"Fact\",\r\n    ],\r\n    require_shape_functions = False,\r\n)\r\n```\r\n\r\nList your op next to `\"Fact\"`.  Next you add your replacement function to\r\n[`tensorflow/python/user_ops/user_ops.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/user_ops/user_ops.py).\r\nTypically your function will call the generated function to actually add the op\r\nto the graph.  The hidden version of the generated function will be in the\r\n`gen_user_ops` package and start with an underscore (\"`_`\").  For example:\r\n\r\n```python\r\ndef my_fact():\r\n    \"\"\"Example of overriding the generated code for an Op.\"\"\"\r\n    return gen_user_ops._fact()\r\n```\r\n\r\n### The C++ Op wrapper <a class=\"md-anchor\" id=\"AUTOGENERATED-the-c---op-wrapper\"></a>\r\n\r\nC++ op wrappers are created automatically for all ops placed in the\r\n[`tensorflow/core/user_ops`][user_ops] directory, when you build Tensorflow. For\r\nexample, ops in `tensorflow/core/user_ops/zero_out.cc` will generate wrappers in\r\n`bazel-genfiles/tensorflow/cc/ops/user_ops.{h,cc}`.\r\n\r\nAll generated wrappers for user ops are automatically\r\nimported into [`tensorflow/cc/ops/standard_ops.h`][standard_ops-cc] with the\r\nstatement\r\n\r\n```c++\r\n#include \"tensorflow/cc/ops/user_ops.h\"\r\n```\r\n\r\n## Verify it works <a class=\"md-anchor\" id=\"AUTOGENERATED-verify-it-works\"></a>\r\n\r\nA good way to verify that you've successfully implemented your Op is to write a\r\ntest for it. Create the file\r\n`tensorflow/python/kernel_tests/zero_out_op_test.py` with the contents:\r\n\r\n```python\r\nimport tensorflow as tf\r\n\r\n\r\nclass ZeroOutTest(tf.test.TestCase):\r\n  def testZeroOut(self):\r\n    with self.test_session():\r\n      result = tf.user_ops.zero_out([5, 4, 3, 2, 1])\r\n      self.assertAllEqual(result.eval(), [5, 0, 0, 0, 0])\r\n```\r\n\r\nThen run your test:\r\n\r\n```sh\r\n$ bazel test tensorflow/python:zero_out_op_test\r\n```\r\n\r\n## Validation <a class=\"md-anchor\" id=\"Validation\"></a>\r\n\r\nThe example above assumed that the Op applied to a tensor of any shape.  What\r\nif it only applied to vectors?  That means adding a check to the above OpKernel\r\nimplementation.\r\n\r\n```c++\r\n  void Compute(OpKernelContext* context) override {\r\n    // Grab the input tensor\r\n    const Tensor& input_tensor = context->input(0);\r\n\r\n    OP_REQUIRES(context, TensorShapeUtils::IsVector(input_tensor.shape()),\r\n                errors::InvalidArgument(\"ZeroOut expects a 1-D vector.\"));\r\n    // ...\r\n  }\r\n```\r\n\r\nThis asserts that the input is a vector, and returns having set the\r\n`InvalidArgument` status if it isn't.  The\r\n[`OP_REQUIRES` macro][validation-macros] takes three arguments:\r\n\r\n*   The `context`, which can either be an `OpKernelContext` or\r\n    `OpKernelConstruction` pointer (see\r\n    [`tensorflow/core/framework/op_kernel.h`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op_kernel.h)),\r\n    for its `SetStatus()` method.\r\n*   The condition.  For example, there are functions for validating the shape\r\n    of a tensor in\r\n    [`tensorflow/core/public/tensor_shape.h`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/public/tensor_shape.h)\r\n*   The error itself, which is represented by a `Status` object, see\r\n    [`tensorflow/core/public/status.h`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/public/status.h). A\r\n    `Status` has both a type (frequently `InvalidArgument`, but see the list of\r\n    types) and a message.  Functions for constructing an error may be found in\r\n    [`tensorflow/core/lib/core/errors.h`][validation-macros].\r\n\r\nAlternatively, if you want to test whether a `Status` object returned from some\r\nfunction is an error, and if so return it, use\r\n[`OP_REQUIRES_OK`][validation-macros].  Both of these macros return from the\r\nfunction on error.\r\n\r\n## Op registration <a class=\"md-anchor\" id=\"AUTOGENERATED-op-registration\"></a>\r\n\r\n### Attrs <a class=\"md-anchor\" id=\"Attrs\"></a>\r\n\r\nOps can have attrs, whose values are set when the Op is added to a graph. These\r\nare used to configure the Op, and their values can be accessed both within the\r\nkernel implementation and in the types of inputs and outputs in the Op\r\nregistration. Prefer using an input instead of an attr when possible, since\r\ninputs are more flexible.  They can change every step, be set using a feed, etc.\r\nAttrs are used for things that can't be done with inputs: any configuration\r\nthat affects the signature (number or type of inputs or outputs) or that\r\ncan't change from step-to-step.\r\n\r\nYou define an attr when you register the Op, by specifying its name and type\r\nusing the `Attr` method, which expects a spec of the form:\r\n\r\n```\r\n<name>: <attr-type-expr>\r\n```\r\n\r\nwhere `<name>` begins with a letter and can be composed of alphanumeric\r\ncharacters and underscores, and `<attr-type-expr>` is a type expression of the\r\nform [described below](#attr-types)\r\n\r\nFor example, if you'd like the `ZeroOut` Op to preserve a user-specified index,\r\ninstead of only the 0th element, you can register the Op like so:\r\n\r\n<code class=\"lang-c++\"><pre>\r\nREGISTER\\_OP(\"ZeroOut\")\r\n    <b>.Attr(\"preserve\\_index: int\")</b>\r\n    .Input(\"to\\_zero: int32\")\r\n    .Output(\"zeroed: int32\");\r\n</pre></code>\r\n\r\nYour kernel can then access this attr in its constructor via the `context`\r\nparameter:\r\n\r\n<code class=\"lang-c++\"><pre>\r\nclass ZeroOutOp : public OpKernel {\r\n public:\r\n  explicit ZeroOutOp(OpKernelConstruction\\* context) : OpKernel(context) {<b>\r\n    // Get the index of the value to preserve\r\n    OP\\_REQUIRES\\_OK(context,\r\n                   context-&gt;GetAttr(\"preserve\\_index\", &preserve\\_index\\_));\r\n    // Check that preserve\\_index is positive\r\n    OP\\_REQUIRES(context, preserve\\_index_ &gt;= 0,\r\n                errors::InvalidArgument(\"Need preserve\\_index &gt;= 0, got \",\r\n                                        preserve\\_index_));\r\n  </b>}\r\n  void Compute(OpKernelContext\\* context) override {\r\n    // ...\r\n  }\r\n <b>private:\r\n  int preserve\\_index\\_;</b>\r\n};\r\n</pre></code>\r\n\r\nwhich can then be used in the `Compute` method:\r\n\r\n<code class=\"lang-c++\"><pre>\r\n  void Compute(OpKernelContext\\* context) override {\r\n    // ...\r\n<br/>    <b>// Check that preserve_index is in range\r\n    OP\\_REQUIRES(context, preserve\\_index_ &lt; input.dimension(0),\r\n                errors::InvalidArgument(\"preserve\\_index out of range\"));<br/>\r\n    </b>// Set all the elements of the output tensor to 0\r\n    const int N = input.size();\r\n    for (int i = 0; i < N; i++) {\r\n      output\\_flat(i) = 0;\r\n    }<br/>\r\n    <b>// Preserve the requested input value\r\n    output\\_flat(preserve\\_index\\_) = input(preserve\\_index\\_);</b>\r\n  }\r\n</pre></code>\r\n\r\n> To preserve [backwards compatibility](#backwards-compatibility), you should\r\n> specify a [default value](#default-values-constraints) when adding an attr to\r\n> an existing op:\r\n>\r\n> <code class=\"lang-c++\"><pre>\r\n> REGISTER\\_OP(\"ZeroOut\")\r\n>     <b>.Attr(\"preserve\\_index: int = 0\")</b>\r\n>     .Input(\"to_zero: int32\")\r\n>     .Output(\"zeroed: int32\");\r\n> </pre></code>\r\n\r\n### Attr types <a class=\"md-anchor\" id=\"AUTOGENERATED-attr-types\"></a>\r\n\r\nThe following types are supported in an attr:\r\n\r\n* `string`: Any sequence of bytes (not required to be UTF8).\r\n* `int`: A signed integer.\r\n* `float`: A floating point number.\r\n* `bool`: True or false.\r\n* `type`: One of the (non-ref) values of [`DataType`][DataTypeString].\r\n* `shape`: A [`TensorShapeProto`][TensorShapeProto].\r\n* `tensor`: A [`TensorProto`][TensorProto].\r\n* `list(<type>)`: A list of `<type>`, where `<type>` is one of the above types.\r\n  Note that `list(list(<type>))` is invalid.\r\n\r\nSee also: [`op_def_builder.cc:FinalizeAttr`][FinalizeAttr] for a definitive list.\r\n\r\n#### Default values & constraints <a class=\"md-anchor\" id=\"AUTOGENERATED-default-values---constraints\"></a>\r\n\r\nAttrs may have default values, and some types of attrs can have constraints. To\r\ndefine an attr with constraints, you can use the following `<attr-type-expr>`s:\r\n\r\n* `{'<string1>', '<string2>'}`: The value must be a string that has either the\r\n  value `<string1>` or `<string2>`.  The name of the type, `string`, is implied\r\n  when you use this syntax.  This emulates an enum:\r\n\r\n  ```c++\r\n  REGISTER_OP(\"EnumExample\")\r\n      .Attr(\"e: {'apple', 'orange'}\");\r\n  ```\r\n\r\n* `{<type1>, <type2>}`: The value is of type `type`, and must be one of\r\n  `<type1>` or `<type2>`, where `<type1>` and `<type2>` are supported\r\n  [tensor types](../../resources/dims_types.md#data-types).  You don't specify\r\n  that the type of the attr is `type`. This is implied when you have a list of\r\n  types in `{...}`.  For example, in this case the attr `t` is a type that must\r\n  be an `int32`, a `float`, or a `bool`:\r\n\r\n  ```c++\r\n  REGISTER_OP(\"RestrictedTypeExample\")\r\n      .Attr(\"t: {int32, float, bool}\");\r\n  ```\r\n\r\n* There are shortcuts for common type constraints:\r\n    * `numbertype`: Type `type` restricted to the numeric (non-string and\r\n      non-bool) types.\r\n    * `realnumbertype`: Like `numbertype` without complex types.\r\n    * `quantizedtype`: Like `numbertype` but just the quantized number types.\r\n\r\n    The specific lists of types allowed by these are defined by the functions\r\n    (like `NumberTypes()`) in\r\n    [`tensorflow/core/framework/types.h`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/types.h).\r\n    In this example the attr `t` must be one of the numeric types:\r\n\r\n    ```c++\r\n    REGISTER_OP(\"NumberType\")\r\n        .Attr(\"t: numbertype\");\r\n    ```\r\n\r\n    For this op:\r\n\r\n    ```python\r\n    tf.number_type(t=tf.int32)  # Valid\r\n    tf.number_type(t=tf.bool)   # Invalid\r\n    ```\r\n\r\n* `int >= <n>`: The value must be an int whose value is greater than or equal to\r\n  `<n>`, where `<n>` is a natural number.\r\n\r\n  For example, the following Op registration specifies that the attr `a` must\r\n  have a value that is at least `2`:\r\n\r\n  ```c++\r\n  REGISTER_OP(\"MinIntExample\")\r\n      .Attr(\"a: int >= 2\");\r\n  ```\r\n\r\n* `list(<type>) >= <n>`: A list of type `<type>` whose length is greater than\r\n  or equal to `<n>`.\r\n\r\n  For example, the following Op registration specifies that the attr `a` is a\r\n  list of types (either `int32` or `float`), and that there must be at least 3\r\n  of them:\r\n\r\n  ```c++\r\n  REGISTER_OP(\"TypeListExample\")\r\n      .Attr(\"a: list({int32, float}) >= 3\");\r\n  ```\r\n\r\nTo set a default value for an attr (making it optional in the generated code),\r\nadd `= <default>` to the end, as in:\r\n\r\n```c++\r\nREGISTER_OP(\"AttrDefaultExample\")\r\n    .Attr(\"i: int = 0\");\r\n```\r\n\r\nThe supported syntax of the default value is what would be used in the proto\r\nrepresentation of the resulting GraphDef definition.\r\n\r\nHere are examples for how to specify a default for all types:\r\n\r\n```c++\r\nREGISTER_OP(\"AttrDefaultExampleForAllTypes\")\r\n   .Attr(\"s: string = 'foo'\")\r\n   .Attr(\"i: int = 0\")\r\n   .Attr(\"f: float = 1.0\")\r\n   .Attr(\"b: bool = true\")\r\n   .Attr(\"ty: type = DT_INT32\")\r\n   .Attr(\"sh: shape = { dim { size: 1 } dim { size: 2 } }\")\r\n   .Attr(\"te: tensor = { dtype: DT_INT32 int_val: 5 }\")\r\n   .Attr(\"l_empty: list(int) = []\")\r\n   .Attr(\"l_int: list(int) = [2, 3, 5, 7]\");\r\n```\r\n\r\nNote in particular that the values of type `type` use [the `DT_*` names\r\nfor the types](../../resources/dims_types.md#data-types).\r\n\r\n### Polymorphism <a class=\"md-anchor\" id=\"Polymorphism\"></a>\r\n#### Type Polymorphism <a class=\"md-anchor\" id=\"type-polymorphism\"></a>\r\n\r\nFor ops that can take different types as input or produce different output\r\ntypes, you can specify [an attr](#attrs) in\r\n[an input or output type](#inputs-outputs) in the Op registration.  Typically\r\nyou would then register an `OpKernel` for each supported type.\r\n\r\nFor instance, if you'd like the `ZeroOut` Op to work on `float`s\r\nin addition to `int32`s, your Op registration might look like:\r\n\r\n<code class=\"lang-c++\"><pre>\r\nREGISTER\\_OP(\"ZeroOut\")\r\n    <b>.Attr(\"T: {float, int32}\")</b>\r\n    .Input(\"to_zero: <b>T</b>\")\r\n    .Output(\"zeroed: <b>T</b>\");\r\n</pre></code>\r\n\r\nYour Op registration now specifies that the input's type must be `float`, or\r\n`int32`, and that its output will be the same type, since both have type `T`.\r\n\r\n> A note on naming:{#naming} Inputs, outputs, and attrs generally should be\r\n> given snake_case names.  The one exception is attrs that are used as the type\r\n> of an input or in the type of an input. Those attrs can be inferred when the\r\n> op is added to the graph and so don't appear in the op's function.  For\r\n> example, this last definition of ZeroOut will generate a Python function that\r\n> looks like:\r\n>\r\n> ```python\r\n> def zero_out(to_zero, name=None):\r\n>   \"\"\"...\r\n>   Args:\r\n>     to_zero: A `Tensor`. Must be one of the following types:\r\n>         `float32`, `int32`.\r\n>     name: A name for the operation (optional).\r\n>\r\n>   Returns:\r\n>     A `Tensor`. Has the same type as `to_zero`.\r\n>   \"\"\"\r\n> ```\r\n>\r\n> If `to_zero` is passed an `int32` tensor, then `T` is automatically set to\r\n> `int32` (well, actually `DT_INT32`). Those inferred attrs are given\r\n> Capitalized or CamelCase names.\r\n>\r\n> Compare this with an op that has a type attr that determines the output\r\n> type:\r\n>\r\n> ```c++\r\n> REGISTER_OP(\"StringToNumber\")\r\n>     .Input(\"string_tensor: string\")\r\n>     .Output(\"output: out_type\")\r\n>     .Attr(\"out_type: {float, int32}\");\r\n>     .Doc(R\"doc(\r\n> Converts each string in the input Tensor to the specified numeric type.\r\n> )doc\");\r\n> ```\r\n>\r\n> In this case, the user has to specify the output type, as in the generated\r\n> Python:\r\n>\r\n> ```python\r\n> def string_to_number(string_tensor, out_type=None, name=None):\r\n>   \"\"\"Converts each string in the input Tensor to the specified numeric type.\r\n>\r\n>   Args:\r\n>     string_tensor: A `Tensor` of type `string`.\r\n>     out_type: An optional `tf.DType` from: `tf.float32, tf.int32`.\r\n>       Defaults to `tf.float32`.\r\n>     name: A name for the operation (optional).\r\n>\r\n>   Returns:\r\n>     A `Tensor` of type `out_type`.\r\n>   \"\"\"\r\n> ```\r\n\r\n<code class=\"lang-c++\"><pre>\r\n\\#include \"tensorflow/core/framework/op_kernel.h\"<br/>\r\nclass ZeroOut<b>Int32</b>Op : public OpKernel {\r\n  // as before\r\n};<br/>\r\nclass ZeroOut<b>Float</b>Op : public OpKernel {\r\n public:\r\n  explicit ZeroOut<b>Float</b>Op(OpKernelConstruction\\* context)\r\n      : OpKernel(context) {}<br/>\r\n  void Compute(OpKernelContext\\* context) override {\r\n    // Grab the input tensor\r\n    const Tensor& input\\_tensor = context-&gt;input(0);\r\n    auto input = input\\_tensor.flat&lt;<b>float</b>&gt;();<br/>\r\n    // Create an output tensor\r\n    Tensor* output = NULL;\r\n    OP\\_REQUIRES\\_OK(context,\r\n                   context-&gt;allocate\\_output(0, input_tensor.shape(), &output));\r\n    auto output\\_flat = output-&gt;template flat&lt;<b>float</b>&gt;();<br/>\r\n    // Set all the elements of the output tensor to 0\r\n    const int N = input.size();\r\n    for (int i = 0; i &lt; N; i++) {\r\n      output\\_flat(i) = 0;\r\n    }<br/>\r\n    // Preserve the first input value\r\n    if (N &gt; 0) output\\_flat(0) = input(0);\r\n  }\r\n};<br/><b>\r\n// Note that TypeConstraint&lt;int32&gt;(\"T\") means that attr \"T\" (defined\r\n// in the Op registration above) must be \"int32\" to use this template\r\n// instantiation.</b>\r\nREGISTER\\_KERNEL\\_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE\\_CPU)\r\n    <b>.TypeConstraint&lt;int32&gt;(\"T\"),</b>\r\n    ZeroOutOp<b>Int32</b>);\r\n<b>REGISTER\\_KERNEL\\_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE\\_CPU)\r\n    .TypeConstraint&lt;float&gt;(\"T\"),\r\n    ZeroOutFloatOp);\r\n</b></pre></code>\r\n\r\n> To preserve [backwards compatibility](#backwards-compatibility), you should\r\n> specify a [default value](#default-values-constraints) when adding an attr to\r\n> an existing op:\r\n>\r\n> <code class=\"lang-c++\"><pre>\r\n> REGISTER\\_OP(\"ZeroOut\")\r\n>   <b>.Attr(\"T: {float, int32} = DT_INT32\")</b>\r\n>   .Input(\"to_zero: T\")\r\n>   .Output(\"zeroed: T\")\r\n> </pre></code>\r\n\r\nLets say you wanted to add more types, say `double`:\r\n\r\n<code class=\"lang-c++\"><pre>\r\nREGISTER\\_OP(\"ZeroOut\")\r\n    <b>.Attr(\"T: {float, <b>double,</b> int32}\")</b>\r\n    .Input(\"to_zero: <b>T</b>\")\r\n    .Output(\"zeroed: <b>T</b>\");\r\n</pre></code>\r\n\r\nInstead of writing another `OpKernel` with redundant code as above, often you\r\nwill be able to use a C++ template instead.  You will still have one kernel\r\nregistration (`REGISTER\\_KERNEL\\_BUILDER` call) per overload.\r\n\r\n<code class=\"lang-c++\"><pre>\r\n<b>template &lt;typename T&gt;</b>\r\nclass ZeroOutOp : public OpKernel {\r\n public:\r\n  explicit ZeroOutOp(OpKernelConstruction\\* context) : OpKernel(context) {}<br/>\r\n  void Compute(OpKernelContext\\* context) override {\r\n    // Grab the input tensor\r\n    const Tensor& input\\_tensor = context-&gt;input(0);\r\n    auto input = input\\_tensor.flat<b>&lt;T&gt;</b>();<br/>\r\n    // Create an output tensor\r\n    Tensor* output = NULL;\r\n    OP\\_REQUIRES\\_OK(context,\r\n                   context-&gt;allocate\\_output(0, input_tensor.shape(), &output));\r\n    auto output\\_flat = output-&gt;template flat<b>&lt;T&gt;</b>();<br/>\r\n    // Set all the elements of the output tensor to 0\r\n    const int N = input.size();\r\n    for (int i = 0; i &lt; N; i++) {\r\n      output\\_flat(i) = 0;\r\n    }<br/>\r\n    // Preserve the first input value\r\n    if (N &gt; 0) output\\_flat(0) = input(0);\r\n  }\r\n};<br/>\r\n// Note that TypeConstraint&lt;int32&gt;(\"T\") means that attr \"T\" (defined\r\n// in the Op registration above) must be \"int32\" to use this template\r\n// instantiation.</b>\r\nREGISTER\\_KERNEL\\_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE\\_CPU)\r\n    .TypeConstraint&lt;int32&gt;(\"T\"),\r\n    <b>ZeroOutOp&lt;int32&gt;</b>);\r\nREGISTER\\_KERNEL\\_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE\\_CPU)\r\n    .TypeConstraint&lt;float&gt;(\"T\"),\r\n    <b>ZeroOutOp&lt;float&gt;</b>);\r\n<b>REGISTER\\_KERNEL\\_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE\\_CPU)\r\n    .TypeConstraint&lt;double&gt;(\"T\"),\r\n    ZeroOutOp&lt;double&gt;);\r\n</b></pre></code>\r\n\r\nIf you have more than a couple overloads, you can put the registration in a\r\nmacro.\r\n\r\n```c++\r\n#include \"tensorflow/core/framework/op_kernel.h\"\r\n\r\n#define REGISTER_KERNEL(type)                                       \\\r\n  REGISTER_KERNEL_BUILDER(                                          \\\r\n      Name(\"ZeroOut\").Device(DEVICE_CPU).TypeConstraint<type>(\"T\"), \\\r\n      ZeroOutOp<type>)\r\n\r\nREGISTER_KERNEL(int32);\r\nREGISTER_KERNEL(float);\r\nREGISTER_KERNEL(double);\r\n\r\n#undef REGISTER_KERNEL\r\n```\r\n\r\nDepending on the list of types you are registering the kernel for, you may be\r\nable to use a macro provided by\r\n[`tensorflow/core/framework/register_types.h`][register_types]:\r\n\r\n```c++\r\n#include \"tensorflow/core/framework/op_kernel.h\"\r\n#include \"tensorflow/core/framework/register_types.h\"\r\n\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Attr(\"T: realnumbertype\")\r\n    .Input(\"to_zero: T\")\r\n    .Output(\"zeroed: T\");\r\n\r\ntemplate <typename T>\r\nclass ZeroOutOp : public OpKernel { ... };\r\n\r\n#define REGISTER_KERNEL(type)                                       \\\r\n  REGISTER_KERNEL_BUILDER(                                          \\\r\n      Name(\"ZeroOut\").Device(DEVICE_CPU).TypeConstraint<type>(\"T\"), \\\r\n      ZeroOutOp<type>)\r\n\r\nTF_CALL_REAL_NUMBER_TYPES(REGISTER_KERNEL);\r\n\r\n#undef REGISTER_KERNEL\r\n```\r\n\r\n#### List Inputs and Outputs <a class=\"md-anchor\" id=\"list-input-output\"></a>\r\n\r\nIn addition to being able to accept or produce different types, ops can consume\r\nor produce a variable number of tensors.\r\n\r\nIn the next example, the attr `T` holds a *list* of types, and is used as the\r\ntype of both the input `in` and the output `out`.  The input and output are\r\nlists of tensors of that type (and the number and types of tensors in the output\r\nare the same as the input, since both have type `T`).\r\n\r\n```c++\r\nREGISTER_OP(\"PolymorphicListExample\")\r\n    .Attr(\"T: list(type)\")\r\n    .Input(\"in: T\")\r\n    .Output(\"out: T\");\r\n```\r\n\r\nYou can also place restrictions on what types can be specified in the list. In\r\nthis next case, the input is a list of `float` and `double` tensors. The Op\r\naccepts, for example, input types `(float, double, float)` and in that case the\r\noutput type would also be `(float, double, float)`.\r\n\r\n```c++\r\nREGISTER_OP(\"ListTypeRestrictionExample\")\r\n    .Attr(\"T: list({float, double})\")\r\n    .Input(\"in: T\")\r\n    .Output(\"out: T\");\r\n```\r\n\r\nIf you want all the tensors in a list to be of the same type, you might do\r\nsomething like:\r\n\r\n```c++\r\nREGISTER_OP(\"IntListInputExample\")\r\n    .Attr(\"N: int\")\r\n    .Input(\"in: N * int32\")\r\n    .Output(\"out: int32\");\r\n```\r\n\r\nThis accepts a list of `int32` tensors, and uses an `int` attr `N` to\r\nspecify the length of the list.\r\n\r\nThis can be made [type polymorphic](#type-polymorphism) as well.  In the next\r\nexample, the input is a list of tensors (with length `\"N\"`) of the same (but\r\nunspecified) type (`\"T\"`), and the output is a single tensor of matching type:\r\n\r\n```c++\r\nREGISTER_OP(\"SameListInputExample\")\r\n    .Attr(\"N: int\")\r\n    .Attr(\"T: type\")\r\n    .Input(\"in: N * T\")\r\n    .Output(\"out: T\");\r\n```\r\n\r\nBy default, tensor lists have a minimum length of 1. You can change that default\r\nusing\r\n[a `\">=\"` constraint on the corresponding attr](#default-values-constraints).\r\nIn this next example, the input is a list of at least 2 `int32` tensors:\r\n\r\n```c++\r\nREGISTER_OP(\"MinLengthIntListExample\")\r\n    .Attr(\"N: int >= 2\")\r\n    .Input(\"in: N * int32\")\r\n    .Output(\"out: int32\");\r\n```\r\n\r\nThe same syntax works with `\"list(type)\"` attrs:\r\n\r\n```c++\r\nREGISTER_OP(\"MinimumLengthPolymorphicListExample\")\r\n    .Attr(\"T: list(type) >= 3\")\r\n    .Input(\"in: T\")\r\n    .Output(\"out: T\");\r\n```\r\n\r\n### Inputs and Outputs <a class=\"md-anchor\" id=\"AUTOGENERATED-inputs-and-outputs\"></a>\r\n\r\nTo summarize the above, an Op registration can have multiple inputs and outputs:\r\n\r\n```c++\r\nREGISTER_OP(\"MultipleInsAndOuts\")\r\n    .Input(\"y: int32\")\r\n    .Input(\"z: float\")\r\n    .Output(\"a: string\")\r\n    .Output(\"b: int32\");\r\n```\r\n\r\nEach input or output spec is of the form:\r\n\r\n```\r\n<name>: <io-type-expr>\r\n```\r\n\r\nwhere `<name>` begins with a letter and can be composed of alphanumeric\r\ncharacters and underscores. `<io-type-expr>` is one of the following type\r\nexpressions:\r\n\r\n* `<type>`, where `<type>` is a supported input type (e.g. `float`, `int32`,\r\n  `string`). This specifies a single tensor of the given type.\r\n\r\n  See\r\n  [the list of supported Tensor types](../../resources/dims_types.md#data-types).\r\n\r\n  ```c++\r\n  REGISTER_OP(\"BuiltInTypesExample\")\r\n      .Input(\"integers: int32\")\r\n      .Input(\"complex_numbers: scomplex64\");\r\n  ```\r\n\r\n* `<attr-type>`, where `<attr-type>` is the name of an [Attr](#attrs) with type\r\n  `type` or `list(type)` (with a possible type restriction). This syntax allows\r\n  for [polymorphic ops](#Polymorphism).\r\n\r\n  ```c++\r\n  REGISTER_OP(\"PolymorphicSingleInput\")\r\n      .Attr(\"T: type\")\r\n      .Input(\"in: T);\r\n\r\n  REGISTER_OP(\"RestrictedPolymorphicSingleInput\")\r\n      .Attr(\"T: {int32, int64}\")\r\n      .Input(\"in: T);\r\n  ```\r\n\r\n  Referencing an attr of type `list(type)` allows you to accept a sequence of\r\n  tensors.\r\n\r\n  ```c++\r\n  REGISTER_OP(\"ArbitraryTensorSequenceExample\")\r\n      .Attr(\"T: list(type)\")\r\n      .Input(\"in: T\")\r\n      .Output(\"out: T\");\r\n\r\n  REGISTER_OP(\"RestrictedTensorSequenceExample\")\r\n      .Attr(\"T: list({int32, int64})\")\r\n      .Input(\"in: T\")\r\n      .Output(\"out: T\");\r\n  ```\r\n\r\n  Note that the number and types of tensors in the output `out` is the same as\r\n  in the input `in`, since both are of type `T`.\r\n\r\n* For a sequence of tensors with the same type: `<number> * <type>`, where\r\n  `<number>` is the name of an [Attr](#attrs) with type `int`.  The `<type>` can\r\n  either be\r\n  [a specific type like `int32` or `float`](../../resources/dims_types.md#data-types),\r\n  or the name of an attr with type `type`.  As an example of the first, this\r\n  Op accepts a list of `int32` tensors:\r\n\r\n  ```c++\r\n  REGISTER_OP(\"Int32SequenceExample\")\r\n      .Attr(\"NumTensors: int\")\r\n      .Input(\"in: NumTensors * int32\")\r\n  ```\r\n\r\n  Whereas this Op accepts a list of tensors of any type, as long as they are all\r\n  the same:\r\n\r\n  ```c++\r\n  REGISTER_OP(\"SameTypeSequenceExample\")\r\n      .Attr(\"NumTensors: int\")\r\n      .Attr(\"T: type\")\r\n      .Input(\"in: NumTensors * T\")\r\n  ```\r\n\r\n* For a reference to a tensor: `Ref(<type>)`, where `<type>` is one of the\r\n  previous types.\r\n\r\n> A note on naming: Any attr used in the type of an input will be inferred.  By\r\n> convention those inferred attrs use capital names (like `T` or `N`).\r\n> Otherwise inputs, outputs, and attrs have names like function parameters\r\n> (e.g. `num_outputs`).  For more details, see the\r\n> [earlier note on naming](#naming).\r\n\r\nFor more details, see\r\n[`tensorflow/core/framework/op_def_builder.h`][op_def_builder].\r\n\r\n### Backwards compatibility <a class=\"md-anchor\" id=\"AUTOGENERATED-backwards-compatibility\"></a>\r\n\r\nIn general, changes to specifications must be backwards-compatible: changing the\r\nspecification of an Op must not break prior serialized GraphDefs constructed\r\nfrom older specfications.\r\n\r\nThere are several ways to preserve backwards-compatibility.\r\n\r\n1. Any new attrs added to an operation must have default values defined, and\r\n   with that default value the Op must have the original behavior. To change an\r\n   operation from not polymorphic to polymorphic, you *must* give a default\r\n   value to the new type attr to preserve the original signature by default. For\r\n   example, if your operation was:\r\n\r\n   ```c++\r\n   REGISTER_OP(\"MyGeneralUnaryOp\")\r\n       .Input(\"in: float\")\r\n       .Output(\"out: float\");\r\n   ```\r\n\r\n   you can make it polymorphic in a backwards-compatible way using:\r\n\r\n   ```c++\r\n   REGISTER_OP(\"MyGeneralUnaryOp\")\r\n       .Input(\"in: T\")\r\n       .Output(\"out: T\")\r\n       .Attr(\"T: numerictype = float\");\r\n   ```\r\n\r\n1. You can safely make a constraint on an attr less restrictive.  For example,\r\n   you can change from `{int32, int64}` to `{int32, int64, float}` or from\r\n   `{\"apple\", \"orange\"}` to `{\"apple\", \"banana\", \"orange\"}`.\r\n\r\n1. Namespace any new Ops you create, by prefixing the Op names with something\r\n   unique to your project. This avoids having your Op colliding with any Ops\r\n   that might be included in future versions of Tensorflow.\r\n\r\n1. Plan ahead! Try to anticipate future uses for the Op. Some signature changes\r\n   can't be done in a compatible way (for example, adding an input, or making a\r\n   single input into a list).\r\n\r\nIf you cannot make your change to an operation backwards compatible, then\r\ncreate a new operation with a new name with the new semantics.\r\n\r\n## GPU Support <a class=\"md-anchor\" id=\"mult-archs\"></a>\r\n\r\nYou can implement different OpKernels and register one for CPU and another for\r\nGPU, just like you can [register kernels for different types](#Polymorphism).\r\nThere are several examples of kernels with GPU support in\r\n[`tensorflow/core/kernels/`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/).\r\nNotice some kernels have a CPU version in a `.cc` file, a GPU version in a file\r\nending in `_gpu.cu.cc`, and some code shared in common in a `.h` file.\r\n\r\nFor example, the [`pad` op](../../api_docs/python/array_ops.md#pad) has\r\neverything but the GPU kernel in [`tensorflow/core/kernels/pad_op.cc`][pad_op].\r\nThe GPU kernel is in\r\n[`tensorflow/core/kernels/pad_op_gpu.cu.cc`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/pad_op_gpu.cu.cc),\r\nand the shared code is a templated class defined in\r\n[`tensorflow/core/kernels/pad_op.h`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/pad_op.h).\r\nOne thing to note, even when the GPU kernel version of `pad` is used, it still\r\nneeds its `\"paddings\"` input in CPU memory.  To mark that inputs or outputs are\r\nkept on the CPU, add a `HostMemory()` call to the kernel registration, e.g.:\r\n\r\n```c++\r\n#define REGISTER_GPU_KERNEL(T)                         \\\r\n  REGISTER_KERNEL_BUILDER(Name(\"Pad\")                  \\\r\n                              .Device(DEVICE_GPU)      \\\r\n                              .TypeConstraint<T>(\"T\")  \\\r\n                              .HostMemory(\"paddings\"), \\\r\n                          PadOp<GPUDevice, T>)\r\n```\r\n\r\n## Implement the gradient in Python <a class=\"md-anchor\" id=\"AUTOGENERATED-implement-the-gradient-in-python\"></a>\r\n\r\nGiven a graph of ops, TensorFlow uses automatic differentiation\r\n(backpropagation) to add new ops representing gradients with respect to the\r\nexisting ops (see\r\n[Gradient Computation](../../api_docs/python/train.md#gradient-computation)).\r\nTo make automatic differentiation work for new ops, you must register a gradient\r\nfunction which computes gradients with respect to the ops' inputs given\r\ngradients with respect to the ops' outputs.\r\n\r\nMathematically, if an op computes \\\\(y = f(x)\\\\) the registered gradient op\r\nconverts gradients \\\\(\\partial / \\partial y\\\\) with respect to \\\\(y\\\\) into\r\ngradients \\\\(\\partial / \\partial x\\\\) with respect to \\\\(x\\\\) via the chain\r\nrule:\r\n\r\n$$\\frac{\\partial}{\\partial x}\r\n    = \\frac{\\partial}{\\partial y} \\frac{\\partial y}{\\partial x}\r\n    = \\frac{\\partial}{\\partial y} \\frac{\\partial f}{\\partial x}.$$\r\n\r\nIn the case of `ZeroOut`, only one entry in the input affects the output, so the\r\ngradient with respect to the input is a sparse \"one hot\" tensor.  This is\r\nexpressed as follows:\r\n\r\n```python\r\nfrom tensorflow.python.framework import ops\r\nfrom tensorflow.python.ops import array_ops\r\nfrom tensorflow.python.ops import sparse_ops\r\n\r\n@ops.RegisterGradient(\"ZeroOut\")\r\ndef _zero_out_grad(op, grad):\r\n  \"\"\"The gradients for `zero_out`.\r\n\r\n  Args:\r\n    op: The `zero_out` `Operation` that we are differentiating, which we can use\r\n      to find the inputs and outputs of the original op.\r\n    grad: Gradient with respect to the output of the `zero_out` op.\r\n\r\n  Returns:\r\n    Gradients with respect to the input of `zero_out`.\r\n  \"\"\"\r\n  to_zero = op.inputs[0]\r\n  shape = array_ops.shape(to_zero)\r\n  index = array_ops.zeros_like(shape)\r\n  first_grad = array_ops.reshape(grad, [-1])[0]\r\n  to_zero_grad = sparse_ops.sparse_to_dense(index, shape, first_grad, 0)\r\n  return [to_zero_grad]  # List of one Tensor, since we have one input\r\n```\r\n\r\nDetails about registering gradient functions with\r\n[`ops.RegisterGradient`](../../api_docs/python/framework.md#RegisterGradient):\r\n\r\n* For an op with one output, the gradient function will take an\r\n  [`Operation`](../../api_docs/python/framework.md#Operation) `op` and a\r\n  [`Tensor`](../../api_docs/python/framework.md#Tensor) `grad` and build new ops\r\n  out of the tensors\r\n  [`op.inputs[i]`](../../api_docs/python/framework.md#Operation.inputs),\r\n  [`op.outputs[i]`](../../api_docs/python/framework.md#Operation.outputs), and `grad`.  Information\r\n  about any attrs can be found via\r\n  [`op.get_attr`](../../api_docs/python/framework.md#Operation.get_attr).\r\n\r\n* If the op has multiple outputs, the gradient function will take `op` and\r\n  `grads`, where `grads` is a list of gradients with respect to each output.\r\n  The result of the gradient function must be a list of `Tensor` objects\r\n  representing the gradients with respect to each input.\r\n\r\n* If there is no well-defined gradient for some input, such as for integer\r\n  inputs used as indices, the corresponding returned gradient should be\r\n  `None`.  For example, for an op taking a floating point tensor `x` and an\r\n  integer index `i`, the gradient function would `return [x_grad, None]`.\r\n\r\n* If there is no meaningful gradient for the op at all, use\r\n  `ops.NoGradient(\"OpName\")` to disable automatic differentiation.\r\n\r\nNote that at the time the gradient function is called, only the data flow graph\r\nof ops is available, not the tensor data itself.  Thus, all computation must be\r\nperformed using other tensorflow ops, to be run at graph execution time.\r\n\r\n## Implement a shape function in Python <a class=\"md-anchor\" id=\"AUTOGENERATED-implement-a-shape-function-in-python\"></a>\r\n\r\nThe TensorFlow Python API has a feature called \"shape inference\" that provides\r\ninformation about the shapes of tensors without having to execute the\r\ngraph. Shape inference is supported by \"shape functions\" that are registered for\r\neach op type, and perform two roles: asserting that the shapes of the inputs are\r\ncompatible, and specifying the shapes for the outputs. A shape function is a\r\nPython function that takes an\r\n[`Operation`](../../api_docs/python/framework.md#Operation) as input, and\r\nreturns a list of\r\n[`TensorShape`](../../api_docs/python/framework.md#TensorShape) objects (one per\r\noutput of the op). To register a shape function, apply the\r\n[`tf.RegisterShape` decorator](../../api_docs/python/framework.md#RegisterShape)\r\nto a shape function. For example, the\r\n[`ZeroOut` op defined above](#define_interface) would have a shape function like\r\nthe following:\r\n\r\n```python\r\n@tf.RegisterShape(\"ZeroOut\"):\r\ndef _zero_out_shape(op):\r\n  \"\"\"Shape function for the ZeroOut op.\r\n\r\n  This is the unconstrained version of ZeroOut, which produces an output\r\n  with the same shape as its input.\r\n  \"\"\"\r\n  return [op.inputs[0].get_shape()]\r\n```\r\n\r\nA shape function can also constrain the shape of an input. For the version of\r\n[`ZeroOut` with a vector shape constraint](#Validation), the shape function\r\nwould be as follows:\r\n\r\n```python\r\n@tf.RegisterShape(\"ZeroOut\"):\r\ndef _zero_out_shape(op):\r\n  \"\"\"Shape function for the ZeroOut op.\r\n\r\n  This is the constrained version of ZeroOut, which requires the input to\r\n  have rank 1 (a vector).\r\n  \"\"\"\r\n  input_shape = op.inputs[0].get_shape().with_rank(1)\r\n  return [input_shape]\r\n```\r\n\r\nIf your op is [polymorphic with multiple inputs](#Polymorphism), use the\r\nproperties of the operation to determine the number of shapes to check:\r\n\r\n```\r\n@tf.RegisterShape(\"IntListInputExample\")\r\ndef _int_list_input_example_shape(op):\r\n  \"\"\"Shape function for the \"IntListInputExample\" op.\r\n\r\n  All inputs and the output are matrices of the same size.\r\n  \"\"\"\r\n  output_shape = tf.TensorShape(None)\r\n  for input in op.inputs:\r\n    output_shape = output_shape.merge_with(input.get_shape().with_rank(2))\r\n  return [output_shape]\r\n```\r\n\r\nSince shape inference is an optional feature, and the shapes of tensors may vary\r\ndynamically, shape functions must be robust to incomplete shape information for\r\nany of the inputs. The [`merge_with`](../../api_docs/python/framework.md)\r\nmethod allows the caller to assert that two shapes are the same, even if either\r\nor both of them do not have complete information. Shape functions are defined\r\nfor all of the\r\n[standard Python ops](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/),\r\nand provide many different usage examples.\r\n\r\n[core-array_ops]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/ops/array_ops.cc\r\n[python-user_ops]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/user_ops/user_ops.py\r\n[tf-kernels]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/\r\n[user_ops]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/user_ops/\r\n[pad_op]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/pad_op.cc\r\n[standard_ops-py]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/standard_ops.py\r\n[standard_ops-cc]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/cc/ops/standard_ops.h\r\n[python-BUILD]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/BUILD\r\n[validation-macros]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/lib/core/errors.h\r\n[op_def_builder]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op_def_builder.h\r\n[register_types]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/register_types.h\r\n[FinalizeAttr]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op_def_builder.cc#FinalizeAttr\r\n[DataTypeString]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/types.cc#DataTypeString\r\n[python-BUILD]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/BUILD\r\n[types-proto]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/types.proto\r\n[TensorShapeProto]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/tensor_shape.proto\r\n[TensorProto]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/tensor.proto\r\n"
  },
  {
    "path": "SOURCE/how_tos/adding_an_op/zero_out_1_test.py",
    "content": "\"\"\"Test for version 1 of the zero_out op.\"\"\"\r\n\r\nimport tensorflow.python.platform\r\n\r\nimport tensorflow as tf\r\nfrom tensorflow.g3doc.how_tos.adding_an_op import gen_zero_out_op_1\r\n\r\n\r\nclass ZeroOut1Test(tf.test.TestCase):\r\n\r\n  def test(self):\r\n    with self.test_session():\r\n      result = gen_zero_out_op_1.zero_out([5, 4, 3, 2, 1])\r\n      self.assertAllEqual(result.eval(), [5, 0, 0, 0, 0])\r\n\r\n\r\nif __name__ == '__main__':\r\n  tf.test.main()\r\n"
  },
  {
    "path": "SOURCE/how_tos/adding_an_op/zero_out_2_test.py",
    "content": "\"\"\"Test for version 2 of the zero_out op.\"\"\"\r\n\r\nimport tensorflow.python.platform\r\n\r\nimport tensorflow as tf\r\nfrom tensorflow.g3doc.how_tos.adding_an_op import gen_zero_out_op_2\r\nfrom tensorflow.g3doc.how_tos.adding_an_op import zero_out_grad_2\r\nfrom tensorflow.python.kernel_tests import gradient_checker\r\n\r\n\r\nclass ZeroOut2Test(tf.test.TestCase):\r\n\r\n  def test(self):\r\n    with self.test_session():\r\n      result = gen_zero_out_op_2.zero_out([5, 4, 3, 2, 1])\r\n      self.assertAllEqual(result.eval(), [5, 0, 0, 0, 0])\r\n\r\n  def test_grad(self):\r\n    with self.test_session():\r\n      shape = (5,)\r\n      x = tf.constant([5, 4, 3, 2, 1], dtype=tf.float32)\r\n      y = gen_zero_out_op_2.zero_out(x)\r\n      err = gradient_checker.ComputeGradientError(x, shape, y, shape)\r\n      self.assertLess(err, 1e-4)\r\n\r\n\r\nif __name__ == '__main__':\r\n  tf.test.main()\r\n"
  },
  {
    "path": "SOURCE/how_tos/adding_an_op/zero_out_3_test.py",
    "content": "\"\"\"Test for version 3 of the zero_out op.\"\"\"\r\n\r\nimport tensorflow.python.platform\r\n\r\nimport tensorflow as tf\r\nfrom tensorflow.g3doc.how_tos.adding_an_op import gen_zero_out_op_3\r\n\r\n\r\nclass ZeroOut3Test(tf.test.TestCase):\r\n\r\n  def test(self):\r\n    with self.test_session():\r\n      result = gen_zero_out_op_3.zero_out([5, 4, 3, 2, 1])\r\n      self.assertAllEqual(result.eval(), [5, 0, 0, 0, 0])\r\n\r\n  def testAttr(self):\r\n    with self.test_session():\r\n      result = gen_zero_out_op_3.zero_out([5, 4, 3, 2, 1], preserve_index=3)\r\n      self.assertAllEqual(result.eval(), [0, 0, 0, 2, 0])\r\n\r\n  def testNegative(self):\r\n    with self.test_session():\r\n      result = gen_zero_out_op_3.zero_out([5, 4, 3, 2, 1], preserve_index=-1)\r\n      with self.assertRaisesOpError(\"Need preserve_index >= 0, got -1\"):\r\n        result.eval()\r\n\r\n  def testLarge(self):\r\n    with self.test_session():\r\n      result = gen_zero_out_op_3.zero_out([5, 4, 3, 2, 1], preserve_index=17)\r\n      with self.assertRaisesOpError(\"preserve_index out of range\"):\r\n        result.eval()\r\n\r\n\r\nif __name__ == '__main__':\r\n  tf.test.main()\r\n"
  },
  {
    "path": "SOURCE/how_tos/adding_an_op/zero_out_grad_2.py",
    "content": "\"\"\"The gradient of the tutorial zero_out op.\"\"\"\r\n\r\nfrom tensorflow.python.framework import ops\r\nfrom tensorflow.python.ops import array_ops\r\nfrom tensorflow.python.ops import sparse_ops\r\n\r\n\r\n@ops.RegisterGradient(\"ZeroOut\")\r\ndef _zero_out_grad(op, grad):\r\n  \"\"\"The gradients for `zero_out`.\r\n\r\n  Args:\r\n    op: The `zero_out` `Operation` that we are differentiating, which we can use\r\n      to find the inputs and outputs of the original op.\r\n    grad: Gradient with respect to the output of the `zero_out` op.\r\n\r\n  Returns:\r\n    Gradients with respect to the input of `zero_out`.\r\n  \"\"\"\r\n  to_zero = op.inputs[0]\r\n  shape = array_ops.shape(to_zero)\r\n  index = array_ops.zeros_like(shape)\r\n  first_grad = array_ops.reshape(grad, [-1])[0]\r\n  to_zero_grad = sparse_ops.sparse_to_dense(index, shape, first_grad, 0)\r\n  return [to_zero_grad]  # List of one Tensor, since we have one input\r\n"
  },
  {
    "path": "SOURCE/how_tos/adding_an_op/zero_out_op_kernel_1.cc",
    "content": "#include \"tensorflow/core/framework/op.h\"\r\n#include \"tensorflow/core/framework/op_kernel.h\"\r\n\r\nusing namespace tensorflow;\r\n\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Input(\"to_zero: int32\")\r\n    .Output(\"zeroed: int32\")\r\n    .Doc(R\"doc(\r\nZeros out all but the first value of a Tensor.\r\n\r\nzeroed: A Tensor whose first value is identical to `to_zero`, and 0\r\n  otherwise.\r\n)doc\");\r\n\r\nclass ZeroOutOp : public OpKernel {\r\n public:\r\n  explicit ZeroOutOp(OpKernelConstruction* context) : OpKernel(context) {}\r\n\r\n  void Compute(OpKernelContext* context) override {\r\n    // Grab the input tensor\r\n    const Tensor& input_tensor = context->input(0);\r\n    auto input = input_tensor.flat<int32>();\r\n\r\n    // Create an output tensor\r\n    Tensor* output_tensor = NULL;\r\n    OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),\r\n                                                     &output_tensor));\r\n    auto output = output_tensor->template flat<int32>();\r\n\r\n    // Set all but the first element of the output tensor to 0.\r\n    const int N = input.size();\r\n    for (int i = 1; i < N; i++) {\r\n      output(i) = 0;\r\n    }\r\n\r\n    // Preserve the first input value.\r\n    if (N > 0) output(0) = input(0);\r\n  }\r\n};\r\n\r\nREGISTER_KERNEL_BUILDER(Name(\"ZeroOut\").Device(DEVICE_CPU), ZeroOutOp);\r\n"
  },
  {
    "path": "SOURCE/how_tos/adding_an_op/zero_out_op_kernel_2.cc",
    "content": "#include \"tensorflow/core/framework/op_kernel.h\"\r\n#include \"tensorflow/core/framework/register_types.h\"\r\n\r\nusing namespace tensorflow;\r\n\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Attr(\"T: realnumbertype\")\r\n    .Input(\"to_zero: T\")\r\n    .Output(\"zeroed: T\")\r\n    .Doc(R\"doc(\r\nZeros out all but the first value of a Tensor.\r\n\r\nzeroed: A Tensor whose first value is identical to `to_zero`, and 0\r\n  otherwise.\r\n)doc\");\r\n\r\nREGISTER_OP(\"ZeroOut2\")\r\n    .Attr(\"T: realnumbertype\")\r\n    .Input(\"to_zero: T\")\r\n    .Output(\"zeroed: T\")\r\n    .Doc(R\"doc(\r\nZeros out all but the first value of a Tensor.\r\n\r\nzeroed: A Tensor whose first value is identical to `to_zero`, and 0\r\n  otherwise.\r\n)doc\");\r\n\r\nREGISTER_OP(\"ZeroOut3\")\r\n    .Attr(\"T: realnumbertype\")\r\n    .Input(\"to_zero: T\")\r\n    .Output(\"zeroed: T\")\r\n    .Doc(R\"doc(\r\nZeros out all but the first value of a Tensor.\r\n\r\nzeroed: A Tensor whose first value is identical to `to_zero`, and 0\r\n  otherwise.\r\n)doc\");\r\n\r\ntemplate <typename T>\r\nclass ZeroOutOp : public OpKernel {\r\n public:\r\n  explicit ZeroOutOp(OpKernelConstruction* context) : OpKernel(context) {}\r\n\r\n  void Compute(OpKernelContext* context) override {\r\n    // Grab the input tensor\r\n    const Tensor& input_tensor = context->input(0);\r\n    auto input = input_tensor.flat<T>();\r\n\r\n    // Create an output tensor\r\n    Tensor* output = NULL;\r\n    OP_REQUIRES_OK(context,\r\n                   context->allocate_output(0, input_tensor.shape(), &output));\r\n    auto output_flat = output->template flat<T>();\r\n\r\n    // Set all the elements of the output tensor to 0\r\n    const int N = input.size();\r\n    for (int i = 0; i < N; i++) {\r\n      output_flat(i) = 0;\r\n    }\r\n\r\n    // Preserve the first input value\r\n    if (N > 0) output_flat(0) = input(0);\r\n  }\r\n};\r\n\r\nREGISTER_KERNEL_BUILDER(Name(\"ZeroOut\")\r\n                            .Device(DEVICE_CPU)\r\n                            .TypeConstraint<float>(\"T\"),\r\n                        ZeroOutOp<float>);\r\nREGISTER_KERNEL_BUILDER(Name(\"ZeroOut\")\r\n                            .Device(DEVICE_CPU)\r\n                            .TypeConstraint<double>(\"T\"),\r\n                        ZeroOutOp<double>);\r\nREGISTER_KERNEL_BUILDER(Name(\"ZeroOut\")\r\n                            .Device(DEVICE_CPU)\r\n                            .TypeConstraint<int>(\"T\"),\r\n                        ZeroOutOp<int>);\r\n\r\n#define REGISTER_KERNEL(type)                                       \\\r\n  REGISTER_KERNEL_BUILDER(                                          \\\r\n      Name(\"ZeroOut2\").Device(DEVICE_CPU).TypeConstraint<type>(\"T\"), \\\r\n      ZeroOutOp<type>)\r\n\r\nREGISTER_KERNEL(float);\r\nREGISTER_KERNEL(double);\r\nREGISTER_KERNEL(int32);\r\n\r\n#undef REGISTER_KERNEL\r\n\r\n#define REGISTER_KERNEL(type)                                       \\\r\n  REGISTER_KERNEL_BUILDER(                                          \\\r\n      Name(\"ZeroOut3\").Device(DEVICE_CPU).TypeConstraint<type>(\"T\"), \\\r\n      ZeroOutOp<type>)\r\n\r\nTF_CALL_REAL_NUMBER_TYPES(REGISTER_KERNEL);\r\n\r\n#undef REGISTER_KERNEL\r\n"
  },
  {
    "path": "SOURCE/how_tos/adding_an_op/zero_out_op_kernel_3.cc",
    "content": "#include \"tensorflow/core/framework/op.h\"\r\n#include \"tensorflow/core/framework/op_kernel.h\"\r\n\r\nusing namespace tensorflow;\r\n\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Attr(\"preserve_index: int = 0\")\r\n    .Input(\"to_zero: int32\")\r\n    .Output(\"zeroed: int32\");\r\n\r\nclass ZeroOutOp : public OpKernel {\r\n public:\r\n  explicit ZeroOutOp(OpKernelConstruction* context) : OpKernel(context) {\r\n    // Get the index of the value to preserve\r\n    OP_REQUIRES_OK(context,\r\n                   context->GetAttr(\"preserve_index\", &preserve_index_));\r\n    // Check that preserve\\_index is positive\r\n    OP_REQUIRES(context, preserve_index_ >= 0,\r\n                errors::InvalidArgument(\"Need preserve_index >= 0, got \",\r\n                                        preserve_index_));\r\n  }\r\n\r\n  void Compute(OpKernelContext* context) override {\r\n    // Grab the input tensor\r\n    const Tensor& input_tensor = context->input(0);\r\n    auto input = input_tensor.flat<int32>();\r\n\r\n    // Check that preserve_index is in range\r\n    OP_REQUIRES(context, preserve_index_ < input.dimension(0),\r\n                errors::InvalidArgument(\"preserve_index out of range\"));\r\n\r\n    // Create an output tensor\r\n    Tensor* output_tensor = NULL;\r\n    OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),\r\n                                                     &output_tensor));\r\n    auto output = output_tensor->template flat<int32>();\r\n\r\n    // Set all the elements of the output tensor to 0\r\n    const int N = input.size();\r\n    for (int i = 0; i < N; i++) {\r\n      output(i) = 0;\r\n    }\r\n\r\n    // Preserve the requested input value\r\n    output(preserve_index_) = input(preserve_index_);\r\n  }\r\n\r\n private:\r\n  int preserve_index_;\r\n};\r\n\r\nREGISTER_KERNEL_BUILDER(Name(\"ZeroOut\").Device(DEVICE_CPU), ZeroOutOp);\r\n"
  },
  {
    "path": "SOURCE/how_tos/adding_an_op.md",
    "content": "# 增加一个新 Op <a class=\"md-anchor\" id=\"AUTOGENERATED-adding-a-new-op\"></a>\r\n\r\n预备知识:\r\n\r\n* 对 C++ 有一定了解.\r\n* 已经[下载 TensorFlow 源代码](tensorflow-zh/SOURCE/get_started/introduction.md#source)并有能力编译它.\r\n\r\n如果现有的库没有涵盖你想要的操作, 你可以自己定制一个. 为了使定制的 Op 能够兼容原有的库\r\n, 你必须做以下工作:\r\n\r\n* 在一个 C++ 文件中注册新 Op. Op 的注册与实现是相互独立的. 在其注册时描述了 Op 该如何执行.\r\n例如, 注册 Op 时定义了 Op 的名字, 并指定了它的输入和输出.\r\n* 使用 C++ 实现 Op. 每一个实现称之为一个 \"kernel\", 可以存在多个 kernel, 以适配不同的架构\r\n(CPU, GPU 等)或不同的输入/输出类型.\r\n* 创建一个 Python 包装器（wrapper）. 这个包装器是创建 Op 的公开 API. 当注册 Op 时, 会自动生成一个默认\r\n默认的包装器. 既可以直接使用默认包装器, 也可以添加一个新的包装器. \r\n* (可选) 写一个函数计算 Op 的梯度.\r\n* (可选) 写一个函数, 描述 Op 的输入和输出 shape. 该函数能够允许从 Op 推断 shape.\r\n* 测试 Op, 通常使用 Pyhton。如果你定义了梯度，你可以使用Python的[GradientChecker](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/kernel_tests/gradient_checker.py)来测试它。\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n\r\n## 内容\r\n\r\n### [增加一个新 Op](#AUTOGENERATED-adding-a-new-op)\r\n\r\n* [定义 Op 的接口](#define_interface)\r\n* [为 Op 实现 kernel](#AUTOGENERATED-implement-the-kernel-for-the-op)\r\n* [生成客户端包装器](#AUTOGENERATED-generate-the-client-wrapper)\r\n  * [Python Op 包装器](#AUTOGENERATED-the-python-op-wrapper)\r\n  * [C++ Op 包装器](#AUTOGENERATED-the-c---op-wrapper)\r\n* [检查 Op 能否正常工作](#AUTOGENERATED-verify-it-works)\r\n* [验证条件](#Validation)\r\n* [Op 注册](#AUTOGENERATED-op-registration)\r\n  * [属性](#Attrs)\r\n  * [属性类型](#AUTOGENERATED-attr-types)\r\n  * [多态](#Polymorphism)\r\n  * [输入和输出](#AUTOGENERATED-inputs-and-outputs)\r\n  * [向后兼容性](#AUTOGENERATED-backwards-compatibility)\r\n* [GPU 支持](#mult-archs)\r\n* [使用 Python 实现梯度](#AUTOGENERATED-implement-the-gradient-in-python)\r\n* [使用 Python 实现 shape 函数](#AUTOGENERATED-implement-a-shape-function-in-python)\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## 定义 Op 的接口 <a class=\"md-anchor\" id=\"define_interface\"></a>\r\n\r\n向 TensorFlow 系统注册来定义 Op 的接口. 在注册时, 指定 Op 的名称, 它的输入(类型和名称)\r\n和输出(类型和名称), 和所需要任何 [属性](#Attrs)的文档说明.\r\n\r\n为了让你有直观的认识, 创建一个简单的 Op 作为例子. 该 Op 接受一个 `int32` 类型 tensor 作为\r\n输入, 输出这个 tensor 的一个副本, 副本与原 tensor 唯一的区别在于第一个元素被置为 0. 创建\r\n文件 `tensorflow/core/user_ops/zero_out.cc`, 并调用 `REGISTER_OP` 宏来定义 Op 的接口.\r\n\r\n```\r\n #include \"tensorflow/core/framework/op.h\"\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Input(\"to_zero: int32\")\r\n    .Output(\"zeroed: int32\");\r\n```\r\n\r\n`ZeroOut` Op 接受 32 位整型的 tensor `to_zero` 作为输入, 输出 32 位整型的 tensor `zeroed`.\r\n\r\n>命名的注意事项: Op 的名称必须是为唯一的, 并使用驼峰命名法. 以下划线 `_` 开始的名称保留为内部使用.\r\n\r\n## 为 Op 实现 kernel <a class=\"md-anchor\" id=\"AUTOGENERATED-implement-the-kernel-for-the-op\"></a>\r\n\r\n在定义接口之后, 提供一个或多个 Op 的实现. 为这些 kernel 的每一个创建一个对应的类, 继承\r\n`OpKernel`, 覆盖 `Compute` 方法. `Compute` 方法提供一个类型为 `OpKernelContext*` 的参数 `context`, 用于访问一些有用的信息, 例如输入和输出的 tensor. \r\n\r\n将 kernel 添加到刚才创建的文件中, kernel 看起来和下面的代码类似:\r\n\r\n```\r\n #include \"tensorflow/core/framework/op_kernel.h\"\r\nusing namespace tensorflow;\r\nclass ZeroOutOp : public OpKernel {\r\n public:\r\n  explicit ZeroOutOp(OpKernelConstruction* context) : OpKernel(context) {}\r\n  void Compute(OpKernelContext* context) override {\r\n    // 获取输入 tensor.\r\n    const Tensor& input_tensor = context->input(0);\r\n    auto input = input_tensor.flat<int32>();\r\n   // 创建一个输出 tensor.\r\n    Tensor* output_tensor = NULL;\r\n    OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),\r\n                                                     &output_tensor));\r\n    auto output = output_tensor->template flat<int32>();\r\n    // 设置 tensor 除第一个之外的元素均设为 0.\r\n    const int N = input.size();\r\n    for (int i = 1; i < N; i++) {\r\n      output(i) = 0;\r\n    }\r\n    // 尽可能地保留第一个元素的值.\r\n    if (N > 0) output(0) = input(0);\r\n  }\r\n};\r\n```\r\n\r\n实现 kernel 后, 将其注册到 TensorFlow 系统中. 注册时, 可以指定该 kernel 运行时的多个约束\r\n条件. 例如可以指定一个 kernel 在 CPU 上运行, 另一个在 GPU 上运行.\r\n\r\n将下列代码加入到 `zero_out.cc` 中, 注册 `ZeroOut` op:\r\n\r\n```\r\nREGISTER_KERNEL_BUILDER(Name(\"ZeroOut\").Device(DEVICE_CPU), ZeroOutOp);\r\n```\r\n\r\n一旦[创建和重新安装了 TensorFlow ](tensorflow-zh/SOURCE/get_started/os_setup.md#create-pip), \r\nTensorflow 系统可以在需要时引用和使用该 Op.\r\n\r\n## 生成客户端包装器 <a class=\"md-anchor\" id=\"AUTOGENERATED-generate-the-client-wrapper\"></a>\r\n\r\n### Python Op 包装器 <a class=\"md-anchor\" id=\"AUTOGENERATED-the-python-op-wrapper\"></a>\r\n\r\n当编译 TensorFlow 时, 所有放在 [`tensorflow/core/user_ops`][user_ops] 目录下\r\n的 Op 会自动在 `bazel-genfiles/tensorflow/python/ops/gen_user_ops.py` 文件\r\n中生成 Python Op 包装器. 通过以下声明, 把那些 Op 引入到 [`tensorflow/python/user_ops/user_ops.py`][python-user_ops]\r\n中:\r\n\r\n```python\r\nfrom tensorflow.python.ops.gen_user_ops import *\r\n```\r\n\r\n你可以选择性将部分函数替换为自己的实现. 为此, 首先要隐藏自动生成的代码, \r\n在 [`tensorflow/python/BUILD`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/BUILD)\r\n文件中, 将其名字添加到 `\"user_ops\"` 的 `hidden` 列表.\r\n\r\n```python\r\ntf_gen_op_wrapper_py(\r\n    name = \"user_ops\",\r\n    hidden = [\r\n        \"Fact\",\r\n    ],\r\n    require_shape_functions = False,\r\n)\r\n```\r\n\r\n紧接着 `\"Fact\"` 列出自己的 Op. 然后, 在 \r\n[`tensorflow/python/user_ops/user_ops.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/user_ops/user_ops.py)\r\n中添加你的替代实现函数. 通常, 替代实现函数也会调用自动生成函数来真正把 Op 添加\r\n到图中. 被隐藏的自动生成函数位于 `gen_user_ops` 包中, 名称多了一个下划线前缀\r\n(\"`_`\"). 例如:\r\n\r\n```python\r\ndef my_fact():\r\n    \"\"\"覆盖一个 Op 自动生成代码的示例.\"\"\"\r\n    return gen_user_ops._fact()\r\n```\r\n\r\n### C++ Op 包装器 <a class=\"md-anchor\" id=\"AUTOGENERATED-the-c---op-wrapper\"></a>\r\n\r\n当编译 TensorFlow 时, 所有 [`tensorflow/core/user_ops`][user_ops] 文件夹\r\n下的 Op 会自动创建 C++ Op 包装器. 例如, `tensorflow/core/user_ops/zero_out.cc` 中的 Op 会自动在 `bazel-genfiles/tensorflow/cc/ops/user_ops.{h,cc}`\r\n中生成包装器.\r\n\r\n[`tensorflow/cc/ops/standard_ops.h`][standard_ops-cc] 通过下述申明, \r\n导入用户自定义 Op 自动生成的包装器.\r\n\r\n```\r\n #include \"tensorflow/cc/ops/user_ops.h\"\r\n```\r\n\r\n## 检查 Op 能否正常工作 <a class=\"md-anchor\" id=\"AUTOGENERATED-verify-it-works\"></a>\r\n\r\n验证已经成功实现 Op 的方式是编写测试程序. 创建文件 \r\n`tensorflow/python/kernel_tests/zero_out_op_test.py`, \r\n包含以下内容:\r\n\r\n```python\r\nimport tensorflow as tf\r\nclass ZeroOutTest(tf.test.TestCase):\r\n  def testZeroOut(self):\r\n    with self.test_session():\r\n      result = tf.user_ops.zero_out([5, 4, 3, 2, 1])\r\n      self.assertAllEqual(result.eval(), [5, 0, 0, 0, 0])\r\n```\r\n\r\n然后运行测试:\r\n\r\n```sh\r\n$ bazel test tensorflow/python:zero_out_op_test\r\n```\r\n\r\n## 验证条件 <a class=\"md-anchor\" id=\"Validation\"></a>\r\n\r\n上述示例假定 Op 能够应用在任何 shape 的 tensor 上. 如果只想应用到 vector 上\r\n呢? \r\n这意味需要在上述 OpKernel 实现中添加相关的检查.\r\n\r\n```\r\n  void Compute(OpKernelContext* context) override {\r\n   // 获取输入 tensor\r\n    const Tensor& input_tensor = context->input(0);\r\n    OP_REQUIRES(context, TensorShapeUtils::IsVector(input_tensor.shape()),\r\n                errors::InvalidArgument(\"ZeroOut expects a 1-D vector.\"));\r\n    // ...\r\n  }\r\n```\r\n\r\nOP_REQUIRES 断言的输入是一个 vector, 如果不是 vector, 将设置 `InvalidArgument` 状态并返回.\r\n[`OP_REQUIRES` 宏][validation-macros] 有三个参数:\r\n\r\n*   `context`: 可以是一个 `OpKernelContext` 或 `OpKernelConstruction` 指针\r\n(参见 [`tensorflow/core/framework/op_kernel.h`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op_kernel.h)), \r\n其 `SetStatus()` 方法将被使用到.\r\n*   检查条件: [`tensorflow/core/public/tensor_shape.h`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/public/tensor_shape.h)\r\n中有一些验证 tensor shape 的函数.\r\n*   条件不满足时产生的错误: 错误用一个 `Status` 对象表示, 参见 \r\n[`tensorflow/core/public/status.h`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/public/status.h).\r\n`Status` 包含一个类型 (通常是 `InvalidArgument`, 但也可以是任何类型) 和一个消息. 构造\r\n一个错误的函数位于 [`tensorflow/core/lib/core/errors.h`][validation-macros] 中.\r\n\r\n如果想要测试一个函数返回的 `Status` 对象是否是一个错误, 可以使用 [`OP_REQUIRES_OK`][validation-macros].\r\n这些宏如果检测到错误, 会直接跳出函数, 终止函数执行.\r\n\r\n## Op 注册 <a class=\"md-anchor\" id=\"AUTOGENERATED-op-registration\"></a>\r\n\r\n### 属性 <a class=\"md-anchor\" id=\"Attrs\"></a>\r\n\r\nOp 可以有属性, 属性的值在 Op 添加到图中时被设置. 属性值用于配置 Op, 在 kernel 实现中, Op 注册的输入和输出类型中, 均可访问这些属性值. 尽可能地使用输入代替属性, 因为输入的灵活性更高, 例如可以在执行步骤中\r\n中被更改, 可以使用 feed 等等. 属性可用于实现一些输入无法做到的事情, 例如影响 Op 签名 (即输入输出的数量和类型)\r\n的配置或只读配置可以通过属性实现.\r\n\r\n注册 Op 时可以用 `Attr` 方法指定属性的名称和类型, 以此来定义一个属性, 形式如下:\r\n\r\n```\r\n<name>: <attr-type-expr>\r\n```\r\n\r\n`<name>` 必须以字母开头, 可以由数字, 字母, 下划线组成. `<attr-type-expr>` 是一个类型表达式,\r\n形式[如下](#attr-types):\r\n\r\n例如, 如果想要 `ZeroOut` Op 保存一个用户索引, 指示该 Op 不仅仅只有一个元素, 你可以注册 Op 如下:\r\n\r\n```\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Attr(\"preserve_index: int\")\r\n    .Input(\"to_zero: int32\")\r\n    .Output(\"zeroed: int32\");\r\n```\r\n\r\n你的 kernel 可以在构造函数里, 通过 `context` 参数访问这个属性:\r\n\r\n```\r\nclass ZeroOutOp : public OpKernel {\r\n public:\r\n  explicit ZeroOutOp(OpKernelConstruction * context) : OpKernel(context) {\r\n   // 获取欲保存的索引值\r\n    OP_REQUIRES_OK(context,\r\n                   context->GetAttr(\"preserve_index\", &preserve_index_));\r\n    // 检查 preserve_index 是否为正\r\n    OP_REQUIRES(context, preserve_index_ >= 0,\r\n                errors::InvalidArgument(\"Need preserve_index >= 0, got \",\r\n                                        preserve_index_));\r\n  }\r\n  void Compute(OpKernelContext* context) override {\r\n    // ...\r\n}\r\n private:\r\n  int preserve_index_;\r\n};\r\n```\r\n\r\n该值可以在 `Compute` 方法中被使用:\r\n\r\n```\r\nvoid Compute(OpKernelContext* context) override {\r\n    // ...\r\n   // 检查 preserve_index 范围是否合法\r\nOP_REQUIRES(context, preserve_index_ < input.dimension(0),\r\n                errors::InvalidArgument(\"preserve_index out of range\"));\r\n    // 设置输出 tensor 所有的元素值为 0\r\n   const int N = input.size();\r\n    for (int i = 0; i < N; i++) {\r\n      output_flat(i) = 0;\r\n    }\r\n    // 保存请求的输入值\r\n   output_flat(preserve_index_) = input(preserve_index_);\r\n  }\r\n```\r\n\r\n> 为了维持[向后兼容性](#backwards-compatibility), 将一个属性添加到一个已有的 Op 时,\r\n> 必须指定一个[默认值](#default-values-constraints):\r\n\r\n\r\n```\r\nREGISTER_OP(\"ZeroOut\")\r\n     .Attr(\"preserve_index: int = 0\")\r\n     .Input(\"to_zero: int32\")\r\n     .Output(\"zeroed: int32\");\r\n```\r\n\r\n### 属性类型 <a class=\"md-anchor\" id=\"AUTOGENERATED-attr-types\"></a>\r\n\r\n属性可以使用下面的类型:\r\n\r\n* `string`: 任何二进制字节流 (UTF8 不是必须的).\r\n* `int`: 一个有型整数.\r\n* `float`: 一个浮点数.\r\n* `bool`: 真或假.\r\n* `type`: [`DataType`][DataTypeString] 非引用类型之一.\r\n* `shape`: 一个 [`TensorShapeProto`][TensorShapeProto].\r\n* `tensor`: 一个 [`TensorProto`][TensorProto].\r\n* `list(<type>)`: `<type>` 列表, 其中 `<type>` 是上述类型之一.\r\n  注意 `list(list(<type>))` 是无效的.\r\n\r\n权威的列表以 [`op_def_builder.cc:FinalizeAttr`][FinalizeAttr] 为准.\r\n\r\n#### 默认值和约束条件 <a class=\"md-anchor\" id=\"AUTOGENERATED-default-values---constraints\"></a>\r\n\r\n属性可能有默认值, 一些类型的属性可以有约束条件. 为了定义一个有约束条件的属性, 你可以使用下列的 \r\n`<attr-type-expr>` 形式:\r\n\r\n* `{'<string1>', '<string2>'}`: 属性值必须是一个字符串, 取值可以为 `<string1>` 或 `<string2>`.\r\n值的语法已经暗示了值的类型为 `string`, 已经暗示了. 下述语句模拟了一个枚举值:\r\n\r\n```\r\nREGISTER_OP(\"EnumExample\")\r\n      .Attr(\"e: {'apple', 'orange'}\");\r\n```\r\n\r\n* `{<type1>, <type2>}`: 值是 `type` 类型, 且必须为 `<type1>` 或 `<type2>` 之一, 当然\r\n`<type1>` 和 `<type2>` 必须都是有效的 [tensor 类型](tensorflow-zh/SOURCE/resources/dims_types.md#data-types).\r\n你无须指定属性的类型为 `type`, 而是通过 `{...}` 语句给出一个类型列表. 例如, 在下面的例子里, \r\n属性 `t` 的类型必须为 `int32`, `float`, 或 `bool`:\r\n\r\n```\r\nREGISTER_OP(\"RestrictedTypeExample\")\r\n      .Attr(\"t: {int32, float, bool}\");\r\n```\r\n\r\n* 这里有一些常见类型约束条件的快捷方式:\r\n\r\n  * `numbertype`: 限制类型为数字类型, 即非 string 非 bool 的类型.\r\n  * `realnumbertype`: 与 `numbertype` 区别是不支持复杂类型.\r\n  * `quantizedtype`: 与 `numbertype` 区别是只支持量化数值 (quantized number type).\r\n    \r\n这些类型的列表在 [`tensorflow/core/framework/types.h`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/types.h)\r\n文件中通过函数定义 (如 `NumberTypes()`).\r\n本例中属性 `t` 必须为某种数字类型:\r\n\r\n```\r\nREGISTER_OP(\"NumberType\")\r\n        .Attr(\"t: numbertype\");\r\n```\r\n\r\n对于这个 Op:\r\n\r\n```python\r\ntf.number_type(t=tf.int32)  # 有效\r\ntf.number_type(t=tf.bool)   # 无效\r\n```\r\n\r\n* `int >= <n>`: 值必须是一个整数, 且取值大于等于 `<n>`, `<n>` 是一个自然数.\r\n\r\n例如, 下列 Op 注册操作指定了属性 `a` 的取值至少为 `2`.\r\n\r\n```\r\nREGISTER_OP(\"MinIntExample\")\r\n      .Attr(\"a: int >= 2\");\r\n```\r\n\r\n* `list(<type>) >= <n>`: 一个 `<type>` 类型列表, 列表长度必须大于等于 `<n>`.\r\n\r\n例如, 下面的 Op 注册操作指定属性 `a` 是一个列表, 列表中的元素类型是 `int32` 或  `float`列表长度至少为3.\r\n\r\n```\r\nREGISTER_OP(\"TypeListExample\")\r\n      .Attr(\"a: list({int32, float}) >= 3\");\r\n```\r\n\r\n通过添加 `= <default>` 到约束条件末尾, 给一个属性设置默认值 (使其在自动生成的代码里\r\n变成可选属性), 如下:\r\n\r\n```\r\nREGISTER_OP(\"AttrDefaultExample\")\r\n    .Attr(\"i: int = 0\");\r\n```\r\n\r\n默认值支持的语法将在最终 GraphDef 定义的 protobuf 表示中被使用.\r\n\r\n下面是给所有类型赋予默认值的例子:\r\n\r\n```\r\nREGISTER_OP(\"AttrDefaultExampleForAllTypes\")\r\n   .Attr(\"s: string = 'foo'\")\r\n   .Attr(\"i: int = 0\")\r\n   .Attr(\"f: float = 1.0\")\r\n   .Attr(\"b: bool = true\")\r\n   .Attr(\"ty: type = DT_INT32\")\r\n   .Attr(\"sh: shape = { dim { size: 1 } dim { size: 2 } }\")\r\n   .Attr(\"te: tensor = { dtype: DT_INT32 int_val: 5 }\")\r\n   .Attr(\"l_empty: list(int) = []\")\r\n   .Attr(\"l_int: list(int) = [2, 3, 5, 7]\");\r\n```\r\n\r\n请特别注意那些类型值里面包含的 [`DT_*` 名称](tensorflow-zh/SOURCE/resources/dims_types.md#data-types).\r\n\r\n### 多态 <a class=\"md-anchor\" id=\"Polymorphism\"></a>\r\n\r\n#### Type Polymorphism <a class=\"md-anchor\" id=\"type-polymorphism\"></a>\r\n\r\n对于那些可以使用不同类型输入或产生不同类型输出的 Op, 可以注册 Op 时为输入/输出类型里指定一个[属性](#attrs).\r\n一般紧接着, 会为每一个支持的类型注册一个 `OpKernel`.\r\n\r\n例如, 除了 `int32` 外, 想要 `ZeroOut` Op 支持 `float`, 注册代码如下:\r\n\r\n```\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Attr(\"T: {float, int32}\")\r\n    .Input(\"to_zero: <b>T</b>\")\r\n    .Output(\"zeroed: <b>T</b>\");\r\n```\r\n\r\n这段 Op 注册代码现在指定了输入的类型必须为 `float` 或 `int32`, 而且\r\n既然输入和输出制定了同样的类型 `T`, 输出也同样如此.\r\n\r\n> 一个命名建议:{#naming} 输入, 输出, 和属性通常使用 snake_case 命名法.\r\n> 唯一的例外是属性被用作输入类型或是输入类型的一部分. 当添加到图中时, 这些属性\r\n> 可以被推断出来, 因此不会出现在 Op 的函数里. 例如, 最后一个 ZeroOut 定义\r\n> 生成的 Python 函数如下:\r\n\r\n```python\r\ndef zero_out(to_zero, name=None):\r\n   \"\"\"...\r\n   参数:\r\n     to_zero: 一个 `Tensor`. 必须为下列类型之一:\r\n         `float32`, `int32`.\r\n     name: 操作的名字 (可选).\r\n\r\n   返回值:\r\n     一个 `Tensor`, 类型和 `to_zero` 一样.\r\n   \"\"\"\r\n```\r\n\r\n> 如果输入的 `to_zero` 是一个 `int32` 的tensor, 然后 `T` 将被自动\r\n> 设置为 `int32` (实际上是 `DT_INT32`). 那些推导出的属性的名称字母全大写\r\n> 或采用驼峰命名法.\r\n>\r\n> 下面是一个输出类型自动推断的例子, 读者可以对比一下:\r\n\r\n```\r\nREGISTER_OP(\"StringToNumber\")\r\n     .Input(\"string_tensor: string\")\r\n     .Output(\"output: out_type\")\r\n     .Attr(\"out_type: {float, int32}\");\r\n     .Doc(R\"doc(\r\n Converts each string in the input Tensor to the specified numeric type.\r\n )doc\");\r\n```\r\n\r\n> 在这种情况下, 用户需要在生成的 Python 代码中指定输出类型.\r\n\r\n```python\r\ndef string_to_number(string_tensor, out_type=None, name=None):\r\n   \"\"\"将输入 Tensor 中的每一个字符串转化成指定的数字类型\r\n\r\n   参数:\r\n     string_tensor: 一个 `string` 类型的 `Tensor`.\r\n     out_type: 一个可选的 `tf.DType`, 取值为 `tf.float32, tf.int32`.\r\n       默认值是 `tf.float32`.\r\n     name: 操作的名称 (可选).\r\n\r\n   返回值:\r\n     一个 `out_type` 类型的 `Tensor`.\r\n   \"\"\"\r\n```\r\n\r\n```\r\n #include \"tensorflow/core/framework/op_kernel.h\"\r\nclass ZeroOutInt32Op : public OpKernel {\r\n  // 和之前一样\r\n};\r\nclass ZeroOutFloatOp : public OpKernel {\r\n public:\r\n  explicit ZeroOutFloatOp(OpKernelConstruction * context)\r\n      : OpKernel(context) {}\r\n  void Compute(OpKernelContext * context) override {\r\n    // 获取输入 tensor\r\n    const Tensor& input_tensor = context->input(0);\r\n    auto input = input_tensor.flat<float>();\r\n    // 创建一个输出 tensor\r\n    Tensor * output = NULL;\r\n    OP_REQUIRES_OK(context,\r\n                    context->allocate_output(0, input_tensor.shape(), &output));\r\n    auto output_flat = output->template flat<float>();\r\n    // 设置输出 tensor 的所有元素为 0\r\n    const int N = input.size();\r\n    for (int i = 0; i &lt; N; i++) {\r\n      output_flat(i) = 0;\r\n    }<br/>\r\n    // 保留第一个输入值\r\n    if (N &gt; 0) output_flat(0) = input(0);\r\n  }\r\n};\r\n// 注意, TypeConstraint<int32>(\"T\") 意味着属性 \"T\" (在上面 Op 注册代码中\r\n// 定义的) 必须是 \"int32\", 才能实例化. \r\nREGISTER_KERNEL_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE_CPU)\r\n    .TypeConstraint&lt;int32&gt;(\"T\"),\r\n    ZeroOutOpInt32);\r\nREGISTER_KERNEL_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE_CPU)\r\n    .TypeConstraint<float>(\"T\"),\r\n    ZeroOutFloatOp);\r\n```\r\n\r\n> 为了保持[向后兼容性](#backwards-compatibility), 你在为一个\r\n> 已有的 op 添加属性时, 必须指定一个[默认值](#default-values-constraints):\r\n\r\n```\r\nREGISTER_OP(\"ZeroOut\")\r\n  .Attr(\"T: {float, int32} = DT_INT32\")\r\n  .Input(\"to_zero: T\")\r\n  .Output(\"zeroed: T\")\r\n```\r\n\r\n如果需要添加更多类型, 例如 `double`:\r\n\r\n```\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Attr(\"T: {float, double, int32}\")\r\n    .Input(\"to_zero: T\")\r\n    .Output(\"zeroed: T\");\r\n```\r\n\r\n为了避免为新增的类型写冗余的 `OpKernel` 代码, 通常可以写一个 C++ 模板作为替代. \r\n当然, 仍然需要为每一个重载版本定义一个 keneral 注册 (`REGISTER\\_KERNEL\\_BUILDER` 调用).\r\n\r\n```\r\ntemplate <typename T>;\r\nclass ZeroOutOp : public OpKernel {\r\n public:\r\n    explicit ZeroOutOp(OpKernelConstruction* context) : OpKernel(context) {}\r\n  void Compute(OpKernelContext* context) override {\r\n    // 获取输入 tensor\r\n     const Tensor& input_tensor = context->input(0);\r\n    auto input = input_tensor.flat<T>();\r\n    // 创建一个输出 tensor\r\n      Tensor* output = NULL;\r\n    OP_REQUIRES_OK(context,\r\n                   context->allocate_output(0, input_tensor.shape(), &output));\r\n    auto output_flat = output->template flat<T>();\r\n    // 设置输出 tensor 的所有元素为 0\r\n   const int N = input.size();\r\n    for (int i = 0; i < N; i++) {\r\n      output_flat(i) = 0;\r\n    }\r\n    // Preserve the first input value\r\n    if (N > 0) output_flat(0) = input(0);\r\n  }\r\n};\r\n};<br/>\r\n// 注意, TypeConstraint<int32>(\"T\") 意味着属性 \"T\" (在上面 Op 注册代码中\r\n// 定义的) 必须是 \"int32\", 才能实例化. </b>\r\nREGISTER_KERNEL_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE_CPU)\r\n    .TypeConstraint<int32>(\"T\"),\r\n    ZeroOutOp<int32>);\r\nREGISTER_KERNEL_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE_CPU)\r\n    .TypeConstraint<float>(\"T\"),\r\n    ZeroOutOp<float>);\r\nREGISTER_KERNEL_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE_CPU)\r\n    .TypeConstraint<double>(\"T\"),\r\n    ZeroOutOp<double>);\r\n```\r\n\r\n如果有很多重载版本, 可以将注册操作通过一个宏来实现.\r\n\r\n```\r\n #include \"tensorflow/core/framework/op_kernel.h\"\r\n #define REGISTER_KERNEL(type)                                       \\\r\n  REGISTER_KERNEL_BUILDER(                                          \\\r\n      Name(\"ZeroOut\").Device(DEVICE_CPU).TypeConstraint<type>(\"T\"), \\\r\n      ZeroOutOp<type>)\r\nREGISTER_KERNEL(int32);\r\nREGISTER_KERNEL(float);\r\nREGISTER_KERNEL(double);\r\n #undef REGISTER_KERNEL\r\n```\r\n\r\n取决于注册 kernel 使用哪些类型, 你可能可以使用[`tensorflow/core/framework/register_types.h`][register_types]\r\n提供的宏:\r\n\r\n```\r\n #include \"tensorflow/core/framework/op_kernel.h\"\r\n #include \"tensorflow/core/framework/register_types.h\"\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Attr(\"T: realnumbertype\")\r\n    .Input(\"to_zero: T\")\r\n    .Output(\"zeroed: T\");\r\ntemplate <typename T>\r\nclass ZeroOutOp : public OpKernel { ... };\r\n #define REGISTER_KERNEL(type)                                       \\\r\n  REGISTER_KERNEL_BUILDER(                                          \\\r\n      Name(\"ZeroOut\").Device(DEVICE_CPU).TypeConstraint<type>(\"T\"), \\\r\n      ZeroOutOp<type>)\r\nTF_CALL_REAL_NUMBER_TYPES(REGISTER_KERNEL);\r\n #undef REGISTER_KERNEL\r\n```\r\n\r\n#### 列表输入和输出 <a class=\"md-anchor\" id=\"list-input-output\"></a>\r\n\r\n除了能够使用不同类型的 tensor 作为输入或输出, Op 还支持使用多个 tensor 作为输入或输出.\r\n\r\n在接下来的例子里, 属性 `T` 存储了一个类型*列表*, 并同时作为输入 `in` 和输出 `out` 的类型.\r\n输入和输出均为指定类型的 tensor 列表. 既然输入和输出的类型均为 `T`, 它们的 tensor 数量和类型\r\n是一致的.\r\n\r\n```\r\nREGISTER_OP(\"PolymorphicListExample\")\r\n    .Attr(\"T: list(type)\")\r\n    .Input(\"in: T\")\r\n    .Output(\"out: T\");\r\n```\r\n\r\n可以为列表中可存放的类型设置约束条件. 在下一个例子中, 输入是 `float` 和 \r\n`double` 类型的 tensor 列表. 例如, 这个 Op 可接受的\r\n输入类型为 `(float, double, float)` 的数据, 且在此情况下, 输出类型同样\r\n为 `(float, double, float)`.\r\n\r\n```\r\nREGISTER_OP(\"ListTypeRestrictionExample\")\r\n    .Attr(\"T: list({float, double})\")\r\n    .Input(\"in: T\")\r\n    .Output(\"out: T\");\r\n```\r\n\r\n如果想要一个列表中的所有 tensor 是同一类型, 你需要写下列代码:\r\n\r\n```\r\nREGISTER_OP(\"IntListInputExample\")\r\n    .Attr(\"N: int\")\r\n    .Input(\"in: N * int32\")\r\n    .Output(\"out: int32\");\r\n```\r\n\r\n这段代码接受 `int32` tensor 列表, 并用一个 `int` 属性 `N`\r\n来指定列表的长度.\r\n\r\n这也可用于[类型推断](#type-polymorphism). 在下一个例子中,\r\n输入是一个 tensor 列表, 长度为 `\"N\"`, 类型为 `\"T\"`, 输出是单个 `\"T\"` 的 tensor:\r\n\r\n```\r\nREGISTER_OP(\"SameListInputExample\")\r\n    .Attr(\"N: int\")\r\n    .Attr(\"T: type\")\r\n    .Input(\"in: N * T\")\r\n    .Output(\"out: T\");\r\n```\r\n\r\n默认情况下, tensor 列表的最小长度为1. 这个约束条件可以通过\r\n[为指定的属性增加一个 `\">=\"` 约束](#default-values-constraints)来变更:\r\n\r\n```\r\nREGISTER_OP(\"MinLengthIntListExample\")\r\n    .Attr(\"N: int >= 2\")\r\n    .Input(\"in: N * int32\")\r\n    .Output(\"out: int32\");\r\n```\r\n\r\n同样的语法也适用于 `\"list(type)\"` 属性:\r\n\r\n```\r\nREGISTER_OP(\"MinimumLengthPolymorphicListExample\")\r\n    .Attr(\"T: list(type) >= 3\")\r\n    .Input(\"in: T\")\r\n    .Output(\"out: T\");\r\n```\r\n\r\n### 输入和输出 <a class=\"md-anchor\" id=\"AUTOGENERATED-inputs-and-outputs\"></a>\r\n\r\n总结一下上述内容, 一个 Op 注册操作可以指定多个输入和输出:\r\n\r\n```\r\nREGISTER_OP(\"MultipleInsAndOuts\")\r\n    .Input(\"y: int32\")\r\n    .Input(\"z: float\")\r\n    .Output(\"a: string\")\r\n    .Output(\"b: int32\");\r\n```\r\n\r\n每一个输入或输出形式如下:\r\n\r\n```\r\n<name>: <io-type-expr>\r\n```\r\n\r\n其中, `<name>` 以字母打头, 且只能由数字, 字母和下划线组成. `<io-type-expr>` 可以是\r\n下列类型表达式之一:\r\n\r\n* `<type>`, 一个合法的输入类型, 如 `float`, `int32`, `string`. 这可用于指定给定类型的单个 tensor.\r\n\r\n参见[合法 Tensor 类型列表](tensorflow-zh/SOURCE/resources/dims_types.md#data-types).\r\n\r\n```\r\nREGISTER_OP(\"BuiltInTypesExample\")\r\n      .Input(\"integers: int32\")\r\n      .Input(\"complex_numbers: scomplex64\");\r\n```\r\n\r\n* `<attr-type>`, 一个[属性](#attrs)和一个类型 `type` 或类型列表 `list(type)`(可能\r\n  包含类型限制). 该语法可实现[多态 Op](#Polymorphism).\r\n\r\n```\r\nREGISTER_OP(\"PolymorphicSingleInput\")\r\n      .Attr(\"T: type\")\r\n      .Input(\"in: T);\r\nREGISTER_OP(\"RestrictedPolymorphicSingleInput\")\r\n      .Attr(\"T: {int32, int64}\")\r\n      .Input(\"in: T);\r\n```\r\n\r\n将属性的类型设置为 `list(type)` 将允许你接受一个序列的 tensor.\r\n\r\n```\r\nREGISTER_OP(\"ArbitraryTensorSequenceExample\")\r\n      .Attr(\"T: list(type)\")\r\n      .Input(\"in: T\")\r\n      .Output(\"out: T\");\r\nREGISTER_OP(\"RestrictedTensorSequenceExample\")\r\n      .Attr(\"T: list({int32, int64})\")\r\n      .Input(\"in: T\")\r\n      .Output(\"out: T\");\r\n```\r\n\r\n注意, 输入和输出均为 `T`, 意味着输入和输出的类型与数量均相同.\r\n\r\n* `<number> * <type>`, 一组拥有相同类型的 tensor, `<number>` 是一个 `int` 类型属性的名称.\r\n  `<type>` 可以是[一个类似于 `int32` 和 `float` 的特定类型](tensorflow-zh/SOURCE/resources/dims_types.md#data-types), \r\n  或者一个 `type` 类型属性的名字. 前者的例子如下, 该例子接受一个 `int32` tensor 列表作为 Op 输入:\r\n\r\n```\r\nREGISTER_OP(\"Int32SequenceExample\")\r\n      .Attr(\"NumTensors: int\")\r\n      .Input(\"in: NumTensors * int32\")\r\n```\r\n\r\n后者的例子如下, 该例子接受一个泛型 tensor 列表作为 Op 输入:\r\n\r\n```\r\nREGISTER_OP(\"SameTypeSequenceExample\")\r\n      .Attr(\"NumTensors: int\")\r\n      .Attr(\"T: type\")\r\n      .Input(\"in: NumTensors * T\")\r\n```\r\n\r\n* Tensor 的引用表示为 `Ref(<type>)`, 其中 `<type>` 是上述类型之一.\r\n\r\n> 一个命名建议: 当使用属性表示一个输入的类型时, 该类型可以被推断出来. 实现该特性, 将需要推断\r\n> 的类型用大写名称表示 (如 `T` 或 `N`), 其它的输入, 输出, 和属性像使用函数参数一样使用这些\r\n> 大写名称. 参见之前的[命名建议](#naming)章节查看更多细节.\r\n\r\n更多细节参见 [`tensorflow/core/framework/op_def_builder.h`][op_def_builder].\r\n\r\n### 向后兼容性 <a class=\"md-anchor\" id=\"AUTOGENERATED-backwards-compatibility\"></a>\r\n\r\n通常, 对规范的改变必须保持向后兼容性: Op 使用新规范后, 需保证使用旧规范构造的序列化 GraphDef 仍能正确工作.\r\n\r\n下面是几种保持向后兼容性的方式:\r\n\r\n1. 任何添加到 Op 的新属性必须有默认值, 且默认值下的行为有明确定义. 将一个非多态的操作变为多态操作,\r\n   你*必须*为新的类型属性赋予默认值, 以保持原始的函数签名. 例如, 有如下操作:\r\n\r\n```\r\nREGISTER_OP(\"MyGeneralUnaryOp\")\r\n       .Input(\"in: float\")\r\n       .Output(\"out: float\");\r\n```\r\n\r\n可以通过下述方式将其变为多态, 且保持向后兼容性:\r\n\r\n```\r\nREGISTER_OP(\"MyGeneralUnaryOp\")\r\n       .Input(\"in: T\")\r\n       .Output(\"out: T\")\r\n       .Attr(\"T: numerictype = float\");\r\n```\r\n\r\n1.放宽一个属性的约束条件是安全的. 例如, 你可以将 `{int32, int64}` 变为 `{int32, int64, float}`,\r\n   或者, 将 `{\"apple\", \"orange\"}` 变为 `{\"apple\", \"banana\", \"orange\"}`.\r\n\r\n2.通过给 Op 名称添加一些项目中唯一的标识作为前缀, 来为新建的 Op 添加命名空间. 命名空间\r\n   可以预防你的 Op 与 TensorFlow 未来版本里的内置 Op 产生命名冲突.\r\n\r\n3.超前计划! 尝试着去预测 Op 未来的的用途, 超前设计, 毕竟, 一些签名的变更无法保证兼容性\r\n   (例如, 增加新的输入, 或将原来的单元素输入变成一个列表).\r\n\r\n如果不能以兼容的方式改变一个操作, 那就创建一个全新的操作, 来实现所需功能.\r\n\r\n## GPU 支持 <a class=\"md-anchor\" id=\"mult-archs\"></a>\r\n\r\n你可以实现不同的 OpKernel, 将其中之一注册到 GPU, 另一个注册到 GPU, 正如[为不同的类型注册 kernel ](#Polymorphism)一样. \r\n[`tensorflow/core/kernels/`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/) 中有一些 GPU 支持的例子. \r\n注意, 一些 kernel 的 CPU 版本位于 `.cc` 文件, GPU 版本位于 `_gpu.cu.cc` 文件, 共享的代码位于 `.h` 文件.\r\n\r\n例如, [`pad` op](tensorflow-zh/SOURCE/api_docs/python/array_ops.md#pad) 除了 GPU kernel 外的其它代码\r\n均在 [`tensorflow/core/kernels/pad_op.cc`][pad_op] 中. GPU kernel 位于 [`tensorflow/core/kernels/pad_op_gpu.cu.cc`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/pad_op_gpu.cu.cc), \r\n共享的一个模板类代码定义在 [`tensorflow/core/kernels/pad_op.h`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/pad_op.h).\r\n需要注意的事情是, 即使使用 `pad` 的 GPU 版本时, 仍然需要将 `\"paddings\"` 输入放置到内存中.\r\n为了实现这一点, 将输入或输出标记为必须保存在内存中, 为 kernel 注册一个 `HostMemory()` 调用.\r\n如下:\r\n\r\n```\r\n #define REGISTER_GPU_KERNEL(T)                         \\\r\nREGISTER_KERNEL_BUILDER(Name(\"Pad\")                  \\\r\n                              .Device(DEVICE_GPU)      \\\r\n                              .TypeConstraint<T>(\"T\")  \\\r\n                              .HostMemory(\"paddings\"), \\\r\n                          PadOp<GPUDevice, T>)\r\n```\r\n\r\n## 使用 Python 实现梯度 <a class=\"md-anchor\" id=\"AUTOGENERATED-implement-the-gradient-in-python\"></a>\r\n\r\n给定一个 Op 组成的图, TensorFlow 使用自动微分 (反向传播) 来添加新的 Op 以表示梯度运算, 同时\r\n不影响已有的 Op (参见[梯度运算](tensorflow-zh/SOURCE/api_docs/python/train.md#gradient-computation)).\r\n为了使自动微分能够与新的 Op 协同工作, 必须注册一个梯度函数, 从 Op 的输入计算梯度, 并返回代表\r\n梯度值的输出.\r\n\r\n数学上, 如果一个 Op 计算 \\\\(y = f(x)\\\\), 注册的梯度 Op 通过以下链式法则, 将 \\\\(\\partial / \\partial y\\\\)\r\n的梯度运算转化为 \\\\(\\partial / \\partial x\\\\) 的梯度运算.\r\n\r\n$$\\frac{\\partial}{\\partial x}\r\n    = \\frac{\\partial}{\\partial y} \\frac{\\partial y}{\\partial x}\r\n    = \\frac{\\partial}{\\partial y} \\frac{\\partial f}{\\partial x}.$$\r\n\r\n在 `ZeroOut` 的例子中, 输入中只有一个项会影响输出, 所以, 代表输入的梯度值的 tensor 也只有 \r\n一个输入项. 如下所示:\r\n\r\n```python\r\nfrom tensorflow.python.framework import ops\r\nfrom tensorflow.python.ops import array_ops\r\nfrom tensorflow.python.ops import sparse_ops\r\n\r\n@ops.RegisterGradient(\"ZeroOut\")\r\ndef _zero_out_grad(op, grad):\r\n  \"\"\"`zero_out` 的梯度.\r\n\r\n  参数:\r\n    op: 欲进行微分的 `zero_out` `操作`, 可以用于获取原始 Op 的输入和输出.\r\n    grad: 代表 `zero_out` 输出的梯度 Op.\r\n\r\n  返回:\r\n    代表输入 `zero_out` 的微分.\r\n  \"\"\"\r\n  to_zero = op.inputs[0]\r\n  shape = array_ops.shape(to_zero)\r\n  index = array_ops.zeros_like(shape)\r\n  first_grad = array_ops.reshape(grad, [-1])[0]\r\n  to_zero_grad = sparse_ops.sparse_to_dense(index, shape, first_grad, 0)\r\n  return [to_zero_grad]  # 单个 Tensor 的列表, 既然只有一个输入\r\n```\r\n\r\n使用 [`ops.RegisterGradient`](tensorflow-zh/SOURCE/api_docs/python/framework.md#RegisterGradient) \r\n注册梯度函数需要注意的一些细节:\r\n\r\n* 对于仅有一个输出的 Op, 梯度函数使用 [`Operation`](tensorflow-zh/SOURCE/api_docs/python/framework.md#Operation) `op`\r\n  和一个 [`Tensor`](tensorflow-zh/SOURCE/api_docs/python/framework.md#Tensor) `grad` 作为参数, 并从\r\n  [`op.inputs[i]`](tensorflow-zh/SOURCE/api_docs/python/framework.md#Operation.inputs),\r\n  [`op.outputs[i]`](tensorflow-zh/SOURCE/api_docs/python/framework.md#Operation.outputs), \r\n  和 `grad` 构建新的 Op. 属性的信息可以通过 [`op.get_attr`](tensorflow-zh/SOURCE/api_docs/python/framework.md#Operation.get_attr) 获取.\r\n \r\n* 如果 Op 有多个输出, 梯度函数将使用 `op` 和 `grads` 作为参数, 其中, `grads` 是一个\r\n  梯度 Op 的列表, 为每一个输出计算梯度. 梯度函数的输出必须是一个 `Tensor` 对象列表, 对应到\r\n  每一个输入的梯度.\r\n\r\n* 如果没有为一些输入定义梯度, 譬如用作索引的整型, 这些输入返回的梯度为 `None`. 举一个例子,\r\n  如果一个 Op 的输入为一个浮点数 tensor `x` 和一个整型索引 `i`, 那么梯度函数将返回\r\n  `[x_grad, None]`.\r\n\r\n* 如果梯度对于一个 Op 来说毫无意义, 使用 `ops.NoGradient(\"OpName\")` 禁用自动差分.\r\n\r\n注意当梯度函数被调用时, 作用的对象是数据流图中的 Op, 而不是 tensor 数据本身. 因此, 只有在图运行时, \r\n梯度运算才会被其它 tensorflow Op 的执行动作所触发.\r\n\r\n## 在 Python 中实现一个形状函数 <a class=\"md-anchor\" id=\"AUTOGENERATED-implement-a-shape-function-in-python\"></a>\r\n\r\nTensorFlow Python API 有一个 \"形状推断\" 功能, 可以不执行图就获取 tensor 的形状信息.\r\n形状推断功能藉由每一个 Op 类型注册的 \"形状函数\" 来支持, 该函数有两个规则: 假设所有输入的\r\n形状必须是兼容的, 以及指定输出的形状. 一个形状函数以一个 [`Operation`](tensorflow-zh/SOURCE/api_docs/python/framework.md#Operation)\r\n作为输入, 返回一个 [`TensorShape`](tensorflow-zh/SOURCE/api_docs/python/framework.md#TensorShape)\r\n对象列表 (每一个输出一个对象). 使用 [`tf.RegisterShape` 装饰器](tensorflow-zh/SOURCE/api_docs/python/framework.md#RegisterShape)\r\n注册形状函数. 例如, [上文定义的 `ZeroOut` Op](#define_interface) 的形状函数如下:\r\n\r\n```python\r\n@tf.RegisterShape(\"ZeroOut\"):\r\ndef _zero_out_shape(op):\r\n  \"\"\"ZeroOut Op 的形状函数.\r\n\r\n  这是 ZeroOut 形状函数的无约束版本, 为每一个输出产生的形状和对应的输入一样. \r\n  \"\"\"\r\n  return [op.inputs[0].get_shape()]\r\n```\r\n\r\n一个形状函数也可以约束输入的形状. 下面是 [ZeroOut 形状函数的 vector 输入约束](#Validation)版本:\r\n\r\n```python\r\n@tf.RegisterShape(\"ZeroOut\"):\r\ndef _zero_out_shape(op):\r\n  \"\"\"ZeroOut Op 的形状函数.\r\n\r\n  这是 ZeroOut 形状函数的约束版本, 要输入的 rank 必须是 1 (即使一个 vector).\r\n  \"\"\"\r\n  input_shape = op.inputs[0].get_shape().with_rank(1)\r\n  return [input_shape]\r\n```\r\n\r\n如果 Op 是[多输入的多态 Op](#Polymorphism), 使用操作的属性来决定需要检查的形状数量:\r\n\r\n```\r\n@tf.RegisterShape(\"IntListInputExample\")\r\ndef _int_list_input_example_shape(op):\r\n  \"\"\" \"IntListInputExample\" Op 的形状函数.\r\n\r\n  所有的输入和输出是同大小的矩阵.\r\n  \"\"\"\r\n  output_shape = tf.TensorShape(None)\r\n  for input in op.inputs:\r\n    output_shape = output_shape.merge_with(input.get_shape().with_rank(2))\r\n  return [output_shape]\r\n```\r\n\r\n既然形状推断是一个可选的特性, 且 tensor 的形状可能动态变化, 形状函数必须足够健壮, 能够处理任意\r\n输入形状信息缺失的情形. [`merge_with`](tensorflow-zh/SOURCE/api_docs/python/framework.md) 方法能够帮助\r\n调用者判断两个形状是否是一样的, 即使两个形状的信息不全, 该函数同样有效. 所有的[标准 Python Op](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/)\r\n的形状函数都已经定义好了, 并且已经有很多不同的使用示例.\r\n\r\n[core-array_ops]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/ops/array_ops.cc\r\n[python-user_ops]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/user_ops/user_ops.py\r\n[tf-kernels]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/\r\n[user_ops]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/user_ops/\r\n[pad_op]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/pad_op.cc\r\n[standard_ops-py]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/standard_ops.py\r\n[standard_ops-cc]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/cc/ops/standard_ops.h\r\n[python-BUILD]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/BUILD\r\n[validation-macros]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/lib/core/errors.h\r\n[op_def_builder]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op_def_builder.h\r\n[register_types]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/register_types.h\r\n[FinalizeAttr]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op_def_builder.cc#FinalizeAttr\r\n[DataTypeString]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/types.cc#DataTypeString\r\n[python-BUILD]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/BUILD\r\n[types-proto]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/types.proto\r\n[TensorShapeProto]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/tensor_shape.proto\r\n[TensorProto]:https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/tensor.proto\r\n\r\n> 原文：[Adding a New Op](http://www.tensorflow.org/how_tos/adding_an_op/index.html#adding-a-new-op)  翻译：[@doc001](https://github.com/PFZheng) 校对：[@ZHNathanielLee](https://github.com/ZHNathanielLee)\r\n"
  },
  {
    "path": "SOURCE/how_tos/graph_viz/index.md",
    "content": "# TensorBoard: Graph Visualization <a class=\"md-anchor\" id=\"AUTOGENERATED-tensorboard--graph-visualization\"></a>\r\n\r\nTensorFlow computation graphs are powerful but complicated. The graph visualization can help you understand and debug them. Here's an example of the visualization at work.\r\n\r\n![Visualization of a TensorFlow graph](./graph_vis_animation.gif \"Visualization of a TensorFlow graph\")\r\n*Visualization of a TensorFlow graph.*\r\n\r\nTo see your own graph, run TensorBoard pointing it to the log directory of the job, click on the graph tab on the top pane and select the appropriate run using the menu at the upper left corner. For in depth information on how to run TensorBoard and make sure you are logging all the necessary information, see [Summaries and TensorBoard](../../how_tos/summaries_and_tensorboard/index.md).\r\n\r\n## Name scoping and nodes <a class=\"md-anchor\" id=\"AUTOGENERATED-name-scoping-and-nodes\"></a>\r\n\r\nTypical TensorFlow graphs can have many thousands of nodes--far too many to see\r\neasily all at once, or even to lay out using standard graph tools. To simplify,\r\nvariable names can be scoped and the visualization uses this information to\r\ndefine a hierarchy on the nodes in the graph.  By default, only the top of this\r\nhierarchy is shown. Here is an example that defines three operations under the\r\n`hidden` name scope using\r\n[`tf.name_scope`](../../api_docs/python/framework.md#name_scope):\r\n\r\n```python\r\nimport tensorflow as tf\r\n\r\nwith tf.name_scope('hidden') as scope:\r\n  a = tf.constant(5, name='alpha')\r\n  W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0), name='weights')\r\n  b = tf.Variable(tf.zeros([1]), name='biases')\r\n```\r\n\r\nThis results in the following three op names:\r\n\r\n* *hidden*/alpha\r\n* *hidden*/weights\r\n* *hidden*/biases\r\n\r\nBy default, the visualization will collapse all three into a node labeled `hidden`.\r\nThe extra detail isn't lost. You can double-click, or click\r\non the orange `+` sign in the top right to expand the node, and then you'll see\r\nthree subnodes for `alpha`, `weights` and `biases`.\r\n\r\nHere's a real-life example of a more complicated node in its initial and\r\nexpanded states.\r\n\r\n<table width=\"100%;\">\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"./pool1_collapsed.png\" alt=\"Unexpanded name scope\" title=\"Unexpanded name scope\" />\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"./pool1_expanded.png\" alt=\"Expanded name scope\" title=\"Expanded name scope\" />\r\n    </td>\r\n  </tr>\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      Initial view of top-level name scope <code>pool_1</code>. Clicking on the orange <code>+</code> button on the top right or double-clicking on the node itself will expand it.\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      Expanded view of <code>pool_1</code> name scope. Clicking on the orange <code>-</code> button on the top right or double-clicking on the node itself will collapse the name scope.\r\n    </td>\r\n  </tr>\r\n</table>\r\n\r\nGrouping nodes by name scopes is critical to making a legible graph. If you're\r\nbuilding a model, name scopes give you control over the resulting visualization.\r\n**The better your name scopes, the better your visualization.**\r\n\r\nThe figure above illustrates a second aspect of the visualization. TensorFlow\r\ngraphs have two kinds of connections: data dependencies and control\r\ndependencies. Data dependencies show the flow of tensors between two ops and\r\nare shown as solid arrows, while control dependencies use dotted lines. In the\r\nexpanded view (right side of the figure above) all the connections are data\r\ndependencies with the exception of the dotted line connecting `CheckNumerics`\r\nand `control_dependency`.\r\n\r\nThere's a second trick to simplifying the layout. Most TensorFlow graphs have a\r\nfew nodes with many connections to other nodes. For example, many nodes might\r\nhave a control dependencies on an initialization step. Drawing all edges\r\nbetween the `init` node and its dependencies would create a very cluttered\r\nview.\r\n\r\nTo reduce clutter, the visualization separates out all high-degree nodes to an\r\n*auxiliary* area on the right and doesn't draw lines to represent their edges.\r\nInstead of lines, we draw small *node icons* to indicate the connections.\r\nSeparating out the auxiliary nodes typically doesn't remove critical\r\ninformation since these nodes are usually related to bookkeeping functions.\r\n\r\n<table width=\"100%;\">\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"./conv_1.png\" alt=\"conv_1 is part of the main graph\" title=\"conv_1 is part of the main graph\" />\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"./save.png\" alt=\"save is extracted as auxiliary node\" title=\"save is extracted as auxiliary node\" />\r\n    </td>\r\n  </tr>\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      Node <code>conv_1</code> is connected to <code>save</code>. Note the little <code>save</code> node icon on its right.\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      <code>save</code> has a high degree, and will appear as an auxiliary node. The connection with <code>conv_1</code> is shown as a node icon on its left. To further reduce clutter, since <code>save</code> has a lot of connections, we show the first 5 and abbreviate the others as <code>... 12 more</code>.\r\n    </td>\r\n  </tr>\r\n</table>\r\n\r\nOne last structural simplification is *series collapsing*. Sequential\r\nmotifs--that is, nodes whose names differ by a number at the end and have\r\nisomorphic structures--are collapsed into a single *stack* of nodes, as shown\r\nbelow. For networks with long sequences, this greatly simplifies the view. As\r\nwith hierarchical nodes, double-clicking expands the series.\r\n\r\n<table width=\"100%;\">\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"./series.png\" alt=\"Sequence of nodes\" title=\"Sequence of nodes\" />\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"./series_expanded.png\" alt=\"Expanded sequence of nodes\" title=\"Expanded sequence of nodes\" />\r\n    </td>\r\n  </tr>\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      A collapsed view of a node sequence.\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      A small piece of the expanded view, after double-click.\r\n    </td>\r\n  </tr>\r\n</table>\r\n\r\nFinally, as one last aid to legibility, the visualization uses special icons\r\nfor constants and summary nodes. To summarize, here's a table of node symbols:\r\n\r\nSymbol | Meaning\r\n--- | ---\r\n![Name scope](./namespace_node.png \"Name scope\") | *High-level* node representing a name scope. Double-click to expand a high-level node.\r\n![Sequence of unconnected nodes](./horizontal_stack.png \"Sequence of unconnected nodes\") | Sequence of numbered nodes that are not connected to each other.\r\n![Sequence of connected nodes](./vertical_stack.png \"Sequence of connected nodes\") | Sequence of numbered nodes that are connected to each other.\r\n![Operation node](./op_node.png \"Operation node\") | An individual operation node.\r\n![Constant node](./constant.png \"Constant node\") | A constant.\r\n![Summary node](./summary.png \"Summary node\") | A summary node.\r\n![Data flow edge](./dataflow_edge.png \"Data flow edge\") | Edge showing the data flow between operations.\r\n![Control dependency edge](./control_edge.png \"Control dependency edge\") | Edge showing the control dependency between operations.\r\n![Reference edge](./reference_edge.png \"Reference edge\") | A reference edge showing that the outgoing operation node can mutate the incoming tensor.\r\n\r\n## Interaction <a class=\"md-anchor\" id=\"AUTOGENERATED-interaction\"></a>\r\n\r\nNavigate the graph by panning and zooming. Click and drag to pan, and use a\r\nscroll gesture to zoom. Double-click on a node, or click on its `+` button, to\r\nexpand a name scope that represents a group of operations. To easily keep\r\ntrack of the current viewpoint when zooming and panning, there is a minimap in\r\nthe bottom right corner.\r\n\r\nTo close an open node, double-click it again or click its `-` button. You can\r\nalso click once to select a node. It will turn a darker color, and details\r\nabout it and the nodes it connects to will appear in the info card at upper\r\nright corner of the visualization.\r\n\r\n<table width=\"100%;\">\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"./infocard.png\" alt=\"Info card of a name scope\" title=\"Info card of a name scope\" />\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"./infocard_op.png\" alt=\"Info card of operation node\" title=\"Info card of operation node\" />\r\n    </td>\r\n  </tr>\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      Info card showing detailed information for the <code>conv2</code> name scope. The inputs and outputs are combined from the inputs and outputs of the operation nodes inside the name scope. For name scopes no attributes are shown.\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      Info card showing detailed information for the <code>DecodeRaw</code> operation node. In addition to inputs and outputs, the card shows the device and the attributes associated with the current operation.\r\n    </td>\r\n  </tr>\r\n</table>\r\n\r\nSelection can also be helpful in understanding high-degree nodes. Select any\r\nhigh-degree node, and the corresponding node icons for its other connections\r\nwill be selected as well. This makes it easy, for example, to see which nodes\r\nare being saved--and which aren't.\r\n\r\nClicking on a node name in the info card will select it. If necessary, the\r\nviewpoint will automatically pan so that the node is visible.\r\n\r\nFinally, you can choose two color schemes for your graph, using the color menu\r\nabove the legend. The default *Structure View* shows structure: when two\r\nhigh-level nodes have the same structure, they appear in the same color of the\r\nrainbow. Uniquely structured nodes are gray. There's a second view, which shows\r\nwhat device the different operations run on. Name scopes are colored\r\nproportionally to the fraction of devices for the operations inside them.\r\n\r\nThe images below give an illustration for a piece of a real-life graph.\r\n\r\n<table width=\"100%;\">\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"./colorby_structure.png\" alt=\"Color by structure\" title=\"Color by structure\" />\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"./colorby_device.png\" alt=\"Color by device\" title=\"Color by device\" />\r\n    </td>\r\n  </tr>\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      Structure view: The gray nodes have unique structure. The orange <code>conv1</code> and <code>conv2</code> nodes have the same structure, and analogously for nodes with other colors.\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      Device view: Name scopes are colored proportionally to the fraction of devices of the operation nodes inside them. Here, purple means GPU and the green is CPU.\r\n    </td>\r\n  </tr>\r\n</table>\r\n"
  },
  {
    "path": "SOURCE/how_tos/graph_viz.md",
    "content": "# TensorBoard: 图表可视化 <a class=\"md-anchor\" id=\"AUTOGENERATED-tensorboard--graph-visualization\"></a>\r\n\r\nTensorFlow 图表计算强大而又复杂，图表可视化在理解和调试时显得非常有帮助。 下面是一个运作时的可式化例子。\r\n\r\n![一个TensorFlow图表的可视化](../images/graph_vis_animation.gif)\r\n\"一个TensorFlow图表的可视化\")\r\n*一个TensorFlow图表的可视化。*\r\n\r\n为了显示自己的图表，需将 TensorBoard 指向此工作的日志目录并运行，点击图表顶部窗格的标签页，然后在左上角的菜单中选择合适的运行。想要深入学习关于如何运行 TensorBoard 以及如何保证所有必要信息被记录下来，请查看 [Summaries 和 TensorBoard](tensorflow-zh/SOURCE/how_tos/summaries_and_tensorboard/index.md).\r\n\r\n## 名称域（Name scoping）和节点（Node） <a class=\"md-anchor\" id=\"AUTOGENERATED-name-scoping-and-nodes\"></a>\r\n\r\n典型的 TensorFlow 可以有数以千计的节点，如此多而难以一下全部看到，甚至无法使用标准图表工具来展示。为简单起见，我们为变量名划定范围，并且可视化把该信息用于在图表中的节点上定义一个层级。默认情况下， 只有顶层节点会显示。下面这个例子使用[`tf.name_scope`](tensorflow-zh/SOURCE/api_docs/python/framework.md#name_scope)在`hidden`命名域下定义了三个操作:\r\n\r\n```python\r\nimport tensorflow as tf\r\n\r\nwith tf.name_scope('hidden') as scope:\r\n  a = tf.constant(5, name='alpha')\r\n  W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0), name='weights')\r\n  b = tf.Variable(tf.zeros([1]), name='biases')\r\n```\r\n\r\n结果是得到了下面三个操作名:\r\n\r\n* *hidden*/alpha\r\n* *hidden*/weights\r\n* *hidden*/biases\r\n\r\n默认地，三个操作名会折叠为一个节点并标注为`hidden`。其额外细节并没有丢失，你可以双击，或点击右上方橙色的`+`来展开节点，然后就会看到三个子节点`alpha`，`weights`和`biases`了。\r\n\r\n这有一个生动的例子，例中有一个更复杂的节点，节点处于其初始和展开状态。\r\n\r\n<table width=\"100%;\">\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"../images/pool1_collapsed.png\" alt=\"未展开的名称域\" title=\"未展开的名称域\" />\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"../images/pool1_expanded.png\" alt=\"展开的名称域\" title=\"展开的名称域\" />\r\n    </td>\r\n  </tr>\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      顶级名称域的初始视图<code>pool_1</code>，点击右上方橙色的<code>+</code>按钮或双击节点来展开。\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      展开的<code>pool_1</code>名称域视图，点击右上方橙色的<code>-</code>按钮或双击节点来收起此名称域。\r\n    </td>\r\n  </tr>\r\n</table>\r\n\r\n通过名称域把节点分组来得到可读性高的图表很关键的。如果你在构建一个模型，名称域就可以用来控制可视化结果。**你的名称域越好，可视性就越好。**\r\n\r\n上面的图像例子说明了可视化的另一方面， TensorFlow 图表有两种连接关系：数据依赖和控制依赖。数据依赖显示两个操作之间的tensor流程，用实心箭头指示，而控制依赖用点线表示。在已展开的视图(上面的右图)中，除了用点线连接的`CheckNumerics`和`control_dependency`之外，所有连接都是数据依赖的。\r\n\r\n还有一种手段用来简化布局。大多数 TensorFlow 图表有一部分节点，这部分节点和其他节点之间有很多连接。比如，许多节点在初始化阶段可能会有一个控制依赖，而绘制所有`init`节点的边缘和其依赖可能会创造出一个混乱的视图。\r\n\r\n为了减少混乱，可视化把所有 high-degree 节点分离到右边的一个*从属*区域， 而不会绘制线条来表示他们的边缘。线条也不用来表示连接了，我们绘制了小*节点图标*来指示这些连接关系。分离出从属节点通常不会把关键信息删除掉，因为这些节点和内构功能是相关的。\r\n\r\n<table width=\"100%;\">\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"../images/conv_1.png\" alt=\"conv_1是主图表的部分\" title=\"conv_1是主图表的部分\" />\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"../images/save.png\" alt=\"save被抽出为从属节点\" title=\"save被抽出为从属节点\" />\r\n    </td>\r\n  </tr>\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      节点<code>conv_1</code>被连接到<code>save</code>，注意其右边<code>save</code>节点图标。\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      <code>save</code> has a high degree, 并会作为从属节点出现，与<code>conv_1</code>的连接作为一个节点图标显示在其左边。为了继续减少杂乱，既然<code>save</code>有很多连接，我们则只显示前5个，而把其余的缩略为<code>... 12 more</code>。\r\n    </td>\r\n  </tr>\r\n</table>\r\n\r\n最后一个结构上的简化法叫做*序列折叠（series collapsing）*。 序列基序（Sequential motifs）是拥有相同结构并且其名称结尾的数字不同的节点，它们被折叠进一个单独的节点块（stack）中。对长序列网络来说，序列折叠极大地简化了视图，对于已层叠的节点，双击会展开序列。\r\n\r\n<table width=\"100%;\">\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"../images/series.png\" alt=\"节点序列\" title=\"节点序列\" />\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"../images/series_expanded.png\" alt=\"展开的节点序列\" title=\"展开的节点序列\" />\r\n    </td>\r\n  </tr>\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      一个节点序列的折叠视图。\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      视图的一小块, 双击后展开。\r\n    </td>\r\n  </tr>\r\n</table>\r\n\r\n最后，针对易读性的最后一点要说到的是，可视化为常节点和摘要节点使用了特别的图标，总结起来有下面这些节点符号：\r\n\r\n符号 | 意义\r\n--- | ---\r\n![名称域](../images/namespace_node.png \"名称域\") | *High-level*节点代表一个名称域，双击则展开一个高层节点。\r\n![断线节点序列](../images/horizontal_stack.png \"断线节点序列\") | 彼此之间不连接的有限个节点序列。\r\n![相连节点序列](../images/vertical_stack.png \"相连节点序列\") | 彼此之间相连的有限个节点序列。\r\n![操作节点](../images/op_node.png \"操作节点\") | 一个单独的操作节点。\r\n![常量节点](../images/constant.png \"常量节点\") | 一个常量结点。\r\n![摘要节点](../images/summary.png \"摘要节点\") | 一个摘要节点。\r\n![数据流边](../images/dataflow_edge.png \"数据流边\") | 显示各操作间的数据流边。\r\n![控制依赖边](../images/control_edge.png \"控制依赖边\") | 显示各操作间的控制依赖边。\r\n![引用边](../images/reference_edge.png \"引用边\") | 引用边，表示出度操作节点可以使入度tensor发生变化。\r\n\r\n## 交互 <a class=\"md-anchor\" id=\"AUTOGENERATED-interaction\"></a>\r\n\r\n通过平移和缩放来导航图表，点击和拖动用于平移，滚动手势用于缩放。双击一个节点或点击其`+`按钮来展开代表一组操作的名称域。右下角有一个小地图可以在缩放和平移时方便的改变当前视角。\r\n\r\n要关闭一个打开的节点，再次双击它或点击它的`-`按钮，你也可以只点击一次来选中一个节点，节点的颜色会加深，并且会看到节点的详情，其连接到的节点会在可视化右上角的详情卡片显现。\r\n\r\n<table width=\"100%;\">\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"../images/infocard.png\" alt=\"一个名称域的详情卡片\" title=\"一个名称域的详情卡片\" />\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"../images/infocard_op.png\" alt=\"操作节点的详情卡片\" title=\"操作节点的详情卡片\" />\r\n    </td>\r\n  </tr>\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      详情卡片展示<code>conv2</code>名称域的详细信息，名称域中操作节点的输入和输出被结合在一起，适用于不显示属性的名称域。\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      详情卡片展示<code>DecodeRaw</code>操作节点，除了输入和输出，卡片也会展示与当前节点相关的设备和属性。\r\n    </td>\r\n  </tr>\r\n</table>\r\n\r\n选择对于 high-degree 节点的理解也很有帮助，选择任意节点，则与它的其余连接相应的节点也会选中，这使得在进行例如查看哪一个节点是否已保存等操作时非常容易。\r\n\r\n点击详情卡片中的一个节点名称时会选中该节点，必要的话，视角会自动平移以使该节点可见。\r\n\r\n最后，使用图例上方的颜色菜单，你可以给你的图表选择两个颜色方案。默认的*结构视图*下，当两个 high-level 节点颜色一样时，其会以相同的彩虹色彩出现，而结构唯一的节点颜色是灰色。还有一个视图则展示了不同的操作运行于什么设备之上。名称域被恰当的根据其中的操作节点的设备片件来着色。\r\n\r\n下图是一张真实图表的图解：\r\n\r\n<table width=\"100%;\">\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"../images/colorby_structure.png\" alt=\"按结构着色\" title=\"按结构着色\" />\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      <img src=\"../images/colorby_device.png\" alt=\"按设备着色\" title=\"按设备着色\" />\r\n    </td>\r\n  </tr>\r\n  <tr>\r\n    <td style=\"width: 50%;\">\r\n      结构视图：灰色节点的结构是唯一的。橙色的<code>conv1</code>和<code>conv2</code>节点有相同的结构, 其他颜色的节点也类似。\r\n    </td>\r\n    <td style=\"width: 50%;\">\r\n      设备视图：名称域根据其中的操作节点的设备片件来着色，在此紫色代表GPU，绿色代表CPU。\r\n    </td>\r\n  </tr>\r\n</table>\r\n\r\n原文: [TensorBoard: Graph Visualization](../images/index.html#tensorboard-graph-visualization)\r\n翻译: [@Warln](https://github.com/Warln)  校对: [lucky521](https://github.com/lucky521)\r\n"
  },
  {
    "path": "SOURCE/how_tos/new_data_formats/Custom Data Readers.md",
    "content": "# 自定义数据读取 <a class=\"md-anchor\" id=\"AUTOGENERATED-custom-data-readers\"></a>\r\n\r\n基本要求:\r\n\r\n*   熟悉 C++ 编程。\r\n*   确保\r\n    [下载 TensorFlow 源文件](../../get_started/os_setup.md#source), 并可编译使用。\r\n\r\n我们将支持文件格式的任务分成两部分：\r\n\r\n*   文件格式: 我们使用 *Reader* Op来从文件中读取一个 *record* (可以使任意字符串)。\r\n*   记录格式: 我们使用解码器或者解析运算将一个字符串记录转换为TensorFlow可以使用的张量。\r\n\r\n例如， 读取一个\r\n[CSV 文件](https://en.wikipedia.org/wiki/Comma-separated_values)，我们使用\r\n[一个文本读写器](../../api_docs/python/io_ops.md#TextLineReader)，\r\n然后是[从一行文本中解析CSV数据的运算](../../api_docs/python/io_ops.md#decode_csv)。\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## 主要内容\r\n### [自定义数据读取](#AUTOGENERATED-custom-data-readers)\r\n* [编写一个文件格式读写器](#AUTOGENERATED-writing-a-reader-for-a-file-format)\r\n* [编写一个记录格式Op](#AUTOGENERATED-writing-an-op-for-a-record-format)\r\n\r\n\r\n## 编写一个文件格式读写器 <a class=\"md-anchor\" id=\"AUTOGENERATED-writing-a-reader-for-a-file-format\"></a>\r\n\r\n Reader 是专门用来读取文件中的记录的。TensorFlow中内建了一些读写器Op的实例：\r\n\r\n*   [tf.TFRecordReader](../../api_docs/python/io_ops.md#TFRecordReader)\r\n    ([代码位于kernels/tf_record_reader_op.cc](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/tf_record_reader_op.cc))\r\n*   [tf.FixedLengthRecordReader](../../api_docs/python/io_ops.md#FixedLengthRecordReader)\r\n    ([代码位于 kernels/fixed_length_record_reader_op.cc](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/fixed_length_record_reader_op.cc))\r\n*   [tf.TextLineReader](../../api_docs/python/io_ops.md#TextLineReader)\r\n    ([代码位于 kernels/text_line_reader_op.cc](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/text_line_reader_op.cc))\r\n\r\n你可以看到这些读写器的界面是一样的，唯一的差异是在它们的构造函数中。最重要的方法是 Read。\r\n它需要一个行列参数，通过这个行列参数，可以在需要的时候随时读取文件名 (例如： 当 Read Op首次运行，或者\r\n前一个 Read` 从一个文件中读取最后一条记录时)。它将会生成两个标量张量： 一个字符串和一个字符串关键值。\r\n\r\n新创建一个名为 SomeReader 的读写器，需要以下步骤：\r\n\r\n1.  在 C++ 中, 定义一个\r\n    [tensorflow::ReaderBase](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/reader_base.h)的子类，命名为\r\n     \"SomeReader\".\r\n2.  在 C++ 中，注册一个新的读写器Op和Kernel，命名为 \"SomeReader\"。\r\n3.  在 Python 中, 定义一个 [tf.ReaderBase](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/io_ops.py) 的子类，命名为 \"SomeReader\"。\r\n\r\n你可以把所有的 C++ 代码放在\r\ntensorflow/core/user_ops/some_reader_op.cc文件中.  读取文件的代码将被嵌入到C++ 的 ReaderBase 类的迭代中。 这个 ReaderBase 类 是在 [tensorflow/core/kernels/reader_base.h](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/reader_base.h) 中定义的。\r\n你需要执行以下的方法：\r\n\r\n*   OnWorkStartedLocked：打开下一个文件\r\n*   ReadLocked：读取一个记录或报告 EOF/error\r\n*   OnWorkFinishedLocked：关闭当前文件\r\n*   ResetLocked：清空记录，例如：一个错误记录\r\n\r\n以上这些方法的名字后面都带有 \"Locked\"， 表示  ReaderBase 在调用任何一个方法之前确保获得互斥锁，这样就不用担心线程安全（虽然只保护了该类中的元素而不是全局的）。\r\n\r\n对于 OnWorkStartedLocked, 需要打开的文件名是 current_work() 函数的返回值。  此时的 ReadLocked 的数字签名如下:\r\n\r\n```c++\r\nStatus ReadLocked(string* key, string* value, bool* produced, bool* at_end)\r\n```\r\n\r\n如果 ReadLocked 从文件中成功读取了一条记录，它将更新为：\r\n\r\n*   *key： 记录的标志位，通过该标志位可以重新定位到该记录。 可以包含从 current_work() 返回值获得的文件名，并追加一个记录号或其他信息。\r\n*   *value： 包含记录的内容。\r\n*   *produced： 设置为 true。\r\n\r\n当你在文件（EOF）末尾，设置 *at_end 为 true ，在任何情况下，都将返回 Status::OK()。 当出现错误的时候，只需要使用\r\n[tensorflow/core/lib/core/errors.h](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/lib/core/errors.h) 中的一个辅助功能就可以简单地返回，不需要做任何参数修改。\r\n\r\n接下来你讲创建一个实际的读写器Op。 如果你已经熟悉了[添加新的Op](../../how_tos/adding_an_op/index.md) 那会很有帮助。 主要步骤如下：\r\n\r\n*   注册Op。\r\n*   定义并注册 OpKernel。\r\n\r\n要注册Op，你需要用到一个调用指令定义在\r\n[tensorflow/core/framework/op.h](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op.h)中的REGISTER_OP。\r\n\r\n读写器 Op 没有输入，只有 Ref(string) 类型的单输出。它们调用 SetIsStateful()，并有一个\r\ncontainer 字符串和 shared_name 属性.  你可以在一个 Doc 中定义配置或包含文档的额外属性。 例如：详见\r\n[tensorflow/core/ops/io_ops.cc](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/ops/io_ops.cc)等：\r\n\r\n```c++\r\n#include \"tensorflow/core/framework/op.h\"\r\n\r\nREGISTER_OP(\"TextLineReader\")\r\n    .Output(\"reader_handle: Ref(string)\")\r\n    .Attr(\"skip_header_lines: int = 0\")\r\n    .Attr(\"container: string = ''\")\r\n    .Attr(\"shared_name: string = ''\")\r\n    .SetIsStateful()\r\n    .Doc(R\"doc(\r\nA Reader that outputs the lines of a file delimited by '\\n'.\r\n)doc\");\r\n```\r\n要定义一个 OpKernel， 读写器可以使用定义在[tensorflow/core/framework/reader_op_kernel.h](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/reader_op_kernel.h)中的 ReaderOpKernel 的递减快捷方式，并运行一个叫 SetReaderFactory 的构造函数。\r\n定义所需要的类之后，你需要通过 REGISTER_KERNEL_BUILDER(...) 注册这个类。\r\n\r\n一个没有属性的例子：\r\n\r\n```c++\r\n#include \"tensorflow/core/framework/reader_op_kernel.h\"\r\n\r\nclass TFRecordReaderOp : public ReaderOpKernel {\r\n public:\r\n  explicit TFRecordReaderOp(OpKernelConstruction* context)\r\n      : ReaderOpKernel(context) {\r\n    Env* env = context->env();\r\n    SetReaderFactory([this, env]() { return new TFRecordReader(name(), env); });\r\n  }\r\n};\r\n\r\nREGISTER_KERNEL_BUILDER(Name(\"TFRecordReader\").Device(DEVICE_CPU),\r\n                        TFRecordReaderOp);\r\n```\r\n一个带有属性的例子：\r\n\r\n```c++\r\n#include \"tensorflow/core/framework/reader_op_kernel.h\"\r\n\r\nclass TextLineReaderOp : public ReaderOpKernel {\r\n public:\r\n  explicit TextLineReaderOp(OpKernelConstruction* context)\r\n      : ReaderOpKernel(context) {\r\n    int skip_header_lines = -1;\r\n    OP_REQUIRES_OK(context,\r\n                   context->GetAttr(\"skip_header_lines\", &skip_header_lines));\r\n    OP_REQUIRES(context, skip_header_lines >= 0,\r\n                errors::InvalidArgument(\"skip_header_lines must be >= 0 not \",\r\n                                        skip_header_lines));\r\n    Env* env = context->env();\r\n    SetReaderFactory([this, skip_header_lines, env]() {\r\n      return new TextLineReader(name(), skip_header_lines, env);\r\n    });\r\n  }\r\n};\r\n\r\nREGISTER_KERNEL_BUILDER(Name(\"TextLineReader\").Device(DEVICE_CPU),\r\n                        TextLineReaderOp);\r\n```\r\n\r\n最后一步是添加 Python 包装器，你需要将 tensorflow.python.ops.io_ops 导入到\r\n[tensorflow/python/user_ops/user_ops.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/user_ops/user_ops.py)，并添加一个 [io_ops.ReaderBase](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/io_ops.py)的衍生函数。\r\n\r\n```python\r\nfrom tensorflow.python.framework import ops\r\nfrom tensorflow.python.ops import common_shapes\r\nfrom tensorflow.python.ops import io_ops\r\n\r\nclass SomeReader(io_ops.ReaderBase):\r\n\r\n    def __init__(self, name=None):\r\n        rr = gen_user_ops.some_reader(name=name)\r\n        super(SomeReader, self).__init__(rr)\r\n\r\n\r\nops.NoGradient(\"SomeReader\")\r\nops.RegisterShape(\"SomeReader\")(common_shapes.scalar_shape)\r\n```\r\n你可以在\r\n[tensorflow/python/ops/io_ops.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/io_ops.py)中查看一些范例。\r\n\r\n## 编写一个记录格式Op <a class=\"md-anchor\" id=\"AUTOGENERATED-writing-an-op-for-a-record-format\"></a>\r\n\r\n一般来说，这是一个普通的Op， 需要一个标量字符串记录作为输入， 因此遵循 [添加Op的说明](../../how_tos/adding_an_op/index.md)。 你可以选择一个标量字符串作为输入， 并包含在错误消息中报告不正确的格式化数据。\r\n\r\n用于解码记录的运算实例：\r\n\r\n*   [tf.parse_single_example](../../api_docs/python/io_ops.md#parse_single_example)\r\n    (and\r\n    [tf.parse_example](../../api_docs/python/io_ops.md#parse_example))\r\n*   [tf.decode_csv](../../api_docs/python/io_ops.md#decode_csv)\r\n*   [tf.decode_raw](../../api_docs/python/io_ops.md#decode_raw)\r\n\r\n请注意，使用多个Op 来解码某个特定的记录格式也是有效的。 例如，你有一张以字符串格式保存在\r\n[tf.train.Example 协议缓冲区](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/example.proto)的图像文件。\r\n根据该图像的格式， 你可能从\r\n[tf.parse_single_example](../../api_docs/python/io_ops.md#parse_single_example) 的Op 读取响应输出并调用 [tf.decode_jpeg](../../api_docs/python/image.md#decode_jpeg)，\r\n[tf.decode_png](../../api_docs/python/image.md#decode_png)， 或者\r\n[tf.decode_raw](../../api_docs/python/io_ops.md#decode_raw)。通过读取 tf.decode_raw 的响应输出并使用[tf.slice](../../api_docs/python/array_ops.md#slice) 和\r\n[tf.reshape](../../api_docs/python/array_ops.md#reshape) 来提取数据是通用的方法。\r\n> 原文：[Custom Data Readers](http://tensorflow.org/how_tos/new_data_formats/index.html#custom-data-readers)  翻译：[@derekshang](https://github.com/derekshang)  校对：\r\n"
  },
  {
    "path": "SOURCE/how_tos/new_data_formats/index.md",
    "content": "# Custom Data Readers <a class=\"md-anchor\" id=\"AUTOGENERATED-custom-data-readers\"></a>\r\n\r\nPREREQUISITES:\r\n\r\n*   Some familiarity with C++.\r\n*   Must have\r\n    [downloaded TensorFlow source](../../get_started/os_setup.md#source), and be\r\n    able to build it.\r\n\r\nWe divide the task of supporting a file format into two pieces:\r\n\r\n*   File formats: We use a *Reader* Op to read a *record* (which can be any\r\n    string) from a file.\r\n*   Record formats: We use decoder or parsing Ops to turn a string record\r\n    into tensors usable by TensorFlow.\r\n\r\nFor example, to read a\r\n[CSV file](https://en.wikipedia.org/wiki/Comma-separated_values), we use\r\n[a Reader for text files](../../api_docs/python/io_ops.md#TextLineReader)\r\nfollowed by\r\n[an Op that parses CSV data from a line of text](../../api_docs/python/io_ops.md#decode_csv).\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Custom Data Readers](#AUTOGENERATED-custom-data-readers)\r\n* [Writing a Reader for a file format](#AUTOGENERATED-writing-a-reader-for-a-file-format)\r\n* [Writing an Op for a record format](#AUTOGENERATED-writing-an-op-for-a-record-format)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## Writing a Reader for a file format <a class=\"md-anchor\" id=\"AUTOGENERATED-writing-a-reader-for-a-file-format\"></a>\r\n\r\nA `Reader` is something that reads records from a file.  There are some examples\r\nof Reader Ops already built into TensorFlow:\r\n\r\n*   [`tf.TFRecordReader`](../../api_docs/python/io_ops.md#TFRecordReader)\r\n    ([source in `kernels/tf_record_reader_op.cc`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/tf_record_reader_op.cc))\r\n*   [`tf.FixedLengthRecordReader`](../../api_docs/python/io_ops.md#FixedLengthRecordReader)\r\n    ([source in `kernels/fixed_length_record_reader_op.cc`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/fixed_length_record_reader_op.cc))\r\n*   [`tf.TextLineReader`](../../api_docs/python/io_ops.md#TextLineReader)\r\n    ([source in `kernels/text_line_reader_op.cc`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/text_line_reader_op.cc))\r\n\r\nYou can see these all expose the same interface, the only differences\r\nare in their constructors.  The most important method is `read`.\r\nIt takes a queue argument, which is where it gets filenames to\r\nread from whenever it needs one (e.g. when the `read` op first runs, or\r\nthe previous `read` reads the last record from a file).  It produces\r\ntwo scalar tensors: a string key and and a string value.\r\n\r\nTo create a new reader called `SomeReader`, you will need to:\r\n\r\n1.  In C++, define a subclass of\r\n    [`tensorflow::ReaderBase`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/reader_base.h)\r\n    called `SomeReader`.\r\n2.  In C++, register a new reader op and kernel with the name `\"SomeReader\"`.\r\n3.  In Python, define a subclass of [`tf.ReaderBase`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/io_ops.py) called `SomeReader`.\r\n\r\nYou can put all the C++ code in a file in\r\n`tensorflow/core/user_ops/some_reader_op.cc`.  The code to read a file will live\r\nin a descendant of the C++ `ReaderBase` class, which is defined in\r\n[`tensorflow/core/kernels/reader_base.h`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/reader_base.h).\r\nYou will need to implement the following methods:\r\n\r\n*   `OnWorkStartedLocked`: open the next file\r\n*   `ReadLocked`: read a record or report EOF/error\r\n*   `OnWorkFinishedLocked`: close the current file, and\r\n*   `ResetLocked`: get a clean slate after, e.g., an error\r\n\r\nThese methods have names ending in \"Locked\" since `ReaderBase` makes sure\r\nto acquire a mutex before calling any of these methods, so you generally don't\r\nhave to worry about thread safety (though that only protects the members of the\r\nclass, not global state).\r\n\r\nFor `OnWorkStartedLocked`, the name of the file to open is the value returned by\r\nthe `current_work()` method.  `ReadLocked` has this signature:\r\n\r\n```c++\r\nStatus ReadLocked(string* key, string* value, bool* produced, bool* at_end)\r\n```\r\n\r\nIf `ReadLocked` successfully reads a record from the file, it should fill in:\r\n\r\n*   `*key`: with an identifier for the record, that a human could use to find\r\n    this record again.  You can include the filename from `current_work()`,\r\n    and append a record number or whatever.\r\n*   `*value`: with the contents of the record.\r\n*   `*produced`: set to `true`.\r\n\r\nIf you hit the end of a file (EOF), set `*at_end` to `true`.  In either case,\r\nreturn `Status::OK()`.  If there is an error, simply return it using one of the\r\nhelper functions from\r\n[`tensorflow/core/lib/core/errors.h`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/lib/core/errors.h)\r\nwithout modifying any arguments.\r\n\r\nNext you will create the actual Reader op.  It will help if you are familiar\r\nwith [the adding an op how-to](../../how_tos/adding_an_op/index.md).  The main steps\r\nare:\r\n\r\n*   Registering the op.\r\n*   Define and register an `OpKernel`.\r\n\r\nTo register the op, you will use a `REGISTER_OP` call defined in\r\n[`tensorflow/core/framework/op.h`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op.h).\r\nReader ops never take any input and always have a single output with type\r\n`Ref(string)`.  They should always call `SetIsStateful()`, and have a string\r\n`container` and `shared_name` attrs.  You may optionally define additional attrs\r\nfor configuration or include documentation in a `Doc`.  For examples, see\r\n[`tensorflow/core/ops/io_ops.cc`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/ops/io_ops.cc),\r\ne.g.:\r\n\r\n```c++\r\n#include \"tensorflow/core/framework/op.h\"\r\n\r\nREGISTER_OP(\"TextLineReader\")\r\n    .Output(\"reader_handle: Ref(string)\")\r\n    .Attr(\"skip_header_lines: int = 0\")\r\n    .Attr(\"container: string = ''\")\r\n    .Attr(\"shared_name: string = ''\")\r\n    .SetIsStateful()\r\n    .Doc(R\"doc(\r\nA Reader that outputs the lines of a file delimited by '\\n'.\r\n)doc\");\r\n```\r\n\r\nTo define an `OpKernel`, Readers can use the shortcut of descending from\r\n`ReaderOpKernel`, defined in\r\n[`tensorflow/core/framework/reader_op_kernel.h`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/reader_op_kernel.h),\r\nand implement a constructor that calls `SetReaderFactory`.  After defining\r\nyour class, you will need to register it using `REGISTER_KERNEL_BUILDER(...)`.\r\nAn example with no attrs:\r\n\r\n```c++\r\n#include \"tensorflow/core/framework/reader_op_kernel.h\"\r\n\r\nclass TFRecordReaderOp : public ReaderOpKernel {\r\n public:\r\n  explicit TFRecordReaderOp(OpKernelConstruction* context)\r\n      : ReaderOpKernel(context) {\r\n    Env* env = context->env();\r\n    SetReaderFactory([this, env]() { return new TFRecordReader(name(), env); });\r\n  }\r\n};\r\n\r\nREGISTER_KERNEL_BUILDER(Name(\"TFRecordReader\").Device(DEVICE_CPU),\r\n                        TFRecordReaderOp);\r\n```\r\n\r\nAn example with attrs:\r\n\r\n```c++\r\n#include \"tensorflow/core/framework/reader_op_kernel.h\"\r\n\r\nclass TextLineReaderOp : public ReaderOpKernel {\r\n public:\r\n  explicit TextLineReaderOp(OpKernelConstruction* context)\r\n      : ReaderOpKernel(context) {\r\n    int skip_header_lines = -1;\r\n    OP_REQUIRES_OK(context,\r\n                   context->GetAttr(\"skip_header_lines\", &skip_header_lines));\r\n    OP_REQUIRES(context, skip_header_lines >= 0,\r\n                errors::InvalidArgument(\"skip_header_lines must be >= 0 not \",\r\n                                        skip_header_lines));\r\n    Env* env = context->env();\r\n    SetReaderFactory([this, skip_header_lines, env]() {\r\n      return new TextLineReader(name(), skip_header_lines, env);\r\n    });\r\n  }\r\n};\r\n\r\nREGISTER_KERNEL_BUILDER(Name(\"TextLineReader\").Device(DEVICE_CPU),\r\n                        TextLineReaderOp);\r\n```\r\n\r\nThe last step is to add the Python wrapper.  You will import\r\n`tensorflow.python.ops.io_ops` in\r\n[`tensorflow/python/user_ops/user_ops.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/user_ops/user_ops.py)\r\nand add a descendant of [`io_ops.ReaderBase`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/io_ops.py).\r\n\r\n```python\r\nfrom tensorflow.python.framework import ops\r\nfrom tensorflow.python.ops import common_shapes\r\nfrom tensorflow.python.ops import io_ops\r\n\r\nclass SomeReader(io_ops.ReaderBase):\r\n\r\n    def __init__(self, name=None):\r\n        rr = gen_user_ops.some_reader(name=name)\r\n        super(SomeReader, self).__init__(rr)\r\n\r\n\r\nops.NoGradient(\"SomeReader\")\r\nops.RegisterShape(\"SomeReader\")(common_shapes.scalar_shape)\r\n```\r\n\r\nYou can see some examples in\r\n[`tensorflow/python/ops/io_ops.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/io_ops.py).\r\n\r\n## Writing an Op for a record format <a class=\"md-anchor\" id=\"AUTOGENERATED-writing-an-op-for-a-record-format\"></a>\r\n\r\nGenerally this is an ordinary op that takes a scalar string record as input, and\r\nso follow [the instructions to add an Op](../../how_tos/adding_an_op/index.md).  You may\r\noptionally take a scalar string key as input, and include that in error messages\r\nreporting improperly formatted data.  That way users can more easily track down\r\nwhere the bad data came from.\r\n\r\nExamples of Ops useful for decoding records:\r\n\r\n*   [`tf.parse_single_example`](../../api_docs/python/io_ops.md#parse_single_example)\r\n    (and\r\n    [`tf.parse_example`](../../api_docs/python/io_ops.md#parse_example))\r\n*   [`tf.decode_csv`](../../api_docs/python/io_ops.md#decode_csv)\r\n*   [`tf.decode_raw`](../../api_docs/python/io_ops.md#decode_raw)\r\n\r\nNote that it can be useful to use multiple Ops to decode a particular record\r\nformat.  For example, you may have an image saved as a string in\r\n[a `tf.train.Example` protocol buffer](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/example.proto).\r\nDepending on the format of that image, you might take the corresponding output\r\nfrom a\r\n[`tf.parse_single_example`](../../api_docs/python/io_ops.md#parse_single_example)\r\nop and call [`tf.decode_jpeg`](../../api_docs/python/image.md#decode_jpeg),\r\n[`tf.decode_png`](../../api_docs/python/image.md#decode_png), or\r\n[`tf.decode_raw`](../../api_docs/python/io_ops.md#decode_raw).  It is common to\r\ntake the output of `tf.decode_raw` and use\r\n[`tf.slice`](../../api_docs/python/array_ops.md#slice) and\r\n[`tf.reshape`](../../api_docs/python/array_ops.md#reshape) to extract pieces.\r\n"
  },
  {
    "path": "SOURCE/how_tos/new_data_formats.md",
    "content": "# 自定义数据读取 <a class=\"md-anchor\" id=\"AUTOGENERATED-custom-data-readers\"></a>\r\n\r\n基本要求:\r\n\r\n* 熟悉 C++ 编程。\r\n* 确保[下载 TensorFlow 源文件](tensorflow-zh/SOURCE/get_started/os_setup.md#source), 并可编译使用。\r\n\r\n我们将支持文件格式的任务分成两部分：\r\n\r\n* 文件格式: 我们使用 *Reader* Op来从文件中读取一个 *record* (可以使任意字符串)。\r\n* 记录格式: 我们使用解码器或者解析运算将一个字符串记录转换为TensorFlow可以使用的张量。\r\n\r\n例如， 读取一个\r\n[CSV 文件](https://en.wikipedia.org/wiki/Comma-separated_values)，我们使用\r\n[一个文本读写器](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#TextLineReader)，\r\n然后是[从一行文本中解析CSV数据的运算](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#decode_csv)。\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n\r\n## 主要内容\r\n\r\n### [自定义数据读取](#AUTOGENERATED-custom-data-readers)\r\n\r\n* [编写一个文件格式读写器](#AUTOGENERATED-writing-a-reader-for-a-file-format)\r\n* [编写一个记录格式Op](#AUTOGENERATED-writing-an-op-for-a-record-format)\r\n\r\n## 编写一个文件格式读写器 <a class=\"md-anchor\" id=\"AUTOGENERATED-writing-a-reader-for-a-file-format\"></a>\r\n\r\nReader 是专门用来读取文件中的记录的。TensorFlow中内建了一些读写器Op的实例：\r\n\r\n* [tf.TFRecordReader](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#TFRecordReader)\r\n    ([代码位于kernels/tf_record_reader_op.cc](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/tf_record_reader_op.cc))\r\n* [tf.FixedLengthRecordReader](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#FixedLengthRecordReader)\r\n    ([代码位于 kernels/fixed_length_record_reader_op.cc](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/fixed_length_record_reader_op.cc))\r\n* [tf.TextLineReader](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#TextLineReader)\r\n    ([代码位于 kernels/text_line_reader_op.cc](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/text_line_reader_op.cc))\r\n\r\n你可以看到这些读写器的界面是一样的，唯一的差异是在它们的构造函数中。最重要的方法是 Read。\r\n它需要一个行列参数，通过这个行列参数，可以在需要的时候随时读取文件名 (例如： 当 Read Op首次运行，或者前一个 Read` 从一个文件中读取最后一条记录时)。它将会生成两个标量张量： 一个字符串和一个字符串关键值。\r\n\r\n新创建一个名为 SomeReader 的读写器，需要以下步骤：\r\n\r\n1. 在 C++ 中, 定义一个\r\n    [tensorflow::ReaderBase](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/reader_base.h)的子类，命名为\r\n     \"SomeReader\".\r\n2. 在 C++ 中，注册一个新的读写器Op和Kernel，命名为 \"SomeReader\"。\r\n3. 在 Python 中, 定义一个 [tf.ReaderBase](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/io_ops.py) 的子类，命名为 \"SomeReader\"。\r\n\r\n你可以把所有的 C++ 代码放在\r\n`tensorflow/core/user_ops/some_reader_op.cc`文件中.  读取文件的代码将被嵌入到C++ 的 ReaderBase 类的迭代中。 这个 ReaderBase 类 是在 [tensorflow/core/kernels/reader_base.h](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/reader_base.h) 中定义的。\r\n你需要执行以下的方法：\r\n\r\n* OnWorkStartedLocked：打开下一个文件\r\n* ReadLocked：读取一个记录或报告 EOF/error\r\n* OnWorkFinishedLocked：关闭当前文件\r\n* ResetLocked：清空记录，例如：一个错误记录\r\n\r\n以上这些方法的名字后面都带有 \"Locked\"， 表示  ReaderBase 在调用任何一个方法之前确保获得互斥锁，这样就不用担心线程安全（虽然只保护了该类中的元素而不是全局的）。\r\n\r\n对于 OnWorkStartedLocked, 需要打开的文件名是 `current_work()` 函数的返回值。此时的 ReadLocked 的数字签名如下:\r\n\r\n```\r\nStatus ReadLocked(string* key, string* value, bool* produced, bool* at_end)\r\n```\r\n\r\n如果 ReadLocked 从文件中成功读取了一条记录，它将更新为：\r\n\r\n* *key： 记录的标志位，通过该标志位可以重新定位到该记录。 可以包含从 current_work() 返回值获得的文件名，并追加一个记录号或其他信息。\r\n* *value： 包含记录的内容。\r\n* *produced： 设置为 true。\r\n\r\n当你在文件（EOF）末尾，设置 *at_end 为 true ，在任何情况下，都将返回 Status::OK()。 当出现错误的时候，只需要使用\r\n[tensorflow/core/lib/core/errors.h](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/lib/core/errors.h) 中的一个辅助功能就可以简单地返回，不需要做任何参数修改。\r\n\r\n接下来你讲创建一个实际的读写器Op。 如果你已经熟悉了[添加新的Op](tensorflow-zh/SOURCE/how_tos/adding_an_op/index.md) 那会很有帮助。 主要步骤如下：\r\n\r\n* 注册Op。\r\n* 定义并注册 OpKernel。\r\n\r\n要注册Op，你需要用到一个调用指令定义在\r\n[tensorflow/core/framework/op.h](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op.h)中的REGISTER_OP。\r\n\r\n读写器 Op 没有输入，只有 Ref(string) 类型的单输出。它们调用 SetIsStateful()，并有一个\r\ncontainer 字符串和 shared_name 属性.  你可以在一个 Doc 中定义配置或包含文档的额外属性。 例如：详见\r\n[tensorflow/core/ops/io_ops.cc](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/ops/io_ops.cc)等：\r\n\r\n```\r\n #include \"tensorflow/core/framework/op.h\"\r\nREGISTER_OP(\"TextLineReader\")\r\n    .Output(\"reader_handle: Ref(string)\")\r\n    .Attr(\"skip_header_lines: int = 0\")\r\n    .Attr(\"container: string = ''\")\r\n    .Attr(\"shared_name: string = ''\")\r\n    .SetIsStateful()\r\n    .Doc(R\"doc(\r\nA Reader that outputs the lines of a file delimited by '\\n'.\r\n)doc\");\r\n```\r\n要定义一个 OpKernel， 读写器可以使用定义在[tensorflow/core/framework/reader_op_kernel.h](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/reader_op_kernel.h)中的 ReaderOpKernel 的递减快捷方式，并运行一个叫 SetReaderFactory 的构造函数。\r\n定义所需要的类之后，你需要通过 REGISTER_KERNEL_BUILDER(...) 注册这个类。\r\n\r\n一个没有属性的例子：\r\n\r\n```\r\n #include \"tensorflow/core/framework/reader_op_kernel.h\"\r\nclass TFRecordReaderOp : public ReaderOpKernel {\r\n public:\r\n  explicit TFRecordReaderOp(OpKernelConstruction* context)\r\n      : ReaderOpKernel(context) {\r\n    Env* env = context->env();\r\n    SetReaderFactory([this, env]() { return new TFRecordReader(name(), env); });\r\n  }\r\n};\r\nREGISTER_KERNEL_BUILDER(Name(\"TFRecordReader\").Device(DEVICE_CPU),\r\n                        TFRecordReaderOp);\r\n```\r\n一个带有属性的例子：\r\n\r\n```\r\n #include \"tensorflow/core/framework/reader_op_kernel.h\"\r\nclass TextLineReaderOp : public ReaderOpKernel {\r\n public:\r\n  explicit TextLineReaderOp(OpKernelConstruction* context)\r\n      : ReaderOpKernel(context) {\r\n    int skip_header_lines = -1;\r\n    OP_REQUIRES_OK(context,\r\n                   context->GetAttr(\"skip_header_lines\", &skip_header_lines));\r\n    OP_REQUIRES(context, skip_header_lines >= 0,\r\n                errors::InvalidArgument(\"skip_header_lines must be >= 0 not \",\r\n                                        skip_header_lines));\r\n    Env* env = context->env();\r\n    SetReaderFactory([this, skip_header_lines, env]() {\r\n      return new TextLineReader(name(), skip_header_lines, env);\r\n    });\r\n  }\r\n};\r\nREGISTER_KERNEL_BUILDER(Name(\"TextLineReader\").Device(DEVICE_CPU),\r\n                        TextLineReaderOp);\r\n```\r\n\r\n最后一步是添加 Python 包装器，你需要将 tensorflow.python.ops.io_ops 导入到\r\n[tensorflow/python/user_ops/user_ops.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/user_ops/user_ops.py)，并添加一个 [io_ops.ReaderBase](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/io_ops.py)的衍生函数。\r\n\r\n```\r\nfrom tensorflow.python.framework import ops\r\nfrom tensorflow.python.ops import common_shapes\r\nfrom tensorflow.python.ops import io_ops\r\nclass SomeReader(io_ops.ReaderBase):\r\n    def __init__(self, name=None):\r\n        rr = gen_user_ops.some_reader(name=name)\r\n        super(SomeReader, self).__init__(rr)\r\nops.NoGradient(\"SomeReader\")\r\nops.RegisterShape(\"SomeReader\")(common_shapes.scalar_shape)\r\n```\r\n\r\n你可以在\r\n[tensorflow/python/ops/io_ops.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/io_ops.py)中查看一些范例。\r\n\r\n## 编写一个记录格式Op <a class=\"md-anchor\" id=\"AUTOGENERATED-writing-an-op-for-a-record-format\"></a>\r\n\r\n一般来说，这是一个普通的Op， 需要一个标量字符串记录作为输入， 因此遵循 [添加Op的说明](tensorflow-zh/SOURCE/how_tos/adding_an_op/index.md)。 你可以选择一个标量字符串作为输入， 并包含在错误消息中报告不正确的格式化数据。\r\n\r\n用于解码记录的运算实例：\r\n\r\n* [tf.parse_single_example](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#parse_single_example)\r\n    (and [tf.parse_example](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#parse_example))\r\n* [tf.decode_csv](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#decode_csv)\r\n* [tf.decode_raw](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#decode_raw)\r\n\r\n请注意，使用多个Op 来解码某个特定的记录格式也是有效的。 例如，你有一张以字符串格式保存在\r\n[tf.train.Example 协议缓冲区](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/example.proto)的图像文件。\r\n根据该图像的格式， 你可能从\r\n[tf.parse_single_example](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#parse_single_example) 的Op 读取响应输出并调用 [tf.decode_jpeg](tensorflow-zh/SOURCE/api_docs/python/image.md#decode_jpeg)，\r\n[tf.decode_png](tensorflow-zh/SOURCE/api_docs/python/image.md#decode_png)， 或者\r\n[tf.decode_raw](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#decode_raw)。通过读取 tf.decode_raw 的响应输出并使用[tf.slice](tensorflow-zh/SOURCE/api_docs/python/array_ops.md#slice) 和\r\n[tf.reshape](tensorflow-zh/SOURCE/api_docs/python/array_ops.md#reshape) 来提取数据是通用的方法。\r\n> 原文：[Custom Data Readers](http://tensorflow.org/how_tos/new_data_formats/index.html#custom-data-readers)  翻译：[@derekshang](https://github.com/derekshang)  校对：[Wiki](https://github.com/jikexueyuanwiki)\r\n"
  },
  {
    "path": "SOURCE/how_tos/overview.md",
    "content": "# 综述 Overview\r\n\r\n## Variables: 创建，初始化，保存，和恢复\r\n\r\nTensorFlow Variables 是内存中的容纳 tensor 的缓存。这一小节介绍了用它们在模型训练时(during training)创建、保存和更新模型参数(model parameters) 的方法。\r\n\r\n[参看教程](../how_tos/variables.md)\r\n\r\n## TensorFlow 机制 101 \r\n\r\n用 MNIST 手写数字识别作为一个小例子，一步一步的将使用 TensorFlow 基础架构(infrastructure)训练大规模模型的细节做详细介绍。\r\n\r\n[参看教程](../tutorials/mnist_tf.md)\r\n\r\n## TensorBoard: 学习过程的可视化 \r\n\r\n对模型进行训练和评估时，TensorBoard 是一个很有用的可视化工具。此教程解释了创建和运行 TensorBoard 的方法，和使用摘要操作(Summary ops)的方法，通过添加摘要操作(Summary ops)，可以自动把数据传输到 TensorBoard 所使用的事件文件。\r\n\r\n[参看教程](../how_tos/summaries_and_tensorboard.md)\r\n\r\n## TensorBoard: 图的可视化 \r\n\r\n此教程介绍了在 TensorBoard 中使用可视化工具的方法，它可以帮助你理解张量流图的过程并 debug。\r\n\r\n[参看教程](../how_tos/graph_viz.md)\r\n\r\n## 数据读入 \r\n\r\n此教程介绍了把数据传入 TensorSlow 程序的三种主要的方法： Feeding, Reading 和 Preloading.\r\n\r\n[参看教程](../how_tos/reading_data.md)\r\n\r\n## 线程和队列 \r\n\r\n此教程介绍 TensorFlow 中为了更容易进行异步和并发训练的各种不同结构(constructs)。\r\n\r\n[参看教程](../how_tos/threading_and_queues.md)\r\n\r\n## 添加新的 Op \r\n\r\nTensorFlow 已经提供一整套节点操作(）operation)，你可以在你的 graph 中随意使用它们，不过这里有关于添加自定义操作(custom op)的细节。\r\n\r\n[参看教程](../how_tos/adding_an_op.md)。\r\n\r\n## 自定义数据的 Readers \r\n\r\n如果你有相当大量的自定义数据集合，可能你想要对 TensorFlow 的 Data Readers 进行扩展，使它能直接以数据自身的格式将其读入。\r\n\r\n[参看教程](../how_tos/new_data_formats.md)。\r\n\r\n## 使用 GPUs \r\n\r\n此教程描述了用多个 GPU 构建和运行模型的方法。\r\n\r\n[参看教程](../how_tos/using_gpu.md)\r\n\r\n## 共享变量 Sharing Variables\r\n\r\n当在多 GPU 上部署大型的模型，或展开复杂的 LSTMs 或 RNNs 时，在模型构建代码的不同位置对许多相同的变量（Variable）进行读写常常是必须的。设计变量作用域（Variable Scope）机制的目的就是为了帮助上述任务的实现。\r\n\r\n[参看教程](../how_tos/variable_scope/index.md)。\r\n\r\n原文： [How-to](http://tensorflow.org/how_tos/index.html) \r\n\r\n翻译：[Terence Cooper](https://github.com/TerenceCooper) \r\n\r\n校对：[lonlonago](https://github.com/lonlonago)\r\n<div class='sections-order' style=\"display: none;\">\r\n<!-- variables/index.md -->\r\n<!-- ../tutorials/mnist/tf/index.md -->\r\n<!-- summaries_and_tensorboard/index.md -->\r\n<!-- graph_viz/index.md -->\r\n<!-- reading_data/index.md -->\r\n<!-- threading_and_queues/index.md -->\r\n<!-- adding_an_op/index.md -->\r\n<!-- new_data_formats/index.md -->\r\n<!-- using_gpu/index.md -->\r\n<!-- variable_scope/index.md -->\r\n</div>\r\n\r\n"
  },
  {
    "path": "SOURCE/how_tos/reading_data/__init__.py",
    "content": ""
  },
  {
    "path": "SOURCE/how_tos/reading_data/convert_to_records.py",
    "content": "\"\"\"Converts MNIST data to TFRecords file format with Example protos.\"\"\"\r\nfrom __future__ import print_function\r\n\r\nimport os\r\nimport tensorflow.python.platform\r\n\r\nimport numpy\r\nimport tensorflow as tf\r\nfrom tensorflow.g3doc.tutorials.mnist import input_data\r\n\r\n\r\nTRAIN_IMAGES = 'train-images-idx3-ubyte.gz'  # MNIST filenames\r\nTRAIN_LABELS = 'train-labels-idx1-ubyte.gz'\r\nTEST_IMAGES = 't10k-images-idx3-ubyte.gz'\r\nTEST_LABELS = 't10k-labels-idx1-ubyte.gz'\r\n\r\n\r\ntf.app.flags.DEFINE_string('directory', 'data',\r\n                           'Directory to download data files and write the '\r\n                           'converted result')\r\ntf.app.flags.DEFINE_integer('validation_size', 5000,\r\n                            'Number of examples to separate from the training '\r\n                            'data for the validation set.')\r\nFLAGS = tf.app.flags.FLAGS\r\n\r\n\r\ndef _int64_feature(value):\r\n  return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))\r\n\r\n\r\ndef _bytes_feature(value):\r\n  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))\r\n\r\n\r\ndef convert_to(images, labels, name):\r\n  num_examples = labels.shape[0]\r\n  if images.shape[0] != num_examples:\r\n    raise ValueError(\"Images size %d does not match label size %d.\" %\r\n                     (dat.shape[0], num_examples))\r\n  rows = images.shape[1]\r\n  cols = images.shape[2]\r\n  depth = images.shape[3]\r\n\r\n  filename = os.path.join(FLAGS.directory, name + '.tfrecords')\r\n  print('Writing', filename)\r\n  writer = tf.python_io.TFRecordWriter(filename)\r\n  for index in range(num_examples):\r\n    image_raw = images[index].tostring()\r\n    example = tf.train.Example(features=tf.train.Features(feature={\r\n        'height': _int64_feature(rows),\r\n        'width': _int64_feature(cols),\r\n        'depth': _int64_feature(depth),\r\n        'label': _int64_feature(int(labels[index])),\r\n        'image_raw': _bytes_feature(image_raw)}))\r\n    writer.write(example.SerializeToString())\r\n\r\n\r\ndef main(argv):\r\n  # Get the data.\r\n  train_images_filename = input_data.maybe_download(\r\n      TRAIN_IMAGES, FLAGS.directory)\r\n  train_labels_filename = input_data.maybe_download(\r\n      TRAIN_LABELS, FLAGS.directory)\r\n  test_images_filename = input_data.maybe_download(\r\n      TEST_IMAGES, FLAGS.directory)\r\n  test_labels_filename = input_data.maybe_download(\r\n      TEST_LABELS, FLAGS.directory)\r\n\r\n  # Extract it into numpy arrays.\r\n  train_images = input_data.extract_images(train_images_filename)\r\n  train_labels = input_data.extract_labels(train_labels_filename)\r\n  test_images = input_data.extract_images(test_images_filename)\r\n  test_labels = input_data.extract_labels(test_labels_filename)\r\n\r\n  # Generate a validation set.\r\n  validation_images = train_images[:FLAGS.validation_size, :, :, :]\r\n  validation_labels = train_labels[:FLAGS.validation_size]\r\n  train_images = train_images[FLAGS.validation_size:, :, :, :]\r\n  train_labels = train_labels[FLAGS.validation_size:]\r\n\r\n  # Convert to Examples and write the result to TFRecords.\r\n  convert_to(train_images, train_labels, 'train')\r\n  convert_to(validation_images, validation_labels, 'validation')\r\n  convert_to(test_images, test_labels, 'test')\r\n\r\n\r\nif __name__ == '__main__':\r\n  tf.app.run()\r\n"
  },
  {
    "path": "SOURCE/how_tos/reading_data/fully_connected_preloaded.py",
    "content": "\"\"\"Trains the MNIST network using preloaded data in a constant.\r\n\r\nCommand to run this py_binary target:\r\n\r\nbazel run -c opt \\\r\n    <...>/tensorflow/g3doc/how_tos/reading_data:fully_connected_preloaded\r\n\"\"\"\r\nfrom __future__ import print_function\r\nimport os.path\r\nimport time\r\n\r\nimport tensorflow.python.platform\r\nimport numpy\r\nimport tensorflow as tf\r\n\r\nfrom tensorflow.g3doc.tutorials.mnist import input_data\r\nfrom tensorflow.g3doc.tutorials.mnist import mnist\r\n\r\n\r\n# Basic model parameters as external flags.\r\nflags = tf.app.flags\r\nFLAGS = flags.FLAGS\r\nflags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')\r\nflags.DEFINE_integer('num_epochs', 2, 'Number of epochs to run trainer.')\r\nflags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.')\r\nflags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.')\r\nflags.DEFINE_integer('batch_size', 100, 'Batch size.  '\r\n                     'Must divide evenly into the dataset sizes.')\r\nflags.DEFINE_string('train_dir', 'data', 'Directory to put the training data.')\r\nflags.DEFINE_boolean('fake_data', False, 'If true, uses fake data '\r\n                     'for unit testing.')\r\n\r\n\r\ndef run_training():\r\n  \"\"\"Train MNIST for a number of epochs.\"\"\"\r\n  # Get the sets of images and labels for training, validation, and\r\n  # test on MNIST.\r\n  data_sets = input_data.read_data_sets(FLAGS.train_dir, FLAGS.fake_data)\r\n\r\n  # Tell TensorFlow that the model will be built into the default Graph.\r\n  with tf.Graph().as_default():\r\n    with tf.name_scope('input'):\r\n      # Input data\r\n      input_images = tf.constant(data_sets.train.images)\r\n      input_labels = tf.constant(data_sets.train.labels)\r\n\r\n      image, label = tf.train.slice_input_producer(\r\n          [input_images, input_labels], num_epochs=FLAGS.num_epochs)\r\n      label = tf.cast(label, tf.int32)\r\n      images, labels = tf.train.batch(\r\n          [image, label], batch_size=FLAGS.batch_size)\r\n\r\n    # Build a Graph that computes predictions from the inference model.\r\n    logits = mnist.inference(images, FLAGS.hidden1, FLAGS.hidden2)\r\n\r\n    # Add to the Graph the Ops for loss calculation.\r\n    loss = mnist.loss(logits, labels)\r\n\r\n    # Add to the Graph the Ops that calculate and apply gradients.\r\n    train_op = mnist.training(loss, FLAGS.learning_rate)\r\n\r\n    # Add the Op to compare the logits to the labels during evaluation.\r\n    eval_correct = mnist.evaluation(logits, labels)\r\n\r\n    # Build the summary operation based on the TF collection of Summaries.\r\n    summary_op = tf.merge_all_summaries()\r\n\r\n    # Create a saver for writing training checkpoints.\r\n    saver = tf.train.Saver()\r\n\r\n    # Create the op for initializing variables.\r\n    init_op = tf.initialize_all_variables()\r\n\r\n    # Create a session for running Ops on the Graph.\r\n    sess = tf.Session()\r\n\r\n    # Run the Op to initialize the variables.\r\n    sess.run(init_op)\r\n\r\n    # Instantiate a SummaryWriter to output summaries and the Graph.\r\n    summary_writer = tf.train.SummaryWriter(FLAGS.train_dir,\r\n                                            graph_def=sess.graph_def)\r\n\r\n    # Start input enqueue threads.\r\n    coord = tf.train.Coordinator()\r\n    threads = tf.train.start_queue_runners(sess=sess, coord=coord)\r\n\r\n    # And then after everything is built, start the training loop.\r\n    try:\r\n      step = 0\r\n      while not coord.should_stop():\r\n        start_time = time.time()\r\n\r\n        # Run one step of the model.\r\n        _, loss_value = sess.run([train_op, loss])\r\n\r\n        duration = time.time() - start_time\r\n\r\n        # Write the summaries and print an overview fairly often.\r\n        if step % 100 == 0:\r\n          # Print status to stdout.\r\n          print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value,\r\n                                                     duration))\r\n          # Update the events file.\r\n          summary_str = sess.run(summary_op)\r\n          summary_writer.add_summary(summary_str, step)\r\n          step += 1\r\n\r\n        # Save a checkpoint periodically.\r\n        if (step + 1) % 1000 == 0:\r\n          print('Saving')\r\n          saver.save(sess, FLAGS.train_dir, global_step=step)\r\n\r\n        step += 1\r\n    except tf.errors.OutOfRangeError:\r\n      print('Saving')\r\n      saver.save(sess, FLAGS.train_dir, global_step=step)\r\n      print('Done training for %d epochs, %d steps.' % (FLAGS.num_epochs, step))\r\n    finally:\r\n      # When done, ask the threads to stop.\r\n      coord.request_stop()\r\n\r\n    # Wait for threads to finish.\r\n    coord.join(threads)\r\n    sess.close()\r\n\r\n\r\ndef main(_):\r\n  run_training()\r\n\r\n\r\nif __name__ == '__main__':\r\n  tf.app.run()\r\n"
  },
  {
    "path": "SOURCE/how_tos/reading_data/fully_connected_preloaded_var.py",
    "content": "\"\"\"Trains the MNIST network using preloaded data stored in a variable.\r\n\r\nCommand to run this py_binary target:\r\n\r\nbazel run -c opt \\\r\n    <...>/tensorflow/g3doc/how_tos/reading_data:fully_connected_preloaded_var\r\n\"\"\"\r\nfrom __future__ import print_function\r\nimport os.path\r\nimport time\r\n\r\nimport tensorflow.python.platform\r\nimport numpy\r\nimport tensorflow as tf\r\n\r\nfrom tensorflow.g3doc.tutorials.mnist import input_data\r\nfrom tensorflow.g3doc.tutorials.mnist import mnist\r\n\r\n\r\n# Basic model parameters as external flags.\r\nflags = tf.app.flags\r\nFLAGS = flags.FLAGS\r\nflags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')\r\nflags.DEFINE_integer('num_epochs', 2, 'Number of epochs to run trainer.')\r\nflags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.')\r\nflags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.')\r\nflags.DEFINE_integer('batch_size', 100, 'Batch size.  '\r\n                     'Must divide evenly into the dataset sizes.')\r\nflags.DEFINE_string('train_dir', 'data', 'Directory to put the training data.')\r\nflags.DEFINE_boolean('fake_data', False, 'If true, uses fake data '\r\n                     'for unit testing.')\r\n\r\n\r\ndef run_training():\r\n  \"\"\"Train MNIST for a number of epochs.\"\"\"\r\n  # Get the sets of images and labels for training, validation, and\r\n  # test on MNIST.\r\n  data_sets = input_data.read_data_sets(FLAGS.train_dir, FLAGS.fake_data)\r\n\r\n  # Tell TensorFlow that the model will be built into the default Graph.\r\n  with tf.Graph().as_default():\r\n    with tf.name_scope('input'):\r\n      # Input data\r\n      images_initializer = tf.placeholder(\r\n          dtype=data_sets.train.images.dtype,\r\n          shape=data_sets.train.images.shape)\r\n      labels_initializer = tf.placeholder(\r\n          dtype=data_sets.train.labels.dtype,\r\n          shape=data_sets.train.labels.shape)\r\n      input_images = tf.Variable(\r\n          images_initializer, trainable=False, collections=[])\r\n      input_labels = tf.Variable(\r\n          labels_initializer, trainable=False, collections=[])\r\n\r\n      image, label = tf.train.slice_input_producer(\r\n          [input_images, input_labels], num_epochs=FLAGS.num_epochs)\r\n      label = tf.cast(label, tf.int32)\r\n      images, labels = tf.train.batch(\r\n          [image, label], batch_size=FLAGS.batch_size)\r\n\r\n    # Build a Graph that computes predictions from the inference model.\r\n    logits = mnist.inference(images, FLAGS.hidden1, FLAGS.hidden2)\r\n\r\n    # Add to the Graph the Ops for loss calculation.\r\n    loss = mnist.loss(logits, labels)\r\n\r\n    # Add to the Graph the Ops that calculate and apply gradients.\r\n    train_op = mnist.training(loss, FLAGS.learning_rate)\r\n\r\n    # Add the Op to compare the logits to the labels during evaluation.\r\n    eval_correct = mnist.evaluation(logits, labels)\r\n\r\n    # Build the summary operation based on the TF collection of Summaries.\r\n    summary_op = tf.merge_all_summaries()\r\n\r\n    # Create a saver for writing training checkpoints.\r\n    saver = tf.train.Saver()\r\n\r\n    # Create the op for initializing variables.\r\n    init_op = tf.initialize_all_variables()\r\n\r\n    # Create a session for running Ops on the Graph.\r\n    sess = tf.Session()\r\n\r\n    # Run the Op to initialize the variables.\r\n    sess.run(init_op)\r\n    sess.run(input_images.initializer,\r\n             feed_dict={images_initializer: data_sets.train.images})\r\n    sess.run(input_labels.initializer,\r\n             feed_dict={labels_initializer: data_sets.train.labels})\r\n\r\n    # Instantiate a SummaryWriter to output summaries and the Graph.\r\n    summary_writer = tf.train.SummaryWriter(FLAGS.train_dir,\r\n                                            graph_def=sess.graph_def)\r\n\r\n    # Start input enqueue threads.\r\n    coord = tf.train.Coordinator()\r\n    threads = tf.train.start_queue_runners(sess=sess, coord=coord)\r\n\r\n    # And then after everything is built, start the training loop.\r\n    try:\r\n      step = 0\r\n      while not coord.should_stop():\r\n        start_time = time.time()\r\n\r\n        # Run one step of the model.\r\n        _, loss_value = sess.run([train_op, loss])\r\n\r\n        duration = time.time() - start_time\r\n\r\n        # Write the summaries and print an overview fairly often.\r\n        if step % 100 == 0:\r\n          # Print status to stdout.\r\n          print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value,\r\n                                                     duration))\r\n          # Update the events file.\r\n          summary_str = sess.run(summary_op)\r\n          summary_writer.add_summary(summary_str, step)\r\n          step += 1\r\n\r\n        # Save a checkpoint periodically.\r\n        if (step + 1) % 1000 == 0:\r\n          print('Saving')\r\n          saver.save(sess, FLAGS.train_dir, global_step=step)\r\n\r\n        step += 1\r\n    except tf.errors.OutOfRangeError:\r\n      print('Saving')\r\n      saver.save(sess, FLAGS.train_dir, global_step=step)\r\n      print('Done training for %d epochs, %d steps.' % (FLAGS.num_epochs, step))\r\n    finally:\r\n      # When done, ask the threads to stop.\r\n      coord.request_stop()\r\n\r\n    # Wait for threads to finish.\r\n    coord.join(threads)\r\n    sess.close()\r\n\r\n\r\ndef main(_):\r\n  run_training()\r\n\r\n\r\nif __name__ == '__main__':\r\n  tf.app.run()\r\n"
  },
  {
    "path": "SOURCE/how_tos/reading_data/fully_connected_reader.py",
    "content": "\"\"\"Train and Eval the MNIST network.\r\n\r\nThis version is like fully_connected_feed.py but uses data converted\r\nto a TFRecords file containing tf.train.Example protocol buffers.\r\nSee tensorflow/g3doc/how_tos/reading_data.md#reading-from-files\r\nfor context.\r\n\r\nYOU MUST run convert_to_records before running this (but you only need to\r\nrun it once).\r\n\"\"\"\r\nfrom __future__ import print_function\r\n\r\nimport os.path\r\nimport time\r\n\r\nimport tensorflow.python.platform\r\nimport numpy\r\nimport tensorflow as tf\r\n\r\nfrom tensorflow.g3doc.tutorials.mnist import mnist\r\n\r\n\r\n# Basic model parameters as external flags.\r\nflags = tf.app.flags\r\nFLAGS = flags.FLAGS\r\nflags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')\r\nflags.DEFINE_integer('num_epochs', 2, 'Number of epochs to run trainer.')\r\nflags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.')\r\nflags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.')\r\nflags.DEFINE_integer('batch_size', 100, 'Batch size.')\r\nflags.DEFINE_string('train_dir', 'data', 'Directory with the training data.')\r\n\r\n# Constants used for dealing with the files, matches convert_to_records.\r\nTRAIN_FILE = 'train.tfrecords'\r\nVALIDATION_FILE = 'validation.tfrecords'\r\n\r\n\r\ndef read_and_decode(filename_queue):\r\n  reader = tf.TFRecordReader()\r\n  _, serialized_example = reader.read(filename_queue)\r\n  features = tf.parse_single_example(\r\n      serialized_example,\r\n      dense_keys=['image_raw', 'label'],\r\n      # Defaults are not specified since both keys are required.\r\n      dense_types=[tf.string, tf.int64])\r\n\r\n  # Convert from a scalar string tensor (whose single string has\r\n  # length mnist.IMAGE_PIXELS) to a uint8 tensor with shape\r\n  # [mnist.IMAGE_PIXELS].\r\n  image = tf.decode_raw(features['image_raw'], tf.uint8)\r\n  image.set_shape([mnist.IMAGE_PIXELS])\r\n\r\n  # OPTIONAL: Could reshape into a 28x28 image and apply distortions\r\n  # here.  Since we are not applying any distortions in this\r\n  # example, and the next step expects the image to be flattened\r\n  # into a vector, we don't bother.\r\n\r\n  # Convert from [0, 255] -> [-0.5, 0.5] floats.\r\n  image = tf.cast(image, tf.float32) * (1. / 255) - 0.5\r\n\r\n  # Convert label from a scalar uint8 tensor to an int32 scalar.\r\n  label = tf.cast(features['label'], tf.int32)\r\n\r\n  return image, label\r\n\r\n\r\ndef inputs(train, batch_size, num_epochs):\r\n  \"\"\"Reads input data num_epochs times.\r\n\r\n  Args:\r\n    train: Selects between the training (True) and validation (False) data.\r\n    batch_size: Number of examples per returned batch.\r\n    num_epochs: Number of times to read the input data, or 0/None to\r\n       train forever.\r\n\r\n  Returns:\r\n    A tuple (images, labels), where:\r\n    * images is a float tensor with shape [batch_size, mnist.IMAGE_PIXELS]\r\n      in the range [-0.5, 0.5].\r\n    * labels is an int32 tensor with shape [batch_size] with the true label,\r\n      a number in the range [0, mnist.NUM_CLASSES).\r\n    Note that an tf.train.QueueRunner is added to the graph, which\r\n    must be run using e.g. tf.train.start_queue_runners().\r\n  \"\"\"\r\n  if not num_epochs: num_epochs = None\r\n  filename = os.path.join(FLAGS.train_dir,\r\n                          TRAIN_FILE if train else VALIDATION_FILE)\r\n\r\n  with tf.name_scope('input'):\r\n    filename_queue = tf.train.string_input_producer(\r\n        [filename], num_epochs=num_epochs)\r\n\r\n    # Even when reading in multiple threads, share the filename\r\n    # queue.\r\n    image, label = read_and_decode(filename_queue)\r\n\r\n    # Shuffle the examples and collect them into batch_size batches.\r\n    # (Internally uses a RandomShuffleQueue.)\r\n    # We run this in two threads to avoid being a bottleneck.\r\n    images, sparse_labels = tf.train.shuffle_batch(\r\n        [image, label], batch_size=batch_size, num_threads=2,\r\n        capacity=1000 + 3 * batch_size,\r\n        # Ensures a minimum amount of shuffling of examples.\r\n        min_after_dequeue=1000)\r\n\r\n    return images, sparse_labels\r\n\r\n\r\ndef run_training():\r\n  \"\"\"Train MNIST for a number of steps.\"\"\"\r\n\r\n  # Tell TensorFlow that the model will be built into the default Graph.\r\n  with tf.Graph().as_default():\r\n    # Input images and labels.\r\n    images, labels = inputs(train=True, batch_size=FLAGS.batch_size,\r\n                            num_epochs=FLAGS.num_epochs)\r\n\r\n    # Build a Graph that computes predictions from the inference model.\r\n    logits = mnist.inference(images,\r\n                             FLAGS.hidden1,\r\n                             FLAGS.hidden2)\r\n\r\n    # Add to the Graph the loss calculation.\r\n    loss = mnist.loss(logits, labels)\r\n\r\n    # Add to the Graph operations that train the model.\r\n    train_op = mnist.training(loss, FLAGS.learning_rate)\r\n\r\n    # The op for initializing the variables.\r\n    init_op = tf.initialize_all_variables()\r\n\r\n    # Create a session for running operations in the Graph.\r\n    sess = tf.Session()\r\n\r\n    # Initialize the variables (the trained variables and the\r\n    # epoch counter).\r\n    sess.run(init_op)\r\n\r\n    # Start input enqueue threads.\r\n    coord = tf.train.Coordinator()\r\n    threads = tf.train.start_queue_runners(sess=sess, coord=coord)\r\n\r\n    try:\r\n      step = 0\r\n      while not coord.should_stop():\r\n        start_time = time.time()\r\n\r\n        # Run one step of the model.  The return values are\r\n        # the activations from the `train_op` (which is\r\n        # discarded) and the `loss` op.  To inspect the values\r\n        # of your ops or variables, you may include them in\r\n        # the list passed to sess.run() and the value tensors\r\n        # will be returned in the tuple from the call.\r\n        _, loss_value = sess.run([train_op, loss])\r\n\r\n        duration = time.time() - start_time\r\n\r\n        # Print an overview fairly often.\r\n        if step % 100 == 0:\r\n          print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value,\r\n                                                     duration))\r\n        step += 1\r\n    except tf.errors.OutOfRangeError:\r\n      print('Done training for %d epochs, %d steps.' % (FLAGS.num_epochs, step))\r\n    finally:\r\n      # When done, ask the threads to stop.\r\n      coord.request_stop()\r\n\r\n    # Wait for threads to finish.\r\n    coord.join(threads)\r\n    sess.close()\r\n\r\n\r\ndef main(_):\r\n  run_training()\r\n\r\n\r\nif __name__ == '__main__':\r\n  tf.app.run()\r\n"
  },
  {
    "path": "SOURCE/how_tos/reading_data/index.md",
    "content": "# Reading data <a class=\"md-anchor\" id=\"AUTOGENERATED-reading-data\"></a>\r\n\r\nThere are three main methods of getting data into a TensorFlow program:\r\n\r\n*   Feeding: Python code provides the data when running each step.\r\n*   Reading from files: an input pipeline reads the data from files\r\n    at the beginning of a TensorFlow graph.\r\n*   Preloaded data: a constant or variable in the TensorFlow graph holds\r\n    all the data (for small data sets).\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## Contents\r\n### [Reading data](#AUTOGENERATED-reading-data)\r\n* [Feeding](#Feeding)\r\n* [Reading from files](#AUTOGENERATED-reading-from-files)\r\n  * [Filenames, shuffling, and epoch limits](#AUTOGENERATED-filenames--shuffling--and-epoch-limits)\r\n  * [File formats](#AUTOGENERATED-file-formats)\r\n  * [Preprocessing](#AUTOGENERATED-preprocessing)\r\n  * [Batching](#AUTOGENERATED-batching)\r\n  * [Creating threads to prefetch using `QueueRunner` objects](#QueueRunner)\r\n  * [Filtering records or producing multiple examples per record](#AUTOGENERATED-filtering-records-or-producing-multiple-examples-per-record)\r\n  * [Sparse input data](#AUTOGENERATED-sparse-input-data)\r\n* [Preloaded data](#AUTOGENERATED-preloaded-data)\r\n* [Multiple input pipelines](#AUTOGENERATED-multiple-input-pipelines)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## Feeding <a class=\"md-anchor\" id=\"Feeding\"></a>\r\n\r\nTensorFlow's feed mechanism lets you inject data into any Tensor in a\r\ncomputation graph. A python computation can thus feed data directly into the\r\ngraph.\r\n\r\nSupply feed data through the `feed_dict` argument to a run() or eval() call\r\nthat initiates computation.\r\n\r\n```python\r\nwith tf.Session():\r\n  input = tf.placeholder(tf.float32)\r\n  classifier = ...\r\n  print classifier.eval(feed_dict={input: my_python_preprocessing_fn()})\r\n```\r\n\r\nWhile you can replace any Tensor with feed data, including variables and\r\nconstants, the best practice is to use a\r\n[`placeholder` op](../../api_docs/python/io_ops.md#placeholder) node. A\r\n`placeholder` exists solely to serve as the target of feeds. It is not\r\ninitialized and contains no data. A placeholder generates an error if\r\nit is executed without a feed, so you won't forget to feed it.\r\n\r\nAn example using `placeholder` and feeding to train on MNIST data can be found\r\nin\r\n[`tensorflow/g3doc/tutorials/mnist/fully_connected_feed.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/fully_connected_feed.py),\r\nand is described in the [MNIST tutorial](../../tutorials/mnist/tf/index.md).\r\n\r\n## Reading from files <a class=\"md-anchor\" id=\"AUTOGENERATED-reading-from-files\"></a>\r\n\r\nA typical pipeline for reading records from files has the following stages:\r\n\r\n1.  The list of filenames\r\n2.  *Optional* filename shuffling\r\n3.  *Optional* epoch limit\r\n4.  Filename queue\r\n5.  A Reader for the file format\r\n6.  A decoder for a record read by the reader\r\n7.  *Optional* preprocessing\r\n8.  Example queue\r\n\r\n### Filenames, shuffling, and epoch limits <a class=\"md-anchor\" id=\"AUTOGENERATED-filenames--shuffling--and-epoch-limits\"></a>\r\n\r\nFor the list of filenames, use either a constant string Tensor (like\r\n`[\"file0\", \"file1\"]` or `[(\"file%d\" % i) for i in range(2)]`) or the\r\n[`tf.train.match_filenames_once`\r\nfunction](../../api_docs/python/io_ops.md#match_filenames_once).\r\n\r\nPass the list of filenames to the [`tf.train.string_input_producer`\r\nfunction](../../api_docs/python/io_ops.md#string_input_producer).\r\n`string_input_producer` creates a FIFO queue for holding the filenames until\r\nthe reader needs them.\r\n\r\n`string_input_producer` has options for shuffling and setting a maximum number\r\nof epochs. A queue runner adds the whole list of filenames to the queue once\r\nfor each epoch, shuffling the filenames within an epoch if `shuffle=True`.\r\nThis procedure provides a uniform sampling of files, so that examples are not\r\nunder- or over- sampled relative to each other.\r\n\r\nThe queue runner works in a thread separate from the reader that pulls\r\nfilenames from the queue, so the shuffling and enqueuing process does not\r\nblock the reader.\r\n\r\n### File formats <a class=\"md-anchor\" id=\"AUTOGENERATED-file-formats\"></a>\r\n\r\nSelect the reader that matches your input file format and pass the filename\r\nqueue to the reader's read method.  The read method outputs a key identifying\r\nthe file and record (useful for debugging if you have some weird records), and\r\na scalar string value. Use one (or more) of the decoder and conversion ops to\r\ndecode this string into the tensors that make up an example.\r\n\r\n#### CSV files <a class=\"md-anchor\" id=\"AUTOGENERATED-csv-files\"></a>\r\n\r\nTo read text files in [comma-separated value (CSV)\r\nformat](https://tools.ietf.org/html/rfc4180), use a\r\n[`TextLineReader`](../../api_docs/python/io_ops.md#TextLineReader) with the\r\n[`decode_csv`](../../api_docs/python/io_ops.md#decode_csv) operation. For example:\r\n\r\n```python\r\nfilename_queue = tf.train.string_input_producer([\"file0.csv\", \"file1.csv\"])\r\n\r\nreader = tf.TextLineReader()\r\nkey, value = reader.read(filename_queue)\r\n\r\n# Default values, in case of empty columns. Also specifies the type of the\r\n# decoded result.\r\nrecord_defaults = [[1], [1], [1], [1], [1]]\r\ncol1, col2, col3, col4, col5 = tf.decode_csv(\r\n    value, record_defaults=record_defaults)\r\nfeatures = tf.concat(0, [col1, col2, col3, col4])\r\n\r\nwith tf.Session() as sess:\r\n  # Start populating the filename queue.\r\n  coord = tf.train.Coordinator()\r\n  threads = tf.train.start_queue_runners(coord=coord)\r\n\r\n  for i in range(1200):\r\n    # Retrieve a single instance:\r\n    example, label = sess.run([features, col5])\r\n\r\n  coord.request_stop()\r\n  coord.join(threads)\r\n```\r\n\r\nEach execution of `read` reads a single line from the file. The\r\n`decode_csv` op then parses the result into a list of tensors. The\r\n`record_defaults` argument determines the type of the resulting tensors and\r\nsets the default value to use if a value is missing in the input string.\r\n\r\nYou must call `tf.train.start_queue_runners` to populate the queue before\r\nyou call `run` or `eval` to execute the `read`. Otherwise `read` will\r\nblock while it waits for filenames from the queue.\r\n\r\n#### Fixed length records <a class=\"md-anchor\" id=\"AUTOGENERATED-fixed-length-records\"></a>\r\n\r\nTo read binary files in which each record is a fixed number of bytes, use\r\n[`tf.FixedLengthRecordReader`](../../api_docs/python/io_ops.md#FixedLengthRecordReader)\r\nwith the [`tf.decode_raw`](../../api_docs/python/io_ops.md#decode_raw) operation.\r\nThe `decode_raw` op converts from a string to a uint8 tensor.\r\n\r\nFor example, [the CIFAR-10 dataset](http://www.cs.toronto.edu/~kriz/cifar.html)\r\nuses a file format where each record is represented using a fixed number of\r\nbytes: 1 byte for the label followed by 3072 bytes of image data. Once you have\r\na uint8 tensor, standard operations can slice out each piece and reformat as\r\nneeded. For CIFAR-10, you can see how to do the reading and decoding in\r\n[`tensorflow/models/image/cifar10/cifar10_input.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_input.py)\r\nand described in\r\n[this tutorial](../../tutorials/deep_cnn/index.md#prepare-the-data).\r\n\r\n#### Standard TensorFlow format <a class=\"md-anchor\" id=\"AUTOGENERATED-standard-tensorflow-format\"></a>\r\n\r\nAnother approach is to convert whatever data you have into a supported format.\r\nThis approach makes it easier to mix and match data sets and network\r\narchitectures. The recommended format for TensorFlow is a TFRecords file\r\ncontaining\r\n[`tf.train.Example` protocol buffers](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/example.proto)\r\n(which contain\r\n[`Features`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/feature.proto)\r\nas a field).  You write a little program that gets your data, stuffs it in an\r\n`Example` protocol buffer, serializes the protocol buffer to a string, and then\r\nwrites the string to a TFRecords file using the\r\n[`tf.python_io.TFRecordWriter` class](../../api_docs/python/python_io.md#TFRecordWriter).\r\nFor example,\r\n[`tensorflow/g3doc/how_tos/reading_data/convert_to_records.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/convert_to_records.py)\r\nconverts MNIST data to this format.\r\n\r\nTo read a file of TFRecords, use\r\n[`tf.TFRecordReader`](../../api_docs/python/io_ops.md#TFRecordReader) with\r\nthe [`tf.parse_single_example`](../../api_docs/python/io_ops.md#parse_single_example)\r\ndecoder. The `parse_single_example` op decodes the example protocol buffers into\r\ntensors. An MNIST example using the data produced by `convert_to_records` can be\r\nfound in\r\n[`tensorflow/g3doc/how_tos/reading_data/fully_connected_reader.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/fully_connected_reader.py),\r\nwhich you can compare with the `fully_connected_feed` version.\r\n\r\n### Preprocessing <a class=\"md-anchor\" id=\"AUTOGENERATED-preprocessing\"></a>\r\n\r\nYou can then do any preprocessing of these examples you want. This would be any\r\nprocessing that doesn't depend on trainable parameters. Examples include\r\nnormalization of your data, picking a random slice, adding noise or distortions,\r\netc.  See\r\n[`tensorflow/models/image/cifar10/cifar10.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10.py)\r\nfor an example.\r\n\r\n### Batching <a class=\"md-anchor\" id=\"AUTOGENERATED-batching\"></a>\r\n\r\nAt the end of the pipeline we use another queue to batch together examples for\r\ntraining, evaluation, or inference.  For this we use a queue that randomizes the\r\norder of examples, using the\r\n[`tf.train.shuffle_batch` function](../../api_docs/python/io_ops.md#shuffle_batch).\r\n\r\nExample:\r\n\r\n```\r\ndef read_my_file_format(filename_queue):\r\n  reader = tf.SomeReader()\r\n  key, record_string = reader.read(filename_queue)\r\n  example, label = tf.some_decoder(record_string)\r\n  processed_example = some_processing(example)\r\n  return processed_example, label\r\n\r\ndef input_pipeline(filenames, batch_size, num_epochs=None):\r\n  filename_queue = tf.train.string_input_producer(\r\n      filenames, num_epochs=num_epochs, shuffle=True)\r\n  example, label = read_my_file_format(filename_queue)\r\n  # min_after_dequeue defines how big a buffer we will randomly sample\r\n  #   from -- bigger means better shuffling but slower start up and more\r\n  #   memory used.\r\n  # capacity must be larger than min_after_dequeue and the amount larger\r\n  #   determines the maximum we will prefetch.  Recommendation:\r\n  #   min_after_dequeue + (num_threads + a small safety margin) * batch_size\r\n  min_after_dequeue = 10000\r\n  capacity = min_after_dequeue + 3 * batch_size\r\n  example_batch, label_batch = tf.train.shuffle_batch(\r\n      [example, label], batch_size=batch_size, capacity=capacity,\r\n      min_after_dequeue=min_after_dequeue)\r\n  return example_batch, label_batch\r\n```\r\n\r\nIf you need more parallelism or shuffling of examples between files, use\r\nmultiple reader instances using the\r\n[`tf.train.shuffle_batch_join` function](../../api_docs/python/io_ops.md#shuffle_batch_join).\r\nFor example:\r\n\r\n```\r\ndef read_my_file_format(filename_queue):\r\n  # Same as above\r\n\r\ndef input_pipeline(filenames, batch_size, read_threads, num_epochs=None):\r\n  filename_queue = tf.train.string_input_producer(\r\n      filenames, num_epochs=num_epochs, shuffle=True)\r\n  example_list = [read_my_file_format(filename_queue)\r\n                  for _ in range(read_threads)]\r\n  min_after_dequeue = 10000\r\n  capacity = min_after_dequeue + 3 * batch_size\r\n  example_batch, label_batch = tf.train.shuffle_batch_join(\r\n      example_list, batch_size=batch_size, capacity=capacity,\r\n      min_after_dequeue=min_after_dequeue)\r\n  return example_batch, label_batch\r\n```\r\n\r\nYou still only use a single filename queue that is shared by all the readers.\r\nThat way we ensure that the different readers use different files from the same\r\nepoch until all the files from the epoch have been started.  (It is also usually\r\nsufficient to have a single thread filling the filename queue.)\r\n\r\nAn alternative is to use a single reader via the\r\n[`tf.train.shuffle_batch` function](../../api_docs/python/io_ops.md#shuffle_batch)\r\nwith `num_threads` bigger than 1.  This will make it read from a single file at\r\nthe same time (but faster than with 1 thread), instead of N files at once.\r\nThis can be important:\r\n\r\n*   If you have more reading threads than input files, to avoid the risk that\r\n    you will have two threads reading the same example from the same file near\r\n    each other.\r\n*   Or if reading N files in parallel causes too many disk seeks.\r\n\r\nHow many threads do you need? the `tf.train.shuffle_batch*` functions add a\r\nsummary to the graph that indicates how full the example queue is. If you have\r\nenough reading threads, that summary will stay above zero.  You can\r\n[view your summaries as training progresses using TensorBoard](../../how_tos/summaries_and_tensorboard/index.md).\r\n\r\n### Creating threads to prefetch using `QueueRunner` objects <a class=\"md-anchor\" id=\"QueueRunner\"></a>\r\n\r\nThe short version: many of the `tf.train` functions listed above add\r\n[`QueueRunner`](../../api_docs/python/train.md#QueueRunner) objects to your\r\ngraph.  These require that you call\r\n[`tf.train.start_queue_runners`](../../api_docs/python/train.md#start_queue_runners)\r\nbefore running any training or inference steps, or it will hang forever. This\r\nwill start threads that run the input pipeline, filling the example queue so\r\nthat the dequeue to get the examples will succeed.  This is best combined with a\r\n[`tf.train.Coordinator`](../../api_docs/python/train.md#Coordinator) to cleanly\r\nshut down these threads when there are errors. If you set a limit on the number\r\nof epochs, that will use an epoch counter that will need to be intialized.  The\r\nrecommended code pattern combining these is:\r\n\r\n```python\r\n# Create the graph, etc.\r\ninit_op = tf.initialize_all_variables()\r\n\r\n# Create a session for running operations in the Graph.\r\nsess = tf.Session()\r\n\r\n# Initialize the variables (like the epoch counter).\r\nsess.run(init_op)\r\n\r\n# Start input enqueue threads.\r\ncoord = tf.train.Coordinator()\r\nthreads = tf.train.start_queue_runners(sess=sess, coord=coord)\r\n\r\ntry:\r\n    while not coord.should_stop():\r\n        # Run training steps or whatever\r\n        sess.run(train_op)\r\n\r\nexcept tf.errors.OutOfRangeError:\r\n    print 'Done training -- epoch limit reached'\r\nfinally:\r\n    # When done, ask the threads to stop.\r\n    coord.request_stop()\r\n\r\n# Wait for threads to finish.\r\ncoord.join(threads)\r\nsess.close()\r\n```\r\n\r\n#### Aside: What is happening here? <a class=\"md-anchor\" id=\"AUTOGENERATED-aside--what-is-happening-here-\"></a>\r\n\r\nFirst we create the graph. It will have a few pipeline stages that are\r\nconnected by queues. The first stage will generate filenames to read and enqueue\r\nthem in the filename queue. The second stage consumes filenames (using a\r\n`Reader`), produces examples, and enqueues them in an example queue. Depending\r\non how you have set things up, you may actually have a few independent copies of\r\nthe second stage, so that you can read from multiple files in parallel. At the\r\nend of these stages is an enqueue operation, which enqueues into a queue that\r\nthe next stage dequeues from. We want to start threads running these enqueuing\r\noperations, so that our training loop can dequeue examples from the example\r\nqueue.\r\n\r\n<div style=\"width:70%; margin-left:12%; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"AnimatedFileQueues.gif\">\r\n</div>\r\n\r\nThe helpers in `tf.train` that create these queues and enqueuing operations add\r\na [`tf.train.QueueRunner`](../../api_docs/python/train.md#QueueRunner) to the\r\ngraph using the\r\n[`tf.train.add_queue_runner`](../../api_docs/python/train.md#add_queue_runner)\r\nfunction. Each `QueueRunner` is responsible for one stage, and holds the list of\r\nenqueue operations that need to be run in threads. Once the graph is\r\nconstructed, the\r\n[`tf.train.start_queue_runners`](../../api_docs/python/train.md#start_queue_runners)\r\nfunction asks each QueueRunner in the graph to start its threads running the\r\nenqueuing operations.\r\n\r\nIf all goes well, you can now run your training steps and the queues will be\r\nfilled by the background threads. If you have set an epoch limit, at some point\r\nan attempt to dequeue examples will get an\r\n[`tf.OutOfRangeError`](../../api_docs/python/client.md#OutOfRangeError).  This\r\nis the TensorFlow equivalent of \"end of file\" (EOF) -- this means the epoch\r\nlimit has been reached and no more examples are available.\r\n\r\nThe last ingredient is the\r\n[`Coordinator`](../../api_docs/python/train.md#Coordinator). This is responsible\r\nfor letting all the threads know if anything has signalled a shut down. Most\r\ncommonly this would be because an exception was raised, for example one of the\r\nthreads got an error when running some operation (or an ordinary Python\r\nexception).\r\n\r\nFor more about threading, queues, QueueRunners, and Coordinators\r\n[see here](../../how_tos/threading_and_queues/index.md).\r\n\r\n#### Aside: How clean shut-down when limiting epochs works <a class=\"md-anchor\" id=\"AUTOGENERATED-aside--how-clean-shut-down-when-limiting-epochs-works\"></a>\r\n\r\nImagine you have a model that has set a limit on the number of epochs to train\r\non.  That means that the thread generating filenames will only run that many\r\ntimes before generating an `OutOfRange` error. The QueueRunner will catch that\r\nerror, close the filename queue, and exit the thread. Closing the queue does two\r\nthings:\r\n\r\n*   Any future attempt to enqueue in the filename queue will generate an error.\r\n    At this point there shouldn't be any threads trying to do that, but this\r\n    is helpful when queues are closed due to other errors.\r\n*   Any current or future dequeue will either succeed (if there are enough\r\n    elements left) or fail (with an `OutOfRange` error) immediately.  They won't\r\n    block waiting for more elements to be enqueued, since by the previous point\r\n    that can't happen.\r\n\r\nThe point is that when the filename queue is closed, there will likely still be\r\nmany filenames in that queue, so the next stage of the pipeline (with the reader\r\nand other preprocessing) may continue running for some time.  Once the filename\r\nqueue is exhausted, though, the next attempt to dequeue a filename (e.g. from a\r\nreader that has finished with the file it was working on) will trigger an\r\n`OutOfRange` error.  In this case, though, you might have multiple threads\r\nassociated with a single QueueRunner.  If this isn't the last thread in the\r\nQueueRunner, the `OutOfRange` error just causes the one thread to exit.  This\r\nallows the other threads, which are still finishing up their last file, to\r\nproceed until they finish as well.  (Assuming you are using a\r\n[`tf.train.Coordinator`](../../api_docs/python/train.md#Coordinator),\r\nother types of errors will cause all the threads to stop.)  Once all the reader\r\nthreads hit the `OutOfRange` error, only then does the next queue, the example\r\nqueue, gets closed.\r\n\r\nAgain, the example queue will have some elements queued, so training will\r\ncontinue until those are exhausted.  If the example queue is a\r\n[`RandomShuffleQueue`](../../api_docs/python/io_ops.md#RandomShuffleQueue), say\r\nbecause you are using `shuffle_batch` or `shuffle_batch_join`, it normally will\r\navoid ever going having fewer than its `min_after_dequeue` attr elements\r\nbuffered.  However, once the queue is closed that restriction will be lifted and\r\nthe queue will eventually empty.  At that point the actual training threads,\r\nwhen they try and dequeue from example queue, will start getting `OutOfRange`\r\nerrors and exiting.  Once all the training threads are done,\r\n[`tf.train.Coordinator.join`](../../api_docs/python/train.md#Coordinator.join)\r\nwill return and you can exit cleanly.\r\n\r\n### Filtering records or producing multiple examples per record <a class=\"md-anchor\" id=\"AUTOGENERATED-filtering-records-or-producing-multiple-examples-per-record\"></a>\r\n\r\nInstead of examples with shapes `[x, y, z]`, you will produce a batch of\r\nexamples with shape `[batch, x, y, z]`.  The batch size can be 0 if you want to\r\nfilter this record out (maybe it is in a hold-out set?), or bigger than 1 if you\r\nare producing multiple examples per record.  Then simply set `enqueue_many=True`\r\nwhen calling one of the batching functions (such as `shuffle_batch` or\r\n`shuffle_batch_join`).\r\n\r\n### Sparse input data <a class=\"md-anchor\" id=\"AUTOGENERATED-sparse-input-data\"></a>\r\n\r\nSparseTensors don't play well with queues. If you use SparseTensors you have\r\nto decode the string records using\r\n[`tf.parse_example`](../../api_docs/python/io_ops.md#parse_example) **after**\r\nbatching (instead of using `tf.parse_single_example` before batching).\r\n\r\n## Preloaded data <a class=\"md-anchor\" id=\"AUTOGENERATED-preloaded-data\"></a>\r\n\r\nThis is only used for small data sets that can be loaded entirely in memory.\r\nThere are two approaches:\r\n\r\n* Store the data in a constant.\r\n* Store the data in a variable, that you initialize and then never change.\r\n\r\nUsing a constant is a bit simpler, but uses more memory (since the constant is\r\nstored inline in the graph data structure, which may be duplicated a few times).\r\n\r\n```python\r\ntraining_data = ...\r\ntraining_labels = ...\r\nwith tf.Session():\r\n  input_data = tf.constant(training_data)\r\n  input_labels = tf.constant(training_labels)\r\n  ...\r\n```\r\n\r\nTo instead use a variable, you need to also initialize it after the graph has been built.\r\n\r\n```python\r\ntraining_data = ...\r\ntraining_labels = ...\r\nwith tf.Session() as sess:\r\n  data_initializer = tf.placeholder(dtype=training_data.dtype,\r\n                                    shape=training_data.shape)\r\n  label_initializer = tf.placeholder(dtype=training_labels.dtype,\r\n                                     shape=training_labels.shape)\r\n  input_data = tf.Variable(data_initalizer, trainable=False, collections=[])\r\n  input_labels = tf.Variable(label_initalizer, trainable=False, collections=[])\r\n  ...\r\n  sess.run(input_data.initializer,\r\n           feed_dict={data_initializer: training_data})\r\n  sess.run(input_labels.initializer,\r\n           feed_dict={label_initializer: training_lables})\r\n```\r\n\r\nSetting `trainable=False` keeps the variable out of the\r\n`GraphKeys.TRAINABLE_VARIABLES` collection in the graph, so we won't try and\r\nupdate it when training.  Setting `collections=[]` keeps the variable out of the\r\n`GraphKeys.VARIABLES` collection used for saving and restoring checkpoints.\r\n\r\nEither way,\r\n[`tf.train.slice_input_producer function`](../../api_docs/python/io_ops.md#slice_input_producer)\r\ncan be used to produce a slice at a time.  This shuffles the examples across an\r\nentire epoch, so further shuffling when batching is undesirable.  So instead of\r\nusing the `shuffle_batch` functions, we use the plain\r\n[`tf.train.batch` function](../../api_docs/python/io_ops.md#batch).  To use\r\nmultiple preprocessing threads, set the `num_threads` parameter to a number\r\nbigger than 1.\r\n\r\nAn MNIST example that preloads the data using constants can be found in\r\n[`tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded.py), and one that preloads the data using variables can be found in\r\n[`tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded_var.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded_var.py),\r\nYou can compare these with the `fully_connected_feed` and\r\n`fully_connected_reader` versions above.\r\n\r\n## Multiple input pipelines <a class=\"md-anchor\" id=\"AUTOGENERATED-multiple-input-pipelines\"></a>\r\n\r\nCommonly you will want to train on one dataset and evaluate (or \"eval\") on\r\nanother.  One way to do this is to actually have two separate processes:\r\n\r\n* The training process reads training input data and periodically writes\r\n  checkpoint files with all the trained variables.\r\n* The evaluation process restores the checkpoint files into an inference\r\n  model that reads validation input data.\r\n\r\nThis is what is done in\r\n[the example CIFAR-10 model](../../tutorials/deep_cnn/index.md#save-and-restore-checkpoints).  This has a couple of benefits:\r\n\r\n* The eval is performed on a single snapshot of the trained variables.\r\n* You can perform the eval even after training has completed and exited.\r\n\r\nYou can have the train and eval in the same graph in the same process, and share\r\ntheir trained variables.  See\r\n[the shared variables tutorial](../../how_tos/variable_scope/index.md).\r\n"
  },
  {
    "path": "SOURCE/how_tos/reading_data.md",
    "content": "# 数据读取 <a class=\"md-anchor\" id=\"AUTOGENERATED-reading-data\"></a>\r\n\r\nTensorFlow程序读取数据一共有3种方法:\r\n\r\n*   供给数据(Feeding)： 在TensorFlow程序运行的每一步， 让Python代码来供给数据。\r\n*   从文件读取数据： 在TensorFlow图的起始， 让一个输入管线从文件中读取数据。\r\n*   预加载数据： 在TensorFlow图中定义常量或变量来保存所有数据(仅适用于数据量比较小的情况)。\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n## 目录\r\n### [数据读取](#AUTOGENERATED-reading-data)\r\n* [供给数据(Feeding)](#Feeding)\r\n* [从文件读取数据](#AUTOGENERATED-reading-from-files)\r\n  * [文件名, 乱序(shuffling), 和最大训练迭代数(epoch limits)](#AUTOGENERATED-filenames--shuffling--and-epoch-limits)\r\n  * [文件格式](#AUTOGENERATED-file-formats)\r\n  * [预处理](#AUTOGENERATED-preprocessing)\r\n  * [批处理](#AUTOGENERATED-batching)\r\n  * [使用`QueueRunner`创建预读线程](#QueueRunner)\r\n  * [对记录进行过滤或者为每个纪录创建多个样本](#AUTOGENERATED-filtering-records-or-producing-multiple-examples-per-record)\r\n  * [序列化输入数据(Sparse input data)](#AUTOGENERATED-sparse-input-data)\r\n* [预加载数据](#AUTOGENERATED-preloaded-data)\r\n* [多管线输入](#AUTOGENERATED-multiple-input-pipelines)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## 供给数据 <a class=\"md-anchor\" id=\"Feeding\"></a>\r\n\r\nTensorFlow的数据供给机制允许你在TensorFlow运算图中将数据注入到任一张量中。因此，python运算可以把数据直接设置到TensorFlow图中。\r\n\r\n通过给run()或者eval()函数输入`feed_dict`参数， 可以启动运算过程。\r\n\r\n```python\r\nwith tf.Session():\r\n  input = tf.placeholder(tf.float32)\r\n  classifier = ...\r\n  print classifier.eval(feed_dict={input: my_python_preprocessing_fn()})\r\n```\r\n\r\n虽然你可以使用常量和变量来替换任何一个张量， 但是最好的做法应该是使用[`placeholder` op](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#placeholder)节点。设计`placeholder`节点的唯一的意图就是为了提供数据供给(feeding)的方法。`placeholder`节点被声明的时候是未初始化的， 也不包含数据， 如果没有为它供给数据， 则TensorFlow运算的时候会产生错误， 所以千万不要忘了为`placeholder`提供数据。\r\n\r\n可以在[`tensorflow/g3doc/tutorials/mnist/fully_connected_feed.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/fully_connected_feed.py)找到使用`placeholder`和MNIST训练的例子，[MNIST tutorial](tensorflow-zh/SOURCE/tutorials/mnist/tf/index.md)也讲述了这一例子。\r\n\r\n## 从文件读取数据 <a class=\"md-anchor\" id=\"AUTOGENERATED-reading-from-files\"></a>\r\n\r\n一共典型的文件读取管线会包含下面这些步骤：\r\n\r\n1.  文件名列表\r\n2.  *可配置的* 文件名乱序(shuffling)\r\n3.  *可配置的* 最大训练迭代数(epoch limit)\r\n4.  文件名队列\r\n5.  针对输入文件格式的阅读器\r\n6.  纪录解析器\r\n7.  *可配置的*预处理器\r\n8.  样本队列\r\n\r\n### 文件名, 乱序(shuffling), 和最大训练迭代数(epoch limits) <a class=\"md-anchor\" id=\"AUTOGENERATED-filenames--shuffling--and-epoch-limits\"></a>\r\n\r\n可以使用字符串张量(比如`[\"file0\", \"file1\"]`, `[(\"file%d\" % i) for i in range(2)]`， `[(\"file%d\" % i) for i in range(2)]`) 或者[`tf.train.match_filenames_once` 函数](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#match_filenames_once)来产生文件名列表。\r\n\r\n将文件名列表交给[`tf.train.string_input_producer` 函数](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#string_input_producer).`string_input_producer`来生成一个先入先出的队列， 文件阅读器会需要它来读取数据。\r\n\r\n`string_input_producer` 提供的可配置参数来设置文件名乱序和最大的训练迭代数， `QueueRunner`会为每次迭代(epoch)将所有的文件名加入文件名队列中， 如果`shuffle=True`的话， 会对文件名进行乱序处理。这一过程是比较均匀的，因此它可以产生均衡的文件名队列。\r\n\r\n这个`QueueRunner`的工作线程是独立于文件阅读器的线程， 因此乱序和将文件名推入到文件名队列这些过程不会阻塞文件阅读器运行。\r\n\r\n### 文件格式 <a class=\"md-anchor\" id=\"AUTOGENERATED-file-formats\"></a>\r\n\r\n根据你的文件格式， 选择对应的文件阅读器， 然后将文件名队列提供给阅读器的`read`方法。阅读器的`read`方法会输出一个key来表征输入的文件和其中的纪录(对于调试非常有用)，同时得到一个字符串标量， 这个字符串标量可以被一个或多个解析器，或者转换操作将其解码为张量并且构造成为样本。\r\n\r\n#### CSV 文件 <a class=\"md-anchor\" id=\"AUTOGENERATED-csv-files\"></a>\r\n\r\n从CSV文件中读取数据， 需要使用[`TextLineReader`](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#TextLineReader)和[`decode_csv`](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#decode_csv) 操作， 如下面的例子所示：\r\n\r\n```python\r\nfilename_queue = tf.train.string_input_producer([\"file0.csv\", \"file1.csv\"])\r\n\r\nreader = tf.TextLineReader()\r\nkey, value = reader.read(filename_queue)\r\n\r\n# Default values, in case of empty columns. Also specifies the type of the\r\n# decoded result.\r\nrecord_defaults = [[1], [1], [1], [1], [1]]\r\ncol1, col2, col3, col4, col5 = tf.decode_csv(\r\n    value, record_defaults=record_defaults)\r\nfeatures = tf.concat(0, [col1, col2, col3, col4])\r\n\r\nwith tf.Session() as sess:\r\n  # Start populating the filename queue.\r\n  coord = tf.train.Coordinator()\r\n  threads = tf.train.start_queue_runners(coord=coord)\r\n\r\n  for i in range(1200):\r\n    # Retrieve a single instance:\r\n    example, label = sess.run([features, col5])\r\n\r\n  coord.request_stop()\r\n  coord.join(threads)\r\n```\r\n每次`read`的执行都会从文件中读取一行内容， `decode_csv` 操作会解析这一行内容并将其转为张量列表。如果输入的参数有缺失，`record_default`参数可以根据张量的类型来设置默认值。\r\n\r\n在调用`run`或者`eval`去执行`read`之前， 你必须调用`tf.train.start_queue_runners`来将文件名填充到队列。否则`read`操作会被阻塞到文件名队列中有值为止。\r\n\r\n#### 固定长度的记录 <a class=\"md-anchor\" id=\"AUTOGENERATED-fixed-length-records\"></a>\r\n\r\n从二进制文件中读取固定长度纪录， 可以使用[`tf.FixedLengthRecordReader`](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#FixedLengthRecordReader)的[`tf.decode_raw`](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#decode_raw)操作。`decode_raw`操作可以讲一个字符串转换为一个uint8的张量。\r\n\r\n举例来说，[the CIFAR-10 dataset](http://www.cs.toronto.edu/~kriz/cifar.html)的文件格式定义是：每条记录的长度都是固定的，一个字节的标签，后面是3072字节的图像数据。uint8的张量的标准操作就可以从中获取图像片并且根据需要进行重组。 例子代码可以在[`tensorflow/models/image/cifar10/cifar10_input.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_input.py)找到，具体讲述可参见[教程](tensorflow-zh/SOURCE/tutorials/deep_cnn/index.md#prepare-the-data).\r\n\r\n#### 标准TensorFlow格式 <a class=\"md-anchor\" id=\"AUTOGENERATED-standard-tensorflow-format\"></a>\r\n\r\n另一种保存记录的方法可以允许你讲任意的数据转换为TensorFlow所支持的格式， 这种方法可以使TensorFlow的数据集更容易与网络应用架构相匹配。这种建议的方法就是使用TFRecords文件，TFRecords文件包含了[`tf.train.Example` 协议内存块(protocol buffer)](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/example.proto)(协议内存块包含了字段\r\n[`Features`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/feature.proto))。你可以写一段代码获取你的数据， 将数据填入到`Example`协议内存块(protocol buffer)，将协议内存块序列化为一个字符串， 并且通过[`tf.python_io.TFRecordWriter` class](tensorflow-zh/SOURCE/api_docs/python/python_io.md#TFRecordWriter)写入到TFRecords文件。[`tensorflow/g3doc/how_tos/reading_data/convert_to_records.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/convert_to_records.py)就是这样的一个例子。\r\n\r\n从TFRecords文件中读取数据， 可以使用[`tf.TFRecordReader`](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#TFRecordReader)的[`tf.parse_single_example`](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#parse_single_example)解析器。这个`parse_single_example`操作可以将`Example`协议内存块(protocol buffer)解析为张量。 MNIST的例子就使用了`convert_to_records` 所构建的数据。 请参看[`tensorflow/g3doc/how_tos/reading_data/fully_connected_reader.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/fully_connected_reader.py), 您也可以将这个例子跟`fully_connected_feed`的版本加以比较。\r\n\r\n### 预处理 <a class=\"md-anchor\" id=\"AUTOGENERATED-preprocessing\"></a>\r\n\r\n你可以对输入的样本进行任意的预处理， 这些预处理不依赖于训练参数， 你可以在[`tensorflow/models/image/cifar10/cifar10.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10.py)找到数据归一化， 提取随机数据片，增加噪声或失真等等预处理的例子。\r\n\r\n### 批处理 <a class=\"md-anchor\" id=\"AUTOGENERATED-batching\"></a>\r\n\r\n在数据输入管线的末端， 我们需要有另一个队列来执行输入样本的训练，评价和推理。因此我们使用[`tf.train.shuffle_batch` 函数](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#shuffle_batch)来对队列中的样本进行乱序处理\r\n\r\n示例:\r\n\r\n```\r\ndef read_my_file_format(filename_queue):\r\n  reader = tf.SomeReader()\r\n  key, record_string = reader.read(filename_queue)\r\n  example, label = tf.some_decoder(record_string)\r\n  processed_example = some_processing(example)\r\n  return processed_example, label\r\n\r\ndef input_pipeline(filenames, batch_size, num_epochs=None):\r\n  filename_queue = tf.train.string_input_producer(\r\n      filenames, num_epochs=num_epochs, shuffle=True)\r\n  example, label = read_my_file_format(filename_queue)\r\n  # min_after_dequeue defines how big a buffer we will randomly sample\r\n  #   from -- bigger means better shuffling but slower start up and more\r\n  #   memory used.\r\n  # capacity must be larger than min_after_dequeue and the amount larger\r\n  #   determines the maximum we will prefetch.  Recommendation:\r\n  #   min_after_dequeue + (num_threads + a small safety margin) * batch_size\r\n  min_after_dequeue = 10000\r\n  capacity = min_after_dequeue + 3 * batch_size\r\n  example_batch, label_batch = tf.train.shuffle_batch(\r\n      [example, label], batch_size=batch_size, capacity=capacity,\r\n      min_after_dequeue=min_after_dequeue)\r\n  return example_batch, label_batch\r\n```\r\n\r\n如果你需要对不同文件中的样子有更强的乱序和并行处理，可以使用[`tf.train.shuffle_batch_join` 函数](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#shuffle_batch_join).\r\n示例:\r\n\r\n```\r\ndef read_my_file_format(filename_queue):\r\n  # Same as above\r\n\r\ndef input_pipeline(filenames, batch_size, read_threads, num_epochs=None):\r\n  filename_queue = tf.train.string_input_producer(\r\n      filenames, num_epochs=num_epochs, shuffle=True)\r\n  example_list = [read_my_file_format(filename_queue)\r\n                  for _ in range(read_threads)]\r\n  min_after_dequeue = 10000\r\n  capacity = min_after_dequeue + 3 * batch_size\r\n  example_batch, label_batch = tf.train.shuffle_batch_join(\r\n      example_list, batch_size=batch_size, capacity=capacity,\r\n      min_after_dequeue=min_after_dequeue)\r\n  return example_batch, label_batch\r\n```\r\n\r\n在这个例子中， 你虽然只使用了一个文件名队列， 但是TensorFlow依然能保证多个文件阅读器从同一次迭代(epoch)的不同文件中读取数据，知道这次迭代的所有文件都被开始读取为止。（通常来说一个线程来对文件名队列进行填充的效率是足够的）\r\n\r\n另一种替代方案是： 使用[`tf.train.shuffle_batch` 函数](tensorflow-zh/SOURCE/api_docs/python/io_ops.md#shuffle_batch),设置`num_threads`的值大于1。 这种方案可以保证同一时刻只在一个文件中进行读取操作(但是读取速度依然优于单线程)，而不是之前的同时读取多个文件。这种方案的优点是：\r\n*   避免了两个不同的线程从同一个文件中读取同一个样本。\r\n*   避免了过多的磁盘搜索操作。\r\n\r\n你一共需要多少个读取线程呢？ 函数`tf.train.shuffle_batch*`为TensorFlow图提供了获取文件名队列中的元素个数之和的方法。 如果你有足够多的读取线程， 文件名队列中的元素个数之和应该一直是一个略高于0的数。具体可以参考[TensorBoard:可视化学习](tensorflow-zh/SOURCE/how_tos/summaries_and_tensorboard/index.md).\r\n\r\n### 创建线程并使用`QueueRunner`对象来预取 <a class=\"md-anchor\" id=\"QueueRunner\"></a>\r\n\r\n简单来说：使用上面列出的许多`tf.train`函数添加[`QueueRunner`](../../api_docs/python/train.md#QueueRunner)到你的数据流图中。在你运行任何训练步骤之前，需要调用[`tf.train.start_queue_runners`](../../api_docs/python/train.md#start_queue_runners)函数，否则数据流图将一直挂起。[`tf.train.start_queue_runners`](../../api_docs/python/train.md#start_queue_runners) 这个函数将会启动输入管道的线程，填充样本到队列中，以便出队操作可以从队列中拿到样本。这种情况下最好配合使用一个[`tf.train.Coordinator`](../../api_docs/python/train.md#Coordinator)，这样可以在发生错误的情况下正确地关闭这些线程。如果你对训练迭代数做了限制，那么需要使用一个训练迭代数计数器，并且需要被初始化。推荐的代码模板如下：\r\n\r\n```python\r\n# Create the graph, etc.\r\ninit_op = tf.initialize_all_variables()\r\n\r\n# Create a session for running operations in the Graph.\r\nsess = tf.Session()\r\n\r\n# Initialize the variables (like the epoch counter).\r\nsess.run(init_op)\r\n\r\n# Start input enqueue threads.\r\ncoord = tf.train.Coordinator()\r\nthreads = tf.train.start_queue_runners(sess=sess, coord=coord)\r\n\r\ntry:\r\n    while not coord.should_stop():\r\n        # Run training steps or whatever\r\n        sess.run(train_op)\r\n\r\nexcept tf.errors.OutOfRangeError:\r\n    print 'Done training -- epoch limit reached'\r\nfinally:\r\n    # When done, ask the threads to stop.\r\n    coord.request_stop()\r\n\r\n# Wait for threads to finish.\r\ncoord.join(threads)\r\nsess.close()\r\n```\r\n\r\n#### 疑问: 这是怎么回事? <a class=\"md-anchor\" id=\"AUTOGENERATED-aside--what-is-happening-here-\"></a>\r\n\r\n首先，我们先创建数据流图，这个数据流图由一些流水线的阶段组成，阶段间用队列连接在一起。第一阶段将生成文件名，我们读取这些文件名并且把他们排到文件名队列中。第二阶段从文件中读取数据（使用`Reader`），产生样本，而且把样本放在一个样本队列中。根据你的设置，实际上也可以拷贝第二阶段的样本，使得他们相互独立，这样就可以从多个文件中并行读取。在第二阶段的最后是一个排队操作，就是入队到队列中去，在下一阶段出队。因为我们是要开始运行这些入队操作的线程，所以我们的训练循环会使得样本队列中的样本不断地出队。\r\n\r\n<div style=\"width:70%; margin-left:12%; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/AnimatedFileQueues.gif\">\r\n</div>\r\n\r\n在`tf.train`中要创建这些队列和执行入队操作，就要添加[`tf.train.QueueRunner`](../../api_docs/python/train.md#QueueRunner)到一个使用[`tf.train.add_queue_runner`](../../api_docs/python/train.md#add_queue_runner)函数的数据流图中。每个`QueueRunner`负责一个阶段，处理那些需要在线程中运行的入队操作的列表。一旦数据流图构造成功，[`tf.train.start_queue_runners`](../../api_docs/python/train.md#start_queue_runners)函数就会要求数据流图中每个`QueueRunner`去开始它的线程运行入队操作。\r\n\r\n如果一切顺利的话，你现在可以执行你的训练步骤，同时队列也会被后台线程来填充。如果您设置了最大训练迭代数，在某些时候，样本出队的操作可能会得到一个[`tf.OutOfRangeError`](../../api_docs/python/client.md#OutOfRangeError)的错误。这其实是TensorFlow的“文件结束”（EOF） ———— 这就意味着已经达到了最大训练迭代数，已经没有更多可用的样本了。\r\n\r\n最后一个因素是[`Coordinator`](../../api_docs/python/train.md#Coordinator)。这是负责在收到任何关闭信号的时候，让所有的线程都知道。最常用的是在发生异常时这种情况就会呈现出来，比如说其中一个线程在运行某些操作时出现错误（或一个普通的Python异常）。\r\n\r\n想要了解更多的关于threading, queues, QueueRunners, and Coordinators的内容可以[看这里](../../how_tos/threading_and_queues/index.md).\r\n\r\n#### 疑问: 在达到最大训练迭代数的时候如何清理关闭线程? <a class=\"md-anchor\" id=\"AUTOGENERATED-aside--how-clean-shut-down-when-limiting-epochs-works\"></a>\r\n\r\n想象一下，你有一个模型并且设置了最大训练迭代数。这意味着，生成文件的那个线程将只会在产生`OutOfRange`错误之前运行许多次。该`QueueRunner`会捕获该错误，并且关闭文件名的队列，最后退出线程。关闭队列做了两件事情：\r\n\r\n*   如果还试着对文件名队列执行入队操作时将发生错误。任何线程不应该尝试去这样做，但是当队列因为其他错误而关闭时，这就会有用了。\r\n*   任何当前或将来出队操作要么成功（如果队列中还有足够的元素）或立即失败（发生`OutOfRange`错误）。它们不会防止等待更多的元素被添加到队列中，因为上面的一点已经保证了这种情况不会发生。\r\n\r\n关键是，当在文件名队列被关闭时候，有可能还有许多文件名在该队列中，这样下一阶段的流水线（包括reader和其它预处理）还可以继续运行一段时间。 一旦文件名队列空了之后，如果后面的流水线还要尝试从文件名队列中取出一个文件名（例如，从一个已经处理完文件的reader中），这将会触发`OutOfRange`错误。在这种情况下，即使你可能有一个QueueRunner关联着多个线程。如果这不是在QueueRunner中的最后那个线程，`OutOfRange`错误仅仅只会使得一个线程退出。这使得其他那些正处理自己的最后一个文件的线程继续运行，直至他们完成为止。 （但如果假设你使用的是[`tf.train.Coordinator`](../../api_docs/python/train.md#Coordinator)，其他类型的错误将导致所有线程停止）。一旦所有的reader线程触发`OutOfRange`错误，然后才是下一个队列，再是样本队列被关闭。\r\n\r\n同样，样本队列中会有一些已经入队的元素，所以样本训练将一直持续直到样本队列中再没有样本为止。如果样本队列是一个[`RandomShuffleQueue`](../../api_docs/python/io_ops.md#RandomShuffleQueue)，因为你使用了`shuffle_batch` 或者 `shuffle_batch_join`，所以通常不会出现以往那种队列中的元素会比`min_after_dequeue` 定义的更少的情况。 然而，一旦该队列被关闭，`min_after_dequeue`设置的限定值将失效，最终队列将为空。在这一点来说，当实际训练线程尝试从样本队列中取出数据时，将会触发`OutOfRange`错误，然后训练线程会退出。一旦所有的培训线程完成，[`tf.train.Coordinator.join`](../../api_docs/python/train.md#Coordinator.join)会返回，你就可以正常退出了。\r\n\r\n### 筛选记录或产生每个记录的多个样本 <a class=\"md-anchor\" id=\"AUTOGENERATED-filtering-records-or-producing-multiple-examples-per-record\"></a>\r\n\r\n举个例子，有形式为`[x, y, z]`的样本，我们可以生成一批形式为`[batch, x, y, z]`的样本。 如果你想滤除这个记录（或许不需要这样的设置），那么可以设置batch的大小为0；但如果你需要每个记录产生多个样本，那么batch的值可以大于1。 然后很简单，只需调用批处理函数（比如： `shuffle_batch` or `shuffle_batch_join`）去设置`enqueue_many=True`就可以实现。\r\n\r\n### 稀疏输入数据 <a class=\"md-anchor\" id=\"AUTOGENERATED-sparse-input-data\"></a>\r\n\r\nSparseTensors这种数据类型使用队列来处理不是太好。如果要使用SparseTensors你就必须在批处理**之后**使用[`tf.parse_example`](../../api_docs/python/io_ops.md#parse_example) 去解析字符串记录 (而不是在批处理**之前**使用 `tf.parse_single_example`) 。\r\n\r\n## 预取数据 <a class=\"md-anchor\" id=\"AUTOGENERATED-preloaded-data\"></a>\r\n\r\n这仅用于可以完全加载到存储器中的小的数据集。有两种方法：\r\n\r\n* 存储在常数中。\r\n* 存储在变量中，初始化后，永远不要改变它的值。\r\n\r\n使用常数更简单一些，但是会使用更多的内存（因为常数会内联的存储在数据流图数据结构中，这个结构体可能会被复制几次）。\r\n\r\n```python\r\ntraining_data = ...\r\ntraining_labels = ...\r\nwith tf.Session():\r\n  input_data = tf.constant(training_data)\r\n  input_labels = tf.constant(training_labels)\r\n  ...\r\n```\r\n\r\n要改为使用变量的方式，您就需要在数据流图建立后初始化这个变量。\r\n\r\n```python\r\ntraining_data = ...\r\ntraining_labels = ...\r\nwith tf.Session() as sess:\r\n  data_initializer = tf.placeholder(dtype=training_data.dtype,\r\n                                    shape=training_data.shape)\r\n  label_initializer = tf.placeholder(dtype=training_labels.dtype,\r\n                                     shape=training_labels.shape)\r\n  input_data = tf.Variable(data_initalizer, trainable=False, collections=[])\r\n  input_labels = tf.Variable(label_initalizer, trainable=False, collections=[])\r\n  ...\r\n  sess.run(input_data.initializer,\r\n           feed_dict={data_initializer: training_data})\r\n  sess.run(input_labels.initializer,\r\n           feed_dict={label_initializer: training_lables})\r\n```\r\n\r\n设定`trainable=False` 可以防止该变量被数据流图的 `GraphKeys.TRAINABLE_VARIABLES` 收集, 这样我们就不会在训练的时候尝试更新它的值； 设定 `collections=[]` 可以防止`GraphKeys.VARIABLES` 收集后做为保存和恢复的中断点。\r\n\r\n无论哪种方式，[`tf.train.slice_input_producer function`](../../api_docs/python/io_ops.md#slice_input_producer)函数可以被用来每次产生一个切片。这样就会让样本在整个迭代中被打乱，所以在使用批处理的时候不需要再次打乱样本。所以我们不使用`shuffle_batch`函数，取而代之的是纯[`tf.train.batch` 函数](../../api_docs/python/io_ops.md#batch)。 如果要使用多个线程进行预处理，需要将`num_threads`参数设置为大于1的数字。\r\n\r\n在[`tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded.py) 中可以找到一个MNIST例子，使用常数来预加载。 另外使用变量来预加载的例子在[`tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded_var.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded_var.py)，你可以用上面 `fully_connected_feed` 和 `fully_connected_reader` 的描述来进行比较。\r\n\r\n## 多输入管道 <a class=\"md-anchor\" id=\"AUTOGENERATED-multiple-input-pipelines\"></a>\r\n\r\n通常你会在一个数据集上面训练，然后在另外一个数据集上做评估计算(或称为 \"eval\")。 这样做的一种方法是，实际上包含两个独立的进程：\r\n\r\n* 训练过程中读取输入数据，并定期将所有的训练的变量写入还原点文件）。\r\n* 在计算过程中恢复还原点文件到一个推理模型中，读取有效的输入数据。\r\n\r\n这两个进程在下面的例子中已经完成了：[the example CIFAR-10 model](../../tutorials/deep_cnn/index.md#save-and-restore-checkpoints)，有以下几个好处：\r\n\r\n* eval被当做训练后变量的一个简单映射。\r\n* 你甚至可以在训练完成和退出后执行eval。\r\n\r\n您可以在同一个进程的相同的数据流图中有训练和eval，并分享他们的训练后的变量。参考[the shared variables tutorial](../../how_tos/variable_scope/index.md).\r\n\r\n原文地址：[Reading data](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/how_tos/reading_data/index.md) 翻译：[volvet](https://github.com/volvet) and [zhangkom](https://github.com/zhangkom) 校对：\r\n"
  },
  {
    "path": "SOURCE/how_tos/summaries_and_tensorboard/index.md",
    "content": "# TensorBoard: Visualizing Learning <a class=\"md-anchor\" id=\"AUTOGENERATED-tensorboard--visualizing-learning\"></a>\r\n\r\nThe computations you'll use TensorBoard for - like training a massive\r\ndeep neural network - can be complex and confusing. To make it easier to\r\nunderstand, debug, and optimize TensorFlow programs, we've included a suite of\r\nvisualization tools called TensorBoard. You can use TensorBoard to visualize\r\nyour TensorFlow graph, plot quantitative metrics about the execution of your\r\ngraph, and show additional data like images that pass through it. When\r\nTensorBoard is fully configured, it looks like this:\r\n\r\n![MNIST TensorBoard](./mnist_tensorboard.png \"MNIST TensorBoard\")\r\n\r\n\r\n## Serializing the data <a class=\"md-anchor\" id=\"AUTOGENERATED-serializing-the-data\"></a>\r\n\r\nTensorBoard operates by reading TensorFlow events files, which contain summary\r\ndata that you can generate when running TensorFlow. Here's the general\r\nlifecycle for summary data within TensorBoard.\r\n\r\nFirst, create the TensorFlow graph that you'd like to collect summary\r\ndata from, and decide which nodes you would like to annotate with\r\n[summary operations]\r\n(../../api_docs/python/train.md#summary-operations).\r\n\r\nFor example, suppose you are training a convolutional neural network for\r\nrecognizing MNIST digits. You'd like to record how the learning rate\r\nvaries over time, and how the objective function is changing. Collect these by\r\nattaching [`scalar_summary`](../../api_docs/python/train.md#scalar_summary) ops\r\nto the nodes that output the learning rate and loss respectively. Then, give\r\neach `scalar_summary` a meaningful `tag`, like `'learning rate'` or `'loss\r\nfunction'`.\r\n\r\nPerhaps you'd also like to visualize the distributions of activations coming\r\noff a particular layer, or the distribution of gradients or weights. Collect\r\nthis data by attaching\r\n[`histogram_summary`](../../api_docs/python/train.md#histogram_summary) ops to\r\nthe gradient outputs and to the variable that holds your weights, respectively.\r\n\r\nFor details on all of the summary operations avaiable, check out the docs on\r\n[summary operations]\r\n(../../api_docs/python/train.md#summary-operations).\r\n\r\nOperations in TensorFlow don't do anything until you run them, or an op that\r\ndepends on their output. And the summary nodes that we've just created are\r\nperipheral to your graph: none of the ops you are currently running depend on\r\nthem. So, to generate summaries, we need to run all of these summary nodes.\r\nManaging them by hand would be tedious, so use\r\n[`tf.merge_all_summaries`](../../api_docs/python/train.md#merge_all_summaries)\r\nto combine them into a single op that generates all the summary data.\r\n\r\nThen, you can just run the merged summary op, which will generate a serialized\r\n`Summary` protobuf object with all of your summary data at a given step.\r\nFinally, to write this summary data to disk, pass the summary protobuf to a\r\n[`tf.train.SummaryWriter`](../../api_docs/python/train.md#SummaryWriter).\r\n\r\nThe `SummaryWriter` takes a logdir in its constructor - this logdir is quite\r\nimportant, it's the directory where all of the events will be written out.\r\nAlso, the `SummaryWriter` can optionally take a `GraphDef` in its constructor.\r\nIf it receives one, then TensorBoard will visualize your graph as well.\r\n\r\nNow that you've modified your graph and have a `SummaryWriter`, you're ready to\r\nstart runing your network! If you want, you could run the merged summary op\r\nevery single step, and record a ton of training data. That's likely to be more\r\ndata than you need, though. Instead, consider running the merged summary op\r\nevery hundred steps or so, as in the following code example.\r\n\r\n```python\r\nmerged_summary_op = tf.merge_all_summaries()\r\nsummary_writer = tf.train.SummaryWriter('/tmp/mnist_logs', sess.graph)\r\ntotal_step = 0\r\nwhile training:\r\n  total_step += 1\r\n  session.run(training_op)\r\n  if total_step % 100 == 0:\r\n    summary_str = session.run(merged_summary_op)\r\n    summary_writer.add_summary(summary_str, total_step)\r\n```\r\n\r\nYou're now all set to visualize this data using TensorBoard.\r\n\r\n\r\n## Launching TensorBoard <a class=\"md-anchor\" id=\"AUTOGENERATED-launching-tensorboard\"></a>\r\n\r\nTo run TensorBoard, use the command\r\n\r\n    python tensorflow/tensorboard/tensorboard.py --logdir=path/to/log-directory\r\n\r\nwhere `logdir` points to the directory where the `SummaryWriter` serialized its\r\ndata.  If this `logdir` directory contains subdirectories which contain\r\nserialized data from separate runs, then TensorBoard will visualize the data\r\nfrom all of those runs. Once TensorBoard is running, navigate your web browser\r\nto `localhost:6006` to view the TensorBoard.\r\n\r\nIf you have pip installed TensorBoard, you can use the simpler command\r\n\r\n    tensorboard --logdir=/path/to/log-directory\r\n\r\nWhen looking at TensorBoard, you will see the navigation tabs in the top right\r\ncorner. Each tab represents a set of serialized data that can be visualized.\r\nFor any tab you are looking at, if the logs being looked at by TensorBoard do\r\nnot contain any data relevant to that tab, a message will be displayed\r\nindicating how to serialize data that is applicable to that tab.\r\n\r\nFor in depth information on how to use the *graph* tab to visualize your graph,\r\nsee [TensorBoard: Visualizing your graph](../../how_tos/graph_viz/index.md).\r\n"
  },
  {
    "path": "SOURCE/how_tos/summaries_and_tensorboard.md",
    "content": "# TensorBoard:可视化学习　<a class=\"md-anchor\" id=\"AUTOGENERATED-tensorboard--visualizing-learning\"></a>\r\n\r\nTensorBoard 涉及到的运算，通常是在训练庞大的深度神经网络中出现的复杂而又难以理解的运算。\r\n\r\n为了更方便 TensorFlow 程序的理解、调试与优化，我们发布了一套叫做 TensorBoard 的可视化工具。你可以用 TensorBoard 来展现你的 TensorFlow 图像，绘制图像生成的定量指标图以及附加数据。\r\n\r\n当 TensorBoard 设置完成后，它应该是这样子的：\r\n\r\n![MNIST TensorBoard](../images/mnist_tensorboard.png)\r\n\r\n## 数据序列化　<a class=\"md-anchor\" id=\"AUTOGENERATED-serializing-the-data\"></a>\r\n\r\nTensorBoard 通过读取 TensorFlow 的事件文件来运行。TensorFlow 的事件文件包括了你会在 TensorFlow 运行中涉及到的主要数据。下面是 TensorBoard 中汇总数据（Summary data）的大体生命周期。\r\n\r\n首先，创建你想汇总数据的 TensorFlow 图，然后再选择你想在哪个节点进行[汇总(summary)操作](../api_docs/python/train.md#summary_options)。\r\n\r\n比如，假设你正在训练一个卷积神经网络，用于识别 MNISt 标签。你可能希望记录学习速度(learning rate)的如何变化，以及目标函数如何变化。通过向节点附加[scalar_summary](../api_docs/python/train.md#scalary_summary)操作来分别输出学习速度和期望误差。然后你可以给每个 scalary_summary 分配一个有意义的 `标签`，比如 `'learning rate'` 和 `'loss function'`。\r\n\r\n或者你还希望显示一个特殊层中激活的分布，或者梯度权重的分布。可以通过分别附加 [histogram_summary](../api_docs/python/train.md#histogram_summary) 运算来收集权重变量和梯度输出。\r\n\r\n所有可用的 summary 操作详细信息，可以查看[summary_operation](../api_docs/python/train.md#summary_operation)文档。\r\n\r\n在TensorFlow中，所有的操作只有当你执行，或者另一个操作依赖于它的输出时才会运行。我们刚才创建的这些节点（summary nodes）都围绕着你的图像：没有任何操作依赖于它们的结果。因此，为了生成汇总信息，我们需要运行所有这些节点。这样的手动工作是很乏味的，因此可以使用[tf.merge_all_summaries](../api_docs/python/train.md#scalary_summary)来将他们合并为一个操作。\r\n\r\n然后你可以执行合并命令，它会依据特点步骤将所有数据生成一个序列化的`Summary` protobuf对象。最后，为了将汇总数据写入磁盘，需要将汇总的protobuf对象传递给[tf.train.Summarywriter](../api_docs/python/train.md#SummaryWriter)。\r\n\r\n`SummaryWriter` 的构造函数中包含了参数 logdir。这个 logdir 非常重要，所有事件都会写到它所指的目录下。此外，`SummaryWriter` 中还包含了一个可选择的参数 `GraphDef`。如果输入了该参数，那么 TensorBoard 也会显示你的图像。\r\n\r\n现在已经修改了你的图，也有了 `SummaryWriter`，现在就可以运行你的神经网络了！如果你愿意的话，你可以每一步执行一次合并汇总，这样你会得到一大堆训练数据。这很有可能超过了你想要的数据量。你也可以每一百步执行一次合并汇总，或者如下面代码里示范的这样。\r\n\r\n```python\r\nmerged_summary_op = tf.merge_all_summaries()\r\nsummary_writer = tf.train.SummaryWriter('/tmp/mnist_logs', sess.graph)\r\ntotal_step = 0\r\nwhile training:\r\n  total_step += 1\r\n  session.run(training_op)\r\n  if total_step % 100 == 0:\r\n    summary_str = session.run(merged_summary_op)\r\n    summary_writer.add_summary(summary_str, total_step)\r\n```\r\n\r\n现在已经准备好用 TensorBoard 来可视化这些数据了。\r\n\r\n## 启动TensorBoard　<a class=\"md-anchor\" id=\"AUTOGENERATED-launching-tensorboard\"></a>\r\n\r\n输入下面的指令来启动TensorBoard\r\n\r\n```\r\npython tensorflow/tensorboard/tensorboard.py --logdir=path/to/log-directory\r\n```\r\n\r\n这里的参数 `logdir` 指向 `SummaryWriter` 序列化数据的存储路径。如果`logdir`目录的子目录中包含另一次运行时的数据，那么 TensorBoard 会展示所有运行的数据。一旦 TensorBoard 开始运行，你可以通过在浏览器中输入 `localhost:6006` 来查看 TensorBoard。\r\n\r\n如果你已经通过pip安装了 TensorBoard，你可以通过执行更为简单地命令来访问 TensorBoard\r\n\r\n```\r\ntensorboard --logdir=/path/to/log-directory\r\n```\r\n\r\n进入 TensorBoard 的界面时，你会在右上角看到导航选项卡，每一个选项卡将展现一组可视化的序列化数据集 。对于你查看的每一个选项卡，如果 TensorBoard 中没有数据与这个选项卡相关的话，则会显示一条提示信息指示你如何序列化相关数据。\r\n\r\n更多更详细的关于如何使用 graph 选项来显示你的图像的信息。参见 [TensorBoard:图表可视化](./graph_viz.md)\r\n\r\n原文地址：[TensorBoard:Visualizing Learning](http://tensorflow.org/how_tos/summaries_and_tensorboard/index.html#tensorboard-visualizing-learning) 翻译：[thylaco1eo](https://github.com/thylaco1eo) 校对：[lucky521](https://github.com/lucky521)\r\n"
  },
  {
    "path": "SOURCE/how_tos/threading_and_queues/index.md",
    "content": "# Threading and Queues <a class=\"md-anchor\" id=\"AUTOGENERATED-threading-and-queues\"></a>\r\n\r\nQueues are a powerful mechanism for asynchronous computation using TensorFlow.\r\n\r\nLike everything in TensorFlow, a queue is a node in a TensorFlow graph. It's a\r\nstateful node, like variable: other nodes can modify its content. In\r\nparticular, nodes can enqueue new items in to the queue, or dequeue existing\r\nitems from the queue.\r\n\r\nTo get a feel for queues, let's consider a simple example. We will create a\r\n\"first in, first out\" queue (`FIFOQueue`) and fill it with zeros.\r\nThen we'll construct a graph\r\nthat takes an item off the queue, adds one to that item, and puts it back on the\r\nend of the queue. Slowly, the numbers on the queue increase.\r\n\r\n<div style=\"width:70%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"threading_and_queues/IncremeterFifoQueue.gif\">\r\n</div>\r\n\r\n`Enqueue`, `EnqueueMany`, and `Dequeue` are special nodes. They take a pointer\r\nto the queue instead of a normal value, allowing them to change it. We recommend\r\nyou think of these as being like methods of the queue. In fact, in the Python\r\nAPI, they are methods of the queue object (eg. `q.enqueue(...)`).\r\n\r\nNow that you have a bit of a feel for queues, let's dive into the details...\r\n\r\n## Queue Use Overview <a class=\"md-anchor\" id=\"AUTOGENERATED-queue-use-overview\"></a>\r\n\r\nQueues, such as `FIFOQueue` and `RandomShuffleQueue`, are important TensorFlow\r\nobjects for computing tensors asynchronously in a graph.\r\n\r\nFor example, a typical input architecture is to use a `RandomShuffleQueue` to\r\nprepare inputs for training a model:\r\n\r\n* Multiple threads prepare training examples and push them in the queue.\r\n* A training thread executes a training op that dequeues mini-batches from the\r\n  queue\r\n\r\nThis architecture has many benefits, as highlighted in the\r\n[Reading data how to](../reading_data), which also gives an overview of\r\nfunctions that simplify the construction of input pipelines.\r\n\r\nThe TensorFlow `Session` object is multithreaded, so multiple threads can\r\neasily use the same session and run ops in parallel.  However, it is not always\r\neasy to implement a Python program that drives threads as described above.  All\r\nthreads must be able to stop together, exceptions must be caught and\r\nreported, and queues must be properly closed when stopping.\r\n\r\nTensorFlow provides two classes to help:\r\n[tf.Coordinator](../../api_docs/python/train.md#Coordinator) and\r\n[tf.QueueRunner](../../api_docs/python/train.md#QueueRunner). These two classes\r\nare designed to be used together. The `Coordinator` class helps multiple threads\r\nstop together and report exceptions to a program that waits for them to stop.\r\nThe `QueueRunner` class is used to create a number of threads cooperating to\r\nenqueue tensors in the same queue.\r\n\r\n## Coordinator <a class=\"md-anchor\" id=\"AUTOGENERATED-coordinator\"></a>\r\n\r\nThe Coordinator class helps multiple threads stop together.\r\n\r\nIts key methods are:\r\n\r\n* `should_stop()`: returns True if the threads should stop.\r\n* `request_stop(<exception>)`: requests that threads should stop.\r\n* `join(<list of threads>)`: waits until the specified threads have stopped.\r\n\r\nYou first create a `Coordinator` object, and then create a number of threads\r\nthat use the coordinator.  The threads typically run loops that stop when\r\n`should_stop()` returns `True`.\r\n\r\nAny thread can decide that the computation should stop.  It only has to call\r\n`request_stop()` and the other threads will stop as `should_stop()` will then\r\nreturn `True`.\r\n\r\n```python\r\n# Thread body: loop until the coordinator indicates a stop was requested.\r\n# If some condition becomes true, ask the coordinator to stop.\r\ndef MyLoop(coord):\r\n  while not coord.should_stop():\r\n    ...do something...\r\n    if ...some condition...:\r\n      coord.request_stop()\r\n\r\n# Main code: create a coordinator.\r\ncoord = Coordinator()\r\n\r\n# Create 10 threads that run 'MyLoop()'\r\nthreads = [threading.Thread(target=MyLoop, args=(coord)) for i in xrange(10)]\r\n\r\n# Start the threads and wait for all of them to stop.\r\nfor t in threads: t.start()\r\ncoord.join(threads)\r\n```\r\n\r\nObviously, the coordinator can manage threads doing very different things.\r\nThey don't have to be all the same as in the example above.  The coordinator\r\nalso has support to capture and report exceptions.  See the [Coordinator class](../../api_docs/python/train.md#Coordinator) documentation for more details.\r\n\r\n## QueueRunner <a class=\"md-anchor\" id=\"AUTOGENERATED-queuerunner\"></a>\r\n\r\nThe `QueueRunner` class creates a number of threads that repeatedly run an\r\nenqueue op.  These threads can use a coordinator to stop together.  In\r\naddition, a queue runner runs a *closer thread* that automatically closes the\r\nqueue if an exception is reported to the coordinator.\r\n\r\nYou can use a queue runner to implement the architecture described above.\r\n\r\nFirst build a graph that uses a `Queue` for input examples.  Add ops that\r\nprocess examples and enqueue them in the queue.  Add training ops that start by\r\ndequeueing from the queue.\r\n\r\n```python\r\nexample = ...ops to create one example...\r\n# Create a queue, and an op that enqueues examples one at a time in the queue.\r\nqueue = tf.RandomShuffleQueue(...)\r\nenqueue_op = queue.enqueue(example)\r\n# Create a training graph that starts by dequeuing a batch of examples.\r\ninputs = queue.dequeue_many(batch_size)\r\ntrain_op = ...use 'inputs' to build the training part of the graph...\r\n```\r\n\r\nIn the Python training program, create a `QueueRunner` that will run a few\r\nthreads to process and enqueue examples.  Create a `Coordinator` and ask the\r\nqueue runner to start its threads with the coordinator.  Write a training loop\r\nthat also uses the coordinator.\r\n\r\n```\r\n# Create a queue runner that will run 4 threads in parallel to enqueue\r\n# examples.\r\nqr = tf.train.QueueRunner(queue, [enqueue_op] * 4)\r\n\r\n# Launch the graph.\r\nsess = tf.Session()\r\n# Create a coordinator, launch the queue runner threads.\r\ncoord = tf.train.Coordinator()\r\nenqueue_threads = qr.create_threads(sess, coord=coord, start=True)\r\n# Run the training loop, controlling termination with the coordinator.\r\nfor step in xrange(1000000):\r\n    if coord.should_stop():\r\n        break\r\n    sess.run(train_op)\r\n# When done, ask the threads to stop.\r\ncoord.request_stop()\r\n# And wait for them to actually do it.\r\ncoord.join(threads)\r\n```\r\n\r\n## Handling Exceptions <a class=\"md-anchor\" id=\"AUTOGENERATED-handling-exceptions\"></a>\r\n\r\nThreads started by queue runners do more than just run the enqueue ops.  They\r\nalso catch and handle exceptions generated by queues, including\r\n`OutOfRangeError` which is used to report that a queue was closed.\r\n\r\nA training program that uses a coordinator must similarly catch and report\r\nexceptions in its main loop.\r\n\r\nHere is an improved version of the training loop above.\r\n\r\n```python\r\ntry:\r\n    for step in xrange(1000000):\r\n        if coord.should_stop():\r\n            break\r\n        sess.run(train_op)\r\nexcept Exception, e:\r\n   # Report exceptions to the coordinator.\r\n   coord.request_stop(e)\r\n\r\n# Terminate as usual.  It is innocuous to request stop twice.\r\ncoord.request_stop()\r\ncoord.join(threads)\r\n```\r\n\r\n原文地址：([Threading and Queues](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/how_tos/threading_and_queues/index.md)) 翻译：([zhangkom](https://github.com/zhangkom)) 校对：\r\n"
  },
  {
    "path": "SOURCE/how_tos/threading_and_queues.md",
    "content": "# 线程和队列 <a class=\"md-anchor\" id=\"AUTOGENERATED-threading-and-queues\"></a>\r\n\r\n在使用TensorFlow进行异步计算时，队列是一种强大的机制。\r\n\r\n正如TensorFlow中的其他组件一样，队列就是TensorFlow图中的节点。这是一种有状态的节点，就像变量一样：其他节点可以修改它的内容。具体来说，其他节点可以把新元素插入到队列后端(rear)，也可以把队列前端(front)的元素删除。\r\n\r\n为了感受一下队列，让我们来看一个简单的例子。我们先创建一个“先入先出”的队列（FIFOQueue），并将其内部所有元素初始化为零。然后，我们构建一个TensorFlow图，它从队列前端取走一个元素，加上1之后，放回队列的后端。慢慢地，队列的元素的值就会增加。\r\n\r\n<div style=\"width:70%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/IncremeterFifoQueue.gif\">\r\n</div>\r\n`Enqueue`、 `EnqueueMany`和`Dequeue`都是特殊的节点。他们需要获取队列指针，而非普通的值，如此才能修改队列内容。我们建议您将它们看作队列的方法。事实上，在Python API中，它们就是队列对象的方法（例如`q.enqueue(...)`）。\r\n\r\n现在你已经对队列有了一定的了解，让我们深入到细节...\r\n\r\n## 队列使用概述 <a class=\"md-anchor\" id=\"AUTOGENERATED-queue-use-overview\"></a>\r\n\r\n队列，如`FIFOQueue`和`RandomShuffleQueue`，在TensorFlow的张量异步计算时都非常重要。\r\n\r\n例如，一个典型的输入结构：是使用一个`RandomShuffleQueue`来作为模型训练的输入：\r\n\r\n* 多个线程准备训练样本，并且把这些样本推入队列。\r\n* 一个训练线程执行一个训练操作，此操作会从队列中移除最小批次的样本（mini-batches)。\r\n\r\n这种结构具有许多优点，正如在[Reading data how to](../reading_data)中强调的，同时，[Reading data how to](../reading_data)也概括地描述了如何简化输入管道的构造过程。\r\n\r\nTensorFlow的`Session`对象是可以支持多线程的，因此多个线程可以很方便地使用同一个会话（Session）并且并行地执行操作。然而，在Python程序实现这样的并行运算却并不容易。所有线程都必须能被同步终止，异常必须能被正确捕获并报告，回话终止的时候， 队列必须能被正确地关闭。\r\n\r\n所幸TensorFlow提供了两个类来帮助多线程的实现：[tf.Coordinator](tensorflow-zh/SOURCE/api_docs/python/train.md#Coordinator)和\r\n[tf.QueueRunner](tensorflow-zh/SOURCE/api_docs/python/train.md#QueueRunner)。从设计上这两个类必须被一起使用。`Coordinator`类可以用来同时停止多个工作线程并且向那个在等待所有工作线程终止的程序报告异常。`QueueRunner`类用来协调多个工作线程同时将多个张量推入同一个队列中。\r\n\r\n## Coordinator <a class=\"md-anchor\" id=\"AUTOGENERATED-coordinator\"></a>\r\n\r\nCoordinator类用来帮助多个线程协同工作，多个线程同步终止。\r\n其主要方法有：\r\n\r\n* `should_stop()`:如果线程应该停止则返回True。\r\n* `request_stop(<exception>)`: 请求该线程停止。\r\n* `join(<list of threads>)`:等待被指定的线程终止。\r\n\r\n首先创建一个`Coordinator`对象，然后建立一些使用`Coordinator`对象的线程。这些线程通常一直循环运行，一直到`should_stop()`返回True时停止。\r\n任何线程都可以决定计算什么时候应该停止。它只需要调用`request_stop()`，同时其他线程的`should_stop()`将会返回`True`，然后都停下来。\r\n\r\n```python\r\n# 线程体：循环执行，直到`Coordinator`收到了停止请求。\r\n# 如果某些条件为真，请求`Coordinator`去停止其他线程。\r\ndef MyLoop(coord):\r\n  while not coord.should_stop():\r\n    ...do something...\r\n    if ...some condition...:\r\n      coord.request_stop()\r\n\r\n# Main code: create a coordinator.\r\ncoord = Coordinator()\r\n\r\n# Create 10 threads that run 'MyLoop()'\r\nthreads = [threading.Thread(target=MyLoop, args=(coord)) for i in xrange(10)]\r\n\r\n# Start the threads and wait for all of them to stop.\r\nfor t in threads: t.start()\r\ncoord.join(threads)\r\n```\r\n\r\n显然，Coordinator可以管理线程去做不同的事情。上面的代码只是一个简单的例子，在设计实现的时候不必完全照搬。Coordinator还支持捕捉和报告异常, 具体可以参考[Coordinator class](tensorflow-zh/SOURCE/api_docs/python/train.md#Coordinator)的文档。\r\n\r\n## QueueRunner <a class=\"md-anchor\" id=\"AUTOGENERATED-queuerunner\"></a>\r\n\r\n`QueueRunner`类会创建一组线程， 这些线程可以重复的执行Enquene操作， 他们使用同一个Coordinator来处理线程同步终止。此外，一个QueueRunner会运行一个*closer thread*，当Coordinator收到异常报告时，这个*closer thread*会自动关闭队列。\r\n\r\n您可以使用一个queue runner，来实现上述结构。\r\n首先建立一个TensorFlow图表，这个图表使用队列来输入样本。增加处理样本并将样本推入队列中的操作。增加training操作来移除队列中的样本。\r\n\r\n```python\r\nexample = ...ops to create one example...\r\n# Create a queue, and an op that enqueues examples one at a time in the queue.\r\nqueue = tf.RandomShuffleQueue(...)\r\nenqueue_op = queue.enqueue(example)\r\n# Create a training graph that starts by dequeuing a batch of examples.\r\ninputs = queue.dequeue_many(batch_size)\r\ntrain_op = ...use 'inputs' to build the training part of the graph...\r\n```\r\n\r\n在Python的训练程序中，创建一个`QueueRunner`来运行几个线程， 这几个线程处理样本，并且将样本推入队列。创建一个`Coordinator`，让queue runner使用`Coordinator`来启动这些线程，创建一个训练的循环， 并且使用`Coordinator`来控制`QueueRunner`的线程们的终止。\r\n\r\n```\r\n# Create a queue runner that will run 4 threads in parallel to enqueue\r\n# examples.\r\nqr = tf.train.QueueRunner(queue, [enqueue_op] * 4)\r\n\r\n# Launch the graph.\r\nsess = tf.Session()\r\n# Create a coordinator, launch the queue runner threads.\r\ncoord = tf.train.Coordinator()\r\nenqueue_threads = qr.create_threads(sess, coord=coord, start=True)\r\n# Run the training loop, controlling termination with the coordinator.\r\nfor step in xrange(1000000):\r\n    if coord.should_stop():\r\n        break\r\n    sess.run(train_op)\r\n# When done, ask the threads to stop.\r\ncoord.request_stop()\r\n# And wait for them to actually do it.\r\ncoord.join(threads)\r\n```\r\n\r\n## 异常处理 <a class=\"md-anchor\" id=\"AUTOGENERATED-handling-exceptions\"></a>\r\n\r\n通过queue runners启动的线程不仅仅只处理推送样本到队列。他们还捕捉和处理由队列产生的异常，包括`OutOfRangeError`异常，这个异常是用于报告队列被关闭。\r\n使用`Coordinator`的训练程序在主循环中必须同时捕捉和报告异常。\r\n下面是对上面训练循环的改进版本。\r\n\r\n```python\r\ntry:\r\n    for step in xrange(1000000):\r\n        if coord.should_stop():\r\n            break\r\n        sess.run(train_op)\r\nexcept Exception, e:\r\n   # Report exceptions to the coordinator.\r\n   coord.request_stop(e)\r\n\r\n# Terminate as usual.  It is innocuous to request stop twice.\r\ncoord.request_stop()\r\ncoord.join(threads)\r\n```\r\n\r\n原文地址：[Threading and Queues](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/how_tos/threading_and_queues/index.md) 翻译：[zhangkom](https://github.com/zhangkom) 校对：[volvet](https://github.com/volvet)\r\n"
  },
  {
    "path": "SOURCE/how_tos/using_gpu/index.md",
    "content": "# Using GPUs <a class=\"md-anchor\" id=\"AUTOGENERATED-using-gpus\"></a>\r\n\r\n## Supported devices <a class=\"md-anchor\" id=\"AUTOGENERATED-supported-devices\"></a>\r\n\r\nOn a typical system, there are multiple computing devices. In TensorFlow, the\r\nsupported device types are `CPU` and `GPU`.  They are represented as\r\n`strings`. For example:\r\n\r\n*  `\"/cpu:0\"`: The CPU of your machine.\r\n*  `\"/gpu:0\"`: The GPU of your machine, if you have one.\r\n*  `\"/gpu:1\"`: The second GPU of your machine, etc.\r\n\r\nIf a TensorFlow operation has both CPU and GPU implementations, the\r\nGPU devices will be given priority when the operation is assigned to\r\na device. For example, `matmul` has both CPU and GPU kernels.  On a\r\nsystem with devices `cpu:0` and `gpu:0`, `gpu:0` will be selected to run\r\n`matmul`.\r\n\r\n## Logging Device placement <a class=\"md-anchor\" id=\"AUTOGENERATED-logging-device-placement\"></a>\r\n\r\nTo find out which devices your operations and tensors are assigned to, create\r\nthe session with `log_device_placement` configuration option set to `True`.\r\n\r\n```python\r\n# Creates a graph.\r\na = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')\r\nb = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')\r\nc = tf.matmul(a, b)\r\n# Creates a session with log_device_placement set to True.\r\nsess = tf.Session(config=tf.ConfigProto(log_device_placement=True))\r\n# Runs the op.\r\nprint sess.run(c)\r\n```\r\n\r\nYou should see the following output:\r\n\r\n```\r\nDevice mapping:\r\n/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci bus\r\nid: 0000:05:00.0\r\nb: /job:localhost/replica:0/task:0/gpu:0\r\na: /job:localhost/replica:0/task:0/gpu:0\r\nMatMul: /job:localhost/replica:0/task:0/gpu:0\r\n[[ 22.  28.]\r\n [ 49.  64.]]\r\n\r\n```\r\n\r\n## Manual device placement <a class=\"md-anchor\" id=\"AUTOGENERATED-manual-device-placement\"></a>\r\n\r\nIf you would like a particular operation to run on a device of your\r\nchoice instead of what's automatically selected for you, you can use\r\n`with tf.device` to create a device context such that all the operations\r\nwithin that context will have the same device assignment.\r\n\r\n```python\r\n# Creates a graph.\r\nwith tf.device('/cpu:0'):\r\n  a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')\r\n  b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')\r\nc = tf.matmul(a, b)\r\n# Creates a session with log_device_placement set to True.\r\nsess = tf.Session(config=tf.ConfigProto(log_device_placement=True))\r\n# Runs the op.\r\nprint sess.run(c)\r\n```\r\n\r\nYou will see that now `a` and `b` are assigned to `cpu:0`.\r\n\r\n```\r\nDevice mapping:\r\n/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci bus\r\nid: 0000:05:00.0\r\nb: /job:localhost/replica:0/task:0/cpu:0\r\na: /job:localhost/replica:0/task:0/cpu:0\r\nMatMul: /job:localhost/replica:0/task:0/gpu:0\r\n[[ 22.  28.]\r\n [ 49.  64.]]\r\n```\r\n\r\n## Using a single GPU on a multi-GPU system <a class=\"md-anchor\" id=\"AUTOGENERATED-using-a-single-gpu-on-a-multi-gpu-system\"></a>\r\n\r\nIf you have more than one GPU in your system, the GPU with the lowest ID will be\r\nselected by default. If you would like to run on a different GPU, you will need\r\nto specify the preference explicitly:\r\n\r\n```python\r\n# Creates a graph.\r\nwith tf.device('/gpu:2'):\r\n  a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')\r\n  b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')\r\n  c = tf.matmul(a, b)\r\n# Creates a session with log_device_placement set to True.\r\nsess = tf.Session(config=tf.ConfigProto(log_device_placement=True))\r\n# Runs the op.\r\nprint sess.run(c)\r\n```\r\n\r\nIf the device you have specified does not exist, you will get\r\n`InvalidArgumentError`:\r\n\r\n```\r\nInvalidArgumentError: Invalid argument: Cannot assign a device to node 'b':\r\nCould not satisfy explicit device specification '/gpu:2'\r\n   [[Node: b = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [3,2]\r\n   values: 1 2 3...>, _device=\"/gpu:2\"]()]]\r\n```\r\n\r\nIf you would like TensorFlow to automatically choose an existing and\r\nsupported device to run the operations in case the specified one doesn't\r\nexist, you can set `allow_soft_placement` to `True` in the configuration\r\noption when creating the session.\r\n\r\n```python\r\n# Creates a graph.\r\nwith tf.device('/gpu:2'):\r\n  a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')\r\n  b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')\r\n  c = tf.matmul(a, b)\r\n# Creates a session with allow_soft_placement and log_device_placement set\r\n# to True.\r\nsess = tf.Session(config=tf.ConfigProto(\r\n      allow_soft_placement=True, log_device_placement=True))\r\n# Runs the op.\r\nprint sess.run(c)\r\n```\r\n\r\n## Using multiple GPUs <a class=\"md-anchor\" id=\"AUTOGENERATED-using-multiple-gpus\"></a>\r\n\r\nIf you would like to run TensorFlow on multiple GPUs, you can construct your\r\nmodel in a multi-tower fashion where each tower is assigned to a different GPU.\r\nFor example:\r\n\r\n```\r\n# Creates a graph.\r\nc = []\r\nfor d in ['/gpu:2', '/gpu:3']:\r\n  with tf.device(d):\r\n    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])\r\n    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])\r\n    c.append(tf.matmul(a, b))\r\nwith tf.device('/cpu:0'):\r\n  sum = tf.add_n(c)\r\n# Creates a session with log_device_placement set to True.\r\nsess = tf.Session(config=tf.ConfigProto(log_device_placement=True))\r\n# Runs the op.\r\nprint sess.run(sum)\r\n```\r\n\r\nYou will see the following output.\r\n\r\n```\r\nDevice mapping:\r\n/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K20m, pci bus\r\nid: 0000:02:00.0\r\n/job:localhost/replica:0/task:0/gpu:1 -> device: 1, name: Tesla K20m, pci bus\r\nid: 0000:03:00.0\r\n/job:localhost/replica:0/task:0/gpu:2 -> device: 2, name: Tesla K20m, pci bus\r\nid: 0000:83:00.0\r\n/job:localhost/replica:0/task:0/gpu:3 -> device: 3, name: Tesla K20m, pci bus\r\nid: 0000:84:00.0\r\nConst_3: /job:localhost/replica:0/task:0/gpu:3\r\nConst_2: /job:localhost/replica:0/task:0/gpu:3\r\nMatMul_1: /job:localhost/replica:0/task:0/gpu:3\r\nConst_1: /job:localhost/replica:0/task:0/gpu:2\r\nConst: /job:localhost/replica:0/task:0/gpu:2\r\nMatMul: /job:localhost/replica:0/task:0/gpu:2\r\nAddN: /job:localhost/replica:0/task:0/cpu:0\r\n[[  44.   56.]\r\n [  98.  128.]]\r\n```\r\n\r\nThe [cifar10 tutorial](../../tutorials/deep_cnn/index.md) is a good example\r\ndemonstrating how to do training with multiple GPUs.\r\n"
  },
  {
    "path": "SOURCE/how_tos/using_gpu.md",
    "content": "# 使用 GPUs <a class=\"md-anchor\" id=\"AUTOGENERATED-using-gpus\"></a>\r\n\r\n## 支持的设备 <a class=\"md-anchor\" id=\"AUTOGENERATED-supported-devices\"></a>\r\n\r\n在一套标准的系统上通常有多个计算设备. TensorFlow 支持 CPU 和 GPU 这两种设备. 我们用指定字符串\r\n`strings` 来标识这些设备. 比如:\r\n\r\n*  `\"/cpu:0\"`: 机器中的 CPU\r\n*  `\"/gpu:0\"`: 机器中的 GPU, 如果你有一个的话.\r\n*  `\"/gpu:1\"`: 机器中的第二个 GPU, 以此类推...\r\n\r\n如果一个 TensorFlow 的 operation 中兼有 CPU 和 GPU 的实现, 当这个算子被指派设备时, GPU 有优先权. 比如`matmul`中 CPU\r\n和 GPU kernel 函数都存在. 那么在 `cpu:0` 和 `gpu:0` 中, `matmul` operation 会被指派给 `gpu:0` .\r\n\r\n## 记录设备指派情况 <a class=\"md-anchor\" id=\"AUTOGENERATED-logging-device-placement\"></a>\r\n\r\n为了获取你的 operations 和 Tensor 被指派到哪个设备上运行, 用 `log_device_placement` 新建一个 `session`, 并设置为 `True`.\r\n\r\n```python\r\n# 新建一个 graph.\r\na = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')\r\nb = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')\r\nc = tf.matmul(a, b)\r\n# 新建session with log_device_placement并设置为True.\r\nsess = tf.Session(config=tf.ConfigProto(log_device_placement=True))\r\n# 运行这个 op.\r\nprint sess.run(c)\r\n```\r\n\r\n你应该能看见以下输出:\r\n\r\n```\r\nDevice mapping:\r\n/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci bus\r\nid: 0000:05:00.0\r\nb: /job:localhost/replica:0/task:0/gpu:0\r\na: /job:localhost/replica:0/task:0/gpu:0\r\nMatMul: /job:localhost/replica:0/task:0/gpu:0\r\n[[ 22.  28.]\r\n [ 49.  64.]]\r\n\r\n```\r\n\r\n## 手工指派设备 <a class=\"md-anchor\" id=\"AUTOGENERATED-manual-device-placement\"></a>\r\n\r\n如果你不想使用系统来为 operation 指派设备, 而是手工指派设备, 你可以用 `with tf.device`\r\n创建一个设备环境, 这个环境下的 operation 都统一运行在环境指定的设备上.\r\n\r\n```python\r\n# 新建一个graph.\r\nwith tf.device('/cpu:0'):\r\n  a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')\r\n  b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')\r\nc = tf.matmul(a, b)\r\n# 新建session with log_device_placement并设置为True.\r\nsess = tf.Session(config=tf.ConfigProto(log_device_placement=True))\r\n# 运行这个op.\r\nprint sess.run(c)\r\n```\r\n\r\n你会发现现在 `a` 和 `b` 操作都被指派给了 `cpu:0`.\r\n\r\n```\r\nDevice mapping:\r\n/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci bus\r\nid: 0000:05:00.0\r\nb: /job:localhost/replica:0/task:0/cpu:0\r\na: /job:localhost/replica:0/task:0/cpu:0\r\nMatMul: /job:localhost/replica:0/task:0/gpu:0\r\n[[ 22.  28.]\r\n [ 49.  64.]]\r\n```\r\n\r\n## 在多GPU系统里使用单一GPU<a class=\"md-anchor\" id=\"AUTOGENERATED-using-a-single-gpu-on-a-multi-gpu-system\"></a>\r\n\r\n如果你的系统里有多个 GPU, 那么 ID 最小的 GPU 会默认使用. 如果你想用别的 GPU, 可以用下面的方法显式的声明你的偏好:\r\n\r\n```python\r\n# 新建一个 graph.\r\nwith tf.device('/gpu:2'):\r\n  a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')\r\n  b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')\r\n  c = tf.matmul(a, b)\r\n# 新建 session with log_device_placement 并设置为 True.\r\nsess = tf.Session(config=tf.ConfigProto(log_device_placement=True))\r\n# 运行这个 op.\r\nprint sess.run(c)\r\n```\r\n\r\n如果你指定的设备不存在, 你会收到 `InvalidArgumentError` 错误提示:\r\n\r\n```\r\nInvalidArgumentError: Invalid argument: Cannot assign a device to node 'b':\r\nCould not satisfy explicit device specification '/gpu:2'\r\n   [[Node: b = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [3,2]\r\n   values: 1 2 3...>, _device=\"/gpu:2\"]()]]\r\n```\r\n\r\n为了避免出现你指定的设备不存在这种情况, 你可以在创建的 `session` 里把参数 `allow_soft_placement` 设置为 `True`, 这样 tensorFlow 会自动选择一个存在并且支持的设备来运行 operation.\r\n\r\n```python\r\n# 新建一个 graph.\r\nwith tf.device('/gpu:2'):\r\n  a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')\r\n  b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')\r\n  c = tf.matmul(a, b)\r\n# 新建 session with log_device_placement 并设置为 True.\r\nsess = tf.Session(config=tf.ConfigProto(\r\n      allow_soft_placement=True, log_device_placement=True))\r\n# 运行这个 op.\r\nprint sess.run(c)\r\n```\r\n\r\n## 使用多个 GPU <a class=\"md-anchor\" id=\"AUTOGENERATED-using-multiple-gpus\"></a>\r\n\r\n如果你想让 TensorFlow 在多个 GPU 上运行, 你可以建立 multi-tower 结构, 在这个结构\r\n里每个 tower 分别被指配给不同的 GPU 运行. 比如:\r\n\r\n```\r\n# 新建一个 graph.\r\nc = []\r\nfor d in ['/gpu:2', '/gpu:3']:\r\n  with tf.device(d):\r\n    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])\r\n    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])\r\n    c.append(tf.matmul(a, b))\r\nwith tf.device('/cpu:0'):\r\n  sum = tf.add_n(c)\r\n# 新建session with log_device_placement并设置为True.\r\nsess = tf.Session(config=tf.ConfigProto(log_device_placement=True))\r\n# 运行这个op.\r\nprint sess.run(sum)\r\n```\r\n\r\n你会看到如下输出:\r\n\r\n```\r\nDevice mapping:\r\n/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K20m, pci bus\r\nid: 0000:02:00.0\r\n/job:localhost/replica:0/task:0/gpu:1 -> device: 1, name: Tesla K20m, pci bus\r\nid: 0000:03:00.0\r\n/job:localhost/replica:0/task:0/gpu:2 -> device: 2, name: Tesla K20m, pci bus\r\nid: 0000:83:00.0\r\n/job:localhost/replica:0/task:0/gpu:3 -> device: 3, name: Tesla K20m, pci bus\r\nid: 0000:84:00.0\r\nConst_3: /job:localhost/replica:0/task:0/gpu:3\r\nConst_2: /job:localhost/replica:0/task:0/gpu:3\r\nMatMul_1: /job:localhost/replica:0/task:0/gpu:3\r\nConst_1: /job:localhost/replica:0/task:0/gpu:2\r\nConst: /job:localhost/replica:0/task:0/gpu:2\r\nMatMul: /job:localhost/replica:0/task:0/gpu:2\r\nAddN: /job:localhost/replica:0/task:0/cpu:0\r\n[[  44.   56.]\r\n [  98.  128.]]\r\n```\r\n\r\n[cifar10 tutorial](tensorflow-zh/SOURCE/tutorials/deep_cnn/index.md) 这个例子很好的演示了怎样用GPU集群训练.\r\n\r\n> 原文:[using_gpu](http://tensorflow.org/how_tos/using_gpu/index.md)\r\n翻译:[@lianghyv](https://github.com/lianghyv) 校对:[Wiki](https://github.com/jikexueyuanwiki)\r\n"
  },
  {
    "path": "SOURCE/how_tos/variable_scope/index.md",
    "content": "# 共享变量<a class=\"md-anchor\" id=\"AUTOGENERATED-sharing-variables\"></a>\r\n你可以在[怎么使用变量](../../how_tos/variables/index.md)中所描述的方式来创建，初始化，保存及加载单一的变量.但是当创建复杂的模块时，通常你需要共享大量变量集并且如果你还想在同一个地方初始化这所有的变量,我们又该怎么做呢.本教程就是演示如何使用`tf.variable_scope()` 和`tf.get_variable()`两个方法来实现这一点.\r\n\r\n\r\n## 问题<a class=\"md-anchor\" id=\"AUTOGENERATED-the-problem\"></a>\r\n假设你为图片过滤器创建了一个简单的模块，和我们的[卷积神经网络教程](../../tutorials/deep_cnn/index.md)模块相似,但是这里包括两个卷积（为了简化实例这里只有两个）.如果你仅使用`tf.Variable`变量,那么你的模块就如[怎么使用变量](../../how_tos/variables/index.md)里面所解释的是一样的模块.\r\n\r\n```python\r\ndef my_image_filter(input_images):\r\n    conv1_weights = tf.Variable(tf.random_normal([5, 5, 32, 32]),\r\n        name=\"conv1_weights\")\r\n    conv1_biases = tf.Variable(tf.zeros([32]), name=\"conv1_biases\")\r\n    conv1 = tf.nn.conv2d(input_images, conv1_weights,\r\n        strides=[1, 1, 1, 1], padding='SAME')\r\n    relu1 = tf.nn.relu(conv1 + conv1_biases)\r\n\r\n    conv2_weights = tf.Variable(tf.random_normal([5, 5, 32, 32]),\r\n        name=\"conv2_weights\")\r\n    conv2_biases = tf.Variable(tf.zeros([32]), name=\"conv2_biases\")\r\n    conv2 = tf.nn.conv2d(relu1, conv2_weights,\r\n        strides=[1, 1, 1, 1], padding='SAME')\r\n    return tf.nn.relu(conv2 + conv2_biases)\r\n```\r\n\r\n你很容易想到，模块集很快就比一个模块变得更为复杂，仅在这里我们就有了四个不同的变量：`conv1_weights`,`conv1_biases`, `conv2_weights`, 和`conv2_biases`.\r\n当我们想重用这个模块时问题还在增多.假设你想把你的图片过滤器运用到两张不同的图片， `image1`和`image2`.你想通过拥有同一个参数的同一个过滤器来过滤两张图片，你可以调用`my_image_filter()`两次，但是这会产生两组变量.\r\n\r\n```python\r\n# First call creates one set of variables.\r\nresult1 = my_image_filter(image1)\r\n# Another set is created in the second call.\r\nresult2 = my_image_filter(image2)\r\n```\r\n通常共享变量的方法就是在单独的代码块中来创建他们并且通过使用他们的函数.如使用字典的例子：\r\n\r\n```python\r\nvariables_dict = {\r\n    \"conv1_weights\": tf.Variable(tf.random_normal([5, 5, 32, 32]),\r\n        name=\"conv1_weights\")\r\n    \"conv1_biases\": tf.Variable(tf.zeros([32]), name=\"conv1_biases\")\r\n    ... etc. ...\r\n}\r\n\r\ndef my_image_filter(input_images, variables_dict):\r\n    conv1 = tf.nn.conv2d(input_images, variables_dict[\"conv1_weights\"],\r\n        strides=[1, 1, 1, 1], padding='SAME')\r\n    relu1 = tf.nn.relu(conv1 + variables_dict[\"conv1_biases\"])\r\n\r\n    conv2 = tf.nn.conv2d(relu1, variables_dict[\"conv2_weights\"],\r\n        strides=[1, 1, 1, 1], padding='SAME')\r\n    return tf.nn.relu(conv2 + variables_dict[\"conv2_biases\"])\r\n\r\n# The 2 calls to my_image_filter() now use the same variables\r\nresult1 = my_image_filter(image1, variables_dict)\r\nresult2 = my_image_filter(image2, variables_dict)\r\n```\r\n虽然使用上面的方式创建变量是很方便的，但是在这个模块代码之外却破坏了其封装性：\r\n*  在构建视图的代码中标明变量的名字，类型，形状来创建.\r\n*  当代码改变了，调用的地方也许就会产生或多或少或不同类型的变量.\r\n\r\n解决此类问题的方法之一就是使用类来创建模块，在需要的地方使用类来小心地管理他们需要的变量.\r\n一个更高明的做法,不用调用类，而是利用TensorFlow 提供了*变量作用域* 机制，当构建一个视图时,很容易就可以共享命名过的变量.\r\n\r\n## 变量作用域实例<a class=\"md-anchor\" id=\"AUTOGENERATED-variable-scope-example\"></a>\r\n\r\n\r\n变量作用域机制在TensorFlow中主要由两部分组成：\r\n* `tf.get_variable(<name>, <shape>, <initializer>)`:\r\n通过所给的名字创建或是返回一个变量.\r\n* `tf.variable_scope(<scope_name>)`:\r\n通过 `tf.get_variable()`为变量名指定命名空间.\r\n\r\n方法 `tf.get_variable()` 用来获取或创建一个变量，而不是直接调用`tf.Variable`.它采用的不是像`tf.Variable这样直接获取值来初始化的方法.一个初始化就是一个方法，创建其形状并且为这个形状提供一个张量.这里有一些在TensorFlow中使用的初始化变量：\r\n\r\n* `tf.constant_initializer(value)` 初始化一切所提供的值,\r\n* `tf.random_uniform_initializer(a, b)`从a到b均匀初始化,\r\n* `tf.random_normal_initializer(mean, stddev)` 用所给平均值和标准差初始化均匀分布.\r\n\r\n为了了解`tf.get_variable()`怎么解决前面所讨论的问题,让我们在单独的方法里面创建一个卷积来重构一下代码，命名为`conv_relu`：\r\n\r\n\r\n```python\r\ndef conv_relu(input, kernel_shape, bias_shape):\r\n    # Create variable named \"weights\".\r\n    weights = tf.get_variable(\"weights\", kernel_shape,\r\n        initializer=tf.random_normal_initializer())\r\n    # Create variable named \"biases\".\r\n    biases = tf.get_variable(\"biases\", bias_shape,\r\n        initializer=tf.constant_intializer(0.0))\r\n    conv = tf.nn.conv2d(input, weights,\r\n        strides=[1, 1, 1, 1], padding='SAME')\r\n    return tf.nn.relu(conv + biases)\r\n```\r\n这个方法中用了`\"weights\"` 和`\"biases\"`两个简称.而我们更偏向于用`conv1` 和 `conv2`这两个变量的写法,但是不同的变量需要不同的名字.这就是`tf.variable_scope()` 变量起作用的地方.他为变量指定了相应的命名空间.\r\n\r\n```python\r\ndef my_image_filter(input_images):\r\n    with tf.variable_scope(\"conv1\"):\r\n        # Variables created here will be named \"conv1/weights\", \"conv1/biases\".\r\n        relu1 = conv_relu(input_images, [5, 5, 32, 32], [32])\r\n    with tf.variable_scope(\"conv2\"):\r\n        # Variables created here will be named \"conv2/weights\", \"conv2/biases\".\r\n        return conv_relu(relu1, [5, 5, 32, 32], [32])\r\n```\r\n现在，让我们看看当我们调用 `my_image_filter()` 两次时究竟会发生了什么.\r\n\r\n\r\n```\r\nresult1 = my_image_filter(image1)\r\nresult2 = my_image_filter(image2)\r\n# Raises ValueError(... conv1/weights already exists ...)\r\n```\r\n就像你看见的一样，`tf.get_variable()`会检测已经存在的变量是否已经共享.如果你想共享他们，你需要像下面使用的一样，通过`reuse_variables()`这个方法来指定.\r\n\r\n```\r\nwith tf.variable_scope(\"image_filters\") as scope:\r\n    result1 = my_image_filter(image1)\r\n    scope.reuse_variables()\r\n    result2 = my_image_filter(image2)\r\n```\r\n用这种方式来共享变量是非常好的，轻量级而且安全.\r\n\r\n\r\n## 变量作用域是怎么工作的？<a class=\"md-anchor\" id=\"AUTOGENERATED-how-does-variable-scope-work-\"></a>\r\n\r\n### 理解 `tf.get_variable()` <a class=\"md-anchor\" id=\"AUTOGENERATED-understanding--tf.get_variable---\"></a>\r\n为了理解变量作用域，首先完全理解`tf.get_variable()`是怎么工作的是很有必要的.\r\n通常我们就是这样调用`tf.get_variable` 的.\r\n\r\n```python\r\nv = tf.get_variable(name, shape, dtype, initializer)\r\n```\r\n此调用做了有关作用域的两件事中的其中之一，方法调入.总的有两种情况.\r\n\r\n* 情况1:当`tf.get_variable_scope().reuse == False`时，作用域就是为创建新变量所设置的.\r\n\r\n这种情况下，`v`将通过`tf.Variable`所提供的形状和数据类型来重新创建.创建变量的全称将会由当前变量作用域名+所提供的`名字`所组成,并且还会检查来确保没有任何变量使用这个全称.如果这个全称已经有一个变量使用了，那么方法将会抛出`ValueError`错误.如果一个变量被创建,他将会用`initializer(shape)`进行初始化.比如：\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    v = tf.get_variable(\"v\", [1])\r\nassert v.name == \"foo/v:0\"\r\n```\r\n* 情况1：当`tf.get_variable_scope().reuse == True`时，作用域是为重用变量所设置\r\n\r\n这种情况下，调用就会搜索一个已经存在的变量，他的全称和当前变量的作用域名+所提供的`名字`是否相等.如果不存在相应的变量，就会抛出`ValueError` 错误.如果变量找到了，就返回这个变量.如下：\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    v = tf.get_variable(\"v\", [1])\r\nwith tf.variable_scope(\"foo\", reuse=True):\r\n    v1 = tf.get_variable(\"v\", [1])\r\nassert v1 == v\r\n```\r\n\r\n### `tf.variable_scope()` 基础<a class=\"md-anchor\" id=\"AUTOGENERATED-basics-of--tf.variable_scope---\"></a>\r\n知道`tf.get_variable()`是怎么工作的,使得理解变量作用域变得很容易.变量作用域的主方法带有一个名称，它将会作为前缀用于变量名,并且带有一个重用标签来区分以上的两种情况.嵌套的作用域附加名字所用的规则和文件目录的规则很类似：\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    with tf.variable_scope(\"bar\"):\r\n        v = tf.get_variable(\"v\", [1])\r\nassert v.name == \"foo/bar/v:0\"\r\n```\r\n当前变量作用域可以用`tf.get_variable_scope()`进行检索并且`reuse` 标签可以通过调用`tf.get_variable_scope().reuse_variables()`设置为`True` .\r\n\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    v = tf.get_variable(\"v\", [1])\r\n    tf.get_variable_scope().reuse_variables()\r\n    v1 = tf.get_variable(\"v\", [1])\r\nassert v1 == v\r\n```\r\n注意你*不能*设置`reuse`标签为`False`.其中的原因就是允许改写创建模块的方法.想一下你前面写得方法`my_image_filter(inputs)`.有人在变量作用域内调用`reuse=True` 是希望所有内部变量都被重用.如果允许在方法体内强制执行`reuse=False`，将会打破内部结构并且用这种方法使得很难再共享参数.\r\n\r\n即使你不能直接设置 `reuse` 为 `False` ,但是你可以输入一个重用变量作用域,然后就释放掉,就成为非重用的变量.当打开一个变量作用域时,使用`reuse=True` 作为参数是可以的.但也要注意，同一个原因，`reuse` 参数是不可继承.所以当你打开一个重用变量作用域，那么所有的子作用域也将会被重用.\r\n\r\n\r\n```python\r\nwith tf.variable_scope(\"root\"):\r\n    # At start, the scope is not reusing.\r\n    assert tf.get_variable_scope().reuse == False\r\n    with tf.variable_scope(\"foo\"):\r\n        # Opened a sub-scope, still not reusing.\r\n        assert tf.get_variable_scope().reuse == False\r\n    with tf.variable_scope(\"foo\", reuse=True):\r\n        # Explicitly opened a reusing scope.\r\n        assert tf.get_variable_scope().reuse == True\r\n        with tf.variable_scope(\"bar\"):\r\n            # Now sub-scope inherits the reuse flag.\r\n            assert tf.get_variable_scope().reuse == True\r\n    # Exited the reusing scope, back to a non-reusing one.\r\n    assert tf.get_variable_scope().reuse == False\r\n```\r\n\r\n### 获取变量作用域<a class=\"md-anchor\" id=\"AUTOGENERATED-capturing-variable-scope\"></a>\r\n在上面的所有例子中，我们共享参数只因为他们的名字是一致的，那是因为我们开启一个变量作用域重用时刚好用了同一个字符串.在更复杂的情况，他可以通过变量作用域对象来使用，而不是通过依赖于右边的名字来使用.为此,变量作用域可以被获取并使用，而不是仅作为当开启一个新的变量作用域的名字.\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\") as foo_scope:\r\n    v = tf.get_variable(\"v\", [1])\r\nwith tf.variable_scope(foo_scope)\r\n    w = tf.get_variable(\"w\", [1])\r\nwith tf.variable_scope(foo_scope, reuse=True)\r\n    v1 = tf.get_variable(\"v\", [1])\r\n    w1 = tf.get_variable(\"w\", [1])\r\nassert v1 == v\r\nassert w1 == w\r\n```\r\n当开启一个变量作用域，使用一个预先已经存在的作用域时，我们会跳过当前变量作用域的前缀而直接成为一个完全不同的作用域.这就是我们做得完全独立的地方.\r\n\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\") as foo_scope:\r\n    assert foo_scope.name == \"foo\"\r\nwith tf.variable_scope(\"bar\")\r\n    with tf.variable_scope(\"baz\") as other_scope:\r\n        assert other_scope.name == \"bar/baz\"\r\n        with tf.variable_scope(foo_scope) as foo_scope2:\r\n            assert foo_scope2.name == \"foo\"  # Not changed.\r\n```\r\n\r\n### 变量作用域中的初始化器<a class=\"md-anchor\" id=\"AUTOGENERATED-initializers-in-variable-scope\"></a>\r\n使用`tf.get_variable()`允许你重写方法来创建或者重用变量,并且可以被外部透明调用.但是如果我们想改变创建变量的初始化器那要怎么做呢？是否我们需要为所有的创建变量方法传递一个额外的参数呢？那在大多数情况下，当我们想在一个地方并且为所有的方法的所有的变量设置一个默认初始化器，那又改怎么做呢？为了解决这些问题，变量作用域可以携带一个默认的初始化器.他可以被子作用域继承并传递给`tf.get_variable()` 调用.但是如果其他初始化器被明确地指定,那么他将会被重写.\r\n\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\", initializer=tf.constant_initializer(0.4)):\r\n    v = tf.get_variable(\"v\", [1])\r\n    assert v.eval() == 0.4  # Default initializer as set above.\r\n    w = tf.get_variable(\"w\", [1], initializer=tf.constant_initializer(0.3)):\r\n    assert w.eval() == 0.3  # Specific initializer overrides the default.\r\n    with tf.variable_scope(\"bar\"):\r\n        v = tf.get_variable(\"v\", [1])\r\n        assert v.eval() == 0.4  # Inherited default initializer.\r\n    with tf.variable_scope(\"baz\", initializer=tf.constant_initializer(0.2)):\r\n        v = tf.get_variable(\"v\", [1])\r\n        assert v.eval() == 0.2  # Changed default initializer.\r\n```\r\n\r\n### 在`tf.variable_scope()`中ops的名称<a class=\"md-anchor\" id=\"AUTOGENERATED-names-of-ops-in--tf.variable_scope---\"></a>\r\n我们讨论 `tf.variable_scope` 怎么处理变量的名字.但是又是如何在作用域中影响到\r\n其他ops的名字的呢？ops在一个变量作用域的内部创建，那么他应该是共享他的名字，这是很自然的想法.出于这样的原因，当我们用`with tf.variable_scope(\"name\")`时，这就间接地开启了一个`tf.name_scope(\"name\")`.比如：\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    x = 1.0 + tf.get_variable(\"v\", [1])\r\nassert x.op.name == \"foo/add\"\r\n```\r\n名称作用域可以被开启并添加到一个变量作用域中,然后他们只会影响到ops的名称,而不会影响到变量.\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    with tf.name_scope(\"bar\"):\r\n        v = tf.get_variable(\"v\", [1])\r\n        x = 1.0 + v\r\nassert v.name == \"foo/v:0\"\r\nassert x.op.name == \"foo/bar/add\"\r\n```\r\n当用一个引用对象而不是一个字符串去开启一个变量作用域时，我们就不会为ops改变当前的名称作用域.\r\n\r\n\r\n\r\n## 使用实例 <a class=\"md-anchor\" id=\"AUTOGENERATED-examples-of-use\"></a>\r\n这里有一些指向怎么使用变量作用域的文件.特别是，他被大量用于\r\n[时间递归神经网络](https://zh.wikipedia.org/wiki/%E9%80%92%E5%BD%92%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C)和`sequence-to-sequence`模型,\r\n\r\n\r\nFile | What's in it?\r\n--- | ---\r\n`models/image/cifar10.py` |图像中检测对象的模型.\r\n`models/rnn/rnn_cell.py` |时间递归神经网络的元方法集.\r\n`models/rnn/seq2seq.py` |为创建`sequence-to-sequence`模型的方法集.\r\n原文：[Sharing Variables](http://www.tensorflow.org/how_tos/variable_scope/index.md) 翻译：[nb312](https://github.com/nb312)\r\n"
  },
  {
    "path": "SOURCE/how_tos/variable_scope.md",
    "content": "# 共享变量<a class=\"md-anchor\" id=\"AUTOGENERATED-sharing-variables\"></a>\r\n\r\n你可以在[怎么使用变量](tensorflow-zh/SOURCE/how_tos/variables/index.md)中所描述的方式来创建，初始化，保存及加载单一的变量.但是当创建复杂的模块时，通常你需要共享大量变量集并且如果你还想在同一个地方初始化这所有的变量,我们又该怎么做呢.本教程就是演示如何使用`tf.variable_scope()` 和`tf.get_variable()`两个方法来实现这一点.\r\n\r\n## 问题<a class=\"md-anchor\" id=\"AUTOGENERATED-the-problem\"></a>\r\n\r\n假设你为图片过滤器创建了一个简单的模块，和我们的[卷积神经网络教程](tensorflow-zh/SOURCE/tutorials/deep_cnn/index.md)模块相似,但是这里包括两个卷积（为了简化实例这里只有两个）.如果你仅使用`tf.Variable`变量,那么你的模块就如[怎么使用变量](tensorflow-zh/SOURCE/how_tos/variables/index.md)里面所解释的是一样的模块.\r\n\r\n```python\r\ndef my_image_filter(input_images):\r\n    conv1_weights = tf.Variable(tf.random_normal([5, 5, 32, 32]),\r\n        name=\"conv1_weights\")\r\n    conv1_biases = tf.Variable(tf.zeros([32]), name=\"conv1_biases\")\r\n    conv1 = tf.nn.conv2d(input_images, conv1_weights,\r\n        strides=[1, 1, 1, 1], padding='SAME')\r\n    relu1 = tf.nn.relu(conv1 + conv1_biases)\r\n\r\n    conv2_weights = tf.Variable(tf.random_normal([5, 5, 32, 32]),\r\n        name=\"conv2_weights\")\r\n    conv2_biases = tf.Variable(tf.zeros([32]), name=\"conv2_biases\")\r\n    conv2 = tf.nn.conv2d(relu1, conv2_weights,\r\n        strides=[1, 1, 1, 1], padding='SAME')\r\n    return tf.nn.relu(conv2 + conv2_biases)\r\n```\r\n\r\n你很容易想到，模块集很快就比一个模块变得更为复杂，仅在这里我们就有了四个不同的变量：`conv1_weights`,`conv1_biases`, `conv2_weights`, 和`conv2_biases`.\r\n当我们想重用这个模块时问题还在增多.假设你想把你的图片过滤器运用到两张不同的图片， `image1`和`image2`.你想通过拥有同一个参数的同一个过滤器来过滤两张图片，你可以调用`my_image_filter()`两次，但是这会产生两组变量.\r\n\r\n```python\r\n# First call creates one set of variables.\r\nresult1 = my_image_filter(image1)\r\n# Another set is created in the second call.\r\nresult2 = my_image_filter(image2)\r\n```\r\n\r\n通常共享变量的方法就是在单独的代码块中来创建他们并且通过使用他们的函数.如使用字典的例子：\r\n\r\n```python\r\nvariables_dict = {\r\n    \"conv1_weights\": tf.Variable(tf.random_normal([5, 5, 32, 32]),\r\n        name=\"conv1_weights\")\r\n    \"conv1_biases\": tf.Variable(tf.zeros([32]), name=\"conv1_biases\")\r\n    ... etc. ...\r\n}\r\n\r\ndef my_image_filter(input_images, variables_dict):\r\n    conv1 = tf.nn.conv2d(input_images, variables_dict[\"conv1_weights\"],\r\n        strides=[1, 1, 1, 1], padding='SAME')\r\n    relu1 = tf.nn.relu(conv1 + variables_dict[\"conv1_biases\"])\r\n\r\n    conv2 = tf.nn.conv2d(relu1, variables_dict[\"conv2_weights\"],\r\n        strides=[1, 1, 1, 1], padding='SAME')\r\n    return tf.nn.relu(conv2 + variables_dict[\"conv2_biases\"])\r\n\r\n# The 2 calls to my_image_filter() now use the same variables\r\nresult1 = my_image_filter(image1, variables_dict)\r\nresult2 = my_image_filter(image2, variables_dict)\r\n```\r\n\r\n虽然使用上面的方式创建变量是很方便的，但是在这个模块代码之外却破坏了其封装性：\r\n\r\n*  在构建试图的代码中标明变量的名字，类型，形状来创建.\r\n*  当代码改变了，调用的地方也许就会产生或多或少或不同类型的变量.\r\n\r\n解决此类问题的方法之一就是使用类来创建模块，在需要的地方使用类来小心地管理他们需要的变量.\r\n一个更高明的做法,不用调用类，而是利用TensorFlow 提供了*变量作用域* 机制，当构建一个视图时,很容易就可以共享命名过的变量.\r\n\r\n## 变量作用域实例<a class=\"md-anchor\" id=\"AUTOGENERATED-variable-scope-example\"></a>\r\n\r\n变量作用域机制在TensorFlow中主要由两部分组成：\r\n\r\n* `tf.get_variable(<name>, <shape>, <initializer>)`:\r\n通过所给的名字创建或是返回一个变量.\r\n* `tf.variable_scope(<scope_name>)`:\r\n通过 `tf.get_variable()`为变量名指定命名空间.\r\n\r\n方法 `tf.get_variable()` 用来获取或创建一个变量，而不是直接调用`tf.Variable`.它采用的不是像`tf.Variable这样直接获取值来初始化的方法.一个初始化就是一个方法，创建其形状并且为这个形状提供一个张量.这里有一些在TensorFlow中使用的初始化变量：\r\n\r\n* `tf.constant_initializer(value)` 初始化一切所提供的值,\r\n* `tf.random_uniform_initializer(a, b)`从a到b均匀初始化,\r\n* `tf.random_normal_initializer(mean, stddev)` 用所给平均值和标准差初始化均匀分布.\r\n\r\n为了了解`tf.get_variable()`怎么解决前面所讨论的问题,让我们在单独的方法里面创建一个卷积来重构一下代码，命名为`conv_relu`：\r\n\r\n\r\n```python\r\ndef conv_relu(input, kernel_shape, bias_shape):\r\n    # Create variable named \"weights\".\r\n    weights = tf.get_variable(\"weights\", kernel_shape,\r\n        initializer=tf.random_normal_initializer())\r\n    # Create variable named \"biases\".\r\n    biases = tf.get_variable(\"biases\", bias_shape,\r\n        initializer=tf.constant_intializer(0.0))\r\n    conv = tf.nn.conv2d(input, weights,\r\n        strides=[1, 1, 1, 1], padding='SAME')\r\n    return tf.nn.relu(conv + biases)\r\n```\r\n\r\n这个方法中用了`\"weights\"` 和`\"biases\"`两个简称.而我们更偏向于用`conv1` 和 `conv2`这两个变量的写法,但是不同的变量需要不同的名字.这就是`tf.variable_scope()` 变量起作用的地方.他为变量指定了相应的命名空间.\r\n\r\n```python\r\ndef my_image_filter(input_images):\r\n    with tf.variable_scope(\"conv1\"):\r\n        # Variables created here will be named \"conv1/weights\", \"conv1/biases\".\r\n        relu1 = conv_relu(input_images, [5, 5, 32, 32], [32])\r\n    with tf.variable_scope(\"conv2\"):\r\n        # Variables created here will be named \"conv2/weights\", \"conv2/biases\".\r\n        return conv_relu(relu1, [5, 5, 32, 32], [32])\r\n```\r\n\r\n现在，让我们看看当我们调用 `my_image_filter()` 两次时究竟会发生了什么.\r\n\r\n\r\n```\r\nresult1 = my_image_filter(image1)\r\nresult2 = my_image_filter(image2)\r\n# Raises ValueError(... conv1/weights already exists ...)\r\n```\r\n就像你看见的一样，`tf.get_variable()`会检测已经存在的变量是否已经共享.如果你想共享他们，你需要像下面使用的一样，通过`reuse_variables()`这个方法来指定.\r\n\r\n```\r\nwith tf.variable_scope(\"image_filters\") as scope:\r\n    result1 = my_image_filter(image1)\r\n    scope.reuse_variables()\r\n    result2 = my_image_filter(image2)\r\n```\r\n\r\n用这种方式来共享变量是非常好的，轻量级而且安全.\r\n\r\n## 变量作用域是怎么工作的？<a class=\"md-anchor\" id=\"AUTOGENERATED-how-does-variable-scope-work-\"></a>\r\n\r\n### 理解 `tf.get_variable()` <a class=\"md-anchor\" id=\"AUTOGENERATED-understanding--tf.get_variable---\"></a>\r\n\r\n为了理解变量作用域，首先完全理解`tf.get_variable()`是怎么工作的是很有必要的.\r\n通常我们就是这样调用`tf.get_variable` 的.\r\n\r\n```python\r\nv = tf.get_variable(name, shape, dtype, initializer)\r\n```\r\n\r\n此调用做了有关作用域的两件事中的其中之一，方法调入.总的有两种情况.\r\n\r\n* 情况1:当`tf.get_variable_scope().reuse == False`时，作用域就是为创建新变量所设置的.\r\n\r\n这种情况下，`v`将通过`tf.Variable`所提供的形状和数据类型来重新创建.创建变量的全称将会由当前变量作用域名+所提供的`名字`所组成,并且还会检查来确保没有任何变量使用这个全称.如果这个全称已经有一个变量使用了，那么方法将会抛出`ValueError`错误.如果一个变量被创建,他将会用`initializer(shape)`进行初始化.比如：\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    v = tf.get_variable(\"v\", [1])\r\nassert v.name == \"foo/v:0\"\r\n```\r\n\r\n* 情况1：当`tf.get_variable_scope().reuse == True`时，作用域是为重用变量所设置\r\n\r\n这种情况下，调用就会搜索一个已经存在的变量，他的全称和当前变量的作用域名+所提供的`名字`是否相等.如果不存在相应的变量，就会抛出`ValueError` 错误.如果变量找到了，就返回这个变量.如下：\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    v = tf.get_variable(\"v\", [1])\r\nwith tf.variable_scope(\"foo\", reuse=True):\r\n    v1 = tf.get_variable(\"v\", [1])\r\nassert v1 == v\r\n```\r\n\r\n### `tf.variable_scope()` 基础<a class=\"md-anchor\" id=\"AUTOGENERATED-basics-of--tf.variable_scope---\"></a>\r\n\r\n知道`tf.get_variable()`是怎么工作的,使得理解变量作用域变得很容易.变量作用域的主方法带有一个名称，它将会作为前缀用于变量名,并且带有一个重用标签来区分以上的两种情况.嵌套的作用域附加名字所用的规则和文件目录的规则很类似：\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    with tf.variable_scope(\"bar\"):\r\n        v = tf.get_variable(\"v\", [1])\r\nassert v.name == \"foo/bar/v:0\"\r\n```\r\n\r\n当前变量作用域可以用`tf.get_variable_scope()`进行检索并且`reuse` 标签可以通过调用`tf.get_variable_scope().reuse_variables()`设置为`True` .\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    v = tf.get_variable(\"v\", [1])\r\n    tf.get_variable_scope().reuse_variables()\r\n    v1 = tf.get_variable(\"v\", [1])\r\nassert v1 == v\r\n```\r\n\r\n注意你*不能*设置`reuse`标签为`False`.其中的原因就是允许改写创建模块的方法.想一下你前面写得方法`my_image_filter(inputs)`.有人在变量作用域内调用`reuse=True` 是希望所有内部变量都被重用.如果允许在方法体内强制执行`reuse=False`，将会打破内部结构并且用这种方法使得很难再共享参数.\r\n\r\n即使你不能直接设置 `reuse` 为 `False` ,但是你可以输入一个重用变量作用域,然后就释放掉,就成为非重用的变量.当打开一个变量作用域时,使用`reuse=True` 作为参数是可以的.但也要注意，同一个原因，`reuse` 参数是不可继承.所以当你打开一个重用变量作用域，那么所有的子作用域也将会被重用.\r\n\r\n```python\r\nwith tf.variable_scope(\"root\"):\r\n    # At start, the scope is not reusing.\r\n    assert tf.get_variable_scope().reuse == False\r\n    with tf.variable_scope(\"foo\"):\r\n        # Opened a sub-scope, still not reusing.\r\n        assert tf.get_variable_scope().reuse == False\r\n    with tf.variable_scope(\"foo\", reuse=True):\r\n        # Explicitly opened a reusing scope.\r\n        assert tf.get_variable_scope().reuse == True\r\n        with tf.variable_scope(\"bar\"):\r\n            # Now sub-scope inherits the reuse flag.\r\n            assert tf.get_variable_scope().reuse == True\r\n    # Exited the reusing scope, back to a non-reusing one.\r\n    assert tf.get_variable_scope().reuse == False\r\n```\r\n\r\n### 获取变量作用域<a class=\"md-anchor\" id=\"AUTOGENERATED-capturing-variable-scope\"></a>\r\n\r\n在上面的所有例子中，我们共享参数只因为他们的名字是一致的，那是因为我们开启一个变量作用域重用时刚好用了同一个字符串.在更复杂的情况，他可以通过变量作用域对象来使用，而不是通过依赖于右边的名字来使用.为此,变量作用域可以被获取并使用，而不是仅作为当开启一个新的变量作用域的名字.\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\") as foo_scope:\r\n    v = tf.get_variable(\"v\", [1])\r\nwith tf.variable_scope(foo_scope)\r\n    w = tf.get_variable(\"w\", [1])\r\nwith tf.variable_scope(foo_scope, reuse=True)\r\n    v1 = tf.get_variable(\"v\", [1])\r\n    w1 = tf.get_variable(\"w\", [1])\r\nassert v1 == v\r\nassert w1 == w\r\n```\r\n\r\n当开启一个变量作用域，使用一个预先已经存在的作用域时，我们会跳过当前变量作用域的前缀而直接成为一个完全不同的作用域.这就是我们做得完全独立的地方.\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\") as foo_scope:\r\n    assert foo_scope.name == \"foo\"\r\nwith tf.variable_scope(\"bar\")\r\n    with tf.variable_scope(\"baz\") as other_scope:\r\n        assert other_scope.name == \"bar/baz\"\r\n        with tf.variable_scope(foo_scope) as foo_scope2:\r\n            assert foo_scope2.name == \"foo\"  # Not changed.\r\n```\r\n\r\n### 变量作用域中的初始化器<a class=\"md-anchor\" id=\"AUTOGENERATED-initializers-in-variable-scope\"></a>\r\n\r\n使用`tf.get_variable()`允许你重写方法来创建或者重用变量,并且可以被外部透明调用.但是如果我们想改变创建变量的初始化器那要怎么做呢？是否我们需要为所有的创建变量方法传递一个额外的参数呢？那在大多数情况下，当我们想在一个地方并且为所有的方法的所有的变量设置一个默认初始化器，那又改怎么做呢？为了解决这些问题，变量作用域可以携带一个默认的初始化器.他可以被子作用域继承并传递给`tf.get_variable()` 调用.但是如果其他初始化器被明确地指定,那么他将会被重写.\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\", initializer=tf.constant_initializer(0.4)):\r\n    v = tf.get_variable(\"v\", [1])\r\n    assert v.eval() == 0.4  # Default initializer as set above.\r\n    w = tf.get_variable(\"w\", [1], initializer=tf.constant_initializer(0.3)):\r\n    assert w.eval() == 0.3  # Specific initializer overrides the default.\r\n    with tf.variable_scope(\"bar\"):\r\n        v = tf.get_variable(\"v\", [1])\r\n        assert v.eval() == 0.4  # Inherited default initializer.\r\n    with tf.variable_scope(\"baz\", initializer=tf.constant_initializer(0.2)):\r\n        v = tf.get_variable(\"v\", [1])\r\n        assert v.eval() == 0.2  # Changed default initializer.\r\n```\r\n\r\n### 在`tf.variable_scope()`中ops的名称<a class=\"md-anchor\" id=\"AUTOGENERATED-names-of-ops-in--tf.variable_scope---\"></a>\r\n\r\n我们讨论 `tf.variable_scope` 怎么处理变量的名字.但是又是如何在作用域中影响到\r\n其他ops的名字的呢？ops在一个变量作用域的内部创建，那么他应该是共享他的名字，这是很自然的想法.出于这样的原因，当我们用`with tf.variable_scope(\"name\")`时，这就间接地开启了一个`tf.name_scope(\"name\")`.比如：\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    x = 1.0 + tf.get_variable(\"v\", [1])\r\nassert x.op.name == \"foo/add\"\r\n```\r\n\r\n名称作用域可以被开启并添加到一个变量作用域中,然后他们只会影响到ops的名称,而不会影响到变量.\r\n\r\n```python\r\nwith tf.variable_scope(\"foo\"):\r\n    with tf.name_scope(\"bar\"):\r\n        v = tf.get_variable(\"v\", [1])\r\n        x = 1.0 + v\r\nassert v.name == \"foo/v:0\"\r\nassert x.op.name == \"foo/bar/add\"\r\n```\r\n\r\n当用一个引用对象而不是一个字符串去开启一个变量作用域时，我们就不会为ops改变当前的名称作用域.\r\n\r\n## 使用实例 <a class=\"md-anchor\" id=\"AUTOGENERATED-examples-of-use\"></a>\r\n\r\n这里有一些指向怎么使用变量作用域的文件.特别是，他被大量用于\r\n[时间递归神经网络](https://zh.wikipedia.org/wiki/%E9%80%92%E5%BD%92%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C)和`sequence-to-sequence`模型,\r\n\r\nFile | What's in it?\r\n--- | ---\r\n`models/image/cifar10.py` |图像中检测对象的模型.\r\n`models/rnn/rnn_cell.py` |时间递归神经网络的元方法集.\r\n`models/rnn/seq2seq.py` |为创建`sequence-to-sequence`模型的方法集.\r\n原文：[Sharing Variables](http://www.tensorflow.org/how_tos/variable_scope/index.md) 翻译：[nb312](https://github.com/nb312)校对：[Wiki](https://github.com/jikexueyuanwiki)\r\n"
  },
  {
    "path": "SOURCE/how_tos/variables/index.md",
    "content": "# Variables: Creation, Initialization, Saving, and Loading <a class=\"md-anchor\" id=\"AUTOGENERATED-variables--creation--initialization--saving--and-loading\"></a>\r\n\r\nWhen you train a model, you use [variables](../../api_docs/python/state_ops.md)\r\nto hold and update parameters.  Variables are in-memory buffers containing\r\ntensors.  They must be explicitly initialized and can be saved to disk during\r\nand after training. You can later restore saved values to exercise or analyse\r\nthe model.\r\n\r\nThis document references the following TensorFlow classes.  Follow the links to\r\ntheir reference manual for a complete description of their API:\r\n\r\n*  The [`tf.Variable`](../../api_docs/python/state_ops.md#Variable) class.\r\n*  The [`tf.train.Saver`](../../api_docs/python/state_ops.md#Saver) class.\r\n\r\n\r\n## Creation <a class=\"md-anchor\" id=\"AUTOGENERATED-creation\"></a>\r\n\r\nWhen you create a [Variable](../../api_docs/python/state_ops.md) you pass a\r\n`Tensor` as its initial value to the `Variable()` constructor.  TensorFlow\r\nprovides a collection of ops that produce tensors often used for initialization\r\nfrom [constants or random values](../../api_docs/python/constant_op.md).\r\n\r\nNote that all these ops require you to specify the shape of the tensors.  That\r\nshape automatically becomes the shape of the variable.  Variables generally\r\nhave a fixed shape, but TensorFlow provides advanced mechanisms to reshape\r\nvariables.\r\n\r\n```python\r\n# Create two variables.\r\nweights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),\r\n                      name=\"weights\")\r\nbiases = tf.Variable(tf.zeros([200]), name=\"biases\")\r\n```\r\n\r\nCalling `tf.Variable()` adds several ops to the graph:\r\n\r\n*  A `variable` op that holds the variable value.\r\n*  An initializer op that sets the variable to its initial value.  This is\r\n   actually a `tf.assign` op.\r\n*  The ops for the initial value, such as the `zeros` op for the `biases`\r\n   variable in the example are also added to the graph.\r\n\r\nThe value returned by `tf.Variable()` value is an instance of the Python class\r\n`tf.Variable`.\r\n\r\n## Initialization <a class=\"md-anchor\" id=\"AUTOGENERATED-initialization\"></a>\r\n\r\nVariable initializers must be run explicitly before other ops in your model can\r\nbe run.  The easiest way to do that is to add an op that runs all the variable\r\ninitializers, and run that op before using the model.\r\n\r\nYou can alternatively restore variable values from a checkpoint file, see\r\nbelow.\r\n\r\nUse `tf.initialize_all_variables()` to add an op to run variable initializers.\r\nOnly run that op after you have fully constructed your model and launched it in\r\na session.\r\n\r\n```python\r\n# Create two variables.\r\nweights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),\r\n                      name=\"weights\")\r\nbiases = tf.Variable(tf.zeros([200]), name=\"biases\")\r\n...\r\n# Add an op to initialize the variables.\r\ninit_op = tf.initialize_all_variables()\r\n\r\n# Later, when launching the model\r\nwith tf.Session() as sess:\r\n  # Run the init operation.\r\n  sess.run(init_op)\r\n  ...\r\n  # Use the model\r\n  ...\r\n```\r\n\r\n### Initialization from another Variable <a class=\"md-anchor\" id=\"AUTOGENERATED-initialization-from-another-variable\"></a>\r\n\r\nYou sometimes need to initialize a variable from the initial value of another\r\nvariable.  As the op added by `tf.initialize_all_variables()` initializes all\r\nvariables in parallel you have to be careful when this is needed.\r\n\r\nTo initialize a new variable from the value of another variable use the other\r\nvariable's `initialized_value()` property.  You can use the initialized value\r\ndirectly as the initial value for the new variable, or you can use it as any\r\nother tensor to compute a value for the new variable.\r\n\r\n\r\n```python\r\n# Create a variable with a random value.\r\nweights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),\r\n                      name=\"weights\")\r\n# Create another variable with the same value as 'weights'.\r\nw2 = tf.Variable(weights.initialized_value(), name=\"w2\")\r\n# Create another variable with twice the value of 'weights'\r\nw_twice = tf.Variable(weights.initialized_value() * 0.2, name=\"w_twice\")\r\n```\r\n\r\n### Custom Initialization <a class=\"md-anchor\" id=\"AUTOGENERATED-custom-initialization\"></a>\r\n\r\nThe convenience function `tf.initialize_all_variables()` adds an op to\r\ninitialize *all variables* in the model.  You can also pass it an explicit list\r\nof variables to initialize.  See the\r\n[Variables Documentation](../../api_docs/python/state_ops.md) for more options,\r\nincluding checking if variables are initialized.\r\n\r\n## Saving and Restoring <a class=\"md-anchor\" id=\"AUTOGENERATED-saving-and-restoring\"></a>\r\n\r\nThe easiest way to save and restore a model is to use a `tf.train.Saver` object.\r\nThe constructor adds `save` and `restore` ops to the graph for all, or a\r\nspecified list, of the variables in the graph.  The saver object provides\r\nmethods to run these ops, specifying paths for the checkpoint files to write to\r\nor read from.\r\n\r\n### Checkpoint Files <a class=\"md-anchor\" id=\"AUTOGENERATED-checkpoint-files\"></a>\r\n\r\nVariables are saved in binary files that, roughly, contain a map from variable\r\nnames to tensor values.\r\n\r\nWhen you create a `Saver` object, you can optionally choose names for the\r\nvariables in the checkpoint files.  By default, it uses the value of the\r\n[`Variable.name`](../../api_docs/python/state_ops.md#Variable.name) property for\r\neach variable.\r\n\r\n### Saving Variables <a class=\"md-anchor\" id=\"AUTOGENERATED-saving-variables\"></a>\r\n\r\nCreate a `Saver` with `tf.train.Saver()` to manage all variables in\r\nthe model.\r\n\r\n```python\r\n# Create some variables.\r\nv1 = tf.Variable(..., name=\"v1\")\r\nv2 = tf.Variable(..., name=\"v2\")\r\n...\r\n# Add an op to initialize the variables.\r\ninit_op = tf.initialize_all_variables()\r\n\r\n# Add ops to save and restore all the variables.\r\nsaver = tf.train.Saver()\r\n\r\n# Later, launch the model, initialize the variables, do some work, save the\r\n# variables to disk.\r\nwith tf.Session() as sess:\r\n  sess.run(init_op)\r\n  # Do some work with the model.\r\n  ..\r\n  # Save the variables to disk.\r\n  save_path = saver.save(sess, \"/tmp/model.ckpt\")\r\n  print \"Model saved in file: \", save_path\r\n```\r\n\r\n### Restoring Variables <a class=\"md-anchor\" id=\"AUTOGENERATED-restoring-variables\"></a>\r\n\r\nThe same `Saver` object is used to restore variables.  Note that when you\r\nrestore variables from a file you do not have to initialize them beforehand.\r\n\r\n```python\r\n# Create some variables.\r\nv1 = tf.Variable(..., name=\"v1\")\r\nv2 = tf.Variable(..., name=\"v2\")\r\n...\r\n# Add ops to save and restore all the variables.\r\nsaver = tf.train.Saver()\r\n\r\n# Later, launch the model, use the saver to restore variables from disk, and\r\n# do some work with the model.\r\nwith tf.Session() as sess:\r\n  # Restore variables from disk.\r\n  saver.restore(sess, \"/tmp/model.ckpt\")\r\n  print \"Model restored.\"\r\n  # Do some work with the model\r\n  ...\r\n```\r\n\r\n### Choosing which Variables to Save and Restore <a class=\"md-anchor\" id=\"AUTOGENERATED-choosing-which-variables-to-save-and-restore\"></a>\r\n\r\nIf you do not pass any argument to `tf.train.Saver()` the saver handles all\r\nvariables in the graph.  Each one of them is saved under the name that was\r\npassed when the variable was created.\r\n\r\nIt is sometimes useful to explicitly specify names for variables in the\r\ncheckpoint files.  For example, you may have trained a model with a variable\r\nnamed `\"weights\"` whose value you want to restore in a new variable named\r\n`\"params\"`.\r\n\r\nIt is also sometimes useful to only save or restore a subset of the variables\r\nused by a model.  For example, you may have trained a neural net with 5 layers,\r\nand you now want to train a new model with 6 layers, restoring the parameters\r\nfrom the 5 layers of the previously trained model into the first 5 layers of\r\nthe new model.\r\n\r\nYou can easily specify the names and variables to save by passing to the\r\n`tf.train.Saver()` constructor a Python dictionary: keys are the\r\nnames to use, values are the variables to manage.\r\n\r\nNotes:\r\n\r\n*  You can create as many saver objects as you want if you need to save and\r\n   restore different subsets of the model variables.  The same variable can be\r\n   listed in multiple saver objects, its value is only changed when the saver\r\n   `restore()` method is run.\r\n\r\n*  If you only restore a subset of the model variables at the start\r\n   of a session, you have to run an initialize op for the other variables.  See\r\n   [`tf.initialize_variables()`](../../api_docs/python/state_ops.md#initialize_variables)\r\n   for more information.\r\n\r\n```python\r\n# Create some variables.\r\nv1 = tf.Variable(..., name=\"v1\")\r\nv2 = tf.Variable(..., name=\"v2\")\r\n...\r\n# Add ops to save and restore only 'v2' using the name \"my_v2\"\r\nsaver = tf.train.Saver({\"my_v2\": v2})\r\n# Use the saver object normally after that.\r\n...\r\n```\r\n"
  },
  {
    "path": "SOURCE/how_tos/variables.md",
    "content": "# 变量:创建、初始化、保存和加载#\r\n\r\n当训练模型时，用[变量](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/api_docs/python/state_ops.md)来存储和更新参数。变量包含张量 (Tensor)存放于内存的缓存区。建模时它们需要被明确地初始化，模型训练后它们必须被存储到磁盘。这些变量的值可在之后模型训练和分析是被加载。\r\n\r\n本文档描述以下两个TensorFlow类。点击以下链接可查看完整的API文档：\r\n\r\n- [`tf.Variable`](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/api_docs/python/state_ops.md#Variable) 类\r\n- [`tf.train.Saver`](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/api_docs/python/state_ops.md#Saver) 类\r\n\r\n## 创建\r\n\r\n当创建一个[变量](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/api_docs/python/state_ops.md)时，你将一个`张量`作为初始值传入构造函数`Variable()`。TensorFlow提供了一系列操作符来初始化张量，初始值是[常量或是随机值](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/api_docs/python/constant_op.md)。\r\n \r\n注意，所有这些操作符都需要你指定张量的shape。那个形状自动成为变量的shape。变量的shape通常是固定的，但TensorFlow提供了高级的机制来重新调整其行列数。\r\n\r\n```python\r\n# Create two variables.\r\nweights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),\r\n                      name=\"weights\")\r\nbiases = tf.Variable(tf.zeros([200]), name=\"biases\")\r\n```\r\n\r\n调用`tf.Variable()`添加一些操作(Op, operation)到graph：\r\n\r\n- 一个`Variable`操作存放变量的值。\r\n- 一个初始化op将变量设置为初始值。这事实上是一个`tf.assign`操作.\r\n- 初始值的操作，例如示例中对`biases`变量的`zeros`操作也被加入了graph。\r\n\r\n`tf.Variable`的返回值是Python的`tf.Variable`类的一个实例。\r\n\r\n## 初始化\r\n\r\n变量的初始化必须在模型的其它操作运行之前先明确地完成。最简单的方法就是添加一个给所有变量初始化的操作，并在使用模型之前首先运行那个操作。\r\n\r\n你或者可以从检查点文件中重新获取变量值，详见下文。\r\n\r\n使用`tf.initialize_all_variables()`添加一个操作对变量做初始化。记得在完全构建好模型并加载之后再运行那个操作。\r\n\r\n\r\n```python\r\n# Create two variables.\r\nweights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),\r\n                      name=\"weights\")\r\nbiases = tf.Variable(tf.zeros([200]), name=\"biases\")\r\n...\r\n# Add an op to initialize the variables.\r\ninit_op = tf.initialize_all_variables()\r\n\r\n# Later, when launching the model\r\nwith tf.Session() as sess:\r\n  # Run the init operation.\r\n  sess.run(init_op)\r\n  ...\r\n  # Use the model\r\n  ...\r\n```    \r\n\r\n### 由另一个变量初始化\r\n\r\n你有时候会需要用另一个变量的初始化值给当前变量初始化。由于`tf.initialize_all_variables()`是并行地初始化所有变量，所以在有这种需求的情况下需要小心。\r\n\r\n用其它变量的值初始化一个新的变量时，使用其它变量的`initialized_value()`属性。你可以直接把已初始化的值作为新变量的初始值，或者把它当做tensor计算得到一个值赋予新变量。\r\n\r\n```python\r\n# Create a variable with a random value.\r\nweights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),\r\n                      name=\"weights\")\r\n# Create another variable with the same value as 'weights'.\r\nw2 = tf.Variable(weights.initialized_value(), name=\"w2\")\r\n# Create another variable with twice the value of 'weights'\r\nw_twice = tf.Variable(weights.initialized_value() * 0.2, name=\"w_twice\")\r\n```\r\n\r\n### 自定义初始化\r\n\r\n`tf.initialize_all_variables()`函数便捷地添加一个op来初始化模型的所有变量。你也可以给它传入一组变量进行初始化。详情请见[Variables Documentation](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/api_docs/python/state_ops.md)，包括检查变量是否被初始化。\r\n\r\n## 保存和加载\r\n\r\n最简单的保存和恢复模型的方法是使用`tf.train.Saver`对象。构造器给graph的所有变量，或是定义在列表里的变量，添加`save`和`restore`ops。saver对象提供了方法来运行这些ops，定义检查点文件的读写路径。\r\n\r\n### 检查点文件\r\n\r\n变量存储在二进制文件里，主要包含从变量名到tensor值的映射关系。\r\n\r\n当你创建一个`Saver`对象时，你可以选择性地为检查点文件中的变量挑选变量名。默认情况下，将每个变量[`Variable.name`](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/api_docs/python/state_ops.md#Variable.name)属性的值。\r\n\r\n### 保存变量\r\n\r\n用`tf.train.Saver()`创建一个`Saver`来管理模型中的所有变量。\r\n\r\n```python\r\n# Create some variables.\r\nv1 = tf.Variable(..., name=\"v1\")\r\nv2 = tf.Variable(..., name=\"v2\")\r\n...\r\n# Add an op to initialize the variables.\r\ninit_op = tf.initialize_all_variables()\r\n\r\n# Add ops to save and restore all the variables.\r\nsaver = tf.train.Saver()\r\n\r\n# Later, launch the model, initialize the variables, do some work, save the\r\n# variables to disk.\r\nwith tf.Session() as sess:\r\n  sess.run(init_op)\r\n  # Do some work with the model.\r\n  ..\r\n  # Save the variables to disk.\r\n  save_path = saver.save(sess, \"/tmp/model.ckpt\")\r\n  print \"Model saved in file: \", save_path\r\n```\r\n\t\r\n### 恢复变量\r\n\r\n用同一个`Saver`对象来恢复变量。注意，当你从文件中恢复变量时，不需要事先对它们做初始化。\r\n\r\n```python\r\n# Create some variables.\r\nv1 = tf.Variable(..., name=\"v1\")\r\nv2 = tf.Variable(..., name=\"v2\")\r\n...\r\n# Add ops to save and restore all the variables.\r\nsaver = tf.train.Saver()\r\n\r\n# Later, launch the model, use the saver to restore variables from disk, and\r\n# do some work with the model.\r\nwith tf.Session() as sess:\r\n  # Restore variables from disk.\r\n  saver.restore(sess, \"/tmp/model.ckpt\")\r\n  print \"Model restored.\"\r\n  # Do some work with the model\r\n  ...\r\n```\r\n\r\n### 选择存储和恢复哪些变量\r\n\r\n如果你不给`tf.train.Saver()`传入任何参数，那么saver将处理graph中的所有变量。其中每一个变量都以变量创建时传入的名称被保存。\r\n\r\n有时候在检查点文件中明确定义变量的名称很有用。举个例子，你也许已经训练得到了一个模型，其中有个变量命名为`\"weights\"`，你想把它的值恢复到一个新的变量`\"params\"`中。\r\n\r\n有时候仅保存和恢复模型的一部分变量很有用。再举个例子，你也许训练得到了一个5层神经网络，现在想训练一个6层的新模型，可以将之前5层模型的参数导入到新模型的前5层中。\r\n\r\n你可以通过给`tf.train.Saver()`构造函数传入Python字典，很容易地定义需要保持的变量及对应名称：键对应使用的名称，值对应被管理的变量。\r\n\r\n注意：\r\n\r\n- 如果需要保存和恢复模型变量的不同子集，可以创建任意多个saver对象。同一个变量可被列入多个saver对象中，只有当saver的`restore()`函数被运行时，它的值才会发生改变。\r\n- 如果你仅在session开始时恢复模型变量的一个子集，你需要对剩下的变量执行初始化op。详情请见[`tf.initialize_variables()`](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/api_docs/python/state_ops.md#initialize_variables)。\r\n\r\n\r\n```python\r\n# Create some variables.\r\nv1 = tf.Variable(..., name=\"v1\")\r\nv2 = tf.Variable(..., name=\"v2\")\r\n...\r\n# Add ops to save and restore only 'v2' using the name \"my_v2\"\r\nsaver = tf.train.Saver({\"my_v2\": v2})\r\n# Use the saver object normally after that.\r\n...\r\n```\r\n\r\n>原文链接: [http://tensorflow.org/how_tos/variables/index.html](http://tensorflow.org/how_tos/variables/index.html) \r\n>翻译：[赵屹华](https://github.com/zhyhooo) 校对：[Wiki](https://github.com/jikexueyuanwiki)"
  },
  {
    "path": "SOURCE/images/getting_started.dot",
    "content": "digraph Dependencies {\r\n  node [shape = oval];\r\n  \"predictions: MatMul()\" -> \"data: Concat()\"\r\n  \"data: Concat()\" -> data_left\r\n  \"data: Concat()\" -> data_right\r\n  \"predictions: MatMul()\" -> \"weight_matrix: Reshape()\"\r\n  \"weight_matrix: Reshape()\" -> \"new_weights: Add()\"\r\n  \"new_weights: Add()\" -> weights \r\n  \"new_weights: Add()\" -> deltas\r\n  \"update: Assign()\" -> weights\r\n  \"update: Assign()\" -> \"new_weights: Add()\"\r\n  \"InitializeAllVariables()\" -> weights\r\n  \"InitializeAllVariables()\" -> init_value\r\n}"
  },
  {
    "path": "SOURCE/index.md",
    "content": "# TensorFlow <a class=\"md-anchor\" id=\"AUTOGENERATED-tensorflow\"></a>\r\n\r\n<!-- Note: This file is ignored in building the external site tensorflow.org -->\r\n\r\n## Introduction <a class=\"md-anchor\" id=\"AUTOGENERATED-introduction\"></a>\r\n\r\nTensorFlow&#8482; is an open source software library for numerical computation\r\nusing data flow graphs.  Nodes in the graph represent mathematical operations,\r\nwhile the graph edges represent the multidimensional data arrays (tensors) that\r\nflow between them.  This flexible architecture allows you to deploy computation\r\nto one or more CPUs or GPUs in a desktop, server, or mobile device without\r\nrewriting code.  TensorFlow was originally developed by researchers and\r\nengineers working on the Google Brain team within Google's Machine Intelligence\r\nresearch organization for the purposes of conducting machine learning and deep\r\nneural networks research.  The system is general enough to be applicable in a\r\nwide variety of other domains as well.  The following documents show you how\r\nto set up and use the TensorFlow system.\r\n\r\n## Table of Contents <a class=\"md-anchor\" id=\"AUTOGENERATED-table-of-contents\"></a>\r\n<!--#include virtual=\"sitemap.md\" -->\r\n\r\n"
  },
  {
    "path": "SOURCE/personal.md",
    "content": "## TensorFlow 个人学习心得\r\n\r\n说明：本章所列链接为个人学习TensorFlow的心得，很多是博客地址，从实践的角度帮助大家更好的理解官方文档的内容\r\n\r\n示例：\r\n\r\n- [我的TensorFlow学习博客](http://wiki.jikexueyuan.com)"
  },
  {
    "path": "SOURCE/resource.md",
    "content": "## TensorFlow 相关资源\r\n\r\n- [Google官方Blog宣布TensorFlow开源](https://googleblog.blogspot.com/2015/11/tensorflow-smarter-machine-learning-for.html)\r\n- [TensorFlow WhitePaper(PDF下载)](http://download.tensorflow.org/paper/whitepaper2015.pdf)\r\n- [Jeff Dean 介绍 TensorFlow（视频）](https://www.youtube.com/watch?v=90-S1M7Ny_o&t=21m2s)\r\n- [TensorFlow 简化版接口 Scikit Flow](https://github.com/google/skflow)\r\n- [TensorFlow 使用样例](https://github.com/aymericdamien/TensorFlow-Examples)\r\n- [TensorFlow 与 mxnet, caffe 对比 @chenrudan](http://chenrudan.github.io/blog/2015/11/18/comparethreeopenlib.html)\r\n- [TensorFlow 与 torch7, caffe, mxnet 在内存使用量以及性能对比 @mli](https://github.com/dmlc/mxnet/blob/master/doc/overview_chn.md)"
  },
  {
    "path": "SOURCE/resources/bib.md",
    "content": "# BibTex 引用<a class=\"md-anchor\" id=\"AUTOGENERATED-bibtex-citation\"></a>\r\n\r\n如果你在研究中使用了 TensorFlow，并且希望引用 TensorFlow系统。我们建议你引用一下白皮书。\r\n\r\n```\r\n@misc{tensorflow2015-whitepaper,\r\ntitle={{TensorFlow}: Large-Scale Machine Learning on Heterogeneous Systems},\r\nurl={http://tensorflow.org/},\r\nnote={Software available from tensorflow.org},\r\nauthor={\r\n    Mart\\'{\\i}n~Abadi and\r\n    Ashish~Agarwal and\r\n    Paul~Barham and\r\n    Eugene~Brevdo and\r\n    Zhifeng~Chen and\r\n    Craig~Citro and\r\n    Greg~S.~Corrado and\r\n    Andy~Davis and\r\n    Jeffrey~Dean and\r\n    Matthieu~Devin and\r\n    Sanjay~Ghemawat and\r\n    Ian~Goodfellow and\r\n    Andrew~Harp and\r\n    Geoffrey~Irving and\r\n    Michael~Isard and\r\n    Yangqing Jia and\r\n    Rafal~Jozefowicz and\r\n    Lukasz~Kaiser and\r\n    Manjunath~Kudlur and\r\n    Josh~Levenberg and\r\n    Dan~Man\\'{e} and\r\n    Rajat~Monga and\r\n    Sherry~Moore and\r\n    Derek~Murray and\r\n    Chris~Olah and\r\n    Mike~Schuster and\r\n    Jonathon~Shlens and\r\n    Benoit~Steiner and\r\n    Ilya~Sutskever and\r\n    Kunal~Talwar and\r\n    Paul~Tucker and\r\n    Vincent~Vanhoucke and\r\n    Vijay~Vasudevan and\r\n    Fernanda~Vi\\'{e}gas and\r\n    Oriol~Vinyals and\r\n    Pete~Warden and\r\n    Martin~Wattenberg and\r\n    Martin~Wicke and\r\n    Yuan~Yu and\r\n    Xiaoqiang~Zheng},\r\n  year={2015},\r\n}\r\n```\r\n\r\n文本形式如下：\r\n\r\n```\r\nMartín Abadi, Ashish Agarwal, Paul Barham, Eugene Brevdo,\r\nZhifeng Chen, Craig Citro, Greg S. Corrado, Andy Davis,\r\nJeffrey Dean, Matthieu Devin, Sanjay Ghemawat, Ian Goodfellow,\r\nAndrew Harp, Geoffrey Irving, Michael Isard, Rafal Jozefowicz, Yangqing Jia,\r\nLukasz Kaiser, Manjunath Kudlur, Josh Levenberg, Dan Mané, Mike Schuster,\r\nRajat Monga, Sherry Moore, Derek Murray, Chris Olah, Jonathon Shlens,\r\nBenoit Steiner, Ilya Sutskever, Kunal Talwar, Paul Tucker,\r\nVincent Vanhoucke, Vijay Vasudevan, Fernanda Viégas,\r\nOriol Vinyals, Pete Warden, Martin Wattenberg, Martin Wicke,\r\nYuan Yu, and Xiaoqiang Zheng.\r\nTensorFlow: Large-scale machine learning on heterogeneous systems,\r\n2015. Software available from tensorflow.org.\r\n```\r\n\r\n> 原文：http://tensorflow.org/resources/bib.md 翻译：[Jim-Zenn](https://github.com/Jim-Zenn) 校对：[Wiki](https://github.com/jikexueyuanwiki)\r\n"
  },
  {
    "path": "SOURCE/resources/dims_types.md",
    "content": "# 张量的阶、形状、数据类型<a class=\"md-anchor\" id=\"AUTOGENERATED-tensor-ranks--shapes--and-types\"></a>\r\n\r\nTensorFlow用张量这种数据结构来表示所有的数据.你可以把一个张量想象成一个n维的数组或列表.一个张量有一个静态类型和动态类型的维数.张量可以在图中的节点之间流通.\r\n\r\n## 阶 <a class=\"md-anchor\" id=\"AUTOGENERATED-rank\"></a>\r\n\r\n在TensorFlow系统中，张量的维数来被描述为*阶*.但是张量的阶和矩阵的阶并不是同一个概念.张量的阶（有时是关于如*顺序*或*度数*或者是*n维*）是张量维数的一个数量描述.比如，下面的张量（使用Python中list定义的）就是2阶.\r\n\r\n```\r\n    t = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\r\n```\r\n\r\n你可以认为一个二阶张量就是我们平常所说的矩阵，一阶张量可以认为是一个向量.对于一个二阶张量你可以用语句`t[i, j]`来访问其中的任何元素.而对于三阶张量你可以用't[i, j, k]'来访问其中的任何元素.\r\n\r\n\r\n\r\n阶 |数学实例| Python 例子\r\n--- | --- | ---\r\n0 | 纯量 (只有大小) | `s = 483`\r\n1 | 向量(大小和方向) | `v = [1.1, 2.2, 3.3]`\r\n2 | 矩阵(数据表) | `m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]`\r\n3 | 3阶张量 (数据立体) | `t = [[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]]`\r\nn | n阶 (自己想想看) | `....`\r\n\r\n## 形状 <a class=\"md-anchor\" id=\"AUTOGENERATED-shape\"></a>\r\n\r\nTensorFlow文档中使用了三种记号来方便地描述张量的维度：阶，形状以及维数.下表展示了他们之间的关系：\r\n\r\n阶 | 形状 | 维数 | 实例\r\n--- | --- | --- | ---\r\n0 | [  ] | 0-D | 一个 0维张量.  一个纯量.\r\n1 | [D0] | 1-D | 一个1维张量的形式[5].\r\n2 | [D0, D1] | 2-D |一个2维张量的形式[3, 4].\r\n3 | [D0, D1, D2] | 3-D | 一个3维张量的形式 [1, 4, 3].\r\nn | [D0, D1, ... Dn] | n-D | 一个n维张量的形式 [D0, D1, ... Dn].\r\n\r\n\r\n形状可以通过Python中的整数列表或元祖（int list或tuples）来表示，也或者用[`TensorShape` class](../api_docs/python/framework.md#TensorShape).\r\n\r\n## 数据类型<a class=\"md-anchor\" id=\"AUTOGENERATED-data-types\"></a>\r\n\r\n除了维度，Tensors有一个数据类型属性.你可以为一个张量指定下列数据类型中的任意一个类型：\r\n\r\n\r\n数据类型 | Python 类型| 描述\r\n--- | --- | ---\r\n`DT_FLOAT` | `tf.float32` | 32 位浮点数.\r\n`DT_DOUBLE` | `tf.float64` | 64 位浮点数.\r\n`DT_INT64` | `tf.int64` | 64 位有符号整型.\r\n`DT_INT32` | `tf.int32` | 32 位有符号整型.\r\n`DT_INT16` | `tf.int16` | 16 位有符号整型.\r\n`DT_INT8` | `tf.int8` |   8  位有符号整型.\r\n`DT_UINT8` | `tf.uint8` | 8  位无符号整型.\r\n`DT_STRING` | `tf.string` | 可变长度的字节数组.每一个张量元素都是一个字节数组.\r\n`DT_BOOL` | `tf.bool` |布尔型.\r\n`DT_COMPLEX64` | `tf.complex64` | 由两个32位浮点数组成的复数:实数和虚数.\r\n`DT_QINT32` | `tf.qint32` | 用于量化Ops的32位有符号整型.\r\n`DT_QINT8` | `tf.qint8` | 用于量化Ops的8位有符号整型.\r\n`DT_QUINT8` | `tf.quint8` |用于量化Ops的8位无符号整型.\r\n\r\n\r\n\r\n\r\n原文：[Tensor Ranks, Shapes, and Types](http://www.tensorflow.org/resources/dims_types.md) \r\n\r\n翻译：[nb312](https://github.com/nb312)\r\n\r\n校对：[lonlonago]( https://github.com/lonlonago)\r\n"
  },
  {
    "path": "SOURCE/resources/faq.md",
    "content": "# 常见问题 <a class=\"md-anchor\" id=\"AUTOGENERATED-frequently-asked-questions\"></a>\r\n\r\n此文档对关于TensorFlow的一些常见问题提供了答案，如果这里没有你问题的答案，你可能会在[社区资源](tensorflow-zh/SOURCE/resoreces/index.md)中找到它。\r\n\r\n<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->\r\n\r\n## 内容\r\n\r\n### [常见问题](#AUTOGENERATED-frequently-asked-questions)\r\n\r\n* [建立 TensorFlow graph](#AUTOGENERATED-building-a-tensorflow-graph)\r\n* [运行 TensorFlow 计算过程](#AUTOGENERATED-running-a-tensorflow-computation)\r\n* [Variables](#AUTOGENERATED-variables)\r\n* [Tensor shapes](#AUTOGENERATED-tensor-shapes)\r\n* [TensorBoard](#AUTOGENERATED-tensorboard)\r\n* [扩展 TensorFlow](#AUTOGENERATED-extending-tensorflow)\r\n* [其他问题](#AUTOGENERATED-miscellaneous)\r\n\r\n\r\n<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->\r\n\r\n## 建立 TensorFlow graph <a class=\"md-anchor\" id=\"AUTOGENERATED-building-a-tensorflow-graph\"></a>\r\n\r\n参看\r\n[建立 graph 的 API 文档](../api_docs/python/framework.md).\r\n\r\n#### 为什么`c = tf.matmul(a, b)` 不立即执行矩阵相乘？ <a class=\"md-anchor\" id=\"AUTOGENERATED-why-does--c---tf.matmul-a--b---not-execute-the-matrix-multiplication-immediately-\"></a>\r\n\r\n在 TensorFlow 的 Python API 中, `a`, `b`, and `c` 都是\r\n[`Tensor`](../api_docs/python/framework.md#Tensor) 对象. 一个 `Tensor` 对象是一个操作(operation)结果的字符别名,它实际上并不储存操作(operation)输出结果的值。\r\nTensorFlow 鼓励用户去建立复杂的表达式（如整个神经网络及其梯度）来形成 data flow graph 。\r\n然后你可以将整个 data flow graph 的计算过程交给一个 TensorFlow 的 [`Session`](../api_docs/python/client.md#Session),\r\n此 `Session` 可以运行整个计算过程，比起操作(operations)一条一条的执行效率高的多。\r\n\r\n#### 设备是如何命名的? <a class=\"md-anchor\" id=\"AUTOGENERATED-how-are-devices-named-\"></a>\r\n\r\n对CPU设备而言，支持的设备名是`\"/device:CPU:0\"` (或 `\"/cup:0\"`)，对第 *i* 个 GPU 设备是`\"/device:GPU:i\"` (或 `\"/gpu:i\"`)\r\n\r\n#### 如何在指定的设备上运行操作(operations)？ <a class=\"md-anchor\" id=\"AUTOGENERATED-how-do-i-place-operations-on-a-particular-device-\"></a>\r\n\r\n在 [`with tf.device(name):`](../api_docs/python/framework.md#device) context 中创建操作(operation)，这样可以在指定的设备上运行操作(operation)。\r\n关于 TensorFlow 怎样将操作(operations)分配给设备的细节，参看 [TensorFlow使用 GPU ](../how_tos/using_gpu/index.md); 使用多 GPU 的示范实例参看 [CIFAR-10 教程](../tutorials/deep_cnn/index.md)。\r\n\r\n#### 可用的 tensor 有哪些不同的类型？ <a class=\"md-anchor\" id=\"AUTOGENERATED-what-are-the-different-types-of-tensors-that-are-available-\"></a>\r\n\r\nTensorFlow 支持许多种不同的数据类型和 tensor shape ，更多细节请参看 [ranks, shapes, and type reference](../resources/dims_types.md)\r\n\r\n## 运行 TensorFlow 计算过程。 <a class=\"md-anchor\" id=\"AUTOGENERATED-running-a-tensorflow-computation\"></a>\r\n\r\n参看\r\n[运行 graph 的 API 文档](../api_docs/python/client.md).\r\n\r\n#### 请详细解释 feeding 和 placeholders？ <a class=\"md-anchor\" id=\"AUTOGENERATED-what-s-the-deal-with-feeding-and-placeholders-\"></a>\r\n\r\nFeeding 是 TensorFlow Session API 的一种机制，它允许你在运行时用不同的值替换一个或多个 tensor 的值。\r\n[`Session.run()`](../api_docs/python/client.md#Session.run) 的参数 `feed_dict` 是一个字典，\r\n它将 [`Tensor`](../api_docs/python/framework.md) 对象映射为 numpy 的数组（和一些其他类型）。\r\n在执行 step 时，这些数组就是 tensor 的值。\r\n\r\n你常会碰到某些 tensor 总是有值的，比如 inputs。 [`tf.placeholder()`](../api_docs/python/io_ops.md#placeholder) 操作(operation)允许你定义一种必须提供值的 tensor ，你也可以随意限定它们的 shape。关于如何使用 placelolders 和 feeding 为神经网络提供训练数据的例子，请参看[初学者的 MNIST 教程](../tutorials/mnist/beginners/index.md)\r\n\r\n#### `Session.run()` 和 `Tensor.eval()` 有什么区别？ <a class=\"md-anchor\" id=\"AUTOGENERATED-what-is-the-difference-between--session.run----and--tensor.eval----\"></a>\r\n\r\n如果 `t` 是一个 [`Tensor`](../api_docs/python/framework.md#Tensor) 对象， [`t.eval()`](../api_docs/python/framework.md#Tensor.eval) 就是 [`sess.run(t)`](../api_docs/python/client.md#Session.run) （`sess` 是当前[默认 session](../api_docs/python/client.md#get_default_session)）的简写。\r\n以下两段小程序是等效的：\r\n\r\n```python\r\n# 使用 `Session.run()`.\r\nsess = tf.Session()\r\nc = tf.constant(5.0)\r\nprint sess.run(c)\r\n\r\n# 使用 `Tensor.eval()`.\r\nc = tf.constant(5.0)\r\nwith tf.Session():\r\n  print c.eval()\r\n```\r\n\r\n在第二个例子中， session 的作用就象 [context manager](https://docs.python.org/2.7/reference/compound_stmts.html#with) ， context manager 在 `with` 块的生存期，将 session 作为默认的 session。对简单应用的情形（如单元测试），context manager 的方法可以得到更简洁的代码； 如果你的代码要处理多个 graph 和 session ，更直白的方式可能是显式调用 `Session.run()`。\r\n\r\n\r\n####  Sessions 有生存期吗？ 调用时产生的 tensors 呢？<a class=\"md-anchor\" id=\"AUTOGENERATED-do-sessions-have-a-lifetime--what-about-intermediate-tensors-\"></a>\r\n\r\n Session 能够占有资源，例如 [variables](../api_docs/python/state_ops.md#Variable)，[queues](../api_docs/python/io_ops.md#QueueBase), 和\r\n[readers](../api_docs/python/io_ops.md#ReaderBase); 这些资源会使用相当大量的内存。 当调用[`Session.close()`](../api_docs/python/client.md#Session.close) 关闭 session 后，这些资源（和相关的内存）就被释放了。\r\n\r\n作为调用 [`Session.run()`](../api_docs/python/client.md) 过程的一部分所创建的 tensors, 会在调用时或调用结束前释放。\r\n\r\n#### 我可以在多个计算机上运行分布式的训练吗？ <a class=\"md-anchor\" id=\"AUTOGENERATED-can-i-run-distributed-training-on-multiple-computers-\"></a>\r\n\r\n最初的 TensorFlow 开源版本支持单一计算机内的多设备（CPUs 和 GPUs）。 \r\n我们也正在致力于一个分布式的版本：如果你有兴趣，请告知我们，这样我们可以做相应的调整。\r\n\r\n#### 运行时会并行计算图的执行的各个部分(parts of graph execution)吗？ <a class=\"md-anchor\" id=\"AUTOGENERATED-does-the-runtime-parallelize-parts-of-graph-execution-\"></a>\r\n\r\nTensorFlow 运行时会在许多不同的层面(dimensions)并行图的执行(graph execution)：\r\n\r\n* 在一个CPU中用多核或是一个GPU中用多线程来并行许多单独的操作(operation)。\r\n* 在 TensorFlow  graph 中各个独立的节点可以在多个设备上并行，这样就提供了加速的可能。[CIFAR-10 用多 GPU 训练](../tutorials/deep_cnn/index.md).\r\n* Session API 允许并行执行多并发的 steps （如 调用 [Session.run()](../api_docs/python/client.md#Session.run)）。\r\n  如果单一的 step 不使用你计算机中所有的资源，这种方法可以使运行时有更高的吞吐量。\r\n\r\n#### TensorFlow 支持哪些客户端编程语言？ <a class=\"md-anchor\" id=\"AUTOGENERATED-which-client-languages-are-supported-in-tensorflow-\"></a>\r\n\r\nTensorFlow 被设计成为支持多种客户端语言。当前支持最好的客户端语言是 [Python](../api_docs/python/index.md)。 [C++ 客户端 API](../api_docs/cc/index.md) 提供了启动 graph 和运行 steps 的接口； 我们还有一个 [用 C++ 建立 graph 的 API](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/cc/tutorials/example_trainer.cc)，此 API 是实验性的。\r\n\r\n从社区的利益出发，我们想要支持更多的客户端语言。 TensorFlow 有一个 [基于 C 的客户端 API](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/public/tensor_c_api.h)，它使得用许多不同的语言创建客户端变得很容易。我们请大家在新语言绑定上做出努力。\r\n\r\n#### TensorFlow 会利用我计算机上所有可用的设备（GPUs 和 CPUs）吗？ <a class=\"md-anchor\" id=\"AUTOGENERATED-does-tensorflow-make-use-of-all-the-devices--gpus-and-cpus--available-on-my-machine-\"></a>\r\n\r\nTensorFlow 支持多 GPU 和 CPU。 有关 TensorFlow 如何将操作(operations)分配到设备的细节请参看 [TensorFlow 如何使用 GPUs](../how_tos/using_gpu/index.md) 的文档，有关使用多 GPU 的示范实例请参看 [CIFAR-10 教程](../tutorials/deep_cnn/index.md)\r\n\r\n请注意， TensorFlow 只使用计算能力(compute capability)大于 3.5 的 GPU 设备。\r\n\r\n#### 当使用一个 reader 或 一个 queue 时，为什么 `Session.run()` 会挂起？ <a class=\"md-anchor\" id=\"AUTOGENERATED-why-does--session.run----hang-when-using-a-reader-or-a-queue-\"></a>\r\n\r\n[reader](../api_docs/python/io_ops.md#ReaderBase) 类和 [queue](../api_docs/python/io_ops.md#QueueBase) 类提供特殊的操作(operations)，这些操作(operations)在有可用的输入(对有界队列则是空闲空间)前会 *阻塞* 。使用这些操作(operations)你可以创建复杂的[输入流水线(input pipelines)](../how_tos/reading_data/index.md) ,不过，这会使 TensorFlow 的计算过程更复杂。有关如何使用这些操作(operations)的更多信息请参看 how-to 文档中的[使用 `QueueRunner` 对象来控制 queues 和 readers](../how_tos/reading_data/index.md#QueueRunners)。\r\n\r\n## Variables <a class=\"md-anchor\" id=\"AUTOGENERATED-variables\"></a>\r\n\r\n参看 [Variables](../how_tos/variables/index.md)\r\n和 [变量作用域](../how_tos/variable_scope/index.md) 的 how-to 文档，还有[关于变量的 API 文档](../api_docs/python/state_ops.md)\r\n\r\n#### 变量的生存期是？ <a class=\"md-anchor\" id=\"AUTOGENERATED-what-is-the-lifetime-of-a-variable-\"></a>\r\n\r\n在某一 session 中，当你一开始运行 [`tf.Variable.initializer`](../api_docs/python/state_ops.md#Variable.initializer) 操作(operation)时，变量就会被创建。此 [`session 关闭后`](../api_docs/python/client.md#Session.close)它就被摧毁(destroyed)了。\r\n\r\n#### 并发读取或存入变量时会是什么情况？ <a class=\"md-anchor\" id=\"AUTOGENERATED-how-do-variables-behave-when-they-are-concurrently-accessed-\"></a>\r\n\r\n变量可以进行并发的读和写操作(operation)。由于变量是并发(concurrently)更新的， 所以从一个变量中读出的值可能会改变。在不互斥的条件下，对一个变量的并发的许多赋值操作(operation)是默认允许运行的。在对一个变量赋值时，如果想要加锁，可以将 `use_locking=True` 传递给 [`Variable.assign()`](../api_docs/python/state_ops.md#Variable.assign)。\r\n\r\n## Tensor shapes <a class=\"md-anchor\" id=\"AUTOGENERATED-tensor-shapes\"></a>\r\n\r\n参看\r\n[`TensorShape` API 文档](../api_docs/python/framework.md#TensorShape).\r\n\r\n#### 在 Python 中我怎么判断一个 tensor 的 shape ？ <a class=\"md-anchor\" id=\"AUTOGENERATED-how-can-i-determine-the-shape-of-a-tensor-in-python-\"></a>\r\n\r\n在 TensorFlow 中，一个 tensor 具备静态和动态两种 shape 。静态的 shape 可以用 [`tf.Tensor.get_shape()`](../api_docs/python/framework.md#Tensor.get_shape) 方法读出：这种 shape 是由此 tensor 在创建时使用的操作(operations)推导得出的，可能是 [partially complete](../api_docs/python/framework.md#TensorShape) 的。如果静态 shape 没有完整定义(not fully defined)的话，则一个 tensor 的动态 shape 可通过求 [`tf.shape(t)`](../api_docs/python/array_ops.md#shape) 的值得到。\r\n\r\n#### `x.set_shape()` 和 `x = tf.reshape(x)` 有什么区别？ <a class=\"md-anchor\" id=\"AUTOGENERATED-what-is-the-difference-between--x.set_shape----and--x---tf.reshape-x---\"></a>\r\n\r\n[`tf.Tensor.set_shape()`](../api_docs/python/framework.md) 方法(method)会更新(updates)一个 `Tensor` 对象的静态 shape ，当静态 shape 信息不能够直接推导得出的时候，此方法常用来提供额外的 shape 信息。它不改变此 tensor 动态 shape 的信息。\r\n\r\n[`tf.reshape()`](../api_docs/python/array_ops.md#reshape) 操作(operation)会以不同的动态 shape 创建一个新的 tensor。\r\n\r\n#### 我怎么创建这样一个 graph ，它在批次大小可变(variable batch sizes)的情形下也可以正常运作？ <a class=\"md-anchor\" id=\"AUTOGENERATED-how-do-i-build-a-graph-that-works-with-variable-batch-sizes-\"></a>\r\n\r\n如果能够创建一个 graph ，在批次大小可变(variable batch sizes)的情形下也可以正常运作将会是十分有用的，例如可以使用相同的代码完成（小）批量训练((mini-)batch training)和单例推导(single-instance inference)。这样生成的 graph 可以[保存起来当作协议缓存(protocol buffer)](../api_docs/python/framework.md#Graph.as_graph_def)，也可以[导入至其他的程序](../api_docs/python/framework.md#import_graph_def)。\r\n\r\n创建一个可变大小的 graph 时，要记住最重要的事情是不要将批次大小(batch size)编码成为 Python 常数，而是用一个字符性(symbolic)的 `Tensor` 来表示。下面的提示可能会有用：\r\n\r\n* 用 [`batch_size = tf.shape(input)[0]`](../api_docs/python/array_ops.md#shape) 从一个叫 `input` 的 `Tensor` 提取批次的维度(batch dimention)，再将其存入一个名为 `batch_size` 的 `Tensor` 。\r\n\r\n* 用 [`tf.reduce_mean()`](../api_docs/python/math_ops.md#reduce_mean) 而不是 `tf.reduce_sum(...) / batch_size`。\r\n\r\n* 如果你使用 [placeholders for feeding input](../how_tos/reading_data/index.md#Feeding)，你就可以用 [`tf.placeholder(..., shape=[None, ...])`](../api_docs/python/io_ops.md#placeholder) 通过创建 placeholder 来具体指定一个可变的批次维度(variable batch dimention)。shape 的 `None` 元素与可变大小的维度(a variable-sized dimension)相对应。\r\n\r\n## TensorBoard <a class=\"md-anchor\" id=\"AUTOGENERATED-tensorboard\"></a>\r\n\r\n#### 我怎样视觉化一个 TensorFlow graph ？ <a class=\"md-anchor\" id=\"AUTOGENERATED-how-can-i-visualize-a-tensorflow-graph-\"></a>\r\n\r\n参看[ graph 的视觉化教程](../how_tos/graph_viz/index.md).\r\n\r\n#### 向 TensorBoard 发送数据的最简单的方法是什么？ <a class=\"md-anchor\" id=\"AUTOGENERATED-what-is-the-simplest-way-to-send-data-to-tensorboard-\"></a>\r\n\r\n给你的 TensorFlow  graph 增加 summary 操作(ops)，接着用 [`SummaryWriter`](../api_docs/python/train.md#SummaryWriter) 将这些 summaries 写入一个 log directory。然后用以下命令启动 TensorBoard 。\r\n\r\n    `python tensorflow/tensorboard/tensorboard.py --logdir=path/to/log-directory`\r\n\r\n更多细节请参看 [Summaries 和 TensorBoard 教程](../how_tos/summaries_and_tensorboard/index.md)。\r\n\r\n## 扩展 TensorFlow <a class=\"md-anchor\" id=\"AUTOGENERATED-extending-tensorflow\"></a>\r\n\r\n参看有关[向 TensorFlow 添加新操作(oprations)](../how_tos/adding_an_op/index.md) 的 how-to 文档。\r\n\r\n#### 我的数据是自定义格式，要怎样用 TensorFlow 来读取它？ <a class=\"md-anchor\" id=\"AUTOGENERATED-my-data-is-in-a-custom-format.-how-do-i-read-it-using-tensorflow-\"></a>\r\n\r\n有两种主要的操作(operation)来处理自定义格式的数据。\r\n\r\n较简单的方法：用 Python 编写一段分词的代码(parsing code)，将数据转换成为 numpy array，然后用此数据把一个 [`tf.placeholder()`]\r\n(../api_docs/python/io_ops.md#placeholder) 传送给一个 tensor 。更多的细节参见 [使用 placeholders 进行输入](../how_tos/reading_data/index.md#Feeding) 的相关文档。这个方法比较容易实现，不过分词的部分会成为性能的瓶颈。\r\n\r\n更高效的方法是[添加一个用 C++ 编写的操作(op)](../how_tos/adding_an_op/index.md)，用这个操作(operation)来对你的数据格式进行分词(parse)。\r\n[新数据格式处理指南](../how_tos/new_data_formats/index.md)中有更多相关步骤的信息。\r\n\r\n#### 我如何定义操作(operation)使得它能够接受可变数量的输入？ <a class=\"md-anchor\" id=\"AUTOGENERATED-how-do-i-define-an-operation-that-takes-a-variable-number-of-inputs-\"></a>\r\n\r\nTensorFlow 的操作(operation)注册机制允许你定义几种输入：单独的 tensor，一列相同类型的 tensors (例如把一个可变长列表中的 tensors 相加)， 一列不同类型的 tensors (例如将一个 tuple 中的 tensors 入队(enqueue))。有关怎样定义这些不同的输入类型的更多细节，请参看[添加具有一列输入或输出的操作(op)](../how_tos/adding_an_op/index.md#list-input-output)的相关文档。\r\n\r\n## 其他问题 <a class=\"md-anchor\" id=\"AUTOGENERATED-miscellaneous\"></a>\r\n\r\n#### TensorFlow 能使用 Python 3 吗？ <a class=\"md-anchor\" id=\"AUTOGENERATED-does-tensorflow-work-with-python-3-\"></a>\r\n\r\n我们只用 Python 2.7 进行了测试。我们了解对 Python 3 的兼容性来说，还需要有一些修改，欢迎大家朝这个方向多努力。\r\n\r\n#### TensorFlow 的代码风格有什么规则？ <a class=\"md-anchor\" id=\"AUTOGENERATED-what-is-tensorflow-s-coding-style-convention-\"></a>\r\n\r\nTensorFlow Python API 遵循 [PEP8](https://www.python.org/dev/peps/pep-0008/) 惯例。\r\n<sup>*</sup> 特别的，我们使用 `CamelCase` 格式作为类名， `snake_case` 格式作为方程名， 方法名， 和属性名。我们也遵循\r\n[Google Python style guide](https://google.github.io/styleguide/pyguide.html)。\r\n\r\nTensorFlow C++ 代码遵循 [Google C++ style guide](http://google.github.io/styleguide/cppguide.html)。\r\n\r\n(<sup>*</sup> 有一条例外: 我们使用 2 空格缩进而不是 4 空格缩进)\r\n\r\n\r\n原文：[Frequently Asked Questions](http://tensorflow.org/resources/faq.md) 翻译：[Terence Cooper](https://github.com/TerenceCooper) 校对：[Wiki](https://github.com/jikexueyuanwiki)"
  },
  {
    "path": "SOURCE/resources/glossary.md",
    "content": "# 术语表\r\n\r\n### 广播操作(Broadcasting operation)\r\n\r\n一种用[numpy-style broadcasting](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)来保证tensor参数的形态兼容的操作。\r\n\r\n### Devices\r\n\r\n一块可以用来运算并且拥有自己的地址空间的硬件，比如GPU和CPU。\r\n\r\n### eval\r\n\r\nTensor 的一个方法，返回 Tensor 的值。触发任意一个图计算都需要计算出这个值。只能在一个已经启动的会话的图中才能调用该 Tensor 值。\r\n\r\n### Feed\r\n\r\nTensorFlow 的一个概念：把一个 Tensor 直接连接到一个会话图表中的任意节点。feed 不是在构建图(graph)的时候创建，而是在触发图的执行操作时去申请。一个 feed 临时替代一个带有 Tensor 值的节点。把feed数据作为run( )方法和eval( )方法的参数来初始化运算。方法运行结束后，替换的 feed 就会消失，而最初的节点定义仍然还在。可以通过tf.placeholder( )把特定的节点指定为 feed 节点来创建它们。详见[Basic Usage](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/get_started/basic_usage.md).\r\n\r\n### Fetch\r\n\r\nTensorFlow中的一个概念：为了取回运算操作的输出结果。取回的申请发生在触发执行图操作的时候，而不是发生在建立图的时候。如果要取回一个或多个节点（node）的 Tensor 值，可以通过在 Session 对象上调用run( )方法并将待取回节点（node）的列表作为参数来执行图表(graph)。详见[Basic Usage](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/get_started/basic_usage.md)。\r\n\r\n### Graph(图)\r\n\r\n把运算任务描述成一个直接的无环图形（DAG），图表中的节点（node）代表必须要实现的一些操作。图中的边代表数据或者可控的依赖。GratheDef 是系统中描述一个图表的协议(api)，它由一个 NodeDefs 集合组成。一个GraphDef可以转化成一个更容易操作的图表对象。\r\n\r\n### IndexedSlices（索引化切片）\r\n\r\n在 Python API 中，TensorFlow 仅仅在第一维上对 Tensor 有所体现。如果一个 Tensor 有k维，那么一个 IndexedSlices 实例在逻辑上代表一个沿着这个 Tensor 第一维的(k-1)维切片的集合。切片的索引被连续储存在一个单独的一维向量中，而对应的切片则被拼接成一个单独的k维 Tensor。如果 sparsity 不是受限于第一维空间，请用 \nSparseTensor。\r\n\r\n### Node（节点）\r\n\r\n图中的一个元素。\r\n把启动一个特定操作的方式称为特定运算图表的一个节点，包括任何用来配置这个操作的属性的值。对于那些多形态的操作，这些属性包括能完全决定这个节点（Node）签名的充分信息。详见graph.proto。\r\n\r\n### 操作（Op/operation）\r\n\r\n在 TensorFlow 的运行时中，它是一种类似 add 或 matmul 或 concat的运算。可以用[how to add an op](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/how_tos/adding_an_op/index.md)中的方法来向运行时添加新的操作。\r\n\r\n在 Python 的API中，它是图中的一个节点。在[tf.Operation](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/framework.md#Operation)类中列举出了这些操作。一个操作(Operation)的 type 属性决定这个节点（node）的操作类型，比如add和matmul。\r\n\r\n### Run\r\n\r\n在一个运行的图中执行某种操作的行为。要求图必须运行在会话中。\r\n\r\n在 Python 的 API 中，它是 Session 类的一个方法[tf.Session.run](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/client.md#Session)。可以通过 Tensors 来订阅或获取run( )操作。\r\n\r\n在C++的API中，它是[tensorflow::Session](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/client.md#Session)类\n的一个方法。\r\n\r\n### Session(会话)\r\n\r\n启动图的第一步是创建一个 Session 对象。Session 提供在图中执行操作的一些方法。\r\n\r\n在 Python API中，使用[tf.Session](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/client.md#Session)。\r\n\r\n在 C++ 的API中，[tensorflow::Session](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/cc/ClassSession.md)是用来创建一个图并运行操作的类：\r\n\r\n### Shape\r\n\r\nTensor 的维度和它们的大小。\r\n\r\n在一个已经启动的图中，它表示流动在节点（node）之间的 Tensor 的属性。一些操作对 shape 有比较强的要求，如果没有 Shape 属性则会报告错误。\r\n\r\n在 Python API中，用创建图的 API 来说明 Tensor 的 Shape 属性。Tensor 的Shape 属性要么只有部分已知，要么全部未知。详见[tf.TensroShape](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/framework.md#TensorShape)\r\n\r\n在C++中，Shape 类用来表示 Tensor 的维度。[tensorflow::TensorShape](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/cc/ClassTensorShape.md)。\r\n\r\n### SparseTensor\r\n\r\n在 Python API 中，它用来表示在 TensorFlow 中稀疏散落在任意地方的 Tensor 。SparseTensor 以字典-值格式来储存那些沿着索引的非空值。换言之，m个非空值，就包含一个长度为m的值向量和一个由m列索引(indices)组成的矩阵。为了提升效率，SparseTensor 需要将 indice（索引）按维度的增加来按序存储，比如行主序。如果稀疏值仅沿着第一维度，就用 IndexedSlices。\r\n\r\n### Tensor\r\n\r\nTensor是一种特定的多维数组。比如，一个浮点型的四维数组表示一小批由[batch,height，width，channel]组成的图片。\r\n\r\n在一个运行的图(graph)中，它是一种流动在节点（node）之间的数据。\r\n在 Python 中，Tensor 类表示添加到图的操作中的输入和输出，见[tf.Tensor](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/framework.md#Tensor)，这样的类不持有数据。\r\n\r\n在C++中，Tensor是方法[Session::Run( )](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/cc/ClassSession.md)的返回值，见[tensorflow::Tensor](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/cc/ClassTensor.md)，这样的 Tensor 持有数据。\n\r\n原文：[Glossary](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/resources/glossary.md) \n\n翻译：[leege100](https://github.com/leege100)\n\n校对：[lonlonago](https://github.com/lonlonago)"
  },
  {
    "path": "SOURCE/resources/overview.md",
    "content": "# 其他资源 <a class=\"md-anchor\" id=\"AUTOGENERATED-additional-resources\"></a>\r\n\r\n## TensorFlow 白皮书 <a class=\"md-anchor\" id=\"AUTOGENERATED-tensorflow-whitepaper\"></a>\r\n\r\n在这份白皮书里，你可以找到关于 TensorFlow 编程模型的更多详情和 TensorFlow 的实现原理。\r\n\r\n* [TensorFlow: Large-scale machine learning on heterogeneous systems](http://download.tensorflow.org/paper/whitepaper2015.pdf)\r\n\r\n### 引用 <a class=\"md-anchor\" id=\"AUTOGENERATED-citation\"></a>\r\n\r\n如果你在你的研究中使用了 TensorFlow，并且希望在引用中注记 TensorFlow，我们建议你引用上面这篇论文。你可以使用这个  [BibTeX 目录](../resources/bib.md)。  随着项目的继续发展，我们还会在这个建议引用列表添加新的论文。\r\n\r\n## 社区 <a class=\"md-anchor\" id=\"AUTOGENERATED-community\"></a>\r\n\r\n### 讨论 <a class=\"md-anchor\" id=\"AUTOGENERATED-discuss\"></a>\r\n\r\n* [GitHub ](https://github.com/tensorflow/tensorflow)\r\n* [Stack Overflow](https://stackoverflow.com/questions/tagged/tensorflow)\r\n* [TensorFlow 讨论邮件列表](https://groups.google.com/a/tensorflow.org/d/forum/discuss)\r\n\r\n### 报告 Issues <a class=\"md-anchor\" id=\"AUTOGENERATED-report-issues\"></a>\r\n\r\n* [TensorFlow issues](https://github.com/tensorflow/tensorflow/issues)\r\n\r\n### 开发 <a class=\"md-anchor\" id=\"AUTOGENERATED-development\"></a>\r\n\r\n* 如果你有兴趣为 TensorFlow 贡献代码请\r\n  [阅读这篇代码贡献指南](https://github.com/tensorflow/tensorflow/blob/master/CONTRIBUTING.md).\r\n\r\n> 原文：[http://tensorflow.org/resources/bib.md](http://tensorflow.org/resources/bib.md)\r\n> \r\n> 翻译：[Jim-Zenn](https://github.com/Jim-Zenn)\r\n> \r\n> 校对：[lonlonago]( https://github.com/lonlonago)\r\n\r\n<div class='sections-order' style=\"display: none;\">\r\n<!-- bib.md -->\r\n<!-- uses.md -->\r\n<!-- faq.md -->\r\n<!-- glossary.md -->\r\n<!-- dims_types.md -->\r\n</div>\r\n"
  },
  {
    "path": "SOURCE/resources/uses.md",
    "content": "# 应用实例 <a class=\"md-anchor\" id=\"AUTOGENERATED-example-uses\"></a>\r\n\r\n本页介绍了一些 TensorFlow 系统当前在实际中的应用。\r\n\r\n> 如果您在做研究、教育、或在某些产品中正在使用 TensorFlow，\r\n> 我们非常乐意在这里添加一些有关您的使用情况。\r\n> 请随时给我们发电子邮件简要说明您是如何使用TensorFlow的，\r\n> 或者给我们发 pull request来添加一个条目到本文件。\r\n\r\n下面列出了一些 TensorFlow 的用途。\r\n\r\n* **RankBrain**\r\n  * **组织**: Google\r\n  * **域名**: Information Retrieval\r\n  * **描述**: 对www.google.com搜索排名大规模部署的深层神经网络。\r\n  * **更多信息**: [\"Google Turning Over Its Lucrative Search to AI Machines\"](http://www.bloomberg.com/news/articles/2015-10-26/google-turning-its-lucrative-web-search-over-to-ai-machines)\r\n\r\n* **Inception Image Classification Model**\r\n  * **组织**: Google\r\n  * **描述**: 研究高精确的计算机视觉模型，赢得了2014年Imagenet图像分类的挑战 (ILSVRC 2014)\r\n  * **更多信息**: 关于 Baseline model 的描述 [Arxiv paper](http://arxiv.org/abs/1409.4842)\r\n\r\n* **SmartReply**\r\n  * **组织**: Google\r\n  * **描述**: 基于深度 LSTM 模型的自动生成电子邮件回复\r\n  * **更多信息**: [Google research blog post](http://googleresearch.blogspot.com/2015/11/computer-respond-to-this-email.html)\r\n\r\n* **Massively Multitask Networks for Drug Discovery**\r\n  * **组织**: Google and Stanford University\r\n  * **域名**: Drug discovery\r\n  * **描述**: 基于深度神经网络模型的药物探索\r\n  * **更多信息**: [Arxiv paper](http://arxiv.org/abs/1502.02072)\r\n\r\n* **On-Device Computer Vision for OCR**\r\n  * **组织**: Google\r\n  * **描述**: 用设备内置的计算机视觉模型来做‘光学字符识别’（OCR）以实现实时翻译。\r\n  * **更多信息**: [Google Research blog post](http://googleresearch.blogspot.com/2015/07/how-google-translate-squeezes-deep.html)\r\n\r\n原文:[http://tensorflow.org/resources/uses.md](http://tensorflow.org/resources/uses.md) \r\n\r\n翻 译:[andyiac](https://github.com/andyiac)\r\n\r\n校 对:[lonlonago](https://github.com/lonlonago)\r\n\r\n"
  },
  {
    "path": "SOURCE/tutorials/BUILD",
    "content": "# Description:\r\n# Top-level tutorials files\r\n\r\npackage(default_visibility = [\"//tensorflow:internal\"])\r\n\r\nlicenses([\"notice\"])  # Apache 2.0\r\n\r\nexports_files([\"LICENSE\"])\r\n\r\nfilegroup(\r\n    name = \"all_files\",\r\n    srcs = glob(\r\n        [\"**/*\"],\r\n        exclude = [\r\n            \"**/METADATA\",\r\n            \"**/OWNERS\",\r\n        ],\r\n    ),\r\n)\r\n"
  },
  {
    "path": "SOURCE/tutorials/__init__.py",
    "content": ""
  },
  {
    "path": "SOURCE/tutorials/deep_cnn/cifar_tensorboard.html",
    "content": "<html>\r\n\r\n<head>\r\n  <title>TensorBoard Demo</title>\r\n  <script src=\"/tensorboard/webcomponents-lite.min.js\"></script>\r\n  <link rel=\"import\" href=\"/tensorboard/tf-tensorboard-demo.html\">\r\n  <style>\r\n\r\n  html,body {\r\n    margin: 0;\r\n    padding: 0;\r\n    height: 100%;\r\n    font-family: \"RobotoDraft\",\"Roboto\",sans-serif;\r\n  }\r\n\r\n</style>\r\n</head>\r\n<body>\r\n  <tf-tensorboard-demo data-dir=\"/tensorboard/cifar\"></tf-tensorboard-demo>\r\n</body>\r\n</html>\r\n"
  },
  {
    "path": "SOURCE/tutorials/deep_cnn/index.md",
    "content": "# Convolutional Neural Networks <a class=\"md-anchor\" id=\"AUTOGENERATED-convolutional-neural-networks\"></a>\r\n\r\n> **NOTE:** This tutorial is intended for *advanced* users of TensorFlow\r\nand assumes expertise and experience in machine learning.\r\n\r\n## Overview <a class=\"md-anchor\" id=\"AUTOGENERATED-overview\"></a>\r\n\r\nCIFAR-10 classification is a common benchmark problem in machine learning.  The\r\nproblem is to classify RGB 32x32 pixel images across 10 categories:\r\n```airplane, automobile, bird, cat, deer, dog, frog, horse, ship, and truck.```\r\n\r\n![CIFAR-10 Samples](./cifar_samples.png \"CIFAR-10 Samples, from http://www.cs.toronto.edu/~kriz/cifar.html\")\r\n\r\nFor more details refer to the [CIFAR-10 page](http://www.cs.toronto.edu/~kriz/cifar.html)\r\nand a [Tech Report](http://www.cs.toronto.edu/~kriz/learning-features-2009-TR.pdf)\r\nby Alex Krizhevsky.\r\n\r\n### Goals <a class=\"md-anchor\" id=\"AUTOGENERATED-goals\"></a>\r\n\r\nThe goal of this tutorial is to build a relatively small convolutional neural\r\nnetwork (CNN) for recognizing images. In the process, this tutorial:\r\n\r\n1. Highlights a canonical organization for network architecture,\r\ntraining and evaluation.\r\n2. Provides a template for constructing larger and more sophisticated models.\r\n\r\nThe reason CIFAR-10 was selected was that it is complex enough to exercise\r\nmuch of TensorFlow's ability to scale to large models. At the same time,\r\nthe model is small enough to train fast, which is ideal for trying out\r\nnew ideas and experimenting with new techniques.\r\n\r\n### Highlights of the Tutorial <a class=\"md-anchor\" id=\"AUTOGENERATED-highlights-of-the-tutorial\"></a>\r\nThe CIFAR-10 tutorial demonstrates several important constructs for\r\ndesigning larger and more sophisticated models in TensorFlow:\r\n\r\n* Core mathematical components including [convolution](\r\n../../api_docs/python/nn.md#conv2d), [rectified linear activations](\r\n../../api_docs/python/nn.md#relu), [max pooling](\r\n../../api_docs/python/nn.md#max_pool) and [local response normalization](\r\n../../api_docs/python/nn.md#local_response_normalization).\r\n* [Visualization](../../how_tos/summaries_and_tensorboard/index.md)\r\nof network activities during training, including input images,\r\nlosses and distributions of activations and gradients.\r\n* Routines for calculating the\r\n[moving average](../../api_docs/python/train.md#ExponentialMovingAverage)\r\nof learned parameters and using these averages\r\nduring evaluation to boost predictive performance.\r\n* Implementation of a\r\n[learning rate schedule](../../api_docs/python/train.md#exponential_decay)\r\nthat systematically decrements over time.\r\n* Prefetching [queues](../../api_docs/python/io_ops.md#shuffle_batch)\r\nfor input\r\ndata to isolate the model from disk latency and expensive image pre-processing.\r\n\r\nWe also provide a multi-GPU version of the model which demonstrates:\r\n\r\n* Configuring a model to train across multiple GPU cards in parallel.\r\n* Sharing and updating variables among multiple GPUs.\r\n\r\nWe hope that this tutorial provides a launch point for building larger CNNs for\r\nvision tasks on TensorFlow.\r\n\r\n### Model Architecture <a class=\"md-anchor\" id=\"AUTOGENERATED-model-architecture\"></a>\r\n\r\nThe model in this CIFAR-10 tutorial is a multi-layer architecture consisting of\r\nalternating convolutions and nonlinearities. These layers are followed by fully\r\nconnected layers leading into a softmax classifier.  The model follows the\r\narchitecture described by\r\n[Alex Krizhevsky](https://code.google.com/p/cuda-convnet/), with a few\r\ndifferences in the top few layers.\r\n\r\nThis model achieves a peak performance of about 86% accuracy within a few hours\r\nof training time on a GPU. Please see [below](#evaluating-a-model) and the code\r\nfor details.  It consists of 1,068,298 learnable parameters and requires about\r\n19.5M multiply-add operations to compute inference on a single image.\r\n\r\n## Code Organization <a class=\"md-anchor\" id=\"AUTOGENERATED-code-organization\"></a>\r\n\r\nThe code for this tutorial resides in\r\n[`tensorflow/models/image/cifar10/`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/).\r\n\r\nFile | Purpose\r\n--- | ---\r\n[`cifar10_input.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_input.py) | Reads the native CIFAR-10 binary file format.\r\n[`cifar10.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10.py) | Builds the CIFAR-10 model.\r\n[`cifar10_train.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_train.py) | Trains a CIFAR-10 model on a CPU or GPU.\r\n[`cifar10_multi_gpu_train.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_multi_gpu_train.py) | Trains a CIFAR-10 model on multiple GPUs.\r\n[`cifar10_eval.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_eval.py) | Evaluates the predictive performance of a CIFAR-10 model.\r\n\r\n\r\n## CIFAR-10 Model <a class=\"md-anchor\" id=\"AUTOGENERATED-cifar-10-model\"></a>\r\n\r\nThe CIFAR-10 network is largely contained in\r\n[`cifar10.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10.py).\r\nThe complete training\r\ngraph contains roughly 765 operations. We find that we can make the code most\r\nreusable by constructing the graph with the following modules:\r\n\r\n1. [**Model inputs:**](#model-inputs) `inputs()` and `distorted_inputs()` add\r\noperations that read and preprocess CIFAR images for evaluation and training,\r\nrespectively.\r\n1. [**Model prediction:**](#model-prediction) `inference()`\r\nadds operations that perform inference, i.e. classification, on supplied images.\r\n1. [**Model training:**](#model-training) `loss()` and `train()`\r\nadd operations that compute the loss,\r\ngradients, variable updates and visualization summaries.\r\n\r\n### Model Inputs <a class=\"md-anchor\" id=\"model-inputs\"></a>\r\n\r\nThe input part of the model is built by the functions `inputs()` and\r\n`distorted_inputs()` which read images from the CIFAR-10 binary data files.\r\nThese files contain fixed byte length records, so we use\r\n[`tf.FixedLengthRecordReader`](../../api_docs/python/io_ops.md#FixedLengthRecordReader).\r\nSee [Reading Data](../../how_tos/reading_data/index.md#reading-from-files) to\r\nlearn more about how the `Reader` class works.\r\n\r\nThe images are processed as follows:\r\n\r\n*  They are cropped to 24 x 24 pixels, centrally for evaluation or\r\n   [randomly](../../api_docs/python/image.md#random_crop) for training.\r\n*  They are [approximately whitened](../../api_docs/python/image.md#per_image_whitening)\r\n   to make the model insensitive to dynamic range.\r\n\r\nFor training, we additionally apply a series of random distortions to\r\nartificially increase the data set size:\r\n\r\n* [Randomly flip](../../api_docs/python/image.md#random_flip_left_right) the image from left to right.\r\n* Randomly distort the [image brightness](../../api_docs/python/image.md#random_brightness).\r\n* Randomly distort the [image contrast](../../api_docs/python/image.md#tf_image_random_contrast).\r\n\r\nPlease see the [Images](../../api_docs/python/image.md) page for the list of\r\navailable distortions. We also attach an\r\n[`image_summary`](../../api_docs/python/train.md#image_summary) to the images\r\nso that we may visualize them in TensorBoard.  This is a good practice to verify\r\nthat inputs are built correctly.\r\n\r\n<div style=\"width:50%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n  <img style=\"width:70%\" src=\"./cifar_image_summary.png\">\r\n</div>\r\n\r\nReading images from disk and distorting them can use a non-trivial amount of\r\nprocessing time. To prevent these operations from slowing down training, we run\r\nthem inside 16 separate threads which continuously fill a TensorFlow\r\n[queue](../../api_docs/python/io_ops.md#shuffle_batch).\r\n\r\n### Model Prediction <a class=\"md-anchor\" id=\"model-prediction\"></a>\r\n\r\nThe prediction part of the model is constructed by the `inference()` function\r\nwhich adds operations to compute the *logits* of the predictions. That part of\r\nthe model is organized as follows:\r\n\r\nLayer Name | Description\r\n--- | ---\r\n`conv1` | [convolution](../../api_docs/python/nn.md#conv2d) and [rectified linear](../../api_docs/python/nn.md#relu) activation.\r\n`pool1` | [max pooling](../../api_docs/python/nn.md#max_pool).\r\n`norm1` | [local response normalization](../../api_docs/python/nn.md#local_response_normalization).\r\n`conv2` | [convolution](../../api_docs/python/nn.md#conv2d) and [rectified linear](../../api_docs/python/nn.md#relu) activation.\r\n`norm2` | [local response normalization](../../api_docs/python/nn.md#local_response_normalization).\r\n`pool2` | [max pooling](../../api_docs/python/nn.md#max_pool).\r\n`local3` | [fully connected layer with rectified linear activation](../../api_docs/python/nn.md).\r\n`local4` | [fully connected layer with rectified linear activation](../../api_docs/python/nn.md).\r\n`softmax_linear` | linear transformation to produce logits.\r\n\r\nHere is a graph generated from TensorBoard describing the inference operation:\r\n\r\n<div style=\"width:15%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n  <img style=\"width:100%\" src=\"./cifar_graph.png\">\r\n</div>\r\n\r\n> **EXERCISE**: The output of `inference` are un-normalized logits. Try editing\r\nthe network architecture to return normalized predictions using [`tf.softmax()`]\r\n(../../api_docs/python/nn.md#softmax).\r\n\r\nThe `inputs()` and `inference()` functions provide all the components\r\nnecessary to perform evaluation on a model. We now shift our focus towards\r\nbuilding operations for training a model.\r\n\r\n> **EXERCISE:** The model architecture in `inference()` differs slightly from\r\nthe CIFAR-10 model specified in\r\n[cuda-convnet](https://code.google.com/p/cuda-convnet/).  In particular, the top\r\nlayers are locally connected and not fully connected. Try editing the\r\narchitecture to exactly replicate that fully connected model.\r\n\r\n### Model Training <a class=\"md-anchor\" id=\"model-training\"></a>\r\n\r\nThe usual method for training a network to perform N-way classification is\r\n[multinomial logistic regression](https://en.wikipedia.org/wiki/Multinomial_logistic_regression),\r\naka. *softmax regression*. Softmax regression applies a\r\n[softmax](../../api_docs/python/nn.md#softmax) nonlinearity to the\r\noutput of the network and calculates the\r\n[cross-entropy](../../api_docs/python/nn.md#softmax_cross_entropy_with_logits)\r\nbetween the normalized predictions and a\r\n[1-hot encoding](../../api_docs/python/sparse_ops.md#sparse_to_dense) of the label.\r\nFor regularization, we also apply the usual\r\n[weight decay](../../api_docs/python/nn.md#l2_loss) losses to all learned\r\nvariables.  The objective function for the model is the sum of the cross entropy\r\nloss and all these weight decay terms, as returned by the `loss()` function.\r\n\r\nWe visualize it in TensorBoard with a [scalar_summary](../../api_docs/python/train.md#scalar_summary):\r\n\r\n![CIFAR-10 Loss](./cifar_loss.png \"CIFAR-10 Total Loss\")\r\n\r\nWe train the model using standard\r\n[gradient descent](https://en.wikipedia.org/wiki/Gradient_descent)\r\nalgorithm (see [Training](../../api_docs/python/train.md) for other methods)\r\nwith a learning rate that\r\n[exponentially decays](../../api_docs/python/train.md#exponential_decay)\r\nover time.\r\n\r\n![CIFAR-10 Learning Rate Decay](./cifar_lr_decay.png \"CIFAR-10 Learning Rate Decay\")\r\n\r\nThe `train()` function adds the operations needed to minimize the objective by\r\ncalculating the gradient and updating the learned variables (see\r\n[`GradientDescentOptimizer`](../../api_docs/python/train.md#GradientDescentOptimizer)\r\nfor details).  It returns an operation that executes all the calculations\r\nneeded to train and update the model for one batch of images.\r\n\r\n## Launching and Training the Model <a class=\"md-anchor\" id=\"AUTOGENERATED-launching-and-training-the-model\"></a>\r\n\r\nWe have built the model, let's now launch it and run the training operation with\r\nthe script `cifar10_train.py`.\r\n\r\n```shell\r\npython cifar10_train.py\r\n```\r\n\r\n**NOTE:** The first time you run any target in the CIFAR-10 tutorial,\r\nthe CIFAR-10 dataset is automatically downloaded. The data set is ~160MB\r\nso you may want to grab a quick cup of coffee for your first run.\r\n\r\nYou should see the output:\r\n\r\n```shell\r\nFilling queue with 20000 CIFAR images before starting to train. This will take a few minutes.\r\n2015-11-04 11:45:45.927302: step 0, loss = 4.68 (2.0 examples/sec; 64.221 sec/batch)\r\n2015-11-04 11:45:49.133065: step 10, loss = 4.66 (533.8 examples/sec; 0.240 sec/batch)\r\n2015-11-04 11:45:51.397710: step 20, loss = 4.64 (597.4 examples/sec; 0.214 sec/batch)\r\n2015-11-04 11:45:54.446850: step 30, loss = 4.62 (391.0 examples/sec; 0.327 sec/batch)\r\n2015-11-04 11:45:57.152676: step 40, loss = 4.61 (430.2 examples/sec; 0.298 sec/batch)\r\n2015-11-04 11:46:00.437717: step 50, loss = 4.59 (406.4 examples/sec; 0.315 sec/batch)\r\n...\r\n```\r\n\r\nThe script reports the total loss every 10 steps as well the speed at which\r\nthe last batch of data was processed. A few comments:\r\n\r\n* The first batch of data can be inordinately slow (e.g. several minutes) as the\r\npreprocessing threads fill up the shuffling queue with 20,000 processed CIFAR\r\nimages.\r\n\r\n* The reported loss is the average loss of the most recent batch. Remember that\r\nthis loss is the sum of the cross entropy and all weight decay terms.\r\n\r\n* Keep an eye on the processing speed of a batch. The numbers shown above were\r\nobtained on a Tesla K40c. If you are running on a CPU, expect slower performance.\r\n\r\n\r\n> **EXERCISE:** When experimenting, it is sometimes annoying that the first\r\ntraining step can take so long. Try decreasing the number of images initially\r\nthat initially fill up the queue.  Search for `NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN`\r\nin `cifar10.py`.\r\n\r\n`cifar10_train.py` periodically [saves](../../api_docs/python/state_ops.md#Saver)\r\nall model parameters in\r\n[checkpoint files](../../how_tos/variables/index.md#saving-and-restoring)\r\nbut it does *not* evaluate the model. The checkpoint file\r\nwill be used by `cifar10_eval.py` to measure the predictive\r\nperformance (see [Evaluating a Model](#evaluating-a-model) below).\r\n\r\n\r\nIf you followed the previous steps, then you have now started training\r\na CIFAR-10 model. [Congratulations!](https://www.youtube.com/watch?v=9bZkp7q19f0)\r\n\r\nThe terminal text returned from `cifar10_train.py` provides minimal insight into\r\nhow the model is training. We want more insight into the model during training:\r\n\r\n* Is the loss *really* decreasing or is that just noise?\r\n* Is the model being provided appropriate images?\r\n* Are the gradients, activations and weights reasonable?\r\n* What is the learning rate currently at?\r\n\r\n[TensorBoard](../../how_tos/summaries_and_tensorboard/index.md) provides this\r\nfunctionality, displaying data exported periodically from `cifar10_train.py` via\r\na\r\n[`SummaryWriter`](../../api_docs/python/train.md#SummaryWriter).\r\n\r\nFor instance, we can watch how the distribution of activations and degree of\r\nsparsity in `local3` features evolve during training:\r\n\r\n<div style=\"width:100%; margin:auto; margin-bottom:10px; margin-top:20px; display: flex; flex-direction: row\">\r\n  <img style=\"flex-grow:1; flex-shrink:1;\" src=\"./cifar_sparsity.png\">\r\n  <img style=\"flex-grow:1; flex-shrink:1;\" src=\"./cifar_activations.png\">\r\n</div>\r\n\r\nIndividual loss functions, as well as the total loss, are particularly\r\ninteresting to track over time. However, the loss exhibits a considerable amount\r\nof noise due to the small batch size employed by training.  In practice we find\r\nit extremely useful to visualize their moving averages in addition to their raw\r\nvalues.  See how the scripts use\r\n[`ExponentialMovingAverage`](../../api_docs/python/train.md#ExponentialMovingAverage)\r\nfor this purpose.\r\n\r\n## Evaluating a Model <a class=\"md-anchor\" id=\"evaluating-a-model\"></a>\r\n\r\nLet us now evaluate how well the trained model performs on a hold-out data set.\r\nthe model is evaluated by the script `cifar10_eval.py`.  It constructs the model\r\nwith the `inference()` function and uses all 10,000 images in the evaluation set\r\nof CIFAR-10. It calculates the *precision at 1:* how often the top prediction\r\nmatches the true label of the image.\r\n\r\nTo monitor how the model improves during training, the evaluation script runs\r\nperiodically on the latest checkpoint files created by the `cifar10_train.py`.\r\n\r\n```shell\r\npython cifar10_eval.py\r\n```\r\n\r\n> Be careful not to run the evaluation and training binary on the same GPU or\r\nelse you might run out of memory. Consider running the evaluation on\r\na separate GPU if available or suspending the training binary while running\r\nthe evaluation on the same GPU.\r\n\r\nYou should see the output:\r\n\r\n```shell\r\n2015-11-06 08:30:44.391206: precision @ 1 = 0.860\r\n...\r\n```\r\n\r\nThe script merely returns the precision @ 1 periodically -- in this case\r\nit returned 86% accuracy. `cifar10_eval.py` also\r\nexports summaries that may be visualized in TensorBoard. These summaries\r\nprovide additional insight into the model during evaluation.\r\n\r\nThe training script calculates the\r\n[moving average](../../api_docs/python/train.md#ExponentialMovingAverage)\r\nversion of all learned variables. The evaluation script substitutes\r\nall learned model parameters with the moving average version. This\r\nsubstitution boosts model performance at evaluation time.\r\n\r\n> **EXERCISE:** Employing averaged parameters may boost predictive performance\r\nby about 3% as measured by precision @ 1. Edit `cifar10_eval.py` to not employ\r\nthe averaged parameters for the model and verify that the predictive performance\r\ndrops.\r\n\r\n\r\n## Training a Model Using Multiple GPU Cards <a class=\"md-anchor\" id=\"AUTOGENERATED-training-a-model-using-multiple-gpu-cards\"></a>\r\n\r\nModern workstations may contain multiple GPUs for scientific computation.\r\nTensorFlow can leverage this environment to run the training operation\r\nconcurrently across multiple cards.\r\n\r\nTraining a model in a parallel, distributed fashion requires\r\ncoordinating training processes. For what follows we term *model replica*\r\nto be one copy of a model training on a subset of data.\r\n\r\nNaively employing asynchronous updates of model parameters\r\nleads to sub-optimal training performance\r\nbecause an individual model replica might be trained on a stale\r\ncopy of the model parameters. Conversely, employing fully synchronous\r\nupdates will be as slow as the slowest model replica.\r\n\r\nIn a workstation with multiple GPU cards, each GPU will have similar speed\r\nand contain enough memory to run an entire CIFAR-10 model. Thus, we opt to\r\ndesign our training system in the following manner:\r\n\r\n* Place an individual model replica on each GPU.\r\n* Update model parameters synchronously by waiting for all GPUs to finish\r\nprocessing a batch of data.\r\n\r\nHere is a diagram of this model:\r\n\r\n<div style=\"width:40%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n  <img style=\"width:100%\" src=\"./Parallelism.png\">\r\n</div>\r\n\r\nNote that each GPU computes inference as well as the gradients for a unique\r\nbatch of data. This setup effectively permits dividing up a larger batch\r\nof data across the GPUs.\r\n\r\nThis setup requires that all GPUs share the model parameters. A well-known\r\nfact is that transferring data to and from GPUs is quite slow. For this\r\nreason, we decide to store and update all model parameters on the CPU (see\r\ngreen box). A fresh set of model parameters is transferred to the GPU\r\nwhen a new batch of data is processed by all GPUs.\r\n\r\nThe GPUs are synchronized in operation. All gradients are accumulated from\r\nthe GPUs and averaged (see green box). The model parameters are updated with\r\nthe gradients averaged across all model replicas.\r\n\r\n### Placing Variables and Operations on Devices <a class=\"md-anchor\" id=\"AUTOGENERATED-placing-variables-and-operations-on-devices\"></a>\r\n\r\nPlacing operations and variables on devices requires some special\r\nabstractions.\r\n\r\nThe first abstraction we require is a function for computing inference and\r\ngradients for a single model replica. In the code we term this abstraction\r\na \"tower\". We must set two attributes for each tower:\r\n\r\n* A unique name for all operations within a tower.\r\n[`tf.name_scope()`](../../api_docs/python/framework.md#name_scope) provides\r\nthis unique name by prepending a scope. For instance, all operations in\r\nthe first tower are prepended with `tower_0`, e.g. `tower_0/conv1/Conv2D`.\r\n\r\n* A preferred hardware device to run the operation within a tower.\r\n[`tf.device()`](../../api_docs/python/framework.md#device) specifies this. For\r\ninstance, all operations in the first tower reside within `device('/gpu:0')`\r\nscope indicating that they should be run on the first GPU.\r\n\r\nAll variables are pinned to the CPU and accessed via\r\n[`tf.get_variable()`](../../api_docs/python/state_ops.md#get_variable)\r\nin order to share them in a multi-GPU version.\r\nSee how-to on [Sharing Variables](../../how_tos/variable_scope/index.md).\r\n\r\n### Launching and Training the Model on Multiple GPU cards <a class=\"md-anchor\" id=\"AUTOGENERATED-launching-and-training-the-model-on-multiple-gpu-cards\"></a>\r\n\r\nIf you have several GPU cards installed on your machine you can use them to\r\ntrain the model faster with the `cifar10_multi_gpu_train.py` script.  It is a\r\nvariation of the training script that parallelizes the model across multiple GPU\r\ncards.\r\n\r\n```shell\r\npython cifar10_multi_gpu_train.py --num_gpus=2\r\n```\r\n\r\nThe training script should output:\r\n\r\n```shell\r\nFilling queue with 20000 CIFAR images before starting to train. This will take a few minutes.\r\n2015-11-04 11:45:45.927302: step 0, loss = 4.68 (2.0 examples/sec; 64.221 sec/batch)\r\n2015-11-04 11:45:49.133065: step 10, loss = 4.66 (533.8 examples/sec; 0.240 sec/batch)\r\n2015-11-04 11:45:51.397710: step 20, loss = 4.64 (597.4 examples/sec; 0.214 sec/batch)\r\n2015-11-04 11:45:54.446850: step 30, loss = 4.62 (391.0 examples/sec; 0.327 sec/batch)\r\n2015-11-04 11:45:57.152676: step 40, loss = 4.61 (430.2 examples/sec; 0.298 sec/batch)\r\n2015-11-04 11:46:00.437717: step 50, loss = 4.59 (406.4 examples/sec; 0.315 sec/batch)\r\n...\r\n```\r\n\r\nNote that the number of GPU cards used defaults to 1. Additionally, if only 1\r\nGPU is available on your machine, all computations will be placed on it, even if\r\nyou ask for more.\r\n\r\n> **EXERCISE:** The default settings for `cifar10_train.py` is to\r\nrun on a batch size of 128. Try running `cifar10_multi_gpu_train.py` on 2 GPUs\r\nwith a batch size of 64 and compare the training speed.\r\n\r\n## Next Steps <a class=\"md-anchor\" id=\"AUTOGENERATED-next-steps\"></a>\r\n\r\n[Congratulations!](https://www.youtube.com/watch?v=9bZkp7q19f0) You have\r\ncompleted the CIFAR-10 tutorial.\r\n\r\nIf you are now interested in developing and training your own image\r\nclassification system, we recommend forking this tutorial and replacing\r\ncomponents to build address your image classification problem.\r\n\r\n> **EXERCISE:** Download the\r\n[Street View House Numbers (SVHN)](http://ufldl.stanford.edu/housenumbers/) data set.\r\nFork the CIFAR-10 tutorial and swap in the SVHN as the input data. Try adapting\r\nthe network architecture to improve predictive performance.\r\n"
  },
  {
    "path": "SOURCE/tutorials/deep_cnn.md",
    "content": "# 卷积神经网络 <a class=\"md-anchor\" id=\"AUTOGENERATED-convolutional-neural-networks\"></a>\r\n\r\n> **注意:** 本教程适用于对Tensorflow有丰富经验的用户，并假定用户有机器学习相关领域的专业知识和经验。\r\n\r\n## 概述 <a class=\"md-anchor\" id=\"AUTOGENERATED-overview\"></a>\r\n\r\n对CIFAR-10 数据集的分类是机器学习中一个公开的基准测试问题，其任务是对一组大小为32x32的RGB图像进行分类，这些图像涵盖了10个类别：  \r\n```飞机， 汽车， 鸟， 猫， 鹿， 狗， 青蛙， 马， 船以及卡车。```\r\n\r\n![CIFAR-10 Samples](../images/cifar_samples.png \"CIFAR-10 Samples, from http://www.cs.toronto.edu/~kriz/cifar.html\")\r\n\r\n想了解更多信息请参考[CIFAR-10 page](http://www.cs.toronto.edu/~kriz/cifar.html)，以及Alex Krizhevsky写的[技术报告](http://www.cs.toronto.edu/~kriz/learning-features-2009-TR.pdf)  \r\n\r\n### 目标 <a class=\"md-anchor\" id=\"AUTOGENERATED-goals\"></a>\r\n\r\n本教程的目标是建立一个用于识别图像的相对较小的卷积神经网络，在这一过程中，本教程会：\r\n\r\n1. 着重于建立一个规范的网络组织结构，训练并进行评估；\r\n2. 为建立更大规模更加复杂的模型提供一个范例\r\n\r\n选择CIFAR-10是因为它的复杂程度足以用来检验TensorFlow中的大部分功能，并可将其扩展为更大的模型。与此同时由于模型较小所以训练速度很快，比较适合用来测试新的想法，检验新的技术。\r\n\r\n### 本教程的重点 <a class=\"md-anchor\" id=\"AUTOGENERATED-highlights-of-the-tutorial\"></a>\r\nCIFAR-10 教程演示了在TensorFlow上构建更大更复杂模型的几个种重要内容：  \r\n* 相关核心数学对象，如[卷积](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#conv2d)、[修正线性激活](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#relu)、[最大池化](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#max_pool)以及[局部响应归一化](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#local_response_normalization)；  \r\n* 训练过程中一些网络行为的[可视化](https://github.com/jikexueyuanwiki/tensorflow-zh/tree/master/SOURCE/how_tos/summaries_and_tensorboard/index.md)，这些行为包括输入图像、损失情况、网络行为的分布情况以及梯度；  \r\n* 算法学习参数的[移动平均值](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md#ExponentialMovingAverage)的计算函数，以及在评估阶段使用这些平均值提高预测性能；  \r\n* 实现了一种机制，使得[学习率](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md#exponential_decay)随着时间的推移而递减；  \r\n* 为输入数据设计预存取[队列](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/io_ops.md#shuffle_batch)，将磁盘延迟和高开销的图像预处理操作与模型分离开来处理；  \r\n\r\n我们也提供了模型的多GPU版本，用以表明：  \r\n* 可以配置模型后使其在多个GPU上并行的训练  \r\n* 可以在多个GPU之间共享和更新变量值  \r\n\r\n我们希望本教程给大家开了个头，使得在Tensorflow上可以为视觉相关工作建立更大型的CNN模型\r\n\r\n### 模型架构 <a class=\"md-anchor\" id=\"AUTOGENERATED-model-architecture\"></a>\r\n\r\n本教程中的模型是一个多层架构，由卷积层和非线性层(nonlinearities)交替多次排列后构成。这些层最终通过全连通层对接到softmax分类器上。这一模型除了最顶部的几层外，基本跟[Alex Krizhevsky](https://code.google.com/p/cuda-convnet/)提出的模型一致。    \r\n\r\n在一个GPU上经过几个小时的训练后，该模型最高可以达到86%的精度。细节请查看[下面](#evaluating-a-model)的描述以及代码。模型中包含了1,068,298个学习参数，对一副图像进行分类大概需要19.5M个乘加操作。\r\n\r\n## 代码组织 <a class=\"md-anchor\" id=\"AUTOGENERATED-code-organization\"></a>\r\n\r\n本教程的代码位于[`tensorflow/models/image/cifar10/`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/).\r\n\r\n文件 | 作用\r\n--- | ---\r\n[`cifar10_input.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_input.py) | 读取本地CIFAR-10的二进制文件格式的内容。\r\n[`cifar10.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10.py) | 建立CIFAR-10的模型。\r\n[`cifar10_train.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_train.py) | 在CPU或GPU上训练CIFAR-10的模型。\r\n[`cifar10_multi_gpu_train.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_multi_gpu_train.py) | 在多GPU上训练CIFAR-10的模型。\r\n[`cifar10_eval.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_eval.py) | 评估CIFAR-10模型的预测性能。\r\n\r\n## CIFAR-10 模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-cifar-10-model\"></a>\r\n\r\nCIFAR-10 网络模型部分的代码位于\r\n[`cifar10.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10.py).\r\n完整的训练图中包含约765个操作。但是我们发现通过下面的模块来构造训练图可以最大限度的提高代码复用率:\r\n\r\n1. [**模型输入:**](#model-inputs) 包括`inputs()`  、 `distorted_inputs()`等一些操作，分别用于读取CIFAR的图像并进行预处理，做为后续评估和训练的输入； \r\n2. [**模型预测:**](#model-prediction) 包括`inference()`等一些操作，用于进行统计计算，比如在提供的图像进行分类；\r\nadds operations that perform inference, i.e. classification, on supplied images.\r\n3. [**模型训练:**](#model-training) 包括`loss()` and `train()`等一些操作，用于计算损失、计算梯度、进行变量更新以及呈现最终结果。\r\n\r\n### 模型输入 <a class=\"md-anchor\" id=\"model-inputs\"></a>\r\n\r\n输入模型是通过 `inputs()` 和`distorted_inputs()`函数建立起来的，这2个函数会从CIFAR-10二进制文件中读取图片文件，由于每个图片的存储字节数是固定的，因此可以使用[`tf.FixedLengthRecordReader`](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/io_ops.md#FixedLengthRecordReader)函数。更多的关于`Reader`类的功能可以查看[Reading Data](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/how_tos/reading_data/index.md#reading-from-files)。\r\n\r\n图片文件的处理流程如下：  \r\n\r\n*  图片会被统一裁剪到24x24像素大小，裁剪中央区域用于评估或[随机](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/image.md#random_crop)裁剪用于训练；\r\n*  图片会进行[近似的白化处理](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/image.md#per_image_whitening)，使得模型对图片的动态范围变化不敏感。\r\n\r\n对于训练，我们另外采取了一系列随机变换的方法来人为的增加数据集的大小：\r\n\r\n* 对图像进行[随机的左右翻转](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/image.md#random_flip_left_right)；\r\n* 随机变换[图像的亮度](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/image.md#random_brightness)；\r\n* 随机变换[图像的对比度](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/image.md#tf_image_random_contrast)；\r\n\r\n可以在[Images](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/image.md)页的列表中查看所有可用的变换，对于每个原始图我们还附带了一个[`image_summary`](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md#image_summary)，以便于在TensorBoard中查看。这对于检查输入图像是否正确十分有用。  \r\n\r\n<div style=\"width:50%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n  <img style=\"width:70%\" src=\"../images/cifar_image_summary.png\">\r\n</div>\r\n\r\n从磁盘上加载图像并进行变换需要花费不少的处理时间。为了避免这些操作减慢训练过程，我们在16个独立的线程中并行进行这些操作，这16个线程被连续的安排在一个TensorFlow[队列](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/io_ops.md#shuffle_batch)中。  \r\n\r\n### 模型预测 <a class=\"md-anchor\" id=\"model-prediction\"></a>\r\n\r\n模型的预测流程由`inference()`构造，该函数会添加必要的操作步骤用于计算预测值的 *logits*，其对应的模型组织方式如下所示：\r\n\r\nLayer 名称 | 描述\r\n--- | ---\r\n`conv1` | 实现[卷积](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#conv2d) 以及 [rectified linear](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#relu) activation.\r\n`pool1` | [max pooling](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#max_pool).\r\n`norm1` | [局部响应归一化](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#local_response_normalization).\r\n`conv2` | [卷积](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#conv2d) and [rectified linear](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#relu) activation.\r\n`norm2` | [局部响应归一化](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#local_response_normalization).\r\n`pool2` | [max pooling](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#max_pool).\r\n`local3` | [基于修正线性激活的全连接层](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md).\r\n`local4` | [基于修正线性激活的全连接层](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md).\r\n`softmax_linear` | 进行线性变换以输出 logits.\r\n\r\n这里有一个由TensorBoard绘制的图形，用于描述模型建立过程中经过的步骤：\r\n\r\n<div style=\"width:15%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n  <img style=\"width:100%\" src=\"../images/cifar_graph.png\">\r\n</div>\r\n\r\n> **练习**: `inference`的输出是未归一化的logits，尝试使用[`tf.softmax()`](tensorflow-zh/SOURCE/api_docs/python/nn.md#softmax)修改网络架构后返回归一化的预测值。\r\n\r\n`inputs()` 和 `inference()` 函数提供了评估模型时所需的所有构件，现在我们把讲解的重点从构建一个模型转向训练一个模型。\r\n\r\n> **练习:** `inference()` 中的模型跟[cuda-convnet](https://code.google.com/p/cuda-convnet/)中描述的CIFAR-10模型有些许不同，其差异主要在于其顶层不是全连接层而是局部连接层，可以尝试修改网络架构来准确的复制全连接模型。\r\n\r\n### 模型训练 <a class=\"md-anchor\" id=\"model-training\"></a>\r\n\r\n训练一个可进行N维分类的网络的常用方法是使用[多项式逻辑回归](https://en.wikipedia.org/wiki/Multinomial_logistic_regression),又被叫做*softmax 回归*。Softmax 回归在网络的输出层上附加了一个[softmax](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#softmax) nonlinearity，并且计算归一化的预测值和label的[1-hot encoding](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/sparse_ops.md#sparse_to_dense)的[交叉熵](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#softmax_cross_entropy_with_logits)。在正则化过程中，我们会对所有学习变量应用[权重衰减损失](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#l2_loss)。模型的目标函数是求交叉熵损失和所有权重衰减项的和，`loss()`函数的返回值就是这个值。\r\n\r\n在TensorBoard中使用[scalar_summary](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md#scalar_summary)来查看该值的变化情况：\r\n\r\n![CIFAR-10 Loss](../images/cifar_loss.png \"CIFAR-10 Total Loss\")\r\n\r\n我们使用标准的梯度下降算法来训练模型（也可以在[Training](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md)中看看其他方法），其学习率随时间以指数形式衰减。\r\n\r\n![CIFAR-10 Learning Rate Decay](../images/cifar_lr_decay.png \"CIFAR-10 Learning Rate Decay\")\r\n\r\n`train()` 函数会添加一些操作使得目标函数最小化，这些操作包括计算梯度、更新学习变量（详细信息请查看[`GradientDescentOptimizer`](tensorflow-zh/SOURCE/api_docs/python/train.md#GradientDescentOptimizer)）。`train()` 函数最终会返回一个用以对一批图像执行所有计算的操作步骤，以便训练并更新模型。\r\n\r\n## 开始执行并训练模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-launching-and-training-the-model\"></a>\r\n\r\n我们已经把模型建立好了，现在通过执行脚本`cifar10_train.py`来启动训练过程。\r\n\r\n```shell\r\npython cifar10_train.py\r\n```\r\n\r\n**注意:** 当第一次在CIFAR-10教程上启动任何任务时，会自动下载CIFAR-10数据集，该数据集大约有160M大小，因此第一次运行时泡杯咖啡小栖一会吧。\r\n\r\n你应该可以看到如下类似的输出:\r\n\r\n```shell\r\nFilling queue with 20000 CIFAR images before starting to train. This will take a few minutes.\r\n2015-11-04 11:45:45.927302: step 0, loss = 4.68 (2.0 examples/sec; 64.221 sec/batch)\r\n2015-11-04 11:45:49.133065: step 10, loss = 4.66 (533.8 examples/sec; 0.240 sec/batch)\r\n2015-11-04 11:45:51.397710: step 20, loss = 4.64 (597.4 examples/sec; 0.214 sec/batch)\r\n2015-11-04 11:45:54.446850: step 30, loss = 4.62 (391.0 examples/sec; 0.327 sec/batch)\r\n2015-11-04 11:45:57.152676: step 40, loss = 4.61 (430.2 examples/sec; 0.298 sec/batch)\r\n2015-11-04 11:46:00.437717: step 50, loss = 4.59 (406.4 examples/sec; 0.315 sec/batch)\r\n...\r\n```\r\n\r\n脚本会在每10步训练过程后打印出总损失值，以及最后一批数据的处理速度。下面是几点注释：\r\n\r\n* 第一批数据会非常的慢（大概要几分钟时间），因为预处理线程要把20,000个待处理的CIFAR图像填充到重排队列中；\r\n\r\n* 打印出来的损失值是最近一批数据的损失值的均值。请记住损失值是交叉熵和权重衰减项的和；\r\n\r\n* 上面打印结果中关于一批数据的处理速度是在Tesla K40C上统计出来的，如果你运行在CPU上，性能会比此要低；\r\n\r\n> **练习:** 当实验时，第一阶段的训练时间有时会非常的长，长到足以让人生厌。可以尝试减少初始化时初始填充到队列中图片数量来改变这种情况。在`cifar10.py`中搜索`NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN`并修改之。  \r\n\r\n`cifar10_train.py` 会周期性的在[检查点文件](https://github.com/jikexueyuanwiki/tensorflow-zh/tree/master/SOURCE/how_tos/variables/index.md#saving-and-restoring)中[保存](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/state_ops.md#Saver)模型中的所有参数，但是*不会*对模型进行评估。`cifar10_eval.py`会使用该检查点文件来测试预测性能（详见下面的描述：[评估模型](#评估模型)）。\r\n\r\n如果按照上面的步骤做下来，你应该已经开始训练一个CIFAR-10模型了。[恭喜你!](https://www.youtube.com/watch?v=9bZkp7q19f0)\r\n\r\n`cifar10_train.py`输出的终端信息中提供了关于模型如何训练的一些信息，但是我们可能希望了解更多关于模型训练时的信息，比如：  \r\n* 损失是*真的*在减小还是看到的只是噪声数据？  \r\n* 为模型提供的图片是否合适？  \r\n* 梯度、激活、权重的值是否合理？  \r\n* 当前的学习率是多少？  \r\n\r\n[TensorBoard](https://github.com/jikexueyuanwiki/tensorflow-zh/tree/master/SOURCE/how_tos/summaries_and_tensorboard/index.md)提供了该功能，可以通过`cifar10_train.py`中的[`SummaryWriter`](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md#SummaryWriter)周期性的获取并显示这些数据。\r\n \r\n比如我们可以在训练过程中查看`local3`的激活情况，以及其特征维度的稀疏情况：\r\n\r\n<div style=\"width:100%; margin:auto; margin-bottom:10px; margin-top:20px; display: flex; flex-direction: row\">\r\n  <img style=\"flex-grow:1; flex-shrink:1;\" src=\"../images/cifar_sparsity.png\">\r\n  <img style=\"flex-grow:1; flex-shrink:1;\" src=\"../images/cifar_activations.png\">\r\n</div>\r\n\r\n相比于总损失，在训练过程中的单项损失尤其值得人们的注意。但是由于训练中使用的数据批量比较小，损失值中夹杂了相当多的噪声。在实践过程中，我们也发现相比于原始值，损失值的移动平均值显得更为有意义。请参阅脚本[`ExponentialMovingAverage`](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md#ExponentialMovingAverage)了解如何实现。\r\n\r\n## 评估模型 <a class=\"md-anchor\" id=\"evaluating-a-model\"></a>  \r\n\r\n现在可以在另一部分数据集上来评估训练模型的性能。脚本文件`cifar10_eval.py`对模型进行了评估，利用 `inference()`函数重构模型，并使用了在评估数据集所有10,000张CIFAR-10图片进行测试。最终计算出的精度为*1:N*，N=预测值中置信度最高的一项与图片真实label匹配的频次。(It calculates the *precision at 1:* how often the top prediction matches the true label of the image)。\r\n\r\n为了监控模型在训练过程中的改进情况，评估用的脚本文件会周期性的在最新的检查点文件上运行，这些检查点文件是由`cifar10_train.py`产生。\r\n\r\n```shell\r\npython cifar10_eval.py\r\n```\r\n\r\n>注意：不要在同一块GPU上同时运行训练程序和评估程序，因为可能会导致内存耗尽。尽可能的在其它单独的GPU上运行评估程序，或者在同一块GPU上运行评估程序时先挂起训练程序。\r\n\r\n你可能会看到如下所示输出:\r\n\r\n```shell\r\n2015-11-06 08:30:44.391206: precision @ 1 = 0.860\r\n...\r\n```\r\n\r\n评估脚本只是周期性的返回precision@1 (The script merely returns the precision @ 1 periodically)--在该例中返回的准确率是86%。`cifar10_eval.py` 同时也返回其它一些可以在TensorBoard中进行可视化的简要信息。可以通过这些简要信息在评估过程中进一步的了解模型。\r\n\r\n训练脚本会为所有学习变量计算其[移动均值](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md#ExponentialMovingAverage),评估脚本则直接将所有学习到的模型参数替换成对应的移动均值。这一替代方式可以在评估过程中提升模型的性能。  \r\n\r\n> **练习:** 通过precision @ 1测试发现，使用均值参数可以将预测性能提高约3%，在`cifar10_eval.py`中尝试修改为不采用均值参数的方式，并确认由此带来的预测性能下降。  \r\n\r\n## 在多个GPU板卡上训练模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-training-a-model-using-multiple-gpu-cards\"></a>\r\n\r\n现代的工作站可能包含多个GPU进行科学计算。TensorFlow可以利用这一环境在多个GPU卡上运行训练程序。\r\n\r\n在并行、分布式的环境中进行训练，需要对训练程序进行协调。对于接下来的描述，术语*模型拷贝*（*model replica*）特指在一个数据子集中训练出来的模型的一份拷贝。\r\n\r\n如果天真的对模型参数的采用异步方式更新将会导致次优的训练性能，这是因为我们可能会基于一个旧的模型参数的拷贝去训练一个模型。但与此相反采用完全同步更新的方式，其速度将会变得和最慢的模型一样慢(Conversely, employing fully synchronous updates will be as slow as the slowest model replica.)。\r\n\r\n在具有多个GPU的工作站中，每个GPU的速度基本接近，并且都含有足够的内存来运行整个CIFAR-10模型。因此我们选择以下方式来设计我们的训练系统：  \r\n\r\n* 在每个GPU上放置单独的模型副本；\r\n  \r\n* 等所有GPU处理完一批数据后再同步更新模型的参数；\r\n\r\n下图示意了该模型的结构：:\r\n\r\n<div style=\"width:40%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n  <img style=\"width:100%\" src=\"../images/Parallelism.png\">\r\n</div>\r\n\r\n可以看到，每一个GPU会用一批独立的数据计算梯度和估计值。这种设置可以非常有效的将一大批数据分割到各个GPU上。\r\n\r\n这一机制要求所有GPU能够共享模型参数。但是众所周知在GPU之间传输数据非常的慢，因此我们决定在CPU上存储和更新所有模型的参数(对应图中绿色矩形的位置)。这样一来，GPU在处理一批新的数据之前会更新一遍的参数。\r\n\r\n图中所有的GPU是同步运行的。所有GPU中的梯度会累积并求平均值(绿色方框部分)。模型参数会利用所有模型副本梯度的均值来更新。  \r\n\r\n### 在多个设备中设置变量和操作  \r\n\r\n在多个设备中设置变量和操作时需要做一些特殊的抽象。\r\n\r\n我们首先需要把在单个模型拷贝中计算估计值和梯度的行为抽象到一个函数中。在代码中，我们称这个抽象对象为“tower”。对于每一个“tower”我们都需要设置它的两个属性：  \r\n* 在一个tower中为所有操作设定一个唯一的名称。[`tf.name_scope()`](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/framework.md#name_scope)通过添加一个范围前缀来提供该唯一名称。比如，第一个tower中的所有操作都会附带一个前缀`tower_0`，示例：`tower_0/conv1/Conv2D`；\r\n\r\n* 在一个tower中运行操作的优先硬件设备。 [`tf.device()`](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/framework.md#device) 提供该信息。比如，在第一个tower中的所有操作都位于 `device('/gpu:0')`范围中，暗含的意思是这些操作应该运行在第一块GPU上；\r\n\r\n为了在多个GPU上共享变量，所有的变量都绑定在CPU上，并通过[`tf.get_variable()`](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/state_ops.md#get_variable)访问。可以查看[Sharing Variables](https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/how_tos/variables/index.md)以了解如何共享变量。\r\n\r\n### 启动并在多个GPU上训练模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-launching-and-training-the-model-on-multiple-gpu-cards\"></a>\r\n\r\n如果你的机器上安装有多块GPU，你可以通过使用`cifar10_multi_gpu_train.py`脚本来加速模型训练。该脚本是训练脚本的一个变种，使用多个GPU实现模型并行训练。\r\n\r\n```shell\r\npython cifar10_multi_gpu_train.py --num_gpus=2\r\n```\r\n\r\n训练脚本的输出如下所示：\r\n\r\n```shell\r\nFilling queue with 20000 CIFAR images before starting to train. This will take a few minutes.\r\n2015-11-04 11:45:45.927302: step 0, loss = 4.68 (2.0 examples/sec; 64.221 sec/batch)\r\n2015-11-04 11:45:49.133065: step 10, loss = 4.66 (533.8 examples/sec; 0.240 sec/batch)\r\n2015-11-04 11:45:51.397710: step 20, loss = 4.64 (597.4 examples/sec; 0.214 sec/batch)\r\n2015-11-04 11:45:54.446850: step 30, loss = 4.62 (391.0 examples/sec; 0.327 sec/batch)\r\n2015-11-04 11:45:57.152676: step 40, loss = 4.61 (430.2 examples/sec; 0.298 sec/batch)\r\n2015-11-04 11:46:00.437717: step 50, loss = 4.59 (406.4 examples/sec; 0.315 sec/batch)\r\n...\r\n```\r\n需要注意的是默认的GPU使用数是1，此外，如果你的机器上只有一个GPU，那么所有的计算都只会在一个GPU上运行，即便你可能设置的是N个。\r\n\r\n> **练习:** `cifar10_train.py`中的批处理大小默认配置是128。尝试在2个GPU上运行`cifar10_multi_gpu_train.py`脚本，并且设定批处理大小为64，然后比较2种方式的训练速度。\r\n\r\n## 下一步 <a class=\"md-anchor\" id=\"AUTOGENERATED-next-steps\"></a>\r\n\r\n[恭喜你!](https://www.youtube.com/watch?v=9bZkp7q19f0) 你已经完成了CIFAR-10教程。\r\n如果你对开发和训练自己的图像分类系统感兴趣，我们推荐你新建一个基于该教程的分支，并修改其中的内容以建立解决您问题的图像分类系统。\r\n\r\n> **练习:** 下载[Street View House Numbers (SVHN)](http://ufldl.stanford.edu/housenumbers/) 数据集。新建一个CIFAR-10教程的分支，并将输入数据替换成SVHN。尝试改变网络结构以提高预测性能。\r\n\r\n> 原文：[Convolutional Neural Networks](http://tensorflow.org/tutorials/deep_cnn/index.md)  翻译：[oskycar](https://github.com/oskycar) 校对：[KK4SBB](https://github.com/zhyhooo)\r\n"
  },
  {
    "path": "SOURCE/tutorials/mandelbrot/index.md",
    "content": "# Mandelbrot Set <a class=\"md-anchor\" id=\"AUTOGENERATED-mandelbrot-set\"></a>\r\n\r\nVisualizing the Mandelbrot set doesn't have anything to do with machine\r\nlearning, but it makes for a fun example of how one can use TensorFlow for\r\ngeneral mathematics.  This is actually a pretty naive implementation of the\r\nvisualization, but it makes the point.  (We may end up providing a more\r\nelaborate implementation down the line to produce more truly beautiful images.)\r\n\r\nNote: This tutorial was originally prepared as an IPython notebook.\r\n\r\n## Basic Setup <a class=\"md-anchor\" id=\"AUTOGENERATED-basic-setup\"></a>\r\n\r\nWe'll need a few imports to get started.\r\n\r\n```python\r\n# Import libraries for simulation\r\nimport tensorflow as tf\r\nimport numpy as np\r\n\r\n# Imports for visualization\r\nimport PIL.Image\r\nfrom cStringIO import StringIO\r\nfrom IPython.display import clear_output, Image, display\r\nimport scipy.ndimage as nd\r\n```\r\n\r\nNow we'll define a function to actually display the image once we have\r\niteration counts.\r\n\r\n```python\r\ndef DisplayFractal(a, fmt='jpeg'):\r\n  \"\"\"Display an array of iteration counts as a\r\n     colorful picture of a fractal.\"\"\"\r\n  a_cyclic = (6.28*a/20.0).reshape(list(a.shape)+[1])\r\n  img = np.concatenate([10+20*np.cos(a_cyclic),\r\n                        30+50*np.sin(a_cyclic),\r\n                        155-80*np.cos(a_cyclic)], 2)\r\n  img[a==a.max()] = 0\r\n  a = img\r\n  a = np.uint8(np.clip(a, 0, 255))\r\n  f = StringIO()\r\n  PIL.Image.fromarray(a).save(f, fmt)\r\n  display(Image(data=f.getvalue()))\r\n```\r\n\r\n## Session and Variable Initialization <a class=\"md-anchor\" id=\"AUTOGENERATED-session-and-variable-initialization\"></a>\r\n\r\nFor playing around like this, we often use an interactive session, but a regular\r\nsession would work as well.\r\n\r\n```python\r\n   sess = tf.InteractiveSession()\r\n```\r\n\r\nIt's handy that we can freely mix NumPy and TensorFlow.\r\n\r\n```python\r\n# Use NumPy to create a 2D array of complex numbers on [-2,2]x[-2,2]\r\n\r\nY, X = np.mgrid[-1.3:1.3:0.005, -2:1:0.005]\r\nZ = X+1j*Y\r\n```\r\n\r\nNow we define and initialize TensorFlow tensors.\r\n\r\n```python\r\nxs = tf.constant(Z.astype(\"complex64\"))\r\nzs = tf.Variable(xs)\r\nns = tf.Variable(tf.zeros_like(xs, \"float32\"))\r\n```\r\n\r\nTensorFlow requires that you explicitly initialize variables before using them.\r\n\r\n```python\r\ntf.initialize_all_variables().run()\r\n```\r\n\r\n## Defining and Running the Computation <a class=\"md-anchor\" id=\"AUTOGENERATED-defining-and-running-the-computation\"></a>\r\n\r\nNow we specify more of the computation...\r\n\r\n```python\r\n# Compute the new values of z: z^2 + x\r\nzs_ = zs*zs + xs\r\n\r\n# Have we diverged with this new value?\r\nnot_diverged = tf.complex_abs(zs_) < 4\r\n\r\n# Operation to update the zs and the iteration count.\r\n#\r\n# Note: We keep computing zs after they diverge! This\r\n#       is very wasteful! There are better, if a little\r\n#       less simple, ways to do this.\r\n#\r\nstep = tf.group(\r\n  zs.assign(zs_),\r\n  ns.assign_add(tf.cast(not_diverged, \"float32\"))\r\n  )\r\n```\r\n\r\n... and run it for a couple hundred steps\r\n\r\n```python\r\nfor i in range(200): step.run()\r\n```\r\n\r\nLet's see what we've got.\r\n\r\n```python\r\nDisplayFractal(ns.eval())\r\n```\r\n\r\n![jpeg](mandelbrot_output.jpg)\r\n\r\nNot bad!\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/tutorials/mandelbrot.md",
    "content": "# 曼德布洛特(Mandelbrot)集合 <a class=\"md-anchor\" id=\"AUTOGENERATED-mandelbrot-set\"></a>\r\n\r\n虽然可视化曼德布洛特(Mandelbrot)集合与机器学习没有任何关系，但这对于将TensorFlow应用在数学更广泛的领域是一个有趣的例子。实际上，这是tensorflow一个非常直截了当的可视化运用。（我们最终也许会提供一种更加精心设计的运用方式来生成真正更加美丽的图像。）\r\n\r\n说明：本教程使用了IPython的notebook。\r\n\r\n## 基本步骤 <a class=\"md-anchor\" id=\"AUTOGENERATED-basic-setup\"></a>\r\n\r\n首先，我们需要导入一些库。\r\n\r\n```python\r\n# 导入仿真库\r\nimport tensorflow as tf\r\nimport numpy as np\r\n\r\n# 导入可视化库\r\nimport PIL.Image\r\nfrom cStringIO import StringIO\r\nfrom IPython.display import clear_output, Image, display\r\nimport scipy.ndimage as nd\r\n```\r\n\r\n现在我们将定义一个函数来显示迭代计算出的图像。\r\n\r\n```python\r\ndef DisplayFractal(a, fmt='jpeg'):\r\n  \"\"\"显示迭代计算出的彩色分形图像。\"\"\"\r\n  a_cyclic = (6.28*a/20.0).reshape(list(a.shape)+[1])\r\n  img = np.concatenate([10+20*np.cos(a_cyclic),\r\n                        30+50*np.sin(a_cyclic),\r\n                        155-80*np.cos(a_cyclic)], 2)\r\n  img[a==a.max()] = 0\r\n  a = img\r\n  a = np.uint8(np.clip(a, 0, 255))\r\n  f = StringIO()\r\n  PIL.Image.fromarray(a).save(f, fmt)\r\n  display(Image(data=f.getvalue()))\r\n```\r\n\r\n## 会话（session）和变量（variable）初始化 <a class=\"md-anchor\" id=\"AUTOGENERATED-session-and-variable-initialization\"></a>\r\n\r\n为了操作的方便，我们常常使用交互式会话（interactive session），但普通会话（regular session）也能正常使用。\r\n\r\n```python\r\n   sess = tf.InteractiveSession()\r\n```\r\n\r\n我们可以自由的混合使用NumPy和TensorFlow，这一点非常方便。\r\n\r\n```python\r\n# 使用NumPy创建一个在[-2,2]x[-2,2]范围内的2维复数数组\r\n\r\nY, X = np.mgrid[-1.3:1.3:0.005, -2:1:0.005]\r\nZ = X+1j*Y\r\n```\r\n\r\n现在我们定义并初始化一组TensorFlow的张量 （tensors）。\r\n\r\n```python\r\nxs = tf.constant(Z.astype(\"complex64\"))\r\nzs = tf.Variable(xs)\r\nns = tf.Variable(tf.zeros_like(xs, \"float32\"))\r\n```\r\n\r\nTensorFlow在使用之前需要你明确给定变量的初始值。\r\n\r\n```python\r\ntf.initialize_all_variables().run()\r\n```\r\n\r\n## 定义并运行计算 <a class=\"md-anchor\" id=\"AUTOGENERATED-defining-and-running-the-computation\"></a>\r\n\r\n现在我们指定更多的计算...\r\n\r\n```python\r\n# 计算一个新值z: z^2 + x\r\nzs_ = zs*zs + xs\r\n\r\n# 这个新值会发散吗？\r\nnot_diverged = tf.complex_abs(zs_) < 4\r\n\r\n# 更新zs并且迭代计算。\r\n#\r\n# 说明：在这些值发散之后，我们仍然在计算zs，这个计算消耗特别大！\r\n#      如果稍微简单点，这里有更好的方法来处理。\r\n#\r\nstep = tf.group(\r\n  zs.assign(zs_),\r\n  ns.assign_add(tf.cast(not_diverged, \"float32\"))\r\n  )\r\n```\r\n\r\n...继续执行几百个步骤\r\n\r\n```python\r\nfor i in range(200): step.run()\r\n```\r\n\r\n让我们看看我们得到了什么。\r\n\r\n```python\r\nDisplayFractal(ns.eval())\r\n```\r\n\r\n![jpeg](../images/mandelbrot_output.jpg)\r\n\r\n结果不错！\r\n\r\n> 原文：[Mandelbrot Set](http://tensorflow.org/tutorials/mandelbrot/index.md)  翻译：[ericxk](https://github.com/ericxk)  校对：[tensorfly](https://github.com/tensorfly)\r\n"
  },
  {
    "path": "SOURCE/tutorials/mnist/__init__.py",
    "content": ""
  },
  {
    "path": "SOURCE/tutorials/mnist/beginners/index.md",
    "content": "# MNIST For ML Beginners <a class=\"md-anchor\" id=\"AUTOGENERATED-mnist-for-ml-beginners\"></a>\r\n\r\n*This tutorial is intended for readers who are new to both machine learning and\r\nTensorFlow. If you already\r\nknow what MNIST is, and what softmax (multinomial logistic) regression is,\r\nyou might prefer this [faster paced tutorial](../../../tutorials/mnist/pros/index.md).*\r\n\r\nWhen one learns how to program, there's a tradition that the first thing you do\r\nis print \"Hello World.\" Just like programming has Hello World, machine learning\r\nhas MNIST.\r\n\r\nMNIST is a simple computer vision dataset. It consists of images of handwritten\r\ndigits like these:\r\n\r\n<div style=\"width:40%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"img/MNIST.png\">\r\n</div>\r\n\r\nIt also includes labels for each image, telling us which digit it is. For\r\nexample, the labels for the above images are 5, 0, 4, and 1.\r\n\r\nIn this tutorial, we're going to train a model to look at images and predict\r\nwhat digits they are. Our goal isn't to train a really elaborate model that\r\nachieves state-of-the-art performance -- although we'll give you code to do that\r\nlater! -- but rather to dip a toe into using TensorFlow. As such, we're going\r\nto start with a very simple model, called a Softmax Regression.\r\n\r\nThe actual code for this tutorial is very short, and all the interesting\r\nstuff happens in just three lines. However, it is very\r\nimportant to understand the ideas behind it: both how TensorFlow works and the\r\ncore machine learning concepts. Because of this, we are going to very carefully\r\nwork through the code.\r\n\r\n## The MNIST Data <a class=\"md-anchor\" id=\"AUTOGENERATED-the-mnist-data\"></a>\r\n\r\nThe MNIST data is hosted on\r\n[Yann LeCun's website](http://yann.lecun.com/exdb/mnist/).  For your\r\nconvenience, we've included some python code to download and install the data\r\nautomatically. You can either download\r\n[the code](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/input_data.py)\r\nand import it as below, or simply copy and paste it in.\r\n\r\n```python\r\nimport input_data\r\nmnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)\r\n```\r\n\r\nThe downloaded data is split into two parts, 60,000 data points of training\r\ndata (`mnist.train`) and 10,000 points of test data (`mnist.test`).  This\r\nsplit is very important: it's essential in machine learning that we\r\nhave separate data which we don't learn from so that we can make sure\r\nthat what we've learned actually generalizes!\r\n\r\nAs mentioned earlier, every MNIST data point has two parts: an image of a\r\nhandwritten digit and a corresponding label. We will call the images \"xs\" and\r\nthe labels \"ys\". Both the training set and test set contain xs and ys, for\r\nexample the training images are `mnist.train.images` and the train labels are\r\n`mnist.train.labels`.\r\n\r\nEach image is 28 pixels by 28 pixels. We can interpret this as a big array of\r\nnumbers:\r\n\r\n<div style=\"width:50%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"img/MNIST-Matrix.png\">\r\n</div>\r\n\r\nWe can flatten this array into a vector of 28x28 = 784 numbers. It doesn't\r\nmatter how we flatten the array, as long as we're consistent between images.\r\nFrom this perspective, the MNIST images are just a bunch of points in a\r\n784-dimensional vector space, with a\r\n[very rich structure](http://colah.github.io/posts/2014-10-Visualizing-MNIST/)\r\n(warning: computationally intensive visualizations).\r\n\r\nFlattening the data throws away information about the 2D structure of the image.\r\nIsn't that bad? Well, the best computer vision methods do exploit this\r\nstructure, and we will in later tutorials. But the simple method we will be\r\nusing here, a softmax regression, won't.\r\n\r\nThe result is that `mnist.train.images` is a tensor (an n-dimensional array) with a\r\nshape of `[60000, 784]`. The first dimension indexes the images and the second\r\ndimension indexes the pixels in each image. Each entry in the tensor is the\r\npixel intensity between 0 and 1, for a particular pixel in a particular image.\r\n\r\n<div style=\"width:40%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"img/mnist-train-xs.png\">\r\n</div>\r\n\r\nThe corresponding labels in MNIST are numbers between 0 and 9, describing\r\nwhich digit a given image is of.\r\nFor the purposes of this tutorial, we're going to want our labels\r\nas \"one-hot vectors\". A one-hot vector is a vector which is 0 in most\r\ndimensions, and 1 in a single dimension. In this case, the \\\\(n\\\\)th digit will be\r\nrepresented as a vector which is 1 in the \\\\(n\\\\)th dimensions. For example, 3 would be \\\\([0,0,0,1,0,0,0,0,0,0]\\\\).\r\nConsequently, `mnist.train.labels` is a\r\n`[60000, 10]` array of floats.\r\n\r\n<div style=\"width:40%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"img/mnist-train-ys.png\">\r\n</div>\r\n\r\nWe're now ready to actually make our model!\r\n\r\n## Softmax Regressions <a class=\"md-anchor\" id=\"AUTOGENERATED-softmax-regressions\"></a>\r\n\r\nWe know that every image in MNIST is a digit, whether it's a zero or a nine. We\r\nwant to be able to look at an image and give probabilities for it being each\r\ndigit. For example, our model might look at a picture of a nine and be 80% sure\r\nit's a nine, but give a 5% chance to it being an eight (because of the top loop)\r\nand a bit of probability to all the others because it isn't sure.\r\n\r\nThis is a classic case where a softmax regression is a natural, simple model.\r\nIf you want to assign probabilities to an object being one of several different\r\nthings, softmax is the thing to do. Even later on, when we train more\r\nsophisticated models, the final step will be a layer of softmax.\r\n\r\nA softmax regression has two steps: first we add up the evidence of our input\r\nbeing in certain classes, and then we convert that evidence into probabilities.\r\n\r\nTo tally up the evidence that a given image is in a particular class, we do a\r\nweighted sum of the pixel intensities. The weight is negative if that pixel\r\nhaving a high intensity is evidence against the image being in that class,\r\nand positive if it is evidence in favor.\r\n\r\nThe following diagram shows the weights one model learned for each of these\r\nclasses. Red represents negative weights, while blue represents positive\r\nweights.\r\n\r\n<div style=\"width:40%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"img/softmax-weights.png\">\r\n</div>\r\n\r\nWe also add some extra evidence called a bias. Basically, we want to be able\r\nto say that some things are more likely independent of the input. The result is\r\nthat the evidence for a class \\\\(i\\\\) given an input \\\\(x\\\\) is:\r\n\r\n$$\\text{evidence}_i = \\sum_j W_{i,~ j} x_j + b_i$$\r\n\r\nwhere \\\\(W\\_i\\\\) is the weights and \\\\(b\\_i\\\\) is the bias for class \\\\(i\\\\),\r\nand \\\\(j\\\\) is an index for summing over the pixels in our input image \\\\(x\\\\).\r\nWe then convert the evidence tallies into our predicted probabilities\r\n\\\\(y\\\\) using the \"softmax\" function:\r\n\r\n$$y = \\text{softmax}(\\text{evidence})$$\r\n\r\nHere softmax is serving as an \"activation\" or \"link\" function, shaping\r\nthe output of our linear function into the form we want -- in this case, a\r\nprobability distribution over 10 cases.\r\nYou can think of it as converting tallies\r\nof evidence into probabilities of our input being in each class.\r\nIt's defined as:\r\n\r\n$$\\text{softmax}(x) = \\text{normalize}(\\exp(x))$$\r\n\r\nIf you expand that equation out, you get:\r\n\r\n$$\\text{softmax}(x)_i = \\frac{\\exp(x_i)}{\\sum_j \\exp(x_j)}$$\r\n\r\nBut it's often more helpful to think of softmax the first way:\r\nexponentiating its inputs and then normalizing them. The exponentiation\r\nmeans that one unit more evidence increases the weight given to any hypothesis\r\nmultiplicatively. And conversely, having one less unit of evidence means that a\r\nhypothesis gets a fraction of its earlier weight. No hypothesis ever has zero\r\nor negative weight. Softmax then normalizes these weights, so that they add up\r\nto one, forming a valid probability distribution. (To get more intuition about\r\nthe softmax function, check out the\r\n[section](http://neuralnetworksanddeeplearning.com/chap3.html#softmax)\r\non it in Michael Nieslen's book, complete with an interactive visualization.)\r\n\r\n\r\nYou can picture our softmax regression as looking something like the following,\r\nalthough with a lot more \\\\(x\\\\)s. For each output, we compute a weighted sum of\r\nthe \\\\(x\\\\)s, add a bias, and then apply softmax.\r\n\r\n<div style=\"width:55%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"img/softmax-regression-scalargraph.png\">\r\n</div>\r\n\r\nIf we write that out as equations, we get:\r\n\r\n<div style=\"width:52%; margin-left:25%; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"img/softmax-regression-scalarequation.png\">\r\n</div>\r\n\r\nWe can \"vectorize\" this procedure, turning it into a matrix multiplication\r\nand vector addition. This is helpful for computational efficiency. (It's also\r\na useful way to think.)\r\n\r\n<div style=\"width:50%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"img/softmax-regression-vectorequation.png\">\r\n</div>\r\n\r\nMore compactly, we can just write:\r\n\r\n$$y = \\text{softmax}(Wx + b)$$\r\n\r\n\r\n## Implementing the Regression <a class=\"md-anchor\" id=\"AUTOGENERATED-implementing-the-regression\"></a>\r\n\r\n\r\nTo do efficient numerical computing in Python, we typically use libraries like\r\nNumPy that do expensive operations such as matrix multiplication outside Python,\r\nusing highly efficient code implemented in another language.\r\nUnfortunately, there can still be a lot of overhead from switching back to\r\nPython every operation. This overhead is especially bad if you want to run\r\ncomputations on GPUs or in a distributed manner, where there can be a high cost\r\nto transferring data.\r\n\r\nTensorFlow also does its heavy lifting outside python,\r\nbut it takes things a step further to avoid this overhead.\r\nInstead of running a single expensive operation independently\r\nfrom Python, TensorFlow lets us describe a graph of interacting operations that\r\nrun entirely outside Python. (Approaches like this can be seen in a few\r\nmachine learning libraries.)\r\n\r\nTo use TensorFlow, we need to import it.\r\n\r\n```python\r\nimport tensorflow as tf\r\n```\r\n\r\nWe describe these interacting operations by manipulating symbolic variables.\r\nLet's create one:\r\n\r\n```python\r\nx = tf.placeholder(\"float\", [None, 784])\r\n```\r\n\r\n`x` isn't a specific value. It's a `placeholder`, a value that we'll input when\r\nwe ask TensorFlow to run a computation. We want to be able to input any number\r\nof MNIST images, each flattened into a 784-dimensional vector. We represent\r\nthis as a 2d tensor of floating point numbers, with a shape `[None, 784]`.\r\n(Here `None` means that a dimension can be of any length.)\r\n\r\nWe also need the weights and biases for our model. We could imagine treating\r\nthese like additional inputs, but TensorFlow has an even better way to handle\r\nit: `Variable`.\r\nA `Variable` is a modifiable tensor that lives in TensorFlow's graph of\r\ninteracting\r\noperations. It can be used and even modified by the computation. For machine\r\nlearning applications, one generally has the model parameters be `Variable`s.\r\n\r\n```python\r\nW = tf.Variable(tf.zeros([784,10]))\r\nb = tf.Variable(tf.zeros([10]))\r\n```\r\n\r\nWe create these `Variable`s by giving `tf.Variable` the initial value of the\r\n`Variable`: in this case, we initialize both `W` and `b` as tensors full of\r\nzeros. Since we are going to learn `W` and `b`, it doesn't matter very much\r\nwhat they initially are.\r\n\r\nNotice that `W` has a shape of [784, 10] because we want to multiply the\r\n784-dimensional image vectors by it to produce 10-dimensional vectors of\r\nevidence for the difference classes. `b` has a shape of [10] so we can add it\r\nto the output.\r\n\r\nWe can now implement our model. It only takes one line!\r\n\r\n```python\r\ny = tf.nn.softmax(tf.matmul(x,W) + b)\r\n```\r\n\r\nFirst, we multiply `x` by `W` with the expression `tf.matmul(x,W)`. This is\r\nflipped from when we multiplied them in our equation, where we had \\\\(Wx\\\\), as a\r\nsmall trick\r\nto deal with `x` being a 2D tensor with multiple inputs. We then add `b`, and\r\nfinally apply `tf.nn.softmax`.\r\n\r\nThat's it. It only took us one line to define our model, after a couple short\r\nlines of setup. That isn't because TensorFlow is designed to make a softmax\r\nregression particularly easy: it's just a very flexible way to describe many\r\nkinds of numerical computations, from machine learning models to physics\r\nsimulations. And once defined, our model can be run on different devices:\r\nyour computer's CPU, GPUs, and even phones!\r\n\r\n\r\n## Training <a class=\"md-anchor\" id=\"AUTOGENERATED-training\"></a>\r\n\r\nIn order to train our model, we need to define what it means for the  model to\r\nbe good. Well, actually, in machine learning we typically define what it means\r\nfor a model to be bad, called the cost or loss, and then try to minimize how bad\r\nit is. But the two are equivalent.\r\n\r\nOne very common, very nice cost function is \"cross-entropy.\" Surprisingly,\r\ncross-entropy arises from thinking about information compressing codes in\r\ninformation theory but it winds up being an important idea in lots of areas,\r\nfrom gambling to machine learning. It's defined:\r\n\r\n$$H_{y'}(y) = -\\sum_i y'_i \\log(y_i)$$\r\n\r\nWhere \\\\(y\\\\) is our predicted probability distribution, and \\\\(y'\\\\) is the true\r\ndistribution (the one-hot vector we'll input).  In some rough sense, the\r\ncross-entropy is measuring how inefficient our predictions are for describing\r\nthe truth. Going into more detail about cross-entropy is beyond the scope of\r\nthis tutorial, but it's well worth\r\n[understanding](http://colah.github.io/posts/2015-09-Visual-Information/).\r\n\r\nTo implement cross-entropy we need to first add a new placeholder to input\r\nthe correct answers:\r\n\r\n```python\r\ny_ = tf.placeholder(\"float\", [None,10])\r\n```\r\n\r\nThen we can implement the cross-entropy, \\\\(-\\sum y'\\log(y)\\\\):\r\n\r\n```python\r\ncross_entropy = -tf.reduce_sum(y_*tf.log(y))\r\n```\r\n\r\nFirst, `tf.log` computes the logarithm of each element of `y`. Next, we multiply\r\neach element of `y_` with the corresponding element of `tf.log(y)`. Finally,\r\n`tf.reduce_sum` adds all the elements of the tensor. (Note that this isn't\r\njust the cross-entropy of the truth with a single prediction, but the sum of the\r\ncross-entropies for all 100 images we looked at. How well we are doing on 100\r\ndata points is a much better description of how good our model is than a single\r\ndata point.)\r\n\r\nNow that we know what we want our model to do, it's very easy to have TensorFlow\r\ntrain it to do so.\r\nBecause TensorFlow knows the entire graph of your computations, it\r\ncan automatically use the [backpropagation\r\nalgorithm](http://colah.github.io/posts/2015-08-Backprop/)\r\nto efficiently determine how your variables affect the cost you ask it minimize.\r\nThen it can apply your choice of optimization algorithm to modify the variables\r\nand reduce the cost.\r\n\r\n```python\r\ntrain_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)\r\n```\r\n\r\nIn this case, we ask TensorFlow to minimize `cross_entropy` using the gradient\r\ndescent algorithm with a learning rate of 0.01. Gradient descent is a simple\r\nprocedure, where TensorFlow simply shifts each variable a little bit in the\r\ndirection that reduces the cost. But TensorFlow also provides\r\n[many other optimization algorithms]\r\n(../../../api_docs/python/train.md#optimizers): using one is as simple as\r\ntweaking one line.\r\n\r\nWhat TensorFlow actually does here, behind the scenes, is it adds new operations\r\nto your graph which\r\nimplement backpropagation and gradient descent. Then it gives you back a\r\nsingle operation which, when run, will do a step of gradient descent training,\r\nslightly tweaking your variables to reduce the cost.\r\n\r\nNow we have our model set up to train. One last thing before we launch it,\r\nwe have to add an operation to initialize the variables we created:\r\n\r\n```python\r\ninit = tf.initialize_all_variables()\r\n```\r\n\r\nWe can now launch the model in a `Session`, and run the operation that\r\ninitializes the variables:\r\n\r\n```python\r\nsess = tf.Session()\r\nsess.run(init)\r\n```\r\n\r\nLet's train -- we'll run the training step 1000 times!\r\n\r\n```python\r\nfor i in range(1000):\r\n  batch_xs, batch_ys = mnist.train.next_batch(100)\r\n  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})\r\n```\r\n\r\nEach step of the loop, we get a \"batch\" of one hundred random data points from\r\nour training set. We run `train_step` feeding in the batches data to replace\r\nthe `placeholder`s.\r\n\r\nUsing small batches of random data is called stochastic training -- in\r\nthis case, stochastic gradient descent. Ideally, we'd like to use all our data\r\nfor every step of training because that would give us a better sense of what\r\nwe should be doing, but that's expensive. So, instead, we use a different subset\r\nevery time. Doing this is cheap and has much of the same benefit.\r\n\r\n\r\n\r\n## Evaluating Our Model <a class=\"md-anchor\" id=\"AUTOGENERATED-evaluating-our-model\"></a>\r\n\r\nHow well does our model do?\r\n\r\nWell, first let's figure out where we predicted the correct label. `tf.argmax`\r\nis an extremely useful function which gives you the index of the highest entry\r\nin a tensor along some axis. For example, `tf.argmax(y,1)` is the label our\r\nmodel thinks is most likely for each input, while `tf.argmax(y_,1)` is the\r\ncorrect label. We can use `tf.equal` to check if our prediction matches the\r\ntruth.\r\n\r\n```python\r\ncorrect_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\r\n```\r\n\r\nThat gives us a list of booleans. To determine what fraction are correct, we\r\ncast to floating point numbers and then take the mean. For example,\r\n`[True, False, True, True]` would become `[1,0,1,1]` which would become `0.75`.\r\n\r\n```python\r\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\r\n```\r\n\r\nFinally, we ask for our accuracy on our test data.\r\n\r\n```python\r\nprint sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})\r\n```\r\n\r\nThis should be about 91%.\r\n\r\nIs that good? Well, not really. In fact, it's pretty bad. This is because we're\r\nusing a very simple model. With some small changes, we can get to\r\n97%. The best models can get to over 99.7% accuracy! (For more information, have\r\na look at this\r\n[list of results](http://rodrigob.github.io/are_we_there_yet/build/classification_datasets_results.html).)\r\n\r\nWhat matters is that we learned from this model. Still, if you're feeling a bit\r\ndown about these results, check out [the next tutorial](../../../tutorials/overview.md) where we\r\ndo a lot better, and learn how to build more sophisticated models using\r\nTensorFlow!\r\n"
  },
  {
    "path": "SOURCE/tutorials/mnist/download/index.md",
    "content": "# MNIST Data Download <a class=\"md-anchor\" id=\"AUTOGENERATED-mnist-data-download\"></a>\r\n\r\nCode: [tensorflow/g3doc/tutorials/mnist/](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/)\r\n\r\nThe goal of this tutorial is to show how to download the dataset files required\r\nfor handwritten digit classification using the (classic) MNIST data set.\r\n\r\n## Tutorial Files <a class=\"md-anchor\" id=\"AUTOGENERATED-tutorial-files\"></a>\r\n\r\nThis tutorial references the following files:\r\n\r\nFile | Purpose\r\n--- | ---\r\n[`input_data.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/input_data.py) | The code to download the MNIST dataset for training and evaluation.\r\n\r\n## Prepare the Data <a class=\"md-anchor\" id=\"AUTOGENERATED-prepare-the-data\"></a>\r\n\r\nMNIST is a classic problem in machine learning. The problem is to look at\r\ngreyscale 28x28 pixel images of handwritten digits and determine which digit\r\nthe image represents, for all the digits from zero to nine.\r\n\r\n![MNIST Digits](../tf/mnist_digits.png \"MNIST Digits\")\r\n\r\nFor more information, refer to [Yann LeCun's MNIST page](http://yann.lecun.com/exdb/mnist/)\r\nor [Chris Olah's visualizations of MNIST](http://colah.github.io/posts/2014-10-Visualizing-MNIST/).\r\n\r\n### Download <a class=\"md-anchor\" id=\"AUTOGENERATED-download\"></a>\r\n\r\n[Yann LeCun's MNIST page](http://yann.lecun.com/exdb/mnist/)\r\nalso hosts the training and test data for download.\r\n\r\nFile | Purpose\r\n--- | ---\r\n[`train-images-idx3-ubyte.gz`](http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz) | training set images - 55000 training images, 5000 validation images\r\n[`train-labels-idx1-ubyte.gz`](http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz) | training set labels matching the images\r\n[`t10k-images-idx3-ubyte.gz`](http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz) | test set images - 10000 images\r\n[`t10k-labels-idx1-ubyte.gz`](http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz) | test set labels matching the images\r\n\r\nIn the `input_data.py` file, the `maybe_download()` function will ensure these\r\nfiles are downloaded into a local data folder for training.\r\n\r\nThe folder name is specified in a flag variable at the top of the\r\n`fully_connected_feed.py` file and may be changed to fit your needs.\r\n\r\n### Unpack and Reshape <a class=\"md-anchor\" id=\"AUTOGENERATED-unpack-and-reshape\"></a>\r\n\r\nThe files themselves are not in any standard image format and are manually\r\nunpacked (following the instructions available at the website) by the\r\n`extract_images()` and `extract_labels()` functions in `input_data.py`.\r\n\r\nThe image data is extracted into a 2d tensor of: `[image index, pixel index]`\r\nwhere each entry is the intensity value of a specific pixel in a specific\r\nimage, rescaled from `[0, 255]` to `[-0.5, 0.5]`.  The \"image index\" corresponds\r\nto an image in the dataset, counting up from zero to the size of the dataset.\r\nAnd the \"pixel index\" corresponds to a specific pixel in that image, ranging\r\nfrom zero to the number of pixels in the image.\r\n\r\nThe 60000 examples in the `train-*` files are then split into 55000 examples\r\nfor training and 5000 examples for validation. For all of the 28x28\r\npixel greyscale images in the datasets the image size is 784 and so the output\r\ntensor for the training set images is of shape `[55000, 784]`.\r\n\r\nThe label data is extracted into a 1d tensor of: `[image index]`\r\nwith the class identifier for each example as the value. For the training set\r\nlabels, this would then be of shape `[55000]`.\r\n\r\n### DataSet Object <a class=\"md-anchor\" id=\"AUTOGENERATED-dataset-object\"></a>\r\n\r\nThe underlying code will download, unpack, and reshape images and labels for\r\nthe following datasets:\r\n\r\nDataset | Purpose\r\n--- | ---\r\n`data_sets.train` | 55000 images and labels, for primary training.\r\n`data_sets.validation` | 5000 images and labels, for iterative validation of training accuracy.\r\n`data_sets.test` | 10000 images and labels, for final testing of trained accuracy.\r\n\r\nThe `read_data_sets()` function will return a dictionary with a `DataSet`\r\ninstance for each of these three sets of data.  The `DataSet.next_batch()`\r\nmethod can be used to fetch a tuple consisting of `batch_size` lists of images\r\nand labels to be fed into the running TensorFlow session.\r\n\r\n```python\r\nimages_feed, labels_feed = data_set.next_batch(FLAGS.batch_size)\r\n```\r\n"
  },
  {
    "path": "SOURCE/tutorials/mnist/fully_connected_feed.py",
    "content": "\"\"\"Trains and Evaluates the MNIST network using a feed dictionary.\r\n\r\nTensorFlow install instructions:\r\nhttps://tensorflow.org/get_started/os_setup.html\r\n\r\nMNIST tutorial:\r\nhttps://tensorflow.org/tutorials/mnist/tf/index.html\r\n\r\n\"\"\"\r\nfrom __future__ import print_function\r\n# pylint: disable=missing-docstring\r\nimport os.path\r\nimport time\r\n\r\nimport tensorflow.python.platform\r\nimport numpy\r\nimport tensorflow as tf\r\n\r\nfrom tensorflow.g3doc.tutorials.mnist import input_data\r\nfrom tensorflow.g3doc.tutorials.mnist import mnist\r\n\r\n\r\n# Basic model parameters as external flags.\r\nflags = tf.app.flags\r\nFLAGS = flags.FLAGS\r\nflags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')\r\nflags.DEFINE_integer('max_steps', 2000, 'Number of steps to run trainer.')\r\nflags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.')\r\nflags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.')\r\nflags.DEFINE_integer('batch_size', 100, 'Batch size.  '\r\n                     'Must divide evenly into the dataset sizes.')\r\nflags.DEFINE_string('train_dir', 'data', 'Directory to put the training data.')\r\nflags.DEFINE_boolean('fake_data', False, 'If true, uses fake data '\r\n                     'for unit testing.')\r\n\r\n\r\ndef placeholder_inputs(batch_size):\r\n  \"\"\"Generate placeholder variables to represent the the input tensors.\r\n\r\n  These placeholders are used as inputs by the rest of the model building\r\n  code and will be fed from the downloaded data in the .run() loop, below.\r\n\r\n  Args:\r\n    batch_size: The batch size will be baked into both placeholders.\r\n\r\n  Returns:\r\n    images_placeholder: Images placeholder.\r\n    labels_placeholder: Labels placeholder.\r\n  \"\"\"\r\n  # Note that the shapes of the placeholders match the shapes of the full\r\n  # image and label tensors, except the first dimension is now batch_size\r\n  # rather than the full size of the train or test data sets.\r\n  images_placeholder = tf.placeholder(tf.float32, shape=(batch_size,\r\n                                                         mnist.IMAGE_PIXELS))\r\n  labels_placeholder = tf.placeholder(tf.int32, shape=(batch_size))\r\n  return images_placeholder, labels_placeholder\r\n\r\n\r\ndef fill_feed_dict(data_set, images_pl, labels_pl):\r\n  \"\"\"Fills the feed_dict for training the given step.\r\n\r\n  A feed_dict takes the form of:\r\n  feed_dict = {\r\n      <placeholder>: <tensor of values to be passed for placeholder>,\r\n      ....\r\n  }\r\n\r\n  Args:\r\n    data_set: The set of images and labels, from input_data.read_data_sets()\r\n    images_pl: The images placeholder, from placeholder_inputs().\r\n    labels_pl: The labels placeholder, from placeholder_inputs().\r\n\r\n  Returns:\r\n    feed_dict: The feed dictionary mapping from placeholders to values.\r\n  \"\"\"\r\n  # Create the feed_dict for the placeholders filled with the next\r\n  # `batch size ` examples.\r\n  images_feed, labels_feed = data_set.next_batch(FLAGS.batch_size,\r\n                                                 FLAGS.fake_data)\r\n  feed_dict = {\r\n      images_pl: images_feed,\r\n      labels_pl: labels_feed,\r\n  }\r\n  return feed_dict\r\n\r\n\r\ndef do_eval(sess,\r\n            eval_correct,\r\n            images_placeholder,\r\n            labels_placeholder,\r\n            data_set):\r\n  \"\"\"Runs one evaluation against the full epoch of data.\r\n\r\n  Args:\r\n    sess: The session in which the model has been trained.\r\n    eval_correct: The Tensor that returns the number of correct predictions.\r\n    images_placeholder: The images placeholder.\r\n    labels_placeholder: The labels placeholder.\r\n    data_set: The set of images and labels to evaluate, from\r\n      input_data.read_data_sets().\r\n  \"\"\"\r\n  # And run one epoch of eval.\r\n  true_count = 0  # Counts the number of correct predictions.\r\n  steps_per_epoch = int(data_set.num_examples / FLAGS.batch_size)\r\n  num_examples = steps_per_epoch * FLAGS.batch_size\r\n  for step in xrange(steps_per_epoch):\r\n    feed_dict = fill_feed_dict(data_set,\r\n                               images_placeholder,\r\n                               labels_placeholder)\r\n    true_count += sess.run(eval_correct, feed_dict=feed_dict)\r\n  precision = float(true_count) / float(num_examples)\r\n  print('  Num examples: %d  Num correct: %d  Precision @ 1: %0.04f' %\r\n        (num_examples, true_count, precision))\r\n\r\n\r\ndef run_training():\r\n  \"\"\"Train MNIST for a number of steps.\"\"\"\r\n  # Get the sets of images and labels for training, validation, and\r\n  # test on MNIST.\r\n  data_sets = input_data.read_data_sets(FLAGS.train_dir, FLAGS.fake_data)\r\n\r\n  # Tell TensorFlow that the model will be built into the default Graph.\r\n  with tf.Graph().as_default():\r\n    # Generate placeholders for the images and labels.\r\n    images_placeholder, labels_placeholder = placeholder_inputs(\r\n        FLAGS.batch_size)\r\n\r\n    # Build a Graph that computes predictions from the inference model.\r\n    logits = mnist.inference(images_placeholder,\r\n                             FLAGS.hidden1,\r\n                             FLAGS.hidden2)\r\n\r\n    # Add to the Graph the Ops for loss calculation.\r\n    loss = mnist.loss(logits, labels_placeholder)\r\n\r\n    # Add to the Graph the Ops that calculate and apply gradients.\r\n    train_op = mnist.training(loss, FLAGS.learning_rate)\r\n\r\n    # Add the Op to compare the logits to the labels during evaluation.\r\n    eval_correct = mnist.evaluation(logits, labels_placeholder)\r\n\r\n    # Build the summary operation based on the TF collection of Summaries.\r\n    summary_op = tf.merge_all_summaries()\r\n\r\n    # Create a saver for writing training checkpoints.\r\n    saver = tf.train.Saver()\r\n\r\n    # Create a session for running Ops on the Graph.\r\n    sess = tf.Session()\r\n\r\n    # Run the Op to initialize the variables.\r\n    init = tf.initialize_all_variables()\r\n    sess.run(init)\r\n\r\n    # Instantiate a SummaryWriter to output summaries and the Graph.\r\n    summary_writer = tf.train.SummaryWriter(FLAGS.train_dir,\r\n                                            graph_def=sess.graph_def)\r\n\r\n    # And then after everything is built, start the training loop.\r\n    for step in xrange(FLAGS.max_steps):\r\n      start_time = time.time()\r\n\r\n      # Fill a feed dictionary with the actual set of images and labels\r\n      # for this particular training step.\r\n      feed_dict = fill_feed_dict(data_sets.train,\r\n                                 images_placeholder,\r\n                                 labels_placeholder)\r\n\r\n      # Run one step of the model.  The return values are the activations\r\n      # from the `train_op` (which is discarded) and the `loss` Op.  To\r\n      # inspect the values of your Ops or variables, you may include them\r\n      # in the list passed to sess.run() and the value tensors will be\r\n      # returned in the tuple from the call.\r\n      _, loss_value = sess.run([train_op, loss],\r\n                               feed_dict=feed_dict)\r\n\r\n      duration = time.time() - start_time\r\n\r\n      # Write the summaries and print an overview fairly often.\r\n      if step % 100 == 0:\r\n        # Print status to stdout.\r\n        print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value, duration))\r\n        # Update the events file.\r\n        summary_str = sess.run(summary_op, feed_dict=feed_dict)\r\n        summary_writer.add_summary(summary_str, step)\r\n\r\n      # Save a checkpoint and evaluate the model periodically.\r\n      if (step + 1) % 1000 == 0 or (step + 1) == FLAGS.max_steps:\r\n        saver.save(sess, FLAGS.train_dir, global_step=step)\r\n        # Evaluate against the training set.\r\n        print('Training Data Eval:')\r\n        do_eval(sess,\r\n                eval_correct,\r\n                images_placeholder,\r\n                labels_placeholder,\r\n                data_sets.train)\r\n        # Evaluate against the validation set.\r\n        print('Validation Data Eval:')\r\n        do_eval(sess,\r\n                eval_correct,\r\n                images_placeholder,\r\n                labels_placeholder,\r\n                data_sets.validation)\r\n        # Evaluate against the test set.\r\n        print('Test Data Eval:')\r\n        do_eval(sess,\r\n                eval_correct,\r\n                images_placeholder,\r\n                labels_placeholder,\r\n                data_sets.test)\r\n\r\n\r\ndef main(_):\r\n  run_training()\r\n\r\n\r\nif __name__ == '__main__':\r\n  tf.app.run()\r\n"
  },
  {
    "path": "SOURCE/tutorials/mnist/input_data.py",
    "content": "\"\"\"Functions for downloading and reading MNIST data.\"\"\"\r\nfrom __future__ import print_function\r\nimport gzip\r\nimport os\r\nimport urllib\r\n\r\nimport numpy\r\n\r\nSOURCE_URL = 'http://yann.lecun.com/exdb/mnist/'\r\n\r\n\r\ndef maybe_download(filename, work_directory):\r\n  \"\"\"Download the data from Yann's website, unless it's already here.\"\"\"\r\n  if not os.path.exists(work_directory):\r\n    os.mkdir(work_directory)\r\n  filepath = os.path.join(work_directory, filename)\r\n  if not os.path.exists(filepath):\r\n    filepath, _ = urllib.urlretrieve(SOURCE_URL + filename, filepath)\r\n    statinfo = os.stat(filepath)\r\n    print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')\r\n  return filepath\r\n\r\n\r\ndef _read32(bytestream):\r\n  dt = numpy.dtype(numpy.uint32).newbyteorder('>')\r\n  return numpy.frombuffer(bytestream.read(4), dtype=dt)\r\n\r\n\r\ndef extract_images(filename):\r\n  \"\"\"Extract the images into a 4D uint8 numpy array [index, y, x, depth].\"\"\"\r\n  print('Extracting', filename)\r\n  with gzip.open(filename) as bytestream:\r\n    magic = _read32(bytestream)\r\n    if magic != 2051:\r\n      raise ValueError(\r\n          'Invalid magic number %d in MNIST image file: %s' %\r\n          (magic, filename))\r\n    num_images = _read32(bytestream)\r\n    rows = _read32(bytestream)\r\n    cols = _read32(bytestream)\r\n    buf = bytestream.read(rows * cols * num_images)\r\n    data = numpy.frombuffer(buf, dtype=numpy.uint8)\r\n    data = data.reshape(num_images, rows, cols, 1)\r\n    return data\r\n\r\n\r\ndef dense_to_one_hot(labels_dense, num_classes=10):\r\n  \"\"\"Convert class labels from scalars to one-hot vectors.\"\"\"\r\n  num_labels = labels_dense.shape[0]\r\n  index_offset = numpy.arange(num_labels) * num_classes\r\n  labels_one_hot = numpy.zeros((num_labels, num_classes))\r\n  labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1\r\n  return labels_one_hot\r\n\r\n\r\ndef extract_labels(filename, one_hot=False):\r\n  \"\"\"Extract the labels into a 1D uint8 numpy array [index].\"\"\"\r\n  print('Extracting', filename)\r\n  with gzip.open(filename) as bytestream:\r\n    magic = _read32(bytestream)\r\n    if magic != 2049:\r\n      raise ValueError(\r\n          'Invalid magic number %d in MNIST label file: %s' %\r\n          (magic, filename))\r\n    num_items = _read32(bytestream)\r\n    buf = bytestream.read(num_items)\r\n    labels = numpy.frombuffer(buf, dtype=numpy.uint8)\r\n    if one_hot:\r\n      return dense_to_one_hot(labels)\r\n    return labels\r\n\r\n\r\nclass DataSet(object):\r\n\r\n  def __init__(self, images, labels, fake_data=False):\r\n    if fake_data:\r\n      self._num_examples = 10000\r\n    else:\r\n      assert images.shape[0] == labels.shape[0], (\r\n          \"images.shape: %s labels.shape: %s\" % (images.shape,\r\n                                                 labels.shape))\r\n      self._num_examples = images.shape[0]\r\n\r\n      # Convert shape from [num examples, rows, columns, depth]\r\n      # to [num examples, rows*columns] (assuming depth == 1)\r\n      assert images.shape[3] == 1\r\n      images = images.reshape(images.shape[0],\r\n                              images.shape[1] * images.shape[2])\r\n      # Convert from [0, 255] -> [0.0, 1.0].\r\n      images = images.astype(numpy.float32)\r\n      images = numpy.multiply(images, 1.0 / 255.0)\r\n    self._images = images\r\n    self._labels = labels\r\n    self._epochs_completed = 0\r\n    self._index_in_epoch = 0\r\n\r\n  @property\r\n  def images(self):\r\n    return self._images\r\n\r\n  @property\r\n  def labels(self):\r\n    return self._labels\r\n\r\n  @property\r\n  def num_examples(self):\r\n    return self._num_examples\r\n\r\n  @property\r\n  def epochs_completed(self):\r\n    return self._epochs_completed\r\n\r\n  def next_batch(self, batch_size, fake_data=False):\r\n    \"\"\"Return the next `batch_size` examples from this data set.\"\"\"\r\n    if fake_data:\r\n      fake_image = [1.0 for _ in xrange(784)]\r\n      fake_label = 0\r\n      return [fake_image for _ in xrange(batch_size)], [\r\n          fake_label for _ in xrange(batch_size)]\r\n    start = self._index_in_epoch\r\n    self._index_in_epoch += batch_size\r\n    if self._index_in_epoch > self._num_examples:\r\n      # Finished epoch\r\n      self._epochs_completed += 1\r\n      # Shuffle the data\r\n      perm = numpy.arange(self._num_examples)\r\n      numpy.random.shuffle(perm)\r\n      self._images = self._images[perm]\r\n      self._labels = self._labels[perm]\r\n      # Start next epoch\r\n      start = 0\r\n      self._index_in_epoch = batch_size\r\n      assert batch_size <= self._num_examples\r\n    end = self._index_in_epoch\r\n    return self._images[start:end], self._labels[start:end]\r\n\r\n\r\ndef read_data_sets(train_dir, fake_data=False, one_hot=False):\r\n  class DataSets(object):\r\n    pass\r\n  data_sets = DataSets()\r\n\r\n  if fake_data:\r\n    data_sets.train = DataSet([], [], fake_data=True)\r\n    data_sets.validation = DataSet([], [], fake_data=True)\r\n    data_sets.test = DataSet([], [], fake_data=True)\r\n    return data_sets\r\n\r\n  TRAIN_IMAGES = 'train-images-idx3-ubyte.gz'\r\n  TRAIN_LABELS = 'train-labels-idx1-ubyte.gz'\r\n  TEST_IMAGES = 't10k-images-idx3-ubyte.gz'\r\n  TEST_LABELS = 't10k-labels-idx1-ubyte.gz'\r\n  VALIDATION_SIZE = 5000\r\n\r\n  local_file = maybe_download(TRAIN_IMAGES, train_dir)\r\n  train_images = extract_images(local_file)\r\n\r\n  local_file = maybe_download(TRAIN_LABELS, train_dir)\r\n  train_labels = extract_labels(local_file, one_hot=one_hot)\r\n\r\n  local_file = maybe_download(TEST_IMAGES, train_dir)\r\n  test_images = extract_images(local_file)\r\n\r\n  local_file = maybe_download(TEST_LABELS, train_dir)\r\n  test_labels = extract_labels(local_file, one_hot=one_hot)\r\n\r\n  validation_images = train_images[:VALIDATION_SIZE]\r\n  validation_labels = train_labels[:VALIDATION_SIZE]\r\n  train_images = train_images[VALIDATION_SIZE:]\r\n  train_labels = train_labels[VALIDATION_SIZE:]\r\n\r\n  data_sets.train = DataSet(train_images, train_labels)\r\n  data_sets.validation = DataSet(validation_images, validation_labels)\r\n  data_sets.test = DataSet(test_images, test_labels)\r\n\r\n  return data_sets\r\n"
  },
  {
    "path": "SOURCE/tutorials/mnist/mnist.py",
    "content": "\"\"\"Builds the MNIST network.\r\n\r\nImplements the inference/loss/training pattern for model building.\r\n\r\n1. inference() - Builds the model as far as is required for running the network\r\nforward to make predictions.\r\n2. loss() - Adds to the inference model the layers required to generate loss.\r\n3. training() - Adds to the loss model the Ops required to generate and\r\napply gradients.\r\n\r\nThis file is used by the various \"fully_connected_*.py\" files and not meant to\r\nbe run.\r\n\r\nTensorFlow install instructions:\r\nhttps://tensorflow.org/get_started/os_setup.html\r\n\r\nMNIST tutorial:\r\nhttps://tensorflow.org/tutorials/mnist/tf/index.html\r\n\"\"\"\r\nimport math\r\n\r\nimport tensorflow.python.platform\r\nimport tensorflow as tf\r\n\r\n# The MNIST dataset has 10 classes, representing the digits 0 through 9.\r\nNUM_CLASSES = 10\r\n\r\n# The MNIST images are always 28x28 pixels.\r\nIMAGE_SIZE = 28\r\nIMAGE_PIXELS = IMAGE_SIZE * IMAGE_SIZE\r\n\r\n\r\ndef inference(images, hidden1_units, hidden2_units):\r\n    \"\"\"Build the MNIST model up to where it may be used for inference.\r\n\r\n    Args:\r\n      images: Images placeholder, from inputs().\r\n      hidden1: Size of the first hidden layer.\r\n      hidden2: Size of the second hidden layer.\r\n\r\n    Returns:\r\n      softmax_linear: Output tensor with the computed logits.\r\n    \"\"\"\r\n    # Hidden 1\r\n    with tf.name_scope('hidden1') as scope:\r\n        weights = tf.Variable(\r\n            tf.truncated_normal([IMAGE_PIXELS, hidden1_units],\r\n                                stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))),\r\n            name='weights')\r\n        biases = tf.Variable(tf.zeros([hidden1_units]),\r\n                             name='biases')\r\n        hidden1 = tf.nn.relu(tf.matmul(images, weights) + biases)\r\n    # Hidden 2\r\n    with tf.name_scope('hidden2') as scope:\r\n        weights = tf.Variable(\r\n            tf.truncated_normal([hidden1_units, hidden2_units],\r\n                                stddev=1.0 / math.sqrt(float(hidden1_units))),\r\n            name='weights')\r\n        biases = tf.Variable(tf.zeros([hidden2_units]),\r\n                             name='biases')\r\n        hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases)\r\n    # Linear\r\n    with tf.name_scope('softmax_linear') as scope:\r\n        weights = tf.Variable(\r\n            tf.truncated_normal([hidden2_units, NUM_CLASSES],\r\n                                stddev=1.0 / math.sqrt(float(hidden2_units))),\r\n            name='weights')\r\n        biases = tf.Variable(tf.zeros([NUM_CLASSES]),\r\n                             name='biases')\r\n        logits = tf.matmul(hidden2, weights) + biases\r\n    return logits\r\n\r\n\r\ndef loss(logits, labels):\r\n    \"\"\"Calculates the loss from the logits and the labels.\r\n\r\n    Args:\r\n      logits: Logits tensor, float - [batch_size, NUM_CLASSES].\r\n      labels: Labels tensor, int32 - [batch_size].\r\n\r\n    Returns:\r\n      loss: Loss tensor of type float.\r\n    \"\"\"\r\n    # Convert from sparse integer labels in the range [0, NUM_CLASSSES)\r\n    # to 1-hot dense float vectors (that is we will have batch_size vectors,\r\n    # each with NUM_CLASSES values, all of which are 0.0 except there will\r\n    # be a 1.0 in the entry corresponding to the label).\r\n    batch_size = tf.size(labels)\r\n    labels = tf.expand_dims(labels, 1)\r\n    indices = tf.expand_dims(tf.range(0, batch_size, 1), 1)\r\n    concated = tf.concat(1, [indices, labels])\r\n    onehot_labels = tf.sparse_to_dense(\r\n        concated, tf.pack([batch_size, NUM_CLASSES]), 1.0, 0.0)\r\n    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits,\r\n                                                            onehot_labels,\r\n                                                            name='xentropy')\r\n    loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')\r\n    return loss\r\n\r\n\r\ndef training(loss, learning_rate):\r\n    \"\"\"Sets up the training Ops.\r\n\r\n    Creates a summarizer to track the loss over time in TensorBoard.\r\n\r\n    Creates an optimizer and applies the gradients to all trainable variables.\r\n\r\n    The Op returned by this function is what must be passed to the\r\n    `sess.run()` call to cause the model to train.\r\n\r\n    Args:\r\n      loss: Loss tensor, from loss().\r\n      learning_rate: The learning rate to use for gradient descent.\r\n\r\n    Returns:\r\n      train_op: The Op for training.\r\n    \"\"\"\r\n    # Add a scalar summary for the snapshot loss.\r\n    tf.scalar_summary(loss.op.name, loss)\r\n    # Create the gradient descent optimizer with the given learning rate.\r\n    optimizer = tf.train.GradientDescentOptimizer(learning_rate)\r\n    # Create a variable to track the global step.\r\n    global_step = tf.Variable(0, name='global_step', trainable=False)\r\n    # Use the optimizer to apply the gradients that minimize the loss\r\n    # (and also increment the global step counter) as a single training step.\r\n    train_op = optimizer.minimize(loss, global_step=global_step)\r\n    return train_op\r\n\r\n\r\ndef evaluation(logits, labels):\r\n    \"\"\"Evaluate the quality of the logits at predicting the label.\r\n\r\n    Args:\r\n      logits: Logits tensor, float - [batch_size, NUM_CLASSES].\r\n      labels: Labels tensor, int32 - [batch_size], with values in the\r\n        range [0, NUM_CLASSES).\r\n\r\n    Returns:\r\n      A scalar int32 tensor with the number of examples (out of batch_size)\r\n      that were predicted correctly.\r\n    \"\"\"\r\n    # For a classifier model, we can use the in_top_k Op.\r\n    # It returns a bool tensor with shape [batch_size] that is true for\r\n    # the examples where the label's is was in the top k (here k=1)\r\n    # of all logits for that example.\r\n    correct = tf.nn.in_top_k(logits, labels, 1)\r\n    # Return the number of true entries.\r\n    return tf.reduce_sum(tf.cast(correct, tf.int32))\r\n"
  },
  {
    "path": "SOURCE/tutorials/mnist/mnist_softmax.py",
    "content": "\"\"\"A very simple MNIST classifer.\r\n\r\nSee extensive documentation at ??????? (insert public URL)\r\n\"\"\"\r\nfrom __future__ import print_function\r\n\r\n# Import data\r\nimport input_data\r\nmnist = input_data.read_data_sets(\"/tmp/data/\", one_hot=True)\r\n\r\nimport tensorflow as tf\r\nsess = tf.InteractiveSession()\r\n\r\n# Create the model\r\nx = tf.placeholder(\"float\", [None, 784])\r\nW = tf.Variable(tf.zeros([784,10]))\r\nb = tf.Variable(tf.zeros([10]))\r\n#y = tf.nn.softmax(tf.matmul(x,W) + b)  # this will be lead an error because of log(0)\r\ny = tf.nn.log_softmax(tf.matmul(x,W) + b)\r\n\r\n# Define loss and optimizer\r\ny_ = tf.placeholder(\"float\", [None,10])\r\n#cross_entropy = -tf.reduce_sum(y_*tf.log(y))\r\ncross_entropy = -tf.reduce_sum(y_*y)\r\ntrain_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)\r\n\r\n# Train\r\ntf.initialize_all_variables().run()\r\nfor i in range(1000):\r\n  batch_xs, batch_ys = mnist.train.next_batch(100)\r\n  train_step.run({x: batch_xs, y_: batch_ys})\r\n\r\n# Test trained model\r\ncorrect_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\r\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\r\nprint(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))\r\n"
  },
  {
    "path": "SOURCE/tutorials/mnist/pros/index.md",
    "content": "#  Deep MNIST for Experts <a class=\"md-anchor\" id=\"AUTOGENERATED-deep-mnist-for-experts\"></a>\r\n\r\nTensorFlow is a powerful library for doing large-scale numerical computation.\r\nOne of the tasks at which it excels is implementing and training deep neural\r\nnetworks.\r\nIn this tutorial we will learn the basic building blocks of a TensorFlow model\r\nwhile constructing a deep convolutional MNIST classifier.\r\n\r\n*This introduction assumes familiarity with neural networks and the MNIST\r\ndataset. If you don't have\r\na background with them, check out the\r\n[introduction for beginners](../../../tutorials/mnist/beginners/index.md).*\r\n\r\n## Setup <a class=\"md-anchor\" id=\"AUTOGENERATED-setup\"></a>\r\n\r\nBefore we create our model, we will first load the MNIST dataset, and start a\r\nTensorFlow session.\r\n\r\n### Load MNIST Data <a class=\"md-anchor\" id=\"AUTOGENERATED-load-mnist-data\"></a>\r\n\r\nFor your convenience, we've included\r\n[a script](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/input_data.py)\r\nwhich automatically downloads and imports the MNIST dataset. It will create a\r\ndirectory `'MNIST_data'` in which to store the data files.\r\n\r\n```python\r\nimport input_data\r\nmnist = input_data.read_data_sets('MNIST_data', one_hot=True)\r\n```\r\n\r\nHere `mnist` is a lightweight class which stores the training, validation, and\r\ntesting sets as NumPy arrays.\r\nIt also provides a function for iterating through data minibatches, which we\r\nwill use below.\r\n\r\n### Start TensorFlow InteractiveSession <a class=\"md-anchor\" id=\"AUTOGENERATED-start-tensorflow-interactivesession\"></a>\r\n\r\nTensorflow relies on a highly efficient C++ backend to do its computation. The\r\nconnection to this backend is called a session.  The common usage for TensorFlow\r\nprograms is to first create a graph and then launch it in a session.\r\n\r\nHere we instead use the convenience `InteractiveSession` class, which\r\nmakes TensorFlow more flexible about how you\r\nstructure your code.\r\nIt allows you to interleave operations which build a\r\n[computation graph](../../../get_started/basic_usage.md#the-computation-graph)\r\nwith ones that run the graph.\r\nThis is particularly convenient when working in interactive contexts like\r\niPython.\r\nIf you are not using an `InteractiveSession`, then you should build\r\nthe entire computation graph before starting a session and [launching the\r\ngraph](../../../get_started/basic_usage.md#launching-the-graph-in-a-session).\r\n\r\n```python\r\nimport tensorflow as tf\r\nsess = tf.InteractiveSession()\r\n```\r\n\r\n#### Computation Graph <a class=\"md-anchor\" id=\"AUTOGENERATED-computation-graph\"></a>\r\n\r\nTo do efficient numerical computing in Python, we typically use libraries like\r\nNumPy that do expensive operations such as matrix multiplication outside Python,\r\nusing highly efficient code implemented in another language.\r\nUnfortunately, there can still be a lot of overhead from switching back to\r\nPython every operation. This overhead is especially bad if you want to run\r\ncomputations on GPUs or in a distributed manner, where there can be a high cost\r\nto transferring data.\r\n\r\nTensorFlow also does its heavy lifting outside Python,\r\nbut it takes things a step further to avoid this overhead.\r\nInstead of running a single expensive operation independently\r\nfrom Python, TensorFlow lets us describe a graph of interacting operations that\r\nrun entirely outside Python.\r\nThis approach is similar to that used in Theano or Torch.\r\n\r\nThe role of the Python code is therefore to build this external computation\r\ngraph, and to dictate which parts of the computation graph should be run. See\r\nthe\r\n[Computation Graph](../../../get_started/basic_usage.md#the-computation-graph)\r\nsection of\r\n[Basic Usage](../../../get_started/basic_usage.md)\r\nfor more detail.\r\n\r\n## Build a Softmax Regression Model <a class=\"md-anchor\" id=\"AUTOGENERATED-build-a-softmax-regression-model\"></a>\r\n\r\nIn this section we will build a softmax regression model with a single linear\r\nlayer. In the next section, we will extend this to the case of softmax\r\nregression with a multilayer convolutional network.\r\n\r\n### Placeholders <a class=\"md-anchor\" id=\"AUTOGENERATED-placeholders\"></a>\r\n\r\nWe start building the computation graph by creating nodes for the\r\ninput images and target output classes.\r\n\r\n```python\r\nx = tf.placeholder(\"float\", shape=[None, 784])\r\ny_ = tf.placeholder(\"float\", shape=[None, 10])\r\n```\r\n\r\nHere `x` and `y_` aren't specific values. Rather, they are each a `placeholder`\r\n-- a value that we'll input when we ask TensorFlow to run a computation.\r\n\r\nThe input images `x` will consist of a 2d tensor of floating point numbers.\r\nHere we assign it a `shape` of `[None, 784]`, where `784` is the dimensionality of\r\na single flattened MNIST image, and `None` indicates that the first dimension,\r\ncorresponding to the batch size, can be of any size.\r\nThe target output classes `y_` will also consist of a 2d tensor,\r\nwhere each row is a one-hot 10-dimensional vector indicating\r\nwhich digit class the corresponding MNIST image belongs to.\r\n\r\nThe `shape` argument to `placeholder` is optional, but it allows TensorFlow\r\nto automatically catch bugs stemming from inconsistent tensor shapes.\r\n\r\n### Variables <a class=\"md-anchor\" id=\"AUTOGENERATED-variables\"></a>\r\n\r\nWe now define the weights `W` and biases `b` for our model. We could imagine treating\r\nthese like additional inputs, but TensorFlow has an even better way to handle\r\nthem: `Variable`.\r\nA `Variable` is a value that lives in TensorFlow's computation graph.\r\nIt can be used and even modified by the computation. In machine\r\nlearning applications, one generally has the model paramaters be `Variable`s.\r\n\r\n```python\r\nW = tf.Variable(tf.zeros([784,10]))\r\nb = tf.Variable(tf.zeros([10]))\r\n```\r\n\r\nWe pass the initial value for each parameter in the call to `tf.Variable`.\r\nIn this case, we initialize both `W` and `b` as tensors full of\r\nzeros. `W` is a 784x10 matrix (because we have 784 input features\r\nand 10 outputs) and `b` is a 10-dimensional vector (because we have 10 classes).\r\n\r\nBefore `Variable`s can be used within a session, they must be initialized using\r\nthat session.\r\nThis step takes the initial values (in this case tensors full of zeros) that\r\nhave already been specified, and assigns them to each `Variable`. This can be\r\ndone for all `Variables` at once.\r\n\r\n```python\r\nsess.run(tf.initialize_all_variables())\r\n```\r\n\r\n### Predicted Class and Cost Function <a class=\"md-anchor\" id=\"AUTOGENERATED-predicted-class-and-cost-function\"></a>\r\n\r\nWe can now implement our regression model. It only takes one line!\r\nWe multiply the vectorized input images `x` by the weight matrix `W`, add\r\nthe bias `b`, and compute the softmax probabilities that are assigned to each\r\nclass.\r\n\r\n```python\r\ny = tf.nn.softmax(tf.matmul(x,W) + b)\r\n```\r\n\r\nThe cost function to be minimized during training can be specified just as\r\neasily. Our cost function will be the cross-entropy between the target and the\r\nmodel's prediction.\r\n\r\n```python\r\ncross_entropy = -tf.reduce_sum(y_*tf.log(y))\r\n```\r\n\r\nNote that `tf.reduce_sum` sums across all images in the minibatch, as well as\r\nall classes. We are computing the cross entropy for the entire minibatch.\r\n\r\n## Train the Model <a class=\"md-anchor\" id=\"AUTOGENERATED-train-the-model\"></a>\r\n\r\nNow that we have defined our model and training cost function, it is\r\nstraightforward to train using TensorFlow.\r\nBecause TensorFlow knows the entire computation graph, it\r\ncan use automatic differentiation to find the gradients of the cost with\r\nrespect to each of the variables.\r\nTensorFlow has a variety of\r\n[builtin optimization algorithms]\r\n(../../../api_docs/python/train.md#optimizers).\r\nFor this example, we will use steepest gradient descent, with a step length of\r\n0.01, to descend the cross entropy.\r\n\r\n```python\r\ntrain_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)\r\n```\r\n\r\nWhat TensorFlow actually did in that single line was to add new operations to\r\nthe computation graph. These operations included ones to compute gradients,\r\ncompute parameter update steps, and apply update steps to the parameters.\r\n\r\nThe returned operation `train_step`, when run, will apply the gradient\r\ndescent updates to the parameters. Training the model can therefore be\r\naccomplished by repeatedly running `train_step`.\r\n\r\n```python\r\nfor i in range(1000):\r\n  batch = mnist.train.next_batch(50)\r\n  train_step.run(feed_dict={x: batch[0], y_: batch[1]})\r\n```\r\n\r\nEach training iteration we load 50 training examples. We then run the\r\n`train_step` operation, using `feed_dict` to replace the `placeholder` tensors\r\n`x` and `y_` with the training examples.\r\nNote that you can replace any tensor in your computation graph using `feed_dict`\r\n-- it's not restricted to just `placeholder`s.\r\n\r\n### Evaluate the Model <a class=\"md-anchor\" id=\"AUTOGENERATED-evaluate-the-model\"></a>\r\n\r\nHow well did our model do?\r\n\r\nFirst we'll figure out where we predicted the correct label. `tf.argmax`\r\nis an extremely useful function which gives you the index of the highest entry\r\nin a tensor along some axis. For example, `tf.argmax(y,1)` is the label our\r\nmodel thinks is most likely for each input, while `tf.argmax(y_,1)` is the\r\ntrue label. We can use `tf.equal` to check if our prediction matches the\r\ntruth.\r\n\r\n```python\r\ncorrect_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\r\n```\r\n\r\nThat gives us a list of booleans. To determine what fraction are correct, we\r\ncast to floating point numbers and then take the mean. For example,\r\n`[True, False, True, True]` would become `[1,0,1,1]` which would become `0.75`.\r\n\r\n```python\r\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\r\n```\r\n\r\nFinally, we can evaluate our accuracy on the test data. This should be about\r\n91% correct.\r\n\r\n```python\r\nprint accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels})\r\n```\r\n\r\n## Build a Multilayer Convolutional Network <a class=\"md-anchor\" id=\"AUTOGENERATED-build-a-multilayer-convolutional-network\"></a>\r\n\r\nGetting 91% accuracy on MNIST is bad. It's almost embarrassingly bad. In this\r\nsection, we'll fix that, jumping from a very simple model to something moderatly\r\nsophisticated: a small convolutional neural network. This will get us to around\r\n99.2% accuracy -- not state of the art, but respectable.\r\n\r\n### Weight Initialization <a class=\"md-anchor\" id=\"AUTOGENERATED-weight-initialization\"></a>\r\n\r\nTo create this model, we're going to need to create a lot of weights and biases.\r\nOne should generally initialize weights with a small amount of noise for\r\nsymmetry breaking, and to prevent 0 gradients. Since we're using ReLU neurons,\r\nit is also good practice to initialize them with a slightly positive initial\r\nbias to avoid \"dead neurons.\" Instead of doing this repeatedly while we build\r\nthe model, let's create two handy functions to do it for us.\r\n\r\n```python\r\ndef weight_variable(shape):\r\n  initial = tf.truncated_normal(shape, stddev=0.1)\r\n  return tf.Variable(initial)\r\n\r\ndef bias_variable(shape):\r\n  initial = tf.constant(0.1, shape=shape)\r\n  return tf.Variable(initial)\r\n```\r\n\r\n### Convolution and Pooling <a class=\"md-anchor\" id=\"AUTOGENERATED-convolution-and-pooling\"></a>\r\n\r\nTensorFlow also gives us a lot of flexibility in convolution and pooling\r\noperations. How do we handle the boundaries? What is our stride size?\r\nIn this example, we're always going to choose the vanilla version.\r\nOur convolutions uses a stride of one and are zero padded so that the\r\noutput is the same size as the input. Our pooling is plain old max pooling\r\nover 2x2 blocks. To keep our code cleaner, let's also abstract those operations\r\ninto functions.\r\n\r\n```python\r\ndef conv2d(x, W):\r\n  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')\r\n\r\ndef max_pool_2x2(x):\r\n  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],\r\n                        strides=[1, 2, 2, 1], padding='SAME')\r\n```\r\n\r\n### First Convolutional Layer <a class=\"md-anchor\" id=\"AUTOGENERATED-first-convolutional-layer\"></a>\r\n\r\nWe can now implement our first layer. It will consist of convolution, followed\r\nby max pooling. The convolutional will compute 32 features for each 5x5 patch.\r\nIts weight tensor will have a shape of `[5, 5, 1, 32]`. The first two\r\ndimensions are the patch size, the next is the number of input channels, and\r\nthe last is the number of output channels. We will also have a bias vector with\r\na component for each output channel.\r\n\r\n```python\r\nW_conv1 = weight_variable([5, 5, 1, 32])\r\nb_conv1 = bias_variable([32])\r\n```\r\n\r\nTo apply the layer, we first reshape `x` to a 4d tensor, with the second and\r\nthird dimensions corresponding to image width and height, and the final\r\ndimension corresponding to the number of color channels.\r\n\r\n```python\r\nx_image = tf.reshape(x, [-1,28,28,1])\r\n```\r\n\r\nWe then convolve `x_image` with the weight tensor, add the\r\nbias, apply the ReLU function, and finally max pool.\r\n\r\n```python\r\nh_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)\r\nh_pool1 = max_pool_2x2(h_conv1)\r\n```\r\n\r\n### Second Convolutional Layer <a class=\"md-anchor\" id=\"AUTOGENERATED-second-convolutional-layer\"></a>\r\n\r\nIn order to build a deep network, we stack several layers of this type. The\r\nsecond layer will have 64 features for each 5x5 patch.\r\n\r\n```python\r\nW_conv2 = weight_variable([5, 5, 32, 64])\r\nb_conv2 = bias_variable([64])\r\n\r\nh_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)\r\nh_pool2 = max_pool_2x2(h_conv2)\r\n```\r\n\r\n### Densely Connected Layer <a class=\"md-anchor\" id=\"AUTOGENERATED-densely-connected-layer\"></a>\r\n\r\nNow that the image size has been reduced to 7x7, we add a fully-connected layer\r\nwith 1024 neurons to allow processing on the entire image. We reshape the tensor\r\nfrom the pooling layer into a batch of vectors,\r\nmultiply by a weight matrix, add a bias, and apply a ReLU.\r\n\r\n```python\r\nW_fc1 = weight_variable([7 * 7 * 64, 1024])\r\nb_fc1 = bias_variable([1024])\r\n\r\nh_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])\r\nh_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)\r\n```\r\n\r\n#### Dropout <a class=\"md-anchor\" id=\"AUTOGENERATED-dropout\"></a>\r\n\r\nTo reduce overfitting, we will apply dropout before the readout layer.\r\nWe create a `placeholder` for the probability that a neuron's output is kept\r\nduring dropout. This allows us to turn dropout on during training, and turn it\r\noff during testing.\r\nTensorFlow's `tf.nn.dropout` op automatically handles scaling neuron outputs in\r\naddition to masking them, so dropout just works without any additional scaling.\r\n\r\n```python\r\nkeep_prob = tf.placeholder(\"float\")\r\nh_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)\r\n```\r\n\r\n### Readout Layer <a class=\"md-anchor\" id=\"AUTOGENERATED-readout-layer\"></a>\r\n\r\nFinally, we add a softmax layer, just like for the one layer softmax regression\r\nabove.\r\n\r\n```python\r\nW_fc2 = weight_variable([1024, 10])\r\nb_fc2 = bias_variable([10])\r\n\r\ny_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)\r\n```\r\n\r\n### Train and Evaluate the Model <a class=\"md-anchor\" id=\"AUTOGENERATED-train-and-evaluate-the-model\"></a>\r\n\r\nHow well does this model do?\r\nTo train and evaluate it we will use code that is nearly identical to that for\r\nthe simple one layer SoftMax network above.\r\nThe differences are that: we will replace the steepest gradient descent\r\noptimizer with the more sophisticated ADAM optimizer; we will include the\r\nadditional parameter `keep_prob` in `feed_dict` to control the dropout rate;\r\nand we will add logging to every 100th iteration in the training process.\r\n\r\n```python\r\ncross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))\r\ntrain_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)\r\ncorrect_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))\r\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\r\nsess.run(tf.initialize_all_variables())\r\nfor i in range(20000):\r\n  batch = mnist.train.next_batch(50)\r\n  if i%100 == 0:\r\n    train_accuracy = accuracy.eval(feed_dict={\r\n        x:batch[0], y_: batch[1], keep_prob: 1.0})\r\n    print \"step %d, training accuracy %g\"%(i, train_accuracy)\r\n  train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})\r\n\r\nprint \"test accuracy %g\"%accuracy.eval(feed_dict={\r\n    x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})\r\n```\r\n\r\nThe final test set accuracy after running this code should be approximately 99.2%.\r\n\r\nWe have learned how to quickly and easily build, train, and evaluate a\r\nfairly sophisticated deep learning model using TensorFlow.\r\n"
  },
  {
    "path": "SOURCE/tutorials/mnist/tf/index.md",
    "content": "\r\n# TensorFlow Mechanics 101 <a class=\"md-anchor\" id=\"AUTOGENERATED-tensorflow-mechanics-101\"></a>\r\n\r\nCode: [tensorflow/g3doc/tutorials/mnist/](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/)\r\n\r\nThe goal of this tutorial is to show how to use TensorFlow to train and\r\nevaluate a simple feed-forward neural network for handwritten digit\r\nclassification using the (classic) MNIST data set.  The intended audience for\r\nthis tutorial is experienced machine learning users interested in using\r\nTensorFlow.\r\n\r\nThese tutorials are not intended for teaching Machine Learning in general.\r\n\r\nPlease ensure you have followed the instructions to [install TensorFlow](../../../get_started/os_setup.md).\r\n\r\n## Tutorial Files <a class=\"md-anchor\" id=\"AUTOGENERATED-tutorial-files\"></a>\r\n\r\nThis tutorial references the following files:\r\n\r\nFile | Purpose\r\n--- | ---\r\n[`mnist.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/mnist.py) | The code to build a fully-connected MNIST model.\r\n[`fully_connected_feed.py`](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/fully_connected_feed.py) | The main code to train the built MNIST model against the downloaded dataset using a feed dictionary.\r\n\r\nSimply run the `fully_connected_feed.py` file directly to start training:\r\n\r\n`python fully_connected_feed.py`\r\n\r\n## Prepare the Data <a class=\"md-anchor\" id=\"AUTOGENERATED-prepare-the-data\"></a>\r\n\r\nMNIST is a classic problem in machine learning. The problem is to look at\r\ngreyscale 28x28 pixel images of handwritten digits and determine which digit\r\nthe image represents, for all the digits from zero to nine.\r\n\r\n![MNIST Digits](./mnist_digits.png \"MNIST Digits\")\r\n\r\nFor more information, refer to [Yann LeCun's MNIST page](http://yann.lecun.com/exdb/mnist/)\r\nor [Chris Olah's visualizations of MNIST](http://colah.github.io/posts/2014-10-Visualizing-MNIST/).\r\n\r\n### Download <a class=\"md-anchor\" id=\"AUTOGENERATED-download\"></a>\r\n\r\nAt the top of the `run_training()` method, the `input_data.read_data_sets()`\r\nfunction will ensure that the correct data has been downloaded to your local\r\ntraining folder and then unpack that data to return a dictionary of `DataSet`\r\ninstances.\r\n\r\n```python\r\ndata_sets = input_data.read_data_sets(FLAGS.train_dir, FLAGS.fake_data)\r\n```\r\n\r\n**NOTE**: The `fake_data` flag is used for unit-testing purposes and may be\r\nsafely ignored by the reader.\r\n\r\nDataset | Purpose\r\n--- | ---\r\n`data_sets.train` | 55000 images and labels, for primary training.\r\n`data_sets.validation` | 5000 images and labels, for iterative validation of training accuracy.\r\n`data_sets.test` | 10000 images and labels, for final testing of trained accuracy.\r\n\r\nFor more information about the data, please read the [Download](../../../tutorials/mnist/download/index.md)\r\ntutorial.\r\n\r\n### Inputs and Placeholders <a class=\"md-anchor\" id=\"AUTOGENERATED-inputs-and-placeholders\"></a>\r\n\r\nThe `placeholder_inputs()` function creates two [`tf.placeholder`](../../../api_docs/python/io_ops.md#placeholder)\r\nops that define the shape of the inputs, including the `batch_size`, to the\r\nrest of the graph and into which the actual training examples will be fed.\r\n\r\n```python\r\nimages_placeholder = tf.placeholder(tf.float32, shape=(batch_size,\r\n                                                       IMAGE_PIXELS))\r\nlabels_placeholder = tf.placeholder(tf.int32, shape=(batch_size))\r\n```\r\n\r\nFurther down, in the training loop, the full image and label datasets are\r\nsliced to fit the `batch_size` for each step, matched with these placeholder\r\nops, and then passed into the `sess.run()` function using the `feed_dict`\r\nparameter.\r\n\r\n## Build the Graph <a class=\"md-anchor\" id=\"AUTOGENERATED-build-the-graph\"></a>\r\n\r\nAfter creating placeholders for the data, the graph is built from the\r\n`mnist.py` file according to a 3-stage pattern: `inference()`, `loss()`, and\r\n`training()`.\r\n\r\n1.  `inference()` - Builds the graph as far as is required for running\r\nthe network forward to make predictions.\r\n1.  `loss()` - Adds to the inference graph the ops required to generate\r\nloss.\r\n1.  `training()` - Adds to the loss graph the ops required to compute\r\nand apply gradients.\r\n\r\n<div style=\"width:95%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n  <img style=\"width:100%\" src=\"./mnist_subgraph.png\">\r\n</div>\r\n\r\n### Inference <a class=\"md-anchor\" id=\"AUTOGENERATED-inference\"></a>\r\n\r\nThe `inference()` function builds the graph as far as needed to\r\nreturn the tensor that would contain the output predictions.\r\n\r\nIt takes the images placeholder as input and builds on top\r\nof it a pair of fully connected layers with ReLu activation followed by a ten\r\nnode linear layer specifying the output logits.\r\n\r\nEach layer is created beneath a unique [`tf.name_scope`](../../../api_docs/python/framework.md#name_scope)\r\nthat acts as a prefix to the items created within that scope.\r\n\r\n```python\r\nwith tf.name_scope('hidden1') as scope:\r\n```\r\n\r\nWithin the defined scope, the weights and biases to be used by each of these\r\nlayers are generated into [`tf.Variable`](../../../api_docs/python/state_ops.md#Variable)\r\ninstances, with their desired shapes:\r\n\r\n```python\r\nweights = tf.Variable(\r\n    tf.truncated_normal([IMAGE_PIXELS, hidden1_units],\r\n                        stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))),\r\n    name='weights')\r\nbiases = tf.Variable(tf.zeros([hidden1_units]),\r\n                     name='biases')\r\n```\r\n\r\nWhen, for instance, these are created under the `hidden1` scope, the unique\r\nname given to the weights variable would be \"`hidden1/weights`\".\r\n\r\nEach variable is given initializer ops as part of their construction.\r\n\r\nIn this most common case, the weights are initialized with the\r\n[`tf.truncated_normal`](../../../api_docs/python/constant_op.md#truncated_normal)\r\nand given their shape of a 2-D tensor with\r\nthe first dim representing the number of units in the layer from which the\r\nweights connect and the second dim representing the number of\r\nunits in the layer to which the weights connect.  For the first layer, named\r\n`hidden1`, the dimensions are `[IMAGE_PIXELS, hidden1_units]` because the\r\nweights are connecting the image inputs to the hidden1 layer.  The\r\n`tf.truncated_normal` initializer generates a random distribution with a given\r\nmean and standard deviation.\r\n\r\nThen the biases are initialized with [`tf.zeros`](../../../api_docs/python/constant_op.md#zeros)\r\nto ensure they start with all zero values, and their shape is simply the number\r\nof units in the layer to which they connect.\r\n\r\nThe graph's three primary ops -- two [`tf.nn.relu`](../../../api_docs/python/nn.md#relu)\r\nops wrapping [`tf.matmul`](../../../api_docs/python/math_ops.md#matmul)\r\nfor the hidden layers and one extra `tf.matmul` for the logits -- are then\r\ncreated, each in turn, with their `tf.Variable` instances connected to the\r\ninput placeholder or the output tensor of the layer beneath each.\r\n\r\n```python\r\nhidden1 = tf.nn.relu(tf.matmul(images, weights) + biases)\r\n```\r\n\r\n```python\r\nhidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases)\r\n```\r\n\r\n```python\r\nlogits = tf.matmul(hidden2, weights) + biases\r\n```\r\n\r\nFinally, the `logits` tensor that will contain the output is returned.\r\n\r\n### Loss <a class=\"md-anchor\" id=\"AUTOGENERATED-loss\"></a>\r\n\r\nThe `loss()` function further builds the graph by adding the required loss\r\nops.\r\n\r\nFirst, the values from the `labels_placeholder` are encoded as a tensor of 1-hot\r\nvalues. For example, if the class identifier is '3' the value is converted to:\r\n<br>`[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]`\r\n\r\n```python\r\nbatch_size = tf.size(labels)\r\nlabels = tf.expand_dims(labels, 1)\r\nindices = tf.expand_dims(tf.range(0, batch_size, 1), 1)\r\nconcated = tf.concat(1, [indices, labels])\r\nonehot_labels = tf.sparse_to_dense(\r\n    concated, tf.pack([batch_size, NUM_CLASSES]), 1.0, 0.0)\r\n```\r\n\r\nA [`tf.nn.softmax_cross_entropy_with_logits`](../../../api_docs/python/nn.md#softmax_cross_entropy_with_logits)\r\nop is then added to compare the output logits from the `inference()` function\r\nand the 1-hot labels.\r\n\r\n```python\r\ncross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits,\r\n                                                        onehot_labels,\r\n                                                        name='xentropy')\r\n```\r\n\r\nIt then uses [`tf.reduce_mean`](../../../api_docs/python/math_ops.md#reduce_mean)\r\nto average the cross entropy values across the batch dimension (the first\r\ndimension) as the total loss.\r\n\r\n```python\r\nloss = tf.reduce_mean(cross_entropy, name='xentropy_mean')\r\n```\r\n\r\nAnd the tensor that will then contain the loss value is returned.\r\n\r\n> Note: Cross-entropy is an idea from information theory that allows us\r\n> to describe how bad it is to believe the predictions of the neural network,\r\n> given what is actually true. For more information, read the blog post Visual\r\n> Information Theory (http://colah.github.io/posts/2015-09-Visual-Information/)\r\n\r\n### Training <a class=\"md-anchor\" id=\"AUTOGENERATED-training\"></a>\r\n\r\nThe `training()` function adds the operations needed to minimize the loss via\r\ngradient descent.\r\n\r\nFirstly, it takes the loss tensor from the `loss()` function and hands it to a\r\n[`tf.scalar_summary`](../../../api_docs/python/train.md#scalar_summary),\r\nan op for generating summary values into the events file when used with a\r\n`SummaryWriter` (see below).  In this case, it will emit the snapshot value of\r\nthe loss every time the summaries are written out.\r\n\r\n```python\r\ntf.scalar_summary(loss.op.name, loss)\r\n```\r\n\r\nNext, we instantiate a [`tf.train.GradientDescentOptimizer`](../../../api_docs/python/train.md#GradientDescentOptimizer)\r\nresponsible for applying gradients with the requested learning rate.\r\n\r\n```python\r\noptimizer = tf.train.GradientDescentOptimizer(FLAGS.learning_rate)\r\n```\r\n\r\nWe then generate a single variable to contain a counter for the global\r\ntraining step and the [`minimize()`](../../../api_docs/python/train.md#Optimizer.minimize)\r\nop is used to both update the trainable weights in the system and increment the\r\nglobal step.  This is, by convention, known as the `train_op` and is what must\r\nbe run by a TensorFlow session in order to induce one full step of training\r\n(see below).\r\n\r\n```python\r\nglobal_step = tf.Variable(0, name='global_step', trainable=False)\r\ntrain_op = optimizer.minimize(loss, global_step=global_step)\r\n```\r\n\r\nThe tensor containing the outputs of the training op is returned.\r\n\r\n## Train the Model <a class=\"md-anchor\" id=\"AUTOGENERATED-train-the-model\"></a>\r\n\r\nOnce the graph is built, it can be iteratively trained and evaluated in a loop\r\ncontrolled by the user code in `fully_connected_feed.py`.\r\n\r\n### The Graph <a class=\"md-anchor\" id=\"AUTOGENERATED-the-graph\"></a>\r\n\r\nAt the top of the `run_training()` function is a python `with` command that\r\nindicates all of the built ops are to be associated with the default\r\nglobal [`tf.Graph`](../../../api_docs/python/framework.md#Graph)\r\ninstance.\r\n\r\n```python\r\nwith tf.Graph().as_default():\r\n```\r\n\r\nA `tf.Graph` is a collection of ops that may be executed together as a group.\r\nMost TensorFlow uses will only need to rely on the single default graph.\r\n\r\nMore complicated uses with multiple graphs are possible, but beyond the scope of\r\nthis simple tutorial.\r\n\r\n### The Session <a class=\"md-anchor\" id=\"AUTOGENERATED-the-session\"></a>\r\n\r\nOnce all of the build preparation has been completed and all of the necessary\r\nops generated, a [`tf.Session`](../../../api_docs/python/client.md#Session)\r\nis created for running the graph.\r\n\r\n```python\r\nsess = tf.Session()\r\n```\r\n\r\nAlternately, a `Session` may be generated into a `with` block for scoping:\r\n\r\n```python\r\nwith tf.Session() as sess:\r\n```\r\n\r\nThe empty parameter to session indicates that this code will attach to\r\n(or create if not yet created) the default local session.\r\n\r\nImmediately after creating the session, all of the `tf.Variable`\r\ninstances are initialized by calling [`sess.run()`](../../../api_docs/python/client.md#Session.run)\r\non their initialization op.\r\n\r\n```python\r\ninit = tf.initialize_all_variables()\r\nsess.run(init)\r\n```\r\n\r\nThe [`sess.run()`](../../../api_docs/python/client.md#Session.run)\r\nmethod will run the complete subset of the graph that\r\ncorresponds to the op(s) passed as parameters.  In this first call, the `init`\r\nop is a [`tf.group`](../../../api_docs/python/control_flow_ops.md#group)\r\nthat contains only the initializers for the variables.  None of the rest of the\r\ngraph is run here; that happens in the training loop below.\r\n\r\n### Train Loop <a class=\"md-anchor\" id=\"AUTOGENERATED-train-loop\"></a>\r\n\r\nAfter initializing the variables with the session, training may begin.\r\n\r\nThe user code controls the training per step, and the simplest loop that\r\ncan do useful training is:\r\n\r\n```python\r\nfor step in xrange(max_steps):\r\n    sess.run(train_op)\r\n```\r\n\r\nHowever, this tutorial is slightly more complicated in that it must also slice\r\nup the input data for each step to match the previously generated placeholders.\r\n\r\n#### Feed the Graph <a class=\"md-anchor\" id=\"AUTOGENERATED-feed-the-graph\"></a>\r\n\r\nFor each step, the code will generate a feed dictionary that will contain the\r\nset of examples on which to train for the step, keyed by the placeholder\r\nops they represent.\r\n\r\nIn the `fill_feed_dict()` function, the given `DataSet` is queried for its next\r\n`batch_size` set of images and labels, and tensors matching the placeholders are\r\nfilled containing the next images and labels.\r\n\r\n```python\r\nimages_feed, labels_feed = data_set.next_batch(FLAGS.batch_size)\r\n```\r\n\r\nA python dictionary object is then generated with the placeholders as keys and\r\nthe representative feed tensors as values.\r\n\r\n```python\r\nfeed_dict = {\r\n    images_placeholder: images_feed,\r\n    labels_placeholder: labels_feed,\r\n}\r\n```\r\n\r\nThis is passed into the `sess.run()` function's `feed_dict` parameter to provide\r\nthe input examples for this step of training.\r\n\r\n#### Check the Status <a class=\"md-anchor\" id=\"AUTOGENERATED-check-the-status\"></a>\r\n\r\nThe code specifies two values to fetch in its run call: `[train_op, loss]`.\r\n\r\n```python\r\nfor step in xrange(FLAGS.max_steps):\r\n    feed_dict = fill_feed_dict(data_sets.train,\r\n                               images_placeholder,\r\n                               labels_placeholder)\r\n    _, loss_value = sess.run([train_op, loss],\r\n                             feed_dict=feed_dict)\r\n```\r\n\r\nBecause there are two values to fetch, `sess.run()` returns a tuple with two\r\nitems.  Each `Tensor` in the list of values to fetch corresponds to a numpy\r\narray in the returned tuple, filled with the value of that tensor during this\r\nstep of training. Since `train_op` is an `Operation` with no output value, the\r\ncorresponding element in the returned tuple is `None` and, thus,\r\ndiscarded. However, the value of the `loss` tensor may become NaN if the model\r\ndiverges during training, so we capture this value for logging.\r\n\r\nAssuming that the training runs fine without NaNs, the training loop also\r\nprints a simple status text every 100 steps to let the user know the state of\r\ntraining.\r\n\r\n```python\r\nif step % 100 == 0:\r\n    print 'Step %d: loss = %.2f (%.3f sec)' % (step, loss_value, duration)\r\n```\r\n\r\n#### Visualize the Status <a class=\"md-anchor\" id=\"AUTOGENERATED-visualize-the-status\"></a>\r\n\r\nIn order to emit the events files used by [TensorBoard](../../../how_tos/summaries_and_tensorboard/index.md),\r\nall of the summaries (in this case, only one) are collected into a single op\r\nduring the graph building phase.\r\n\r\n```python\r\nsummary_op = tf.merge_all_summaries()\r\n```\r\n\r\nAnd then after the session is created, a [`tf.train.SummaryWriter`](../../../api_docs/python/train.md#SummaryWriter)\r\nmay be instantiated to write the events files, which\r\ncontain both the graph itself and the values of the summaries.\r\n\r\n```python\r\nsummary_writer = tf.train.SummaryWriter(FLAGS.train_dir,\r\n                                        graph_def=sess.graph_def)\r\n```\r\n\r\nLastly, the events file will be updated with new summary values every time the\r\n`summary_op` is run and the ouput passed to the writer's `add_summary()`\r\nfunction.\r\n\r\n```python\r\nsummary_str = sess.run(summary_op, feed_dict=feed_dict)\r\nsummary_writer.add_summary(summary_str, step)\r\n```\r\n\r\nWhen the events files are written, TensorBoard may be run against the training\r\nfolder to display the values from the summaries.\r\n\r\n![MNIST TensorBoard](./mnist_tensorboard.png \"MNIST TensorBoard\")\r\n\r\n**NOTE**: For more info about how to build and run Tensorboard, please see the accompanying tutorial [Tensorboard: Visualizing Your Training](../../../how_tos/summaries_and_tensorboard/index.md).\r\n\r\n#### Save a Checkpoint <a class=\"md-anchor\" id=\"AUTOGENERATED-save-a-checkpoint\"></a>\r\n\r\nIn order to emit a checkpoint file that may be used to later restore a model\r\nfor further training or evaluation, we instantiate a\r\n[`tf.train.Saver`](../../../api_docs/python/state_ops.md#Saver).\r\n\r\n```python\r\nsaver = tf.train.Saver()\r\n```\r\n\r\nIn the training loop, the [`saver.save()`](../../../api_docs/python/state_ops.md#Saver.save)\r\nmethod will periodically be called to write a checkpoint file to the training\r\ndirectory with the current values of all the trainable variables.\r\n\r\n```python\r\nsaver.save(sess, FLAGS.train_dir, global_step=step)\r\n```\r\n\r\nAt some later point in the future, training might be resumed by using the\r\n[`saver.restore()`](../../../api_docs/python/state_ops.md#Saver.restore)\r\nmethod to reload the model parameters.\r\n\r\n```python\r\nsaver.restore(sess, FLAGS.train_dir)\r\n```\r\n\r\n## Evaluate the Model <a class=\"md-anchor\" id=\"AUTOGENERATED-evaluate-the-model\"></a>\r\n\r\nEvery thousand steps, the code will attempt to evaluate the model against both\r\nthe training and test datasets.  The `do_eval()` function is called thrice, for\r\nthe training, validation, and test datasets.\r\n\r\n```python\r\nprint 'Training Data Eval:'\r\ndo_eval(sess,\r\n        eval_correct,\r\n        images_placeholder,\r\n        labels_placeholder,\r\n        data_sets.train)\r\nprint 'Validation Data Eval:'\r\ndo_eval(sess,\r\n        eval_correct,\r\n        images_placeholder,\r\n        labels_placeholder,\r\n        data_sets.validation)\r\nprint 'Test Data Eval:'\r\ndo_eval(sess,\r\n        eval_correct,\r\n        images_placeholder,\r\n        labels_placeholder,\r\n        data_sets.test)\r\n```\r\n\r\n> Note that more complicated usage would usually sequester the `data_sets.test`\r\n> to only be checked after significant amounts of hyperparameter tuning.  For\r\n> the sake of a simple little MNIST problem, however, we evaluate against all of\r\n> the data.\r\n\r\n### Build the Eval Graph <a class=\"md-anchor\" id=\"AUTOGENERATED-build-the-eval-graph\"></a>\r\n\r\nBefore opening the default Graph, the test data should have been fetched by\r\ncalling the `get_data(train=False)` function with the parameter set to grab\r\nthe test dataset.\r\n\r\n```python\r\ntest_all_images, test_all_labels = get_data(train=False)\r\n```\r\n\r\nBefore entering the training loop, the Eval op should have been built\r\nby calling the `evaluation()` function from `mnist.py` with the same\r\nlogits/labels parameters as the `loss()` function.\r\n\r\n```python\r\neval_correct = mnist.evaluation(logits, labels_placeholder)\r\n```\r\n\r\nThe `evaluation()` function simply generates a [`tf.nn.in_top_k`](../../../api_docs/python/nn.md#in_top_k)\r\nop that can automatically score each model output as correct if the true label\r\ncan be found in the K most-likely predictions.  In this case, we set the value\r\nof K to 1 to only consider a prediction correct if it is for the true label.\r\n\r\n```python\r\neval_correct = tf.nn.in_top_k(logits, labels, 1)\r\n```\r\n\r\n### Eval Output <a class=\"md-anchor\" id=\"AUTOGENERATED-eval-output\"></a>\r\n\r\nOne can then create a loop for filling a `feed_dict` and calling `sess.run()`\r\nagainst the `eval_correct` op to evaluate the model on the given dataset.\r\n\r\n```python\r\nfor step in xrange(steps_per_epoch):\r\n    feed_dict = fill_feed_dict(data_set,\r\n                               images_placeholder,\r\n                               labels_placeholder)\r\n    true_count += sess.run(eval_correct, feed_dict=feed_dict)\r\n```\r\n\r\nThe `true_count` variable simply accumulates all of the predictions that the\r\n`in_top_k` op has determined to be correct.  From there, the precision may be\r\ncalculated from simply dividing by the total number of examples.\r\n\r\n```python\r\nprecision = float(true_count) / float(num_examples)\r\nprint '  Num examples: %d  Num correct: %d  Precision @ 1: %0.02f' % (\r\n    num_examples, true_count, precision)\r\n```\r\n"
  },
  {
    "path": "SOURCE/tutorials/mnist_beginners.md",
    "content": "# MNIST机器学习入门 <a class=\"md-anchor\" id=\"AUTOGENERATED-mnist-for-ml-beginners\"></a>\r\n\r\n*这个教程的目标读者是对机器学习和TensorFlow都不太了解的新手。如果你已经了解MNIST和softmax回归(softmax regression)的相关知识，你可以阅读这个[快速上手教程](./mnist_pros.md)。*\r\n \r\n当我们开始学习编程的时候，第一件事往往是学习打印\"Hello World\"。就好比编程入门有Hello World，机器学习入门有MNIST。\r\n\r\nMNIST是一个入门级的计算机视觉数据集，它包含各种手写数字图片：\r\n<div style=\"width:40%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/MNIST.png\">\r\n</div>\r\n \r\n它也包含每一张图片对应的标签，告诉我们这个是数字几。比如，上面这四张图片的标签分别是5，0，4，1。\r\n \r\n在此教程中，我们将训练一个机器学习模型用于预测图片里面的数字。我们的目的不是要设计一个世界一流的复杂模型 -- 尽管我们会在之后给你源代码去实现一流的预测模型 -- 而是要介绍下如何使用TensorFlow。所以，我们这里会从一个很简单的数学模型开始，它叫做Softmax Regression。\r\n\r\n对应这个教程的实现代码很短，而且真正有意思的内容只包含在三行代码里面。但是，去理解包含在这些代码里面的设计思想是非常重要的：TensorFlow工作流程和机器学习的基本概念。因此，这个教程会很详细地介绍这些代码的实现原理。\r\n\r\n## MNIST数据集 <a class=\"md-anchor\" id=\"AUTOGENERATED-the-mnist-data\"></a>\r\n\r\nMNIST数据集的官网是[Yann LeCun's website](http://yann.lecun.com/exdb/mnist/)。在这里，我们提供了一份python源代码用于自动下载和安装这个数据集。你可以下载[这份代码](https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/tutorials/mnist/input_data.py)，然后用下面的代码导入到你的项目里面，也可以直接复制粘贴到你的代码文件里面。\r\n\r\n```python\r\nimport tensorflow.examples.tutorials.mnist.input_data as input_data\r\nmnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)\r\n```\r\n\r\n下载下来的数据集被分成两部分：60000行的训练数据集（`mnist.train`）和10000行的测试数据集（`mnist.test`）。这样的切分很重要，在机器学习模型设计时必须有一个单独的测试数据集不用于训练而是用来评估这个模型的性能，从而更加容易把设计的模型推广到其他数据集上（泛化）。\r\n\r\n正如前面提到的一样，每一个MNIST数据单元有两部分组成：一张包含手写数字的图片和一个对应的标签。我们把这些图片设为“xs”，把这些标签设为“ys”。训练数据集和测试数据集都包含xs和ys，比如训练数据集的图片是 `mnist.train.images` ，训练数据集的标签是 `mnist.train.labels`。\r\n \r\n每一张图片包含28X28个像素点。我们可以用一个数字数组来表示这张图片：\r\n\r\n<div style=\"width:50%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/MNIST-Matrix.png\">\r\n</div>\r\n\r\n我们把这个数组展开成一个向量，长度是 28x28 = 784。如何展开这个数组（数字间的顺序）不重要，只要保持各个图片采用相同的方式展开。从这个角度来看，MNIST数据集的图片就是在784维向量空间里面的点, 并且拥有比较[复杂的结构](http://colah.github.io/posts/2014-10-Visualizing-MNIST/) (提醒: 此类数据的可视化是计算密集型的)。  \r\n\r\n展平图片的数字数组会丢失图片的二维结构信息。这显然是不理想的，最优秀的计算机视觉方法会挖掘并利用这些结构信息，我们会在后续教程中介绍。但是在这个教程中我们忽略这些结构，所介绍的简单数学模型，softmax回归(softmax regression)，不会利用这些结构信息。\r\n  \r\n因此，在MNIST训练数据集中，`mnist.train.images` 是一个形状为 `[60000, 784]` 的张量，第一个维度数字用来索引图片，第二个维度数字用来索引每张图片中的像素点。在此张量里的每一个元素，都表示某张图片里的某个像素的强度值，值介于0和1之间。\r\n\r\n<div style=\"width:40%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/mnist-train-xs.png\">\r\n</div>\r\n\r\n相对应的MNIST数据集的标签是介于0到9的数字，用来描述给定图片里表示的数字。为了用于这个教程，我们使标签数据是\"one-hot vectors\"。 一个one-hot向量除了某一位的数字是1以外其余各维度数字都是0。所以在此教程中，数字n将表示成一个只有在第n维度（从0开始）数字为1的10维向量。比如，标签0将表示成([1,0,0,0,0,0,0,0,0,0,0])。因此， `mnist.train.labels` 是一个 `[60000, 10]` 的数字矩阵。 \r\n\r\n<div style=\"width:40%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/mnist-train-ys.png\">\r\n</div>\r\n\r\n现在，我们准备好可以开始构建我们的模型啦！\r\n\r\n## Softmax回归介绍 <a class=\"md-anchor\" id=\"AUTOGENERATED-softmax-regressions\"></a>\r\n \r\n我们知道MNIST的每一张图片都表示一个数字，从0到9。我们希望得到给定图片代表每个数字的概率。比如说，我们的模型可能推测一张包含9的图片代表数字9的概率是80%但是判断它是8的概率是5%（因为8和9都有上半部分的小圆），然后给予它代表其他数字的概率更小的值。\r\n\r\n这是一个使用softmax回归（softmax regression）模型的经典案例。softmax模型可以用来给不同的对象分配概率。即使在之后，我们训练更加精细的模型时，最后一步也需要用softmax来分配概率。\r\n \r\nsoftmax回归（softmax regression）分两步：第一步\r\n \r\n为了得到一张给定图片属于某个特定数字类的证据（evidence），我们对图片像素值进行加权求和。如果这个像素具有很强的证据说明这张图片不属于该类，那么相应的权值为负数，相反如果这个像素拥有有利的证据支持这张图片属于这个类，那么权值是正数。\r\n\r\n下面的图片显示了一个模型学习到的图片上每个像素对于特定数字类的权值。红色代表负数权值，蓝色代表正数权值。\r\n\r\n<div style=\"width:40%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/softmax-weights.png\">\r\n</div>\r\n\r\n我们也需要加入一个额外的偏置量（bias），因为输入往往会带有一些无关的干扰量。因此对于给定的输入图片 **x** 它代表的是数字 **i** 的证据可以表示为\r\n\r\n![](../images/mnist1.png)\r\n\r\n其中 ![](../images/mnist2.png) 代表权重，![](../images/mnist3.png) 代表数字 **i** 类的偏置量，**j** 代表给定图片 **x** 的像素索引用于像素求和。然后用softmax函数可以把这些证据转换成概率 **y**：\r\n\r\n![](../images/mnist4.png)\r\n\r\n这里的softmax可以看成是一个激励（activation）函数或者链接（link）函数，把我们定义的线性函数的输出转换成我们想要的格式，也就是关于10个数字类的概率分布。因此，给定一张图片，它对于每一个数字的吻合度可以被softmax函数转换成为一个概率值。softmax函数可以定义为：\r\n\r\n![](../images/mnist5.png)\r\n\r\n展开等式右边的子式，可以得到：\r\n\r\n![](../images/mnist6.png)\r\n \r\n但是更多的时候把softmax模型函数定义为前一种形式：把输入值当成幂指数求值，再正则化这些结果值。这个幂运算表示，更大的证据对应更大的假设模型（hypothesis）里面的乘数权重值。反之，拥有更少的证据意味着在假设模型里面拥有更小的乘数系数。假设模型里的权值不可以是0值或者负值。Softmax然后会正则化这些权重值，使它们的总和等于1，以此构造一个有效的概率分布。（更多的关于Softmax函数的信息，可以参考Michael Nieslen的书里面的这个[部分](http://neuralnetworksanddeeplearning.com/chap3.html#softmax)，其中有关于softmax的可交互式的可视化解释。）\r\n\r\n对于softmax回归模型可以用下面的图解释，对于输入的`xs`加权求和，再分别加上一个偏置量，最后再输入到softmax函数中：\r\n\r\n<div style=\"width:55%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/softmax-regression-scalargraph.png\">\r\n</div>\r\n\r\n如果把它写成一个等式，我们可以得到：\r\n\r\n<div style=\"width:52%; margin-left:25%; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/softmax-regression-scalarequation.png\">\r\n</div>\r\n\r\n我们也可以用向量表示这个计算过程：用矩阵乘法和向量相加。这有助于提高计算效率。（也是一种更有效的思考方式）\r\n\r\n<div style=\"width:50%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/softmax-regression-vectorequation.png\">\r\n</div>\r\n\r\n更进一步，可以写成更加紧凑的方式：\r\n\r\n![](../images/mnist7.png)\r\n\r\n## 实现回归模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-implementing-the-regression\"></a>\r\n\r\n为了用python实现高效的数值计算，我们通常会使用函数库，比如NumPy，会把类似矩阵乘法这样的复杂运算使用其他外部语言实现。不幸的是，从外部计算切换回Python的每一个操作，仍然是一个很大的开销。如果你用GPU来进行外部计算，这样的开销会更大。用分布式的计算方式，也会花费更多的资源用来传输数据。\r\n\r\nTensorFlow也把复杂的计算放在python之外完成，但是为了避免前面说的那些开销，它做了进一步完善。Tensorflow不单独地运行单一的复杂计算，而是让我们可以先用图描述一系列可交互的计算操作，然后全部一起在Python之外运行。（这样类似的运行方式，可以在不少的机器学习库中看到。）\r\n\r\n使用TensorFlow之前，首先导入它：\r\n\r\n```python\r\nimport tensorflow as tf\r\n```\r\n\r\n我们通过操作符号变量来描述这些可交互的操作单元，可以用下面的方式创建一个：\r\n\r\n```python\r\nx = tf.placeholder(tf.float32, [None, 784])\r\n```\r\n\r\n`x`不是一个特定的值，而是一个占位符`placeholder`，我们在TensorFlow运行计算时输入这个值。我们希望能够输入任意数量的MNIST图像，每一张图展平成784维的向量。我们用2维的浮点数张量来表示这些图，这个张量的形状是`[None，784 ]`。（这里的`None`表示此张量的第一个维度可以是任何长度的。）\r\n\r\n我们的模型也需要权重值和偏置量，当然我们可以把它们当做是另外的输入（使用占位符），但TensorFlow有一个更好的方法来表示它们：`Variable` 。\r\n一个`Variable`代表一个可修改的张量，存在在TensorFlow的用于描述交互性操作的图中。它们可以用于计算输入值，也可以在计算中被修改。对于各种机器学习应用，一般都会有模型参数，可以用`Variable`表示。\r\n\r\n```python\r\nW = tf.Variable(tf.zeros([784,10]))\r\nb = tf.Variable(tf.zeros([10]))\r\n```\r\n\r\n我们赋予`tf.Variable`不同的初值来创建不同的`Variable`：在这里，我们都用全为零的张量来初始化`W`和`b`。因为我们要学习`W`和`b`的值，它们的初值可以随意设置。\r\n\r\n注意，`W`的维度是[784，10]，因为我们想要用784维的图片向量乘以它以得到一个10维的证据值向量，每一位对应不同数字类。`b`的形状是[10]，所以我们可以直接把它加到输出上面。\r\n\r\n现在，我们可以实现我们的模型啦。只需要一行代码！\r\n\r\n```python\r\ny = tf.nn.softmax(tf.matmul(x,W) + b)\r\n```\r\n\r\n首先，我们用`tf.matmul(​​X，W)`表示`x`乘以`W`，对应之前等式里面的![](../images/mnist8.png)，这里`x`是一个2维张量拥有多个输入。然后再加上`b`，把和输入到`tf.nn.softmax`函数里面。\r\n\r\n至此，我们先用了几行简短的代码来设置变量，然后只用了一行代码来定义我们的模型。TensorFlow不仅仅可以使softmax回归模型计算变得特别简单，它也用这种非常灵活的方式来描述其他各种数值计算，从机器学习模型对物理学模拟仿真模型。一旦被定义好之后，我们的模型就可以在不同的设备上运行：计算机的CPU，GPU，甚至是手机！\r\n\r\n## 训练模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-training\"></a>\r\n\r\n为了训练我们的模型，我们首先需要定义一个指标来评估这个模型是好的。其实，在机器学习，我们通常定义指标来表示一个模型是坏的，这个指标称为成本（cost）或损失（loss），然后尽量最小化这个指标。但是，这两种方式是相同的。\r\n\r\n一个非常常见的，非常漂亮的成本函数是“交叉熵”（cross-entropy）。交叉熵产生于信息论里面的信息压缩编码技术，但是它后来演变成为从博弈论到机器学习等其他领域里的重要技术手段。它的定义如下：\r\n\r\n![](../images/mnist10.png)\r\n\r\n**y** 是我们预测的概率分布, **y'** 是实际的分布（我们输入的one-hot vector)。比较粗糙的理解是，交叉熵是用来衡量我们的预测用于描述真相的低效性。更详细的关于交叉熵的解释超出本教程的范畴，但是你很有必要好好[理解它](http://colah.github.io/posts/2015-09-Visual-Information/)。\r\n\r\n为了计算交叉熵，我们首先需要添加一个新的占位符用于输入正确值：\r\n\r\n```python\r\ny_ = tf.placeholder(\"float\", [None,10])\r\n```\r\n\r\n然后我们可以用 ![](../images/mnist9.png) 计算交叉熵:\r\n\r\n```python\r\ncross_entropy = -tf.reduce_sum(y_*tf.log(y))\r\n```\r\n\r\n首先，用 `tf.log` 计算 `y` 的每个元素的对数。接下来，我们把 `y_` 的每一个元素和 `tf.log(y)` 的对应元素相乘。最后，用 `tf.reduce_sum` 计算张量的所有元素的总和。（注意，这里的交叉熵不仅仅用来衡量单一的一对预测和真实值，而是所有100幅图片的交叉熵的总和。对于100个数据点的预测表现比单一数据点的表现能更好地描述我们的模型的性能。\r\n\r\n现在我们知道我们需要我们的模型做什么啦，用TensorFlow来训练它是非常容易的。因为TensorFlow拥有一张描述你各个计算单元的图，它可以自动地使用[反向传播算法(backpropagation algorithm)](http://colah.github.io/posts/2015-08-Backprop/)来有效地确定你的变量是如何影响你想要最小化的那个成本值的。然后，TensorFlow会用你选择的优化算法来不断地修改变量以降低成本。\r\n\r\n```python\r\ntrain_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)\r\n```\r\n\r\n在这里，我们要求TensorFlow用梯度下降算法（gradient descent algorithm）以0.01的学习速率最小化交叉熵。梯度下降算法（gradient descent algorithm）是一个简单的学习过程，TensorFlow只需将每个变量一点点地往使成本不断降低的方向移动。当然TensorFlow也提供了[其他许多优化算法](../api_docs/python/train.md#optimizers)：只要简单地调整一行代码就可以使用其他的算法。\r\n\r\nTensorFlow在这里实际上所做的是，它会在后台给描述你的计算的那张图里面增加一系列新的计算操作单元用于实现反向传播算法和梯度下降算法。然后，它返回给你的只是一个单一的操作，当运行这个操作时，它用梯度下降算法训练你的模型，微调你的变量，不断减少成本。\r\n\r\n现在，我们已经设置好了我们的模型。在运行计算之前，我们需要添加一个操作来初始化我们创建的变量：\r\n\r\n```python\r\ninit = tf.initialize_all_variables()\r\n```\r\n\r\n现在我们可以在一个`Session`里面启动我们的模型，并且初始化变量：\r\n\r\n```python\r\nsess = tf.Session()\r\nsess.run(init)\r\n```\r\n\r\n然后开始训练模型，这里我们让模型循环训练1000次！\r\n\r\n```python\r\nfor i in range(1000):\r\n  batch_xs, batch_ys = mnist.train.next_batch(100)\r\n  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})\r\n```\r\n\r\n该循环的每个步骤中，我们都会随机抓取训练数据中的100个批处理数据点，然后我们用这些数据点作为参数替换之前的占位符来运行`train_step`。 \r\n\r\n使用一小部分的随机数据来进行训练被称为随机训练（stochastic training）- 在这里更确切的说是随机梯度下降训练。在理想情况下，我们希望用我们所有的数据来进行每一步的训练，因为这能给我们更好的训练结果，但显然这需要很大的计算开销。所以，每一次训练我们可以使用不同的数据子集，这样做既可以减少计算开销，又可以最大化地学习到数据集的总体特性。\r\n\r\n## 评估我们的模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-evaluating-our-model\"></a>\r\n\r\n那么我们的模型性能如何呢？\r\n\r\n首先让我们找出那些预测正确的标签。`tf.argmax` 是一个非常有用的函数，它能给出某个tensor对象在某一维上的其数据最大值所在的索引值。由于标签向量是由0,1组成，因此最大值1所在的索引位置就是类别标签，比如`tf.argmax(y,1)`返回的是模型对于任一输入x预测到的标签值，而 `tf.argmax(y_,1)` 代表正确的标签，我们可以用 `tf.equal` 来检测我们的预测是否真实标签匹配(索引位置一样表示匹配)。\r\n\r\n```python\r\ncorrect_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\r\n```\r\n\r\n这行代码会给我们一组布尔值。为了确定正确预测项的比例，我们可以把布尔值转换成浮点数，然后取平均值。例如，`[True, False, True, True]` 会变成 `[1,0,1,1]` ，取平均值后得到 `0.75`.\r\n\r\n```python\r\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\r\n```\r\n\r\n最后，我们计算所学习到的模型在测试数据集上面的正确率。\r\n\r\n```python\r\nprint sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})\r\n```\r\n\r\n这个最终结果值应该大约是91%。\r\n\r\n这个结果好吗？嗯，并不太好。事实上，这个结果是很差的。这是因为我们仅仅使用了一个非常简单的模型。不过，做一些小小的改进，我们就可以得到97％的正确率。最好的模型甚至可以获得超过99.7％的准确率！（想了解更多信息，可以看看这个关于各种模型的[性能对比列表](http://rodrigob.github.io/are_we_there_yet/build/classification_datasets_results.html)。)\r\n\r\n比结果更重要的是，我们从这个模型中学习到的设计思想。不过，如果你仍然对这里的结果有点失望，可以查看[下一个教程](./mnist_pros.md)，在那里你可以学习如何用TensorFlow构建更加复杂的模型以获得更好的性能！\r\n\r\n原文地址：[MNIST For ML Beginners](http://tensorflow.org/tutorials/mnist/beginners/index.md) 翻译：[linbojin](https://github.com/linbojin) 校对：\r\n"
  },
  {
    "path": "SOURCE/tutorials/mnist_download.md",
    "content": "# MNIST 数据下载 <a class=\"md-anchor\" id=\"AUTOGENERATED-mnist-data-download\"></a>\r\n\r\n源码: [tensorflow/g3doc/tutorials/mnist/](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/tutorials/mnist)\r\n\r\n本教程的目标是展示如何下载用于手写数字分类问题所要用到的（经典）MNIST数据集。\r\n\r\n## 教程 文件 <a class=\"md-anchor\" id=\"AUTOGENERATED-tutorial-files\"></a>\r\n\r\n本教程需要使用以下文件：\r\n\r\n文件 | 目的\r\n--- | ---\r\n[`input_data.py`](https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/tutorials/mnist/input_data.py) | 下载用于训练和测试的MNIST数据集的源码\r\n\r\n## 准备数据 <a class=\"md-anchor\" id=\"AUTOGENERATED-prepare-the-data\"></a>\r\n\r\nMNIST是在机器学习领域中的一个经典问题。该问题解决的是把28x28像素的灰度手写数字图片识别为相应的数字，其中数字的范围从0到9.\r\n\r\n![MNIST Digits](../images/mnist_digits.png \"MNIST Digits\")\r\n\r\n更多详情, 请参考 [Yann LeCun's MNIST page](http://yann.lecun.com/exdb/mnist/)\r\n或 [Chris Olah's visualizations of MNIST](http://colah.github.io/posts/2014-10-Visualizing-MNIST/).\r\n\r\n### 下载 <a class=\"md-anchor\" id=\"AUTOGENERATED-download\"></a>\r\n\r\n[Yann LeCun's MNIST page](http://yann.lecun.com/exdb/mnist/)\r\n也提供了训练集与测试集数据的下载。\r\n\r\n文件 | 内容\r\n--- | ---\r\n[`train-images-idx3-ubyte.gz`](http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz) | 训练集图片 - 55000 张 训练图片, 5000 张 验证图片\r\n[`train-labels-idx1-ubyte.gz`](http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz) | 训练集图片对应的数字标签\r\n[`t10k-images-idx3-ubyte.gz`](http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz) | 测试集图片 - 10000 张 图片\r\n[`t10k-labels-idx1-ubyte.gz`](http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz) | 测试集图片对应的数字标签\r\n\r\n在 `input_data.py` 文件中,  `maybe_download()` 函数可以确保这些训练数据下载到本地文件夹中。\r\n\r\n文件夹的名字在\r\n`fully_connected_feed.py` 文件的顶部由一个标记变量指定，你可以根据自己的需要进行修改。\r\n### 解压 与 重构 <a class=\"md-anchor\" id=\"AUTOGENERATED-unpack-and-reshape\"></a>\r\n\r\n这些文件本身并没有使用标准的图片格式储存，并且需要使用`input_data.py`文件中`extract_images()` 和`extract_labels()`函数来手动解压（页面中有相关说明）。\r\n\r\n图片数据将被解压成2维的tensor：`[image index, pixel index]`\r\n其中每一项表示某一图片中特定像素的强度值, 范围从 `[0, 255]` 到 `[-0.5, 0.5]`。  \"image index\"代表数据集中图片的编号, 从0到数据集的上限值。\"pixel index\"代表该图片中像素点得个数, 从0到图片的像素上限值。\r\n\r\n以`train-*`开头的文件中包括60000个样本，其中分割出55000个样本作为训练集，其余的5000个样本作为验证集。因为所有数据集中28x28像素的灰度图片的尺寸为784，所以训练集输出的tensor格式为`[55000, 784]`。\r\n\r\n数字标签数据被解压称1维的tensor: `[image index]`，它定义了每个样本数值的类别分类。对于训练集的标签来说，这个数据规模就是:`[55000]`。\r\n\r\n### 数据集 对象 <a class=\"md-anchor\" id=\"AUTOGENERATED-dataset-object\"></a>\r\n\r\n底层的源码将会执行下载、解压、重构图片和标签数据来组成以下的数据集对象:\r\n\r\n数据集 | 目的\r\n--- | ---\r\n`data_sets.train` | 55000 组 图片和标签, 用于训练。\r\n`data_sets.validation` | 5000 组 图片和标签, 用于迭代验证训练的准确性。\r\n`data_sets.test` | 10000 组 图片和标签, 用于最终测试训练的准确性。\r\n\r\n执行`read_data_sets()`函数将会返回一个`DataSet`实例，其中包含了以上三个数据集。函数`DataSet.next_batch()`是用于获取以`batch_size`为大小的一个元组，其中包含了一组图片和标签，该元组会被用于当前的TensorFlow运算会话中。\r\n```python\r\nimages_feed, labels_feed = data_set.next_batch(FLAGS.batch_size)\r\n```\r\n原文地址：[MNIST Data Download](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/tutorials/mnist/download/index.md) 翻译：[btpeter](https://github.com/btpeter) 校对：waiwaizheng"
  },
  {
    "path": "SOURCE/tutorials/mnist_pros.md",
    "content": "# 深入MNIST <a class=\"md-anchor\" id=\"AUTOGENERATED-deep-mnist-for-experts\"></a>\r\n\r\nTensorFlow是一个非常强大的用来做大规模数值计算的库。其所擅长的任务之一就是实现以及训练深度神经网络。\r\n\r\n在本教程中，我们将学到构建一个TensorFlow模型的基本步骤，并将通过这些步骤为MNIST构建一个深度卷积神经网络。\r\n\r\n*这个教程假设你已经熟悉神经网络和MNIST数据集。如果你尚未了解，请查看[新手指南](./mnist_beginners.md).*\r\n\r\n## 安装 <a class=\"md-anchor\" id=\"AUTOGENERATED-setup\"></a>\r\n\r\n在创建模型之前，我们会先加载MNIST数据集，然后启动一个TensorFlow的session。\r\n\r\n### 加载MNIST数据 <a class=\"md-anchor\" id=\"AUTOGENERATED-load-mnist-data\"></a>\r\n\r\n为了方便起见，我们已经准备了[一个脚本](https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/tutorials/mnist/input_data.py)来自动下载和导入MNIST数据集。它会自动创建一个`'MNIST_data'`的目录来存储数据。\r\n\r\n```python\r\nimport input_data\r\nmnist = input_data.read_data_sets('MNIST_data', one_hot=True)\r\n```\r\n\r\n这里，`mnist`是一个轻量级的类。它以Numpy数组的形式存储着训练、校验和测试数据集。同时提供了一个函数，用于在迭代中获得minibatch，后面我们将会用到。\r\n\r\n### 运行TensorFlow的InteractiveSession <a class=\"md-anchor\" id=\"AUTOGENERATED-start-tensorflow-interactivesession\"></a>\r\n\r\nTensorflow依赖于一个高效的C++后端来进行计算。与后端的这个连接叫做session。一般而言，使用TensorFlow程序的流程是先创建一个图，然后在session中启动它。\r\n\r\n这里，我们使用更加方便的`InteractiveSession`类。通过它，你可以更加灵活地构建你的代码。它能让你在运行图的时候，插入一些[计算图](../get_started/basic_usage.md#the-computation-graph)，这些计算图是由某些操作(operations)构成的。这对于工作在交互式环境中的人们来说非常便利，比如使用IPython。如果你没有使用`InteractiveSession`，那么你需要在启动session之前构建整个计算图，然后[启动该计算图](../get_started/basic_usage.md#launching-the-graph-in-a-session)。\r\n\r\n```python\r\nimport tensorflow as tf\r\nsess = tf.InteractiveSession()\r\n```\r\n\r\n#### 计算图 <a class=\"md-anchor\" id=\"AUTOGENERATED-computation-graph\"></a>\r\n\r\n为了在Python中进行高效的数值计算，我们通常会使用像NumPy一类的库，将一些诸如矩阵乘法的耗时操作在Python环境的外部来计算，这些计算通常会通过其它语言并用更为高效的代码来实现。\r\n\r\n但遗憾的是，每一个操作切换回Python环境时仍需要不小的开销。如果你想在GPU或者分布式环境中计算时，这一开销更加可怖，这一开销主要可能是用来进行数据迁移。\r\n\r\nTensorFlow也是在Python外部完成其主要工作，但是进行了改进以避免这种开销。其并没有采用在Python外部独立运行某个耗时操作的方式，而是先让我们描述一个交互操作图，然后完全将其运行在Python外部。这与Theano或Torch的做法类似。\r\n\r\n因此Python代码的目的是用来构建这个可以在外部运行的计算图，以及安排计算图的哪一部分应该被运行。详情请查看[基本用法](../../../get_started/basic_usage.md)中的[计算图表](../../../get_started/basic_usage.md#the-computation-graph)一节。\r\n\r\n## 构建Softmax 回归模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-build-a-softmax-regression-model\"></a>\r\n\r\n在这一节中我们将建立一个拥有一个线性层的softmax回归模型。在下一节，我们会将其扩展为一个拥有多层卷积网络的softmax回归模型。\r\n\r\n### 占位符 <a class=\"md-anchor\" id=\"AUTOGENERATED-placeholders\"></a>\r\n\r\n我们通过为输入图像和目标输出类别创建节点，来开始构建计算图。\r\n\r\n```python\r\nx = tf.placeholder(\"float\", shape=[None, 784])\r\ny_ = tf.placeholder(\"float\", shape=[None, 10])\r\n```\r\n\r\n这里的`x`和`y`并不是特定的值，相反，他们都只是一个`占位符`，可以在TensorFlow运行某一计算时根据该占位符输入具体的值。\r\n\r\n输入图片`x`是一个2维的浮点数张量。这里，分配给它的`shape`为`[None, 784]`，其中`784`是一张展平的MNIST图片的维度。`None`表示其值大小不定，在这里作为第一个维度值，用以指代batch的大小，意即`x`的数量不定。输出类别值`y_`也是一个2维张量，其中每一行为一个10维的one-hot向量,用于代表对应某一MNIST图片的类别。\r\n\r\n虽然`placeholder`的`shape`参数是可选的，但有了它，TensorFlow能够自动捕捉因数据维度不一致导致的错误。\r\n\r\n### 变量 <a class=\"md-anchor\" id=\"AUTOGENERATED-variables\"></a>\r\n\r\n我们现在为模型定义权重`W`和偏置`b`。可以将它们当作额外的输入量，但是TensorFlow有一个更好的处理方式：`变量`。一个`变量`代表着TensorFlow计算图中的一个值，能够在计算过程中使用，甚至进行修改。在机器学习的应用过程中，模型参数一般用`Variable`来表示。\r\n\r\n```python\r\nW = tf.Variable(tf.zeros([784,10]))\r\nb = tf.Variable(tf.zeros([10]))\r\n```\r\n\r\n我们在调用`tf.Variable`的时候传入初始值。在这个例子里，我们把`W`和`b`都初始化为零向量。`W`是一个784x10的矩阵（因为我们有784个特征和10个输出值）。`b`是一个10维的向量（因为我们有10个分类）。\r\n\r\n\r\nBefore `Variable`s can be used within a session, they must be initialized using\r\nthat session.\r\nThis step takes the initial values (in this case tensors full of zeros) that\r\nhave already been specified, and assigns them to each `Variable`. This can be\r\ndone for all `Variables` at once.\r\n\r\n`变量`需要通过seesion初始化后，才能在session中使用。这一初始化步骤为，为初始值指定具体值（本例当中是全为零），并将其分配给每个`变量`,可以一次性为所有`变量`完成此操作。\r\n\r\n```python\r\nsess.run(tf.initialize_all_variables())\r\n```\r\n\r\n### 类别预测与损失函数 <a class=\"md-anchor\" id=\"AUTOGENERATED-predicted-class-and-cost-function\"></a>\r\n\r\n现在我们可以实现我们的回归模型了。这只需要一行！我们把向量化后的图片`x`和权重矩阵`W`相乘，加上偏置`b`，然后计算每个分类的softmax概率值。\r\n\r\n```python\r\ny = tf.nn.softmax(tf.matmul(x,W) + b)\r\n```\r\n可以很容易的为训练过程指定最小化误差用的损失函数，我们的损失函数是目标类别和预测类别之间的交叉熵。\r\n\r\n```python\r\ncross_entropy = -tf.reduce_sum(y_*tf.log(y))\r\n```\r\n\r\n注意，`tf.reduce_sum`把minibatch里的每张图片的交叉熵值都加起来了。我们计算的交叉熵是指整个minibatch的。\r\n\r\n## 训练模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-train-the-model\"></a>\r\n\r\n我们已经定义好模型和训练用的损失函数，那么用TensorFlow进行训练就很简单了。因为TensorFlow知道整个计算图，它可以使用自动微分法找到对于各个变量的损失的梯度值。TensorFlow有[大量内置的优化算法](../../../api_docs/python/train.md#optimizers) 这个例子中，我们用最速下降法让交叉熵下降，步长为0.01.\r\n\r\n```python\r\ntrain_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)\r\n```\r\n\r\n这一行代码实际上是用来往计算图上添加一个新操作，其中包括计算梯度，计算每个参数的步长变化，并且计算出新的参数值。\r\n\r\n返回的`train_step`操作对象，在运行时会使用梯度下降来更新参数。因此，整个模型的训练可以通过反复地运行`train_step`来完成。\r\n\r\n```python\r\nfor i in range(1000):\r\n  batch = mnist.train.next_batch(50)\r\n  train_step.run(feed_dict={x: batch[0], y_: batch[1]})\r\n```\r\n\r\n每一步迭代，我们都会加载50个训练样本，然后执行一次`train_step`，并通过`feed_dict`将`x` 和 `y_`张量`占位符`用训练训练数据替代。\r\n\r\n注意，在计算图中，你可以用`feed_dict`来替代任何张量，并不仅限于替换`占位符`。\r\n\r\n### 评估模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-evaluate-the-model\"></a>\r\n\r\n那么我们的模型性能如何呢？\r\n\r\n首先让我们找出那些预测正确的标签。`tf.argmax` 是一个非常有用的函数，它能给出某个tensor对象在某一维上的其数据最大值所在的索引值。由于标签向量是由0,1组成，因此最大值1所在的索引位置就是类别标签，比如`tf.argmax(y,1)`返回的是模型对于任一输入x预测到的标签值，而 `tf.argmax(y_,1)` 代表正确的标签，我们可以用 `tf.equal` 来检测我们的预测是否真实标签匹配(索引位置一样表示匹配)。\r\n\r\n```python\r\ncorrect_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\r\n```\r\n\r\n这里返回一个布尔数组。为了计算我们分类的准确率，我们将布尔值转换为浮点数来代表对、错，然后取平均值。例如：`[True, False, True, True]`变为`[1,0,1,1]`，计算出平均值为`0.75`。\r\n\r\n```python\r\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\r\n```\r\n\r\n最后，我们可以计算出在测试数据上的准确率，大概是91%。\r\n\r\n```python\r\nprint accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels})\r\n```\r\n\r\n## 构建一个多层卷积网络 <a class=\"md-anchor\" id=\"AUTOGENERATED-build-a-multilayer-convolutional-network\"></a>\r\n\r\n在MNIST上只有91%正确率，实在太糟糕。在这个小节里，我们用一个稍微复杂的模型：卷积神经网络来改善效果。这会达到大概99.2%的准确率。虽然不是最高，但是还是比较让人满意。\r\n\r\n### 权重初始化 <a class=\"md-anchor\" id=\"AUTOGENERATED-weight-initialization\"></a>\r\n\r\n为了创建这个模型，我们需要创建大量的权重和偏置项。这个模型中的权重在初始化时应该加入少量的噪声来打破对称性以及避免0梯度。由于我们使用的是ReLU神经元，因此比较好的做法是用一个较小的正数来初始化偏置项，以避免神经元节点输出恒为0的问题（dead neurons）。为了不在建立模型的时候反复做初始化操作，我们定义两个函数用于初始化。\r\n\r\n```python\r\ndef weight_variable(shape):\r\n  initial = tf.truncated_normal(shape, stddev=0.1)\r\n  return tf.Variable(initial)\r\n\r\ndef bias_variable(shape):\r\n  initial = tf.constant(0.1, shape=shape)\r\n  return tf.Variable(initial)\r\n```\r\n\r\n### 卷积和池化 <a class=\"md-anchor\" id=\"AUTOGENERATED-convolution-and-pooling\"></a>\r\n\r\nTensorFlow在卷积和池化上有很强的灵活性。我们怎么处理边界？步长应该设多大？在这个实例里，我们会一直使用vanilla版本。我们的卷积使用1步长（stride size），0边距（padding size）的模板，保证输出和输入是同一个大小。我们的池化用简单传统的2x2大小的模板做max pooling。为了代码更简洁，我们把这部分抽象成一个函数。\r\n\r\n```python\r\ndef conv2d(x, W):\r\n  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')\r\n\r\ndef max_pool_2x2(x):\r\n  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],\r\n                        strides=[1, 2, 2, 1], padding='SAME')\r\n```\r\n\r\n### 第一层卷积 <a class=\"md-anchor\" id=\"AUTOGENERATED-first-convolutional-layer\"></a>\r\n\r\n现在我们可以开始实现第一层了。它由一个卷积接一个max pooling完成。卷积在每个5x5的patch中算出32个特征。卷积的权重张量形状是`[5, 5, 1, 32]`，前两个维度是patch的大小，接着是输入的通道数目，最后是输出的通道数目。 而对于每一个输出通道都有一个对应的偏置量。\r\n\r\n```python\r\nW_conv1 = weight_variable([5, 5, 1, 32])\r\nb_conv1 = bias_variable([32])\r\n```\r\n为了用这一层，我们把`x`变成一个4d向量，其第2、第3维对应图片的宽、高，最后一维代表图片的颜色通道数(因为是灰度图所以这里的通道数为1，如果是rgb彩色图，则为3)。\r\n\r\n```python\r\nx_image = tf.reshape(x, [-1,28,28,1])\r\n```\r\n\r\nWe then convolve `x_image` with the weight tensor, add the\r\nbias, apply the ReLU function, and finally max pool.\r\n我们把`x_image`和权值向量进行卷积，加上偏置项，然后应用ReLU激活函数，最后进行max pooling。 \r\n\r\n```python\r\nh_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)\r\nh_pool1 = max_pool_2x2(h_conv1)\r\n```\r\n\r\n### 第二层卷积 <a class=\"md-anchor\" id=\"AUTOGENERATED-second-convolutional-layer\"></a>\r\n\r\n为了构建一个更深的网络，我们会把几个类似的层堆叠起来。第二层中，每个5x5的patch会得到64个特征。\r\n\r\n```python\r\nW_conv2 = weight_variable([5, 5, 32, 64])\r\nb_conv2 = bias_variable([64])\r\n\r\nh_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)\r\nh_pool2 = max_pool_2x2(h_conv2)\r\n```\r\n\r\n### 密集连接层 <a class=\"md-anchor\" id=\"AUTOGENERATED-densely-connected-layer\"></a>\r\n\r\n现在，图片尺寸减小到7x7，我们加入一个有1024个神经元的全连接层，用于处理整个图片。我们把池化层输出的张量reshape成一些向量，乘上权重矩阵，加上偏置，然后对其使用ReLU。\r\n\r\n```python\r\nW_fc1 = weight_variable([7 * 7 * 64, 1024])\r\nb_fc1 = bias_variable([1024])\r\n\r\nh_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])\r\nh_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)\r\n```\r\n\r\n#### Dropout <a class=\"md-anchor\" id=\"AUTOGENERATED-dropout\"></a>\r\n\r\n为了减少过拟合，我们在输出层之前加入dropout。我们用一个`placeholder`来代表一个神经元的输出在dropout中保持不变的概率。这样我们可以在训练过程中启用dropout，在测试过程中关闭dropout。\r\nTensorFlow的`tf.nn.dropout`操作除了可以屏蔽神经元的输出外，还会自动处理神经元输出值的scale。所以用dropout的时候可以不用考虑scale。\r\n\r\n```python\r\nkeep_prob = tf.placeholder(\"float\")\r\nh_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)\r\n```\r\n\r\n### 输出层 <a class=\"md-anchor\" id=\"AUTOGENERATED-readout-layer\"></a>\r\n\r\n最后，我们添加一个softmax层，就像前面的单层softmax regression一样。\r\n\r\n```python\r\nW_fc2 = weight_variable([1024, 10])\r\nb_fc2 = bias_variable([10])\r\n\r\ny_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)\r\n```\r\n\r\n### 训练和评估模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-train-and-evaluate-the-model\"></a>\r\n\r\n这个模型的效果如何呢？\r\n\r\n为了进行训练和评估，我们使用与之前简单的单层SoftMax神经网络模型几乎相同的一套代码，只是我们会用更加复杂的ADAM优化器来做梯度最速下降，在`feed_dict`中加入额外的参数`keep_prob`来控制dropout比例。然后每100次迭代输出一次日志。\r\n\r\n```python\r\ncross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))\r\ntrain_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)\r\ncorrect_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))\r\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\r\nsess.run(tf.initialize_all_variables())\r\nfor i in range(20000):\r\n  batch = mnist.train.next_batch(50)\r\n  if i%100 == 0:\r\n    train_accuracy = accuracy.eval(feed_dict={\r\n        x:batch[0], y_: batch[1], keep_prob: 1.0})\r\n    print \"step %d, training accuracy %g\"%(i, train_accuracy)\r\n  train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})\r\n\r\nprint \"test accuracy %g\"%accuracy.eval(feed_dict={\r\n    x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})\r\n```\r\n\r\n以上代码，在最终测试集上的准确率大概是99.2%。\r\n\r\n目前为止，我们已经学会了用TensorFlow快捷地搭建、训练和评估一个复杂一点儿的深度学习模型。\r\n\r\n原文地址：[Deep MNIST for Experts](http://tensorflow.org/tutorials/mnist/pros/index.html) 翻译：[chenweican](https://github.com/chenweican) 校对：[HongyangWang](https://github.com/wanghong-yang)\r\n\r\n\n"
  },
  {
    "path": "SOURCE/tutorials/mnist_tf.md",
    "content": "# TensorFlow运作方式入门 <a class=\"md-anchor\" id=\"AUTOGENERATED-tensorflow-mechanics-101\"></a>\r\n\r\n代码：[tensorflow/g3doc/tutorials/mnist/](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/tutorials/mnist)\r\n\r\n本篇教程的目的，是向大家展示如何利用TensorFlow使用（经典）MNIST数据集训练并评估一个用于识别手写数字的简易前馈神经网络（feed-forward neural network）。我们的目标读者，是有兴趣使用TensorFlow的资深机器学习人士。\r\n\r\n因此，撰写该系列教程并不是为了教大家机器学习领域的基础知识。\r\n\r\n在学习本教程之前，请确保您已按照[安装TensorFlow](../get_started/os_setup.md)教程中的要求，完成了安装。\r\n\r\n## 教程使用的文件 <a class=\"md-anchor\" id=\"AUTOGENERATED-tutorial-files\"></a>\r\n\r\n\r\n本教程引用如下文件：\r\n\r\n文件 | 目的\r\n--- | ---\r\n[`mnist.py`](https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/tutorials/mnist/mnist.py) | 构建一个完全连接（fully connected）的MINST模型所需的代码。\r\n[`fully_connected_feed.py`](https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/tutorials/mnist/fully_connected_feed.py) | 利用下载的数据集训练构建好的MNIST模型的主要代码，以数据反馈字典（feed dictionary）的形式作为输入模型。\r\n\r\n\r\n只需要直接运行`fully_connected_feed.py`文件，就可以开始训练：\r\n\r\n`python fully_connected_feed.py`\r\n\r\n## 准备数据 <a class=\"md-anchor\" id=\"AUTOGENERATED-prepare-the-data\"></a>\r\n\r\nMNIST是机器学习领域的一个经典问题，指的是让机器查看一系列大小为28x28像素的手写数字灰度图像，并判断这些图像代表0-9中的哪一个数字。\r\n\r\n![MNIST手写数字](.././images/mnist_digits.png \"MNIST手写数字\")\r\n\r\n更多相关信息，请查阅[Yann LeCun网站中关于MNIST的介绍](http://yann.lecun.com/exdb/mnist/)\r\n或者[Chris Olah对MNIST的可视化探索](http://colah.github.io/posts/2014-10-Visualizing-MNIST/)。\r\n\r\n### 下载 <a class=\"md-anchor\" id=\"AUTOGENERATED-download\"></a>\r\n\r\n在`run_training()`方法的一开始，`input_data.read_data_sets()`函数会确保你的本地训练文件夹中，已经下载了正确的数据，然后将这些数据解压并返回一个含有`DataSet`实例的字典。\r\n\r\n```python\r\ndata_sets = input_data.read_data_sets(FLAGS.train_dir, FLAGS.fake_data)\r\n```\r\n\r\n**注意**：`fake_data`标记是用于单元测试的，读者可以不必理会。\r\n\r\n数据集 | 目的\r\n--- | ---\r\n`data_sets.train` | 55000个图像和标签（labels），作为主要训练集。\r\n`data_sets.validation` | 5000个图像和标签，用于迭代验证训练准确度。\r\n`data_sets.test` | 10000个图像和标签，用于最终测试训练准确度（trained accuracy）。\r\n\r\n了解更多数据有关信息，请查阅此系列教程的[数据下载](mnist/download/index.md)\r\n部分.\r\n\r\n### 输入与占位符（Inputs and Placeholders） <a class=\"md-anchor\" id=\"AUTOGENERATED-inputs-and-placeholders\"></a>\r\n\r\n`placeholder_inputs()`函数将生成两个[`tf.placeholder`](../api_docs/python/io_ops.md#placeholder)操作，定义传入图表中的shape参数，shape参数中包括`batch_size `值，后续还会将实际的训练用例传入图表。\r\n\r\n```python\r\nimages_placeholder = tf.placeholder(tf.float32, shape=(batch_size,\r\n                                                       IMAGE_PIXELS))\r\nlabels_placeholder = tf.placeholder(tf.int32, shape=(batch_size))\r\n```\r\n\r\n在训练循环（training loop）的后续步骤中，传入的整个图像和标签数据集会被切片，以符合每一个操作所设置的`batch_size`值，占位符操作将会填补以符合这个`batch_size`值。然后使用`feed_dict`参数，将数据传入`sess.run()`函数。\r\n\r\n## 构建图表 （Build the Graph）<a class=\"md-anchor\" id=\"AUTOGENERATED-build-the-graph\"></a>\r\n\r\n在为数据创建占位符之后，就可以运行`mnist.py`文件，经过三阶段的模式函数操作：`inference()`， `loss()`，和`training()`。图表就构建完成了。\r\n\r\n1.`inference()` —— 尽可能地构建好图表，满足促使神经网络向前反馈并做出预测的要求。\r\n\r\n2.`loss()` —— 往inference图表中添加生成损失（loss）所需要的操作（ops）。\r\n\r\n3.`training()` —— 往损失图表中添加计算并应用梯度（gradients）所需的操作。\r\n\r\n<div style=\"width:95%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n  <img style=\"width:100%\" src=\".././images/mnist_subgraph.png\">\r\n</div>\r\n\r\n### 推理（Inference） <a class=\"md-anchor\" id=\"AUTOGENERATED-inference\"></a>\r\n\r\n`inference()`函数会尽可能地构建图表，做到返回包含了预测结果（output prediction）的Tensor。\r\n\r\n它接受图像占位符为输入，在此基础上借助ReLu(Rectified Linear Units)激活函数，构建一对完全连接层（layers），以及一个有着十个节点（node）、指明了输出logits模型的线性层。\r\n\r\n每一层都创建于一个唯一的[`tf.name_scope`](../api_docs/python/framework.md#name_scope)之下，创建于该作用域之下的所有元素都将带有其前缀。\r\n\r\n```python\r\nwith tf.name_scope('hidden1') as scope:\r\n```\r\n\r\n在定义的作用域中，每一层所使用的权重和偏差都在[`tf.Variable`](../api_docs/python/state_ops.md#Variable)实例中生成，并且包含了各自期望的shape。\r\n\r\n```python\r\nweights = tf.Variable(\r\n    tf.truncated_normal([IMAGE_PIXELS, hidden1_units],\r\n                        stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))),\r\n    name='weights')\r\nbiases = tf.Variable(tf.zeros([hidden1_units]),\r\n                     name='biases')\r\n```\r\n\r\n例如，当这些层是在`hidden1`作用域下生成时，赋予权重变量的独特名称将会是\"`hidden1/weights`\"。\r\n\r\n每个变量在构建时，都会获得初始化操作（initializer ops）。\r\n\r\n在这种最常见的情况下，通过[`tf.truncated_normal`](../api_docs/python/constant_op.md#truncated_normal)函数初始化权重变量，给赋予的shape则是一个二维tensor，其中第一个维度代表该层中权重变量所连接（connect from）的单元数量，第二个维度代表该层中权重变量所连接到的（connect to）单元数量。对于名叫`hidden1`的第一层，相应的维度则是`[IMAGE_PIXELS, hidden1_units]`，因为权重变量将图像输入连接到了`hidden1`层。`tf.truncated_normal`初始函数将根据所得到的均值和标准差，生成一个随机分布。\r\n\r\n然后，通过[`tf.zeros`](../api_docs/python/constant_op.md#zeros)函数初始化偏差变量（biases），确保所有偏差的起始值都是0，而它们的shape则是其在该层中所接到的（connect to）单元数量。\r\n\r\n图表的三个主要操作，分别是两个[`tf.nn.relu`](../api_docs/python/nn.md#relu)操作，它们中嵌入了隐藏层所需的[`tf.matmul`](../api_docs/python/math_ops.md#matmul)；以及logits模型所需的另外一个`tf.matmul`。三者依次生成，各自的`tf.Variable`实例则与输入占位符或下一层的输出tensor所连接。\r\n\r\n```python\r\nhidden1 = tf.nn.relu(tf.matmul(images, weights) + biases)\r\n```\r\n\r\n```python\r\nhidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases)\r\n```\r\n\r\n```python\r\nlogits = tf.matmul(hidden2, weights) + biases\r\n```\r\n\r\n最后，程序会返回包含了输出结果的`logits`Tensor。\r\n\r\n### 损失（Loss）<a class=\"md-anchor\" id=\"AUTOGENERATED-loss\"></a>\r\n\r\n`loss()`函数通过添加所需的损失操作，进一步构建图表。\r\n\r\n首先，`labels_placeholer`中的值，将被编码为一个含有1-hot values的Tensor。例如，如果类标识符为“3”，那么该值就会被转换为：\r\n<br>`[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]`\r\n\r\n```python\r\nbatch_size = tf.size(labels)\r\nlabels = tf.expand_dims(labels, 1)\r\nindices = tf.expand_dims(tf.range(0, batch_size, 1), 1)\r\nconcated = tf.concat(1, [indices, labels])\r\nonehot_labels = tf.sparse_to_dense(\r\n    concated, tf.pack([batch_size, NUM_CLASSES]), 1.0, 0.0)\r\n```\r\n\r\n之后，又添加一个[`tf.nn.softmax_cross_entropy_with_logits`](../api_docs/python/nn.md#softmax_cross_entropy_with_logits)操作，用来比较`inference()`函数与1-hot标签所输出的logits Tensor。\r\n\r\n```python\r\ncross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits,\r\n                                                        onehot_labels,\r\n                                                        name='xentropy')\r\n```\r\n\r\n然后，使用[`tf.reduce_mean`](../api_docs/python/math_ops.md#reduce_mean)函数，计算batch维度（第一维度）下交叉熵（cross entropy）的平均值，将将该值作为总损失。\r\n\r\n```python\r\nloss = tf.reduce_mean(cross_entropy, name='xentropy_mean')\r\n```\r\n\r\n最后，程序会返回包含了损失值的Tensor。\r\n\r\n> 注意：交叉熵是信息理论中的概念，可以让我们描述如果基于已有事实，相信神经网络所做的推测最坏会导致什么结果。更多详情，请查阅博文《可视化信息理论》(http://colah.github.io/posts/2015-09-Visual-Information/)\r\n\r\n### 训练 <a class=\"md-anchor\" id=\"AUTOGENERATED-training\"></a>\r\n\r\n`training()`函数添加了通过梯度下降（gradient descent）将损失最小化所需的操作。\r\n\r\n首先，该函数从`loss()`函数中获取损失Tensor，将其交给[`tf.scalar_summary`](../api_docs/python/train.md#scalar_summary)，后者在与`SummaryWriter`（见下文）配合使用时，可以向事件文件（events file）中生成汇总值（summary values）。在本篇教程中，每次写入汇总值时，它都会释放损失Tensor的当前值（snapshot value）。\r\n\r\n```python\r\ntf.scalar_summary(loss.op.name, loss)\r\n```\r\n\r\n接下来，我们实例化一个[`tf.train.GradientDescentOptimizer`](../api_docs/python/train.md#GradientDescentOptimizer)，负责按照所要求的学习效率（learning rate）应用梯度下降法（gradients）。\r\n\r\n```python\r\noptimizer = tf.train.GradientDescentOptimizer(FLAGS.learning_rate)\r\n```\r\n\r\n之后，我们生成一个变量用于保存全局训练步骤（global training step）的数值，并使用[`minimize()`](../api_docs/python/train.md#Optimizer.minimize)函数更新系统中的三角权重（triangle weights）、增加全局步骤的操作。根据惯例，这个操作被称为 `train_op`，是TensorFlow会话（session）诱发一个完整训练步骤所必须运行的操作（见下文）。\r\n\r\n```python\r\nglobal_step = tf.Variable(0, name='global_step', trainable=False)\r\ntrain_op = optimizer.minimize(loss, global_step=global_step)\r\n```\r\n\r\n最后，程序返回包含了训练操作（training op）输出结果的Tensor。\r\n\r\n## 训练模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-train-the-model\"></a>\r\n\r\n一旦图表构建完毕，就通过`fully_connected_feed.py`文件中的用户代码进行循环地迭代式训练和评估。\r\n\r\n### 图表 <a class=\"md-anchor\" id=\"AUTOGENERATED-the-graph\"></a>\r\n\r\n在`run_training()`这个函数的一开始，是一个Python语言中的`with`命令，这个命令表明所有已经构建的操作都要与默认的[`tf.Graph`](../api_docs/python/framework.md#Graph)全局实例关联起来。\r\n\r\n```python\r\nwith tf.Graph().as_default():\r\n```\r\n\r\n`tf.Graph`实例是一系列可以作为整体执行的操作。TensorFlow的大部分场景只需要依赖默认图表一个实例即可。\r\n\r\n利用多个图表的更加复杂的使用场景也是可能的，但是超出了本教程的范围。\r\n\r\n### 会话 <a class=\"md-anchor\" id=\"AUTOGENERATED-the-session\"></a>\r\n\r\n完成全部的构建准备、生成全部所需的操作之后，我们就可以创建一个[`tf.Session`](../api_docs/python/client.md#Session)，用于运行图表。\r\n\r\n```python\r\nsess = tf.Session()\r\n```\r\n\r\n另外，也可以利用`with`代码块生成`Session`，限制作用域：\r\n\r\n```python\r\nwith tf.Session() as sess:\r\n```\r\n\r\n`Session`函数中没有传入参数，表明该代码将会依附于（如果还没有创建会话，则会创建新的会话）默认的本地会话。\r\n\r\n生成会话之后，所有`tf.Variable`实例都会立即通过调用各自初始化操作中的[`sess.run()`](../api_docs/python/client.md#Session.run)函数进行初始化。\r\n\r\n```python\r\ninit = tf.initialize_all_variables()\r\nsess.run(init)\r\n```\r\n\r\n[`sess.run()`](../api_docs/python/client.md#Session.run)方法将会运行图表中与作为参数传入的操作相对应的完整子集。在初次调用时，`init`操作只包含了变量初始化程序[`tf.group`](../api_docs/python/control_flow_ops.md#group)。图表的其他部分不会在这里，而是在下面的训练循环运行。\r\n\r\n### 训练循环 <a class=\"md-anchor\" id=\"AUTOGENERATED-train-loop\"></a>\r\n\r\n完成会话中变量的初始化之后，就可以开始训练了。\r\n\r\n训练的每一步都是通过用户代码控制，而能实现有效训练的最简单循环就是：\r\n\r\n```python\r\nfor step in xrange(max_steps):\r\n    sess.run(train_op)\r\n```\r\n\r\n但是，本教程中的例子要更为复杂一点，原因是我们必须把输入的数据根据每一步的情况进行切分，以匹配之前生成的占位符。\r\n\r\n#### 向图表提供反馈 <a class=\"md-anchor\" id=\"AUTOGENERATED-feed-the-graph\"></a>\r\n\r\n执行每一步时，我们的代码会生成一个反馈字典（feed dictionary），其中包含对应步骤中训练所要使用的例子，这些例子的哈希键就是其所代表的占位符操作。\r\n\r\n`fill_feed_dict`函数会查询给定的`DataSet`，索要下一批次`batch_size`的图像和标签，与占位符相匹配的Tensor则会包含下一批次的图像和标签。\r\n\r\n```python\r\nimages_feed, labels_feed = data_set.next_batch(FLAGS.batch_size)\r\n```\r\n\r\n然后，以占位符为哈希键，创建一个Python字典对象，键值则是其代表的反馈Tensor。\r\n\r\n```python\r\nfeed_dict = {\r\n    images_placeholder: images_feed,\r\n    labels_placeholder: labels_feed,\r\n}\r\n```\r\n\r\n这个字典随后作为`feed_dict`参数，传入`sess.run()`函数中，为这一步的训练提供输入样例。\r\n\r\n#### 检查状态 <a class=\"md-anchor\" id=\"AUTOGENERATED-check-the-status\"></a>\r\n\r\n在运行`sess.run`函数时，要在代码中明确其需要获取的两个值：`[train_op, loss]`。\r\n\r\n```python\r\nfor step in xrange(FLAGS.max_steps):\r\n    feed_dict = fill_feed_dict(data_sets.train,\r\n                               images_placeholder,\r\n                               labels_placeholder)\r\n    _, loss_value = sess.run([train_op, loss],\r\n                             feed_dict=feed_dict)\r\n```\r\n\r\n因为要获取这两个值，`sess.run()`会返回一个有两个元素的元组。其中每一个`Tensor`对象，对应了返回的元组中的numpy数组，而这些数组中包含了当前这步训练中对应Tensor的值。由于`train_op`并不会产生输出，其在返回的元祖中的对应元素就是`None`，所以会被抛弃。但是，如果模型在训练中出现偏差，`loss` Tensor的值可能会变成NaN，所以我们要获取它的值，并记录下来。\r\n\r\n假设训练一切正常，没有出现NaN，训练循环会每隔100个训练步骤，就打印一行简单的状态文本，告知用户当前的训练状态。\r\n\r\n```python\r\nif step % 100 == 0:\r\n    print 'Step %d: loss = %.2f (%.3f sec)' % (step, loss_value, duration)\r\n```\r\n\r\n#### 状态可视化 <a class=\"md-anchor\" id=\"AUTOGENERATED-visualize-the-status\"></a>\r\n\r\n为了释放[TensorBoard](../how_tos/summaries_and_tensorboard.md)所使用的事件文件（events file），所有的即时数据（在这里只有一个）都要在图表构建阶段合并至一个操作（op）中。\r\n\r\n```python\r\nsummary_op = tf.merge_all_summaries()\r\n```\r\n\r\n在创建好会话（session）之后，可以实例化一个[`tf.train.SummaryWriter`](../api_docs/python/train.md#SummaryWriter)，用于写入包含了图表本身和即时数据具体值的事件文件。\r\n\r\n```python\r\nsummary_writer = tf.train.SummaryWriter(FLAGS.train_dir,\r\n                                        graph_def=sess.graph_def)\r\n```\r\n\r\n最后，每次运行`summary_op`时，都会往事件文件中写入最新的即时数据，函数的输出会传入事件文件读写器（writer）的`add_summary()`函数。。\r\n\r\n```python\r\nsummary_str = sess.run(summary_op, feed_dict=feed_dict)\r\nsummary_writer.add_summary(summary_str, step)\r\n```\r\n\r\n事件文件写入完毕之后，可以就训练文件夹打开一个TensorBoard，查看即时数据的情况。\r\n\r\n![MNIST TensorBoard](../images/mnist_tensorboard.png \"MNIST TensorBoard\")\r\n\r\n**注意**：了解更多如何构建并运行TensorBoard的信息，请查看相关教程[Tensorboard：训练过程可视化](../how_tos/summaries_and_tensorboard.md)。\r\n\r\n#### 保存检查点（checkpoint）\r\n\r\n为了得到可以用来后续恢复模型以进一步训练或评估的检查点文件（checkpoint file），我们实例化一个[`tf.train.Saver`](../api_docs/python/state_ops.md#Saver)。\r\n\r\n```python\r\nsaver = tf.train.Saver()\r\n```\r\n\r\n在训练循环中，将定期调用[`saver.save()`](../api_docs/python/state_ops.md#Saver.save)方法，向训练文件夹中写入包含了当前所有可训练变量值得检查点文件。\r\n\r\n```python\r\nsaver.save(sess, FLAGS.train_dir, global_step=step)\r\n```\r\n\r\n这样，我们以后就可以使用[`saver.restore()`](../api_docs/python/state_ops.md#Saver.restore)方法，重载模型的参数，继续训练。\r\n\r\n```python\r\nsaver.restore(sess, FLAGS.train_dir)\r\n```\r\n## 评估模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-evaluate-the-model\"></a>\r\n\r\n每隔一千个训练步骤，我们的代码会尝试使用训练数据集与测试数据集，对模型进行评估。`do_eval`函数会被调用三次，分别使用训练数据集、验证数据集合测试数据集。\r\n\r\n```python\r\nprint 'Training Data Eval:'\r\ndo_eval(sess,\r\n        eval_correct,\r\n        images_placeholder,\r\n        labels_placeholder,\r\n        data_sets.train)\r\nprint 'Validation Data Eval:'\r\ndo_eval(sess,\r\n        eval_correct,\r\n        images_placeholder,\r\n        labels_placeholder,\r\n        data_sets.validation)\r\nprint 'Test Data Eval:'\r\ndo_eval(sess,\r\n        eval_correct,\r\n        images_placeholder,\r\n        labels_placeholder,\r\n        data_sets.test)\r\n```\r\n\r\n>注意，更复杂的使用场景通常是，先隔绝`data_sets.test`测试数据集，只有在大量的超参数优化调整（hyperparameter tuning）之后才进行检查。但是，由于MNIST问题比较简单，我们在这里一次性评估所有的数据。\r\n\r\n### 构建评估图表（Eval Graph）<a class=\"md-anchor\" id=\"AUTOGENERATED-build-the-eval-graph\"></a>\r\n\r\n在打开默认图表（Graph）之前，我们应该先调用`get_data(train=False)`函数，抓取测试数据集。\r\n\r\n```python\r\ntest_all_images, test_all_labels = get_data(train=False)\r\n```\r\n\r\n在进入训练循环之前，我们应该先调用`mnist.py`文件中的`evaluation`函数，传入的logits和标签参数要与`loss`函数的一致。这样做事为了先构建Eval操作。\r\n\r\n```python\r\neval_correct = mnist.evaluation(logits, labels_placeholder)\r\n```\r\n\r\n`evaluation`函数会生成[`tf.nn.in_top_k`](../api_docs/python/nn.md#in_top_k)\r\n操作，如果在K个最有可能的预测中可以发现真的标签，那么这个操作就会将模型输出标记为正确。在本文中，我们把K的值设置为1，也就是只有在预测是真的标签时，才判定它是正确的。\r\n\r\n```python\r\neval_correct = tf.nn.in_top_k(logits, labels, 1)\r\n```\r\n\r\n### 评估图表的输出（Eval Output） <a class=\"md-anchor\" id=\"AUTOGENERATED-eval-output\"></a>\r\n\r\n之后，我们可以创建一个循环，往其中添加`feed_dict`，并在调用`sess.run()`函数时传入`eval_correct`操作，目的就是用给定的数据集评估模型。\r\n\r\n```python\r\nfor step in xrange(steps_per_epoch):\r\n    feed_dict = fill_feed_dict(data_set,\r\n                               images_placeholder,\r\n                               labels_placeholder)\r\n    true_count += sess.run(eval_correct, feed_dict=feed_dict)\r\n```\r\n\r\n`true_count`变量会累加所有`in_top_k`操作判定为正确的预测之和。接下来，只需要将正确测试的总数，除以例子总数，就可以得出准确率了。\r\n\r\n```python\r\nprecision = float(true_count) / float(num_examples)\r\nprint '  Num examples: %d  Num correct: %d  Precision @ 1: %0.02f' % (\r\n    num_examples, true_count, precision)\r\n```\r\n\r\n> 原文：[TensorFlow Mechanics 101](http://www.tensorflow.org/tutorials/mnist/tf/index.md)\r\n> 翻译：[bingjin](https://github.com/bingjin)\r\n> 校对：[LichAmnesia](https://github.com/LichAmnesia)\r\n"
  },
  {
    "path": "SOURCE/tutorials/overview.md",
    "content": "# 综述\r\n\r\n## 面向机器学习初学者的 MNIST 初级教程\r\n\r\n如果你是机器学习领域的新手, 我们推荐你从本文开始阅读. 本文通过讲述一个经典的问题, 手写数字识别 (MNIST), 让你对多类分类 (multiclass classification) 问题有直观的了解.\r\n\r\n[阅读教程](../tutorials/mnist_beginners.md)\r\n\r\n## 面向机器学习专家的 MNIST 高级教程\r\n\r\n如果你已经对其它深度学习软件比较熟悉, 并且也对 MNIST 很熟悉, 这篇教程能够引导你对 TensorFlow 有初步了解.\r\n\r\n[阅读教程](../tutorials/mnist_pros.md)\r\n\r\n## TensorFlow 使用指南\r\n\r\n这是一篇技术教程, 详细介绍了如何使用 TensorFlow 架构训练大规模模型. 本文继续使用MNIST 作为例子.\r\n\r\n[阅读教程](../tutorials/mnist_tf.md)\r\n\r\n## 卷积神经网络 \r\n\r\n这篇文章介绍了如何使用 TensorFlow 在 CIFAR-10 数据集上训练卷积神经网络. 卷积神经网络是为图像识别量身定做的一个模型. 相比其它模型, 该模型利用了平移不变性(translation invariance), 从而能够更更简洁有效地表示视觉内容.\r\n\r\n[阅读教程](../tutorials/deep_cnn.md)\r\n\r\n## 单词的向量表示\r\n\r\n本文让你了解为什么学会使用向量来表示单词, 即单词嵌套 (word embedding), 是一件很有用的事情. 文章中介绍的 word2vec 模型, 是一种高效学习嵌套的方法. 本文还涉及了对比噪声(noise-contrastive) 训练方法的一些高级细节, 该训练方法是训练嵌套领域最近最大的进展. \r\n\r\n[阅读教程](../tutorials/word2vec.md)\r\n\r\n## 循环神经网络 (Recurrent Neural Network, 简称 RNN)\r\n\r\n一篇 RNN 的介绍文章, 文章中训练了一个 LSTM 网络来预测一个英文句子的下一个单词(该任务有时候被称作语言建模).\r\n\r\n[阅读教程](../tutorials/recurrent.md)\r\n\r\n## 序列到序列模型 (Sequence-to-Sequence Model)\r\n\r\nRNN 教程的后续, 该教程采用序列到序列模型进行机器翻译. 你将学会构建一个完全基于机器学习,端到端的 `英语-法语` 翻译器.\r\n\r\n[阅读教程](../tutorials/seq2seq.md)\r\n\r\n## Mandelbrot 集合\r\n\r\nTensorFlow 可以用于与机器学习完全无关的其它计算领域. 这里实现了一个原生的 Mandelbrot 集合的可视化程序.\r\n\r\n[阅读教程](../tutorials/mandelbrot.md)\r\n\r\n## 偏微分方程\r\n\r\n这是另外一个非机器学习计算的例子, 我们利用一个原生实现的偏微分方程, 对雨滴落在池塘上的过程进行仿真.\r\n\r\n[阅读教程](../tutorials/pdes.md)\r\n\r\n## MNIST 数据下载\r\n\r\n一篇关于下载 MNIST 手写识别数据集的详细教程.\r\n\r\n[阅读教程](../tutorials/mnist_download.md)\r\n\r\n## 视觉物体识别 (Visual Object Recognition)\r\n\r\n我们将毫无保留地发布已经选训练好的, 目前最先进的 Inception 物体识别模型.\r\n\r\n敬请期待...\r\n\r\n## Deep Dream 视幻觉软件\r\n\r\n我们将发布一个 TensorFlow 版本的 [Deep Dream](https://github.com/google/deepdream),这是一款基于 Inception 识别模型的神经网络视幻觉软件.\r\n\r\n敬请期待...\r\n\r\n\r\n<div class='sections-order' style=\"display: none;\">\r\n<!-- mnist/beginners/index.md -->\r\n<!-- mnist/pros/index.md -->\r\n<!-- mnist/tf/index.md -->\r\n<!-- deep_cnn/index.md -->\r\n<!-- word2vec/index.md -->\r\n<!-- recurrent/index.md -->\r\n<!-- seq2seq/index.md -->\r\n<!-- mandelbrot/index.md -->\r\n<!-- pdes/index.md -->\r\n<!-- mnist/download/index.md -->\r\n</div>\r\n\r\n> 原文：[Overview](http://tensorflow.org/tutorials)  翻译：[@doc001](https://github.com/PFZheng)  校对：[@eric_xu](https://github.com/ericxk)\r\n\r\n\r\n"
  },
  {
    "path": "SOURCE/tutorials/pdes/index.md",
    "content": "# 偏积分方程 <a class=\"md-anchor\" id=\"AUTOGENERATED-partial-differential-equations\"></a>\r\n\r\n ***TensorFlow*** 不仅仅是用来机器学习，它更可以用来模拟仿真。在这里，我们将通过模拟仿真几滴落入一块方形水池的雨点的例子，来引导您如何使用 ***TensorFlow*** 中的偏积分方程来模拟仿真的基本使用方法。\r\n\r\n>注：本教程最初是准备做为一个 **IPython** 的手册。\r\n>>译者注:关于偏积分方程的相关知识，译者推荐读者查看 [**网易公开课**](http://open.163.com/) 上的[**《麻省理工学院公开课：多变量微积分》**](http://open.163.com/special/opencourse/multivariable.html)课程。\r\n\r\n## 基本设置 <a class=\"md-anchor\" id=\"AUTOGENERATED-basic-setup\"></a>\r\n\r\n首先,我们需要导入一些必要的引用。\r\n\r\n```python\r\n#Import libraries for simulation\r\nimport tensorflow as tf\r\nimport numpy as np\r\n\r\n#Imports for visualization\r\nimport PIL.Image\r\nfrom cStringIO import StringIO\r\nfrom IPython.display import clear_output, Image, display\r\n```\r\n\r\n然后，我们还需要一个用于表示池塘表面状态的函数。\r\n\r\n```python\r\ndef DisplayArray(a, fmt='jpeg', rng=[0,1]):\r\n  \"\"\"Display an array as a picture.\"\"\"\r\n  a = (a - rng[0])/float(rng[1] - rng[0])*255\r\n  a = np.uint8(np.clip(a, 0, 255))\r\n  f = StringIO()\r\n  PIL.Image.fromarray(a).save(f, fmt)\r\n  display(Image(data=f.getvalue()))\r\n```\r\n\r\n最后，为了方便演示，这里我们需要打开一个交互的 ***TensorFlow*** 会话。当然为了以后能方便调用，我们可以把相关代码写到一个可以执行的***Python***文件中。\r\n\r\n```python\r\nsess = tf.InteractiveSession()\r\n```\r\n\r\n## 定义计算函数 <a class=\"md-anchor\" id=\"AUTOGENERATED-computational-convenience-functions\"></a>\r\n\r\n\r\n```python\r\ndef make_kernel(a):\r\n  \"\"\"Transform a 2D array into a convolution kernel\"\"\"\r\n  a = np.asarray(a)\r\n  a = a.reshape(list(a.shape) + [1,1])\r\n  return tf.constant(a, dtype=1)\r\n\r\ndef simple_conv(x, k):\r\n  \"\"\"A simplified 2D convolution operation\"\"\"\r\n  x = tf.expand_dims(tf.expand_dims(x, 0), -1)\r\n  y = tf.nn.depthwise_conv2d(x, k, [1, 1, 1, 1], padding='SAME')\r\n  return y[0, :, :, 0]\r\n\r\ndef laplace(x):\r\n  \"\"\"Compute the 2D laplacian of an array\"\"\"\r\n  laplace_k = make_kernel([[0.5, 1.0, 0.5],\r\n                           [1.0, -6., 1.0],\r\n                           [0.5, 1.0, 0.5]])\r\n  return simple_conv(x, laplace_k)\r\n```\r\n\r\n## 定义偏积分方程 <a class=\"md-anchor\" id=\"AUTOGENERATED-define-the-pde\"></a>\r\n\r\n首先,我们需要创建一个完美的 500 × 500 的正方形池塘,就像是我们在现实中找到的一样。.\r\n\r\n```python\r\nN = 500\r\n```\r\n\r\n然后，我们需要创建了一个池塘和几滴将要坠入池塘的雨滴。\r\n\r\n```python\r\n# Initial Conditions -- some rain drops hit a pond\r\n\r\n# Set everything to zero\r\nu_init = np.zeros([N, N], dtype=\"float32\")\r\nut_init = np.zeros([N, N], dtype=\"float32\")\r\n\r\n# Some rain drops hit a pond at random points\r\nfor n in range(40):\r\n  a,b = np.random.randint(0, N, 2)\r\n  u_init[a,b] = np.random.uniform()\r\n\r\nDisplayArray(u_init, rng=[-0.1, 0.1])\r\n```\r\n\r\n![jpeg](pde_output_1.jpg)\r\n\r\n现在，让我们来指定该微分方程的一些详细参数。\r\n\r\n```python\r\n# Parameters:\r\n# eps -- time resolution\r\n# damping -- wave damping\r\neps = tf.placeholder(tf.float32, shape=())\r\ndamping = tf.placeholder(tf.float32, shape=())\r\n\r\n# Create variables for simulation state\r\nU  = tf.Variable(u_init)\r\nUt = tf.Variable(ut_init)\r\n\r\n# Discretized PDE update rules\r\nU_ = U + eps * Ut\r\nUt_ = Ut + eps * (laplace(U) - damping * Ut)\r\n\r\n# Operation to update the state\r\nstep = tf.group(\r\n  U.assign(U_),\r\n  Ut.assign(Ut_))\r\n```\r\n\r\n## 开始仿真 <a class=\"md-anchor\" id=\"AUTOGENERATED-run-the-simulation\"></a>\r\n\r\n为了能看清仿真效果，我们可以用一个简单的 **for** 循环来远行我们的仿真程序。\r\n\r\n```python\r\n# Initialize state to initial conditions\r\ntf.initialize_all_variables().run()\r\n\r\n# Run 1000 steps of PDE\r\nfor i in range(1000):\r\n  # Step simulation\r\n  step.run({eps: 0.03, damping: 0.04})\r\n  # Visualize every 50 steps\r\n  if i % 50 == 0:\r\n    clear_output()\r\n    DisplayArray(U.eval(), rng=[-0.1, 0.1])\r\n```\r\n\r\n![jpeg](pde_output_2.jpg)\r\n\r\n看！！ 雨点落在池塘中,和现实中泛起了无数涟漪。\r\n\r\n> 原文链接:[http://tensorflow.org/tutorials/pdes/index.md](http://tensorflow.org/tutorials/pdes/index.md)    翻译:[@wangaicc](https://github.com/wangaicc)   校对： \r\n"
  },
  {
    "path": "SOURCE/tutorials/pdes.md",
    "content": "# 偏微分方程 <a class=\"md-anchor\" id=\"AUTOGENERATED-partial-differential-equations\"></a>\r\n\r\n***TensorFlow*** 不仅仅是用来机器学习，它更可以用来模拟仿真。在这里，我们将通过模拟仿真几滴落入一块方形水池的雨点的例子，来引导您如何使用 ***TensorFlow*** 中的偏微分方程来模拟仿真的基本使用方法。\r\n\r\n>注：本教程最初是准备做为一个 **IPython** 的手册。\r\n>>译者注:关于偏微分方程的相关知识，译者推荐读者查看 [**网易公开课**](http://open.163.com/) 上的[**《麻省理工学院公开课：多变量微积分》**](http://open.163.com/special/opencourse/multivariable.html)课程。\r\n\r\n## 基本设置 <a class=\"md-anchor\" id=\"AUTOGENERATED-basic-setup\"></a>\r\n\r\n首先,我们需要导入一些必要的引用。\r\n\r\n```python\r\n#导入模拟仿真需要的库\r\nimport tensorflow as tf\r\nimport numpy as np\r\n\r\n#导入可视化需要的库\r\nimport PIL.Image\r\nfrom cStringIO import StringIO\r\nfrom IPython.display import clear_output, Image, display\r\n```\r\n\r\n然后，我们还需要一个用于表示池塘表面状态的函数。\r\n\r\n```python\r\ndef DisplayArray(a, fmt='jpeg', rng=[0,1]):\r\n  \"\"\"Display an array as a picture.\"\"\"\r\n  a = (a - rng[0])/float(rng[1] - rng[0])*255\r\n  a = np.uint8(np.clip(a, 0, 255))\r\n  f = StringIO()\r\n  PIL.Image.fromarray(a).save(f, fmt)\r\n  display(Image(data=f.getvalue()))\r\n```\r\n\r\n最后，为了方便演示，这里我们需要打开一个 ***TensorFlow*** 的交互会话（interactive session）。当然为了以后能方便调用，我们可以把相关代码写到一个可以执行的***Python***文件中。\r\n\r\n```python\r\nsess = tf.InteractiveSession()\r\n```\r\n\r\n## 定义计算函数 <a class=\"md-anchor\" id=\"AUTOGENERATED-computational-convenience-functions\"></a>\r\n\r\n```python\r\ndef make_kernel(a):\r\n  \"\"\"Transform a 2D array into a convolution kernel\"\"\"\r\n  a = np.asarray(a)\r\n  a = a.reshape(list(a.shape) + [1,1])\r\n  return tf.constant(a, dtype=1)\r\n\r\ndef simple_conv(x, k):\r\n  \"\"\"A simplified 2D convolution operation\"\"\"\r\n  x = tf.expand_dims(tf.expand_dims(x, 0), -1)\r\n  y = tf.nn.depthwise_conv2d(x, k, [1, 1, 1, 1], padding='SAME')\r\n  return y[0, :, :, 0]\r\n\r\ndef laplace(x):\r\n  \"\"\"Compute the 2D laplacian of an array\"\"\"\r\n  laplace_k = make_kernel([[0.5, 1.0, 0.5],\r\n                           [1.0, -6., 1.0],\r\n                           [0.5, 1.0, 0.5]])\r\n  return simple_conv(x, laplace_k)\r\n```\r\n\r\n## 定义偏微分方程 <a class=\"md-anchor\" id=\"AUTOGENERATED-define-the-pde\"></a>\r\n\r\n首先,我们需要创建一个完美的 500 × 500 的正方形池塘,就像是我们在现实中找到的一样。\r\n\r\n```python\r\nN = 500\r\n```\r\n\r\n然后，我们需要创建了一个池塘和几滴将要坠入池塘的雨滴。\r\n\r\n```python\r\n# Initial Conditions -- some rain drops hit a pond\r\n\r\n# Set everything to zero\r\nu_init = np.zeros([N, N], dtype=\"float32\")\r\nut_init = np.zeros([N, N], dtype=\"float32\")\r\n\r\n# Some rain drops hit a pond at random points\r\nfor n in range(40):\r\n  a,b = np.random.randint(0, N, 2)\r\n  u_init[a,b] = np.random.uniform()\r\n\r\nDisplayArray(u_init, rng=[-0.1, 0.1])\r\n```\r\n\r\n![jpeg](../images/pde_output_1.jpg)\r\n\r\n现在，让我们来指定该微分方程的一些详细参数。\r\n\r\n```python\r\n# Parameters:\r\n# eps -- time resolution\r\n# damping -- wave damping\r\neps = tf.placeholder(tf.float32, shape=())\r\ndamping = tf.placeholder(tf.float32, shape=())\r\n\r\n# Create variables for simulation state\r\nU  = tf.Variable(u_init)\r\nUt = tf.Variable(ut_init)\r\n\r\n# Discretized PDE update rules\r\nU_ = U + eps * Ut\r\nUt_ = Ut + eps * (laplace(U) - damping * Ut)\r\n\r\n# Operation to update the state\r\nstep = tf.group(\r\n  U.assign(U_),\r\n  Ut.assign(Ut_))\r\n```\r\n\r\n## 开始仿真 <a class=\"md-anchor\" id=\"AUTOGENERATED-run-the-simulation\"></a>\r\n\r\n为了能看清仿真效果，我们可以用一个简单的 **for** 循环来远行我们的仿真程序。\r\n\r\n```python\r\n# Initialize state to initial conditions\r\ntf.initialize_all_variables().run()\r\n\r\n# Run 1000 steps of PDE\r\nfor i in range(1000):\r\n  # Step simulation\r\n  step.run({eps: 0.03, damping: 0.04})\r\n  # Visualize every 50 steps\r\n  if i % 50 == 0:\r\n    clear_output()\r\n    DisplayArray(U.eval(), rng=[-0.1, 0.1])\r\n```\r\n\r\n![jpeg](../images/pde_output_2.jpg)\r\n\r\n看！！ 雨点落在池塘中,和现实中一样的泛起了涟漪。\r\n\r\n> 原文链接:[http://tensorflow.org/tutorials/pdes/index.md](http://tensorflow.org/tutorials/pdes/index.md)    翻译:[@wangaicc](https://github.com/wangaicc)   校对:[@tensorfly](https://github.com/tensorfly)\r\n"
  },
  {
    "path": "SOURCE/tutorials/recurrent/index.md",
    "content": "# Recurrent Neural Networks <a class=\"md-anchor\" id=\"AUTOGENERATED-recurrent-neural-networks\"></a>\r\n\r\n## Introduction <a class=\"md-anchor\" id=\"AUTOGENERATED-introduction\"></a>\r\n\r\nTake a look at [this great article]\r\n(http://colah.github.io/posts/2015-08-Understanding-LSTMs/)\r\nfor an introduction to recurrent neural networks and LSTMs in particular.\r\n\r\n## Language Modeling <a class=\"md-anchor\" id=\"AUTOGENERATED-language-modeling\"></a>\r\n\r\nIn this tutorial we will show how to train a recurrent neural network on\r\na challenging task of language modeling. The goal of the problem is to fit a\r\nprobabilistic model which assigns probablities to sentences. It does so by\r\npredicting next words in a text given a history of previous words. For this\r\npurpose we will use the Penn Tree Bank (PTB) dataset, which is a popular\r\nbenchmark for measuring quality of these models, whilst being small and\r\nrelatively fast to train.\r\n\r\nLanguage modeling is key to many interesting problems such as speech\r\nrecognition, machine translation, or image captioning. It is also fun, too --\r\ntake a look [here] (http://karpathy.github.io/2015/05/21/rnn-effectiveness/).\r\n\r\nFor the purpose of this tutorial, we will reproduce the results from\r\n[Zaremba et al., 2014] (http://arxiv.org/abs/1409.2329), which achieves very\r\ngood results on the PTB dataset.\r\n\r\n## Tutorial Files <a class=\"md-anchor\" id=\"AUTOGENERATED-tutorial-files\"></a>\r\n\r\nThis tutorial references the following files from `models/rnn/ptb`:\r\n\r\nFile | Purpose\r\n--- | ---\r\n`ptb_word_lm.py` | The code to train a language model on the PTB dataset.\r\n`reader.py` | The code to read the dataset.\r\n\r\n## Download and Prepare the Data <a class=\"md-anchor\" id=\"AUTOGENERATED-download-and-prepare-the-data\"></a>\r\n\r\nThe data required for this tutorial is in the data/ directory of the\r\nPTB dataset from Tomas Mikolov's webpage:\r\nhttp://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz\r\n\r\nThe dataset is already preprocessed and contains overall 10000 different words,\r\nincluding the end-of-sentence marker and a special symbol (\\<unk\\>) for rare\r\nwords. We convert all of them in the `reader.py` to unique integer identifiers\r\nto make it easy for the neural network to process.\r\n\r\n## The Model <a class=\"md-anchor\" id=\"AUTOGENERATED-the-model\"></a>\r\n\r\n### LSTM <a class=\"md-anchor\" id=\"AUTOGENERATED-lstm\"></a>\r\n\r\nThe core of the model consists of an LSTM cell that processes one word at the\r\ntime and computes probabilities of the possible continuations of the sentence.\r\nThe memory state of the network is initialized with a vector of zeros and gets\r\nupdated after reading each word. Also, for computational reasons, we will\r\nprocess data in mini-batches of size `batch_size`.\r\n\r\nThe basic pseudocode looks as follows:\r\n\r\n```python\r\nlstm = rnn_cell.BasicLSTMCell(lstm_size)\r\n# Initial state of the LSTM memory.\r\nstate = tf.zeros([batch_size, lstm.state_size])\r\n\r\nloss = 0.0\r\nfor current_batch_of_words in words_in_dataset:\r\n    # The value of state is updated after processing each batch of words.\r\n    output, state = lstm(current_batch_of_words, state)\r\n\r\n    # The LSTM output can be used to make next word predictions\r\n    logits = tf.matmul(output, softmax_w) + softmax_b\r\n    probabilities = tf.nn.softmax(logits)\r\n    loss += loss_function(probabilities, target_words)\r\n```\r\n\r\n### Truncated Backpropagation <a class=\"md-anchor\" id=\"AUTOGENERATED-truncated-backpropagation\"></a>\r\n\r\nIn order to make the learning process tractable, it is a common practice to\r\ntruncate the gradients for backpropagation to a fixed number (`num_steps`)\r\nof unrolled steps.\r\nThis is easy to implement by feeding inputs of length `num_steps` at a time and\r\ndoing backward pass after each iteration.\r\n\r\nA simplifed version of the code for the graph creation for truncated\r\nbackpropagation:\r\n\r\n```python\r\n# Placeholder for the inputs in a given iteration.\r\nwords = tf.placeholder(tf.int32, [batch_size, num_steps])\r\n\r\nlstm = rnn_cell.BasicLSTMCell(lstm_size)\r\n# Initial state of the LSTM memory.\r\ninitial_state = state = tf.zeros([batch_size, lstm.state_size])\r\n\r\nfor i in range(len(num_steps)):\r\n    # The value of state is updated after processing each batch of words.\r\n    output, state = lstm(words[:, i], state)\r\n\r\n    # The rest of the code.\r\n    # ...\r\n\r\nfinal_state = state\r\n```\r\n\r\nAnd this is how to implement an iteration over the whole dataset:\r\n\r\n```python\r\n# A numpy array holding the state of LSTM after each batch of words.\r\nnumpy_state = initial_state.eval()\r\ntotal_loss = 0.0\r\nfor current_batch_of_words in words_in_dataset:\r\n    numpy_state, current_loss = session.run([final_state, loss],\r\n        # Initialize the LSTM state from the previous iteration.\r\n        feed_dict={initial_state: numpy_state, words: current_batch_of_words})\r\n    total_loss += current_loss\r\n```\r\n\r\n### Inputs <a class=\"md-anchor\" id=\"AUTOGENERATED-inputs\"></a>\r\n\r\nThe word IDs will be embedded into a dense representation (see the\r\n[Vector Representations Tutorial](../../tutorials/word2vec/index.md)) before feeding to\r\nthe LSTM. This allows the model to efficiently represent the knowledge about\r\nparticular words. It is also easy to write:\r\n\r\n```python\r\n# embedding_matrix is a tensor of shape [vocabulary_size, embedding size]\r\nword_embeddings = tf.nn.embedding_lookup(embedding_matrix, word_ids)\r\n```\r\n\r\nThe embedding matrix will be initialized randomly and the model will learn to\r\ndifferentiate the meaning of words just by looking at the data.\r\n\r\n### Loss Fuction <a class=\"md-anchor\" id=\"AUTOGENERATED-loss-fuction\"></a>\r\n\r\nWe want to minimize the average negative log probability of the target words:\r\n\r\n$$ \\text{loss} = -\\frac{1}{N}\\sum_{i=1}^{N} \\ln p_{\\text{target}_i} $$\r\n\r\nIt is not very difficult to implement but the function\r\n`sequence_loss_by_example` is already available, so we can just use it here.\r\n\r\nThe typical measure reported in the papers is average per-word perplexity (often\r\njust called perplexity), which is equal to\r\n\r\n$$e^{-\\frac{1}{N}\\sum_{i=1}^{N} \\ln p_{\\text{target}_i}} = e^{\\text{loss}} $$\r\n\r\nand we will monitor its value throughout the training process.\r\n\r\n### Stacking multiple LSTMs <a class=\"md-anchor\" id=\"AUTOGENERATED-stacking-multiple-lstms\"></a>\r\n\r\nTo give the model more expressive power, we can add multiple layers of LSTMs\r\nto process the data. The output of the first layer will become the input of\r\nthe second and so on.\r\n\r\nWe have a class called `MultiRNNCell` that makes the implementation seamless:\r\n\r\n```python\r\nlstm = rnn_cell.BasicLSTMCell(lstm_size)\r\nstacked_lstm = rnn_cell.MultiRNNCell([lstm] * number_of_layers)\r\n\r\ninitial_state = state = stacked_lstm.zero_state(batch_size, tf.float32)\r\nfor i in range(len(num_steps)):\r\n    # The value of state is updated after processing each batch of words.\r\n    output, state = stacked_lstm(words[:, i], state)\r\n\r\n    # The rest of the code.\r\n    # ...\r\n\r\nfinal_state = state\r\n```\r\n\r\n## Compile and Run the Code <a class=\"md-anchor\" id=\"AUTOGENERATED-compile-and-run-the-code\"></a>\r\n\r\nFirst, the library needs to be built. To compile it on CPU:\r\n\r\n```\r\nbazel build -c opt tensorflow/models/rnn/ptb:ptb_word_lm\r\n```\r\n\r\nAnd if you have a fast GPU, run the following:\r\n\r\n```\r\nbazel build -c opt --config=cuda tensorflow/models/rnn/ptb:ptb_word_lm\r\n```\r\n\r\nNow we can run the model:\r\n\r\n```\r\nbazel-bin/tensorflow/models/rnn/ptb/ptb_word_lm \\\r\n  --data_path=/tmp/simple-examples/data/ --alsologtostderr --model small\r\n```\r\n\r\nThere are 3 supported model configurations in the tutorial code: \"small\",\r\n\"medium\" and \"large\". The difference between them is in size of the LSTMs and\r\nthe set of hyperparameters used for training.\r\n\r\nThe larger the model, the better results it should get. The `small` model should\r\nbe able to reach perplexity below 120 on the test set and the `large` one below\r\n80, though it might take several hours to train.\r\n\r\n## What Next? <a class=\"md-anchor\" id=\"AUTOGENERATED-what-next-\"></a>\r\n\r\nThere are several tricks that we haven't mentioned that make the model better,\r\nincluding:\r\n\r\n* decreasing learning rate schedule,\r\n* dropout between the LSTM layers.\r\n\r\nStudy the code and modify it to improve the model even further.\r\n"
  },
  {
    "path": "SOURCE/tutorials/recurrent.md",
    "content": "# 递归神经网络 <a class=\"md-anchor\" id=\"AUTOGENERATED-recurrent-neural-networks\"></a>\r\n\r\n## 介绍 <a class=\"md-anchor\" id=\"AUTOGENERATED-introduction\"></a>\r\n\r\n可以在 [this great article](http://colah.github.io/posts/2015-08-Understanding-LSTMs/) 查看循环神经网络(RNN)以及 LSTM 的介绍。\r\n\r\n## 语言模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-language-modeling\"></a>\r\n\r\n此教程将展示如何在高难度的语言模型中训练循环神经网络。该问题的目标是获得一个能确定语句概率的概率模型。为了做到这一点，通过之前已经给出的词语来预测后面的词语。我们将使用 PTB(Penn Tree Bank) 数据集，这是一种常用来衡量模型的基准，同时它比较小而且训练起来相对快速。\r\n\r\n语言模型是很多有趣难题的关键所在，比如语音识别，机器翻译，图像字幕等。它很有意思--可以参看 [here](http://karpathy.github.io/2015/05/21/rnn-effectiveness/)。\r\n\r\n本教程的目的是重现 [Zaremba et al., 2014](http://arxiv.org/abs/1409.2329) 的成果，他们在 PTB 数据集上得到了很棒的结果。\r\n\r\n## 教程文件 <a class=\"md-anchor\" id=\"AUTOGENERATED-tutorial-files\"></a>\r\n\r\n本教程使用的下面文件的目录是 `models/rnn/ptb`:\r\n\r\n文件 | 作用\r\n--- | ---\r\n`ptb_word_lm.py` | 在 PTB 数据集上训练一个语言模型.\r\n`reader.py` | 读取数据集.\r\n\r\n## 下载及准备数据 <a class=\"md-anchor\" id=\"AUTOGENERATED-download-and-prepare-the-data\"></a>\r\n\r\n本教程需要的数据在 data/ 路径下，来源于 Tomas Mikolov 网站上的 PTB 数据集`http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz`。\r\n\r\n该数据集已经预先处理过并且包含了全部的 10000 个不同的词语，其中包括语句结束标记符，以及标记稀有词语的特殊符号 `(<unk>)` 。我们在 `reader.py` 中转换所有的词语，让他们各自有唯一的整型标识符，便于神经网络处理。\r\n\r\n## 模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-the-model\"></a>\r\n\r\n### LSTM <a class=\"md-anchor\" id=\"AUTOGENERATED-lstm\"></a>\r\n\r\n模型的核心由一个 LSTM 单元组成，其可以在某时刻处理一个词语，以及计算语句可能的延续性的概率。网络的存储状态由一个零矢量初始化并在读取每一个词语后更新。而且，由于计算上的原因，我们将以 `batch_size` 为最小批量来处理数据。\r\n\r\n基础的伪代码就像下面这样：\r\n\r\n```python\r\nlstm = rnn_cell.BasicLSTMCell(lstm_size)\r\n# 初始化 LSTM 存储状态.\r\nstate = tf.zeros([batch_size, lstm.state_size])\r\n\r\nloss = 0.0\r\nfor current_batch_of_words in words_in_dataset:\r\n    # 每次处理一批词语后更新状态值.\r\n    output, state = lstm(current_batch_of_words, state)\r\n\r\n    # LSTM 输出可用于产生下一个词语的预测\r\n    logits = tf.matmul(output, softmax_w) + softmax_b\r\n    probabilities = tf.nn.softmax(logits)\r\n    loss += loss_function(probabilities, target_words)\r\n```\r\n\r\n### 截断反向传播 <a class=\"md-anchor\" id=\"AUTOGENERATED-truncated-backpropagation\"></a>\r\n\r\n为使学习过程易于处理，通常的做法是将反向传播的梯度在（按时间）展开的步骤上照一个固定长度(`num_steps`)截断。\r\n通过在一次迭代中的每个时刻上提供长度为 `num_steps` 的输入和每次迭代完成之后反向传导，这会很容易实现。\r\n\r\n一个简化版的用于计算图创建的截断反向传播代码：\r\n\r\n```python\r\n# 一次给定的迭代中的输入占位符.\r\nwords = tf.placeholder(tf.int32, [batch_size, num_steps])\r\n\r\nlstm = rnn_cell.BasicLSTMCell(lstm_size)\r\n# 初始化 LSTM 存储状态.\r\ninitial_state = state = tf.zeros([batch_size, lstm.state_size])\r\n\r\nfor i in range(len(num_steps)):\r\n    # 每处理一批词语后更新状态值.\r\n    output, state = lstm(words[:, i], state)\r\n\r\n    # 其余的代码.\r\n    # ...\r\n\r\nfinal_state = state\r\n```\r\n\r\n下面展现如何实现迭代整个数据集：\r\n\r\n```python\r\n# 一个 numpy 数组，保存每一批词语之后的 LSTM 状态.\r\nnumpy_state = initial_state.eval()\r\ntotal_loss = 0.0\r\nfor current_batch_of_words in words_in_dataset:\r\n    numpy_state, current_loss = session.run([final_state, loss],\r\n        # 通过上一次迭代结果初始化 LSTM 状态.\r\n        feed_dict={initial_state: numpy_state, words: current_batch_of_words})\r\n    total_loss += current_loss\r\n```\r\n\r\n### 输入 <a class=\"md-anchor\" id=\"AUTOGENERATED-inputs\"></a>\r\n\r\n在输入 LSTM 前，词语 ID 被嵌入到了一个密集的表示中(查看 [矢量表示教程](tensorflow-zh/SOURCE/tutorials/word2vec/index.md))。这种方式允许模型高效地表示词语，也便于写代码：\r\n\r\n```python\r\n# embedding_matrix 张量的形状是： [vocabulary_size, embedding_size]\r\nword_embeddings = tf.nn.embedding_lookup(embedding_matrix, word_ids)\r\n```\r\n\r\n嵌入的矩阵会被随机地初始化，模型会学会通过数据分辨不同词语的意思。\r\n\r\n### 损失函数 <a class=\"md-anchor\" id=\"AUTOGENERATED-loss-fuction\"></a>\r\n\r\n我们想使目标词语的平均负对数概率最小\r\n\r\n![](../images/re.png)\r\n\r\n实现起来并非很难，而且函数 `sequence_loss_by_example` 已经有了，可以直接使用。\r\n\r\n论文中的典型衡量标准是每个词语的平均困惑度（perplexity），计算式为\r\n\r\n![](../images/re1.png)\r\n\r\n同时我们会观察训练过程中的困惑度值（perplexity）。\r\n\r\n### 多个 LSTM 层堆叠 <a class=\"md-anchor\" id=\"AUTOGENERATED-stacking-multiple-lstms\"></a>\r\n\r\n要想给模型更强的表达能力，可以添加多层 LSTM 来处理数据。第一层的输出作为第二层的输入，以此类推。\r\n\r\n类 `MultiRNNCell` 可以无缝的将其实现：\r\n\r\n```python\r\nlstm = rnn_cell.BasicLSTMCell(lstm_size)\r\nstacked_lstm = rnn_cell.MultiRNNCell([lstm] * number_of_layers)\r\n\r\ninitial_state = state = stacked_lstm.zero_state(batch_size, tf.float32)\r\nfor i in range(len(num_steps)):\r\n    # 每次处理一批词语后更新状态值.\r\n    output, state = stacked_lstm(words[:, i], state)\r\n\r\n    # 其余的代码.\r\n    # ...\r\n\r\nfinal_state = state\r\n```\r\n\r\n## 编译并运行代码 <a class=\"md-anchor\" id=\"AUTOGENERATED-compile-and-run-the-code\"></a>\r\n\r\n首先需要构建库，在 CPU 上编译：\r\n\r\n```\r\nbazel build -c opt tensorflow/models/rnn/ptb:ptb_word_lm\r\n```\r\n\r\n如果你有一个强大的 GPU，可以运行：\r\n\r\n```\r\nbazel build -c opt --config=cuda tensorflow/models/rnn/ptb:ptb_word_lm\r\n```\r\n\r\n运行模型：\r\n\r\n```\r\nbazel-bin/tensorflow/models/rnn/ptb/ptb_word_lm \\\r\n  --data_path=/tmp/simple-examples/data/ --alsologtostderr --model small\r\n```\r\n\r\n教程代码中有 3 个支持的模型配置参数：\"small\"，\r\n\"medium\" 和 \"large\"。它们指的是 LSTM 的大小，以及用于训练的超参数集。\r\n\r\n模型越大，得到的结果应该更好。在测试集中 `small` 模型应该可以达到低于 120 的困惑度（perplexity），`large` 模型则是低于 80，但它可能花费数小时来训练。\r\n\r\n## 除此之外？ <a class=\"md-anchor\" id=\"AUTOGENERATED-what-next-\"></a>\r\n\r\n还有几个优化模型的技巧没有提到，包括：\r\n\r\n* 随时间降低学习率,\r\n* LSTM 层间 dropout.\r\n\r\n继续学习和更改代码以进一步改善模型吧。\r\n\r\n原文：[Recurrent Neural Networks](http://tensorflow.org/tutorials/recurrent/index.md)\r\n翻译：[Warln](https://github.com/Warln)\r\n校对：[HongyangWang](https://github.com/wanghong-yang)\r\n\r\n"
  },
  {
    "path": "SOURCE/tutorials/seq2seq/index.md",
    "content": "# Sequence-to-Sequence Models <a class=\"md-anchor\" id=\"AUTOGENERATED-sequence-to-sequence-models\"></a>\r\n\r\nRecurrent neural networks can learn to model language, as already discussed\r\nin the [RNN Tutorial](../../tutorials/recurrent/index.md)\r\n(if you did not read it, please go through it before proceeding with this one).\r\nThis raises an interesting question: could we condition the generated words on\r\nsome input and generate a meaningful response? For example, could we train\r\na neural network to translate from English to French? It turns out that\r\nthe answer is *yes*.\r\n\r\nThis tutorial will show you how to build and train such a system end-to-end.\r\nYou can start by running this binary.\r\n\r\n```\r\nbazel run -c opt <...>/models/rnn/translate/translate.py\r\n  --data_dir [your_data_directory]\r\n```\r\n\r\nIt will download English-to-French translation data from the\r\n[WMT'15 Website](http://www.statmt.org/wmt15/translation-task.html)\r\nprepare it for training and train. It takes  about 20GB of disk space,\r\nand a while to download and prepare (see [later](#run_it) for details),\r\nso you can start and leave it running while reading this tutorial.\r\n\r\nThis tutorial references the following files from `models/rnn`.\r\n\r\nFile | What's in it?\r\n--- | ---\r\n`seq2seq.py` | Library for building sequence-to-sequence models.\r\n`translate/seq2seq_model.py` | Neural translation sequence-to-sequence model.\r\n`translate/data_utils.py` | Helper functions for preparing translation data.\r\n`translate/translate.py` | Binary that trains and runs the translation model.\r\n\r\n\r\n## Sequence-to-Sequence Basics <a class=\"md-anchor\" id=\"AUTOGENERATED-sequence-to-sequence-basics\"></a>\r\n\r\nA basic sequence-to-sequence model, as introduced in\r\n[Cho et al., 2014](http://arxiv.org/pdf/1406.1078v3.pdf),\r\nconsists of two recurrent neural networks (RNNs): an *encoder* that\r\nprocesses the input and a *decoder* that generates the output.\r\nThis basic architecture is depicted below.\r\n\r\n<div style=\"width:80%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"basic_seq2seq.png\" />\r\n</div>\r\n\r\nEach box in the picture above represents a cell of the RNN, most commonly\r\na GRU cell or an LSTM cell (see the [RNN Tutorial](../../tutorials/recurrent/index.md)\r\nfor an explanation of those). Encoder and decoder can share weights or,\r\nas is more common, use a different set of parameters. Mutli-layer cells\r\nhave been successfully used in sequence-to-sequence models too, e.g. for\r\ntranslation [Sutskever et al., 2014](http://arxiv.org/abs/1409.3215).\r\n\r\nIn the basic model depicted above, every input has to be encoded into\r\na fixed-size state vector, as that is the only thing passed to the decoder.\r\nTo allow the decoder more direct access to the input, an *attention* mechanism\r\nwas introduced in [Bahdanu et al., 2014](http://arxiv.org/abs/1409.0473).\r\nWe will not go into the details of the attention mechanism (see the paper),\r\nsuffice it to say that it allows the decoder to peek into the input at every\r\ndecoding step. A multi-layer sequence-to-sequence network with LSTM cells and\r\nattention mechanism in the decoder looks like this.\r\n\r\n<div style=\"width:80%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"attention_seq2seq.png\" />\r\n</div>\r\n\r\n## TensorFlow seq2seq Library <a class=\"md-anchor\" id=\"AUTOGENERATED-tensorflow-seq2seq-library\"></a>\r\n\r\nAs you can see above, there are many different sequence-to-sequence\r\nmodels. Each of these models can use different RNN cells, but all\r\nof them accept encoder inputs and decoder inputs. This motivates\r\nthe interfaces in the TensorFlow seq2seq library (`models/rnn/seq2seq.py`).\r\nThe basic RNN encoder-decoder sequence-to-sequence model works as follows.\r\n\r\n```python\r\noutputs, states = basic_rnn_seq2seq(encoder_inputs, decoder_inputs, cell)\r\n```\r\n\r\nIn the above call, `encoder_inputs` are a list of tensors representing inputs\r\nto the encoder, i.e., corresponding to the letters *A, B, C* in the first\r\npicture above. Similarly, `decoder_inputs` are tensors representing inputs\r\nto the decoder, *GO, W, X, Y, Z* on the first picture.\r\n\r\nThe `cell` argument is an instance of the `models.rnn.rnn_cell.RNNCell` class\r\nthat determines which cell will be used inside the model. You can use\r\nan existing cell, such as `GRUCell` or `LSTMCell`, or you can write your own.\r\nMoreover, `rnn_cell` provides wrappers to construct multi-layer cells,\r\nadd dropout to cell inputs or outputs, or to do other transformations,\r\nsee the [RNN Tutorial](../../tutorials/recurrent/index.md) for examples.\r\n\r\nThe call to `basic_rnn_seq2seq` returns two arguments: `outputs` and `states`.\r\nBoth of them are lists of tensors of the same length as `decoder_inputs`.\r\nNaturally, `outputs` correspond to the outputs of the decoder in each time-step,\r\nin the first picture above that would be *W, X, Y, Z, EOS*. The returned\r\n`states` represent the internal state of the decoder at every time-step.\r\n\r\nIn many applications of sequence-to-sequence models, the output of the decoder\r\nat time t is fed back and becomes the input of the decoder at time t+1. At test\r\ntime, when decoding a sequence, this is how the sequence is constructed.\r\nDuring training, on the other hand, it is common to provide the correct input\r\nto the decoder at every time-step, even if the decoder made a mistake before.\r\nFunctions in `seq2seq.py` support both modes using the `feed_previous` argument.\r\nFor example, let's analyze the following use of an embedding RNN model.\r\n\r\n```python\r\noutputs, states = embedding_rnn_seq2seq(\r\n    encoder_inputs, decoder_inputs, cell,\r\n    num_encoder_symbols, num_decoder_symbols,\r\n    output_projection=None, feed_previous=False)\r\n```\r\n\r\nIn the `embedding_rnn_seq2seq` model, all inputs (both `encoder_inputs` and\r\n`decoder_inputs`) are integer-tensors that represent discrete values.\r\nThey will be embedded into a dense representation (see the\r\n[Vectors Representations Tutorial](../../tutorials/word2vec/index.md) for more details\r\non embeddings), but to construct these embeddings we need to specify\r\nthe maximum number of discrete symbols that will appear: `num_encoder_symbols`\r\non the encoder side, and `num_decoder_symbols` on the decoder side.\r\n\r\nIn the above invocation, we set `feed_previous` to False. This means that the\r\ndecoder will use `decoder_inputs` tensors as provided. If we set `feed_previous`\r\nto True, the decoder would only use the first element of `decoder_inputs`.\r\nAll other tensors from this list would be ignored, and instead the previous\r\noutput of the encoder would be used. This is used for decoding translations\r\nin our translation model, but it can also be used during training, to make\r\nthe model more robust to its own mistakes, similar\r\nto [Bengio et al., 2015](http://arxiv.org/pdf/1506.03099v2.pdf).\r\n\r\nOne more important argument used above is `output_projection`. If not specified,\r\nthe outputs of the embedding model will be tensors of shape batch-size by\r\n`num_decoder_symbols` as they represent the logits for each generated symbol.\r\nWhen training models with large output vocabularies, i.e., when\r\n`num_decoder_symbols` is large, it is not practical to store these large\r\ntensors. Instead, it is better to return smaller output tensors, which will\r\nlater be projected onto a large output tensor using `output_projection`.\r\nThis allows to use our seq2seq models with a sampled softmax loss, as described\r\nin [Jean et. al., 2015](http://arxiv.org/pdf/1412.2007v2.pdf).\r\n\r\nIn addition to `basic_rnn_seq2seq` and `embedding_rnn_seq2seq` there are a few\r\nmore sequence-to-sequence models in `seq2seq.py`, take a look there. They all\r\nhave similar interfaces, so we will not describe them in detail. We will use\r\n`embedding_attention_seq2seq` for our translation model below.\r\n\r\n## Neural Translation Model <a class=\"md-anchor\" id=\"AUTOGENERATED-neural-translation-model\"></a>\r\n\r\nWhile the core of the sequence-to-sequence model is constructed by\r\nthe functions in `models/rnn/seq2seq.py`, there are still a few tricks\r\nthat are worth mentioning that are used in our translation model in\r\n`models/rnn/translate/seq2seq_model.py`.\r\n\r\n### Sampled softmax and output projection <a class=\"md-anchor\" id=\"AUTOGENERATED-sampled-softmax-and-output-projection\"></a>\r\n\r\nFor one, as already mentioned above, we want to use sampled softmax to\r\nhandle large output vocabulary. To decode from it, we need to keep track\r\nof the output projection. Both the sampled softmax loss and the output\r\nprojections are constructed by the following code in `seq2seq_model.py`.\r\n\r\n```python\r\n  if num_samples > 0 and num_samples < self.target_vocab_size:\r\n    w = tf.get_variable(\"proj_w\", [size, self.target_vocab_size])\r\n    w_t = tf.transpose(w)\r\n    b = tf.get_variable(\"proj_b\", [self.target_vocab_size])\r\n    output_projection = (w, b)\r\n\r\n    def sampled_loss(inputs, labels):\r\n      labels = tf.reshape(labels, [-1, 1])\r\n      return tf.nn.sampled_softmax_loss(w_t, b, inputs, labels, num_samples,\r\n                                        self.target_vocab_size)\r\n```\r\n\r\nFirst, note that we only construct a sampled softmax if the number of samples\r\n(512 by default) is smaller that the target vocabulary size. For vocabularies\r\nsmaller than 512 it might be a better idea to just use a standard softmax loss.\r\n\r\nThen, as you can see, we construct an output projection. It is a pair,\r\nconsisting of a weight matrix and a bias vector. If used, the rnn cell\r\nwill return vectors of shape batch-size by `size`, rather than batch-size\r\nby `target_vocab_size`. To recover logits, we need to multiply by the weight\r\nmatrix and add the biases, as is done in lines 124-126 in `seq2seq_model.py`.\r\n\r\n```python\r\nif output_projection is not None:\r\n  self.outputs[b] = [tf.matmul(output, output_projection[0]) +\r\n                     output_projection[1] for ...]\r\n```\r\n\r\n### Bucketing and padding <a class=\"md-anchor\" id=\"AUTOGENERATED-bucketing-and-padding\"></a>\r\n\r\nIn addition to sampled softmax, our translation model also makes use\r\nof *bucketing*, which is a method to efficiently handle sentences of\r\ndifferent lengths. Let us first clarify the problem. When translating\r\nEnglish to French, we will have English sentences of different lengths L1\r\non input, and French sentences of different lengths L2 on output. Since\r\nthe English sentence is passed as `encoder_inputs`, and the French sentence\r\ncomes as `decoder_inputs` (prefixed by a GO symbol), we should in principle\r\ncreate a seq2seq model for every pair (L1, L2+1) of lengths of an English\r\nand French sentence. This would result in an enormous graph consisting of\r\nmany very similar subgraphs. On the other hand, we could just pad every\r\nsentence with a special PAD symbol. Then we'd need only one seq2seq model,\r\nfor the padded lengths. But on shorter sentence our model would be inefficient,\r\nencoding and decoding many PAD symbols that are useless.\r\n\r\nAs a compromise between contructing a graph for every pair of lengths and\r\npadding to a single length, we use a number of *buckets* and pad each sentence\r\nto the length of the bucket above it. In `translate.py` we use the following\r\ndefault buckets.\r\n\r\n```python\r\nbuckets = [(5, 10), (10, 15), (20, 25), (40, 50)]\r\n```\r\n\r\nThis means that if the input is an English sentence with 3 tokens,\r\nand the corresponding output is a French sentence with 6 tokens,\r\nthen they will be put in the first bucket and padded to length 5 for\r\nencoder inputs, and length 10 for decoder inputs. If we have an English\r\nsentence with 8 tokens and the corresponding French sentence has 18 tokens,\r\nthen they will not fit into the (10, 15) bucket, and so the (20, 25) bucket\r\nwill be used, i.e. the English sentence will be padded to 20, and the French\r\none to 25.\r\n\r\nRemember that when constructing decoder inputs we prepend the special `GO`\r\nsymbol to the input data. This is done in the `get_batch()` function in\r\n`seq2seq_model.py`, which also reverses the input English sentence.\r\nReversing the inputs was shown to improve results for the neural translation\r\nmodel in [Sutskever et al., 2014](http://arxiv.org/abs/1409.3215).\r\nTo put it all together, imagine we have the sentence \"I go.\", tokenized\r\nas `[\"I\", \"go\", \".\"]` as input and the sentence \"Je vais.\" as output,\r\ntokenized `[\"Je\", \"vais\", \".\"]`. It will be put in the (5, 10) bucket,\r\nwith encoder inputs representing `[PAD PAD \".\" \"go\" \"I\"]` and decoder\r\ninputs `[GO \"Je\" \"vais\" \".\" EOS PAD PAD PAD PAD PAD]`.\r\n\r\n\r\n## Let's Run It <a class=\"md-anchor\" id=\"run_it\"></a>\r\n\r\nTo train the model described above, we need to a large English-French corpus.\r\nWe will use the *10^9-French-English corpus* from the\r\n[WMT'15 Website](http://www.statmt.org/wmt15/translation-task.html)\r\nfor training, and the 2013 news test from the same site as development set.\r\nBoth data-sets will be downloaded to `data_dir` and training will start,\r\nsaving checkpoints in `train_dir`, when this command is run.\r\n\r\n```\r\nbazel run -c opt <...>/models/rnn/translate:translate\r\n  --data_dir [your_data_directory] --train_dir [checkpoints_directory]\r\n  --en_vocab_size=40000 --fr_vocab_size=40000\r\n```\r\n\r\nIt takes  about 18GB of disk space and several hours to prepare the training\r\ncorpus. It is unpacked, vocabulary files are created in `data_dir`, and then\r\nthe corpus is tokenized and converted to integer ids. Note the parameters\r\nthat determine vocabulary sizes. In the example above, all words outside\r\nthe 40K most common ones will be converted to an `UNK` token representing\r\nunknown words. So if you change vocabulary size, the binary will re-map\r\nthe corpus to token-ids again.\r\n\r\nAfter the data is prepared, training starts. Default parameters in `translate`\r\nare set to quite large values. Large models trained over a long time give good\r\nresults, but it might take too long or use too much memory for your GPU.\r\nYou can request to train a smaller model as in the following example.\r\n\r\n```\r\nbazel run -c opt <...>/models/rnn/translate:translate\r\n  --data_dir [your_data_directory] --train_dir [checkpoints_directory]\r\n  --size=256 --num_layers=2 --steps_per_checkpoint=50\r\n```\r\n\r\nThe above command will train a model with 2 layers (the default is 3),\r\neach layer with 256 units (default is 1024), and will save a checkpoint\r\nevery 50 steps (the default is 200). You can play with these parameters\r\nto find out how large a model can be to fit into the memory of your GPU.\r\n\r\nDuring training, every `steps_per_checkpoint` steps the binary will print\r\nout statistics from recent steps. With the default parameters (3 layers\r\nof size 1024), first messages look like this.\r\n\r\n```\r\nglobal step 200 learning rate 0.5000 step-time 1.39 perplexity 1720.62\r\n  eval: bucket 0 perplexity 184.97\r\n  eval: bucket 1 perplexity 248.81\r\n  eval: bucket 2 perplexity 341.64\r\n  eval: bucket 3 perplexity 469.04\r\nglobal step 400 learning rate 0.5000 step-time 1.38 perplexity 379.89\r\n  eval: bucket 0 perplexity 151.32\r\n  eval: bucket 1 perplexity 190.36\r\n  eval: bucket 2 perplexity 227.46\r\n  eval: bucket 3 perplexity 238.66\r\n```\r\n\r\nYou can see that each step takes just under 1.4 seconds, the perplexity\r\non the training set and the perplexities on the development set\r\nfor each bucket. After about 30K steps, we see perplexities on short\r\nsentences (bucket 0 and 1) going into single digits.\r\nSince the training corpus contains ~22M sentences, one epoch (going through\r\nthe training data once) takes about 340K steps with batch-size of 64. At this\r\npoint the model can be used for translating English sentences to French\r\nusing the `--decode` option.\r\n\r\n```\r\nbazel run -c opt <...>/models/rnn/translate:translate --decode\r\n  --data_dir [your_data_directory] --train_dir [checkpoints_directory]\r\n\r\nReading model parameters from /tmp/translate.ckpt-340000\r\n>  Who is the president of the United States?\r\n Qui est le président des États-Unis ?\r\n```\r\n\r\n## What Next? <a class=\"md-anchor\" id=\"AUTOGENERATED-what-next-\"></a>\r\n\r\nThe example above shows how you can build your own English-to-French\r\ntranslator, end-to-end. Run it and see how the model performs for yourself.\r\nWhile it has reasonable quality, the default parameters will not give you\r\nthe best translation model. Here are a few things you can improve.\r\n\r\nFirst of all, we use a very promitive tokenizer, the `basic_tokenizer` function\r\nin `data_utils`. A better tokenizer can be found on the\r\n[WMT'15 Website](http://www.statmt.org/wmt15/translation-task.html).\r\nUsing that tokenizer, and a larger vocabulary, should improve your translations.\r\n\r\nAlso, the default parameters of the translation model are not tuned.\r\nYou can try changing the learning rate, decay, or initializing the weights\r\nof your model in a different way. You can also change the default\r\n`GradientDescentOptimizer` in `seq2seq_model.py` to a more advanced one, such\r\nas `AdagradOptimizer`. Try these things and see how they improve your results!\r\n\r\nFinally, the model presented above can be used for any sequence-to-sequence\r\ntask, not only for translation. Even if you want to transform a sequence to\r\na tree, for example to generate a parsing tree, the same model as above can\r\ngive state-of-the-art results, as demonstrated in\r\n[Vinyals & Kaiser et al., 2015](http://arxiv.org/abs/1412.7449).\r\nSo you can not only build your own translator, you can also build a parser,\r\na chat-bot, or any program that comes to your mind. Experiment!\r\n"
  },
  {
    "path": "SOURCE/tutorials/seq2seq.md",
    "content": "# Sequence-to-Sequence Models <a class=\"md-anchor\" id=\"AUTOGENERATED-sequence-to-sequence-models\"></a>\r\n\r\nRecurrent neural networks can learn to model language, as already discussed\r\nin the [RNN Tutorial](tensorflow-zh/SOURCE/tutorials/recurrent/index.md)\r\n(if you did not read it, please go through it before proceeding with this one).\r\nThis raises an interesting question: could we condition the generated words on\r\nsome input and generate a meaningful response? For example, could we train\r\na neural network to translate from English to French? It turns out that\r\nthe answer is *yes*.\r\n\r\nThis tutorial will show you how to build and train such a system end-to-end.\r\nYou can start by running this binary.\r\n\r\n```\r\nbazel run -c opt <...>/models/rnn/translate/translate.py\r\n  --data_dir [your_data_directory]\r\n```\r\n\r\nIt will download English-to-French translation data from the\r\n[WMT'15 Website](http://www.statmt.org/wmt15/translation-task.html)\r\nprepare it for training and train. It takes  about 20GB of disk space,\r\nand a while to download and prepare (see [later](#run_it) for details),\r\nso you can start and leave it running while reading this tutorial.\r\n\r\nThis tutorial references the following files from `models/rnn`.\r\n\r\nFile | What's in it?\r\n--- | ---\r\n`seq2seq.py` | Library for building sequence-to-sequence models.\r\n`translate/seq2seq_model.py` | Neural translation sequence-to-sequence model.\r\n`translate/data_utils.py` | Helper functions for preparing translation data.\r\n`translate/translate.py` | Binary that trains and runs the translation model.\r\n\r\n\r\n## Sequence-to-Sequence Basics <a class=\"md-anchor\" id=\"AUTOGENERATED-sequence-to-sequence-basics\"></a>\r\n\r\nA basic sequence-to-sequence model, as introduced in\r\n[Cho et al., 2014](http://arxiv.org/pdf/1406.1078v3.pdf),\r\nconsists of two recurrent neural networks (RNNs): an *encoder* that\r\nprocesses the input and a *decoder* that generates the output.\r\nThis basic architecture is depicted below.\r\n\r\n<div style=\"width:80%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"basic_seq2seq.png\" />\r\n</div>\r\n\r\nEach box in the picture above represents a cell of the RNN, most commonly\r\na GRU cell or an LSTM cell (see the [RNN Tutorial](tensorflow-zh/SOURCE/tutorials/recurrent/index.md)\r\nfor an explanation of those). Encoder and decoder can share weights or,\r\nas is more common, use a different set of parameters. Mutli-layer cells\r\nhave been successfully used in sequence-to-sequence models too, e.g. for\r\ntranslation [Sutskever et al., 2014](http://arxiv.org/abs/1409.3215).\r\n\r\nIn the basic model depicted above, every input has to be encoded into\r\na fixed-size state vector, as that is the only thing passed to the decoder.\r\nTo allow the decoder more direct access to the input, an *attention* mechanism\r\nwas introduced in [Bahdanu et al., 2014](http://arxiv.org/abs/1409.0473).\r\nWe will not go into the details of the attention mechanism (see the paper),\r\nsuffice it to say that it allows the decoder to peek into the input at every\r\ndecoding step. A multi-layer sequence-to-sequence network with LSTM cells and\r\nattention mechanism in the decoder looks like this.\r\n\r\n<div style=\"width:80%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"attention_seq2seq.png\" />\r\n</div>\r\n\r\n## TensorFlow seq2seq Library <a class=\"md-anchor\" id=\"AUTOGENERATED-tensorflow-seq2seq-library\"></a>\r\n\r\nAs you can see above, there are many different sequence-to-sequence\r\nmodels. Each of these models can use different RNN cells, but all\r\nof them accept encoder inputs and decoder inputs. This motivates\r\nthe interfaces in the TensorFlow seq2seq library (`models/rnn/seq2seq.py`).\r\nThe basic RNN encoder-decoder sequence-to-sequence model works as follows.\r\n\r\n```python\r\noutputs, states = basic_rnn_seq2seq(encoder_inputs, decoder_inputs, cell)\r\n```\r\n\r\nIn the above call, `encoder_inputs` are a list of tensors representing inputs\r\nto the encoder, i.e., corresponding to the letters *A, B, C* in the first\r\npicture above. Similarly, `decoder_inputs` are tensors representing inputs\r\nto the decoder, *GO, W, X, Y, Z* on the first picture.\r\n\r\nThe `cell` argument is an instance of the `models.rnn.rnn_cell.RNNCell` class\r\nthat determines which cell will be used inside the model. You can use\r\nan existing cell, such as `GRUCell` or `LSTMCell`, or you can write your own.\r\nMoreover, `rnn_cell` provides wrappers to construct multi-layer cells,\r\nadd dropout to cell inputs or outputs, or to do other transformations,\r\nsee the [RNN Tutorial](tensorflow-zh/SOURCE/tutorials/recurrent/index.md) for examples.\r\n\r\nThe call to `basic_rnn_seq2seq` returns two arguments: `outputs` and `states`.\r\nBoth of them are lists of tensors of the same length as `decoder_inputs`.\r\nNaturally, `outputs` correspond to the outputs of the decoder in each time-step,\r\nin the first picture above that would be *W, X, Y, Z, EOS*. The returned\r\n`states` represent the internal state of the decoder at every time-step.\r\n\r\nIn many applications of sequence-to-sequence models, the output of the decoder\r\nat time t is fed back and becomes the input of the decoder at time t+1. At test\r\ntime, when decoding a sequence, this is how the sequence is constructed.\r\nDuring training, on the other hand, it is common to provide the correct input\r\nto the decoder at every time-step, even if the decoder made a mistake before.\r\nFunctions in `seq2seq.py` support both modes using the `feed_previous` argument.\r\nFor example, let's analyze the following use of an embedding RNN model.\r\n\r\n```python\r\noutputs, states = embedding_rnn_seq2seq(\r\n    encoder_inputs, decoder_inputs, cell,\r\n    num_encoder_symbols, num_decoder_symbols,\r\n    output_projection=None, feed_previous=False)\r\n```\r\n\r\nIn the `embedding_rnn_seq2seq` model, all inputs (both `encoder_inputs` and\r\n`decoder_inputs`) are integer-tensors that represent discrete values.\r\nThey will be embedded into a dense representation (see the\r\n[Vectors Representations Tutorial](tensorflow-zh/SOURCE/tutorials/word2vec/index.md) for more details\r\non embeddings), but to construct these embeddings we need to specify\r\nthe maximum number of discrete symbols that will appear: `num_encoder_symbols`\r\non the encoder side, and `num_decoder_symbols` on the decoder side.\r\n\r\nIn the above invocation, we set `feed_previous` to False. This means that the\r\ndecoder will use `decoder_inputs` tensors as provided. If we set `feed_previous`\r\nto True, the decoder would only use the first element of `decoder_inputs`.\r\nAll other tensors from this list would be ignored, and instead the previous\r\noutput of the encoder would be used. This is used for decoding translations\r\nin our translation model, but it can also be used during training, to make\r\nthe model more robust to its own mistakes, similar\r\nto [Bengio et al., 2015](http://arxiv.org/pdf/1506.03099v2.pdf).\r\n\r\nOne more important argument used above is `output_projection`. If not specified,\r\nthe outputs of the embedding model will be tensors of shape batch-size by\r\n`num_decoder_symbols` as they represent the logits for each generated symbol.\r\nWhen training models with large output vocabularies, i.e., when\r\n`num_decoder_symbols` is large, it is not practical to store these large\r\ntensors. Instead, it is better to return smaller output tensors, which will\r\nlater be projected onto a large output tensor using `output_projection`.\r\nThis allows to use our seq2seq models with a sampled softmax loss, as described\r\nin [Jean et. al., 2015](http://arxiv.org/pdf/1412.2007v2.pdf).\r\n\r\nIn addition to `basic_rnn_seq2seq` and `embedding_rnn_seq2seq` there are a few\r\nmore sequence-to-sequence models in `seq2seq.py`, take a look there. They all\r\nhave similar interfaces, so we will not describe them in detail. We will use\r\n`embedding_attention_seq2seq` for our translation model below.\r\n\r\n## Neural Translation Model <a class=\"md-anchor\" id=\"AUTOGENERATED-neural-translation-model\"></a>\r\n\r\nWhile the core of the sequence-to-sequence model is constructed by\r\nthe functions in `models/rnn/seq2seq.py`, there are still a few tricks\r\nthat are worth mentioning that are used in our translation model in\r\n`models/rnn/translate/seq2seq_model.py`.\r\n\r\n### Sampled softmax and output projection <a class=\"md-anchor\" id=\"AUTOGENERATED-sampled-softmax-and-output-projection\"></a>\r\n\r\nFor one, as already mentioned above, we want to use sampled softmax to\r\nhandle large output vocabulary. To decode from it, we need to keep track\r\nof the output projection. Both the sampled softmax loss and the output\r\nprojections are constructed by the following code in `seq2seq_model.py`.\r\n\r\n```python\r\n  if num_samples > 0 and num_samples < self.target_vocab_size:\r\n    w = tf.get_variable(\"proj_w\", [size, self.target_vocab_size])\r\n    w_t = tf.transpose(w)\r\n    b = tf.get_variable(\"proj_b\", [self.target_vocab_size])\r\n    output_projection = (w, b)\r\n\r\n    def sampled_loss(inputs, labels):\r\n      labels = tf.reshape(labels, [-1, 1])\r\n      return tf.nn.sampled_softmax_loss(w_t, b, inputs, labels, num_samples,\r\n                                        self.target_vocab_size)\r\n```\r\n\r\nFirst, note that we only construct a sampled softmax if the number of samples\r\n(512 by default) is smaller that the target vocabulary size. For vocabularies\r\nsmaller than 512 it might be a better idea to just use a standard softmax loss.\r\n\r\nThen, as you can see, we construct an output projection. It is a pair,\r\nconsisting of a weight matrix and a bias vector. If used, the rnn cell\r\nwill return vectors of shape batch-size by `size`, rather than batch-size\r\nby `target_vocab_size`. To recover logits, we need to multiply by the weight\r\nmatrix and add the biases, as is done in lines 124-126 in `seq2seq_model.py`.\r\n\r\n```python\r\nif output_projection is not None:\r\n  self.outputs[b] = [tf.matmul(output, output_projection[0]) +\r\n                     output_projection[1] for ...]\r\n```\r\n\r\n### Bucketing and padding <a class=\"md-anchor\" id=\"AUTOGENERATED-bucketing-and-padding\"></a>\r\n\r\nIn addition to sampled softmax, our translation model also makes use\r\nof *bucketing*, which is a method to efficiently handle sentences of\r\ndifferent lengths. Let us first clarify the problem. When translating\r\nEnglish to French, we will have English sentences of different lengths L1\r\non input, and French sentences of different lengths L2 on output. Since\r\nthe English sentence is passed as `encoder_inputs`, and the French sentence\r\ncomes as `decoder_inputs` (prefixed by a GO symbol), we should in principle\r\ncreate a seq2seq model for every pair (L1, L2+1) of lengths of an English\r\nand French sentence. This would result in an enormous graph consisting of\r\nmany very similar subgraphs. On the other hand, we could just pad every\r\nsentence with a special PAD symbol. Then we'd need only one seq2seq model,\r\nfor the padded lengths. But on shorter sentence our model would be inefficient,\r\nencoding and decoding many PAD symbols that are useless.\r\n\r\nAs a compromise between contructing a graph for every pair of lengths and\r\npadding to a single length, we use a number of *buckets* and pad each sentence\r\nto the length of the bucket above it. In `translate.py` we use the following\r\ndefault buckets.\r\n\r\n```python\r\nbuckets = [(5, 10), (10, 15), (20, 25), (40, 50)]\r\n```\r\n\r\nThis means that if the input is an English sentence with 3 tokens,\r\nand the corresponding output is a French sentence with 6 tokens,\r\nthen they will be put in the first bucket and padded to length 5 for\r\nencoder inputs, and length 10 for decoder inputs. If we have an English\r\nsentence with 8 tokens and the corresponding French sentence has 18 tokens,\r\nthen they will not fit into the (10, 15) bucket, and so the (20, 25) bucket\r\nwill be used, i.e. the English sentence will be padded to 20, and the French\r\none to 25.\r\n\r\nRemember that when constructing decoder inputs we prepend the special `GO`\r\nsymbol to the input data. This is done in the `get_batch()` function in\r\n`seq2seq_model.py`, which also reverses the input English sentence.\r\nReversing the inputs was shown to improve results for the neural translation\r\nmodel in [Sutskever et al., 2014](http://arxiv.org/abs/1409.3215).\r\nTo put it all together, imagine we have the sentence \"I go.\", tokenized\r\nas `[\"I\", \"go\", \".\"]` as input and the sentence \"Je vais.\" as output,\r\ntokenized `[\"Je\", \"vais\", \".\"]`. It will be put in the (5, 10) bucket,\r\nwith encoder inputs representing `[PAD PAD \".\" \"go\" \"I\"]` and decoder\r\ninputs `[GO \"Je\" \"vais\" \".\" EOS PAD PAD PAD PAD PAD]`.\r\n\r\n\r\n## Let's Run It <a class=\"md-anchor\" id=\"run_it\"></a>\r\n\r\nTo train the model described above, we need to a large English-French corpus.\r\nWe will use the *10^9-French-English corpus* from the\r\n[WMT'15 Website](http://www.statmt.org/wmt15/translation-task.html)\r\nfor training, and the 2013 news test from the same site as development set.\r\nBoth data-sets will be downloaded to `data_dir` and training will start,\r\nsaving checkpoints in `train_dir`, when this command is run.\r\n\r\n```\r\nbazel run -c opt <...>/models/rnn/translate:translate\r\n  --data_dir [your_data_directory] --train_dir [checkpoints_directory]\r\n  --en_vocab_size=40000 --fr_vocab_size=40000\r\n```\r\n\r\nIt takes  about 18GB of disk space and several hours to prepare the training\r\ncorpus. It is unpacked, vocabulary files are created in `data_dir`, and then\r\nthe corpus is tokenized and converted to integer ids. Note the parameters\r\nthat determine vocabulary sizes. In the example above, all words outside\r\nthe 40K most common ones will be converted to an `UNK` token representing\r\nunknown words. So if you change vocabulary size, the binary will re-map\r\nthe corpus to token-ids again.\r\n\r\nAfter the data is prepared, training starts. Default parameters in `translate`\r\nare set to quite large values. Large models trained over a long time give good\r\nresults, but it might take too long or use too much memory for your GPU.\r\nYou can request to train a smaller model as in the following example.\r\n\r\n```\r\nbazel run -c opt <...>/models/rnn/translate:translate\r\n  --data_dir [your_data_directory] --train_dir [checkpoints_directory]\r\n  --size=256 --num_layers=2 --steps_per_checkpoint=50\r\n```\r\n\r\nThe above command will train a model with 2 layers (the default is 3),\r\neach layer with 256 units (default is 1024), and will save a checkpoint\r\nevery 50 steps (the default is 200). You can play with these parameters\r\nto find out how large a model can be to fit into the memory of your GPU.\r\n\r\nDuring training, every `steps_per_checkpoint` steps the binary will print\r\nout statistics from recent steps. With the default parameters (3 layers\r\nof size 1024), first messages look like this.\r\n\r\n```\r\nglobal step 200 learning rate 0.5000 step-time 1.39 perplexity 1720.62\r\n  eval: bucket 0 perplexity 184.97\r\n  eval: bucket 1 perplexity 248.81\r\n  eval: bucket 2 perplexity 341.64\r\n  eval: bucket 3 perplexity 469.04\r\nglobal step 400 learning rate 0.5000 step-time 1.38 perplexity 379.89\r\n  eval: bucket 0 perplexity 151.32\r\n  eval: bucket 1 perplexity 190.36\r\n  eval: bucket 2 perplexity 227.46\r\n  eval: bucket 3 perplexity 238.66\r\n```\r\n\r\nYou can see that each step takes just under 1.4 seconds, the perplexity\r\non the training set and the perplexities on the development set\r\nfor each bucket. After about 30K steps, we see perplexities on short\r\nsentences (bucket 0 and 1) going into single digits.\r\nSince the training corpus contains ~22M sentences, one epoch (going through\r\nthe training data once) takes about 340K steps with batch-size of 64. At this\r\npoint the model can be used for translating English sentences to French\r\nusing the `--decode` option.\r\n\r\n```\r\nbazel run -c opt <...>/models/rnn/translate:translate --decode\r\n  --data_dir [your_data_directory] --train_dir [checkpoints_directory]\r\n\r\nReading model parameters from /tmp/translate.ckpt-340000\r\n>  Who is the president of the United States?\r\n Qui est le président des États-Unis ?\r\n```\r\n\r\n## What Next? <a class=\"md-anchor\" id=\"AUTOGENERATED-what-next-\"></a>\r\n\r\nThe example above shows how you can build your own English-to-French\r\ntranslator, end-to-end. Run it and see how the model performs for yourself.\r\nWhile it has reasonable quality, the default parameters will not give you\r\nthe best translation model. Here are a few things you can improve.\r\n\r\nFirst of all, we use a very promitive tokenizer, the `basic_tokenizer` function\r\nin `data_utils`. A better tokenizer can be found on the\r\n[WMT'15 Website](http://www.statmt.org/wmt15/translation-task.html).\r\nUsing that tokenizer, and a larger vocabulary, should improve your translations.\r\n\r\nAlso, the default parameters of the translation model are not tuned.\r\nYou can try changing the learning rate, decay, or initializing the weights\r\nof your model in a different way. You can also change the default\r\n`GradientDescentOptimizer` in `seq2seq_model.py` to a more advanced one, such\r\nas `AdagradOptimizer`. Try these things and see how they improve your results!\r\n\r\nFinally, the model presented above can be used for any sequence-to-sequence\r\ntask, not only for translation. Even if you want to transform a sequence to\r\na tree, for example to generate a parsing tree, the same model as above can\r\ngive state-of-the-art results, as demonstrated in\r\n[Vinyals & Kaiser et al., 2015](http://arxiv.org/abs/1412.7449).\r\nSo you can not only build your own translator, you can also build a parser,\r\na chat-bot, or any program that comes to your mind. Experiment!\r\n"
  },
  {
    "path": "SOURCE/tutorials/word2vec/__init__.py",
    "content": ""
  },
  {
    "path": "SOURCE/tutorials/word2vec/index.md",
    "content": "# Vector Representations of Words <a class=\"md-anchor\" id=\"AUTOGENERATED-vector-representations-of-words\"></a>\r\n\r\n\r\n在本教程我们来看一下[Mikolov et al](http://papers.nips.cc/paper/5021-distributed-representations-of-words-and-phrases-and-their-compositionality.pdf)中提到的word2vec模型。该模型是用于学习文字的向量表示，称之为“word embedding”。\r\n\r\n## 亮点 <a class=\"md-anchor\" id=\"AUTOGENERATED-highlights\"></a>\r\n\r\n\r\n本教程意在展现出在TensorfLow中构建word2vec模型有趣、本质的部分。\r\n\r\n\r\n* 我们从我们为何需要使用向量表示文字开始。\r\n* 我们通过直观地例子观察模型背后的本质，以及它是如何训练的（通过一些数学方法评估）。\r\n* 同时我们也展示了TensorFlow对该模型的简单实现。\r\n* 最后，我们着眼于让给这个简单版本的模型表现更好。\r\n\r\n我们会在教程的推进中循序渐进地解释代码，但是如果你更希望直入主题，可以在\r\n[tensorflow/examples/tutorials/word2vec/word2vec_basic.py](https://www.tensorflow.org/code/tensorflow/examples/tutorials/word2vec/word2vec_basic.py)查看到一个最简单的实现。这个基本的例子提供的代码可以完成下载一些数据，简单训练后展示结果。一旦你觉得已经完全掌握了这个简单版本，你可以查看\r\n[tensorflow/models/embedding/word2vec.py](https://www.tensorflow.org/code/tensorflow/models/embedding/word2vec.py)，这里提供了一些更复杂的实现，同时也展示了TensorFlow的一些更进阶的特性，比如如何更高效地使用线程将数据送入文本模型，再比如如何在训练中设置检查点等等。\r\n\r\n但是首先，让我们来看一下为何需要学习word embeddings。如果你对word embeddings相关内容已经是个专家了，那么请安心跳过本节内容，直接深入细节干一些脏活吧。\r\n\r\n## 动机: 为什么需要学习 Word Embeddings? <a class=\"md-anchor\" id=\"AUTOGENERATED-motivation--why-learn-word-embeddings-\"></a>\r\n\r\n通常图像或音频系统处理的是由图片中所有单个原始像素点强度值或者音频中功率谱密度的强度值，把它们编码成丰富、高纬度的向量数据集。对于物体或语音识别这一类的任务，我们所需的全部信息已经都存储在原始数据中（显然人类本身就是依赖原始数据进行日常的物体或语音识别的）。然后，自然语言处理系统通常将词汇作为离散的单一符号，例如 \"cat\" 一词或可表示为 `Id537` ，而 \"dog\" 一词或可表示为 `Id143`。这些符号编码毫无规律，无法提供不同词汇之间可能存在的关联信息。换句话说，在处理关于 \"dogs\" 一词的信息时，模型将无法利用已知的关于 \"cats\" 的信息（例如，它们都是动物，有四条腿，可作为宠物等等）。可见，将词汇表达为上述的独立离散符号将进一步导致数据稀疏，使我们在训练统计模型时不得不寻求更多的数据。而词汇的向量表示将克服上述的难题。\r\n\r\n<div style=\"width:100%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"img/audio-image-text.png\" alt>\r\n</div>\r\n\r\n[向量空间模型](https://en.wikipedia.org/wiki/Vector_space_model) (VSMs)将词汇表达（嵌套）于一个连续的向量空间中，语义近似的词汇被映射为相邻的数据点。向量空间模型在自然语言处理领域中有着漫长且丰富的历史，不过几乎所有利用这一模型的方法都依赖于\r\n[分布式假设](https://en.wikipedia.org/wiki/Distributional_semantics#Distributional_Hypothesis)，其核心思想为出现于上下文情景中的词汇都有相类似的语义。采用这一假设的研究方法大致分为以下两类：*基于技术的方法* (e.g.\r\n[潜在语义分析](https://en.wikipedia.org/wiki/Latent_semantic_analysis))，\r\n和 *预测方法* (e.g.\r\n[神经概率化语言模型](http://www.scholarpedia.org/article/Neural_net_language_models)).\r\n\r\n其中它们的区别在如下论文中又详细阐述\r\n[Baroni et al.](http://clic.cimec.unitn.it/marco/publications/acl2014/baroni-etal-countpredict-acl2014.pdf)，不过简而言之：基于计数的方法计算某词汇与其邻近词汇在一个大型语料库中共同出现的频率及其他统计量，然后将这些统计量映射到一个小型且稠密的向量中。预测方法则试图直接从某词汇的邻近词汇对其进行预测，在此过程中利用已经学习到的小型且稠密的*嵌套向量*。 \r\n\r\nWord2vec是一种可以进行高效率词嵌套学习的预测模型。其两种变体分别为：连续词袋模型（CBOW）及Skip-Gram模型。从算法角度看，这两种方法非常相似，其区别为CBOW根据源词上下文词汇（'the cat sits on the'）来预测目标词汇（例如，‘mat’），而Skip-Gram模型做法相反，它通过目标词汇来预测源词汇。Skip-Gram模型采取CBOW的逆过程的动机在于：CBOW算法对于很多分布式信息进行了平滑处理（例如将一整段上下文信息视为一个单一观察量）。很多情况下，对于小型的数据集，这一处理是有帮助的。相形之下，Skip-Gram模型将每个“上下文-目标词汇”的组合视为一个新观察量，这种做法在大型数据集中会更为有效。本教程余下部分将着重讲解Skip-Gram模型。\r\n\r\n\r\n## 处理噪声对比训练 <a class=\"md-anchor\" id=\"AUTOGENERATED-scaling-up-with-noise-contrastive-training\"></a>\r\n\r\n神经概率化语言模型通常使用[极大似然法](https://en.wikipedia.org/wiki/Maximum_likelihood) (ML) 进行训练，其中通过 [*softmax* function](https://en.wikipedia.org/wiki/Softmax_function) 来最大化当提供前一个单词 \\\\(h\\\\) (代表 \"history\")，后一个单词的概率 \\\\(w_t\\\\) (代表 \"target\")，\r\n\r\n$$\r\n\\begin{align}\r\nP(w_t | h) &= \\text{softmax}(\\exp \\{ \\text{score}(w_t, h) \\}) \\\\\r\n           &= \\frac{\\exp \\{ \\text{score}(w_t, h) \\} }\r\n             {\\sum_\\text{Word w' in Vocab} \\exp \\{ \\text{score}(w', h) \\} }.\r\n\\end{align}\r\n$$\r\n\r\n当 \\\\(\\text{score}(w\\_t, h)\\\\) 计算了文字 \\\\(w\\_t\\\\) 和 上下文 \\\\(h\\\\) 的相容性（通常使用向量积）。我们使用对数似然函数来训练训练集的最大值，比如通过：\r\n\r\n$$\r\n\\begin{align}\r\n J_\\text{ML} &= \\log P(w_t | h) \\\\\r\n  &= \\text{score}(w_t, h) -\r\n     \\log \\left( \\sum_\\text{Word w' in Vocab} \\exp \\{ \\text{score}(w', h) \\} \\right)\r\n\\end{align}\r\n$$\r\n\r\n这里提出了一个解决语言概率模型的合适的通用方法。然而这个方法实际执行起来开销非常大，因为我们需要去计算并正则化当前上下文环境 \\\\(h\\\\) 中所有其他 \\\\(V\\\\) 单词 \\\\(w'\\\\) 的概率得分，*在每一步训练迭代中*。\r\n\r\n<div style=\"width:60%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"img/softmax-nplm.png\" alt>\r\n</div>\r\n\r\n从另一个角度来说，当使用word2vec模型时，我们并不需要对概率模型中的所有特征进行学习。而CBOW模型和Skip-Gram模型为了避免这种情况发生，使用一个二分类器（逻辑回归）在同一个上下文环境里从 \\\\(k\\\\) 虚构的 (噪声) 单词 \\\\(\\tilde w\\\\) 区分出真正的目标单词 \\\\(w_t\\\\)。我们下面详细阐述一下CBOW模型，对于Skip-Gram模型只要简单地做相反的操作即可。\r\n\r\n<div style=\"width:60%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"img/nce-nplm.png\" alt>\r\n</div>\r\n\r\n从数学角度来说，我们的目标是对每个样本最大化：\r\n\r\n$$J_\\text{NEG} = \\log Q_\\theta(D=1 |w_t, h) +\r\n  k \\mathop{\\mathbb{E}}_{\\tilde w \\sim P_\\text{noise}}\r\n     \\left[ \\log Q_\\theta(D = 0 |\\tilde w, h) \\right]$$\r\n\r\n其中 \\\\(Q_\\theta(D=1 | w, h)\\\\) 代表的是数据集在当前上下文 \\\\(h\\\\) ，根据所学习的嵌套向量 \\\\(\\theta\\\\) ，目标单词 \\\\(w\\\\) 使用二分类逻辑回归计算得出的概率。在实践中，我们通过在噪声分布中绘制比对文字来获得近似的期望值（通过计算[蒙特卡洛平均值](https://en.wikipedia.org/wiki/Monte_Carlo_integration)）。\r\n\r\n当真实地目标单词被分配到较高的概率，同时噪声单词的概率很低时，目标函数也就达到最大值了。从技术层面来说，这种方法叫做\r\n[负抽样](http://papers.nips.cc/paper/5021-distributed-representations-of-words-and-phrases-and-their-compositionality.pdf)，而且使用这个损失函数在数学层面上也有很好的解释：这个更新过程也近似于softmax函数的更新。这在计算上将会有很大的优势，因为当计算这个损失函数时，只是有我们挑选出来的 (\\\\(k\\\\)) 个 *噪声单词*，而没有使用整个语料库 (\\\\(V\\\\))。这使得训练变得非常快。我们实际上使用了与[noise-contrastive estimation (NCE)](http://papers.nips.cc/paper/5165-learning-word-embeddings-efficiently-with-noise-contrastive-estimation.pdf)介绍的非常相似的方法，这在TensorFlow中已经封装了一个很便捷的函数`tf.nn.nce_loss()`。\r\n\r\n让我们在实践中来直观地体会它是如何运作的！\r\n\r\n## Skip-gram 模型<a class=\"md-anchor\" id=\"AUTOGENERATED-the-skip-gram-model\"></a>\r\n\r\n下面来看一下这个数据集\r\n\r\n`the quick brown fox jumped over the lazy dog`\r\n\r\n我们首先对一些单词以及它们的上下文环境建立一个数据集。我们可以以任何合理的方式定义‘上下文’，而通常上这个方式是根据文字的句法语境的（使用语法原理的方式处理当前目标单词可以看一下这篇文献 [Levy et al.](https://levyomer.files.wordpress.com/2014/04/dependency-based-word-embeddings-acl-2014.pdf)，比如说把目标单词左边的内容当做一个‘上下文’，或者以目标单词右边的内容，等等。现在我们把目标单词的左右单词视作一个上下文， 使用大小为1的窗口，这样就得到这样一个由`(上下文, 目标单词)` 组成的数据集：\r\n\r\n`([the, brown], quick), ([quick, fox], brown), ([brown, jumped], fox), ...`\r\n\r\n前文提到Skip-Gram模型是把目标单词和上下文颠倒过来，所以在这个问题中，举个例子，就是用'quick'来预测 'the' 和 'brown' ，用 'brown' 预测 'quick' 和 'brown' 。因此这个数据集就变成由`(输入, 输出)`组成的：\r\n\r\n`(quick, the), (quick, brown), (brown, quick), (brown, fox), ...`\r\n\r\n目标函数通常是对整个数据集建立的，但是本问题中要对每一个样本（或者是一个`batch_size` 很小的样本集，通常设置为`16 <= batch_size <= 512`）在同一时间执行特别的操作，称之为[随机梯度下降](https://en.wikipedia.org/wiki/Stochastic_gradient_descent)\r\n(SGD)。我们来看一下训练过程中每一步的执行。\r\n\r\n假设用 \\\\(t\\\\)表示上面这个例子中`quick` 来预测 `the` 的训练的单个循环。用 `num_noise` 定义从噪声分布中挑选出来的噪声（相反的）单词的个数，通常使用一元分布，\\\\(P(w)\\\\)。为了简单起见，我们就定`num_noise=1`，用 `sheep` 选作噪声词。接下来就可以计算每一对观察值和噪声值的损失函数了，每一个执行步骤就可表示为：\r\n\r\n$$J^{(t)}_\\text{NEG} = \\log Q_\\theta(D=1 | \\text{the, quick}) +\r\n  \\log(Q_\\theta(D=0 | \\text{sheep, quick}))$$.\r\n\r\n整个计算过程的目标是通过更新嵌套参数 \\\\(\\theta\\\\) 来逼近目标函数（这个这个例子中就是使目标函数最大化）。为此我们要计算损失函数中嵌套参数 \\\\(\\theta\\\\) 的梯度，比如，\r\n\\\\(\\frac{\\partial}{\\partial \\theta} J_\\text{NEG}\\\\) (幸好TensorFlow封装了工具函数可以简单调用!)。对于整个数据集，当梯度下降的过程中不断地更新参数，对应产生的效果就是不断地移动每个单词的嵌套向量，直到可以把真实单词和噪声单词很好得区分开。\r\n\r\n我们可以把学习向量映射到2维中以便我们观察，其中用到的技术可以参考\r\n[t-SNE 降纬技术](http://lvdmaaten.github.io/tsne/)。当我们用可视化的方式来观察这些向量，就可以很明显的获取单词之间语义信息的关系，这实际上是非常有用的。当我们第一次发现这样的诱导向量空间中，展示了一些特定的语义关系，这是非常有趣的，比如文字中 *male-female*，*gender* 甚至还有 *country-capital* 的关系, 如下方的图所示 (也可以参考\r\n[Mikolov et al., 2013](http://www.aclweb.org/anthology/N13-1090)论文中的例子)。\r\n\r\n<div style=\"width:100%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"img/linear-relationships.png\" alt>\r\n</div>\r\n\r\n这也解释了为什么这些向量在传统的NLP问题中可作为特性使用，比如用在对一个演讲章节打个标签，或者对一个专有名词的识别\r\n(看看如下这个例子\r\n[Collobert et al.](http://arxiv.org/pdf/1103.0398v1.pdf)或者\r\n[Turian et al.](http://www.aclweb.org/anthology/P10-1040))。\r\n\r\n不过现在让我们用它们来画漂亮的图表吧！\r\n\r\n## 建立图形 <a class=\"md-anchor\" id=\"AUTOGENERATED-building-the-graph\"></a>\r\n\r\n这里谈得都是嵌套，那么先来定义一个嵌套参数矩阵。我们用均匀分布的随机值来初始化这个大矩阵。\r\n\r\n\r\n```python\r\nembeddings = tf.Variable(\r\n    tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))\r\n```\r\n\r\n对噪声-比对的损失计算就使用一个逻辑回归模型。对此，我们需要对语料库中的每个单词定义一个权重值和偏差值。(也可称之为`输出权重` 与之对应的 `输入嵌套值`)。定义如下。\r\n\r\n```python\r\nnce_weights = tf.Variable(\r\n  tf.truncated_normal([vocabulary_size, embedding_size],\r\n                      stddev=1.0 / math.sqrt(embedding_size)))\r\nnce_biases = tf.Variable(tf.zeros([vocabulary_size]))\r\n```\r\n\r\n我们有了这些参数之后，就可以定义Skip-Gram模型了。简单起见，假设我们已经把语料库中的文字整型化了，这样每个整型代表一个单词(细节请查看\r\n[tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py))。Skip-Gram模型有两个输入。一个是一组用整型表示的上下文单词，另一个是目标单词。给这些输入建立占位符节点，之后就可以填入数据了。\r\n\r\n```python\r\n# 建立输入占位符\r\ntrain_inputs = tf.placeholder(tf.int32, shape=[batch_size])\r\ntrain_labels = tf.placeholder(tf.int32, shape=[batch_size, 1])\r\n```\r\n\r\n然后我们需要对批数据中的单词建立嵌套向量，TensorFlow提供了方便的工具函数。\r\n\r\n```python\r\nembed = tf.nn.embedding_lookup(embeddings, train_inputs)\r\n```\r\n\r\n好了，现在我们有了每个单词的嵌套向量，接下来就是使用噪声-比对的训练方式来预测目标单词。\r\n\r\n```python\r\n# 计算 NCE 损失函数, 每次使用负标签的样本.\r\nloss = tf.reduce_mean(\r\n  tf.nn.nce_loss(nce_weights, nce_biases, embed, train_labels,\r\n                 num_sampled, vocabulary_size))\r\n```\r\n\r\n我们对损失函数建立了图形节点，然后我们需要计算相应梯度和更新参数的节点，比如说在这里我们会使用随机梯度下降法，TensorFlow也已经封装好了该过程。\r\n\r\n```python\r\n# 使用 SGD 控制器.\r\noptimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0).minimize(loss)\r\n```\r\n\r\n## 训练模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-training-the-model\"></a>\r\n\r\n训练的过程很简单，只要在循环中使用`feed_dict`不断给占位符填充数据，同时调用\r\n[`session.run`](../../api_docs/python/client.md#Session.run)即可。\r\n\r\n```python\r\nfor inputs, labels in generate_batch(...):\r\n  feed_dict = {training_inputs: inputs, training_labels: labels}\r\n  _, cur_loss = session.run([optimizer, loss], feed_dict=feed_dict)\r\n```\r\n\r\n完整地例子可参考\r\n[tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py](./word2vec_basic.py).\r\n\r\n## 嵌套学习结果可视化 <a class=\"md-anchor\" id=\"AUTOGENERATED-visualizing-the-learned-embeddings\"></a>\r\n\r\n使用t-SNE来看一下嵌套学习完成的结果。\r\n\r\n<div style=\"width:100%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"img/tsne.png\" alt>\r\n</div>\r\n\r\nEt voila! 与预期的一样，相似的单词被聚类在一起。对word2vec模型更复杂的实现需要用到TensorFlow一些更高级的特性，具体是实现可以参考\r\n[tensorflow/models/embedding/word2vec.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/embedding/word2vec.py)。\r\n\r\n## 嵌套学习的评估: 类比推理 <a class=\"md-anchor\" id=\"AUTOGENERATED-evaluating-embeddings--analogical-reasoning\"></a>\r\n\r\n词嵌套在NLP的预测问题中是非常有用且使用广泛地。如果要检测一个模型是否是可以成熟地区分词性或者区分专有名词的模型，最简单的办法就是直接检验它的预测词性、语义关系的能力，比如让它解决形如`king is to queen as father is to ?`这样的问题。这种方法叫做*类比推理* ，可参考[Mikolov and colleagues](http://msr-waypoint.com/en-us/um/people/gzweig/Pubs/NAACL2013Regularities.pdf)，数据集下载地址为: \r\nhttps://word2vec.googlecode.com/svn/trunk/questions-words.txt。\r\n\r\nTo see how we do this evaluation如何执行这样的评估，可以看`build_eval_graph()`和\r\n`eval()`这两个函数在下面源码中的使用\r\n[tensorflow/models/embedding/word2vec.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/embedding/word2vec.py).\r\n\r\n超参数的选择对该问题解决的准确性有巨大的影响。想要模型具有很好的表现，需要有一个巨大的训练数据集，同时仔细调整参数的选择并且使用例如二次抽样的一些技巧。不过这些问题已经超出了本教程的范围。\r\n\r\n## 优化实现 <a class=\"md-anchor\" id=\"AUTOGENERATED-optimizing-the-implementation\"></a>\r\n\r\n以上简单的例子展示了TensorFlow的灵活性。比如说，我们可以很轻松得用现成的`tf.nn.sampled_softmax_loss()`来代替`tf.nn.nce_loss()`构成目标函数。如果你对损失函数想做新的尝试，你可以用TensorFlow手动编写新的目标函数的表达式，然后用控制器执行计算。这种灵活性的价值体现在，当我们探索一个机器学习模型时，我们可以很快地遍历这些尝试，从中选出最优。\r\n\r\n一旦你有了一个满意的模型结构，或许它就可以使实现运行地更高效（在短时间内覆盖更多的数据）。比如说，在本教程中使用的简单代码，实际运行速度都不错，因为我们使用Python来读取和填装数据，而这些在TensorFlow后台只需执行非常少的工作。如果你发现你的模型在输入数据时存在严重的瓶颈，你可以根据自己的实际问题自行实现一个数据阅读器，参考\r\n[新的数据格式](../../how_tos/new_data_formats/index.md)。对于Skip-Gram\r\n模型，我们已经完成了如下这个例子\r\n[tensorflow/models/embedding/word2vec.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/embedding/word2vec.py)。\r\n\r\n如果I/O问题对你的模型已经不再是个问题，并且想进一步地优化性能，或许你可以自行编写TensorFlow操作单元，详见\r\n[添加一个新的操作](../../how_tos/adding_an_op/index.md)。相应的，我们也提供了Skip-Gram模型的例子\r\n[tensorflow/models/embedding/word2vec_optimized.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/embedding/word2vec_optimized.py)。请自行调节以上几个过程的标准，使模型在每个运行阶段有更好地性能。\r\n\r\n## 总结 <a class=\"md-anchor\" id=\"AUTOGENERATED-conclusion\"></a>\r\n\r\n在本教程中我们介绍了word2vec模型,它在解决词嵌套问题中具有良好的性能。我们解释了使用词嵌套模型的实用性，并且讨论了如何使用TensorFlow实现该模型的高效训练。总的来说，我们希望这个例子能够让向你展示TensorFlow可以提供实验初期的灵活性，以及在后期优化模型时对模型内部的可操控性。\r\n\r\n原文地址：[Vector Representation of Words](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/tutorials/word2vec/index.md) 翻译：[btpeter](https://github.com/btpeter) 校对：waiwaizheng\r\n"
  },
  {
    "path": "SOURCE/tutorials/word2vec/word2vec_basic.py",
    "content": "from __future__ import print_function\r\nimport tensorflow.python.platform\r\n\r\nimport collections\r\nimport math\r\nimport numpy as np\r\nimport os\r\nimport random\r\nimport tensorflow as tf\r\nimport urllib\r\nimport zipfile\r\n\r\n# Step 1: Download the data.\r\nurl = 'http://mattmahoney.net/dc/'\r\n\r\ndef maybe_download(filename, expected_bytes):\r\n  \"\"\"Download a file if not present, and make sure it's the right size.\"\"\"\r\n  if not os.path.exists(filename):\r\n    filename, _ = urllib.urlretrieve(url + filename, filename)\r\n  statinfo = os.stat(filename)\r\n  if statinfo.st_size == expected_bytes:\r\n    print('Found and verified', filename)\r\n  else:\r\n    print(statinfo.st_size)\r\n    raise Exception(\r\n        'Failed to verify ' + filename + '. Can you get to it with a browser?')\r\n  return filename\r\n\r\nfilename = maybe_download('text8.zip', 31344016)\r\n\r\n\r\n# Read the data into a string.\r\ndef read_data(filename):\r\n  f = zipfile.ZipFile(filename)\r\n  for name in f.namelist():\r\n    return f.read(name).split()\r\n  f.close()\r\n\r\nwords = read_data(filename)\r\nprint('Data size', len(words))\r\n\r\n# Step 2: Build the dictionary and replace rare words with UNK token.\r\nvocabulary_size = 50000\r\n\r\ndef build_dataset(words):\r\n  count = [['UNK', -1]]\r\n  count.extend(collections.Counter(words).most_common(vocabulary_size - 1))\r\n  dictionary = dict()\r\n  for word, _ in count:\r\n    dictionary[word] = len(dictionary)\r\n  data = list()\r\n  unk_count = 0\r\n  for word in words:\r\n    if word in dictionary:\r\n      index = dictionary[word]\r\n    else:\r\n      index = 0  # dictionary['UNK']\r\n      unk_count = unk_count + 1\r\n    data.append(index)\r\n  count[0][1] = unk_count\r\n  reverse_dictionary = dict(zip(dictionary.values(), dictionary.keys()))\r\n  return data, count, dictionary, reverse_dictionary\r\n\r\ndata, count, dictionary, reverse_dictionary = build_dataset(words)\r\ndel words  # Hint to reduce memory.\r\nprint('Most common words (+UNK)', count[:5])\r\nprint('Sample data', data[:10])\r\n\r\ndata_index = 0\r\n\r\n\r\n# Step 4: Function to generate a training batch for the skip-gram model.\r\ndef generate_batch(batch_size, num_skips, skip_window):\r\n  global data_index\r\n  assert batch_size % num_skips == 0\r\n  assert num_skips <= 2 * skip_window\r\n  batch = np.ndarray(shape=(batch_size), dtype=np.int32)\r\n  labels = np.ndarray(shape=(batch_size, 1), dtype=np.int32)\r\n  span = 2 * skip_window + 1 # [ skip_window target skip_window ]\r\n  buffer = collections.deque(maxlen=span)\r\n  for _ in range(span):\r\n    buffer.append(data[data_index])\r\n    data_index = (data_index + 1) % len(data)\r\n  for i in range(batch_size / num_skips):\r\n    target = skip_window  # target label at the center of the buffer\r\n    targets_to_avoid = [ skip_window ]\r\n    for j in range(num_skips):\r\n      while target in targets_to_avoid:\r\n        target = random.randint(0, span - 1)\r\n      targets_to_avoid.append(target)\r\n      batch[i * num_skips + j] = buffer[skip_window]\r\n      labels[i * num_skips + j, 0] = buffer[target]\r\n    buffer.append(data[data_index])\r\n    data_index = (data_index + 1) % len(data)\r\n  return batch, labels\r\n\r\nbatch, labels = generate_batch(batch_size=8, num_skips=2, skip_window=1)\r\nfor i in range(8):\r\n  print(batch[i], '->', labels[i, 0])\r\n  print(reverse_dictionary[batch[i]], '->', reverse_dictionary[labels[i, 0]])\r\n\r\n# Step 5: Build and train a skip-gram model.\r\n\r\nbatch_size = 128\r\nembedding_size = 128  # Dimension of the embedding vector.\r\nskip_window = 1       # How many words to consider left and right.\r\nnum_skips = 2         # How many times to reuse an input to generate a label.\r\n\r\n# We pick a random validation set to sample nearest neighbors. Here we limit the\r\n# validation samples to the words that have a low numeric ID, which by\r\n# construction are also the most frequent.\r\nvalid_size = 16     # Random set of words to evaluate similarity on.\r\nvalid_window = 100  # Only pick dev samples in the head of the distribution.\r\nvalid_examples = np.array(random.sample(xrange(valid_window), valid_size))\r\nnum_sampled = 64    # Number of negative examples to sample.\r\n\r\ngraph = tf.Graph()\r\n\r\nwith graph.as_default():\r\n\r\n  # Input data.\r\n  train_inputs = tf.placeholder(tf.int32, shape=[batch_size])\r\n  train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1])\r\n  valid_dataset = tf.constant(valid_examples, dtype=tf.int32)\r\n\r\n  # Construct the variables.\r\n  embeddings = tf.Variable(\r\n      tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))\r\n  nce_weights = tf.Variable(\r\n      tf.truncated_normal([vocabulary_size, embedding_size],\r\n                          stddev=1.0 / math.sqrt(embedding_size)))\r\n  nce_biases = tf.Variable(tf.zeros([vocabulary_size]))\r\n\r\n  # Look up embeddings for inputs.\r\n  embed = tf.nn.embedding_lookup(embeddings, train_inputs)\r\n\r\n  # Compute the average NCE loss for the batch.\r\n  # tf.nce_loss automatically draws a new sample of the negative labels each\r\n  # time we evaluate the loss.\r\n  loss = tf.reduce_mean(\r\n      tf.nn.nce_loss(nce_weights, nce_biases, train_labels,embed,\r\n                     num_sampled, vocabulary_size))\r\n\r\n  # Construct the SGD optimizer using a learning rate of 1.0.\r\n  optimizer = tf.train.GradientDescentOptimizer(1.0).minimize(loss)\r\n\r\n  # Compute the cosine similarity between minibatch examples and all embeddings.\r\n  norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True))\r\n  normalized_embeddings = embeddings / norm\r\n  valid_embeddings = tf.nn.embedding_lookup(\r\n      normalized_embeddings, valid_dataset)\r\n  similarity = tf.matmul(\r\n      valid_embeddings, normalized_embeddings, transpose_b=True)\r\n\r\n# Step 6: Begin training\r\nnum_steps = 100001\r\n\r\nwith tf.Session(graph=graph) as session:\r\n  # We must initialize all variables before we use them.\r\n  tf.initialize_all_variables().run()\r\n  print(\"Initialized\")\r\n\r\n  average_loss = 0\r\n  for step in xrange(num_steps):\r\n    batch_inputs, batch_labels = generate_batch(\r\n        batch_size, num_skips, skip_window)\r\n    feed_dict = {train_inputs : batch_inputs, train_labels : batch_labels}\r\n\r\n    # We perform one update step by evaluating the optimizer op (including it\r\n    # in the list of returned values for session.run()\r\n    _, loss_val = session.run([optimizer, loss], feed_dict=feed_dict)\r\n    average_loss += loss_val\r\n\r\n    if step % 2000 == 0:\r\n      if step > 0:\r\n        average_loss = average_loss / 2000\r\n      # The average loss is an estimate of the loss over the last 2000 batches.\r\n      print(\"Average loss at step \", step, \": \", average_loss)\r\n      average_loss = 0\r\n\r\n    # note that this is expensive (~20% slowdown if computed every 500 steps)\r\n    if step % 10000 == 0:\r\n      sim = similarity.eval()\r\n      for i in xrange(valid_size):\r\n        valid_word = reverse_dictionary[valid_examples[i]]\r\n        top_k = 8 # number of nearest neighbors\r\n        nearest = (-sim[i, :]).argsort()[1:top_k+1]\r\n        log_str = \"Nearest to %s:\" % valid_word\r\n        for k in xrange(top_k):\r\n          close_word = reverse_dictionary[nearest[k]]\r\n          log_str = \"%s %s,\" % (log_str, close_word)\r\n        print(log_str)\r\n  final_embeddings = normalized_embeddings.eval()\r\n\r\n# Step 7: Visualize the embeddings.\r\n\r\ndef plot_with_labels(low_dim_embs, labels, filename='tsne.png'):\r\n  assert low_dim_embs.shape[0] >= len(labels), \"More labels than embeddings\"\r\n  plt.figure(figsize=(18, 18))  #in inches\r\n  for i, label in enumerate(labels):\r\n    x, y = low_dim_embs[i,:]\r\n    plt.scatter(x, y)\r\n    plt.annotate(label,\r\n                 xy=(x, y),\r\n                 xytext=(5, 2),\r\n                 textcoords='offset points',\r\n                 ha='right',\r\n                 va='bottom')\r\n\r\n  plt.savefig(filename)\r\n\r\ntry:\r\n  from sklearn.manifold import TSNE\r\n  import matplotlib.pyplot as plt\r\n\r\n  tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)\r\n  plot_only = 500\r\n  low_dim_embs = tsne.fit_transform(final_embeddings[:plot_only,:])\r\n  labels = list(dictionary.keys())[:plot_only]\r\n  plot_with_labels(low_dim_embs, labels)\r\n\r\nexcept ImportError:\r\n  print(\"Please install sklearn and matplotlib to visualize embeddings.\")\r\n"
  },
  {
    "path": "SOURCE/tutorials/word2vec.md",
    "content": "# Vector Representations of Words <a class=\"md-anchor\" id=\"AUTOGENERATED-vector-representations-of-words\"></a>\r\n\r\n在本教程我们来看一下[Mikolov et al](http://papers.nips.cc/paper/5021-distributed-representations-of-words-and-phrases-and-their-compositionality.pdf)中提到的word2vec模型。该模型是用于学习文字的向量表示，称之为“word embedding”。\r\n\r\n## 亮点 <a class=\"md-anchor\" id=\"AUTOGENERATED-highlights\"></a>\r\n\r\n本教程意在展现出在TensorfLow中构建word2vec模型有趣、本质的部分。\r\n\r\n* 我们从我们为何需要使用向量表示文字开始。\r\n* 我们通过直观地例子观察模型背后的本质，以及它是如何训练的（通过一些数学方法评估）。\r\n* 同时我们也展示了TensorFlow对该模型的简单实现。\r\n* 最后，我们着眼于让给这个简单版本的模型表现更好。\r\n\r\n我们会在教程的推进中循序渐进地解释代码，但是如果你更希望直入主题，可以在\r\n[tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py)查看到一个最简单的实现。这个基本的例子提供的代码可以完成下载一些数据，简单训练后展示结果。一旦你觉得已经完全掌握了这个简单版本，你可以查看\r\n[tensorflow/models/embedding/word2vec.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/embedding/word2vec.py)，这里提供了一些更复杂的实现，同时也展示了TensorFlow的一些更进阶的特性，比如如何更高效地使用线程将数据送入文本模型，再比如如何在训练中设置检查点等等。\r\n\r\n但是首先，让我们来看一下为何需要学习word embeddings。如果你对word embeddings相关内容已经是个专家了，那么请安心跳过本节内容，直接深入细节干一些脏活吧。\r\n\r\n## 动机: 为什么需要学习 Word Embeddings? <a class=\"md-anchor\" id=\"AUTOGENERATED-motivation--why-learn-word-embeddings-\"></a>\r\n\r\n通常图像或音频系统处理的是由图片中所有单个原始像素点强度值或者音频中功率谱密度的强度值，把它们编码成丰富、高纬度的向量数据集。对于物体或语音识别这一类的任务，我们所需的全部信息已经都存储在原始数据中（显然人类本身就是依赖原始数据进行日常的物体或语音识别的）。然后，自然语言处理系统通常将词汇作为离散的单一符号，例如 \"cat\" 一词或可表示为 `Id537` ，而 \"dog\" 一词或可表示为 `Id143`。这些符号编码毫无规律，无法提供不同词汇之间可能存在的关联信息。换句话说，在处理关于 \"dogs\" 一词的信息时，模型将无法利用已知的关于 \"cats\" 的信息（例如，它们都是动物，有四条腿，可作为宠物等等）。可见，将词汇表达为上述的独立离散符号将进一步导致数据稀疏，使我们在训练统计模型时不得不寻求更多的数据。而词汇的向量表示将克服上述的难题。\r\n\r\n<div style=\"width:100%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/audio-image-text.png\" alt>\r\n</div>\r\n\r\n[向量空间模型](https://en.wikipedia.org/wiki/Vector_space_model) (VSMs)将词汇表达（嵌套）于一个连续的向量空间中，语义近似的词汇被映射为相邻的数据点。向量空间模型在自然语言处理领域中有着漫长且丰富的历史，不过几乎所有利用这一模型的方法都依赖于\r\n[分布式假设](https://en.wikipedia.org/wiki/Distributional_semantics#Distributional_Hypothesis)，其核心思想为出现于上下文情景中的词汇都有相类似的语义。采用这一假设的研究方法大致分为以下两类：*基于计数的方法* (e.g.\r\n[潜在语义分析](https://en.wikipedia.org/wiki/Latent_semantic_analysis))，\r\n和 *预测方法* (e.g.\r\n[神经概率化语言模型](http://www.scholarpedia.org/article/Neural_net_language_models)).\r\n\r\n其中它们的区别在如下论文中又详细阐述\r\n[Baroni et al.](http://clic.cimec.unitn.it/marco/publications/acl2014/baroni-etal-countpredict-acl2014.pdf)，不过简而言之：基于计数的方法计算某词汇与其邻近词汇在一个大型语料库中共同出现的频率及其他统计量，然后将这些统计量映射到一个小型且稠密的向量中。预测方法则试图直接从某词汇的邻近词汇对其进行预测，在此过程中利用已经学习到的小型且稠密的*嵌套向量*。 \r\n\r\nWord2vec是一种可以进行高效率词嵌套学习的预测模型。其两种变体分别为：连续词袋模型（CBOW）及Skip-Gram模型。从算法角度看，这两种方法非常相似，其区别为CBOW根据源词上下文词汇（'the cat sits on the'）来预测目标词汇（例如，‘mat’），而Skip-Gram模型做法相反，它通过目标词汇来预测源词汇。Skip-Gram模型采取CBOW的逆过程的动机在于：CBOW算法对于很多分布式信息进行了平滑处理（例如将一整段上下文信息视为一个单一观察量）。很多情况下，对于小型的数据集，这一处理是有帮助的。相形之下，Skip-Gram模型将每个“上下文-目标词汇”的组合视为一个新观察量，这种做法在大型数据集中会更为有效。本教程余下部分将着重讲解Skip-Gram模型。\r\n\r\n## 处理噪声对比训练 <a class=\"md-anchor\" id=\"AUTOGENERATED-scaling-up-with-noise-contrastive-training\"></a>\r\n\r\n神经概率化语言模型通常使用[极大似然法](https://en.wikipedia.org/wiki/Maximum_likelihood) (ML) 进行训练，其中通过 [*softmax* function](https://en.wikipedia.org/wiki/Softmax_function) 来最大化当提供前一个单词 **h** (代表 \"history\")，后一个单词的概率 ![](../images/vr1.png) (代表 \"target\")，\r\n\r\n![](../images/vr2.png)\r\n\r\n当 **score(w_t,h)** 计算了文字 **w_t**  和 上下文 **h** 的相容性（通常使用向量积）。我们使用对数似然函数来训练训练集的最大值，比如通过：\r\n\r\n![](../images/word2vec2.png)\r\n\r\n这里提出了一个解决语言概率模型的合适的通用方法。然而这个方法实际执行起来开销非常大，因为我们需要去计算并正则化当前上下文环境 **h** 中所有其他 **V** 单词 **w'** 的概率得分，*在每一步训练迭代中*。\r\n\r\n<div style=\"width:60%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/softmax-nplm.png\" alt>\r\n</div>\r\n\r\n从另一个角度来说，当使用word2vec模型时，我们并不需要对概率模型中的所有特征进行学习。而CBOW模型和Skip-Gram模型为了避免这种情况发生，使用一个二分类器（逻辑回归）在同一个上下文环境里从 **k** 虚构的 (噪声) 单词 ![](../images/rw5.png) 区分出真正的目标单词 ![](../images/rw4.png)。我们下面详细阐述一下CBOW模型，对于Skip-Gram模型只要简单地做相反的操作即可。\r\n\r\n<div style=\"width:60%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/nce-nplm.png\" alt>\r\n</div>\r\n\r\n从数学角度来说，我们的目标是对每个样本最大化：\r\n\r\n![](../images/rw6.png)\r\n\r\n其中 ![](../images/rw7.png) 代表的是数据集在当前上下文 **h** ，根据所学习的嵌套向量 ![](../images/theta.png) ，目标单词 **w** 使用二分类逻辑回归计算得出的概率。在实践中，我们通过在噪声分布中绘制比对文字来获得近似的期望值（通过计算[蒙特卡洛平均值](https://en.wikipedia.org/wiki/Monte_Carlo_integration)）。\r\n\r\n当真实地目标单词被分配到较高的概率，同时噪声单词的概率很低时，目标函数也就达到最大值了。从技术层面来说，这种方法叫做\r\n[负抽样](http://papers.nips.cc/paper/5021-distributed-representations-of-words-and-phrases-and-their-compositionality.pdf)，而且使用这个损失函数在数学层面上也有很好的解释：这个更新过程也近似于softmax函数的更新。这在计算上将会有很大的优势，因为当计算这个损失函数时，只是有我们挑选出来的 **k** 个 *噪声单词*，而没有使用整个语料库 **V**。这使得训练变得非常快。我们实际上使用了与[noise-contrastive estimation (NCE)](http://papers.nips.cc/paper/5165-learning-word-embeddings-efficiently-with-noise-contrastive-estimation.pdf)介绍的非常相似的方法，这在TensorFlow中已经封装了一个很便捷的函数`tf.nn.nce_loss()`。\r\n\r\n让我们在实践中来直观地体会它是如何运作的！\r\n\r\n## Skip-gram 模型<a class=\"md-anchor\" id=\"AUTOGENERATED-the-skip-gram-model\"></a>\r\n\r\n下面来看一下这个数据集\r\n\r\n`the quick brown fox jumped over the lazy dog`\r\n\r\n我们首先对一些单词以及它们的上下文环境建立一个数据集。我们可以以任何合理的方式定义‘上下文’，而通常上这个方式是根据文字的句法语境的（使用语法原理的方式处理当前目标单词可以看一下这篇文献 [Levy et al.](https://levyomer.files.wordpress.com/2014/04/dependency-based-word-embeddings-acl-2014.pdf)，比如说把目标单词左边的内容当做一个‘上下文’，或者以目标单词右边的内容，等等。现在我们把目标单词的左右单词视作一个上下文， 使用大小为1的窗口，这样就得到这样一个由`(上下文, 目标单词)` 组成的数据集：\r\n\r\n`([the, brown], quick), ([quick, fox], brown), ([brown, jumped], fox), ...`\r\n\r\n前文提到Skip-Gram模型是把目标单词和上下文颠倒过来，所以在这个问题中，举个例子，就是用'quick'来预测 'the' 和 'brown' ，用 'brown' 预测 'quick' 和 'brown' 。因此这个数据集就变成由`(输入, 输出)`组成的：\r\n\r\n`(quick, the), (quick, brown), (brown, quick), (brown, fox), ...`\r\n\r\n目标函数通常是对整个数据集建立的，但是本问题中要对每一个样本（或者是一个`batch_size` 很小的样本集，通常设置为`16 <= batch_size <= 512`）在同一时间执行特别的操作，称之为[随机梯度下降](https://en.wikipedia.org/wiki/Stochastic_gradient_descent)\r\n(SGD)。我们来看一下训练过程中每一步的执行。\r\n\r\n假设用 **t** 表示上面这个例子中`quick` 来预测 `the` 的训练的单个循环。用 `num_noise` 定义从噪声分布中挑选出来的噪声（相反的）单词的个数，通常使用一元分布，**P(w)**。为了简单起见，我们就定`num_noise=1`，用 `sheep` 选作噪声词。接下来就可以计算每一对观察值和噪声值的损失函数了，每一个执行步骤就可表示为：\r\n\r\n![](../images/vr4.png)\r\n\r\n整个计算过程的目标是通过更新嵌套参数 ![](../images/theta.png) 来逼近目标函数（这个这个例子中就是使目标函数最大化）。为此我们要计算损失函数中嵌套参数 ![](../images/theta.png) 的梯度，比如，\r\n![](../images/vr5.png) (幸好TensorFlow封装了工具函数可以简单调用!)。对于整个数据集，当梯度下降的过程中不断地更新参数，对应产生的效果就是不断地移动每个单词的嵌套向量，直到可以把真实单词和噪声单词很好得区分开。\r\n\r\n我们可以把学习向量映射到2维中以便我们观察，其中用到的技术可以参考\r\n[t-SNE 降纬技术](http://lvdmaaten.github.io/tsne/)。当我们用可视化的方式来观察这些向量，就可以很明显的获取单词之间语义信息的关系，这实际上是非常有用的。当我们第一次发现这样的诱导向量空间中，展示了一些特定的语义关系，这是非常有趣的，比如文字中 *male-female*，*gender* 甚至还有 *country-capital* 的关系, 如下方的图所示 (也可以参考\r\n[Mikolov et al., 2013](http://www.aclweb.org/anthology/N13-1090)论文中的例子)。\r\n\r\n<div style=\"width:100%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/linear-relationships.png\" alt>\r\n</div>\r\n\r\n这也解释了为什么这些向量在传统的NLP问题中可作为特性使用，比如用在对一个演讲章节打个标签，或者对一个专有名词的识别\r\n(看看如下这个例子\r\n[Collobert et al.](http://arxiv.org/pdf/1103.0398v1.pdf)或者\r\n[Turian et al.](http://www.aclweb.org/anthology/P10-1040))。\r\n\r\n不过现在让我们用它们来画漂亮的图表吧！\r\n\r\n## 建立图形 <a class=\"md-anchor\" id=\"AUTOGENERATED-building-the-graph\"></a>\r\n\r\n这里谈得都是嵌套，那么先来定义一个嵌套参数矩阵。我们用唯一的随机值来初始化这个大矩阵。\r\n\r\n\r\n```python\r\nembeddings = tf.Variable(\r\n    tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))\r\n```\r\n\r\n对噪声-比对的损失计算就使用一个逻辑回归模型。对此，我们需要对语料库中的每个单词定义一个权重值和偏差值。(也可称之为`输出权重` 与之对应的 `输入嵌套值`)。定义如下。\r\n\r\n```python\r\nnce_weights = tf.Variable(\r\n  tf.truncated_normal([vocabulary_size, embedding_size],\r\n                      stddev=1.0 / math.sqrt(embedding_size)))\r\nnce_biases = tf.Variable(tf.zeros([vocabulary_size]))\r\n```\r\n\r\n我们有了这些参数之后，就可以定义Skip-Gram模型了。简单起见，假设我们已经把语料库中的文字整型化了，这样每个整型代表一个单词(细节请查看\r\n[tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py))。Skip-Gram模型有两个输入。一个是一组用整型表示的上下文单词，另一个是目标单词。给这些输入建立占位符节点，之后就可以填入数据了。\r\n\r\n```python\r\n# 建立输入占位符\r\ntrain_inputs = tf.placeholder(tf.int32, shape=[batch_size])\r\ntrain_labels = tf.placeholder(tf.int32, shape=[batch_size, 1])\r\n```\r\n\r\n然后我们需要对批数据中的单词建立嵌套向量，TensorFlow提供了方便的工具函数。\r\n\r\n```python\r\nembed = tf.nn.embedding_lookup(embeddings, train_inputs)\r\n```\r\n\r\n好了，现在我们有了每个单词的嵌套向量，接下来就是使用噪声-比对的训练方式来预测目标单词。\r\n\r\n```python\r\n# 计算 NCE 损失函数, 每次使用负标签的样本.\r\nloss = tf.reduce_mean(\r\n  tf.nn.nce_loss(nce_weights, nce_biases, embed, train_labels,\r\n                 num_sampled, vocabulary_size))\r\n```\r\n\r\n我们对损失函数建立了图形节点，然后我们需要计算相应梯度和更新参数的节点，比如说在这里我们会使用随机梯度下降法，TensorFlow也已经封装好了该过程。\r\n\r\n```python\r\n# 使用 SGD 控制器.\r\noptimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0).minimize(loss)\r\n```\r\n\r\n## 训练模型 <a class=\"md-anchor\" id=\"AUTOGENERATED-training-the-model\"></a>\r\n\r\n训练的过程很简单，只要在循环中使用`feed_dict`不断给占位符填充数据，同时调用\r\n[`session.run`](tensorflow-zh/SOURCE/api_docs/python/client.md#Session.run)即可。\r\n\r\n```python\r\nfor inputs, labels in generate_batch(...):\r\n  feed_dict = {training_inputs: inputs, training_labels: labels}\r\n  _, cur_loss = session.run([optimizer, loss], feed_dict=feed_dict)\r\n```\r\n\r\n完整地例子可参考\r\n[tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py](./word2vec_basic.py).\r\n\r\n## 嵌套学习结果可视化 <a class=\"md-anchor\" id=\"AUTOGENERATED-visualizing-the-learned-embeddings\"></a>\r\n\r\n使用t-SNE来看一下嵌套学习完成的结果。\r\n\r\n<div style=\"width:100%; margin:auto; margin-bottom:10px; margin-top:20px;\">\r\n<img style=\"width:100%\" src=\"../images/tsne.png\" alt>\r\n</div>\r\n\r\nEt voila! 与预期的一样，相似的单词被聚类在一起。对word2vec模型更复杂的实现需要用到TensorFlow一些更高级的特性，具体是实现可以参考\r\n[tensorflow/models/embedding/word2vec.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/embedding/word2vec.py)。\r\n\r\n## 嵌套学习的评估: 类比推理 <a class=\"md-anchor\" id=\"AUTOGENERATED-evaluating-embeddings--analogical-reasoning\"></a>\r\n\r\n词嵌套在NLP的预测问题中是非常有用且使用广泛地。如果要检测一个模型是否是可以成熟地区分词性或者区分专有名词的模型，最简单的办法就是直接检验它的预测词性、语义关系的能力，比如让它解决形如`king is to queen as father is to ?`这样的问题。这种方法叫做*类比推理* ，可参考[Mikolov and colleagues](http://msr-waypoint.com/en-us/um/people/gzweig/Pubs/NAACL2013Regularities.pdf)，数据集下载地址为: \r\nhttps://word2vec.googlecode.com/svn/trunk/questions-words.txt。\r\n\r\nTo see how we do this evaluation如何执行这样的评估，可以看`build_eval_graph()`和\r\n`eval()`这两个函数在下面源码中的使用\r\n[tensorflow/models/embedding/word2vec.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/embedding/word2vec.py).\r\n\r\n超参数的选择对该问题解决的准确性有巨大的影响。想要模型具有很好的表现，需要有一个巨大的训练数据集，同时仔细调整参数的选择并且使用例如二次抽样的一些技巧。不过这些问题已经超出了本教程的范围。\r\n\r\n## 优化实现 <a class=\"md-anchor\" id=\"AUTOGENERATED-optimizing-the-implementation\"></a>\r\n\r\n以上简单的例子展示了TensorFlow的灵活性。比如说，我们可以很轻松得用现成的`tf.nn.sampled_softmax_loss()`来代替`tf.nn.nce_loss()`构成目标函数。如果你对损失函数想做新的尝试，你可以用TensorFlow手动编写新的目标函数的表达式，然后用控制器执行计算。这种灵活性的价值体现在，当我们探索一个机器学习模型时，我们可以很快地遍历这些尝试，从中选出最优。\r\n\r\n一旦你有了一个满意的模型结构，或许它就可以使实现运行地更高效（在短时间内覆盖更多的数据）。比如说，在本教程中使用的简单代码，实际运行速度都不错，因为我们使用Python来读取和填装数据，而这些在TensorFlow后台只需执行非常少的工作。如果你发现你的模型在输入数据时存在严重的瓶颈，你可以根据自己的实际问题自行实现一个数据阅读器，参考\r\n[新的数据格式](tensorflow-zh/SOURCE/how_tos/new_data_formats/index.md)。对于Skip-Gram\r\n模型，我们已经完成了如下这个例子\r\n[tensorflow/models/embedding/word2vec.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/embedding/word2vec.py)。\r\n\r\n如果I/O问题对你的模型已经不再是个问题，并且想进一步地优化性能，或许你可以自行编写TensorFlow操作单元，详见\r\n[添加一个新的操作](tensorflow-zh/SOURCE/how_tos/adding_an_op/index.md)。相应的，我们也提供了Skip-Gram模型的例子\r\n[tensorflow/models/embedding/word2vec_optimized.py](https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/embedding/word2vec_optimized.py)。请自行调节以上几个过程的标准，使模型在每个运行阶段有更好地性能。\r\n\r\n## 总结 <a class=\"md-anchor\" id=\"AUTOGENERATED-conclusion\"></a>\r\n\r\n在本教程中我们介绍了word2vec模型,它在解决词嵌套问题中具有良好的性能。我们解释了使用词嵌套模型的实用性，并且讨论了如何使用TensorFlow实现该模型的高效训练。总的来说，我们希望这个例子能够让向你展示TensorFlow可以提供实验初期的灵活性，以及在后期优化模型时对模型内部的可操控性。\r\n\r\n原文地址：[Vector Representation of Words](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/tutorials/word2vec/index.md) 翻译：[btpeter](https://github.com/btpeter) 校对：waiwaizheng\r\n"
  },
  {
    "path": "TOC.md",
    "content": "- 起步\r\n  - [介绍](SOURCE/get_started/introduction.md)\r\n  - [下载及安装](SOURCE/get_started/os_setup.md)\r\n  - [基本用法](SOURCE/get_started/basic_usage.md)\r\n- 教程\r\n  - [总览](SOURCE/tutorials/overview.md)\r\n  - [MNIST 机器学习入门](SOURCE/tutorials/mnist_beginners.md)\r\n  - [深入 MNIST](SOURCE/tutorials/mnist_pros.md)\r\n  - [TensorFlow 运作方式入门](SOURCE/tutorials/mnist_tf.md)\r\n  - [卷积神经网络](SOURCE/tutorials/deep_cnn.md)\r\n  - [字词的向量表示](SOURCE/tutorials/word2vec.md)\r\n  - [递归神经网络](SOURCE/tutorials/recurrent.md)\r\n  - [曼德布洛特(Mandelbrot)集合](SOURCE/tutorials/mandelbrot.md)\r\n  - [偏微分方程](SOURCE/tutorials/pdes.md) \r\n  - [MNIST数据下载](SOURCE/tutorials/mnist_download.md)\r\n- 运作方式\r\n  - [总览](SOURCE/how_tos/overview.md) \r\n  - [变量:创建、初始化、保存和加载](SOURCE/how_tos/variables.md) \r\n  - [TensorBoard:可视化学习](SOURCE/how_tos/summaries_and_tensorboard.md) \r\n  - [TensorBoard:图表可视化](SOURCE/how_tos/graph_viz.md) \r\n  - [读取数据](SOURCE/how_tos/reading_data.md) \r\n  - [线程和队列](SOURCE/how_tos/threading_and_queues.md) \r\n  - [添加新的Op](SOURCE/how_tos/adding_an_op.md) \r\n  - [自定义数据读取](SOURCE/how_tos/new_data_formats.md) \r\n  - [使用gpu](SOURCE/how_tos/using_gpu.md) \r\n  - [共享变量](SOURCE/how_tos/variable_scope.md) \r\n- 资源\r\n  - [总览](SOURCE/resources/overview.md) \r\n  - [BibTex 引用](SOURCE/resources/bib.md) \r\n  - [示例使用](SOURCE/resources/uses.md)\r\n  - [FAQ](SOURCE/resources/faq.md)\r\n  - [术语表](SOURCE/resources/glossary.md)\r\n  - [Tensor排名、形状和类型](SOURCE/resources/dims_types.md)\r\n- 其他\r\n  - [常见问题汇总](SOURCE/faq.md)\r\n  - [相关资源](SOURCE/resource.md)\r\n  - [个人学习心得](SOURCE/personal.md)\r\n\r\n  \r\n \n"
  },
  {
    "path": "config.json",
    "content": "{\r\n    \"name\": \"TensorFlow 官方文档中文版\",\r\n    \"introduction\": \"Google 人工智能系统 TensorFlow 官方文档中文版协同翻译。\",\r\n    \"path\": {\r\n        \"content\": \"SOURCE\",\r\n        \"images\": \"SOURCE/images\"\r\n    }\r\n\r\n}\r\n"
  },
  {
    "path": "learn-github.md",
    "content": "\r\n## 注册 GitHub 账号\r\n\r\n<http://wiki.jikexueyuan.com/project/github-basics/sign-up.html>\r\n\r\n## 创建仓库\r\n\r\n<http://wiki.jikexueyuan.com/project/github-basics/creat-new-repo.html>\r\n\r\n## 工具安装，克隆仓库到本地\r\n\r\nWindows 用户详见文档：\r\n\r\n<http://wiki.jikexueyuan.com/project/github-basics/github-for-windows.html>\r\n\r\nMac 用户详见文档：\r\n\r\n<http://wiki.jikexueyuan.com/project/github-basics/github-for-mac.html>\r\n\r\n>注意：你克隆到本地的文件内至少应包含一个 README.md 文件。![](images/begin.png)\r\n\r\n至此，你已经拥有了一个 GitHub 账号，一个远程仓库，一个本地仓库。下一步，你将要花费 5 分钟的时间了解下 [Markdown 的简单语法](markdown.md)\r\n"
  },
  {
    "path": "markdown.md",
    "content": "## Markdown 工具下载\r\n\r\n- Windows 用户推荐使用编辑器 MarkdownPad  \r\n下载地址：<http://markdownpad.com>\r\n  \r\n- Mac 用户推荐使用编辑器 Mou   \r\n下载地址：<http://25.io/mou>\r\n\r\n## 标题\r\n\r\n在标题内容前添加 `# + 空格` 就可以实现标题样式的添加：\r\n\r\n- 一级标题就添加一个 `#` + 空格，例如：# 我是一级标题\r\n- 二级标题就添加两个 `#` + 空格，例如：## 我是二级标题。\r\n- 三级标题就添加三个 `#` + 空格，例如：### 我是三级标题。 \r\n\r\n依次类推...\r\n\r\n## 特殊文字处理\r\n\r\n### 换行\r\n\r\n书写示例：\r\n\r\n```\r\n我是第一行（后面有两个空格）  \r\n我是第二行\r\n```\r\n\r\n效果：  \r\n\r\n我是第一行（后面有两个空格）  \r\n我是第二行\r\n\r\n### 加粗\r\n\r\n书写示例：\r\n\r\n```\r\n**我是粗体**\r\n```\r\n\r\n效果：  \r\n**我是粗体** \r\n\r\n### 标亮\r\n\r\n书写示例：\r\n\r\n```\r\n`请把我标亮`\r\n```\r\n\r\n效果：  \r\n\r\n`请把我标亮` \r\n\r\n## 列表\r\n\r\n### 一级列表（不带序号）\r\n\r\n书写示例：\r\n\r\n```\r\n- 列表 1（减号 + 空格）\r\n- 列表 2\r\n```\r\n\r\n效果：\r\n\r\n- 列表 1（减号 + 空格）\r\n- 列表 2\r\n\r\n### 一级列表（带序号）\r\n\r\n书写示例：\r\n\r\n```\r\n1. 列表 1（自然数 + 点 + 空格）\r\n2. 列表 2\r\n```\r\n\r\n效果：\r\n\r\n1. 列表 1（自然数 + 点 + 空格）\r\n2. 列表 2\r\n\r\n### 二级列表（不带序号）\r\n\r\n书写示例：\r\n\r\n```\r\n- 列表 1（一级列表：减号 + 空格）\r\n - 列表 1.1（二级列表：空格 + 减号 + 空格）\r\n - 列表 1.2\r\n- 列表 2\r\n```\r\n\r\n效果：\r\n\r\n- 列表 1（一级列表：减号 + 空格）   \r\n - 列表 1.1（二级列表：空格 + 减号 + 空格）   \r\n - 列表 1.2   \r\n- 列表 2\r\n\r\n## 添加图片\r\n\r\n书写示例：\r\n\r\n```\r\n![图片描述性文字](图片相对地址)\r\n```\r\n\r\n效果：  \r\n\r\n![我是示例图片的描述](http://s1.jikexueyuan.com/common/images/logo_c8caff4.png)\r\n\r\n## 添加超链接  \r\n\r\n书写示例：\r\n\r\n```\r\n[链接描述文字（可见）](链接地址)\r\n```\r\n\r\n效果：  \r\n\r\n[点击跳转到极客学院](http://www.jikexueyuan.com) \r\n\r\n## 引用\r\n\r\n书写示例：\r\n\r\n```\r\n>注意：  \r\n我是引用的内容（空一行就结束引用）。\r\n```\r\n\r\n效果：\r\n  \r\n>注意：  \r\n我是引用的内容（空一行就结束引用）。\r\n\r\n## 添加代码\r\n\r\n书写示例：\r\n\r\n<pre>\r\n```\r\n// 代码区域的上下分别用三个 ` 括起来\r\npublic class Person {\r\n    // 代码缩进请使用 四个空格，不要使用 Tab\r\n}\r\n```\r\n</pre>\r\n\r\n效果： \r\n\r\n```\r\n// 代码区域的上下分别用三个 ` 括起来\r\npublic class Person {\r\n    // 代码缩进请使用 四个空格，不要使用 Tab\r\n}\r\n```\r\n\r\n## 锚点使用\r\n\r\n书写示例：\r\n\r\n```\r\n极客学院是中国最大的 IT 职业在线教育平台。[[锚点名]](#footnode)\r\n\r\n[锚点名]<a name=\"footnode\"></a>我的示例。\r\n\r\n```\r\n\r\n效果：\r\n\r\n极客学院是中国最大的 IT 职业在线教育平台。[[锚点名]](#footnode)\r\n\r\n[锚点名]<a name=\"footnode\"></a>我的示例。\r\n\r\n## 特别注意\r\n\r\n1. 英文字母与中文之间需要两边空格（英文与符号之间不空格）\r\n2. 高亮英文之间需要两边空格\r\n3. 代码符号\\`\\`\\`上面需要空一行，结束```下面需要空一行\r\n4. 中文之间和短英文之间使用中文标点\r\n5. 一篇文章中只能出现一个＃号标题\r\n6. 标题＃号之后需要空一格\r\n\r\n\r\n## 表格\r\n\r\n书写示例：\r\n\r\n```\r\n| Prefix       | Framework   |\r\n| ------------- |:-------------:| -----:|\r\n| NS      | Foundation (OS X and iOS) and Application Kit (OS X)  |\r\n| UI      | UIKit (iOS)  |\r\n| AB      | Address Book  |\r\n| CA      | Core Animation  |\r\n| CI      | Core Image  |\r\n```\r\n\r\n效果： \r\n\r\n| Prefix       | Framework   |\r\n| ------------- |:-------------:| -----:|\r\n| NS      | Foundation (OS X and iOS) and Application Kit (OS X)  |\r\n| UI      | UIKit (iOS)  |\r\n| AB      | Address Book  |\r\n| CA      | Core Animation  |\r\n| CI      | Core Image  |\r\n"
  },
  {
    "path": "tex_pdf/api/c4s00.tex",
    "content": "\r\n\r\n\\section{Overview}\\label{api_overview}\r\n\r\nTensorFlow has APIs available in several languages both for constructing\r\nand executing a TensorFlow graph. The Python API is at present the most\r\ncomplete and the easiest to use, but the C++ API may offer some\r\nperformance advantages in graph execution, and supports deployment to\r\nsmall devices such as Android.\r\n\r\nOver time, we hope that the TensorFlow community will develop front ends\r\nfor languages like Go, Java, JavaScript, Lua R, and perhaps others. With\r\n\\href{http://swig.org}{SWIG}, it's relatively easy to develop a\r\nTensorFlow interface for your favorite language.\r\n\r\nNote: Many practical aspects of usage are covered in the Mechanics tab,\r\nand some additional documentation not specific to any particular\r\nlanguage API is available in the Resources tab."
  },
  {
    "path": "tex_pdf/api/cc/ClassEnv.md",
    "content": "# Class `tensorflow::Env` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--env-\"></a>\r\n\r\nAn interface used by the tensorflow implementation to access operating system functionality like the filesystem etc.\r\n\r\nCallers may wish to provide a custom Env object to get fine grain control.\r\n\r\nAll Env implementations are safe for concurrent access from multiple threads without any external synchronization.\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::Env::Env()`](#tensorflow_Env_Env)\r\n* [`virtual tensorflow::Env::~Env()`](#virtual_tensorflow_Env_Env)\r\n* [`virtual Status tensorflow::Env::NewRandomAccessFile(const string &fname, RandomAccessFile **result)=0`](#virtual_Status_tensorflow_Env_NewRandomAccessFile)\r\n  * Creates a brand new random access read-only file with the specified name.\r\n* [`virtual Status tensorflow::Env::NewWritableFile(const string &fname, WritableFile **result)=0`](#virtual_Status_tensorflow_Env_NewWritableFile)\r\n  * Creates an object that writes to a new file with the specified name.\r\n* [`virtual Status tensorflow::Env::NewAppendableFile(const string &fname, WritableFile **result)=0`](#virtual_Status_tensorflow_Env_NewAppendableFile)\r\n  * Creates an object that either appends to an existing file, or writes to a new file (if the file does not exist to begin with).\r\n* [`virtual bool tensorflow::Env::FileExists(const string &fname)=0`](#virtual_bool_tensorflow_Env_FileExists)\r\n  * Returns true iff the named file exists.\r\n* [`virtual Status tensorflow::Env::GetChildren(const string &dir, std::vector< string > *result)=0`](#virtual_Status_tensorflow_Env_GetChildren)\r\n  * Stores in *result the names of the children of the specified directory. The names are relative to \"dir\".\r\n* [`virtual Status tensorflow::Env::DeleteFile(const string &fname)=0`](#virtual_Status_tensorflow_Env_DeleteFile)\r\n  * Deletes the named file.\r\n* [`virtual Status tensorflow::Env::CreateDir(const string &dirname)=0`](#virtual_Status_tensorflow_Env_CreateDir)\r\n  * Creates the specified directory.\r\n* [`virtual Status tensorflow::Env::DeleteDir(const string &dirname)=0`](#virtual_Status_tensorflow_Env_DeleteDir)\r\n  * Deletes the specified directory.\r\n* [`virtual Status tensorflow::Env::GetFileSize(const string &fname, uint64 *file_size)=0`](#virtual_Status_tensorflow_Env_GetFileSize)\r\n  * Stores the size of fname in *file_size.\r\n* [`virtual Status tensorflow::Env::RenameFile(const string &src, const string &target)=0`](#virtual_Status_tensorflow_Env_RenameFile)\r\n  * Renames file src to target. If target already exists, it will be replaced.\r\n* [`virtual uint64 tensorflow::Env::NowMicros()=0`](#virtual_uint64_tensorflow_Env_NowMicros)\r\n  * Returns the number of micro-seconds since some fixed point in time. Only useful for computing deltas of time.\r\n* [`virtual void tensorflow::Env::SleepForMicroseconds(int micros)=0`](#virtual_void_tensorflow_Env_SleepForMicroseconds)\r\n  * Sleeps/delays the thread for the prescribed number of micro-seconds.\r\n* [`virtual Thread* tensorflow::Env::StartThread(const ThreadOptions &thread_options, const string &name, std::function< void()> fn) TF_MUST_USE_RESULT=0`](#virtual_Thread_tensorflow_Env_StartThread)\r\n  * Returns a new thread that is running fn() and is identified (for debugging/performance-analysis) by \"name\".\r\n* [`static Env* tensorflow::Env::Default()`](#static_Env_tensorflow_Env_Default)\r\n  * Returns a default environment suitable for the current operating system.\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::Env::Env()` <a class=\"md-anchor\" id=\"tensorflow_Env_Env\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual tensorflow::Env::~Env()` <a class=\"md-anchor\" id=\"virtual_tensorflow_Env_Env\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::Env::NewRandomAccessFile(const string &fname, RandomAccessFile **result)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_NewRandomAccessFile\"></a>\r\n\r\nCreates a brand new random access read-only file with the specified name.\r\n\r\nOn success, stores a pointer to the new file in *result and returns OK. On failure stores NULL in *result and returns non-OK. If the file does not exist, returns a non-OK status.\r\n\r\nThe returned file may be concurrently accessed by multiple threads.\r\n\r\n#### `virtual Status tensorflow::Env::NewWritableFile(const string &fname, WritableFile **result)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_NewWritableFile\"></a>\r\n\r\nCreates an object that writes to a new file with the specified name.\r\n\r\nDeletes any existing file with the same name and creates a new file. On success, stores a pointer to the new file in *result and returns OK. On failure stores NULL in *result and returns non-OK.\r\n\r\nThe returned file will only be accessed by one thread at a time.\r\n\r\n#### `virtual Status tensorflow::Env::NewAppendableFile(const string &fname, WritableFile **result)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_NewAppendableFile\"></a>\r\n\r\nCreates an object that either appends to an existing file, or writes to a new file (if the file does not exist to begin with).\r\n\r\nOn success, stores a pointer to the new file in *result and returns OK. On failure stores NULL in *result and returns non-OK.\r\n\r\nThe returned file will only be accessed by one thread at a time.\r\n\r\n#### `virtual bool tensorflow::Env::FileExists(const string &fname)=0` <a class=\"md-anchor\" id=\"virtual_bool_tensorflow_Env_FileExists\"></a>\r\n\r\nReturns true iff the named file exists.\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::Env::GetChildren(const string &dir, std::vector< string > *result)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_GetChildren\"></a>\r\n\r\nStores in *result the names of the children of the specified directory. The names are relative to \"dir\".\r\n\r\nOriginal contents of *results are dropped.\r\n\r\n#### `virtual Status tensorflow::Env::DeleteFile(const string &fname)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_DeleteFile\"></a>\r\n\r\nDeletes the named file.\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::Env::CreateDir(const string &dirname)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_CreateDir\"></a>\r\n\r\nCreates the specified directory.\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::Env::DeleteDir(const string &dirname)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_DeleteDir\"></a>\r\n\r\nDeletes the specified directory.\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::Env::GetFileSize(const string &fname, uint64 *file_size)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_GetFileSize\"></a>\r\n\r\nStores the size of fname in *file_size.\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::Env::RenameFile(const string &src, const string &target)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Env_RenameFile\"></a>\r\n\r\nRenames file src to target. If target already exists, it will be replaced.\r\n\r\n\r\n\r\n#### `virtual uint64 tensorflow::Env::NowMicros()=0` <a class=\"md-anchor\" id=\"virtual_uint64_tensorflow_Env_NowMicros\"></a>\r\n\r\nReturns the number of micro-seconds since some fixed point in time. Only useful for computing deltas of time.\r\n\r\n\r\n\r\n#### `virtual void tensorflow::Env::SleepForMicroseconds(int micros)=0` <a class=\"md-anchor\" id=\"virtual_void_tensorflow_Env_SleepForMicroseconds\"></a>\r\n\r\nSleeps/delays the thread for the prescribed number of micro-seconds.\r\n\r\n\r\n\r\n#### `virtual Thread* tensorflow::Env::StartThread(const ThreadOptions &thread_options, const string &name, std::function< void()> fn) TF_MUST_USE_RESULT=0` <a class=\"md-anchor\" id=\"virtual_Thread_tensorflow_Env_StartThread\"></a>\r\n\r\nReturns a new thread that is running fn() and is identified (for debugging/performance-analysis) by \"name\".\r\n\r\nCaller takes ownership of the result and must delete it eventually (the deletion will block until fn() stops running).\r\n\r\n#### `static Env* tensorflow::Env::Default()` <a class=\"md-anchor\" id=\"static_Env_tensorflow_Env_Default\"></a>\r\n\r\nReturns a default environment suitable for the current operating system.\r\n\r\nSophisticated users may wish to provide their own Env implementation instead of relying on this default environment.\r\n\r\nThe result of Default() belongs to this library and must never be deleted.\r\n"
  },
  {
    "path": "tex_pdf/api/cc/ClassEnvWrapper.md",
    "content": "# Class `tensorflow::EnvWrapper` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--envwrapper-\"></a>\r\n\r\nAn implementation of Env that forwards all calls to another Env .\r\n\r\nMay be useful to clients who wish to override just part of the functionality of another Env .\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::EnvWrapper::EnvWrapper(Env *t)`](#tensorflow_EnvWrapper_EnvWrapper)\r\n  * Initializes an EnvWrapper that delegates all calls to *t.\r\n* [`virtual tensorflow::EnvWrapper::~EnvWrapper()`](#virtual_tensorflow_EnvWrapper_EnvWrapper)\r\n* [`Env* tensorflow::EnvWrapper::target() const`](#Env_tensorflow_EnvWrapper_target)\r\n  * Returns the target to which this Env forwards all calls.\r\n* [`Status tensorflow::EnvWrapper::NewRandomAccessFile(const string &f, RandomAccessFile **r) override`](#Status_tensorflow_EnvWrapper_NewRandomAccessFile)\r\n  * Creates a brand new random access read-only file with the specified name.\r\n* [`Status tensorflow::EnvWrapper::NewWritableFile(const string &f, WritableFile **r) override`](#Status_tensorflow_EnvWrapper_NewWritableFile)\r\n  * Creates an object that writes to a new file with the specified name.\r\n* [`Status tensorflow::EnvWrapper::NewAppendableFile(const string &f, WritableFile **r) override`](#Status_tensorflow_EnvWrapper_NewAppendableFile)\r\n  * Creates an object that either appends to an existing file, or writes to a new file (if the file does not exist to begin with).\r\n* [`bool tensorflow::EnvWrapper::FileExists(const string &f) override`](#bool_tensorflow_EnvWrapper_FileExists)\r\n  * Returns true iff the named file exists.\r\n* [`Status tensorflow::EnvWrapper::GetChildren(const string &dir, std::vector< string > *r) override`](#Status_tensorflow_EnvWrapper_GetChildren)\r\n  * Stores in *result the names of the children of the specified directory. The names are relative to \"dir\".\r\n* [`Status tensorflow::EnvWrapper::DeleteFile(const string &f) override`](#Status_tensorflow_EnvWrapper_DeleteFile)\r\n  * Deletes the named file.\r\n* [`Status tensorflow::EnvWrapper::CreateDir(const string &d) override`](#Status_tensorflow_EnvWrapper_CreateDir)\r\n  * Creates the specified directory.\r\n* [`Status tensorflow::EnvWrapper::DeleteDir(const string &d) override`](#Status_tensorflow_EnvWrapper_DeleteDir)\r\n  * Deletes the specified directory.\r\n* [`Status tensorflow::EnvWrapper::GetFileSize(const string &f, uint64 *s) override`](#Status_tensorflow_EnvWrapper_GetFileSize)\r\n  * Stores the size of fname in *file_size.\r\n* [`Status tensorflow::EnvWrapper::RenameFile(const string &s, const string &t) override`](#Status_tensorflow_EnvWrapper_RenameFile)\r\n  * Renames file src to target. If target already exists, it will be replaced.\r\n* [`uint64 tensorflow::EnvWrapper::NowMicros() override`](#uint64_tensorflow_EnvWrapper_NowMicros)\r\n  * Returns the number of micro-seconds since some fixed point in time. Only useful for computing deltas of time.\r\n* [`void tensorflow::EnvWrapper::SleepForMicroseconds(int micros) override`](#void_tensorflow_EnvWrapper_SleepForMicroseconds)\r\n  * Sleeps/delays the thread for the prescribed number of micro-seconds.\r\n* [`Thread* tensorflow::EnvWrapper::StartThread(const ThreadOptions &thread_options, const string &name, std::function< void()> fn) override`](#Thread_tensorflow_EnvWrapper_StartThread)\r\n  * Returns a new thread that is running fn() and is identified (for debugging/performance-analysis) by \"name\".\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::EnvWrapper::EnvWrapper(Env *t)` <a class=\"md-anchor\" id=\"tensorflow_EnvWrapper_EnvWrapper\"></a>\r\n\r\nInitializes an EnvWrapper that delegates all calls to *t.\r\n\r\n\r\n\r\n#### `virtual tensorflow::EnvWrapper::~EnvWrapper()` <a class=\"md-anchor\" id=\"virtual_tensorflow_EnvWrapper_EnvWrapper\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `Env* tensorflow::EnvWrapper::target() const` <a class=\"md-anchor\" id=\"Env_tensorflow_EnvWrapper_target\"></a>\r\n\r\nReturns the target to which this Env forwards all calls.\r\n\r\n\r\n\r\n#### `Status tensorflow::EnvWrapper::NewRandomAccessFile(const string &f, RandomAccessFile **r) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_NewRandomAccessFile\"></a>\r\n\r\nCreates a brand new random access read-only file with the specified name.\r\n\r\nOn success, stores a pointer to the new file in *result and returns OK. On failure stores NULL in *result and returns non-OK. If the file does not exist, returns a non-OK status.\r\n\r\nThe returned file may be concurrently accessed by multiple threads.\r\n\r\n#### `Status tensorflow::EnvWrapper::NewWritableFile(const string &f, WritableFile **r) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_NewWritableFile\"></a>\r\n\r\nCreates an object that writes to a new file with the specified name.\r\n\r\nDeletes any existing file with the same name and creates a new file. On success, stores a pointer to the new file in *result and returns OK. On failure stores NULL in *result and returns non-OK.\r\n\r\nThe returned file will only be accessed by one thread at a time.\r\n\r\n#### `Status tensorflow::EnvWrapper::NewAppendableFile(const string &f, WritableFile **r) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_NewAppendableFile\"></a>\r\n\r\nCreates an object that either appends to an existing file, or writes to a new file (if the file does not exist to begin with).\r\n\r\nOn success, stores a pointer to the new file in *result and returns OK. On failure stores NULL in *result and returns non-OK.\r\n\r\nThe returned file will only be accessed by one thread at a time.\r\n\r\n#### `bool tensorflow::EnvWrapper::FileExists(const string &f) override` <a class=\"md-anchor\" id=\"bool_tensorflow_EnvWrapper_FileExists\"></a>\r\n\r\nReturns true iff the named file exists.\r\n\r\n\r\n\r\n#### `Status tensorflow::EnvWrapper::GetChildren(const string &dir, std::vector< string > *r) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_GetChildren\"></a>\r\n\r\nStores in *result the names of the children of the specified directory. The names are relative to \"dir\".\r\n\r\nOriginal contents of *results are dropped.\r\n\r\n#### `Status tensorflow::EnvWrapper::DeleteFile(const string &f) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_DeleteFile\"></a>\r\n\r\nDeletes the named file.\r\n\r\n\r\n\r\n#### `Status tensorflow::EnvWrapper::CreateDir(const string &d) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_CreateDir\"></a>\r\n\r\nCreates the specified directory.\r\n\r\n\r\n\r\n#### `Status tensorflow::EnvWrapper::DeleteDir(const string &d) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_DeleteDir\"></a>\r\n\r\nDeletes the specified directory.\r\n\r\n\r\n\r\n#### `Status tensorflow::EnvWrapper::GetFileSize(const string &f, uint64 *s) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_GetFileSize\"></a>\r\n\r\nStores the size of fname in *file_size.\r\n\r\n\r\n\r\n#### `Status tensorflow::EnvWrapper::RenameFile(const string &s, const string &t) override` <a class=\"md-anchor\" id=\"Status_tensorflow_EnvWrapper_RenameFile\"></a>\r\n\r\nRenames file src to target. If target already exists, it will be replaced.\r\n\r\n\r\n\r\n#### `uint64 tensorflow::EnvWrapper::NowMicros() override` <a class=\"md-anchor\" id=\"uint64_tensorflow_EnvWrapper_NowMicros\"></a>\r\n\r\nReturns the number of micro-seconds since some fixed point in time. Only useful for computing deltas of time.\r\n\r\n\r\n\r\n#### `void tensorflow::EnvWrapper::SleepForMicroseconds(int micros) override` <a class=\"md-anchor\" id=\"void_tensorflow_EnvWrapper_SleepForMicroseconds\"></a>\r\n\r\nSleeps/delays the thread for the prescribed number of micro-seconds.\r\n\r\n\r\n\r\n#### `Thread* tensorflow::EnvWrapper::StartThread(const ThreadOptions &thread_options, const string &name, std::function< void()> fn) override` <a class=\"md-anchor\" id=\"Thread_tensorflow_EnvWrapper_StartThread\"></a>\r\n\r\nReturns a new thread that is running fn() and is identified (for debugging/performance-analysis) by \"name\".\r\n\r\nCaller takes ownership of the result and must delete it eventually (the deletion will block until fn() stops running).\r\n"
  },
  {
    "path": "tex_pdf/api/cc/ClassRandomAccessFile.md",
    "content": "# Class `tensorflow::RandomAccessFile` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--randomaccessfile-\"></a>\r\n\r\nA file abstraction for randomly reading the contents of a file.\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::RandomAccessFile::RandomAccessFile()`](#tensorflow_RandomAccessFile_RandomAccessFile)\r\n* [`virtual tensorflow::RandomAccessFile::~RandomAccessFile()`](#virtual_tensorflow_RandomAccessFile_RandomAccessFile)\r\n* [`virtual Status tensorflow::RandomAccessFile::Read(uint64 offset, size_t n, StringPiece *result, char *scratch) const =0`](#virtual_Status_tensorflow_RandomAccessFile_Read)\r\n  * Reads up to \"n\" bytes from the file starting at \"offset\".\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::RandomAccessFile::RandomAccessFile()` <a class=\"md-anchor\" id=\"tensorflow_RandomAccessFile_RandomAccessFile\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual tensorflow::RandomAccessFile::~RandomAccessFile()` <a class=\"md-anchor\" id=\"virtual_tensorflow_RandomAccessFile_RandomAccessFile\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::RandomAccessFile::Read(uint64 offset, size_t n, StringPiece *result, char *scratch) const =0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_RandomAccessFile_Read\"></a>\r\n\r\nReads up to \"n\" bytes from the file starting at \"offset\".\r\n\r\n\"scratch[0..n-1]\" may be written by this routine. Sets \"*result\" to the data that was read (including if fewer than \"n\" bytes were successfully read). May set \"*result\" to point at data in \"scratch[0..n-1]\", so \"scratch[0..n-1]\" must be live when \"*result\" is used.\r\n\r\nOn OK returned status: \"n\" bytes have been stored in \"*result\". On non-OK returned status: [0..n] bytes have been stored in \"*result\".\r\n\r\nReturns `OUT_OF_RANGE` if fewer than n bytes were stored in \"*result\" because of EOF.\r\n\r\nSafe for concurrent use by multiple threads.\r\n"
  },
  {
    "path": "tex_pdf/api/cc/ClassSession.md",
    "content": "# Class `tensorflow::Session` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--session-\"></a>\r\n\r\nA Session instance lets a caller drive a TensorFlow graph computation.\r\n\r\nWhen a Session is created with a given target, a new Session object is bound to the universe of resources specified by that target. Those resources are available to this session to perform computation described in the GraphDef. After extending the session with a graph, the caller uses the Run() API to perform the computation and potentially fetch outputs as Tensors.\r\n\r\nExample:\r\n\r\n```c++ tensorflow::GraphDef graph;\r\n// ... Create or load graph into \"graph\".\r\n\r\n// This example uses the default options which connects\r\n// to a local runtime.\r\ntensorflow::SessionOptions options;\r\nstd::unique_ptr<tensorflow::Session>\r\nsession(tensorflow::NewSession(options));\r\n\r\n// Create the session with this graph.\r\ntensorflow::Status s = session->Create(graph);\r\nif (!s.ok()) { ... }\r\n\r\n// Run the graph and fetch the first output of the \"output\"\r\n// operation, and also run to but do not return anything\r\n// for the \"update_state\" operation.\r\nstd::vector<tensorflow::Tensor> outputs;\r\ns = session->Run({}, {\"output:0\"}, {\"update_state\"}, &outputs);\r\nif (!s.ok()) { ... }\r\n\r\n// Map the output as a flattened float tensor, and do something\r\n// with it.\r\nauto output_tensor = outputs[0].flat<float>();\r\nif (output_tensor(0) > 0.5) { ... }\r\n\r\n// Close the session to release the resources associated with\r\n// this session.\r\nsession->Close()\r\n\r\n```\r\n\r\nA Session allows concurrent calls to Run() , though a Session must be created / extended by a single thread.\r\n\r\nOnly one thread must call Close() , and Close() must only be called after all other calls to Run() have returned.\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`virtual Status tensorflow::Session::Create(const GraphDef &graph)=0`](#virtual_Status_tensorflow_Session_Create)\r\n  * Create the graph to be used for the session.\r\n* [`virtual Status tensorflow::Session::Extend(const GraphDef &graph)=0`](#virtual_Status_tensorflow_Session_Extend)\r\n  * Adds operations to the graph that is already registered with the Session .\r\n* [`virtual Status tensorflow::Session::Run(const std::vector< std::pair< string, Tensor > > &inputs, const std::vector< string > &output_tensor_names, const std::vector< string > &target_node_names, std::vector< Tensor > *outputs)=0`](#virtual_Status_tensorflow_Session_Run)\r\n  * Runs the graph with the provided input tensors and fills `outputs` for the endpoints specified in `output_tensor_names`. Runs to but does not return Tensors for the nodes in `target_node_names`.\r\n* [`virtual Status tensorflow::Session::Close()=0`](#virtual_Status_tensorflow_Session_Close)\r\n  * Closes this session.\r\n* [`virtual tensorflow::Session::~Session()`](#virtual_tensorflow_Session_Session)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `virtual Status tensorflow::Session::Create(const GraphDef &graph)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Session_Create\"></a>\r\n\r\nCreate the graph to be used for the session.\r\n\r\nReturns an error if this session has already been created with a graph. To re-use the session with a different graph, the caller must Close() the session first.\r\n\r\n#### `virtual Status tensorflow::Session::Extend(const GraphDef &graph)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Session_Extend\"></a>\r\n\r\nAdds operations to the graph that is already registered with the Session .\r\n\r\nThe names of new operations in \"graph\" must not exist in the graph that is already registered.\r\n\r\n#### `virtual Status tensorflow::Session::Run(const std::vector< std::pair< string, Tensor > > &inputs, const std::vector< string > &output_tensor_names, const std::vector< string > &target_node_names, std::vector< Tensor > *outputs)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Session_Run\"></a>\r\n\r\nRuns the graph with the provided input tensors and fills `outputs` for the endpoints specified in `output_tensor_names`. Runs to but does not return Tensors for the nodes in `target_node_names`.\r\n\r\nThe order of tensors in `outputs` will match the order provided by `output_tensor_names`.\r\n\r\nIf `Run` returns `OK()`, then `outputs->size()` will be equal to `output_tensor_names.size()`. If `Run` does not return `OK()`, the state of `outputs` is undefined.\r\n\r\nREQUIRES: The name of each Tensor of the input or output must match a \"Tensor endpoint\" in the `GraphDef` passed to ` Create() `.\r\n\r\nREQUIRES: outputs is not nullptr if `output_tensor_names` is non-empty.\r\n\r\n#### `virtual Status tensorflow::Session::Close()=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_Session_Close\"></a>\r\n\r\nCloses this session.\r\n\r\nClosing a session releases the resources used by this session on the TensorFlow runtime (specified during session creation by the ` SessionOptions::target ` field).\r\n\r\n#### `virtual tensorflow::Session::~Session()` <a class=\"md-anchor\" id=\"virtual_tensorflow_Session_Session\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/cc/ClassStatus.md",
    "content": "# Class `tensorflow::Status` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--status-\"></a>\r\n\r\n\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::Status::Status()`](#tensorflow_Status_Status)\r\n  * Create a success status.\r\n* [`tensorflow::Status::~Status()`](#tensorflow_Status_Status)\r\n* [`tensorflow::Status::Status(tensorflow::error::Code code, tensorflow::StringPiece msg)`](#tensorflow_Status_Status)\r\n  * Create a status with the specified error code and msg as a human-readable string containing more detailed information.\r\n* [`tensorflow::Status::Status(const Status &s)`](#tensorflow_Status_Status)\r\n  * Copy the specified status.\r\n* [`void tensorflow::Status::operator=(const Status &s)`](#void_tensorflow_Status_operator_)\r\n* [`bool tensorflow::Status::ok() const`](#bool_tensorflow_Status_ok)\r\n  * Returns true iff the status indicates success.\r\n* [`tensorflow::error::Code tensorflow::Status::code() const`](#tensorflow_error_Code_tensorflow_Status_code)\r\n* [`const string& tensorflow::Status::error_message() const`](#const_string_tensorflow_Status_error_message)\r\n* [`bool tensorflow::Status::operator==(const Status &x) const`](#bool_tensorflow_Status_operator_)\r\n* [`bool tensorflow::Status::operator!=(const Status &x) const`](#bool_tensorflow_Status_operator_)\r\n* [`void tensorflow::Status::Update(const Status &new_status)`](#void_tensorflow_Status_Update)\r\n  * If ` ok() `, stores `new_status` into `*this`. If `!ok()`, preserves the current status, but may augment with additional information about `new_status`.\r\n* [`string tensorflow::Status::ToString() const`](#string_tensorflow_Status_ToString)\r\n  * Return a string representation of this status suitable for printing. Returns the string `\"OK\"` for success.\r\n* [`static Status tensorflow::Status::OK()`](#static_Status_tensorflow_Status_OK)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::Status::Status()` <a class=\"md-anchor\" id=\"tensorflow_Status_Status\"></a>\r\n\r\nCreate a success status.\r\n\r\n\r\n\r\n#### `tensorflow::Status::~Status()` <a class=\"md-anchor\" id=\"tensorflow_Status_Status\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `tensorflow::Status::Status(tensorflow::error::Code code, tensorflow::StringPiece msg)` <a class=\"md-anchor\" id=\"tensorflow_Status_Status\"></a>\r\n\r\nCreate a status with the specified error code and msg as a human-readable string containing more detailed information.\r\n\r\n\r\n\r\n#### `tensorflow::Status::Status(const Status &s)` <a class=\"md-anchor\" id=\"tensorflow_Status_Status\"></a>\r\n\r\nCopy the specified status.\r\n\r\n\r\n\r\n#### `void tensorflow::Status::operator=(const Status &s)` <a class=\"md-anchor\" id=\"void_tensorflow_Status_operator_\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `bool tensorflow::Status::ok() const` <a class=\"md-anchor\" id=\"bool_tensorflow_Status_ok\"></a>\r\n\r\nReturns true iff the status indicates success.\r\n\r\n\r\n\r\n#### `tensorflow::error::Code tensorflow::Status::code() const` <a class=\"md-anchor\" id=\"tensorflow_error_Code_tensorflow_Status_code\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `const string& tensorflow::Status::error_message() const` <a class=\"md-anchor\" id=\"const_string_tensorflow_Status_error_message\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `bool tensorflow::Status::operator==(const Status &x) const` <a class=\"md-anchor\" id=\"bool_tensorflow_Status_operator_\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `bool tensorflow::Status::operator!=(const Status &x) const` <a class=\"md-anchor\" id=\"bool_tensorflow_Status_operator_\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `void tensorflow::Status::Update(const Status &new_status)` <a class=\"md-anchor\" id=\"void_tensorflow_Status_Update\"></a>\r\n\r\nIf ` ok() `, stores `new_status` into `*this`. If `!ok()`, preserves the current status, but may augment with additional information about `new_status`.\r\n\r\nConvenient way of keeping track of the first error encountered. Instead of: `if (overall_status.ok()) overall_status = new_status` Use: `overall_status.Update(new_status);`\r\n\r\n#### `string tensorflow::Status::ToString() const` <a class=\"md-anchor\" id=\"string_tensorflow_Status_ToString\"></a>\r\n\r\nReturn a string representation of this status suitable for printing. Returns the string `\"OK\"` for success.\r\n\r\n\r\n\r\n#### `static Status tensorflow::Status::OK()` <a class=\"md-anchor\" id=\"static_Status_tensorflow_Status_OK\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/cc/ClassTensor.md",
    "content": "# Class `tensorflow::Tensor` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--tensor-\"></a>\r\n\r\nRepresents an n-dimensional array of values.\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::Tensor::Tensor()`](#tensorflow_Tensor_Tensor)\r\n  * Default Tensor constructor. Creates a 1-dimension, 0-element float tensor.\r\n* [`tensorflow::Tensor::Tensor(DataType type, const TensorShape &shape)`](#tensorflow_Tensor_Tensor)\r\n  * Creates a Tensor of the given `type` and `shape`.\r\n* [`tensorflow::Tensor::Tensor(Allocator *a, DataType type, const TensorShape &shape)`](#tensorflow_Tensor_Tensor)\r\n  * Creates a tensor with the input `type` and `shape`, using the allocator `a` to allocate the underlying buffer.\r\n* [`tensorflow::Tensor::Tensor(DataType type)`](#tensorflow_Tensor_Tensor)\r\n  * Creates an uninitialized Tensor of the given data type.\r\n* [`tensorflow::Tensor::Tensor(const Tensor &other)`](#tensorflow_Tensor_Tensor)\r\n* [`tensorflow::Tensor::~Tensor()`](#tensorflow_Tensor_Tensor)\r\n  * Copy constructor.\r\n* [`DataType tensorflow::Tensor::dtype() const`](#DataType_tensorflow_Tensor_dtype)\r\n  * Returns the data type.\r\n* [`const TensorShape& tensorflow::Tensor::shape() const`](#const_TensorShape_tensorflow_Tensor_shape)\r\n  * Returns the shape of the tensor.\r\n* [`int tensorflow::Tensor::dims() const`](#int_tensorflow_Tensor_dims)\r\n  * Convenience accessor for the tensor shape.\r\n* [`int64 tensorflow::Tensor::dim_size(int d) const`](#int64_tensorflow_Tensor_dim_size)\r\n  * Convenience accessor for the tensor shape.\r\n* [`int64 tensorflow::Tensor::NumElements() const`](#int64_tensorflow_Tensor_NumElements)\r\n  * Convenience accessor for the tensor shape.\r\n* [`bool tensorflow::Tensor::IsSameSize(const Tensor &b) const`](#bool_tensorflow_Tensor_IsSameSize)\r\n* [`bool tensorflow::Tensor::IsInitialized() const`](#bool_tensorflow_Tensor_IsInitialized)\r\n  * Has this Tensor been initialized?\r\n* [`size_t tensorflow::Tensor::TotalBytes() const`](#size_t_tensorflow_Tensor_TotalBytes)\r\n  * Returns the estimated memory usage of this tensor.\r\n* [`Tensor& tensorflow::Tensor::operator=(const Tensor &other)`](#Tensor_tensorflow_Tensor_operator_)\r\n  * Assign operator. This tensor shares other&apos;s underlying storage.\r\n* [`bool tensorflow::Tensor::CopyFrom(const Tensor &other, const TensorShape &shape) TF_MUST_USE_RESULT`](#bool_tensorflow_Tensor_CopyFrom)\r\n  * Copy the other tensor into this tensor and reshape it.\r\n* [`Tensor tensorflow::Tensor::Slice(int64 dim0_start, int64 dim0_limit) const`](#Tensor_tensorflow_Tensor_Slice)\r\n  * Slice this tensor along the 1st dimension.\r\n* [`bool tensorflow::Tensor::FromProto(const TensorProto &other) TF_MUST_USE_RESULT`](#bool_tensorflow_Tensor_FromProto)\r\n  * Parse `other` and construct the tensor.\r\n* [`bool tensorflow::Tensor::FromProto(Allocator *a, const TensorProto &other) TF_MUST_USE_RESULT`](#bool_tensorflow_Tensor_FromProto)\r\n* [`void tensorflow::Tensor::AsProtoField(TensorProto *proto) const`](#void_tensorflow_Tensor_AsProtoField)\r\n  * Fills in `proto` with `*this` tensor&apos;s content.\r\n* [`void tensorflow::Tensor::AsProtoTensorContent(TensorProto *proto) const`](#void_tensorflow_Tensor_AsProtoTensorContent)\r\n* [`TTypes<T>::Vec tensorflow::Tensor::vec()`](#TTypes_T_Vec_tensorflow_Tensor_vec)\r\n  * Return the tensor data as an `Eigen::Tensor` with the type and sizes of this ` Tensor `.\r\n* [`TTypes<T>::Matrix tensorflow::Tensor::matrix()`](#TTypes_T_Matrix_tensorflow_Tensor_matrix)\r\n* [`TTypes< T, NDIMS >::Tensor tensorflow::Tensor::tensor()`](#TTypes_T_NDIMS_Tensor_tensorflow_Tensor_tensor)\r\n* [`TTypes<T>::Flat tensorflow::Tensor::flat()`](#TTypes_T_Flat_tensorflow_Tensor_flat)\r\n  * Return the tensor data as an `Eigen::Tensor` of the data type and a specified shape.\r\n* [`TTypes<T>::UnalignedFlat tensorflow::Tensor::unaligned_flat()`](#TTypes_T_UnalignedFlat_tensorflow_Tensor_unaligned_flat)\r\n* [`TTypes<T>::Matrix tensorflow::Tensor::flat_inner_dims()`](#TTypes_T_Matrix_tensorflow_Tensor_flat_inner_dims)\r\n* [`TTypes<T>::Matrix tensorflow::Tensor::flat_outer_dims()`](#TTypes_T_Matrix_tensorflow_Tensor_flat_outer_dims)\r\n* [`TTypes< T, NDIMS >::Tensor tensorflow::Tensor::shaped(gtl::ArraySlice< int64 > new_sizes)`](#TTypes_T_NDIMS_Tensor_tensorflow_Tensor_shaped)\r\n* [`TTypes< T, NDIMS >::UnalignedTensor tensorflow::Tensor::unaligned_shaped(gtl::ArraySlice< int64 > new_sizes)`](#TTypes_T_NDIMS_UnalignedTensor_tensorflow_Tensor_unaligned_shaped)\r\n* [`TTypes< T >::Scalar tensorflow::Tensor::scalar()`](#TTypes_T_Scalar_tensorflow_Tensor_scalar)\r\n  * Return the Tensor data as a `TensorMap` of fixed size 1: `TensorMap<TensorFixedSize<T, 1>>`.\r\n* [`TTypes<T>::ConstVec tensorflow::Tensor::vec() const`](#TTypes_T_ConstVec_tensorflow_Tensor_vec)\r\n  * Const versions of all the methods above.\r\n* [`TTypes<T>::ConstMatrix tensorflow::Tensor::matrix() const`](#TTypes_T_ConstMatrix_tensorflow_Tensor_matrix)\r\n* [`TTypes< T, NDIMS >::ConstTensor tensorflow::Tensor::tensor() const`](#TTypes_T_NDIMS_ConstTensor_tensorflow_Tensor_tensor)\r\n* [`TTypes<T>::ConstFlat tensorflow::Tensor::flat() const`](#TTypes_T_ConstFlat_tensorflow_Tensor_flat)\r\n* [`TTypes<T>::UnalignedConstFlat tensorflow::Tensor::unaligned_flat() const`](#TTypes_T_UnalignedConstFlat_tensorflow_Tensor_unaligned_flat)\r\n* [`TTypes<T>::ConstMatrix tensorflow::Tensor::flat_inner_dims() const`](#TTypes_T_ConstMatrix_tensorflow_Tensor_flat_inner_dims)\r\n* [`TTypes<T>::ConstMatrix tensorflow::Tensor::flat_outer_dims() const`](#TTypes_T_ConstMatrix_tensorflow_Tensor_flat_outer_dims)\r\n* [`TTypes< T, NDIMS >::ConstTensor tensorflow::Tensor::shaped(gtl::ArraySlice< int64 > new_sizes) const`](#TTypes_T_NDIMS_ConstTensor_tensorflow_Tensor_shaped)\r\n* [`TTypes< T, NDIMS >::UnalignedConstTensor tensorflow::Tensor::unaligned_shaped(gtl::ArraySlice< int64 > new_sizes) const`](#TTypes_T_NDIMS_UnalignedConstTensor_tensorflow_Tensor_unaligned_shaped)\r\n* [`TTypes< T >::ConstScalar tensorflow::Tensor::scalar() const`](#TTypes_T_ConstScalar_tensorflow_Tensor_scalar)\r\n* [`string tensorflow::Tensor::SummarizeValue(int64 max_entries) const`](#string_tensorflow_Tensor_SummarizeValue)\r\n  * Render the first `max_entries` values in `*this` into a string.\r\n* [`string tensorflow::Tensor::DebugString() const`](#string_tensorflow_Tensor_DebugString)\r\n  * A human-readable summary of the tensor suitable for debugging.\r\n* [`void tensorflow::Tensor::FillDescription(TensorDescription *description) const`](#void_tensorflow_Tensor_FillDescription)\r\n* [`StringPiece tensorflow::Tensor::tensor_data() const`](#StringPiece_tensorflow_Tensor_tensor_data)\r\n  * Returns a `StringPiece` mapping the current tensor&apos;s buffer.\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::Tensor::Tensor()` <a class=\"md-anchor\" id=\"tensorflow_Tensor_Tensor\"></a>\r\n\r\nDefault Tensor constructor. Creates a 1-dimension, 0-element float tensor.\r\n\r\n\r\n\r\n#### `tensorflow::Tensor::Tensor(DataType type, const TensorShape &shape)` <a class=\"md-anchor\" id=\"tensorflow_Tensor_Tensor\"></a>\r\n\r\nCreates a Tensor of the given `type` and `shape`.\r\n\r\nThe underlying buffer is allocated using a `CPUAllocator`.\r\n\r\n#### `tensorflow::Tensor::Tensor(Allocator *a, DataType type, const TensorShape &shape)` <a class=\"md-anchor\" id=\"tensorflow_Tensor_Tensor\"></a>\r\n\r\nCreates a tensor with the input `type` and `shape`, using the allocator `a` to allocate the underlying buffer.\r\n\r\n`a` must outlive the lifetime of this Tensor .\r\n\r\n#### `tensorflow::Tensor::Tensor(DataType type)` <a class=\"md-anchor\" id=\"tensorflow_Tensor_Tensor\"></a>\r\n\r\nCreates an uninitialized Tensor of the given data type.\r\n\r\n\r\n\r\n#### `tensorflow::Tensor::Tensor(const Tensor &other)` <a class=\"md-anchor\" id=\"tensorflow_Tensor_Tensor\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `tensorflow::Tensor::~Tensor()` <a class=\"md-anchor\" id=\"tensorflow_Tensor_Tensor\"></a>\r\n\r\nCopy constructor.\r\n\r\n\r\n\r\n#### `DataType tensorflow::Tensor::dtype() const` <a class=\"md-anchor\" id=\"DataType_tensorflow_Tensor_dtype\"></a>\r\n\r\nReturns the data type.\r\n\r\n\r\n\r\n#### `const TensorShape& tensorflow::Tensor::shape() const` <a class=\"md-anchor\" id=\"const_TensorShape_tensorflow_Tensor_shape\"></a>\r\n\r\nReturns the shape of the tensor.\r\n\r\n\r\n\r\n#### `int tensorflow::Tensor::dims() const` <a class=\"md-anchor\" id=\"int_tensorflow_Tensor_dims\"></a>\r\n\r\nConvenience accessor for the tensor shape.\r\n\r\nFor all shape accessors, see comments for relevant methods of ` TensorShape ` in ` tensor_shape.h `.\r\n\r\n#### `int64 tensorflow::Tensor::dim_size(int d) const` <a class=\"md-anchor\" id=\"int64_tensorflow_Tensor_dim_size\"></a>\r\n\r\nConvenience accessor for the tensor shape.\r\n\r\n\r\n\r\n#### `int64 tensorflow::Tensor::NumElements() const` <a class=\"md-anchor\" id=\"int64_tensorflow_Tensor_NumElements\"></a>\r\n\r\nConvenience accessor for the tensor shape.\r\n\r\n\r\n\r\n#### `bool tensorflow::Tensor::IsSameSize(const Tensor &b) const` <a class=\"md-anchor\" id=\"bool_tensorflow_Tensor_IsSameSize\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `bool tensorflow::Tensor::IsInitialized() const` <a class=\"md-anchor\" id=\"bool_tensorflow_Tensor_IsInitialized\"></a>\r\n\r\nHas this Tensor been initialized?\r\n\r\n\r\n\r\n#### `size_t tensorflow::Tensor::TotalBytes() const` <a class=\"md-anchor\" id=\"size_t_tensorflow_Tensor_TotalBytes\"></a>\r\n\r\nReturns the estimated memory usage of this tensor.\r\n\r\n\r\n\r\n#### `Tensor& tensorflow::Tensor::operator=(const Tensor &other)` <a class=\"md-anchor\" id=\"Tensor_tensorflow_Tensor_operator_\"></a>\r\n\r\nAssign operator. This tensor shares other&apos;s underlying storage.\r\n\r\n\r\n\r\n#### `bool tensorflow::Tensor::CopyFrom(const Tensor &other, const TensorShape &shape) TF_MUST_USE_RESULT` <a class=\"md-anchor\" id=\"bool_tensorflow_Tensor_CopyFrom\"></a>\r\n\r\nCopy the other tensor into this tensor and reshape it.\r\n\r\nThis tensor shares other&apos;s underlying storage. Returns `true` iff `other.shape()` has the same number of elements of the given `shape`.\r\n\r\n#### `Tensor tensorflow::Tensor::Slice(int64 dim0_start, int64 dim0_limit) const` <a class=\"md-anchor\" id=\"Tensor_tensorflow_Tensor_Slice\"></a>\r\n\r\nSlice this tensor along the 1st dimension.\r\n\r\nI.e., the returned tensor satisifies returned[i, ...] == this[dim0_start + i, ...]. The returned tensor shares the underlying tensor buffer with this tensor.\r\n\r\nNOTE: The returned tensor may not satisfies the same alignment requirement as this tensor depending on the shape. The caller must check the returned tensor&apos;s alignment before calling certain methods that have alignment requirement (e.g., ` flat() `, `tensor()`).\r\n\r\nREQUIRES: ` dims() ` >= 1 REQUIRES: `0 <= dim0_start <= dim0_limit <= dim_size(0)`\r\n\r\n#### `bool tensorflow::Tensor::FromProto(const TensorProto &other) TF_MUST_USE_RESULT` <a class=\"md-anchor\" id=\"bool_tensorflow_Tensor_FromProto\"></a>\r\n\r\nParse `other` and construct the tensor.\r\n\r\nReturns `true` iff the parsing succeeds. If the parsing fails, the state of `*this` is unchanged.\r\n\r\n#### `bool tensorflow::Tensor::FromProto(Allocator *a, const TensorProto &other) TF_MUST_USE_RESULT` <a class=\"md-anchor\" id=\"bool_tensorflow_Tensor_FromProto\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `void tensorflow::Tensor::AsProtoField(TensorProto *proto) const` <a class=\"md-anchor\" id=\"void_tensorflow_Tensor_AsProtoField\"></a>\r\n\r\nFills in `proto` with `*this` tensor&apos;s content.\r\n\r\n` AsProtoField() ` fills in the repeated field for `proto.dtype()`, while `AsProtoTensorContent()` encodes the content in `proto.tensor_content()` in a compact form.\r\n\r\n#### `void tensorflow::Tensor::AsProtoTensorContent(TensorProto *proto) const` <a class=\"md-anchor\" id=\"void_tensorflow_Tensor_AsProtoTensorContent\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes<T>::Vec tensorflow::Tensor::vec()` <a class=\"md-anchor\" id=\"TTypes_T_Vec_tensorflow_Tensor_vec\"></a>\r\n\r\nReturn the tensor data as an `Eigen::Tensor` with the type and sizes of this ` Tensor `.\r\n\r\nUse these methods when you know the data type and the number of dimensions of the Tensor and you want an `Eigen::Tensor` automatically sized to the ` Tensor ` sizes. The implementation check fails if either type or sizes mismatch.\r\n\r\nExample:\r\n\r\n```c++ typedef float T;\r\nTensor my_mat(...built with Shape{rows: 3, cols: 5}...);\r\nauto mat = my_mat.matrix<T>();    // 2D Eigen::Tensor, 3 x 5.\r\nauto mat = my_mat.tensor<T, 2>(); // 2D Eigen::Tensor, 3 x 5.\r\nauto vec = my_mat.vec<T>();       // CHECK fails as my_mat is 2D.\r\nauto vec = my_mat.tensor<T, 3>(); // CHECK fails as my_mat is 2D.\r\nauto mat = my_mat.matrix<int32>();// CHECK fails as type mismatch.\r\n\r\n```\r\n\r\n#### `TTypes<T>::Matrix tensorflow::Tensor::matrix()` <a class=\"md-anchor\" id=\"TTypes_T_Matrix_tensorflow_Tensor_matrix\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes< T, NDIMS >::Tensor tensorflow::Tensor::tensor()` <a class=\"md-anchor\" id=\"TTypes_T_NDIMS_Tensor_tensorflow_Tensor_tensor\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes<T>::Flat tensorflow::Tensor::flat()` <a class=\"md-anchor\" id=\"TTypes_T_Flat_tensorflow_Tensor_flat\"></a>\r\n\r\nReturn the tensor data as an `Eigen::Tensor` of the data type and a specified shape.\r\n\r\nThese methods allow you to access the data with the dimensions and sizes of your choice. You do not need to know the number of dimensions of the Tensor to call them. However, they `CHECK` that the type matches and the dimensions requested creates an `Eigen::Tensor` with the same number of elements as the tensor.\r\n\r\nExample:\r\n\r\n```c++ typedef float T;\r\nTensor my_ten(...built with Shape{planes: 4, rows: 3, cols: 5}...);\r\n// 1D Eigen::Tensor, size 60:\r\nauto flat = my_ten.flat<T>();\r\n// 2D Eigen::Tensor 12 x 5:\r\nauto inner = my_ten.flat_inner_dims<T>();\r\n// 2D Eigen::Tensor 4 x 15:\r\nauto outer = my_ten.shaped<T, 2>({4, 15});\r\n// CHECK fails, bad num elements:\r\nauto outer = my_ten.shaped<T, 2>({4, 8});\r\n// 3D Eigen::Tensor 6 x 5 x 2:\r\nauto weird = my_ten.shaped<T, 3>({6, 5, 2});\r\n// CHECK fails, type mismatch:\r\nauto bad   = my_ten.flat<int32>();\r\n\r\n```\r\n\r\n#### `TTypes<T>::UnalignedFlat tensorflow::Tensor::unaligned_flat()` <a class=\"md-anchor\" id=\"TTypes_T_UnalignedFlat_tensorflow_Tensor_unaligned_flat\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes<T>::Matrix tensorflow::Tensor::flat_inner_dims()` <a class=\"md-anchor\" id=\"TTypes_T_Matrix_tensorflow_Tensor_flat_inner_dims\"></a>\r\n\r\n\r\n\r\nReturns the data as an Eigen::Tensor with 2 dimensions, collapsing all Tensor dimensions but the last one into the first dimension of the result.\r\n\r\n#### `TTypes<T>::Matrix tensorflow::Tensor::flat_outer_dims()` <a class=\"md-anchor\" id=\"TTypes_T_Matrix_tensorflow_Tensor_flat_outer_dims\"></a>\r\n\r\n\r\n\r\nReturns the data as an Eigen::Tensor with 2 dimensions, collapsing all Tensor dimensions but the first one into the last dimension of the result.\r\n\r\n#### `TTypes< T, NDIMS >::Tensor tensorflow::Tensor::shaped(gtl::ArraySlice< int64 > new_sizes)` <a class=\"md-anchor\" id=\"TTypes_T_NDIMS_Tensor_tensorflow_Tensor_shaped\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes< T, NDIMS >::UnalignedTensor tensorflow::Tensor::unaligned_shaped(gtl::ArraySlice< int64 > new_sizes)` <a class=\"md-anchor\" id=\"TTypes_T_NDIMS_UnalignedTensor_tensorflow_Tensor_unaligned_shaped\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes< T >::Scalar tensorflow::Tensor::scalar()` <a class=\"md-anchor\" id=\"TTypes_T_Scalar_tensorflow_Tensor_scalar\"></a>\r\n\r\nReturn the Tensor data as a `TensorMap` of fixed size 1: `TensorMap<TensorFixedSize<T, 1>>`.\r\n\r\nUsing ` scalar() ` allows the compiler to perform optimizations as the size of the tensor is known at compile time.\r\n\r\n#### `TTypes<T>::ConstVec tensorflow::Tensor::vec() const` <a class=\"md-anchor\" id=\"TTypes_T_ConstVec_tensorflow_Tensor_vec\"></a>\r\n\r\nConst versions of all the methods above.\r\n\r\n\r\n\r\n#### `TTypes<T>::ConstMatrix tensorflow::Tensor::matrix() const` <a class=\"md-anchor\" id=\"TTypes_T_ConstMatrix_tensorflow_Tensor_matrix\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes< T, NDIMS >::ConstTensor tensorflow::Tensor::tensor() const` <a class=\"md-anchor\" id=\"TTypes_T_NDIMS_ConstTensor_tensorflow_Tensor_tensor\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes<T>::ConstFlat tensorflow::Tensor::flat() const` <a class=\"md-anchor\" id=\"TTypes_T_ConstFlat_tensorflow_Tensor_flat\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes<T>::UnalignedConstFlat tensorflow::Tensor::unaligned_flat() const` <a class=\"md-anchor\" id=\"TTypes_T_UnalignedConstFlat_tensorflow_Tensor_unaligned_flat\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes<T>::ConstMatrix tensorflow::Tensor::flat_inner_dims() const` <a class=\"md-anchor\" id=\"TTypes_T_ConstMatrix_tensorflow_Tensor_flat_inner_dims\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes<T>::ConstMatrix tensorflow::Tensor::flat_outer_dims() const` <a class=\"md-anchor\" id=\"TTypes_T_ConstMatrix_tensorflow_Tensor_flat_outer_dims\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes< T, NDIMS >::ConstTensor tensorflow::Tensor::shaped(gtl::ArraySlice< int64 > new_sizes) const` <a class=\"md-anchor\" id=\"TTypes_T_NDIMS_ConstTensor_tensorflow_Tensor_shaped\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes< T, NDIMS >::UnalignedConstTensor tensorflow::Tensor::unaligned_shaped(gtl::ArraySlice< int64 > new_sizes) const` <a class=\"md-anchor\" id=\"TTypes_T_NDIMS_UnalignedConstTensor_tensorflow_Tensor_unaligned_shaped\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TTypes< T >::ConstScalar tensorflow::Tensor::scalar() const` <a class=\"md-anchor\" id=\"TTypes_T_ConstScalar_tensorflow_Tensor_scalar\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `string tensorflow::Tensor::SummarizeValue(int64 max_entries) const` <a class=\"md-anchor\" id=\"string_tensorflow_Tensor_SummarizeValue\"></a>\r\n\r\nRender the first `max_entries` values in `*this` into a string.\r\n\r\n\r\n\r\n#### `string tensorflow::Tensor::DebugString() const` <a class=\"md-anchor\" id=\"string_tensorflow_Tensor_DebugString\"></a>\r\n\r\nA human-readable summary of the tensor suitable for debugging.\r\n\r\n\r\n\r\n#### `void tensorflow::Tensor::FillDescription(TensorDescription *description) const` <a class=\"md-anchor\" id=\"void_tensorflow_Tensor_FillDescription\"></a>\r\n\r\n\r\n\r\nFill in the `TensorDescription` proto with metadata about the tensor that is useful for monitoring and debugging.\r\n\r\n#### `StringPiece tensorflow::Tensor::tensor_data() const` <a class=\"md-anchor\" id=\"StringPiece_tensorflow_Tensor_tensor_data\"></a>\r\n\r\nReturns a `StringPiece` mapping the current tensor&apos;s buffer.\r\n\r\nThe returned `StringPiece` may point to memory location on devices that the CPU cannot address directly.\r\n\r\nNOTE: The underlying tensor buffer is refcounted, so the lifetime of the contents mapped by the `StringPiece` matches the lifetime of the buffer; callers should arrange to make sure the buffer does not get destroyed while the `StringPiece` is still used.\r\n\r\nREQUIRES: `DataTypeCanUseMemcpy( dtype() )`.\r\n"
  },
  {
    "path": "tex_pdf/api/cc/ClassTensorBuffer.md",
    "content": "# Class `tensorflow::TensorBuffer` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--tensorbuffer-\"></a>\r\n\r\n\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::TensorBuffer::~TensorBuffer() override`](#tensorflow_TensorBuffer_TensorBuffer)\r\n* [`virtual void* tensorflow::TensorBuffer::data() const =0`](#virtual_void_tensorflow_TensorBuffer_data)\r\n* [`virtual size_t tensorflow::TensorBuffer::size() const =0`](#virtual_size_t_tensorflow_TensorBuffer_size)\r\n* [`virtual TensorBuffer* tensorflow::TensorBuffer::root_buffer()=0`](#virtual_TensorBuffer_tensorflow_TensorBuffer_root_buffer)\r\n* [`virtual void tensorflow::TensorBuffer::FillAllocationDescription(AllocationDescription *proto) const =0`](#virtual_void_tensorflow_TensorBuffer_FillAllocationDescription)\r\n* [`T* tensorflow::TensorBuffer::base() const`](#T_tensorflow_TensorBuffer_base)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::TensorBuffer::~TensorBuffer() override` <a class=\"md-anchor\" id=\"tensorflow_TensorBuffer_TensorBuffer\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual void* tensorflow::TensorBuffer::data() const =0` <a class=\"md-anchor\" id=\"virtual_void_tensorflow_TensorBuffer_data\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual size_t tensorflow::TensorBuffer::size() const =0` <a class=\"md-anchor\" id=\"virtual_size_t_tensorflow_TensorBuffer_size\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual TensorBuffer* tensorflow::TensorBuffer::root_buffer()=0` <a class=\"md-anchor\" id=\"virtual_TensorBuffer_tensorflow_TensorBuffer_root_buffer\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual void tensorflow::TensorBuffer::FillAllocationDescription(AllocationDescription *proto) const =0` <a class=\"md-anchor\" id=\"virtual_void_tensorflow_TensorBuffer_FillAllocationDescription\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `T* tensorflow::TensorBuffer::base() const` <a class=\"md-anchor\" id=\"T_tensorflow_TensorBuffer_base\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/cc/ClassTensorShape.md",
    "content": "# Class `tensorflow::TensorShape` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--tensorshape-\"></a>\r\n\r\nManages the dimensions of a Tensor and their sizes.\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::TensorShape::TensorShape(gtl::ArraySlice< int64 > dim_sizes)`](#tensorflow_TensorShape_TensorShape)\r\n  * Construct a ` TensorShape ` from the provided sizes. REQUIRES: `dim_sizes[i] >= 0`\r\n* [`tensorflow::TensorShape::TensorShape(std::initializer_list< int64 > dim_sizes)`](#tensorflow_TensorShape_TensorShape)\r\n* [`tensorflow::TensorShape::TensorShape(const TensorShapeProto &proto)`](#tensorflow_TensorShape_TensorShape)\r\n  * REQUIRES: `IsValid(proto)`\r\n* [`tensorflow::TensorShape::TensorShape()`](#tensorflow_TensorShape_TensorShape)\r\n* [`void tensorflow::TensorShape::Clear()`](#void_tensorflow_TensorShape_Clear)\r\n  * Clear a tensor shape.\r\n* [`void tensorflow::TensorShape::AddDim(int64 size)`](#void_tensorflow_TensorShape_AddDim)\r\n  * Add a dimension to the end (\"inner-most\"). REQUIRES: `size >= 0`\r\n* [`void tensorflow::TensorShape::AppendShape(const TensorShape &shape)`](#void_tensorflow_TensorShape_AppendShape)\r\n  * Appends all the dimensions from `shape`.\r\n* [`void tensorflow::TensorShape::InsertDim(int d, int64 size)`](#void_tensorflow_TensorShape_InsertDim)\r\n  * Insert a dimension somewhere in the ` TensorShape `. REQUIRES: `0 <= d <= dims() ` REQUIRES: `size >= 0`\r\n* [`void tensorflow::TensorShape::set_dim(int d, int64 size)`](#void_tensorflow_TensorShape_set_dim)\r\n  * Modifies the size of the dimension `d` to be `size` REQUIRES: `0 <= d < dims() ` REQUIRES: `size >= 0`\r\n* [`void tensorflow::TensorShape::RemoveDim(int d)`](#void_tensorflow_TensorShape_RemoveDim)\r\n  * Removes dimension `d` from the ` TensorShape `. REQUIRES: `0 <= d < dims() `\r\n* [`int tensorflow::TensorShape::dims() const`](#int_tensorflow_TensorShape_dims)\r\n  * Return the number of dimensions in the tensor.\r\n* [`int64 tensorflow::TensorShape::dim_size(int d) const`](#int64_tensorflow_TensorShape_dim_size)\r\n  * Returns the number of elements in dimension `d`. REQUIRES: `0 <= d < dims() `\r\n* [`gtl::ArraySlice<int64> tensorflow::TensorShape::dim_sizes() const`](#gtl_ArraySlice_int64_tensorflow_TensorShape_dim_sizes)\r\n  * Returns sizes of all dimensions.\r\n* [`int64 tensorflow::TensorShape::num_elements() const`](#int64_tensorflow_TensorShape_num_elements)\r\n  * Returns the number of elements in the tensor.\r\n* [`bool tensorflow::TensorShape::IsSameSize(const TensorShape &b) const`](#bool_tensorflow_TensorShape_IsSameSize)\r\n* [`bool tensorflow::TensorShape::operator==(const TensorShape &b) const`](#bool_tensorflow_TensorShape_operator_)\r\n* [`void tensorflow::TensorShape::AsProto(TensorShapeProto *proto) const`](#void_tensorflow_TensorShape_AsProto)\r\n  * Fill `*proto` from `*this`.\r\n* [`Eigen::DSizes< Eigen::DenseIndex, NDIMS > tensorflow::TensorShape::AsEigenDSizes() const`](#Eigen_DSizes_Eigen_DenseIndex_NDIMS_tensorflow_TensorShape_AsEigenDSizes)\r\n  * Fill `*dsizes` from `*this`.\r\n* [`Eigen::DSizes< Eigen::DenseIndex, NDIMS > tensorflow::TensorShape::AsEigenDSizesWithPadding() const`](#Eigen_DSizes_Eigen_DenseIndex_NDIMS_tensorflow_TensorShape_AsEigenDSizesWithPadding)\r\n* [`TensorShapeIter tensorflow::TensorShape::begin() const`](#TensorShapeIter_tensorflow_TensorShape_begin)\r\n  * For iterating through the dimensions.\r\n* [`TensorShapeIter tensorflow::TensorShape::end() const`](#TensorShapeIter_tensorflow_TensorShape_end)\r\n* [`string tensorflow::TensorShape::DebugString() const`](#string_tensorflow_TensorShape_DebugString)\r\n  * For error messages.\r\n* [`string tensorflow::TensorShape::ShortDebugString() const`](#string_tensorflow_TensorShape_ShortDebugString)\r\n* [`static bool tensorflow::TensorShape::IsValid(const TensorShapeProto &proto)`](#static_bool_tensorflow_TensorShape_IsValid)\r\n  * Returns `true` iff `proto` is a valid tensor shape.\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::TensorShape::TensorShape(gtl::ArraySlice< int64 > dim_sizes)` <a class=\"md-anchor\" id=\"tensorflow_TensorShape_TensorShape\"></a>\r\n\r\nConstruct a ` TensorShape ` from the provided sizes. REQUIRES: `dim_sizes[i] >= 0`\r\n\r\n\r\n\r\n#### `tensorflow::TensorShape::TensorShape(std::initializer_list< int64 > dim_sizes)` <a class=\"md-anchor\" id=\"tensorflow_TensorShape_TensorShape\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `tensorflow::TensorShape::TensorShape(const TensorShapeProto &proto)` <a class=\"md-anchor\" id=\"tensorflow_TensorShape_TensorShape\"></a>\r\n\r\nREQUIRES: `IsValid(proto)`\r\n\r\n\r\n\r\n#### `tensorflow::TensorShape::TensorShape()` <a class=\"md-anchor\" id=\"tensorflow_TensorShape_TensorShape\"></a>\r\n\r\n\r\n\r\nCreate a tensor shape with no dimensions and one element, which you can then call ` AddDim() ` on.\r\n\r\n#### `void tensorflow::TensorShape::Clear()` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShape_Clear\"></a>\r\n\r\nClear a tensor shape.\r\n\r\n\r\n\r\n#### `void tensorflow::TensorShape::AddDim(int64 size)` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShape_AddDim\"></a>\r\n\r\nAdd a dimension to the end (\"inner-most\"). REQUIRES: `size >= 0`\r\n\r\n\r\n\r\n#### `void tensorflow::TensorShape::AppendShape(const TensorShape &shape)` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShape_AppendShape\"></a>\r\n\r\nAppends all the dimensions from `shape`.\r\n\r\n\r\n\r\n#### `void tensorflow::TensorShape::InsertDim(int d, int64 size)` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShape_InsertDim\"></a>\r\n\r\nInsert a dimension somewhere in the ` TensorShape `. REQUIRES: `0 <= d <= dims() ` REQUIRES: `size >= 0`\r\n\r\n\r\n\r\n#### `void tensorflow::TensorShape::set_dim(int d, int64 size)` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShape_set_dim\"></a>\r\n\r\nModifies the size of the dimension `d` to be `size` REQUIRES: `0 <= d < dims() ` REQUIRES: `size >= 0`\r\n\r\n\r\n\r\n#### `void tensorflow::TensorShape::RemoveDim(int d)` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShape_RemoveDim\"></a>\r\n\r\nRemoves dimension `d` from the ` TensorShape `. REQUIRES: `0 <= d < dims() `\r\n\r\n\r\n\r\n#### `int tensorflow::TensorShape::dims() const` <a class=\"md-anchor\" id=\"int_tensorflow_TensorShape_dims\"></a>\r\n\r\nReturn the number of dimensions in the tensor.\r\n\r\n\r\n\r\n#### `int64 tensorflow::TensorShape::dim_size(int d) const` <a class=\"md-anchor\" id=\"int64_tensorflow_TensorShape_dim_size\"></a>\r\n\r\nReturns the number of elements in dimension `d`. REQUIRES: `0 <= d < dims() `\r\n\r\n\r\n\r\n#### `gtl::ArraySlice<int64> tensorflow::TensorShape::dim_sizes() const` <a class=\"md-anchor\" id=\"gtl_ArraySlice_int64_tensorflow_TensorShape_dim_sizes\"></a>\r\n\r\nReturns sizes of all dimensions.\r\n\r\n\r\n\r\n#### `int64 tensorflow::TensorShape::num_elements() const` <a class=\"md-anchor\" id=\"int64_tensorflow_TensorShape_num_elements\"></a>\r\n\r\nReturns the number of elements in the tensor.\r\n\r\nWe use `int64` and not `size_t` to be compatible with `Eigen::Tensor` which uses `ptrdiff_t`.\r\n\r\n#### `bool tensorflow::TensorShape::IsSameSize(const TensorShape &b) const` <a class=\"md-anchor\" id=\"bool_tensorflow_TensorShape_IsSameSize\"></a>\r\n\r\n\r\n\r\nReturns true if `*this` and `b` have the same sizes. Ignores dimension names.\r\n\r\n#### `bool tensorflow::TensorShape::operator==(const TensorShape &b) const` <a class=\"md-anchor\" id=\"bool_tensorflow_TensorShape_operator_\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `void tensorflow::TensorShape::AsProto(TensorShapeProto *proto) const` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShape_AsProto\"></a>\r\n\r\nFill `*proto` from `*this`.\r\n\r\n\r\n\r\n#### `Eigen::DSizes< Eigen::DenseIndex, NDIMS > tensorflow::TensorShape::AsEigenDSizes() const` <a class=\"md-anchor\" id=\"Eigen_DSizes_Eigen_DenseIndex_NDIMS_tensorflow_TensorShape_AsEigenDSizes\"></a>\r\n\r\nFill `*dsizes` from `*this`.\r\n\r\n\r\n\r\n#### `Eigen::DSizes< Eigen::DenseIndex, NDIMS > tensorflow::TensorShape::AsEigenDSizesWithPadding() const` <a class=\"md-anchor\" id=\"Eigen_DSizes_Eigen_DenseIndex_NDIMS_tensorflow_TensorShape_AsEigenDSizesWithPadding\"></a>\r\n\r\n\r\n\r\nSame as ` AsEigenDSizes() ` but allows for `NDIMS > dims() ` in which case we pad the rest of the sizes with 1.\r\n\r\n#### `TensorShapeIter tensorflow::TensorShape::begin() const` <a class=\"md-anchor\" id=\"TensorShapeIter_tensorflow_TensorShape_begin\"></a>\r\n\r\nFor iterating through the dimensions.\r\n\r\n\r\n\r\n#### `TensorShapeIter tensorflow::TensorShape::end() const` <a class=\"md-anchor\" id=\"TensorShapeIter_tensorflow_TensorShape_end\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `string tensorflow::TensorShape::DebugString() const` <a class=\"md-anchor\" id=\"string_tensorflow_TensorShape_DebugString\"></a>\r\n\r\nFor error messages.\r\n\r\n\r\n\r\n#### `string tensorflow::TensorShape::ShortDebugString() const` <a class=\"md-anchor\" id=\"string_tensorflow_TensorShape_ShortDebugString\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShape::IsValid(const TensorShapeProto &proto)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShape_IsValid\"></a>\r\n\r\nReturns `true` iff `proto` is a valid tensor shape.\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/cc/ClassTensorShapeIter.md",
    "content": "# Class `tensorflow::TensorShapeIter` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--tensorshapeiter-\"></a>\r\n\r\n\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::TensorShapeIter::TensorShapeIter(const TensorShape *shape, int d)`](#tensorflow_TensorShapeIter_TensorShapeIter)\r\n* [`bool tensorflow::TensorShapeIter::operator==(const TensorShapeIter &rhs)`](#bool_tensorflow_TensorShapeIter_operator_)\r\n* [`bool tensorflow::TensorShapeIter::operator!=(const TensorShapeIter &rhs)`](#bool_tensorflow_TensorShapeIter_operator_)\r\n* [`void tensorflow::TensorShapeIter::operator++()`](#void_tensorflow_TensorShapeIter_operator_)\r\n* [`TensorShapeDim tensorflow::TensorShapeIter::operator*()`](#TensorShapeDim_tensorflow_TensorShapeIter_operator_)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::TensorShapeIter::TensorShapeIter(const TensorShape *shape, int d)` <a class=\"md-anchor\" id=\"tensorflow_TensorShapeIter_TensorShapeIter\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `bool tensorflow::TensorShapeIter::operator==(const TensorShapeIter &rhs)` <a class=\"md-anchor\" id=\"bool_tensorflow_TensorShapeIter_operator_\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `bool tensorflow::TensorShapeIter::operator!=(const TensorShapeIter &rhs)` <a class=\"md-anchor\" id=\"bool_tensorflow_TensorShapeIter_operator_\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `void tensorflow::TensorShapeIter::operator++()` <a class=\"md-anchor\" id=\"void_tensorflow_TensorShapeIter_operator_\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `TensorShapeDim tensorflow::TensorShapeIter::operator*()` <a class=\"md-anchor\" id=\"TensorShapeDim_tensorflow_TensorShapeIter_operator_\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/cc/ClassTensorShapeUtils.md",
    "content": "# Class `tensorflow::TensorShapeUtils` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--tensorshapeutils-\"></a>\r\n\r\nStatic helper routines for ` TensorShape `. Includes a few common predicates on a tensor shape.\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`static bool tensorflow::TensorShapeUtils::IsScalar(const TensorShape &shape)`](#static_bool_tensorflow_TensorShapeUtils_IsScalar)\r\n* [`static bool tensorflow::TensorShapeUtils::IsVector(const TensorShape &shape)`](#static_bool_tensorflow_TensorShapeUtils_IsVector)\r\n* [`static bool tensorflow::TensorShapeUtils::IsLegacyScalar(const TensorShape &shape)`](#static_bool_tensorflow_TensorShapeUtils_IsLegacyScalar)\r\n* [`static bool tensorflow::TensorShapeUtils::IsLegacyVector(const TensorShape &shape)`](#static_bool_tensorflow_TensorShapeUtils_IsLegacyVector)\r\n* [`static bool tensorflow::TensorShapeUtils::IsVectorOrHigher(const TensorShape &shape)`](#static_bool_tensorflow_TensorShapeUtils_IsVectorOrHigher)\r\n* [`static bool tensorflow::TensorShapeUtils::IsMatrix(const TensorShape &shape)`](#static_bool_tensorflow_TensorShapeUtils_IsMatrix)\r\n* [`static bool tensorflow::TensorShapeUtils::IsMatrixOrHigher(const TensorShape &shape)`](#static_bool_tensorflow_TensorShapeUtils_IsMatrixOrHigher)\r\n* [`static TensorShape tensorflow::TensorShapeUtils::MakeShape(const T *dims, int n)`](#static_TensorShape_tensorflow_TensorShapeUtils_MakeShape)\r\n  * Returns a ` TensorShape ` whose dimensions are `dims[0]`, `dims[1]`, ..., `dims[n-1]`.\r\n* [`static string tensorflow::TensorShapeUtils::ShapeListString(const gtl::ArraySlice< TensorShape > &shapes)`](#static_string_tensorflow_TensorShapeUtils_ShapeListString)\r\n* [`static bool tensorflow::TensorShapeUtils::StartsWith(const TensorShape &shape0, const TensorShape &shape1)`](#static_bool_tensorflow_TensorShapeUtils_StartsWith)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::IsScalar(const TensorShape &shape)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_IsScalar\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::IsVector(const TensorShape &shape)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_IsVector\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::IsLegacyScalar(const TensorShape &shape)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_IsLegacyScalar\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::IsLegacyVector(const TensorShape &shape)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_IsLegacyVector\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::IsVectorOrHigher(const TensorShape &shape)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_IsVectorOrHigher\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::IsMatrix(const TensorShape &shape)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_IsMatrix\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::IsMatrixOrHigher(const TensorShape &shape)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_IsMatrixOrHigher\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static TensorShape tensorflow::TensorShapeUtils::MakeShape(const T *dims, int n)` <a class=\"md-anchor\" id=\"static_TensorShape_tensorflow_TensorShapeUtils_MakeShape\"></a>\r\n\r\nReturns a ` TensorShape ` whose dimensions are `dims[0]`, `dims[1]`, ..., `dims[n-1]`.\r\n\r\n\r\n\r\n#### `static string tensorflow::TensorShapeUtils::ShapeListString(const gtl::ArraySlice< TensorShape > &shapes)` <a class=\"md-anchor\" id=\"static_string_tensorflow_TensorShapeUtils_ShapeListString\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `static bool tensorflow::TensorShapeUtils::StartsWith(const TensorShape &shape0, const TensorShape &shape1)` <a class=\"md-anchor\" id=\"static_bool_tensorflow_TensorShapeUtils_StartsWith\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/cc/ClassThread.md",
    "content": "# Class `tensorflow::Thread` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--thread-\"></a>\r\n\r\n\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::Thread::Thread()`](#tensorflow_Thread_Thread)\r\n* [`virtual tensorflow::Thread::~Thread()`](#virtual_tensorflow_Thread_Thread)\r\n  * Blocks until the thread of control stops running.\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::Thread::Thread()` <a class=\"md-anchor\" id=\"tensorflow_Thread_Thread\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual tensorflow::Thread::~Thread()` <a class=\"md-anchor\" id=\"virtual_tensorflow_Thread_Thread\"></a>\r\n\r\nBlocks until the thread of control stops running.\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/cc/ClassWritableFile.md",
    "content": "# Class `tensorflow::WritableFile` <a class=\"md-anchor\" id=\"AUTOGENERATED-class--tensorflow--writablefile-\"></a>\r\n\r\nA file abstraction for sequential writing.\r\n\r\nThe implementation must provide buffering since callers may append small fragments at a time to the file.\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::WritableFile::WritableFile()`](#tensorflow_WritableFile_WritableFile)\r\n* [`virtual tensorflow::WritableFile::~WritableFile()`](#virtual_tensorflow_WritableFile_WritableFile)\r\n* [`virtual Status tensorflow::WritableFile::Append(const StringPiece &data)=0`](#virtual_Status_tensorflow_WritableFile_Append)\r\n* [`virtual Status tensorflow::WritableFile::Close()=0`](#virtual_Status_tensorflow_WritableFile_Close)\r\n* [`virtual Status tensorflow::WritableFile::Flush()=0`](#virtual_Status_tensorflow_WritableFile_Flush)\r\n* [`virtual Status tensorflow::WritableFile::Sync()=0`](#virtual_Status_tensorflow_WritableFile_Sync)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::WritableFile::WritableFile()` <a class=\"md-anchor\" id=\"tensorflow_WritableFile_WritableFile\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual tensorflow::WritableFile::~WritableFile()` <a class=\"md-anchor\" id=\"virtual_tensorflow_WritableFile_WritableFile\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::WritableFile::Append(const StringPiece &data)=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_WritableFile_Append\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::WritableFile::Close()=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_WritableFile_Close\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::WritableFile::Flush()=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_WritableFile_Flush\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `virtual Status tensorflow::WritableFile::Sync()=0` <a class=\"md-anchor\" id=\"virtual_Status_tensorflow_WritableFile_Sync\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/cc/StructSessionOptions.md",
    "content": "# Struct `tensorflow::SessionOptions` <a class=\"md-anchor\" id=\"AUTOGENERATED-struct--tensorflow--sessionoptions-\"></a>\r\n\r\nConfiguration information for a Session .\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`Env* tensorflow::SessionOptions::env`](#Env_tensorflow_SessionOptions_env)\r\n  * The environment to use.\r\n* [`string tensorflow::SessionOptions::target`](#string_tensorflow_SessionOptions_target)\r\n  * The TensorFlow runtime to connect to.\r\n* [`ConfigProto tensorflow::SessionOptions::config`](#ConfigProto_tensorflow_SessionOptions_config)\r\n  * Configuration options.\r\n* [`tensorflow::SessionOptions::SessionOptions()`](#tensorflow_SessionOptions_SessionOptions)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `Env* tensorflow::SessionOptions::env` <a class=\"md-anchor\" id=\"Env_tensorflow_SessionOptions_env\"></a>\r\n\r\nThe environment to use.\r\n\r\n\r\n\r\n#### `string tensorflow::SessionOptions::target` <a class=\"md-anchor\" id=\"string_tensorflow_SessionOptions_target\"></a>\r\n\r\nThe TensorFlow runtime to connect to.\r\n\r\nIf &apos;target&apos; is empty or unspecified, the local TensorFlow runtime implementation will be used. Otherwise, the TensorFlow engine defined by &apos;target&apos; will be used to perform all computations.\r\n\r\n\"target\" can be either a single entry or a comma separated list of entries. Each entry is a resolvable address of the following format: local ip:port host:port ... other system-specific formats to identify tasks and jobs ...\r\n\r\nNOTE: at the moment &apos;local&apos; maps to an in-process service-based runtime.\r\n\r\nUpon creation, a single session affines itself to one of the remote processes, with possible load balancing choices when the \"target\" resolves to a list of possible processes.\r\n\r\nIf the session disconnects from the remote process during its lifetime, session calls may fail immediately.\r\n\r\n#### `ConfigProto tensorflow::SessionOptions::config` <a class=\"md-anchor\" id=\"ConfigProto_tensorflow_SessionOptions_config\"></a>\r\n\r\nConfiguration options.\r\n\r\n\r\n\r\n#### `tensorflow::SessionOptions::SessionOptions()` <a class=\"md-anchor\" id=\"tensorflow_SessionOptions_SessionOptions\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/cc/StructState.md",
    "content": "# Struct `tensorflow::Status::State` <a class=\"md-anchor\" id=\"AUTOGENERATED-struct--tensorflow--status--state-\"></a>\r\n\r\n\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`tensorflow::error::Code tensorflow::Status::State::code`](#tensorflow_error_Code_tensorflow_Status_State_code)\r\n* [`string tensorflow::Status::State::msg`](#string_tensorflow_Status_State_msg)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `tensorflow::error::Code tensorflow::Status::State::code` <a class=\"md-anchor\" id=\"tensorflow_error_Code_tensorflow_Status_State_code\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `string tensorflow::Status::State::msg` <a class=\"md-anchor\" id=\"string_tensorflow_Status_State_msg\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/cc/StructTensorShapeDim.md",
    "content": "# Struct `tensorflow::TensorShapeDim` <a class=\"md-anchor\" id=\"AUTOGENERATED-struct--tensorflow--tensorshapedim-\"></a>\r\n\r\n\r\n\r\n\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`int tensorflow::TensorShapeDim::size`](#int_tensorflow_TensorShapeDim_size)\r\n* [`tensorflow::TensorShapeDim::TensorShapeDim(int64 s)`](#tensorflow_TensorShapeDim_TensorShapeDim)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `int tensorflow::TensorShapeDim::size` <a class=\"md-anchor\" id=\"int_tensorflow_TensorShapeDim_size\"></a>\r\n\r\n\r\n\r\n\r\n\r\n#### `tensorflow::TensorShapeDim::TensorShapeDim(int64 s)` <a class=\"md-anchor\" id=\"tensorflow_TensorShapeDim_TensorShapeDim\"></a>\r\n\r\n\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/cc/StructThreadOptions.md",
    "content": "# Struct `tensorflow::ThreadOptions` <a class=\"md-anchor\" id=\"AUTOGENERATED-struct--tensorflow--threadoptions-\"></a>\r\n\r\nOptions to configure a Thread .\r\n\r\nNote that the options are all hints, and the underlying implementation may choose to ignore it.\r\n\r\n##Member Summary <a class=\"md-anchor\" id=\"AUTOGENERATED-member-summary\"></a>\r\n\r\n* [`size_t tensorflow::ThreadOptions::stack_size`](#size_t_tensorflow_ThreadOptions_stack_size)\r\n  * Thread stack size to use (in bytes).\r\n* [`size_t tensorflow::ThreadOptions::guard_size`](#size_t_tensorflow_ThreadOptions_guard_size)\r\n  * Guard area size to use near thread stacks to use (in bytes)\r\n\r\n##Member Details <a class=\"md-anchor\" id=\"AUTOGENERATED-member-details\"></a>\r\n\r\n#### `size_t tensorflow::ThreadOptions::stack_size` <a class=\"md-anchor\" id=\"size_t_tensorflow_ThreadOptions_stack_size\"></a>\r\n\r\nThread stack size to use (in bytes).\r\n\r\n\r\n\r\n#### `size_t tensorflow::ThreadOptions::guard_size` <a class=\"md-anchor\" id=\"size_t_tensorflow_ThreadOptions_guard_size\"></a>\r\n\r\nGuard area size to use near thread stacks to use (in bytes)\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/cc/index.md",
    "content": "# TensorFlow C++ Session API reference documentation <a class=\"md-anchor\" id=\"AUTOGENERATED-tensorflow-c---session-api-reference-documentation\"></a>\r\n\r\nTensorFlow's public C++ API includes only the API for executing graphs, as of\r\nversion 0.5. To control the execution of a graph from C++:\r\n\r\n1. Build the computation graph using the [Python API](../../api_docs/python/).\r\n1. Use [tf.train.write_graph()](../../api_docs/python/train.md#write_graph) to\r\nwrite the graph to a file.\r\n1. Load the graph using the C++ Session API. For example:\r\n\r\n  ```c++\r\n  // Reads a model graph definition from disk, and creates a session object you\r\n  // can use to run it.\r\n  Status LoadGraph(string graph_file_name, Session** session) {\r\n    GraphDef graph_def;\r\n    TF_RETURN_IF_ERROR(\r\n        ReadBinaryProto(Env::Default(), graph_file_name, &graph_def));\r\n    TF_RETURN_IF_ERROR(NewSession(SessionOptions(), session));\r\n    TF_RETURN_IF_ERROR((*session)->Create(graph_def));\r\n    return Status::OK();\r\n  }\r\n```\r\n\r\n1. Run the graph with a call to `session->Run()`\r\n\r\n\r\n##Classes <a class=\"md-anchor\" id=\"AUTOGENERATED-classes\"></a>\r\n\r\n* [tensorflow::Env](../../api_docs/cc/ClassEnv.md)\r\n* [tensorflow::EnvWrapper](../../api_docs/cc/ClassEnvWrapper.md)\r\n* [tensorflow::RandomAccessFile](../../api_docs/cc/ClassRandomAccessFile.md)\r\n* [tensorflow::Session](../../api_docs/cc/ClassSession.md)\r\n* [tensorflow::Status](../../api_docs/cc/ClassStatus.md)\r\n* [tensorflow::Tensor](../../api_docs/cc/ClassTensor.md)\r\n* [tensorflow::TensorBuffer](../../api_docs/cc/ClassTensorBuffer.md)\r\n* [tensorflow::TensorShape](../../api_docs/cc/ClassTensorShape.md)\r\n* [tensorflow::TensorShapeIter](../../api_docs/cc/ClassTensorShapeIter.md)\r\n* [tensorflow::TensorShapeUtils](../../api_docs/cc/ClassTensorShapeUtils.md)\r\n* [tensorflow::Thread](../../api_docs/cc/ClassThread.md)\r\n* [tensorflow::WritableFile](../../api_docs/cc/ClassWritableFile.md)\r\n\r\n##Structs <a class=\"md-anchor\" id=\"AUTOGENERATED-structs\"></a>\r\n\r\n* [tensorflow::SessionOptions](../../api_docs/cc/StructSessionOptions.md)\r\n* [tensorflow::Status::State](../../api_docs/cc/StructState.md)\r\n* [tensorflow::TensorShapeDim](../../api_docs/cc/StructTensorShapeDim.md)\r\n* [tensorflow::ThreadOptions](../../api_docs/cc/StructThreadOptions.md)\r\n\r\n\r\n<div class='sections-order' style=\"display: none;\">\r\n<!--\r\n<!-- ClassEnv.md -->\r\n<!-- ClassEnvWrapper.md -->\r\n<!-- ClassRandomAccessFile.md -->\r\n<!-- ClassSession.md -->\r\n<!-- ClassStatus.md -->\r\n<!-- ClassTensor.md -->\r\n<!-- ClassTensorBuffer.md -->\r\n<!-- ClassTensorShape.md -->\r\n<!-- ClassTensorShapeIter.md -->\r\n<!-- ClassTensorShapeUtils.md -->\r\n<!-- ClassThread.md -->\r\n<!-- ClassWritableFile.md -->\r\n<!-- StructSessionOptions.md -->\r\n<!-- StructState.md -->\r\n<!-- StructTensorShapeDim.md -->\r\n<!-- StructThreadOptions.md -->\r\n-->\r\n</div>\r\n"
  },
  {
    "path": "tex_pdf/api/python/c4s01_framework.tex",
    "content": "\r\n\r\n\r\n\\section{Building Graphs} \\label{building-graphs}\r\n\r\n\\subsection{Contents}\\label{contents}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-building-graphs}{Building\r\nGraphs}}{Building Graphs}}\\label{building-graphs-1}\r\n\r\n\\begin{itemize}\r\n   \\setlength{\\parsep}{-5pt}\r\n   \\setlength{\\itemsep}{-5pt}\r\n\\item \\hyperref[core-graph-data-structures]{Core graph data structures}\r\n\\item \\hyperref[class-tf.graph]{class \\lstinline{tf.Graph}}\r\n\\item \\hyperref[class-tf.operation]{class \\lstinline{tf.Operation}}\r\n\\item \\protect\\hyperlink{Tensor}{class \\lstinline{tf.Tensor}}\r\n\\item \\protect\\hyperlink{AUTOGENERATED-tensor-types}{Tensor types}\r\n\\item \\protect\\hyperlink{DType}{\\lstinline{class tf.DType}}\r\n\\item \\protect\\hyperlink{asux5fdtype}{\\lstinline{tf.as_dtype(type_value)}}\r\n\\item \\protect\\hyperlink{AUTOGENERATED-utility-functions}{Utility functions}\r\n\\item \\protect\\hyperlink{device}{\\lstinline{tf.device(dev)}}\r\n\\item \\protect\\hyperlink{nameux5fscope}{\\lstinline{tf.name_scope(name)}}\r\n\\item \\protect\\hyperlink{controlux5fdependencies}{\\lstinline{tf.control_dependencies(control_inputs)}}\r\n\\item \\protect\\hyperlink{convertux5ftoux5ftensor}{\\lstinline{tf.convert_to_tensor(value, dtype=None, name=None)}}\r\n\\item \\protect\\hyperlink{getux5fdefaultux5fgraph}{\\lstinline{tf.get_default_graph()}}\r\n\\item \\protect\\hyperlink{importux5fgraphux5fdef}{\\lstinline{tf.import_graph_def(graph_def, input_map=None, return_elements=None, name=None, op_dict=None)}}\r\n\\item \\protect\\hyperlink{AUTOGENERATED-graph-collections}{Graph collections}\r\n\\item \\protect\\hyperlink{addux5ftoux5fcollection}{\\lstinline{tf.add_to_collection(name, value)}}\r\n\\item \\protect\\hyperlink{getux5fcollection}{\\lstinline{tf.get_collection(key, scope=None)}}\r\n\\item \\protect\\hyperlink{GraphKeys}{\\lstinline{class tf.GraphKeys}}\r\n\\item \\protect\\hyperlink{AUTOGENERATED-defining-new-operations}{Defining new\r\n  operations}\r\n\\item \\protect\\hyperlink{RegisterGradient}{\\lstinline{class tf.RegisterGradient}}\r\n\\item \\protect\\hyperlink{NoGradient}{\\lstinline{tf.NoGradient(op_type)}}\r\n\\item \\protect\\hyperlink{RegisterShape}{\\lstinline{class tf.RegisterShape}}\r\n\\item \\protect\\hyperlink{TensorShape}{\\lstinline{class tf.TensorShape}}\r\n\\item \\protect\\hyperlink{Dimension}{\\lstinline{class tf.Dimension}}\r\n\\item \\protect\\hyperlink{opux5fscope}{\\lstinline{tf.op_scope(values, name, default_name)}}\r\n\\item \\protect\\hyperlink{getux5fseed}{\\lstinline{tf.get_seed(op_seed)}}\r\n\\end{itemize}\r\n\r\nClasses and functions for building TensorFlow graphs.\r\n\r\n\\subsection{Core graph data structures}\\label{core-graph-data-structures}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{class tf.Graph}}{class tf.Graph }}\\label{class-tf.graph}\r\n\r\nA TensorFlow computation, represented as a dataflow graph.\r\n\r\nA \\lstinline{Graph} contains a set of\r\n\\href{../../api_docs/python/framework.md\\#Operation}{\\lstinline{Operation}}\r\nobjects, which represent units of computation; and\r\n\\href{../../api_docs/python/framework.md\\#Tensor}{\\lstinline{Tensor}}\r\nobjects, which represent the units of data that flow between operations.\r\n\r\nA default \\lstinline{Graph} is always registered, and accessible by calling\r\n\\href{../../api_docs/python/framework.md\\#get_default_graph}{\\lstinline{tf.get_default_graph()}}.\r\nTo add an operation to the default graph, simply call one of the\r\nfunctions that defines a new \\lstinline{Operation}:\r\n\r\n\\begin{lstlisting}\r\nc = tf.constant(4.0)\r\nassert c.graph is tf.get_default_graph()\r\n\\end{lstlisting}\r\n\r\nAnother typical usage involves the\r\n\\href{../../api_docs/python/framework.md\\#Graph.as_default}{\\lstinline{Graph.as_default()}}\r\ncontext manager, which overrides the current default graph for the\r\nlifetime of the context:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{g }\\OperatorTok{=} \\NormalTok{tf.Graph()}\r\n\\ControlFlowTok{with} \\NormalTok{g.as_default():}\r\n  \\CommentTok{# Define operations and tensors in `g`.}\r\n  \\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{30.0}\\NormalTok{)}\r\n  \\ControlFlowTok{assert} \\NormalTok{c.graph }\\OperatorTok{is} \\NormalTok{g}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nImportant note: This class \\emph{is not} thread-safe for graph\r\nconstruction. All operations should be created from a single thread, or\r\nexternal synchronization must be provided. Unless otherwise specified,\r\nall methods are not thread-safe.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.__init__()}\r\n}{tf.Graph.__init__() }}\\label{tf.graph.ux5fux5finitux5fux5f}\r\n\r\nCreates a new, empty Graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.as_default()}\r\n}{tf.Graph.as_default() }}\\label{tf.graph.asux5fdefault}\r\n\r\nReturns a context manager that makes this \\lstinline{Graph} the default\r\ngraph.\r\n\r\nThis method should be used if you want to create multiple graphs in the\r\nsame process. For convenience, a global default graph is provided, and\r\nall ops will be added to this graph if you do not create a new graph\r\nexplicitly. Use this method the \\lstinline{with} keyword to specify that\r\nops created within the scope of a block should be added to this graph.\r\n\r\nThe default graph is a property of the current thread. If you create a\r\nnew thread, and wish to use the default graph in that thread, you must\r\nexplicitly add a \\lstinline{with g.as_default():} in that thread's\r\nfunction.\r\n\r\nThe following code examples are equivalent:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 1. Using Graph.as_default():}\r\n\\NormalTok{g }\\OperatorTok{=} \\NormalTok{tf.Graph()}\r\n\\ControlFlowTok{with} \\NormalTok{g.as_default():}\r\n  \\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{5.0}\\NormalTok{)}\r\n  \\ControlFlowTok{assert} \\NormalTok{c.graph }\\OperatorTok{is} \\NormalTok{g}\r\n\r\n\\CommentTok{# 2. Constructing and making default:}\r\n\\ControlFlowTok{with} \\NormalTok{tf.Graph().as_default() }\\ImportTok{as} \\NormalTok{g:}\r\n  \\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{5.0}\\NormalTok{)}\r\n  \\ControlFlowTok{assert} \\NormalTok{c.graph }\\OperatorTok{is} \\NormalTok{g}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Returns: }\\label{returns}\r\n\r\nA context manager for using this graph as the default graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.as_graph_def(from_version=None)}\r\n}{tf.Graph.as_graph_def(from_version=None) }}\\label{tf.graph.asux5fgraphux5fdeffromux5fversionnone}\r\n\r\nReturns a serialized \\lstinline{GraphDef} representation of this graph.\r\n\r\nThe serialized \\lstinline{GraphDef} can be imported into another\r\n\\lstinline{Graph} (using\r\n\\protect\\hyperlink{importux5fgraphux5fdef}{\\lstinline{import_graph_def()}})\r\nor used with the \\href{../../api_docs/cc/index.md}{C++ Session API}.\r\n\r\nThis method is thread-safe.\r\n\r\n\\subparagraph{Args: }\\label{args}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{from_version}: Optional. If this is set, returns a\r\n  \\lstinline{GraphDef} containing only the nodes that were added to this\r\n  graph since its \\lstinline{version} property had the given value.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-1}\r\n\r\nA\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/graph.proto}{\\lstinline{GraphDef}}\r\nprotocol buffer.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.finalize()}\r\n}{tf.Graph.finalize() }}\\label{tf.graph.finalize}\r\n\r\nFinalizes this graph, making it read-only.\r\n\r\nAfter calling \\lstinline{g.finalize()}, no new operations can be added to\r\n\\lstinline{g}. This method is used to ensure that no operations are added\r\nto a graph when it is shared between multiple threads, for example when\r\nusing a\r\n\\href{../../api_docs/python/train.md\\#QueueRunner}{\\lstinline{QueueRunner}}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.finalized}\r\n}{tf.Graph.finalized }}\\label{tf.graph.finalized}\r\n\r\nTrue if this graph has been finalized.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.control_dependencies(control_inputs)}\r\n}{tf.Graph.control_dependencies(control_inputs) }}\\label{tf.graph.controlux5fdependenciescontrolux5finputs}\r\n\r\nReturns a context manager that specifies control dependencies.\r\n\r\nUse with the \\lstinline{with} keyword to specify that all operations\r\nconstructed within the context should have control dependencies on\r\n\\lstinline{control_inputs}. For example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{g.control_dependencies([a, b, c]):}\r\n  \\CommentTok{# `d` and `e` will only run after `a`, `b`, and `c` have executed.}\r\n  \\NormalTok{d }\\OperatorTok{=} \\NormalTok{...}\r\n  \\NormalTok{e }\\OperatorTok{=} \\NormalTok{...}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nMultiple calls to \\lstinline{control_dependencies()} can be nested, and in\r\nthat case a new \\lstinline{Operation} will have control dependencies on the\r\nunion of \\lstinline{control_inputs} from all active contexts.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{g.control_dependencies([a, b]):}\r\n  \\CommentTok{# Ops declared here run after `a` and `b`.}\r\n  \\ControlFlowTok{with} \\NormalTok{g.control_dependencies([c, d]):}\r\n    \\CommentTok{# Ops declared here run after `a`, `b`, `c`, and `d`.}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\emph{N.B.} The control dependencies context applies \\emph{only} to ops\r\nthat are constructed within the context. Merely using an op or tensor in\r\nthe context does not add a control dependency. The following example\r\nillustrates this point:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# WRONG}\r\n\\KeywordTok{def} \\NormalTok{my_func(pred, tensor):}\r\n  \\NormalTok{t }\\OperatorTok{=} \\NormalTok{tf.matmul(tensor, tensor)}\r\n  \\ControlFlowTok{with} \\NormalTok{tf.control_dependencies([pred]):}\r\n    \\CommentTok{# The matmul op is created outside the context, so no control}\r\n    \\CommentTok{# dependency will be added.}\r\n    \\ControlFlowTok{return} \\NormalTok{t}\r\n\r\n\\CommentTok{# RIGHT}\r\n\\KeywordTok{def} \\NormalTok{my_func(pred, tensor):}\r\n  \\ControlFlowTok{with} \\NormalTok{tf.control_dependencies([pred]):}\r\n    \\CommentTok{# The matmul op is created in the context, so a control dependency}\r\n    \\CommentTok{# will be added.}\r\n    \\ControlFlowTok{return} \\NormalTok{tf.matmul(tensor, tensor)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{control_inputs}: A list of \\lstinline{Operation} or\r\n  \\lstinline{Tensor} objects, which must be executed or computed before\r\n  running the operations defined in the context.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-2}\r\n\r\nA context manager that specifies control dependencies for all operations\r\nconstructed within the context.\r\n\r\n\\subparagraph{Raises: }\\label{raises}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{control_inputs} is not a list of\r\n  \\lstinline{Operation} or \\lstinline{Tensor} objects.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.device(device_name_or_function)}\r\n}{tf.Graph.device(device_name_or_function) }}\\label{tf.graph.devicedeviceux5fnameux5forux5ffunction}\r\n\r\nReturns a context manager that specifies the default device to use.\r\n\r\nThe \\lstinline{device_name_or_function} argument may either be a device\r\nname string, a device function, or None:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  If it is a device name string, all operations constructed in this\r\n  context will be assigned to the device with that name.\r\n\\item\r\n  If it is a function, it will be treated as function from Operation\r\n  objects to device name strings, and invoked each time a new Operation\r\n  is created. The Operation will be assigned to the device with the\r\n  returned name.\r\n\\item\r\n  If it is None, the default device will be cleared.\r\n\\end{itemize}\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{g.device(}\\StringTok{'/gpu:0'}\\NormalTok{):}\r\n  \\CommentTok{# All operations constructed in this context will be placed}\r\n  \\CommentTok{# on GPU 0.}\r\n  \\ControlFlowTok{with} \\NormalTok{g.device(}\\VariableTok{None}\\NormalTok{):}\r\n    \\CommentTok{# All operations constructed in this context will have no}\r\n    \\CommentTok{# assigned device.}\r\n\r\n\\CommentTok{# Defines a function from `Operation` to device string.}\r\n\\KeywordTok{def} \\NormalTok{matmul_on_gpu(n):}\r\n  \\ControlFlowTok{if} \\NormalTok{n.}\\BuiltInTok{type} \\OperatorTok{==} \\StringTok{\"MatMul\"}\\NormalTok{:}\r\n    \\ControlFlowTok{return} \\StringTok{\"/gpu:0\"}\r\n  \\ControlFlowTok{else}\\NormalTok{:}\r\n    \\ControlFlowTok{return} \\StringTok{\"/cpu:0\"}\r\n\r\n\\ControlFlowTok{with} \\NormalTok{g.device(matmul_on_gpu):}\r\n  \\CommentTok{# All operations of type \"MatMul\" constructed in this context}\r\n  \\CommentTok{# will be placed on GPU 0; all other operations will be placed}\r\n  \\CommentTok{# on CPU 0.}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{device_name_or_function}: The device name or function to\r\n  use in the context.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-3}\r\n\r\nA context manager that specifies the default device to use for newly\r\ncreated ops.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.name_scope(name)}\r\n}{tf.Graph.name_scope(name) }}\\label{tf.graph.nameux5fscopename}\r\n\r\nReturns a context manager that creates hierarchical names for\r\noperations.\r\n\r\nA graph maintains a stack of name scopes. A\r\n\\lstinline{with name_scope(...):} statement pushes a new name onto the\r\nstack for the lifetime of the context.\r\n\r\nThe \\lstinline{name} argument will be interpreted as follows:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  A string (not ending with `/') will create a new name scope, in which\r\n  \\lstinline{name} is appended to the prefix of all operations created in\r\n  the context. If \\lstinline{name} has been used before, it will be made\r\n  unique by calling \\lstinline{self.unique_name(name)}.\r\n\\item\r\n  A scope previously captured from a\r\n  \\lstinline{with g.name_scope(...) as   scope:} statement will be\r\n  treated as an ``absolute'' name scope, which makes it possible to\r\n  re-enter existing scopes.\r\n\\item\r\n  A value of \\lstinline{None} or the empty string will reset the current\r\n  name scope to the top-level (empty) name scope.\r\n\\end{itemize}\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.Graph().as_default() }\\ImportTok{as} \\NormalTok{g:}\r\n  \\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{5.0}\\NormalTok{, name}\\OperatorTok{=}\\StringTok{\"c\"}\\NormalTok{)}\r\n  \\ControlFlowTok{assert} \\NormalTok{c_1.name }\\OperatorTok{==} \\StringTok{\"c\"}\r\n  \\NormalTok{c_1 }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{6.0}\\NormalTok{, name}\\OperatorTok{=}\\StringTok{\"c\"}\\NormalTok{)}\r\n  \\ControlFlowTok{assert} \\NormalTok{c_1.name }\\OperatorTok{==} \\StringTok{\"c_1\"}\r\n\r\n  \\CommentTok{# Creates a scope called \"nested\"}\r\n  \\ControlFlowTok{with} \\NormalTok{g.name_scope(}\\StringTok{\"nested\"}\\NormalTok{) }\\ImportTok{as} \\NormalTok{scope:}\r\n    \\NormalTok{nested_c }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{10.0}\\NormalTok{, name}\\OperatorTok{=}\\StringTok{\"c\"}\\NormalTok{)}\r\n    \\ControlFlowTok{assert} \\NormalTok{nested_c.name }\\OperatorTok{==} \\StringTok{\"nested/c\"}\r\n\r\n    \\CommentTok{# Creates a nested scope called \"inner\".}\r\n    \\ControlFlowTok{with} \\NormalTok{g.name_scope(}\\StringTok{\"inner\"}\\NormalTok{):}\r\n      \\NormalTok{nested_inner_c }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{20.0}\\NormalTok{, name}\\OperatorTok{=}\\StringTok{\"c\"}\\NormalTok{)}\r\n      \\ControlFlowTok{assert} \\NormalTok{nested_inner_c.name }\\OperatorTok{==} \\StringTok{\"nested/inner/c\"}\r\n\r\n    \\CommentTok{# Create a nested scope called \"inner_1\".}\r\n    \\ControlFlowTok{with} \\NormalTok{g.name_scope(}\\StringTok{\"inner\"}\\NormalTok{):}\r\n      \\NormalTok{nested_inner_1_c }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{30.0}\\NormalTok{, name}\\OperatorTok{=}\\StringTok{\"c\"}\\NormalTok{)}\r\n      \\ControlFlowTok{assert} \\NormalTok{nested_inner_1_c.name }\\OperatorTok{==} \\StringTok{\"nested/inner_1/c\"}\r\n\r\n      \\CommentTok{# Treats `scope` as an absolute name scope, and}\r\n      \\CommentTok{# switches to the \"nested/\" scope.}\r\n      \\ControlFlowTok{with} \\NormalTok{g.name_scope(scope):}\r\n        \\NormalTok{nested_d }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{40.0}\\NormalTok{, name}\\OperatorTok{=}\\StringTok{\"d\"}\\NormalTok{)}\r\n        \\ControlFlowTok{assert} \\NormalTok{nested_d.name }\\OperatorTok{==} \\StringTok{\"nested/d\"}\r\n\r\n        \\ControlFlowTok{with} \\NormalTok{g.name_scope(}\\StringTok{\"\"}\\NormalTok{):}\r\n          \\NormalTok{e }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{50.0}\\NormalTok{, name}\\OperatorTok{=}\\StringTok{\"e\"}\\NormalTok{)}\r\n          \\ControlFlowTok{assert} \\NormalTok{e.name }\\OperatorTok{==} \\StringTok{\"e\"}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nThe name of the scope itself can be captured by\r\n\\lstinline{with g.name_scope(...) as scope:}, which stores the name of\r\nthe scope in the variable \\lstinline{scope}. This value can be used to name\r\nan operation that represents the overall result of executing the ops in\r\na scope. For example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{inputs }\\OperatorTok{=} \\NormalTok{tf.constant(...)}\r\n\\ControlFlowTok{with} \\NormalTok{g.name_scope(}\\StringTok{'my_layer'}\\NormalTok{) }\\ImportTok{as} \\NormalTok{scope:}\r\n  \\NormalTok{weights }\\OperatorTok{=} \\NormalTok{tf.Variable(..., name}\\OperatorTok{=}\\StringTok{\"weights\"}\\NormalTok{)}\r\n  \\NormalTok{biases }\\OperatorTok{=} \\NormalTok{tf.Variable(..., name}\\OperatorTok{=}\\StringTok{\"biases\"}\\NormalTok{)}\r\n  \\NormalTok{affine }\\OperatorTok{=} \\NormalTok{tf.matmul(inputs, weights) }\\OperatorTok{+} \\NormalTok{biases}\r\n  \\NormalTok{output }\\OperatorTok{=} \\NormalTok{tf.nn.relu(affine, name}\\OperatorTok{=}\\NormalTok{scope)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{name}: A name for the scope.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-4}\r\n\r\nA context manager that installs \\lstinline{name} as a new name scope.\r\n\r\nA \\lstinline{Graph} instance supports an arbitrary number of\r\n``collections'' that are identified by name. For convenience when\r\nbuilding a large graph, collections can store groups of related objects:\r\nfor example, the \\lstinline{tf.Variable} uses a collection (named\r\n\\href{../../api_docs/python/framework.md\\#GraphKeys}{\\lstinline{tf.GraphKeys.VARIABLES}})\r\nfor all variables that are created during the construction of a graph.\r\nThe caller may define additional collections by specifying a new name.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.add_to_collection(name, value)}\r\n}{tf.Graph.add_to_collection(name, value) }}\\label{tf.graph.addux5ftoux5fcollectionname-value}\r\n\r\nStores \\lstinline{value} in the collection with the given \\lstinline{name}.\r\n\r\n\\subparagraph{Args: }\\label{args-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{name}: The key for the collection. For example, the\r\n  \\lstinline{GraphKeys} class contains many standard names for collections.\r\n\\item\r\n  \\lstinline{value}: The value to add to the collection.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.get_collection(name, scope=None)}\r\n}{tf.Graph.get_collection(name, scope=None) }}\\label{tf.graph.getux5fcollectionname-scopenone}\r\n\r\nReturns a list of values in the collection with the given \\lstinline{name}.\r\n\r\n\\subparagraph{Args: }\\label{args-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{key}: The key for the collection. For example, the\r\n  \\lstinline{GraphKeys} class contains many standard names for collections.\r\n\\item\r\n  \\lstinline{scope}: (Optional.) If supplied, the resulting list is\r\n  filtered to include only items whose name begins with this string.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-5}\r\n\r\nThe list of values in the collection with the given \\lstinline{name}, or an\r\nempty list if no value has been added to that collection. The list\r\ncontains the values in the order under which they were collected.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.as_graph_element(obj, allow_tensor=True, allow_operation=True)}\r\n}{tf.Graph.as_graph_element(obj, allow_tensor=True, allow_operation=True) }}\\label{tf.graph.asux5fgraphux5felementobj-allowux5ftensortrue-allowux5foperationtrue}\r\n\r\nReturns the object referred to by \\lstinline{obj}, as an \\lstinline{Operation}\r\nor \\lstinline{Tensor}.\r\n\r\nThis function validates that \\lstinline{obj} represents an element of this\r\ngraph, and gives an informative error message if it is not.\r\n\r\nThis function is the canonical way to get/validate an object of one of\r\nthe allowed types from an external argument reference in the Session\r\nAPI.\r\n\r\nThis method may be called concurrently from multiple threads.\r\n\r\n\\subparagraph{Args: }\\label{args-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{obj}: A \\lstinline{Tensor}, an \\lstinline{Operation}, or the name of\r\n  a tensor or operation. Can also be any object with an\r\n  \\lstinline{_as_graph_element()} method that returns a value of one of\r\n  these types.\r\n\\item\r\n  \\lstinline{allow_tensor}: If true, \\lstinline{obj} may refer to a\r\n  \\lstinline{Tensor}.\r\n\\item\r\n  \\lstinline{allow_operation}: If true, \\lstinline{obj} may refer to an\r\n  \\lstinline{Operation}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-6}\r\n\r\nThe \\lstinline{Tensor} or \\lstinline{Operation} in the Graph corresponding to\r\n\\lstinline{obj}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{obj} is not a type we support\r\n  attempting to convert to types.\r\n\\item\r\n  \\lstinline{ValueError}: If \\lstinline{obj} is of an appropriate type but\r\n  invalid. For example, an invalid string.\r\n\\item\r\n  \\lstinline{KeyError}: If \\lstinline{obj} is not an object in the graph.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.get_operation_by_name(name)}\r\n}{tf.Graph.get_operation_by_name(name) }}\\label{tf.graph.getux5foperationux5fbyux5fnamename}\r\n\r\nReturns the \\lstinline{Operation} with the given \\lstinline{name}.\r\n\r\nThis method may be called concurrently from multiple threads.\r\n\r\n\\subparagraph{Args: }\\label{args-7}\r\n\r\n\\begin{itemize}\r\n\\item \\lstinline{name}: The name of the \\lstinline{Operation} to return.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-7}\r\n\r\nThe \\lstinline{Operation} with the given \\lstinline{name}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{name} is not a string.\r\n\\item\r\n  \\lstinline{KeyError}: If \\lstinline{name} does not correspond to an\r\n  operation in this graph.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.get_tensor_by_name(name)}\r\n}{tf.Graph.get_tensor_by_name(name) }}\\label{tf.graph.getux5ftensorux5fbyux5fnamename}\r\n\r\nReturns the \\lstinline{Tensor} with the given \\lstinline{name}.\r\n\r\nThis method may be called concurrently from multiple threads.\r\n\r\n\\subparagraph{Args: }\\label{args-8}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{name}: The name of the \\lstinline{Tensor} to return.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-8}\r\n\r\nThe \\lstinline{Tensor} with the given \\lstinline{name}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{name} is not a string.\r\n\\item\r\n  \\lstinline{KeyError}: If \\lstinline{name} does not correspond to a tensor in\r\n  this graph.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.get_operations()}\r\n}{tf.Graph.get_operations() }}\\label{tf.graph.getux5foperations}\r\n\r\nReturn the list of operations in the graph.\r\n\r\nYou can modify the operations in place, but modifications to the list\r\nsuch as inserts/delete have no effect on the list of operations known to\r\nthe graph.\r\n\r\nThis method may be called concurrently from multiple threads.\r\n\r\n\\subparagraph{Returns: }\\label{returns-9}\r\n\r\nA list of Operations.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.get_default_device()}\r\n}{tf.Graph.get_default_device() }}\\label{tf.graph.getux5fdefaultux5fdevice}\r\n\r\nReturns the default device.\r\n\r\n\\subparagraph{Returns: }\\label{returns-10}\r\n\r\nA string.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.seed}\r\n}{tf.Graph.seed }}\\label{tf.graph.seed}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.unique_name(name)}\r\n}{tf.Graph.unique_name(name) }}\\label{tf.graph.uniqueux5fnamename}\r\n\r\nReturn a unique Operation name for ``name''.\r\n\r\nNote: You rarely need to call unique_name() directly. Most of the time\r\nyou just need to create ``with g.name_scope()'' blocks to generate\r\nstructured names.\r\n\r\n\\lstinline{unique_name} is used to generate structured names, separated by\r\n``/'', to help identify Operations when debugging a Graph. Operation\r\nnames are displayed in error messages reported by the TensorFlow\r\nruntime, and in various visualization tools such as TensorBoard.\r\n\r\n\\subparagraph{Args: }\\label{args-9}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{name}: The name for an \\lstinline{Operation}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-11}\r\n\r\nA string to be passed to \\lstinline{create_op()} that will be used to name\r\nthe operation being created.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.version}\r\n}{tf.Graph.version }}\\label{tf.graph.version}\r\n\r\nReturns a version number that increases as ops are added to the graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.create_op(op_type, inputs, dtypes, input_types=None, name=None, attrs=None, op_def=None, compute_shapes=True)}\r\n}{tf.Graph.create_op(op_type, inputs, dtypes, input_types=None, name=None, attrs=None, op_def=None, compute_shapes=True) }}\\label{tf.graph.createux5fopopux5ftype-inputs-dtypes-inputux5ftypesnone-namenone-attrsnone-opux5fdefnone-computeux5fshapestrue}\r\n\r\nCreates an \\lstinline{Operation} in this graph.\r\n\r\nThis is a low-level interface for creating an \\lstinline{Operation}. Most\r\nprograms will not call this method directly, and instead use the Python\r\nop constructors, such as \\lstinline{tf.constant()}, which add ops to the\r\ndefault graph.\r\n\r\n\\subparagraph{Args: }\\label{args-10}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{op_type}: The \\lstinline{Operation} type to create. This\r\n  corresponds to the \\lstinline{OpDef.name} field for the proto that\r\n  defines the operation.\r\n\\item\r\n  \\lstinline{inputs}: A list of \\lstinline{Tensor} objects that will be inputs\r\n  to the \\lstinline{Operation}.\r\n\\item\r\n  \\lstinline{dtypes}: A list of \\lstinline{DType} objects that will be the\r\n  types of the tensors that the operation produces.\r\n\\item\r\n  \\lstinline{input_types}: (Optional.) A list of \\lstinline{DType}s that will\r\n  be the types of the tensors that the operation consumes. By default,\r\n  uses the base \\lstinline{DType} of each input in \\lstinline{inputs}.\r\n  Operations that expect reference-typed inputs must specify\r\n  \\lstinline{input_types} explicitly.\r\n\\item\r\n  \\lstinline{name}: (Optional.) A string name for the operation. If not\r\n  specified, a name is generated based on \\lstinline{op_type}.\r\n\\item\r\n  \\lstinline{attrs}: (Optional.) A list of \\lstinline{AttrValue} protos for\r\n  the \\lstinline{attr} field of the \\lstinline{NodeDef} proto that will\r\n  represent the operation.\r\n\\item\r\n  \\lstinline{op_def}: (Optional.) The \\lstinline{OpDef} proto that describes\r\n  the \\lstinline{op_type} that the operation will have.\r\n\\item\r\n  \\lstinline{compute_shapes}: (Optional.) If True, shape inference will be\r\n  performed to compute the shapes of the outputs.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: if any of the inputs is not a \\lstinline{Tensor}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-12}\r\n\r\nAn \\lstinline{Operation} object.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Graph.gradient_override_map(op_type_map)}\r\n}{tf.Graph.gradient_override_map(op_type_map) }}\\label{tf.graph.gradientux5foverrideux5fmapopux5ftypeux5fmap}\r\n\r\nEXPERIMENTAL: A context manager for overriding gradient functions.\r\n\r\nThis context manager can be used to override the gradient function that\r\nwill be used for ops within the scope of the context.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\AttributeTok{@tf.RegisterGradient}\\NormalTok{(}\\StringTok{\"CustomSquare\"}\\NormalTok{)}\r\n\\KeywordTok{def} \\NormalTok{_custom_square_grad(op, inputs):}\r\n  \\CommentTok{# ...}\r\n\r\n\\ControlFlowTok{with} \\NormalTok{tf.Graph().as_default() }\\ImportTok{as} \\NormalTok{g:}\r\n  \\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{5.0}\\NormalTok{)}\r\n  \\NormalTok{s_1 }\\OperatorTok{=} \\NormalTok{tf.square(c)  }\\CommentTok{# Uses the default gradient for tf.square.}\r\n  \\ControlFlowTok{with} \\NormalTok{g.gradient_override_map(\\{}\\StringTok{\"Square\"}\\NormalTok{: }\\StringTok{\"CustomSquare\"}\\NormalTok{\\}):}\r\n    \\NormalTok{s_2 }\\OperatorTok{=} \\NormalTok{tf.square(s_2)  }\\CommentTok{# Uses _custom_square_grad to compute the}\r\n                          \\CommentTok{# gradient of s_2.}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-11}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{op_type_map}: A dictionary mapping op type strings to\r\n  alternative op type strings.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-13}\r\n\r\nA context manager that sets the alternative op type to be used for one\r\nor more ops created in that context.\r\n\r\n\\subparagraph{Raises: }\\label{raises-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{op_type_map} is not a dictionary\r\n  mapping strings to strings.\r\n\\end{itemize}\r\n\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\r\n\\subsubsection{class \\lstinline{tf.Operation}} \\label{class-tf.operation}\r\n\r\nRepresents a graph node that performs computation on tensors.\r\n\r\nAn \\lstinline{Operation} is a node in a TensorFlow \\lstinline{Graph} that\r\ntakes zero or more \\lstinline{Tensor} objects as input, and produces zero\r\nor more \\lstinline{Tensor} objects as output. Objects of type\r\n\\lstinline{Operation} are created by calling a Python op constructor (such\r\nas\r\n\\href{../../api_docs/python/math_ops.md\\#matmul}{\\lstinline{tf.matmul()}})\r\nor\r\n\\href{../../api_docs/python/framework.md\\#Graph.create_op}{\\lstinline{Graph.create_op()}}.\r\n\r\nFor example \\lstinline{c = tf.matmul(a, b)} creates an\r\n\\lstinline{Operation} of type \"MatMul\" that takes tensors \\lstinline{a} and\r\n\\lstinline{b} as input, and produces \\lstinline{c} as output.\r\n\r\nAfter the graph has been launched in a session, an \\lstinline{Operation}\r\ncan be executed by passing it to\r\n\\href{../../api_docs/python/client.md\\#Session.run}{\\lstinline{Session.run()}}.\r\n\\lstinline{op.run()} is a shortcut for calling\r\n\\lstinline{tf.get_default_session().run(op)}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Operation.name}\r\n}{tf.Operation.name }}\\label{tf.operation.name}\r\n\r\nThe full name of this operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Operation.type}\r\n}{tf.Operation.type }}\\label{tf.operation.type}\r\n\r\nThe type of the op (e.g. \\lstinline{\"MatMul\"}).\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Operation.inputs}\r\n}{tf.Operation.inputs }}\\label{tf.operation.inputs}\r\n\r\nThe list of \\lstinline{Tensor} objects representing the data inputs of this\r\nop.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Operation.control_inputs}\r\n}{tf.Operation.control_inputs }}\\label{tf.operation.controlux5finputs}\r\n\r\nThe \\lstinline{Operation} objects on which this op has a control\r\ndependency.\r\n\r\nBefore this op is executed, TensorFlow will ensure that the operations\r\nin \\lstinline{self.control_inputs} have finished executing. This mechanism\r\ncan be used to run ops sequentially for performance reasons, or to\r\nensure that the side effects of an op are observed in the correct order.\r\n\r\n\\subparagraph{Returns: }\\label{returns-14}\r\n\r\nA list of \\lstinline{Operation} objects.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Operation.outputs}\r\n}{tf.Operation.outputs }}\\label{tf.operation.outputs}\r\n\r\nThe list of \\lstinline{Tensor} objects representing the outputs of this op.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Operation.device}\r\n}{tf.Operation.device }}\\label{tf.operation.device}\r\n\r\nThe name of the device to which this op has been assigned, if any.\r\n\r\n\\subparagraph{Returns: }\\label{returns-15}\r\n\r\nThe string name of the device to which this op has been assigned, or\r\nNone if it has not been assigned to a device.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Operation.graph}\r\n}{tf.Operation.graph }}\\label{tf.operation.graph}\r\n\r\nThe \\lstinline{Graph} that contains this operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Operation.run(feed_dict=None, session=None)}\r\n}{tf.Operation.run(feed_dict=None, session=None) }}\\label{tf.operation.run}\r\n\r\nRuns this operation in a \\lstinline{Session}.\r\n\r\nCalling this method will execute all preceding operations that produce\r\nthe inputs needed for this operation.\r\n\r\n\\emph{N.B.} Before invoking \\lstinline{Operation.run()}, its graph must\r\nhave been launched in a session, and either a default session must be\r\navailable, or \\lstinline{session} must be specified explicitly.\r\n\r\n\\subparagraph{Args: }\\label{args-12}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{feed_dict}: A dictionary that maps \\lstinline{Tensor} objects to\r\n  feed values. See\r\n  \\href{../../api_docs/python/client.md\\#Session.run}{\\lstinline{Session.run()}}\r\n  for a description of the valid feed values.\r\n\\item\r\n  \\lstinline{session}: (Optional.) The \\lstinline{Session} to be used to run\r\n  to this operation. If none, the default session will be used.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Operation.get_attr(name)}\r\n}{tf.Operation.get_attr(name) }}\\label{tf.operation.getux5fattrname}\r\n\r\nReturns the value of the attr of this op with the given \\lstinline{name}.\r\n\r\n\\subparagraph{Args: }\\label{args-13}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{name}: The name of the attr to fetch.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-16}\r\n\r\nThe value of the attr, as a Python object.\r\n\r\n\\subparagraph{Raises: }\\label{raises-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: If this op does not have an attr with the given\r\n  \\lstinline{name}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Operation.traceback}\r\n}{tf.Operation.traceback }}\\label{tf.operation.traceback}\r\n\r\nReturns the call stack from when this operation was constructed.\r\n\r\n\\paragraph{Other Methods }\\label{other-methods}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Operation.__init__(node_def, g, inputs=None, output_types=None, control_inputs=None, input_types=None, original_op=None, op_def=None)}\r\n}{tf.Operation.__init__(node_def, g, inputs=None, output_types=None, control_inputs=None, input_types=None, original_op=None, op_def=None) }}\\label{tf.operation.ux5fux5finitux5fux5fnodeux5fdef-g-inputsnone-outputux5ftypesnone-controlux5finputsnone-inputux5ftypesnone-originalux5fopnone-opux5fdefnone}\r\n\r\nCreates an \\lstinline{Operation}.\r\n\r\nNOTE: This constructor validates the name of the Operation (passed as\r\n``node_def.name''). Valid Operation names match the following regular\r\nexpression:\r\n\r\n{[}A-Za-z0-9.{]}{[}A-Za-z0-9_.-/{]}*\r\n\r\n\\subparagraph{Args: }\\label{args-14}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{node_def}: graph_pb2.NodeDef. NodeDef for the Operation.\r\n  Used for attributes of graph_pb2.NodeDef, typically ``name'', ``op'',\r\n  and ``device''. The ``input'' attribute is irrelevant here as it will\r\n  be computed when generating the model.\r\n\\item\r\n  \\lstinline{g}: Graph. The parent graph.\r\n\\item\r\n  \\lstinline{inputs}: list of Tensor objects. The inputs to this Operation.\r\n\\item\r\n  \\lstinline{output_types}: list of types_pb2.DataType. List of the types\r\n  of the Tensors computed by this operation. The length of this list\r\n  indicates the number of output endpoints of the Operation.\r\n\\item\r\n  \\lstinline{control_inputs}: list of operations or tensors from which to\r\n  have a control dependency.\r\n\\item\r\n  \\lstinline{input_types}: List of types_pb2.DataType representing the\r\n  types of the Tensors accepted by the Operation. By default uses\r\n  {[}x.dtype.base_dtype for x in inputs{]}. Operations that expect\r\n  reference-typed inputs must specify these explicitly.\r\n\\item\r\n  \\lstinline{original_op}: Optional. Used to associate the new Operation\r\n  with an existing Operation (for example, a replica with the op that\r\n  was replicated).\r\n\\item\r\n  \\lstinline{op_def}: Optional. The op_def_pb2.OpDef proto that\r\n  describes the op type that this Operation represents.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-7}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: if control inputs are not Operations or Tensors,\r\n  or if node_def is not a NodeDef, or if g is not a Graph, or if inputs\r\n  are not Tensors, or if inputs and input_types are incompatible.\r\n\\item\r\n  \\lstinline{ValueError}: if the node_def name is not valid.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Operation.node_def}\r\n}{tf.Operation.node_def }}\\label{tf.operation.nodeux5fdef}\r\n\r\nReturns a serialized \\lstinline{NodeDef} representation of this operation.\r\n\r\n\\subparagraph{Returns: }\\label{returns-17}\r\n\r\nA\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/graph.proto}{\\lstinline{NodeDef}}\r\nprotocol buffer.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Operation.op_def}\r\n}{tf.Operation.op_def }}\\label{tf.operation.opux5fdef}\r\n\r\nReturns the \\lstinline{OpDef} proto that represents the type of this op.\r\n\r\n\\subparagraph{Returns: }\\label{returns-18}\r\n\r\nAn\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op_def.proto}{\\lstinline{OpDef}}\r\nprotocol buffer.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Operation.values()}\r\n}{tf.Operation.values() }}\\label{tf.operation.values}\r\n\r\nDEPRECATED: Use outputs.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{class tf.Tensor}\r\n}{class tf.Tensor }}\\label{class-tf.tensor}\r\n\r\nRepresents a value produced by an \\lstinline{Operation}.\r\n\r\nA \\lstinline{Tensor} is a symbolic handle to one of the outputs of an\r\n\\lstinline{Operation}. It does not hold the values of that operation's\r\noutput, but instead provides a means of computing those values in a\r\nTensorFlow\r\n\\href{../../api_docs/python/client.md\\#Session}{\\lstinline{Session}}.\r\n\r\nThis class has two primary purposes:\r\n\r\n\\begin{enumerate}\r\n\\def\\labelenumi{\\arabic{enumi}.}\r\n\\item\r\n  A \\lstinline{Tensor} can be passed as an input to another\r\n  \\lstinline{Operation}. This builds a dataflow connection between\r\n  operations, which enables TensorFlow to execute an entire\r\n  \\lstinline{Graph} that represents a large, multi-step computation.\r\n\\item\r\n  After the graph has been launched in a session, the value of the\r\n  \\lstinline{Tensor} can be computed by passing it to\r\n  \\href{../../api_docs/python/client.md\\#Session.run}{\\lstinline{Session.run()}}.\r\n  \\lstinline{t.eval()} is a shortcut for calling\r\n  \\lstinline{tf.get_default_session().run(t)}.\r\n\\end{enumerate}\r\n\r\nIn the following example, \\lstinline{c}, \\lstinline{d}, and \\lstinline{e} are\r\nsymbolic \\lstinline{Tensor} objects, whereas \\lstinline{result} is a numpy\r\narray that stores a concrete value:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Build a dataflow graph.}\r\n\\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.constant([[}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{2.0}\\NormalTok{], [}\\FloatTok{3.0}\\NormalTok{, }\\FloatTok{4.0}\\NormalTok{]])}\r\n\\NormalTok{d }\\OperatorTok{=} \\NormalTok{tf.constant([[}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{1.0}\\NormalTok{], [}\\FloatTok{0.0}\\NormalTok{, }\\FloatTok{1.0}\\NormalTok{]])}\r\n\\NormalTok{e }\\OperatorTok{=} \\NormalTok{tf.matmul(c, d)}\r\n\r\n\\CommentTok{# Construct a `Session` to execut the graph.}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session()}\r\n\r\n\\CommentTok{# Execute the graph and store the value that `e` represents in `result`.}\r\n\\NormalTok{result }\\OperatorTok{=} \\NormalTok{sess.run(e)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Tensor.dtype}\r\n}{tf.Tensor.dtype }}\\label{tf.tensor.dtype}\r\n\r\nThe \\lstinline{DType} of elements in this tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Tensor.name}\r\n}{tf.Tensor.name }}\\label{tf.tensor.name}\r\n\r\nThe string name of this tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Tensor.value_index}\r\n}{tf.Tensor.value_index }}\\label{tf.tensor.valueux5findex}\r\n\r\nThe index of this tensor in the outputs of its \\lstinline{Operation}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Tensor.graph}\r\n}{tf.Tensor.graph }}\\label{tf.tensor.graph}\r\n\r\nThe \\lstinline{Graph} that contains this tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Tensor.op}\r\n}{tf.Tensor.op }}\\label{tf.tensor.op}\r\n\r\nThe \\lstinline{Operation} that produces this tensor as an output.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Tensor.consumers()}\r\n}{tf.Tensor.consumers() }}\\label{tf.tensor.consumers}\r\n\r\nReturns a list of \\lstinline{Operation}s that consume this tensor.\r\n\r\n\\subparagraph{Returns: }\\label{returns-19}\r\n\r\nA list of \\lstinline{Operation}s.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Tensor.eval(feed_dict=None, session=None)}\r\n}{tf.Tensor.eval(feed_dict=None, session=None) }}\\label{tf.tensor.eval}\r\n\r\nEvaluates this tensor in a \\lstinline{Session}.\r\n\r\nCalling this method will execute all preceding operations that produce\r\nthe inputs needed for the operation that produces this tensor.\r\n\r\n\\emph{N.B.} Before invoking \\lstinline{Tensor.eval()}, its graph must have\r\nbeen launched in a session, and either a default session must be\r\navailable, or \\lstinline{session} must be specified explicitly.\r\n\r\n\\subparagraph{Args: }\\label{args-15}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{feed_dict}: A dictionary that maps \\lstinline{Tensor} objects to\r\n  feed values. See\r\n  \\href{../../api_docs/python/client.md\\#Session.run}{\\lstinline{Session.run()}}\r\n  for a description of the valid feed values.\r\n\\item\r\n  \\lstinline{session}: (Optional.) The \\lstinline{Session} to be used to\r\n  evaluate this tensor. If none, the default session will be used.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-20}\r\n\r\nA numpy array corresponding to the value of this tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Tensor.get_shape()}\r\n}{tf.Tensor.get_shape() }}\\label{tf.tensor.getux5fshape}\r\n\r\nReturns the \\lstinline{TensorShape} that represents the shape of this\r\ntensor.\r\n\r\nThe shape is computed using shape inference functions that are\r\nregistered for each \\lstinline{Operation} type using\r\n\\lstinline{tf.RegisterShape}. See\r\n\\href{../../api_docs/python/framework.md\\#TensorShape}{\\lstinline{TensorShape}}\r\nfor more details of what a shape represents.\r\n\r\nThe inferred shape of a tensor is used to provide shape information\r\nwithout having to launch the graph in a session. This can be used for\r\ndebugging, and providing early error messages. For example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.constant([[}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{2.0}\\NormalTok{, }\\FloatTok{3.0}\\NormalTok{], [}\\FloatTok{4.0}\\NormalTok{, }\\FloatTok{5.0}\\NormalTok{, }\\FloatTok{6.0}\\NormalTok{]])}\r\n\r\n\\BuiltInTok{print} \\NormalTok{c.get_shape()}\r\n\\OperatorTok{==>} \\NormalTok{TensorShape([Dimension(}\\DecValTok{2}\\NormalTok{), Dimension(}\\DecValTok{3}\\NormalTok{)])}\r\n\r\n\\NormalTok{d }\\OperatorTok{=} \\NormalTok{tf.constant([[}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{0.0}\\NormalTok{], [}\\FloatTok{0.0}\\NormalTok{, }\\FloatTok{1.0}\\NormalTok{], [}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{0.0}\\NormalTok{], [}\\FloatTok{0.0}\\NormalTok{, }\\FloatTok{1.0}\\NormalTok{]])}\r\n\r\n\\BuiltInTok{print} \\NormalTok{d.get_shape()}\r\n\\OperatorTok{==>} \\NormalTok{TensorShape([Dimension(}\\DecValTok{4}\\NormalTok{), Dimension(}\\DecValTok{2}\\NormalTok{)])}\r\n\r\n\\CommentTok{# Raises a ValueError, because `c` and `d` do not have compatible}\r\n\\CommentTok{# inner dimensions.}\r\n\\NormalTok{e }\\OperatorTok{=} \\NormalTok{tf.matmul(c, d)}\r\n\r\n\\NormalTok{f }\\OperatorTok{=} \\NormalTok{tf.matmul(c, d, transpose_a}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{, transpose_b}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{)}\r\n\r\n\\BuiltInTok{print} \\NormalTok{f.get_shape()}\r\n\\OperatorTok{==>} \\NormalTok{TensorShape([Dimension(}\\DecValTok{3}\\NormalTok{), Dimension(}\\DecValTok{4}\\NormalTok{)])}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nIn some cases, the inferred shape may have unknown dimensions. If the\r\ncaller has additional information about the values of these dimensions,\r\n\\lstinline{Tensor.set_shape()} can be used to augment the inferred shape.\r\n\r\n\\subparagraph{Returns: }\\label{returns-21}\r\n\r\nA \\lstinline{TensorShape} representing the shape of this tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Tensor.set_shape(shape)}\r\n}{tf.Tensor.set_shape(shape) }}\\label{tf.tensor.setux5fshapeshape}\r\n\r\nUpdates the shape of this tensor.\r\n\r\nThis method can be called multiple times, and will merge the given\r\n\\lstinline{shape} with the current shape of this tensor. It can be used to\r\nprovide additional information about the shape of this tensor that\r\ncannot be inferred from the graph alone. For example, this can be used\r\nto provide additional information about the shapes of images:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{_, image_data }\\OperatorTok{=} \\NormalTok{tf.TFRecordReader(...).read(...)}\r\n\\NormalTok{image }\\OperatorTok{=} \\NormalTok{tf.image.decode_png(image_data, channels}\\OperatorTok{=}\\DecValTok{3}\\NormalTok{)}\r\n\r\n\\CommentTok{# The height and width dimensions of `image` are data dependent, and}\r\n\\CommentTok{# cannot be computed without executing the op.}\r\n\\BuiltInTok{print} \\NormalTok{image.get_shape()}\r\n\\OperatorTok{==>} \\NormalTok{TensorShape([Dimension(}\\VariableTok{None}\\NormalTok{), Dimension(}\\VariableTok{None}\\NormalTok{), Dimension(}\\DecValTok{3}\\NormalTok{)])}\r\n\r\n\\CommentTok{# We know that each image in this dataset is 28 x 28 pixels.}\r\n\\NormalTok{image.set_shape([}\\DecValTok{28}\\NormalTok{, }\\DecValTok{28}\\NormalTok{, }\\DecValTok{3}\\NormalTok{])}\r\n\\BuiltInTok{print} \\NormalTok{image.get_shape()}\r\n\\OperatorTok{==>} \\NormalTok{TensorShape([Dimension(}\\DecValTok{28}\\NormalTok{), Dimension(}\\DecValTok{28}\\NormalTok{), Dimension(}\\DecValTok{3}\\NormalTok{)])}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-16}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{shape}: A \\lstinline{TensorShape} representing the shape of this\r\n  tensor.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-8}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: If \\lstinline{shape} is not compatible with the\r\n  current shape of this tensor.\r\n\\end{itemize}\r\n\r\n\\paragraph{Other Methods }\\label{other-methods-1}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Tensor.__init__(op, value_index, dtype)}\r\n}{tf.Tensor.__init__(op, value_index, dtype) }}\\label{tf.tensor.ux5fux5finitux5fux5fop-valueux5findex-dtype}\r\n\r\nCreates a new \\lstinline{Tensor}.\r\n\r\n\\subparagraph{Args: }\\label{args-17}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{op}: An \\lstinline{Operation}. \\lstinline{Operation} that computes\r\n  this tensor.\r\n\\item\r\n  \\lstinline{value_index}: An \\lstinline{int}. Index of the operation's\r\n  endpoint that produces this tensor.\r\n\\item\r\n  \\lstinline{dtype}: A \\lstinline{types.DType}. Type of data stored in this\r\n  tensor.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-9}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If the op is not an \\lstinline{Operation}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Tensor.device}\r\n}{tf.Tensor.device }}\\label{tf.tensor.device}\r\n\r\nThe name of the device on which this tensor will be produced, or None.\r\n\r\n\\subsection{Tensor types }\\label{tensor-types}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{class tf.DType}\r\n}{class tf.DType }}\\label{class-tf.dtype}\r\n\r\nRepresents the type of the elements in a \\lstinline{Tensor}.\r\n\r\nThe following \\lstinline{DType} objects are defined:\r\n\r\n\\begin{itemize}\r\n\\item\r\n  \\lstinline{tf.float32}: 32-bit single-precision floating-point.\r\n\\item\r\n  \\lstinline{tf.float64}: 64-bit double-precision floating-point.\r\n\\item\r\n  \\lstinline{tf.bfloat16}: 16-bit truncated floating-point.\r\n\\item\r\n  \\lstinline{tf.complex64}: 64-bit single-precision complex.\r\n\\item\r\n  \\lstinline{tf.int8}: 8-bit signed integer.\r\n\\item\r\n  \\lstinline{tf.uint8}: 8-bit unsigned integer.\r\n\\item\r\n  \\lstinline{tf.int32}: 32-bit signed integer.\r\n\\item\r\n  \\lstinline{tf.int64}: 64-bit signed integer.\r\n\\item\r\n  \\lstinline{tf.bool}: Boolean.\r\n\\item\r\n  \\lstinline{tf.string}: String.\r\n\\item\r\n  \\lstinline{tf.qint8}: Quantized 8-bit signed integer.\r\n\\item\r\n  \\lstinline{tf.quint8}: Quantized 8-bit unsigned integer.\r\n\\item\r\n  \\lstinline{tf.qint32}: Quantized 32-bit signed integer.\r\n\\end{itemize}\r\n\r\nIn addition, variants of these types with the \\lstinline{_ref} suffix are\r\ndefined for reference-typed tensors.\r\n\r\nThe \\lstinline{tf.as_dtype()} function converts numpy types and string\r\ntype names to a \\lstinline{DType} object.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.DType.is_compatible_with(other)}\r\n}{tf.DType.is_compatible_with(other) }}\\label{tf.dtype.isux5fcompatibleux5fwithother}\r\n\r\nReturns True if the \\lstinline{other} DType will be converted to this\r\nDType.\r\n\r\nThe conversion rules are as follows:\r\n\r\n\\begin{lstlisting}\r\nDType(T)       .is_compatible_with(DType(T))        == True\r\nDType(T)       .is_compatible_with(DType(T).as_ref) == True\r\nDType(T).as_ref.is_compatible_with(DType(T))        == False\r\nDType(T).as_ref.is_compatible_with(DType(T).as_ref) == True\r\n\\end{lstlisting}\r\n\r\n\\subparagraph{Args: }\\label{args-18}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{other}: A \\lstinline{DType} (or object that may be converted to a\r\n  \\lstinline{DType}).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-22}\r\n\r\nTrue if a Tensor of the \\lstinline{other} \\lstinline{DType} will be implicitly\r\nconverted to this \\lstinline{DType}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.DType.name}\r\n}{tf.DType.name }}\\label{tf.dtype.name}\r\n\r\nReturns the string name for this \\lstinline{DType}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.DType.base_dtype}\r\n}{tf.DType.base_dtype }}\\label{tf.dtype.baseux5fdtype}\r\n\r\nReturns a non-reference \\lstinline{DType} based on this \\lstinline{DType}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.DType.is_ref_dtype}\r\n}{tf.DType.is_ref_dtype }}\\label{tf.dtype.isux5frefux5fdtype}\r\n\r\nReturns \\lstinline{True} if this \\lstinline{DType} represents a reference\r\ntype.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.DType.as_ref}\r\n}{tf.DType.as_ref }}\\label{tf.dtype.asux5fref}\r\n\r\nReturns a reference \\lstinline{DType} based on this \\lstinline{DType}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.DType.is_integer}\r\n}{tf.DType.is_integer }}\\label{tf.dtype.isux5finteger}\r\n\r\nReturns whether this is a (non-quantized) integer type.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.DType.is_quantized}\r\n}{tf.DType.is_quantized }}\\label{tf.dtype.isux5fquantized}\r\n\r\nReturns whether this is a quantized data type.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.DType.as_numpy_dtype}\r\n}{tf.DType.as_numpy_dtype }}\\label{tf.dtype.asux5fnumpyux5fdtype}\r\n\r\nReturns a \\lstinline{numpy.dtype} based on this \\lstinline{DType}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.DType.as_datatype_enum}\r\n}{tf.DType.as_datatype_enum }}\\label{tf.dtype.asux5fdatatypeux5fenum}\r\n\r\nReturns a \\lstinline{types_pb2.DataType} enum value based on this\r\n\\lstinline{DType}.\r\n\r\n\\paragraph{Other Methods }\\label{other-methods-2}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.DType.__init__(type_enum)}\r\n}{tf.DType.__init__(type_enum) }}\\label{tf.dtype.ux5fux5finitux5fux5ftypeux5fenum}\r\n\r\nCreates a new \\lstinline{DataType}.\r\n\r\nNOTE(mrry): In normal circumstances, you should not need to construct a\r\nDataType object directly. Instead, use the types.as_dtype() function.\r\n\r\n\\subparagraph{Args: }\\label{args-19}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{type_enum}: A \\lstinline{types_pb2.DataType} enum value.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-10}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{type_enum} is not a value\r\n  \\lstinline{types_pb2.DataType}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.DType.max}\r\n}{tf.DType.max }}\\label{tf.dtype.max}\r\n\r\nReturns the maximum representable value in this data type.\r\n\r\n\\subparagraph{Raises: }\\label{raises-11}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: if this is a non-numeric, unordered, or quantized\r\n  type.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.DType.min}\r\n}{tf.DType.min }}\\label{tf.dtype.min}\r\n\r\nReturns the minimum representable value in this data type.\r\n\r\n\\subparagraph{Raises: }\\label{raises-12}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: if this is a non-numeric, unordered, or quantized\r\n  type.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.as_dtype(type_value)}\r\n}{tf.as_dtype(type_value) }}\\label{tf.asux5fdtypetypeux5fvalue}\r\n\r\nConverts the given \\lstinline{type_value} to a \\lstinline{DType}.\r\n\r\n\\subparagraph{Args: }\\label{args-20}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{type_value}: A value that can be converted to a\r\n  \\lstinline{tf.DType} object. This may currently be a \\lstinline{tf.DType}\r\n  object, a\r\n  \\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/types.proto}{\\lstinline{DataType}\r\n  enum}, a string type name, or a \\lstinline{numpy.dtype}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-23}\r\n\r\nA \\lstinline{DType} corresponding to \\lstinline{type_value}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-13}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{type_value} cannot be converted to a\r\n  \\lstinline{DType}.\r\n\\end{itemize}\r\n\r\n\\subsection{Utility functions }\\label{utility-functions}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.device(dev)}\r\n}{tf.device(dev) }}\\label{tf.devicedev}\r\n\r\nWrapper for \\lstinline{Graph.device()} using the default graph.\r\n\r\nSee\r\n\\href{../../api_docs/python/framework.md\\#Graph.name_scope}{\\lstinline{Graph.name_scope()}}\r\nfor more details.\r\n\r\n\\subparagraph{Args: }\\label{args-21}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{device_name_or_function}: The device name or function to\r\n  use in the context.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-24}\r\n\r\nA context manager that specifies the default device to use for newly\r\ncreated ops.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.name_scope(name)}\r\n}{tf.name_scope(name) }}\\label{tf.nameux5fscopename}\r\n\r\nWrapper for \\lstinline{Graph.name_scope()} using the default graph.\r\n\r\nSee\r\n\\href{../../api_docs/python/framework.md\\#Graph.name_scope}{\\lstinline{Graph.name_scope()}}\r\nfor more details.\r\n\r\n\\subparagraph{Args: }\\label{args-22}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{name}: A name for the scope.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-25}\r\n\r\nA context manager that installs \\lstinline{name} as a new name scope in the\r\ndefault graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.control_dependencies(control_inputs)}\r\n}{tf.control_dependencies(control_inputs) }}\\label{tf.controlux5fdependenciescontrolux5finputs}\r\n\r\nWrapper for \\lstinline{Graph.control_dependencies()} using the default\r\ngraph.\r\n\r\nSee\r\n\\href{../../api_docs/python/framework.md\\#Graph.control_dependencies}{\\lstinline{Graph.control_dependencies()}}\r\nfor more details.\r\n\r\n\\subparagraph{Args: }\\label{args-23}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{control_inputs}: A list of \\lstinline{Operation} or\r\n  \\lstinline{Tensor} objects, which must be executed or computed before\r\n  running the operations defined in the context.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-26}\r\n\r\nA context manager that specifies control dependencies for all operations\r\nconstructed within the context.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.convert_to_tensor(value, dtype=None, name=None)}\r\n}{tf.convert_to_tensor(value, dtype=None, name=None) }}\\label{tf.convertux5ftoux5ftensorvalue-dtypenone-namenone}\r\n\r\nConverts the given \\lstinline{value} to a \\lstinline{Tensor}.\r\n\r\nThis function converts Python objects of various types to\r\n\\lstinline{Tensor} objects. It accepts \\lstinline{Tensor} objects, numpy\r\narrays, Python lists, and Python scalars. For example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ImportTok{import} \\NormalTok{numpy }\\ImportTok{as} \\NormalTok{np}\r\n\\NormalTok{array }\\OperatorTok{=} \\NormalTok{np.random.rand((}\\DecValTok{32}\\NormalTok{, }\\DecValTok{100}\\NormalTok{, }\\DecValTok{100}\\NormalTok{))}\r\n\r\n\\KeywordTok{def} \\NormalTok{my_func(arg):}\r\n  \\NormalTok{arg }\\OperatorTok{=} \\NormalTok{tf.convert_to_tensor(arg, dtype}\\OperatorTok{=}\\NormalTok{tf.float32)}\r\n  \\ControlFlowTok{return} \\NormalTok{tf.matmul(arg, arg) }\\OperatorTok{+} \\NormalTok{arg}\r\n\r\n\\CommentTok{# The following calls are equivalent.}\r\n\\NormalTok{value_1 }\\OperatorTok{=} \\NormalTok{my_func(tf.constant([[}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{2.0}\\NormalTok{], [}\\FloatTok{3.0}\\NormalTok{, }\\FloatTok{4.0}\\NormalTok{]]))}\r\n\\NormalTok{value_2 }\\OperatorTok{=} \\NormalTok{my_func([[}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{2.0}\\NormalTok{], [}\\FloatTok{3.0}\\NormalTok{, }\\FloatTok{4.0}\\NormalTok{]])}\r\n\\NormalTok{value_3 }\\OperatorTok{=} \\NormalTok{my_func(np.array([[}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{2.0}\\NormalTok{], [}\\FloatTok{3.0}\\NormalTok{, }\\FloatTok{4.0}\\NormalTok{]], dtype}\\OperatorTok{=}\\NormalTok{np.float32))}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nThis function can be useful when composing a new operation in Python\r\n(such as \\lstinline{my_func} in the example above). All standard Python op\r\nconstructors apply this function to each of their Tensor-valued inputs,\r\nwhich allows those ops to accept numpy arrays, Python lists, and scalars\r\nin addition to \\lstinline{Tensor} objects.\r\n\r\n\\subparagraph{Args: }\\label{args-24}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{value}: An object whose type has a registered \\lstinline{Tensor}\r\n  conversion function.\r\n\\item\r\n  \\lstinline{dtype}: Optional element type for the returned tensor. If\r\n  missing, the type is inferred from the type of \\lstinline{value}.\r\n\\item\r\n  \\lstinline{name}: Optional name to use if a new \\lstinline{Tensor} is\r\n  created.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-27}\r\n\r\nA \\lstinline{Tensor} based on \\lstinline{value}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-14}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If no conversion function is registered for\r\n  \\lstinline{value}.\r\n\\item\r\n  \\lstinline{RuntimeError}: If a registered conversion function returns an\r\n  invalid value.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.get_default_graph()}\r\n}{tf.get_default_graph() }}\\label{tf.getux5fdefaultux5fgraph}\r\n\r\nReturns the default graph for the current thread.\r\n\r\nThe returned graph will be the innermost graph on which a\r\n\\lstinline{Graph.as_default()} context has been entered, or a global\r\ndefault graph if none has been explicitly created.\r\n\r\n\\emph{N.B.} The default graph is a property of the current thread. If\r\nyou create a new thread, and wish to use the default graph in that\r\nthread, you must explicitly add a \\lstinline{with g.as_default():} in\r\nthat thread's function.\r\n\r\n\\subparagraph{Returns: }\\label{returns-28}\r\n\r\nThe default \\lstinline{Graph} being used in the current thread.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.import_graph_def(graph_def, input_map=None, return_elements=None, name=None, op_dict=None)}\r\n}{tf.import_graph_def(graph_def, input_map=None, return_elements=None, name=None, op_dict=None) }}\\label{tf.importux5fgraphux5fdefgraphux5fdef-inputux5fmapnone-returnux5felementsnone-namenone-opux5fdictnone}\r\n\r\nImports the TensorFlow graph in \\lstinline{graph_def} into the Python\r\n\\lstinline{Graph}.\r\n\r\nThis function provides a way to import a serialized TensorFlow\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/graph.proto}{\\lstinline{GraphDef}}\r\nprotocol buffer, and extract individual objects in the \\lstinline{GraphDef}\r\nas \\protect\\hyperlink{Tensor}{\\lstinline{Tensor}} and\r\n\\protect\\hyperlink{Operation}{\\lstinline{Operation}} objects. See\r\n\\protect\\hyperlink{Graph.asux5fgraphux5fdef}{\\lstinline{Graph.as_graph_def()}}\r\nfor a way to create a \\lstinline{GraphDef} proto.\r\n\r\n\\subparagraph{Args: }\\label{args-25}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{graph_def}: A \\lstinline{GraphDef} proto containing operations\r\n  to be imported into the default graph.\r\n\\item\r\n  \\lstinline{input_map}: A dictionary mapping input names (as strings) in\r\n  \\lstinline{graph_def} to \\lstinline{Tensor} objects. The values of the\r\n  named input tensors in the imported graph will be re-mapped to the\r\n  respective \\lstinline{Tensor} values.\r\n\\item\r\n  \\lstinline{return_elements}: A list of strings containing operation\r\n  names in \\lstinline{graph_def} that will be returned as\r\n  \\lstinline{Operation} objects; and/or tensor names in \\lstinline{graph_def}\r\n  that will be returned as \\lstinline{Tensor} objects.\r\n\\item\r\n  \\lstinline{name}: (Optional.) A prefix that will be prepended to the\r\n  names in \\lstinline{graph_def}. Defaults to \\lstinline{\"import\"}.\r\n\\item\r\n  \\lstinline{op_dict}: (Optional.) A dictionary mapping op type names to\r\n  \\lstinline{OpDef} protos. Must contain an \\lstinline{OpDef} proto for each\r\n  op type named in \\lstinline{graph_def}. If omitted, uses the\r\n  \\lstinline{OpDef} protos registered in the global registry.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-29}\r\n\r\nA list of \\lstinline{Operation} and/or \\lstinline{Tensor} objects from the\r\nimported graph, corresponding to the names in `return_elements'.\r\n\r\n\\subparagraph{Raises: }\\label{raises-15}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{graph_def} is not a \\lstinline{GraphDef}\r\n  proto,\r\n  \\lstinline{input_map\\textquotesingle{} is not a dictionary mapping strings to}Tensor\\lstinline{objects, or}return_elements`\r\n  is not a list of strings.\r\n\\item\r\n  \\lstinline{ValueError}: If \\lstinline{input_map}, or\r\n  \\lstinline{return_elements} contains names that do not appear in\r\n  \\lstinline{graph_def}, or \\lstinline{graph_def} is not well-formed\r\n  (e.g.~it refers to an unknown tensor).\r\n\\end{itemize}\r\n\r\n\\subsection{Graph collections }\\label{graph-collections}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.add_to_collection(name, value)}\r\n}{tf.add_to_collection(name, value) }}\\label{tf.addux5ftoux5fcollectionname-value}\r\n\r\nWrapper for \\lstinline{Graph.add_to_collection()} using the default\r\ngraph.\r\n\r\nSee\r\n\\href{../../api_docs/python/framework.md\\#Graph.add_to_collection}{\\lstinline{Graph.add_to_collection()}}\r\nfor more details.\r\n\r\n\\subparagraph{Args: }\\label{args-26}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{name}: The key for the collection. For example, the\r\n  \\lstinline{GraphKeys} class contains many standard names for collections.\r\n\\item\r\n  \\lstinline{value}: The value to add to the collection.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.get_collection(key, scope=None)}\r\n}{tf.get_collection(key, scope=None) }}\\label{tf.getux5fcollectionkey-scopenone}\r\n\r\nWrapper for \\lstinline{Graph.get_collection()} using the default graph.\r\n\r\nSee\r\n\\href{../../api_docs/python/framework.md\\#Graph.get_collection}{\\lstinline{Graph.get_collection()}}\r\nfor more details.\r\n\r\n\\subparagraph{Args: }\\label{args-27}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{key}: The key for the collection. For example, the\r\n  \\lstinline{GraphKeys} class contains many standard names for collections.\r\n\\item\r\n  \\lstinline{scope}: (Optional.) If supplied, the resulting list is\r\n  filtered to include only items whose name begins with this string.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-30}\r\n\r\nThe list of values in the collection with the given \\lstinline{name}, or an\r\nempty list if no value has been added to that collection. The list\r\ncontains the values in the order under which they were collected.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{class tf.GraphKeys}\r\n}{class tf.GraphKeys }}\\label{class-tf.graphkeys}\r\n\r\nStandard names to use for graph collections.\r\n\r\nThe standard library uses various well-known names to collect and\r\nretrieve values associated with a graph. For example, the\r\n\\lstinline{tf.Optimizer} subclasses default to optimizing the variables\r\ncollected under \\lstinline{tf.GraphKeys.TRAINABLE_VARIABLES} if none is\r\nspecified, but it is also possible to pass an explicit list of\r\nvariables.\r\n\r\nThe following standard keys are defined:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{VARIABLES}: the \\lstinline{Variable} objects that comprise a\r\n  model, and must be saved and restored together. See\r\n  \\href{../../api_docs/python/state_ops.md\\#all_variables}{\\lstinline{tf.all_variables()}}\r\n  for more details.\r\n\\item\r\n  \\lstinline{TRAINABLE_VARIABLES}: the subset of \\lstinline{Variable} objects\r\n  that will be trained by an optimizer. See\r\n  \\href{../../api_docs/python/state_ops.md\\#trainable_variables}{\\lstinline{tf.trainable_variables()}}\r\n  for more details.\r\n\\item\r\n  \\lstinline{SUMMARIES}: the summary \\lstinline{Tensor} objects that have been\r\n  created in the graph. See\r\n  \\href{../../api_docs/python/train.md\\#merge_all_summaries}{\\lstinline{tf.merge_all_summaries()}}\r\n  for more details.\r\n\\item\r\n  \\lstinline{QUEUE_RUNNERS}: the \\lstinline{QueueRunner} objects that are\r\n  used to produce input for a computation. See\r\n  \\href{../../api_docs/python/train.md\\#start_queue_runners}{\\lstinline{tf.start_queue_runners()}}\r\n  for more details.\r\n\\end{itemize}\r\n\r\n\\subsection{Defining new operations }\\label{defining-new-operations}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{class tf.RegisterGradient}\r\n}{class tf.RegisterGradient }}\\label{class-tf.registergradient}\r\n\r\nA decorator for registering the gradient function for an op type.\r\n\r\nThis decorator is only used when defining a new op type. For an op with\r\n\\lstinline{m} inputs and \\lstinline{n} inputs, the gradient function is a\r\nfunction that takes the original \\lstinline{Operation} and \\lstinline{n}\r\n\\lstinline{Tensor} objects (representing the gradients with respect to each\r\noutput of the op), and returns \\lstinline{m} \\lstinline{Tensor} objects\r\n(representing the partial gradients with respect to each input of the\r\nop).\r\n\r\nFor example, assuming that operations of type \\lstinline{\"Sub\"} take two\r\ninputs \\lstinline{x} and \\lstinline{y}, and return a single output\r\n\\lstinline{x - y}, the following gradient function would be registered:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\AttributeTok{@tf.RegisterGradient}\\NormalTok{(}\\StringTok{\"Sub\"}\\NormalTok{)}\r\n\\KeywordTok{def} \\NormalTok{_sub_grad(unused_op, grad):}\r\n  \\ControlFlowTok{return} \\NormalTok{grad, tf.Neg(grad)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nThe decorator argument \\lstinline{op_type} is the string type of an\r\noperation. This corresponds to the \\lstinline{OpDef.name} field for the\r\nproto that defines the operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.RegisterGradient.__init__(op_type)}\r\n}{tf.RegisterGradient.__init__(op_type) }}\\label{tf.registergradient.ux5fux5finitux5fux5fopux5ftype}\r\n\r\nCreates a new decorator with \\lstinline{op_type} as the Operation type.\r\n\r\n\\subparagraph{Args: }\\label{args-28}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{op_type}: The string type of an operation. This corresponds\r\n  to the \\lstinline{OpDef.name} field for the proto that defines the\r\n  operation.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.NoGradient(op_type)}\r\n}{tf.NoGradient(op_type) }}\\label{tf.nogradientopux5ftype}\r\n\r\nSpecifies that ops of type \\lstinline{op_type} do not have a defined\r\ngradient.\r\n\r\nThis function is only used when defining a new op type. It may be used\r\nfor ops such as \\lstinline{tf.size()} that are not differentiable. For\r\nexample:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{tf.NoGradient(}\\StringTok{\"Size\"}\\NormalTok{)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-29}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{op_type}: The string type of an operation. This corresponds\r\n  to the \\lstinline{OpDef.name} field for the proto that defines the\r\n  operation.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-16}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{op_type} is not a string.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{class tf.RegisterShape}\r\n}{class tf.RegisterShape }}\\label{class-tf.registershape}\r\n\r\nA decorator for registering the shape function for an op type.\r\n\r\nThis decorator is only used when defining a new op type. A shape\r\nfunction is a function from an \\lstinline{Operation} object to a list of\r\n\\lstinline{TensorShape} objects, with one \\lstinline{TensorShape} for each\r\noutput of the operation.\r\n\r\nFor example, assuming that operations of type \\lstinline{\"Sub\"} take two\r\ninputs \\lstinline{x} and \\lstinline{y}, and return a single output\r\n\\lstinline{x - y}, all with the same shape, the following shape function\r\nwould be registered:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\AttributeTok{@tf.RegisterShape}\\NormalTok{(}\\StringTok{\"Sub\"}\\NormalTok{)}\r\n\\KeywordTok{def} \\NormalTok{_sub_shape(op):}\r\n  \\ControlFlowTok{return} \\NormalTok{[op.inputs[}\\DecValTok{0}\\NormalTok{].get_shape().merge_with(op.inputs[}\\DecValTok{1}\\NormalTok{].get_shape())]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nThe decorator argument \\lstinline{op_type} is the string type of an\r\noperation. This corresponds to the \\lstinline{OpDef.name} field for the\r\nproto that defines the operation. - - -\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.RegisterShape.__init__(op_type)}\r\n}{tf.RegisterShape.__init__(op_type) }}\\label{tf.registershape.ux5fux5finitux5fux5fopux5ftype}\r\n\r\nSaves the ``op_type'' as the Operation type.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{class tf.TensorShape}\r\n}{class tf.TensorShape }}\\label{class-tf.tensorshape}\r\n\r\nRepresents the shape of a \\lstinline{Tensor}.\r\n\r\nA \\lstinline{TensorShape} represents a possibly-partial shape specification\r\nfor a \\lstinline{Tensor}. It may be one of the following:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\emph{Fully-known shape:} has a known number of dimensions and a known\r\n  size for each dimension.\r\n\\item\r\n  \\emph{Partially-known shape:} has a known number of dimensions, and an\r\n  unknown size for one or more dimension.\r\n\\item\r\n  \\emph{Unknown shape:} has an unknown number of dimensions, and an\r\n  unknown size in all dimensions.\r\n\\end{itemize}\r\n\r\nIf a tensor is produced by an operation of type \\lstinline{\"Foo\"}, its\r\nshape may be inferred if there is a registered shape function for\r\n\\lstinline{\"Foo\"}. See\r\n\\href{../../api_docs/python/framework.md\\#RegisterShape}{\\lstinline{tf.RegisterShape()}}\r\nfor details of shape functions and how to register them. Alternatively,\r\nthe shape may be set explicitly using\r\n\\href{../../api_docs/python/framework.md\\#Tensor.set_shape}{\\lstinline{Tensor.set_shape()}}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.merge_with(other)}\r\n}{tf.TensorShape.merge_with(other) }}\\label{tf.tensorshape.mergeux5fwithother}\r\n\r\nReturns a \\lstinline{TensorShape} combining the information in\r\n\\lstinline{self} and \\lstinline{other}.\r\n\r\nThe dimensions in \\lstinline{self} and \\lstinline{other} are merged\r\nelementwise, according to the rules defined for\r\n\\lstinline{Dimension.merge_with()}.\r\n\r\n\\subparagraph{Args: }\\label{args-30}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{other}: Another \\lstinline{TensorShape}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-31}\r\n\r\nA \\lstinline{TensorShape} containing the combined information of\r\n\\lstinline{self} and \\lstinline{other}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-17}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: If \\lstinline{self} and \\lstinline{other} are not\r\n  compatible.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.concatenate(other)}\r\n}{tf.TensorShape.concatenate(other) }}\\label{tf.tensorshape.concatenateother}\r\n\r\nReturns the concatenation of the dimension in \\lstinline{self} and\r\n\\lstinline{other}.\r\n\r\n\\emph{N.B.} If either \\lstinline{self} or \\lstinline{other} is completely\r\nunknown, concatenation will discard information about the other shape.\r\nIn future, we might support concatenation that preserves this\r\ninformation for use with slicing.\r\n\r\n\\subparagraph{Args: }\\label{args-31}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{other}: Another \\lstinline{TensorShape}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-32}\r\n\r\nA \\lstinline{TensorShape} whose dimensions are the concatenation of the\r\ndimensions in \\lstinline{self} and \\lstinline{other}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.ndims}\r\n}{tf.TensorShape.ndims }}\\label{tf.tensorshape.ndims}\r\n\r\nReturns the rank of this shape, or None if it is unspecified.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.dims}\r\n}{tf.TensorShape.dims }}\\label{tf.tensorshape.dims}\r\n\r\nReturns a list of Dimensions, or None if the shape is unspecified.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.as_list()}\r\n}{tf.TensorShape.as_list() }}\\label{tf.tensorshape.asux5flist}\r\n\r\nReturns a list of integers or None for each dimension.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.is_compatible_with(other)}\r\n}{tf.TensorShape.is_compatible_with(other) }}\\label{tf.tensorshape.isux5fcompatibleux5fwithother}\r\n\r\nReturns True iff \\lstinline{self} is compatible with \\lstinline{other}.\r\n\r\nTwo possibly-partially-defined shapes are compatible if there exists a\r\nfully-defined shape that both shapes can represent. Thus, compatibility\r\nallows the shape inference code to reason about partially-defined\r\nshapes. For example:\r\n\r\n\\begin{itemize}\r\n\\item\r\n  TensorShape(None) is compatible with all shapes.\r\n\\item\r\n  TensorShape({[}None, None{]}) is compatible with all two-dimensional\r\n  shapes, such as TensorShape({[}32, 784{]}), and also\r\n  TensorShape(None). It is not compatible with, for example,\r\n  TensorShape({[}None{]}) or TensorShape({[}None, None, None{]}).\r\n\\item\r\n  TensorShape({[}32, None{]}) is compatible with all two-dimensional\r\n  shapes with size 32 in the 0th dimension, and also\r\n  TensorShape({[}None, None{]}) and TensorShape(None). It is not\r\n  compatible with, for example, TensorShape({[}32{]}),\r\n  TensorShape({[}32, None, 1{]}) or TensorShape({[}64, None{]}).\r\n\\item\r\n  TensorShape({[}32, 784{]}) is compatible with itself, and also\r\n  TensorShape({[}32, None{]}), TensorShape({[}None, 784{]}),\r\n  TensorShape({[}None, None{]}) and TensorShape(None). It is not\r\n  compatible with, for example, TensorShape({[}32, 1, 784{]}) or\r\n  TensorShape({[}None{]}).\r\n\\end{itemize}\r\n\r\nThe compatibility relation is reflexive and symmetric, but not\r\ntransitive. For example, TensorShape({[}32, 784{]}) is compatible with\r\nTensorShape(None), and TensorShape(None) is compatible with\r\nTensorShape({[}4, 4{]}), but TensorShape({[}32, 784{]}) is not\r\ncompatible with TensorShape({[}4, 4{]}).\r\n\r\n\\subparagraph{Args: }\\label{args-32}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{other}: Another TensorShape.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-33}\r\n\r\nTrue iff \\lstinline{self} is compatible with \\lstinline{other}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.is_fully_defined()}\r\n}{tf.TensorShape.is_fully_defined() }}\\label{tf.tensorshape.isux5ffullyux5fdefined}\r\n\r\nReturns True iff \\lstinline{self} is fully defined in every dimension.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.with_rank(rank)}\r\n}{tf.TensorShape.with_rank(rank) }}\\label{tf.tensorshape.withux5frankrank}\r\n\r\nReturns a shape based on \\lstinline{self} with the given rank.\r\n\r\nThis method promotes a completely unknown shape to one with a known\r\nrank.\r\n\r\n\\subparagraph{Args: }\\label{args-33}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{rank}: An integer.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-34}\r\n\r\nA shape that is at least as specific as \\lstinline{self} with the given\r\nrank.\r\n\r\n\\subparagraph{Raises: }\\label{raises-18}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: If \\lstinline{self} does not represent a shape with\r\n  the given \\lstinline{rank}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.with_rank_at_least(rank)}\r\n}{tf.TensorShape.with_rank_at_least(rank) }}\\label{tf.tensorshape.withux5frankux5fatux5fleastrank}\r\n\r\nReturns a shape based on \\lstinline{self} with at least the given rank.\r\n\r\n\\subparagraph{Args: }\\label{args-34}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{rank}: An integer.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-35}\r\n\r\nA shape that is at least as specific as \\lstinline{self} with at least the\r\ngiven rank.\r\n\r\n\\subparagraph{Raises: }\\label{raises-19}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: If \\lstinline{self} does not represent a shape with\r\n  at least the given \\lstinline{rank}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.with_rank_at_most(rank)}\r\n}{tf.TensorShape.with_rank_at_most(rank) }}\\label{tf.tensorshape.withux5frankux5fatux5fmostrank}\r\n\r\nReturns a shape based on \\lstinline{self} with at most the given rank.\r\n\r\n\\subparagraph{Args: }\\label{args-35}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{rank}: An integer.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-36}\r\n\r\nA shape that is at least as specific as \\lstinline{self} with at most the\r\ngiven rank.\r\n\r\n\\subparagraph{Raises: }\\label{raises-20}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: If \\lstinline{self} does not represent a shape with\r\n  at most the given \\lstinline{rank}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.assert_has_rank(rank)}\r\n}{tf.TensorShape.assert_has_rank(rank) }}\\label{tf.tensorshape.assertux5fhasux5frankrank}\r\n\r\nRaises an exception if \\lstinline{self} is not compatible with the given\r\n\\lstinline{rank}.\r\n\r\n\\subparagraph{Args: }\\label{args-36}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{rank}: An integer.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-21}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: If \\lstinline{self} does not represent a shape with\r\n  the given \\lstinline{rank}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.assert_same_rank(other)}\r\n}{tf.TensorShape.assert_same_rank(other) }}\\label{tf.tensorshape.assertux5fsameux5frankother}\r\n\r\nRaises an exception if \\lstinline{self} and \\lstinline{other} do not have\r\ncompatible ranks.\r\n\r\n\\subparagraph{Args: }\\label{args-37}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{other}: Another \\lstinline{TensorShape}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-22}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: If \\lstinline{self} and \\lstinline{other} do not\r\n  represent shapes with the same rank.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.assert_is_compatible_with(other)}\r\n}{tf.TensorShape.assert_is_compatible_with(other) }}\\label{tf.tensorshape.assertux5fisux5fcompatibleux5fwithother}\r\n\r\nRaises exception if \\lstinline{self} and \\lstinline{other} do not represent\r\nthe same shape.\r\n\r\nThis method can be used to assert that there exists a shape that both\r\n\\lstinline{self} and \\lstinline{other} represent.\r\n\r\n\\subparagraph{Args: }\\label{args-38}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{other}: Another TensorShape.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-23}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: If \\lstinline{self} and \\lstinline{other} do not\r\n  represent the same shape.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.assert_is_fully_defined()}\r\n}{tf.TensorShape.assert_is_fully_defined() }}\\label{tf.tensorshape.assertux5fisux5ffullyux5fdefined}\r\n\r\nRaises an exception if \\lstinline{self} is not fully defined in every\r\ndimension.\r\n\r\n\\subparagraph{Raises: }\\label{raises-24}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: If \\lstinline{self} does not have a known value for\r\n  every dimension.\r\n\\end{itemize}\r\n\r\n\\paragraph{Other Methods }\\label{other-methods-3}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.__init__(dims)}\r\n}{tf.TensorShape.__init__(dims) }}\\label{tf.tensorshape.ux5fux5finitux5fux5fdims}\r\n\r\nCreates a new TensorShape with the given dimensions.\r\n\r\n\\subparagraph{Args: }\\label{args-39}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{dims}: A list of Dimensions, or None if the shape is\r\n  unspecified.\r\n\\item\r\n  \\lstinline{DEPRECATED}: A single integer is treated as a singleton list.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.as_dimension_list()}\r\n}{tf.TensorShape.as_dimension_list() }}\\label{tf.tensorshape.asux5fdimensionux5flist}\r\n\r\nDEPRECATED: use as_list().\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.TensorShape.num_elements()}\r\n}{tf.TensorShape.num_elements() }}\\label{tf.tensorshape.numux5felements}\r\n\r\nReturns the total number of elements, or none for incomplete shapes.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{class tf.Dimension}\r\n}{class tf.Dimension }}\\label{class-tf.dimension}\r\n\r\nRepresents the value of one dimension in a TensorShape. - - -\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Dimension.__init__(value)}\r\n}{tf.Dimension.__init__(value) }}\\label{tf.dimension.ux5fux5finitux5fux5fvalue}\r\n\r\nCreates a new Dimension with the given value.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Dimension.assert_is_compatible_with(other)}\r\n}{tf.Dimension.assert_is_compatible_with(other) }}\\label{tf.dimension.assertux5fisux5fcompatibleux5fwithother}\r\n\r\nRaises an exception if \\lstinline{other} is not compatible with this\r\nDimension.\r\n\r\n\\subparagraph{Args: }\\label{args-40}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{other}: Another Dimension.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-25}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: If \\lstinline{self} and \\lstinline{other} are not\r\n  compatible (see is_compatible_with).\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Dimension.is_compatible_with(other)}\r\n}{tf.Dimension.is_compatible_with(other) }}\\label{tf.dimension.isux5fcompatibleux5fwithother}\r\n\r\nReturns true if \\lstinline{other} is compatible with this Dimension.\r\n\r\nTwo known Dimensions are compatible if they have the same value. An\r\nunknown Dimension is compatible with all other Dimensions.\r\n\r\n\\subparagraph{Args: }\\label{args-41}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{other}: Another Dimension.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-37}\r\n\r\nTrue if this Dimension and \\lstinline{other} are compatible.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Dimension.merge_with(other)}\r\n}{tf.Dimension.merge_with(other) }}\\label{tf.dimension.mergeux5fwithother}\r\n\r\nReturns a Dimension that combines the information in \\lstinline{self} and\r\n\\lstinline{other}.\r\n\r\nDimensions are combined as follows:\r\n\r\nDimension(n) .merge_with(Dimension(n)) == Dimension(n) Dimension(n)\r\n.merge_with(Dimension(None)) == Dimension(n)\r\nDimension(None).merge_with(Dimension(n)) == Dimension(n)\r\nDimension(None).merge_with(Dimension(None)) == Dimension(None)\r\nDimension(n) .merge_with(Dimension(m)) raises ValueError for n != m\r\n\r\n\\subparagraph{Args: }\\label{args-42}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{other}: Another Dimension.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-38}\r\n\r\nA Dimension containing the combined information of \\lstinline{self} and\r\n\\lstinline{other}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-26}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: If \\lstinline{self} and \\lstinline{other} are not\r\n  compatible (see is_compatible_with).\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Dimension.value}\r\n}{tf.Dimension.value }}\\label{tf.dimension.value}\r\n\r\nThe value of this dimension, or None if it is unknown.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.op_scope(values, name, default_name)}\r\n}{tf.op_scope(values, name, default_name) }}\\label{tf.opux5fscopevalues-name-defaultux5fname}\r\n\r\nReturns a context manager for use when defining a Python op.\r\n\r\nThis context manager validates that the given \\lstinline{values} are from\r\nthe same graph, ensures that that graph is the default graph, and pushes\r\na name scope.\r\n\r\nFor example, to define a new Python op called \\lstinline{my_op}:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\KeywordTok{def} \\NormalTok{my_op(a, b, c, name}\\OperatorTok{=}\\VariableTok{None}\\NormalTok{):}\r\n  \\ControlFlowTok{with} \\NormalTok{tf.op_scope([a, b, c], name, }\\StringTok{\"MyOp\"}\\NormalTok{) }\\ImportTok{as} \\NormalTok{scope:}\r\n    \\NormalTok{a }\\OperatorTok{=} \\NormalTok{tf.convert_to_tensor(a, name}\\OperatorTok{=}\\StringTok{\"a\"}\\NormalTok{)}\r\n    \\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.convert_to_tensor(b, name}\\OperatorTok{=}\\StringTok{\"b\"}\\NormalTok{)}\r\n    \\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.convert_to_tensor(c, name}\\OperatorTok{=}\\StringTok{\"c\"}\\NormalTok{)}\r\n    \\CommentTok{# Define some computation that uses `a`, `b`, and `c`.}\r\n    \\ControlFlowTok{return} \\NormalTok{foo_op(..., name}\\OperatorTok{=}\\NormalTok{scope)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-43}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{values}: The list of \\lstinline{Tensor} arguments that are passed\r\n  to the op function.\r\n\\item\r\n  \\lstinline{name}: The name argument that is passed to the op function.\r\n\\item\r\n  \\lstinline{default_name}: The default name to use if the \\lstinline{name}\r\n  argument is \\lstinline{None}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-39}\r\n\r\nA context manager for use in defining a Python op.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.get_seed(op_seed)}\r\n}{tf.get_seed(op_seed) }}\\label{tf.getux5fseedopux5fseed}\r\n\r\nReturns the local seeds an operation should use given an op-specific\r\nseed.\r\n\r\nGiven operation-specific seed, \\lstinline{op_seed}, this helper function\r\nreturns two seeds derived from graph-level and op-level seeds. Many\r\nrandom operations internally use the two seeds to allow user to change\r\nthe seed globally for a graph, or for only specific operations.\r\n\r\nFor details on how the graph-level seed interacts with op seeds, see\r\n\\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\lstinline{set_random_seed}}.\r\n\r\n\\subparagraph{Args: }\\label{args-44}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{op_seed}: integer.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-40}\r\n\r\nA tuple of two integers that should be used for the local seed of this\r\noperation.\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/python/c4s02_constant_op.tex",
    "content": "\r\n\\section{Constants, Sequences, and Random Values} \\label{const_seq_rnd}\r\n\r\n\\footnote{Note: Functions taking Tensor arguments can also take anything accepted by tf.convert_to_tensor.}\r\n\r\n\\subsection{Contents}\\label{contents}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-constants--sequences--and-random-values}{Constants,\r\nSequences, and Random\r\nValues}}{Constants, Sequences, and Random Values}}\\label{constants-sequences-and-random-values-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-constant-value-tensors}{Constant\r\n  Value Tensors}\r\n\\item\r\n  \\protect\\hyperlink{zeros}{\\texttt{tf.zeros(shape,\\ dtype=tf.float32,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{zerosux5flike}{\\texttt{tf.zeros\\_like(tensor,\\ dtype=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{ones}{\\texttt{tf.ones(shape,\\ dtype=tf.float32,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{onesux5flike}{\\texttt{tf.ones\\_like(tensor,\\ dtype=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{fill}{\\texttt{tf.fill(dims,\\ value,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{constant}{\\texttt{tf.constant(value,\\ dtype=None,\\ shape=None,\\ name=\\textquotesingle{}Const\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-sequences}{Sequences}\r\n\\item\r\n  \\protect\\hyperlink{linspace}{\\texttt{tf.linspace(start,\\ stop,\\ num,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{range}{\\texttt{tf.range(start,\\ limit,\\ delta=1,\\ name=\\textquotesingle{}range\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-random-tensors}{Random Tensors}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-examples-}{Examples:}\r\n\\item\r\n  \\protect\\hyperlink{randomux5fnormal}{\\texttt{tf.random\\_normal(shape,\\ mean=0.0,\\ stddev=1.0,\\ dtype=tf.float32,\\ seed=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{truncatedux5fnormal}{\\texttt{tf.truncated\\_normal(shape,\\ mean=0.0,\\ stddev=1.0,\\ dtype=tf.float32,\\ seed=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{randomux5funiform}{\\texttt{tf.random\\_uniform(shape,\\ minval=0.0,\\ maxval=1.0,\\ dtype=tf.float32,\\ seed=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{randomux5fshuffle}{\\texttt{tf.random\\_shuffle(value,\\ seed=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{setux5frandomux5fseed}{\\texttt{tf.set\\_random\\_seed(seed)}}\r\n\\end{itemize}\r\n\r\n\\subsection{Constant Value Tensors }\\label{constant-value-tensors}\r\n\r\nTensorFlow provides several operations that you can use to generate\r\nconstants.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.zeros(shape,\\ dtype=tf.float32,\\ name=None)}\r\n}{tf.zeros(shape, dtype=tf.float32, name=None) }}\\label{tf.zerosshape-dtypetf.float32-namenone}\r\n\r\nCreates a tensor with all elements set to zero.\r\n\r\nThis operation returns a tensor of type \\texttt{dtype} with shape\r\n\\texttt{shape} and all elements set to zero.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{tf.zeros([}\\DecValTok{3}\\NormalTok{, }\\DecValTok{4}\\NormalTok{], int32) }\\OperatorTok{==>} \\NormalTok{[[}\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{], [}\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{], [}\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{]]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{shape}: Either a list of integers, or a 1-D \\texttt{Tensor} of\r\n  type \\texttt{int32}.\r\n\\item\r\n  \\texttt{dtype}: The type of an element in the resulting\r\n  \\texttt{Tensor}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns}\r\n\r\nA \\texttt{Tensor} with all elements set to zero.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.zeros\\_like(tensor,\\ dtype=None,\\ name=None)}\r\n}{tf.zeros\\_like(tensor, dtype=None, name=None) }}\\label{tf.zerosux5fliketensor-dtypenone-namenone}\r\n\r\nCreates a tensor with all elements set to zero.\r\n\r\nGiven a single tensor (\\texttt{tensor}), this operation returns a tensor\r\nof the same type and shape as \\texttt{tensor} with all elements set to\r\nzero. Optionally, you can use \\texttt{dtype} to specify a new type for\r\nthe returned tensor.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 'tensor' is [[1, 2, 3], [4, 5, 6]]}\r\n\\NormalTok{tf.zeros_like(tensor) }\\OperatorTok{==>} \\NormalTok{[[}\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{], [}\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{]]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-1}\r\n\r\n\\begin{itemize}\r\n\\item\r\n  \\texttt{tensor}: A \\texttt{Tensor}.\r\n\\item\r\n  \\texttt{dtype}: A type for the returned \\texttt{Tensor}. Must be\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int8}, \\texttt{int16},\r\n  \\texttt{int32}, \\texttt{int64}, \\texttt{uint8}, or \\texttt{complex64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-1}\r\n\r\nA \\texttt{Tensor} with all elements set to zero.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.ones(shape,\\ dtype=tf.float32,\\ name=None)}\r\n}{tf.ones(shape, dtype=tf.float32, name=None) }}\\label{tf.onesshape-dtypetf.float32-namenone}\r\n\r\nCreates a tensor with all elements set to 1.\r\n\r\nThis operation returns a tensor of type \\texttt{dtype} with shape\r\n\\texttt{shape} and all elements set to 1.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{tf.ones([}\\DecValTok{2}\\NormalTok{, }\\DecValTok{3}\\NormalTok{], int32) }\\OperatorTok{==>} \\NormalTok{[[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{], [}\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{]]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{shape}: Either a list of integers, or a 1-D \\texttt{Tensor} of\r\n  type \\texttt{int32}.\r\n\\item\r\n  \\texttt{dtype}: The type of an element in the resulting\r\n  \\texttt{Tensor}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-2}\r\n\r\nA \\texttt{Tensor} with all elements set to 1.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.ones\\_like(tensor,\\ dtype=None,\\ name=None)}\r\n}{tf.ones\\_like(tensor, dtype=None, name=None) }}\\label{tf.onesux5fliketensor-dtypenone-namenone}\r\n\r\nCreates a tensor with all elements set to 1.\r\n\r\nGiven a single tensor (\\texttt{tensor}), this operation returns a tensor\r\nof the same type and shape as \\texttt{tensor} with all elements set to\r\n1. Optionally, you can specify a new type (\\texttt{dtype}) for the\r\nreturned tensor.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 'tensor' is [[1, 2, 3], [4, 5, 6]]}\r\n\\NormalTok{tf.ones_like(tensor) }\\OperatorTok{==>} \\NormalTok{[[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{], [}\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{]]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-3}\r\n\r\n\\begin{itemize}\r\n\\item\r\n  \\texttt{tensor}: A \\texttt{Tensor}.\r\n\\item\r\n  \\texttt{dtype}: A type for the returned \\texttt{Tensor}. Must be\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int8}, \\texttt{int16},\r\n  \\texttt{int32}, \\texttt{int64}, \\texttt{uint8}, or \\texttt{complex64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-3}\r\n\r\nA \\texttt{Tensor} with all elements set to 1.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.fill(dims,\\ value,\\ name=None)}\r\n}{tf.fill(dims, value, name=None) }}\\label{tf.filldims-value-namenone}\r\n\r\nCreates a tensor filled with a scalar value.\r\n\r\nThis operation creates a tensor of shape \\texttt{dims} and fills it with\r\n\\texttt{value}.\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\n# output tensor shape needs to be [2, 3]\r\n# so 'dims' is [2, 3]\r\nfill(dims, 9) ==> [[9, 9, 9]\r\n                   [9, 9, 9]]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{dims}: A \\texttt{Tensor} of type \\texttt{int32}. 1-D.\r\n  Represents the shape of the output tensor.\r\n\\item\r\n  \\texttt{value}: A \\texttt{Tensor}. 0-D (scalar). Value to fill the\r\n  returned tensor.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-4}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{value}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.constant(value,\\ dtype=None,\\ shape=None,\\ name=\\textquotesingle{}Const\\textquotesingle{})}\r\n}{tf.constant(value, dtype=None, shape=None, name='Const') }}\\label{tf.constantvalue-dtypenone-shapenone-nameconst}\r\n\r\nCreates a constant tensor.\r\n\r\nThe resulting tensor is populated with values of type \\texttt{dtype}, as\r\nspecified by arguments \\texttt{value} and (optionally) \\texttt{shape}\r\n(see examples below).\r\n\r\nThe argument \\texttt{value} can be a constant value, or a list of values\r\nof type \\texttt{dtype}. If \\texttt{value} is a list, then the length of\r\nthe list must be less than or equal to the number of elements implied by\r\nthe \\texttt{shape} argument (if specified). In the case where the list\r\nlength is less than the number of elements specified by \\texttt{shape},\r\nthe last element in the list will be used to fill the remaining entries.\r\n\r\nThe argument \\texttt{shape} is optional. If present, it specifies the\r\ndimensions of the resulting tensor. If not present, then the tensor is a\r\nscalar (0-D) if \\texttt{value} is a scalar, or 1-D otherwise.\r\n\r\nIf the argument \\texttt{dtype} is not specified, then the type is\r\ninferred from the type of \\texttt{value}.\r\n\r\nFor example:\r\n\r\n```python \\# Constant 1-D Tensor populated with value list. tensor =\r\ntf.constant({[}1, 2, 3, 4, 5, 6, 7{]}) =\\textgreater{} {[}1 2 3 4 5 6\r\n7{]}\r\n\r\n\\# Constant 2-D tensor populated with scalar value -1. tensor =\r\ntf.constant(-1.0, shape={[}2, 3{]}) =\\textgreater{} {[}{[}-1. -1.\r\n-1.{]}{[}-1. -1. -1.{]}{]} ```\r\n\r\n\\subparagraph{Args: }\\label{args-5}\r\n\r\n\\begin{itemize}\r\n\\item\r\n  \\texttt{value}: A constant value (or list) of output type\r\n  \\texttt{dtype}.\r\n\\item\r\n  \\texttt{dtype}: The type of the elements of the resulting tensor.\r\n\\item\r\n  \\texttt{shape}: Optional dimensions of resulting tensor.\r\n\\item\r\n  \\texttt{name}: Optional name for the tensor.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-5}\r\n\r\nA Constant Tensor.\r\n\r\n\\subsection{Sequences }\\label{sequences}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.linspace(start,\\ stop,\\ num,\\ name=None)}\r\n}{tf.linspace(start, stop, num, name=None) }}\\label{tf.linspacestart-stop-num-namenone}\r\n\r\nGenerates values in an interval.\r\n\r\nA sequence of \\texttt{num} evenly-spaced values are generated beginning\r\nat \\texttt{start}. If \\texttt{num\\ \\textgreater{}\\ 1}, the values in the\r\nsequence increase by \\texttt{stop\\ -\\ start\\ /\\ num\\ -\\ 1}, so that the\r\nlast one is exactly \\texttt{stop}.\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\ntf.linspace(10.0, 12.0, 3, name=\"linspace\") => [ 10.0  11.0  12.0]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{start}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}. First entry in the range.\r\n\\item\r\n  \\texttt{stop}: A \\texttt{Tensor}. Must have the same type as\r\n  \\texttt{start}. Last entry in the range.\r\n\\item\r\n  \\texttt{num}: A \\texttt{Tensor} of type \\texttt{int32}. Number of\r\n  values to generate.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-6}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{start}. 1-D. The\r\ngenerated values.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.range(start,\\ limit,\\ delta=1,\\ name=\\textquotesingle{}range\\textquotesingle{})}\r\n}{tf.range(start, limit, delta=1, name='range') }}\\label{tf.rangestart-limit-delta1-namerange}\r\n\r\nCreates a sequence of integers.\r\n\r\nThis operation creates a sequence of integers that begins at\r\n\\texttt{start} and extends by increments of \\texttt{delta} up to but not\r\nincluding \\texttt{limit}.\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\n# 'start' is 3\r\n# 'limit' is 18\r\n# 'delta' is 3\r\ntf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-7}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{start}: A 0-D (scalar) of type \\texttt{int32}. First entry in\r\n  sequence.\r\n\\item\r\n  \\texttt{limit}: A 0-D (scalar) of type \\texttt{int32}. Upper limit of\r\n  sequence, exclusive.\r\n\\item\r\n  \\texttt{delta}: A 0-D \\texttt{Tensor} (scalar) of type \\texttt{int32}.\r\n  Optional. Default is 1. Number that increments \\texttt{start}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-7}\r\n\r\nAn 1-D \\texttt{int32} \\texttt{Tensor}.\r\n\r\n\\subsection{Random Tensors }\\label{random-tensors}\r\n\r\nTensorFlow has several ops that create random tensors with different\r\ndistributions. The random ops are stateful, and create new random values\r\neach time they are evaluated.\r\n\r\nThe \\texttt{seed} keyword argument in these functions acts in\r\nconjunction with the graph-level random seed. Changing either the\r\ngraph-level seed using\r\n\\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\texttt{set\\_random\\_seed}}\r\nor the op-level seed will change the underlying seed of these\r\noperations. Setting neither graph-level nor op-level seed, results in a\r\nrandom seed for all operations. See\r\n\\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\texttt{set\\_random\\_seed}}\r\nfor details on the interaction between operation-level and graph-level\r\nrandom seeds.\r\n\r\n\\subsubsection{Examples: }\\label{examples}\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Create a tensor of shape [2, 3] consisting of random normal values, with mean}\r\n\\CommentTok{# -1 and standard deviation 4.}\r\n\\NormalTok{norm }\\OperatorTok{=} \\NormalTok{tf.random_normal([}\\DecValTok{2}\\NormalTok{, }\\DecValTok{3}\\NormalTok{], mean}\\OperatorTok{=-}\\DecValTok{1}\\NormalTok{, stddev}\\OperatorTok{=}\\DecValTok{4}\\NormalTok{)}\r\n\r\n\\CommentTok{# Shuffle the first dimension of a tensor}\r\n\\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.constant([[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{2}\\NormalTok{], [}\\DecValTok{3}\\NormalTok{, }\\DecValTok{4}\\NormalTok{], [}\\DecValTok{5}\\NormalTok{, }\\DecValTok{6}\\NormalTok{]])}\r\n\\NormalTok{shuff }\\OperatorTok{=} \\NormalTok{tf.random_shuffle(c)}\r\n\r\n\\CommentTok{# Each time we run these ops, different results are generated}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session()}\r\n\\BuiltInTok{print} \\NormalTok{sess.run(norm)}\r\n\\BuiltInTok{print} \\NormalTok{sess.run(norm)}\r\n\r\n\\CommentTok{# Set an op-level seed to generate repeatable sequences across sessions.}\r\n\\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.constant([[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{2}\\NormalTok{], [}\\DecValTok{3}\\NormalTok{, }\\DecValTok{4}\\NormalTok{], [}\\DecValTok{5}\\NormalTok{, }\\DecValTok{6}\\NormalTok{]])}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session()}\r\n\\NormalTok{norm }\\OperatorTok{=} \\NormalTok{tf.random_normal(c, seed}\\OperatorTok{=}\\DecValTok{1234}\\NormalTok{)}\r\n\\BuiltInTok{print} \\NormalTok{sess.run(norm)}\r\n\\BuiltInTok{print} \\NormalTok{sess.run(norm)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nAnother common use of random values is the intialization of variables.\r\nAlso see the \\href{../../how_tos/variables/index.md}{Variables How To}.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Use random uniform values in [0, 1) as the initializer for a variable of shape}\r\n\\CommentTok{# [2, 3]. The default type is float32.}\r\n\\NormalTok{var }\\OperatorTok{=} \\NormalTok{tf.Variable(tf.random_uniform([}\\DecValTok{2}\\NormalTok{, }\\DecValTok{3}\\NormalTok{]), name}\\OperatorTok{=}\\StringTok{\"var\"}\\NormalTok{)}\r\n\\NormalTok{init }\\OperatorTok{=} \\NormalTok{tf.initialize_all_variables()}\r\n\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session()}\r\n\\NormalTok{sess.run(init)}\r\n\\BuiltInTok{print} \\NormalTok{sess.run(var)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.random\\_normal(shape,\\ mean=0.0,\\ stddev=1.0,\\ dtype=tf.float32,\\ seed=None,\\ name=None)}\r\n}{tf.random\\_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None) }}\\label{tf.randomux5fnormalshape-mean0.0-stddev1.0-dtypetf.float32-seednone-namenone}\r\n\r\nOutputs random values from a normal distribution.\r\n\r\n\\subparagraph{Args: }\\label{args-8}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{shape}: A 1-D integer Tensor or Python array. The shape of the\r\n  output tensor.\r\n\\item\r\n  \\texttt{mean}: A 0-D Tensor or Python value of type \\texttt{dtype}.\r\n  The mean of the normal distribution.\r\n\\item\r\n  \\texttt{stddev}: A 0-D Tensor or Python value of type \\texttt{dtype}.\r\n  The standard deviation of the normal distribution.\r\n\\item\r\n  \\texttt{dtype}: The type of the output.\r\n\\item\r\n  \\texttt{seed}: A Python integer. Used to create a random seed for the\r\n  distribution. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\texttt{set\\_random\\_seed}}\r\n  for behavior.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-8}\r\n\r\nA tensor of the specified shape filled with random normal values.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.truncated\\_normal(shape,\\ mean=0.0,\\ stddev=1.0,\\ dtype=tf.float32,\\ seed=None,\\ name=None)}\r\n}{tf.truncated\\_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None) }}\\label{tf.truncatedux5fnormalshape-mean0.0-stddev1.0-dtypetf.float32-seednone-namenone}\r\n\r\nOutputs random values from a truncated normal distribution.\r\n\r\nThe generated values follow a normal distribution with specified mean\r\nand standard deviation, except that values whose magnitude is more than\r\n2 standard deviations from the mean are dropped and re-picked.\r\n\r\n\\subparagraph{Args: }\\label{args-9}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{shape}: A 1-D integer Tensor or Python array. The shape of the\r\n  output tensor.\r\n\\item\r\n  \\texttt{mean}: A 0-D Tensor or Python value of type \\texttt{dtype}.\r\n  The mean of the truncated normal distribution.\r\n\\item\r\n  \\texttt{stddev}: A 0-D Tensor or Python value of type \\texttt{dtype}.\r\n  The standard deviation of the truncated normal distribution.\r\n\\item\r\n  \\texttt{dtype}: The type of the output.\r\n\\item\r\n  \\texttt{seed}: A Python integer. Used to create a random seed for the\r\n  distribution. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\texttt{set\\_random\\_seed}}\r\n  for behavior.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-9}\r\n\r\nA tensor of the specified shape filled with random truncated normal\r\nvalues.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.random\\_uniform(shape,\\ minval=0.0,\\ maxval=1.0,\\ dtype=tf.float32,\\ seed=None,\\ name=None)}\r\n}{tf.random\\_uniform(shape, minval=0.0, maxval=1.0, dtype=tf.float32, seed=None, name=None) }}\\label{tf.randomux5funiformshape-minval0.0-maxval1.0-dtypetf.float32-seednone-namenone}\r\n\r\nOutputs random values from a uniform distribution.\r\n\r\nThe generated values follow a uniform distribution in the range\r\n\\texttt{{[}minval,\\ maxval)}. The lower bound \\texttt{minval} is\r\nincluded in the range, while the upper bound \\texttt{maxval} is\r\nexcluded.\r\n\r\n\\subparagraph{Args: }\\label{args-10}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{shape}: A 1-D integer Tensor or Python array. The shape of the\r\n  output tensor.\r\n\\item\r\n  \\texttt{minval}: A 0-D Tensor or Python value of type \\texttt{dtype}.\r\n  The lower bound on the range of random values to generate.\r\n\\item\r\n  \\texttt{maxval}: A 0-D Tensor or Python value of type \\texttt{dtype}.\r\n  The upper bound on the range of random values to generate.\r\n\\item\r\n  \\texttt{dtype}: The type of the output.\r\n\\item\r\n  \\texttt{seed}: A Python integer. Used to create a random seed for the\r\n  distribution. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\texttt{set\\_random\\_seed}}\r\n  for behavior.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-10}\r\n\r\nA tensor of the specified shape filled with random uniform values.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.random\\_shuffle(value,\\ seed=None,\\ name=None)}\r\n}{tf.random\\_shuffle(value, seed=None, name=None) }}\\label{tf.randomux5fshufflevalue-seednone-namenone}\r\n\r\nRandomly shuffles a tensor along its first dimension.\r\n\r\nThe tensor is shuffled along dimension 0, such that each\r\n\\texttt{value{[}j{]}} is mapped to one and only one\r\n\\texttt{output{[}i{]}}. For example, a mapping that might occur for a\r\n3x2 tensor is:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{[[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{2}\\NormalTok{],       [[}\\DecValTok{5}\\NormalTok{, }\\DecValTok{6}\\NormalTok{],}\r\n \\NormalTok{[}\\DecValTok{3}\\NormalTok{, }\\DecValTok{4}\\NormalTok{],  }\\OperatorTok{==>}   \\NormalTok{[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{2}\\NormalTok{],}\r\n \\NormalTok{[}\\DecValTok{5}\\NormalTok{, }\\DecValTok{6}\\NormalTok{]]        [}\\DecValTok{3}\\NormalTok{, }\\DecValTok{4}\\NormalTok{]]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-11}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{value}: A Tensor to be shuffled.\r\n\\item\r\n  \\texttt{seed}: A Python integer. Used to create a random seed for the\r\n  distribution. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\texttt{set\\_random\\_seed}}\r\n  for behavior.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-11}\r\n\r\nA tensor of same shape and type as \\texttt{value}, shuffled along its\r\nfirst dimension.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.set\\_random\\_seed(seed)}\r\n}{tf.set\\_random\\_seed(seed) }}\\label{tf.setux5frandomux5fseedseed}\r\n\r\nSets the graph-level random seed.\r\n\r\nOperations that rely on a random seed actually derive it from two seeds:\r\nthe graph-level and operation-level seeds. This sets the graph-level\r\nseed.\r\n\r\nIts interactions with operation-level seeds is as follows:\r\n\r\n\\begin{enumerate}\r\n\\def\\labelenumi{\\arabic{enumi}.}\r\n\\tightlist\r\n\\item\r\n  If neither the graph-level nor the operation seed is set: A random\r\n  seed is used for this op.\r\n\\item\r\n  If the graph-level seed is set, but the operation seed is not: The\r\n  system deterministically picks an operation seed in conjunction with\r\n  the graph-level seed so that it gets a unique random sequence.\r\n\\item\r\n  If the graph-level seed is not set, but the operation seed is set: A\r\n  default graph-level seed and the specified operation seed are used to\r\n  determine the random sequence.\r\n\\item\r\n  If both the graph-level and the operation seed are set: Both seeds are\r\n  used in conjunction to determine the random sequence.\r\n\\end{enumerate}\r\n\r\nTo illustrate the user-visible effects, consider these examples:\r\n\r\nTo generate different sequences across sessions, set neither graph-level\r\nnor op-level seeds:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{a }\\OperatorTok{=} \\NormalTok{tf.random_uniform([}\\DecValTok{1}\\NormalTok{])}\r\n\\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.random_normal([}\\DecValTok{1}\\NormalTok{])}\r\n\r\n\\BuiltInTok{print} \\StringTok{\"Session 1\"}\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session() }\\ImportTok{as} \\NormalTok{sess1:}\r\n  \\BuiltInTok{print} \\NormalTok{sess1.run(a)  }\\CommentTok{# generates 'A1'}\r\n  \\BuiltInTok{print} \\NormalTok{sess1.run(a)  }\\CommentTok{# generates 'A2'}\r\n  \\BuiltInTok{print} \\NormalTok{sess1.run(b)  }\\CommentTok{# generates 'B1'}\r\n  \\BuiltInTok{print} \\NormalTok{sess1.run(b)  }\\CommentTok{# generates 'B2'}\r\n\r\n\\BuiltInTok{print} \\StringTok{\"Session 2\"}\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session() }\\ImportTok{as} \\NormalTok{sess2:}\r\n  \\BuiltInTok{print} \\NormalTok{sess2.run(a)  }\\CommentTok{# generates 'A3'}\r\n  \\BuiltInTok{print} \\NormalTok{sess2.run(a)  }\\CommentTok{# generates 'A4'}\r\n  \\BuiltInTok{print} \\NormalTok{sess2.run(b)  }\\CommentTok{# generates 'B3'}\r\n  \\BuiltInTok{print} \\NormalTok{sess2.run(b)  }\\CommentTok{# generates 'B4'}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nTo generate the same repeatable sequence for an op across sessions, set\r\nthe seed for the op:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{a }\\OperatorTok{=} \\NormalTok{tf.random_uniform([}\\DecValTok{1}\\NormalTok{], seed}\\OperatorTok{=}\\DecValTok{1}\\NormalTok{)}\r\n\\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.random_normal([}\\DecValTok{1}\\NormalTok{])}\r\n\r\n\\CommentTok{# Repeatedly running this block with the same graph will generate the same}\r\n\\CommentTok{# sequence of values for 'a', but different sequences of values for 'b'.}\r\n\\BuiltInTok{print} \\StringTok{\"Session 1\"}\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session() }\\ImportTok{as} \\NormalTok{sess1:}\r\n  \\BuiltInTok{print} \\NormalTok{sess1.run(a)  }\\CommentTok{# generates 'A1'}\r\n  \\BuiltInTok{print} \\NormalTok{sess1.run(a)  }\\CommentTok{# generates 'A2'}\r\n  \\BuiltInTok{print} \\NormalTok{sess1.run(b)  }\\CommentTok{# generates 'B1'}\r\n  \\BuiltInTok{print} \\NormalTok{sess1.run(b)  }\\CommentTok{# generates 'B2'}\r\n\r\n\\BuiltInTok{print} \\StringTok{\"Session 2\"}\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session() }\\ImportTok{as} \\NormalTok{sess2:}\r\n  \\BuiltInTok{print} \\NormalTok{sess2.run(a)  }\\CommentTok{# generates 'A1'}\r\n  \\BuiltInTok{print} \\NormalTok{sess2.run(a)  }\\CommentTok{# generates 'A2'}\r\n  \\BuiltInTok{print} \\NormalTok{sess2.run(b)  }\\CommentTok{# generates 'B3'}\r\n  \\BuiltInTok{print} \\NormalTok{sess2.run(b)  }\\CommentTok{# generates 'B4'}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nTo make the random sequences generated by all ops be repeatable across\r\nsessions, set a graph-level seed:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{tf.set_random_seed(}\\DecValTok{1234}\\NormalTok{)}\r\n\\NormalTok{a }\\OperatorTok{=} \\NormalTok{tf.random_uniform([}\\DecValTok{1}\\NormalTok{])}\r\n\\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.random_normal([}\\DecValTok{1}\\NormalTok{])}\r\n\r\n\\CommentTok{# Repeatedly running this block with the same graph will generate different}\r\n\\CommentTok{# sequences of 'a' and 'b'.}\r\n\\BuiltInTok{print} \\StringTok{\"Session 1\"}\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session() }\\ImportTok{as} \\NormalTok{sess1:}\r\n  \\BuiltInTok{print} \\NormalTok{sess1.run(a)  }\\CommentTok{# generates 'A1'}\r\n  \\BuiltInTok{print} \\NormalTok{sess1.run(a)  }\\CommentTok{# generates 'A2'}\r\n  \\BuiltInTok{print} \\NormalTok{sess1.run(b)  }\\CommentTok{# generates 'B1'}\r\n  \\BuiltInTok{print} \\NormalTok{sess1.run(b)  }\\CommentTok{# generates 'B2'}\r\n\r\n\\BuiltInTok{print} \\StringTok{\"Session 2\"}\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session() }\\ImportTok{as} \\NormalTok{sess2:}\r\n  \\BuiltInTok{print} \\NormalTok{sess2.run(a)  }\\CommentTok{# generates 'A1'}\r\n  \\BuiltInTok{print} \\NormalTok{sess2.run(a)  }\\CommentTok{# generates 'A2'}\r\n  \\BuiltInTok{print} \\NormalTok{sess2.run(b)  }\\CommentTok{# generates 'B1'}\r\n  \\BuiltInTok{print} \\NormalTok{sess2.run(b)  }\\CommentTok{# generates 'B2'}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-12}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{seed}: integer.\r\n\\end{itemize}\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/python/c4s03_state_ops.tex",
    "content": "\r\n\r\n\\section{Variables }\\label{variables}\r\n\r\nNote: Functions taking \\lstinline{Tensor} arguments can also take anything\r\naccepted by\r\n\\href{../../api_docs/python/framework.md\\#convert_to_tensor}{\\lstinline{tf.convert\\_to\\_tensor}}.\r\n\r\n\\subsection{Contents}\\label{contents}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-variables}{Variables}}{Variables}}\\label{variables-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-variables}{Variables}\r\n\\item\r\n  \\protect\\hyperlink{Variable}{\\lstinline{class\\ tf.Variable}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-variable-helper-functions}{Variable\r\n  helper functions}\r\n\\item\r\n  \\protect\\hyperlink{allux5fvariables}{\\lstinline{tf.all\\_variables()}}\r\n\\item\r\n  \\protect\\hyperlink{trainableux5fvariables}{\\lstinline{tf.trainable\\_variables()}}\r\n\\item\r\n  \\protect\\hyperlink{initializeux5fallux5fvariables}{\\lstinline{tf.initialize\\_all\\_variables()}}\r\n\\item\r\n  \\protect\\hyperlink{initializeux5fvariables}{\\lstinline{tf.initialize\\_variables(var\\_list,\\ name=\\textquotesingle{}init\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{assertux5fvariablesux5finitialized}{\\lstinline{tf.assert\\_variables\\_initialized(var\\_list=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-saving-and-restoring-variables}{Saving\r\n  and Restoring Variables}\r\n\\item\r\n  \\protect\\hyperlink{Saver}{\\lstinline{class\\ tf.train.Saver}}\r\n\\item\r\n  \\protect\\hyperlink{latestux5fcheckpoint}{\\lstinline{tf.train.latest\\_checkpoint(checkpoint\\_dir,\\ latest\\_filename=None)}}\r\n\\item\r\n  \\protect\\hyperlink{getux5fcheckpointux5fstate}{\\lstinline{tf.train.get\\_checkpoint\\_state(checkpoint\\_dir,\\ latest\\_filename=None)}}\r\n\\item\r\n  \\protect\\hyperlink{updateux5fcheckpointux5fstate}{\\lstinline{tf.train.update\\_checkpoint\\_state(save\\_dir,\\ model\\_checkpoint\\_path,\\ all\\_model\\_checkpoint\\_paths=None,\\ latest\\_filename=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-sharing-variables}{Sharing Variables}\r\n\\item\r\n  \\protect\\hyperlink{getux5fvariable}{\\lstinline{tf.get\\_variable(name,\\ shape=None,\\ dtype=tf.float32,\\ initializer=None,\\ trainable=True,\\ collections=None)}}\r\n\\item\r\n  \\protect\\hyperlink{getux5fvariableux5fscope}{\\lstinline{tf.get\\_variable\\_scope()}}\r\n\\item\r\n  \\protect\\hyperlink{variableux5fscope}{\\lstinline{tf.variable\\_scope(name\\_or\\_scope,\\ reuse=None,\\ initializer=None)}}\r\n\\item\r\n  \\protect\\hyperlink{constantux5finitializer}{\\lstinline{tf.constant\\_initializer(value=0.0)}}\r\n\\item\r\n  \\protect\\hyperlink{randomux5fnormalux5finitializer}{\\lstinline{tf.random\\_normal\\_initializer(mean=0.0,\\ stddev=1.0,\\ seed=None)}}\r\n\\item\r\n  \\protect\\hyperlink{truncatedux5fnormalux5finitializer}{\\lstinline{tf.truncated\\_normal\\_initializer(mean=0.0,\\ stddev=1.0,\\ seed=None)}}\r\n\\item\r\n  \\protect\\hyperlink{randomux5funiformux5finitializer}{\\lstinline{tf.random\\_uniform\\_initializer(minval=0.0,\\ maxval=1.0,\\ seed=None)}}\r\n\\item\r\n  \\protect\\hyperlink{uniformux5funitux5fscalingux5finitializer}{\\lstinline{tf.uniform\\_unit\\_scaling\\_initializer(factor=1.0,\\ seed=None)}}\r\n\\item\r\n  \\protect\\hyperlink{zerosux5finitializer}{\\lstinline{tf.zeros\\_initializer(shape,\\ dtype=tf.float32)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-sparse-variable-updates}{Sparse\r\n  Variable Updates}\r\n\\item\r\n  \\protect\\hyperlink{scatterux5fupdate}{\\lstinline{tf.scatter\\_update(ref,\\ indices,\\ updates,\\ use\\_locking=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{scatterux5fadd}{\\lstinline{tf.scatter\\_add(ref,\\ indices,\\ updates,\\ use\\_locking=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{scatterux5fsub}{\\lstinline{tf.scatter\\_sub(ref,\\ indices,\\ updates,\\ use\\_locking=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{sparseux5fmask}{\\lstinline{tf.sparse\\_mask(a,\\ mask\\_indices,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{IndexedSlices}{\\lstinline{class\\ tf.IndexedSlices}}\r\n\\end{itemize}\r\n\r\n\\subsection{Variables }\\label{variables-2}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{class\\ tf.Variable}\r\n}{class tf.Variable }}\\label{class-tf.variable}\r\n\r\nSee the \\href{../../how_tos/variables/index.md}{Variables How To} for a\r\nhigh level overview.\r\n\r\nA variable maintains state in the graph across calls to \\lstinline{run()}.\r\nYou add a variable to the graph by constructing an instance of the class\r\n\\lstinline{Variable}.\r\n\r\nThe \\lstinline{Variable()} constructor requires an initial value for the\r\nvariable, which can be a \\lstinline{Tensor} of any type and shape. The\r\ninitial value defines the type and shape of the variable. After\r\nconstruction, the type and shape of the variable are fixed. The value\r\ncan be changed using one of the assign methods.\r\n\r\nIf you want to change the shape of a variable later you have to use an\r\n\\lstinline{assign} Op with \\lstinline{validate\\_shape=False}.\r\n\r\nJust like any \\lstinline{Tensor}, variables created with\r\n\\lstinline{Variable()} can be used as inputs for other Ops in the graph.\r\nAdditionally, all the operators overloaded for the \\lstinline{Tensor} class\r\nare carried over to variables, so you can also add nodes to the graph by\r\njust doing arithmetic on variables.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ImportTok{import} \\NormalTok{tensorflow }\\ImportTok{as} \\NormalTok{tf}\r\n\r\n\\CommentTok{# Create a variable.}\r\n\\NormalTok{w }\\OperatorTok{=} \\NormalTok{tf.Variable(}\\OperatorTok{<}\\NormalTok{initial}\\OperatorTok{-}\\NormalTok{value}\\OperatorTok{>}\\NormalTok{, name}\\OperatorTok{=<}\\NormalTok{optional}\\OperatorTok{-}\\NormalTok{name}\\OperatorTok{>}\\NormalTok{)}\r\n\r\n\\CommentTok{# Use the variable in the graph like any Tensor.}\r\n\\NormalTok{y }\\OperatorTok{=} \\NormalTok{tf.matmul(w, ...another variable }\\OperatorTok{or} \\NormalTok{tensor...)}\r\n\r\n\\CommentTok{# The overloaded operators are available too.}\r\n\\NormalTok{z }\\OperatorTok{=} \\NormalTok{tf.sigmoid(w }\\OperatorTok{+} \\NormalTok{b)}\r\n\r\n\\CommentTok{# Assign a new value to the variable with `assign()` or a related method.}\r\n\\NormalTok{w.assign(w }\\OperatorTok{+} \\FloatTok{1.0}\\NormalTok{)}\r\n\\NormalTok{w.assign_add(}\\FloatTok{1.0}\\NormalTok{)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nWhen you launch the graph, variables have to be explicitly initialized\r\nbefore you can run Ops that use their value. You can initialize a\r\nvariable by running its \\emph{initializer op}, restoring the variable\r\nfrom a save file, or simply running an \\lstinline{assign} Op that assigns a\r\nvalue to the variable. In fact, the variable \\emph{initializer op} is\r\njust an \\lstinline{assign} Op that assigns the variable's initial value to\r\nthe variable itself.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Launch the graph in a session.}\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session() }\\ImportTok{as} \\NormalTok{sess:}\r\n    \\CommentTok{# Run the variable initializer.}\r\n    \\NormalTok{sess.run(w.initializer)}\r\n    \\CommentTok{# ...you now can run ops that use the value of 'w'...}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nThe most common initialization pattern is to use the convenience\r\nfunction \\lstinline{initialize_all_variables()} to add an Op to the graph\r\nthat initializes all the variables. You then run that Op after launching\r\nthe graph.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Add an Op to initialize all variables.}\r\n\\NormalTok{init_op }\\OperatorTok{=} \\NormalTok{tf.initialize_all_variables()}\r\n\r\n\\CommentTok{# Launch the graph in a session.}\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session() }\\ImportTok{as} \\NormalTok{sess:}\r\n    \\CommentTok{# Run the Op that initializes all variables.}\r\n    \\NormalTok{sess.run(init_op)}\r\n    \\CommentTok{# ...you can now run any Op that uses variable values...}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nIf you need to create a variable with an initial value dependent on\r\nanother variable, use the other variable's\r\n\\lstinline{initialized_value()}. This ensures that variables are\r\ninitialized in the right order.\r\n\r\nAll variables are automatically collected in the graph where they are\r\ncreated. By default, the constructor adds the new variable to the graph\r\ncollection \\lstinline{GraphKeys.VARIABLES}. The convenience function\r\n\\lstinline{all_variables()} returns the contents of that collection.\r\n\r\nWhen building a machine learning model it is often convenient to\r\ndistinguish betwen variables holding the trainable model parameters and\r\nother variables such as a \\lstinline{global step} variable used to count\r\ntraining steps. To make this easier, the variable constructor supports a\r\n\\lstinline{trainable=\\textless{}bool\\textgreater{}} parameter. If\r\n\\lstinline{True}, the new variable is also added to the graph collection\r\n\\lstinline{GraphKeys.TRAINABLE_VARIABLES}. The convenience function\r\n\\lstinline{trainable_variables()} returns the contents of this collection.\r\nThe various \\lstinline{Optimizer} classes use this collection as the\r\ndefault list of variables to optimize.\r\n\r\nCreating a variable.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.__init__(initial_value, trainable=True, collections=None, validate_shape=True, name=None)}\r\n}{tf.Variable.__init__(initial_value, trainable=True, collections=None, validate_shape=True, name=None) }}\\label{tf.variable.ux5fux5finitux5fux5finitialux5fvalue-trainabletrue-collectionsnone-validateux5fshapetrue-namenone}\r\n\r\nCreates a new variable with value \\lstinline{initial_value}.\r\n\r\nThe new variable is added to the graph collections listed in\r\n\\lstinline{collections}, which defaults to\r\n\\lstinline{{[}GraphKeys.VARIABLES{]}}.\r\n\r\nIf \\lstinline{trainable} is \\lstinline{True} the variable is also added to the\r\ngraph collection \\lstinline{GraphKeys.TRAINABLE_VARIABLES}.\r\n\r\nThis constructor creates both a \\lstinline{variable} Op and an\r\n\\lstinline{assign} Op to set the variable to its initial value.\r\n\r\n\\subparagraph{Args: }\\label{args}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{initial_value}: A \\lstinline{Tensor}, or Python object\r\n  convertible to a \\lstinline{Tensor}. The initial value for the Variable.\r\n  Must have a shape specified unless \\lstinline{validate_shape} is set to\r\n  False.\r\n\\item\r\n  \\lstinline{trainable}: If \\lstinline{True}, the default, also adds the\r\n  variable to the graph collection\r\n  \\lstinline{GraphKeys.TRAINABLE_VARIABLES}. This collection is used as\r\n  the default list of variables to use by the \\lstinline{Optimizer}\r\n  classes.\r\n\\item\r\n  \\lstinline{collections}: List of graph collections keys. The new variable\r\n  is added to these collections. Defaults to\r\n  \\lstinline{{[}GraphKeys.VARIABLES{]}}.\r\n\\item\r\n  \\lstinline{validate_shape}: If \\lstinline{False}, allows the variable to be\r\n  initialized with a value of unknown shape. If \\lstinline{True}, the\r\n  default, the shape of \\lstinline{initial_value} must be known.\r\n\\item\r\n  \\lstinline{name}: Optional name for the variable. Defaults to\r\n  \\lstinline{\\textquotesingle{}Variable\\textquotesingle{}} and gets\r\n  uniquified automatically.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns}\r\n\r\nA Variable.\r\n\r\n\\subparagraph{Raises: }\\label{raises}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: If the initial value does not have a shape and\r\n  \\lstinline{validate_shape} is \\lstinline{True}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.initialized_value()}\r\n}{tf.Variable.initialized_value() }}\\label{tf.variable.initializedux5fvalue}\r\n\r\nReturns the value of the initialized variable.\r\n\r\nYou should use this instead of the variable itself to initialize another\r\nvariable with a value that depends on the value of this variable.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Initialize 'v' with a random tensor.}\r\n\\NormalTok{v }\\OperatorTok{=} \\NormalTok{tf.Variable(tf.truncated_normal([}\\DecValTok{10}\\NormalTok{, }\\DecValTok{40}\\NormalTok{]))}\r\n\\CommentTok{# Use `initialized_value` to guarantee that `v` has been}\r\n\\CommentTok{# initialized before its value is used to initialize `w`.}\r\n\\CommentTok{# The random values are picked only once.}\r\n\\NormalTok{w }\\OperatorTok{=} \\NormalTok{tf.Variable(v.initialized_value() }\\OperatorTok{*} \\FloatTok{2.0}\\NormalTok{)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Returns: }\\label{returns-1}\r\n\r\nA \\lstinline{Tensor} holding the value of this variable after its\r\ninitializer has run.\r\n\r\nChanging a variable value.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.assign(value, use_locking=False)}\r\n}{tf.Variable.assign(value, use_locking=False) }}\\label{tf.variable.assignvalue-useux5flockingfalse}\r\n\r\nAssigns a new value to the variable.\r\n\r\nThis is essentially a shortcut for \\lstinline{assign(self,\\ value)}.\r\n\r\n\\subparagraph{Args: }\\label{args-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{value}: A \\lstinline{Tensor}. The new value for this variable.\r\n\\item\r\n  \\lstinline{use\\_locking}: If \\lstinline{True}, use locking during the\r\n  assignment.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-2}\r\n\r\nA \\lstinline{Tensor} that will hold the new value of this variable after\r\nthe assignment has completed.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.assign\\_add(delta,\\ use\\_locking=False)}\r\n}{tf.Variable.assign\\_add(delta, use\\_locking=False) }}\\label{tf.variable.assignux5fadddelta-useux5flockingfalse}\r\n\r\nAdds a value to this variable.\r\n\r\nThis is essentially a shortcut for \\lstinline{assign\\_add(self,\\ delta)}.\r\n\r\n\\subparagraph{Args: }\\label{args-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{delta}: A \\lstinline{Tensor}. The value to add to this variable.\r\n\\item\r\n  \\lstinline{use\\_locking}: If \\lstinline{True}, use locking during the\r\n  operation.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-3}\r\n\r\nA \\lstinline{Tensor} that will hold the new value of this variable after\r\nthe addition has completed.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.assign\\_sub(delta,\\ use\\_locking=False)}\r\n}{tf.Variable.assign\\_sub(delta, use\\_locking=False) }}\\label{tf.variable.assignux5fsubdelta-useux5flockingfalse}\r\n\r\nSubtracts a value from this variable.\r\n\r\nThis is essentially a shortcut for \\lstinline{assign\\_sub(self,\\ delta)}.\r\n\r\n\\subparagraph{Args: }\\label{args-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{delta}: A \\lstinline{Tensor}. The value to subtract from this\r\n  variable.\r\n\\item\r\n  \\lstinline{use\\_locking}: If \\lstinline{True}, use locking during the\r\n  operation.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-4}\r\n\r\nA \\lstinline{Tensor} that will hold the new value of this variable after\r\nthe subtraction has completed.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.scatter\\_sub(sparse\\_delta,\\ use\\_locking=False)}\r\n}{tf.Variable.scatter\\_sub(sparse\\_delta, use\\_locking=False) }}\\label{tf.variable.scatterux5fsubsparseux5fdelta-useux5flockingfalse}\r\n\r\nSubtracts \\lstinline{IndexedSlices} from this variable.\r\n\r\nThis is essentially a shortcut for\r\n\\lstinline{scatter\\_sub(self,\\ sparse\\_delta.indices,\\ sparse\\_delta.values)}.\r\n\r\n\\subparagraph{Args: }\\label{args-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{sparse\\_delta}: \\lstinline{IndexedSlices} to be subtracted from\r\n  this variable.\r\n\\item\r\n  \\lstinline{use\\_locking}: If \\lstinline{True}, use locking during the\r\n  operation.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-5}\r\n\r\nA \\lstinline{Tensor} that will hold the new value of this variable after\r\nthe scattered subtraction has completed.\r\n\r\n\\subparagraph{Raises: }\\label{raises-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: if \\lstinline{sparse\\_delta} is not an\r\n  \\lstinline{IndexedSlices}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.count\\_up\\_to(limit)}\r\n}{tf.Variable.count\\_up\\_to(limit) }}\\label{tf.variable.countux5fupux5ftolimit}\r\n\r\nIncrements this variable until it reaches \\lstinline{limit}.\r\n\r\nWhen that Op is run it tries to increment the variable by \\lstinline{1}. If\r\nincrementing the variable would bring it above \\lstinline{limit} then the\r\nOp raises the exception \\lstinline{OutOfRangeError}.\r\n\r\nIf no error is raised, the Op outputs the value of the variable before\r\nthe increment.\r\n\r\nThis is essentially a shortcut for \\lstinline{count\\_up\\_to(self,\\ limit)}.\r\n\r\n\\subparagraph{Args: }\\label{args-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{limit}: value at which incrementing the variable raises an\r\n  error.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-6}\r\n\r\nA \\lstinline{Tensor} that will hold the variable value before the\r\nincrement. If no other Op modifies this variable, the values produced\r\nwill all be distinct.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.eval(session=None)}\r\n}{tf.Variable.eval(session=None) }}\\label{tf.variable.evalsessionnone}\r\n\r\nIn a session, computes and returns the value of this variable.\r\n\r\nThis is not a graph construction method, it does not add ops to the\r\ngraph.\r\n\r\nThis convenience method requires a session where the graph containing\r\nthis variable has been launched. If no session is passed, the default\r\nsession is used. See the\r\n\\href{../../api_docs/python/client.md\\#Session}{Session class} for more\r\ninformation on launching a graph and on sessions.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{v }\\OperatorTok{=} \\NormalTok{tf.Variable([}\\DecValTok{1}\\NormalTok{, }\\DecValTok{2}\\NormalTok{])}\r\n\\NormalTok{init }\\OperatorTok{=} \\NormalTok{tf.initialize_all_variables()}\r\n\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session() }\\ImportTok{as} \\NormalTok{sess:}\r\n    \\NormalTok{sess.run(init)}\r\n    \\CommentTok{# Usage passing the session explicitly.}\r\n    \\BuiltInTok{print} \\NormalTok{v.}\\BuiltInTok{eval}\\NormalTok{(sess)}\r\n    \\CommentTok{# Usage with the default session.  The 'with' block}\r\n    \\CommentTok{# above makes 'sess' the default session.}\r\n    \\BuiltInTok{print} \\NormalTok{v.}\\BuiltInTok{eval}\\NormalTok{()}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{session}: The session to use to evaluate this variable. If\r\n  none, the default session is used.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-7}\r\n\r\nA numpy \\lstinline{ndarray} with a copy of the value of this variable.\r\n\r\nProperties.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.name}\r\n}{tf.Variable.name }}\\label{tf.variable.name}\r\n\r\nThe name of this variable.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.dtype}\r\n}{tf.Variable.dtype }}\\label{tf.variable.dtype}\r\n\r\nThe \\lstinline{DType} of this variable.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.get\\_shape()}\r\n}{tf.Variable.get\\_shape() }}\\label{tf.variable.getux5fshape}\r\n\r\nThe \\lstinline{TensorShape} of this variable.\r\n\r\n\\subparagraph{Returns: }\\label{returns-8}\r\n\r\nA \\lstinline{TensorShape}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.device}\r\n}{tf.Variable.device }}\\label{tf.variable.device}\r\n\r\nThe device of this variable.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.initializer}\r\n}{tf.Variable.initializer }}\\label{tf.variable.initializer}\r\n\r\nThe initializer operation for this variable.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.graph}\r\n}{tf.Variable.graph }}\\label{tf.variable.graph}\r\n\r\nThe \\lstinline{Graph} of this variable.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.Variable.op}\r\n}{tf.Variable.op }}\\label{tf.variable.op}\r\n\r\nThe \\lstinline{Operation} of this variable.\r\n\r\n\\subsection{Variable helper functions }\\label{variable-helper-functions}\r\n\r\nTensorFlow provides a set of functions to help manage the set of\r\nvariables collected in the graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.all\\_variables()}\r\n}{tf.all\\_variables() }}\\label{tf.allux5fvariables}\r\n\r\nReturns all variables collected in the graph.\r\n\r\nThe \\lstinline{Variable()} constructor automatically adds new variables to\r\nthe graph collection \\lstinline{GraphKeys.VARIABLES}. This convenience\r\nfunction returns the contents of that collection.\r\n\r\n\\subparagraph{Returns: }\\label{returns-9}\r\n\r\nA list of \\lstinline{Variable} objects.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.trainable\\_variables()}\r\n}{tf.trainable\\_variables() }}\\label{tf.trainableux5fvariables}\r\n\r\nReturns all variables created with \\lstinline{trainable=True}.\r\n\r\nWhen passed \\lstinline{trainable=True}, the \\lstinline{Variable()} constructor\r\nautomatically adds new variables to the graph collection\r\n\\lstinline{GraphKeys.TRAINABLE\\_VARIABLES}. This convenience function\r\nreturns the contents of that collection.\r\n\r\n\\subparagraph{Returns: }\\label{returns-10}\r\n\r\nA list of Variable objects.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.initialize\\_all\\_variables()}\r\n}{tf.initialize\\_all\\_variables() }}\\label{tf.initializeux5fallux5fvariables}\r\n\r\nReturns an Op that initializes all variables.\r\n\r\nThis is just a shortcut for\r\n\\lstinline{initialize\\_variables(all\\_variables())}\r\n\r\n\\subparagraph{Returns: }\\label{returns-11}\r\n\r\nAn Op that initializes all variables in the graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.initialize\\_variables(var\\_list,\\ name=\\textquotesingle{}init\\textquotesingle{})}\r\n}{tf.initialize\\_variables(var\\_list, name='init') }}\\label{tf.initializeux5fvariablesvarux5flist-nameinit}\r\n\r\nReturns an Op that initializes a list of variables.\r\n\r\nAfter you launch the graph in a session, you can run the returned Op to\r\ninitialize all the variables in \\lstinline{var\\_list}. This Op runs all the\r\ninitializers of the variables in \\lstinline{var\\_list} in parallel.\r\n\r\nCalling \\lstinline{initialize\\_variables()} is equivalent to passing the\r\nlist of initializers to \\lstinline{Group()}.\r\n\r\nIf \\lstinline{var\\_list} is empty, however, the function still returns an\r\nOp that can be run. That Op just has no effect.\r\n\r\n\\subparagraph{Args: }\\label{args-7}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{var\\_list}: List of \\lstinline{Variable} objects to initialize.\r\n\\item\r\n  \\lstinline{name}: Optional name for the returned operation.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-12}\r\n\r\nAn Op that run the initializers of all the specified variables.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.assert\\_variables\\_initialized(var\\_list=None)}\r\n}{tf.assert\\_variables\\_initialized(var\\_list=None) }}\\label{tf.assertux5fvariablesux5finitializedvarux5flistnone}\r\n\r\nReturns an Op to check if variables are initialized.\r\n\r\nWhen run, the returned Op will raise the exception\r\n\\lstinline{FailedPreconditionError} if any of the variables has not yet\r\nbeen initialized.\r\n\r\nNote: This function is implemented by trying to fetch the values of the\r\nvariables. If one of the variables is not initialized a message may be\r\nlogged by the C++ runtime. This is expected.\r\n\r\n\\subparagraph{Args: }\\label{args-8}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{var\\_list}: List of \\lstinline{Variable} objects to check.\r\n  Defaults to the value of \\lstinline{all\\_variables().}\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-13}\r\n\r\nAn Op, or None if there are no variables.\r\n\r\n\\subsection{Saving and Restoring Variables\r\n}\\label{saving-and-restoring-variables}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{class\\ tf.train.Saver}\r\n}{class tf.train.Saver }}\\label{class-tf.train.saver}\r\n\r\nSaves and restores variables.\r\n\r\nSee \\href{../../how_tos/variables/index.md}{Variables} for an overview\r\nof variables, saving and restoring.\r\n\r\nThe \\lstinline{Saver} class adds ops to save and restore variables to and\r\nfrom \\emph{checkpoints}. It also provides convenience methods to run\r\nthese ops.\r\n\r\nCheckpoints are binary files in a proprietary format which map variable\r\nnames to tensor values. The best way to examine the contents of a\r\ncheckpoint is to load it using a \\lstinline{Saver}.\r\n\r\nSavers can automatically number checkpoint filenames with a provided\r\ncounter. This lets you keep multiple checkpoints at different steps\r\nwhile training a model. For example you can number the checkpoint\r\nfilenames with the training step number. To avoid filling up disks,\r\nsavers manage checkpoint files automatically. For example, they can keep\r\nonly the N most recent files, or one checkpoint for every N hours of\r\ntraining.\r\n\r\nYou number checkpoint filenames by passing a value to the optional\r\n\\lstinline{global\\_step} argument to \\lstinline{save()}:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{saver.save(sess, }\\StringTok{'my-model'}\\NormalTok{, global_step}\\OperatorTok{=}\\DecValTok{0}\\NormalTok{) }\\OperatorTok{==>} \\NormalTok{filename: }\\StringTok{'my-model-0'}\r\n\\NormalTok{...}\r\n\\NormalTok{saver.save(sess, }\\StringTok{'my-model'}\\NormalTok{, global_step}\\OperatorTok{=}\\DecValTok{1000}\\NormalTok{) }\\OperatorTok{==>} \\NormalTok{filename: }\\StringTok{'my-model-1000'}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nAdditionally, optional arguments to the \\lstinline{Saver()} constructor let\r\nyou control the proliferation of checkpoint files on disk:\r\n\r\n\\begin{itemize}\r\n\\item\r\n  \\lstinline{max\\_to\\_keep} indicates the maximum number of recent\r\n  checkpoint files to keep. As new files are created, older files are\r\n  deleted. If None or 0, all checkpoint files are kept. Defaults to 5\r\n  (that is, the 5 most recent checkpoint files are kept.)\r\n\\item\r\n  \\lstinline{keep\\_checkpoint\\_every\\_n\\_hours}: In addition to keeping the\r\n  most recent \\lstinline{max\\_to\\_keep} checkpoint files, you might want to\r\n  keep one checkpoint file for every N hours of training. This can be\r\n  useful if you want to later analyze how a model progressed during a\r\n  long training session. For example, passing\r\n  \\lstinline{keep\\_checkpoint\\_every\\_n\\_hours=2} ensures that you keep one\r\n  checkpoint file for every 2 hours of training. The default value of\r\n  10,000 hours effectively disables the feature.\r\n\\end{itemize}\r\n\r\nNote that you still have to call the \\lstinline{save()} method to save the\r\nmodel. Passing these arguments to the constructor will not save\r\nvariables automatically for you.\r\n\r\nA training program that saves regularly looks like:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{...}\r\n\\CommentTok{# Create a saver.}\r\n\\NormalTok{saver }\\OperatorTok{=} \\NormalTok{tf.train.Saver(...variables...)}\r\n\\CommentTok{# Launch the graph and train, saving the model every 1,000 steps.}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session()}\r\n\\ControlFlowTok{for} \\NormalTok{step }\\OperatorTok{in} \\BuiltInTok{xrange}\\NormalTok{(}\\DecValTok{1000000}\\NormalTok{):}\r\n    \\NormalTok{sess.run(..training_op..)}\r\n    \\ControlFlowTok{if} \\NormalTok{step }\\OperatorTok{%} \\DecValTok{1000} \\OperatorTok{==} \\DecValTok{0}\\NormalTok{:}\r\n        \\CommentTok{# Append the step number to the checkpoint name:}\r\n        \\NormalTok{saver.save(sess, }\\StringTok{'my-model'}\\NormalTok{, global_step}\\OperatorTok{=}\\NormalTok{step)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nIn addition to checkpoint files, savers keep a protocol buffer on disk\r\nwith the list of recent checkpoints. This is used to manage numbered\r\ncheckpoint files and by \\lstinline{latest\\_checkpoint()}, which makes it\r\neasy to discover the path to the most recent checkpoint. That protocol\r\nbuffer is stored in a file named `checkpoint' next to the checkpoint\r\nfiles.\r\n\r\nIf you create several savers, you can specify a different filename for\r\nthe protocol buffer file in the call to \\lstinline{save()}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.train.Saver.\\_\\_init\\_\\_(var\\_list=None,\\ reshape=False,\\ sharded=False,\\ max\\_to\\_keep=5,\\ keep\\_checkpoint\\_every\\_n\\_hours=10000.0,\\ name=None,\\ restore\\_sequentially=False,\\ saver\\_def=None,\\ builder=None)}\r\n}{tf.train.Saver.\\_\\_init\\_\\_(var\\_list=None, reshape=False, sharded=False, max\\_to\\_keep=5, keep\\_checkpoint\\_every\\_n\\_hours=10000.0, name=None, restore\\_sequentially=False, saver\\_def=None, builder=None) }}\\label{tf.train.saver.ux5fux5finitux5fux5fvarux5flistnone-reshapefalse-shardedfalse-maxux5ftoux5fkeep5-keepux5fcheckpointux5feveryux5fnux5fhours10000.0-namenone-restoreux5fsequentiallyfalse-saverux5fdefnone-buildernone}\r\n\r\nCreates a \\lstinline{Saver}.\r\n\r\nThe constructor adds ops to save and restore variables.\r\n\r\n\\lstinline{var\\_list} specifies the variables that will be saved and\r\nrestored. It can be passed as a \\lstinline{dict} or a list:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  A \\lstinline{dict} of names to variables: The keys are the names that\r\n  will be used to save or restore the variables in the checkpoint files.\r\n\\item\r\n  A list of variables: The variables will be keyed with their op name in\r\n  the checkpoint files.\r\n\\end{itemize}\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{v1 }\\OperatorTok{=} \\NormalTok{tf.Variable(..., name}\\OperatorTok{=}\\StringTok{'v1'}\\NormalTok{)}\r\n\\NormalTok{v2 }\\OperatorTok{=} \\NormalTok{tf.Variable(..., name}\\OperatorTok{=}\\StringTok{'v2'}\\NormalTok{)}\r\n\r\n\\CommentTok{# Pass the variables as a dict:}\r\n\\NormalTok{saver }\\OperatorTok{=} \\NormalTok{tf.train.Saver(\\{}\\StringTok{'v1'}\\NormalTok{: v1, }\\StringTok{'v2'}\\NormalTok{: v2\\})}\r\n\r\n\\CommentTok{# Or pass them as a list.}\r\n\\NormalTok{saver }\\OperatorTok{=} \\NormalTok{tf.train.Saver([v1, v2])}\r\n\\CommentTok{# Passing a list is equivalent to passing a dict with the variable op names}\r\n\\CommentTok{# as keys:}\r\n\\NormalTok{saver }\\OperatorTok{=} \\NormalTok{tf.train.Saver(\\{v.op.name: v }\\ControlFlowTok{for} \\NormalTok{v }\\OperatorTok{in} \\NormalTok{[v1, v2]\\})}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nThe optional \\lstinline{reshape} argument, if True, allows restoring a\r\nvariable from a save file where the variable had a different shape, but\r\nthe same number of elements and type. This is useful if you have\r\nreshaped a variable and want to reload it from an older checkpoint.\r\n\r\nThe optional \\lstinline{sharded} argument, if True, instructs the saver to\r\nshard checkpoints per device.\r\n\r\n\\subparagraph{Args: }\\label{args-9}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{var\\_list}: A list of Variables or a dictionary mapping names\r\n  to Variables. If None, defaults to the list of all variables.\r\n\\item\r\n  \\lstinline{reshape}: If True, allows restoring parameters from a\r\n  checkpoint where the variables have a different shape.\r\n\\item\r\n  \\lstinline{sharded}: If True, shard the checkpoints, one per device.\r\n\\item\r\n  \\lstinline{max\\_to\\_keep}: maximum number of recent checkpoints to keep.\r\n  Defaults to 10,000 hours.\r\n\\item\r\n  \\lstinline{keep\\_checkpoint\\_every\\_n\\_hours}: How often to keep\r\n  checkpoints. Defaults to 10,000 hours.\r\n\\item\r\n  \\lstinline{name}: string. Optional name to use as a prefix when adding\r\n  operations.\r\n\\item\r\n  \\lstinline{restore\\_sequentially}: A Bool, which if true, causes restore\r\n  of different variables to happen sequentially within each device. This\r\n  can lower memory usage when restoring very large models.\r\n\\item\r\n  \\lstinline{saver\\_def}: Optional SaverDef proto to use instead of running\r\n  the builder. This is only useful for specialty code that wants to\r\n  recreate a Saver object for a previously built Graph that had a Saver.\r\n  The saver\\_def proto should be the one returned by the\r\n  as\\_saver\\_def() call of the Saver that was created for that Graph.\r\n\\item\r\n  \\lstinline{builder}: Optional SaverBuilder to use if a saver\\_def was not\r\n  provided. Defaults to BaseSaverBuilder().\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{var\\_list} is invalid.\r\n\\item\r\n  \\lstinline{ValueError}: If any of the keys or values in\r\n  \\lstinline{var\\_list} is not unique.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.train.Saver.save(sess,\\ save\\_path,\\ global\\_step=None,\\ latest\\_filename=None)}\r\n}{tf.train.Saver.save(sess, save\\_path, global\\_step=None, latest\\_filename=None) }}\\label{tf.train.saver.savesess-saveux5fpath-globalux5fstepnone-latestux5ffilenamenone}\r\n\r\nSaves variables.\r\n\r\nThis method runs the ops added by the constructor for saving variables.\r\nIt requires a session in which the graph was launched. The variables to\r\nsave must also have been initialized.\r\n\r\nThe method returns the path of the newly created checkpoint file. This\r\npath can be passed directly to a call to \\lstinline{restore()}.\r\n\r\n\\subparagraph{Args: }\\label{args-10}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{sess}: A Session to use to save the variables.\r\n\\item\r\n  \\lstinline{save\\_path}: string. Path to the checkpoint filename. If the\r\n  saver is \\lstinline{sharded}, this is the prefix of the sharded\r\n  checkpoint filename.\r\n\\item\r\n  \\lstinline{global\\_step}: If provided the global step number is appended\r\n  to \\lstinline{save\\_path} to create the checkpoint filename. The optional\r\n  argument can be a Tensor, a Tensor name or an integer.\r\n\\item\r\n  \\lstinline{latest\\_filename}: Optional name for the protocol buffer file\r\n  that will contains the list of most recent checkpoint filenames. That\r\n  file, kept in the same directory as the checkpoint files, is\r\n  automatically managed by the saver to keep track of recent\r\n  checkpoints. Defaults to `checkpoint'.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-14}\r\n\r\nA string: path at which the variables were saved. If the saver is\r\nsharded, this string ends with: `-?????-of-nnnnn' where `nnnnn' is the\r\nnumber of shards created.\r\n\r\n\\subparagraph{Raises: }\\label{raises-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{sess} is not a Session.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.train.Saver.restore(sess,\\ save\\_path)}\r\n}{tf.train.Saver.restore(sess, save\\_path) }}\\label{tf.train.saver.restoresess-saveux5fpath}\r\n\r\nRestores previously saved variables.\r\n\r\nThis method runs the ops added by the constructor for restoring\r\nvariables. It requires a session in which the graph was launched. The\r\nvariables to restore do not have to have been initialized, as restoring\r\nis itself a way to initialize variables.\r\n\r\nThe \\lstinline{save\\_path} argument is typically a value previously\r\nreturned from a \\lstinline{save()} call, or a call to\r\n\\lstinline{latest\\_checkpoint()}.\r\n\r\n\\subparagraph{Args: }\\label{args-11}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{sess}: A Session to use to restore the parameters.\r\n\\item\r\n  \\lstinline{save\\_path}: Path where parameters were previously saved.\r\n\\end{itemize}\r\n\r\nOther utility methods.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.train.Saver.last\\_checkpoints}\r\n}{tf.train.Saver.last\\_checkpoints }}\\label{tf.train.saver.lastux5fcheckpoints}\r\n\r\nList of not-yet-deleted checkpoint filenames.\r\n\r\nYou can pass any of the returned values to \\lstinline{restore()}.\r\n\r\n\\subparagraph{Returns: }\\label{returns-15}\r\n\r\nA list of checkpoint filenames, sorted from oldest to newest.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.train.Saver.set\\_last\\_checkpoints(last\\_checkpoints)}\r\n}{tf.train.Saver.set\\_last\\_checkpoints(last\\_checkpoints) }}\\label{tf.train.saver.setux5flastux5fcheckpointslastux5fcheckpoints}\r\n\r\nSets the list of not-yet-deleted checkpoint filenames.\r\n\r\n\\subparagraph{Args: }\\label{args-12}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{last\\_checkpoints}: a list of checkpoint filenames.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{AssertionError}: if the list of checkpoint filenames has\r\n  already been set.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.train.Saver.as\\_saver\\_def()}\r\n}{tf.train.Saver.as\\_saver\\_def() }}\\label{tf.train.saver.asux5fsaverux5fdef}\r\n\r\nGenerates a \\lstinline{SaverDef} representation of this saver.\r\n\r\n\\subparagraph{Returns: }\\label{returns-16}\r\n\r\nA \\lstinline{SaverDef} proto.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.train.latest\\_checkpoint(checkpoint\\_dir,\\ latest\\_filename=None)}\r\n}{tf.train.latest\\_checkpoint(checkpoint\\_dir, latest\\_filename=None) }}\\label{tf.train.latestux5fcheckpointcheckpointux5fdir-latestux5ffilenamenone}\r\n\r\nFinds the filename of latest saved checkpoint file.\r\n\r\n\\subparagraph{Args: }\\label{args-13}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{checkpoint\\_dir}: Directory where the variables were saved.\r\n\\item\r\n  \\lstinline{latest\\_filename}: Optional name for the protocol buffer file\r\n  that contains the list of most recent checkpoint filenames. See the\r\n  corresponding argument to \\lstinline{Saver.save()}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-17}\r\n\r\nThe full path to the latest checkpoint or None if no checkpoint was\r\nfound.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.train.get\\_checkpoint\\_state(checkpoint\\_dir,\\ latest\\_filename=None)}\r\n}{tf.train.get\\_checkpoint\\_state(checkpoint\\_dir, latest\\_filename=None) }}\\label{tf.train.getux5fcheckpointux5fstatecheckpointux5fdir-latestux5ffilenamenone}\r\n\r\nReturns CheckpointState proto from the ``checkpoint'' file.\r\n\r\nIf the ``checkpoint'' file contains a valid CheckpointState proto,\r\nreturns it.\r\n\r\n\\subparagraph{Args: }\\label{args-14}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{checkpoint\\_dir}: The directory of checkpoints.\r\n\\item\r\n  \\lstinline{latest\\_filename}: Optional name of the checkpoint file.\r\n  Default to `checkpoint'.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-18}\r\n\r\nA CheckpointState if the state was available, None otherwise.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.train.update\\_checkpoint\\_state(save\\_dir,\\ model\\_checkpoint\\_path,\\ all\\_model\\_checkpoint\\_paths=None,\\ latest\\_filename=None)}\r\n}{tf.train.update\\_checkpoint\\_state(save\\_dir, model\\_checkpoint\\_path, all\\_model\\_checkpoint\\_paths=None, latest\\_filename=None) }}\\label{tf.train.updateux5fcheckpointux5fstatesaveux5fdir-modelux5fcheckpointux5fpath-allux5fmodelux5fcheckpointux5fpathsnone-latestux5ffilenamenone}\r\n\r\nUpdates the content of the `checkpoint' file.\r\n\r\nThis updates the checkpoint file containing a CheckpointState proto.\r\n\r\n\\subparagraph{Args: }\\label{args-15}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{save\\_dir}: Directory where the model was saved.\r\n\\item\r\n  \\lstinline{model\\_checkpoint\\_path}: The checkpoint file.\r\n\\item\r\n  \\lstinline{all\\_model\\_checkpoint\\_paths}: list of strings. Paths to all\r\n  not-yet-deleted checkpoints, sorted from oldest to newest. If this is\r\n  a non-empty list, the last element must be equal to\r\n  model\\_checkpoint\\_path. These paths are also saved in the\r\n  CheckpointState proto.\r\n\\item\r\n  \\lstinline{latest\\_filename}: Optional name of the checkpoint file.\r\n  Default to `checkpoint'.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{RuntimeError}: If the save paths conflict.\r\n\\end{itemize}\r\n\r\n\\subsection{Sharing Variables }\\label{sharing-variables}\r\n\r\nTensorFlow provides several classes and operations that you can use to\r\ncreate variables contingent on certain conditions.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.get\\_variable(name,\\ shape=None,\\ dtype=tf.float32,\\ initializer=None,\\ trainable=True,\\ collections=None)}\r\n}{tf.get\\_variable(name, shape=None, dtype=tf.float32, initializer=None, trainable=True, collections=None) }}\\label{tf.getux5fvariablename-shapenone-dtypetf.float32-initializernone-trainabletrue-collectionsnone}\r\n\r\nGets an existing variable with these parameters or create a new one.\r\n\r\nThis function prefixes the name with the current variable scope and\r\nperforms reuse checks. See the\r\n\\href{../../how_tos/variable_scope/index.md}{Variable Scope How To} for\r\nan extensive description of how reusing works. Here is a basic example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{):}\r\n    \\NormalTok{v }\\OperatorTok{=} \\NormalTok{get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])  }\\CommentTok{# v.name == \"foo/v:0\"}\r\n    \\NormalTok{w }\\OperatorTok{=} \\NormalTok{get_variable(}\\StringTok{\"w\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])  }\\CommentTok{# w.name == \"foo/w:0\"}\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{, reuse}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{)}\r\n    \\NormalTok{v1 }\\OperatorTok{=} \\NormalTok{get_variable(}\\StringTok{\"v\"}\\NormalTok{)  }\\CommentTok{# The same as v above.}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nIf initializer is \\lstinline{None} (the default), the default initializer\r\npassed in the constructor is used. If that one is \\lstinline{None} too, a\r\n\\lstinline{UniformUnitScalingInitializer} will be used.\r\n\r\n\\subparagraph{Args: }\\label{args-16}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{name}: the name of the new or existing variable.\r\n\\item\r\n  \\lstinline{shape}: shape of the new or existing variable.\r\n\\item\r\n  \\lstinline{dtype}: type of the new or existing variable (defaults to\r\n  \\lstinline{DT\\_FLOAT}).\r\n\\item\r\n  \\lstinline{initializer}: initializer for the variable if one is created.\r\n\\item\r\n  \\lstinline{trainable}: If \\lstinline{True} also add the variable to the\r\n  graph collection \\lstinline{GraphKeys.TRAINABLE\\_VARIABLES} (see\r\n  variables.Variable).\r\n\\item\r\n  \\lstinline{collections}: List of graph collections keys to add the\r\n  Variable to. Defaults to \\lstinline{{[}GraphKeys.VARIABLES{]}} (see\r\n  variables.Variable).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-19}\r\n\r\nThe created or existing variable.\r\n\r\n\\subparagraph{Raises: }\\label{raises-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: when creating a new variable and shape is not\r\n  declared, or when violating reuse during variable creation. Reuse is\r\n  set inside \\lstinline{variable\\_scope}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.get\\_variable\\_scope()}\r\n}{tf.get\\_variable\\_scope() }}\\label{tf.getux5fvariableux5fscope}\r\n\r\nReturns the current variable scope.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.variable\\_scope(name\\_or\\_scope,\\ reuse=None,\\ initializer=None)}\r\n}{tf.variable\\_scope(name\\_or\\_scope, reuse=None, initializer=None) }}\\label{tf.variableux5fscopenameux5forux5fscope-reusenone-initializernone}\r\n\r\nReturns a context for variable scope.\r\n\r\nVariable scope allows to create new variables and to share already\r\ncreated ones while providing checks to not create or share by accident.\r\nFor details, see the\r\n\\href{../../how_tos/variable_scope/index.md}{Variable Scope How To},\r\nhere we present only a few basic examples.\r\n\r\nSimple example of how to create a new variable:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{):}\r\n    \\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"bar\"}\\NormalTok{):}\r\n        \\NormalTok{v }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n        \\ControlFlowTok{assert} \\NormalTok{v.name }\\OperatorTok{==} \\StringTok{\"foo/bar/v:0\"}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nBasic example of sharing a variable:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{):}\r\n    \\NormalTok{v }\\OperatorTok{=} \\NormalTok{get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{, reuse}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{):}\r\n    \\NormalTok{v1 }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n\\ControlFlowTok{assert} \\NormalTok{v1 }\\OperatorTok{==} \\NormalTok{v}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nSharing a variable by capturing a scope and setting reuse:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{) }\\ImportTok{as} \\NormalTok{scope.}\r\n    \\NormalTok{v }\\OperatorTok{=} \\NormalTok{get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n    \\NormalTok{scope.reuse_variables()}\r\n    \\NormalTok{v1 }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n\\ControlFlowTok{assert} \\NormalTok{v1 }\\OperatorTok{==} \\NormalTok{v}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nTo prevent accidental sharing of variables, we raise an exception when\r\ngetting an existing variable in a non-reusing scope.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{) }\\ImportTok{as} \\NormalTok{scope.}\r\n    \\NormalTok{v }\\OperatorTok{=} \\NormalTok{get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n    \\NormalTok{v1 }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n    \\CommentTok{#  Raises ValueError(\"... v already exists ...\").}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nSimilarly, we raise an exception when trying to get a variable that does\r\nnot exist in reuse mode.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{, reuse}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{):}\r\n    \\NormalTok{v }\\OperatorTok{=} \\NormalTok{get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n    \\CommentTok{#  Raises ValueError(\"... v does not exists ...\").}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nNote that the \\lstinline{reuse} flag is inherited: if we open a reusing\r\nscope, then all its sub-scopes become reusing as well.\r\n\r\n\\subparagraph{Args: }\\label{args-17}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{name\\_or\\_scope}: \\lstinline{string} or \\lstinline{VariableScope}:\r\n  the scope to open.\r\n\\item\r\n  \\lstinline{reuse}: \\lstinline{True} or \\lstinline{None}; if \\lstinline{True}, we\r\n  go into reuse mode for this scope as well as all sub-scopes; if\r\n  \\lstinline{None}, we just inherit the parent scope reuse.\r\n\\item\r\n  \\lstinline{initializer}: default initializer for variables within this\r\n  scope.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Yields: }\\label{yields}\r\n\r\nA scope that can be to captured and reused.\r\n\r\n\\subparagraph{Raises: }\\label{raises-7}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: when trying to reuse within a create scope, or\r\n  create within a reuse scope, or if reuse is not \\lstinline{None} or\r\n  \\lstinline{True}.\r\n\\item\r\n  \\lstinline{TypeError}: when the types of some arguments are not\r\n  appropriate.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.constant\\_initializer(value=0.0)}\r\n}{tf.constant\\_initializer(value=0.0) }}\\label{tf.constantux5finitializervalue0.0}\r\n\r\nReturns an initializer that generates Tensors with a single value.\r\n\r\n\\subparagraph{Args: }\\label{args-18}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{value}: A Python scalar. All elements of the initialized\r\n  variable will be set to this value.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-20}\r\n\r\nAn initializer that generates Tensors with a single value.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.random\\_normal\\_initializer(mean=0.0,\\ stddev=1.0,\\ seed=None)}\r\n}{tf.random\\_normal\\_initializer(mean=0.0, stddev=1.0, seed=None) }}\\label{tf.randomux5fnormalux5finitializermean0.0-stddev1.0-seednone}\r\n\r\nReturns an initializer that generates Tensors with a normal\r\ndistribution.\r\n\r\n\\subparagraph{Args: }\\label{args-19}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{mean}: a python scalar or a scalar tensor. Mean of the random\r\n  values to generate.\r\n\\item\r\n  \\lstinline{stddev}: a python scalar or a scalar tensor. Standard\r\n  deviation of the random values to generate.\r\n\\item\r\n  \\lstinline{seed}: A Python integer. Used to create random seeds. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\lstinline{set\\_random\\_seed}}\r\n  for behavior.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-21}\r\n\r\nAn initializer that generates Tensors with a normal distribution.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.truncated\\_normal\\_initializer(mean=0.0,\\ stddev=1.0,\\ seed=None)}\r\n}{tf.truncated\\_normal\\_initializer(mean=0.0, stddev=1.0, seed=None) }}\\label{tf.truncatedux5fnormalux5finitializermean0.0-stddev1.0-seednone}\r\n\r\nReturns an initializer that generates a truncated normal distribution.\r\n\r\nThese values are similar to values from a random\\_normal\\_initializer\r\nexcept that values more than two standard deviations from the mean are\r\ndiscarded and re-drawn. This is the recommended initializer for neural\r\nnetwork weights and filters.\r\n\r\n\\subparagraph{Args: }\\label{args-20}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{mean}: a python scalar or a scalar tensor. Mean of the random\r\n  values to generate.\r\n\\item\r\n  \\lstinline{stddev}: a python scalar or a scalar tensor. Standard\r\n  deviation of the random values to generate.\r\n\\item\r\n  \\lstinline{seed}: A Python integer. Used to create random seeds. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\lstinline{set\\_random\\_seed}}\r\n  for behavior.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-22}\r\n\r\nAn initializer that generates Tensors with a truncated normal\r\ndistribution.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.random\\_uniform\\_initializer(minval=0.0,\\ maxval=1.0,\\ seed=None)}\r\n}{tf.random\\_uniform\\_initializer(minval=0.0, maxval=1.0, seed=None) }}\\label{tf.randomux5funiformux5finitializerminval0.0-maxval1.0-seednone}\r\n\r\nReturns an initializer that generates Tensors with a uniform\r\ndistribution.\r\n\r\n\\subparagraph{Args: }\\label{args-21}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{minval}: a python scalar or a scalar tensor. lower bound of\r\n  the range of random values to generate.\r\n\\item\r\n  \\lstinline{maxval}: a python scalar or a scalar tensor. upper bound of\r\n  the range of random values to generate.\r\n\\item\r\n  \\lstinline{seed}: A Python integer. Used to create random seeds. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\lstinline{set\\_random\\_seed}}\r\n  for behavior.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-23}\r\n\r\nAn initializer that generates Tensors with a uniform distribution.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.uniform\\_unit\\_scaling\\_initializer(factor=1.0,\\ seed=None)}\r\n}{tf.uniform\\_unit\\_scaling\\_initializer(factor=1.0, seed=None) }}\\label{tf.uniformux5funitux5fscalingux5finitializerfactor1.0-seednone}\r\n\r\nReturns an initializer that generates tensors without scaling variance.\r\n\r\nWhen initializing a deep network, it is in principle advantageous to\r\nkeep the scale of the input variance constant, so it does not explode or\r\ndiminish by reaching the final layer. If the input is \\lstinline{x} and the\r\noperation \\lstinline{x\\ *\\ W}, and we want to initialize \\lstinline{W}\r\nuniformly at random, we need to pick \\lstinline{W} from\r\n\r\n\\begin{verbatim}\r\n[-sqrt(3) / sqrt(dim), sqrt(3) / sqrt(dim)]\r\n\\end{verbatim}\r\n\r\nto keep the scale intact, where \\lstinline{dim\\ =\\ W.shape{[}0{]}} (the\r\nsize of the input). A similar calculation for convolutional networks\r\ngives an analogous result with \\lstinline{dim} equal to the product of the\r\nfirst 3 dimensions. When nonlinearities are present, we need to multiply\r\nthis by a constant \\lstinline{factor}. See\r\n\\url{https://arxiv.org/pdf/1412.6558v3.pdf} for deeper motivation,\r\nexperiments and the calculation of constants. In section 2.3 there, the\r\nconstants were numerically computed: for a linear layer it's 1.0, relu:\r\n\\textasciitilde{}1.43, tanh: \\textasciitilde{}1.15.\r\n\r\n\\subparagraph{Args: }\\label{args-22}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{factor}: Float. A multiplicative factor by which the values\r\n  will be scaled.\r\n\\item\r\n  \\lstinline{seed}: A Python integer. Used to create random seeds. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\lstinline{set\\_random\\_seed}}\r\n  for behavior.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-24}\r\n\r\nAn initializer that generates tensors with unit variance.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.zeros\\_initializer(shape,\\ dtype=tf.float32)}\r\n}{tf.zeros\\_initializer(shape, dtype=tf.float32) }}\\label{tf.zerosux5finitializershape-dtypetf.float32}\r\n\r\nAn adaptor for zeros() to match the Initializer spec.\r\n\r\n\\subsection{Sparse Variable Updates }\\label{sparse-variable-updates}\r\n\r\nThe sparse update ops modify a subset of the entries in a dense\r\n\\lstinline{Variable}, either overwriting the entries or adding /\r\nsubtracting a delta. These are useful for training embedding models and\r\nsimilar lookup-based networks, since only a small subset of embedding\r\nvectors change in any given step.\r\n\r\nSince a sparse update of a large tensor may be generated automatically\r\nduring gradient computation (as in the gradient of\r\n\\href{../../api_docs/python/array_ops.md\\#gather}{\\lstinline{tf.gather}}),\r\nan \\protect\\hyperlink{IndexedSlices}{\\lstinline{IndexedSlices}} class is\r\nprovided that encapsulates a set of sparse indices and values.\r\n\\lstinline{IndexedSlices} objects are detected and handled automatically by\r\nthe optimizers in most cases.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.scatter\\_update(ref,\\ indices,\\ updates,\\ use\\_locking=None,\\ name=None)}\r\n}{tf.scatter\\_update(ref, indices, updates, use\\_locking=None, name=None) }}\\label{tf.scatterux5fupdateref-indices-updates-useux5flockingnone-namenone}\r\n\r\nApplies sparse updates to a variable reference.\r\n\r\nThis operation computes\r\n\r\n\\begin{verbatim}\r\n# Scalar indices\r\nref[indices, ...] = updates[...]\r\n\r\n# Vector indices (for each i)\r\nref[indices[i], ...] = updates[i, ...]\r\n\r\n# High rank indices (for each i, ..., j)\r\nref[indices[i, ..., j], ...] = updates[i, ..., j, ...]\r\n\\end{verbatim}\r\n\r\nThis operation outputs \\lstinline{ref} after the update is done. This makes\r\nit easier to chain operations that need to use the reset value.\r\n\r\nIf \\lstinline{indices} contains duplicate entries, lexicographically later\r\nentries override earlier entries.\r\n\r\nRequires \\lstinline{updates.shape\\ =\\ indices.shape\\ +\\ ref.shape{[}1:{]}}.\r\n\r\n\\subparagraph{Args: }\\label{args-23}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ref}: A mutable \\lstinline{Tensor}. Should be from a\r\n  \\lstinline{Variable} node.\r\n\\item\r\n  \\lstinline{indices}: A \\lstinline{Tensor}. Must be one of the following\r\n  types: \\lstinline{int32}, \\lstinline{int64}. A tensor of indices into the\r\n  first dimension of \\lstinline{ref}.\r\n\\item\r\n  \\lstinline{updates}: A \\lstinline{Tensor}. Must have the same type as\r\n  \\lstinline{ref}. A tensor of updated values to store in \\lstinline{ref}.\r\n\\item\r\n  \\lstinline{use\\_locking}: An optional \\lstinline{bool}. Defaults to\r\n  \\lstinline{True}. If True, the assignment will be protected by a lock;\r\n  otherwise the behavior is undefined, but may exhibit less contention.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-25}\r\n\r\nSame as \\lstinline{ref}. Returned as a convenience for operations that want\r\nto use the updated values after the update is done.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.scatter\\_add(ref,\\ indices,\\ updates,\\ use\\_locking=None,\\ name=None)}\r\n}{tf.scatter\\_add(ref, indices, updates, use\\_locking=None, name=None) }}\\label{tf.scatterux5faddref-indices-updates-useux5flockingnone-namenone}\r\n\r\nAdds sparse updates to a variable reference.\r\n\r\nThis operation computes\r\n\r\n\\begin{verbatim}\r\n# Scalar indices\r\nref[indices, ...] += updates[...]\r\n\r\n# Vector indices (for each i)\r\nref[indices[i], ...] += updates[i, ...]\r\n\r\n# High rank indices (for each i, ..., j)\r\nref[indices[i, ..., j], ...] += updates[i, ..., j, ...]\r\n\\end{verbatim}\r\n\r\nThis operation outputs \\lstinline{ref} after the update is done. This makes\r\nit easier to chain operations that need to use the reset value.\r\n\r\nDuplicate entries are handled correctly: if multiple \\lstinline{indices}\r\nreference the same location, their contributions add.\r\n\r\nRequires \\lstinline{updates.shape\\ =\\ indices.shape\\ +\\ ref.shape{[}1:{]}}.\r\n\r\n\\subparagraph{Args: }\\label{args-24}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ref}: A mutable \\lstinline{Tensor}. Must be one of the following\r\n  types: \\lstinline{float32}, \\lstinline{float64}, \\lstinline{int64},\r\n  \\lstinline{int32}, \\lstinline{uint8}, \\lstinline{int16}, \\lstinline{int8},\r\n  \\lstinline{complex64}, \\lstinline{qint8}, \\lstinline{quint8}, \\lstinline{qint32}.\r\n  Should be from a \\lstinline{Variable} node.\r\n\\item\r\n  \\lstinline{indices}: A \\lstinline{Tensor}. Must be one of the following\r\n  types: \\lstinline{int32}, \\lstinline{int64}. A tensor of indices into the\r\n  first dimension of \\lstinline{ref}.\r\n\\item\r\n  \\lstinline{updates}: A \\lstinline{Tensor}. Must have the same type as\r\n  \\lstinline{ref}. A tensor of updated values to add to \\lstinline{ref}.\r\n\\item\r\n  \\lstinline{use\\_locking}: An optional \\lstinline{bool}. Defaults to\r\n  \\lstinline{False}. If True, the addition will be protected by a lock;\r\n  otherwise the behavior is undefined, but may exhibit less contention.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-26}\r\n\r\nSame as \\lstinline{ref}. Returned as a convenience for operations that want\r\nto use the updated values after the update is done.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.scatter\\_sub(ref,\\ indices,\\ updates,\\ use\\_locking=None,\\ name=None)}\r\n}{tf.scatter\\_sub(ref, indices, updates, use\\_locking=None, name=None) }}\\label{tf.scatterux5fsubref-indices-updates-useux5flockingnone-namenone}\r\n\r\nSubtracts sparse updates to a variable reference.\r\n\r\n\\begin{verbatim}\r\n# Scalar indices\r\nref[indices, ...] -= updates[...]\r\n\r\n# Vector indices (for each i)\r\nref[indices[i], ...] -= updates[i, ...]\r\n\r\n# High rank indices (for each i, ..., j)\r\nref[indices[i, ..., j], ...] -= updates[i, ..., j, ...]\r\n\\end{verbatim}\r\n\r\nThis operation outputs \\lstinline{ref} after the update is done. This makes\r\nit easier to chain operations that need to use the reset value.\r\n\r\nDuplicate entries are handled correctly: if multiple \\lstinline{indices}\r\nreference the same location, their (negated) contributions add.\r\n\r\nRequires \\lstinline{updates.shape\\ =\\ indices.shape\\ +\\ ref.shape{[}1:{]}}.\r\n\r\n\\subparagraph{Args: }\\label{args-25}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ref}: A mutable \\lstinline{Tensor}. Must be one of the following\r\n  types: \\lstinline{float32}, \\lstinline{float64}, \\lstinline{int64},\r\n  \\lstinline{int32}, \\lstinline{uint8}, \\lstinline{int16}, \\lstinline{int8},\r\n  \\lstinline{complex64}, \\lstinline{qint8}, \\lstinline{quint8}, \\lstinline{qint32}.\r\n  Should be from a \\lstinline{Variable} node.\r\n\\item\r\n  \\lstinline{indices}: A \\lstinline{Tensor}. Must be one of the following\r\n  types: \\lstinline{int32}, \\lstinline{int64}. A tensor of indices into the\r\n  first dimension of \\lstinline{ref}.\r\n\\item\r\n  \\lstinline{updates}: A \\lstinline{Tensor}. Must have the same type as\r\n  \\lstinline{ref}. A tensor of updated values to subtract from\r\n  \\lstinline{ref}.\r\n\\item\r\n  \\lstinline{use\\_locking}: An optional \\lstinline{bool}. Defaults to\r\n  \\lstinline{False}. If True, the subtraction will be protected by a lock;\r\n  otherwise the behavior is undefined, but may exhibit less contention.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-27}\r\n\r\nSame as \\lstinline{ref}. Returned as a convenience for operations that want\r\nto use the updated values after the update is done.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.sparse\\_mask(a,\\ mask\\_indices,\\ name=None)}\r\n}{tf.sparse\\_mask(a, mask\\_indices, name=None) }}\\label{tf.sparseux5fmaska-maskux5findices-namenone}\r\n\r\nMasks elements of \\lstinline{IndexedSlices}.\r\n\r\nGiven an \\lstinline{IndexedSlices} instance \\lstinline{a}, returns another\r\n\\lstinline{IndexedSlices} that contains a subset of the slices of\r\n\\lstinline{a}. Only the slices at indices specified in\r\n\\lstinline{mask\\_indices} are returned.\r\n\r\nThis is useful when you need to extract a subset of slices in an\r\n\\lstinline{IndexedSlices} object.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# `a` contains slices at indices [12, 26, 37, 45] from a large tensor}\r\n\\CommentTok{# with shape [1000, 10]}\r\n\\NormalTok{a.indices }\\OperatorTok{=>} \\NormalTok{[}\\DecValTok{12}\\NormalTok{, }\\DecValTok{26}\\NormalTok{, }\\DecValTok{37}\\NormalTok{, }\\DecValTok{45}\\NormalTok{]}\r\n\\NormalTok{tf.shape(a.values) }\\OperatorTok{=>} \\NormalTok{[}\\DecValTok{4}\\NormalTok{, }\\DecValTok{10}\\NormalTok{]}\r\n\r\n\\CommentTok{# `b` will be the subset of `a` slices at its second and third indices, so}\r\n\\CommentTok{# we want to mask of its first and last indices (which are at absolute}\r\n\\CommentTok{# indices 12, 45)}\r\n\\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.sparse_mask(a, [}\\DecValTok{12}\\NormalTok{, }\\DecValTok{45}\\NormalTok{])}\r\n\r\n\\NormalTok{b.indices }\\OperatorTok{=>} \\NormalTok{[}\\DecValTok{26}\\NormalTok{, }\\DecValTok{37}\\NormalTok{]}\r\n\\NormalTok{tf.shape(b.values) }\\OperatorTok{=>} \\NormalTok{[}\\DecValTok{2}\\NormalTok{, }\\DecValTok{10}\\NormalTok{]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-26}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{a}: An \\lstinline{IndexedSlices} instance.\r\n\\item\r\n  \\lstinline{mask\\_indices}: Indices of elements to mask.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-28}\r\n\r\nThe masked \\lstinline{IndexedSlices} instance.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{class\\ tf.IndexedSlices}\r\n}{class tf.IndexedSlices }}\\label{class-tf.indexedslices}\r\n\r\nA sparse representation of a set of tensor slices at given indices.\r\n\r\nThis class is a simple wrapper for a pair of \\lstinline{Tensor} objects:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{values}: A \\lstinline{Tensor} of any dtype with shape\r\n  \\lstinline{{[}D0,\\ D1,\\ ...,\\ Dn{]}}.\r\n\\item\r\n  \\lstinline{indices}: A 1-D integer \\lstinline{Tensor} with shape\r\n  \\lstinline{{[}D0{]}}.\r\n\\end{itemize}\r\n\r\nAn \\lstinline{IndexedSlices} is typically used to represent a subset of a\r\nlarger tensor \\lstinline{dense} of shape\r\n\\lstinline{{[}LARGE0,\\ D1,\\ ..\\ ,\\ DN{]}} where\r\n\\lstinline{LARGE0\\ \\textgreater{}\\textgreater{}\\ D0}. The values in\r\n\\lstinline{indices} are the indices in the first dimension of the slices\r\nthat have been extracted from the larger tensor.\r\n\r\nThe dense tensor \\lstinline{dense} represented by an \\lstinline{IndexedSlices}\r\n\\lstinline{slices} has\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{dense[slices.indices[i], :, :, :, ...] }\\OperatorTok{=} \\NormalTok{slices.values[i, :, :, :, ...]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nThe \\lstinline{IndexedSlices} class is used principally in the definition\r\nof gradients for operations that have sparse gradients (e.g.\r\n\\href{../../api_docs/python/array_ops.md\\#gather}{\\lstinline{tf.gather}}).\r\n\r\nContrast this representation with\r\n\\href{../../api_docs/python/sparse_ops.md\\#SparseTensor}{\\lstinline{SparseTensor}},\r\nwhich uses multi-dimensional indices and scalar values.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.IndexedSlices.\\_\\_init\\_\\_(values,\\ indices,\\ dense\\_shape=None)}\r\n}{tf.IndexedSlices.\\_\\_init\\_\\_(values, indices, dense\\_shape=None) }}\\label{tf.indexedslices.ux5fux5finitux5fux5fvalues-indices-denseux5fshapenone}\r\n\r\nCreates an \\lstinline{IndexedSlices}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.IndexedSlices.values}\r\n}{tf.IndexedSlices.values }}\\label{tf.indexedslices.values}\r\n\r\nA \\lstinline{Tensor} containing the values of the slices.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.IndexedSlices.indices}\r\n}{tf.IndexedSlices.indices }}\\label{tf.indexedslices.indices}\r\n\r\nA 1-D \\lstinline{Tensor} containing the indices of the slices.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.IndexedSlices.dense\\_shape}\r\n}{tf.IndexedSlices.dense\\_shape }}\\label{tf.indexedslices.denseux5fshape}\r\n\r\nA 1-D \\lstinline{Tensor} containing the shape of the corresponding dense\r\ntensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.IndexedSlices.name}\r\n}{tf.IndexedSlices.name }}\\label{tf.indexedslices.name}\r\n\r\nThe name of this \\lstinline{IndexedSlices}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.IndexedSlices.dtype}\r\n}{tf.IndexedSlices.dtype }}\\label{tf.indexedslices.dtype}\r\n\r\nThe \\lstinline{DType} of elements in this tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.IndexedSlices.device}\r\n}{tf.IndexedSlices.device }}\\label{tf.indexedslices.device}\r\n\r\nThe name of the device on which \\lstinline{values} will be produced, or\r\n\\lstinline{None}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\lstinline{tf.IndexedSlices.op}\r\n}{tf.IndexedSlices.op }}\\label{tf.indexedslices.op}\r\n\r\nThe \\lstinline{Operation} that produces \\lstinline{values} as an output.\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/python/c4s04_array_ops.tex",
    "content": "\r\n\r\n\\section{Tensor Transformations }\\label{tensor-transformations}\r\n\r\nNote: Functions taking \\lstinline{Tensor} arguments can also take anything\r\naccepted by\r\n\\href{../../api_docs/python/framework.md\\#convert_to_tensor}{\\lstinline{tf.convert_to_tensor}}.\r\n\r\n\\subsection{Contents}\\label{contents}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-tensor-transformations}{Tensor\r\nTransformations}}{Tensor Transformations}}\\label{tensor-transformations-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-casting}{Casting}\r\n\\item\r\n  \\protect\\hyperlink{stringux5ftoux5fnumber}{\\lstinline{tf.string_to_number(string_tensor, out_type=None, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{toux5fdouble}{\\lstinline{tf.to_double(x, name=\\textquotesingle{}ToDouble\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{toux5ffloat}{\\lstinline{tf.to_float(x, name=\\textquotesingle{}ToFloat\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{toux5fbfloat16}{\\lstinline{tf.to_bfloat16(x, name=\\textquotesingle{}ToBFloat16\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{toux5fint32}{\\lstinline{tf.to_int32(x, name=\\textquotesingle{}ToInt32\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{toux5fint64}{\\lstinline{tf.to_int64(x, name=\\textquotesingle{}ToInt64\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{cast}{\\lstinline{tf.cast(x, dtype, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-shapes-and-shaping}{Shapes and\r\n  Shaping}\r\n\\item\r\n  \\protect\\hyperlink{shape}{\\lstinline{tf.shape(input, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{size}{\\lstinline{tf.size(input, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{rank}{\\lstinline{tf.rank(input, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{reshape}{\\lstinline{tf.reshape(tensor, shape, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{squeeze}{\\lstinline{tf.squeeze(input, squeeze_dims=None, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{expandux5fdims}{\\lstinline{tf.expand_dims(input, dim, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-slicing-and-joining}{Slicing and\r\n  Joining}\r\n\\item\r\n  \\protect\\hyperlink{slice}{\\lstinline{tf.slice(input_, begin, size, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{split}{\\lstinline{tf.split(split_dim, num_split, value, name=\\textquotesingle{}split\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{tile}{\\lstinline{tf.tile(input, multiples, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{pad}{\\lstinline{tf.pad(input, paddings, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{concat}{\\lstinline{tf.concat(concat_dim, values, name=\\textquotesingle{}concat\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{pack}{\\lstinline{tf.pack(values, name=\\textquotesingle{}pack\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{unpack}{\\lstinline{tf.unpack(value, num=None, name=\\textquotesingle{}unpack\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{reverseux5fsequence}{\\lstinline{tf.reverse_sequence(input, seq_lengths, seq_dim, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{reverse}{\\lstinline{tf.reverse(tensor, dims, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{transpose}{\\lstinline{tf.transpose(a, perm=None, name=\\textquotesingle{}transpose\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{gather}{\\lstinline{tf.gather(params, indices, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{dynamicux5fpartition}{\\lstinline{tf.dynamic_partition(data, partitions, num_partitions, name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{dynamicux5fstitch}{\\lstinline{tf.dynamic_stitch(indices, data, name=None)}}\r\n\\end{itemize}\r\n\r\n\\subsection{Casting }\\label{casting}\r\n\r\nTensorFlow provides several operations that you can use to cast tensor\r\ndata types in your graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.string_to_number(string_tensor, out_type=None, name=None)}\r\n}{tf.string_to_number(string_tensor, out_type=None, name=None) }}\\label{tf.stringux5ftoux5fnumberstringux5ftensor-outux5ftypenone-namenone}\r\n\r\nConverts each string in the input Tensor to the specified numeric type.\r\n\r\n(Note that int32 overflow results in an error while float overflow\r\nresults in a rounded value.)\r\n\r\n\\subparagraph{Args: }\\label{args}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item \\lstinline{string_tensor}: A \\lstinline{Tensor} of type \\lstinline{string}.\r\n\\item \\lstinline{out_type}: An optional \\lstinline{tf.DType} from: \\lstinline{tf.float32, tf.int32}. Defaults to \\lstinline{tf.float32}. The numeric type to interpret each string in string_tensor as.\r\n\\item \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns}\r\n\r\nA \\lstinline{Tensor} of type \\lstinline{out_type}. A Tensor of the same shape\r\nas the input string_tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.to_double(x, name=\\textquotesingle{}ToDouble\\textquotesingle{})}\r\n}{tf.to_double(x, name='ToDouble') }}\\label{tf.toux5fdoublex-nametodouble}\r\n\r\nCasts a tensor to type \\lstinline{float64}.\r\n\r\n\\subparagraph{Args: }\\label{args-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{x}: A \\lstinline{Tensor} or \\lstinline{SparseTensor}.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-1}\r\n\r\nA \\lstinline{Tensor} or \\lstinline{SparseTensor} with same shape as \\lstinline{x}\r\nwith type \\lstinline{float64}.\r\n\r\n\\subparagraph{Raises: }\\label{raises}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{x} cannot be cast to the\r\n  \\lstinline{float64}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.to_float(x, name=\\textquotesingle{}ToFloat\\textquotesingle{})}\r\n}{tf.to_float(x, name='ToFloat') }}\\label{tf.toux5ffloatx-nametofloat}\r\n\r\nCasts a tensor to type \\lstinline{float32}.\r\n\r\n\\subparagraph{Args: }\\label{args-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{x}: A \\lstinline{Tensor} or \\lstinline{SparseTensor}.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-2}\r\n\r\nA \\lstinline{Tensor} or \\lstinline{SparseTensor} with same shape as \\lstinline{x}\r\nwith type \\lstinline{float32}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{x} cannot be cast to the\r\n  \\lstinline{float32}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.to_bfloat16(x, name=\\textquotesingle{}ToBFloat16\\textquotesingle{})}\r\n}{tf.to_bfloat16(x, name='ToBFloat16') }}\\label{tf.toux5fbfloat16x-nametobfloat16}\r\n\r\nCasts a tensor to type \\lstinline{bfloat16}.\r\n\r\n\\subparagraph{Args: }\\label{args-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{x}: A \\lstinline{Tensor} or \\lstinline{SparseTensor}.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-3}\r\n\r\nA \\lstinline{Tensor} or \\lstinline{SparseTensor} with same shape as \\lstinline{x}\r\nwith type \\lstinline{bfloat16}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{x} cannot be cast to the\r\n  \\lstinline{bfloat16}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.to_int32(x, name=\\textquotesingle{}ToInt32\\textquotesingle{})}\r\n}{tf.to_int32(x, name='ToInt32') }}\\label{tf.toux5fint32x-nametoint32}\r\n\r\nCasts a tensor to type \\lstinline{int32}.\r\n\r\n\\subparagraph{Args: }\\label{args-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{x}: A \\lstinline{Tensor} or \\lstinline{SparseTensor}.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-4}\r\n\r\nA \\lstinline{Tensor} or \\lstinline{SparseTensor} with same shape as \\lstinline{x}\r\nwith type \\lstinline{int32}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{x} cannot be cast to the\r\n  \\lstinline{int32}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.to_int64(x, name=\\textquotesingle{}ToInt64\\textquotesingle{})}\r\n}{tf.to_int64(x, name='ToInt64') }}\\label{tf.toux5fint64x-nametoint64}\r\n\r\nCasts a tensor to type \\lstinline{int64}.\r\n\r\n\\subparagraph{Args: }\\label{args-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{x}: A \\lstinline{Tensor} or \\lstinline{SparseTensor}.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-5}\r\n\r\nA \\lstinline{Tensor} or \\lstinline{SparseTensor} with same shape as \\lstinline{x}\r\nwith type \\lstinline{int64}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{x} cannot be cast to the\r\n  \\lstinline{int64}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.cast(x, dtype, name=None)}\r\n}{tf.cast(x, dtype, name=None) }}\\label{tf.castx-dtype-namenone}\r\n\r\nCasts a tensor to a new type.\r\n\r\nThe operation casts \\lstinline{x} (in case of \\lstinline{Tensor}) or\r\n\\lstinline{x.values} (in case of \\lstinline{SparseTensor}) to \\lstinline{dtype}.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# tensor `a` is [1.8, 2.2], dtype=tf.float}\r\n\\NormalTok{tf.cast(a, tf.int32) }\\OperatorTok{==>} \\NormalTok{[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{2}\\NormalTok{]  }\\CommentTok{# dtype=tf.int32}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{x}: A \\lstinline{Tensor} or \\lstinline{SparseTensor}.\r\n\\item\r\n  \\lstinline{dtype}: The destination type.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-6}\r\n\r\nA \\lstinline{Tensor} or \\lstinline{SparseTensor} with same shape as\r\n\\lstinline{x}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{TypeError}: If \\lstinline{x} cannot be cast to the\r\n  \\lstinline{dtype}.\r\n\\end{itemize}\r\n\r\n\\subsection{Shapes and Shaping }\\label{shapes-and-shaping}\r\n\r\nTensorFlow provides several operations that you can use to determine the\r\nshape of a tensor and change the shape of a tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.shape(input, name=None)}\r\n}{tf.shape(input, name=None) }}\\label{tf.shapeinput-namenone}\r\n\r\nReturns the shape of a tensor.\r\n\r\nThis operation returns a 1-D integer tensor representing the shape of\r\n\\lstinline{input}.\r\n\r\nFor example:\r\n\r\n\\begin{lstlisting}\r\n# 't' is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]\r\nshape(t) ==> [2, 2, 3]\r\n\\end{lstlisting}\r\n\r\n\\subparagraph{Args: }\\label{args-7}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{input}: A \\lstinline{Tensor}.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-7}\r\n\r\nA \\lstinline{Tensor} of type \\lstinline{int32}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.size(input, name=None)}\r\n}{tf.size(input, name=None) }}\\label{tf.sizeinput-namenone}\r\n\r\nReturns the size of a tensor.\r\n\r\nThis operation returns an integer representing the number of elements in\r\n\\lstinline{input}.\r\n\r\nFor example:\r\n\r\n\\begin{lstlisting}\r\n# 't' is [[[1, 1,, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]]\r\nsize(t) ==> 12\r\n\\end{lstlisting}\r\n\r\n\\subparagraph{Args: }\\label{args-8}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{input}: A \\lstinline{Tensor}.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-8}\r\n\r\nA \\lstinline{Tensor} of type \\lstinline{int32}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.rank(input, name=None)}\r\n}{tf.rank(input, name=None) }}\\label{tf.rankinput-namenone}\r\n\r\nReturns the rank of a tensor.\r\n\r\nThis operation returns an integer representing the rank of\r\n\\lstinline{input}.\r\n\r\nFor example:\r\n\r\n\\begin{lstlisting}\r\n# 't' is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]\r\n# shape of tensor 't' is [2, 2, 3]\r\nrank(t) ==> 3\r\n\\end{lstlisting}\r\n\r\n\\textbf{Note}: The rank of a tensor is not the same as the rank of a\r\nmatrix. The rank of a tensor is the number of indices required to\r\nuniquely select each element of the tensor. Rank is also known as\r\n``order'', ``degree'', or ``ndims.''\r\n\r\n\\subparagraph{Args: }\\label{args-9}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{input}: A \\lstinline{Tensor}.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-9}\r\n\r\nA \\lstinline{Tensor} of type \\lstinline{int32}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.reshape(tensor, shape, name=None)}\r\n}{tf.reshape(tensor, shape, name=None) }}\\label{tf.reshapetensor-shape-namenone}\r\n\r\nReshapes a tensor.\r\n\r\nGiven \\lstinline{tensor}, this operation returns a tensor that has the same\r\nvalues as \\lstinline{tensor} with shape \\lstinline{shape}.\r\n\r\nIf \\lstinline{shape} is the special value \\lstinline{{[}-1{]}}, then\r\n\\lstinline{tensor} is flattened and the operation outputs a 1-D tensor with\r\nall elements of \\lstinline{tensor}.\r\n\r\nIf \\lstinline{shape} is 1-D or higher, then the operation returns a tensor\r\nwith shape \\lstinline{shape} filled with the values of \\lstinline{tensor}. In\r\nthis case, the number of elements implied by \\lstinline{shape} must be the\r\nsame as the number of elements in \\lstinline{tensor}.\r\n\r\nFor example:\r\n\r\n\\begin{lstlisting}\r\n# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]\r\n# tensor 't' has shape [9]\r\nreshape(t, [3, 3]) ==> [[1, 2, 3]\r\n                        [4, 5, 6]\r\n                        [7, 8, 9]]\r\n\r\n# tensor 't' is [[[1, 1], [2, 2]]\r\n#                [[3, 3], [4, 4]]]\r\n# tensor 't' has shape [2, 2]\r\nreshape(t, [2, 4]) ==> [[1, 1, 2, 2]\r\n                        [3, 3, 4, 4]]\r\n\r\n# tensor 't' is [[[1, 1, 1],\r\n#                 [2, 2, 2]],\r\n#                [[3, 3, 3],\r\n#                 [4, 4, 4]],\r\n#                [[5, 5, 5],\r\n#                 [6, 6, 6]]]\r\n# tensor 't' has shape [3, 2, 3]\r\n# pass '[-1]' to flatten 't'\r\nreshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]\r\n\\end{lstlisting}\r\n\r\n\\subparagraph{Args: }\\label{args-10}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{tensor}: A \\lstinline{Tensor}.\r\n\\item\r\n  \\lstinline{shape}: A \\lstinline{Tensor} of type \\lstinline{int32}. Defines the\r\n  shape of the output tensor.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-10}\r\n\r\nA \\lstinline{Tensor}. Has the same type as \\lstinline{tensor}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.squeeze(input, squeeze_dims=None, name=None)}\r\n}{tf.squeeze(input, squeeze_dims=None, name=None) }}\\label{tf.squeezeinput-squeezeux5fdimsnone-namenone}\r\n\r\nRemoves dimensions of size 1 from the shape of a tensor.\r\n\r\nGiven a tensor \\lstinline{input}, this operation returns a tensor of the\r\nsame type with all dimensions of size 1 removed. If you don't want to\r\nremove all size 1 dimensions, you can remove specific size 1 dimensions\r\nby specifying \\lstinline{squeeze_dims}.\r\n\r\nFor example:\r\n\r\n\\begin{lstlisting}\r\n# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]\r\nshape(squeeze(t)) ==> [2, 3]\r\n\\end{lstlisting}\r\n\r\nOr, to remove specific size 1 dimensions:\r\n\r\n\\begin{lstlisting}\r\n# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]\r\nshape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1]\r\n\\end{lstlisting}\r\n\r\n\\subparagraph{Args: }\\label{args-11}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{input}: A \\lstinline{Tensor}. The \\lstinline{input} to squeeze.\r\n\\item\r\n  \\lstinline{squeeze_dims}: An optional list of \\lstinline{ints}. Defaults to\r\n  \\lstinline{{[}{]}}. If specified, only squeezes the dimensions listed.\r\n  The dimension index starts at 0. It is an error to squeeze a dimension\r\n  that is not 1.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-11}\r\n\r\nA \\lstinline{Tensor}. Has the same type as \\lstinline{input}. Contains the\r\nsame data as \\lstinline{input}, but has one or more dimensions of size 1\r\nremoved.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.expand_dims(input, dim, name=None)}\r\n}{tf.expand_dims(input, dim, name=None) }}\\label{tf.expandux5fdimsinput-dim-namenone}\r\n\r\nInserts a dimension of 1 into a tensor's shape.\r\n\r\nGiven a tensor \\lstinline{input}, this operation inserts a dimension of 1\r\nat the dimension index \\lstinline{dim} of \\lstinline{input}'s shape. The\r\ndimension index \\lstinline{dim} starts at zero; if you specify a negative\r\nnumber for \\lstinline{dim} it is counted backward from the end.\r\n\r\nThis operation is useful if you want to add a batch dimension to a\r\nsingle element. For example, if you have a single image of shape\r\n\\lstinline{{[}height, width, channels{]}}, you can make it a batch of 1\r\nimage with \\lstinline{expand_dims(image, 0)}, which will make the shape\r\n\\lstinline{{[}1, height, width, channels{]}}.\r\n\r\nOther examples:\r\n\r\n\\begin{lstlisting}\r\n# 't' is a tensor of shape [2]\r\nshape(expand_dims(t, 0)) ==> [1, 2]\r\nshape(expand_dims(t, 1)) ==> [2, 1]\r\nshape(expand_dims(t, -1)) ==> [2, 1]\r\n\r\n# 't2' is a tensor of shape [2, 3, 5]\r\nshape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]\r\nshape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]\r\nshape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]\r\n\\end{lstlisting}\r\n\r\nThis operation requires that:\r\n\r\n\\lstinline{-1-input.dims() \\textless{}= dim \\textless{}= input.dims()}\r\n\r\nThis operation is related to \\lstinline{squeeze()}, which removes\r\ndimensions of size 1.\r\n\r\n\\subparagraph{Args: }\\label{args-12}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{input}: A \\lstinline{Tensor}.\r\n\\item\r\n  \\lstinline{dim}: A \\lstinline{Tensor} of type \\lstinline{int32}. 0-D (scalar).\r\n  Specifies the dimension index at which to expand the shape of\r\n  \\lstinline{input}.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-12}\r\n\r\nA \\lstinline{Tensor}. Has the same type as \\lstinline{input}. Contains the\r\nsame data as \\lstinline{input}, but its shape has an additional dimension\r\nof size 1 added.\r\n\r\n\\subsection{Slicing and Joining }\\label{slicing-and-joining}\r\n\r\nTensorFlow provides several operations to slice or extract parts of a\r\ntensor, or join multiple tensors together.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.slice(input_, begin, size, name=None)}\r\n}{tf.slice(input_, begin, size, name=None) }}\\label{tf.sliceinputux5f-begin-size-namenone}\r\n\r\nExtracts a slice from a tensor.\r\n\r\nThis operation extracts a slice of size \\lstinline{size} from a tensor\r\n\\lstinline{input} starting at the location specified by \\lstinline{begin}. The\r\nslice \\lstinline{size} is represented as a tensor shape, where\r\n\\lstinline{size{[}i{]}} is the number of elements of the 'i'th dimension of\r\n\\lstinline{input} that you want to slice. The starting location\r\n(\\lstinline{begin}) for the slice is represented as an offset in each\r\ndimension of \\lstinline{input}. In other words, \\lstinline{begin{[}i{]}} is\r\nthe offset into the 'i'th dimension of \\lstinline{input} that you want to\r\nslice from.\r\n\r\n\\lstinline{begin} is zero-based; \\lstinline{size} is one-based. If\r\n\\lstinline{size{[}i{]}} is -1, all remaining elements in dimension i are\r\nincluded in the slice. In other words, this is equivalent to setting:\r\n\r\n\\lstinline{size{[}i{]} = input.dim_size(i) - begin{[}i{]}}\r\n\r\nThis operation requires that:\r\n\r\n\\lstinline{0 \\textless{}= begin{[}i{]} \\textless{}= begin{[}i{]} + size{[}i{]} \\textless{}= Di  for i in {[}0, n{]}}\r\n\r\nFor example:\r\n\r\n\\begin{lstlisting}\r\n# 'input' is [[[1, 1, 1], [2, 2, 2]],\r\n#             [[3, 3, 3], [4, 4, 4]],\r\n#             [[5, 5, 5], [6, 6, 6]]]\r\ntf.slice(input, [1, 0, 0], [1, 1, 3]) ==> [[[3, 3, 3]]]\r\ntf.slice(input, [1, 0, 0], [1, 2, 3]) ==> [[[3, 3, 3],\r\n                                            [4, 4, 4]]]\r\ntf.slice(input, [1, 0, 0], [2, 1, 3]) ==> [[[3, 3, 3]],\r\n                                           [[5, 5, 5]]]\r\n\\end{lstlisting}\r\n\r\n\\subparagraph{Args: }\\label{args-13}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{input_}: A \\lstinline{Tensor}.\r\n\\item\r\n  \\lstinline{begin}: An \\lstinline{int32} or \\lstinline{int64} \\lstinline{Tensor}.\r\n\\item\r\n  \\lstinline{size}: An \\lstinline{int32} or \\lstinline{int64} \\lstinline{Tensor}.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-13}\r\n\r\nA \\lstinline{Tensor} the same type as \\lstinline{input}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.split(split_dim, num_split, value, name=\\textquotesingle{}split\\textquotesingle{})}\r\n}{tf.split(split_dim, num_split, value, name='split') }}\\label{tf.splitsplitux5fdim-numux5fsplit-value-namesplit}\r\n\r\nSplits a tensor into \\lstinline{num_split} tensors along one dimension.\r\n\r\nSplits \\lstinline{value} along dimension \\lstinline{split_dim} into\r\n\\lstinline{num_split} smaller tensors. Requires that \\lstinline{num_split}\r\nevenly divide \\lstinline{value.shape{[}split_dim{]}}.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 'value' is a tensor with shape [5, 30]}\r\n\\CommentTok{# Split 'value' into 3 tensors along dimension 1}\r\n\\NormalTok{split0, split1, split2 }\\OperatorTok{=} \\NormalTok{tf.split(}\\DecValTok{1}\\NormalTok{, }\\DecValTok{3}\\NormalTok{, value)}\r\n\\NormalTok{tf.shape(split0) }\\OperatorTok{==>} \\NormalTok{[}\\DecValTok{5}\\NormalTok{, }\\DecValTok{10}\\NormalTok{]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-14}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{split_dim}: A 0-D \\lstinline{int32} \\lstinline{Tensor}. The\r\n  dimension along which to split. Must be in the range\r\n  \\lstinline{{[}0, rank(value))}.\r\n\\item\r\n  \\lstinline{num_split}: A 0-D \\lstinline{int32} \\lstinline{Tensor}. The number\r\n  of ways to split.\r\n\\item\r\n  \\lstinline{value}: The \\lstinline{Tensor} to split.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-14}\r\n\r\n\\lstinline{num_split} \\lstinline{Tensor} objects resulting from splitting\r\n\\lstinline{value}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.tile(input, multiples, name=None)}\r\n}{tf.tile(input, multiples, name=None) }}\\label{tf.tileinput-multiples-namenone}\r\n\r\nConstructs a tensor by tiling a given tensor.\r\n\r\nThis operation creates a new tensor by replicating \\lstinline{input}\r\n\\lstinline{multiples} times. The output tensor's i'th dimension has\r\n\\lstinline{input.dims(i) * multiples{[}i{]}} elements, and the values of\r\n\\lstinline{input} are replicated \\lstinline{multiples{[}i{]}} times along the\r\n'i'th dimension. For example, tiling \\lstinline{{[}a b c d{]}} by\r\n\\lstinline{{[}2{]}} produces \\lstinline{{[}a b c d a b c d{]}}.\r\n\r\n\\subparagraph{Args: }\\label{args-15}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{input}: A \\lstinline{Tensor}. 1-D or higher.\r\n\\item\r\n  \\lstinline{multiples}: A \\lstinline{Tensor} of type \\lstinline{int32}. 1-D.\r\n  Length must be the same as the number of dimensions in \\lstinline{input}\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-15}\r\n\r\nA \\lstinline{Tensor}. Has the same type as \\lstinline{input}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.pad(input, paddings, name=None)}\r\n}{tf.pad(input, paddings, name=None) }}\\label{tf.padinput-paddings-namenone}\r\n\r\nPads a tensor with zeros.\r\n\r\nThis operation pads a \\lstinline{input} with zeros according to the\r\n\\lstinline{paddings} you specify. \\lstinline{paddings} is an integer tensor\r\nwith shape \\lstinline{{[}Dn, 2{]}}, where n is the rank of \\lstinline{input}.\r\nFor each dimension D of \\lstinline{input}, \\lstinline{paddings{[}D, 0{]}}\r\nindicates how many zeros to add before the contents of \\lstinline{input} in\r\nthat dimension, and \\lstinline{paddings{[}D, 1{]}} indicates how many\r\nzeros to add after the contents of \\lstinline{input} in that dimension.\r\n\r\nThe padded size of each dimension D of the output is:\r\n\r\n\\lstinline{paddings(D, 0) + input.dim_size(D) + paddings(D, 1)}\r\n\r\nFor example:\r\n\r\n\\begin{lstlisting}\r\n# 't' is [[1, 1], [2, 2]]\r\n# 'paddings' is [[1, 1]], [2, 2]]\r\n# rank of 't' is 2\r\npad(t, paddings) ==> [[0, 0, 0, 0, 0]\r\n                      [0, 0, 0, 0, 0]\r\n                      [0, 1, 1, 0, 0]\r\n                     [[0, 2, 2, 0, 0]\r\n                      [0, 0, 0, 0, 0]]\r\n\\end{lstlisting}\r\n\r\n\\subparagraph{Args: }\\label{args-16}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{input}: A \\lstinline{Tensor}.\r\n\\item\r\n  \\lstinline{paddings}: A \\lstinline{Tensor} of type \\lstinline{int32}.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-16}\r\n\r\nA \\lstinline{Tensor}. Has the same type as \\lstinline{input}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.concat(concat_dim, values, name=\\textquotesingle{}concat\\textquotesingle{})}\r\n}{tf.concat(concat_dim, values, name='concat') }}\\label{tf.concatconcatux5fdim-values-nameconcat}\r\n\r\nConcatenates tensors along one dimension.\r\n\r\nConcatenates the list of tensors \\lstinline{values} along dimension\r\n\\lstinline{concat_dim}. If\r\n\\lstinline{values{[}i{]}.shape = {[}D0, D1, ... Dconcat_dim(i), ...Dn{]}},\r\nthe concatenated result has shape\r\n\r\n\\begin{lstlisting}\r\n[D0, D1, ... Rconcat_dim, ...Dn]\r\n\\end{lstlisting}\r\n\r\nwhere\r\n\r\n\\begin{lstlisting}\r\nRconcat_dim = sum(Dconcat_dim(i))\r\n\\end{lstlisting}\r\n\r\nThat is, the data from the input tensors is joined along the\r\n\\lstinline{concat_dim} dimension.\r\n\r\nThe number of dimensions of the input tensors must match, and all\r\ndimensions except \\lstinline{concat_dim} must be equal.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{t1 }\\OperatorTok{=} \\NormalTok{[[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{2}\\NormalTok{, }\\DecValTok{3}\\NormalTok{], [}\\DecValTok{4}\\NormalTok{, }\\DecValTok{5}\\NormalTok{, }\\DecValTok{6}\\NormalTok{]]}\r\n\\NormalTok{t2 }\\OperatorTok{=} \\NormalTok{[[}\\DecValTok{7}\\NormalTok{, }\\DecValTok{8}\\NormalTok{, }\\DecValTok{9}\\NormalTok{], [}\\DecValTok{10}\\NormalTok{, }\\DecValTok{11}\\NormalTok{, }\\DecValTok{12}\\NormalTok{]]}\r\n\\NormalTok{tf.concat(}\\DecValTok{0}\\NormalTok{, [t1, t2]) }\\OperatorTok{==>} \\NormalTok{[[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{2}\\NormalTok{, }\\DecValTok{3}\\NormalTok{], [}\\DecValTok{4}\\NormalTok{, }\\DecValTok{5}\\NormalTok{, }\\DecValTok{6}\\NormalTok{], [}\\DecValTok{7}\\NormalTok{, }\\DecValTok{8}\\NormalTok{, }\\DecValTok{9}\\NormalTok{], [}\\DecValTok{10}\\NormalTok{, }\\DecValTok{11}\\NormalTok{, }\\DecValTok{12}\\NormalTok{]]}\r\n\\NormalTok{tf.concat(}\\DecValTok{1}\\NormalTok{, [t1, t2]) }\\OperatorTok{==>} \\NormalTok{[[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{2}\\NormalTok{, }\\DecValTok{3}\\NormalTok{, }\\DecValTok{7}\\NormalTok{, }\\DecValTok{8}\\NormalTok{, }\\DecValTok{9}\\NormalTok{], [}\\DecValTok{4}\\NormalTok{, }\\DecValTok{5}\\NormalTok{, }\\DecValTok{6}\\NormalTok{, }\\DecValTok{10}\\NormalTok{, }\\DecValTok{11}\\NormalTok{, }\\DecValTok{12}\\NormalTok{]]}\r\n\r\n\\CommentTok{# tensor t3 with shape [2, 3]}\r\n\\CommentTok{# tensor t4 with shape [2, 3]}\r\n\\NormalTok{tf.shape(tf.concat(}\\DecValTok{0}\\NormalTok{, [t3, t4])) }\\OperatorTok{==>} \\NormalTok{[}\\DecValTok{4}\\NormalTok{, }\\DecValTok{3}\\NormalTok{]}\r\n\\NormalTok{tf.shape(tf.concat(}\\DecValTok{1}\\NormalTok{, [t3, t4])) }\\OperatorTok{==>} \\NormalTok{[}\\DecValTok{2}\\NormalTok{, }\\DecValTok{6}\\NormalTok{]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-17}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{concat_dim}: 0-D \\lstinline{int32} \\lstinline{Tensor}. Dimension\r\n  along which to concatenate.\r\n\\item\r\n  \\lstinline{values}: A list of \\lstinline{Tensor} objects or a single\r\n  \\lstinline{Tensor}.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-17}\r\n\r\nA \\lstinline{Tensor} resulting from concatenation of the input tensors.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.pack(values, name=\\textquotesingle{}pack\\textquotesingle{})}\r\n}{tf.pack(values, name='pack') }}\\label{tf.packvalues-namepack}\r\n\r\nPacks a list of rank-\\lstinline{R} tensors into one rank-\\lstinline{(R+1)}\r\ntensor.\r\n\r\nPacks tensors in \\lstinline{values} into a tensor with rank one higher than\r\neach tensor in \\lstinline{values} and shape\r\n\\lstinline{{[}len(values){]} + values{[}0{]}.shape}. The output satisfies\r\n\\lstinline{output{[}i, ...{]} = values{[}i{]}{[}...{]}}.\r\n\r\nThis is the opposite of unpack. The numpy equivalent is\r\n\r\n\\begin{lstlisting}\r\ntf.pack([x, y, z]) = np.asarray([x, y, z])\r\n\\end{lstlisting}\r\n\r\n\\subparagraph{Args: }\\label{args-18}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{values}: A list of \\lstinline{Tensor} objects with the same shape\r\n  and type.\r\n\\item\r\n  \\lstinline{name}: A name for this operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-18}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{output}: A packed \\lstinline{Tensor} with the same type as\r\n  \\lstinline{values}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.unpack(value, num=None, name=\\textquotesingle{}unpack\\textquotesingle{})}\r\n}{tf.unpack(value, num=None, name='unpack') }}\\label{tf.unpackvalue-numnone-nameunpack}\r\n\r\nUnpacks the outer dimension of a rank-\\lstinline{R} tensor into\r\nrank-\\lstinline{(R-1)} tensors.\r\n\r\nUnpacks \\lstinline{num} tensors from \\lstinline{value} along the first\r\ndimension. If \\lstinline{num} is not specified (the default), it is\r\ninferred from \\lstinline{value}'s shape. If \\lstinline{value.shape{[}0{]}} is\r\nnot known, \\lstinline{ValueError} is raised.\r\n\r\nThe ith tensor in \\lstinline{output} is the slice\r\n\\lstinline{value{[}i, ...{]}}. Each tensor in \\lstinline{output} has shape\r\n\\lstinline{value.shape{[}1:{]}}.\r\n\r\nThis is the opposite of pack. The numpy equivalent is\r\n\r\n\\begin{lstlisting}\r\ntf.unpack(x, n) = list(x)\r\n\\end{lstlisting}\r\n\r\n\\subparagraph{Args: }\\label{args-19}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{value}: A rank \\lstinline{R \\textgreater{} 0} \\lstinline{Tensor}\r\n  to be unpacked.\r\n\\item\r\n  \\lstinline{num}: An \\lstinline{int}. The first dimension of value.\r\n  Automatically inferred if \\lstinline{None} (the default).\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-19}\r\n\r\nThe list of \\lstinline{Tensor} objects unpacked from \\lstinline{value}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{ValueError}: If \\lstinline{num} is unspecified and cannot be\r\n  inferred.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.reverse_sequence(input, seq_lengths, seq_dim, name=None)}\r\n}{tf.reverse_sequence(input, seq_lengths, seq_dim, name=None) }}\\label{tf.reverseux5fsequenceinput-sequx5flengths-sequx5fdim-namenone}\r\n\r\nReverses variable length slices in dimension \\lstinline{seq_dim}.\r\n\r\nThis op first slices \\lstinline{input} along the first dimension, and for\r\neach slice \\lstinline{i}, reverses the first \\lstinline{seq_lengths{[}i{]}}\r\nelements along the dimension \\lstinline{seq_dim}.\r\n\r\nThe elements of \\lstinline{seq_lengths} must obey\r\n\\lstinline{seq_lengths{[}i{]} \\textless{} input.dims{[}seq_dim{]}}, and\r\n\\lstinline{seq_lengths} must be a vector of length \\lstinline{input.dims(0)}.\r\n\r\nThe output slice \\lstinline{i} along dimension 0 is then given by input\r\nslice \\lstinline{i}, with the first \\lstinline{seq_lengths{[}i{]}} slices\r\nalong dimension \\lstinline{seq_dim} reversed.\r\n\r\nFor example:\r\n\r\n\\begin{lstlisting}\r\n# Given this:\r\nseq_dim = 1\r\ninput.dims = (4, ...)\r\nseq_lengths = [7, 2, 3, 5]\r\n\r\n# then slices of input are reversed on seq_dim, but only up to seq_lengths:\r\noutput[0, 0:7, :, ...] = input[0, 7:0:-1, :, ...]\r\noutput[1, 0:2, :, ...] = input[1, 2:0:-1, :, ...]\r\noutput[2, 0:3, :, ...] = input[2, 3:0:-1, :, ...]\r\noutput[3, 0:5, :, ...] = input[3, 5:0:-1, :, ...]\r\n\r\n# while entries past seq_lens are copied through:\r\noutput[0, 7:, :, ...] = input[0, 7:, :, ...]\r\noutput[1, 2:, :, ...] = input[1, 2:, :, ...]\r\noutput[2, 3:, :, ...] = input[2, 3:, :, ...]\r\noutput[3, 2:, :, ...] = input[3, 2:, :, ...]\r\n\\end{lstlisting}\r\n\r\n\\subparagraph{Args: }\\label{args-20}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{input}: A \\lstinline{Tensor}. The input to reverse.\r\n\\item\r\n  \\lstinline{seq_lengths}: A \\lstinline{Tensor} of type \\lstinline{int64}. 1-D\r\n  with length \\lstinline{input.dims(0)} and\r\n  \\lstinline{max(seq_lengths) \\textless{} input.dims(seq_dim)}\r\n\\item\r\n  \\lstinline{seq_dim}: An \\lstinline{int}. The dimension which is partially\r\n  reversed.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-20}\r\n\r\nA \\lstinline{Tensor}. Has the same type as \\lstinline{input}. The partially\r\nreversed input. It has the same shape as \\lstinline{input}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.reverse(tensor, dims, name=None)}\r\n}{tf.reverse(tensor, dims, name=None) }}\\label{tf.reversetensor-dims-namenone}\r\n\r\nReverses specific dimensions of a tensor.\r\n\r\nGiven a \\lstinline{tensor}, and a \\lstinline{bool} tensor \\lstinline{dims}\r\nrepresenting the dimensions of \\lstinline{tensor}, this operation reverses\r\neach dimension i of \\lstinline{tensor} where \\lstinline{dims{[}i{]}} is\r\n\\lstinline{True}.\r\n\r\n\\lstinline{tensor} can have up to 8 dimensions. The number of dimensions of\r\n\\lstinline{tensor} must equal the number of elements in \\lstinline{dims}. In\r\nother words:\r\n\r\n\\lstinline{rank(tensor) = size(dims)}\r\n\r\nFor example:\r\n\r\n\\begin{lstlisting}\r\n# tensor 't' is [[[[ 0,  1,  2,  3],\r\n#                  [ 4,  5,  6,  7],\r\n#                  [ 8,  9, 10, 11]],\r\n#                 [[12, 13, 14, 15],\r\n#                  [16, 17, 18, 19],\r\n#                  [20, 21, 22, 23]]]]\r\n# tensor 't' shape is [1, 2, 3, 4]\r\n\r\n# 'dims' is [False, False, False, True]\r\nreverse(t, dims) ==> [[[[ 3,  2,  1,  0],\r\n                        [ 7,  6,  5,  4],\r\n                        [ 11, 10, 9, 8]],\r\n                       [[15, 14, 13, 12],\r\n                        [19, 18, 17, 16],\r\n                        [23, 22, 21, 20]]]]\r\n\r\n# 'dims' is [False, True, False, False]\r\nreverse(t, dims) ==> [[[[12, 13, 14, 15],\r\n                        [16, 17, 18, 19],\r\n                        [20, 21, 22, 23]\r\n                       [[ 0,  1,  2,  3],\r\n                        [ 4,  5,  6,  7],\r\n                        [ 8,  9, 10, 11]]]]\r\n\r\n# 'dims' is [False, False, True, False]\r\nreverse(t, dims) ==> [[[[8, 9, 10, 11],\r\n                        [4, 5, 6, 7],\r\n                        [0, 1, 2, 3]]\r\n                       [[20, 21, 22, 23],\r\n                        [16, 17, 18, 19],\r\n                        [12, 13, 14, 15]]]]\r\n\\end{lstlisting}\r\n\r\n\\subparagraph{Args: }\\label{args-21}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{tensor}: A \\lstinline{Tensor}. Must be one of the following\r\n  types: \\lstinline{uint8}, \\lstinline{int8}, \\lstinline{int32}, \\lstinline{bool},\r\n  \\lstinline{float32}, \\lstinline{float64}. Up to 8-D.\r\n\\item\r\n  \\lstinline{dims}: A \\lstinline{Tensor} of type \\lstinline{bool}. 1-D. The\r\n  dimensions to reverse.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-21}\r\n\r\nA \\lstinline{Tensor}. Has the same type as \\lstinline{tensor}. The same shape\r\nas \\lstinline{tensor}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.transpose(a, perm=None, name=\\textquotesingle{}transpose\\textquotesingle{})}\r\n}{tf.transpose(a, perm=None, name='transpose') }}\\label{tf.transposea-permnone-nametranspose}\r\n\r\nTransposes \\lstinline{a}. Permutes the dimensions according to\r\n\\lstinline{perm}.\r\n\r\nThe returned tensor's dimension i will correspond to the input dimension\r\n\\lstinline{perm{[}i{]}}. If \\lstinline{perm} is not given, it is set to\r\n(n-1\\ldots{}0), where n is the rank of the input tensor. Hence by\r\ndefault, this operation performs a regular matrix transpose on 2-D input\r\nTensors.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 'x' is [[1 2 3]}\r\n\\CommentTok{#         [4 5 6]]}\r\n\\NormalTok{tf.transpose(x) }\\OperatorTok{==>} \\NormalTok{[[}\\DecValTok{1} \\DecValTok{4}\\NormalTok{]}\r\n                     \\NormalTok{[}\\DecValTok{2} \\DecValTok{5}\\NormalTok{]}\r\n                     \\NormalTok{[}\\DecValTok{3} \\DecValTok{6}\\NormalTok{]]}\r\n\r\n\\CommentTok{# Equivalently}\r\n\\NormalTok{tf.transpose(x perm}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{0}\\NormalTok{, }\\DecValTok{1}\\NormalTok{]) }\\OperatorTok{==>} \\NormalTok{[[}\\DecValTok{1} \\DecValTok{4}\\NormalTok{]}\r\n                                 \\NormalTok{[}\\DecValTok{2} \\DecValTok{5}\\NormalTok{]}\r\n                                 \\NormalTok{[}\\DecValTok{3} \\DecValTok{6}\\NormalTok{]]}\r\n\r\n\\CommentTok{# 'perm' is more useful for n-dimensional tensors, for n > 2}\r\n\\CommentTok{# 'x' is   [[[1  2  3]}\r\n\\CommentTok{#            [4  5  6]]}\r\n\\CommentTok{#           [[7  8  9]}\r\n\\CommentTok{#            [10 11 12]]]}\r\n\\CommentTok{# Take the transpose of the matrices in dimension-0}\r\n\\NormalTok{tf.transpose(b, perm}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{0}\\NormalTok{, }\\DecValTok{2}\\NormalTok{, }\\DecValTok{1}\\NormalTok{]) }\\OperatorTok{==>} \\NormalTok{[[[}\\DecValTok{1}  \\DecValTok{4}\\NormalTok{]}\r\n                                      \\NormalTok{[}\\DecValTok{2}  \\DecValTok{5}\\NormalTok{]}\r\n                                      \\NormalTok{[}\\DecValTok{3}  \\DecValTok{6}\\NormalTok{]]}\r\n\r\n                                     \\NormalTok{[[}\\DecValTok{7} \\DecValTok{10}\\NormalTok{]}\r\n                                      \\NormalTok{[}\\DecValTok{8} \\DecValTok{11}\\NormalTok{]}\r\n                                      \\NormalTok{[}\\DecValTok{9} \\DecValTok{12}\\NormalTok{]]]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-22}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{a}: A \\lstinline{Tensor}.\r\n\\item\r\n  \\lstinline{perm}: A permutation of the dimensions of \\lstinline{a}.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-22}\r\n\r\nA transposed \\lstinline{Tensor}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.gather(params, indices, name=None)}\r\n}{tf.gather(params, indices, name=None) }}\\label{tf.gatherparams-indices-namenone}\r\n\r\nGather slices from \\lstinline{params} according to \\lstinline{indices}.\r\n\r\n\\lstinline{indices} must be an integer tensor of any dimension (usually 0-D\r\nor 1-D). Produces an output tensor with shape\r\n\\lstinline{indices.shape + params.shape{[}1:{]}} where:\r\n\r\n\\begin{lstlisting}\r\n# Scalar indices\r\noutput[:, ..., :] = params[indices, :, ... :]\r\n\r\n# Vector indices\r\noutput[i, :, ..., :] = params[indices[i], :, ... :]\r\n\r\n# Higher rank indices\r\noutput[i, ..., j, :, ... :] = params[indices[i, ..., j], :, ..., :]\r\n\\end{lstlisting}\r\n\r\nIf \\lstinline{indices} is a permutation and\r\n\\lstinline{len(indices) == params.shape{[}0{]}} then this operation will\r\npermute \\lstinline{params} accordingly.\r\n\r\n\\subparagraph{Args: }\\label{args-23}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{params}: A \\lstinline{Tensor}.\r\n\\item\r\n  \\lstinline{indices}: A \\lstinline{Tensor}. Must be one of the following\r\n  types: \\lstinline{int32}, \\lstinline{int64}.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-23}\r\n\r\nA \\lstinline{Tensor}. Has the same type as \\lstinline{params}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.dynamic_partition(data, partitions, num_partitions, name=None)}\r\n}{tf.dynamic_partition(data, partitions, num_partitions, name=None) }}\\label{tf.dynamicux5fpartitiondata-partitions-numux5fpartitions-namenone}\r\n\r\nPartitions \\lstinline{data} into \\lstinline{num_partitions} tensors using\r\nindices from \\lstinline{partitions}.\r\n\r\nFor each index tuple \\lstinline{js} of size \\lstinline{partitions.ndim}, the\r\nslice \\lstinline{data{[}js, ...{]}} becomes part of\r\n\\lstinline{outputs{[}partitions{[}js{]}{]}}. The slices with\r\n\\lstinline{partitions{[}js{]} = i} are placed in \\lstinline{outputs{[}i{]}}\r\nin lexicographic order of \\lstinline{js}, and the first dimension of\r\n\\lstinline{outputs{[}i{]}} is the number of entries in \\lstinline{partitions}\r\nequal to \\lstinline{i}. In detail,\r\n\r\n\\begin{lstlisting}\r\noutputs[i].shape = [sum(partitions == i)] + data.shape[partitions.ndim:]\r\n\r\noutputs[i] = pack([data[js, ...] for js if partitions[js] == i])\r\n\\end{lstlisting}\r\n\r\n\\lstinline{data.shape} must start with \\lstinline{partitions.shape}.\r\n\r\nFor example:\r\n\r\n\\begin{lstlisting}\r\n# Scalar partitions\r\npartitions = 1\r\nnum_partitions = 2\r\ndata = [10, 20]\r\noutputs[0] = []  # Empty with shape [0, 2]\r\noutputs[1] = [[10, 20]]\r\n\r\n# Vector partitions\r\npartitions = [0, 0, 1, 1, 0]\r\nnum_partitions = 2\r\ndata = [10, 20, 30, 40, 50]\r\noutputs[0] = [10, 20, 50]\r\noutputs[1] = [30, 40]\r\n\\end{lstlisting}\r\n\r\n\\subparagraph{Args: }\\label{args-24}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{data}: A \\lstinline{Tensor}.\r\n\\item\r\n  \\lstinline{partitions}: A \\lstinline{Tensor} of type \\lstinline{int32}. Any\r\n  shape. Indices in the range \\lstinline{{[}0, num_partitions)}.\r\n\\item\r\n  \\lstinline{num_partitions}: An \\lstinline{int} that is\r\n  \\lstinline{\\textgreater{}= 1}. The number of partitions to output.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-24}\r\n\r\nA list of \\lstinline{num_partitions} \\lstinline{Tensor} objects of the same\r\ntype as data.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\lstinline{tf.dynamic_stitch(indices, data, name=None)}\r\n}{tf.dynamic_stitch(indices, data, name=None) }}\\label{tf.dynamicux5fstitchindices-data-namenone}\r\n\r\nInterleave the values from the \\lstinline{data} tensors into a single\r\ntensor.\r\n\r\nBuilds a merged tensor such that\r\n\r\n\\begin{lstlisting}\r\nmerged[indices[m][i, ..., j], ...] = data[m][i, ..., j, ...]\r\n\\end{lstlisting}\r\n\r\nFor example, if each \\lstinline{indices{[}m{]}} is scalar or vector, we\r\nhave\r\n\r\n\\begin{lstlisting}\r\n# Scalar indices\r\nmerged[indices[m], ...] = data[m][...]\r\n\r\n# Vector indices\r\nmerged[indices[m][i], ...] = data[m][i, ...]\r\n\\end{lstlisting}\r\n\r\nEach \\lstinline{data{[}i{]}.shape} must start with the corresponding\r\n\\lstinline{indices{[}i{]}.shape}, and the rest of\r\n\\lstinline{data{[}i{]}.shape} must be constant w.r.t. \\lstinline{i}. That is,\r\nwe must have\r\n\\lstinline{data{[}i{]}.shape = indices{[}i{]}.shape + constant}. In\r\nterms of this \\lstinline{constant}, the output shape is\r\n\r\n\\begin{lstlisting}\r\nmerged.shape = [max(indices)] + constant\r\n\\end{lstlisting}\r\n\r\nValues are merged in order, so if an index appears in both\r\n\\lstinline{indices{[}m{]}{[}i{]}} and \\lstinline{indices{[}n{]}{[}j{]}} for\r\n\\lstinline{(m,i) \\textless{} (n,j)} the slice \\lstinline{data{[}n{]}{[}j{]}}\r\nwill appear in the merged result.\r\n\r\nFor example:\r\n\r\n\\begin{lstlisting}\r\nindices[0] = 6\r\nindices[1] = [4, 1]\r\nindices[2] = [[5, 2], [0, 3]]\r\ndata[0] = [61, 62]\r\ndata[1] = [[41, 42], [11, 12]]\r\ndata[2] = [[[51, 52], [21, 22]], [[1, 2], [31, 32]]]\r\nmerged = [[1, 2], [11, 12], [21, 22], [31, 32], [41, 42],\r\n          [51, 52], [61, 62]]\r\n\\end{lstlisting}\r\n\r\n\\subparagraph{Args: }\\label{args-25}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\lstinline{indices}: A list of at least 2 \\lstinline{Tensor} objects of type\r\n  \\lstinline{int32}.\r\n\\item\r\n  \\lstinline{data}: A list with the same number of \\lstinline{Tensor} objects\r\n  as \\lstinline{indices} of \\lstinline{Tensor} objects of the same type.\r\n\\item\r\n  \\lstinline{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-25}\r\n\r\nA \\lstinline{Tensor}. Has the same type as \\lstinline{data}.\r\n"
  },
  {
    "path": "tex_pdf/api/python/c4s05_math_ops.tex",
    "content": "\r\n\r\n\\section{Math }\\label{math}\r\n\r\nNote: Functions taking \\texttt{Tensor} arguments can also take anything\r\naccepted by\r\n\\href{../../api_docs/python/framework.md\\#convert_to_tensor}{\\texttt{tf.convert\\_to\\_tensor}}.\r\n\r\n\\subsection{Contents}\\label{contents}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-math}{Math}}{Math}}\\label{math-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-arithmetic-operators}{Arithmetic\r\n  Operators}\r\n\\item\r\n  \\protect\\hyperlink{add}{\\texttt{tf.add(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{sub}{\\texttt{tf.sub(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{mul}{\\texttt{tf.mul(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{div}{\\texttt{tf.div(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{mod}{\\texttt{tf.mod(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-basic-math-functions}{Basic Math\r\n  Functions}\r\n\\item\r\n  \\protect\\hyperlink{addux5fn}{\\texttt{tf.add\\_n(inputs,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{abs}{\\texttt{tf.abs(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{neg}{\\texttt{tf.neg(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{sign}{\\texttt{tf.sign(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{inv}{\\texttt{tf.inv(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{square}{\\texttt{tf.square(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{round}{\\texttt{tf.round(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{sqrt}{\\texttt{tf.sqrt(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{rsqrt}{\\texttt{tf.rsqrt(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{pow}{\\texttt{tf.pow(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{exp}{\\texttt{tf.exp(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{log}{\\texttt{tf.log(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{ceil}{\\texttt{tf.ceil(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{floor}{\\texttt{tf.floor(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{maximum}{\\texttt{tf.maximum(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{minimum}{\\texttt{tf.minimum(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{cos}{\\texttt{tf.cos(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{sin}{\\texttt{tf.sin(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-matrix-math-functions}{Matrix Math\r\n  Functions}\r\n\\item\r\n  \\protect\\hyperlink{diag}{\\texttt{tf.diag(diagonal,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{transpose}{\\texttt{tf.transpose(a,\\ perm=None,\\ name=\\textquotesingle{}transpose\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{matmul}{\\texttt{tf.matmul(a,\\ b,\\ transpose\\_a=False,\\ transpose\\_b=False,\\ a\\_is\\_sparse=False,\\ b\\_is\\_sparse=False,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{batchux5fmatmul}{\\texttt{tf.batch\\_matmul(x,\\ y,\\ adj\\_x=None,\\ adj\\_y=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{matrixux5fdeterminant}{\\texttt{tf.matrix\\_determinant(input,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{batchux5fmatrixux5fdeterminant}{\\texttt{tf.batch\\_matrix\\_determinant(input,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{matrixux5finverse}{\\texttt{tf.matrix\\_inverse(input,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{batchux5fmatrixux5finverse}{\\texttt{tf.batch\\_matrix\\_inverse(input,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{cholesky}{\\texttt{tf.cholesky(input,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{batchux5fcholesky}{\\texttt{tf.batch\\_cholesky(input,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-complex-number-functions}{Complex\r\n  Number Functions}\r\n\\item\r\n  \\protect\\hyperlink{complex}{\\texttt{tf.complex(real,\\ imag,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{complexux5fabs}{\\texttt{tf.complex\\_abs(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{conj}{\\texttt{tf.conj(in\\_,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{imag}{\\texttt{tf.imag(in\\_,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{real}{\\texttt{tf.real(in\\_,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-reduction}{Reduction}\r\n\\item\r\n  \\protect\\hyperlink{reduceux5fsum}{\\texttt{tf.reduce\\_sum(input\\_tensor,\\ reduction\\_indices=None,\\ keep\\_dims=False,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{reduceux5fprod}{\\texttt{tf.reduce\\_prod(input\\_tensor,\\ reduction\\_indices=None,\\ keep\\_dims=False,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{reduceux5fmin}{\\texttt{tf.reduce\\_min(input\\_tensor,\\ reduction\\_indices=None,\\ keep\\_dims=False,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{reduceux5fmax}{\\texttt{tf.reduce\\_max(input\\_tensor,\\ reduction\\_indices=None,\\ keep\\_dims=False,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{reduceux5fmean}{\\texttt{tf.reduce\\_mean(input\\_tensor,\\ reduction\\_indices=None,\\ keep\\_dims=False,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{reduceux5fall}{\\texttt{tf.reduce\\_all(input\\_tensor,\\ reduction\\_indices=None,\\ keep\\_dims=False,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{reduceux5fany}{\\texttt{tf.reduce\\_any(input\\_tensor,\\ reduction\\_indices=None,\\ keep\\_dims=False,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{accumulateux5fn}{\\texttt{tf.accumulate\\_n(inputs,\\ shape=None,\\ tensor\\_dtype=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-segmentation}{Segmentation}\r\n\\item\r\n  \\protect\\hyperlink{segmentux5fsum}{\\texttt{tf.segment\\_sum(data,\\ segment\\_ids,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{segmentux5fprod}{\\texttt{tf.segment\\_prod(data,\\ segment\\_ids,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{segmentux5fmin}{\\texttt{tf.segment\\_min(data,\\ segment\\_ids,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{segmentux5fmax}{\\texttt{tf.segment\\_max(data,\\ segment\\_ids,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{segmentux5fmean}{\\texttt{tf.segment\\_mean(data,\\ segment\\_ids,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{unsortedux5fsegmentux5fsum}{\\texttt{tf.unsorted\\_segment\\_sum(data,\\ segment\\_ids,\\ num\\_segments,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{sparseux5fsegmentux5fsum}{\\texttt{tf.sparse\\_segment\\_sum(data,\\ indices,\\ segment\\_ids,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{sparseux5fsegmentux5fmean}{\\texttt{tf.sparse\\_segment\\_mean(data,\\ indices,\\ segment\\_ids,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-sequence-comparison-and-indexing}{Sequence\r\n  Comparison and Indexing}\r\n\\item\r\n  \\protect\\hyperlink{argmin}{\\texttt{tf.argmin(input,\\ dimension,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{argmax}{\\texttt{tf.argmax(input,\\ dimension,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{listdiff}{\\texttt{tf.listdiff(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{where}{\\texttt{tf.where(input,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{unique}{\\texttt{tf.unique(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{editux5fdistance}{\\texttt{tf.edit\\_distance(hypothesis,\\ truth,\\ normalize=True,\\ name=\\textquotesingle{}edit\\_distance\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{invertux5fpermutation}{\\texttt{tf.invert\\_permutation(x,\\ name=None)}}\r\n\\end{itemize}\r\n\r\n\\subsection{Arithmetic Operators }\\label{arithmetic-operators}\r\n\r\nTensorFlow provides several operations that you can use to add basic\r\narithmetic operators to your graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.add(x,\\ y,\\ name=None)}\r\n}{tf.add(x, y, name=None) }}\\label{tf.addx-y-namenone}\r\n\r\nReturns x + y element-wise.\r\n\r\n\\emph{NOTE}: Add supports broadcasting. AddN does not.\r\n\r\n\\subparagraph{Args: }\\label{args}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int8}, \\texttt{int16},\r\n  \\texttt{int32}, \\texttt{complex64}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.sub(x,\\ y,\\ name=None)}\r\n}{tf.sub(x, y, name=None) }}\\label{tf.subx-y-namenone}\r\n\r\nReturns x - y element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{complex64}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-1}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.mul(x,\\ y,\\ name=None)}\r\n}{tf.mul(x, y, name=None) }}\\label{tf.mulx-y-namenone}\r\n\r\nReturns x * y element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int8}, \\texttt{int16},\r\n  \\texttt{int32}, \\texttt{complex64}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-2}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.div(x,\\ y,\\ name=None)}\r\n}{tf.div(x, y, name=None) }}\\label{tf.divx-y-namenone}\r\n\r\nReturns x / y element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{complex64}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-3}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.mod(x,\\ y,\\ name=None)}\r\n}{tf.mod(x, y, name=None) }}\\label{tf.modx-y-namenone}\r\n\r\nReturns element-wise remainder of division.\r\n\r\n\\subparagraph{Args: }\\label{args-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{int32}, \\texttt{int64}, \\texttt{float32}, \\texttt{float64}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-4}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\subsection{Basic Math Functions }\\label{basic-math-functions}\r\n\r\nTensorFlow provides several operations that you can use to add basic\r\nmathematical functions to your graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.add\\_n(inputs,\\ name=None)}\r\n}{tf.add\\_n(inputs, name=None) }}\\label{tf.addux5fninputs-namenone}\r\n\r\nAdd all input tensors element wise.\r\n\r\n\\subparagraph{Args: }\\label{args-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{inputs}: A list of at least 1 \\texttt{Tensor} objects of the\r\n  same type in: \\texttt{float32}, \\texttt{float64}, \\texttt{int64},\r\n  \\texttt{int32}, \\texttt{uint8}, \\texttt{int16}, \\texttt{int8},\r\n  \\texttt{complex64}, \\texttt{qint8}, \\texttt{quint8}, \\texttt{qint32}.\r\n  Must all be the same size and shape.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-5}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{inputs}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.abs(x,\\ name=None)}\r\n}{tf.abs(x, name=None) }}\\label{tf.absx-namenone}\r\n\r\nComputes the absolute value of a tensor.\r\n\r\nGiven a tensor of real numbers \\texttt{x}, this operation returns a\r\ntensor containing the absolute value of each element in \\texttt{x}. For\r\nexample, if x is an input element and y is an output element, this\r\noperation computes \\textbackslash{}(y =\r\n\\textbar{}x\\textbar{}\\textbackslash{}).\r\n\r\nSee \\protect\\hyperlink{tfux5fcomplexux5fabs}{\\texttt{tf.complex\\_abs()}}\r\nto compute the absolute value of a complex number.\r\n\r\n\\subparagraph{Args: }\\label{args-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor} of type \\texttt{float}, \\texttt{double},\r\n  \\texttt{int32}, or \\texttt{int64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-6}\r\n\r\nA \\texttt{Tensor} the same size and type as \\texttt{x} with absolute\r\nvalues.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.neg(x,\\ name=None)}\r\n}{tf.neg(x, name=None) }}\\label{tf.negx-namenone}\r\n\r\nComputes numerical negative value element-wise.\r\n\r\nI.e., \\textbackslash{}(y = -x\\textbackslash{}).\r\n\r\n\\subparagraph{Args: }\\label{args-7}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{complex64}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-7}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.sign(x,\\ name=None)}\r\n}{tf.sign(x, name=None) }}\\label{tf.signx-namenone}\r\n\r\nReturns an element-wise indication of the sign of a number.\r\n\r\ny = sign(x) = -1 if x \\textless{} 0; 0 if x == 0; 1 if x \\textgreater{}\r\n0.\r\n\r\n\\subparagraph{Args: }\\label{args-8}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-8}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.inv(x,\\ name=None)}\r\n}{tf.inv(x, name=None) }}\\label{tf.invx-namenone}\r\n\r\nComputes the reciprocal of x element-wise.\r\n\r\nI.e., \\textbackslash{}(y = 1 / x\\textbackslash{}).\r\n\r\n\\subparagraph{Args: }\\label{args-9}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{complex64}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-9}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.square(x,\\ name=None)}\r\n}{tf.square(x, name=None) }}\\label{tf.squarex-namenone}\r\n\r\nComputes square of x element-wise.\r\n\r\nI.e., \\textbackslash{}(y = x * x = x\\^{}2\\textbackslash{}).\r\n\r\n\\subparagraph{Args: }\\label{args-10}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{complex64}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-10}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.round(x,\\ name=None)}\r\n}{tf.round(x, name=None) }}\\label{tf.roundx-namenone}\r\n\r\nRounds the values of a tensor to the nearest integer, element-wise.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 'a' is [0.9, 2.5, 2.3, -4.4]}\r\n\\NormalTok{tf.}\\BuiltInTok{round}\\NormalTok{(a) }\\OperatorTok{==>} \\NormalTok{[ }\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{3.0}\\NormalTok{, }\\FloatTok{2.0}\\NormalTok{, }\\OperatorTok{-}\\FloatTok{4.0} \\NormalTok{]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-11}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor} of type \\texttt{float} or\r\n  \\texttt{double}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-11}\r\n\r\nA \\texttt{Tensor} of same shape and type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.sqrt(x,\\ name=None)}\r\n}{tf.sqrt(x, name=None) }}\\label{tf.sqrtx-namenone}\r\n\r\nComputes square root of x element-wise.\r\n\r\nI.e., \\textbackslash{}(y = \\sqrt{x} = x\\^{}\\{1/2\\}\\textbackslash{}).\r\n\r\n\\subparagraph{Args: }\\label{args-12}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{complex64}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-12}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.rsqrt(x,\\ name=None)}\r\n}{tf.rsqrt(x, name=None) }}\\label{tf.rsqrtx-namenone}\r\n\r\nComputes reciprocal of square root of x element-wise.\r\n\r\nI.e., \\textbackslash{}(y = 1 / \\sqrt{x}\\textbackslash{}).\r\n\r\n\\subparagraph{Args: }\\label{args-13}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{complex64}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-13}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.pow(x,\\ y,\\ name=None)}\r\n}{tf.pow(x, y, name=None) }}\\label{tf.powx-y-namenone}\r\n\r\nComputes the power of one value to another.\r\n\r\nGiven a tensor \\texttt{x} and a tensor \\texttt{y}, this operation\r\ncomputes \\textbackslash{}(x\\^{}y\\textbackslash{}) for corresponding\r\nelements in \\texttt{x} and \\texttt{y}. For example:\r\n\r\n\\begin{verbatim}\r\n# tensor 'x' is [[2, 2]], [3, 3]]\r\n# tensor 'y' is [[8, 16], [2, 3]]\r\ntf.pow(x, y) ==> [[256, 65536], [9, 27]]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-14}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor} of type \\texttt{float}, \\texttt{double},\r\n  \\texttt{int32}, \\texttt{complex64}, or \\texttt{int64}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor} of type \\texttt{float}, \\texttt{double},\r\n  \\texttt{int32}, \\texttt{complex64}, or \\texttt{int64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-14}\r\n\r\nA \\texttt{Tensor}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.exp(x,\\ name=None)}\r\n}{tf.exp(x, name=None) }}\\label{tf.expx-namenone}\r\n\r\nComputes exponential of x element-wise. \\textbackslash{}(y =\r\ne\\^{}x\\textbackslash{}).\r\n\r\n\\subparagraph{Args: }\\label{args-15}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{complex64}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-15}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.log(x,\\ name=None)}\r\n}{tf.log(x, name=None) }}\\label{tf.logx-namenone}\r\n\r\nComputes natural logrithm of x element-wise.\r\n\r\nI.e., \\textbackslash{}(y = \\log\\_e x\\textbackslash{}).\r\n\r\n\\subparagraph{Args: }\\label{args-16}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{complex64}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-16}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.ceil(x,\\ name=None)}\r\n}{tf.ceil(x, name=None) }}\\label{tf.ceilx-namenone}\r\n\r\nReturns element-wise smallest integer in not less than x.\r\n\r\n\\subparagraph{Args: }\\label{args-17}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-17}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.floor(x,\\ name=None)}\r\n}{tf.floor(x, name=None) }}\\label{tf.floorx-namenone}\r\n\r\nReturns element-wise largest integer not greater than x.\r\n\r\n\\subparagraph{Args: }\\label{args-18}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-18}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.maximum(x,\\ y,\\ name=None)}\r\n}{tf.maximum(x, y, name=None) }}\\label{tf.maximumx-y-namenone}\r\n\r\nReturns the max of x and y (i.e.~x \\textgreater{} y ? x : y)\r\nelement-wise, broadcasts.\r\n\r\n\\subparagraph{Args: }\\label{args-19}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-19}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.minimum(x,\\ y,\\ name=None)}\r\n}{tf.minimum(x, y, name=None) }}\\label{tf.minimumx-y-namenone}\r\n\r\nReturns the min of x and y (i.e.~x \\textless{} y ? x : y) element-wise,\r\nbroadcasts.\r\n\r\n\\subparagraph{Args: }\\label{args-20}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-20}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.cos(x,\\ name=None)}\r\n}{tf.cos(x, name=None) }}\\label{tf.cosx-namenone}\r\n\r\nComputes cos of x element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-21}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{complex64}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-21}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.sin(x,\\ name=None)}\r\n}{tf.sin(x, name=None) }}\\label{tf.sinx-namenone}\r\n\r\nComputes sin of x element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-22}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{complex64}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-22}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}.\r\n\r\n\\subsection{Matrix Math Functions }\\label{matrix-math-functions}\r\n\r\nTensorFlow provides several operations that you can use to add basic\r\nmathematical functions for matrices to your graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.diag(diagonal,\\ name=None)}\r\n}{tf.diag(diagonal, name=None) }}\\label{tf.diagdiagonal-namenone}\r\n\r\nReturns a diagonal tensor with a given diagonal values.\r\n\r\nGiven a \\texttt{diagonal}, this operation returns a tensor with the\r\n\\texttt{diagonal} and everything else padded with zeros. The diagonal is\r\ncomputed as follows:\r\n\r\nAssume \\texttt{diagonal} has dimensions {[}D1,\\ldots{}, Dk{]}, then the\r\noutput is a tensor of rank 2k with dimensions {[}D1,\\ldots{}, Dk,\r\nD1,\\ldots{}, Dk{]} where:\r\n\r\n\\texttt{output{[}i1,...,\\ ik,\\ i1,...,\\ ik{]}\\ =\\ diagonal{[}i1,\\ ...,\\ ik{]}}\r\nand 0 everywhere else.\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\n# 'diagonal' is [1, 2, 3, 4]\r\ntf.diag(diagonal) ==> [[1, 0, 0, 0]\r\n                       [0, 2, 0, 0]\r\n                       [0, 0, 3, 0]\r\n                       [0, 0, 0, 4]]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-23}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{diagonal}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{int64}. Rank k tensor where k is at most 3.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-23}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{diagonal}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.transpose(a,\\ perm=None,\\ name=\\textquotesingle{}transpose\\textquotesingle{})}\r\n}{tf.transpose(a, perm=None, name='transpose') }}\\label{tf.transposea-permnone-nametranspose}\r\n\r\nTransposes \\texttt{a}. Permutes the dimensions according to\r\n\\texttt{perm}.\r\n\r\nThe returned tensor's dimension i will correspond to the input dimension\r\n\\texttt{perm{[}i{]}}. If \\texttt{perm} is not given, it is set to\r\n(n-1\\ldots{}0), where n is the rank of the input tensor. Hence by\r\ndefault, this operation performs a regular matrix transpose on 2-D input\r\nTensors.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 'x' is [[1 2 3]}\r\n\\CommentTok{#         [4 5 6]]}\r\n\\NormalTok{tf.transpose(x) }\\OperatorTok{==>} \\NormalTok{[[}\\DecValTok{1} \\DecValTok{4}\\NormalTok{]}\r\n                     \\NormalTok{[}\\DecValTok{2} \\DecValTok{5}\\NormalTok{]}\r\n                     \\NormalTok{[}\\DecValTok{3} \\DecValTok{6}\\NormalTok{]]}\r\n\r\n\\CommentTok{# Equivalently}\r\n\\NormalTok{tf.transpose(x perm}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{0}\\NormalTok{, }\\DecValTok{1}\\NormalTok{]) }\\OperatorTok{==>} \\NormalTok{[[}\\DecValTok{1} \\DecValTok{4}\\NormalTok{]}\r\n                                 \\NormalTok{[}\\DecValTok{2} \\DecValTok{5}\\NormalTok{]}\r\n                                 \\NormalTok{[}\\DecValTok{3} \\DecValTok{6}\\NormalTok{]]}\r\n\r\n\\CommentTok{# 'perm' is more useful for n-dimensional tensors, for n > 2}\r\n\\CommentTok{# 'x' is   [[[1  2  3]}\r\n\\CommentTok{#            [4  5  6]]}\r\n\\CommentTok{#           [[7  8  9]}\r\n\\CommentTok{#            [10 11 12]]]}\r\n\\CommentTok{# Take the transpose of the matrices in dimension-0}\r\n\\NormalTok{tf.transpose(b, perm}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{0}\\NormalTok{, }\\DecValTok{2}\\NormalTok{, }\\DecValTok{1}\\NormalTok{]) }\\OperatorTok{==>} \\NormalTok{[[[}\\DecValTok{1}  \\DecValTok{4}\\NormalTok{]}\r\n                                      \\NormalTok{[}\\DecValTok{2}  \\DecValTok{5}\\NormalTok{]}\r\n                                      \\NormalTok{[}\\DecValTok{3}  \\DecValTok{6}\\NormalTok{]]}\r\n\r\n                                     \\NormalTok{[[}\\DecValTok{7} \\DecValTok{10}\\NormalTok{]}\r\n                                      \\NormalTok{[}\\DecValTok{8} \\DecValTok{11}\\NormalTok{]}\r\n                                      \\NormalTok{[}\\DecValTok{9} \\DecValTok{12}\\NormalTok{]]]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-24}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{a}: A \\texttt{Tensor}.\r\n\\item\r\n  \\texttt{perm}: A permutation of the dimensions of \\texttt{a}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-24}\r\n\r\nA transposed \\texttt{Tensor}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.matmul(a,\\ b,\\ transpose\\_a=False,\\ transpose\\_b=False,\\ a\\_is\\_sparse=False,\\ b\\_is\\_sparse=False,\\ name=None)}\r\n}{tf.matmul(a, b, transpose\\_a=False, transpose\\_b=False, a\\_is\\_sparse=False, b\\_is\\_sparse=False, name=None) }}\\label{tf.matmula-b-transposeux5fafalse-transposeux5fbfalse-aux5fisux5fsparsefalse-bux5fisux5fsparsefalse-namenone}\r\n\r\nMultiplies matrix \\texttt{a} by matrix \\texttt{b}, producing \\texttt{a}\r\n* \\texttt{b}.\r\n\r\nThe inputs must be two-dimensional matrices, with matching inner\r\ndimensions, possibly after transposition.\r\n\r\nBoth matrices must be of the same type. The supported types are:\r\n\\texttt{float}, \\texttt{double}, \\texttt{int32}, \\texttt{complex64}.\r\n\r\nEither matrix can be transposed on the fly by setting the corresponding\r\nflag to \\texttt{True}. This is \\texttt{False} by default.\r\n\r\nIf one or both of the matrices contain a lot of zeros, a more efficient\r\nmultiplication algorithm can be used by setting the corresponding\r\n\\texttt{a\\_is\\_sparse} or \\texttt{b\\_is\\_sparse} flag to \\texttt{True}.\r\nThese are \\texttt{False} by default.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 2-D tensor `a`}\r\n\\NormalTok{a }\\OperatorTok{=} \\NormalTok{tf.constant([}\\DecValTok{1}\\NormalTok{, }\\DecValTok{2}\\NormalTok{, }\\DecValTok{3}\\NormalTok{, }\\DecValTok{4}\\NormalTok{, }\\DecValTok{5}\\NormalTok{, }\\DecValTok{6}\\NormalTok{], shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{2}\\NormalTok{, }\\DecValTok{3}\\NormalTok{]) }\\OperatorTok{=>} \\NormalTok{[[}\\DecValTok{1}\\NormalTok{. }\\DecValTok{2}\\NormalTok{. }\\DecValTok{3}\\NormalTok{.]}\r\n                                                      \\NormalTok{[}\\DecValTok{4}\\NormalTok{. }\\DecValTok{5}\\NormalTok{. }\\DecValTok{6}\\NormalTok{.]]}\r\n\\CommentTok{# 2-D tensor `b`}\r\n\\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.constant([}\\DecValTok{7}\\NormalTok{, }\\DecValTok{8}\\NormalTok{, }\\DecValTok{9}\\NormalTok{, }\\DecValTok{10}\\NormalTok{, }\\DecValTok{11}\\NormalTok{, }\\DecValTok{12}\\NormalTok{], shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{3}\\NormalTok{, }\\DecValTok{2}\\NormalTok{]) }\\OperatorTok{=>} \\NormalTok{[[}\\DecValTok{7}\\NormalTok{. }\\DecValTok{8}\\NormalTok{.]}\r\n                                                         \\NormalTok{[}\\DecValTok{9}\\NormalTok{. }\\DecValTok{10}\\NormalTok{.]}\r\n                                                         \\NormalTok{[}\\DecValTok{11}\\NormalTok{. }\\DecValTok{12}\\NormalTok{.]]}\r\n\\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.matmul(a, b) }\\OperatorTok{=>} \\NormalTok{[[}\\DecValTok{58} \\DecValTok{64}\\NormalTok{]}\r\n                        \\NormalTok{[}\\DecValTok{139} \\DecValTok{154}\\NormalTok{]]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-25}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{a}: \\texttt{Tensor} of type \\texttt{float}, \\texttt{double},\r\n  \\texttt{int32} or \\texttt{complex64}.\r\n\\item\r\n  \\texttt{b}: \\texttt{Tensor} with same type as \\texttt{a}.\r\n\\item\r\n  \\texttt{transpose\\_a}: If \\texttt{True}, \\texttt{a} is transposed\r\n  before multiplication.\r\n\\item\r\n  \\texttt{transpose\\_b}: If \\texttt{True}, \\texttt{b} is transposed\r\n  before multiplication.\r\n\\item\r\n  \\texttt{a\\_is\\_sparse}: If \\texttt{True}, \\texttt{a} is treated as a\r\n  sparse matrix.\r\n\\item\r\n  \\texttt{b\\_is\\_sparse}: If \\texttt{True}, \\texttt{b} is treated as a\r\n  sparse matrix.\r\n\\item\r\n  \\texttt{name}: Name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-25}\r\n\r\nA \\texttt{Tensor} of the same type as \\texttt{a}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.batch\\_matmul(x,\\ y,\\ adj\\_x=None,\\ adj\\_y=None,\\ name=None)}\r\n}{tf.batch\\_matmul(x, y, adj\\_x=None, adj\\_y=None, name=None) }}\\label{tf.batchux5fmatmulx-y-adjux5fxnone-adjux5fynone-namenone}\r\n\r\nMultiplies slices of two tensors in batches.\r\n\r\nMultiplies all slices of \\texttt{Tensor} \\texttt{x} and \\texttt{y} (each\r\nslice can be viewed as an element of a batch), and arranges the\r\nindividual results in a single output tensor of the same batch size.\r\nEach of the individual slices can optionally be adjointed (to adjoint a\r\nmatrix means to transpose and conjugate it) before multiplication by\r\nsetting the \\texttt{adj\\_x} or \\texttt{adj\\_y} flag to \\texttt{True},\r\nwhich are by default \\texttt{False}.\r\n\r\nThe input tensors \\texttt{x} and \\texttt{y} are 3-D or higher with shape\r\n\\texttt{{[}...,\\ r\\_x,\\ c\\_x{]}} and \\texttt{{[}...,\\ r\\_y,\\ c\\_y{]}}.\r\n\r\nThe output tensor is 3-D or higher with shape\r\n\\texttt{{[}...,\\ r\\_o,\\ c\\_o{]}}, where:\r\n\r\n\\begin{verbatim}\r\nr_o = c_x if adj_x else r_x\r\nc_o = r_y if adj_y else c_y\r\n\\end{verbatim}\r\n\r\nIt is computed as:\r\n\r\n\\begin{verbatim}\r\nout[..., :, :] = matrix(x[..., :, :]) * matrix(y[..., :, :])\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-26}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{complex64}. 3-D or higher with shape\r\n  \\texttt{{[}...,\\ r\\_x,\\ c\\_x{]}}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n  3-D or higher with shape \\texttt{{[}...,\\ r\\_y,\\ c\\_y{]}}.\r\n\\item\r\n  \\texttt{adj\\_x}: An optional \\texttt{bool}. Defaults to\r\n  \\texttt{False}. If \\texttt{True}, adjoint the slices of \\texttt{x}.\r\n  Defaults to \\texttt{False}.\r\n\\item\r\n  \\texttt{adj\\_y}: An optional \\texttt{bool}. Defaults to\r\n  \\texttt{False}. If \\texttt{True}, adjoint the slices of \\texttt{y}.\r\n  Defaults to \\texttt{False}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-26}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{x}. 3-D or higher with\r\nshape \\texttt{{[}...,\\ r\\_o,\\ c\\_o{]}}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.matrix\\_determinant(input,\\ name=None)}\r\n}{tf.matrix\\_determinant(input, name=None) }}\\label{tf.matrixux5fdeterminantinput-namenone}\r\n\r\nCalculates the determinant of a square matrix.\r\n\r\n\\subparagraph{Args: }\\label{args-27}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}. A tensor of shape\r\n  \\texttt{{[}M,\\ M{]}}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-27}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{input}. A scalar, equal\r\nto the determinant of the input.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.batch\\_matrix\\_determinant(input,\\ name=None)}\r\n}{tf.batch\\_matrix\\_determinant(input, name=None) }}\\label{tf.batchux5fmatrixux5fdeterminantinput-namenone}\r\n\r\nCalculates the determinants for a batch of square matrices.\r\n\r\nThe input is a tensor of shape \\texttt{{[}...,\\ M,\\ M{]}} whose\r\ninner-most 2 dimensions form square matrices. The output is a 1-D tensor\r\ncontaining the determinants for all input submatrices\r\n\\texttt{{[}...,\\ :,\\ :{]}}.\r\n\r\n\\subparagraph{Args: }\\label{args-28}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}. Shape is\r\n  \\texttt{{[}...,\\ M,\\ M{]}}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-28}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{input}. Shape is\r\n\\texttt{{[}...{]}}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.matrix\\_inverse(input,\\ name=None)}\r\n}{tf.matrix\\_inverse(input, name=None) }}\\label{tf.matrixux5finverseinput-namenone}\r\n\r\nCalculates the inverse of a square invertible matrix. Checks for\r\ninvertibility.\r\n\r\n\\subparagraph{Args: }\\label{args-29}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}. Shape is \\texttt{{[}M,\\ M{]}}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-29}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{input}. Shape is\r\n\\texttt{{[}M,\\ M{]}} containing the matrix inverse of the input.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.batch\\_matrix\\_inverse(input,\\ name=None)}\r\n}{tf.batch\\_matrix\\_inverse(input, name=None) }}\\label{tf.batchux5fmatrixux5finverseinput-namenone}\r\n\r\nCalculates the inverse of square invertible matrices. Checks for\r\ninvertibility.\r\n\r\nThe input is a tensor of shape \\texttt{{[}...,\\ M,\\ M{]}} whose\r\ninner-most 2 dimensions form square matrices. The output is a tensor of\r\nthe same shape as the input containing the inverse for all input\r\nsubmatrices \\texttt{{[}...,\\ :,\\ :{]}}.\r\n\r\n\\subparagraph{Args: }\\label{args-30}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}. Shape is\r\n  \\texttt{{[}...,\\ M,\\ M{]}}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-30}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{input}. Shape is\r\n\\texttt{{[}...,\\ M,\\ M{]}}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.cholesky(input,\\ name=None)}\r\n}{tf.cholesky(input, name=None) }}\\label{tf.choleskyinput-namenone}\r\n\r\nCalculates the Cholesky decomposition of a square matrix.\r\n\r\nThe input has to be symmetric and positive definite. Only the\r\nlower-triangular part of the input will be used for this operation. The\r\nupper-triangular part will not be read.\r\n\r\nThe result is the lower-triangular matrix of the Cholesky decomposition\r\nof the input.\r\n\r\n\\subparagraph{Args: }\\label{args-31}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float64}, \\texttt{float32}. Shape is \\texttt{{[}M,\\ M{]}}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-31}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{input}. Shape is\r\n\\texttt{{[}M,\\ M{]}}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.batch\\_cholesky(input,\\ name=None)}\r\n}{tf.batch\\_cholesky(input, name=None) }}\\label{tf.batchux5fcholeskyinput-namenone}\r\n\r\nCalculates the Cholesky decomposition of a batch of square matrices.\r\n\r\nThe input is a tensor of shape \\texttt{{[}...,\\ M,\\ M{]}} whose\r\ninner-most 2 dimensions form square matrices, with the same constraints\r\nas the single matrix Cholesky decomposition above. The output is a\r\ntensor of the same shape as the input containing the Cholesky\r\ndecompositions for all input submatrices \\texttt{{[}...,\\ :,\\ :{]}}.\r\n\r\n\\subparagraph{Args: }\\label{args-32}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float64}, \\texttt{float32}. Shape is\r\n  \\texttt{{[}...,\\ M,\\ M{]}}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-32}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{input}. Shape is\r\n\\texttt{{[}...,\\ M,\\ M{]}}.\r\n\r\n\\subsection{Complex Number Functions }\\label{complex-number-functions}\r\n\r\nTensorFlow provides several operations that you can use to add complex\r\nnumber functions to your graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.complex(real,\\ imag,\\ name=None)}\r\n}{tf.complex(real, imag, name=None) }}\\label{tf.complexreal-imag-namenone}\r\n\r\nConverts two real numbers to a complex number.\r\n\r\nGiven a tensor \\texttt{real} representing the real part of a complex\r\nnumber, and a tensor \\texttt{imag} representing the imaginary part of a\r\ncomplex number, this operation computes complex numbers elementwise of\r\nthe form \\textbackslash{}(a + bj\\textbackslash{}), where \\emph{a}\r\nrepresents the \\texttt{real} part and \\emph{b} represents the\r\n\\texttt{imag} part.\r\n\r\nThe input tensors \\texttt{real} and \\texttt{imag} must be the same\r\nshape.\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\n# tensor 'real' is [2.25, 3.25]\r\n# tensor `imag` is [4.75, 5.75]\r\ntf.complex(real, imag) ==> [[2.25 + 4.74j], [3.25 + 5.75j]]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-33}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{real}: A \\texttt{Tensor} of type \\texttt{float}.\r\n\\item\r\n  \\texttt{imag}: A \\texttt{Tensor} of type \\texttt{float}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-33}\r\n\r\nA \\texttt{Tensor} of type \\texttt{complex64}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.complex\\_abs(x,\\ name=None)}\r\n}{tf.complex\\_abs(x, name=None) }}\\label{tf.complexux5fabsx-namenone}\r\n\r\nComputes the complex absolute value of a tensor.\r\n\r\nGiven a tensor \\texttt{x} of complex numbers, this operation returns a\r\ntensor of type \\texttt{float} that is the absolute value of each element\r\nin \\texttt{x}. All elements in \\texttt{x} must be complex numbers of the\r\nform \\textbackslash{}(a + bj\\textbackslash{}). The absolute value is\r\ncomputed as \\textbackslash{}( \\sqrt{a^2 + b^2}\\textbackslash{}).\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\n# tensor 'x' is [[-2.25 + 4.75j], [-3.25 + 5.75j]]\r\ntf.complex_abs(x) ==> [5.25594902, 6.60492229]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-34}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor} of type \\texttt{complex64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-34}\r\n\r\nA \\texttt{Tensor} of type \\texttt{float32}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.conj(in\\_,\\ name=None)}\r\n}{tf.conj(in\\_, name=None) }}\\label{tf.conjinux5f-namenone}\r\n\r\nReturns the complex conjugate of a complex number.\r\n\r\nGiven a tensor \\texttt{in} of complex numbers, this operation returns a\r\ntensor of complex numbers that are the complex conjugate of each element\r\nin \\texttt{in}. The complex numbers in \\texttt{in} must be of the form\r\n\\textbackslash{}(a + bj\\textbackslash{}), where \\emph{a} is the real\r\npart and \\emph{b} is the imaginary part.\r\n\r\nThe complex conjugate returned by this operation is of the form\r\n\\textbackslash{}(a - bj\\textbackslash{}).\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\n# tensor 'in' is [-2.25 + 4.75j, 3.25 + 5.75j]\r\ntf.conj(in) ==> [-2.25 - 4.75j, 3.25 - 5.75j]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-35}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{in\\_}: A \\texttt{Tensor} of type \\texttt{complex64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-35}\r\n\r\nA \\texttt{Tensor} of type \\texttt{complex64}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.imag(in\\_,\\ name=None)}\r\n}{tf.imag(in\\_, name=None) }}\\label{tf.imaginux5f-namenone}\r\n\r\nReturns the imaginary part of a complex number.\r\n\r\nGiven a tensor \\texttt{in} of complex numbers, this operation returns a\r\ntensor of type \\texttt{float} that is the imaginary part of each element\r\nin \\texttt{in}. All elements in \\texttt{in} must be complex numbers of\r\nthe form \\textbackslash{}(a + bj\\textbackslash{}), where \\emph{a} is the\r\nreal part and \\emph{b} is the imaginary part returned by this operation.\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\n# tensor 'in' is [-2.25 + 4.75j, 3.25 + 5.75j]\r\ntf.imag(in) ==> [4.75, 5.75]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-36}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{in\\_}: A \\texttt{Tensor} of type \\texttt{complex64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-36}\r\n\r\nA \\texttt{Tensor} of type \\texttt{float32}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.real(in\\_,\\ name=None)}\r\n}{tf.real(in\\_, name=None) }}\\label{tf.realinux5f-namenone}\r\n\r\nReturns the real part of a complex number.\r\n\r\nGiven a tensor \\texttt{in} of complex numbers, this operation returns a\r\ntensor of type \\texttt{float} that is the real part of each element in\r\n\\texttt{in}. All elements in \\texttt{in} must be complex numbers of the\r\nform \\textbackslash{}(a + bj\\textbackslash{}), where \\emph{a} is the\r\nreal part returned by this operation and \\emph{b} is the imaginary part.\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\n# tensor 'in' is [-2.25 + 4.75j, 3.25 + 5.75j]\r\ntf.real(in) ==> [-2.25, 3.25]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-37}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{in\\_}: A \\texttt{Tensor} of type \\texttt{complex64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-37}\r\n\r\nA \\texttt{Tensor} of type \\texttt{float32}.\r\n\r\n\\subsection{Reduction }\\label{reduction}\r\n\r\nTensorFlow provides several operations that you can use to perform\r\ncommon math computations that reduce various dimensions of a tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.reduce\\_sum(input\\_tensor,\\ reduction\\_indices=None,\\ keep\\_dims=False,\\ name=None)}\r\n}{tf.reduce\\_sum(input\\_tensor, reduction\\_indices=None, keep\\_dims=False, name=None) }}\\label{tf.reduceux5fsuminputux5ftensor-reductionux5findicesnone-keepux5fdimsfalse-namenone}\r\n\r\nComputes the sum of elements across dimensions of a tensor.\r\n\r\nReduces \\texttt{input\\_tensor} along the dimensions given in\r\n\\texttt{reduction\\_indices}. Unless \\texttt{keep\\_dims} is true, the\r\nrank of the tensor is reduced by 1 for each entry in\r\n\\texttt{reduction\\_indices}. If \\texttt{keep\\_dims} is true, the reduced\r\ndimensions are retained with length 1.\r\n\r\nIf \\texttt{reduction\\_indices} has no entries, all dimensions are\r\nreduced, and a tensor with a single element is returned.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 'x' is [[1, 1, 1]]}\r\n\\CommentTok{#         [1, 1, 1]]}\r\n\\NormalTok{tf.reduce_sum(x) }\\OperatorTok{==>} \\DecValTok{6}\r\n\\NormalTok{tf.reduce_sum(x, }\\DecValTok{0}\\NormalTok{) }\\OperatorTok{==>} \\NormalTok{[}\\DecValTok{2}\\NormalTok{, }\\DecValTok{2}\\NormalTok{, }\\DecValTok{2}\\NormalTok{]}\r\n\\NormalTok{tf.reduce_sum(x, }\\DecValTok{1}\\NormalTok{) }\\OperatorTok{==>} \\NormalTok{[}\\DecValTok{3}\\NormalTok{, }\\DecValTok{3}\\NormalTok{]}\r\n\\NormalTok{tf.reduce_sum(x, }\\DecValTok{1}\\NormalTok{, keep_dims}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{) }\\OperatorTok{==>} \\NormalTok{[[}\\DecValTok{3}\\NormalTok{], [}\\DecValTok{3}\\NormalTok{]]}\r\n\\NormalTok{tf.reduce_sum(x, [}\\DecValTok{0}\\NormalTok{, }\\DecValTok{1}\\NormalTok{]) }\\OperatorTok{==>} \\DecValTok{6}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-38}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input\\_tensor}: The tensor to reduce. Should have numeric\r\n  type.\r\n\\item\r\n  \\texttt{reduction\\_indices}: The dimensions to reduce. If\r\n  \\texttt{None} (the defaut), reduces all dimensions.\r\n\\item\r\n  \\texttt{keep\\_dims}: If true, retains reduced dimensions with length\r\n  1.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-38}\r\n\r\nThe reduced tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.reduce\\_prod(input\\_tensor,\\ reduction\\_indices=None,\\ keep\\_dims=False,\\ name=None)}\r\n}{tf.reduce\\_prod(input\\_tensor, reduction\\_indices=None, keep\\_dims=False, name=None) }}\\label{tf.reduceux5fprodinputux5ftensor-reductionux5findicesnone-keepux5fdimsfalse-namenone}\r\n\r\nComputes the product of elements across dimensions of a tensor.\r\n\r\nReduces \\texttt{input\\_tensor} along the dimensions given in\r\n\\texttt{reduction\\_indices}. Unless \\texttt{keep\\_dims} is true, the\r\nrank of the tensor is reduced by 1 for each entry in\r\n\\texttt{reduction\\_indices}. If \\texttt{keep\\_dims} is true, the reduced\r\ndimensions are retained with length 1.\r\n\r\nIf \\texttt{reduction\\_indices} has no entries, all dimensions are\r\nreduced, and a tensor with a single element is returned.\r\n\r\n\\subparagraph{Args: }\\label{args-39}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input\\_tensor}: The tensor to reduce. Should have numeric\r\n  type.\r\n\\item\r\n  \\texttt{reduction\\_indices}: The dimensions to reduce. If\r\n  \\texttt{None} (the defaut), reduces all dimensions.\r\n\\item\r\n  \\texttt{keep\\_dims}: If true, retains reduced dimensions with length\r\n  1.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-39}\r\n\r\nThe reduced tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.reduce\\_min(input\\_tensor,\\ reduction\\_indices=None,\\ keep\\_dims=False,\\ name=None)}\r\n}{tf.reduce\\_min(input\\_tensor, reduction\\_indices=None, keep\\_dims=False, name=None) }}\\label{tf.reduceux5fmininputux5ftensor-reductionux5findicesnone-keepux5fdimsfalse-namenone}\r\n\r\nComputes the minimum of elements across dimensions of a tensor.\r\n\r\nReduces \\texttt{input\\_tensor} along the dimensions given in\r\n\\texttt{reduction\\_indices}. Unless \\texttt{keep\\_dims} is true, the\r\nrank of the tensor is reduced by 1 for each entry in\r\n\\texttt{reduction\\_indices}. If \\texttt{keep\\_dims} is true, the reduced\r\ndimensions are retained with length 1.\r\n\r\nIf \\texttt{reduction\\_indices} has no entries, all dimensions are\r\nreduced, and a tensor with a single element is returned.\r\n\r\n\\subparagraph{Args: }\\label{args-40}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input\\_tensor}: The tensor to reduce. Should have numeric\r\n  type.\r\n\\item\r\n  \\texttt{reduction\\_indices}: The dimensions to reduce. If\r\n  \\texttt{None} (the defaut), reduces all dimensions.\r\n\\item\r\n  \\texttt{keep\\_dims}: If true, retains reduced dimensions with length\r\n  1.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-40}\r\n\r\nThe reduced tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.reduce\\_max(input\\_tensor,\\ reduction\\_indices=None,\\ keep\\_dims=False,\\ name=None)}\r\n}{tf.reduce\\_max(input\\_tensor, reduction\\_indices=None, keep\\_dims=False, name=None) }}\\label{tf.reduceux5fmaxinputux5ftensor-reductionux5findicesnone-keepux5fdimsfalse-namenone}\r\n\r\nComputes the maximum of elements across dimensions of a tensor.\r\n\r\nReduces \\texttt{input\\_tensor} along the dimensions given in\r\n\\texttt{reduction\\_indices}. Unless \\texttt{keep\\_dims} is true, the\r\nrank of the tensor is reduced by 1 for each entry in\r\n\\texttt{reduction\\_indices}. If \\texttt{keep\\_dims} is true, the reduced\r\ndimensions are retained with length 1.\r\n\r\nIf \\texttt{reduction\\_indices} has no entries, all dimensions are\r\nreduced, and a tensor with a single element is returned.\r\n\r\n\\subparagraph{Args: }\\label{args-41}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input\\_tensor}: The tensor to reduce. Should have numeric\r\n  type.\r\n\\item\r\n  \\texttt{reduction\\_indices}: The dimensions to reduce. If\r\n  \\texttt{None} (the defaut), reduces all dimensions.\r\n\\item\r\n  \\texttt{keep\\_dims}: If true, retains reduced dimensions with length\r\n  1.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-41}\r\n\r\nThe reduced tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.reduce\\_mean(input\\_tensor,\\ reduction\\_indices=None,\\ keep\\_dims=False,\\ name=None)}\r\n}{tf.reduce\\_mean(input\\_tensor, reduction\\_indices=None, keep\\_dims=False, name=None) }}\\label{tf.reduceux5fmeaninputux5ftensor-reductionux5findicesnone-keepux5fdimsfalse-namenone}\r\n\r\nComputes the mean of elements across dimensions of a tensor.\r\n\r\nReduces \\texttt{input\\_tensor} along the dimensions given in\r\n\\texttt{reduction\\_indices}. Unless \\texttt{keep\\_dims} is true, the\r\nrank of the tensor is reduced by 1 for each entry in\r\n\\texttt{reduction\\_indices}. If \\texttt{keep\\_dims} is true, the reduced\r\ndimensions are retained with length 1.\r\n\r\nIf \\texttt{reduction\\_indices} has no entries, all dimensions are\r\nreduced, and a tensor with a single element is returned.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 'x' is [[1., 1. ]]}\r\n\\CommentTok{#         [2., 2.]]}\r\n\\NormalTok{tf.reduce_mean(x) }\\OperatorTok{==>} \\FloatTok{1.5}\r\n\\NormalTok{tf.reduce_mean(x, }\\DecValTok{0}\\NormalTok{) }\\OperatorTok{==>} \\NormalTok{[}\\FloatTok{1.5}\\NormalTok{, }\\FloatTok{1.5}\\NormalTok{]}\r\n\\NormalTok{tf.reduce_mean(x, }\\DecValTok{1}\\NormalTok{) }\\OperatorTok{==>} \\NormalTok{[}\\DecValTok{1}\\NormalTok{.,  }\\DecValTok{2}\\NormalTok{.]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-42}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input\\_tensor}: The tensor to reduce. Should have numeric\r\n  type.\r\n\\item\r\n  \\texttt{reduction\\_indices}: The dimensions to reduce. If\r\n  \\texttt{None} (the defaut), reduces all dimensions.\r\n\\item\r\n  \\texttt{keep\\_dims}: If true, retains reduced dimensions with length\r\n  1.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-42}\r\n\r\nThe reduced tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.reduce\\_all(input\\_tensor,\\ reduction\\_indices=None,\\ keep\\_dims=False,\\ name=None)}\r\n}{tf.reduce\\_all(input\\_tensor, reduction\\_indices=None, keep\\_dims=False, name=None) }}\\label{tf.reduceux5fallinputux5ftensor-reductionux5findicesnone-keepux5fdimsfalse-namenone}\r\n\r\nComputes the ``logical and'' of elements across dimensions of a tensor.\r\n\r\nReduces \\texttt{input\\_tensor} along the dimensions given in\r\n\\texttt{reduction\\_indices}. Unless \\texttt{keep\\_dims} is true, the\r\nrank of the tensor is reduced by 1 for each entry in\r\n\\texttt{reduction\\_indices}. If \\texttt{keep\\_dims} is true, the reduced\r\ndimensions are retained with length 1.\r\n\r\nIf \\texttt{reduction\\_indices} has no entries, all dimensions are\r\nreduced, and a tensor with a single element is returned.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 'x' is [[True,  True]]}\r\n\\CommentTok{#         [False, False]]}\r\n\\NormalTok{tf.reduce_all(x) }\\OperatorTok{==>} \\VariableTok{False}\r\n\\NormalTok{tf.reduce_all(x, }\\DecValTok{0}\\NormalTok{) }\\OperatorTok{==>} \\NormalTok{[}\\VariableTok{False}\\NormalTok{, }\\VariableTok{False}\\NormalTok{]}\r\n\\NormalTok{tf.reduce_all(x, }\\DecValTok{1}\\NormalTok{) }\\OperatorTok{==>} \\NormalTok{[}\\VariableTok{True}\\NormalTok{, }\\VariableTok{False}\\NormalTok{]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-43}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input\\_tensor}: The boolean tensor to reduce.\r\n\\item\r\n  \\texttt{reduction\\_indices}: The dimensions to reduce. If\r\n  \\texttt{None} (the defaut), reduces all dimensions.\r\n\\item\r\n  \\texttt{keep\\_dims}: If true, retains reduced dimensions with length\r\n  1.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-43}\r\n\r\nThe reduced tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.reduce\\_any(input\\_tensor,\\ reduction\\_indices=None,\\ keep\\_dims=False,\\ name=None)}\r\n}{tf.reduce\\_any(input\\_tensor, reduction\\_indices=None, keep\\_dims=False, name=None) }}\\label{tf.reduceux5fanyinputux5ftensor-reductionux5findicesnone-keepux5fdimsfalse-namenone}\r\n\r\nComputes the ``logical or'' of elements across dimensions of a tensor.\r\n\r\nReduces \\texttt{input\\_tensor} along the dimensions given in\r\n\\texttt{reduction\\_indices}. Unless \\texttt{keep\\_dims} is true, the\r\nrank of the tensor is reduced by 1 for each entry in\r\n\\texttt{reduction\\_indices}. If \\texttt{keep\\_dims} is true, the reduced\r\ndimensions are retained with length 1.\r\n\r\nIf \\texttt{reduction\\_indices} has no entries, all dimensions are\r\nreduced, and a tensor with a single element is returned.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 'x' is [[True,  True]]}\r\n\\CommentTok{#         [False, False]]}\r\n\\NormalTok{tf.reduce_any(x) }\\OperatorTok{==>} \\VariableTok{True}\r\n\\NormalTok{tf.reduce_any(x, }\\DecValTok{0}\\NormalTok{) }\\OperatorTok{==>} \\NormalTok{[}\\VariableTok{True}\\NormalTok{, }\\VariableTok{True}\\NormalTok{]}\r\n\\NormalTok{tf.reduce_any(x, }\\DecValTok{1}\\NormalTok{) }\\OperatorTok{==>} \\NormalTok{[}\\VariableTok{True}\\NormalTok{, }\\VariableTok{False}\\NormalTok{]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-44}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input\\_tensor}: The boolean tensor to reduce.\r\n\\item\r\n  \\texttt{reduction\\_indices}: The dimensions to reduce. If\r\n  \\texttt{None} (the defaut), reduces all dimensions.\r\n\\item\r\n  \\texttt{keep\\_dims}: If true, retains reduced dimensions with length\r\n  1.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-44}\r\n\r\nThe reduced tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.accumulate\\_n(inputs,\\ shape=None,\\ tensor\\_dtype=None,\\ name=None)}\r\n}{tf.accumulate\\_n(inputs, shape=None, tensor\\_dtype=None, name=None) }}\\label{tf.accumulateux5fninputs-shapenone-tensorux5fdtypenone-namenone}\r\n\r\nReturns the element-wise sum of a list of tensors.\r\n\r\nOptionally, pass \\texttt{shape} and \\texttt{tensor\\_dtype} for shape and\r\ntype checking, otherwise, these are inferred.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# tensor 'a' is [[1, 2], [3, 4]}\r\n\\CommentTok{# tensor `b` is [[5, 0], [0, 6]]}\r\n\\NormalTok{tf.accumulate_n([a, b, a]) }\\OperatorTok{==>} \\NormalTok{[[}\\DecValTok{7}\\NormalTok{, }\\DecValTok{4}\\NormalTok{], [}\\DecValTok{6}\\NormalTok{, }\\DecValTok{14}\\NormalTok{]]}\r\n\r\n\\CommentTok{# Explicitly pass shape and type}\r\n\\NormalTok{tf.accumulate_n([a, b, a], shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{2}\\NormalTok{, }\\DecValTok{2}\\NormalTok{], tensor_dtype}\\OperatorTok{=}\\NormalTok{tf.int32)}\r\n  \\OperatorTok{==>} \\NormalTok{[[}\\DecValTok{7}\\NormalTok{, }\\DecValTok{4}\\NormalTok{], [}\\DecValTok{6}\\NormalTok{, }\\DecValTok{14}\\NormalTok{]]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-45}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{inputs}: A list of \\texttt{Tensor} objects, each with same\r\n  shape and type.\r\n\\item\r\n  \\texttt{shape}: Shape of elements of \\texttt{inputs}.\r\n\\item\r\n  \\texttt{tensor\\_dtype}: The type of \\texttt{inputs}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-45}\r\n\r\nA \\texttt{Tensor} of same shape and type as the elements of\r\n\\texttt{inputs}.\r\n\r\n\\subparagraph{Raises: }\\label{raises}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: If \\texttt{inputs} don't all have same shape and\r\n  dtype or the shape cannot be inferred.\r\n\\end{itemize}\r\n\r\n\\subsection{Segmentation }\\label{segmentation}\r\n\r\nTensorFlow provides several operations that you can use to perform\r\ncommon math computations on tensor segments. Here a segmentation is a\r\npartitioning of a tensor along the first dimension, i.e.~it defines a\r\nmapping from the first dimension onto \\texttt{segment\\_ids}. The\r\n\\texttt{segment\\_ids} tensor should be the size of the first dimension,\r\n\\texttt{d0}, with consecutive IDs in the range \\texttt{0} to \\texttt{k},\r\nwhere \\texttt{k\\textless{}d0}. In particular, a segmentation of a matrix\r\ntensor is a mapping of rows to segments.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.constant([[}\\DecValTok{1}\\NormalTok{,}\\DecValTok{2}\\NormalTok{,}\\DecValTok{3}\\NormalTok{,}\\DecValTok{4}\\NormalTok{], [}\\OperatorTok{-}\\DecValTok{1}\\NormalTok{,}\\OperatorTok{-}\\DecValTok{2}\\NormalTok{,}\\OperatorTok{-}\\DecValTok{3}\\NormalTok{,}\\OperatorTok{-}\\DecValTok{4}\\NormalTok{], [}\\DecValTok{5}\\NormalTok{,}\\DecValTok{6}\\NormalTok{,}\\DecValTok{7}\\NormalTok{,}\\DecValTok{8}\\NormalTok{]])}\r\n\\NormalTok{tf.segment_sum(c, tf.constant([}\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{1}\\NormalTok{]))}\r\n  \\OperatorTok{==>}  \\NormalTok{[[}\\DecValTok{0} \\DecValTok{0} \\DecValTok{0} \\DecValTok{0}\\NormalTok{]}\r\n        \\NormalTok{[}\\DecValTok{5} \\DecValTok{6} \\DecValTok{7} \\DecValTok{8}\\NormalTok{]]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.segment\\_sum(data,\\ segment\\_ids,\\ name=None)}\r\n}{tf.segment\\_sum(data, segment\\_ids, name=None) }}\\label{tf.segmentux5fsumdata-segmentux5fids-namenone}\r\n\r\nComputes the sum along segments of a tensor.\r\n\r\nRead \\href{../../api_docs/python/math_ops.md\\#segmentation}{the section\r\non Segmentation} for an explanation of segments.\r\n\r\nComputes a tensor such that \\textbackslash{}(output\\_i = \\sum\\_j\r\ndata\\_j\\textbackslash{}) where sum is over \\texttt{j} such that\r\n\\texttt{segment\\_ids{[}j{]}\\ ==\\ i}.\r\n\r\n\\subparagraph{Args: }\\label{args-46}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{data}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64},\r\n  \\texttt{uint8}, \\texttt{int16}, \\texttt{int8}.\r\n\\item\r\n  \\texttt{segment\\_ids}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{int32}, \\texttt{int64}. A 1-D tensor whose rank is\r\n  equal to the rank of \\texttt{data}'s first dimension. Values should be\r\n  sorted and can be repeated.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-46}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{data}. Has same shape as\r\ndata, except for dimension\\_0 which has size \\texttt{k}, the number of\r\nsegments.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.segment\\_prod(data,\\ segment\\_ids,\\ name=None)}\r\n}{tf.segment\\_prod(data, segment\\_ids, name=None) }}\\label{tf.segmentux5fproddata-segmentux5fids-namenone}\r\n\r\nComputes the product along segments of a tensor.\r\n\r\nRead \\href{../../api_docs/python/math_ops.md\\#segmentation}{the section\r\non Segmentation} for an explanation of segments.\r\n\r\nComputes a tensor such that \\textbackslash{}(output\\_i = \\prod\\_j\r\ndata\\_j\\textbackslash{}) where the product is over \\texttt{j} such that\r\n\\texttt{segment\\_ids{[}j{]}\\ ==\\ i}.\r\n\r\n\\subparagraph{Args: }\\label{args-47}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{data}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64},\r\n  \\texttt{uint8}, \\texttt{int16}, \\texttt{int8}.\r\n\\item\r\n  \\texttt{segment\\_ids}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{int32}, \\texttt{int64}. A 1-D tensor whose rank is\r\n  equal to the rank of \\texttt{data}'s first dimension. Values should be\r\n  sorted and can be repeated.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-47}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{data}. Has same shape as\r\ndata, except for dimension\\_0 which has size \\texttt{k}, the number of\r\nsegments.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.segment\\_min(data,\\ segment\\_ids,\\ name=None)}\r\n}{tf.segment\\_min(data, segment\\_ids, name=None) }}\\label{tf.segmentux5fmindata-segmentux5fids-namenone}\r\n\r\nComputes the minimum along segments of a tensor.\r\n\r\nRead \\href{../../api_docs/python/math_ops.md\\#segmentation}{the section\r\non Segmentation} for an explanation of segments.\r\n\r\nComputes a tensor such that \\textbackslash{}(output\\_i =\r\n\\min\\_j(data\\_j)\\textbackslash{}) where \\texttt{min} is over \\texttt{j}\r\nsuch that \\texttt{segment\\_ids{[}j{]}\\ ==\\ i}.\r\n\r\n\\subparagraph{Args: }\\label{args-48}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{data}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64},\r\n  \\texttt{uint8}, \\texttt{int16}, \\texttt{int8}.\r\n\\item\r\n  \\texttt{segment\\_ids}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{int32}, \\texttt{int64}. A 1-D tensor whose rank is\r\n  equal to the rank of \\texttt{data}'s first dimension. Values should be\r\n  sorted and can be repeated.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-48}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{data}. Has same shape as\r\ndata, except for dimension\\_0 which has size \\texttt{k}, the number of\r\nsegments.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.segment\\_max(data,\\ segment\\_ids,\\ name=None)}\r\n}{tf.segment\\_max(data, segment\\_ids, name=None) }}\\label{tf.segmentux5fmaxdata-segmentux5fids-namenone}\r\n\r\nComputes the maximum along segments of a tensor.\r\n\r\nRead \\href{../../api_docs/python/math_ops.md\\#segmentation}{the section\r\non Segmentation} for an explanation of segments.\r\n\r\nComputes a tensor such that \\textbackslash{}(output\\_i =\r\n\\max\\_j(data\\_j)\\textbackslash{}) where \\texttt{max} is over \\texttt{j}\r\nsuch that \\texttt{segment\\_ids{[}j{]}\\ ==\\ i}.\r\n\r\n\\subparagraph{Args: }\\label{args-49}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{data}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64},\r\n  \\texttt{uint8}, \\texttt{int16}, \\texttt{int8}.\r\n\\item\r\n  \\texttt{segment\\_ids}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{int32}, \\texttt{int64}. A 1-D tensor whose rank is\r\n  equal to the rank of \\texttt{data}'s first dimension. Values should be\r\n  sorted and can be repeated.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-49}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{data}. Has same shape as\r\ndata, except for dimension\\_0 which has size \\texttt{k}, the number of\r\nsegments.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.segment\\_mean(data,\\ segment\\_ids,\\ name=None)}\r\n}{tf.segment\\_mean(data, segment\\_ids, name=None) }}\\label{tf.segmentux5fmeandata-segmentux5fids-namenone}\r\n\r\nComputes the mean along segments of a tensor.\r\n\r\nRead \\href{../../api_docs/python/math_ops.md\\#segmentation}{the section\r\non Segmentation} for an explanation of segments.\r\n\r\nComputes a tensor such that \\textbackslash{}(output\\_i =\r\n\\frac{\\sum_j data_j}{N}\\textbackslash{}) where \\texttt{mean} is over\r\n\\texttt{j} such that \\texttt{segment\\_ids{[}j{]}\\ ==\\ i} and \\texttt{N}\r\nis the total number of values summed.\r\n\r\n\\subparagraph{Args: }\\label{args-50}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{data}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64},\r\n  \\texttt{uint8}, \\texttt{int16}, \\texttt{int8}.\r\n\\item\r\n  \\texttt{segment\\_ids}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{int32}, \\texttt{int64}. A 1-D tensor whose rank is\r\n  equal to the rank of \\texttt{data}'s first dimension. Values should be\r\n  sorted and can be repeated.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-50}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{data}. Has same shape as\r\ndata, except for dimension\\_0 which has size \\texttt{k}, the number of\r\nsegments.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.unsorted\\_segment\\_sum(data,\\ segment\\_ids,\\ num\\_segments,\\ name=None)}\r\n}{tf.unsorted\\_segment\\_sum(data, segment\\_ids, num\\_segments, name=None) }}\\label{tf.unsortedux5fsegmentux5fsumdata-segmentux5fids-numux5fsegments-namenone}\r\n\r\nComputes the sum along segments of a tensor.\r\n\r\nRead \\href{../../api_docs/python/math_ops.md\\#segmentation}{the section\r\non Segmentation} for an explanation of segments.\r\n\r\nComputes a tensor such that \\textbackslash{}(output\\_i = \\sum\\_j\r\ndata\\_j\\textbackslash{}) where sum is over \\texttt{j} such that\r\n\\texttt{segment\\_ids{[}j{]}\\ ==\\ i}. Unlike \\texttt{SegmentSum},\r\n\\texttt{segment\\_ids} need not be sorted and need not cover all values\r\nin the full range of valid values.\r\n\r\nIf the sum is empty for a given segment ID \\texttt{i},\r\n\\texttt{output{[}i{]}\\ =\\ 0}.\r\n\r\n\\texttt{num\\_segments} should equal the number of distinct segment IDs.\r\n\r\n\\subparagraph{Args: }\\label{args-51}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{data}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64},\r\n  \\texttt{uint8}, \\texttt{int16}, \\texttt{int8}.\r\n\\item\r\n  \\texttt{segment\\_ids}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{int32}, \\texttt{int64}. A 1-D tensor whose rank is\r\n  equal to the rank of \\texttt{data}'s first dimension.\r\n\\item\r\n  \\texttt{num\\_segments}: A \\texttt{Tensor} of type \\texttt{int32}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-51}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{data}. Has same shape as\r\ndata, except for dimension\\_0 which has size \\texttt{num\\_segments}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.sparse\\_segment\\_sum(data,\\ indices,\\ segment\\_ids,\\ name=None)}\r\n}{tf.sparse\\_segment\\_sum(data, indices, segment\\_ids, name=None) }}\\label{tf.sparseux5fsegmentux5fsumdata-indices-segmentux5fids-namenone}\r\n\r\nComputes the sum along sparse segments of a tensor.\r\n\r\nRead \\href{../../api_docs/python/math_ops.md\\#segmentation}{the section\r\non Segmentation} for an explanation of segments.\r\n\r\nLike \\texttt{SegmentSum}, but \\texttt{segment\\_ids} can have rank less\r\nthan \\texttt{data}'s first dimension, selecting a subset of\r\ndimension\\_0, specified by \\texttt{indices}.\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\nc = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])\r\n\r\n# Select two rows, one segment.\r\ntf.sparse_segment_sum(c, tf.constant([0, 1]), tf.constant([0, 0]))\r\n  ==> [[0 0 0 0]]\r\n\r\n# Select two rows, two segment.\r\ntf.sparse_segment_sum(c, tf.constant([0, 1]), tf.constant([0, 1]))\r\n  ==> [[ 1  2  3  4]\r\n       [-1 -2 -3 -4]]\r\n\r\n# Select all rows, two segments.\r\ntf.sparse_segment_sum(c, tf.constant([0, 1, 2]), tf.constant([0, 0, 1]))\r\n  ==> [[0 0 0 0]\r\n       [5 6 7 8]]\r\n\r\n# Which is equivalent to:\r\ntf.segment_sum(c, tf.constant([0, 0, 1]))\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-52}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{data}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64},\r\n  \\texttt{uint8}, \\texttt{int16}, \\texttt{int8}.\r\n\\item\r\n  \\texttt{indices}: A \\texttt{Tensor} of type \\texttt{int32}. A 1-D\r\n  tensor. Has same rank as \\texttt{segment\\_ids}.\r\n\\item\r\n  \\texttt{segment\\_ids}: A \\texttt{Tensor} of type \\texttt{int32}. A 1-D\r\n  tensor. Values should be sorted and can be repeated.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-52}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{data}. Has same shape as\r\ndata, except for dimension\\_0 which has size \\texttt{k}, the number of\r\nsegments.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.sparse\\_segment\\_mean(data,\\ indices,\\ segment\\_ids,\\ name=None)}\r\n}{tf.sparse\\_segment\\_mean(data, indices, segment\\_ids, name=None) }}\\label{tf.sparseux5fsegmentux5fmeandata-indices-segmentux5fids-namenone}\r\n\r\nComputes the mean along sparse segments of a tensor.\r\n\r\nRead \\href{../../api_docs/python/math_ops.md\\#segmentation}{the section\r\non Segmentation} for an explanation of segments.\r\n\r\nLike \\texttt{SegmentMean}, but \\texttt{segment\\_ids} can have rank less\r\nthan \\texttt{data}'s first dimension, selecting a subset of\r\ndimension\\_0, specified by \\texttt{indices}.\r\n\r\n\\subparagraph{Args: }\\label{args-53}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{data}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}.\r\n\\item\r\n  \\texttt{indices}: A \\texttt{Tensor} of type \\texttt{int32}. A 1-D\r\n  tensor. Has same rank as \\texttt{segment\\_ids}.\r\n\\item\r\n  \\texttt{segment\\_ids}: A \\texttt{Tensor} of type \\texttt{int32}. A 1-D\r\n  tensor. Values should be sorted and can be repeated.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-53}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{data}. Has same shape as\r\ndata, except for dimension\\_0 which has size \\texttt{k}, the number of\r\nsegments.\r\n\r\n\\subsection{Sequence Comparison and Indexing\r\n}\\label{sequence-comparison-and-indexing}\r\n\r\nTensorFlow provides several operations that you can use to add sequence\r\ncomparison and index extraction to your graph. You can use these\r\noperations to determine sequence differences and determine the indexes\r\nof specific values in a tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.argmin(input,\\ dimension,\\ name=None)}\r\n}{tf.argmin(input, dimension, name=None) }}\\label{tf.argmininput-dimension-namenone}\r\n\r\nReturns the index with the smallest value across dimensions of a tensor.\r\n\r\n\\subparagraph{Args: }\\label{args-54}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int64}, \\texttt{int32},\r\n  \\texttt{uint8}, \\texttt{int16}, \\texttt{int8}, \\texttt{complex64},\r\n  \\texttt{qint8}, \\texttt{quint8}, \\texttt{qint32}.\r\n\\item\r\n  \\texttt{dimension}: A \\texttt{Tensor} of type \\texttt{int32}. int32, 0\r\n  \\textless{}= dimension \\textless{} rank(input). Describes which\r\n  dimension of the input Tensor to reduce across. For vectors, use\r\n  dimension = 0.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-54}\r\n\r\nA \\texttt{Tensor} of type \\texttt{int64}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.argmax(input,\\ dimension,\\ name=None)}\r\n}{tf.argmax(input, dimension, name=None) }}\\label{tf.argmaxinput-dimension-namenone}\r\n\r\nReturns the index with the largest value across dimensions of a tensor.\r\n\r\n\\subparagraph{Args: }\\label{args-55}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int64}, \\texttt{int32},\r\n  \\texttt{uint8}, \\texttt{int16}, \\texttt{int8}, \\texttt{complex64},\r\n  \\texttt{qint8}, \\texttt{quint8}, \\texttt{qint32}.\r\n\\item\r\n  \\texttt{dimension}: A \\texttt{Tensor} of type \\texttt{int32}. int32, 0\r\n  \\textless{}= dimension \\textless{} rank(input). Describes which\r\n  dimension of the input Tensor to reduce across. For vectors, use\r\n  dimension = 0.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-55}\r\n\r\nA \\texttt{Tensor} of type \\texttt{int64}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.listdiff(x,\\ y,\\ name=None)}\r\n}{tf.listdiff(x, y, name=None) }}\\label{tf.listdiffx-y-namenone}\r\n\r\nComputes the difference between two lists of numbers.\r\n\r\nGiven a list \\texttt{x} and a list \\texttt{y}, this operation returns a\r\nlist \\texttt{out} that represents all numbers that are in \\texttt{x} but\r\nnot in \\texttt{y}. The returned list \\texttt{out} is sorted in the same\r\norder that the numbers appear in \\texttt{x} (duplicates are preserved).\r\nThis operation also returns a list \\texttt{idx} that represents the\r\nposition of each \\texttt{out} element in \\texttt{x}. In other words:\r\n\r\n\\texttt{out{[}i{]}\\ =\\ x{[}idx{[}i{]}{]}\\ for\\ i\\ in\\ {[}0,\\ 1,\\ ...,\\ len(out)\\ -\\ 1{]}}\r\n\r\nFor example, given this input:\r\n\r\n\\begin{verbatim}\r\nx = [1, 2, 3, 4, 5, 6]\r\ny = [1, 3, 5]\r\n\\end{verbatim}\r\n\r\nThis operation would return:\r\n\r\n\\begin{verbatim}\r\nout ==> [2, 4, 6]\r\nidx ==> [1, 3, 5]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-56}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. 1-D. Values to keep.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n  1-D. Values to remove.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-56}\r\n\r\nA tuple of \\texttt{Tensor} objects (out, idx).\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{out}: A \\texttt{Tensor}. Has the same type as \\texttt{x}. 1-D.\r\n  Values present in \\texttt{x} but not in \\texttt{y}.\r\n\\item\r\n  \\texttt{idx}: A \\texttt{Tensor} of type \\texttt{int32}. 1-D. Positions\r\n  of \\texttt{x} values preserved in \\texttt{out}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.where(input,\\ name=None)}\r\n}{tf.where(input, name=None) }}\\label{tf.whereinput-namenone}\r\n\r\nReturns locations of true values in a boolean tensor.\r\n\r\nThis operation returns the coordinates of true elements in\r\n\\texttt{input}. The coordinates are returned in a 2-D tensor where the\r\nfirst dimension (rows) represents the number of true elements, and the\r\nsecond dimension (columns) represents the coordinates of the true\r\nelements. Keep in mind, the shape of the output tensor can vary\r\ndepending on how many true values there are in \\texttt{input}. Indices\r\nare output in row-major order.\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\n# 'input' tensor is [[True, False]\r\n#                    [True, False]]\r\n# 'input' has two true values, so output has two coordinates.\r\n# 'input' has rank of 2, so coordinates have two indices.\r\nwhere(input) ==> [[0, 0],\r\n                  [1, 0]]\r\n\r\n# `input` tensor is [[[True, False]\r\n#                     [True, False]]\r\n#                    [[False, True]\r\n#                     [False, True]]\r\n#                    [[False, False]\r\n#                     [False, True]]]\r\n# 'input' has 5 true values, so output has 5 coordinates.\r\n# 'input' has rank of 3, so coordinates have three indices.\r\nwhere(input) ==> [[0, 0, 0],\r\n                  [0, 1, 0],\r\n                  [1, 0, 1],\r\n                  [1, 1, 1],\r\n                  [2, 1, 1]]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-57}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor} of type \\texttt{bool}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-57}\r\n\r\nA \\texttt{Tensor} of type \\texttt{int64}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.unique(x,\\ name=None)}\r\n}{tf.unique(x, name=None) }}\\label{tf.uniquex-namenone}\r\n\r\nFinds unique elements in a 1-D tensor.\r\n\r\nThis operation returns a tensor \\texttt{y} containing all of the unique\r\nelements of \\texttt{x} sorted in the same order that they occur in\r\n\\texttt{x}. This operation also returns a tensor \\texttt{idx} the same\r\nsize as \\texttt{x} that contains the index of each value of \\texttt{x}\r\nin the unique output \\texttt{y}. In other words:\r\n\r\n\\texttt{y{[}idx{[}i{]}{]}\\ =\\ x{[}i{]}\\ for\\ i\\ in\\ {[}0,\\ 1,...,rank(x)\\ -\\ 1{]}}\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\n# tensor 'x' is [1, 1, 2, 4, 4, 4, 7, 8, 8]\r\ny, idx = unique(x)\r\ny ==> [1, 2, 4, 7, 8]\r\nidx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-58}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. 1-D.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-58}\r\n\r\nA tuple of \\texttt{Tensor} objects (y, idx).\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Has the same type as \\texttt{x}. 1-D.\r\n\\item\r\n  \\texttt{idx}: A \\texttt{Tensor} of type \\texttt{int32}. 1-D.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.edit\\_distance(hypothesis,\\ truth,\\ normalize=True,\\ name=\\textquotesingle{}edit\\_distance\\textquotesingle{})}\r\n}{tf.edit\\_distance(hypothesis, truth, normalize=True, name='edit\\_distance') }}\\label{tf.editux5fdistancehypothesis-truth-normalizetrue-nameeditux5fdistance}\r\n\r\nComputes the Levenshtein distance between sequences.\r\n\r\nThis operation takes variable-length sequences (\\texttt{hypothesis} and\r\n\\texttt{truth}), each provided as a \\texttt{SparseTensor}, and computes\r\nthe Levenshtein distance. You can normalize the edit distance by length\r\nof \\texttt{truth} by setting \\texttt{normalize} to true.\r\n\r\nFor example, given the following input:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 'hypothesis' is a tensor of shape `[2, 1]` with variable-length values:}\r\n\\CommentTok{#   (0,0) = [\"a\"]}\r\n\\CommentTok{#   (1,0) = [\"b\"]}\r\n\\NormalTok{hypothesis }\\OperatorTok{=} \\NormalTok{tf.SparseTensor(}\r\n    \\NormalTok{[[}\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{],}\r\n     \\NormalTok{[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{]],}\r\n    \\NormalTok{[}\\StringTok{\"a\"}\\NormalTok{, }\\StringTok{\"b\"}\\NormalTok{]}\r\n    \\NormalTok{(}\\DecValTok{2}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{))}\r\n\r\n\\CommentTok{# 'truth' is a tensor of shape `[2, 2]` with variable-length values:}\r\n\\CommentTok{#   (0,0) = []}\r\n\\CommentTok{#   (0,1) = [\"a\"]}\r\n\\CommentTok{#   (1,0) = [\"b\", \"c\"]}\r\n\\CommentTok{#   (1,1) = [\"a\"]}\r\n\\NormalTok{truth }\\OperatorTok{=} \\NormalTok{tf.SparseTensor(}\r\n    \\NormalTok{[[}\\DecValTok{0}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{0}\\NormalTok{],}\r\n     \\NormalTok{[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{],}\r\n     \\NormalTok{[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{1}\\NormalTok{],}\r\n     \\NormalTok{[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{0}\\NormalTok{]]}\r\n    \\NormalTok{[}\\StringTok{\"a\"}\\NormalTok{, }\\StringTok{\"b\"}\\NormalTok{, }\\StringTok{\"c\"}\\NormalTok{, }\\StringTok{\"a\"}\\NormalTok{],}\r\n    \\NormalTok{(}\\DecValTok{2}\\NormalTok{, }\\DecValTok{2}\\NormalTok{, }\\DecValTok{2}\\NormalTok{))}\r\n\r\n\\NormalTok{normalize }\\OperatorTok{=} \\VariableTok{True}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nThis operation would return the following:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 'output' is a tensor of shape `[2, 2]` with edit distances normalized}\r\n\\CommentTok{# by 'truth' lengths.}\r\n\\NormalTok{output }\\OperatorTok{==>} \\NormalTok{[[inf, }\\FloatTok{1.0}\\NormalTok{],  }\\CommentTok{# (0,0): no truth, (0,1): no hypothesis}\r\n           \\NormalTok{[}\\FloatTok{0.5}\\NormalTok{, }\\FloatTok{1.0}\\NormalTok{]]  }\\CommentTok{# (1,0): addition, (1,1): no hypothesis}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-59}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{hypothesis}: A \\texttt{SparseTensor} containing hypothesis\r\n  sequences.\r\n\\item\r\n  \\texttt{truth}: A \\texttt{SparseTensor} containing truth sequences.\r\n\\item\r\n  \\texttt{normalize}: A \\texttt{bool}. If \\texttt{True}, normalizes the\r\n  Levenshtein distance by length of \\texttt{truth.}\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-59}\r\n\r\nA dense \\texttt{Tensor} with rank \\texttt{R\\ -\\ 1}, where R is the rank\r\nof the \\texttt{SparseTensor} inputs \\texttt{hypothesis} and\r\n\\texttt{truth}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{TypeError}: If either \\texttt{hypothesis} or \\texttt{truth}\r\n  are not a \\texttt{SparseTensor}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.invert\\_permutation(x,\\ name=None)}\r\n}{tf.invert\\_permutation(x, name=None) }}\\label{tf.invertux5fpermutationx-namenone}\r\n\r\nComputes the inverse permutation of a tensor.\r\n\r\nThis operation computes the inverse of an index permutation. It takes a\r\n1-D integer tensor \\texttt{x}, which represents the indices of a\r\nzero-based array, and swaps each value with its index position. In other\r\nwords, for an ouput tensor \\texttt{y} and an input tensor \\texttt{x},\r\nthis operation computes the following:\r\n\r\n\\texttt{y{[}x{[}i{]}{]}\\ =\\ i\\ for\\ i\\ in\\ {[}0,\\ 1,\\ ...,\\ len(x)\\ -\\ 1{]}}\r\n\r\nThe values must include 0. There can be no duplicate values or negative\r\nvalues.\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\n# tensor `x` is [3, 4, 0, 2, 1]\r\ninvert_permutation(x) ==> [2, 4, 3, 0, 1]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-60}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor} of type \\texttt{int32}. 1-D.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-60}\r\n\r\nA \\texttt{Tensor} of type \\texttt{int32}. 1-D.\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/python/c4s06_control_flow_ops.tex",
    "content": "\r\n\r\n\\section{Control Flow }\\label{control-flow}\r\n\r\nNote: Functions taking \\texttt{Tensor} arguments can also take anything\r\naccepted by\r\n\\href{../../api_docs/python/framework.md\\#convert_to_tensor}{\\texttt{tf.convert\\_to\\_tensor}}.\r\n\r\n\\subsection{Contents}\\label{contents}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-control-flow}{Control\r\nFlow}}{Control Flow}}\\label{control-flow-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-control-flow-operations}{Control Flow\r\n  Operations}\r\n\\item\r\n  \\protect\\hyperlink{identity}{\\texttt{tf.identity(input,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{tuple}{\\texttt{tf.tuple(tensors,\\ name=None,\\ control\\_inputs=None)}}\r\n\\item\r\n  \\protect\\hyperlink{group}{\\texttt{tf.group(*inputs,\\ **kwargs)}}\r\n\\item\r\n  \\protect\\hyperlink{noux5fop}{\\texttt{tf.no\\_op(name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{countux5fupux5fto}{\\texttt{tf.count\\_up\\_to(ref,\\ limit,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-logical-operators}{Logical Operators}\r\n\\item\r\n  \\protect\\hyperlink{logicalux5fand}{\\texttt{tf.logical\\_and(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{logicalux5fnot}{\\texttt{tf.logical\\_not(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{logicalux5for}{\\texttt{tf.logical\\_or(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{logicalux5fxor}{\\texttt{tf.logical\\_xor(x,\\ y,\\ name=\\textquotesingle{}LogicalXor\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-comparison-operators}{Comparison\r\n  Operators}\r\n\\item\r\n  \\protect\\hyperlink{equal}{\\texttt{tf.equal(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{notux5fequal}{\\texttt{tf.not\\_equal(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{less}{\\texttt{tf.less(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{lessux5fequal}{\\texttt{tf.less\\_equal(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{greater}{\\texttt{tf.greater(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{greaterux5fequal}{\\texttt{tf.greater\\_equal(x,\\ y,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{select}{\\texttt{tf.select(condition,\\ t,\\ e,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{where}{\\texttt{tf.where(input,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-debugging-operations}{Debugging\r\n  Operations}\r\n\\item\r\n  \\protect\\hyperlink{isux5ffinite}{\\texttt{tf.is\\_finite(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{isux5finf}{\\texttt{tf.is\\_inf(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{isux5fnan}{\\texttt{tf.is\\_nan(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{verifyux5ftensorux5fallux5ffinite}{\\texttt{tf.verify\\_tensor\\_all\\_finite(t,\\ msg,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{checkux5fnumerics}{\\texttt{tf.check\\_numerics(tensor,\\ message,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{addux5fcheckux5fnumericsux5fops}{\\texttt{tf.add\\_check\\_numerics\\_ops()}}\r\n\\item\r\n  \\protect\\hyperlink{Assert}{\\texttt{tf.Assert(condition,\\ data,\\ summarize=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{Print}{\\texttt{tf.Print(input\\_,\\ data,\\ message=None,\\ first\\_n=None,\\ summarize=None,\\ name=None)}}\r\n\\end{itemize}\r\n\r\n\\subsection{Control Flow Operations }\\label{control-flow-operations}\r\n\r\nTensorFlow provides several operations and classes that you can use to\r\ncontrol the execution of operations and add conditional dependencies to\r\nyour graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.identity(input,\\ name=None)}\r\n}{tf.identity(input, name=None) }}\\label{tf.identityinput-namenone}\r\n\r\nReturn a tensor with the same shape and contents as the input tensor or\r\nvalue.\r\n\r\n\\subparagraph{Args: }\\label{args}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{input}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.tuple(tensors,\\ name=None,\\ control\\_inputs=None)}\r\n}{tf.tuple(tensors, name=None, control\\_inputs=None) }}\\label{tf.tupletensors-namenone-controlux5finputsnone}\r\n\r\nGroup tensors together.\r\n\r\nThis creates a tuple of tensors with the same values as the\r\n\\texttt{tensors} argument, except that the value of each tensor is only\r\nreturned after the values of all tensors have been computed.\r\n\r\n\\texttt{control\\_inputs} contains additional ops that have to finish\r\nbefore this op finishes, but whose outputs are not returned.\r\n\r\nThis can be used as a ``join'' mechanism for parallel computations: all\r\nthe argument tensors can be computed in parallel, but the values of any\r\ntensor returned by \\texttt{tuple} are only available after all the\r\nparallel computations are done.\r\n\r\nSee also \\texttt{group} and \\texttt{with\\_dependencies}.\r\n\r\n\\subparagraph{Args: }\\label{args-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{tensors}: A list of \\texttt{Tensor}s or\r\n  \\texttt{IndexedSlices}, some entries can be \\texttt{None}.\r\n\\item\r\n  \\texttt{name}: (optional) A name to use as a \\texttt{name\\_scope} for\r\n  the operation.\r\n\\item\r\n  \\texttt{control\\_inputs}: List of additional ops to finish before\r\n  returning.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-1}\r\n\r\nSame as \\texttt{tensors}.\r\n\r\n\\subparagraph{Raises: }\\label{raises}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: If \\texttt{tensors} does not contain any\r\n  \\texttt{Tensor} or \\texttt{IndexedSlices}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.group(*inputs,\\ **kwargs)}\r\n}{tf.group(*inputs, **kwargs) }}\\label{tf.groupinputs-kwargs}\r\n\r\nCreate an op that groups multiple operations.\r\n\r\nWhen this op finishes, all ops in \\texttt{input} have finished. This op\r\nhas no output.\r\n\r\nSee also \\texttt{tuple} and \\texttt{with\\_dependencies}.\r\n\r\n\\subparagraph{Args: }\\label{args-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{*inputs}: One or more tensors to group.\r\n\\item\r\n  \\texttt{**kwargs}: Optional parameters to pass when constructing the\r\n  NodeDef.\r\n\\item\r\n  \\texttt{name}: A name for this operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-2}\r\n\r\nAn Operation that executes all its inputs.\r\n\r\n\\subparagraph{Raises: }\\label{raises-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: If an unknown keyword argument is provided, or if\r\n  there are no inputs.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.no\\_op(name=None)}\r\n}{tf.no\\_op(name=None) }}\\label{tf.noux5fopnamenone}\r\n\r\nDoes nothing. Only useful as a placeholder for control edges.\r\n\r\n\\subparagraph{Args: }\\label{args-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-3}\r\n\r\nThe created Operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.count\\_up\\_to(ref,\\ limit,\\ name=None)}\r\n}{tf.count\\_up\\_to(ref, limit, name=None) }}\\label{tf.countux5fupux5ftoref-limit-namenone}\r\n\r\nIncrements `ref' until it reaches `limit'.\r\n\r\nThis operation outputs ``ref'' after the update is done. This makes it\r\neasier to chain operations that need to use the updated value.\r\n\r\n\\subparagraph{Args: }\\label{args-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ref}: A mutable \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{int32}, \\texttt{int64}. Should be from a scalar\r\n  \\texttt{Variable} node.\r\n\\item\r\n  \\texttt{limit}: An \\texttt{int}. If incrementing ref would bring it\r\n  above limit, instead generates an `OutOfRange' error.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-4}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{ref}. A copy of the\r\ninput before increment. If nothing else modifies the input, the values\r\nproduced will all be distinct.\r\n\r\n\\subsection{Logical Operators }\\label{logical-operators}\r\n\r\nTensorFlow provides several operations that you can use to add logical\r\noperators to your graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.logical\\_and(x,\\ y,\\ name=None)}\r\n}{tf.logical\\_and(x, y, name=None) }}\\label{tf.logicalux5fandx-y-namenone}\r\n\r\nReturns the truth value of x AND y element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor} of type \\texttt{bool}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor} of type \\texttt{bool}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-5}\r\n\r\nA \\texttt{Tensor} of type \\texttt{bool}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.logical\\_not(x,\\ name=None)}\r\n}{tf.logical\\_not(x, name=None) }}\\label{tf.logicalux5fnotx-namenone}\r\n\r\nReturns the truth value of NOT x element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor} of type \\texttt{bool}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-6}\r\n\r\nA \\texttt{Tensor} of type \\texttt{bool}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.logical\\_or(x,\\ y,\\ name=None)}\r\n}{tf.logical\\_or(x, y, name=None) }}\\label{tf.logicalux5forx-y-namenone}\r\n\r\nReturns the truth value of x OR y element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-7}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor} of type \\texttt{bool}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor} of type \\texttt{bool}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-7}\r\n\r\nA \\texttt{Tensor} of type \\texttt{bool}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.logical\\_xor(x,\\ y,\\ name=\\textquotesingle{}LogicalXor\\textquotesingle{})}\r\n}{tf.logical\\_xor(x, y, name='LogicalXor') }}\\label{tf.logicalux5fxorx-y-namelogicalxor}\r\n\r\nx \\^{} y = (x \\textbar{} y) \\& \\textasciitilde{}(x \\& y).\r\n\r\n\\subsection{Comparison Operators }\\label{comparison-operators}\r\n\r\nTensorFlow provides several operations that you can use to add\r\ncomparison operators to your graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.equal(x,\\ y,\\ name=None)}\r\n}{tf.equal(x, y, name=None) }}\\label{tf.equalx-y-namenone}\r\n\r\nReturns the truth value of (x == y) element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-8}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64},\r\n  \\texttt{complex64}, \\texttt{quint8}, \\texttt{qint8}, \\texttt{qint32}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-8}\r\n\r\nA \\texttt{Tensor} of type \\texttt{bool}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.not\\_equal(x,\\ y,\\ name=None)}\r\n}{tf.not\\_equal(x, y, name=None) }}\\label{tf.notux5fequalx-y-namenone}\r\n\r\nReturns the truth value of (x != y) element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-9}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64},\r\n  \\texttt{complex64}, \\texttt{quint8}, \\texttt{qint8}, \\texttt{qint32}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-9}\r\n\r\nA \\texttt{Tensor} of type \\texttt{bool}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.less(x,\\ y,\\ name=None)}\r\n}{tf.less(x, y, name=None) }}\\label{tf.lessx-y-namenone}\r\n\r\nReturns the truth value of (x \\textless{} y) element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-10}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-10}\r\n\r\nA \\texttt{Tensor} of type \\texttt{bool}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.less\\_equal(x,\\ y,\\ name=None)}\r\n}{tf.less\\_equal(x, y, name=None) }}\\label{tf.lessux5fequalx-y-namenone}\r\n\r\nReturns the truth value of (x \\textless{}= y) element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-11}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-11}\r\n\r\nA \\texttt{Tensor} of type \\texttt{bool}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.greater(x,\\ y,\\ name=None)}\r\n}{tf.greater(x, y, name=None) }}\\label{tf.greaterx-y-namenone}\r\n\r\nReturns the truth value of (x \\textgreater{} y) element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-12}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-12}\r\n\r\nA \\texttt{Tensor} of type \\texttt{bool}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.greater\\_equal(x,\\ y,\\ name=None)}\r\n}{tf.greater\\_equal(x, y, name=None) }}\\label{tf.greaterux5fequalx-y-namenone}\r\n\r\nReturns the truth value of (x \\textgreater{}= y) element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-13}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64}.\r\n\\item\r\n  \\texttt{y}: A \\texttt{Tensor}. Must have the same type as \\texttt{x}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-13}\r\n\r\nA \\texttt{Tensor} of type \\texttt{bool}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.select(condition,\\ t,\\ e,\\ name=None)}\r\n}{tf.select(condition, t, e, name=None) }}\\label{tf.selectcondition-t-e-namenone}\r\n\r\nSelects elements from \\texttt{t} or \\texttt{e}, depending on\r\n\\texttt{condition}.\r\n\r\nThe \\texttt{condition}, \\texttt{t}, and \\texttt{e} tensors must all have\r\nthe same shape, and the output will also have that shape. The\r\n\\texttt{condition} tensor acts as an element-wise mask that chooses,\r\nbased on the value at each element, whether the corresponding element in\r\nthe output should be taken from \\texttt{t} (if true) or \\texttt{e} (if\r\nfalse). For example:\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\n# 'condition' tensor is [[True, False]\r\n#                        [True, False]]\r\n# 't' is [[1, 1],\r\n#         [1, 1]]\r\n# 'e' is [[2, 2],\r\n#         [2, 2]]\r\nselect(condition, t, e) ==> [[1, 2],\r\n                             [1, 2]]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-14}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{condition}: A \\texttt{Tensor} of type \\texttt{bool}.\r\n\\item\r\n  \\texttt{t}: A \\texttt{Tensor} with the same shape as\r\n  \\texttt{condition}.\r\n\\item\r\n  \\texttt{e}: A \\texttt{Tensor} with the same type and shape as\r\n  \\texttt{t}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-14}\r\n\r\nA \\texttt{Tensor} with the same type and shape as \\texttt{t} and\r\n\\texttt{e}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.where(input,\\ name=None)}\r\n}{tf.where(input, name=None) }}\\label{tf.whereinput-namenone}\r\n\r\nReturns locations of true values in a boolean tensor.\r\n\r\nThis operation returns the coordinates of true elements in\r\n\\texttt{input}. The coordinates are returned in a 2-D tensor where the\r\nfirst dimension (rows) represents the number of true elements, and the\r\nsecond dimension (columns) represents the coordinates of the true\r\nelements. Keep in mind, the shape of the output tensor can vary\r\ndepending on how many true values there are in \\texttt{input}. Indices\r\nare output in row-major order.\r\n\r\nFor example:\r\n\r\n\\begin{verbatim}\r\n# 'input' tensor is [[True, False]\r\n#                    [True, False]]\r\n# 'input' has two true values, so output has two coordinates.\r\n# 'input' has rank of 2, so coordinates have two indices.\r\nwhere(input) ==> [[0, 0],\r\n                  [1, 0]]\r\n\r\n# `input` tensor is [[[True, False]\r\n#                     [True, False]]\r\n#                    [[False, True]\r\n#                     [False, True]]\r\n#                    [[False, False]\r\n#                     [False, True]]]\r\n# 'input' has 5 true values, so output has 5 coordinates.\r\n# 'input' has rank of 3, so coordinates have three indices.\r\nwhere(input) ==> [[0, 0, 0],\r\n                  [0, 1, 0],\r\n                  [1, 0, 1],\r\n                  [1, 1, 1],\r\n                  [2, 1, 1]]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-15}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor} of type \\texttt{bool}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-15}\r\n\r\nA \\texttt{Tensor} of type \\texttt{int64}.\r\n\r\n\\subsection{Debugging Operations }\\label{debugging-operations}\r\n\r\nTensorFlow provides several operations that you can use to validate\r\nvalues and debug your graph.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.is\\_finite(x,\\ name=None)}\r\n}{tf.is\\_finite(x, name=None) }}\\label{tf.isux5ffinitex-namenone}\r\n\r\nReturns which elements of x are finite.\r\n\r\n\\subparagraph{Args: }\\label{args-16}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-16}\r\n\r\nA \\texttt{Tensor} of type \\texttt{bool}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.is\\_inf(x,\\ name=None)}\r\n}{tf.is\\_inf(x, name=None) }}\\label{tf.isux5finfx-namenone}\r\n\r\nReturns which elements of x are Inf.\r\n\r\n\\subparagraph{Args: }\\label{args-17}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-17}\r\n\r\nA \\texttt{Tensor} of type \\texttt{bool}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.is\\_nan(x,\\ name=None)}\r\n}{tf.is\\_nan(x, name=None) }}\\label{tf.isux5fnanx-namenone}\r\n\r\nReturns which elements of x are NaN.\r\n\r\n\\subparagraph{Args: }\\label{args-18}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-18}\r\n\r\nA \\texttt{Tensor} of type \\texttt{bool}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.verify\\_tensor\\_all\\_finite(t,\\ msg,\\ name=None)}\r\n}{tf.verify\\_tensor\\_all\\_finite(t, msg, name=None) }}\\label{tf.verifyux5ftensorux5fallux5ffinitet-msg-namenone}\r\n\r\nAssert that the tensor does not contain any NaN's or Inf's.\r\n\r\n\\subparagraph{Args: }\\label{args-19}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{t}: Tensor to check.\r\n\\item\r\n  \\texttt{msg}: Message to log on failure.\r\n\\item\r\n  \\texttt{name}: A name for this operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-19}\r\n\r\nSame tensor as \\texttt{t}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.check\\_numerics(tensor,\\ message,\\ name=None)}\r\n}{tf.check\\_numerics(tensor, message, name=None) }}\\label{tf.checkux5fnumericstensor-message-namenone}\r\n\r\nChecks a tensor for NaN and Inf values.\r\n\r\nWhen run, reports an \\texttt{InvalidArgument} error if \\texttt{tensor}\r\nhas any values that are not a number (NaN) or infinity (Inf). Otherwise,\r\npasses \\texttt{tensor} as-is.\r\n\r\n\\subparagraph{Args: }\\label{args-20}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{tensor}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{float32}, \\texttt{float64}.\r\n\\item\r\n  \\texttt{message}: A \\texttt{string}. Prefix of the error message.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-20}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{tensor}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.add\\_check\\_numerics\\_ops()}\r\n}{tf.add\\_check\\_numerics\\_ops() }}\\label{tf.addux5fcheckux5fnumericsux5fops}\r\n\r\nConnect a check\\_numerics to every floating point tensor.\r\n\r\n\\texttt{check\\_numerics} operations themselves are added for each\r\n\\texttt{float} or \\texttt{double} tensor in the graph. For all ops in\r\nthe graph, the \\texttt{check\\_numerics} op for all of its\r\n(\\texttt{float} or \\texttt{double}) inputs is guaranteed to run before\r\nthe \\texttt{check\\_numerics} op on any of its outputs.\r\n\r\n\\subparagraph{Returns: }\\label{returns-21}\r\n\r\nA \\texttt{group} op depending on all \\texttt{check\\_numerics} ops added.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.Assert(condition,\\ data,\\ summarize=None,\\ name=None)}\r\n}{tf.Assert(condition, data, summarize=None, name=None) }}\\label{tf.assertcondition-data-summarizenone-namenone}\r\n\r\nAsserts that the given condition is true.\r\n\r\nIf \\texttt{condition} evaluates to false, print the list of tensors in\r\n\\texttt{data}. \\texttt{summarize} determines how many entries of the\r\ntensors to print.\r\n\r\n\\subparagraph{Args: }\\label{args-21}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{condition}: The condition to evaluate.\r\n\\item\r\n  \\texttt{data}: The tensors to print out when condition is false.\r\n\\item\r\n  \\texttt{summarize}: Print this many entries of each tensor.\r\n\\item\r\n  \\texttt{name}: A name for this operation (optional).\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.Print(input\\_,\\ data,\\ message=None,\\ first\\_n=None,\\ summarize=None,\\ name=None)}\r\n}{tf.Print(input\\_, data, message=None, first\\_n=None, summarize=None, name=None) }}\\label{tf.printinputux5f-data-messagenone-firstux5fnnone-summarizenone-namenone}\r\n\r\nPrints a list of tensors.\r\n\r\nThis is an identity op with the side effect of printing \\texttt{data}\r\nwhen evaluating.\r\n\r\n\\subparagraph{Args: }\\label{args-22}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input\\_}: A tensor passed through this op.\r\n\\item\r\n  \\texttt{data}: A list of tensors to print out when op is evaluated.\r\n\\item\r\n  \\texttt{message}: A string, prefix of the error message.\r\n\\item\r\n  \\texttt{first\\_n}: Only log \\texttt{first\\_n} number of times.\r\n  Negative numbers log always; this is the default.\r\n\\item\r\n  \\texttt{summarize}: Only print this many entries of each tensor.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-22}\r\n\r\nSame tensor as \\texttt{input\\_}.\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/python/c4s07_image.tex",
    "content": "\r\n\r\n\r\n\\section{Images }\\label{images}\r\n\r\nNote: Functions taking \\texttt{Tensor} arguments can also take anything\r\naccepted by\r\n\\href{../../api_docs/python/framework.md\\#convert_to_tensor}{\\texttt{tf.convert\\_to\\_tensor}}.\r\n\r\n\\subsection{Contents}\\label{contents}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-images}{Images}}{Images}}\\label{images-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-encoding-and-decoding}{Encoding and\r\n  Decoding}\r\n\\item\r\n  \\protect\\hyperlink{decodeux5fjpeg}{\\texttt{tf.image.decode\\_jpeg(contents,\\ channels=None,\\ ratio=None,\\ fancy\\_upscaling=None,\\ try\\_recover\\_truncated=None,\\ acceptable\\_fraction=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{encodeux5fjpeg}{\\texttt{tf.image.encode\\_jpeg(image,\\ format=None,\\ quality=None,\\ progressive=None,\\ optimize\\_size=None,\\ chroma\\_downsampling=None,\\ density\\_unit=None,\\ x\\_density=None,\\ y\\_density=None,\\ xmp\\_metadata=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{decodeux5fpng}{\\texttt{tf.image.decode\\_png(contents,\\ channels=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{encodeux5fpng}{\\texttt{tf.image.encode\\_png(image,\\ compression=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-resizing}{Resizing}\r\n\\item\r\n  \\protect\\hyperlink{resizeux5fimages}{\\texttt{tf.image.resize\\_images(images,\\ new\\_height,\\ new\\_width,\\ method=0)}}\r\n\\item\r\n  \\protect\\hyperlink{resizeux5farea}{\\texttt{tf.image.resize\\_area(images,\\ size,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{resizeux5fbicubic}{\\texttt{tf.image.resize\\_bicubic(images,\\ size,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{resizeux5fbilinear}{\\texttt{tf.image.resize\\_bilinear(images,\\ size,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{resizeux5fnearestux5fneighbor}{\\texttt{tf.image.resize\\_nearest\\_neighbor(images,\\ size,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-cropping}{Cropping}\r\n\\item\r\n  \\protect\\hyperlink{resizeux5fimageux5fwithux5fcropux5forux5fpad}{\\texttt{tf.image.resize\\_image\\_with\\_crop\\_or\\_pad(image,\\ target\\_height,\\ target\\_width)}}\r\n\\item\r\n  \\protect\\hyperlink{padux5ftoux5fboundingux5fbox}{\\texttt{tf.image.pad\\_to\\_bounding\\_box(image,\\ offset\\_height,\\ offset\\_width,\\ target\\_height,\\ target\\_width)}}\r\n\\item\r\n  \\protect\\hyperlink{cropux5ftoux5fboundingux5fbox}{\\texttt{tf.image.crop\\_to\\_bounding\\_box(image,\\ offset\\_height,\\ offset\\_width,\\ target\\_height,\\ target\\_width)}}\r\n\\item\r\n  \\protect\\hyperlink{randomux5fcrop}{\\texttt{tf.image.random\\_crop(image,\\ size,\\ seed=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{extractux5fglimpse}{\\texttt{tf.image.extract\\_glimpse(input,\\ size,\\ offsets,\\ centered=None,\\ normalized=None,\\ uniform\\_noise=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-flipping-and-transposing}{Flipping\r\n  and Transposing}\r\n\\item\r\n  \\protect\\hyperlink{flipux5fupux5fdown}{\\texttt{tf.image.flip\\_up\\_down(image)}}\r\n\\item\r\n  \\protect\\hyperlink{randomux5fflipux5fupux5fdown}{\\texttt{tf.image.random\\_flip\\_up\\_down(image,\\ seed=None)}}\r\n\\item\r\n  \\protect\\hyperlink{flipux5fleftux5fright}{\\texttt{tf.image.flip\\_left\\_right(image)}}\r\n\\item\r\n  \\protect\\hyperlink{randomux5fflipux5fleftux5fright}{\\texttt{tf.image.random\\_flip\\_left\\_right(image,\\ seed=None)}}\r\n\\item\r\n  \\protect\\hyperlink{transposeux5fimage}{\\texttt{tf.image.transpose\\_image(image)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-image-adjustments}{Image Adjustments}\r\n\\item\r\n  \\protect\\hyperlink{adjustux5fbrightness}{\\texttt{tf.image.adjust\\_brightness(image,\\ delta,\\ min\\_value=None,\\ max\\_value=None)}}\r\n\\item\r\n  \\protect\\hyperlink{randomux5fbrightness}{\\texttt{tf.image.random\\_brightness(image,\\ max\\_delta,\\ seed=None)}}\r\n\\item\r\n  \\protect\\hyperlink{adjustux5fcontrast}{\\texttt{tf.image.adjust\\_contrast(images,\\ contrast\\_factor,\\ min\\_value=None,\\ max\\_value=None)}}\r\n\\item\r\n  \\protect\\hyperlink{randomux5fcontrast}{\\texttt{tf.image.random\\_contrast(image,\\ lower,\\ upper,\\ seed=None)}}\r\n\\item\r\n  \\protect\\hyperlink{perux5fimageux5fwhitening}{\\texttt{tf.image.per\\_image\\_whitening(image)}}\r\n\\end{itemize}\r\n\r\n\\subsection{Encoding and Decoding }\\label{encoding-and-decoding}\r\n\r\nTensorFlow provides Ops to decode and encode JPEG and PNG formats.\r\nEncoded images are represented by scalar string Tensors, decoded images\r\nby 3-D uint8 tensors of shape \\texttt{{[}height,\\ width,\\ channels{]}}.\r\n\r\nThe encode and decode Ops apply to one image at a time. Their input and\r\noutput are all of variable size. If you need fixed size images, pass the\r\noutput of the decode Ops to one of the cropping and resizing Ops.\r\n\r\nNote: The PNG encode and decode Ops support RGBA, but the conversions\r\nOps presently only support RGB, HSV, and GrayScale.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.decode\\_jpeg(contents,\\ channels=None,\\ ratio=None,\\ fancy\\_upscaling=None,\\ try\\_recover\\_truncated=None,\\ acceptable\\_fraction=None,\\ name=None)}\r\n}{tf.image.decode\\_jpeg(contents, channels=None, ratio=None, fancy\\_upscaling=None, try\\_recover\\_truncated=None, acceptable\\_fraction=None, name=None) }}\\label{tf.image.decodeux5fjpegcontents-channelsnone-rationone-fancyux5fupscalingnone-tryux5frecoverux5ftruncatednone-acceptableux5ffractionnone-namenone}\r\n\r\nDecode a JPEG-encoded image to a uint8 tensor.\r\n\r\nThe attr \\texttt{channels} indicates the desired number of color\r\nchannels for the decoded image.\r\n\r\nAccepted values are:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  0: Use the number of channels in the JPEG-encoded image.\r\n\\item\r\n  1: output a grayscale image.\r\n\\item\r\n  3: output an RGB image.\r\n\\end{itemize}\r\n\r\nIf needed, the JPEG-encoded image is transformed to match the requested\r\nnumber of color channels.\r\n\r\nThe attr \\texttt{ratio} allows downscaling the image by an integer\r\nfactor during decoding. Allowed values are: 1, 2, 4, and 8. This is much\r\nfaster than downscaling the image later.\r\n\r\n\\subparagraph{Args: }\\label{args}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{contents}: A \\texttt{Tensor} of type \\texttt{string}. 0-D. The\r\n  JPEG-encoded image.\r\n\\item\r\n  \\texttt{channels}: An optional \\texttt{int}. Defaults to \\texttt{0}.\r\n  Number of color channels for the decoded image.\r\n\\item\r\n  \\texttt{ratio}: An optional \\texttt{int}. Defaults to \\texttt{1}.\r\n  Downscaling ratio.\r\n\\item\r\n  \\texttt{fancy\\_upscaling}: An optional \\texttt{bool}. Defaults to\r\n  \\texttt{True}. If true use a slower but nicer upscaling of the chroma\r\n  planes (yuv420/422 only).\r\n\\item\r\n  \\texttt{try\\_recover\\_truncated}: An optional \\texttt{bool}. Defaults\r\n  to \\texttt{False}. If true try to recover an image from truncated\r\n  input.\r\n\\item\r\n  \\texttt{acceptable\\_fraction}: An optional \\texttt{float}. Defaults to\r\n  \\texttt{1}. The minimum required fraction of lines before a truncated\r\n  input is accepted.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns}\r\n\r\nA \\texttt{Tensor} of type \\texttt{uint8}. 3-D with shape\r\n\\texttt{{[}height,\\ width,\\ channels{]}}..\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.encode\\_jpeg(image,\\ format=None,\\ quality=None,\\ progressive=None,\\ optimize\\_size=None,\\ chroma\\_downsampling=None,\\ density\\_unit=None,\\ x\\_density=None,\\ y\\_density=None,\\ xmp\\_metadata=None,\\ name=None)}\r\n}{tf.image.encode\\_jpeg(image, format=None, quality=None, progressive=None, optimize\\_size=None, chroma\\_downsampling=None, density\\_unit=None, x\\_density=None, y\\_density=None, xmp\\_metadata=None, name=None) }}\\label{tf.image.encodeux5fjpegimage-formatnone-qualitynone-progressivenone-optimizeux5fsizenone-chromaux5fdownsamplingnone-densityux5funitnone-xux5fdensitynone-yux5fdensitynone-xmpux5fmetadatanone-namenone}\r\n\r\nJPEG-encode an image.\r\n\r\n\\texttt{image} is a 3-D uint8 Tensor of shape\r\n\\texttt{{[}height,\\ width,\\ channels{]}}.\r\n\r\nThe attr \\texttt{format} can be used to override the color format of the\r\nencoded output. Values can be:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{\\textquotesingle{}\\textquotesingle{}}: Use a default format\r\n  based on the number of channels in the image.\r\n\\item\r\n  \\texttt{grayscale}: Output a grayscale JPEG image. The\r\n  \\texttt{channels} dimension of \\texttt{image} must be 1.\r\n\\item\r\n  \\texttt{rgb}: Output an RGB JPEG image. The \\texttt{channels}\r\n  dimension of \\texttt{image} must be 3.\r\n\\end{itemize}\r\n\r\nIf \\texttt{format} is not specified or is the empty string, a default\r\nformat is picked in function of the number of channels in\r\n\\texttt{image}:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  1: Output a grayscale image.\r\n\\item\r\n  3: Output an RGB image.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Args: }\\label{args-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: A \\texttt{Tensor} of type \\texttt{uint8}. 3-D with\r\n  shape \\texttt{{[}height,\\ width,\\ channels{]}}.\r\n\\item\r\n  \\texttt{format}: An optional \\texttt{string} from:\r\n  \\texttt{\"\",\\ \"grayscale\",\\ \"rgb\"}. Defaults to \\texttt{\"\"}. Per pixel\r\n  image format.\r\n\\item\r\n  \\texttt{quality}: An optional \\texttt{int}. Defaults to \\texttt{95}.\r\n  Quality of the compression from 0 to 100 (higher is better and\r\n  slower).\r\n\\item\r\n  \\texttt{progressive}: An optional \\texttt{bool}. Defaults to\r\n  \\texttt{False}. If True, create a JPEG that loads progressively\r\n  (coarse to fine).\r\n\\item\r\n  \\texttt{optimize\\_size}: An optional \\texttt{bool}. Defaults to\r\n  \\texttt{False}. If True, spend CPU/RAM to reduce size with no quality\r\n  change.\r\n\\item\r\n  \\texttt{chroma\\_downsampling}: An optional \\texttt{bool}. Defaults to\r\n  \\texttt{True}. See http://en.wikipedia.org/wiki/Chroma\\_subsampling.\r\n\\item\r\n  \\texttt{density\\_unit}: An optional \\texttt{string} from:\r\n  \\texttt{\"in\",\\ \"cm\"}. Defaults to \\texttt{\"in\"}. Unit used to specify\r\n  \\texttt{x\\_density} and \\texttt{y\\_density}: pixels per inch\r\n  (\\texttt{\\textquotesingle{}in\\textquotesingle{}}) or centimeter\r\n  (\\texttt{\\textquotesingle{}cm\\textquotesingle{}}).\r\n\\item\r\n  \\texttt{x\\_density}: An optional \\texttt{int}. Defaults to\r\n  \\texttt{300}. Horizontal pixels per density unit.\r\n\\item\r\n  \\texttt{y\\_density}: An optional \\texttt{int}. Defaults to\r\n  \\texttt{300}. Vertical pixels per density unit.\r\n\\item\r\n  \\texttt{xmp\\_metadata}: An optional \\texttt{string}. Defaults to\r\n  \\texttt{\"\"}. If not empty, embed this XMP metadata in the image\r\n  header.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-1}\r\n\r\nA \\texttt{Tensor} of type \\texttt{string}. 0-D. JPEG-encoded image.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.decode\\_png(contents,\\ channels=None,\\ name=None)}\r\n}{tf.image.decode\\_png(contents, channels=None, name=None) }}\\label{tf.image.decodeux5fpngcontents-channelsnone-namenone}\r\n\r\nDecode a PNG-encoded image to a uint8 tensor.\r\n\r\nThe attr \\texttt{channels} indicates the desired number of color\r\nchannels for the decoded image.\r\n\r\nAccepted values are:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  0: Use the number of channels in the PNG-encoded image.\r\n\\item\r\n  1: output a grayscale image.\r\n\\item\r\n  3: output an RGB image.\r\n\\item\r\n  4: output an RGBA image.\r\n\\end{itemize}\r\n\r\nIf needed, the PNG-encoded image is transformed to match the requested\r\nnumber of color channels.\r\n\r\n\\subparagraph{Args: }\\label{args-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{contents}: A \\texttt{Tensor} of type \\texttt{string}. 0-D. The\r\n  PNG-encoded image.\r\n\\item\r\n  \\texttt{channels}: An optional \\texttt{int}. Defaults to \\texttt{0}.\r\n  Number of color channels for the decoded image.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-2}\r\n\r\nA \\texttt{Tensor} of type \\texttt{uint8}. 3-D with shape\r\n\\texttt{{[}height,\\ width,\\ channels{]}}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.encode\\_png(image,\\ compression=None,\\ name=None)}\r\n}{tf.image.encode\\_png(image, compression=None, name=None) }}\\label{tf.image.encodeux5fpngimage-compressionnone-namenone}\r\n\r\nPNG-encode an image.\r\n\r\n\\texttt{image} is a 3-D uint8 Tensor of shape\r\n\\texttt{{[}height,\\ width,\\ channels{]}} where \\texttt{channels} is:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  1: for grayscale.\r\n\\item\r\n  3: for RGB.\r\n\\item\r\n  4: for RGBA.\r\n\\end{itemize}\r\n\r\nThe ZLIB compression level, \\texttt{compression}, can be -1 for the\r\nPNG-encoder default or a value from 0 to 9. 9 is the highest compression\r\nlevel, generating the smallest output, but is slower.\r\n\r\n\\subparagraph{Args: }\\label{args-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: A \\texttt{Tensor} of type \\texttt{uint8}. 3-D with\r\n  shape \\texttt{{[}height,\\ width,\\ channels{]}}.\r\n\\item\r\n  \\texttt{compression}: An optional \\texttt{int}. Defaults to\r\n  \\texttt{-1}. Compression level.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-3}\r\n\r\nA \\texttt{Tensor} of type \\texttt{string}. 0-D. PNG-encoded image.\r\n\r\n\\subsection{Resizing }\\label{resizing}\r\n\r\nThe resizing Ops accept input images as tensors of several types. They\r\nalways output resized images as float32 tensors.\r\n\r\nThe convenience function\r\n\\protect\\hyperlink{resizeux5fimages}{resize\\_images()} supports both 4-D\r\nand 3-D tensors as input and output. 4-D tensors are for batches of\r\nimages, 3-D tensors for individual images.\r\n\r\nOther resizing Ops only support 3-D individual images as input:\r\n\\protect\\hyperlink{resizeux5farea}{resize\\_area},\r\n\\protect\\hyperlink{resizeux5fbicubic}{resize\\_bicubic},\r\n\\protect\\hyperlink{resizeux5fbilinear}{resize\\_bilinear},\r\n\\protect\\hyperlink{resizeux5fnearestux5fneighbor}{resize\\_nearest\\_neighbor}.\r\n\r\nExample:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Decode a JPG image and resize it to 299 by 299.}\r\n\\NormalTok{image }\\OperatorTok{=} \\NormalTok{tf.image.decode_jpeg(...)}\r\n\\NormalTok{resized_image }\\OperatorTok{=} \\NormalTok{tf.image.resize_bilinear(image, [}\\DecValTok{299}\\NormalTok{, }\\DecValTok{299}\\NormalTok{])}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nMaybe refer to the Queue examples that show how to add images to a Queue\r\nafter resizing them to a fixed size, and how to dequeue batches of\r\nresized images from the Queue.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.resize\\_images(images,\\ new\\_height,\\ new\\_width,\\ method=0)}\r\n}{tf.image.resize\\_images(images, new\\_height, new\\_width, method=0) }}\\label{tf.image.resizeux5fimagesimages-newux5fheight-newux5fwidth-method0}\r\n\r\nResize \\texttt{images} to \\texttt{new\\_width}, \\texttt{new\\_height}\r\nusing the specified \\texttt{method}.\r\n\r\nResized images will be distorted if their original aspect ratio is not\r\nthe same as \\texttt{new\\_width}, \\texttt{new\\_height}. To avoid\r\ndistortions see\r\n\\protect\\hyperlink{resizeux5fimageux5fwithux5fcropux5forux5fpad}{resize\\_image\\_with\\_crop\\_or\\_pad}.\r\n\r\n\\texttt{method} can be one of:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  ResizeMethod.BILINEAR: {[}Bilinear interpolation.{]}\r\n  (https://en.wikipedia.org/wiki/Bilinear\\_interpolation)\r\n\\item\r\n  ResizeMethod.NEAREST\\_NEIGHBOR: {[}Nearest neighbor interpolation.{]}\r\n  (https://en.wikipedia.org/wiki/Nearest-neighbor\\_interpolation)\r\n\\item\r\n  ResizeMethod.BICUBIC: {[}Bicubic interpolation.{]}\r\n  (https://en.wikipedia.org/wiki/Bicubic\\_interpolation)\r\n\\item\r\n  ResizeMethod.AREA: Area interpolation.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Args: }\\label{args-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{images}: 4-D Tensor of shape\r\n  \\texttt{{[}batch,\\ height,\\ width,\\ channels{]}} or 3-D Tensor of\r\n  shape \\texttt{{[}height,\\ width,\\ channels{]}}.\r\n\\item\r\n  \\texttt{new\\_height}: integer.\r\n\\item\r\n  \\texttt{new\\_width}: integer.\r\n\\item\r\n  \\texttt{method}: ResizeMethod. Defaults to\r\n  \\texttt{ResizeMethod.BILINEAR}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if the shape of \\texttt{images} is incompatible\r\n  with the shape arguments to this function\r\n\\item\r\n  \\texttt{ValueError}: if an unsupported resize method is specified.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-4}\r\n\r\nIf \\texttt{images} was 4-D, a 4-D float Tensor of shape\r\n\\texttt{{[}batch,\\ new\\_height,\\ new\\_width,\\ channels{]}}. If\r\n\\texttt{images} was 3-D, a 3-D float Tensor of shape\r\n\\texttt{{[}new\\_height,\\ new\\_width,\\ channels{]}}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.resize\\_area(images,\\ size,\\ name=None)}\r\n}{tf.image.resize\\_area(images, size, name=None) }}\\label{tf.image.resizeux5fareaimages-size-namenone}\r\n\r\nResize \\texttt{images} to \\texttt{size} using area interpolation.\r\n\r\nInput images can be of different types but output images are always\r\nfloat.\r\n\r\n\\subparagraph{Args: }\\label{args-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{images}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{uint8}, \\texttt{int8}, \\texttt{int32},\r\n  \\texttt{float32}, \\texttt{float64}. 4-D with shape\r\n  \\texttt{{[}batch,\\ height,\\ width,\\ channels{]}}.\r\n\\item\r\n  \\texttt{size}: A 1-D int32 Tensor of 2 elements:\r\n  \\texttt{new\\_height,\\ new\\_width}. The new size for the images.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-5}\r\n\r\nA \\texttt{Tensor} of type \\texttt{float32}. 4-D with shape\r\n\\texttt{{[}batch,\\ new\\_height,\\ new\\_width,\\ channels{]}}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.resize\\_bicubic(images,\\ size,\\ name=None)}\r\n}{tf.image.resize\\_bicubic(images, size, name=None) }}\\label{tf.image.resizeux5fbicubicimages-size-namenone}\r\n\r\nResize \\texttt{images} to \\texttt{size} using bicubic interpolation.\r\n\r\nInput images can be of different types but output images are always\r\nfloat.\r\n\r\n\\subparagraph{Args: }\\label{args-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{images}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{uint8}, \\texttt{int8}, \\texttt{int32},\r\n  \\texttt{float32}, \\texttt{float64}. 4-D with shape\r\n  \\texttt{{[}batch,\\ height,\\ width,\\ channels{]}}.\r\n\\item\r\n  \\texttt{size}: A 1-D int32 Tensor of 2 elements:\r\n  \\texttt{new\\_height,\\ new\\_width}. The new size for the images.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-6}\r\n\r\nA \\texttt{Tensor} of type \\texttt{float32}. 4-D with shape\r\n\\texttt{{[}batch,\\ new\\_height,\\ new\\_width,\\ channels{]}}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.resize\\_bilinear(images,\\ size,\\ name=None)}\r\n}{tf.image.resize\\_bilinear(images, size, name=None) }}\\label{tf.image.resizeux5fbilinearimages-size-namenone}\r\n\r\nResize \\texttt{images} to \\texttt{size} using bilinear interpolation.\r\n\r\nInput images can be of different types but output images are always\r\nfloat.\r\n\r\n\\subparagraph{Args: }\\label{args-7}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{images}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{uint8}, \\texttt{int8}, \\texttt{int32},\r\n  \\texttt{float32}, \\texttt{float64}. 4-D with shape\r\n  \\texttt{{[}batch,\\ height,\\ width,\\ channels{]}}.\r\n\\item\r\n  \\texttt{size}: A 1-D int32 Tensor of 2 elements:\r\n  \\texttt{new\\_height,\\ new\\_width}. The new size for the images.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-7}\r\n\r\nA \\texttt{Tensor} of type \\texttt{float32}. 4-D with shape\r\n\\texttt{{[}batch,\\ new\\_height,\\ new\\_width,\\ channels{]}}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.resize\\_nearest\\_neighbor(images,\\ size,\\ name=None)}\r\n}{tf.image.resize\\_nearest\\_neighbor(images, size, name=None) }}\\label{tf.image.resizeux5fnearestux5fneighborimages-size-namenone}\r\n\r\nResize \\texttt{images} to \\texttt{size} using nearest neighbor\r\ninterpolation.\r\n\r\nInput images can be of different types but output images are always\r\nfloat.\r\n\r\n\\subparagraph{Args: }\\label{args-8}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{images}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{uint8}, \\texttt{int8}, \\texttt{int32},\r\n  \\texttt{float32}, \\texttt{float64}. 4-D with shape\r\n  \\texttt{{[}batch,\\ height,\\ width,\\ channels{]}}.\r\n\\item\r\n  \\texttt{size}: A 1-D int32 Tensor of 2 elements:\r\n  \\texttt{new\\_height,\\ new\\_width}. The new size for the images.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-8}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{images}. 4-D with shape\r\n\\texttt{{[}batch,\\ new\\_height,\\ new\\_width,\\ channels{]}}.\r\n\r\n\\subsection{Cropping }\\label{cropping}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.resize\\_image\\_with\\_crop\\_or\\_pad(image,\\ target\\_height,\\ target\\_width)}\r\n}{tf.image.resize\\_image\\_with\\_crop\\_or\\_pad(image, target\\_height, target\\_width) }}\\label{tf.image.resizeux5fimageux5fwithux5fcropux5forux5fpadimage-targetux5fheight-targetux5fwidth}\r\n\r\nCrops and/or pads an image to a target width and height.\r\n\r\nResizes an image to a target width and height by either centrally\r\ncropping the image or padding it evenly with zeros.\r\n\r\nIf \\texttt{width} or \\texttt{height} is greater than the specified\r\n\\texttt{target\\_width} or \\texttt{target\\_height} respectively, this op\r\ncentrally crops along that dimension. If \\texttt{width} or\r\n\\texttt{height} is smaller than the specified \\texttt{target\\_width} or\r\n\\texttt{target\\_height} respectively, this op centrally pads with 0\r\nalong that dimension.\r\n\r\n\\subparagraph{Args: }\\label{args-9}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: 3-D tensor of shape {[}height, width, channels{]}\r\n\\item\r\n  \\texttt{target\\_height}: Target height.\r\n\\item\r\n  \\texttt{target\\_width}: Target width.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if \\texttt{target\\_height} or\r\n  \\texttt{target\\_width} are zero or negative.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-9}\r\n\r\nCropped and/or padded image of shape\r\n\\texttt{{[}target\\_height,\\ target\\_width,\\ channels{]}}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.pad\\_to\\_bounding\\_box(image,\\ offset\\_height,\\ offset\\_width,\\ target\\_height,\\ target\\_width)}\r\n}{tf.image.pad\\_to\\_bounding\\_box(image, offset\\_height, offset\\_width, target\\_height, target\\_width) }}\\label{tf.image.padux5ftoux5fboundingux5fboximage-offsetux5fheight-offsetux5fwidth-targetux5fheight-targetux5fwidth}\r\n\r\nPad \\texttt{image} with zeros to the specified \\texttt{height} and\r\n\\texttt{width}.\r\n\r\nAdds \\texttt{offset\\_height} rows of zeros on top,\r\n\\texttt{offset\\_width} columns of zeros on the left, and then pads the\r\nimage on the bottom and right with zeros until it has dimensions\r\n\\texttt{target\\_height}, \\texttt{target\\_width}.\r\n\r\nThis op does nothing if \\texttt{offset\\_*} is zero and the image already\r\nhas size \\texttt{target\\_height} by \\texttt{target\\_width}.\r\n\r\n\\subparagraph{Args: }\\label{args-10}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: 3-D tensor with shape\r\n  \\texttt{{[}height,\\ width,\\ channels{]}}\r\n\\item\r\n  \\texttt{offset\\_height}: Number of rows of zeros to add on top.\r\n\\item\r\n  \\texttt{offset\\_width}: Number of columns of zeros to add on the left.\r\n\\item\r\n  \\texttt{target\\_height}: Height of output image.\r\n\\item\r\n  \\texttt{target\\_width}: Width of output image.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-10}\r\n\r\n3-D tensor of shape\r\n\\texttt{{[}target\\_height,\\ target\\_width,\\ channels{]}}\r\n\r\n\\subparagraph{Raises: }\\label{raises-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: If the shape of \\texttt{image} is incompatible\r\n  with the \\texttt{offset\\_*} or \\texttt{target\\_*} arguments\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.crop\\_to\\_bounding\\_box(image,\\ offset\\_height,\\ offset\\_width,\\ target\\_height,\\ target\\_width)}\r\n}{tf.image.crop\\_to\\_bounding\\_box(image, offset\\_height, offset\\_width, target\\_height, target\\_width) }}\\label{tf.image.cropux5ftoux5fboundingux5fboximage-offsetux5fheight-offsetux5fwidth-targetux5fheight-targetux5fwidth}\r\n\r\nCrops an image to a specified bounding box.\r\n\r\nThis op cuts a rectangular part out of \\texttt{image}. The top-left\r\ncorner of the returned image is at\r\n\\texttt{offset\\_height,\\ offset\\_width} in \\texttt{image}, and its\r\nlower-right corner is at `offset\\_height + target\\_height, offset\\_width\r\n+ target\\_width'.\r\n\r\n\\subparagraph{Args: }\\label{args-11}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: 3-D tensor with shape\r\n  \\texttt{{[}height,\\ width,\\ channels{]}}\r\n\\item\r\n  \\texttt{offset\\_height}: Vertical coordinate of the top-left corner of\r\n  the result in the input.\r\n\\item\r\n  \\texttt{offset\\_width}: Horizontal coordinate of the top-left corner\r\n  of the result in the input.\r\n\\item\r\n  \\texttt{target\\_height}: Height of the result.\r\n\\item\r\n  \\texttt{target\\_width}: Width of the result.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-11}\r\n\r\n3-D tensor of image with shape\r\n\\texttt{{[}target\\_height,\\ target\\_width,\\ channels{]}}\r\n\r\n\\subparagraph{Raises: }\\label{raises-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: If the shape of \\texttt{image} is incompatible\r\n  with the \\texttt{offset\\_*} or \\texttt{target\\_*} arguments\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.random\\_crop(image,\\ size,\\ seed=None,\\ name=None)}\r\n}{tf.image.random\\_crop(image, size, seed=None, name=None) }}\\label{tf.image.randomux5fcropimage-size-seednone-namenone}\r\n\r\nRandomly crops \\texttt{image} to size\r\n\\texttt{{[}target\\_height,\\ target\\_width{]}}.\r\n\r\nThe offset of the output within \\texttt{image} is uniformly random.\r\n\\texttt{image} always fully contains the result.\r\n\r\n\\subparagraph{Args: }\\label{args-12}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: 3-D tensor of shape\r\n  \\texttt{{[}height,\\ width,\\ channels{]}}\r\n\\item\r\n  \\texttt{size}: 1-D tensor with two elements, specifying target\r\n  \\texttt{{[}height,\\ width{]}}\r\n\\item\r\n  \\texttt{seed}: A Python integer. Used to create a random seed. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\texttt{set\\_random\\_seed}}\r\n  for behavior.\r\n\\item\r\n  \\texttt{name}: A name for this operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-12}\r\n\r\nA cropped 3-D tensor of shape\r\n\\texttt{{[}target\\_height,\\ target\\_width,\\ channels{]}}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.extract\\_glimpse(input,\\ size,\\ offsets,\\ centered=None,\\ normalized=None,\\ uniform\\_noise=None,\\ name=None)}\r\n}{tf.image.extract\\_glimpse(input, size, offsets, centered=None, normalized=None, uniform\\_noise=None, name=None) }}\\label{tf.image.extractux5fglimpseinput-size-offsets-centerednone-normalizednone-uniformux5fnoisenone-namenone}\r\n\r\nExtracts a glimpse from the input tensor.\r\n\r\nReturns a set of windows called glimpses extracted at location\r\n\\texttt{offsets} from the input tensor. If the windows only partially\r\noverlaps the inputs, the non overlapping areas will be filled with\r\nrandom noise.\r\n\r\nThe result is a 4-D tensor of shape\r\n\\texttt{{[}batch\\_size,\\ glimpse\\_height,\\ glimpse\\_width,\\ channels{]}}.\r\nThe channels and batch dimensions are the same as that of the input\r\ntensor. The height and width of the output windows are specified in the\r\n\\texttt{size} parameter.\r\n\r\nThe argument \\texttt{normalized} and \\texttt{centered} controls how the\r\nwindows are built: * If the coordinates are normalized but not centered,\r\n0.0 and 1.0 correspond to the minimum and maximum of each height and\r\nwidth dimension. * If the coordinates are both normalized and centered,\r\nthey range from -1.0 to 1.0. The coordinates (-1.0, -1.0) correspond to\r\nthe upper left corner, the lower right corner is located at (1.0, 1.0)\r\nand the center is at (0, 0). * If the coordinates are not normalized\r\nthey are interpreted as numbers of pixels.\r\n\r\n\\subparagraph{Args: }\\label{args-13}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor} of type \\texttt{float32}. A 4-D\r\n  float tensor of shape\r\n  \\texttt{{[}batch\\_size,\\ height,\\ width,\\ channels{]}}.\r\n\\item\r\n  \\texttt{size}: A \\texttt{Tensor} of type \\texttt{int32}. A 1-D tensor\r\n  of 2 elements containing the size of the glimpses to extract. The\r\n  glimpse height must be specified first, following by the glimpse\r\n  width.\r\n\\item\r\n  \\texttt{offsets}: A \\texttt{Tensor} of type \\texttt{float32}. A 2-D\r\n  integer tensor of shape \\texttt{{[}batch\\_size,\\ 2{]}} containing the\r\n  x, y locations of the center of each window.\r\n\\item\r\n  \\texttt{centered}: An optional \\texttt{bool}. Defaults to\r\n  \\texttt{True}. indicates if the offset coordinates are centered\r\n  relative to the image, in which case the (0, 0) offset is relative to\r\n  the center of the input images. If false, the (0,0) offset corresponds\r\n  to the upper left corner of the input images.\r\n\\item\r\n  \\texttt{normalized}: An optional \\texttt{bool}. Defaults to\r\n  \\texttt{True}. indicates if the offset coordinates are normalized.\r\n\\item\r\n  \\texttt{uniform\\_noise}: An optional \\texttt{bool}. Defaults to\r\n  \\texttt{True}. indicates if the noise should be generated using a\r\n  uniform distribution or a gaussian distribution.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-13}\r\n\r\nA \\texttt{Tensor} of type \\texttt{float32}. A tensor representing the\r\nglimpses\r\n\\texttt{{[}batch\\_size,\\ glimpse\\_height,\\ \\ \\ glimpse\\_width,\\ channels{]}}.\r\n\r\n\\subsection{Flipping and Transposing }\\label{flipping-and-transposing}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.flip\\_up\\_down(image)}\r\n}{tf.image.flip\\_up\\_down(image) }}\\label{tf.image.flipux5fupux5fdownimage}\r\n\r\nFlip an image horizontally (upside down).\r\n\r\nOutputs the contents of \\texttt{image} flipped along the first\r\ndimension, which is \\texttt{height}.\r\n\r\nSee also \\texttt{reverse()}.\r\n\r\n\\subparagraph{Args: }\\label{args-14}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: A 3-D tensor of shape\r\n  \\texttt{{[}height,\\ width,\\ channels{]}.}\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-14}\r\n\r\nA 3-D tensor of the same type and shape as \\texttt{image}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if the shape of \\texttt{image} not supported.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.random\\_flip\\_up\\_down(image,\\ seed=None)}\r\n}{tf.image.random\\_flip\\_up\\_down(image, seed=None) }}\\label{tf.image.randomux5fflipux5fupux5fdownimage-seednone}\r\n\r\nRandomly flips an image vertically (upside down).\r\n\r\nWith a 1 in 2 chance, outputs the contents of \\texttt{image} flipped\r\nalong the first dimension, which is \\texttt{height}. Otherwise output\r\nthe image as-is.\r\n\r\n\\subparagraph{Args: }\\label{args-15}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: A 3-D tensor of shape\r\n  \\texttt{{[}height,\\ width,\\ channels{]}.}\r\n\\item\r\n  \\texttt{seed}: A Python integer. Used to create a random seed. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\texttt{set\\_random\\_seed}}\r\n  for behavior.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-15}\r\n\r\nA 3-D tensor of the same type and shape as \\texttt{image}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if the shape of \\texttt{image} not supported.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.flip\\_left\\_right(image)}\r\n}{tf.image.flip\\_left\\_right(image) }}\\label{tf.image.flipux5fleftux5frightimage}\r\n\r\nFlip an image horizontally (left to right).\r\n\r\nOutputs the contents of \\texttt{image} flipped along the second\r\ndimension, which is \\texttt{width}.\r\n\r\nSee also \\texttt{reverse()}.\r\n\r\n\\subparagraph{Args: }\\label{args-16}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: A 3-D tensor of shape\r\n  \\texttt{{[}height,\\ width,\\ channels{]}.}\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-16}\r\n\r\nA 3-D tensor of the same type and shape as \\texttt{image}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if the shape of \\texttt{image} not supported.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.random\\_flip\\_left\\_right(image,\\ seed=None)}\r\n}{tf.image.random\\_flip\\_left\\_right(image, seed=None) }}\\label{tf.image.randomux5fflipux5fleftux5frightimage-seednone}\r\n\r\nRandomly flip an image horizontally (left to right).\r\n\r\nWith a 1 in 2 chance, outputs the contents of \\texttt{image} flipped\r\nalong the second dimension, which is \\texttt{width}. Otherwise output\r\nthe image as-is.\r\n\r\n\\subparagraph{Args: }\\label{args-17}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: A 3-D tensor of shape\r\n  \\texttt{{[}height,\\ width,\\ channels{]}.}\r\n\\item\r\n  \\texttt{seed}: A Python integer. Used to create a random seed. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\texttt{set\\_random\\_seed}}\r\n  for behavior.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-17}\r\n\r\nA 3-D tensor of the same type and shape as \\texttt{image}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-7}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if the shape of \\texttt{image} not supported.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.transpose\\_image(image)}\r\n}{tf.image.transpose\\_image(image) }}\\label{tf.image.transposeux5fimageimage}\r\n\r\nTranspose an image by swapping the first and second dimension.\r\n\r\nSee also \\texttt{transpose()}.\r\n\r\n\\subparagraph{Args: }\\label{args-18}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: 3-D tensor of shape\r\n  \\texttt{{[}height,\\ width,\\ channels{]}}\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-18}\r\n\r\nA 3-D tensor of shape \\texttt{{[}width,\\ height,\\ channels{]}}\r\n\r\n\\subparagraph{Raises: }\\label{raises-8}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if the shape of \\texttt{image} not supported.\r\n\\end{itemize}\r\n\r\n\\subsection{Image Adjustments }\\label{image-adjustments}\r\n\r\nTensorFlow provides functions to adjust images in various ways:\r\nbrightness, contrast, hue, and saturation. Each adjustment can be done\r\nwith predefined parameters or with random parameters picked from\r\npredefined intervals. Random adjustments are often useful to expand a\r\ntraining set and reduce overfitting.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.adjust\\_brightness(image,\\ delta,\\ min\\_value=None,\\ max\\_value=None)}\r\n}{tf.image.adjust\\_brightness(image, delta, min\\_value=None, max\\_value=None) }}\\label{tf.image.adjustux5fbrightnessimage-delta-minux5fvaluenone-maxux5fvaluenone}\r\n\r\nAdjust the brightness of RGB or Grayscale images.\r\n\r\nThe value \\texttt{delta} is added to all components of the tensor\r\n\\texttt{image}. \\texttt{image} and \\texttt{delta} are cast to\r\n\\texttt{float} before adding, and the resulting values are clamped to\r\n\\texttt{{[}min\\_value,\\ max\\_value{]}}. Finally, the result is cast back\r\nto \\texttt{images.dtype}.\r\n\r\nIf \\texttt{min\\_value} or \\texttt{max\\_value} are not given, they are\r\nset to the minimum and maximum allowed values for \\texttt{image.dtype}\r\nrespectively.\r\n\r\n\\subparagraph{Args: }\\label{args-19}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: A tensor.\r\n\\item\r\n  \\texttt{delta}: A scalar. Amount to add to the pixel values.\r\n\\item\r\n  \\texttt{min\\_value}: Minimum value for output.\r\n\\item\r\n  \\texttt{max\\_value}: Maximum value for output.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-19}\r\n\r\nA tensor of the same shape and type as \\texttt{image}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.random\\_brightness(image,\\ max\\_delta,\\ seed=None)}\r\n}{tf.image.random\\_brightness(image, max\\_delta, seed=None) }}\\label{tf.image.randomux5fbrightnessimage-maxux5fdelta-seednone}\r\n\r\nAdjust the brightness of images by a random factor.\r\n\r\nEquivalent to \\texttt{adjust\\_brightness()} using a \\texttt{delta}\r\nrandomly picked in the interval \\texttt{{[}-max\\_delta,\\ max\\_delta)}.\r\n\r\nNote that \\texttt{delta} is picked as a float. Because for integer type\r\nimages, the brightness adjusted result is rounded before casting,\r\ninteger images may have modifications in the range\r\n\\texttt{{[}-max\\_delta,max\\_delta{]}}.\r\n\r\n\\subparagraph{Args: }\\label{args-20}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: 3-D tensor of shape\r\n  \\texttt{{[}height,\\ width,\\ channels{]}}.\r\n\\item\r\n  \\texttt{max\\_delta}: float, must be non-negative.\r\n\\item\r\n  \\texttt{seed}: A Python integer. Used to create a random seed. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\texttt{set\\_random\\_seed}}\r\n  for behavior.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-20}\r\n\r\n3-D tensor of images of shape \\texttt{{[}height,\\ width,\\ channels{]}}\r\n\r\n\\subparagraph{Raises: }\\label{raises-9}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if max\\_delta is negative.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.adjust\\_contrast(images,\\ contrast\\_factor,\\ min\\_value=None,\\ max\\_value=None)}\r\n}{tf.image.adjust\\_contrast(images, contrast\\_factor, min\\_value=None, max\\_value=None) }}\\label{tf.image.adjustux5fcontrastimages-contrastux5ffactor-minux5fvaluenone-maxux5fvaluenone}\r\n\r\nAdjust contrast of RGB or grayscale images.\r\n\r\n\\texttt{images} is a tensor of at least 3 dimensions. The last 3\r\ndimensions are interpreted as \\texttt{{[}height,\\ width,\\ channels{]}}.\r\nThe other dimensions only represent a collection of images, such as\r\n\\texttt{{[}batch,\\ height,\\ width,\\ channels{]}.}\r\n\r\nContrast is adjusted independently for each channel of each image.\r\n\r\nFor each channel, this Op first computes the mean of the image pixels in\r\nthe channel and then adjusts each component \\texttt{x} of each pixel to\r\n\\texttt{(x\\ -\\ mean)\\ *\\ contrast\\_factor\\ +\\ mean}.\r\n\r\nThe adjusted values are then clipped to fit in the\r\n\\texttt{{[}min\\_value,\\ max\\_value{]}} interval. If \\texttt{min\\_value}\r\nor \\texttt{max\\_value} is not given, it is replaced with the minimum and\r\nmaximum values for the data type of \\texttt{images} respectively.\r\n\r\nThe contrast-adjusted image is always computed as \\texttt{float}, and it\r\nis cast back to its original type after clipping.\r\n\r\n\\subparagraph{Args: }\\label{args-21}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{images}: Images to adjust. At least 3-D.\r\n\\item\r\n  \\texttt{contrast\\_factor}: A float multiplier for adjusting contrast.\r\n\\item\r\n  \\texttt{min\\_value}: Minimum value for clipping the adjusted pixels.\r\n\\item\r\n  \\texttt{max\\_value}: Maximum value for clipping the adjusted pixels.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-21}\r\n\r\nThe constrast-adjusted image or images.\r\n\r\n\\subparagraph{Raises: }\\label{raises-10}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if the arguments are invalid.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.random\\_contrast(image,\\ lower,\\ upper,\\ seed=None)}\r\n}{tf.image.random\\_contrast(image, lower, upper, seed=None) }}\\label{tf.image.randomux5fcontrastimage-lower-upper-seednone}\r\n\r\nAdjust the contrase of an image by a random factor.\r\n\r\nEquivalent to \\texttt{adjust\\_constrast()} but uses a\r\n\\texttt{contrast\\_factor} randomly picked in the interval\r\n\\texttt{{[}lower,\\ upper{]}}.\r\n\r\n\\subparagraph{Args: }\\label{args-22}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: 3-D tensor of shape\r\n  \\texttt{{[}height,\\ width,\\ channels{]}}.\r\n\\item\r\n  \\texttt{lower}: float. Lower bound for the random contrast factor.\r\n\\item\r\n  \\texttt{upper}: float. Upper bound for the random contrast factor.\r\n\\item\r\n  \\texttt{seed}: A Python integer. Used to create a random seed. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\texttt{set\\_random\\_seed}}\r\n  for behavior.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-22}\r\n\r\n3-D tensor of shape \\texttt{{[}height,\\ width,\\ channels{]}}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-11}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if \\texttt{upper\\ \\textless{}=\\ lower} or if\r\n  \\texttt{lower\\ \\textless{}\\ 0}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image.per\\_image\\_whitening(image)}\r\n}{tf.image.per\\_image\\_whitening(image) }}\\label{tf.image.perux5fimageux5fwhiteningimage}\r\n\r\nLinearly scales \\texttt{image} to have zero mean and unit norm.\r\n\r\nThis op computes \\texttt{(x\\ -\\ mean)\\ /\\ adjusted\\_stddev}, where\r\n\\texttt{mean} is the average of all values in image, and\r\n\\texttt{adjusted\\_stddev\\ =\\ max(stddev,\\ 1.0/srqt(image.NumElements()))}.\r\n\r\n\\texttt{stddev} is the standard deviation of all values in\r\n\\texttt{image}. It is capped away from zero to protect against division\r\nby 0 when handling uniform images.\r\n\r\nNote that this implementation is limited: * It only whitens based on the\r\nstatistics of an individual image. * It does not take into account the\r\ncovariance structure.\r\n\r\n\\subparagraph{Args: }\\label{args-23}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{image}: 3-D tensor of shape\r\n  \\texttt{{[}height,\\ width,\\ channels{]}}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-23}\r\n\r\nThe whitened image with same shape as \\texttt{image}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-12}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if the shape of `image' is incompatible with this\r\n  function.\r\n\\end{itemize}\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/python/c4s08_sparse_ops.tex",
    "content": "\r\n\r\n\\section{Sparse Tensors }\\label{sparse-tensors}\r\n\r\nNote: Functions taking \\texttt{Tensor} arguments can also take anything\r\naccepted by\r\n\\href{../../api_docs/python/framework.md\\#convert_to_tensor}{\\texttt{tf.convert\\_to\\_tensor}}.\r\n\r\n\\subsection{Contents}\\label{contents}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-sparse-tensors}{Sparse\r\nTensors}}{Sparse Tensors}}\\label{sparse-tensors-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-sparse-tensor-representation}{Sparse\r\n  Tensor Representation}\r\n\\item\r\n  \\protect\\hyperlink{SparseTensor}{\\texttt{class\\ tf.SparseTensor}}\r\n\\item\r\n  \\protect\\hyperlink{SparseTensorValue}{\\texttt{class\\ tf.SparseTensorValue}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-sparse-to-dense-conversion}{Sparse to\r\n  Dense Conversion}\r\n\\item\r\n  \\protect\\hyperlink{sparseux5ftoux5fdense}{\\texttt{tf.sparse\\_to\\_dense(sparse\\_indices,\\ output\\_shape,\\ sparse\\_values,\\ default\\_value,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{sparseux5ftensorux5ftoux5fdense}{\\texttt{tf.sparse\\_tensor\\_to\\_dense(sp\\_input,\\ default\\_value,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{sparseux5ftoux5findicator}{\\texttt{tf.sparse\\_to\\_indicator(sp\\_input,\\ vocab\\_size,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-manipulation}{Manipulation}\r\n\\item\r\n  \\protect\\hyperlink{sparseux5fconcat}{\\texttt{tf.sparse\\_concat(concat\\_dim,\\ sp\\_inputs,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{sparseux5freorder}{\\texttt{tf.sparse\\_reorder(sp\\_input,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{sparseux5fretain}{\\texttt{tf.sparse\\_retain(sp\\_input,\\ to\\_retain)}}\r\n\\item\r\n  \\protect\\hyperlink{sparseux5ffillux5femptyux5frows}{\\texttt{tf.sparse\\_fill\\_empty\\_rows(sp\\_input,\\ default\\_value,\\ name=None)}}\r\n\\end{itemize}\r\n\r\n\\subsection{Sparse Tensor Representation\r\n}\\label{sparse-tensor-representation}\r\n\r\nTensorflow supports a \\texttt{SparseTensor} representation for data that\r\nis sparse in multiple dimensions. Contrast this representation with\r\n\\texttt{IndexedSlices}, which is efficient for representing tensors that\r\nare sparse in their first dimension, and dense along all other\r\ndimensions.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.SparseTensor}\r\n}{class tf.SparseTensor }}\\label{class-tf.sparsetensor}\r\n\r\nRepresents a sparse tensor.\r\n\r\nTensorflow represents a sparse tensor as three separate dense tensors:\r\n\\texttt{indices}, \\texttt{values}, and \\texttt{dense\\_shape}. In Python,\r\nthe three tensors are collected into a \\texttt{SparseTensor} class for\r\nease of use. If you have separate \\texttt{indices}, \\texttt{values}, and\r\n\\texttt{dense\\_shape} tensors, wrap them in a \\texttt{SparseTensor}\r\nobject before passing to the Ops below.\r\n\r\nConcretely, the sparse tensor\r\n\\texttt{SparseTensor(values,\\ indices,\\ dense\\_shape)} is\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{indices}: A 2-D int64 tensor of shape\r\n  \\texttt{{[}N,\\ ndims{]}}.\r\n\\item\r\n  \\texttt{values}: A 1-D tensor of any type and shape \\texttt{{[}N{]}}.\r\n\\item\r\n  \\texttt{dense\\_shape}: A 1-D int64 tensor of shape\r\n  \\texttt{{[}ndims{]}}.\r\n\\end{itemize}\r\n\r\nwhere \\texttt{N} and \\texttt{ndims} are the number of values, and number\r\nof dimensions in the \\texttt{SparseTensor} respectively.\r\n\r\nThe corresponding dense tensor satisfies\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{dense.shape }\\OperatorTok{=} \\NormalTok{dense_shape}\r\n\\NormalTok{dense[}\\BuiltInTok{tuple}\\NormalTok{(indices[i])] }\\OperatorTok{=} \\NormalTok{values[i]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nBy convention, \\texttt{indices} should be sorted in row-major order (or\r\nequivalently lexigraphic order on the tuples \\texttt{indices{[}i{]}}).\r\nThis is not enforced when \\texttt{SparseTensor} objects are constructed,\r\nbut most Ops assume correct ordering. If the ordering is wrong, it can\r\nbe fixed by calling \\texttt{sparse\\_reorder} on the misordered\r\n\\texttt{SparseTensor}.\r\n\r\nExample: The sparse tensor\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n  \\NormalTok{SparseTensor(values}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{2}\\NormalTok{], indices}\\OperatorTok{=}\\NormalTok{[[}\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{], [}\\DecValTok{1}\\NormalTok{, }\\DecValTok{2}\\NormalTok{]], shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{3}\\NormalTok{, }\\DecValTok{4}\\NormalTok{])}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nrepresents the dense tensor\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n  \\NormalTok{[[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{]}\r\n   \\NormalTok{[}\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{2}\\NormalTok{, }\\DecValTok{0}\\NormalTok{]}\r\n   \\NormalTok{[}\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{]]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.SparseTensor.\\_\\_init\\_\\_(indices,\\ values,\\ shape)}\r\n}{tf.SparseTensor.\\_\\_init\\_\\_(indices, values, shape) }}\\label{tf.sparsetensor.ux5fux5finitux5fux5findices-values-shape}\r\n\r\nCreates a \\texttt{SparseTensor}.\r\n\r\n\\subparagraph{Args: }\\label{args}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{indices}: A 2-D int64 tensor of shape\r\n  \\texttt{{[}N,\\ ndims{]}}.\r\n\\item\r\n  \\texttt{values}: A 1-D tensor of any type and shape \\texttt{{[}N{]}}.\r\n\\item\r\n  \\texttt{dense\\_shape}: A 1-D int64 tensor of shape\r\n  \\texttt{{[}ndims{]}}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns}\r\n\r\nA \\texttt{SparseTensor}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.SparseTensor.indices}\r\n}{tf.SparseTensor.indices }}\\label{tf.sparsetensor.indices}\r\n\r\nThe indices of non-zero values in the represented dense tensor.\r\n\r\n\\subparagraph{Returns: }\\label{returns-1}\r\n\r\nA 2-D Tensor of int64 with shape \\texttt{{[}N,\\ ndims{]}}, where\r\n\\texttt{N} is the number of non-zero values in the tensor, and\r\n\\texttt{ndims} is the rank.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.SparseTensor.values}\r\n}{tf.SparseTensor.values }}\\label{tf.sparsetensor.values}\r\n\r\nThe non-zero values in the represented dense tensor.\r\n\r\n\\subparagraph{Returns: }\\label{returns-2}\r\n\r\nA 1-D Tensor of any data type.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.SparseTensor.dtype}\r\n}{tf.SparseTensor.dtype }}\\label{tf.sparsetensor.dtype}\r\n\r\nThe \\texttt{DType} of elements in this tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.SparseTensor.shape}\r\n}{tf.SparseTensor.shape }}\\label{tf.sparsetensor.shape}\r\n\r\nA 1-D Tensor of int64 representing the shape of the dense tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.SparseTensor.graph}\r\n}{tf.SparseTensor.graph }}\\label{tf.sparsetensor.graph}\r\n\r\nThe \\texttt{Graph} that contains the index, value, and shape tensors.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.SparseTensorValue}\r\n}{class tf.SparseTensorValue }}\\label{class-tf.sparsetensorvalue}\r\n\r\nSparseTensorValue(indices, values, shape) - - -\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.SparseTensorValue.indices}\r\n}{tf.SparseTensorValue.indices }}\\label{tf.sparsetensorvalue.indices}\r\n\r\nAlias for field number 0\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.SparseTensorValue.shape}\r\n}{tf.SparseTensorValue.shape }}\\label{tf.sparsetensorvalue.shape}\r\n\r\nAlias for field number 2\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.SparseTensorValue.values}\r\n}{tf.SparseTensorValue.values }}\\label{tf.sparsetensorvalue.values}\r\n\r\nAlias for field number 1\r\n\r\n\\subsection{Sparse to Dense Conversion\r\n}\\label{sparse-to-dense-conversion}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.sparse\\_to\\_dense(sparse\\_indices,\\ output\\_shape,\\ sparse\\_values,\\ default\\_value,\\ name=None)}\r\n}{tf.sparse\\_to\\_dense(sparse\\_indices, output\\_shape, sparse\\_values, default\\_value, name=None) }}\\label{tf.sparseux5ftoux5fdensesparseux5findices-outputux5fshape-sparseux5fvalues-defaultux5fvalue-namenone}\r\n\r\nConverts a sparse representation into a dense tensor.\r\n\r\nBuilds an array \\texttt{dense} with shape \\texttt{output\\_shape} such\r\nthat\r\n\r\n\\begin{verbatim}\r\n# If sparse_indices is scalar\r\ndense[i] = (i == sparse_indices ? sparse_values : default_value)\r\n\r\n# If sparse_indices is a vector, then for each i\r\ndense[sparse_indices[i]] = sparse_values[i]\r\n\r\n# If sparse_indices is an n by d matrix, then for each i in [0, n)\r\ndense[sparse_indices[i][0], ..., sparse_indices[i][d-1]] = sparse_values[i]\r\n\\end{verbatim}\r\n\r\nAll other values in \\texttt{dense} are set to \\texttt{default\\_value}.\r\nIf \\texttt{sparse\\_values} is a scalar, all sparse indices are set to\r\nthis single value.\r\n\r\n\\subparagraph{Args: }\\label{args-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{sparse\\_indices}: A \\texttt{Tensor}. Must be one of the\r\n  following types: \\texttt{int32}, \\texttt{int64}. 0-D, 1-D, or 2-D.\r\n  \\texttt{sparse\\_indices{[}i{]}} contains the complete index where\r\n  \\texttt{sparse\\_values{[}i{]}} will be placed.\r\n\\item\r\n  \\texttt{output\\_shape}: A \\texttt{Tensor}. Must have the same type as\r\n  \\texttt{sparse\\_indices}. 1-D. Shape of the dense output tensor.\r\n\\item\r\n  \\texttt{sparse\\_values}: A \\texttt{Tensor}. 1-D. Values corresponding\r\n  to each row of \\texttt{sparse\\_indices}, or a scalar value to be used\r\n  for all sparse indices.\r\n\\item\r\n  \\texttt{default\\_value}: A \\texttt{Tensor}. Must have the same type as\r\n  \\texttt{sparse\\_values}. Scalar value to set for indices not specified\r\n  in \\texttt{sparse\\_indices}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-3}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{sparse\\_values}. Dense\r\noutput tensor of shape \\texttt{output\\_shape}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.sparse\\_tensor\\_to\\_dense(sp\\_input,\\ default\\_value,\\ name=None)}\r\n}{tf.sparse\\_tensor\\_to\\_dense(sp\\_input, default\\_value, name=None) }}\\label{tf.sparseux5ftensorux5ftoux5fdensespux5finput-defaultux5fvalue-namenone}\r\n\r\nConverts a \\texttt{SparseTensor} into a dense tensor.\r\n\r\nThis op is a convenience wrapper around \\texttt{sparse\\_to\\_dense} for\r\n\\texttt{SparseTensor}s.\r\n\r\nFor example, if \\texttt{sp\\_input} has shape \\texttt{{[}3,\\ 5{]}} and\r\nnon-empty string values:\r\n\r\n\\begin{verbatim}\r\n[0, 1]: a\r\n[0, 3]: b\r\n[2, 0]: c\r\n\\end{verbatim}\r\n\r\nand \\texttt{default\\_value} is \\texttt{x}, then the output will be a\r\ndense \\texttt{{[}3,\\ 5{]}} string tensor with values:\r\n\r\n\\begin{verbatim}\r\n[[x a x b x]\r\n [x x x x x]\r\n [c x x x x]]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{sp\\_input}: The input \\texttt{SparseTensor}.\r\n\\item\r\n  \\texttt{default\\_value}: Scalar value to set for indices not specified\r\n  in \\texttt{sp\\_input}.\r\n\\item\r\n  \\texttt{name}: A name prefix for the returned tensors (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-4}\r\n\r\nA dense tensor with shape \\texttt{sp\\_input.shape} and values specified\r\nby the non-empty values in \\texttt{sp\\_input}. Indices not in\r\n\\texttt{sp\\_input} are assigned \\texttt{default\\_value}.\r\n\r\n\\subparagraph{Raises: }\\label{raises}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{TypeError}: If \\texttt{sp\\_input} is not a\r\n  \\texttt{SparseTensor}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.sparse\\_to\\_indicator(sp\\_input,\\ vocab\\_size,\\ name=None)}\r\n}{tf.sparse\\_to\\_indicator(sp\\_input, vocab\\_size, name=None) }}\\label{tf.sparseux5ftoux5findicatorspux5finput-vocabux5fsize-namenone}\r\n\r\nConverts a \\texttt{SparseTensor} of ids into a dense bool indicator\r\ntensor.\r\n\r\nThe last dimension of \\texttt{sp\\_input} is discarded and replaced with\r\nthe values of \\texttt{sp\\_input}. If\r\n\\texttt{sp\\_input.shape\\ =\\ {[}D0,\\ D1,\\ ...,\\ Dn,\\ K{]}}, then\r\n\\texttt{output.shape\\ =\\ {[}D0,\\ D1,\\ ...,\\ Dn,\\ vocab\\_size{]}}, where\r\n\r\n\\begin{verbatim}\r\noutput[d_0, d_1, ..., d_n, sp_input[d_0, d_1, ..., d_n, k]] = True\r\n\\end{verbatim}\r\n\r\nand False elsewhere in \\texttt{output}.\r\n\r\nFor example, if \\texttt{sp\\_input.shape\\ =\\ {[}2,\\ 3,\\ 4{]}} with\r\nnon-empty values:\r\n\r\n\\begin{verbatim}\r\n[0, 0, 0]: 0\r\n[0, 1, 0]: 10\r\n[1, 0, 3]: 103\r\n[1, 1, 2]: 112\r\n[1, 1, 3]: 113\r\n[1, 2, 1]: 121\r\n\\end{verbatim}\r\n\r\nand \\texttt{vocab\\_size\\ =\\ 200}, then the output will be a\r\n\\texttt{{[}2,\\ 3,\\ 200{]}} dense bool tensor with False everywhere\r\nexcept at positions\r\n\r\n\\begin{verbatim}\r\n(0, 0, 0), (0, 1, 10), (1, 0, 103), (1, 1, 112), (1, 1, 113), (1, 2, 121).\r\n\\end{verbatim}\r\n\r\nThis op is useful for converting \\texttt{SparseTensor}s into dense\r\nformats for compatibility with ops that expect dense tensors.\r\n\r\nThe input \\texttt{SparseTensor} must be in row-major order.\r\n\r\n\\subparagraph{Args: }\\label{args-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{sp\\_input}: A \\texttt{SparseTensor} of type \\texttt{int32} or\r\n  \\texttt{int64}.\r\n\\item\r\n  \\texttt{vocab\\_size}: The new size of the last dimension, with\r\n  \\texttt{all(0\\ \\textless{}=\\ sp\\_input.values\\ \\textless{}\\ vocab\\_size)}.\r\n\\item\r\n  \\texttt{name}: A name prefix for the returned tensors (optional)\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-5}\r\n\r\nA dense bool indicator tensor representing the indices with specified\r\nvalue.\r\n\r\n\\subparagraph{Raises: }\\label{raises-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{TypeError}: If \\texttt{sp\\_input} is not a\r\n  \\texttt{SparseTensor}.\r\n\\end{itemize}\r\n\r\n\\subsection{Manipulation }\\label{manipulation}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.sparse\\_concat(concat\\_dim,\\ sp\\_inputs,\\ name=None)}\r\n}{tf.sparse\\_concat(concat\\_dim, sp\\_inputs, name=None) }}\\label{tf.sparseux5fconcatconcatux5fdim-spux5finputs-namenone}\r\n\r\nConcatenates a list of \\texttt{SparseTensor} along the specified\r\ndimension.\r\n\r\nConcatenation is with respect to the dense versions of each sparse\r\ninput. It is assumed that each inputs is a \\texttt{SparseTensor} whose\r\nelements are ordered along increasing dimension number.\r\n\r\nAll inputs' shapes must match, except for the concat dimension. The\r\n\\texttt{indices}, \\texttt{values}, and \\texttt{shapes} lists must have\r\nthe same length.\r\n\r\nThe output shape is identical to the inputs', except along the concat\r\ndimension, where it is the sum of the inputs' sizes along that\r\ndimension.\r\n\r\nThe output elements will be resorted to preserve the sort order along\r\nincreasing dimension number.\r\n\r\nThis op runs in \\texttt{O(M\\ log\\ M)} time, where \\texttt{M} is the\r\ntotal number of non-empty values across all inputs. This is due to the\r\nneed for an internal sort in order to concatenate efficiently across an\r\narbitrary dimension.\r\n\r\nFor example, if \\texttt{concat\\_dim\\ =\\ 1} and the inputs are\r\n\r\n\\begin{verbatim}\r\nsp_inputs[0]: shape = [2, 3]\r\n[0, 2]: \"a\"\r\n[1, 0]: \"b\"\r\n[1, 1]: \"c\"\r\n\r\nsp_inputs[1]: shape = [2, 4]\r\n[0, 1]: \"d\"\r\n[0, 2]: \"e\"\r\n\\end{verbatim}\r\n\r\nthen the output will be\r\n\r\n\\begin{verbatim}\r\nshape = [2, 7]\r\n[0, 2]: \"a\"\r\n[0, 4]: \"d\"\r\n[0, 5]: \"e\"\r\n[1, 0]: \"b\"\r\n[1, 1]: \"c\"\r\n\\end{verbatim}\r\n\r\nGraphically this is equivalent to doing\r\n\r\n\\begin{verbatim}\r\n[    a] concat [  d e  ] = [    a   d e  ]\r\n[b c  ]        [       ]   [b c          ]\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{concat\\_dim}: Dimension to concatenate along.\r\n\\item\r\n  \\texttt{sp\\_inputs}: List of \\texttt{SparseTensor} to concatenate.\r\n\\item\r\n  \\texttt{name}: A name prefix for the returned tensors (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-6}\r\n\r\nA \\texttt{SparseTensor} with the concatenated output.\r\n\r\n\\subparagraph{Raises: }\\label{raises-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{TypeError}: If \\texttt{sp\\_inputs} is not a list of\r\n  \\texttt{SparseTensor}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.sparse\\_reorder(sp\\_input,\\ name=None)}\r\n}{tf.sparse\\_reorder(sp\\_input, name=None) }}\\label{tf.sparseux5freorderspux5finput-namenone}\r\n\r\nReorders a \\texttt{SparseTensor} into the canonical, row-major ordering.\r\n\r\nNote that by convention, all sparse ops preserve the canonical ordering\r\nalong increasing dimension number. The only time ordering can be\r\nviolated is during manual manipulation of the indices and values to add\r\nentries.\r\n\r\nReordering does not affect the shape of the \\texttt{SparseTensor}.\r\n\r\nFor example, if sp\\_input has shape \\texttt{{[}4,\\ 5{]}} and\r\n\\texttt{indices} / \\texttt{values}:\r\n\r\n\\begin{verbatim}\r\n[0, 3]: b\r\n[0, 1]: a\r\n[3, 1]: d\r\n[2, 0]: c\r\n\\end{verbatim}\r\n\r\nthen the output will be a \\texttt{SparseTensor} of shape\r\n\\texttt{{[}4,\\ 5{]}} and \\texttt{indices} / \\texttt{values}:\r\n\r\n\\begin{verbatim}\r\n[0, 1]: a\r\n[0, 3]: b\r\n[2, 0]: c\r\n[3, 1]: d\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{sp\\_input}: The input \\texttt{SparseTensor}.\r\n\\item\r\n  \\texttt{name}: A name prefix for the returned tensors (optional)\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-7}\r\n\r\nA \\texttt{SparseTensor} with the same shape and non-empty values, but in\r\ncanonical ordering.\r\n\r\n\\subparagraph{Raises: }\\label{raises-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{TypeError}: If \\texttt{sp\\_input} is not a\r\n  \\texttt{SparseTensor}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.sparse\\_retain(sp\\_input,\\ to\\_retain)}\r\n}{tf.sparse\\_retain(sp\\_input, to\\_retain) }}\\label{tf.sparseux5fretainspux5finput-toux5fretain}\r\n\r\nRetains specified non-empty values within a \\texttt{SparseTensor}.\r\n\r\nFor example, if \\texttt{sp\\_input} has shape \\texttt{{[}4,\\ 5{]}} and 4\r\nnon-empty string values:\r\n\r\n\\begin{verbatim}\r\n[0, 1]: a\r\n[0, 3]: b\r\n[2, 0]: c\r\n[3, 1]: d\r\n\\end{verbatim}\r\n\r\nand \\texttt{to\\_retain\\ =\\ {[}True,\\ False,\\ False,\\ True{]}}, then the\r\noutput will be a \\texttt{SparseTensor} of shape \\texttt{{[}4,\\ 5{]}}\r\nwith 2 non-empty values:\r\n\r\n\\begin{verbatim}\r\n[0, 1]: a\r\n[3, 1]: d\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{sp\\_input}: The input \\texttt{SparseTensor} with \\texttt{N}\r\n  non-empty elements.\r\n\\item\r\n  \\texttt{to\\_retain}: A bool vector of length \\texttt{N} with\r\n  \\texttt{M} true values.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-8}\r\n\r\nA \\texttt{SparseTensor} with the same shape as the input and \\texttt{M}\r\nnon-empty elements corresponding to the true positions in\r\n\\texttt{to\\_retain}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{TypeError}: If \\texttt{sp\\_input} is not a\r\n  \\texttt{SparseTensor}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.sparse\\_fill\\_empty\\_rows(sp\\_input,\\ default\\_value,\\ name=None)}\r\n}{tf.sparse\\_fill\\_empty\\_rows(sp\\_input, default\\_value, name=None) }}\\label{tf.sparseux5ffillux5femptyux5frowsspux5finput-defaultux5fvalue-namenone}\r\n\r\nFills empty rows in the input 2-D \\texttt{SparseTensor} with a default\r\nvalue.\r\n\r\nThis op adds entries with the specified \\texttt{default\\_value} at index\r\n\\texttt{{[}row,\\ 0{]}} for any row in the input that does not already\r\nhave a value.\r\n\r\nFor example, suppose \\texttt{sp\\_input} has shape \\texttt{{[}5,\\ 6{]}}\r\nand non-empty values:\r\n\r\n\\begin{verbatim}\r\n[0, 1]: a\r\n[0, 3]: b\r\n[2, 0]: c\r\n[3, 1]: d\r\n\\end{verbatim}\r\n\r\nRows 1 and 4 are empty, so the output will be of shape\r\n\\texttt{{[}5,\\ 6{]}} with values:\r\n\r\n\\begin{verbatim}\r\n[0, 1]: a\r\n[0, 3]: b\r\n[1, 0]: default_value\r\n[2, 0]: c\r\n[3, 1]: d\r\n[4, 0]: default_value\r\n\\end{verbatim}\r\n\r\nNote that the input may have empty columns at the end, with no effect on\r\nthis op.\r\n\r\nThe output \\texttt{SparseTensor} will be in row-major order and will\r\nhave the same shape as the input.\r\n\r\nThis op also returns an indicator vector such that\r\n\r\n\\begin{verbatim}\r\nempty_row_indicator[i] = True iff row i was an empty row.\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-7}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{sp\\_input}: A \\texttt{SparseTensor} with shape\r\n  \\texttt{{[}N,\\ M{]}}.\r\n\\item\r\n  \\texttt{default\\_value}: The value to fill for empty rows, with the\r\n  same type as \\texttt{sp\\_input.}\r\n\\item\r\n  \\texttt{name}: A name prefix for the returned tensors (optional)\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-9}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{sp\\_ordered\\_output}: A \\texttt{SparseTensor} with shape\r\n  \\texttt{{[}N,\\ M{]}}, and with all empty rows filled in with\r\n  \\texttt{default\\_value}.\r\n\\item\r\n  \\texttt{empty\\_row\\_indicator}: A bool vector of length \\texttt{N}\r\n  indicating whether each input row was empty.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{TypeError}: If \\texttt{sp\\_input} is not a\r\n  \\texttt{SparseTensor}.\r\n\\end{itemize}\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/python/c4s09_io_ops.tex",
    "content": "\r\n\r\n\\section{Inputs and Readers }\\label{inputs-and-readers}\r\n\r\nNote: Functions taking \\texttt{Tensor} arguments can also take anything\r\naccepted by\r\n\\href{../../api_docs/python/framework.md\\#convert_to_tensor}{\\texttt{tf.convert\\_to\\_tensor}}.\r\n\r\n\\subsection{Contents}\\label{contents}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-inputs-and-readers}{Inputs\r\nand Readers}}{Inputs and Readers}}\\label{inputs-and-readers-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-placeholders}{Placeholders}\r\n\\item\r\n  \\protect\\hyperlink{placeholder}{\\texttt{tf.placeholder(dtype,\\ shape=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-readers}{Readers}\r\n\\item\r\n  \\protect\\hyperlink{ReaderBase}{\\texttt{class\\ tf.ReaderBase}}\r\n\\item\r\n  \\protect\\hyperlink{TextLineReader}{\\texttt{class\\ tf.TextLineReader}}\r\n\\item\r\n  \\protect\\hyperlink{WholeFileReader}{\\texttt{class\\ tf.WholeFileReader}}\r\n\\item\r\n  \\protect\\hyperlink{IdentityReader}{\\texttt{class\\ tf.IdentityReader}}\r\n\\item\r\n  \\protect\\hyperlink{TFRecordReader}{\\texttt{class\\ tf.TFRecordReader}}\r\n\\item\r\n  \\protect\\hyperlink{FixedLengthRecordReader}{\\texttt{class\\ tf.FixedLengthRecordReader}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-converting}{Converting}\r\n\\item\r\n  \\protect\\hyperlink{decodeux5fcsv}{\\texttt{tf.decode\\_csv(records,\\ record\\_defaults,\\ field\\_delim=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{decodeux5fraw}{\\texttt{tf.decode\\_raw(bytes,\\ out\\_type,\\ little\\_endian=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-example-protocol-buffer}{Example\r\n  protocol buffer}\r\n\\item\r\n  \\protect\\hyperlink{parseux5fexample}{\\texttt{tf.parse\\_example(serialized,\\ names=None,\\ sparse\\_keys=None,\\ sparse\\_types=None,\\ dense\\_keys=None,\\ dense\\_types=None,\\ dense\\_defaults=None,\\ dense\\_shapes=None,\\ name=\\textquotesingle{}ParseExample\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{parseux5fsingleux5fexample}{\\texttt{tf.parse\\_single\\_example(serialized,\\ names=None,\\ sparse\\_keys=None,\\ sparse\\_types=None,\\ dense\\_keys=None,\\ dense\\_types=None,\\ dense\\_defaults=None,\\ dense\\_shapes=None,\\ name=\\textquotesingle{}ParseSingleExample\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-queues}{Queues}\r\n\\item\r\n  \\protect\\hyperlink{QueueBase}{\\texttt{class\\ tf.QueueBase}}\r\n\\item\r\n  \\protect\\hyperlink{FIFOQueue}{\\texttt{class\\ tf.FIFOQueue}}\r\n\\item\r\n  \\protect\\hyperlink{RandomShuffleQueue}{\\texttt{class\\ tf.RandomShuffleQueue}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-dealing-with-the-filesystem}{Dealing\r\n  with the filesystem}\r\n\\item\r\n  \\protect\\hyperlink{matchingux5ffiles}{\\texttt{tf.matching\\_files(pattern,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{readux5ffile}{\\texttt{tf.read\\_file(filename,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-input-pipeline}{Input pipeline}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-beginning-of-an-input-pipeline}{Beginning\r\n  of an input pipeline}\r\n\\item\r\n  \\protect\\hyperlink{matchux5ffilenamesux5fonce}{\\texttt{tf.train.match\\_filenames\\_once(pattern,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{limitux5fepochs}{\\texttt{tf.train.limit\\_epochs(tensor,\\ num\\_epochs=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{rangeux5finputux5fproducer}{\\texttt{tf.train.range\\_input\\_producer(limit,\\ num\\_epochs=None,\\ shuffle=True,\\ seed=None,\\ capacity=32,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{sliceux5finputux5fproducer}{\\texttt{tf.train.slice\\_input\\_producer(tensor\\_list,\\ num\\_epochs=None,\\ shuffle=True,\\ seed=None,\\ capacity=32,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{stringux5finputux5fproducer}{\\texttt{tf.train.string\\_input\\_producer(string\\_tensor,\\ num\\_epochs=None,\\ shuffle=True,\\ seed=None,\\ capacity=32,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-batching-at-the-end-of-an-input-pipeline}{Batching\r\n  at the end of an input pipeline}\r\n\\item\r\n  \\protect\\hyperlink{batch}{\\texttt{tf.train.batch(tensor\\_list,\\ batch\\_size,\\ num\\_threads=1,\\ capacity=32,\\ enqueue\\_many=False,\\ shapes=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{batchux5fjoin}{\\texttt{tf.train.batch\\_join(tensor\\_list\\_list,\\ batch\\_size,\\ capacity=32,\\ enqueue\\_many=False,\\ shapes=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{shuffleux5fbatch}{\\texttt{tf.train.shuffle\\_batch(tensor\\_list,\\ batch\\_size,\\ capacity,\\ min\\_after\\_dequeue,\\ num\\_threads=1,\\ seed=None,\\ enqueue\\_many=False,\\ shapes=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{shuffleux5fbatchux5fjoin}{\\texttt{tf.train.shuffle\\_batch\\_join(tensor\\_list\\_list,\\ batch\\_size,\\ capacity,\\ min\\_after\\_dequeue,\\ seed=None,\\ enqueue\\_many=False,\\ shapes=None,\\ name=None)}}\r\n\\end{itemize}\r\n\r\n\\subsection{Placeholders }\\label{placeholders}\r\n\r\nTensorFlow provides a placeholder operation that must be fed with data\r\non execution. For more info, see the section on\r\n\\href{../../how_tos/reading_data/index.md\\#feeding}{Feeding data}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.placeholder(dtype,\\ shape=None,\\ name=None)}\r\n}{tf.placeholder(dtype, shape=None, name=None) }}\\label{tf.placeholderdtype-shapenone-namenone}\r\n\r\nInserts a placeholder for a tensor that will be always fed.\r\n\r\n\\textbf{Important}: This tensor will produce an error if evaluated. Its\r\nvalue must be fed using the \\texttt{feed\\_dict} optional argument to\r\n\\texttt{Session.run()}, \\texttt{Tensor.eval()}, or\r\n\\texttt{Operation.run()}.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{x }\\OperatorTok{=} \\NormalTok{tf.placeholder(}\\BuiltInTok{float}\\NormalTok{, shape}\\OperatorTok{=}\\NormalTok{(}\\DecValTok{1024}\\NormalTok{, }\\DecValTok{1024}\\NormalTok{))}\r\n\\NormalTok{y }\\OperatorTok{=} \\NormalTok{tf.matmul(x, x)}\r\n\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session() }\\ImportTok{as} \\NormalTok{sess:}\r\n  \\BuiltInTok{print} \\NormalTok{sess.run(y)  }\\CommentTok{# ERROR: will fail because x was not fed.}\r\n\r\n  \\NormalTok{rand_array }\\OperatorTok{=} \\NormalTok{np.random.rand(}\\DecValTok{1024}\\NormalTok{, }\\DecValTok{1024}\\NormalTok{)}\r\n  \\BuiltInTok{print} \\NormalTok{sess.run(y, feed_dict}\\OperatorTok{=}\\NormalTok{\\{x: rand_array\\})  }\\CommentTok{# Will succeed.}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{dtype}: The type of elements in the tensor to be fed.\r\n\\item\r\n  \\texttt{shape}: The shape of the tensor to be fed (optional). If the\r\n  shape is not specified, you can feed a tensor of any shape.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns}\r\n\r\nA \\texttt{Tensor} that may be used as a handle for feeding a value, but\r\nnot evaluated directly.\r\n\r\n\\subsection{Readers }\\label{readers}\r\n\r\nTensorFlow provides a set of Reader classes for reading data formats.\r\nFor more information on inputs and readers, see\r\n\\href{../../how_tos/reading_data/index.md}{Reading data}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.ReaderBase}\r\n}{class tf.ReaderBase }}\\label{class-tf.readerbase}\r\n\r\nBase class for different Reader types, that produce a record every step.\r\n\r\nConceptually, Readers convert string `work units' into records (key,\r\nvalue pairs). Typically the `work units' are filenames and the records\r\nare extracted from the contents of those files. We want a single record\r\nproduced per step, but a work unit can correspond to many records.\r\n\r\nTherefore we introduce some decoupling using a queue. The queue contains\r\nthe work units and the Reader dequeues from the queue when it is asked\r\nto produce a record (via Read()) but it has finished the last work unit.\r\n- - -\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.ReaderBase.\\_\\_init\\_\\_(reader\\_ref,\\ supports\\_serialize=False)}\r\n}{tf.ReaderBase.\\_\\_init\\_\\_(reader\\_ref, supports\\_serialize=False) }}\\label{tf.readerbase.ux5fux5finitux5fux5freaderux5fref-supportsux5fserializefalse}\r\n\r\nCreates a new ReaderBase.\r\n\r\n\\subparagraph{Args: }\\label{args-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{reader\\_ref}: The operation that implements the reader.\r\n\\item\r\n  \\texttt{supports\\_serialize}: True if the reader implementation can\r\n  serialize its state.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.ReaderBase.num\\_records\\_produced(name=None)}\r\n}{tf.ReaderBase.num\\_records\\_produced(name=None) }}\\label{tf.readerbase.numux5frecordsux5fproducednamenone}\r\n\r\nReturns the number of records this reader has produced.\r\n\r\nThis is the same as the number of Read executions that have succeeded.\r\n\r\n\\subparagraph{Args: }\\label{args-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-1}\r\n\r\nAn int64 Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.ReaderBase.num\\_work\\_units\\_completed(name=None)}\r\n}{tf.ReaderBase.num\\_work\\_units\\_completed(name=None) }}\\label{tf.readerbase.numux5fworkux5funitsux5fcompletednamenone}\r\n\r\nReturns the number of work units this reader has finished processing.\r\n\r\n\\subparagraph{Args: }\\label{args-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-2}\r\n\r\nAn int64 Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.ReaderBase.read(queue,\\ name=None)}\r\n}{tf.ReaderBase.read(queue, name=None) }}\\label{tf.readerbase.readqueue-namenone}\r\n\r\nReturns the next record (key, value pair) produced by a reader.\r\n\r\nWill dequeue a work unit from queue if necessary (e.g.~when the Reader\r\nneeds to start reading from a new file since it has finished with the\r\nprevious file).\r\n\r\n\\subparagraph{Args: }\\label{args-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{queue}: A Queue or a mutable string Tensor representing a\r\n  handle to a Queue, with string work items.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-3}\r\n\r\nA tuple of Tensors (key, value).\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{key}: A string scalar Tensor.\r\n\\item\r\n  \\texttt{value}: A string scalar Tensor.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.ReaderBase.reader\\_ref}\r\n}{tf.ReaderBase.reader\\_ref }}\\label{tf.readerbase.readerux5fref}\r\n\r\nOp that implements the reader.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.ReaderBase.reset(name=None)}\r\n}{tf.ReaderBase.reset(name=None) }}\\label{tf.readerbase.resetnamenone}\r\n\r\nRestore a reader to its initial clean state.\r\n\r\n\\subparagraph{Args: }\\label{args-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-4}\r\n\r\nThe created Operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.ReaderBase.restore\\_state(state,\\ name=None)}\r\n}{tf.ReaderBase.restore\\_state(state, name=None) }}\\label{tf.readerbase.restoreux5fstatestate-namenone}\r\n\r\nRestore a reader to a previously saved state.\r\n\r\nNot all Readers support being restored, so this can produce an\r\nUnimplemented error.\r\n\r\n\\subparagraph{Args: }\\label{args-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{state}: A string Tensor. Result of a SerializeState of a\r\n  Reader with matching type.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-5}\r\n\r\nThe created Operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.ReaderBase.serialize\\_state(name=None)}\r\n}{tf.ReaderBase.serialize\\_state(name=None) }}\\label{tf.readerbase.serializeux5fstatenamenone}\r\n\r\nProduce a string tensor that encodes the state of a reader.\r\n\r\nNot all Readers support being serialized, so this can produce an\r\nUnimplemented error.\r\n\r\n\\subparagraph{Args: }\\label{args-7}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-6}\r\n\r\nA string Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.ReaderBase.supports\\_serialize}\r\n}{tf.ReaderBase.supports\\_serialize }}\\label{tf.readerbase.supportsux5fserialize}\r\n\r\nWhether the Reader implementation can serialize its state.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.TextLineReader}\r\n}{class tf.TextLineReader }}\\label{class-tf.textlinereader}\r\n\r\nA Reader that outputs the lines of a file delimited by newlines.\r\n\r\nNewlines are stripped from the output. See ReaderBase for supported\r\nmethods. - - -\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TextLineReader.\\_\\_init\\_\\_(skip\\_header\\_lines=None,\\ name=None)}\r\n}{tf.TextLineReader.\\_\\_init\\_\\_(skip\\_header\\_lines=None, name=None) }}\\label{tf.textlinereader.ux5fux5finitux5fux5fskipux5fheaderux5flinesnone-namenone}\r\n\r\nCreate a TextLineReader.\r\n\r\n\\subparagraph{Args: }\\label{args-8}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{skip\\_header\\_lines}: An optional int. Defaults to 0. Number\r\n  of lines to skip from the beginning of every file.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TextLineReader.num\\_records\\_produced(name=None)}\r\n}{tf.TextLineReader.num\\_records\\_produced(name=None) }}\\label{tf.textlinereader.numux5frecordsux5fproducednamenone}\r\n\r\nReturns the number of records this reader has produced.\r\n\r\nThis is the same as the number of Read executions that have succeeded.\r\n\r\n\\subparagraph{Args: }\\label{args-9}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-7}\r\n\r\nAn int64 Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TextLineReader.num\\_work\\_units\\_completed(name=None)}\r\n}{tf.TextLineReader.num\\_work\\_units\\_completed(name=None) }}\\label{tf.textlinereader.numux5fworkux5funitsux5fcompletednamenone}\r\n\r\nReturns the number of work units this reader has finished processing.\r\n\r\n\\subparagraph{Args: }\\label{args-10}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-8}\r\n\r\nAn int64 Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TextLineReader.read(queue,\\ name=None)}\r\n}{tf.TextLineReader.read(queue, name=None) }}\\label{tf.textlinereader.readqueue-namenone}\r\n\r\nReturns the next record (key, value pair) produced by a reader.\r\n\r\nWill dequeue a work unit from queue if necessary (e.g.~when the Reader\r\nneeds to start reading from a new file since it has finished with the\r\nprevious file).\r\n\r\n\\subparagraph{Args: }\\label{args-11}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{queue}: A Queue or a mutable string Tensor representing a\r\n  handle to a Queue, with string work items.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-9}\r\n\r\nA tuple of Tensors (key, value).\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{key}: A string scalar Tensor.\r\n\\item\r\n  \\texttt{value}: A string scalar Tensor.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TextLineReader.reader\\_ref}\r\n}{tf.TextLineReader.reader\\_ref }}\\label{tf.textlinereader.readerux5fref}\r\n\r\nOp that implements the reader.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TextLineReader.reset(name=None)}\r\n}{tf.TextLineReader.reset(name=None) }}\\label{tf.textlinereader.resetnamenone}\r\n\r\nRestore a reader to its initial clean state.\r\n\r\n\\subparagraph{Args: }\\label{args-12}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-10}\r\n\r\nThe created Operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TextLineReader.restore\\_state(state,\\ name=None)}\r\n}{tf.TextLineReader.restore\\_state(state, name=None) }}\\label{tf.textlinereader.restoreux5fstatestate-namenone}\r\n\r\nRestore a reader to a previously saved state.\r\n\r\nNot all Readers support being restored, so this can produce an\r\nUnimplemented error.\r\n\r\n\\subparagraph{Args: }\\label{args-13}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{state}: A string Tensor. Result of a SerializeState of a\r\n  Reader with matching type.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-11}\r\n\r\nThe created Operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TextLineReader.serialize\\_state(name=None)}\r\n}{tf.TextLineReader.serialize\\_state(name=None) }}\\label{tf.textlinereader.serializeux5fstatenamenone}\r\n\r\nProduce a string tensor that encodes the state of a reader.\r\n\r\nNot all Readers support being serialized, so this can produce an\r\nUnimplemented error.\r\n\r\n\\subparagraph{Args: }\\label{args-14}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-12}\r\n\r\nA string Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TextLineReader.supports\\_serialize}\r\n}{tf.TextLineReader.supports\\_serialize }}\\label{tf.textlinereader.supportsux5fserialize}\r\n\r\nWhether the Reader implementation can serialize its state.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.WholeFileReader}\r\n}{class tf.WholeFileReader }}\\label{class-tf.wholefilereader}\r\n\r\nA Reader that outputs the entire contents of a file as a value.\r\n\r\nTo use, enqueue filenames in a Queue. The output of Read will be a\r\nfilename (key) and the contents of that file (value).\r\n\r\nSee ReaderBase for supported methods. - - -\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.WholeFileReader.\\_\\_init\\_\\_(name=None)}\r\n}{tf.WholeFileReader.\\_\\_init\\_\\_(name=None) }}\\label{tf.wholefilereader.ux5fux5finitux5fux5fnamenone}\r\n\r\nCreate a WholeFileReader.\r\n\r\n\\subparagraph{Args: }\\label{args-15}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.WholeFileReader.num\\_records\\_produced(name=None)}\r\n}{tf.WholeFileReader.num\\_records\\_produced(name=None) }}\\label{tf.wholefilereader.numux5frecordsux5fproducednamenone}\r\n\r\nReturns the number of records this reader has produced.\r\n\r\nThis is the same as the number of Read executions that have succeeded.\r\n\r\n\\subparagraph{Args: }\\label{args-16}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-13}\r\n\r\nAn int64 Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.WholeFileReader.num\\_work\\_units\\_completed(name=None)}\r\n}{tf.WholeFileReader.num\\_work\\_units\\_completed(name=None) }}\\label{tf.wholefilereader.numux5fworkux5funitsux5fcompletednamenone}\r\n\r\nReturns the number of work units this reader has finished processing.\r\n\r\n\\subparagraph{Args: }\\label{args-17}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-14}\r\n\r\nAn int64 Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.WholeFileReader.read(queue,\\ name=None)}\r\n}{tf.WholeFileReader.read(queue, name=None) }}\\label{tf.wholefilereader.readqueue-namenone}\r\n\r\nReturns the next record (key, value pair) produced by a reader.\r\n\r\nWill dequeue a work unit from queue if necessary (e.g.~when the Reader\r\nneeds to start reading from a new file since it has finished with the\r\nprevious file).\r\n\r\n\\subparagraph{Args: }\\label{args-18}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{queue}: A Queue or a mutable string Tensor representing a\r\n  handle to a Queue, with string work items.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-15}\r\n\r\nA tuple of Tensors (key, value).\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{key}: A string scalar Tensor.\r\n\\item\r\n  \\texttt{value}: A string scalar Tensor.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.WholeFileReader.reader\\_ref}\r\n}{tf.WholeFileReader.reader\\_ref }}\\label{tf.wholefilereader.readerux5fref}\r\n\r\nOp that implements the reader.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.WholeFileReader.reset(name=None)}\r\n}{tf.WholeFileReader.reset(name=None) }}\\label{tf.wholefilereader.resetnamenone}\r\n\r\nRestore a reader to its initial clean state.\r\n\r\n\\subparagraph{Args: }\\label{args-19}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-16}\r\n\r\nThe created Operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.WholeFileReader.restore\\_state(state,\\ name=None)}\r\n}{tf.WholeFileReader.restore\\_state(state, name=None) }}\\label{tf.wholefilereader.restoreux5fstatestate-namenone}\r\n\r\nRestore a reader to a previously saved state.\r\n\r\nNot all Readers support being restored, so this can produce an\r\nUnimplemented error.\r\n\r\n\\subparagraph{Args: }\\label{args-20}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{state}: A string Tensor. Result of a SerializeState of a\r\n  Reader with matching type.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-17}\r\n\r\nThe created Operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.WholeFileReader.serialize\\_state(name=None)}\r\n}{tf.WholeFileReader.serialize\\_state(name=None) }}\\label{tf.wholefilereader.serializeux5fstatenamenone}\r\n\r\nProduce a string tensor that encodes the state of a reader.\r\n\r\nNot all Readers support being serialized, so this can produce an\r\nUnimplemented error.\r\n\r\n\\subparagraph{Args: }\\label{args-21}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-18}\r\n\r\nA string Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.WholeFileReader.supports\\_serialize}\r\n}{tf.WholeFileReader.supports\\_serialize }}\\label{tf.wholefilereader.supportsux5fserialize}\r\n\r\nWhether the Reader implementation can serialize its state.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.IdentityReader}\r\n}{class tf.IdentityReader }}\\label{class-tf.identityreader}\r\n\r\nA Reader that outputs the queued work as both the key and value.\r\n\r\nTo use, enqueue strings in a Queue. Read will take the front work string\r\nand output (work, work).\r\n\r\nSee ReaderBase for supported methods. - - -\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.IdentityReader.\\_\\_init\\_\\_(name=None)}\r\n}{tf.IdentityReader.\\_\\_init\\_\\_(name=None) }}\\label{tf.identityreader.ux5fux5finitux5fux5fnamenone}\r\n\r\nCreate a IdentityReader.\r\n\r\n\\subparagraph{Args: }\\label{args-22}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.IdentityReader.num\\_records\\_produced(name=None)}\r\n}{tf.IdentityReader.num\\_records\\_produced(name=None) }}\\label{tf.identityreader.numux5frecordsux5fproducednamenone}\r\n\r\nReturns the number of records this reader has produced.\r\n\r\nThis is the same as the number of Read executions that have succeeded.\r\n\r\n\\subparagraph{Args: }\\label{args-23}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-19}\r\n\r\nAn int64 Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.IdentityReader.num\\_work\\_units\\_completed(name=None)}\r\n}{tf.IdentityReader.num\\_work\\_units\\_completed(name=None) }}\\label{tf.identityreader.numux5fworkux5funitsux5fcompletednamenone}\r\n\r\nReturns the number of work units this reader has finished processing.\r\n\r\n\\subparagraph{Args: }\\label{args-24}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-20}\r\n\r\nAn int64 Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.IdentityReader.read(queue,\\ name=None)}\r\n}{tf.IdentityReader.read(queue, name=None) }}\\label{tf.identityreader.readqueue-namenone}\r\n\r\nReturns the next record (key, value pair) produced by a reader.\r\n\r\nWill dequeue a work unit from queue if necessary (e.g.~when the Reader\r\nneeds to start reading from a new file since it has finished with the\r\nprevious file).\r\n\r\n\\subparagraph{Args: }\\label{args-25}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{queue}: A Queue or a mutable string Tensor representing a\r\n  handle to a Queue, with string work items.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-21}\r\n\r\nA tuple of Tensors (key, value).\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{key}: A string scalar Tensor.\r\n\\item\r\n  \\texttt{value}: A string scalar Tensor.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.IdentityReader.reader\\_ref}\r\n}{tf.IdentityReader.reader\\_ref }}\\label{tf.identityreader.readerux5fref}\r\n\r\nOp that implements the reader.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.IdentityReader.reset(name=None)}\r\n}{tf.IdentityReader.reset(name=None) }}\\label{tf.identityreader.resetnamenone}\r\n\r\nRestore a reader to its initial clean state.\r\n\r\n\\subparagraph{Args: }\\label{args-26}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-22}\r\n\r\nThe created Operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.IdentityReader.restore\\_state(state,\\ name=None)}\r\n}{tf.IdentityReader.restore\\_state(state, name=None) }}\\label{tf.identityreader.restoreux5fstatestate-namenone}\r\n\r\nRestore a reader to a previously saved state.\r\n\r\nNot all Readers support being restored, so this can produce an\r\nUnimplemented error.\r\n\r\n\\subparagraph{Args: }\\label{args-27}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{state}: A string Tensor. Result of a SerializeState of a\r\n  Reader with matching type.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-23}\r\n\r\nThe created Operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.IdentityReader.serialize\\_state(name=None)}\r\n}{tf.IdentityReader.serialize\\_state(name=None) }}\\label{tf.identityreader.serializeux5fstatenamenone}\r\n\r\nProduce a string tensor that encodes the state of a reader.\r\n\r\nNot all Readers support being serialized, so this can produce an\r\nUnimplemented error.\r\n\r\n\\subparagraph{Args: }\\label{args-28}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-24}\r\n\r\nA string Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.IdentityReader.supports\\_serialize}\r\n}{tf.IdentityReader.supports\\_serialize }}\\label{tf.identityreader.supportsux5fserialize}\r\n\r\nWhether the Reader implementation can serialize its state.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.TFRecordReader}\r\n}{class tf.TFRecordReader }}\\label{class-tf.tfrecordreader}\r\n\r\nA Reader that outputs the records from a TFRecords file.\r\n\r\nSee ReaderBase for supported methods. - - -\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TFRecordReader.\\_\\_init\\_\\_(name=None)}\r\n}{tf.TFRecordReader.\\_\\_init\\_\\_(name=None) }}\\label{tf.tfrecordreader.ux5fux5finitux5fux5fnamenone}\r\n\r\nCreate a TFRecordReader.\r\n\r\n\\subparagraph{Args: }\\label{args-29}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TFRecordReader.num\\_records\\_produced(name=None)}\r\n}{tf.TFRecordReader.num\\_records\\_produced(name=None) }}\\label{tf.tfrecordreader.numux5frecordsux5fproducednamenone}\r\n\r\nReturns the number of records this reader has produced.\r\n\r\nThis is the same as the number of Read executions that have succeeded.\r\n\r\n\\subparagraph{Args: }\\label{args-30}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-25}\r\n\r\nAn int64 Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TFRecordReader.num\\_work\\_units\\_completed(name=None)}\r\n}{tf.TFRecordReader.num\\_work\\_units\\_completed(name=None) }}\\label{tf.tfrecordreader.numux5fworkux5funitsux5fcompletednamenone}\r\n\r\nReturns the number of work units this reader has finished processing.\r\n\r\n\\subparagraph{Args: }\\label{args-31}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-26}\r\n\r\nAn int64 Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TFRecordReader.read(queue,\\ name=None)}\r\n}{tf.TFRecordReader.read(queue, name=None) }}\\label{tf.tfrecordreader.readqueue-namenone}\r\n\r\nReturns the next record (key, value pair) produced by a reader.\r\n\r\nWill dequeue a work unit from queue if necessary (e.g.~when the Reader\r\nneeds to start reading from a new file since it has finished with the\r\nprevious file).\r\n\r\n\\subparagraph{Args: }\\label{args-32}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{queue}: A Queue or a mutable string Tensor representing a\r\n  handle to a Queue, with string work items.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-27}\r\n\r\nA tuple of Tensors (key, value).\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{key}: A string scalar Tensor.\r\n\\item\r\n  \\texttt{value}: A string scalar Tensor.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TFRecordReader.reader\\_ref}\r\n}{tf.TFRecordReader.reader\\_ref }}\\label{tf.tfrecordreader.readerux5fref}\r\n\r\nOp that implements the reader.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TFRecordReader.reset(name=None)}\r\n}{tf.TFRecordReader.reset(name=None) }}\\label{tf.tfrecordreader.resetnamenone}\r\n\r\nRestore a reader to its initial clean state.\r\n\r\n\\subparagraph{Args: }\\label{args-33}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-28}\r\n\r\nThe created Operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TFRecordReader.restore\\_state(state,\\ name=None)}\r\n}{tf.TFRecordReader.restore\\_state(state, name=None) }}\\label{tf.tfrecordreader.restoreux5fstatestate-namenone}\r\n\r\nRestore a reader to a previously saved state.\r\n\r\nNot all Readers support being restored, so this can produce an\r\nUnimplemented error.\r\n\r\n\\subparagraph{Args: }\\label{args-34}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{state}: A string Tensor. Result of a SerializeState of a\r\n  Reader with matching type.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-29}\r\n\r\nThe created Operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TFRecordReader.serialize\\_state(name=None)}\r\n}{tf.TFRecordReader.serialize\\_state(name=None) }}\\label{tf.tfrecordreader.serializeux5fstatenamenone}\r\n\r\nProduce a string tensor that encodes the state of a reader.\r\n\r\nNot all Readers support being serialized, so this can produce an\r\nUnimplemented error.\r\n\r\n\\subparagraph{Args: }\\label{args-35}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-30}\r\n\r\nA string Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.TFRecordReader.supports\\_serialize}\r\n}{tf.TFRecordReader.supports\\_serialize }}\\label{tf.tfrecordreader.supportsux5fserialize}\r\n\r\nWhether the Reader implementation can serialize its state.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.FixedLengthRecordReader}\r\n}{class tf.FixedLengthRecordReader }}\\label{class-tf.fixedlengthrecordreader}\r\n\r\nA Reader that outputs fixed-length records from a file.\r\n\r\nSee ReaderBase for supported methods. - - -\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.FixedLengthRecordReader.\\_\\_init\\_\\_(record\\_bytes,\\ header\\_bytes=None,\\ footer\\_bytes=None,\\ name=None)}\r\n}{tf.FixedLengthRecordReader.\\_\\_init\\_\\_(record\\_bytes, header\\_bytes=None, footer\\_bytes=None, name=None) }}\\label{tf.fixedlengthrecordreader.ux5fux5finitux5fux5frecordux5fbytes-headerux5fbytesnone-footerux5fbytesnone-namenone}\r\n\r\nCreate a FixedLengthRecordReader.\r\n\r\n\\subparagraph{Args: }\\label{args-36}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{record\\_bytes}: An int.\r\n\\item\r\n  \\texttt{header\\_bytes}: An optional int. Defaults to 0.\r\n\\item\r\n  \\texttt{footer\\_bytes}: An optional int. Defaults to 0.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.FixedLengthRecordReader.num\\_records\\_produced(name=None)}\r\n}{tf.FixedLengthRecordReader.num\\_records\\_produced(name=None) }}\\label{tf.fixedlengthrecordreader.numux5frecordsux5fproducednamenone}\r\n\r\nReturns the number of records this reader has produced.\r\n\r\nThis is the same as the number of Read executions that have succeeded.\r\n\r\n\\subparagraph{Args: }\\label{args-37}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-31}\r\n\r\nAn int64 Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.FixedLengthRecordReader.num\\_work\\_units\\_completed(name=None)}\r\n}{tf.FixedLengthRecordReader.num\\_work\\_units\\_completed(name=None) }}\\label{tf.fixedlengthrecordreader.numux5fworkux5funitsux5fcompletednamenone}\r\n\r\nReturns the number of work units this reader has finished processing.\r\n\r\n\\subparagraph{Args: }\\label{args-38}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-32}\r\n\r\nAn int64 Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.FixedLengthRecordReader.read(queue,\\ name=None)}\r\n}{tf.FixedLengthRecordReader.read(queue, name=None) }}\\label{tf.fixedlengthrecordreader.readqueue-namenone}\r\n\r\nReturns the next record (key, value pair) produced by a reader.\r\n\r\nWill dequeue a work unit from queue if necessary (e.g.~when the Reader\r\nneeds to start reading from a new file since it has finished with the\r\nprevious file).\r\n\r\n\\subparagraph{Args: }\\label{args-39}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{queue}: A Queue or a mutable string Tensor representing a\r\n  handle to a Queue, with string work items.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-33}\r\n\r\nA tuple of Tensors (key, value).\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{key}: A string scalar Tensor.\r\n\\item\r\n  \\texttt{value}: A string scalar Tensor.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.FixedLengthRecordReader.reader\\_ref}\r\n}{tf.FixedLengthRecordReader.reader\\_ref }}\\label{tf.fixedlengthrecordreader.readerux5fref}\r\n\r\nOp that implements the reader.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.FixedLengthRecordReader.reset(name=None)}\r\n}{tf.FixedLengthRecordReader.reset(name=None) }}\\label{tf.fixedlengthrecordreader.resetnamenone}\r\n\r\nRestore a reader to its initial clean state.\r\n\r\n\\subparagraph{Args: }\\label{args-40}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-34}\r\n\r\nThe created Operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.FixedLengthRecordReader.restore\\_state(state,\\ name=None)}\r\n}{tf.FixedLengthRecordReader.restore\\_state(state, name=None) }}\\label{tf.fixedlengthrecordreader.restoreux5fstatestate-namenone}\r\n\r\nRestore a reader to a previously saved state.\r\n\r\nNot all Readers support being restored, so this can produce an\r\nUnimplemented error.\r\n\r\n\\subparagraph{Args: }\\label{args-41}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{state}: A string Tensor. Result of a SerializeState of a\r\n  Reader with matching type.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-35}\r\n\r\nThe created Operation.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.FixedLengthRecordReader.serialize\\_state(name=None)}\r\n}{tf.FixedLengthRecordReader.serialize\\_state(name=None) }}\\label{tf.fixedlengthrecordreader.serializeux5fstatenamenone}\r\n\r\nProduce a string tensor that encodes the state of a reader.\r\n\r\nNot all Readers support being serialized, so this can produce an\r\nUnimplemented error.\r\n\r\n\\subparagraph{Args: }\\label{args-42}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-36}\r\n\r\nA string Tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.FixedLengthRecordReader.supports\\_serialize}\r\n}{tf.FixedLengthRecordReader.supports\\_serialize }}\\label{tf.fixedlengthrecordreader.supportsux5fserialize}\r\n\r\nWhether the Reader implementation can serialize its state.\r\n\r\n\\subsection{Converting }\\label{converting}\r\n\r\nTensorFlow provides several operations that you can use to convert\r\nvarious data formats into tensors.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.decode\\_csv(records,\\ record\\_defaults,\\ field\\_delim=None,\\ name=None)}\r\n}{tf.decode\\_csv(records, record\\_defaults, field\\_delim=None, name=None) }}\\label{tf.decodeux5fcsvrecords-recordux5fdefaults-fieldux5fdelimnone-namenone}\r\n\r\nConvert CSV records to tensors. Each column maps to one tensor.\r\n\r\nRFC 4180 format is expected for the CSV records.\r\n(https://tools.ietf.org/html/rfc4180) Note that we allow leading and\r\ntrailing spaces with int or float field.\r\n\r\n\\subparagraph{Args: }\\label{args-43}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{records}: A \\texttt{Tensor} of type \\texttt{string}. Each\r\n  string is a record/row in the csv and all records should have the same\r\n  format.\r\n\\item\r\n  \\texttt{record\\_defaults}: A list of \\texttt{Tensor} objects with\r\n  types from: \\texttt{float32}, \\texttt{int32}, \\texttt{int64},\r\n  \\texttt{string}. One tensor per column of the input record, with\r\n  either a scalar default value for that column or empty if the column\r\n  is required.\r\n\\item\r\n  \\texttt{field\\_delim}: An optional \\texttt{string}. Defaults to\r\n  \\texttt{\",\"}. delimiter to separate fields in a record.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-37}\r\n\r\nA list of \\texttt{Tensor} objects. Has the same type as\r\n\\texttt{record\\_defaults}. Each tensor will have the same shape as\r\nrecords.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.decode\\_raw(bytes,\\ out\\_type,\\ little\\_endian=None,\\ name=None)}\r\n}{tf.decode\\_raw(bytes, out\\_type, little\\_endian=None, name=None) }}\\label{tf.decodeux5frawbytes-outux5ftype-littleux5fendiannone-namenone}\r\n\r\nReinterpret the bytes of a string as a vector of numbers.\r\n\r\n\\subparagraph{Args: }\\label{args-44}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{bytes}: A \\texttt{Tensor} of type \\texttt{string}. All the\r\n  elements must have the same length.\r\n\\item\r\n  \\texttt{out\\_type}: A \\texttt{tf.DType} from:\r\n  \\texttt{tf.float32,\\ tf.float64,\\ tf.int32,\\ tf.uint8,\\ tf.int16,\\ tf.int8,\\ tf.int64}.\r\n\\item\r\n  \\texttt{little\\_endian}: An optional \\texttt{bool}. Defaults to\r\n  \\texttt{True}. Whether the input bytes are in little-endian order.\r\n  Ignored for out\\_types that are stored in a single byte like uint8.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-38}\r\n\r\nA \\texttt{Tensor} of type \\texttt{out\\_type}. A Tensor with one more\r\ndimension than the input bytes. The added dimension will have size equal\r\nto the length of the elements of bytes divided by the number of bytes to\r\nrepresent out\\_type.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{Example protocol buffer }\\label{example-protocol-buffer}\r\n\r\nTensorFlow's\r\n\\href{../../how_tos/reading_data/index.md\\#standard-tensorflow-format}{recommended\r\nformat for training examples} is serialized \\texttt{Example} protocol\r\nbuffers,\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/example.proto}{described\r\nhere}. They contain \\texttt{Features},\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/feature.proto}{described\r\nhere}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.parse\\_example(serialized,\\ names=None,\\ sparse\\_keys=None,\\ sparse\\_types=None,\\ dense\\_keys=None,\\ dense\\_types=None,\\ dense\\_defaults=None,\\ dense\\_shapes=None,\\ name=\\textquotesingle{}ParseExample\\textquotesingle{})}\r\n}{tf.parse\\_example(serialized, names=None, sparse\\_keys=None, sparse\\_types=None, dense\\_keys=None, dense\\_types=None, dense\\_defaults=None, dense\\_shapes=None, name='ParseExample') }}\\label{tf.parseux5fexampleserialized-namesnone-sparseux5fkeysnone-sparseux5ftypesnone-denseux5fkeysnone-denseux5ftypesnone-denseux5fdefaultsnone-denseux5fshapesnone-nameparseexample}\r\n\r\nParses \\texttt{Example} protos.\r\n\r\nParses a number of serialized {[}\\texttt{Example}{]}\r\n(https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/example.proto)\r\nprotos given in \\texttt{serialized}.\r\n\r\n\\texttt{names} may contain descriptive names for the corresponding\r\nserialized protos. These may be useful for debugging purposes, but they\r\nhave no effect on the output. If not \\texttt{None}, \\texttt{names} must\r\nbe the same length as \\texttt{serialized}.\r\n\r\nThis op parses serialized examples into a dictionary mapping keys to\r\n\\texttt{Tensor} and \\texttt{SparseTensor} objects respectively,\r\ndepending on whether the keys appear in \\texttt{sparse\\_keys} or\r\n\\texttt{dense\\_keys}.\r\n\r\nThe key \\texttt{dense\\_keys{[}j{]}} is mapped to a \\texttt{Tensor} of\r\ntype \\texttt{dense\\_types{[}j{]}} and of shape\r\n\\texttt{(serialized.size(),)\\ +\\ dense\\_shapes{[}j{]}}.\r\n\r\n\\texttt{dense\\_defaults} provides defaults for values referenced using\r\n\\texttt{dense\\_keys}. If a key is not present in this dictionary, the\r\ncorresponding dense \\texttt{Feature} is required in all elements of\r\n\\texttt{serialized}.\r\n\r\n\\texttt{dense\\_shapes{[}j{]}} provides the shape of each\r\n\\texttt{Feature} entry referenced by \\texttt{dense\\_keys{[}j{]}}. The\r\nnumber of elements in the \\texttt{Feature} corresponding to\r\n\\texttt{dense\\_key{[}j{]}} must always have\r\n\\texttt{np.prod(dense\\_shapes{[}j{]})} entries. The returned\r\n\\texttt{Tensor} for \\texttt{dense\\_key{[}j{]}} has shape\r\n\\texttt{{[}N{]}\\ +\\ dense\\_shape{[}j{]}}, where \\texttt{N} is the number\r\nof \\texttt{Example}s in \\texttt{serialized}.\r\n\r\nThe key \\texttt{sparse\\_keys{[}j{]}} is mapped to a\r\n\\texttt{SparseTensor} of type \\texttt{sparse\\_types{[}j{]}}. The\r\n\\texttt{SparseTensor} represents a ragged matrix. Its indices are\r\n\\texttt{{[}batch,\\ index{]}} where \\texttt{batch} is the batch entry the\r\nvalue is from, and \\texttt{index} is the value's index in the list of\r\nvalues associated with that feature and example.\r\n\r\nExamples:\r\n\r\nFor example, if one expects a \\texttt{tf.float32} sparse feature\r\n\\texttt{ft} and three serialized \\texttt{Example}s are provided:\r\n\r\n\\begin{verbatim}\r\nserialized = [\r\n  features:\r\n    { feature: [ key: { \"ft\" value: float_list: { value: [1.0, 2.0] } } ] },\r\n  features:\r\n    { feature: [] },\r\n  features:\r\n    { feature: [ key: { \"ft\" value: float_list: { value: [3.0] } } ] }\r\n]\r\n\\end{verbatim}\r\n\r\nthen the output will look like:\r\n\r\n\\begin{verbatim}\r\n{\"ft\": SparseTensor(indices=[[0, 0], [0, 1], [2, 0]],\r\n                    values=[1.0, 2.0, 3.0],\r\n                    shape=(3, 2)) }\r\n\\end{verbatim}\r\n\r\nGiven two \\texttt{Example} input protos in \\texttt{serialized}:\r\n\r\n\\begin{verbatim}\r\n[\r\n  features: {\r\n    feature: { key: \"kw\" value: { bytes_list: { value: [ \"knit\", \"big\" ] } } }\r\n    feature: { key: \"gps\" value: { float_list: { value: [] } } }\r\n  },\r\n  features: {\r\n    feature: { key: \"kw\" value: { bytes_list: { value: [ \"emmy\" ] } } }\r\n    feature: { key: \"dank\" value: { int64_list: { value: [ 42 ] } } }\r\n    feature: { key: \"gps\" value: { } }\r\n  }\r\n]\r\n\\end{verbatim}\r\n\r\nAnd arguments\r\n\r\n\\begin{verbatim}\r\n  names: [\"input0\", \"input1\"],\r\n  sparse_keys: [\"kw\", \"dank\", \"gps\"]\r\n  sparse_types: [DT_STRING, DT_INT64, DT_FLOAT]\r\n\\end{verbatim}\r\n\r\nThen the output is a dictionary:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{\\{}\r\n  \\StringTok{\"kw\"}\\NormalTok{: SparseTensor(}\r\n      \\NormalTok{indices}\\OperatorTok{=}\\NormalTok{[[}\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{], [}\\DecValTok{0}\\NormalTok{, }\\DecValTok{1}\\NormalTok{], [}\\DecValTok{1}\\NormalTok{, }\\DecValTok{0}\\NormalTok{]],}\r\n      \\NormalTok{values}\\OperatorTok{=}\\NormalTok{[}\\StringTok{\"knit\"}\\NormalTok{, }\\StringTok{\"big\"}\\NormalTok{, }\\StringTok{\"emmy\"}\\NormalTok{]}\r\n      \\NormalTok{shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{2}\\NormalTok{, }\\DecValTok{2}\\NormalTok{]),}\r\n  \\CommentTok{\"dank\"}\\NormalTok{: SparseTensor(}\r\n      \\NormalTok{indices}\\OperatorTok{=}\\NormalTok{[[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{0}\\NormalTok{]],}\r\n      \\NormalTok{values}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{42}\\NormalTok{],}\r\n      \\NormalTok{shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{2}\\NormalTok{, }\\DecValTok{1}\\NormalTok{]),}\r\n  \\CommentTok{\"gps\"}\\NormalTok{: SparseTensor(}\r\n      \\NormalTok{indices}\\OperatorTok{=}\\NormalTok{[],}\r\n      \\NormalTok{values}\\OperatorTok{=}\\NormalTok{[],}\r\n      \\NormalTok{shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{2}\\NormalTok{, }\\DecValTok{0}\\NormalTok{]),}\r\n\\NormalTok{\\}}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nFor dense results in two serialized \\texttt{Example}s:\r\n\r\n\\begin{verbatim}\r\n[\r\n  features: {\r\n    feature: { key: \"age\" value: { int64_list: { value: [ 0 ] } } }\r\n    feature: { key: \"gender\" value: { bytes_list: { value: [ \"f\" ] } } }\r\n   },\r\n   features: {\r\n    feature: { key: \"age\" value: { int64_list: { value: [] } } }\r\n    feature: { key: \"gender\" value: { bytes_list: { value: [ \"f\" ] } } }\r\n  }\r\n]\r\n\\end{verbatim}\r\n\r\nWe can use arguments:\r\n\r\n\\begin{verbatim}\r\nnames: [\"input0\", \"input1\"],\r\ndense_keys: np.array([\"age\", \"gender\"]),\r\ndense_types: [tf.int64, tf.string],\r\ndense_defaults: {\r\n  \"age\": -1  # \"age\" defaults to -1 if missing\r\n             # \"gender\" has no specified default so it's required\r\n}\r\ndense_shapes: [(1,), (1,)],  # age, gender, label, weight\r\n\\end{verbatim}\r\n\r\nAnd the expected output is:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{\\{}\r\n  \\StringTok{\"age\"}\\NormalTok{: [[}\\DecValTok{0}\\NormalTok{], [}\\OperatorTok{-}\\DecValTok{1}\\NormalTok{]],}\r\n  \\CommentTok{\"gender\"}\\NormalTok{: [[}\\StringTok{\"f\"}\\NormalTok{], [}\\StringTok{\"f\"}\\NormalTok{]],}\r\n\\NormalTok{\\}}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-45}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{serialized}: A list of strings, a batch of binary serialized\r\n  \\texttt{Example} protos.\r\n\\item\r\n  \\texttt{names}: A list of strings, the names of the serialized protos.\r\n\\item\r\n  \\texttt{sparse\\_keys}: A list of string keys in the examples'\r\n  features. The results for these keys will be returned as\r\n  \\texttt{SparseTensor} objects.\r\n\\item\r\n  \\texttt{sparse\\_types}: A list of \\texttt{DTypes} of the same length\r\n  as \\texttt{sparse\\_keys}. Only \\texttt{tf.float32}\r\n  (\\texttt{FloatList}), \\texttt{tf.int64} (\\texttt{Int64List}), and\r\n  \\texttt{tf.string} (\\texttt{BytesList}) are supported.\r\n\\item\r\n  \\texttt{dense\\_keys}: A list of string keys in the examples' features.\r\n  The results for these keys will be returned as \\texttt{Tensor}s\r\n\\item\r\n  \\texttt{dense\\_types}: A list of DTypes of the same length as\r\n  \\texttt{dense\\_keys}. Only \\texttt{tf.float32} (\\texttt{FloatList}),\r\n  \\texttt{tf.int64} (\\texttt{Int64List}), and \\texttt{tf.string}\r\n  (\\texttt{BytesList}) are supported.\r\n\\item\r\n  \\texttt{dense\\_defaults}: A dict mapping string keys to\r\n  \\texttt{Tensor}s. The keys of the dict must match the dense\\_keys of\r\n  the feature.\r\n\\item\r\n  \\texttt{dense\\_shapes}: A list of tuples with the same length as\r\n  \\texttt{dense\\_keys}. The shape of the data for each dense feature\r\n  referenced by \\texttt{dense\\_keys}.\r\n\\item\r\n  \\texttt{name}: A name for this operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-39}\r\n\r\nA \\texttt{dict} mapping keys to \\texttt{Tensor}s and\r\n\\texttt{SparseTensor}s.\r\n\r\n\\subparagraph{Raises: }\\label{raises}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: If sparse and dense key sets intersect, or input\r\n  lengths do not match up.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.parse\\_single\\_example(serialized,\\ names=None,\\ sparse\\_keys=None,\\ sparse\\_types=None,\\ dense\\_keys=None,\\ dense\\_types=None,\\ dense\\_defaults=None,\\ dense\\_shapes=None,\\ name=\\textquotesingle{}ParseSingleExample\\textquotesingle{})}\r\n}{tf.parse\\_single\\_example(serialized, names=None, sparse\\_keys=None, sparse\\_types=None, dense\\_keys=None, dense\\_types=None, dense\\_defaults=None, dense\\_shapes=None, name='ParseSingleExample') }}\\label{tf.parseux5fsingleux5fexampleserialized-namesnone-sparseux5fkeysnone-sparseux5ftypesnone-denseux5fkeysnone-denseux5ftypesnone-denseux5fdefaultsnone-denseux5fshapesnone-nameparsesingleexample}\r\n\r\nParses a single \\texttt{Example} proto.\r\n\r\nSimilar to \\texttt{parse\\_example}, except:\r\n\r\nFor dense tensors, the returned \\texttt{Tensor} is identical to the\r\noutput of \\texttt{parse\\_example}, except there is no batch dimension,\r\nthe output shape is the same as the shape given in\r\n\\texttt{dense\\_shape}.\r\n\r\nFor \\texttt{SparseTensor}s, the first (batch) column of the indices\r\nmatrix is removed (the indices matrix is a column vector), the values\r\nvector is unchanged, and the first (batch\\_size) entry of the shape\r\nvector is removed (it is now a single element vector).\r\n\r\nSee also \\texttt{parse\\_example}.\r\n\r\n\\subparagraph{Args: }\\label{args-46}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{serialized}: A scalar string, a single serialized Example. See\r\n  parse\\_example documentation for more details.\r\n\\item\r\n  \\texttt{names}: (Optional) A scalar string, the associated name. See\r\n  parse\\_example documentation for more details.\r\n\\item\r\n  \\texttt{sparse\\_keys}: See parse\\_example documentation for more\r\n  details.\r\n\\item\r\n  \\texttt{sparse\\_types}: See parse\\_example documentation for more\r\n  details.\r\n\\item\r\n  \\texttt{dense\\_keys}: See parse\\_example documentation for more\r\n  details.\r\n\\item\r\n  \\texttt{dense\\_types}: See parse\\_example documentation for more\r\n  details.\r\n\\item\r\n  \\texttt{dense\\_defaults}: See parse\\_example documentation for more\r\n  details.\r\n\\item\r\n  \\texttt{dense\\_shapes}: See parse\\_example documentation for more\r\n  details.\r\n\\item\r\n  \\texttt{name}: A name for this operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-40}\r\n\r\nA dictionary mapping keys to Tensors and SparseTensors.\r\n\r\n\\subparagraph{Raises: }\\label{raises-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if ``scalar'' or ``names'' have known shapes, and\r\n  are not scalars.\r\n\\end{itemize}\r\n\r\n\\subsection{Queues }\\label{queues}\r\n\r\nTensorFlow provides several implementations of `Queues', which are\r\nstructures within the TensorFlow computation graph to stage pipelines of\r\ntensors together. The following describe the basic Queue interface and\r\nsome implementations. To see an example use, see\r\n\\href{../../how_tos/threading_and_queues/index.md}{Threading and\r\nQueues}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.QueueBase}\r\n}{class tf.QueueBase }}\\label{class-tf.queuebase}\r\n\r\nBase class for queue implementations.\r\n\r\nA queue is a TensorFlow data structure that stores tensors across\r\nmultiple steps, and exposes operations that enqueue and dequeue tensors.\r\n\r\nEach queue element is a tuple of one or more tensors, where each tuple\r\ncomponent has a static dtype, and may have a static shape. The queue\r\nimplementations support versions of enqueue and dequeue that handle\r\nsingle elements, versions that support enqueuing and dequeuing a batch\r\nof elements at once.\r\n\r\nSee \\protect\\hyperlink{FIFOQueue}{\\texttt{tf.FIFOQueue}} and\r\n\\protect\\hyperlink{RandomShuffleQueue}{\\texttt{tf.RandomShuffleQueue}}\r\nfor concrete implementations of this class, and instructions on how to\r\ncreate them.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.QueueBase.enqueue(vals,\\ name=None)}\r\n}{tf.QueueBase.enqueue(vals, name=None) }}\\label{tf.queuebase.enqueuevals-namenone}\r\n\r\nEnqueues one element to this queue.\r\n\r\nIf the queue is full when this operation executes, it will block until\r\nthe element has been enqueued.\r\n\r\n\\subparagraph{Args: }\\label{args-47}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{vals}: The tuple of \\texttt{Tensor} objects to be enqueued.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-41}\r\n\r\nThe operation that enqueues a new tuple of tensors to the queue.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.QueueBase.enqueue\\_many(vals,\\ name=None)}\r\n}{tf.QueueBase.enqueue\\_many(vals, name=None) }}\\label{tf.queuebase.enqueueux5fmanyvals-namenone}\r\n\r\nEnqueues zero or elements to this queue.\r\n\r\nThis operation slices each component tensor along the 0th dimension to\r\nmake multiple queue elements. All of the tensors in \\texttt{vals} must\r\nhave the same size in the 0th dimension.\r\n\r\nIf the queue is full when this operation executes, it will block until\r\nall of the elements have been enqueued.\r\n\r\n\\subparagraph{Args: }\\label{args-48}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{vals}: The tensor or tuple of tensors from which the queue\r\n  elements are taken.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-42}\r\n\r\nThe operation that enqueues a batch of tuples of tensors to the queue.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.QueueBase.dequeue(name=None)}\r\n}{tf.QueueBase.dequeue(name=None) }}\\label{tf.queuebase.dequeuenamenone}\r\n\r\nDequeues one element from this queue.\r\n\r\nIf the queue is empty when this operation executes, it will block until\r\nthere is an element to dequeue.\r\n\r\n\\subparagraph{Args: }\\label{args-49}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-43}\r\n\r\nThe tuple of tensors that was dequeued.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.QueueBase.dequeue\\_many(n,\\ name=None)}\r\n}{tf.QueueBase.dequeue\\_many(n, name=None) }}\\label{tf.queuebase.dequeueux5fmanyn-namenone}\r\n\r\nDequeues and concatenates \\texttt{n} elements from this queue.\r\n\r\nThis operation concatenates queue-element component tensors along the\r\n0th dimension to make a single component tensor. All of the components\r\nin the dequeued tuple will have size \\texttt{n} in the 0th dimension.\r\n\r\nIf the queue contains fewer than \\texttt{n} elements when this operation\r\nexecutes, it will block until \\texttt{n} elements have been dequeued.\r\n\r\n\\subparagraph{Args: }\\label{args-50}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{n}: A scalar \\texttt{Tensor} containing the number of elements\r\n  to dequeue.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-44}\r\n\r\nThe tuple of concatenated tensors that was dequeued.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.QueueBase.size(name=None)}\r\n}{tf.QueueBase.size(name=None) }}\\label{tf.queuebase.sizenamenone}\r\n\r\nCompute the number of elements in this queue.\r\n\r\n\\subparagraph{Args: }\\label{args-51}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-45}\r\n\r\nA scalar tensor containing the number of elements in this queue.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.QueueBase.close(cancel\\_pending\\_enqueues=False,\\ name=None)}\r\n}{tf.QueueBase.close(cancel\\_pending\\_enqueues=False, name=None) }}\\label{tf.queuebase.closecancelux5fpendingux5fenqueuesfalse-namenone}\r\n\r\nCloses this queue.\r\n\r\nThis operation signals that no more elements will be enqueued in the\r\ngiven queue. Subsequent \\texttt{enqueue} and \\texttt{enqueue\\_many}\r\noperations will fail. Subsequent \\texttt{dequeue} and\r\n\\texttt{dequeue\\_many} operations will continue to succeed if sufficient\r\nelements remain in the queue. Subsequent \\texttt{dequeue} and\r\n\\texttt{dequeue\\_many} operations that would block will fail\r\nimmediately.\r\n\r\nIf \\texttt{cancel\\_pending\\_enqueues} is \\texttt{True}, all pending\r\nrequests will also be cancelled.\r\n\r\n\\subparagraph{Args: }\\label{args-52}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{cancel\\_pending\\_enqueues}: (Optional.) A boolean, defaulting\r\n  to \\texttt{False} (described above).\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-46}\r\n\r\nThe operation that closes the queue.\r\n\r\n\\paragraph{Other Methods }\\label{other-methods}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.QueueBase.\\_\\_init\\_\\_(dtypes,\\ shapes,\\ queue\\_ref)}\r\n}{tf.QueueBase.\\_\\_init\\_\\_(dtypes, shapes, queue\\_ref) }}\\label{tf.queuebase.ux5fux5finitux5fux5fdtypes-shapes-queueux5fref}\r\n\r\nConstructs a queue object from a queue reference.\r\n\r\n\\subparagraph{Args: }\\label{args-53}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{dtypes}: A list of types. The length of dtypes must equal the\r\n  number of tensors in each element.\r\n\\item\r\n  \\texttt{shapes}: Constraints on the shapes of tensors in an element: A\r\n  list of shape tuples or None. This list is the same length as dtypes.\r\n  If the shape of any tensors in the element are constrained, all must\r\n  be; shapes can be None if the shapes should not be constrained.\r\n\\item\r\n  \\texttt{queue\\_ref}: The queue reference, i.e.~the output of the queue\r\n  op.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.QueueBase.dtypes}\r\n}{tf.QueueBase.dtypes }}\\label{tf.queuebase.dtypes}\r\n\r\nThe list of dtypes for each component of a queue element.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.QueueBase.name}\r\n}{tf.QueueBase.name }}\\label{tf.queuebase.name}\r\n\r\nThe name of the underlying queue.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.QueueBase.queue\\_ref}\r\n}{tf.QueueBase.queue\\_ref }}\\label{tf.queuebase.queueux5fref}\r\n\r\nThe underlying queue reference.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.FIFOQueue}\r\n}{class tf.FIFOQueue }}\\label{class-tf.fifoqueue}\r\n\r\nA queue implementation that dequeues elements in first-in-first out\r\norder.\r\n\r\nSee \\protect\\hyperlink{QueueBase}{\\texttt{tf.QueueBase}} for a\r\ndescription of the methods on this class.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.FIFOQueue.\\_\\_init\\_\\_(capacity,\\ dtypes,\\ shapes=None,\\ shared\\_name=None,\\ name=\\textquotesingle{}fifo\\_queue\\textquotesingle{})}\r\n}{tf.FIFOQueue.\\_\\_init\\_\\_(capacity, dtypes, shapes=None, shared\\_name=None, name='fifo\\_queue') }}\\label{tf.fifoqueue.ux5fux5finitux5fux5fcapacity-dtypes-shapesnone-sharedux5fnamenone-namefifoux5fqueue}\r\n\r\nCreates a queue that dequeues elements in a first-in first-out order.\r\n\r\nA \\texttt{FIFOQueue} has bounded capacity; supports multiple concurrent\r\nproducers and consumers; and provides exactly-once delivery.\r\n\r\nA \\texttt{FIFOQueue} holds a list of up to \\texttt{capacity} elements.\r\nEach element is a fixed-length tuple of tensors whose dtypes are\r\ndescribed by \\texttt{dtypes}, and whose shapes are optionally described\r\nby the \\texttt{shapes} argument.\r\n\r\nIf the \\texttt{shapes} argument is specified, each component of a queue\r\nelement must have the respective fixed shape. If it is unspecified,\r\ndifferent queue elements may have different shapes, but the use of\r\n\\texttt{dequeue\\_many} is disallowed.\r\n\r\n\\subparagraph{Args: }\\label{args-54}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{capacity}: An integer. The upper bound on the number of\r\n  elements that may be stored in this queue.\r\n\\item\r\n  \\texttt{dtypes}: A list of \\texttt{DType} objects. The length of\r\n  \\texttt{dtypes} must equal the number of tensors in each queue\r\n  element.\r\n\\item\r\n  \\texttt{shapes}: (Optional.) A list of fully-defined\r\n  \\texttt{TensorShape} objects, with the same length as \\texttt{dtypes}\r\n  or \\texttt{None}.\r\n\\item\r\n  \\texttt{shared\\_name}: (Optional.) If non-empty, this queue will be\r\n  shared under the given name across multiple sessions.\r\n\\item\r\n  \\texttt{name}: Optional name for the queue operation.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.RandomShuffleQueue}\r\n}{class tf.RandomShuffleQueue }}\\label{class-tf.randomshufflequeue}\r\n\r\nA queue implementation that dequeues elements in a random order.\r\n\r\nSee \\protect\\hyperlink{QueueBase}{\\texttt{tf.QueueBase}} for a\r\ndescription of the methods on this class.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.RandomShuffleQueue.\\_\\_init\\_\\_(capacity,\\ min\\_after\\_dequeue,\\ dtypes,\\ shapes=None,\\ seed=None,\\ shared\\_name=None,\\ name=\\textquotesingle{}random\\_shuffle\\_queue\\textquotesingle{})}\r\n}{tf.RandomShuffleQueue.\\_\\_init\\_\\_(capacity, min\\_after\\_dequeue, dtypes, shapes=None, seed=None, shared\\_name=None, name='random\\_shuffle\\_queue') }}\\label{tf.randomshufflequeue.ux5fux5finitux5fux5fcapacity-minux5fafterux5fdequeue-dtypes-shapesnone-seednone-sharedux5fnamenone-namerandomux5fshuffleux5fqueue}\r\n\r\nCreate a queue that dequeues elements in a random order.\r\n\r\nA \\texttt{RandomShuffleQueue} has bounded capacity; supports multiple\r\nconcurrent producers and consumers; and provides exactly-once delivery.\r\n\r\nA \\texttt{RandomShuffleQueue} holds a list of up to \\texttt{capacity}\r\nelements. Each element is a fixed-length tuple of tensors whose dtypes\r\nare described by \\texttt{dtypes}, and whose shapes are optionally\r\ndescribed by the \\texttt{shapes} argument.\r\n\r\nIf the \\texttt{shapes} argument is specified, each component of a queue\r\nelement must have the respective fixed shape. If it is unspecified,\r\ndifferent queue elements may have different shapes, but the use of\r\n\\texttt{dequeue\\_many} is disallowed.\r\n\r\nThe \\texttt{min\\_after\\_dequeue} argument allows the caller to specify a\r\nminimum number of elements that will remain in the queue after a\r\n\\texttt{dequeue} or \\texttt{dequeue\\_many} operation completes, to\r\nensure a minimum level of mixing of elements. This invariant is\r\nmaintained by blocking those operations until sufficient elements have\r\nbeen enqueued. The \\texttt{min\\_after\\_dequeue} argument is ignored\r\nafter the queue has been closed.\r\n\r\n\\subparagraph{Args: }\\label{args-55}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{capacity}: An integer. The upper bound on the number of\r\n  elements that may be stored in this queue.\r\n\\item\r\n  \\texttt{min\\_after\\_dequeue}: An integer (described above).\r\n\\item\r\n  \\texttt{dtypes}: A list of \\texttt{DType} objects. The length of\r\n  \\texttt{dtypes} must equal the number of tensors in each queue\r\n  element.\r\n\\item\r\n  \\texttt{shapes}: (Optional.) A list of fully-defined\r\n  \\texttt{TensorShape} objects, with the same length as \\texttt{dtypes}\r\n  or \\texttt{None}.\r\n\\item\r\n  \\texttt{seed}: A Python integer. Used to create a random seed. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\texttt{set\\_random\\_seed}}\r\n  for behavior.\r\n\\item\r\n  \\texttt{shared\\_name}: (Optional.) If non-empty, this queue will be\r\n  shared under the given name across multiple sessions.\r\n\\item\r\n  \\texttt{name}: Optional name for the queue operation.\r\n\\end{itemize}\r\n\r\n\\subsection{Dealing with the filesystem\r\n}\\label{dealing-with-the-filesystem}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.matching\\_files(pattern,\\ name=None)}\r\n}{tf.matching\\_files(pattern, name=None) }}\\label{tf.matchingux5ffilespattern-namenone}\r\n\r\nReturns the set of files matching a pattern.\r\n\r\nNote that this routine only supports wildcard characters in the basename\r\nportion of the pattern, not in the directory portion.\r\n\r\n\\subparagraph{Args: }\\label{args-56}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{pattern}: A \\texttt{Tensor} of type \\texttt{string}. A\r\n  (scalar) shell wildcard pattern.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-47}\r\n\r\nA \\texttt{Tensor} of type \\texttt{string}. A vector of matching\r\nfilenames.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.read\\_file(filename,\\ name=None)}\r\n}{tf.read\\_file(filename, name=None) }}\\label{tf.readux5ffilefilename-namenone}\r\n\r\nReads and outputs the entire contents of the input filename.\r\n\r\n\\subparagraph{Args: }\\label{args-57}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{filename}: A \\texttt{Tensor} of type \\texttt{string}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-48}\r\n\r\nA \\texttt{Tensor} of type \\texttt{string}.\r\n\r\n\\subsection{Input pipeline }\\label{input-pipeline}\r\n\r\nTensorFlow functions for setting up an input-prefetching pipeline.\r\nPlease see the \\href{../../how_tos/reading_data/index.md}{reading data\r\nhow-to} for context.\r\n\r\n\\subsubsection{Beginning of an input pipeline\r\n}\\label{beginning-of-an-input-pipeline}\r\n\r\nThe ``producer'' functions add a queue to the graph and a corresponding\r\n\\texttt{QueueRunner} for running the subgraph that fills that queue.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.match\\_filenames\\_once(pattern,\\ name=None)}\r\n}{tf.train.match\\_filenames\\_once(pattern, name=None) }}\\label{tf.train.matchux5ffilenamesux5foncepattern-namenone}\r\n\r\nSave the list of files matching pattern, so it is only computed once.\r\n\r\n\\subparagraph{Args: }\\label{args-58}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{pattern}: A file pattern (glob).\r\n\\item\r\n  \\texttt{name}: A name for the operations (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-49}\r\n\r\nA variable that is initialized to the list of files matching pattern.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.limit\\_epochs(tensor,\\ num\\_epochs=None,\\ name=None)}\r\n}{tf.train.limit\\_epochs(tensor, num\\_epochs=None, name=None) }}\\label{tf.train.limitux5fepochstensor-numux5fepochsnone-namenone}\r\n\r\nReturns tensor num\\_epochs times and then raises an OutOfRange error.\r\n\r\n\\subparagraph{Args: }\\label{args-59}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{tensor}: Any Tensor.\r\n\\item\r\n  \\texttt{num\\_epochs}: An integer (optional). If specified, limits the\r\n  number of steps the output tensor may be evaluated.\r\n\\item\r\n  \\texttt{name}: A name for the operations (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-50}\r\n\r\ntensor or OutOfRange.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.range\\_input\\_producer(limit,\\ num\\_epochs=None,\\ shuffle=True,\\ seed=None,\\ capacity=32,\\ name=None)}\r\n}{tf.train.range\\_input\\_producer(limit, num\\_epochs=None, shuffle=True, seed=None, capacity=32, name=None) }}\\label{tf.train.rangeux5finputux5fproducerlimit-numux5fepochsnone-shuffletrue-seednone-capacity32-namenone}\r\n\r\nProduces the integers from 0 to limit-1 in a queue.\r\n\r\n\\subparagraph{Args: }\\label{args-60}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{limit}: An int32 scalar tensor.\r\n\\item\r\n  \\texttt{num\\_epochs}: An integer (optional). If specified,\r\n  \\texttt{range\\_input\\_producer} produces each integer\r\n  \\texttt{num\\_epochs} times before generating an OutOfRange error. If\r\n  not specified, \\texttt{range\\_input\\_producer} can cycle through the\r\n  integers an unlimited number of times.\r\n\\item\r\n  \\texttt{shuffle}: Boolean. If true, the integers are randomly shuffled\r\n  within each epoch.\r\n\\item\r\n  \\texttt{seed}: An integer (optional). Seed used if shuffle == True.\r\n\\item\r\n  \\texttt{capacity}: An integer. Sets the queue capacity.\r\n\\item\r\n  \\texttt{name}: A name for the operations (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-51}\r\n\r\nA Queue with the output integers. A QueueRunner for the Queue is added\r\nto the current Graph's QUEUE\\_RUNNER collection.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.slice\\_input\\_producer(tensor\\_list,\\ num\\_epochs=None,\\ shuffle=True,\\ seed=None,\\ capacity=32,\\ name=None)}\r\n}{tf.train.slice\\_input\\_producer(tensor\\_list, num\\_epochs=None, shuffle=True, seed=None, capacity=32, name=None) }}\\label{tf.train.sliceux5finputux5fproducertensorux5flist-numux5fepochsnone-shuffletrue-seednone-capacity32-namenone}\r\n\r\nProduces a slice of each Tensor in tensor\\_list.\r\n\r\nImplemented using a Queue -- a QueueRunner for the Queue is added to the\r\ncurrent Graph's QUEUE\\_RUNNER collection.\r\n\r\n\\subparagraph{Args: }\\label{args-61}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{tensor\\_list}: A list of Tensors. Every Tensor in tensor\\_list\r\n  must have the same size in the first dimension.\r\n\\item\r\n  \\texttt{num\\_epochs}: An integer (optional). If specified,\r\n  \\texttt{slice\\_input\\_producer} produces each slice\r\n  \\texttt{num\\_epochs} times before generating an OutOfRange error. If\r\n  not specified, \\texttt{slice\\_input\\_producer} can cycle through the\r\n  slices an unlimited number of times.\r\n\\item\r\n  \\texttt{seed}: An integer (optional). Seed used if shuffle == True.\r\n\\item\r\n  \\texttt{capacity}: An integer. Sets the queue capacity.\r\n\\item\r\n  \\texttt{name}: A name for the operations (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-52}\r\n\r\nA list of tensors, one for each element of tensor\\_list. If the tensor\r\nin tensor\\_list has shape {[}N, a, b, .., z{]}, then the corresponding\r\noutput tensor will have shape {[}a, b, \\ldots{}, z{]}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.string\\_input\\_producer(string\\_tensor,\\ num\\_epochs=None,\\ shuffle=True,\\ seed=None,\\ capacity=32,\\ name=None)}\r\n}{tf.train.string\\_input\\_producer(string\\_tensor, num\\_epochs=None, shuffle=True, seed=None, capacity=32, name=None) }}\\label{tf.train.stringux5finputux5fproducerstringux5ftensor-numux5fepochsnone-shuffletrue-seednone-capacity32-namenone}\r\n\r\nOutput strings (e.g.~filenames) to a queue for an input pipeline.\r\n\r\n\\subparagraph{Args: }\\label{args-62}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{string\\_tensor}: A 1-D string tensor with the strings to\r\n  produce.\r\n\\item\r\n  \\texttt{num\\_epochs}: An integer (optional). If specified,\r\n  \\texttt{string\\_input\\_producer} produces each string from\r\n  \\texttt{string\\_tensor} \\texttt{num\\_epochs} times before generating\r\n  an OutOfRange error. If not specified,\r\n  \\texttt{string\\_input\\_producer} can cycle through the strings in\r\n  \\texttt{string\\_tensor} an unlimited number of times.\r\n\\item\r\n  \\texttt{shuffle}: Boolean. If true, the strings are randomly shuffled\r\n  within each epoch.\r\n\\item\r\n  \\texttt{seed}: An integer (optional). Seed used if shuffle == True.\r\n\\item\r\n  \\texttt{capacity}: An integer. Sets the queue capacity.\r\n\\item\r\n  \\texttt{name}: A name for the operations (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-53}\r\n\r\nA queue with the output strings. A QueueRunner for the Queue is added to\r\nthe current Graph's QUEUE\\_RUNNER collection.\r\n\r\n\\subsubsection{Batching at the end of an input pipeline\r\n}\\label{batching-at-the-end-of-an-input-pipeline}\r\n\r\nThese functions add a queue to the graph to assemble a batch of\r\nexamples, with possible shuffling. They also add a \\texttt{QueueRunner}\r\nfor running the subgraph that fills that queue.\r\n\r\nUse \\protect\\hyperlink{batch}{batch} or\r\n\\protect\\hyperlink{batchux5fjoin}{batch\\_join} for batching examples\r\nthat have already been well shuffled. Use\r\n\\protect\\hyperlink{shuffleux5fbatch}{shuffle\\_batch} or\r\n\\protect\\hyperlink{shuffleux5fbatchux5fjoin}{shuffle\\_batch\\_join} for\r\nexamples that would benefit from additional shuffling.\r\n\r\nUse \\protect\\hyperlink{batch}{batch} or\r\n\\protect\\hyperlink{shuffleux5fbatch}{shuffle\\_batch} if you want a\r\nsingle thread producing examples to batch, or if you have a single\r\nsubgraph producing examples but you want to run it in N threads (where\r\nyou increase N until it can keep the queue full). Use\r\n\\protect\\hyperlink{batchux5fjoin}{batch\\_join} or\r\n\\protect\\hyperlink{shuffleux5fbatchux5fjoin}{shuffle\\_batch\\_join} if\r\nyou have N different subgraphs producing examples to batch and you want\r\nthem run by N threads.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.batch(tensor\\_list,\\ batch\\_size,\\ num\\_threads=1,\\ capacity=32,\\ enqueue\\_many=False,\\ shapes=None,\\ name=None)}\r\n}{tf.train.batch(tensor\\_list, batch\\_size, num\\_threads=1, capacity=32, enqueue\\_many=False, shapes=None, name=None) }}\\label{tf.train.batchtensorux5flist-batchux5fsize-numux5fthreads1-capacity32-enqueueux5fmanyfalse-shapesnone-namenone}\r\n\r\nCreates batches of tensors in \\texttt{tensor\\_list}.\r\n\r\nThis function is implemented using a queue. A \\texttt{QueueRunner} for\r\nthe queue is added to the current \\texttt{Graph}'s\r\n\\texttt{QUEUE\\_RUNNER} collection.\r\n\r\nIf \\texttt{enqueue\\_many} is \\texttt{False}, \\texttt{tensor\\_list} is\r\nassumed to represent a single example. An input tensor with shape\r\n\\texttt{{[}x,\\ y,\\ z{]}} will be output as a tensor with shape\r\n\\texttt{{[}batch\\_size,\\ x,\\ y,\\ z{]}}.\r\n\r\nIf \\texttt{enqueue\\_many} is \\texttt{True}, \\texttt{tensor\\_list} is\r\nassumed to represent a batch of examples, where the first dimension is\r\nindexed by example, and all members of \\texttt{tensor\\_list} should have\r\nthe same size in the first dimension. If an input tensor has shape\r\n\\texttt{{[}*,\\ x,\\ y,\\ z{]}}, the output will have shape\r\n\\texttt{{[}batch\\_size,\\ x,\\ y,\\ z{]}}. The \\texttt{capacity} argument\r\ncontrols the how long the prefetching is allowed to grow the queues.\r\n\r\n\\subparagraph{Args: }\\label{args-63}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{tensor\\_list}: The list of tensors to enqueue.\r\n\\item\r\n  \\texttt{batch\\_size}: The new batch size pulled from the queue.\r\n\\item\r\n  \\texttt{num\\_threads}: The number of threads enqueuing\r\n  \\texttt{tensor\\_list}.\r\n\\item\r\n  \\texttt{capacity}: An integer. The maximum number of elements in the\r\n  queue.\r\n\\item\r\n  \\texttt{enqueue\\_many}: Whether each tensor in \\texttt{tensor\\_list}\r\n  is a single example.\r\n\\item\r\n  \\texttt{shapes}: (Optional) The shapes for each example. Defaults to\r\n  the inferred shapes for \\texttt{tensor\\_list}.\r\n\\item\r\n  \\texttt{name}: (Optional) A name for the operations.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-54}\r\n\r\nA list of tensors with the same number and types as\r\n\\texttt{tensor\\_list}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.batch\\_join(tensor\\_list\\_list,\\ batch\\_size,\\ capacity=32,\\ enqueue\\_many=False,\\ shapes=None,\\ name=None)}\r\n}{tf.train.batch\\_join(tensor\\_list\\_list, batch\\_size, capacity=32, enqueue\\_many=False, shapes=None, name=None) }}\\label{tf.train.batchux5fjointensorux5flistux5flist-batchux5fsize-capacity32-enqueueux5fmanyfalse-shapesnone-namenone}\r\n\r\nRuns a list of tensors to fill a queue to create batches of examples.\r\n\r\nEnqueues a different list of tensors in different threads. Implemented\r\nusing a queue -- a \\texttt{QueueRunner} for the queue is added to the\r\ncurrent \\texttt{Graph}'s \\texttt{QUEUE\\_RUNNER} collection.\r\n\r\n\\texttt{len(tensor\\_list\\_list)} threads will be started, with thread\r\n\\texttt{i} enqueuing the tensors from\r\n\\texttt{tensor\\_list\\_list{[}i{]}}.\r\n\\texttt{tensor\\_list\\_list{[}i1{]}{[}j{]}} must match\r\n\\texttt{tensor\\_list\\_list{[}i2{]}{[}j{]}} in type and shape, except in\r\nthe first dimension if \\texttt{enqueue\\_many} is true.\r\n\r\nIf \\texttt{enqueue\\_many} is \\texttt{False}, each\r\n\\texttt{tensor\\_list\\_list{[}i{]}} is assumed to represent a single\r\nexample. An input tensor \\texttt{x} will be output as a tensor with\r\nshape \\texttt{{[}batch\\_size{]}\\ +\\ x.shape}.\r\n\r\nIf \\texttt{enqueue\\_many} is \\texttt{True},\r\n\\texttt{tensor\\_list\\_list{[}i{]}} is assumed to represent a batch of\r\nexamples, where the first dimension is indexed by example, and all\r\nmembers of \\texttt{tensor\\_list\\_list{[}i{]}} should have the same size\r\nin the first dimension. The slices of any input tensor \\texttt{x} are\r\ntreated as examples, and the output tensors will have shape\r\n\\texttt{{[}batch\\_size{]}\\ +\\ x.shape{[}1:{]}}.\r\n\r\nThe \\texttt{capacity} argument controls the how long the prefetching is\r\nallowed to grow the queues.\r\n\r\n\\subparagraph{Args: }\\label{args-64}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{tensor\\_list\\_list}: A list of tuples of tensors to enqueue.\r\n\\item\r\n  \\texttt{batch\\_size}: An integer. The new batch size pulled from the\r\n  queue.\r\n\\item\r\n  \\texttt{capacity}: An integer. The maximum number of elements in the\r\n  queue.\r\n\\item\r\n  \\texttt{enqueue\\_many}: Whether each tensor in\r\n  \\texttt{tensor\\_list\\_list} is a single example.\r\n\\item\r\n  \\texttt{shapes}: (Optional) The shapes for each example. Defaults to\r\n  the inferred shapes for \\texttt{tensor\\_list\\_list{[}i{]}}.\r\n\\item\r\n  \\texttt{name}: (Optional) A name for the operations.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-55}\r\n\r\nA list of tensors with the same number and types as\r\n\\texttt{tensor\\_list\\_list{[}i{]}}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.shuffle\\_batch(tensor\\_list,\\ batch\\_size,\\ capacity,\\ min\\_after\\_dequeue,\\ num\\_threads=1,\\ seed=None,\\ enqueue\\_many=False,\\ shapes=None,\\ name=None)}\r\n}{tf.train.shuffle\\_batch(tensor\\_list, batch\\_size, capacity, min\\_after\\_dequeue, num\\_threads=1, seed=None, enqueue\\_many=False, shapes=None, name=None) }}\\label{tf.train.shuffleux5fbatchtensorux5flist-batchux5fsize-capacity-minux5fafterux5fdequeue-numux5fthreads1-seednone-enqueueux5fmanyfalse-shapesnone-namenone}\r\n\r\nCreates batches by randomly shuffling tensors.\r\n\r\nThis function adds the following to the current \\texttt{Graph}:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  A shuffling queue into which tensors from \\texttt{tensor\\_list} are\r\n  enqueued.\r\n\\item\r\n  A \\texttt{dequeue\\_many} operation to create batches from the queue.\r\n\\item\r\n  A \\texttt{QueueRunner} to \\texttt{QUEUE\\_RUNNER} collection, to\r\n  enqueue the tensors from \\texttt{tensor\\_list}.\r\n\\end{itemize}\r\n\r\nIf \\texttt{enqueue\\_many} is \\texttt{False}, \\texttt{tensor\\_list} is\r\nassumed to represent a single example. An input tensor with shape\r\n\\texttt{{[}x,\\ y,\\ z{]}} will be output as a tensor with shape\r\n\\texttt{{[}batch\\_size,\\ x,\\ y,\\ z{]}}.\r\n\r\nIf \\texttt{enqueue\\_many} is \\texttt{True}, \\texttt{tensor\\_list} is\r\nassumed to represent a batch of examples, where the first dimension is\r\nindexed by example, and all members of \\texttt{tensor\\_list} should have\r\nthe same size in the first dimension. If an input tensor has shape\r\n\\texttt{{[}*,\\ x,\\ y,\\ z{]}}, the output will have shape\r\n\\texttt{{[}batch\\_size,\\ x,\\ y,\\ z{]}}.\r\n\r\nThe \\texttt{capacity} argument controls the how long the prefetching is\r\nallowed to grow the queues.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Creates batches of 32 images and 32 labels.}\r\n\\NormalTok{image_batch, label_batch }\\OperatorTok{=} \\NormalTok{tf.train.shuffle_batch(}\r\n      \\NormalTok{[single_image, single_label],}\r\n      \\NormalTok{batch_size}\\OperatorTok{=}\\DecValTok{32}\\NormalTok{,}\r\n      \\NormalTok{num_threads}\\OperatorTok{=}\\DecValTok{4}\\NormalTok{,}\r\n      \\NormalTok{capacity}\\OperatorTok{=}\\DecValTok{50000}\\NormalTok{,}\r\n      \\NormalTok{min_after_dequeue}\\OperatorTok{=}\\DecValTok{10000}\\NormalTok{)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-65}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{tensor\\_list}: The list of tensors to enqueue.\r\n\\item\r\n  \\texttt{batch\\_size}: The new batch size pulled from the queue.\r\n\\item\r\n  \\texttt{capacity}: An integer. The maximum number of elements in the\r\n  queue.\r\n\\item\r\n  \\texttt{min\\_after\\_dequeue}: Minimum number elements in the queue\r\n  after a dequeue, used to ensure a level of mixing of elements.\r\n\\item\r\n  \\texttt{num\\_threads}: The number of threads enqueuing\r\n  \\texttt{tensor\\_list}.\r\n\\item\r\n  \\texttt{seed}: Seed for the random shuffling within the queue.\r\n\\item\r\n  \\texttt{enqueue\\_many}: Whether each tensor in \\texttt{tensor\\_list}\r\n  is a single example.\r\n\\item\r\n  \\texttt{shapes}: (Optional) The shapes for each example. Defaults to\r\n  the inferred shapes for \\texttt{tensor\\_list}.\r\n\\item\r\n  \\texttt{name}: (Optional) A name for the operations.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-56}\r\n\r\nA list of tensors with the same number and types as\r\n\\texttt{tensor\\_list}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.shuffle\\_batch\\_join(tensor\\_list\\_list,\\ batch\\_size,\\ capacity,\\ min\\_after\\_dequeue,\\ seed=None,\\ enqueue\\_many=False,\\ shapes=None,\\ name=None)}\r\n}{tf.train.shuffle\\_batch\\_join(tensor\\_list\\_list, batch\\_size, capacity, min\\_after\\_dequeue, seed=None, enqueue\\_many=False, shapes=None, name=None) }}\\label{tf.train.shuffleux5fbatchux5fjointensorux5flistux5flist-batchux5fsize-capacity-minux5fafterux5fdequeue-seednone-enqueueux5fmanyfalse-shapesnone-namenone}\r\n\r\nCreate batches by randomly shuffling tensors.\r\n\r\nThis version enqueues a different list of tensors in different threads.\r\nIt adds the following to the current \\texttt{Graph}:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  A shuffling queue into which tensors from \\texttt{tensor\\_list\\_list}\r\n  are enqueued.\r\n\\item\r\n  A \\texttt{dequeue\\_many} operation to create batches from the queue.\r\n\\item\r\n  A \\texttt{QueueRunner} to \\texttt{QUEUE\\_RUNNER} collection, to\r\n  enqueue the tensors from \\texttt{tensor\\_list\\_list}.\r\n\\end{itemize}\r\n\r\n\\texttt{len(tensor\\_list\\_list)} threads will be started, with thread\r\n\\texttt{i} enqueuing the tensors from\r\n\\texttt{tensor\\_list\\_list{[}i{]}}.\r\n\\texttt{tensor\\_list\\_list{[}i1{]}{[}j{]}} must match\r\n\\texttt{tensor\\_list\\_list{[}i2{]}{[}j{]}} in type and shape, except in\r\nthe first dimension if \\texttt{enqueue\\_many} is true.\r\n\r\nIf \\texttt{enqueue\\_many} is \\texttt{False}, each\r\n\\texttt{tensor\\_list\\_list{[}i{]}} is assumed to represent a single\r\nexample. An input tensor with shape \\texttt{{[}x,\\ y,\\ z{]}} will be\r\noutput as a tensor with shape \\texttt{{[}batch\\_size,\\ x,\\ y,\\ z{]}}.\r\n\r\nIf \\texttt{enqueue\\_many} is \\texttt{True},\r\n\\texttt{tensor\\_list\\_list{[}i{]}} is assumed to represent a batch of\r\nexamples, where the first dimension is indexed by example, and all\r\nmembers of \\texttt{tensor\\_list\\_list{[}i{]}} should have the same size\r\nin the first dimension. If an input tensor has shape\r\n\\texttt{{[}*,\\ x,\\ y,\\ z{]}}, the output will have shape\r\n\\texttt{{[}batch\\_size,\\ x,\\ y,\\ z{]}}.\r\n\r\nThe \\texttt{capacity} argument controls the how long the prefetching is\r\nallowed to grow the queues.\r\n\r\n\\subparagraph{Args: }\\label{args-66}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{tensor\\_list\\_list}: A list of tuples of tensors to enqueue.\r\n\\item\r\n  \\texttt{batch\\_size}: An integer. The new batch size pulled from the\r\n  queue.\r\n\\item\r\n  \\texttt{capacity}: An integer. The maximum number of elements in the\r\n  queue.\r\n\\item\r\n  \\texttt{min\\_after\\_dequeue}: Minimum number elements in the queue\r\n  after a dequeue, used to ensure a level of mixing of elements.\r\n\\item\r\n  \\texttt{seed}: Seed for the random shuffling within the queue.\r\n\\item\r\n  \\texttt{enqueue\\_many}: Whether each tensor in\r\n  \\texttt{tensor\\_list\\_list} is a single example.\r\n\\item\r\n  \\texttt{shapes}: (Optional) The shapes for each example. Defaults to\r\n  the inferred shapes for \\texttt{tensor\\_list\\_list{[}i{]}}.\r\n\\item\r\n  \\texttt{name}: (Optional) A name for the operations.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-57}\r\n\r\nA list of tensors with the same number and types as\r\n\\texttt{tensor\\_list\\_list{[}i{]}}.\r\n"
  },
  {
    "path": "tex_pdf/api/python/c4s10_python_io.tex",
    "content": "\r\n\r\n\\section{Data IO (Python functions) }\\label{data-io-python-functions}\r\n\r\n\\subsection{Contents}\\label{contents}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-data-io--python-functions-}{Data\r\nIO (Python\r\nfunctions)}}{Data IO (Python functions)}}\\label{data-io-python-functions-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-data-io--python-functions-}{Data IO\r\n  (Python Functions)}\r\n\\item\r\n  \\protect\\hyperlink{TFRecordWriter}{\\texttt{class\\ tf.python\\_io.TFRecordWriter}}\r\n\\item\r\n  \\protect\\hyperlink{tfux5frecordux5fiterator}{\\texttt{tf.python\\_io.tf\\_record\\_iterator(path)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-tfrecords-format-details}{TFRecords\r\n  Format Details}\r\n\\end{itemize}\r\n\r\n\\subsection{Data IO (Python Functions)\r\n}\\label{data-io-python-functions-2}\r\n\r\nA TFRecords file represents a sequence of (binary) strings. The format\r\nis not random access, so it is suitable for streaming large amounts of\r\ndata but not suitable if fast sharding or other non-sequential access is\r\ndesired.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.python\\_io.TFRecordWriter}\r\n}{class tf.python\\_io.TFRecordWriter }}\\label{class-tf.pythonux5fio.tfrecordwriter}\r\n\r\nA class to write records to a TFRecords file.\r\n\r\nThis class implements \\texttt{\\_\\_enter\\_\\_} and \\texttt{\\_\\_exit\\_\\_},\r\nand can be used in \\texttt{with} blocks like a normal file.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.python\\_io.TFRecordWriter.\\_\\_init\\_\\_(path)}\r\n}{tf.python\\_io.TFRecordWriter.\\_\\_init\\_\\_(path) }}\\label{tf.pythonux5fio.tfrecordwriter.ux5fux5finitux5fux5fpath}\r\n\r\nOpens file \\texttt{path} and creates a \\texttt{TFRecordWriter} writing\r\nto it.\r\n\r\n\\subparagraph{Args: }\\label{args}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{path}: The path to the TFRecords file.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{IOError}: If \\texttt{path} cannot be opened for writing.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.python\\_io.TFRecordWriter.write(record)}\r\n}{tf.python\\_io.TFRecordWriter.write(record) }}\\label{tf.pythonux5fio.tfrecordwriter.writerecord}\r\n\r\nWrite a string record to the file.\r\n\r\n\\subparagraph{Args: }\\label{args-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{record}: str\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.python\\_io.TFRecordWriter.close()}\r\n}{tf.python\\_io.TFRecordWriter.close() }}\\label{tf.pythonux5fio.tfrecordwriter.close}\r\n\r\nClose the file.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.python\\_io.tf\\_record\\_iterator(path)}\r\n}{tf.python\\_io.tf\\_record\\_iterator(path) }}\\label{tf.pythonux5fio.tfux5frecordux5fiteratorpath}\r\n\r\nAn iterator that read the records from a TFRecords file.\r\n\r\n\\subparagraph{Args: }\\label{args-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{path}: The path to the TFRecords file.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Yields: }\\label{yields}\r\n\r\nStrings.\r\n\r\n\\subparagraph{Raises: }\\label{raises-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{IOError}: If \\texttt{path} cannot be opened for reading.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{TFRecords Format Details\r\n}\\label{tfrecords-format-details}\r\n\r\nA TFRecords file contains a sequence of strings with CRC hashes. Each\r\nrecord has the format\r\n\r\n\\begin{verbatim}\r\nuint64 length\r\nuint32 masked_crc32_of_length\r\nbyte   data[length]\r\nuint32 masked_crc32_of_data\r\n\\end{verbatim}\r\n\r\nand the records are concatenated together to produce the file. The\r\nCRC32s are\r\n\\href{https://en.wikipedia.org/wiki/Cyclic_redundancy_check}{described\r\nhere}, and the mask of a CRC is\r\n\r\n\\begin{verbatim}\r\nmasked_crc = ((crc >> 15) | (crc << 17)) + 0xa282ead8ul\r\n\\end{verbatim}\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/python/c4s11_nn.tex",
    "content": "\r\n\r\n\\begin{document}\r\n\r\n\\section{Neural Network }\\label{neural-network}\r\n\r\nNote: Functions taking \\texttt{Tensor} arguments can also take anything\r\naccepted by\r\n\\href{../../api_docs/python/framework.md\\#convert_to_tensor}{\\texttt{tf.convert\\_to\\_tensor}}.\r\n\r\n\\subsection{Contents}\\label{contents}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-neural-network}{Neural\r\nNetwork}}{Neural Network}}\\label{neural-network-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-activation-functions}{Activation\r\n  Functions}\r\n\\item\r\n  \\protect\\hyperlink{relu}{\\texttt{tf.nn.relu(features,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{relu6}{\\texttt{tf.nn.relu6(features,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{softplus}{\\texttt{tf.nn.softplus(features,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{dropout}{\\texttt{tf.nn.dropout(x,\\ keep\\_prob,\\ noise\\_shape=None,\\ seed=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{biasux5fadd}{\\texttt{tf.nn.bias\\_add(value,\\ bias,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{sigmoid}{\\texttt{tf.sigmoid(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{tanh}{\\texttt{tf.tanh(x,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-convolution}{Convolution}\r\n\\item\r\n  \\protect\\hyperlink{conv2d}{\\texttt{tf.nn.conv2d(input,\\ filter,\\ strides,\\ padding,\\ use\\_cudnn\\_on\\_gpu=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{depthwiseux5fconv2d}{\\texttt{tf.nn.depthwise\\_conv2d(input,\\ filter,\\ strides,\\ padding,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{separableux5fconv2d}{\\texttt{tf.nn.separable\\_conv2d(input,\\ depthwise\\_filter,\\ pointwise\\_filter,\\ strides,\\ padding,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-pooling}{Pooling}\r\n\\item\r\n  \\protect\\hyperlink{avgux5fpool}{\\texttt{tf.nn.avg\\_pool(value,\\ ksize,\\ strides,\\ padding,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{maxux5fpool}{\\texttt{tf.nn.max\\_pool(value,\\ ksize,\\ strides,\\ padding,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{maxux5fpoolux5fwithux5fargmax}{\\texttt{tf.nn.max\\_pool\\_with\\_argmax(input,\\ ksize,\\ strides,\\ padding,\\ Targmax=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-normalization}{Normalization}\r\n\\item\r\n  \\protect\\hyperlink{l2ux5fnormalize}{\\texttt{tf.nn.l2\\_normalize(x,\\ dim,\\ epsilon=1e-12,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{localux5fresponseux5fnormalization}{\\texttt{tf.nn.local\\_response\\_normalization(input,\\ depth\\_radius=None,\\ bias=None,\\ alpha=None,\\ beta=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{moments}{\\texttt{tf.nn.moments(x,\\ axes,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-losses}{Losses}\r\n\\item\r\n  \\protect\\hyperlink{l2ux5floss}{\\texttt{tf.nn.l2\\_loss(t,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-classification}{Classification}\r\n\\item\r\n  \\protect\\hyperlink{sigmoidux5fcrossux5fentropyux5fwithux5flogits}{\\texttt{tf.nn.sigmoid\\_cross\\_entropy\\_with\\_logits(logits,\\ targets,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{softmax}{\\texttt{tf.nn.softmax(logits,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{softmaxux5fcrossux5fentropyux5fwithux5flogits}{\\texttt{tf.nn.softmax\\_cross\\_entropy\\_with\\_logits(logits,\\ labels,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-embeddings}{Embeddings}\r\n\\item\r\n  \\protect\\hyperlink{embeddingux5flookup}{\\texttt{tf.nn.embedding\\_lookup(params,\\ ids,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-evaluation}{Evaluation}\r\n\\item\r\n  \\protect\\hyperlink{topux5fk}{\\texttt{tf.nn.top\\_k(input,\\ k,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{inux5ftopux5fk}{\\texttt{tf.nn.in\\_top\\_k(predictions,\\ targets,\\ k,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-candidate-sampling}{Candidate\r\n  Sampling}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-sampled-loss-functions}{Sampled Loss\r\n  Functions}\r\n\\item\r\n  \\protect\\hyperlink{nceux5floss}{\\texttt{tf.nn.nce\\_loss(weights,\\ biases,\\ inputs,\\ labels,\\ num\\_sampled,\\ num\\_classes,\\ num\\_true=1,\\ sampled\\_values=None,\\ remove\\_accidental\\_hits=False,\\ name=\\textquotesingle{}nce\\_loss\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{sampledux5fsoftmaxux5floss}{\\texttt{tf.nn.sampled\\_softmax\\_loss(weights,\\ biases,\\ inputs,\\ labels,\\ num\\_sampled,\\ num\\_classes,\\ num\\_true=1,\\ sampled\\_values=None,\\ remove\\_accidental\\_hits=True,\\ name=\\textquotesingle{}sampled\\_softmax\\_loss\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-candidate-samplers}{Candidate\r\n  Samplers}\r\n\\item\r\n  \\protect\\hyperlink{uniformux5fcandidateux5fsampler}{\\texttt{tf.nn.uniform\\_candidate\\_sampler(true\\_classes,\\ num\\_true,\\ num\\_sampled,\\ unique,\\ range\\_max,\\ seed=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{logux5funiformux5fcandidateux5fsampler}{\\texttt{tf.nn.log\\_uniform\\_candidate\\_sampler(true\\_classes,\\ num\\_true,\\ num\\_sampled,\\ unique,\\ range\\_max,\\ seed=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{learnedux5funigramux5fcandidateux5fsampler}{\\texttt{tf.nn.learned\\_unigram\\_candidate\\_sampler(true\\_classes,\\ num\\_true,\\ num\\_sampled,\\ unique,\\ range\\_max,\\ seed=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{fixedux5funigramux5fcandidateux5fsampler}{\\texttt{tf.nn.fixed\\_unigram\\_candidate\\_sampler(true\\_classes,\\ num\\_true,\\ num\\_sampled,\\ unique,\\ range\\_max,\\ vocab\\_file=\\textquotesingle{}\\textquotesingle{},\\ distortion=0.0,\\ num\\_reserved\\_ids=0,\\ num\\_shards=1,\\ shard=0,\\ unigrams={[}{]},\\ seed=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-miscellaneous-candidate-sampling-utilities}{Miscellaneous\r\n  candidate sampling utilities}\r\n\\item\r\n  \\protect\\hyperlink{computeux5faccidentalux5fhits}{\\texttt{tf.nn.compute\\_accidental\\_hits(true\\_classes,\\ sampled\\_candidates,\\ num\\_true,\\ seed=None,\\ name=None)}}\r\n\\end{itemize}\r\n\r\n\\subsection{Activation Functions }\\label{activation-functions}\r\n\r\nThe activation ops provide different types of nonlinearities for use in\r\nneural networks. These include smooth nonlinearities (\\texttt{sigmoid},\r\n\\texttt{tanh}, and \\texttt{softplus}), continuous but not everywhere\r\ndifferentiable functions (\\texttt{relu}, \\texttt{relu6}, and\r\n\\texttt{relu\\_x}), and random regularization (\\texttt{dropout}).\r\n\r\nAll activation ops apply componentwise, and produce a tensor of the same\r\nshape as the input tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.relu(features,\\ name=None)}\r\n}{tf.nn.relu(features, name=None) }}\\label{tf.nn.relufeatures-namenone}\r\n\r\nComputes rectified linear: \\texttt{max(features,\\ 0)}.\r\n\r\n\\subparagraph{Args: }\\label{args}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{features}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{int64}, \\texttt{uint8}, \\texttt{int16}, \\texttt{int8}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{features}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.relu6(features,\\ name=None)}\r\n}{tf.nn.relu6(features, name=None) }}\\label{tf.nn.relu6features-namenone}\r\n\r\nComputes Rectified Linear 6: \\texttt{min(max(features,\\ 0),\\ 6)}.\r\n\r\n\\subparagraph{Args: }\\label{args-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{features}: A \\texttt{Tensor} with type \\texttt{float},\r\n  \\texttt{double}, \\texttt{int32}, \\texttt{int64}, \\texttt{uint8},\r\n  \\texttt{int16}, or \\texttt{int8}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-1}\r\n\r\nA \\texttt{Tensor} with the same type as \\texttt{features}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.softplus(features,\\ name=None)}\r\n}{tf.nn.softplus(features, name=None) }}\\label{tf.nn.softplusfeatures-namenone}\r\n\r\nComputes softplus: \\texttt{log(exp(features)\\ +\\ 1)}.\r\n\r\n\\subparagraph{Args: }\\label{args-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{features}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{float32}, \\texttt{float64}, \\texttt{int32},\r\n  \\texttt{int64}, \\texttt{uint8}, \\texttt{int16}, \\texttt{int8}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-2}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{features}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.dropout(x,\\ keep\\_prob,\\ noise\\_shape=None,\\ seed=None,\\ name=None)}\r\n}{tf.nn.dropout(x, keep\\_prob, noise\\_shape=None, seed=None, name=None) }}\\label{tf.nn.dropoutx-keepux5fprob-noiseux5fshapenone-seednone-namenone}\r\n\r\nComputes dropout.\r\n\r\nWith probability \\texttt{keep\\_prob}, outputs the input element scaled\r\nup by \\texttt{1\\ /\\ keep\\_prob}, otherwise outputs \\texttt{0}. The\r\nscaling is so that the expected sum is unchanged.\r\n\r\nBy default, each element is kept or dropped independently. If\r\n\\texttt{noise\\_shape} is specified, it must be\r\n\\href{http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html}{broadcastable}\r\nto the shape of \\texttt{x}, and only dimensions with\r\n\\texttt{noise\\_shape{[}i{]}\\ ==\\ shape(x){[}i{]}} will make independent\r\ndecisions. For example, if \\texttt{shape(x)\\ =\\ {[}k,\\ l,\\ m,\\ n{]}} and\r\n\\texttt{noise\\_shape\\ =\\ {[}k,\\ 1,\\ 1,\\ n{]}}, each batch and channel\r\ncomponent will be kept independently and each row and column will be\r\nkept or not kept together.\r\n\r\n\\subparagraph{Args: }\\label{args-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A tensor.\r\n\\item\r\n  \\texttt{keep\\_prob}: A Python float. The probability that each element\r\n  is kept.\r\n\\item\r\n  \\texttt{noise\\_shape}: A 1-D \\texttt{Tensor} of type \\texttt{int32},\r\n  representing the shape for randomly generated keep/drop flags.\r\n\\item\r\n  \\texttt{seed}: A Python integer. Used to create random seeds. See\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\texttt{set\\_random\\_seed}}\r\n  for behavior.\r\n\\item\r\n  \\texttt{name}: A name for this operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-3}\r\n\r\nA Tensor of the same shape of \\texttt{x}.\r\n\r\n\\subparagraph{Raises: }\\label{raises}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: If \\texttt{keep\\_prob} is not in\r\n  \\texttt{(0,\\ 1{]}}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.bias\\_add(value,\\ bias,\\ name=None)}\r\n}{tf.nn.bias\\_add(value, bias, name=None) }}\\label{tf.nn.biasux5faddvalue-bias-namenone}\r\n\r\nAdds \\texttt{bias} to \\texttt{value}.\r\n\r\nThis is (mostly) a special case of \\texttt{tf.add} where \\texttt{bias}\r\nis restricted to 1-D. Broadcasting is supported, so \\texttt{value} may\r\nhave any number of dimensions. Unlike \\texttt{tf.add}, the type of\r\n\\texttt{bias} is allowed to differ from \\texttt{value} in the case where\r\nboth types are quantized.\r\n\r\n\\subparagraph{Args: }\\label{args-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{value}: A \\texttt{Tensor} with type \\texttt{float},\r\n  \\texttt{double}, \\texttt{int64}, \\texttt{int32}, \\texttt{uint8},\r\n  \\texttt{int16}, \\texttt{int8}, or \\texttt{complex64}.\r\n\\item\r\n  \\texttt{bias}: A 1-D \\texttt{Tensor} with size matching the last\r\n  dimension of \\texttt{value}. Must be the same type as \\texttt{value}\r\n  unless \\texttt{value} is a quantized type, in which case a different\r\n  quantized type may be used.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-4}\r\n\r\nA \\texttt{Tensor} with the same type as \\texttt{value}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.sigmoid(x,\\ name=None)}\r\n}{tf.sigmoid(x, name=None) }}\\label{tf.sigmoidx-namenone}\r\n\r\nComputes sigmoid of \\texttt{x} element-wise.\r\n\r\nSpecifically, \\texttt{y\\ =\\ 1\\ /\\ (1\\ +\\ exp(-x))}.\r\n\r\n\\subparagraph{Args: }\\label{args-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A Tensor with type \\texttt{float}, \\texttt{double},\r\n  \\texttt{int32}, \\texttt{complex64}, \\texttt{int64}, or\r\n  \\texttt{qint32}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-5}\r\n\r\nA Tensor with the same type as \\texttt{x} if\r\n\\texttt{x.dtype\\ !=\\ qint32} otherwise the return type is\r\n\\texttt{quint8}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.tanh(x,\\ name=None)}\r\n}{tf.tanh(x, name=None) }}\\label{tf.tanhx-namenone}\r\n\r\nComputes hyperbolic tangent of \\texttt{x} element-wise.\r\n\r\n\\subparagraph{Args: }\\label{args-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A Tensor with type \\texttt{float}, \\texttt{double},\r\n  \\texttt{int32}, \\texttt{complex64}, \\texttt{int64}, or\r\n  \\texttt{qint32}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-6}\r\n\r\nA Tensor with the same type as \\texttt{x} if\r\n\\texttt{x.dtype\\ !=\\ qint32} otherwise the return type is\r\n\\texttt{quint8}.\r\n\r\n\\subsection{Convolution }\\label{convolution}\r\n\r\nThe convolution ops sweep a 2-D filter over a batch of images, applying\r\nthe filter to each window of each image of the appropriate size. The\r\ndifferent ops trade off between generic vs.~specific filters:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{conv2d}: Arbitrary filters that can mix channels together.\r\n\\item\r\n  \\texttt{depthwise\\_conv2d}: Filters that operate on each channel\r\n  independently.\r\n\\item\r\n  \\texttt{separable\\_conv2d}: A depthwise spatial filter followed by a\r\n  pointwise filter.\r\n\\end{itemize}\r\n\r\nNote that although these ops are called ``convolution'', they are\r\nstrictly speaking ``cross-correlation'' since the filter is combined\r\nwith an input window without reversing the filter. For details, see\r\n\\href{https://en.wikipedia.org/wiki/Cross-correlation\\#Properties}{the\r\nproperties of cross-correlation}.\r\n\r\nThe filter is applied to image patches of the same size as the filter\r\nand strided according to the \\texttt{strides} argument.\r\n\\texttt{strides\\ =\\ {[}1,\\ 1,\\ 1,\\ 1{]}} applies the filter to a patch\r\nat every offset, \\texttt{strides\\ =\\ {[}1,\\ 2,\\ 2,\\ 1{]}} applies the\r\nfilter to every other image patch in each dimension, etc.\r\n\r\nIgnoring channels for the moment, the spatial semantics of the\r\nconvolution ops are as follows. If the 4-D \\texttt{input} has shape\r\n\\texttt{{[}batch,\\ in\\_height,\\ in\\_width,\\ ...{]}} and the 4-D\r\n\\texttt{filter} has shape\r\n\\texttt{{[}filter\\_height,\\ filter\\_width,\\ ...{]}}, then\r\n\r\n\\begin{verbatim}\r\nshape(output) = [batch,\r\n                 (in_height - filter_height + 1) / strides[1],\r\n                 (in_width - filter_width + 1) / strides[2],\r\n                 ...]\r\n\r\noutput[b, i, j, :] =\r\n    sum_{di, dj} input[b, strides[1] * i + di, strides[2] * j + dj, ...] *\r\n                 filter[di, dj, ...]\r\n\\end{verbatim}\r\n\r\nSince \\texttt{input} is 4-D, each \\texttt{input{[}b,\\ i,\\ j,\\ :{]}} is a\r\nvector. For \\texttt{conv2d}, these vectors are multiplied by the\r\n\\texttt{filter{[}di,\\ dj,\\ :,\\ :{]}} matrices to produce new vectors.\r\nFor \\texttt{depthwise\\_conv\\_2d}, each scalar component\r\n\\texttt{input{[}b,\\ i,\\ j,\\ k{]}} is multiplied by a vector\r\n\\texttt{filter{[}di,\\ dj,\\ k{]}}, and all the vectors are concatenated.\r\n\r\nIn the formula for \\texttt{shape(output)}, the rounding direction\r\ndepends on padding:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{padding\\ =\\ \\textquotesingle{}SAME\\textquotesingle{}}: Round\r\n  down (only full size windows are considered).\r\n\\item\r\n  \\texttt{padding\\ =\\ \\textquotesingle{}VALID\\textquotesingle{}}: Round\r\n  up (partial windows are included).\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.conv2d(input,\\ filter,\\ strides,\\ padding,\\ use\\_cudnn\\_on\\_gpu=None,\\ name=None)}\r\n}{tf.nn.conv2d(input, filter, strides, padding, use\\_cudnn\\_on\\_gpu=None, name=None) }}\\label{tf.nn.conv2dinput-filter-strides-padding-useux5fcudnnux5fonux5fgpunone-namenone}\r\n\r\nComputes a 2-D convolution given 4-D \\texttt{input} and \\texttt{filter}\r\ntensors.\r\n\r\nGiven an input tensor of shape\r\n\\texttt{{[}batch,\\ in\\_height,\\ in\\_width,\\ in\\_channels{]}} and a\r\nfilter / kernel tensor of shape\r\n\\texttt{{[}filter\\_height,\\ filter\\_width,\\ in\\_channels,\\ out\\_channels{]}},\r\nthis op performs the following:\r\n\r\n\\begin{enumerate}\r\n\\def\\labelenumi{\\arabic{enumi}.}\r\n\\tightlist\r\n\\item\r\n  Flattens the filter to a 2-D matrix with shape\r\n  \\texttt{{[}filter\\_height\\ *\\ filter\\_width\\ *\\ in\\_channels,\\ output\\_channels{]}}.\r\n\\item\r\n  Extracts image patches from the the input tensor to form a\r\n  \\emph{virtual} tensor of shape\r\n  \\texttt{{[}batch,\\ out\\_height,\\ out\\_width,\\ \\ \\ \\ filter\\_height\\ *\\ filter\\_width\\ *\\ in\\_channels{]}}.\r\n\\item\r\n  For each patch, right-multiplies the filter matrix and the image patch\r\n  vector.\r\n\\end{enumerate}\r\n\r\nIn detail,\r\n\r\n\\begin{verbatim}\r\noutput[b, i, j, k] =\r\n    sum_{di, dj, q} input[b, strides[1] * i + di, strides[2] * j + dj, q] *\r\n                    filter[di, dj, q, k]\r\n\\end{verbatim}\r\n\r\nMust have \\texttt{strides{[}0{]}\\ =\\ strides{[}3{]}\\ =\\ 1}. For the most\r\ncommon case of the same horizontal and vertices strides,\r\n\\texttt{strides\\ =\\ {[}1,\\ stride,\\ stride,\\ 1{]}}.\r\n\r\n\\subparagraph{Args: }\\label{args-7}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}.\r\n\\item\r\n  \\texttt{filter}: A \\texttt{Tensor}. Must have the same type as\r\n  \\texttt{input}.\r\n\\item\r\n  \\texttt{strides}: A list of \\texttt{ints}. 1-D of length 4. The stride\r\n  of the sliding window for each dimension of \\texttt{input}.\r\n\\item\r\n  \\texttt{padding}: A \\texttt{string} from: \\texttt{\"SAME\",\\ \"VALID\"}.\r\n  The type of padding algorithm to use.\r\n\\item\r\n  \\texttt{use\\_cudnn\\_on\\_gpu}: An optional \\texttt{bool}. Defaults to\r\n  \\texttt{True}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-7}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{input}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.depthwise\\_conv2d(input,\\ filter,\\ strides,\\ padding,\\ name=None)}\r\n}{tf.nn.depthwise\\_conv2d(input, filter, strides, padding, name=None) }}\\label{tf.nn.depthwiseux5fconv2dinput-filter-strides-padding-namenone}\r\n\r\nDepthwise 2-D convolution.\r\n\r\nGiven an input tensor of shape\r\n\\texttt{{[}batch,\\ in\\_height,\\ in\\_width,\\ in\\_channels{]}} and a\r\nfilter tensor of shape\r\n\\texttt{{[}filter\\_height,\\ filter\\_width,\\ in\\_channels,\\ channel\\_multiplier{]}}\r\ncontaining \\texttt{in\\_channels} convolutional filters of depth 1,\r\n\\texttt{depthwise\\_conv2d} applies a different filter to each input\r\nchannel (expanding from 1 channel to \\texttt{channel\\_multiplier}\r\nchannels for each), then concatenates the results together. The output\r\nhas \\texttt{in\\_channels\\ *\\ channel\\_multiplier} channels.\r\n\r\nIn detail,\r\n\r\n\\begin{verbatim}\r\noutput[b, i, j, k * channel_multiplier + q] =\r\n    sum_{di, dj} input[b, strides[1] * i + di, strides[2] * j + dj, k] *\r\n                 filter[di, dj, k, q]\r\n\\end{verbatim}\r\n\r\nMust have \\texttt{strides{[}0{]}\\ =\\ strides{[}3{]}\\ =\\ 1}. For the most\r\ncommon case of the same horizontal and vertical strides,\r\n\\texttt{strides\\ =\\ {[}1,\\ stride,\\ stride,\\ 1{]}}.\r\n\r\n\\subparagraph{Args: }\\label{args-8}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: 4-D with shape\r\n  \\texttt{{[}batch,\\ in\\_height,\\ in\\_width,\\ in\\_channels{]}}.\r\n\\item\r\n  \\texttt{filter}: 4-D with shape\r\n  \\texttt{{[}filter\\_height,\\ filter\\_width,\\ in\\_channels,\\ channel\\_multiplier{]}}.\r\n\\item\r\n  \\texttt{strides}: 1-D of size 4. The stride of the sliding window for\r\n  each dimension of \\texttt{input}.\r\n\\item\r\n  \\texttt{padding}: A string, either\r\n  \\texttt{\\textquotesingle{}VALID\\textquotesingle{}} or\r\n  \\texttt{\\textquotesingle{}SAME\\textquotesingle{}}. The padding\r\n  algorithm.\r\n\\item\r\n  \\texttt{name}: A name for this operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-8}\r\n\r\nA 4-D \\texttt{Tensor} of shape\r\n\\texttt{{[}batch,\\ out\\_height,\\ out\\_width,\\ in\\_channels\\ *\\ channel\\_multiplier{]}.}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.separable\\_conv2d(input,\\ depthwise\\_filter,\\ pointwise\\_filter,\\ strides,\\ padding,\\ name=None)}\r\n}{tf.nn.separable\\_conv2d(input, depthwise\\_filter, pointwise\\_filter, strides, padding, name=None) }}\\label{tf.nn.separableux5fconv2dinput-depthwiseux5ffilter-pointwiseux5ffilter-strides-padding-namenone}\r\n\r\n2-D convolution with separable filters.\r\n\r\nPerforms a depthwise convolution that acts separately on channels\r\nfollowed by a pointwise convolution that mixes channels. Note that this\r\nis separability between dimensions \\texttt{{[}1,\\ 2{]}} and \\texttt{3},\r\nnot spatial separability between dimensions \\texttt{1} and \\texttt{2}.\r\n\r\nIn detail,\r\n\r\n\\begin{verbatim}\r\noutput[b, i, j, k] = sum_{di, dj, q, r]\r\n    input[b, strides[1] * i + di, strides[2] * j + dj, q] *\r\n    depthwise_filter[di, dj, q, r] *\r\n    pointwise_filter[0, 0, q * channel_multiplier + r, k]\r\n\\end{verbatim}\r\n\r\n\\texttt{strides} controls the strides for the depthwise convolution\r\nonly, since the pointwise convolution has implicit strides of\r\n\\texttt{{[}1,\\ 1,\\ 1,\\ 1{]}}. Must have\r\n\\texttt{strides{[}0{]}\\ =\\ strides{[}3{]}\\ =\\ 1}. For the most common\r\ncase of the same horizontal and vertical strides,\r\n\\texttt{strides\\ =\\ {[}1,\\ stride,\\ stride,\\ 1{]}}.\r\n\r\n\\subparagraph{Args: }\\label{args-9}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: 4-D \\texttt{Tensor} with shape\r\n  \\texttt{{[}batch,\\ in\\_height,\\ in\\_width,\\ in\\_channels{]}}.\r\n\\item\r\n  \\texttt{depthwise\\_filter}: 4-D \\texttt{Tensor} with shape\r\n  \\texttt{{[}filter\\_height,\\ filter\\_width,\\ in\\_channels,\\ channel\\_multiplier{]}}.\r\n  Contains \\texttt{in\\_channels} convolutional filters of depth 1.\r\n\\item\r\n  \\texttt{pointwise\\_filter}: 4-D \\texttt{Tensor} with shape\r\n  \\texttt{{[}1,\\ 1,\\ channel\\_multiplier\\ *\\ in\\_channels,\\ out\\_channels{]}}.\r\n  Pointwise filter to mix channels after \\texttt{depthwise\\_filter} has\r\n  convolved spatially.\r\n\\item\r\n  \\texttt{strides}: 1-D of size 4. The strides for the depthwise\r\n  convolution for each dimension of \\texttt{input}.\r\n\\item\r\n  \\texttt{padding}: A string, either\r\n  \\texttt{\\textquotesingle{}VALID\\textquotesingle{}} or\r\n  \\texttt{\\textquotesingle{}SAME\\textquotesingle{}}. The padding\r\n  algorithm.\r\n\\item\r\n  \\texttt{name}: A name for this operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-9}\r\n\r\nA 4-D \\texttt{Tensor} of shape\r\n\\texttt{{[}batch,\\ out\\_height,\\ out\\_width,\\ out\\_channels{]}}.\r\n\r\n\\subsection{Pooling }\\label{pooling}\r\n\r\nThe pooling ops sweep a rectangular window over the input tensor,\r\ncomputing a reduction operation for each window (average, max, or max\r\nwith argmax). Each pooling op uses rectangular windows of size\r\n\\texttt{ksize} separated by offset \\texttt{strides}. For example, if\r\n\\texttt{strides} is all ones every window is used, if \\texttt{strides}\r\nis all twos every other window is used in each dimension, etc.\r\n\r\nIn detail, the output is\r\n\r\n\\begin{verbatim}\r\noutput[i] = reduce(value[strides * i:strides * i + ksize])\r\n\\end{verbatim}\r\n\r\nfor each tuple of indices \\texttt{i}. The output shape is\r\n\r\n\\begin{verbatim}\r\nshape(output) = (shape(value) - ksize + 1) / strides\r\n\\end{verbatim}\r\n\r\nwhere the rounding direction depends on padding:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{padding\\ =\\ \\textquotesingle{}SAME\\textquotesingle{}}: Round\r\n  down (only full size windows are considered).\r\n\\item\r\n  \\texttt{padding\\ =\\ \\textquotesingle{}VALID\\textquotesingle{}}: Round\r\n  up (partial windows are included).\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.avg\\_pool(value,\\ ksize,\\ strides,\\ padding,\\ name=None)}\r\n}{tf.nn.avg\\_pool(value, ksize, strides, padding, name=None) }}\\label{tf.nn.avgux5fpoolvalue-ksize-strides-padding-namenone}\r\n\r\nPerforms the average pooling on the input.\r\n\r\nEach entry in \\texttt{output} is the mean of the corresponding size\r\n\\texttt{ksize} window in \\texttt{value}.\r\n\r\n\\subparagraph{Args: }\\label{args-10}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{value}: A 4-D \\texttt{Tensor} of shape\r\n  \\texttt{{[}batch,\\ height,\\ width,\\ channels{]}} and type\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{qint8}, \\texttt{quint8},\r\n  or \\texttt{qint32}.\r\n\\item\r\n  \\texttt{ksize}: A list of ints that has length \\textgreater{}= 4. The\r\n  size of the window for each dimension of the input tensor.\r\n\\item\r\n  \\texttt{strides}: A list of ints that has length \\textgreater{}= 4.\r\n  The stride of the sliding window for each dimension of the input\r\n  tensor.\r\n\\item\r\n  \\texttt{padding}: A string, either\r\n  \\texttt{\\textquotesingle{}VALID\\textquotesingle{}} or\r\n  \\texttt{\\textquotesingle{}SAME\\textquotesingle{}}. The padding\r\n  algorithm.\r\n\\item\r\n  \\texttt{name}: Optional name for the operation.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-10}\r\n\r\nA \\texttt{Tensor} with the same type as \\texttt{value}. The average\r\npooled output tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.max\\_pool(value,\\ ksize,\\ strides,\\ padding,\\ name=None)}\r\n}{tf.nn.max\\_pool(value, ksize, strides, padding, name=None) }}\\label{tf.nn.maxux5fpoolvalue-ksize-strides-padding-namenone}\r\n\r\nPerforms the max pooling on the input.\r\n\r\n\\subparagraph{Args: }\\label{args-11}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{value}: A 4-D \\texttt{Tensor} with shape\r\n  \\texttt{{[}batch,\\ height,\\ width,\\ channels{]}} and type\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{qint8}, \\texttt{quint8},\r\n  \\texttt{qint32}.\r\n\\item\r\n  \\texttt{ksize}: A list of ints that has length \\textgreater{}= 4. The\r\n  size of the window for each dimension of the input tensor.\r\n\\item\r\n  \\texttt{strides}: A list of ints that has length \\textgreater{}= 4.\r\n  The stride of the sliding window for each dimension of the input\r\n  tensor.\r\n\\item\r\n  \\texttt{padding}: A string, either\r\n  \\texttt{\\textquotesingle{}VALID\\textquotesingle{}} or\r\n  \\texttt{\\textquotesingle{}SAME\\textquotesingle{}}. The padding\r\n  algorithm.\r\n\\item\r\n  \\texttt{name}: Optional name for the operation.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-11}\r\n\r\nA \\texttt{Tensor} with the same type as \\texttt{value}. The max pooled\r\noutput tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.max\\_pool\\_with\\_argmax(input,\\ ksize,\\ strides,\\ padding,\\ Targmax=None,\\ name=None)}\r\n}{tf.nn.max\\_pool\\_with\\_argmax(input, ksize, strides, padding, Targmax=None, name=None) }}\\label{tf.nn.maxux5fpoolux5fwithux5fargmaxinput-ksize-strides-padding-targmaxnone-namenone}\r\n\r\nPerforms max pooling on the input and outputs both max values and\r\nindices.\r\n\r\nThe indices in \\texttt{argmax} are flattened, so that a maximum value at\r\nposition \\texttt{{[}b,\\ y,\\ x,\\ c{]}} becomes flattened index\r\n\\texttt{((b\\ *\\ height\\ +\\ y)\\ *\\ width\\ +\\ x)\\ *\\ channels\\ +\\ c}.\r\n\r\n\\subparagraph{Args: }\\label{args-12}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor} of type \\texttt{float32}. 4-D with\r\n  shape \\texttt{{[}batch,\\ height,\\ width,\\ channels{]}}. Input to pool\r\n  over.\r\n\\item\r\n  \\texttt{ksize}: A list of \\texttt{ints} that has length\r\n  \\texttt{\\textgreater{}=\\ 4}. The size of the window for each dimension\r\n  of the input tensor.\r\n\\item\r\n  \\texttt{strides}: A list of \\texttt{ints} that has length\r\n  \\texttt{\\textgreater{}=\\ 4}. The stride of the sliding window for each\r\n  dimension of the input tensor.\r\n\\item\r\n  \\texttt{padding}: A \\texttt{string} from: \\texttt{\"SAME\",\\ \"VALID\"}.\r\n  The type of padding algorithm to use.\r\n\\item\r\n  \\texttt{Targmax}: An optional \\texttt{tf.DType} from:\r\n  \\texttt{tf.int32,\\ tf.int64}. Defaults to \\texttt{tf.int64}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-12}\r\n\r\nA tuple of \\texttt{Tensor} objects (output, argmax).\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{output}: A \\texttt{Tensor} of type \\texttt{float32}. The max\r\n  pooled output tensor.\r\n\\item\r\n  \\texttt{argmax}: A \\texttt{Tensor} of type \\texttt{Targmax}. 4-D. The\r\n  flattened indices of the max values chosen for each output.\r\n\\end{itemize}\r\n\r\n\\subsection{Normalization }\\label{normalization}\r\n\r\nNormalization is useful to prevent neurons from saturating when inputs\r\nmay have varying scale, and to aid generalization.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.l2\\_normalize(x,\\ dim,\\ epsilon=1e-12,\\ name=None)}\r\n}{tf.nn.l2\\_normalize(x, dim, epsilon=1e-12, name=None) }}\\label{tf.nn.l2ux5fnormalizex-dim-epsilon1e-12-namenone}\r\n\r\nNormalizes along dimension \\texttt{dim} using an L2 norm.\r\n\r\nFor a 1-D tensor with \\texttt{dim\\ =\\ 0}, computes\r\n\r\n\\begin{verbatim}\r\noutput = x / sqrt(max(sum(x**2), epsilon))\r\n\\end{verbatim}\r\n\r\nFor \\texttt{x} with more dimensions, independently normalizes each 1-D\r\nslice along dimension \\texttt{dim}.\r\n\r\n\\subparagraph{Args: }\\label{args-13}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}.\r\n\\item\r\n  \\texttt{dim}: Dimension along which to normalize.\r\n\\item\r\n  \\texttt{epsilon}: A lower bound value for the norm. Will use\r\n  \\texttt{sqrt(epsilon)} as the divisor if\r\n  \\texttt{norm\\ \\textless{}\\ sqrt(epsilon)}.\r\n\\item\r\n  \\texttt{name}: A name for this operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-13}\r\n\r\nA \\texttt{Tensor} with the same shape as \\texttt{x}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.local\\_response\\_normalization(input,\\ depth\\_radius=None,\\ bias=None,\\ alpha=None,\\ beta=None,\\ name=None)}\r\n}{tf.nn.local\\_response\\_normalization(input, depth\\_radius=None, bias=None, alpha=None, beta=None, name=None) }}\\label{tf.nn.localux5fresponseux5fnormalizationinput-depthux5fradiusnone-biasnone-alphanone-betanone-namenone}\r\n\r\nLocal Response Normalization.\r\n\r\nThe 4-D \\texttt{input} tensor is treated as a 3-D array of 1-D vectors\r\n(along the last dimension), and each vector is normalized independently.\r\nWithin a given vector, each component is divided by the weighted,\r\nsquared sum of inputs within \\texttt{depth\\_radius}. In detail,\r\n\r\n\\begin{verbatim}\r\nsqr_sum[a, b, c, d] =\r\n    sum(input[a, b, c, d - depth_radius : d + depth_radius + 1] ** 2)\r\noutput = input / (bias + alpha * sqr_sum ** beta)\r\n\\end{verbatim}\r\n\r\nFor details, see {[}Krizhevsky et al., ImageNet classification with deep\r\nconvolutional neural networks (NIPS 2012){]}\r\n(http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks).\r\n\r\n\\subparagraph{Args: }\\label{args-14}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor} of type \\texttt{float32}. 4-D.\r\n\\item\r\n  \\texttt{depth\\_radius}: An optional \\texttt{int}. Defaults to\r\n  \\texttt{5}. 0-D. Half-width of the 1-D normalization window.\r\n\\item\r\n  \\texttt{bias}: An optional \\texttt{float}. Defaults to \\texttt{1}. An\r\n  offset (usually positive to avoid dividing by 0).\r\n\\item\r\n  \\texttt{alpha}: An optional \\texttt{float}. Defaults to \\texttt{1}. A\r\n  scale factor, usually positive.\r\n\\item\r\n  \\texttt{beta}: An optional \\texttt{float}. Defaults to \\texttt{0.5}.\r\n  An exponent.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-14}\r\n\r\nA \\texttt{Tensor} of type \\texttt{float32}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.moments(x,\\ axes,\\ name=None)}\r\n}{tf.nn.moments(x, axes, name=None) }}\\label{tf.nn.momentsx-axes-namenone}\r\n\r\nCalculate the mean and variance of \\texttt{x}.\r\n\r\nThe mean and variance are calculated by aggregating the contents of\r\n\\texttt{x} across \\texttt{axes}. If \\texttt{x} is 1-D and\r\n\\texttt{axes\\ =\\ {[}0{]}} this is just the mean and variance of a\r\nvector.\r\n\r\nFor so-called ``global normalization'' needed for convolutional filters\r\npass \\texttt{axes={[}0,\\ 1,\\ 2{]}} (batch, height, width). For batch\r\nnormalization pass \\texttt{axes={[}0{]}} (batch).\r\n\r\n\\subparagraph{Args: }\\label{args-15}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{x}: A \\texttt{Tensor}.\r\n\\item\r\n  \\texttt{axes}: array of ints. Axes along which to compute mean and\r\n  variance.\r\n\\item\r\n  \\texttt{name}: Name used to scope the operations that compute the\r\n  moments.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-15}\r\n\r\nTwo \\texttt{Tensors}: \\texttt{mean} and \\texttt{variance}.\r\n\r\n\\subsection{Losses }\\label{losses}\r\n\r\nThe loss ops measure error between two tensors, or between a tensor and\r\nzero. These can be used for measuring accuracy of a network in a\r\nregression task or for regularization purposes (weight decay).\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.l2\\_loss(t,\\ name=None)}\r\n}{tf.nn.l2\\_loss(t, name=None) }}\\label{tf.nn.l2ux5flosst-namenone}\r\n\r\nL2 Loss.\r\n\r\nComputes half the L2 norm of a tensor without the \\texttt{sqrt}:\r\n\r\n\\begin{verbatim}\r\noutput = sum(t ** 2) / 2\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-16}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{t}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int64}, \\texttt{int32},\r\n  \\texttt{uint8}, \\texttt{int16}, \\texttt{int8}, \\texttt{complex64},\r\n  \\texttt{qint8}, \\texttt{quint8}, \\texttt{qint32}. Typically 2-D, but\r\n  may have any dimensions.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-16}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{t}. 0-D.\r\n\r\n\\subsection{Classification }\\label{classification}\r\n\r\nTensorFlow provides several operations that help you perform\r\nclassification.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.sigmoid\\_cross\\_entropy\\_with\\_logits(logits,\\ targets,\\ name=None)}\r\n}{tf.nn.sigmoid\\_cross\\_entropy\\_with\\_logits(logits, targets, name=None) }}\\label{tf.nn.sigmoidux5fcrossux5fentropyux5fwithux5flogitslogits-targets-namenone}\r\n\r\nComputes sigmoid cross entropy given \\texttt{logits}.\r\n\r\nMeasures the probability error in discrete classification tasks in which\r\neach class is independent and not mutually exclusive. For instance, one\r\ncould perform multilabel classification where a picture can contain both\r\nan elephant and a dog at the same time.\r\n\r\nFor brevity, let \\texttt{x\\ =\\ logits}, \\texttt{z\\ =\\ targets}. The\r\nlogistic loss is\r\n\r\n\\begin{verbatim}\r\nx - x * z + log(1 + exp(-x))\r\n\\end{verbatim}\r\n\r\nTo ensure stability and avoid overflow, the implementation uses\r\n\r\n\\begin{verbatim}\r\nmax(x, 0) - x * z + log(1 + exp(-abs(x)))\r\n\\end{verbatim}\r\n\r\n\\texttt{logits} and \\texttt{targets} must have the same type and shape.\r\n\r\n\\subparagraph{Args: }\\label{args-17}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{logits}: A \\texttt{Tensor} of type \\texttt{float32} or\r\n  \\texttt{float64}.\r\n\\item\r\n  \\texttt{targets}: A \\texttt{Tensor} of the same type and shape as\r\n  \\texttt{logits}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-17}\r\n\r\nA \\texttt{Tensor} of the same shape as \\texttt{logits} with the\r\ncomponentwise logistic losses.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.softmax(logits,\\ name=None)}\r\n}{tf.nn.softmax(logits, name=None) }}\\label{tf.nn.softmaxlogits-namenone}\r\n\r\nComputes softmax activations.\r\n\r\nFor each batch \\texttt{i} and class \\texttt{j} we have\r\n\r\n\\begin{verbatim}\r\nsoftmax[i, j] = exp(logits[i, j]) / sum(exp(logits[i]))\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-18}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{logits}: A \\texttt{Tensor}. Must be one of the following\r\n  types: \\texttt{float32}, \\texttt{float64}. 2-D with shape\r\n  \\texttt{{[}batch\\_size,\\ num\\_classes{]}}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-18}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{logits}. Same shape as\r\n\\texttt{logits}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.softmax\\_cross\\_entropy\\_with\\_logits(logits,\\ labels,\\ name=None)}\r\n}{tf.nn.softmax\\_cross\\_entropy\\_with\\_logits(logits, labels, name=None) }}\\label{tf.nn.softmaxux5fcrossux5fentropyux5fwithux5flogitslogits-labels-namenone}\r\n\r\nComputes softmax cross entropy between \\texttt{logits} and\r\n\\texttt{labels}.\r\n\r\nMeasures the probability error in discrete classification tasks in which\r\nthe classes are mutually exclusive (each entry is in exactly one class).\r\nFor example, each CIFAR-10 image is labeled with one and only one label:\r\nan image can be a dog or a truck, but not both.\r\n\r\n\\textbf{WARNING:} This op expects unscaled logits, since it performs a\r\n\\texttt{softmax} on \\texttt{logits} internally for efficiency. Do not\r\ncall this op with the output of \\texttt{softmax}, as it will produce\r\nincorrect results.\r\n\r\n\\texttt{logits} and \\texttt{labels} must have the same shape\r\n\\texttt{{[}batch\\_size,\\ num\\_classes{]}} and the same dtype (either\r\n\\texttt{float32} or \\texttt{float64}).\r\n\r\n\\subparagraph{Args: }\\label{args-19}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{logits}: Unscaled log probabilities.\r\n\\item\r\n  \\texttt{labels}: Each row \\texttt{labels{[}i{]}} must be a valid\r\n  probability distribution.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-19}\r\n\r\nA 1-D \\texttt{Tensor} of length \\texttt{batch\\_size} of the same type as\r\n\\texttt{logits} with the softmax cross entropy loss.\r\n\r\n\\subsection{Embeddings }\\label{embeddings}\r\n\r\nTensorFlow provides library support for looking up values in embedding\r\ntensors.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.embedding\\_lookup(params,\\ ids,\\ name=None)}\r\n}{tf.nn.embedding\\_lookup(params, ids, name=None) }}\\label{tf.nn.embeddingux5flookupparams-ids-namenone}\r\n\r\nLooks up \\texttt{ids} in a list of embedding tensors.\r\n\r\nThis function is used to perform parallel lookups on the list of tensors\r\nin \\texttt{params}. It is a generalization of\r\n\\href{../../api_docs/python/array_ops.md\\#gather}{\\texttt{tf.gather()}},\r\nwhere \\texttt{params} is interpreted as a partition of a larger\r\nembedding tensor.\r\n\r\nIf \\texttt{len(params)\\ \\textgreater{}\\ 1}, each element \\texttt{id} of\r\n\\texttt{ids} is partitioned between the elements of \\texttt{params} by\r\ncomputing \\texttt{p\\ =\\ id\\ \\%\\ len(params)}, and is then used to look\r\nup the slice \\texttt{params{[}p{]}{[}id\\ //\\ len(params),\\ ...{]}}.\r\n\r\nThe results of the lookup are then concatenated into a dense tensor. The\r\nreturned tensor has shape \\texttt{shape(ids)\\ +\\ shape(params){[}1:{]}}.\r\n\r\n\\subparagraph{Args: }\\label{args-20}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{params}: A list of tensors with the same shape and type.\r\n\\item\r\n  \\texttt{ids}: A \\texttt{Tensor} with type \\texttt{int32} containing\r\n  the ids to be looked up in \\texttt{params}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-20}\r\n\r\nA \\texttt{Tensor} with the same type as the tensors in \\texttt{params}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: If \\texttt{params} is empty.\r\n\\end{itemize}\r\n\r\n\\subsection{Evaluation }\\label{evaluation}\r\n\r\nThe evaluation ops are useful for measuring the performance of a\r\nnetwork. Since they are nondifferentiable, they are typically used at\r\nevaluation time.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.top\\_k(input,\\ k,\\ name=None)}\r\n}{tf.nn.top\\_k(input, k, name=None) }}\\label{tf.nn.topux5fkinput-k-namenone}\r\n\r\nReturns the values and indices of the k largest elements for each row.\r\n\r\n\\textbackslash{}(values\\_\\{i, j\\}\\textbackslash{}) represents the j-th\r\nlargest element in \\textbackslash{}(input\\_i\\textbackslash{}).\r\n\r\n\\textbackslash{}(indices\\_\\{i, j\\}\\textbackslash{}) gives the column\r\nindex of the corresponding element, such that\r\n\\textbackslash{}(input\\_\\{i, indices\\_\\{i, j\\}\\} = values\\_\\{i,\r\nj\\}\\textbackslash{}). If two elements are equal, the lower-index element\r\nappears first.\r\n\r\n\\subparagraph{Args: }\\label{args-21}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor}. Must be one of the following types:\r\n  \\texttt{float32}, \\texttt{float64}, \\texttt{int32}, \\texttt{int64},\r\n  \\texttt{uint8}, \\texttt{int16}, \\texttt{int8}. A batch\\_size x classes\r\n  tensor\r\n\\item\r\n  \\texttt{k}: An \\texttt{int} that is \\texttt{\\textgreater{}=\\ 1}.\r\n  Number of top elements to look for within each row\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-21}\r\n\r\nA tuple of \\texttt{Tensor} objects (values, indices).\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{values}: A \\texttt{Tensor}. Has the same type as\r\n  \\texttt{input}. A batch\\_size x k tensor with the k largest elements\r\n  for each row, sorted in descending order\r\n\\item\r\n  \\texttt{indices}: A \\texttt{Tensor} of type \\texttt{int32}. A\r\n  batch\\_size x k tensor with the index of each value within each row\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.in\\_top\\_k(predictions,\\ targets,\\ k,\\ name=None)}\r\n}{tf.nn.in\\_top\\_k(predictions, targets, k, name=None) }}\\label{tf.nn.inux5ftopux5fkpredictions-targets-k-namenone}\r\n\r\nSays whether the targets are in the top K predictions.\r\n\r\nThis outputs a batch\\_size bool array, an entry out{[}i{]} is true if\r\nthe prediction for the target class is among the top k predictions among\r\nall predictions for example i. Note that the behavior of InTopK differs\r\nfrom the TopK op in its handling of ties; if multiple classes have the\r\nsame prediction value and straddle the top-k boundary, all of those\r\nclasses are considered to be in the top k.\r\n\r\nMore formally, let\r\n\r\n\\textbackslash{}(predictions\\_i\\textbackslash{}) be the predictions for\r\nall classes for example i, \\textbackslash{}(targets\\_i\\textbackslash{})\r\nbe the target class for example i,\r\n\\textbackslash{}(out\\_i\\textbackslash{}) be the output for example i,\r\n\r\n\\[out_i = predictions_{i, targets_i} \\in TopKIncludingTies(predictions_i)\\]\r\n\r\n\\subparagraph{Args: }\\label{args-22}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{predictions}: A \\texttt{Tensor} of type \\texttt{float32}. A\r\n  batch\\_size x classes tensor\r\n\\item\r\n  \\texttt{targets}: A \\texttt{Tensor} of type \\texttt{int32}. A\r\n  batch\\_size vector of class ids\r\n\\item\r\n  \\texttt{k}: An \\texttt{int}. Number of top elements to look at for\r\n  computing precision\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-22}\r\n\r\nA \\texttt{Tensor} of type \\texttt{bool}. Computed Precision at k as a\r\nbool Tensor\r\n\r\n\\subsection{Candidate Sampling }\\label{candidate-sampling}\r\n\r\nDo you want to train a multiclass or multilabel model with thousands or\r\nmillions of output classes (for example, a language model with a large\r\nvocabulary)? Training with a full Softmax is slow in this case, since\r\nall of the classes are evaluated for every training example. Candidate\r\nSampling training algorithms can speed up your step times by only\r\nconsidering a small randomly-chosen subset of contrastive classes\r\n(called candidates) for each batch of training examples.\r\n\r\nSee our {[}Candidate Sampling Algorithms Reference{]}\r\n(../../extras/candidate\\_sampling.pdf)\r\n\r\n\\subsubsection{Sampled Loss Functions }\\label{sampled-loss-functions}\r\n\r\nTensorFlow provides the following sampled loss functions for faster\r\ntraining.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.nce\\_loss(weights,\\ biases,\\ inputs,\\ labels,\\ num\\_sampled,\\ num\\_classes,\\ num\\_true=1,\\ sampled\\_values=None,\\ remove\\_accidental\\_hits=False,\\ name=\\textquotesingle{}nce\\_loss\\textquotesingle{})}\r\n}{tf.nn.nce\\_loss(weights, biases, inputs, labels, num\\_sampled, num\\_classes, num\\_true=1, sampled\\_values=None, remove\\_accidental\\_hits=False, name='nce\\_loss') }}\\label{tf.nn.nceux5flossweights-biases-inputs-labels-numux5fsampled-numux5fclasses-numux5ftrue1-sampledux5fvaluesnone-removeux5faccidentalux5fhitsfalse-namenceux5floss}\r\n\r\nComputes and returns the noise-contrastive estimation training loss.\r\n\r\nSee {[}Noise-contrastive estimation: A new estimation principle for\r\nunnormalized statistical models{]}\r\n(http://www.jmlr.org/proceedings/papers/v9/gutmann10a/gutmann10a.pdf).\r\nAlso see our {[}Candidate Sampling Algorithms Reference{]}\r\n(http://www.tensorflow.org/extras/candidate\\_sampling.pdf)\r\n\r\nNote: In the case where num\\_true \\textgreater{} 1, we assign to each\r\ntarget class the target probability 1 / num\\_true so that the target\r\nprobabilities sum to 1 per-example.\r\n\r\nNote: It would be useful to allow a variable number of target classes\r\nper example. We hope to provide this functionality in a future release.\r\nFor now, if you have a variable number of target classes, you can pad\r\nthem out to a constant number by either repeating them or by padding\r\nwith an otherwise unused class.\r\n\r\n\\subparagraph{Args: }\\label{args-23}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{weights}: A \\texttt{Tensor} of shape {[}num\\_classes, dim{]}.\r\n  The class embeddings.\r\n\\item\r\n  \\texttt{biases}: A \\texttt{Tensor} of shape {[}num\\_classes{]}. The\r\n  class biases.\r\n\\item\r\n  \\texttt{inputs}: A \\texttt{Tensor} of shape {[}batch\\_size, dim{]}.\r\n  The forward activations of the input network.\r\n\\item\r\n  \\texttt{labels}: A \\texttt{Tensor} of type \\texttt{int64} and shape\r\n  \\texttt{{[}batch\\_size,\\ num\\_true{]}}. The target classes.\r\n\\item\r\n  \\texttt{num\\_sampled}: An \\texttt{int}. The number of classes to\r\n  randomly sample per batch.\r\n\\item\r\n  \\texttt{num\\_classes}: An \\texttt{int}. The number of possible\r\n  classes.\r\n\\item\r\n  \\texttt{num\\_true}: An \\texttt{int}. The number of target classes per\r\n  training example.\r\n\\item\r\n  \\texttt{sampled\\_values}: a tuple of\r\n  \\texttt{(sampled\\_candidates,\\ true\\_expected\\_count,\\ \\ \\ sampled\\_expected\\_count)}\r\n  returned by a *\\_candidate\\_sampler function. (if None, we default to\r\n  LogUniformCandidateSampler)\r\n\\item\r\n  \\texttt{remove\\_accidental\\_hits}: A \\texttt{bool}. Whether to remove\r\n  ``accidental hits'' where a sampled class equals one of the target\r\n  classes. If set to \\texttt{True}, this is a ``Sampled Logistic'' loss\r\n  instead of NCE, and we are learning to generate log-odds instead of\r\n  log probabilities. See our {[}Candidate Sampling Algorithms\r\n  Reference{]}\r\n  (http://www.tensorflow.org/extras/candidate\\_sampling.pdf). Default is\r\n  False.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-23}\r\n\r\nA batch\\_size 1-D tensor of per-example NCE losses.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.sampled\\_softmax\\_loss(weights,\\ biases,\\ inputs,\\ labels,\\ num\\_sampled,\\ num\\_classes,\\ num\\_true=1,\\ sampled\\_values=None,\\ remove\\_accidental\\_hits=True,\\ name=\\textquotesingle{}sampled\\_softmax\\_loss\\textquotesingle{})}\r\n}{tf.nn.sampled\\_softmax\\_loss(weights, biases, inputs, labels, num\\_sampled, num\\_classes, num\\_true=1, sampled\\_values=None, remove\\_accidental\\_hits=True, name='sampled\\_softmax\\_loss') }}\\label{tf.nn.sampledux5fsoftmaxux5flossweights-biases-inputs-labels-numux5fsampled-numux5fclasses-numux5ftrue1-sampledux5fvaluesnone-removeux5faccidentalux5fhitstrue-namesampledux5fsoftmaxux5floss}\r\n\r\nComputes and returns the sampled softmax training loss.\r\n\r\nThis is a faster way to train a softmax classifier over a huge number of\r\nclasses.\r\n\r\nThis operation is for training only. It is generally an underestimate of\r\nthe full softmax loss.\r\n\r\nAt inference time, you can compute full softmax probabilities with the\r\nexpression\r\n\\texttt{tf.nn.softmax(tf.matmul(inputs,\\ weights)\\ +\\ biases)}.\r\n\r\nSee our {[}Candidate Sampling Algorithms Reference{]}\r\n(http://www.tensorflow.org/extras/candidate\\_sampling.pdf)\r\n\r\nAlso see Section 3 of http://arxiv.org/abs/1412.2007 for the math.\r\n\r\n\\subparagraph{Args: }\\label{args-24}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{weights}: A \\texttt{Tensor} of shape {[}num\\_classes, dim{]}.\r\n  The class embeddings.\r\n\\item\r\n  \\texttt{biases}: A \\texttt{Tensor} of shape {[}num\\_classes{]}. The\r\n  class biases.\r\n\\item\r\n  \\texttt{inputs}: A \\texttt{Tensor} of shape {[}batch\\_size, dim{]}.\r\n  The forward activations of the input network.\r\n\\item\r\n  \\texttt{labels}: A \\texttt{Tensor} of type \\texttt{int64} and shape\r\n  \\texttt{{[}batch\\_size,\\ num\\_true{]}}. The target classes. Note that\r\n  this format differs from the \\texttt{labels} argument of\r\n  \\texttt{nn.softmax\\_cross\\_entropy\\_with\\_logits}.\r\n\\item\r\n  \\texttt{num\\_sampled}: An \\texttt{int}. The number of classes to\r\n  randomly sample per batch.\r\n\\item\r\n  \\texttt{num\\_classes}: An \\texttt{int}. The number of possible\r\n  classes.\r\n\\item\r\n  \\texttt{num\\_true}: An \\texttt{int}. The number of target classes per\r\n  training example.\r\n\\item\r\n  \\texttt{sampled\\_values}: a tuple of\r\n  \\texttt{(sampled\\_candidates,\\ true\\_expected\\_count,\\ \\ \\ sampled\\_expected\\_count)}\r\n  returned by a *\\_candidate\\_sampler function. (if None, we default to\r\n  LogUniformCandidateSampler)\r\n\\item\r\n  \\texttt{remove\\_accidental\\_hits}: A \\texttt{bool}. whether to remove\r\n  ``accidental hits'' where a sampled class equals one of the target\r\n  classes. Default is True.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-24}\r\n\r\nA batch\\_size 1-D tensor of per-example sampled softmax losses.\r\n\r\n\\subsubsection{Candidate Samplers }\\label{candidate-samplers}\r\n\r\nTensorFlow provides the following samplers for randomly sampling\r\ncandidate classes when using one of the sampled loss functions above.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.uniform\\_candidate\\_sampler(true\\_classes,\\ num\\_true,\\ num\\_sampled,\\ unique,\\ range\\_max,\\ seed=None,\\ name=None)}\r\n}{tf.nn.uniform\\_candidate\\_sampler(true\\_classes, num\\_true, num\\_sampled, unique, range\\_max, seed=None, name=None) }}\\label{tf.nn.uniformux5fcandidateux5fsamplertrueux5fclasses-numux5ftrue-numux5fsampled-unique-rangeux5fmax-seednone-namenone}\r\n\r\nSamples a set of classes using a uniform base distribution.\r\n\r\nThis operation randomly samples a tensor of sampled classes\r\n(\\texttt{sampled\\_candidates}) from the range of integers\r\n\\texttt{{[}0,\\ range\\_max{]}}.\r\n\r\nThe elements of \\texttt{sampled\\_candidates} are drawn without\r\nreplacement (if \\texttt{unique=True}) or with replacement (if\r\n\\texttt{unique=False}) from the base distribution.\r\n\r\nThe base distribution for this operation is the uniform distribution\r\nover the range of integers \\texttt{{[}0,\\ range\\_max{]}}.\r\n\r\nIn addition, this operation returns tensors\r\n\\texttt{true\\_expected\\_count} and \\texttt{sampled\\_expected\\_count}\r\nrepresenting the number of times each of the target classes\r\n(\\texttt{true\\_classes}) and the sampled classes\r\n(\\texttt{sampled\\_candidates}) is expected to occur in an average tensor\r\nof sampled classes. These values correspond to \\texttt{Q(y\\textbar{}x)}\r\ndefined in\r\n\\href{http://www.tensorflow.org/extras/candidate_sampling.pdf}{this\r\ndocument}. If \\texttt{unique=True}, then these are post-rejection\r\nprobabilities and we compute them approximately.\r\n\r\n\\subparagraph{Args: }\\label{args-25}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{true\\_classes}: A \\texttt{Tensor} of type \\texttt{int64} and\r\n  shape \\texttt{{[}batch\\_size,\\ num\\_true{]}}. The target classes.\r\n\\item\r\n  \\texttt{num\\_true}: An \\texttt{int}. The number of target classes per\r\n  training example.\r\n\\item\r\n  \\texttt{num\\_sampled}: An \\texttt{int}. The number of classes to\r\n  randomly sample per batch.\r\n\\item\r\n  \\texttt{unique}: A \\texttt{bool}. Determines whether all sampled\r\n  classes in a batch are unique.\r\n\\item\r\n  \\texttt{range\\_max}: An \\texttt{int}. The number of possible classes.\r\n\\item\r\n  \\texttt{seed}: An \\texttt{int}. An operation-specific seed. Default is\r\n  0.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-25}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{sampled\\_candidates}: A tensor of type \\texttt{int64} and\r\n  shape \\texttt{{[}num\\_sampled{]}}. The sampled classes.\r\n\\item\r\n  \\texttt{true\\_expected\\_count}: A tensor of type \\texttt{float}. Same\r\n  shape as \\texttt{true\\_classes}. The expected counts under the\r\n  sampling distribution of each of \\texttt{true\\_classes}.\r\n\\item\r\n  \\texttt{sampled\\_expected\\_count}: A tensor of type \\texttt{float}.\r\n  Same shape as \\texttt{sampled\\_candidates}. The expected counts under\r\n  the sampling distribution of each of \\texttt{sampled\\_candidates}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.log\\_uniform\\_candidate\\_sampler(true\\_classes,\\ num\\_true,\\ num\\_sampled,\\ unique,\\ range\\_max,\\ seed=None,\\ name=None)}\r\n}{tf.nn.log\\_uniform\\_candidate\\_sampler(true\\_classes, num\\_true, num\\_sampled, unique, range\\_max, seed=None, name=None) }}\\label{tf.nn.logux5funiformux5fcandidateux5fsamplertrueux5fclasses-numux5ftrue-numux5fsampled-unique-rangeux5fmax-seednone-namenone}\r\n\r\nSamples a set of classes using a log-uniform (Zipfian) base\r\ndistribution.\r\n\r\nThis operation randomly samples a tensor of sampled classes\r\n(\\texttt{sampled\\_candidates}) from the range of integers\r\n\\texttt{{[}0,\\ range\\_max{]}}.\r\n\r\nThe elements of \\texttt{sampled\\_candidates} are drawn without\r\nreplacement (if \\texttt{unique=True}) or with replacement (if\r\n\\texttt{unique=False}) from the base distribution.\r\n\r\nThe base distribution for this operation is an approximately log-uniform\r\nor Zipfian distribution:\r\n\r\n\\texttt{P(class)\\ =\\ (log(class\\ +\\ 2)\\ -\\ log(class\\ +\\ 1))\\ /\\ log(range\\_max\\ +\\ 1)}\r\n\r\nThis sampler is useful when the target classes approximately follow such\r\na distribution - for example, if the classes represent words in a\r\nlexicon sorted in decreasing order of frequency. If your classes are not\r\nordered by decreasing frequency, do not use this op.\r\n\r\nIn addition, this operation returns tensors\r\n\\texttt{true\\_expected\\_count} and \\texttt{sampled\\_expected\\_count}\r\nrepresenting the number of times each of the target classes\r\n(\\texttt{true\\_classes}) and the sampled classes\r\n(\\texttt{sampled\\_candidates}) is expected to occur in an average tensor\r\nof sampled classes. These values correspond to \\texttt{Q(y\\textbar{}x)}\r\ndefined in\r\n\\href{http://www.tensorflow.org/extras/candidate_sampling.pdf}{this\r\ndocument}. If \\texttt{unique=True}, then these are post-rejection\r\nprobabilities and we compute them approximately.\r\n\r\n\\subparagraph{Args: }\\label{args-26}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{true\\_classes}: A \\texttt{Tensor} of type \\texttt{int64} and\r\n  shape \\texttt{{[}batch\\_size,\\ num\\_true{]}}. The target classes.\r\n\\item\r\n  \\texttt{num\\_true}: An \\texttt{int}. The number of target classes per\r\n  training example.\r\n\\item\r\n  \\texttt{num\\_sampled}: An \\texttt{int}. The number of classes to\r\n  randomly sample per batch.\r\n\\item\r\n  \\texttt{unique}: A \\texttt{bool}. Determines whether all sampled\r\n  classes in a batch are unique.\r\n\\item\r\n  \\texttt{range\\_max}: An \\texttt{int}. The number of possible classes.\r\n\\item\r\n  \\texttt{seed}: An \\texttt{int}. An operation-specific seed. Default is\r\n  0.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-26}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{sampled\\_candidates}: A tensor of type \\texttt{int64} and\r\n  shape \\texttt{{[}num\\_sampled{]}}. The sampled classes.\r\n\\item\r\n  \\texttt{true\\_expected\\_count}: A tensor of type \\texttt{float}. Same\r\n  shape as \\texttt{true\\_classes}. The expected counts under the\r\n  sampling distribution of each of \\texttt{true\\_classes}.\r\n\\item\r\n  \\texttt{sampled\\_expected\\_count}: A tensor of type \\texttt{float}.\r\n  Same shape as \\texttt{sampled\\_candidates}. The expected counts under\r\n  the sampling distribution of each of \\texttt{sampled\\_candidates}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.learned\\_unigram\\_candidate\\_sampler(true\\_classes,\\ num\\_true,\\ num\\_sampled,\\ unique,\\ range\\_max,\\ seed=None,\\ name=None)}\r\n}{tf.nn.learned\\_unigram\\_candidate\\_sampler(true\\_classes, num\\_true, num\\_sampled, unique, range\\_max, seed=None, name=None) }}\\label{tf.nn.learnedux5funigramux5fcandidateux5fsamplertrueux5fclasses-numux5ftrue-numux5fsampled-unique-rangeux5fmax-seednone-namenone}\r\n\r\nSamples a set of classes from a distribution learned during training.\r\n\r\nThis operation randomly samples a tensor of sampled classes\r\n(\\texttt{sampled\\_candidates}) from the range of integers\r\n\\texttt{{[}0,\\ range\\_max{]}}.\r\n\r\nThe elements of \\texttt{sampled\\_candidates} are drawn without\r\nreplacement (if \\texttt{unique=True}) or with replacement (if\r\n\\texttt{unique=False}) from the base distribution.\r\n\r\nThe base distribution for this operation is constructed on the fly\r\nduring training. It is a unigram distribution over the target classes\r\nseen so far during training. Every integer in\r\n\\texttt{{[}0,\\ range\\_max{]}} begins with a weight of 1, and is\r\nincremented by 1 each time it is seen as a target class. The base\r\ndistribution is not saved to checkpoints, so it is reset when the model\r\nis reloaded.\r\n\r\nIn addition, this operation returns tensors\r\n\\texttt{true\\_expected\\_count} and \\texttt{sampled\\_expected\\_count}\r\nrepresenting the number of times each of the target classes\r\n(\\texttt{true\\_classes}) and the sampled classes\r\n(\\texttt{sampled\\_candidates}) is expected to occur in an average tensor\r\nof sampled classes. These values correspond to \\texttt{Q(y\\textbar{}x)}\r\ndefined in\r\n\\href{http://www.tensorflow.org/extras/candidate_sampling.pdf}{this\r\ndocument}. If \\texttt{unique=True}, then these are post-rejection\r\nprobabilities and we compute them approximately.\r\n\r\n\\subparagraph{Args: }\\label{args-27}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{true\\_classes}: A \\texttt{Tensor} of type \\texttt{int64} and\r\n  shape \\texttt{{[}batch\\_size,\\ num\\_true{]}}. The target classes.\r\n\\item\r\n  \\texttt{num\\_true}: An \\texttt{int}. The number of target classes per\r\n  training example.\r\n\\item\r\n  \\texttt{num\\_sampled}: An \\texttt{int}. The number of classes to\r\n  randomly sample per batch.\r\n\\item\r\n  \\texttt{unique}: A \\texttt{bool}. Determines whether all sampled\r\n  classes in a batch are unique.\r\n\\item\r\n  \\texttt{range\\_max}: An \\texttt{int}. The number of possible classes.\r\n\\item\r\n  \\texttt{seed}: An \\texttt{int}. An operation-specific seed. Default is\r\n  0.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-27}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{sampled\\_candidates}: A tensor of type \\texttt{int64} and\r\n  shape \\texttt{{[}num\\_sampled{]}}. The sampled classes.\r\n\\item\r\n  \\texttt{true\\_expected\\_count}: A tensor of type \\texttt{float}. Same\r\n  shape as \\texttt{true\\_classes}. The expected counts under the\r\n  sampling distribution of each of \\texttt{true\\_classes}.\r\n\\item\r\n  \\texttt{sampled\\_expected\\_count}: A tensor of type \\texttt{float}.\r\n  Same shape as \\texttt{sampled\\_candidates}. The expected counts under\r\n  the sampling distribution of each of \\texttt{sampled\\_candidates}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.fixed\\_unigram\\_candidate\\_sampler(true\\_classes,\\ num\\_true,\\ num\\_sampled,\\ unique,\\ range\\_max,\\ vocab\\_file=\\textquotesingle{}\\textquotesingle{},\\ distortion=0.0,\\ num\\_reserved\\_ids=0,\\ num\\_shards=1,\\ shard=0,\\ unigrams={[}{]},\\ seed=None,\\ name=None)}\r\n}{tf.nn.fixed\\_unigram\\_candidate\\_sampler(true\\_classes, num\\_true, num\\_sampled, unique, range\\_max, vocab\\_file='', distortion=0.0, num\\_reserved\\_ids=0, num\\_shards=1, shard=0, unigrams={[}{]}, seed=None, name=None) }}\\label{tf.nn.fixedux5funigramux5fcandidateux5fsamplertrueux5fclasses-numux5ftrue-numux5fsampled-unique-rangeux5fmax-vocabux5ffile-distortion0.0-numux5freservedux5fids0-numux5fshards1-shard0-unigrams-seednone-namenone}\r\n\r\nSamples a set of classes using the provided (fixed) base distribution.\r\n\r\nThis operation randomly samples a tensor of sampled classes\r\n(\\texttt{sampled\\_candidates}) from the range of integers\r\n\\texttt{{[}0,\\ range\\_max{]}}.\r\n\r\nThe elements of \\texttt{sampled\\_candidates} are drawn without\r\nreplacement (if \\texttt{unique=True}) or with replacement (if\r\n\\texttt{unique=False}) from the base distribution.\r\n\r\nThe base distribution is read from a file or passed in as an in-memory\r\narray. There is also an option to skew the distribution by applying a\r\ndistortion power to the weights.\r\n\r\nIn addition, this operation returns tensors\r\n\\texttt{true\\_expected\\_count} and \\texttt{sampled\\_expected\\_count}\r\nrepresenting the number of times each of the target classes\r\n(\\texttt{true\\_classes}) and the sampled classes\r\n(\\texttt{sampled\\_candidates}) is expected to occur in an average tensor\r\nof sampled classes. These values correspond to \\texttt{Q(y\\textbar{}x)}\r\ndefined in\r\n\\href{http://www.tensorflow.org/extras/candidate_sampling.pdf}{this\r\ndocument}. If \\texttt{unique=True}, then these are post-rejection\r\nprobabilities and we compute them approximately.\r\n\r\n\\subparagraph{Args: }\\label{args-28}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{true\\_classes}: A \\texttt{Tensor} of type \\texttt{int64} and\r\n  shape \\texttt{{[}batch\\_size,\\ num\\_true{]}}. The target classes.\r\n\\item\r\n  \\texttt{num\\_true}: An \\texttt{int}. The number of target classes per\r\n  training example.\r\n\\item\r\n  \\texttt{num\\_sampled}: An \\texttt{int}. The number of classes to\r\n  randomly sample per batch.\r\n\\item\r\n  \\texttt{unique}: A \\texttt{bool}. Determines whether all sampled\r\n  classes in a batch are unique.\r\n\\item\r\n  \\texttt{range\\_max}: An \\texttt{int}. The number of possible classes.\r\n\\item\r\n  \\texttt{vocab\\_file}: Each valid line in this file (which should have\r\n  a CSV-like format) corresponds to a valid word ID. IDs are in\r\n  sequential order, starting from num\\_reserved\\_ids. The last entry in\r\n  each line is expected to be a value corresponding to the count or\r\n  relative probability. Exactly one of \\texttt{vocab\\_file} and\r\n  \\texttt{unigrams} needs to be passed to this operation.\r\n\\item\r\n  \\texttt{distortion}: The distortion is used to skew the unigram\r\n  probability distribution. Each weight is first raised to the\r\n  distortion's power before adding to the internal unigram distribution.\r\n  As a result, \\texttt{distortion\\ =\\ 1.0} gives regular unigram\r\n  sampling (as defined by the vocab file), and\r\n  \\texttt{distortion\\ =\\ 0.0} gives a uniform distribution.\r\n\\item\r\n  \\texttt{num\\_reserved\\_ids}: Optionally some reserved IDs can be added\r\n  in the range \\texttt{{[}0,\\ num\\_reserved\\_ids{]}} by the users. One\r\n  use case is that a special unknown word token is used as ID 0. These\r\n  IDs will have a sampling probability of 0.\r\n\\item\r\n  \\texttt{num\\_shards}: A sampler can be used to sample from a subset of\r\n  the original range in order to speed up the whole computation through\r\n  parallelism. This parameter (together with \\texttt{shard}) indicates\r\n  the number of partitions that are being used in the overall\r\n  computation.\r\n\\item\r\n  \\texttt{shard}: A sampler can be used to sample from a subset of the\r\n  original range in order to speed up the whole computation through\r\n  parallelism. This parameter (together with \\texttt{num\\_shards})\r\n  indicates the particular partition number of the operation, when\r\n  partitioning is being used.\r\n\\item\r\n  \\texttt{unigrams}: A list of unigram counts or probabilities, one per\r\n  ID in sequential order. Exactly one of \\texttt{vocab\\_file} and\r\n  \\texttt{unigrams} should be passed to this operation.\r\n\\item\r\n  \\texttt{seed}: An \\texttt{int}. An operation-specific seed. Default is\r\n  0.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-28}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{sampled\\_candidates}: A tensor of type \\texttt{int64} and\r\n  shape \\texttt{{[}num\\_sampled{]}}. The sampled classes.\r\n\\item\r\n  \\texttt{true\\_expected\\_count}: A tensor of type \\texttt{float}. Same\r\n  shape as \\texttt{true\\_classes}. The expected counts under the\r\n  sampling distribution of each of \\texttt{true\\_classes}.\r\n\\item\r\n  \\texttt{sampled\\_expected\\_count}: A tensor of type \\texttt{float}.\r\n  Same shape as \\texttt{sampled\\_candidates}. The expected counts under\r\n  the sampling distribution of each of \\texttt{sampled\\_candidates}.\r\n\\end{itemize}\r\n\r\n\\subsubsection{Miscellaneous candidate sampling utilities\r\n}\\label{miscellaneous-candidate-sampling-utilities}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.compute\\_accidental\\_hits(true\\_classes,\\ sampled\\_candidates,\\ num\\_true,\\ seed=None,\\ name=None)}\r\n}{tf.nn.compute\\_accidental\\_hits(true\\_classes, sampled\\_candidates, num\\_true, seed=None, name=None) }}\\label{tf.nn.computeux5faccidentalux5fhitstrueux5fclasses-sampledux5fcandidates-numux5ftrue-seednone-namenone}\r\n\r\nCompute the ids of positions in sampled\\_candidates matching\r\ntrue\\_classes.\r\n\r\nIn Candidate Sampling, this operation facilitates virtually removing\r\nsampled classes which happen to match target classes. This is done in\r\nSampled Softmax and Sampled Logistic.\r\n\r\nSee our\r\n\\href{http://www.tensorflow.org/extras/candidate_sampling.pdf}{Candidate\r\nSampling Algorithms Reference}.\r\n\r\nWe presuppose that the \\texttt{sampled\\_candidates} are unique.\r\n\r\nWe call it an `accidental hit' when one of the target classes matches\r\none of the sampled classes. This operation reports accidental hits as\r\ntriples \\texttt{(index,\\ id,\\ weight)}, where \\texttt{index} represents\r\nthe row number in \\texttt{true\\_classes}, \\texttt{id} represents the\r\nposition in \\texttt{sampled\\_candidates}, and weight is\r\n\\texttt{-FLOAT\\_MAX}.\r\n\r\nThe result of this op should be passed through a\r\n\\texttt{sparse\\_to\\_dense} operation, then added to the logits of the\r\nsampled classes. This removes the contradictory effect of accidentally\r\nsampling the true target classes as noise classes for the same example.\r\n\r\n\\subparagraph{Args: }\\label{args-29}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{true\\_classes}: A \\texttt{Tensor} of type \\texttt{int64} and\r\n  shape \\texttt{{[}batch\\_size,\\ num\\_true{]}}. The target classes.\r\n\\item\r\n  \\texttt{sampled\\_candidates}: A tensor of type \\texttt{int64} and\r\n  shape \\texttt{{[}num\\_sampled{]}}. The sampled\\_candidates output of\r\n  CandidateSampler.\r\n\\item\r\n  \\texttt{num\\_true}: An \\texttt{int}. The number of target classes per\r\n  training example.\r\n\\item\r\n  \\texttt{seed}: An \\texttt{int}. An operation-specific seed. Default is\r\n  0.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-29}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{indices}: A \\texttt{Tensor} of type \\texttt{int32} and shape\r\n  \\texttt{{[}num\\_accidental\\_hits{]}}. Values indicate rows in\r\n  \\texttt{true\\_classes}.\r\n\\item\r\n  \\texttt{ids}: A \\texttt{Tensor} of type \\texttt{int64} and shape\r\n  \\texttt{{[}num\\_accidental\\_hits{]}}. Values indicate positions in\r\n  \\texttt{sampled\\_candidates}.\r\n\\item\r\n  \\texttt{weights}: A \\texttt{Tensor} of type \\texttt{float} and shape\r\n  \\texttt{{[}num\\_accidental\\_hits{]}}. Each value is\r\n  \\texttt{-FLOAT\\_MAX}.\r\n\\end{itemize}\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/api/python/c4s12_client.tex",
    "content": "\r\n\r\n\\section{Running Graphs }\\label{running-graphs}\r\n\r\n\\subsection{Contents}\\label{contents}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-running-graphs}{Running\r\nGraphs}}{Running Graphs}}\\label{running-graphs-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-session-management}{Session\r\n  management}\r\n\\item\r\n  \\protect\\hyperlink{Session}{\\texttt{class\\ tf.Session}}\r\n\\item\r\n  \\protect\\hyperlink{InteractiveSession}{\\texttt{class\\ tf.InteractiveSession}}\r\n\\item\r\n  \\protect\\hyperlink{getux5fdefaultux5fsession}{\\texttt{tf.get\\_default\\_session()}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-error-classes}{Error classes}\r\n\\item\r\n  \\protect\\hyperlink{OpError}{\\texttt{class\\ tf.OpError}}\r\n\\item\r\n  \\protect\\hyperlink{CancelledError}{\\texttt{class\\ tf.errors.CancelledError}}\r\n\\item\r\n  \\protect\\hyperlink{UnknownError}{\\texttt{class\\ tf.errors.UnknownError}}\r\n\\item\r\n  \\protect\\hyperlink{InvalidArgumentError}{\\texttt{class\\ tf.errors.InvalidArgumentError}}\r\n\\item\r\n  \\protect\\hyperlink{DeadlineExceededError}{\\texttt{class\\ tf.errors.DeadlineExceededError}}\r\n\\item\r\n  \\protect\\hyperlink{NotFoundError}{\\texttt{class\\ tf.errors.NotFoundError}}\r\n\\item\r\n  \\protect\\hyperlink{AlreadyExistsError}{\\texttt{class\\ tf.errors.AlreadyExistsError}}\r\n\\item\r\n  \\protect\\hyperlink{PermissionDeniedError}{\\texttt{class\\ tf.errors.PermissionDeniedError}}\r\n\\item\r\n  \\protect\\hyperlink{UnauthenticatedError}{\\texttt{class\\ tf.errors.UnauthenticatedError}}\r\n\\item\r\n  \\protect\\hyperlink{ResourceExhaustedError}{\\texttt{class\\ tf.errors.ResourceExhaustedError}}\r\n\\item\r\n  \\protect\\hyperlink{FailedPreconditionError}{\\texttt{class\\ tf.errors.FailedPreconditionError}}\r\n\\item\r\n  \\protect\\hyperlink{AbortedError}{\\texttt{class\\ tf.errors.AbortedError}}\r\n\\item\r\n  \\protect\\hyperlink{OutOfRangeError}{\\texttt{class\\ tf.errors.OutOfRangeError}}\r\n\\item\r\n  \\protect\\hyperlink{UnimplementedError}{\\texttt{class\\ tf.errors.UnimplementedError}}\r\n\\item\r\n  \\protect\\hyperlink{InternalError}{\\texttt{class\\ tf.errors.InternalError}}\r\n\\item\r\n  \\protect\\hyperlink{UnavailableError}{\\texttt{class\\ tf.errors.UnavailableError}}\r\n\\item\r\n  \\protect\\hyperlink{DataLossError}{\\texttt{class\\ tf.errors.DataLossError}}\r\n\\end{itemize}\r\n\r\nThis library contains classes for launching graphs and executing\r\noperations.\r\n\r\nThe \\href{../../get_started/introduction.md\\#basic-usage}{basic usage}\r\nguide has examples of how a graph is launched in a\r\n\\protect\\hyperlink{Session}{\\texttt{tf.Session}}.\r\n\r\n\\subsection{Session management }\\label{session-management}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{class \\lstinline{tf.Session}} \\label{class-tf.session}\r\n\r\nA class for running TensorFlow operations.\r\n\r\nA \\texttt{Session} object encapsulates the environment in which\r\n\\texttt{Operation} objects are executed, and \\texttt{Tensor} objects are\r\nevaluated. For example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Build a graph.}\r\n\\NormalTok{a }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{5.0}\\NormalTok{)}\r\n\\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{6.0}\\NormalTok{)}\r\n\\NormalTok{c }\\OperatorTok{=} \\NormalTok{a }\\OperatorTok{*} \\NormalTok{b}\r\n\r\n\\CommentTok{# Launch the graph in a session.}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session()}\r\n\r\n\\CommentTok{# Evaluate the tensor `c`.}\r\n\\BuiltInTok{print} \\NormalTok{sess.run(c)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nA session may own resources, such as\r\n\\href{../../api_docs/python/state_ops.md\\#Variable}{variables},\r\n\\href{../../api_docs/python/io_ops.md\\#QueueBase}{queues}, and\r\n\\href{../../api_docs/python/io_ops.md\\#ReaderBase}{readers}. It is\r\nimportant to release these resources when they are no longer required.\r\nTo do this, either invoke the\r\n\\protect\\hyperlink{Session.close}{\\texttt{close()}} method on the\r\nsession, or use the session as a context manager. The following two\r\nexamples are equivalent:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Using the `close()` method.}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session()}\r\n\\NormalTok{sess.run(...)}\r\n\\NormalTok{sess.close()}\r\n\r\n\\CommentTok{# Using the context manager.}\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session() }\\ImportTok{as} \\NormalTok{sess:}\r\n  \\NormalTok{sess.run(...)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nThe {[}\\texttt{ConfigProto}{]}\r\n(https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/config.proto)\r\nprotocol buffer exposes various configuration options for a session. For\r\nexample, to create a session that uses soft constraints for device\r\nplacement, and log the resulting placement decisions, create a session\r\nas follows:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Launch the graph in a session that allows soft device placement and}\r\n\\CommentTok{# logs the placement decisions.}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session(config}\\OperatorTok{=}\\NormalTok{tf.ConfigProto(allow_soft_placement}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{,}\r\n                                        \\NormalTok{log_device_placement}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{))}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.Session.\\_\\_init\\_\\_(target=\\textquotesingle{}\\textquotesingle{},\\ graph=None,\\ config=None)}\r\n}{tf.Session.\\_\\_init\\_\\_(target='', graph=None, config=None) }}\\label{tf.session.ux5fux5finitux5fux5ftarget-graphnone-confignone}\r\n\r\nCreates a new TensorFlow session.\r\n\r\nIf no \\texttt{graph} argument is specified when constructing the\r\nsession, the default graph will be launched in the session. If you are\r\nusing more than one graph (created with \\texttt{tf.Graph()} in the same\r\nprocess, you will have to use different sessions for each graph, but\r\neach graph can be used in multiple sessions. In this case, it is often\r\nclearer to pass the graph to be launched explicitly to the session\r\nconstructor.\r\n\r\n\\subparagraph{Args: }\\label{args}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{target}: (Optional.) The execution engine to connect to.\r\n  Defaults to using an in-process engine. At present, no value other\r\n  than the empty string is supported.\r\n\\item\r\n  \\texttt{graph}: (Optional.) The \\texttt{Graph} to be launched\r\n  (described above).\r\n\\item\r\n  \\texttt{config}: (Optional.) A\r\n  \\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/config.proto}{\\texttt{ConfigProto}}\r\n  protocol buffer with configuration options for the session.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.Session.run(fetches,\\ feed\\_dict=None)}\r\n}{tf.Session.run(fetches, feed\\_dict=None) }}\\label{tf.session.run}\r\n\r\nRuns the operations and evaluates the tensors in \\texttt{fetches}.\r\n\r\nThis method runs one ``step'' of TensorFlow computation, by running the\r\nnecessary graph fragment to execute every \\texttt{Operation} and\r\nevaluate every \\texttt{Tensor} in \\texttt{fetches}, substituting the\r\nvalues in \\texttt{feed\\_dict} for the corresponding input values.\r\n\r\nThe \\texttt{fetches} argument may be a list of graph elements or a\r\nsingle graph element, and these determine the return value of this\r\nmethod. A graph element can be one of the following types:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  If the \\emph{i}th element of \\texttt{fetches} is an\r\n  \\href{../../api_docs/python/framework.md\\#Operation}{\\texttt{Operation}},\r\n  the \\emph{i}th return value will be \\texttt{None}.\r\n\\item\r\n  If the \\emph{i}th element of \\texttt{fetches} is a\r\n  \\href{../../api_docs/python/framework.md\\#Tensor}{\\texttt{Tensor}},\r\n  the \\emph{i}th return value will be a numpy ndarray containing the\r\n  value of that tensor.\r\n\\item\r\n  If the \\emph{i}th element of \\texttt{fetches} is a\r\n  \\href{../../api_docs/python/sparse_ops.md\\#SparseTensor}{\\texttt{SparseTensor}},\r\n  the \\emph{i}th return value will be a\r\n  \\href{../../api_docs/python/sparse_ops.md\\#SparseTensorValue}{\\texttt{SparseTensorValue}}\r\n  containing the value of that sparse tensor.\r\n\\end{itemize}\r\n\r\nThe optional \\texttt{feed\\_dict} argument allows the caller to override\r\nthe value of tensors in the graph. Each key in \\texttt{feed\\_dict} can\r\nbe one of the following types:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  If the key is a\r\n  \\href{../../api_docs/python/framework.md\\#Tensor}{\\texttt{Tensor}},\r\n  the value may be a Python scalar, string, list, or numpy ndarray that\r\n  can be converted to the same \\texttt{dtype} as that tensor.\r\n  Additionally, if the key is a\r\n  \\href{../../api_docs/python/io_ops.md\\#placeholder}{placeholder}, the\r\n  shape of the value will be checked for compatibility with the\r\n  placeholder.\r\n\\item\r\n  If the key is a\r\n  \\href{../../api_docs/python/sparse_ops.md\\#SparseTensor}{\\texttt{SparseTensor}},\r\n  the value should be a\r\n  \\href{../../api_docs/python/sparse_ops.md\\#SparseTensorValue}{\\texttt{SparseTensorValue}}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Args: }\\label{args-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{fetches}: A single graph element, or a list of graph elements\r\n  (described above).\r\n\\item\r\n  \\texttt{feed\\_dict}: A dictionary that maps graph elements to values\r\n  (described above).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns}\r\n\r\nEither a single value if \\texttt{fetches} is a single graph element, or\r\na list of values if \\texttt{fetches} is a list (described above).\r\n\r\n\\subparagraph{Raises: }\\label{raises}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{RuntimeError}: If this \\texttt{Session} is in an invalid state\r\n  (e.g.~has been closed).\r\n\\item\r\n  \\texttt{TypeError}: If \\texttt{fetches} or \\texttt{feed\\_dict} keys\r\n  are of an inappropriate type.\r\n\\item\r\n  \\texttt{ValueError}: If \\texttt{fetches} or \\texttt{feed\\_dict} keys\r\n  are invalid or refer to a \\texttt{Tensor} that doesn't exist.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.Session.close()}\r\n}{tf.Session.close() }}\\label{tf.session.close}\r\n\r\nCloses this session.\r\n\r\nCalling this method frees all resources associated with the session.\r\n\r\n\\subparagraph{Raises: }\\label{raises-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{RuntimeError}: If an error occurs while closing the session.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.Session.graph}\r\n}{tf.Session.graph }}\\label{tf.session.graph}\r\n\r\nThe graph that was launched in this session.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.Session.as\\_default()}\r\n}{tf.Session.as\\_default() }}\\label{tf.session.asux5fdefault}\r\n\r\nReturns a context manager that makes this object the default session.\r\n\r\nUse with the \\texttt{with} keyword to specify that calls to\r\n\\href{../../api_docs/python/framework.md\\#Operation.run}{\\texttt{Operation.run()}}\r\nor\r\n\\href{../../api_docs/python/framework.md\\#Tensor.run}{\\texttt{Tensor.run()}}\r\nshould be executed in this session.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.constant(..)}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session()}\r\n\r\n\\ControlFlowTok{with} \\NormalTok{sess.as_default():}\r\n  \\ControlFlowTok{assert} \\NormalTok{tf.get_default_session() }\\OperatorTok{is} \\NormalTok{sess}\r\n  \\BuiltInTok{print} \\NormalTok{c.}\\BuiltInTok{eval}\\NormalTok{()}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nTo get the current default session, use\r\n\\protect\\hyperlink{getux5fdefaultux5fsession}{\\texttt{tf.get\\_default\\_session()}}.\r\n\r\n\\emph{N.B.} The \\texttt{as\\_default} context manager \\emph{does not}\r\nclose the session when you exit the context, and you must close the\r\nsession explicitly.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.constant(...)}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session()}\r\n\\ControlFlowTok{with} \\NormalTok{sess.as_default():}\r\n  \\BuiltInTok{print} \\NormalTok{c.}\\BuiltInTok{eval}\\NormalTok{()}\r\n\\CommentTok{# ...}\r\n\\ControlFlowTok{with} \\NormalTok{sess.as_default():}\r\n  \\BuiltInTok{print} \\NormalTok{c.}\\BuiltInTok{eval}\\NormalTok{()}\r\n\r\n\\NormalTok{sess.close()}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nAlternatively, you can use \\texttt{with\\ tf.Session():} to create a\r\nsession that is automatically closed on exiting the context, including\r\nwhen an uncaught exception is raised.\r\n\r\n\\emph{N.B.} The default graph is a property of the current thread. If\r\nyou create a new thread, and wish to use the default session in that\r\nthread, you must explicitly add a \\texttt{with\\ sess.as\\_default():} in\r\nthat thread's function.\r\n\r\n\\subparagraph{Returns: }\\label{returns-1}\r\n\r\nA context manager using this session as the default session.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.InteractiveSession}\r\n}{class tf.InteractiveSession }}\\label{class-tf.interactivesession}\r\n\r\nA TensorFlow \\texttt{Session} for use in interactive contexts, such as a\r\nshell.\r\n\r\nThe only difference with a regular \\texttt{Session} is that an\r\n\\texttt{InteractiveSession} installs itself as the default session on\r\nconstruction. The methods\r\n\\href{../../api_docs/python/framework.md\\#Tensor.eval}{\\texttt{Tensor.eval()}}\r\nand\r\n\\href{../../api_docs/python/framework.md\\#Operation.run}{\\texttt{Operation.run()}}\r\nwill use that session to run ops.\r\n\r\nThis is convenient in interactive shells and\r\n\\href{http://ipython.org}{IPython notebooks}, as it avoids having to\r\npass an explicit \\texttt{Session} object to run ops.\r\n\r\nFor example:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.InteractiveSession()}\r\n\\NormalTok{a }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{5.0}\\NormalTok{)}\r\n\\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{6.0}\\NormalTok{)}\r\n\\NormalTok{c }\\OperatorTok{=} \\NormalTok{a }\\OperatorTok{*} \\NormalTok{b}\r\n\\CommentTok{# We can just use 'c.eval()' without passing 'sess'}\r\n\\BuiltInTok{print} \\NormalTok{c.}\\BuiltInTok{eval}\\NormalTok{()}\r\n\\NormalTok{sess.close()}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nNote that a regular session installs itself as the default session when\r\nit is created in a \\texttt{with} statement. The common usage in\r\nnon-interactive programs is to follow that pattern:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{a }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{5.0}\\NormalTok{)}\r\n\\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.constant(}\\FloatTok{6.0}\\NormalTok{)}\r\n\\NormalTok{c }\\OperatorTok{=} \\NormalTok{a }\\OperatorTok{*} \\NormalTok{b}\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session():}\r\n  \\CommentTok{# We can also use 'c.eval()' here.}\r\n  \\BuiltInTok{print} \\NormalTok{c.}\\BuiltInTok{eval}\\NormalTok{()}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.InteractiveSession.\\_\\_init\\_\\_(target=\\textquotesingle{}\\textquotesingle{},\\ graph=None)}\r\n}{tf.InteractiveSession.\\_\\_init\\_\\_(target='', graph=None) }}\\label{tf.interactivesession.ux5fux5finitux5fux5ftarget-graphnone}\r\n\r\nCreates a new interactive TensorFlow session.\r\n\r\nIf no \\texttt{graph} argument is specified when constructing the\r\nsession, the default graph will be launched in the session. If you are\r\nusing more than one graph (created with \\texttt{tf.Graph()} in the same\r\nprocess, you will have to use different sessions for each graph, but\r\neach graph can be used in multiple sessions. In this case, it is often\r\nclearer to pass the graph to be launched explicitly to the session\r\nconstructor.\r\n\r\n\\subparagraph{Args: }\\label{args-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{target}: (Optional.) The execution engine to connect to.\r\n  Defaults to using an in-process engine. At present, no value other\r\n  than the empty string is supported.\r\n\\item\r\n  \\texttt{graph}: (Optional.) The \\texttt{Graph} to be launched\r\n  (described above).\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.InteractiveSession.close()}\r\n}{tf.InteractiveSession.close() }}\\label{tf.interactivesession.close}\r\n\r\nCloses an \\texttt{InteractiveSession}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.get\\_default\\_session()}\r\n}{tf.get\\_default\\_session() }}\\label{tf.getux5fdefaultux5fsession}\r\n\r\nReturns the default session for the current thread.\r\n\r\nThe returned \\texttt{Session} will be the innermost session on which a\r\n\\texttt{Session} or \\texttt{Session.as\\_default()} context has been\r\nentered.\r\n\r\n\\emph{N.B.} The default session is a property of the current thread. If\r\nyou create a new thread, and wish to use the default session in that\r\nthread, you must explicitly add a \\texttt{with\\ sess.as\\_default():} in\r\nthat thread's function.\r\n\r\n\\subparagraph{Returns: }\\label{returns-2}\r\n\r\nThe default \\texttt{Session} being used in the current thread.\r\n\r\n\\subsection{Error classes }\\label{error-classes}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.OpError}\r\n}{class tf.OpError }}\\label{class-tf.operror}\r\n\r\nA generic error that is raised when TensorFlow execution fails.\r\n\r\nWhenever possible, the session will raise a more specific subclass of\r\n\\texttt{OpError} from the \\texttt{tf.errors} module.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.OpError.op}\r\n}{tf.OpError.op }}\\label{tf.operror.op}\r\n\r\nThe operation that failed, if known.\r\n\r\n\\emph{N.B.} If the failed op was synthesized at runtime, e.g.~a\r\n\\texttt{Send} or \\texttt{Recv} op, there will be no corresponding\r\n\\href{../../api_docs/python/framework.md\\#Operation}{\\texttt{Operation}}\r\nobject. In that case, this will return \\texttt{None}, and you should\r\ninstead use the\r\n\\protect\\hyperlink{OpError.nodeux5fdef}{\\texttt{OpError.node\\_def}} to\r\ndiscover information about the op.\r\n\r\n\\subparagraph{Returns: }\\label{returns-3}\r\n\r\nThe \\texttt{Operation} that failed, or None.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.OpError.node\\_def}\r\n}{tf.OpError.node\\_def }}\\label{tf.operror.nodeux5fdef}\r\n\r\nThe \\texttt{NodeDef} proto representing the op that failed.\r\n\r\n\\paragraph{Other Methods }\\label{other-methods}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.OpError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message,\\ error\\_code)}\r\n}{tf.OpError.\\_\\_init\\_\\_(node\\_def, op, message, error\\_code) }}\\label{tf.operror.ux5fux5finitux5fux5fnodeux5fdef-op-message-errorux5fcode}\r\n\r\nCreates a new OpError indicating that a particular op failed.\r\n\r\n\\subparagraph{Args: }\\label{args-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{node\\_def}: The graph\\_pb2.NodeDef proto representing the op\r\n  that failed.\r\n\\item\r\n  \\texttt{op}: The ops.Operation that failed, if known; otherwise None.\r\n\\item\r\n  \\texttt{message}: The message string describing the failure.\r\n\\item\r\n  \\texttt{error\\_code}: The error\\_codes\\_pb2.Code describing the error.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.OpError.error\\_code}\r\n}{tf.OpError.error\\_code }}\\label{tf.operror.errorux5fcode}\r\n\r\nThe integer error code that describes the error.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.OpError.message}\r\n}{tf.OpError.message }}\\label{tf.operror.message}\r\n\r\nThe error message that describes the error.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.CancelledError}\r\n}{class tf.errors.CancelledError }}\\label{class-tf.errors.cancellederror}\r\n\r\nRaised when an operation or step is cancelled.\r\n\r\nFor example, a long-running operation (e.g.\r\n\\href{../../api_docs/python/io_ops.md\\#QueueBase.enqueue}{\\texttt{queue.enqueue()}}\r\nmay be cancelled by running another operation (e.g.\r\n\\href{../../api_docs/python/io_ops.md\\#QueueBase.close}{\\texttt{queue.close(cancel\\_pending\\_enqueues=True)}},\r\nor by \\href{../../api_docs/python/client.md\\#Session.close}{closing the\r\nsession}. A step that is running such a long-running operation will fail\r\nby raising \\texttt{CancelledError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.CancelledError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.CancelledError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.cancellederror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates a \\texttt{CancelledError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.UnknownError}\r\n}{class tf.errors.UnknownError }}\\label{class-tf.errors.unknownerror}\r\n\r\nUnknown error.\r\n\r\nAn example of where this error may be returned is if a Status value\r\nreceived from another address space belongs to an error-space that is\r\nnot known to this address space. Also errors raised by APIs that do not\r\nreturn enough error information may be converted to this error.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.UnknownError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message,\\ error\\_code=2)}\r\n}{tf.errors.UnknownError.\\_\\_init\\_\\_(node\\_def, op, message, error\\_code=2) }}\\label{tf.errors.unknownerror.ux5fux5finitux5fux5fnodeux5fdef-op-message-errorux5fcode2}\r\n\r\nCreates an \\texttt{UnknownError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.InvalidArgumentError}\r\n}{class tf.errors.InvalidArgumentError }}\\label{class-tf.errors.invalidargumenterror}\r\n\r\nRaised when an operation receives an invalid argument.\r\n\r\nThis may occur, for example, if an operation is receives an input tensor\r\nthat has an invalid value or shape. For example, the\r\n\\href{../../api_docs/python/math_ops.md\\#matmul}{\\texttt{tf.matmul()}}\r\nop will raise this error if it receives an input that is not a matrix,\r\nand the\r\n\\href{../../api_docs/python/array_ops.md\\#reshape}{\\texttt{tf.reshape()}}\r\nop will raise this error if the new shape does not match the number of\r\nelements in the input tensor.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.InvalidArgumentError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.InvalidArgumentError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.invalidargumenterror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates an \\texttt{InvalidArgumentError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.DeadlineExceededError}\r\n}{class tf.errors.DeadlineExceededError }}\\label{class-tf.errors.deadlineexceedederror}\r\n\r\nRaised when a deadline expires before an operation could complete.\r\n\r\nThis exception is not currently used.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.DeadlineExceededError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.DeadlineExceededError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.deadlineexceedederror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates a \\texttt{DeadlineExceededError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.NotFoundError}\r\n}{class tf.errors.NotFoundError }}\\label{class-tf.errors.notfounderror}\r\n\r\nRaised when a requested entity (e.g., a file or directory) was not\r\nfound.\r\n\r\nFor example, running the\r\n\\href{../../api_docs/python/io_ops.md\\#WholeFileReader}{\\texttt{tf.WholeFileReader.read()}}\r\noperation could raise \\texttt{NotFoundError} if it receives the name of\r\na file that does not exist.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.NotFoundError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.NotFoundError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.notfounderror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates a \\texttt{NotFoundError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.AlreadyExistsError}\r\n}{class tf.errors.AlreadyExistsError }}\\label{class-tf.errors.alreadyexistserror}\r\n\r\nRaised when an entity that we attempted to create already exists.\r\n\r\nFor example, running an operation that saves a file (e.g.\r\n\\href{../../api_docs/python/train.md\\#Saver.save}{\\texttt{tf.train.Saver.save()}})\r\ncould potentially raise this exception if an explicit filename for an\r\nexisting file was passed.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.AlreadyExistsError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.AlreadyExistsError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.alreadyexistserror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates an \\texttt{AlreadyExistsError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.PermissionDeniedError}\r\n}{class tf.errors.PermissionDeniedError }}\\label{class-tf.errors.permissiondeniederror}\r\n\r\nRaised when the caller does not have permission to run an operation.\r\n\r\nFor example, running the\r\n\\href{../../api_docs/python/io_ops.md\\#WholeFileReader}{\\texttt{tf.WholeFileReader.read()}}\r\noperation could raise \\texttt{PermissionDeniedError} if it receives the\r\nname of a file for which the user does not have the read file\r\npermission.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.PermissionDeniedError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.PermissionDeniedError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.permissiondeniederror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates a \\texttt{PermissionDeniedError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.UnauthenticatedError}\r\n}{class tf.errors.UnauthenticatedError }}\\label{class-tf.errors.unauthenticatederror}\r\n\r\nThe request does not have valid authentication credentials.\r\n\r\nThis exception is not currently used.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.UnauthenticatedError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.UnauthenticatedError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.unauthenticatederror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates an \\texttt{UnauthenticatedError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.ResourceExhaustedError}\r\n}{class tf.errors.ResourceExhaustedError }}\\label{class-tf.errors.resourceexhaustederror}\r\n\r\nSome resource has been exhausted.\r\n\r\nFor example, this error might be raised if a per-user quota is\r\nexhausted, or perhaps the entire file system is out of space.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.ResourceExhaustedError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.ResourceExhaustedError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.resourceexhaustederror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates a \\texttt{ResourceExhaustedError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.FailedPreconditionError}\r\n}{class tf.errors.FailedPreconditionError }}\\label{class-tf.errors.failedpreconditionerror}\r\n\r\nOperation was rejected because the system is not in a state to execute\r\nit.\r\n\r\nThis exception is most commonly raised when running an operation that\r\nreads a\r\n\\href{../../api_docs/python/state_ops.md\\#Variable}{\\texttt{tf.Variable}}\r\nbefore it has been initialized.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.FailedPreconditionError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.FailedPreconditionError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.failedpreconditionerror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates a \\texttt{FailedPreconditionError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.AbortedError}\r\n}{class tf.errors.AbortedError }}\\label{class-tf.errors.abortederror}\r\n\r\nThe operation was aborted, typically due to a concurrent action.\r\n\r\nFor example, running a\r\n\\href{../../api_docs/python/io_ops.md\\#QueueBase.enqueue}{\\texttt{queue.enqueue()}}\r\noperation may raise \\texttt{AbortedError} if a\r\n\\href{../../api_docs/python/io_ops.md\\#QueueBase.close}{\\texttt{queue.close()}}\r\noperation previously ran.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.AbortedError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.AbortedError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.abortederror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates an \\texttt{AbortedError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.OutOfRangeError}\r\n}{class tf.errors.OutOfRangeError }}\\label{class-tf.errors.outofrangeerror}\r\n\r\nRaised when an operation executed past the valid range.\r\n\r\nThis exception is raised in ``end-of-file'' conditions, such as when a\r\n\\href{../../api_docs/python/io_ops.md\\#QueueBase.dequeue}{\\texttt{queue.dequeue()}}\r\noperation is blocked on an empty queue, and a\r\n\\href{../../api_docs/python/io_ops.md\\#QueueBase.close}{\\texttt{queue.close()}}\r\noperation executes.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.OutOfRangeError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.OutOfRangeError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.outofrangeerror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates an \\texttt{OutOfRangeError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.UnimplementedError}\r\n}{class tf.errors.UnimplementedError }}\\label{class-tf.errors.unimplementederror}\r\n\r\nRaised when an operation has not been implemented.\r\n\r\nSome operations may raise this error when passed otherwise-valid\r\narguments that it does not currently support. For example, running the\r\n\\href{../../api_docs/python/nn.md\\#max_pool}{\\texttt{tf.nn.max\\_pool()}}\r\noperation would raise this error if pooling was requested on the batch\r\ndimension, because this is not yet supported.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.UnimplementedError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.UnimplementedError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.unimplementederror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates an \\texttt{UnimplementedError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.InternalError}\r\n}{class tf.errors.InternalError }}\\label{class-tf.errors.internalerror}\r\n\r\nRaised when the system experiences an internal error.\r\n\r\nThis exception is raised when some invariant expected by the runtime has\r\nbeen broken. Catching this exception is not recommended.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.InternalError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.InternalError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.internalerror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates an \\texttt{InternalError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.UnavailableError}\r\n}{class tf.errors.UnavailableError }}\\label{class-tf.errors.unavailableerror}\r\n\r\nRaised when the runtime is currently unavailable.\r\n\r\nThis exception is not currently used.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.UnavailableError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.UnavailableError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.unavailableerror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates an \\texttt{UnavailableError}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.errors.DataLossError}\r\n}{class tf.errors.DataLossError }}\\label{class-tf.errors.datalosserror}\r\n\r\nRaised when unrecoverable data loss or corruption is encountered.\r\n\r\nFor example, this may be raised by running a\r\n\\href{../../api_docs/python/io_ops.md\\#WholeFileReader}{\\texttt{tf.WholeFileReader.read()}}\r\noperation, if the file is truncated while it is being read.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.errors.DataLossError.\\_\\_init\\_\\_(node\\_def,\\ op,\\ message)}\r\n}{tf.errors.DataLossError.\\_\\_init\\_\\_(node\\_def, op, message) }}\\label{tf.errors.datalosserror.ux5fux5finitux5fux5fnodeux5fdef-op-message}\r\n\r\nCreates a \\texttt{DataLossError}."
  },
  {
    "path": "tex_pdf/api/python/c4s13_train.tex",
    "content": "\r\n\r\n\\section{Training }\\label{training}\r\n\r\n\\subsection{Contents}\\label{contents}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-training}{Training}}{Training}}\\label{training-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-optimizers}{Optimizers}\r\n\\item\r\n  \\protect\\hyperlink{Optimizer}{\\texttt{class\\ tf.train.Optimizer}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-usage}{Usage}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-processing-gradients-before-applying-them.}{Processing\r\n  gradients before applying them.}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-gating-gradients}{Gating Gradients}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-slots}{Slots}\r\n\\item\r\n  \\protect\\hyperlink{GradientDescentOptimizer}{\\texttt{class\\ tf.train.GradientDescentOptimizer}}\r\n\\item\r\n  \\protect\\hyperlink{AdagradOptimizer}{\\texttt{class\\ tf.train.AdagradOptimizer}}\r\n\\item\r\n  \\protect\\hyperlink{MomentumOptimizer}{\\texttt{class\\ tf.train.MomentumOptimizer}}\r\n\\item\r\n  \\protect\\hyperlink{AdamOptimizer}{\\texttt{class\\ tf.train.AdamOptimizer}}\r\n\\item\r\n  \\protect\\hyperlink{FtrlOptimizer}{\\texttt{class\\ tf.train.FtrlOptimizer}}\r\n\\item\r\n  \\protect\\hyperlink{RMSPropOptimizer}{\\texttt{class\\ tf.train.RMSPropOptimizer}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-gradient-computation}{Gradient\r\n  Computation}\r\n\\item\r\n  \\protect\\hyperlink{gradients}{\\texttt{tf.gradients(ys,\\ xs,\\ grad\\_ys=None,\\ name=\\textquotesingle{}gradients\\textquotesingle{},\\ colocate\\_gradients\\_with\\_ops=False,\\ gate\\_gradients=False,\\ aggregation\\_method=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AggregationMethod}{\\texttt{class\\ tf.AggregationMethod}}\r\n\\item\r\n  \\protect\\hyperlink{stopux5fgradient}{\\texttt{tf.stop\\_gradient(input,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-gradient-clipping}{Gradient Clipping}\r\n\\item\r\n  \\protect\\hyperlink{clipux5fbyux5fvalue}{\\texttt{tf.clip\\_by\\_value(t,\\ clip\\_value\\_min,\\ clip\\_value\\_max,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{clipux5fbyux5fnorm}{\\texttt{tf.clip\\_by\\_norm(t,\\ clip\\_norm,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{clipux5fbyux5faverageux5fnorm}{\\texttt{tf.clip\\_by\\_average\\_norm(t,\\ clip\\_norm,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{clipux5fbyux5fglobalux5fnorm}{\\texttt{tf.clip\\_by\\_global\\_norm(t\\_list,\\ clip\\_norm,\\ use\\_norm=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{globalux5fnorm}{\\texttt{tf.global\\_norm(t\\_list,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-decaying-the-learning-rate}{Decaying\r\n  the learning rate}\r\n\\item\r\n  \\protect\\hyperlink{exponentialux5fdecay}{\\texttt{tf.train.exponential\\_decay(learning\\_rate,\\ global\\_step,\\ decay\\_steps,\\ decay\\_rate,\\ staircase=False,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-moving-averages}{Moving Averages}\r\n\\item\r\n  \\protect\\hyperlink{ExponentialMovingAverage}{\\texttt{class\\ tf.train.ExponentialMovingAverage}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-coordinator-and-queuerunner}{Coordinator\r\n  and QueueRunner}\r\n\\item\r\n  \\protect\\hyperlink{Coordinator}{\\texttt{class\\ tf.train.Coordinator}}\r\n\\item\r\n  \\protect\\hyperlink{QueueRunner}{\\texttt{class\\ tf.train.QueueRunner}}\r\n\\item\r\n  \\protect\\hyperlink{addux5fqueueux5frunner}{\\texttt{tf.train.add\\_queue\\_runner(qr,\\ collection=\\textquotesingle{}queue\\_runners\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{startux5fqueueux5frunners}{\\texttt{tf.train.start\\_queue\\_runners(sess=None,\\ coord=None,\\ daemon=True,\\ start=True,\\ collection=\\textquotesingle{}queue\\_runners\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-summary-operations}{Summary\r\n  Operations}\r\n\\item\r\n  \\protect\\hyperlink{scalarux5fsummary}{\\texttt{tf.scalar\\_summary(tags,\\ values,\\ collections=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{imageux5fsummary}{\\texttt{tf.image\\_summary(tag,\\ tensor,\\ max\\_images=None,\\ collections=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{histogramux5fsummary}{\\texttt{tf.histogram\\_summary(tag,\\ values,\\ collections=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{zeroux5ffraction}{\\texttt{tf.nn.zero\\_fraction(value,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{mergeux5fsummary}{\\texttt{tf.merge\\_summary(inputs,\\ collections=None,\\ name=None)}}\r\n\\item\r\n  \\protect\\hyperlink{mergeux5fallux5fsummaries}{\\texttt{tf.merge\\_all\\_summaries(key=\\textquotesingle{}summaries\\textquotesingle{})}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-adding-summaries-to-event-files}{Adding\r\n  Summaries to Event Files}\r\n\\item\r\n  \\protect\\hyperlink{SummaryWriter}{\\texttt{class\\ tf.train.SummaryWriter}}\r\n\\item\r\n  \\protect\\hyperlink{summaryux5fiterator}{\\texttt{tf.train.summary\\_iterator(path)}}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-training-utilities}{Training\r\n  utilities}\r\n\\item\r\n  \\protect\\hyperlink{globalux5fstep}{\\texttt{tf.train.global\\_step(sess,\\ global\\_step\\_tensor)}}\r\n\\item\r\n  \\protect\\hyperlink{writeux5fgraph}{\\texttt{tf.train.write\\_graph(graph\\_def,\\ logdir,\\ name,\\ as\\_text=True)}}\r\n\\end{itemize}\r\n\r\nThis library provides a set of classes and functions that helps train\r\nmodels.\r\n\r\n\\subsection{Optimizers }\\label{optimizers}\r\n\r\nThe Optimizer base class provides methods to compute gradients for a\r\nloss and apply gradients to variables. A collection of subclasses\r\nimplement classic optimization algorithms such as GradientDescent and\r\nAdagrad.\r\n\r\nYou never instantiate the Optimizer class itself, but instead\r\ninstantiate one of the subclasses.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.train.Optimizer}\r\n}{class tf.train.Optimizer }}\\label{class-tf.train.optimizer}\r\n\r\nBase class for optimizers.\r\n\r\nThis class defines the API to add Ops to train a model. You never use\r\nthis class directly, but instead instantiate one of its subclasses such\r\nas \\texttt{GradientDescentOptimizer}, \\texttt{AdagradOptimizer}, or\r\n\\texttt{MomentumOptimizer}.\r\n\r\n\\subsubsection{Usage }\\label{usage}\r\n\r\n\\begin{verbatim}\r\n# Create an optimizer with the desired parameters.\r\nopt = GradientDescentOptimizer(learning_rate=0.1)\r\n# Add Ops to the graph to minimize a cost by updating a list of variables.\r\n# \"cost\" is a Tensor, and the list of variables contains variables.Variable\r\n# objects.\r\nopt_op = opt.minimize(cost, <list of variables>)\r\n\\end{verbatim}\r\n\r\nIn the training program you will just have to run the returned Op.\r\n\r\n\\begin{verbatim}\r\n# Execute opt_op to do one step of training:\r\nopt_op.run()\r\n\\end{verbatim}\r\n\r\n\\subsubsection{Processing gradients before applying them.\r\n}\\label{processing-gradients-before-applying-them.}\r\n\r\nCalling \\texttt{minimize()} takes care of both computing the gradients\r\nand applying them to the variables. If you want to process the gradients\r\nbefore applying them you can instead use the optimizer in three steps:\r\n\r\n\\begin{enumerate}\r\n\\def\\labelenumi{\\arabic{enumi}.}\r\n\\tightlist\r\n\\item\r\n  Compute the gradients with \\texttt{compute\\_gradients()}.\r\n\\item\r\n  Process the gradients as you wish.\r\n\\item\r\n  Apply the processed gradients with \\texttt{apply\\_gradients()}.\r\n\\end{enumerate}\r\n\r\nExample:\r\n\r\n\\begin{verbatim}\r\n# Create an optimizer.\r\nopt = GradientDescentOptimizer(learning_rate=0.1)\r\n\r\n# Compute the gradients for a list of variables.\r\ngrads_and_vars = opt.compute_gradients(loss, <list of variables>)\r\n\r\n# grads_and_vars is a list of tuples (gradient, variable).  Do whatever you\r\n# need to the 'gradient' part, for example cap them, etc.\r\ncapped_grads_and_vars = [(MyCapper(gv[0]), gv[1])) for gv in grads_and_vars]\r\n\r\n# Ask the optimizer to apply the capped gradients.\r\nopt.apply_gradients(capped_grads_and_vars)\r\n\\end{verbatim}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.Optimizer.\\_\\_init\\_\\_(use\\_locking,\\ name)}\r\n}{tf.train.Optimizer.\\_\\_init\\_\\_(use\\_locking, name) }}\\label{tf.train.optimizer.ux5fux5finitux5fux5fuseux5flocking-name}\r\n\r\nCreate a new Optimizer.\r\n\r\nThis must be called by the constructors of subclasses.\r\n\r\n\\subparagraph{Args: }\\label{args}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{use\\_locking}: Bool. If True apply use locks to prevent\r\n  concurrent updates to variables.\r\n\\item\r\n  \\texttt{name}: A non-empty string. The name to use for accumulators\r\n  created for the optimizer.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if name is malformed.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.Optimizer.minimize(loss,\\ global\\_step=None,\\ var\\_list=None,\\ gate\\_gradients=1,\\ name=None)}\r\n}{tf.train.Optimizer.minimize(loss, global\\_step=None, var\\_list=None, gate\\_gradients=1, name=None) }}\\label{tf.train.optimizer.minimizeloss-globalux5fstepnone-varux5flistnone-gateux5fgradients1-namenone}\r\n\r\nAdd operations to minimize `loss' by updating `var\\_list'.\r\n\r\nThis method simply combines calls compute\\_gradients() and\r\napply\\_gradients(). If you want to process the gradient before applying\r\nthem call compute\\_gradients() and apply\\_gradients() explicitly instead\r\nof using this function.\r\n\r\n\\subparagraph{Args: }\\label{args-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{loss}: A Tensor containing the value to minimize.\r\n\\item\r\n  \\texttt{global\\_step}: Optional Variable to increment by one after the\r\n  variables have been updated.\r\n\\item\r\n  \\texttt{var\\_list}: Optional list of variables.Variable to update to\r\n  minimize `loss'. Defaults to the list of variables collected in the\r\n  graph under the key GraphKeys.TRAINABLE\\_VARIABLES.\r\n\\item\r\n  \\texttt{gate\\_gradients}: How to gate the computation of gradients.\r\n  Can be GATE\\_NONE, GATE\\_OP, or GATE\\_GRAPH.\r\n\\item\r\n  \\texttt{name}: Optional name for the returned operation.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns}\r\n\r\nAn Operation that updates the variables in `var\\_list'. If\r\n`global\\_step' was not None, that operation also increments\r\nglobal\\_step.\r\n\r\n\\subparagraph{Raises: }\\label{raises-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if some of the variables are not\r\n  variables.Variable objects.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.Optimizer.compute\\_gradients(loss,\\ var\\_list=None,\\ gate\\_gradients=1)}\r\n}{tf.train.Optimizer.compute\\_gradients(loss, var\\_list=None, gate\\_gradients=1) }}\\label{tf.train.optimizer.computeux5fgradientsloss-varux5flistnone-gateux5fgradients1}\r\n\r\nCompute gradients of ``loss'' for the variables in ``var\\_list''.\r\n\r\nThis is the first part of minimize(). It returns a list of (gradient,\r\nvariable) pairs where ``gradient'' is the gradient for ``variable''.\r\nNote that ``gradient'' can be a Tensor, a IndexedSlices, or None if\r\nthere is no gradient for the given variable.\r\n\r\n\\subparagraph{Args: }\\label{args-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{loss}: A Tensor containing the value to minimize.\r\n\\item\r\n  \\texttt{var\\_list}: Optional list of variables.Variable to update to\r\n  minimize ``loss''. Defaults to the list of variables collected in the\r\n  graph under the key GraphKey.TRAINABLE\\_VARIABLES.\r\n\\item\r\n  \\texttt{gate\\_gradients}: How to gate the computation of gradients.\r\n  Can be GATE\\_NONE, GATE\\_OP, or GATE\\_GRAPH.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-1}\r\n\r\nA list of (gradient, variable) pairs.\r\n\r\n\\subparagraph{Raises: }\\label{raises-2}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{TypeError}: If var\\_list contains anything else than\r\n  variables.Variable.\r\n\\item\r\n  \\texttt{ValueError}: If some arguments are invalid.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.Optimizer.apply\\_gradients(grads\\_and\\_vars,\\ global\\_step=None,\\ name=None)}\r\n}{tf.train.Optimizer.apply\\_gradients(grads\\_and\\_vars, global\\_step=None, name=None) }}\\label{tf.train.optimizer.applyux5fgradientsgradsux5fandux5fvars-globalux5fstepnone-namenone}\r\n\r\nApply gradients to variables.\r\n\r\nThis is the second part of minimize(). It returns an Operation that\r\napplies gradients.\r\n\r\n\\subparagraph{Args: }\\label{args-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{grads\\_and\\_vars}: List of (gradient, variable) pairs as\r\n  returned by compute\\_gradients().\r\n\\item\r\n  \\texttt{global\\_step}: Optional Variable to increment by one after the\r\n  variables have been updated.\r\n\\item\r\n  \\texttt{name}: Optional name for the returned operation. Default to\r\n  the name passed to the Optimizer constructor.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-2}\r\n\r\nAn Operation that applies the specified gradients. If `global\\_step' was\r\nnot None, that operation also increments global\\_step.\r\n\r\n\\subparagraph{Raises: }\\label{raises-3}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{TypeError}: if grads\\_and\\_vars is malformed.\r\n\\end{itemize}\r\n\r\n\\subsubsection{Gating Gradients }\\label{gating-gradients}\r\n\r\nBoth \\texttt{minimize()} and \\texttt{compute\\_gradients()} accept a\r\n\\texttt{gate\\_gradient} argument that controls the degree of parallelism\r\nduring the application of the gradients.\r\n\r\nThe possible values are: \\texttt{GATE\\_NONE}, \\texttt{GATE\\_OP}, and\r\n\\texttt{GATE\\_GRAPH}.\r\n\r\nGATE\\_NONE: Compute and apply gradients in parallel. This provides the\r\nmaximum parallelism in execution, at the cost of some\r\nnon-reproducibility in the results. For example the two gradients of\r\nMatMul depend on the input values: With \\texttt{GATE\\_NONE} one of the\r\ngradients could be applied to one of the inputs \\emph{before} the other\r\ngradient is computed resulting in non-reproducible results.\r\n\r\nGATE\\_OP: For each Op, make sure all gradients are computed before they\r\nare used. This prevents race conditions for Ops that generate gradients\r\nfor multiple inputs where the gradients depend on the inputs.\r\n\r\nGATE\\_GRAPH: Make sure all gradients for all variables are computed\r\nbefore any one of them is used. This provides the least parallelism but\r\ncan be useful if you want to process all gradients before applying any\r\nof them.\r\n\r\n\\subsubsection{Slots }\\label{slots}\r\n\r\nSome optimizer subclasses, such as \\texttt{MomentumOptimizer} and\r\n\\texttt{AdagradOptimizer} allocate and manage additional variables\r\nassociated with the variables to train. These are called Slots. Slots\r\nhave names and you can ask the optimizer for the names of the slots that\r\nit uses. Once you have a slot name you can ask the optimizer for the\r\nvariable it created to hold the slot value.\r\n\r\nThis can be useful if you want to log debug a training algorithm, report\r\nstats about the slots, etc.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.Optimizer.get\\_slot\\_names()}\r\n}{tf.train.Optimizer.get\\_slot\\_names() }}\\label{tf.train.optimizer.getux5fslotux5fnames}\r\n\r\nReturn a list of the names of slots created by the Optimizer.\r\n\r\nSee get\\_slot().\r\n\r\n\\subparagraph{Returns: }\\label{returns-3}\r\n\r\nA list of strings.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.Optimizer.get\\_slot(var,\\ name)}\r\n}{tf.train.Optimizer.get\\_slot(var, name) }}\\label{tf.train.optimizer.getux5fslotvar-name}\r\n\r\nReturn a slot named ``name'' created for ``var'' by the Optimizer.\r\n\r\nSome Optimizer subclasses use additional variables. For example Momentum\r\nand Adagrad use variables to accumulate updates. This method gives\r\naccess to these Variables if for some reason you need them.\r\n\r\nUse get\\_slot\\_names() to get the list of slot names created by the\r\nOptimizer.\r\n\r\n\\subparagraph{Args: }\\label{args-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{var}: A variable passed to minimize() or apply\\_gradients().\r\n\\item\r\n  \\texttt{name}: A string.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-4}\r\n\r\nThe Variable for the slot if it was created, None otherwise.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.train.GradientDescentOptimizer}\r\n}{class tf.train.GradientDescentOptimizer }}\\label{class-tf.train.gradientdescentoptimizer}\r\n\r\nOptimizer that implements the gradient descent algorithm.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.GradientDescentOptimizer.\\_\\_init\\_\\_(learning\\_rate,\\ use\\_locking=False,\\ name=\\textquotesingle{}GradientDescent\\textquotesingle{})}\r\n}{tf.train.GradientDescentOptimizer.\\_\\_init\\_\\_(learning\\_rate, use\\_locking=False, name='GradientDescent') }}\\label{tf.train.gradientdescentoptimizer.ux5fux5finitux5fux5flearningux5frate-useux5flockingfalse-namegradientdescent}\r\n\r\nConstruct a new gradient descent optimizer.\r\n\r\n\\subparagraph{Args: }\\label{args-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{learning\\_rate}: A Tensor or a floating point value. The\r\n  learning rate to use.\r\n\\item\r\n  \\texttt{use\\_locking}: If True use locks for update operation.s\r\n\\item\r\n  \\texttt{name}: Optional name prefix for the operations created when\r\n  applying gradients. Defaults to ``GradientDescent''.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.train.AdagradOptimizer}\r\n}{class tf.train.AdagradOptimizer }}\\label{class-tf.train.adagradoptimizer}\r\n\r\nOptimizer that implements the Adagrad algorithm.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.AdagradOptimizer.\\_\\_init\\_\\_(learning\\_rate,\\ initial\\_accumulator\\_value=0.1,\\ use\\_locking=False,\\ name=\\textquotesingle{}Adagrad\\textquotesingle{})}\r\n}{tf.train.AdagradOptimizer.\\_\\_init\\_\\_(learning\\_rate, initial\\_accumulator\\_value=0.1, use\\_locking=False, name='Adagrad') }}\\label{tf.train.adagradoptimizer.ux5fux5finitux5fux5flearningux5frate-initialux5faccumulatorux5fvalue0.1-useux5flockingfalse-nameadagrad}\r\n\r\nConstruct a new Adagrad optimizer.\r\n\r\n\\subparagraph{Args: }\\label{args-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{learning\\_rate}: A \\texttt{Tensor} or a floating point value.\r\n  The learning rate.\r\n\\item\r\n  \\texttt{initial\\_accumulator\\_value}: A floating point value. Starting\r\n  value for the accumulators, must be positive.\r\n\\item\r\n  \\texttt{use\\_locking}: If \\texttt{True} use locks for update\r\n  operations.\r\n\\item\r\n  \\texttt{name}: Optional name prefix for the operations created when\r\n  applying gradients. Defaults to ``Adagrad''.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-4}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: If the initial\\_accumulator\\_value is invalid.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.train.MomentumOptimizer}\r\n}{class tf.train.MomentumOptimizer }}\\label{class-tf.train.momentumoptimizer}\r\n\r\nOptimizer that implements the Momentum algorithm.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.MomentumOptimizer.\\_\\_init\\_\\_(learning\\_rate,\\ momentum,\\ use\\_locking=False,\\ name=\\textquotesingle{}Momentum\\textquotesingle{})}\r\n}{tf.train.MomentumOptimizer.\\_\\_init\\_\\_(learning\\_rate, momentum, use\\_locking=False, name='Momentum') }}\\label{tf.train.momentumoptimizer.ux5fux5finitux5fux5flearningux5frate-momentum-useux5flockingfalse-namemomentum}\r\n\r\nConstruct a new Momentum optimizer.\r\n\r\n\\subparagraph{Args: }\\label{args-7}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{learning\\_rate}: A \\texttt{Tensor} or a floating point value.\r\n  The learning rate.\r\n\\item\r\n  \\texttt{momentum}: A \\texttt{Tensor} or a floating point value. The\r\n  momentum.\r\n\\item\r\n  \\texttt{use\\_locking}: If \\texttt{True} use locks for update\r\n  operations.\r\n\\item\r\n  \\texttt{name}: Optional name prefix for the operations created when\r\n  applying gradients. Defaults to ``Momentum''.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.train.AdamOptimizer}\r\n}{class tf.train.AdamOptimizer }}\\label{class-tf.train.adamoptimizer}\r\n\r\nOptimizer that implements the Adam algorithm.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.AdamOptimizer.\\_\\_init\\_\\_(learning\\_rate=0.001,\\ beta1=0.9,\\ beta2=0.999,\\ epsilon=1e-08,\\ use\\_locking=False,\\ name=\\textquotesingle{}Adam\\textquotesingle{})}\r\n}{tf.train.AdamOptimizer.\\_\\_init\\_\\_(learning\\_rate=0.001, beta1=0.9, beta2=0.999, epsilon=1e-08, use\\_locking=False, name='Adam') }}\\label{tf.train.adamoptimizer.ux5fux5finitux5fux5flearningux5frate0.001-beta10.9-beta20.999-epsilon1e-08-useux5flockingfalse-nameadam}\r\n\r\nConstruct a new Adam optimizer.\r\n\r\nImplementation is based on: http://arxiv.org/pdf/1412.6980v7.pdf\r\n\r\nInitialization:\r\n\r\n\\begin{verbatim}\r\nm_0 <- 0 (Initialize initial 1st moment vector)\r\nv_0 <- 0 (Initialize initial 2nd moment vector)\r\nt <- 0 (Initialize timestep)\r\n\\end{verbatim}\r\n\r\nThe update rule for \\texttt{variable} with gradient \\texttt{g} uses an\r\noptimization described at the end of section2 of the paper:\r\n\r\n\\begin{verbatim}\r\nt <- t + 1\r\nlr_t <- learning_rate * sqrt(1 - beta2^t) / (1 - beta1^t)\r\n\r\nm_t <- beta1 * m_{t-1} + (1 - beta1) * g\r\nv_t <- beta2 * v_{t-1} + (1 - beta2) * g * g\r\nvariable <- variable - lr_t * m_t / (sqrt(v_t) + epsilon)\r\n\\end{verbatim}\r\n\r\nThe default value of 1e-8 for epsilon might not be a good default in\r\ngeneral. For example, when training an Inception network on ImageNet a\r\ncurrent good choice is 1.0 or 0.1.\r\n\r\n\\subparagraph{Args: }\\label{args-8}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{learning\\_rate}: A Tensor or a floating point value. The\r\n  learning rate.\r\n\\item\r\n  \\texttt{beta1}: A float value or a constant float tensor. The\r\n  exponential decay rate for the 1st moment estimates.\r\n\\item\r\n  \\texttt{beta2}: A float value or a constant float tensor. The\r\n  exponential decay rate for the 2st moment estimates.\r\n\\item\r\n  \\texttt{epsilon}: A small constant for numerical stability.\r\n\\item\r\n  \\texttt{use\\_locking}: If True use locks for update operation.s\r\n\\item\r\n  \\texttt{name}: Optional name for the operations created when applying\r\n  gradients. Defaults to ``Adam''.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.train.FtrlOptimizer}\r\n}{class tf.train.FtrlOptimizer }}\\label{class-tf.train.ftrloptimizer}\r\n\r\nOptimizer that implements the FTRL algorithm.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.FtrlOptimizer.\\_\\_init\\_\\_(learning\\_rate,\\ learning\\_rate\\_power=-0.5,\\ initial\\_accumulator\\_value=0.1,\\ l1\\_regularization\\_strength=0.0,\\ l2\\_regularization\\_strength=0.0,\\ use\\_locking=False,\\ name=\\textquotesingle{}Ftrl\\textquotesingle{})}\r\n}{tf.train.FtrlOptimizer.\\_\\_init\\_\\_(learning\\_rate, learning\\_rate\\_power=-0.5, initial\\_accumulator\\_value=0.1, l1\\_regularization\\_strength=0.0, l2\\_regularization\\_strength=0.0, use\\_locking=False, name='Ftrl') }}\\label{tf.train.ftrloptimizer.ux5fux5finitux5fux5flearningux5frate-learningux5frateux5fpower-0.5-initialux5faccumulatorux5fvalue0.1-l1ux5fregularizationux5fstrength0.0-l2ux5fregularizationux5fstrength0.0-useux5flockingfalse-nameftrl}\r\n\r\nConstruct a new FTRL optimizer.\r\n\r\nThe Ftrl-proximal algorithm, abbreviated for\r\nFollow-the-regularized-leader, is described in the paper\r\n\\href{https://www.eecs.tufts.edu/~dsculley/papers/ad-click-prediction.pdf}{Ad\r\nClick Prediction: a View from the Trenches}.\r\n\r\nIt can give a good performance vs.~sparsity tradeoff.\r\n\r\nFtrl-proximal uses its own global base learning rate and can behave like\r\nAdagrad with \\texttt{learning\\_rate\\_power=-0.5}, or like gradient\r\ndescent with \\texttt{learning\\_rate\\_power=0.0}.\r\n\r\nThe effective learning rate is adjusted per parameter, relative to this\r\nbase learning rate as:\r\n\r\n\\begin{verbatim}\r\neffective_learning_rate_i = (learning_rate /\r\n    pow(k + summed_squared_gradients_for_i, learning_rate_power));\r\n\\end{verbatim}\r\n\r\nwhere k is the small constant \\texttt{initial\\_accumulator\\_value}.\r\n\r\nNote that the real regularization coefficient of\r\n\\texttt{\\textbar{}w\\textbar{}\\^{}2} for objective function is\r\n\\texttt{1\\ /\\ lambda\\_2} if specifying \\texttt{l2\\ =\\ lambda\\_2} as\r\nargument when using this function.\r\n\r\n\\subparagraph{Args: }\\label{args-9}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{learning\\_rate}: A float value or a constant float\r\n  \\texttt{Tensor}.\r\n\\item\r\n  \\texttt{learning\\_rate\\_power}: A float value, must be less or equal\r\n  to zero.\r\n\\item\r\n  \\texttt{initial\\_accumulator\\_value}: The starting value for\r\n  accumulators. Only positive values are allowed.\r\n\\item\r\n  \\texttt{l1\\_regularization\\_strength}: A float value, must be greater\r\n  than or equal to zero.\r\n\\item\r\n  \\texttt{l2\\_regularization\\_strength}: A float value, must be greater\r\n  than or equal to zero.\r\n\\item\r\n  \\texttt{use\\_locking}: If \\texttt{True} use locks for update\r\n  operations.\r\n\\item\r\n  \\texttt{name}: Optional name prefix for the operations created when\r\n  applying gradients. Defaults to ``Ftrl''.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-5}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ValueError}: if one of the arguments is invalid.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.train.RMSPropOptimizer}\r\n}{class tf.train.RMSPropOptimizer }}\\label{class-tf.train.rmspropoptimizer}\r\n\r\nOptimizer that implements the RMSProp algorithm.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.RMSPropOptimizer.\\_\\_init\\_\\_(learning\\_rate,\\ decay,\\ momentum=0.0,\\ epsilon=1e-10,\\ use\\_locking=False,\\ name=\\textquotesingle{}RMSProp\\textquotesingle{})}\r\n}{tf.train.RMSPropOptimizer.\\_\\_init\\_\\_(learning\\_rate, decay, momentum=0.0, epsilon=1e-10, use\\_locking=False, name='RMSProp') }}\\label{tf.train.rmspropoptimizer.ux5fux5finitux5fux5flearningux5frate-decay-momentum0.0-epsilon1e-10-useux5flockingfalse-namermsprop}\r\n\r\nConstruct a new RMSProp optimizer.\r\n\r\n\\subparagraph{Args: }\\label{args-10}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{learning\\_rate}: A Tensor or a floating point value. The\r\n  learning rate.\r\n\\item\r\n  \\texttt{decay}: discounting factor for the history/coming gradient\r\n\\item\r\n  \\texttt{momentum}: a scalar tensor.\r\n\\item\r\n  \\texttt{epsilon}: small value to avoid zero denominator.\r\n\\item\r\n  \\texttt{use\\_locking}: If True use locks for update operation.\r\n\\item\r\n  \\texttt{name}: Optional name prefic for the operations created when\r\n  applying gradients. Defaults to ``RMSProp''.\r\n\\end{itemize}\r\n\r\n\\subsection{Gradient Computation }\\label{gradient-computation}\r\n\r\nTensorFlow provides functions to compute the derivatives for a given\r\nTensorFlow computation graph, adding operations to the graph. The\r\noptimizer classes automatically compute derivatives on your graph, but\r\ncreators of new Optimizers or expert users can call the lower-level\r\nfunctions below.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.gradients(ys,\\ xs,\\ grad\\_ys=None,\\ name=\\textquotesingle{}gradients\\textquotesingle{},\\ colocate\\_gradients\\_with\\_ops=False,\\ gate\\_gradients=False,\\ aggregation\\_method=None)}\r\n}{tf.gradients(ys, xs, grad\\_ys=None, name='gradients', colocate\\_gradients\\_with\\_ops=False, gate\\_gradients=False, aggregation\\_method=None) }}\\label{tf.gradientsys-xs-gradux5fysnone-namegradients-colocateux5fgradientsux5fwithux5fopsfalse-gateux5fgradientsfalse-aggregationux5fmethodnone}\r\n\r\nConstructs symbolic partial derivatives of \\texttt{ys} w.r.t. x in\r\n\\texttt{xs}.\r\n\r\n\\texttt{ys} and \\texttt{xs} are each a \\texttt{Tensor} or a list of\r\ntensors. \\texttt{grad\\_ys} is a list of \\texttt{Tensor}, holding the\r\ngradients received by the \\texttt{ys}. The list must be the same length\r\nas \\texttt{ys}.\r\n\r\n\\texttt{gradients()} adds ops to the graph to output the partial\r\nderivatives of \\texttt{ys} with respect to \\texttt{xs}. It returns a\r\nlist of \\texttt{Tensor} of length \\texttt{len(xs)} where each tensor is\r\nthe \\texttt{sum(dy/dx)} for y in \\texttt{ys}.\r\n\r\n\\texttt{grad\\_ys} is a list of tensors of the same length as \\texttt{ys}\r\nthat holds the initial gradients for each y in \\texttt{ys}. When\r\n\\texttt{grad\\_ys} is None, we fill in a tensor of '1's of the shape of y\r\nfor each y in \\texttt{ys}. A user can provide their own initial\r\n'grad\\_ys` to compute the derivatives using a different initial gradient\r\nfor each y (e.g., if one wanted to weight the gradient differently for\r\neach value in each y).\r\n\r\n\\subparagraph{Args: }\\label{args-11}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ys}: A \\texttt{Tensor} or list of tensors to be\r\n  differentiated.\r\n\\item\r\n  \\texttt{xs}: A \\texttt{Tensor} or list of tensors to be used for\r\n  differentiation.\r\n\\item\r\n  \\texttt{grad\\_ys}: Optional. A \\texttt{Tensor} or list of tensors the\r\n  same size as \\texttt{ys} and holding the gradients computed for each y\r\n  in \\texttt{ys}.\r\n\\item\r\n  \\texttt{name}: Optional name to use for grouping all the gradient ops\r\n  together. defaults to `gradients'.\r\n\\item\r\n  \\texttt{colocate\\_gradients\\_with\\_ops}: If True, try colocating\r\n  gradients with the corresponding op.\r\n\\item\r\n  \\texttt{gate\\_gradients}: If True, add a tuple around the gradients\r\n  returned for an operations. This avoids some race conditions.\r\n\\item\r\n  \\texttt{aggregation\\_method}: Specifies the method used to combine\r\n  gradient terms. Accepted values are constants defined in the class\r\n  \\texttt{AggregationMethod}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-5}\r\n\r\nA list of \\texttt{sum(dy/dx)} for each x in \\texttt{xs}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-6}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{LookupError}: if one of the operations between \\texttt{x} and\r\n  \\texttt{y} does not have a registered gradient function.\r\n\\item\r\n  \\texttt{ValueError}: if the arguments are invalid.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.AggregationMethod}\r\n}{class tf.AggregationMethod }}\\label{class-tf.aggregationmethod}\r\n\r\nA class listing aggregation methods used to combine gradients.\r\n\r\nComputing partial derivatives can require aggregating gradient\r\ncontributions. This class lists the various methods that can be used to\r\ncombine gradients in the graph:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ADD\\_N}: All of the gradient terms are summed as part of one\r\n  operation using the ``AddN'' op. It has the property that all\r\n  gradients must be ready before any aggregation is performed.\r\n\\item\r\n  \\texttt{DEFAULT}: The system-chosen default aggregation method.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.stop\\_gradient(input,\\ name=None)}\r\n}{tf.stop\\_gradient(input, name=None) }}\\label{tf.stopux5fgradientinput-namenone}\r\n\r\nStops gradient computation.\r\n\r\nWhen executed in a graph, this op outputs its input tensor as-is.\r\n\r\nWhen building ops to compute gradients, this op prevents the\r\ncontribution of its inputs to be taken into account. Normally, the\r\ngradient generator adds ops to a graph to compute the derivatives of a\r\nspecified `loss' by recursively finding out inputs that contributed to\r\nits computation. If you insert this op in the graph it inputs are masked\r\nfrom the gradient generator. They are not taken into account for\r\ncomputing gradients.\r\n\r\nThis is useful any time you want to compute a value with TensorFlow but\r\nneed to pretend that the value was a constant. Some examples include:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  The \\emph{EM} algorithm where the \\emph{M-step} should not involve\r\n  backpropagation through the output of the \\emph{E-step}.\r\n\\item\r\n  Contrastive divergence training of Boltzmann machines where, when\r\n  differentiating the energy function, the training must not\r\n  backpropagate through the graph that generated the samples from the\r\n  model.\r\n\\item\r\n  Adversarial training, where no backprop should happen through the\r\n  adversarial example generation process.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Args: }\\label{args-12}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{input}: A \\texttt{Tensor}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-6}\r\n\r\nA \\texttt{Tensor}. Has the same type as \\texttt{input}.\r\n\r\n\\subsection{Gradient Clipping }\\label{gradient-clipping}\r\n\r\nTensorFlow provides several operations that you can use to add clipping\r\nfunctions to your graph. You can use these functions to perform general\r\ndata clipping, but they're particularly useful for handling exploding or\r\nvanishing gradients.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.clip\\_by\\_value(t,\\ clip\\_value\\_min,\\ clip\\_value\\_max,\\ name=None)}\r\n}{tf.clip\\_by\\_value(t, clip\\_value\\_min, clip\\_value\\_max, name=None) }}\\label{tf.clipux5fbyux5fvaluet-clipux5fvalueux5fmin-clipux5fvalueux5fmax-namenone}\r\n\r\nClips tensor values to a specified min and max.\r\n\r\nGiven a tensor \\texttt{t}, this operation returns a tensor of the same\r\ntype and shape as \\texttt{t} with its values clipped to\r\n\\texttt{clip\\_value\\_min} and \\texttt{clip\\_value\\_max}. Any values less\r\nthan \\texttt{clip\\_value\\_min} are set to \\texttt{clip\\_value\\_min}. Any\r\nvalues greater than \\texttt{clip\\_value\\_max} are set to\r\n\\texttt{clip\\_value\\_max}.\r\n\r\n\\subparagraph{Args: }\\label{args-13}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{t}: A \\texttt{Tensor}.\r\n\\item\r\n  \\texttt{clip\\_value\\_min}: A 0-D (scalar) \\texttt{Tensor}. The minimum\r\n  value to clip by.\r\n\\item\r\n  \\texttt{clip\\_value\\_max}: A 0-D (scalar) \\texttt{Tensor}. The maximum\r\n  value to clip by.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-7}\r\n\r\nA clipped \\texttt{Tensor}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.clip\\_by\\_norm(t,\\ clip\\_norm,\\ name=None)}\r\n}{tf.clip\\_by\\_norm(t, clip\\_norm, name=None) }}\\label{tf.clipux5fbyux5fnormt-clipux5fnorm-namenone}\r\n\r\nClips tensor values to a maximum L2-norm.\r\n\r\nGiven a tensor \\texttt{t}, and a maximum clip value \\texttt{clip\\_norm},\r\nthis operation normalizes \\texttt{t} so that its L2-norm is less than or\r\nequal to\r\n\\texttt{clip\\_norm\\textquotesingle{}.\\ Specifically,\\ if\\ the\\ L2-norm\\ is\\ already\\ less\\ than\\ or\\ equal\\ to}clip\\_norm\\texttt{,\\ then}t\\texttt{is\\ not\\ modified.\\ If\\ the\\ L2-norm\\ is\\ greater\\ than}clip\\_norm\\texttt{,\\ then\\ this\\ operation\\ returns\\ a\\ tensor\\ of\\ the\\ same\\ type\\ and\\ shape\\ as}t`\r\nwith its values set to:\r\n\r\n\\texttt{t\\ *\\ clip\\_norm\\ /\\ l2norm(t)}\r\n\r\nIn this case, the L2-norm of the output tensor is \\texttt{clip\\_norm}.\r\n\r\nThis operation is typically used to clip gradients before applying them\r\nwith an optimizer.\r\n\r\n\\subparagraph{Args: }\\label{args-14}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{t}: A \\texttt{Tensor}.\r\n\\item\r\n  \\texttt{clip\\_norm}: A 0-D (scalar) \\texttt{Tensor} \\textgreater{} 0.\r\n  A maximum clipping value.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-8}\r\n\r\nA clipped \\texttt{Tensor}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.clip\\_by\\_average\\_norm(t,\\ clip\\_norm,\\ name=None)}\r\n}{tf.clip\\_by\\_average\\_norm(t, clip\\_norm, name=None) }}\\label{tf.clipux5fbyux5faverageux5fnormt-clipux5fnorm-namenone}\r\n\r\nClips tensor values to a maximum average L2-norm.\r\n\r\nGiven a tensor \\texttt{t}, and a maximum clip value \\texttt{clip\\_norm},\r\nthis operation normalizes \\texttt{t} so that its average L2-norm is less\r\nthan or equal to\r\n\\texttt{clip\\_norm\\textquotesingle{}.\\ Specifically,\\ if\\ the\\ average\\ L2-norm\\ is\\ already\\ less\\ than\\ or\\ equal\\ to}clip\\_norm\\texttt{,\\ then}t\\texttt{is\\ not\\ modified.\\ If\\ the\\ average\\ L2-norm\\ is\\ greater\\ than}clip\\_norm\\texttt{,\\ then\\ this\\ operation\\ returns\\ a\\ tensor\\ of\\ the\\ same\\ type\\ and\\ shape\\ as}t`\r\nwith its values set to:\r\n\r\n\\texttt{t\\ *\\ clip\\_norm\\ /\\ l2norm\\_avg(t)}\r\n\r\nIn this case, the average L2-norm of the output tensor is\r\n\\texttt{clip\\_norm}.\r\n\r\nThis operation is typically used to clip gradients before applying them\r\nwith an optimizer.\r\n\r\n\\subparagraph{Args: }\\label{args-15}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{t}: A \\texttt{Tensor}.\r\n\\item\r\n  \\texttt{clip\\_norm}: A 0-D (scalar) \\texttt{Tensor} \\textgreater{} 0.\r\n  A maximum clipping value.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-9}\r\n\r\nA clipped \\texttt{Tensor}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.clip\\_by\\_global\\_norm(t\\_list,\\ clip\\_norm,\\ use\\_norm=None,\\ name=None)}\r\n}{tf.clip\\_by\\_global\\_norm(t\\_list, clip\\_norm, use\\_norm=None, name=None) }}\\label{tf.clipux5fbyux5fglobalux5fnormtux5flist-clipux5fnorm-useux5fnormnone-namenone}\r\n\r\nClips values of multiple tensors by the ratio of the sum of their norms.\r\n\r\nGiven a tuple or list of tensors \\texttt{t\\_list}, and a clipping ratio\r\n\\texttt{clip\\_norm}, this operation returns a list of clipped tensors\r\n\\texttt{list\\_clipped} and the global norm (\\texttt{global\\_norm}) of\r\nall tensors in \\texttt{t\\_list}. Optionally, if you've already computed\r\nthe global norm for \\texttt{t\\_list}, you can specify the global norm\r\nwith \\texttt{use\\_norm}.\r\n\r\nTo perform the clipping, the values t\\_list{[}i{]} are set to:\r\n\r\n\\texttt{t\\_list{[}i{]}\\ *\\ clip\\_norm\\ /\\ max(global\\_norm,\\ clip\\_norm)}\r\n\r\nwhere:\r\n\r\n\\texttt{global\\_norm\\ =\\ sqrt(sum({[}l2norm(t)**2\\ for\\ t\\ in\\ t\\_list{]}))}\r\n\r\nIf \\texttt{clip\\_norm\\ \\textgreater{}\\ global\\_norm} then the entries in\r\n\\texttt{t\\_list} remain as they are, otherwise they're all shrunk by the\r\nglobal ratio.\r\n\r\nAny of the entries of \\texttt{t\\_list} that are of type None are\r\nignored.\r\n\r\nThis is the correct way to perform gradient clipping (for example, see\r\nR. Pascanu, T. Mikolov, and Y. Bengio, ``On the difficulty of training\r\nRecurrent Neural Networks''. http://arxiv.org/abs/1211.5063)\r\n\r\nHowever, it is slower than \\texttt{clip\\_by\\_norm()} because all the\r\nparameters must be ready before the clipping operation can be performed.\r\n\r\n\\subparagraph{Args: }\\label{args-16}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{t\\_list}: A tuple or list of mixed \\texttt{Tensors},\r\n  \\texttt{IndexedSlices}, or None.\r\n\\item\r\n  \\texttt{clip\\_norm}: A 0-D (scalar) \\texttt{Tensor} \\textgreater{} 0.\r\n  The clipping ratio.\r\n\\item\r\n  \\texttt{use\\_norm}: A 0-D (scalar) \\texttt{Tensor} of type\r\n  \\texttt{float} (optional). The global norm to use. If not provided,\r\n  \\texttt{global\\_norm()} is used to compute the norm.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-10}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{list\\_clipped}: A list of \\texttt{Tensors} of the same type as\r\n  \\texttt{list\\_t}.\r\n\\item\r\n  \\texttt{global\\_norm}: A 0-D (scalar) \\texttt{Tensor} representing the\r\n  global norm.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-7}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{TypeError}: If \\texttt{t\\_list} is not a sequence.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.global\\_norm(t\\_list,\\ name=None)}\r\n}{tf.global\\_norm(t\\_list, name=None) }}\\label{tf.globalux5fnormtux5flist-namenone}\r\n\r\nComputes the global norm of multiple tensors.\r\n\r\nGiven a tuple or list of tensors \\texttt{t\\_list}, this operation\r\nreturns the global norm of the elements in all tensors in\r\n\\texttt{t\\_list}. The global norm is computed as:\r\n\r\n\\texttt{global\\_norm\\ =\\ sqrt(sum({[}l2norm(t)**2\\ for\\ t\\ in\\ t\\_list{]}))}\r\n\r\nAny entries in \\texttt{t\\_list} that are of type None are ignored.\r\n\r\n\\subparagraph{Args: }\\label{args-17}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{t\\_list}: A tuple or list of mixed \\texttt{Tensors},\r\n  \\texttt{IndexedSlices}, or None.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-11}\r\n\r\nA 0-D (scalar) \\texttt{Tensor} of type \\texttt{float}.\r\n\r\n\\subparagraph{Raises: }\\label{raises-8}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{TypeError}: If \\texttt{t\\_list} is not a sequence.\r\n\\end{itemize}\r\n\r\n\\subsection{Decaying the learning rate\r\n}\\label{decaying-the-learning-rate}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.exponential\\_decay(learning\\_rate,\\ global\\_step,\\ decay\\_steps,\\ decay\\_rate,\\ staircase=False,\\ name=None)}\r\n}{tf.train.exponential\\_decay(learning\\_rate, global\\_step, decay\\_steps, decay\\_rate, staircase=False, name=None) }}\\label{tf.train.exponentialux5fdecaylearningux5frate-globalux5fstep-decayux5fsteps-decayux5frate-staircasefalse-namenone}\r\n\r\nApplies exponential decay to the learning rate.\r\n\r\nWhen training a model, it is often recommended to lower the learning\r\nrate as the training progresses. This function applies an exponential\r\ndecay function to a provided initial learning rate. It requires a\r\n\\texttt{global\\_step} value to compute the decayed learning rate. You\r\ncan just pass a TensorFlow variable that you increment at each training\r\nstep.\r\n\r\nThe function returns the decayed learning rate. It is computed as:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{decayed_learning_rate }\\OperatorTok{=} \\NormalTok{learning_rate }\\OperatorTok{*}\r\n                        \\NormalTok{decay_rate }\\OperatorTok{^} \\NormalTok{(global_step }\\OperatorTok{/} \\NormalTok{decay_steps)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nIf the argument \\texttt{staircase} is \\texttt{True}, then\r\n\\texttt{global\\_step\\ /decay\\_steps} is an integer division and the\r\ndecayed learning rate follows a staircase function.\r\n\r\nExample: decay every 100000 steps with a base of 0.96:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{...}\r\n\\NormalTok{global_step }\\OperatorTok{=} \\NormalTok{tf.Variable(}\\DecValTok{0}\\NormalTok{, trainable}\\OperatorTok{=}\\VariableTok{False}\\NormalTok{)}\r\n\\NormalTok{starter_learning_rate }\\OperatorTok{=} \\FloatTok{0.1}\r\n\\NormalTok{learning_rate }\\OperatorTok{=} \\NormalTok{tf.exponential_decay(starter_learning_rate, global_step,}\r\n                                     \\DecValTok{100000}\\NormalTok{, }\\FloatTok{0.96}\\NormalTok{, staircase}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{)}\r\n\\NormalTok{optimizer }\\OperatorTok{=} \\NormalTok{tf.GradientDescent(learning_rate)}\r\n\\CommentTok{# Passing global_step to minimize() will increment it at each step.}\r\n\\NormalTok{optimizer.minimize(...my loss..., global_step}\\OperatorTok{=}\\NormalTok{global_step)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-18}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{learning\\_rate}: A scalar \\texttt{float32} or \\texttt{float64}\r\n  \\texttt{Tensor} or a Python number. The initial learning rate.\r\n\\item\r\n  \\texttt{global\\_step}: A scalar \\texttt{int32} or \\texttt{int64}\r\n  \\texttt{Tensor} or a Python number. Global step to use for the decay\r\n  computation. Must not be negative.\r\n\\item\r\n  \\texttt{decay\\_steps}: A scalar \\texttt{int32} or \\texttt{int64}\r\n  \\texttt{Tensor} or a Python number. Must be positive. See the decay\r\n  computation above.\r\n\\item\r\n  \\texttt{decay\\_rate}: A scalar \\texttt{float32} or \\texttt{float64}\r\n  \\texttt{Tensor} or a Python number. The decay rate.\r\n\\item\r\n  \\texttt{staircase}: Boolean. It \\texttt{True} decay the learning rate\r\n  at discrete intervals.\r\n\\item\r\n  \\texttt{name}: string. Optional name of the operation. Defaults to\r\n  `ExponentialDecay'\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-12}\r\n\r\nA scalar \\texttt{Tensor} of the same type as \\texttt{learning\\_rate}.\r\nThe decayed learning rate.\r\n\r\n\\subsection{Moving Averages }\\label{moving-averages}\r\n\r\nSome training algorithms, such as GradientDescent and Momentum often\r\nbenefit from maintaining a moving average of variables during\r\noptimization. Using the moving averages for evaluations often improve\r\nresults significantly.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.train.ExponentialMovingAverage}\r\n}{class tf.train.ExponentialMovingAverage }}\\label{class-tf.train.exponentialmovingaverage}\r\n\r\nMaintains moving averages of variables by employing and exponential\r\ndecay.\r\n\r\nWhen training a model, it is often beneficial to maintain moving\r\naverages of the trained parameters. Evaluations that use averaged\r\nparameters sometimes produce significantly better results than the final\r\ntrained values.\r\n\r\nThe \\texttt{apply()} method adds shadow copies of trained variables and\r\nadd ops that maintain a moving average of the trained variables in their\r\nshadow copies. It is used when building the training model. The ops that\r\nmaintain moving averages are typically run after each training step. The\r\n\\texttt{average()} and \\texttt{average\\_name()} methods give access to\r\nthe shadow variables and their names. They are useful when building an\r\nevaluation model, or when restoring a model from a checkpoint file. They\r\nhelp use the moving averages in place of the last trained values for\r\nevaluations.\r\n\r\nThe moving averages are computed using exponential decay. You specify\r\nthe decay value when creating the \\texttt{ExponentialMovingAverage}\r\nobject. The shadow variables are initialized with the same initial\r\nvalues as the trained variables. When you run the ops to maintain the\r\nmoving averages, each shadow variable is updated with the formula:\r\n\r\n\\texttt{shadow\\_variable\\ -=\\ (1\\ -\\ decay)\\ *\\ (shadow\\_variable\\ -\\ variable)}\r\n\r\nThis is mathematically equivalent to the classic formula below, but the\r\nuse of an \\texttt{assign\\_sub} op (the \\texttt{\"-=\"} in the formula)\r\nallows concurrent lockless updates to the variables:\r\n\r\n\\texttt{shadow\\_variable\\ =\\ decay\\ *\\ shadow\\_variable\\ +\\ (1\\ -\\ decay)\\ *\\ variable}\r\n\r\nReasonable values for \\texttt{decay} are close to 1.0, typically in the\r\nmultiple-nines range: 0.999, 0.9999, etc.\r\n\r\nExample usage when creating a training model:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Create variables.}\r\n\\NormalTok{var0 }\\OperatorTok{=} \\NormalTok{tf.Variable(...)}\r\n\\NormalTok{var1 }\\OperatorTok{=} \\NormalTok{tf.Variable(...)}\r\n\\CommentTok{# ... use the variables to build a training model...}\r\n\\NormalTok{...}\r\n\\CommentTok{# Create an op that applies the optimizer.  This is what we usually}\r\n\\CommentTok{# would use as a training op.}\r\n\\NormalTok{opt_op }\\OperatorTok{=} \\NormalTok{opt.minimize(my_loss, [var0, var1])}\r\n\r\n\\CommentTok{# Create an ExponentialMovingAverage object}\r\n\\NormalTok{ema }\\OperatorTok{=} \\NormalTok{tf.train.ExponentialMovingAverage(decay}\\OperatorTok{=}\\FloatTok{0.9999}\\NormalTok{)}\r\n\r\n\\CommentTok{# Create the shadow variables, and add ops to maintain moving averages}\r\n\\CommentTok{# of var0 and var1.}\r\n\\NormalTok{maintain_averages_op }\\OperatorTok{=} \\NormalTok{ema.}\\BuiltInTok{apply}\\NormalTok{([var0, var1])}\r\n\r\n\\CommentTok{# Create an op that will update the moving averages after each training}\r\n\\CommentTok{# step.  This is what we will use in place of the usuall trainig op.}\r\n\\ControlFlowTok{with} \\NormalTok{tf.control_dependencies([opt_op]):}\r\n    \\NormalTok{training_op }\\OperatorTok{=} \\NormalTok{tf.group(maintain_averages_op)}\r\n\r\n\\NormalTok{...train the model by running training_op...}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nThere are two ways to use the moving averages for evaluations:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  Build a model that uses the shadow variables instead of the variables.\r\n  For this, use the \\texttt{average()} method which returns the shadow\r\n  variable for a given variable.\r\n\\item\r\n  Build a model normally but load the checkpoint files to evaluate by\r\n  using the shadow variable names. For this use the\r\n  \\texttt{average\\_name()} method. See the\r\n  \\href{../../api_docs/python/train.md\\#Saver}{Saver class} for more\r\n  information on restoring saved variables.\r\n\\end{itemize}\r\n\r\nExample of restoring the shadow variable values:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Create a Saver that loads variables from their saved shadow values.}\r\n\\NormalTok{shadow_var0_name }\\OperatorTok{=} \\NormalTok{ema.average_name(var0)}\r\n\\NormalTok{shadow_var1_name }\\OperatorTok{=} \\NormalTok{ema.average_name(var1)}\r\n\\NormalTok{saver }\\OperatorTok{=} \\NormalTok{tf.train.Saver(\\{shadow_var0_name: var0, shadow_var1_name: var1\\})}\r\n\\NormalTok{saver.restore(...checkpoint filename...)}\r\n\\CommentTok{# var0 and var1 now hold the moving average values}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.ExponentialMovingAverage.\\_\\_init\\_\\_(decay,\\ num\\_updates=None,\\ name=\\textquotesingle{}ExponentialMovingAverage\\textquotesingle{})}\r\n}{tf.train.ExponentialMovingAverage.\\_\\_init\\_\\_(decay, num\\_updates=None, name='ExponentialMovingAverage') }}\\label{tf.train.exponentialmovingaverage.ux5fux5finitux5fux5fdecay-numux5fupdatesnone-nameexponentialmovingaverage}\r\n\r\nCreates a new ExponentialMovingAverage object.\r\n\r\nThe \\texttt{Apply()} method has to be called to create shadow variables\r\nand add ops to maintain moving averages.\r\n\r\nThe optional \\texttt{num\\_updates} parameter allows one to tweak the\r\ndecay rate dynamically. . It is typical to pass the count of training\r\nsteps, usually kept in a variable that is incremented at each step, in\r\nwhich case the decay rate is lower at the start of training. This makes\r\nmoving averages move faster. If passed, the actual decay rate used is:\r\n\r\n\\texttt{min(decay,\\ (1\\ +\\ num\\_updates)\\ /\\ (10\\ +\\ num\\_updates))}\r\n\r\n\\subparagraph{Args: }\\label{args-19}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{decay}: Float. The decay to use.\r\n\\item\r\n  \\texttt{num\\_updates}: Optional count of number of updates applied to\r\n  variables.\r\n\\item\r\n  \\texttt{name}: String. Optional prefix name to use for the name of ops\r\n  added in \\texttt{Apply()}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.ExponentialMovingAverage.apply(var\\_list=None)}\r\n}{tf.train.ExponentialMovingAverage.apply(var\\_list=None) }}\\label{tf.train.exponentialmovingaverage.applyvarux5flistnone}\r\n\r\nMaintains moving averages of variables.\r\n\r\n\\texttt{var\\_list} must be a list of \\texttt{Variable} or\r\n\\texttt{Tensor} objects. This method creates shadow variables for all\r\nelements of \\texttt{var\\_list}. Shadow variables for \\texttt{Variable}\r\nobjects are initialized to the variable's initial value. For\r\n\\texttt{Tensor} objects, the shadow variables are initialized to 0.\r\n\r\nshadow variables are created with \\texttt{trainable=False} and added to\r\nthe \\texttt{GraphKeys.ALL\\_VARIABLES} collection. They will be returned\r\nby calls to \\texttt{tf.all\\_variables()}.\r\n\r\nReturns an op that updates all shadow variables as described above.\r\n\r\nNote that \\texttt{apply()} can be called multiple times with different\r\nlists of variables.\r\n\r\n\\subparagraph{Args: }\\label{args-20}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{var\\_list}: A list of Variable or Tensor objects. The\r\n  variables and Tensors must be of types float32 or float64.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-13}\r\n\r\nAn Operation that updates the moving averages.\r\n\r\n\\subparagraph{Raises: }\\label{raises-9}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{TypeError}: If the arguments are not all float32 or float64.\r\n\\item\r\n  \\texttt{ValueError}: If the moving average of one of the variables is\r\n  already being computed.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.ExponentialMovingAverage.average\\_name(var)}\r\n}{tf.train.ExponentialMovingAverage.average\\_name(var) }}\\label{tf.train.exponentialmovingaverage.averageux5fnamevar}\r\n\r\nReturns the name of the \\texttt{Variable} holding the average for\r\n\\texttt{var}.\r\n\r\nThe typical scenario for \\texttt{ExponentialMovingAverage} is to compute\r\nmoving averages of variables during training, and restore the variables\r\nfrom the computed moving averages during evaluations.\r\n\r\nTo restore variables, you have to know the name of the shadow variables.\r\nThat name and the original variable can then be passed to a\r\n\\texttt{Saver()} object to restore the variable from the moving average\r\nvalue with:\r\n\\texttt{saver\\ =\\ tf.train.Saver(\\{ema.average\\_name(var):\\ var\\})}\r\n\r\n\\texttt{average\\_name()} can be called whether or not \\texttt{apply()}\r\nhas been called.\r\n\r\n\\subparagraph{Args: }\\label{args-21}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{var}: A \\texttt{Variable} object.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-14}\r\n\r\nA string: the name of the variable that will be used or was used by the\r\n\\texttt{ExponentialMovingAverage\\ class} to hold the moving average of\r\n\\texttt{var}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.ExponentialMovingAverage.average(var)}\r\n}{tf.train.ExponentialMovingAverage.average(var) }}\\label{tf.train.exponentialmovingaverage.averagevar}\r\n\r\nReturns the \\texttt{Variable} holding the average of \\texttt{var}.\r\n\r\n\\subparagraph{Args: }\\label{args-22}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{var}: A \\texttt{Variable} object.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-15}\r\n\r\nA \\texttt{Variable} object or \\texttt{None} if the moving average of\r\n\\texttt{var} is not maintained..\r\n\r\n\\subsection{Coordinator and QueueRunner\r\n}\\label{coordinator-and-queuerunner}\r\n\r\nSee \\href{../../how_tos/threading_and_queues/index.md}{Threading and\r\nQueues} for how to use threads and queues. For documentation on the\r\nQueue API, see \\href{../../api_docs/python/io_ops.md\\#queues}{Queues}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.train.Coordinator}\r\n}{class tf.train.Coordinator }}\\label{class-tf.train.coordinator}\r\n\r\nA coordinator for threads.\r\n\r\nThis class implements a simple mechanism to coordinate the termination\r\nof a set of threads.\r\n\r\n\\paragraph{Usage: }\\label{usage-1}\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Create a coordinator.}\r\n\\NormalTok{coord }\\OperatorTok{=} \\NormalTok{Coordinator()}\r\n\\CommentTok{# Start a number of threads, passing the coordinator to each of them.}\r\n\\NormalTok{...start thread }\\DecValTok{1}\\NormalTok{...(coord, ...)}\r\n\\NormalTok{...start thread N...(coord, ...)}\r\n\\CommentTok{# Wait for all the threads to terminate.}\r\n\\NormalTok{coord.join(threads)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nAny of the threads can call \\texttt{coord.request\\_stop()} to ask for\r\nall the threads to stop. To cooperate with the requests, each thread\r\nmust check for \\texttt{coord.should\\_stop()} on a regular basis.\r\n\\texttt{coord.should\\_stop()} returns \\texttt{True} as soon as\r\n\\texttt{coord.request\\_stop()} has been called.\r\n\r\nA typical thread running with a Coordinator will do something like:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{while} \\OperatorTok{not} \\NormalTok{coord.should_stop():}\r\n   \\NormalTok{...do some work...}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\paragraph{Exception handling: }\\label{exception-handling}\r\n\r\nA thread can report an exception to the Coordinator as part of the\r\n\\texttt{should\\_stop()} call. The exception will be re-raised from the\r\n\\texttt{coord.join()} call.\r\n\r\nThread code:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{try}\\NormalTok{:}\r\n  \\ControlFlowTok{while} \\OperatorTok{not} \\NormalTok{coord.should_stop():}\r\n    \\NormalTok{...do some work...}\r\n\\ControlFlowTok{except} \\PreprocessorTok{Exception}\\NormalTok{, e:}\r\n  \\NormalTok{coord.request_stop(e)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nMain code:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{try}\\NormalTok{:}\r\n  \\NormalTok{...}\r\n  \\NormalTok{coord }\\OperatorTok{=} \\NormalTok{Coordinator()}\r\n  \\CommentTok{# Start a number of threads, passing the coordinator to each of them.}\r\n  \\NormalTok{...start thread }\\DecValTok{1}\\NormalTok{...(coord, ...)}\r\n  \\NormalTok{...start thread N...(coord, ...)}\r\n  \\CommentTok{# Wait for all the threads to terminate.}\r\n  \\NormalTok{coord.join(threads)}\r\n\\ControlFlowTok{except} \\PreprocessorTok{Exception}\\NormalTok{, e:}\r\n  \\NormalTok{...exception that was passed to coord.request_stop()}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\paragraph{Grace period for stopping: }\\label{grace-period-for-stopping}\r\n\r\nAfter a thread has called \\texttt{coord.request\\_stop()} the other\r\nthreads have a fixed time to stop, this is called the `stop grace\r\nperiod' and defaults to 2 minutes. If any of the threads is still alive\r\nafter the grace period expires \\texttt{coord.join()} raises a\r\nRuntimeException reporting the laggards.\r\n\r\n\\begin{verbatim}\r\ntry:\r\n  ...\r\n  coord = Coordinator()\r\n  # Start a number of threads, passing the coordinator to each of them.\r\n  ...start thread 1...(coord, ...)\r\n  ...start thread N...(coord, ...)\r\n  # Wait for all the threads to terminate, give them 10s grace period\r\n  coord.join(threads, stop_grace_period_secs=10)\r\nexcept RuntimeException:\r\n  ...one of the threads took more than 10s to stop after request_stop()\r\n  ...was called.\r\nexcept Exception:\r\n  ...exception that was passed to coord.request_stop()\r\n\\end{verbatim}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.Coordinator.\\_\\_init\\_\\_()}\r\n}{tf.train.Coordinator.\\_\\_init\\_\\_() }}\\label{tf.train.coordinator.ux5fux5finitux5fux5f}\r\n\r\nCreate a new Coordinator.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.Coordinator.join(threads,\\ stop\\_grace\\_period\\_secs=120)}\r\n}{tf.train.Coordinator.join(threads, stop\\_grace\\_period\\_secs=120) }}\\label{tf.train.coordinator.jointhreads-stopux5fgraceux5fperiodux5fsecs120}\r\n\r\nWait for threads to terminate.\r\n\r\nBlocks until all `threads' have terminated or request\\_stop() is called.\r\n\r\nAfter the threads stop, if an `exc\\_info' was passed to request\\_stop,\r\nthat exception is re-reaised.\r\n\r\nGrace period handling: When request\\_stop() is called, threads are given\r\n`stop\\_grace\\_period\\_secs' seconds to terminate. If any of them is\r\nstill alive after that period expires, a RuntimeError is raised. Note\r\nthat if an `exc\\_info' was passed to request\\_stop() then it is raised\r\ninstead of that RuntimeError.\r\n\r\n\\subparagraph{Args: }\\label{args-23}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{threads}: List threading.Threads. The started threads to join.\r\n\\item\r\n  \\texttt{stop\\_grace\\_period\\_secs}: Number of seconds given to threads\r\n  to stop after request\\_stop() has been called.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Raises: }\\label{raises-10}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{RuntimeError}: If any thread is still alive after\r\n  request\\_stop() is called and the grace period expires.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.Coordinator.request\\_stop(ex=None)}\r\n}{tf.train.Coordinator.request\\_stop(ex=None) }}\\label{tf.train.coordinator.requestux5fstopexnone}\r\n\r\nRequest that the threads stop.\r\n\r\nAfter this is called, calls to should\\_stop() will return True.\r\n\r\n\\subparagraph{Args: }\\label{args-24}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{ex}: Optional Exception, or Python `exc\\_info' tuple as\r\n  returned by sys.exc\\_info(). If this is the first call to\r\n  request\\_stop() the corresponding exception is recorded and re-raised\r\n  from join().\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.Coordinator.should\\_stop()}\r\n}{tf.train.Coordinator.should\\_stop() }}\\label{tf.train.coordinator.shouldux5fstop}\r\n\r\nCheck if stop was requested.\r\n\r\n\\subparagraph{Returns: }\\label{returns-16}\r\n\r\nTrue if a stop was requested.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.Coordinator.wait\\_for\\_stop(timeout=None)}\r\n}{tf.train.Coordinator.wait\\_for\\_stop(timeout=None) }}\\label{tf.train.coordinator.waitux5fforux5fstoptimeoutnone}\r\n\r\nWait till the Coordinator is told to stop.\r\n\r\n\\subparagraph{Args: }\\label{args-25}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{timeout}: float. Sleep for up to that many seconds waiting for\r\n  should\\_stop() to become True.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-17}\r\n\r\nTrue if the Coordinator is told stop, False if the timeout expired.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.train.QueueRunner}\r\n}{class tf.train.QueueRunner }}\\label{class-tf.train.queuerunner}\r\n\r\nHolds a list of enqueue operations for a queue, each to be run in a\r\nthread.\r\n\r\nQueues are a convenient TensorFlow mechanism to compute tensors\r\nasynchronously using multiple threads. For example in the canonical\r\n`Input Reader' setup one set of threads generates filenames in a queue;\r\na second set of threads read records from the files, processes them, and\r\nenqueues tensors on a second queue; a third set of threads dequeues\r\nthese input records to construct batches and runs them through training\r\noperations.\r\n\r\nThere are several delicate issues when running multiple threads that\r\nway: closing the queues in sequence as the input is exhausted, correctly\r\ncatching and reporting exceptions, etc.\r\n\r\nThe \\texttt{QueueRunner}, combined with the \\texttt{Coordinator}, helps\r\nhandle these issues. - - -\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.QueueRunner.\\_\\_init\\_\\_(queue,\\ enqueue\\_ops)}\r\n}{tf.train.QueueRunner.\\_\\_init\\_\\_(queue, enqueue\\_ops) }}\\label{tf.train.queuerunner.ux5fux5finitux5fux5fqueue-enqueueux5fops}\r\n\r\nCreate a QueueRunner.\r\n\r\nOn construction the \\texttt{QueueRunner} adds an op to close the queue.\r\nThat op will be run if the enqueue ops raise exceptions.\r\n\r\nWhen you later call the \\texttt{create\\_threads()} method, the\r\n\\texttt{QueueRunner} will create one thread for each op in\r\n\\texttt{enqueue\\_ops}. Each thread will run its enqueue op in parallel\r\nwith the other threads. The enqueue ops do not have to all be the same\r\nop, but it is expected that they all enqueue tensors in \\texttt{queue}.\r\n\r\n\\subparagraph{Args: }\\label{args-26}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{queue}: A \\texttt{Queue}.\r\n\\item\r\n  \\texttt{enqueue\\_ops}: List of enqueue ops to run in threads later.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.QueueRunner.create\\_threads(sess,\\ coord=None,\\ daemon=False,\\ start=False)}\r\n}{tf.train.QueueRunner.create\\_threads(sess, coord=None, daemon=False, start=False) }}\\label{tf.train.queuerunner.createux5fthreadssess-coordnone-daemonfalse-startfalse}\r\n\r\nCreate threads to run the enqueue ops.\r\n\r\nThis method requires a session in which the graph was launched. It\r\ncreates a list of threads, optionally starting them. There is one thread\r\nfor each op passed in \\texttt{enqueue\\_ops}.\r\n\r\nThe \\texttt{coord} argument is an optional coordinator, that the threads\r\nwill use to terminate together and report exceptions. If a coordinator\r\nis given, this method starts an additional thread to close the queue\r\nwhen the coordinator requests a stop.\r\n\r\nThis method may be called again as long as all threads from a previous\r\ncall have stopped.\r\n\r\n\\subparagraph{Args: }\\label{args-27}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{sess}: A \\texttt{Session}.\r\n\\item\r\n  \\texttt{coord}: Optional \\texttt{Coordinator} object for reporting\r\n  errors and checking stop conditions.\r\n\\item\r\n  \\texttt{daemon}: Boolean. If \\texttt{True} make the threads daemon\r\n  threads.\r\n\\item\r\n  \\texttt{start}: Boolean. If \\texttt{True} starts the threads. If\r\n  \\texttt{False} the caller must call the \\texttt{start()} method of the\r\n  returned threads.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-18}\r\n\r\nA list of threads.\r\n\r\n\\subparagraph{Raises: }\\label{raises-11}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{RuntimeError}: If threads from a previous call to\r\n  \\texttt{create\\_threads()} are still running.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.QueueRunner.exceptions\\_raised}\r\n}{tf.train.QueueRunner.exceptions\\_raised }}\\label{tf.train.queuerunner.exceptionsux5fraised}\r\n\r\nExceptions raised but not handled by the \\texttt{QueueRunner} threads.\r\n\r\nExceptions raised in queue runner threads are handled in one of two ways\r\ndepending on whether or not a \\texttt{Coordinator} was passed to\r\n\\texttt{create\\_threads()}:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  With a \\texttt{Coordinator}, exceptions are reported to the\r\n  coordinator and forgotten by the \\texttt{QueueRunner}.\r\n\\item\r\n  Without a \\texttt{Coordinator}, exceptions are captured by the\r\n  \\texttt{QueueRunner} and made available in this\r\n  \\texttt{exceptions\\_raised} property.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-19}\r\n\r\nA list of Python \\texttt{Exception} objects. The list is empty if no\r\nexception was captured. (No exceptions are captured when using a\r\nCoordinator.)\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.add\\_queue\\_runner(qr,\\ collection=\\textquotesingle{}queue\\_runners\\textquotesingle{})}\r\n}{tf.train.add\\_queue\\_runner(qr, collection='queue\\_runners') }}\\label{tf.train.addux5fqueueux5frunnerqr-collectionqueueux5frunners}\r\n\r\nAdds a \\texttt{QueueRunner} to a collection in the graph.\r\n\r\nWhen building a complex model that uses many queues it is often\r\ndifficult to gather all the queue runners that need to be run. This\r\nconvenience function allows you to add a queue runner to a well known\r\ncollection in the graph.\r\n\r\nThe companion method \\texttt{start\\_queue\\_runners()} can be used to\r\nstart threads for all the collected queue runners.\r\n\r\n\\subparagraph{Args: }\\label{args-28}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{qr}: A \\texttt{QueueRunner}.\r\n\\item\r\n  \\texttt{collection}: A \\texttt{GraphKey} specifying the graph\r\n  collection to add the queue runner to. Defaults to\r\n  \\texttt{GraphKeys.QUEUE\\_RUNNERS}.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.start\\_queue\\_runners(sess=None,\\ coord=None,\\ daemon=True,\\ start=True,\\ collection=\\textquotesingle{}queue\\_runners\\textquotesingle{})}\r\n}{tf.train.start\\_queue\\_runners(sess=None, coord=None, daemon=True, start=True, collection='queue\\_runners') }}\\label{tf.train.startux5fqueueux5frunnerssessnone-coordnone-daemontrue-starttrue-collectionqueueux5frunners}\r\n\r\nStarts all queue runners collected in the graph.\r\n\r\nThis is a companion method to \\texttt{add\\_queue\\_runner()}. It just\r\nstarts threads for all queue runners collected in the graph. It returns\r\nthe list of all threads.\r\n\r\n\\subparagraph{Args: }\\label{args-29}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{sess}: \\texttt{Session} used to run the queue ops. Defaults to\r\n  the default session.\r\n\\item\r\n  \\texttt{coord}: Optional \\texttt{Coordinator} for coordinating the\r\n  started threads.\r\n\\item\r\n  \\texttt{daemon}: Whether the threads should be marked as\r\n  \\texttt{daemons}, meaning they don't block program exit.\r\n\\item\r\n  \\texttt{start}: Set to \\texttt{False} to only create the threads, not\r\n  start them.\r\n\\item\r\n  \\texttt{collection}: A \\texttt{GraphKey} specifying the graph\r\n  collection to get the queue runners from. Defaults to\r\n  \\texttt{GraphKeys.QUEUE\\_RUNNERS}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-20}\r\n\r\nA list of threads.\r\n\r\n\\subsection{Summary Operations }\\label{summary-operations}\r\n\r\nThe following ops output\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/summary.proto}{\\texttt{Summary}}\r\nprotocol buffers as serialized string tensors.\r\n\r\nYou can fetch the output of a summary op in a session, and pass it to a\r\n\\href{../../api_docs/python/train.md\\#SummaryWriter}{SummaryWriter} to\r\nappend it to an event file. Event files contain\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/util/event.proto}{\\texttt{Event}}\r\nprotos that can contain \\texttt{Summary} protos along with the timestamp\r\nand step. You can then use TensorBoard to visualize the contents of the\r\nevent files. See\r\n\\href{../../how_tos/summaries_and_tensorboard/index.md}{TensorBoard and\r\nSummaries} for more details.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.scalar\\_summary(tags,\\ values,\\ collections=None,\\ name=None)}\r\n}{tf.scalar\\_summary(tags, values, collections=None, name=None) }}\\label{tf.scalarux5fsummarytags-values-collectionsnone-namenone}\r\n\r\nOutputs a \\texttt{Summary} protocol buffer with scalar values.\r\n\r\nThe input \\texttt{tags} and \\texttt{values} must have the same shape.\r\nThe generated summary has a summary value for each tag-value pair in\r\n\\texttt{tags} and \\texttt{values}.\r\n\r\n\\subparagraph{Args: }\\label{args-30}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{tags}: A 1-D \\texttt{string} \\texttt{Tensor}. Tags for the\r\n  summaries.\r\n\\item\r\n  \\texttt{values}: A 1-D \\texttt{float32} or \\texttt{float64} Tensor.\r\n  Values for the summaries.\r\n\\item\r\n  \\texttt{collections}: Optional list of graph collections keys. The new\r\n  summary op is added to these collections. Defaults to\r\n  \\texttt{{[}GraphKeys.SUMMARIES{]}}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-21}\r\n\r\nA scalar \\texttt{Tensor} of type \\texttt{string}. The serialized\r\n\\texttt{Summary} protocol buffer.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.image\\_summary(tag,\\ tensor,\\ max\\_images=None,\\ collections=None,\\ name=None)}\r\n}{tf.image\\_summary(tag, tensor, max\\_images=None, collections=None, name=None) }}\\label{tf.imageux5fsummarytag-tensor-maxux5fimagesnone-collectionsnone-namenone}\r\n\r\nOutputs a \\texttt{Summary} protocol buffer with images.\r\n\r\nThe summary has up to \\texttt{max\\_images} summary values containing\r\nimages. The images are built from \\texttt{tensor} which must be 4-D with\r\nshape \\texttt{{[}batch\\_size,\\ height,\\ width,\\ channels{]}} and where\r\n\\texttt{channels} can be:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  1: \\texttt{tensor} is interpreted as Grayscale.\r\n\\item\r\n  3: \\texttt{tensor} is interpreted as RGB.\r\n\\item\r\n  4: \\texttt{tensor} is interpreted as RGBA.\r\n\\end{itemize}\r\n\r\nThe images have the same number of channels as the input tensor. Their\r\nvalues are normalized, one image at a time, to fit in the range\r\n\\texttt{{[}0,\\ 255{]}}. The op uses two different normalization\r\nalgorithms:\r\n\r\n\\begin{itemize}\r\n\\item\r\n  If the input values are all positive, they are rescaled so the largest\r\n  one is 255.\r\n\\item\r\n  If any input value is negative, the values are shifted so input value\r\n  0.0 is at 127. They are then rescaled so that either the smallest\r\n  value is 0, or the largest one is 255.\r\n\\end{itemize}\r\n\r\nThe \\texttt{tag} argument is a scalar \\texttt{Tensor} of type\r\n\\texttt{string}. It is used to build the \\texttt{tag} of the summary\r\nvalues:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  If \\texttt{max\\_images} is 1, the summary value tag is\r\n  `\\emph{tag}/image'.\r\n\\item\r\n  If \\texttt{max\\_images} is greater than 1, the summary value tags are\r\n  generated sequentially as `\\emph{tag}/image/0', `\\emph{tag}/image/1',\r\n  etc.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Args: }\\label{args-31}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{tag}: A scalar \\texttt{Tensor} of type \\texttt{string}. Used\r\n  to build the \\texttt{tag} of the summary values.\r\n\\item\r\n  \\texttt{tensor}: A 4-D \\texttt{float32} \\texttt{Tensor} of shape\r\n  \\texttt{{[}batch\\_size,\\ height,\\ width,\\ \\ \\ \\ channels{]}} where\r\n  \\texttt{channels} is 1, 3, or 4.\r\n\\item\r\n  \\texttt{max\\_images}: Max number of batch elements to generate images\r\n  for.\r\n\\item\r\n  \\texttt{collections}: Optional list of ops.GraphKeys. The collections\r\n  to add the summary to. Defaults to {[}ops.GraphKeys.SUMMARIES{]}\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-22}\r\n\r\nA scalar \\texttt{Tensor} of type \\texttt{string}. The serialized\r\n\\texttt{Summary} protocol buffer.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.histogram\\_summary(tag,\\ values,\\ collections=None,\\ name=None)}\r\n}{tf.histogram\\_summary(tag, values, collections=None, name=None) }}\\label{tf.histogramux5fsummarytag-values-collectionsnone-namenone}\r\n\r\nOutputs a \\texttt{Summary} protocol buffer with a histogram.\r\n\r\nThe generated\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/summary.proto}{\\texttt{Summary}}\r\nhas one summary value containing a histogram for \\texttt{values}.\r\n\r\nThis op reports an \\texttt{OutOfRange} error if any value is not finite.\r\n\r\n\\subparagraph{Args: }\\label{args-32}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{tag}: A \\texttt{string} \\texttt{Tensor}. 0-D. Tag to use for\r\n  the summary value.\r\n\\item\r\n  \\texttt{values}: A \\texttt{float32} \\texttt{Tensor}. Any shape. Values\r\n  to use to build the histogram.\r\n\\item\r\n  \\texttt{collections}: Optional list of graph collections keys. The new\r\n  summary op is added to these collections. Defaults to\r\n  \\texttt{{[}GraphKeys.SUMMARIES{]}}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-23}\r\n\r\nA scalar \\texttt{Tensor} of type \\texttt{string}. The serialized\r\n\\texttt{Summary} protocol buffer.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.nn.zero\\_fraction(value,\\ name=None)}\r\n}{tf.nn.zero\\_fraction(value, name=None) }}\\label{tf.nn.zeroux5ffractionvalue-namenone}\r\n\r\nReturns the fraction of zeros in \\texttt{value}.\r\n\r\nIf \\texttt{value} is empty, the result is \\texttt{nan}.\r\n\r\nThis is useful in summaries to measure and report sparsity. For example,\r\n\r\n\\begin{verbatim}\r\nz = tf.Relu(...)\r\nsumm = tf.scalar_summary('sparsity', tf.zero_fraction(z))\r\n\\end{verbatim}\r\n\r\n\\subparagraph{Args: }\\label{args-33}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{value}: A tensor of numeric type.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-24}\r\n\r\nThe fraction of zeros in \\texttt{value}, with type \\texttt{float32}.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.merge\\_summary(inputs,\\ collections=None,\\ name=None)}\r\n}{tf.merge\\_summary(inputs, collections=None, name=None) }}\\label{tf.mergeux5fsummaryinputs-collectionsnone-namenone}\r\n\r\nMerges summaries.\r\n\r\nThis op creates a\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/summary.proto}{\\texttt{Summary}}\r\nprotocol buffer that contains the union of all the values in the input\r\nsummaries.\r\n\r\nWhen the Op is run, it reports an \\texttt{InvalidArgument} error if\r\nmultiple values in the summaries to merge use the same tag.\r\n\r\n\\subparagraph{Args: }\\label{args-34}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{inputs}: A list of \\texttt{string} \\texttt{Tensor} objects\r\n  containing serialized \\texttt{Summary} protocol buffers.\r\n\\item\r\n  \\texttt{collections}: Optional list of graph collections keys. The new\r\n  summary op is added to these collections. Defaults to\r\n  \\texttt{{[}GraphKeys.SUMMARIES{]}}.\r\n\\item\r\n  \\texttt{name}: A name for the operation (optional).\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-25}\r\n\r\nA scalar \\texttt{Tensor} of type \\texttt{string}. The serialized\r\n\\texttt{Summary} protocol buffer resulting from the merging.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.merge\\_all\\_summaries(key=\\textquotesingle{}summaries\\textquotesingle{})}\r\n}{tf.merge\\_all\\_summaries(key='summaries') }}\\label{tf.mergeux5fallux5fsummarieskeysummaries}\r\n\r\nMerges all summaries collected in the default graph.\r\n\r\n\\subparagraph{Args: }\\label{args-35}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{key}: \\texttt{GraphKey} used to collect the summaries.\r\n  Defaults to \\texttt{GraphKeys.SUMMARIES}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-26}\r\n\r\nIf no summaries were collected, returns None. Otherwise returns a scalar\r\n\\texttt{Tensor} of type\\texttt{string} containing the serialized\r\n\\texttt{Summary} protocol buffer resulting from the merging.\r\n\r\n\\subsection{Adding Summaries to Event Files\r\n}\\label{adding-summaries-to-event-files}\r\n\r\nSee \\href{../../how_tos/summaries_and_tensorboard/index.md}{Summaries\r\nand TensorBoard} for an overview of summaries, event files, and\r\nvisualization in TensorBoard.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{class\\ tf.train.SummaryWriter}\r\n}{class tf.train.SummaryWriter }}\\label{class-tf.train.summarywriter}\r\n\r\nWrites \\texttt{Summary} protocol buffers to event files.\r\n\r\nThe \\texttt{SummaryWriter} class provides a mechanism to create an event\r\nfile in a given directory and add summaries and events to it. The class\r\nupdates the file contents asynchronously. This allows a training program\r\nto call methods to add data to the file directly from the training loop,\r\nwithout slowing down training.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.SummaryWriter.\\_\\_init\\_\\_(logdir,\\ graph\\_def=None,\\ max\\_queue=10,\\ flush\\_secs=120)}\r\n}{tf.train.SummaryWriter.\\_\\_init\\_\\_(logdir, graph\\_def=None, max\\_queue=10, flush\\_secs=120) }}\\label{tf.train.summarywriter.ux5fux5finitux5fux5flogdir-graphux5fdefnone-maxux5fqueue10-flushux5fsecs120}\r\n\r\nCreates a \\texttt{SummaryWriter} and an event file.\r\n\r\nOn construction the summary writer creates a new event file in\r\n\\texttt{logdir}. This event file will contain \\texttt{Event} protocol\r\nbuffers constructed when you call one of the following functions:\r\n\\texttt{add\\_summary()}, \\texttt{add\\_event()}, or\r\n\\texttt{add\\_graph()}.\r\n\r\nIf you pass a \\texttt{graph\\_def} protocol buffer to the constructor it\r\nis added to the event file. (This is equivalent to calling\r\n\\texttt{add\\_graph()} later).\r\n\r\nTensorBoard will pick the graph from the file and display it graphically\r\nso you can interactively explore the graph you built. You will usually\r\npass the graph from the session in which you launched it:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{...create a graph...}\r\n\\CommentTok{# Launch the graph in a session.}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session()}\r\n\\CommentTok{# Create a summary writer, add the 'graph_def' to the event file.}\r\n\\NormalTok{writer }\\OperatorTok{=} \\NormalTok{tf.train.SummaryWriter(}\\OperatorTok{<}\\NormalTok{some}\\OperatorTok{-}\\NormalTok{directory}\\OperatorTok{>}\\NormalTok{, sess.graph_def)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nThe other arguments to the constructor control the asynchronous writes\r\nto the event file:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{flush\\_secs}: How often, in seconds, to flush the added\r\n  summaries and events to disk.\r\n\\item\r\n  \\texttt{max\\_queue}: Maximum number of summaries or events pending to\r\n  be written to disk before one of the `add' calls block.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Args: }\\label{args-36}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{logdir}: A string. Directory where event file will be written.\r\n\\item\r\n  \\texttt{graph\\_def}: A \\texttt{GraphDef} protocol buffer.\r\n\\item\r\n  \\texttt{max\\_queue}: Integer. Size of the queue for pending events and\r\n  summaries.\r\n\\item\r\n  \\texttt{flush\\_secs}: Number. How often, in seconds, to flush the\r\n  pending events and summaries to disk.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.SummaryWriter.add\\_summary(summary,\\ global\\_step=None)}\r\n}{tf.train.SummaryWriter.add\\_summary(summary, global\\_step=None) }}\\label{tf.train.summarywriter.addux5fsummarysummary-globalux5fstepnone}\r\n\r\nAdds a \\texttt{Summary} protocol buffer to the event file.\r\n\r\nThis method wraps the provided summary in an \\texttt{Event} procotol\r\nbuffer and adds it to the event file.\r\n\r\nYou can pass the output of any summary op, as-is, to this function. You\r\ncan also pass a \\texttt{Summary} procotol buffer that you manufacture\r\nwith your own data. This is commonly done to report evaluation results\r\nin event files.\r\n\r\n\\subparagraph{Args: }\\label{args-37}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{summary}: A \\texttt{Summary} protocol buffer, optionally\r\n  serialized as a string.\r\n\\item\r\n  \\texttt{global\\_step}: Number. Optional global step value to record\r\n  with the summary.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.SummaryWriter.add\\_event(event)}\r\n}{tf.train.SummaryWriter.add\\_event(event) }}\\label{tf.train.summarywriter.addux5feventevent}\r\n\r\nAdds an event to the event file.\r\n\r\n\\subparagraph{Args: }\\label{args-38}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{event}: An \\texttt{Event} protocol buffer.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.SummaryWriter.add\\_graph(graph\\_def,\\ global\\_step=None)}\r\n}{tf.train.SummaryWriter.add\\_graph(graph\\_def, global\\_step=None) }}\\label{tf.train.summarywriter.addux5fgraphgraphux5fdef-globalux5fstepnone}\r\n\r\nAdds a \\texttt{GraphDef} protocol buffer to the event file.\r\n\r\nThe graph described by the protocol buffer will be displayed by\r\nTensorBoard. Most users pass a graph in the constructor instead.\r\n\r\n\\subparagraph{Args: }\\label{args-39}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{graph\\_def}: A \\texttt{GraphDef} protocol buffer.\r\n\\item\r\n  \\texttt{global\\_step}: Number. Optional global step counter to record\r\n  with the graph.\r\n\\end{itemize}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.SummaryWriter.flush()}\r\n}{tf.train.SummaryWriter.flush() }}\\label{tf.train.summarywriter.flush}\r\n\r\nFlushes the event file to disk.\r\n\r\nCall this method to make sure that all pending events have been written\r\nto disk.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\paragraph{\\texorpdfstring{\\texttt{tf.train.SummaryWriter.close()}\r\n}{tf.train.SummaryWriter.close() }}\\label{tf.train.summarywriter.close}\r\n\r\nFlushes the event file to disk and close the file.\r\n\r\nCall this method when you do not need the summary writer anymore.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.summary\\_iterator(path)}\r\n}{tf.train.summary\\_iterator(path) }}\\label{tf.train.summaryux5fiteratorpath}\r\n\r\nAn iterator for reading \\texttt{Event} protocol buffers from an event\r\nfile.\r\n\r\nYou can use this function to read events written to an event file. It\r\nreturns a Python iterator that yields \\texttt{Event} protocol buffers.\r\n\r\nExample: Print the contents of an events file.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{for} \\NormalTok{e }\\OperatorTok{in} \\NormalTok{tf.summary_iterator(path to events }\\BuiltInTok{file}\\NormalTok{):}\r\n    \\BuiltInTok{print} \\NormalTok{e}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nExample: Print selected summary values.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# This example supposes that the events file contains summaries with a}\r\n\\CommentTok{# summary value tag 'loss'.  These could have been added by calling}\r\n\\CommentTok{# `add_summary()`, passing the output of a scalar summary op created with}\r\n\\CommentTok{# with: `tf.scalar_summary(['loss'], loss_tensor)`.}\r\n\\ControlFlowTok{for} \\NormalTok{e }\\OperatorTok{in} \\NormalTok{tf.summary_iterator(path to events }\\BuiltInTok{file}\\NormalTok{):}\r\n    \\ControlFlowTok{for} \\NormalTok{v }\\OperatorTok{in} \\NormalTok{e.summary.value:}\r\n        \\ControlFlowTok{if} \\NormalTok{v.tag }\\OperatorTok{==} \\StringTok{'loss'}\\NormalTok{:}\r\n            \\BuiltInTok{print} \\NormalTok{v.simple_value}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nSee the protocol buffer definitions of\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/util/event.proto}{Event}\r\nand\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/summary.proto}{Summary}\r\nfor more information about their attributes.\r\n\r\n\\subparagraph{Args: }\\label{args-40}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{path}: The path to an event file created by a\r\n  \\texttt{SummaryWriter}.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Yields: }\\label{yields}\r\n\r\n\\texttt{Event} protocol buffers.\r\n\r\n\\subsection{Training utilities }\\label{training-utilities}\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.global\\_step(sess,\\ global\\_step\\_tensor)}\r\n}{tf.train.global\\_step(sess, global\\_step\\_tensor) }}\\label{tf.train.globalux5fstepsess-globalux5fstepux5ftensor}\r\n\r\nSmall helper to get the global step.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Creates a variable to hold the global_step.}\r\n\\NormalTok{global_step_tensor }\\OperatorTok{=} \\NormalTok{tf.Variable(}\\DecValTok{10}\\NormalTok{, trainable}\\OperatorTok{=}\\VariableTok{False}\\NormalTok{, name}\\OperatorTok{=}\\StringTok{'global_step'}\\NormalTok{)}\r\n\\CommentTok{# Creates a session.}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session()}\r\n\\CommentTok{# Initializes the variable.}\r\n\\NormalTok{sess.run(global_step_tensor.initializer)}\r\n\\BuiltInTok{print} \\StringTok{'global_step:'}\\NormalTok{, tf.train.global_step(sess, global_step_tensor)}\r\n\r\n\\NormalTok{global_step: }\\DecValTok{10}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-41}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{sess}: A brain \\texttt{Session} object.\r\n\\item\r\n  \\texttt{global\\_step\\_tensor}: \\texttt{Tensor} or the \\texttt{name} of\r\n  the operation that contains the global step.\r\n\\end{itemize}\r\n\r\n\\subparagraph{Returns: }\\label{returns-27}\r\n\r\nThe global step value.\r\n\r\n\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.train.write\\_graph(graph\\_def,\\ logdir,\\ name,\\ as\\_text=True)}\r\n}{tf.train.write\\_graph(graph\\_def, logdir, name, as\\_text=True) }}\\label{tf.train.writeux5fgraphgraphux5fdef-logdir-name-asux5ftexttrue}\r\n\r\nWrites a graph proto on disk.\r\n\r\nThe graph is written as a binary proto unless as\\_text is \\texttt{True}.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{v }\\OperatorTok{=} \\NormalTok{tf.Variable(}\\DecValTok{0}\\NormalTok{, name}\\OperatorTok{=}\\StringTok{'my_variable'}\\NormalTok{)}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session()}\r\n\\NormalTok{tf.train.write_graph(sess.graph_def, }\\StringTok{'/tmp/my-model'}\\NormalTok{, }\\StringTok{'train.pbtxt'}\\NormalTok{)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subparagraph{Args: }\\label{args-42}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{graph\\_def}: A \\texttt{GraphDef} protocol buffer.\r\n\\item\r\n  \\texttt{logdir}: Directory where to write the graph.\r\n\\item\r\n  \\texttt{name}: Filename for the graph.\r\n\\item\r\n  \\texttt{as\\_text}: If \\texttt{True}, writes the graph as an ASCII\r\n  proto.\r\n\\end{itemize}\r\n"
  },
  {
    "path": "tex_pdf/api/python/index.tex",
    "content": "\r\n\r\n\\section{TensorFlow Python reference documentation\r\n}\\label{tensorflow-python-reference-documentation}\r\n\r\n\\begin{itemize}\r\n\\item\r\n  \\textbf{\\href{../../api_docs/python/framework.md}{Building Graphs}}:\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#add_to_collection}{\\texttt{add\\_to\\_collection}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#as_dtype}{\\texttt{as\\_dtype}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#control_dependencies}{\\texttt{control\\_dependencies}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#convert_to_tensor}{\\texttt{convert\\_to\\_tensor}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#device}{\\texttt{device}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#Dimension}{\\texttt{Dimension}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#DType}{\\texttt{DType}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#get_collection}{\\texttt{get\\_collection}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#get_default_graph}{\\texttt{get\\_default\\_graph}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#get_seed}{\\texttt{get\\_seed}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#Graph}{\\texttt{Graph}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#GraphKeys}{\\texttt{GraphKeys}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#import_graph_def}{\\texttt{import\\_graph\\_def}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#name_scope}{\\texttt{name\\_scope}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#NoGradient}{\\texttt{NoGradient}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#op_scope}{\\texttt{op\\_scope}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#Operation}{\\texttt{Operation}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#RegisterGradient}{\\texttt{RegisterGradient}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#RegisterShape}{\\texttt{RegisterShape}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#Tensor}{\\texttt{Tensor}}\r\n\\item\r\n  \\href{../../api_docs/python/framework.md\\#TensorShape}{\\texttt{TensorShape}}\r\n\\item\r\n  \\textbf{\\href{../../api_docs/python/constant_op.md}{Constants,\r\n  Sequences, and Random Values}}:\r\n\\item\r\n  \\href{../../api_docs/python/constant_op.md\\#constant}{\\texttt{constant}}\r\n\\item\r\n  \\href{../../api_docs/python/constant_op.md\\#fill}{\\texttt{fill}}\r\n\\item\r\n  \\href{../../api_docs/python/constant_op.md\\#linspace}{\\texttt{linspace}}\r\n\\item\r\n  \\href{../../api_docs/python/constant_op.md\\#ones}{\\texttt{ones}}\r\n\\item\r\n  \\href{../../api_docs/python/constant_op.md\\#ones_like}{\\texttt{ones\\_like}}\r\n\\item\r\n  \\href{../../api_docs/python/constant_op.md\\#random_normal}{\\texttt{random\\_normal}}\r\n\\item\r\n  \\href{../../api_docs/python/constant_op.md\\#random_shuffle}{\\texttt{random\\_shuffle}}\r\n\\item\r\n  \\href{../../api_docs/python/constant_op.md\\#random_uniform}{\\texttt{random\\_uniform}}\r\n\\item\r\n  \\href{../../api_docs/python/constant_op.md\\#range}{\\texttt{range}}\r\n\\item\r\n  \\href{../../api_docs/python/constant_op.md\\#set_random_seed}{\\texttt{set\\_random\\_seed}}\r\n\\item\r\n  \\href{../../api_docs/python/constant_op.md\\#truncated_normal}{\\texttt{truncated\\_normal}}\r\n\\item\r\n  \\href{../../api_docs/python/constant_op.md\\#zeros}{\\texttt{zeros}}\r\n\\item\r\n  \\href{../../api_docs/python/constant_op.md\\#zeros_like}{\\texttt{zeros\\_like}}\r\n\\item\r\n  \\textbf{\\href{../../api_docs/python/state_ops.md}{Variables}}:\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#all_variables}{\\texttt{all\\_variables}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#assert_variables_initialized}{\\texttt{assert\\_variables\\_initialized}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#assign}{\\texttt{assign}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#assign_add}{\\texttt{assign\\_add}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#assign_sub}{\\texttt{assign\\_sub}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#constant_initializer}{\\texttt{constant\\_initializer}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#count_up_to}{\\texttt{count\\_up\\_to}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#device}{\\texttt{device}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#get_checkpoint_state}{\\texttt{get\\_checkpoint\\_state}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#get_variable}{\\texttt{get\\_variable}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#get_variable_scope}{\\texttt{get\\_variable\\_scope}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#IndexedSlices}{\\texttt{IndexedSlices}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#initialize_all_variables}{\\texttt{initialize\\_all\\_variables}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#initialize_variables}{\\texttt{initialize\\_variables}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#latest_checkpoint}{\\texttt{latest\\_checkpoint}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#random_normal_initializer}{\\texttt{random\\_normal\\_initializer}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#random_uniform_initializer}{\\texttt{random\\_uniform\\_initializer}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#Saver}{\\texttt{Saver}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#scatter_add}{\\texttt{scatter\\_add}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#scatter_sub}{\\texttt{scatter\\_sub}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#scatter_update}{\\texttt{scatter\\_update}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#sparse_mask}{\\texttt{sparse\\_mask}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#trainable_variables}{\\texttt{trainable\\_variables}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#truncated_normal_initializer}{\\texttt{truncated\\_normal\\_initializer}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#uniform_unit_scaling_initializer}{\\texttt{uniform\\_unit\\_scaling\\_initializer}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#update_checkpoint_state}{\\texttt{update\\_checkpoint\\_state}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#Variable}{\\texttt{Variable}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#variable_scope}{\\texttt{variable\\_scope}}\r\n\\item\r\n  \\href{../../api_docs/python/state_ops.md\\#zeros_initializer}{\\texttt{zeros\\_initializer}}\r\n\\item\r\n  \\textbf{\\href{../../api_docs/python/array_ops.md}{Tensor\r\n  Transformations}}:\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#cast}{\\texttt{cast}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#concat}{\\texttt{concat}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#dynamic_partition}{\\texttt{dynamic\\_partition}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#dynamic_stitch}{\\texttt{dynamic\\_stitch}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#expand_dims}{\\texttt{expand\\_dims}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#gather}{\\texttt{gather}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#pack}{\\texttt{pack}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#pad}{\\texttt{pad}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#rank}{\\texttt{rank}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#reshape}{\\texttt{reshape}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#reverse}{\\texttt{reverse}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#reverse_sequence}{\\texttt{reverse\\_sequence}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#shape}{\\texttt{shape}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#size}{\\texttt{size}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#slice}{\\texttt{slice}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#split}{\\texttt{split}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#squeeze}{\\texttt{squeeze}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#string_to_number}{\\texttt{string\\_to\\_number}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#tile}{\\texttt{tile}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#to_bfloat16}{\\texttt{to\\_bfloat16}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#to_double}{\\texttt{to\\_double}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#to_float}{\\texttt{to\\_float}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#to_int32}{\\texttt{to\\_int32}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#to_int64}{\\texttt{to\\_int64}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#transpose}{\\texttt{transpose}}\r\n\\item\r\n  \\href{../../api_docs/python/array_ops.md\\#unpack}{\\texttt{unpack}}\r\n\\item\r\n  \\textbf{\\href{../../api_docs/python/math_ops.md}{Math}}:\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#abs}{\\texttt{abs}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#accumulate_n}{\\texttt{accumulate\\_n}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#add}{\\texttt{add}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#add_n}{\\texttt{add\\_n}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#argmax}{\\texttt{argmax}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#argmin}{\\texttt{argmin}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#batch_cholesky}{\\texttt{batch\\_cholesky}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#batch_matmul}{\\texttt{batch\\_matmul}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#batch_matrix_determinant}{\\texttt{batch\\_matrix\\_determinant}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#batch_matrix_inverse}{\\texttt{batch\\_matrix\\_inverse}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#ceil}{\\texttt{ceil}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#cholesky}{\\texttt{cholesky}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#complex}{\\texttt{complex}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#complex_abs}{\\texttt{complex\\_abs}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#conj}{\\texttt{conj}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#cos}{\\texttt{cos}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#diag}{\\texttt{diag}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#div}{\\texttt{div}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#edit_distance}{\\texttt{edit\\_distance}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#exp}{\\texttt{exp}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#floor}{\\texttt{floor}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#imag}{\\texttt{imag}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#inv}{\\texttt{inv}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#invert_permutation}{\\texttt{invert\\_permutation}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#listdiff}{\\texttt{listdiff}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#log}{\\texttt{log}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#matmul}{\\texttt{matmul}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#matrix_determinant}{\\texttt{matrix\\_determinant}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#matrix_inverse}{\\texttt{matrix\\_inverse}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#maximum}{\\texttt{maximum}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#minimum}{\\texttt{minimum}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#mod}{\\texttt{mod}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#mul}{\\texttt{mul}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#neg}{\\texttt{neg}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#pow}{\\texttt{pow}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#real}{\\texttt{real}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#reduce_all}{\\texttt{reduce\\_all}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#reduce_any}{\\texttt{reduce\\_any}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#reduce_max}{\\texttt{reduce\\_max}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#reduce_mean}{\\texttt{reduce\\_mean}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#reduce_min}{\\texttt{reduce\\_min}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#reduce_prod}{\\texttt{reduce\\_prod}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#reduce_sum}{\\texttt{reduce\\_sum}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#round}{\\texttt{round}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#rsqrt}{\\texttt{rsqrt}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#segment_max}{\\texttt{segment\\_max}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#segment_mean}{\\texttt{segment\\_mean}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#segment_min}{\\texttt{segment\\_min}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#segment_prod}{\\texttt{segment\\_prod}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#segment_sum}{\\texttt{segment\\_sum}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#sign}{\\texttt{sign}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#sin}{\\texttt{sin}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#sparse_segment_mean}{\\texttt{sparse\\_segment\\_mean}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#sparse_segment_sum}{\\texttt{sparse\\_segment\\_sum}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#sqrt}{\\texttt{sqrt}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#square}{\\texttt{square}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#sub}{\\texttt{sub}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#transpose}{\\texttt{transpose}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#unique}{\\texttt{unique}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#unsorted_segment_sum}{\\texttt{unsorted\\_segment\\_sum}}\r\n\\item\r\n  \\href{../../api_docs/python/math_ops.md\\#where}{\\texttt{where}}\r\n\\item\r\n  \\textbf{\\href{../../api_docs/python/control_flow_ops.md}{Control\r\n  Flow}}:\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#add_check_numerics_ops}{\\texttt{add\\_check\\_numerics\\_ops}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#Assert}{\\texttt{Assert}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#check_numerics}{\\texttt{check\\_numerics}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#count_up_to}{\\texttt{count\\_up\\_to}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#equal}{\\texttt{equal}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#greater}{\\texttt{greater}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#greater_equal}{\\texttt{greater\\_equal}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#group}{\\texttt{group}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#identity}{\\texttt{identity}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#is_finite}{\\texttt{is\\_finite}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#is_inf}{\\texttt{is\\_inf}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#is_nan}{\\texttt{is\\_nan}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#less}{\\texttt{less}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#less_equal}{\\texttt{less\\_equal}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#logical_and}{\\texttt{logical\\_and}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#logical_not}{\\texttt{logical\\_not}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#logical_or}{\\texttt{logical\\_or}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#logical_xor}{\\texttt{logical\\_xor}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#no_op}{\\texttt{no\\_op}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#not_equal}{\\texttt{not\\_equal}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#Print}{\\texttt{Print}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#select}{\\texttt{select}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#tuple}{\\texttt{tuple}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#verify_tensor_all_finite}{\\texttt{verify\\_tensor\\_all\\_finite}}\r\n\\item\r\n  \\href{../../api_docs/python/control_flow_ops.md\\#where}{\\texttt{where}}\r\n\\item\r\n  \\textbf{\\href{../../api_docs/python/image.md}{Images}}:\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#adjust_brightness}{\\texttt{adjust\\_brightness}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#adjust_contrast}{\\texttt{adjust\\_contrast}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#crop_to_bounding_box}{\\texttt{crop\\_to\\_bounding\\_box}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#decode_jpeg}{\\texttt{decode\\_jpeg}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#decode_png}{\\texttt{decode\\_png}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#encode_jpeg}{\\texttt{encode\\_jpeg}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#encode_png}{\\texttt{encode\\_png}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#extract_glimpse}{\\texttt{extract\\_glimpse}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#flip_left_right}{\\texttt{flip\\_left\\_right}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#flip_up_down}{\\texttt{flip\\_up\\_down}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#pad_to_bounding_box}{\\texttt{pad\\_to\\_bounding\\_box}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#per_image_whitening}{\\texttt{per\\_image\\_whitening}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#random_brightness}{\\texttt{random\\_brightness}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#random_contrast}{\\texttt{random\\_contrast}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#random_crop}{\\texttt{random\\_crop}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#random_flip_left_right}{\\texttt{random\\_flip\\_left\\_right}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#random_flip_up_down}{\\texttt{random\\_flip\\_up\\_down}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#resize_area}{\\texttt{resize\\_area}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#resize_bicubic}{\\texttt{resize\\_bicubic}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#resize_bilinear}{\\texttt{resize\\_bilinear}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#resize_image_with_crop_or_pad}{\\texttt{resize\\_image\\_with\\_crop\\_or\\_pad}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#resize_images}{\\texttt{resize\\_images}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#resize_nearest_neighbor}{\\texttt{resize\\_nearest\\_neighbor}}\r\n\\item\r\n  \\href{../../api_docs/python/image.md\\#transpose_image}{\\texttt{transpose\\_image}}\r\n\\item\r\n  \\textbf{\\href{../../api_docs/python/sparse_ops.md}{Sparse Tensors}}:\r\n\\item\r\n  \\href{../../api_docs/python/sparse_ops.md\\#shape}{\\texttt{shape}}\r\n\\item\r\n  \\href{../../api_docs/python/sparse_ops.md\\#sparse_concat}{\\texttt{sparse\\_concat}}\r\n\\item\r\n  \\href{../../api_docs/python/sparse_ops.md\\#sparse_fill_empty_rows}{\\texttt{sparse\\_fill\\_empty\\_rows}}\r\n\\item\r\n  \\href{../../api_docs/python/sparse_ops.md\\#sparse_reorder}{\\texttt{sparse\\_reorder}}\r\n\\item\r\n  \\href{../../api_docs/python/sparse_ops.md\\#sparse_retain}{\\texttt{sparse\\_retain}}\r\n\\item\r\n  \\href{../../api_docs/python/sparse_ops.md\\#sparse_tensor_to_dense}{\\texttt{sparse\\_tensor\\_to\\_dense}}\r\n\\item\r\n  \\href{../../api_docs/python/sparse_ops.md\\#sparse_to_dense}{\\texttt{sparse\\_to\\_dense}}\r\n\\item\r\n  \\href{../../api_docs/python/sparse_ops.md\\#sparse_to_indicator}{\\texttt{sparse\\_to\\_indicator}}\r\n\\item\r\n  \\href{../../api_docs/python/sparse_ops.md\\#SparseTensor}{\\texttt{SparseTensor}}\r\n\\item\r\n  \\href{../../api_docs/python/sparse_ops.md\\#SparseTensorValue}{\\texttt{SparseTensorValue}}\r\n\\item\r\n  \\textbf{\\href{../../api_docs/python/io_ops.md}{Inputs and Readers}}:\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#batch}{\\texttt{batch}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#batch_join}{\\texttt{batch\\_join}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#decode_csv}{\\texttt{decode\\_csv}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#decode_raw}{\\texttt{decode\\_raw}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#FIFOQueue}{\\texttt{FIFOQueue}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#FixedLengthRecordReader}{\\texttt{FixedLengthRecordReader}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#IdentityReader}{\\texttt{IdentityReader}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#limit_epochs}{\\texttt{limit\\_epochs}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#match_filenames_once}{\\texttt{match\\_filenames\\_once}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#matching_files}{\\texttt{matching\\_files}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#parse_example}{\\texttt{parse\\_example}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#parse_single_example}{\\texttt{parse\\_single\\_example}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#placeholder}{\\texttt{placeholder}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#QueueBase}{\\texttt{QueueBase}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#RandomShuffleQueue}{\\texttt{RandomShuffleQueue}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#range_input_producer}{\\texttt{range\\_input\\_producer}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#read_file}{\\texttt{read\\_file}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#ReaderBase}{\\texttt{ReaderBase}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#shuffle_batch}{\\texttt{shuffle\\_batch}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#shuffle_batch_join}{\\texttt{shuffle\\_batch\\_join}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#size}{\\texttt{size}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#slice_input_producer}{\\texttt{slice\\_input\\_producer}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#string_input_producer}{\\texttt{string\\_input\\_producer}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#TextLineReader}{\\texttt{TextLineReader}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#TFRecordReader}{\\texttt{TFRecordReader}}\r\n\\item\r\n  \\href{../../api_docs/python/io_ops.md\\#WholeFileReader}{\\texttt{WholeFileReader}}\r\n\\item\r\n  \\textbf{\\href{../../api_docs/python/python_io.md}{Data IO (Python\r\n  functions)}}:\r\n\\item\r\n  \\href{../../api_docs/python/python_io.md\\#tf_record_iterator}{\\texttt{tf\\_record\\_iterator}}\r\n\\item\r\n  \\href{../../api_docs/python/python_io.md\\#TFRecordWriter}{\\texttt{TFRecordWriter}}\r\n\\item\r\n  \\textbf{\\href{../../api_docs/python/nn.md}{Neural Network}}:\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#avg_pool}{\\texttt{avg\\_pool}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#bias_add}{\\texttt{bias\\_add}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#compute_accidental_hits}{\\texttt{compute\\_accidental\\_hits}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#conv2d}{\\texttt{conv2d}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#depthwise_conv2d}{\\texttt{depthwise\\_conv2d}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#dropout}{\\texttt{dropout}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#embedding_lookup}{\\texttt{embedding\\_lookup}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#fixed_unigram_candidate_sampler}{\\texttt{fixed\\_unigram\\_candidate\\_sampler}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#in_top_k}{\\texttt{in\\_top\\_k}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#l2_loss}{\\texttt{l2\\_loss}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#l2_normalize}{\\texttt{l2\\_normalize}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#learned_unigram_candidate_sampler}{\\texttt{learned\\_unigram\\_candidate\\_sampler}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#local_response_normalization}{\\texttt{local\\_response\\_normalization}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#log_uniform_candidate_sampler}{\\texttt{log\\_uniform\\_candidate\\_sampler}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#max_pool}{\\texttt{max\\_pool}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#max_pool_with_argmax}{\\texttt{max\\_pool\\_with\\_argmax}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#moments}{\\texttt{moments}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#nce_loss}{\\texttt{nce\\_loss}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#relu}{\\texttt{relu}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#relu6}{\\texttt{relu6}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#sampled_softmax_loss}{\\texttt{sampled\\_softmax\\_loss}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#separable_conv2d}{\\texttt{separable\\_conv2d}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#sigmoid}{\\texttt{sigmoid}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#sigmoid_cross_entropy_with_logits}{\\texttt{sigmoid\\_cross\\_entropy\\_with\\_logits}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#softmax}{\\texttt{softmax}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#softmax_cross_entropy_with_logits}{\\texttt{softmax\\_cross\\_entropy\\_with\\_logits}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#softplus}{\\texttt{softplus}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#tanh}{\\texttt{tanh}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#top_k}{\\texttt{top\\_k}}\r\n\\item\r\n  \\href{../../api_docs/python/nn.md\\#uniform_candidate_sampler}{\\texttt{uniform\\_candidate\\_sampler}}\r\n\\item\r\n  \\textbf{\\href{../../api_docs/python/client.md}{Running Graphs}}:\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#AbortedError}{\\texttt{AbortedError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#AlreadyExistsError}{\\texttt{AlreadyExistsError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#CancelledError}{\\texttt{CancelledError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#DataLossError}{\\texttt{DataLossError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#DeadlineExceededError}{\\texttt{DeadlineExceededError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#FailedPreconditionError}{\\texttt{FailedPreconditionError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#get_default_session}{\\texttt{get\\_default\\_session}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#InteractiveSession}{\\texttt{InteractiveSession}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#InternalError}{\\texttt{InternalError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#InvalidArgumentError}{\\texttt{InvalidArgumentError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#NotFoundError}{\\texttt{NotFoundError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#OpError}{\\texttt{OpError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#OutOfRangeError}{\\texttt{OutOfRangeError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#PermissionDeniedError}{\\texttt{PermissionDeniedError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#ResourceExhaustedError}{\\texttt{ResourceExhaustedError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#Session}{\\texttt{Session}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#UnauthenticatedError}{\\texttt{UnauthenticatedError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#UnavailableError}{\\texttt{UnavailableError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#UnimplementedError}{\\texttt{UnimplementedError}}\r\n\\item\r\n  \\href{../../api_docs/python/client.md\\#UnknownError}{\\texttt{UnknownError}}\r\n\\item\r\n  \\textbf{\\href{../../api_docs/python/train.md}{Training}}:\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#AdagradOptimizer}{\\texttt{AdagradOptimizer}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#AdamOptimizer}{\\texttt{AdamOptimizer}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#add_queue_runner}{\\texttt{add\\_queue\\_runner}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#AggregationMethod}{\\texttt{AggregationMethod}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#clip_by_average_norm}{\\texttt{clip\\_by\\_average\\_norm}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#clip_by_global_norm}{\\texttt{clip\\_by\\_global\\_norm}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#clip_by_norm}{\\texttt{clip\\_by\\_norm}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#clip_by_value}{\\texttt{clip\\_by\\_value}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#Coordinator}{\\texttt{Coordinator}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#exponential_decay}{\\texttt{exponential\\_decay}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#ExponentialMovingAverage}{\\texttt{ExponentialMovingAverage}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#FtrlOptimizer}{\\texttt{FtrlOptimizer}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#global_norm}{\\texttt{global\\_norm}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#global_step}{\\texttt{global\\_step}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#GradientDescentOptimizer}{\\texttt{GradientDescentOptimizer}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#gradients}{\\texttt{gradients}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#histogram_summary}{\\texttt{histogram\\_summary}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#image_summary}{\\texttt{image\\_summary}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#merge_all_summaries}{\\texttt{merge\\_all\\_summaries}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#merge_summary}{\\texttt{merge\\_summary}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#MomentumOptimizer}{\\texttt{MomentumOptimizer}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#Optimizer}{\\texttt{Optimizer}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#QueueRunner}{\\texttt{QueueRunner}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#RMSPropOptimizer}{\\texttt{RMSPropOptimizer}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#scalar_summary}{\\texttt{scalar\\_summary}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#start_queue_runners}{\\texttt{start\\_queue\\_runners}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#stop_gradient}{\\texttt{stop\\_gradient}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#summary_iterator}{\\texttt{summary\\_iterator}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#SummaryWriter}{\\texttt{SummaryWriter}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#write_graph}{\\texttt{write\\_graph}}\r\n\\item\r\n  \\href{../../api_docs/python/train.md\\#zero_fraction}{\\texttt{zero\\_fraction}}\r\n\\end{itemize}\r\n\r\n--\\textgreater{}\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/get_started/c1s01_introduction.tex",
    "content": "%!TEX program = xelatex\r\n% Encoding: UTF8\r\n% SEIKA 2016 ｜ seika@live.ca\r\n\r\n% Chapter 1\r\n% Section 1.1 Introduction\r\n\r\n\\section{Introduction  ||  简介}\r\n\r\nⒺ \\textcolor{etc}{Let's get you up and running with TensorFlow!}\r\n\r\nⒸ 本章的目的是让你了解和运行 TensorFlow！\r\n\r\nⒺ \\textcolor{etc}{But before we even get started, let's peek at what TensorFlow code looks like in the Python API, so you have a sense of where we're headed.}\r\n\r\nⒸ 在开始之前，让我们先看一段使用 Python API 撰写的 TensorFlow 示例代码，\r\n让你对将要学习的内容有初步的印象.\r\n\r\nⒺ \\textcolor{etc}{Here's a little Python program that makes up some data in two dimensions, and then fits a line to it.}\r\n\r\nⒸ 下面这段短小的Python程序将把一些数据放入二维空间，再用一条线来拟合这些数据．\r\n\r\n\\begin{lstlisting}\r\nimport tensorflow as tf\r\nimport numpy as np\r\n\r\n# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3\r\nx_data = np.random.rand(100).astype(\"float32\")\r\ny_data = x_data * 0.1 + 0.3\r\n\r\n# Try to find values for W and b that compute y_data = W * x_data + b\r\n# (We know that W should be 0.1 and b 0.3, but Tensorflow will\r\n# figure that out for us.)\r\nW = tf.Variable(tf.random_uniform([1], -1.0, 1.0))\r\nb = tf.Variable(tf.zeros([1]))\r\ny = W * x_data + b\r\n\r\n# Minimize the mean squared errors.\r\nloss = tf.reduce_mean(tf.square(y - y_data))\r\noptimizer = tf.train.GradientDescentOptimizer(0.5)\r\ntrain = optimizer.minimize(loss)\r\n\r\n# Before starting, initialize the variables. We will 'run' this first.\r\ninit = tf.global_variables_initializer()\r\n\r\n# Launch the graph.\r\nsess = tf.Session()\r\nsess.run(init)\r\n\r\n# Fit the line.\r\nfor step in xrange(201):\r\n  sess.run(train)\r\n  if step % 20 == 0:\r\n    print(step, sess.run(W), sess.run(b))\r\n\r\n# Learns best fit is W: [0.1], b: [0.3]\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{The first part of this code builds the data flow graph. TensorFlow does not actually run any computation until the session is created and the run function is called.}\r\n\r\nⒸ 以上代码的第一部分构建了数据的流向图(flow graph)．在一个session被建立并且\\lstinline{run()}函数被运行前，TensorFlow不会进行任何实质的计算．\r\n\r\nⒺ \\textcolor{etc}{To whet your appetite further, we suggest you check out what a classical machine learning problem looks like in TensorFlow. In the land of neural networks the most \"classic\" classical problem is the MNIST handwritten digit classification. We offer two introductions here, one for machine learning newbies, and one for pros. If you've already trained dozens of MNIST models in other software packages, please take the red pill. If you've never even heard of MNIST, definitely take the blue pill. If you're somewhere in between, we suggest skimming blue, then red.}\r\n\r\nⒸ 为了进一步激发你的学习欲望,我们想让你先看一下TensorFlow是如何解决一个经典的机器学习问题的．在神经网络领域，最为经典的问题莫过于MNIST手写数字分类．为此，我们准备了两篇不同的教程,分别面向初学者和专家．如果你已经使用其它软件训练过许多MNIST模型，请参阅\\hyperref[MINIST_pros]{高级教程(红色药丸)}．如果你以前从未听说过 MNIST ，请先阅读\\hyperref[MINIST_beginner]{初级教程(蓝色药丸)}．如果你的水平介于这两类人之间,我们建议你先快速浏览\\hyperref[MINIST_beginner]{初级教程}，然后再阅读\\hyperref[MINIST_pros]{高级教程}．\r\n\r\n% Add pics and links here.\r\n\r\nⒺ \\textcolor{etc}{If you're already sure you want to learn and install TensorFlow you can skip these and charge ahead. Don't worry, you'll still get to see MNIST -- we'll also use MNIST as an example in our technical tutorial where we elaborate on TensorFlow features.}\r\n\r\n\\begin{center}\r\n\\includegraphics[width=.45\\textwidth]{../SOURCE/images/blue_pill.png}\r\n\\includegraphics[width=.45\\textwidth]{../SOURCE/images/red_pill.png}\r\n\\end{center}\r\n\r\nⒸ 如果你已下定决心准备学习和安装 TensorFlow ，你可以略过这些文字，直接阅读\r\n后面的章节\\footnote{\\textbf{推荐随后阅读内容}：\\hyperref[download_install]{1 下载与安装}， \\hyperref[basic_usage]{2 基本使用}， \\hyperref[tf_mech101]{3 TensorFlow 101}.}．不用担心，你仍然会看到 MNIST --- 在阐述TensorFlow的特性时，\r\n我们还会使用MNIST作为一个样例．"
  },
  {
    "path": "tex_pdf/get_started/c1s02_os_setup.tex",
    "content": "%!TEX program = xelatex\r\n% Encoding: UTF8\r\n% SEIKA 2016 ｜ seika@live.ca\r\n\r\n% Chapter 1\r\n% Section 1.2 OS_Setup\r\n\r\n\r\n\\section {Download and Setup   ||   下载与安装} \\label{download_install}\r\n\r\nⒺ \\textcolor{etc}{You can install TensorFlow either from our provided binary packages or from the github source.}\r\n\r\nⒸ 您可以使用我们提供的二进制包，或者源代码，安装 TensorFlow.\r\n\r\n%\r\n%%\r\n\\subsection {Requirements  ｜  安装需求}\r\n\r\nⒺ \\textcolor{etc}{The TensorFlow Python API currently supports Python 2.7 and Python 3.3+ from source.}\r\n\r\nⒸ TensorFlow Python API 目前支持 Python 2.7 和 python 3.3以上版本．\r\n\r\nⒺ \\textcolor{etc}{The GPU version (Linux only) currently requires the Cuda Toolkit 7.0 and CUDNN 6.5 V2. Please see \\hyperref[install_cuda]{Cuda installation}.}\r\n\r\nⒸ 支持 GPU 运算的版本 (仅限Linux) 需要 Cuda Toolkit 7.0 和 CUDNN 6.5 V2. 具体请参考\\hyperref[install_cuda]{Cuda安装}．\r\n\r\n%\r\n%%\r\n\\subsection {Overview  |  安装总述}\r\n\r\nWe support different ways to install TensorFlow:\r\n\r\nTensorFlow 支持通过以下不同的方式安装：\r\n\r\n\\begin{itemize}\r\n\\item \\hyperref[pip_install]{Pip Install}: \\textcolor{etc}{Install TensorFlow on your machine, possibly upgrading previously installed Python packages. May impact existing Python programs on your machine.}\\\\\r\n\\item \\hyperref[pip_install]{Pip 安装}: 在你的机器上安装TensorFlow，可能会同时更新之前安装的Python包，并且影响到你机器当前可运行的Python程序．\\\\\r\n\\item \\hyperref[virtualenv_install]{Virtualenv Install}: \\textcolor{etc}{Install TensorFlow in its own directory, not impacting any existing Python programs on your machine.}\\\\\r\n\\item \\hyperref[virtualenv_install]{Virtualenv 安装}: 在一个独立的路径下安装TensorFlow，不会影响到你机器当前运行的Python程序．\\\\\r\n\\item \\hyperref[docker_install]{Docker Install}: \\textcolor{etc}{Run TensorFlow in a Docker container isolated from all other programs on your machine.}\\\\\r\n\\item \\hyperref[docker_install]{Docker 安装}: 在一个独立的Docker容器中安装TensorFlow，并且不会影响到你机器上的任何其他程序．\r\n\\end{itemize}\r\n\r\nⒺ \\textcolor{etc}{If you are familiar with Pip, Virtualenv, or Docker, please feel free to adapt the instructions to your particular needs. The names of the pip and Docker images are listed in the corresponding installation sections.}\r\n\r\nⒸ 如果你已经很熟悉Pip、Virtualenv、Docker这些工具的使用，请利用教程中提供的代码，根据你的需求安装TensorFlow．你会在下文的对应的安装教程中找到Pip或Docker安装所需的镜像．\r\n\r\nⒺ \\textcolor{etc}{If you encounter installation errors, see common problems for some solutions.}\r\n\r\nⒸ 如果你遇到了安装错误，请参考章节\\hyperref[comm_prob]{常见问题}寻找解决方案．\r\n\r\n%\r\n%%\r\n\\subsection {Pip Installatioin  |  Pip 安装} \\label{pip_install}\r\n\r\nⒺ \\textcolor{etc}{\\href{https://en.wikipedia.org/wiki/Pip_(package_manager)}{Pip} is a package management system used to install and manage software packages written in Python.}\r\n\r\nⒸ \\href{https://en.wikipedia.org/wiki/Pip_(package_manager)}{Pip} 是一个用于安装和管理Python软件包的管理系统．\r\n\r\nⒺ \\textcolor{etc}{The packages that will be installed or upgraded during the pip install are listed in the \\href{https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/pip_package/setup.py}{REQUIRED\\_PACKAGES section of setup.py}}\r\n\r\nⒸ 安装依赖包(\\href{https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/pip_package/setup.py}{REQUIRED\\_PACKAGES section of setup.py}) 列出了pip安装时将会被安装或更新的库文件．\r\n\r\nⒺ \\textcolor{etc}{Install pip (or pip3 for python3) if it is not already installed:}\r\n\r\nⒸ 如果pip尚未被安装，请使用以下代码先安装pip(如果你使用的是Python 3请安装 pip3 ):\r\n\r\n\\begin{lstlisting}[language = bash]\r\n# Ubuntu/Linux 64-bit\r\n$ sudo apt-get install python-pip python-dev\r\n\\end{lstlisting}\r\n\r\n\\begin{lstlisting}\r\n# Mac OS X\r\n$ sudo easy_install pip\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{Install TensorFlow:}\r\n\r\nⒸ 安装 TensorFlow:\r\n\r\n\\begin{lstlisting}\r\n# Ubuntu/Linux 64-bit, CPU only:\r\n$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.6.0-cp27-none-linux_x86_64.whl\r\n\\end{lstlisting}\r\n\r\n\\begin{lstlisting}\r\n# Ubuntu/Linux 64-bit, GPU enabled:\r\n$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.6.0-cp27-none-linux_x86_64.whl\r\n\\end{lstlisting}\r\n\r\n\\begin{lstlisting}[language = bash]\r\n# Mac OS X, CPU only:\r\n$ sudo easy_install --upgrade six\r\n$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.6.0-py2-none-any.whl\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{For Python 3:}\r\n\r\nⒸ 基于 Python 3 的 TensorFlow 安装:\r\n\r\n\\begin{lstlisting}\r\n# Ubuntu/Linux 64-bit, CPU only:\r\n$ sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.6.0-cp34-none-linux_x86_64.whl\r\n\\end{lstlisting}\r\n\r\n\\begin{lstlisting}\r\n# Ubuntu/Linux 64-bit, GPU enabled:\r\n$ sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.6.0-cp34-none-linux_x86_64.whl\r\n\r\n\\end{lstlisting}\r\n\r\n\\begin{lstlisting}[language = bash]\r\n# Mac OS X, CPU only:\r\n$ sudo easy_install --upgrade six\r\n$ sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.6.0-py3-none-any.whl\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{You can now test your \\hyperref[test_install]{installation}.}\r\n\r\nⒸ 至此你可以\\hyperref[test_install]{测试安装}是否成功．\r\n\r\n\r\n%\r\n%%\r\n\\subsection {Virtualenv installation  |  基于 Virtualenv 安装} \\label{virtualenv_install}\r\n\r\nⒺ \\textcolor{etc}{\\href{http://docs.python-guide.org/en/latest/dev/virtualenvs/}{Virtualenv} is a tool to keep the dependencies required by different Python projects in separate places. The Virtualenv installation of TensorFlow will not override pre-existing version of the Python packages needed by TensorFlow.}\r\n\r\nⒸ \\href{http://docs.python-guide.org/en/latest/dev/virtualenvs/}{Virtualenv} 是一个管理在不同位置存放和调用 Python 项目所需依赖库的工具． TensorFlow 的 Virtualenv 安装不会覆盖先前已安装的 TensorFlow Python依赖包．\r\n\r\nⒺ \\textcolor{etc}{With \\href{https://pypi.python.org/pypi/virtualenv}{Virtualenv} the installation is as follows:}\r\n\r\nⒸ 基于\\href{https://pypi.python.org/pypi/virtualenv}{Virtualenv}的安装分为以下几步:\r\n\r\n\\begin{itemize}\r\n\\item \\textcolor{etc}{Install pip and Virtualenv.}\r\n\\item \\textcolor{etc}{Create a Virtualenv environment.}\r\n\\item \\textcolor{etc}{Activate the Virtualenv environment and install TensorFlow in it.}\r\n\\item \\textcolor{etc}{After the install you will activate the Virtualenv environment each time you want to use TensorFlow.}\r\n\\item 安装 pip 和 Virtualenv.\r\n\\item 建立一个 Virtualenv 环境.\r\n\\item 激活这个 Virtualenv 环境，并且在此环境下安装 TensorFlow.\r\n\\item 安装完成之后，每次你需要使用 TensorFlow 之前必须激活这个 Virtualenv 环境.\r\n\\end{itemize}\r\n\r\nⒺ \\textcolor{etc}{Install pip and Virtualenv:}\r\n\r\nⒸ 安装 pip 和 Virtualenv：\r\n\r\n\\begin{lstlisting}\r\n# Ubuntu/Linux 64-bit\r\n$ sudo apt-get install python-pip python-dev python-virtualenv\r\n\\end{lstlisting}\r\n\r\n\\begin{lstlisting}\r\n# Mac OS X\r\n$ sudo easy_install pip\r\n$ sudo pip install --upgrade virtualenv\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolo{etc}{Create a Virtualenv environment in the directory} \\lstinline{~/tensorflow}:\r\n\r\nⒸ 在\\lstinline{~/tensorflow}路径下建立一个 Virtualenv 环境：\r\n\r\n\\begin{lstlisting}\r\n$ virtualenv --system-site-packages ~/tensorflow\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{Activate the environment and use pip to install TensorFlow inside it:}\r\n\r\nⒸ 激活 Virtualenv 环境并使用pip在该环境下安装TensorFlow：\r\n\r\n\\begin{lstlisting}\r\n$ source ~/tensorflow/bin/activate  # If using bash\r\n$ source ~/tensorflow/bin/activate.csh  # If using csh\r\n(tensorflow)$  # Your prompt should change\r\n\r\n# Ubuntu/Linux 64-bit, CPU only:\r\n(tensorflow)$ pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl\r\n\r\n# Ubuntu/Linux 64-bit, GPU enabled:\r\n(tensorflow)$ pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl\r\n\r\n# Mac OS X, CPU only:\r\n(tensorflow)$ pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl\r\n\\end{lstlisting}\r\n\r\nⒺ and again for python3:\r\n\r\n\\begin{lstlisting}\r\n$ source ~/tensorflow/bin/activate  # If using bash\r\n$ source ~/tensorflow/bin/activate.csh  # If using csh\r\n(tensorflow)$  # Your prompt should change\r\n\r\n# Ubuntu/Linux 64-bit, CPU only:\r\n(tensorflow)$ pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.6.0-cp34-none-linux_x86_64.whl\r\n\r\n# Ubuntu/Linux 64-bit, GPU enabled:\r\n(tensorflow)$ pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.6.0-cp34-none-linux_x86_64.whl\r\n\r\n# Mac OS X, CPU only:\r\n(tensorflow)$ pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.6.0-py3-none-any.whl\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{With the Virtualenv environment activated, you can now \\hyperref[test_install]{test your installation}.}\r\n\r\nⒸ 在 Virtualenv 环境被激活时，您可以\\hyperref[test_install]{测试安装}．\r\n\r\nⒺ \\textcolor{etc}{When you are done using TensorFlow, deactivate the environment.}\r\n\r\nⒸ 当您无需使用 TensorFlow 时，取消激活该环境．\r\n\r\n\\begin{lstlisting}\r\n(tensorflow)$ deactivate\r\n$  # Your prompt should change back\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{To use TensorFlow later you will have to activate the Virtualenv environment again:}\r\n\r\nⒸ 如果需要再次使用 TensorFlow 您需要先再次激活 Virtualenv 环境:\r\n\r\n\\begin{lstlisting}\r\n$ source ~/tensorflow/bin/activate  # If using bash.\r\n$ source ~/tensorflow/bin/activate.csh  # If using csh.\r\n(tensorflow)$  # Your prompt should change.\r\n# Run Python programs that use TensorFlow.\r\n...\r\n# When you are done using TensorFlow, deactivate the environment.\r\n(tensorflow)$ deactivate\r\n\\end{lstlisting}\r\n\r\n%%%%%%%%%%%%%%%%\r\n% Done to here %\r\n%%%%%%%%%%%%%%%%\r\n\r\n%\r\n%%\r\n\\subsection {Docker Installation} \\label{docker_install}\r\n\\href{http://docker.com/}{Docker} is a system to build self contained versions of a Linux operating system running on your machine. When you install and run TensorFlow via Docker it completely isolates the installation from pre-existing packages on your machine.\r\n\r\nWe provide 4 Docker images:\r\n\r\n\\begin{itemize}\r\n\\item \\lstinline{b.gcr.io/tensorflow/tensorflow}: TensorFlow CPU binary image.\r\n\\item \\lstinline{b.gcr.io/tensorflow/tensorflow:latest-devel}:CPU Binary image plus source code.\r\n\\item \\lstinline{b.gcr.io/tensorflow/tensorflow:latest-gpu}:TensorFlow GPU binary image.\r\n\\item \\lstinline{b.gcr.io/tensorflow/tensorflow:latest-devel-gpu}:GPU Binary image plus source code.\r\n\\end{itemize}\r\n\r\nWe also have tags with latest replaced by a released version (eg \\lstinline{0.6.0-gpu}).\r\n\r\nWith Docker the installation is as follows:\r\n\r\n\\begin{itemize}\r\n\\item Install Docker on your machine.\r\n\\item Create a \\href{http://docs.docker.com/engine/installation/ubuntulinux/#create-a-docker-group}{Docker group} to allow launching containers without sudo.\r\n\\item Launch a Docker container with the TensorFlow image. The image gets downloaded automatically on first launch.\r\n\\end{itemize}\r\n\r\nSee \\href{http://docs.docker.com/engine/installation/}{installing Docker} for instructions on installing Docker on your machine.\r\n\r\nAfter Docker is installed, launch a Docker container with the TensorFlow binary image as follows.\r\n\r\n\\begin{lstlisting}\r\n$ docker run -it b.gcr.io/tensorflow/tensorflow\r\n\\end{lstlisting}\r\n\r\nIf you're using a container with GPU support, some additional flags must be passed to expose the GPU device to the container. For the default config, we include a \\href{https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/docker/docker_run_gpu.sh}{script} in the repo with these flags, so the command-line would look like:\r\n\r\n\\begin{lstlisting}\r\n$ path/to/repo/tensorflow/tools/docker/docker_run_gpu.sh b.gcr.io/tensorflow/tensorflow:gpu\r\n\\end{lstlisting}\r\n\r\nYou can now \\hyperref[test_install]{test your installation} within the Docker container.\r\n\r\n\r\n%\r\n%%\r\n\\subsection {Test the TensorFlow installation  |  测试 TensorFlow 安装} \\label{test_install}\r\n\r\n%%%\r\n\\subsubsection {(Optional, Linux) Enable GPU Support}\r\n\r\n\\textcolor{etc}{If you installed the GPU version of TensorFlow, you must also install the Cuda Toolkit~7.0 and CUDNN~6.5~V2. Please see \\hyperref[install_cuda]{Cuda installation}.}\r\n\r\nⒸ 如果您安装了GPU版本的TensorFlow, 您还需要安装 Cuda Toolkit~7.0 和 CUDNN~6.5~V2．请参阅\\hyperref[install_cuda]{Cuda 安装}．\r\n\r\n\\textcolor{etc}{You also need to set the \\lstinline{LD_LIBRARY_PATH} and \\lstinline{CUDA_HOME} environment variables. Consider adding the commands below to your} \\lstinline{~/.bash_profile}. \\textcolor{etc}{These assume your CUDA installation is in \\lstinline{/usr/local/cuda}:}\r\n\r\nⒸ 您需要在先环境变量中设置\\lstinline{LD_LIBRARY_PATH} 和 \\lstinline{CUDA_HOME}．您可以在\\lstinline{~/.bash_profile}中追加一下命令，假设您的CUDA安装位置为\\lstinline{/usr/local/cuda}:\r\n\r\n\\begin{lstlisting}\r\nexport LD_LIBRARY_PATH=\"$LD_LIBRARY_PATH:/usr/local/cuda/lib64\"\r\nexport CUDA_HOME=/usr/local/cuda\r\n\\end{lstlisting}\r\n\r\n%%%\r\n\\subsubsection {Run TensorFlow from the Command Line  |  从命令行运行TensorFlow}\r\n\r\nSee \\hyperref[comm_prob]{common problems} if an error happens.\r\n\r\nⒸ 如果遇到任何报错，请参考\\hyperref[comm_prob]{常见问题}．\r\n\r\nOpen a terminal and type the following:\r\n\r\nⒸ 打开终端，输入以下指令：\r\n\r\n\\begin{lstlisting}\r\n$ python\r\n...\r\n>>> import tensorflow as tf\r\n>>> hello = tf.constant('Hello, TensorFlow!')\r\n>>> sess = tf.Session()\r\n>>> print(sess.run(hello))\r\nHello, TensorFlow!\r\n>>> a = tf.constant(10)\r\n>>> b = tf.constant(32)\r\n>>> print(sess.run(a + b))\r\n42\r\n>>>\r\n\\end{lstlisting}\r\n\r\n%%%\r\n\\subsubsection {Run a TensorFlow demo model  |  运行一个TensorFlow的演示模型}\r\n\r\nAll TensorFlow packages, including the demo models, are installed in the Python library. The exact location of the Python library depends on your system, but is usually one of:\r\n\r\nⒸ 所有版本的TensorFlow的Python库中包都附带了一些演示模型． 具体位位置取决于您的系统，它们通常会在以下位置出现：\r\n\r\n\\begin{lstlisting}\r\n/usr/local/lib/python2.7/dist-packages/tensorflow\r\n/usr/local/lib/python2.7/site-packages/tensorflow\r\n\\end{lstlisting}\r\n\r\nYou can find out the directory with the following command:\r\nⒸ 您可以用以下指令找到它的路径：\r\n\r\n\\begin{lstlisting}\r\n$ python -c 'import site; print(\"\\n\".join(site.getsitepackages()))'\r\n\\end{lstlisting}\r\n\r\nThe simple demo model for classifying handwritten digits from the MNIST dataset is in the sub-directory \\lstinline{models/image/mnist/convolutional.py}. You can run it from the command line as follows:\r\n\r\nⒸ 在子目录\\lstinline{models/image/mnist/convolutional.py}可以找到一个使用MNIST数据集进行手写数字识别的简单案例．您可以使用以下指令在命令行中直接运行：\r\n\r\n\\begin{lstlisting}\r\n# Using 'python -m' to find the program in the python search path:\r\n$ python -m tensorflow.models.image.mnist.convolutional\r\nExtracting data/train-images-idx3-ubyte.gz\r\nExtracting data/train-labels-idx1-ubyte.gz\r\nExtracting data/t10k-images-idx3-ubyte.gz\r\nExtracting data/t10k-labels-idx1-ubyte.gz\r\n...etc...\r\n\r\n# You can alternatively pass the path to the model program file to the python interpreter.\r\n$ python /usr/local/lib/python2.7/dist-packages/tensorflow/models/image/mnist/convolutional.py\r\n...\r\n\\end{lstlisting}\r\n\r\n\r\n%\r\n%%\r\n\\subsection {Installing from source}\r\n\r\nⒺ \\textcolor{etc}{When installing from source you will build a pip wheel that you then install using pip. You'll need pip for that, so install it as described \\hyperref[pip_install]{above}.}\r\n\r\n%%%\r\n\\subsubsection {Clone the TensorFlow repository}\r\n\\begin{lstlisting}\r\n$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow\r\n\\end{lstlisting}\r\n\\lstinline{--recurse-submodules} is required to fetch the protobuf library that TensorFlow depends on.\r\n\r\n\r\n%%%\r\n\\subsubsection {Installation for Linux}\r\n\r\n\\paragraph{Install Bazel}\r\n\r\nFollow instructions here to install the dependencies for Bazel. Then download bazel version 0.1.1 using the installer for your system and run the installer as mentioned there:\r\n\r\n\\begin{lstlisting}\r\n$ chmod +x PATH_TO_INSTALL.SH\r\n$ ./PATH_TO_INSTALL.SH --user\r\n\\end{lstlisting}\r\n\r\nRemember to replace \\lstinline{PATH_TO_INSTALL.SH} with the location where you downloaded the installer.\r\n\r\nFinally, follow the instructions in that script to place bazel into your binary path.\r\n\r\n\\paragraph{Install other dependencies}\\\\\r\n\r\n\\begin{lstlisting}\r\n$ sudo apt-get install python-numpy swig python-dev\r\n\\end{lstlisting}\r\n\r\n\\paragraph{Configure the installation}\r\n\r\nRun the configure script at the root of the tree. The configure script asks you for the path to your python interpreter and allows (optional) configuration of the CUDA libraries (see \\hyperref[install_cuda]{below}).\r\n\r\nThis step is used to locate the python and numpy header files.\r\n\r\n\\begin{lstlisting}\r\n$ ./configure\r\nPlease specify the location of python. [Default is /usr/bin/python]:\r\n\\end{lstlisting}\r\n\r\n\\paragraph{Optional: Install CUDA (GPUs on Linux)} \\label{install_cuda}\r\n\r\nIn order to build or run TensorFlow with GPU support, both Cuda Toolkit 7.0 and CUDNN 6.5 V2 from NVIDIA need to be installed.\r\n\r\nTensorFlow GPU support requires having a GPU card with NVidia Compute Capability >= 3.5. Supported cards include but are not limited to:\r\n\r\n\\begin{itemize}\r\n\\item NVidia Titan\r\n\\item NVidia Titan X\r\n\\item NVidia K20\r\n\\item NVidia K40\r\n\\end{itemize}\r\n\r\nDownload and install Cuda Toolkit 7.0\r\n\r\nhttps://developer.nvidia.com/cuda-toolkit-70\r\n\r\nInstall the toolkit into e.g. /usr/local/cuda\r\n\r\nDownload and install CUDNN Toolkit 6.5\r\n\r\nhttps://developer.nvidia.com/rdp/cudnn-archive\r\n\r\nUncompress and copy the cudnn files into the toolkit directory. Assuming the toolkit is installed in /usr/local/cuda:\r\n\r\n\\begin{lstlisting}\r\ntar xvzf cudnn-6.5-linux-x64-v2.tgz\r\nsudo cp cudnn-6.5-linux-x64-v2/cudnn.h /usr/local/cuda/include\r\nsudo cp cudnn-6.5-linux-x64-v2/libcudnn* /usr/local/cuda/lib64\r\n\\end{lstlisting}\r\n\r\nConfigure TensorFlow's canonical view of Cuda libraries\r\n\r\nWhen running the configure script from the root of your source tree, select the option Y when asked to build TensorFlow with GPU support.\r\n\r\n\\begin{lstlisting}\r\n$ ./configure\r\nPlease specify the location of python. [Default is /usr/bin/python]:\r\nDo you wish to build TensorFlow with GPU support? [y/N] y\r\nGPU support will be enabled for TensorFlow\r\n\r\nPlease specify the location where CUDA 7.0 toolkit is installed. Refer to\r\nREADME.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda\r\n\r\nPlease specify the location where CUDNN 6.5 V2 library is installed. Refer to\r\nREADME.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda\r\n\r\nSetting up Cuda include\r\nSetting up Cuda lib64\r\nSetting up Cuda bin\r\nSetting up Cuda nvvm\r\nConfiguration finished\r\n\\end{lstlisting}\r\n\r\nThis creates a canonical set of symbolic links to the Cuda libraries on your system. Every time you change the Cuda library paths you need to run this step again before you invoke the bazel build command.\r\n\r\nBuild your target with GPU support\r\n\r\nFrom the root of your source tree, run:\r\n\r\n\\begin{lstlisting}\r\n$ bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer\r\n\r\n$ bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu\r\n# Lots of output. This tutorial iteratively calculates the major eigenvalue of\r\n# a 2x2 matrix, on GPU. The last few lines look like this.\r\n000009/000005 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]\r\n000006/000001 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]\r\n000009/000009 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]\r\n\\end{lstlisting}\r\n\r\nNote that \"--config=cuda\" is needed to enable the GPU support.\r\n\r\nEnabling Cuda 3.0\r\n\r\nTensorFlow officially supports Cuda devices with 3.5 and 5.2 compute capabilities. In order to enable earlier Cuda devices such as Grid K520, you need to target Cuda 3.0. This can be done through TensorFlow unofficial settings with \"configure\".\r\n\r\n\\begin{lstlisting}\r\n$ TF_UNOFFICIAL_SETTING=1 ./configure\r\n\r\n# Same as the official settings above\r\n\r\nWARNING: You are configuring unofficial settings in TensorFlow. Because some\r\nexternal libraries are not backward compatible, these settings are largely\r\nuntested and unsupported.\r\n\r\nPlease specify a list of comma-separated Cuda compute capabilities you want to\r\nbuild with. You can find the compute capability of your device at:\r\nhttps://developer.nvidia.com/cuda-gpus.\r\nPlease note that each additional compute capability significantly increases\r\nyour build time and binary size. [Default is: \"3.5,5.2\"]: 3.0\r\n\r\nSetting up Cuda include\r\nSetting up Cuda lib64\r\nSetting up Cuda bin\r\nSetting up Cuda nvvm\r\nConfiguration finished\r\n\\end{lstlisting}\r\n\r\nKnown issues\r\n\r\nAlthough it is possible to build both Cuda and non-Cuda configs under the same source tree, we recommend to run \\\"bazel clean\\\" when switching between these two configs in the same source tree.\r\n\r\nYou have to run configure before running bazel build. Otherwise, the build will fail with a clear error message. In the future, we might consider making this more conveninent by including the configure step in our build process, given necessary bazel new feature support.\r\n\r\n\r\n%%%\r\n\\subsubsection {Installation for Mac OS X}\r\n\r\nWe recommend using \\href{http://brew.sh/}{homebrew} to install the bazel and SWIG dependencies, and installing python dependencies using easy_install or pip.\r\n\r\n\\paragraph{Dependencies}\r\n\r\nFollow instructions here to install the dependencies for Bazel. You can then use homebrew to install bazel and SWIG:\r\n\r\n\\begin{lstlisting}\r\n$ brew install bazel swig\r\n\\end{lstlisting}\r\n\r\nYou can install the python dependencies using easy_install or pip. Using easy_install, run\r\n\r\n\\begin{lstlisting}\r\n$ sudo easy_install -U six\r\n$ sudo easy_install -U numpy\r\n$ sudo easy_install wheel\r\n\\end{lstlisting}\r\n\r\nWe also recommend the \\href{https://ipython.org/}{ipython} enhanced python shell, so best install that too:\r\n\r\n\\begin{lstlisting}\r\n$ sudo easy_install ipython\r\n\\end{lstlisting}\r\n\r\n\\paragraph{Configure the installation}\r\n\r\nRun the \\lstinline{configure} script at the root of the tree. The configure script asks you for the path to your python interpreter.\r\n\r\nThis step is used to locate the python and numpy header files.\r\n\r\n\\begin{lstlisting}\r\n$ ./configure\r\nPlease specify the location of python. [Default is /usr/bin/python]:\r\nDo you wish to build TensorFlow with GPU support? [y/N]\r\n\\end{lstlisting}\r\n\r\n%%%\r\n\\subsubsection {Create the pip package and install}\r\n\r\n\\begin{lstlisting}\r\n$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package\r\n\r\n# To build with GPU support:\r\n$ bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package\r\n\r\n$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg\r\n\r\n# The name of the .whl file will depend on your platform.\r\n$ pip install /tmp/tensorflow_pkg/tensorflow-0.5.0-cp27-none-linux_x86_64.whl\r\n\\end{lstlisting}\r\n\r\n%\r\n%%\r\n\\subsection {Train your first TensorFlow neural net model  |  训练第一个TensorFlow模型}\r\n\r\nⒺ \\textcolor{etc}{Starting from the root of your source tree, run:}\r\n\r\n从根目录开始运行一下指令：\r\n\r\n\\begin{lstlisting}\r\n$ cd tensorflow/models/image/mnist\r\n$ python convolutional.py\r\nSuccesfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.\r\nSuccesfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.\r\nSuccesfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.\r\nSuccesfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.\r\nExtracting data/train-images-idx3-ubyte.gz\r\nExtracting data/train-labels-idx1-ubyte.gz\r\nExtracting data/t10k-images-idx3-ubyte.gz\r\nExtracting data/t10k-labels-idx1-ubyte.gz\r\nInitialized!\r\nEpoch 0.00\r\nMinibatch loss: 12.054, learning rate: 0.010000\r\nMinibatch error: 90.6%\r\nValidation error: 84.6%\r\nEpoch 0.12\r\nMinibatch loss: 3.285, learning rate: 0.010000\r\nMinibatch error: 6.2%\r\nValidation error: 7.0%\r\n...\r\n...\r\n\\end{lstlisting}\r\n\r\n\r\n%\r\n%%\r\n\r\n% \\subsection {Common Problems} \\label{comm_prob}\r\n\\subsection {Common Problems  |  常见问题} \\label{comm_prob}\r\n\r\n%%%\r\n\\subsubsection {GPU-related issues  |  GPU有关问题}\r\n\r\nIf you encounter the following when trying to run a TensorFlow program:\r\n\r\n\\begin{lstlisting}\r\nImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory\r\n\\end{lstlisting}\r\n\r\nMake sure you followed the the GPU installation \\hyperref[install_cuda]{instructions}.\r\n\r\n%%%\r\n\\subsubsection {Pip installation issues  |  Pip安装中的问题}\r\n\r\n\\paragraph{\\lstinline{Can't find setup.py}}\r\n\r\nIf, during pip install, you encounter an error like:\r\n\r\n\\begin{lstlisting}\r\n...\r\nIOError: [Errno 2] No such file or directory: '/tmp/pip-o6Tpui-build/setup.py'\r\n\\end{lstlisting}\r\n\r\nSolution: upgrade your version of pip:\r\n\r\n\\begin{lstlisting}\r\npip install --upgrade pip\r\n\\end{lstlisting}\r\n\r\nThis may require sudo, depending on how pip is installed.\r\n\r\n\\paragraph{\\lstinline{SSLError: SSL_VERIFY_FAILED}}\r\n\r\nIf, during pip install from a URL, you encounter an error like:\r\n\r\n\\begin{lstlisting}\r\n...\r\nSSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed\r\n\\end{lstlisting}\r\n\r\nSolution: Download the wheel manually via curl or wget, and pip install locally.\r\n\r\n%%%\r\n\\subsubsection {Linux issues}\r\n\r\nIf you encounter:\r\n\r\n\\begin{lstlisting}\r\n...\r\n \"__add__\", \"__radd__\",\r\n             ^\r\nSyntaxError: invalid syntax\r\n\\end{lstlisting}\r\n\r\nSolution: make sure you are using Python 2.7.\r\n\r\n%%%\r\n\\subsubsection {Mac OS X: ImportError: No module named copyreg}\r\n\r\nOn Mac OS X, you may encounter the following when importing tensorflow.\r\n\r\n\\begin{lstlisting}\r\n>>> import tensorflow as tf\r\n...\r\nImportError: No module named copyreg\r\n\\end{lstlisting}\r\n\r\nSolution: TensorFlow depends on protobuf, which requires the Python package \\lstinline{six-1.10.0}. Apple's default Python installation only provides \\lstinline{six-1.4.1}.\r\n\r\nYou can resolve the issue in one of the following ways:\r\n\\begin{itemize}\r\n\\item pgrade the Python installation with the current version of \\lstinline{six}:\r\n\\begin{lstlisting}\r\n$ sudo easy_install -U six\r\n\\end{lstlisting}\r\n\r\n\\item Install TensorFlow with a separate Python library:\r\n  \\begin{itemize}\r\n  \\item Virtualenv\r\n  \\item Docker\r\n  \\end{itemize}\r\nInstall a separate copy of Python via Homebrew or MacPorts and re-install TensorFlow in that copy of Python.\r\n\\end{itemize}\r\n\r\n\r\n\r\n\r\n%%%\r\n\\subsubsection {Mac OS X: TypeError: \\lstinline{__init__()} got an unexpected keyword argument 'syntax'}\r\n\r\nOn Mac OS X, you may encounter the following when importing tensorflow.\r\n\r\n\\begin{lstlisting}\r\n>>> import tensorflow as tf\r\nTraceback (most recent call last):\r\n  File \"<stdin>\", line 1, in <module>\r\n  File \"/usr/local/lib/python2.7/site-packages/tensorflow/__init__.py\", line 4, in <module>\r\n    from tensorflow.python import *\r\n  File \"/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py\", line 13, in <module>\r\n    from tensorflow.core.framework.graph_pb2 import *\r\n...\r\n  File \"/usr/local/lib/python2.7/site-packages/tensorflow/core/framework/tensor_shape_pb2.py\", line 22, in <module>\r\n    serialized_pb=_b('\\n,tensorflow/core/framework/tensor_shape.proto\\x12\\ntensorflow\\\"d\\n\\x10TensorShapeProto\\x12-\\n\\x03\\x64im\\x18\\x02 \\x03(\\x0b\\x32 .tensorflow.TensorShapeProto.Dim\\x1a!\\n\\x03\\x44im\\x12\\x0c\\n\\x04size\\x18\\x01 \\x01(\\x03\\x12\\x0c\\n\\x04name\\x18\\x02 \\x01(\\tb\\x06proto3')\r\nTypeError: __init__() got an unexpected keyword argument 'syntax'\r\n\\end{lstlisting}\r\n\r\nThis is due to a conflict between protobuf versions (we require protobuf 3.0.0). The best current solution is to make sure older versions of protobuf are not installed, such as:\r\n\r\n\\begin{lstlisting}\r\n$ pip install --upgrade protobuf\r\n\\end{lstlisting}\r\n\r\n\r\n原文：\\href{http://tensorflow.org/get_started/os_setup.md}{Download and Setup}\r\n"
  },
  {
    "path": "tex_pdf/get_started/c1s03_basic_usage.tex",
    "content": "%!TEX program = xelatex\r\n% Encoding: UTF8\r\n% SEIKA 2016 ｜ seika@live.ca\r\n\r\n% Chapter 1\r\n% Section 1.3 Introduction\r\n\r\n\\section{Basic Usage   ||   使用基础} \\label{basic_usage}\r\n\r\nⒺ \\textcolor{etc}{To use TensorFlow you need to understand how TensorFlow:}\r\n\r\n\\begin{itemize}\r\n\\item \\textcolor{etc}{Represents computations as \\emph{graphs}.}\r\n\\item \\textcolor{etc}{Executes graphs in the context of \\lstinline{Sessions}.}\r\n\\item \\textcolor{etc}{Represents data as \\emph{tensors}.}\r\n\\item \\textcolor{etc}{Maintains state with \\lstinline{Variables}.}\r\n\\item \\textcolor{etc}{Uses feeds and fetches to get data into and out of arbitrary operations.}\r\n\\end{itemize}\r\n\r\nⒸ 使用TensorFlow之前你需要了解关于TensorFlow的以下基础知识:\r\n\r\n\\begin{itemize}\r\n\\item 使用\\emph{图}(\\emph{graphs})来表示计算.\\index{graph}\r\n\\item 在\\emph{会话}(\\lstinline{Session})中执行图.\\index{session}\r\n\\item 使用\\emph{张量}(\\emph{tensors})来代表数据.\\index{tensor}\r\n\\item 通过\\emph{变量}(\\lstinline{Variables})维护状态.\\index{variable}\r\n\\item 使用\\emph{供给}(\\lstinline{feeds})和\\emph{取回}(\\lstinline{fetches})将数据传入或传出任何操作.\r\n\\end{itemize}\r\n\r\n%\r\n%%\r\n\\subsection{Overview  |  总览}\r\n\r\nⒺ \\textcolor{etc}{TensorFlow is a programming system in which you represent computations as graphs. Nodes in the graph are called ops (short for operations). An op takes zero or more \\emph{Tensors}, performs some computation, and produces zero or more \\emph{Tensors}. A Tensor is a typed multi-dimensional array. For example, you can represent a mini-batch of images as a 4-D array of floating point numbers with dimensions \\lstinline{[batch, height, width, channels]}.}\r\n\r\nⒸ TensorFlow 是一个以\\emph{图}(\\emph{graphs})来表示计算的编程系统,图中的节点被称之为 op~(operation的缩写). 一个 op 获得零或多个\\emph{张量}(\\emph{tensors})执行计算,产生零或多个\\emph{张量}。\\emph{张量} 是一个按类型划分的多维数组。例如, 你可以将一小组图像集表示为一个四维浮点数数组,这四个维度分别是\\lstinline{[batch, height, width, channels]}。\r\n\r\nⒺ \\textcolor{etc}{A TensorFlow graph is a description of computations. To compute anything, a graph must be launched in a \\lstinline{Session}. A Session places the graph ops onto \\lstinline{Devices}, such as CPUs or GPUs, and provides methods to execute them. These methods return tensors produced by ops as \\href{http://www.numpy.org/}{numpy} ndarray objects in Python, and as \\lstinline{tensorflow::Tensor} instances in C and C++.}\r\n\r\nⒸ TensorFlow的图是一种对计算的抽象描述。在计算开始前, 图必须在 \\emph{会话}(\\lstinline{Session()}) 中被启动.\\emph{会话}将图的op分发到如 CPU 或 GPU 之类的 \\emph{设备}(\\lstinline{Devices()}) 上, 同时提供执行 op 的方法。这些方法执行后, 将产生的\\emph{张量}(\\emph{tensor})返回。在 Python 语言中, 将返回\\href{http://www.numpy.org}{numpy}的\\lstinline{ndarray} 对象; 在 C 和 C++ 语言中, 将返回\\lstinline{tensorflow::Tensor}实例。\r\n\r\n\r\n%\r\n%%\r\n%✠ \\subsection{The computation graph}\r\n\\subsection {The computation graph  |  计算图} \\label{computation_graph}\r\n\r\nⒺ \\textcolor{etc}{TensorFlow programs are usually structured into a \\emph{construction phase}, that assembles a graph, and an \\emph{execution phase} that uses a session to execute ops in the graph.}\r\n\r\nⒸ 通常，TensorFlow编程可按两个阶段组织起来:\\emph{构建阶段}和\\emph{执行阶段};前者用于组织\\emph{计算图}，而后者利用session中执行\\emph{计算图}中的op操作。\r\n\r\nⒺ \\textcolor{etc}{For example, it is common to create a graph to represent and train a neural network in the construction phase, and then repeatedly execute a set of training ops in the graph in the execution phase.}\r\n\r\nⒸ 例如,在构建阶段创建一个图来表示和训练神经网络，然后在执行阶段反复执行一组op来实现图中的训练。\r\n\r\nⒺ \\textcolor{etc}{TensorFlow can be used from C, C++, and Python programs. It is presently much easier to use the Python library to assemble graphs, as it provides a large set of helper functions not available in the C and C++ libraries.}\r\n\r\nⒸ TensorFlow 支持 C、 C++、 Python 编程语言。 目前, TensorFlow 的 Python 库更加易用,它提供了大量的辅助函数来简化构建图的工作, 而这些函数在 C 和 C++ 库中尚不被支持。\r\n\r\nⒺ \\textcolor{etc}{The session libraries have equivalent functionalities for the three languages.}\r\n\r\nⒸ 这三种语言的会话库 (session libraries) 是一致的.\r\n\r\n%%%\r\n% \\subsubsection {Building the graph}\r\n\\subsubsection {Building the graph  |  构建计算图}\r\n\r\nⒺ \\textcolor{etc}{To build a graph start with ops that do not need any input (source ops), such as Constant, and pass their output to other ops that do computation.}\r\n\r\nⒸ 刚开始基于op建立图的时候一般不需要任何的输入源(source op)，例如输入常量(\\lstinline{Constance})，再将它们传递给其它 op 执行运算。\r\n\r\nⒺ \\textcolor{etc}{The ops constructors in the Python library return objects that stand for the output of the constructed ops. You can pass these to other ops constructors to use as inputs.}\r\n\r\nⒸ Python库中的op构造函数返回代表已被组织好的op作为输出对象，这些对象可以传递给其它\r\nop构造函数作为输入。\r\n\r\nⒺ \\textcolor{etc}{The TensorFlow Python library has a default graph to which ops constructors add nodes. The default graph is sufficient for many applications. See the \\hyperref[class-tf.graph]{Graph class} documentation for how to explicitly manage multiple graphs.}\r\n\r\nⒸ TensorFlow Python 库有一个可被 op构造函数加入计算结点的默认图 (default graph)。 对大多数应用来说，这个默认图已经足够用了。阅读 \\hyperref[class-tf.graph]{Graph 类}文档来了解如何明晰的管理多个图.\r\n\r\n\\begin{lstlisting}\r\nimport tensorflow as tf\r\n\r\n# Create a Constant op that produces a 1x2 matrix.  The op is\r\n# added as a node to the default graph.\r\n#\r\n# The value returned by the constructor represents the output\r\n# of the Constant op.\r\nmatrix1 = tf.constant([[3., 3.]])\r\n\r\n# Create another Constant that produces a 2x1 matrix.\r\nmatrix2 = tf.constant([[2.],[2.]])\r\n\r\n# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.\r\n# The returned value, 'product', represents the result of the matrix\r\n# multiplication.\r\nproduct = tf.matmul(matrix1, matrix2)\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{The default graph now has three nodes: two constant() ops and one matmul() op. To actually multiply the matrices, and get the result of the multiplication, you must launch the graph in a session.}\r\n\r\nⒸ 默认图现在拥有三个节点，两个\\lstinline{constant()} op，一个\\lstinline{matmul()} op. 为了真正进行矩阵乘法运算，得到乘法结果, 你必须在一个会话(session)中载入启动这个图。\r\n\r\n\r\n%%%\r\n\\subsubsection {Launching the graph in a session  |  在会话中载入图} \\label{launching_graph}\r\n\r\nⒺ \\textcolor{etc}{Launching follows construction. To launch a graph, create a Session object. Without arguments the session constructor launches the default graph.}\r\n\r\nⒺ \\textcolor{etc}{See the \\hyperref[class-tf.session]{Session class} for the complete session API.}\r\n\r\nⒸ 构建过程完成后就可运行执行过程。为了载入之前所构建的图，必须先创建一个\\emph{会话}对象(\\lstinline{Session} object)。会话构建器在未指明参数时会载入默认的图。\r\n\r\nⒸ 完整的会话API资料，请参见\\hyperref[class-tf.session]{\\emph{会话类}(Session object)}。\r\n\r\n\\begin{lstlisting}\r\n# Launch the default graph.\r\nsess = tf.Session()\r\n\r\n# To run the matmul op we call the session 'run()' method, passing 'product'\r\n# which represents the output of the matmul op.  This indicates to the call\r\n# that we want to get the output of the matmul op back.\r\n#\r\n# All inputs needed by the op are run automatically by the session.  They\r\n# typically are run in parallel.\r\n#\r\n# The call 'run(product)' thus causes the execution of threes ops in the\r\n# graph: the two constants and matmul.\r\n#\r\n# The output of the op is returned in 'result' as a numpy `ndarray` object.\r\nresult = sess.run(product)\r\nprint(result)\r\n# ==> [[ 12.]]\r\n\r\n# Close the Session when we're done.\r\nsess.close()\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{Sessions should be closed to release resources. You can also enter a Session with a \"with\" block. The Session closes automatically at the end of the with block.}\r\n\r\nⒸ 会话在完成后必须关闭以释放资源。你也可以使用\\lstinline{\"with\"}句块开始一个会话，该会话将在\\lstinline{\"with\"}句块结束时自动关闭。\r\n\r\n\\begin{lstlisting}\r\nwith tf.Session() as sess:\r\n  result = sess.run([product])\r\n  print(result)\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{The TensorFlow implementation translates the graph definition into executable operations distributed across available compute resources, such as the CPU or one of your computer's GPU cards. In general you do not have to specify CPUs or GPUs explicitly. TensorFlow uses your first GPU, if you have one, for as many operations as possible.}\r\n\r\nⒸ TensorFlow 事实上通过一个“翻译”过程，将定义的图转化为不同的可用计算资源间实现分布计算的操作，如CPU或是显卡GPU。通常不需要用户指定具体使用的CPU或GPU，TensorFlow 能自动检测并尽可能的充分利用找到的第一个 GPU 进行运算。\r\n\r\nⒺ \\textcolor{etc}{If you have more than one GPU available on your machine, to use a GPU beyond the first you must assign ops to it explicitly. Use with...Device statements to specify which CPU or GPU to use for operations:}\r\n\r\nⒸ 如果你的设备上有不止一个GPU，你需要明确指定op操作到不同的运算设备以调用它们。使用\\lstinline{with...Device}语句明确指定哪个CPU或GPU将被调用:\r\n\r\n\\begin{lstlisting}\r\nwith tf.Session() as sess:\r\n  with tf.device(\"/gpu:1\"):\r\n    matrix1 = tf.constant([[3., 3.]])\r\n    matrix2 = tf.constant([[2.],[2.]])\r\n    product = tf.matmul(matrix1, matrix2)\r\n    ...\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{Devices are specified with strings. The currently supported devices are:}\\\\\r\nⒺ \\textcolor{etc}{\\lstinline{\"/cpu:0\"}: The CPU of your machine.}\\\\\r\nⒺ \\textcolor{etc}{\\lstinline{\"/gpu:0\"}: The GPU of your machine, if you have one.}\\\\\r\nⒺ \\textcolor{etc}{\\lstinline{\"/gpu:1\"}: The second GPU of your machine, etc.}\r\n\r\nⒺ \\textcolor{etc}{See Using GPUs for more information about GPUs and TensorFlow.}\r\n\r\nⒸ 使用字符串指定设备，目前支持的设备包括:\\\\\r\nⒸ \\lstinline{\"/cpu:0\"}：计算机的CPU；\\\\\r\nⒸ \\lstinline{\"/gpu:0\"}：计算机的第一个GPU，如果可用；\\\\\r\nⒸ \\lstinline{\"/gpu:1\"}：计算机的第二个GPU，以此类推。\r\n\r\nⒸ 关于使用GPU的更多信息，请参阅\\textbf{GPU使用}。\r\n\r\n%\r\n%%\r\n\\subsection{Interactive Usage  |  交互式使用}\r\n\r\nⒺ \\textcolor{etc}{The Python examples in the documentation launch the graph with a \\hyperref[class-tf.session]{\\lstinline{Session}} and use the \\hyperref[tf.session.run]{\\lstinline{Session.run()}} method to execute operations.}\r\n\r\nⒺ \\textcolor{etc}{For ease of use in interactive Python environments, such as \\href{http://ipython.org/}{\\lstinline{IPython}} you can instead use the \\hyperref[class-tf.interactivesession]{\\lstinline{InteractiveSession}} class, and the \\hyperref[tf.tensor.eval]{\\lstinline{Tensor.eval()}} and \\hyperref[tf.operation.run]{\\lstinline{Operation.run()}} methods. This avoids having to keep a variable holding the session.}\r\n\r\nⒸ 文档中的 Python 示例使用一个会话 \\hyperref[class-tf.session]{\\lstinline{Session}} 来启动图, 并调用 \\hyperref[tf.session.run]{\\lstinline{Session.run()}} 方法执行操作。\r\n\r\nⒸ 考虑到如\\href{http://ipython.org}{IPython}这样的交互式Python环境的易用, 可以使用\\hyperref[class-tf.interactivesession]{\\lstinline{InteractiveSession}} 代替\\lstinline{Session}类, 使用 \\hyperref[tf.tensor.eval]{\\lstinline{Tensor.eval()}}和 \\hyperref[tf.operation.run]{\\lstinline{Operation.run()}} 方法代替 \\lstinline{Session.run()}. 这样可以避免使用一个变量来持有会话.\r\n\r\n\\begin{lstlisting}\r\n# Enter an interactive TensorFlow Session.\r\nimport tensorflow as tf\r\nsess = tf.InteractiveSession()\r\n\r\nx = tf.Variable([1.0, 2.0])\r\na = tf.constant([3.0, 3.0])\r\n\r\n# Initialize 'x' using the run() method of its initializer op.\r\nx.initializer.run()\r\n\r\n# Add an op to subtract 'a' from 'x'.  Run it and print the result\r\nsub = tf.subtract(x, a)\r\nprint(sub.eval())\r\n# ==> [-2. -1.]\r\n\r\n# Close the Session when we're done.\r\nsess.close()\r\n\\end{lstlisting}\r\n\r\n%\r\n%%\r\n\r\n\\subsection{Tensors  |  张量}\r\nⒺ \\textcolor{etc}{TensorFlow programs use a tensor data structure to represent all data -- only tensors are passed between operations in the computation graph. You can think of a TensorFlow tensor as an n-dimensional array or list. A tensor has a static type, a rank, and a shape. To learn more about how TensorFlow handles these concepts, see the \\href{https://www.tensorflow.org/versions/master/resources/dims_types.html#tensor-ranks-shapes-and-types}{Rank, Shape, and Type} reference.}\r\n\r\nⒸ TensorFlow 程序使用 tensor 数据结构来代表所有的数据, 计算图中, 操作间传递的数据都是 tensor. 你可以把 TensorFlow 的张量看作是一个 n 维的数组或列表. 一个 tensor 包含一个静态类型 rank, 和一个 shape. 想了解 TensorFlow 是如何处理这些概念的, 参见 Rank, Shape, 和 Type]。\r\n\r\n\r\n%\r\n%%\r\n\\subsection{Variables  |  变量}\r\n\r\nⒺ \\textcolor{etc}{Variables maintain state across executions of the graph. The following example shows a variable serving as a simple counter. See \\hyperref[variables]{Variables} for more details.}\r\n\r\nⒸ \\emph{变量}维持了图执行过程中的状态信息。下面的例子演示了如何使用变量实现一个简单的计数器，更多细节详见\\hyperref[variables]{变量}章节。\r\n\r\n\\begin{lstlisting}\r\n# Create a Variable, that will be initialized to the scalar value 0.\r\n# 建立一个变量，用0初始化它的值\r\nstate = tf.Variable(0, name=\"counter\")\r\n\r\n# Create an Op to add one to `state`.\r\n\r\none = tf.constant(1)\r\nnew_value = tf.add(state, one)\r\nupdate = tf.assign(state, new_value)\r\n\r\n# Variables must be initialized by running an `init` Op after having\r\n# launched the graph.  We first have to add the `init` Op to the graph.\r\ninit_op = tf.global_variables_initializer()\r\n\r\n# Launch the graph and run the ops.\r\nwith tf.Session() as sess:\r\n  # Run the 'init' op\r\n  sess.run(init_op)\r\n  # Print the initial value of 'state'\r\n  print(sess.run(state))\r\n  # Run the op that updates 'state' and print 'state'.\r\n  for _ in range(3):\r\n    sess.run(update)\r\n    print(sess.run(state))\r\n\r\n# output:\r\n\r\n# 0\r\n# 1\r\n# 2\r\n# 3\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{The \\lstinline{assign()} operation in this code is a part of the expression graph just like the \\lstinline{add()} operation, so it does not actually perform the assignment until \\lstinline{run()} executes the expression.}\r\n\r\nⒸ 代码中\\lstinline{assign()}操作是图所描绘的表达式的一部分, 正如\\lstinline {add()}操作一样. 所以在调用\\lstinline {run()}执行表达式之前, 它并不会真正执行赋值操作.\r\n\r\nⒺ \\textcolor{etc}{TYou typically represent the parameters of a statistical model as a set of Variables. For example, you would store the weights for a neural network as a tensor in a Variable. During training you update this tensor by running a training graph repeatedly.}\r\n\r\nⒸ 通常会将一个统计模型中的参数表示为一组变量. 例如, 你可以将一个神经网络的权重作为某个变量存储在一个 tensor 中. 在训练过程中, 通过重复运行训练图, 更新这个 tensor.\r\n\r\n\r\n%\r\n%%\r\n\\subsection{Fetches  |  取回}\r\n\r\nⒺ \\textcolor{etc}{To fetch the outputs of operations, execute the graph with a \\lstinline{run()} call on the \\lstinline{Session} object and pass in the tensors to retrieve. In the previous example we fetched the single node \\lstinline{state}, but you can also fetch multiple tensors:}\r\n\r\nⒸ 为了取回操作的输出内容, 可以在使用 \\lstinline{Session} 对象的 \\lstinline{run()} 调用 执行图时, 传入一些 tensor,这些 tensor 会帮助你取回结果. 在之前的例子里, 我们只取回了单个节点\\lstinline{state}, 但是你也可以取回多个tensor:\r\n\r\n\\begin{lstlisting}\r\ninput1 = tf.constant(3.0)\r\ninput2 = tf.constant(2.0)\r\ninput3 = tf.constant(5.0)\r\nintermed = tf.add(input2, input3)\r\nmul = tf.multiply(input1, intermed)\r\n\r\nwith tf.Session() as sess:\r\n  result = sess.run([mul, intermed])\r\n  print(result)\r\n\r\n# output:\r\n# [array([ 21.], dtype=float32), array([ 7.], dtype=float32)]\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{All the ops needed to produce the values of the requested tensors are run once (not once per requested tensor).}\r\n\r\nⒸ 需要获取的多个 tensor 值，在 op 的一次运行中一起获得（而不是逐个去获取 tensor）。\r\n\r\n%\r\n%%\r\n\\subsection{Feeds  |  供给}\r\n\r\nⒺ \\textcolor{etc}{The examples above introduce tensors into the computation graph by storing them in \\lstinline{Constants} and \\lstinline{Variables}. TensorFlow also provides a feed mechanism for patching a tensor directly into any operation in the graph.}\r\n\r\nⒸ 上述示例在计算图中引入了 tensor, 以 \\emph{常量} (\\lstinline{Constants}) 或 \\emph{变量}(\\lstinline{Variables}) 的形式存储. TensorFlow 还提 \\emph{供给}(\\emph{feed}) 机制, 该机制可临时替代图中的任意操作中的 tensor 可以对图中任何操作提交补丁, 直接插入一个 tensor.\r\n\r\nⒺ \\textcolor{etc}{A feed temporarily replaces the output of an operation with a tensor value. You supply feed data as an argument to a \\lstinline{run()} call. The feed is only used for the run call to which it is passed. The most common use case involves designating specific operations to be \"feed\" operations by using \\lstinline{tf.placeholder()} to create them:}\r\n\r\nⒸ feed 使用一个 tensor 值临时替换一个操作的输出结果. 你可以提供 feed 数据作为 \\lstinline {run()} 调用的参数.feed 只在调用它的方法内有效, 方法结束, feed 就会消失. 最常见的用例是将某些特殊的操作指定为 \"feed\" 操作, 标记的方法是使用\\lstinline{tf.placeholder()}为这些操作创建占位符.\r\n\r\n\\begin{lstlisting}\r\ninput1 = tf.placeholder(tf.float32)\r\ninput2 = tf.placeholder(tf.float32)\r\noutput = tf.multiply(input1, input2)\r\n\r\nwith tf.Session() as sess:\r\n  print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))\r\n\r\n# output:\r\n# [array([ 14.], dtype=float32)]\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{A \\lstinline{placeholder()} operation generates an error if you do not supply a feed for it. See the \\hyperref[minist_tf]{MNIST fully-connected feed tutorial} (\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/fully_connected_feed.py}{source code}) for a larger-scale example of feeds.}\r\n\r\nⒸ 如果没有正确供给, \\lstinline{placeholder()} 操作将会产生一个错误提示.关于feed的规模更大的案例，参见\\hyperref[minist_tf]{MNIST 全连通 feed 教程}以及其\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/fully_connected_feed.py}{源代码}。\r\n\r\n\\href{http://tensorflow.org/get_started/basic_usage.md}{原文：Basic Usage}\r\n"
  },
  {
    "path": "tex_pdf/how_tos/c3s00_overview.tex",
    "content": "\r\n\r\n% \\section{综述 Overview}\\label{ux7efcux8ff0-overview}\r\n\r\n综述 Overview\r\n\r\n\\subsection{Variables:\r\n创建，初始化，保存，和恢复}\\label{variables-ux521bux5efaux521dux59cbux5316ux4fddux5b58ux548cux6062ux590d}\r\n\r\nTensorFlow Variables 是内存中的容纳 tensor\r\n的缓存。这一小节介绍了用它们在模型训练时(during\r\ntraining)创建、保存和更新模型参数(model parameters) 的方法。\r\n\r\n\\href{../how_tos/variables.md}{参看教程}\r\n\r\n\\subsection{TensorFlow 机制 101}\\label{tensorflow-ux673aux5236-101}\r\n\r\n用 MNIST 手写数字识别作为一个小例子，一步一步的将使用 TensorFlow\r\n基础架构(infrastructure)训练大规模模型的细节做详细介绍。\r\n\r\n\\href{../tutorials/mnist_tf.md}{参看教程}\r\n\r\n\\subsection{TensorBoard:\r\n学习过程的可视化}\\label{tensorboard-ux5b66ux4e60ux8fc7ux7a0bux7684ux53efux89c6ux5316}\r\n\r\n对模型进行训练和评估时，TensorBoard\r\n是一个很有用的可视化工具。此教程解释了创建和运行 TensorBoard\r\n的方法，和使用摘要操作(Summary ops)的方法，通过添加摘要操作(Summary\r\nops)，可以自动把数据传输到 TensorBoard 所使用的事件文件。\r\n\r\n\\href{../how_tos/summaries_and_tensorboard.md}{参看教程}\r\n\r\n\\subsection{TensorBoard:\r\n图的可视化}\\label{tensorboard-ux56feux7684ux53efux89c6ux5316}\r\n\r\n此教程介绍了在 TensorBoard\r\n中使用可视化工具的方法，它可以帮助你理解张量流图的过程并 debug。\r\n\r\n\\href{../how_tos/graph_viz.md}{参看教程}\r\n\r\n\\subsection{数据读入}\\label{ux6570ux636eux8bfbux5165}\r\n\r\n此教程介绍了把数据传入 TensorSlow 程序的三种主要的方法： Feeding,\r\nReading 和 Preloading.\r\n\r\n\\href{../how_tos/reading_data.md}{参看教程}\r\n\r\n\\subsection{线程和队列}\\label{ux7ebfux7a0bux548cux961fux5217}\r\n\r\n此教程介绍 TensorFlow\r\n中为了更容易进行异步和并发训练的各种不同结构(constructs)。\r\n\r\n\\href{../how_tos/threading_and_queues.md}{参看教程}\r\n\r\n\\subsection{添加新的 Op}\\label{ux6dfbux52a0ux65b0ux7684-op}\r\n\r\nTensorFlow 已经提供一整套节点操作(）operation)，你可以在你的 graph\r\n中随意使用它们，不过这里有关于添加自定义操作(custom op)的细节。\r\n\r\n\\href{../how_tos/adding_an_op.md}{参看教程}。\r\n\r\n\\subsection{自定义数据的\r\nReaders}\\label{ux81eaux5b9aux4e49ux6570ux636eux7684-readers}\r\n\r\n如果你有相当大量的自定义数据集合，可能你想要对 TensorFlow 的 Data\r\nReaders 进行扩展，使它能直接以数据自身的格式将其读入。\r\n\r\n\\href{../how_tos/new_data_formats.md}{参看教程}。\r\n\r\n\\subsection{使用 GPUs}\\label{ux4f7fux7528-gpus}\r\n\r\n此教程描述了用多个 GPU 构建和运行模型的方法。\r\n\r\n\\href{../how_tos/using_gpu.md}{参看教程}\r\n\r\n\\subsection{共享变量 Sharing\r\nVariables}\\label{ux5171ux4eabux53d8ux91cf-sharing-variables}\r\n\r\n当在多 GPU 上部署大型的模型，或展开复杂的 LSTMs 或 RNNs\r\n时，在模型构建代码的不同位置对许多相同的变量（Variable）进行读写常常是必须的。设计变量作用域（Variable\r\nScope）机制的目的就是为了帮助上述任务的实现。\r\n\r\n\\href{../how_tos/variable_scope/index.md}{参看教程}。\r\n\r\n原文： \\href{http://tensorflow.org/how_tos/index.html}{How-to}\r\n\r\n翻译：\\href{https://github.com/TerenceCooper}{Terence Cooper}\r\n\r\n校对：\\href{https://github.com/lonlonago}{lonlonago}"
  },
  {
    "path": "tex_pdf/how_tos/c3s01_variables.tex",
    "content": "%!TEX program = xelatex\r\n% Encoding: UTF8\r\n% SEIKA 2015\r\n\r\n\r\n% Chapter 3 How to ...\r\n% Section 3.1\r\n\r\n\r\n\r\n\\section{变量:创建、初始化、保存和加载}\\label{variables}\r\n\r\n当训练模型时，用变量来存储和更新参数。变量包含张量 (Tensor)存放于内存的缓存区。建模时它们需要被明确地初始化，模型训练后它们必须被存储到磁盘。这些变量的值可在之后模型训练和分析是被加载。\r\n\r\n本文档描述以下两个TensorFlow类。点击以下链接可查看完整的API文档：\r\n\\begin{itemize}\r\n  \\item tf.Variable 类 % add link here\r\n  \\item tf.train.Saver 类 % add link here\r\n\\end{itemize}\r\n\r\n\\subsection {变量创建}\r\n\r\n当创建一个变量时，你将一个张量作为初始值传入构造函数Variable()。TensorFlow提供了一系列操作符来初始化张量，初始值是常量或是随机值。\r\n% add link here\r\n\r\n\\begin{lstlisting}\r\n# Create two variables.\r\nweights = tf.Variable(tf.random_normal([784, 200], stddev=0.35), name=\"weights\")\r\nbiases = tf.Variable(tf.zeros([200]), name=\"biases\")\r\n\\end{lstlisting}\r\n\r\n调用tf.Variable()添加一些操作(Op, operation)到graph：\r\n\\begin{itemize}\r\n  \\item 一个Variable操作存放变量的值。\r\n  \\item 一个初始化op将变量设置为初始值。这事实上是一个tf.assign操作。\r\n  \\item 初始值的操作，例如示例中对biases变量的zeros操作也被加入了graph。\r\n\\end{itemize}\r\n\\lstinline{tf.Variable}的返回值是Python的\\lstinline{tf.Variable}类的一个实例。\r\n\r\n\\subsection {变量初始化}\r\n\r\n变量的初始化必须在模型的其它操作运行之前先明确地完成。最简单的方法就是添加一个给所有变量初始化的操作，并在使用模型之前首先运行那个操作。\r\n\r\n你或者可以从检查点文件中重新获取变量值，详见下文。\r\n\r\n使用\\lstinline{tf.initialize_all_variables()}添加一个操作对变量做初始化。记得在完全构建好模型并加载之后再运行那个操作。\r\n\r\n\\begin{lstlisting}\r\n# Create two variables.\r\nweights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),\r\n                      name=\"weights\")\r\nbiases = tf.Variable(tf.zeros([200]), name=\"biases\")\r\n...\r\n# Add an op to initialize the variables.\r\ninit_op = tf.initialize_all_variables()\r\n\r\n# Later, when launching the model\r\nwith tf.Session() as sess:\r\n  # Run the init operation.\r\n  sess.run(init_op)\r\n  ...\r\n  # Use the model\r\n  ...\r\n\\end{lstlisting}\r\n\r\n\\subsubsection{由另一个变量初始化}\r\n你有时候会需要用另一个变量的初始化值给当前变量初始化。由于\\lstinline{tf.initialize_all_variables()}是并行地初始化所有变量，所以在有这种需求的情况下需要小心。\r\n\r\n用其它变量的值初始化一个新的变量时，使用其它变量的\\lstinline{initialized_value()}属性。你可以直接把已初始化的值作为新变量的初始值，或者把它当做tensor计算得到一个值赋予新变量。\r\n\r\n\\begin{lstlisting}\r\n# Create a variable with a random value.\r\nweights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),name=\"weights\")\r\n# Create another variable with the same value as 'weights'.\r\nw2 = tf.Variable(weights.initialized_value(), name=\"w2\")\r\n# Create another variable with twice the value of 'weights'\r\nw_twice = tf.Variable(weights.initialized_value() * 0.2, name=\"w_twice\")\r\n\\end{lstlisting}\r\n\r\n\\subsubsection{自定义初始化}\r\n\r\n\\lstinline{tf.initialize_all_variables()}函数便捷地添加一个op来初始化模型的所有变量。你也可以给它传入一组变量进行初始化。详情请见Variables Documentation，包括检查变量是否被初始化。\r\n\r\n% add link of Variables Documentaion here\r\n\\subsection {保存和加载}\r\n最简单的保存和恢复模型的方法是使用\\lseinline{tf.train.Saver}对象。构造器给graph的所有变量，或是定义在列表里的变量，添加\\lstinline{save}和\\lstinline{restoreops}。\\lstinline{saver}对象提供了方法来运行这些ops，定义检查点文件的读写路径。\r\n\r\n\\subsubsection{Checkpoint Files}\r\nVariables are saved in binary files that, roughly, contain a map from variable names to tensor values.\r\n\r\nWhen you create a Saver object, you can optionally choose names for the variables in the checkpoint files. By default, it uses the value of the \\lstinline{Variable.name} property for each variable.\r\n% add link of Variable.name here\r\n% https://www.tensorflow.org/versions/master/api_docs/python/state_ops.html#Variable.name\r\n\r\n\\subsubsection{保存变量}\r\n\r\n用\\lstinline{tf.train.Saver()}创建一个\\lstinline{Saver}来管理模型中的所有变量。\r\n\r\n\\begin{lstlisting}\r\n# Create some variables.\r\nv1 = tf.Variable(..., name=\"v1\")\r\nv2 = tf.Variable(..., name=\"v2\")\r\n...\r\n# Add an op to initialize the variables.\r\ninit_op = tf.initialize_all_variables()\r\n\r\n# Add ops to save and restore all the variables.\r\nsaver = tf.train.Saver()\r\n\r\n# Later, launch the model, initialize the variables, do some work, save the\r\n# variables to disk.\r\nwith tf.Session() as sess:\r\n  sess.run(init_op)\r\n  # Do some work with the model.\r\n  ..\r\n  # Save the variables to disk.\r\n  save_path = saver.save(sess, \"/tmp/model.ckpt\")\r\n  print \"Model saved in file: \", save_path\r\n\\end{lstlisting}\r\n\r\n\\subsubsection{恢复变量}\r\n\r\n用同一个\\lstinline{Saver}对象来恢复变量。注意，当你从文件中恢复变量时，不需要事先对它们做初始化。\r\n\r\n\\begin{lstlisting}\r\n# Create some variables.\r\nv1 = tf.Variable(..., name=\"v1\")\r\nv2 = tf.Variable(..., name=\"v2\")\r\n...\r\n# Add ops to save and restore all the variables.\r\nsaver = tf.train.Saver()\r\n\r\n# Later, launch the model, use the saver to restore variables from disk, and\r\n# do some work with the model.\r\nwith tf.Session() as sess:\r\n  # Restore variables from disk.\r\n  saver.restore(sess, \"/tmp/model.ckpt\")\r\n  print \"Model restored.\"\r\n  # Do some work with the model\r\n  ...\r\n\\end{lstlisting}\r\n\r\n\\subsubsection{选择存储和恢复哪些变量}\r\n如果你不给\\lstinline{tf.train.Saver()}传入任何参数，那么\\lstinline{saver}将处理\\lstinline{graph}中的所有变量。其中每一个变量都以变量创建时传入的名称被保存。\r\n\r\n有时候在检查点文件中明确定义变量的名称很有用。举个例子，你也许已经训练得到了一个模型，其中有个变量命名为\\lstinline{\"weights\"}，你想把它的值恢复到一个新的变量\\lstinline{\"params\"}中。\r\n\r\n有时候仅保存和恢复模型的一部分变量很有用。再举个例子，你也许训练得到了一个5层神经网络，现在想训练一个6层的新模型，可以将之前5层模型的参数导入到新模型的前5层中。\r\n\r\n你可以通过给\\lstinline{tf.train.Saver()}构造函数传入Python字典，很容易地定义需要保持的变量及对应名称：键对应使用的名称，值对应被管理的变量。\r\n\r\n\\textbf{注意}：\r\n\r\n\\begin{quote}\r\nYou can create as many saver objects as you want if you need to save and restore different subsets of the model variables. The same variable can be listed in multiple saver objects, its value is only changed when the saver restore() method is run.\r\n\r\nIf you only restore a subset of the model variables at the start of a session, you have to run an initialize op for the other variables. See \\lstinline{tf.initialize_variables()} for more information.\r\n\\end{quote}\r\n\r\n如果需要保存和恢复模型变量的不同子集，可以创建任意多个saver对象。同一个变量可被列入多个saver对象中，只有当saver的\\lstinline{restore()}函数被运行时，它的值才会发生改变。\r\n\r\n如果你仅在session开始时恢复模型变量的一个子集，你需要对剩下的变量执行初始化op。详情请见\\lstinline{tf.initialize_variables()}。\r\n\r\n% add link of tf.initialize_variables() here\r\n% https://www.tensorflow.org/versions/master/api_docs/python/state_ops.html#initialize_variables\r\n\r\n\\begin{lstlisting}\r\n# Create some variables.\r\nv1 = tf.Variable(..., name=\"v1\")\r\nv2 = tf.Variable(..., name=\"v2\")\r\n...\r\n# Add ops to save and restore only 'v2' using the name \"my_v2\"\r\nsaver = tf.train.Saver({\"my_v2\": v2})\r\n# Use the saver object normally after that.\r\n...\r\n\\end{lstlisting}"
  },
  {
    "path": "tex_pdf/how_tos/c3s02_variable_scope.tex",
    "content": "\r\n\r\n\\section{共享变量}\\label{ux5171ux4eabux53d8ux91cf}\r\n\r\n你可以在\\href{tensorflow-zh/SOURCE/how_tos/variables/index.md}{怎么使用变量}中所描述的方式来创建，初始化，保存及加载单一的变量.但是当创建复杂的模块时，通常你需要共享大量变量集并且如果你还想在同一个地方初始化这所有的变量,我们又该怎么做呢.本教程就是演示如何使用\\texttt{tf.variable\\_scope()}\r\n和\\texttt{tf.get\\_variable()}两个方法来实现这一点.\r\n\r\n\\subsection{问题}\\label{ux95eeux9898}\r\n\r\n假设你为图片过滤器创建了一个简单的模块，和我们的\\href{tensorflow-zh/SOURCE/tutorials/deep_cnn/index.md}{卷积神经网络教程}模块相似,但是这里包括两个卷积（为了简化实例这里只有两个）.如果你仅使用\\texttt{tf.Variable}变量,那么你的模块就如\\href{tensorflow-zh/SOURCE/how_tos/variables/index.md}{怎么使用变量}里面所解释的是一样的模块.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\KeywordTok{def} \\NormalTok{my_image_filter(input_images):}\r\n    \\NormalTok{conv1_weights }\\OperatorTok{=} \\NormalTok{tf.Variable(tf.random_normal([}\\DecValTok{5}\\NormalTok{, }\\DecValTok{5}\\NormalTok{, }\\DecValTok{32}\\NormalTok{, }\\DecValTok{32}\\NormalTok{]),}\r\n        \\NormalTok{name}\\OperatorTok{=}\\StringTok{\"conv1_weights\"}\\NormalTok{)}\r\n    \\NormalTok{conv1_biases }\\OperatorTok{=} \\NormalTok{tf.Variable(tf.zeros([}\\DecValTok{32}\\NormalTok{]), name}\\OperatorTok{=}\\StringTok{\"conv1_biases\"}\\NormalTok{)}\r\n    \\NormalTok{conv1 }\\OperatorTok{=} \\NormalTok{tf.nn.conv2d(input_images, conv1_weights,}\r\n        \\NormalTok{strides}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{], padding}\\OperatorTok{=}\\StringTok{'SAME'}\\NormalTok{)}\r\n    \\NormalTok{relu1 }\\OperatorTok{=} \\NormalTok{tf.nn.relu(conv1 }\\OperatorTok{+} \\NormalTok{conv1_biases)}\r\n\r\n    \\NormalTok{conv2_weights }\\OperatorTok{=} \\NormalTok{tf.Variable(tf.random_normal([}\\DecValTok{5}\\NormalTok{, }\\DecValTok{5}\\NormalTok{, }\\DecValTok{32}\\NormalTok{, }\\DecValTok{32}\\NormalTok{]),}\r\n        \\NormalTok{name}\\OperatorTok{=}\\StringTok{\"conv2_weights\"}\\NormalTok{)}\r\n    \\NormalTok{conv2_biases }\\OperatorTok{=} \\NormalTok{tf.Variable(tf.zeros([}\\DecValTok{32}\\NormalTok{]), name}\\OperatorTok{=}\\StringTok{\"conv2_biases\"}\\NormalTok{)}\r\n    \\NormalTok{conv2 }\\OperatorTok{=} \\NormalTok{tf.nn.conv2d(relu1, conv2_weights,}\r\n        \\NormalTok{strides}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{], padding}\\OperatorTok{=}\\StringTok{'SAME'}\\NormalTok{)}\r\n    \\ControlFlowTok{return} \\NormalTok{tf.nn.relu(conv2 }\\OperatorTok{+} \\NormalTok{conv2_biases)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n你很容易想到，模块集很快就比一个模块变得更为复杂，仅在这里我们就有了四个不同的变量：\\texttt{conv1\\_weights},\\texttt{conv1\\_biases},\r\n\\texttt{conv2\\_weights}, 和\\texttt{conv2\\_biases}.\r\n当我们想重用这个模块时问题还在增多.假设你想把你的图片过滤器运用到两张不同的图片，\r\n\\texttt{image1}和\\texttt{image2}.你想通过拥有同一个参数的同一个过滤器来过滤两张图片，你可以调用\\texttt{my\\_image\\_filter()}两次，但是这会产生两组变量.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# First call creates one set of variables.}\r\n\\NormalTok{result1 }\\OperatorTok{=} \\NormalTok{my_image_filter(image1)}\r\n\\CommentTok{# Another set is created in the second call.}\r\n\\NormalTok{result2 }\\OperatorTok{=} \\NormalTok{my_image_filter(image2)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n通常共享变量的方法就是在单独的代码块中来创建他们并且通过使用他们的函数.如使用字典的例子：\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{variables_dict }\\OperatorTok{=} \\NormalTok{\\{}\r\n    \\StringTok{\"conv1_weights\"}\\NormalTok{: tf.Variable(tf.random_normal([}\\DecValTok{5}\\NormalTok{, }\\DecValTok{5}\\NormalTok{, }\\DecValTok{32}\\NormalTok{, }\\DecValTok{32}\\NormalTok{]),}\r\n        \\NormalTok{name}\\OperatorTok{=}\\StringTok{\"conv1_weights\"}\\NormalTok{)}\r\n    \\CommentTok{\"conv1_biases\"}\\NormalTok{: tf.Variable(tf.zeros([}\\DecValTok{32}\\NormalTok{]), name}\\OperatorTok{=}\\StringTok{\"conv1_biases\"}\\NormalTok{)}\r\n    \\NormalTok{... etc. ...}\r\n\\NormalTok{\\}}\r\n\r\n\\KeywordTok{def} \\NormalTok{my_image_filter(input_images, variables_dict):}\r\n    \\NormalTok{conv1 }\\OperatorTok{=} \\NormalTok{tf.nn.conv2d(input_images, variables_dict[}\\StringTok{\"conv1_weights\"}\\NormalTok{],}\r\n        \\NormalTok{strides}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{], padding}\\OperatorTok{=}\\StringTok{'SAME'}\\NormalTok{)}\r\n    \\NormalTok{relu1 }\\OperatorTok{=} \\NormalTok{tf.nn.relu(conv1 }\\OperatorTok{+} \\NormalTok{variables_dict[}\\StringTok{\"conv1_biases\"}\\NormalTok{])}\r\n\r\n    \\NormalTok{conv2 }\\OperatorTok{=} \\NormalTok{tf.nn.conv2d(relu1, variables_dict[}\\StringTok{\"conv2_weights\"}\\NormalTok{],}\r\n        \\NormalTok{strides}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{], padding}\\OperatorTok{=}\\StringTok{'SAME'}\\NormalTok{)}\r\n    \\ControlFlowTok{return} \\NormalTok{tf.nn.relu(conv2 }\\OperatorTok{+} \\NormalTok{variables_dict[}\\StringTok{\"conv2_biases\"}\\NormalTok{])}\r\n\r\n\\CommentTok{# The 2 calls to my_image_filter() now use the same variables}\r\n\\NormalTok{result1 }\\OperatorTok{=} \\NormalTok{my_image_filter(image1, variables_dict)}\r\n\\NormalTok{result2 }\\OperatorTok{=} \\NormalTok{my_image_filter(image2, variables_dict)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n虽然使用上面的方式创建变量是很方便的，但是在这个模块代码之外却破坏了其封装性：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  在构建试图的代码中标明变量的名字，类型，形状来创建.\r\n\\item\r\n  当代码改变了，调用的地方也许就会产生或多或少或不同类型的变量.\r\n\\end{itemize}\r\n\r\n解决此类问题的方法之一就是使用类来创建模块，在需要的地方使用类来小心地管理他们需要的变量.\r\n一个更高明的做法,不用调用类，而是利用TensorFlow 提供了\\emph{变量作用域}\r\n机制，当构建一个视图时,很容易就可以共享命名过的变量.\r\n\r\n\\subsection{变量作用域实例}\\label{ux53d8ux91cfux4f5cux7528ux57dfux5b9eux4f8b}\r\n\r\n变量作用域机制在TensorFlow中主要由两部分组成：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{tf.get\\_variable(\\textless{}name\\textgreater{},\\ \\textless{}shape\\textgreater{},\\ \\textless{}initializer\\textgreater{})}:\r\n  通过所给的名字创建或是返回一个变量.\r\n\\item\r\n  \\texttt{tf.variable\\_scope(\\textless{}scope\\_name\\textgreater{})}:\r\n  通过 \\texttt{tf.get\\_variable()}为变量名指定命名空间.\r\n\\end{itemize}\r\n\r\n方法 \\texttt{tf.get\\_variable()}\r\n用来获取或创建一个变量，而不是直接调用\\texttt{tf.Variable}.它采用的不是像`tf.Variable这样直接获取值来初始化的方法.一个初始化就是一个方法，创建其形状并且为这个形状提供一个张量.这里有一些在TensorFlow中使用的初始化变量：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{tf.constant\\_initializer(value)} 初始化一切所提供的值,\r\n\\item\r\n  \\texttt{tf.random\\_uniform\\_initializer(a,\\ b)}从a到b均匀初始化,\r\n\\item\r\n  \\texttt{tf.random\\_normal\\_initializer(mean,\\ stddev)}\r\n  用所给平均值和标准差初始化均匀分布.\r\n\\end{itemize}\r\n\r\n为了了解\\texttt{tf.get\\_variable()}怎么解决前面所讨论的问题,让我们在单独的方法里面创建一个卷积来重构一下代码，命名为\\texttt{conv\\_relu}：\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\KeywordTok{def} \\NormalTok{conv_relu(}\\BuiltInTok{input}\\NormalTok{, kernel_shape, bias_shape):}\r\n    \\CommentTok{# Create variable named \"weights\".}\r\n    \\NormalTok{weights }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"weights\"}\\NormalTok{, kernel_shape,}\r\n        \\NormalTok{initializer}\\OperatorTok{=}\\NormalTok{tf.random_normal_initializer())}\r\n    \\CommentTok{# Create variable named \"biases\".}\r\n    \\NormalTok{biases }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"biases\"}\\NormalTok{, bias_shape,}\r\n        \\NormalTok{initializer}\\OperatorTok{=}\\NormalTok{tf.constant_intializer(}\\FloatTok{0.0}\\NormalTok{))}\r\n    \\NormalTok{conv }\\OperatorTok{=} \\NormalTok{tf.nn.conv2d(}\\BuiltInTok{input}\\NormalTok{, weights,}\r\n        \\NormalTok{strides}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{], padding}\\OperatorTok{=}\\StringTok{'SAME'}\\NormalTok{)}\r\n    \\ControlFlowTok{return} \\NormalTok{tf.nn.relu(conv }\\OperatorTok{+} \\NormalTok{biases)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n这个方法中用了\\texttt{\"weights\"}\r\n和\\texttt{\"biases\"}两个简称.而我们更偏向于用\\texttt{conv1} 和\r\n\\texttt{conv2}这两个变量的写法,但是不同的变量需要不同的名字.这就是\\texttt{tf.variable\\_scope()}\r\n变量起作用的地方.他为变量指定了相应的命名空间.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\KeywordTok{def} \\NormalTok{my_image_filter(input_images):}\r\n    \\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"conv1\"}\\NormalTok{):}\r\n        \\CommentTok{# Variables created here will be named \"conv1/weights\", \"conv1/biases\".}\r\n        \\NormalTok{relu1 }\\OperatorTok{=} \\NormalTok{conv_relu(input_images, [}\\DecValTok{5}\\NormalTok{, }\\DecValTok{5}\\NormalTok{, }\\DecValTok{32}\\NormalTok{, }\\DecValTok{32}\\NormalTok{], [}\\DecValTok{32}\\NormalTok{])}\r\n    \\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"conv2\"}\\NormalTok{):}\r\n        \\CommentTok{# Variables created here will be named \"conv2/weights\", \"conv2/biases\".}\r\n        \\ControlFlowTok{return} \\NormalTok{conv_relu(relu1, [}\\DecValTok{5}\\NormalTok{, }\\DecValTok{5}\\NormalTok{, }\\DecValTok{32}\\NormalTok{, }\\DecValTok{32}\\NormalTok{], [}\\DecValTok{32}\\NormalTok{])}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n现在，让我们看看当我们调用 \\texttt{my\\_image\\_filter()}\r\n两次时究竟会发生了什么.\r\n\r\n\\begin{verbatim}\r\nresult1 = my_image_filter(image1)\r\nresult2 = my_image_filter(image2)\r\n# Raises ValueError(... conv1/weights already exists ...)\r\n\\end{verbatim}\r\n\r\n就像你看见的一样，\\texttt{tf.get\\_variable()}会检测已经存在的变量是否已经共享.如果你想共享他们，你需要像下面使用的一样，通过\\texttt{reuse\\_variables()}这个方法来指定.\r\n\r\n\\begin{verbatim}\r\nwith tf.variable_scope(\"image_filters\") as scope:\r\n    result1 = my_image_filter(image1)\r\n    scope.reuse_variables()\r\n    result2 = my_image_filter(image2)\r\n\\end{verbatim}\r\n\r\n用这种方式来共享变量是非常好的，轻量级而且安全.\r\n\r\n\\subsection{变量作用域是怎么工作的？}\\label{ux53d8ux91cfux4f5cux7528ux57dfux662fux600eux4e48ux5de5ux4f5cux7684}\r\n\r\n\\subsubsection{\\texorpdfstring{理解 \\texttt{tf.get\\_variable()}\r\n}{理解 tf.get\\_variable() }}\\label{ux7406ux89e3-tf.getux5fvariable}\r\n\r\n为了理解变量作用域，首先完全理解\\texttt{tf.get\\_variable()}是怎么工作的是很有必要的.\r\n通常我们就是这样调用\\texttt{tf.get\\_variable} 的.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{v }\\OperatorTok{=} \\NormalTok{tf.get_variable(name, shape, dtype, initializer)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n此调用做了有关作用域的两件事中的其中之一，方法调入.总的有两种情况.\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  情况1:当\\texttt{tf.get\\_variable\\_scope().reuse\\ ==\\ False}时，作用域就是为创建新变量所设置的.\r\n\\end{itemize}\r\n\r\n这种情况下，\\texttt{v}将通过\\texttt{tf.Variable}所提供的形状和数据类型来重新创建.创建变量的全称将会由当前变量作用域名+所提供的\\texttt{名字}所组成,并且还会检查来确保没有任何变量使用这个全称.如果这个全称已经有一个变量使用了，那么方法将会抛出\\texttt{ValueError}错误.如果一个变量被创建,他将会用\\texttt{initializer(shape)}进行初始化.比如：\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{):}\r\n    \\NormalTok{v }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n\\ControlFlowTok{assert} \\NormalTok{v.name }\\OperatorTok{==} \\StringTok{\"foo/v:0\"}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  情况1：当\\texttt{tf.get\\_variable\\_scope().reuse\\ ==\\ True}时，作用域是为重用变量所设置\r\n\\end{itemize}\r\n\r\n这种情况下，调用就会搜索一个已经存在的变量，他的全称和当前变量的作用域名+所提供的\\texttt{名字}是否相等.如果不存在相应的变量，就会抛出\\texttt{ValueError}\r\n错误.如果变量找到了，就返回这个变量.如下：\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{):}\r\n    \\NormalTok{v }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{, reuse}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{):}\r\n    \\NormalTok{v1 }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n\\ControlFlowTok{assert} \\NormalTok{v1 }\\OperatorTok{==} \\NormalTok{v}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsubsection{\\texorpdfstring{\\texttt{tf.variable\\_scope()}\r\n基础}{tf.variable\\_scope() 基础}}\\label{tf.variableux5fscope-ux57faux7840}\r\n\r\n知道\\texttt{tf.get\\_variable()}是怎么工作的,使得理解变量作用域变得很容易.变量作用域的主方法带有一个名称，它将会作为前缀用于变量名,并且带有一个重用标签来区分以上的两种情况.嵌套的作用域附加名字所用的规则和文件目录的规则很类似：\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{):}\r\n    \\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"bar\"}\\NormalTok{):}\r\n        \\NormalTok{v }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n\\ControlFlowTok{assert} \\NormalTok{v.name }\\OperatorTok{==} \\StringTok{\"foo/bar/v:0\"}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n当前变量作用域可以用\\texttt{tf.get\\_variable\\_scope()}进行检索并且\\texttt{reuse}\r\n标签可以通过调用\\texttt{tf.get\\_variable\\_scope().reuse\\_variables()}设置为\\texttt{True}\r\n.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{):}\r\n    \\NormalTok{v }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n    \\NormalTok{tf.get_variable_scope().reuse_variables()}\r\n    \\NormalTok{v1 }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n\\ControlFlowTok{assert} \\NormalTok{v1 }\\OperatorTok{==} \\NormalTok{v}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n注意你\\emph{不能}设置\\texttt{reuse}标签为\\texttt{False}.其中的原因就是允许改写创建模块的方法.想一下你前面写得方法\\texttt{my\\_image\\_filter(inputs)}.有人在变量作用域内调用\\texttt{reuse=True}\r\n是希望所有内部变量都被重用.如果允许在方法体内强制执行\\texttt{reuse=False}，将会打破内部结构并且用这种方法使得很难再共享参数.\r\n\r\n即使你不能直接设置 \\texttt{reuse} 为 \\texttt{False}\r\n,但是你可以输入一个重用变量作用域,然后就释放掉,就成为非重用的变量.当打开一个变量作用域时,使用\\texttt{reuse=True}\r\n作为参数是可以的.但也要注意，同一个原因，\\texttt{reuse}\r\n参数是不可继承.所以当你打开一个重用变量作用域，那么所有的子作用域也将会被重用.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"root\"}\\NormalTok{):}\r\n    \\CommentTok{# At start, the scope is not reusing.}\r\n    \\ControlFlowTok{assert} \\NormalTok{tf.get_variable_scope().reuse }\\OperatorTok{==} \\VariableTok{False}\r\n    \\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{):}\r\n        \\CommentTok{# Opened a sub-scope, still not reusing.}\r\n        \\ControlFlowTok{assert} \\NormalTok{tf.get_variable_scope().reuse }\\OperatorTok{==} \\VariableTok{False}\r\n    \\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{, reuse}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{):}\r\n        \\CommentTok{# Explicitly opened a reusing scope.}\r\n        \\ControlFlowTok{assert} \\NormalTok{tf.get_variable_scope().reuse }\\OperatorTok{==} \\VariableTok{True}\r\n        \\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"bar\"}\\NormalTok{):}\r\n            \\CommentTok{# Now sub-scope inherits the reuse flag.}\r\n            \\ControlFlowTok{assert} \\NormalTok{tf.get_variable_scope().reuse }\\OperatorTok{==} \\VariableTok{True}\r\n    \\CommentTok{# Exited the reusing scope, back to a non-reusing one.}\r\n    \\ControlFlowTok{assert} \\NormalTok{tf.get_variable_scope().reuse }\\OperatorTok{==} \\VariableTok{False}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsubsection{获取变量作用域}\\label{ux83b7ux53d6ux53d8ux91cfux4f5cux7528ux57df}\r\n\r\n在上面的所有例子中，我们共享参数只因为他们的名字是一致的，那是因为我们开启一个变量作用域重用时刚好用了同一个字符串.在更复杂的情况，他可以通过变量作用域对象来使用，而不是通过依赖于右边的名字来使用.为此,变量作用域可以被获取并使用，而不是仅作为当开启一个新的变量作用域的名字.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{) }\\ImportTok{as} \\NormalTok{foo_scope:}\r\n    \\NormalTok{v }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(foo_scope)}\r\n    \\NormalTok{w }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"w\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(foo_scope, reuse}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{)}\r\n    \\NormalTok{v1 }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n    \\NormalTok{w1 }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"w\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n\\ControlFlowTok{assert} \\NormalTok{v1 }\\OperatorTok{==} \\NormalTok{v}\r\n\\ControlFlowTok{assert} \\NormalTok{w1 }\\OperatorTok{==} \\NormalTok{w}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n当开启一个变量作用域，使用一个预先已经存在的作用域时，我们会跳过当前变量作用域的前缀而直接成为一个完全不同的作用域.这就是我们做得完全独立的地方.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{) }\\ImportTok{as} \\NormalTok{foo_scope:}\r\n    \\ControlFlowTok{assert} \\NormalTok{foo_scope.name }\\OperatorTok{==} \\StringTok{\"foo\"}\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"bar\"}\\NormalTok{)}\r\n    \\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"baz\"}\\NormalTok{) }\\ImportTok{as} \\NormalTok{other_scope:}\r\n        \\ControlFlowTok{assert} \\NormalTok{other_scope.name }\\OperatorTok{==} \\StringTok{\"bar/baz\"}\r\n        \\ControlFlowTok{with} \\NormalTok{tf.variable_scope(foo_scope) }\\ImportTok{as} \\NormalTok{foo_scope2:}\r\n            \\ControlFlowTok{assert} \\NormalTok{foo_scope2.name }\\OperatorTok{==} \\StringTok{\"foo\"}  \\CommentTok{# Not changed.}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsubsection{变量作用域中的初始化器}\\label{ux53d8ux91cfux4f5cux7528ux57dfux4e2dux7684ux521dux59cbux5316ux5668}\r\n\r\n使用\\texttt{tf.get\\_variable()}允许你重写方法来创建或者重用变量,并且可以被外部透明调用.但是如果我们想改变创建变量的初始化器那要怎么做呢？是否我们需要为所有的创建变量方法传递一个额外的参数呢？那在大多数情况下，当我们想在一个地方并且为所有的方法的所有的变量设置一个默认初始化器，那又改怎么做呢？为了解决这些问题，变量作用域可以携带一个默认的初始化器.他可以被子作用域继承并传递给\\texttt{tf.get\\_variable()}\r\n调用.但是如果其他初始化器被明确地指定,那么他将会被重写.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{, initializer}\\OperatorTok{=}\\NormalTok{tf.constant_initializer(}\\FloatTok{0.4}\\NormalTok{)):}\r\n    \\NormalTok{v }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n    \\ControlFlowTok{assert} \\NormalTok{v.}\\BuiltInTok{eval}\\NormalTok{() }\\OperatorTok{==} \\FloatTok{0.4}  \\CommentTok{# Default initializer as set above.}\r\n    \\NormalTok{w }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"w\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{], initializer}\\OperatorTok{=}\\NormalTok{tf.constant_initializer(}\\FloatTok{0.3}\\NormalTok{)):}\r\n    \\ControlFlowTok{assert} \\NormalTok{w.}\\BuiltInTok{eval}\\NormalTok{() }\\OperatorTok{==} \\FloatTok{0.3}  \\CommentTok{# Specific initializer overrides the default.}\r\n    \\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"bar\"}\\NormalTok{):}\r\n        \\NormalTok{v }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n        \\ControlFlowTok{assert} \\NormalTok{v.}\\BuiltInTok{eval}\\NormalTok{() }\\OperatorTok{==} \\FloatTok{0.4}  \\CommentTok{# Inherited default initializer.}\r\n    \\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"baz\"}\\NormalTok{, initializer}\\OperatorTok{=}\\NormalTok{tf.constant_initializer(}\\FloatTok{0.2}\\NormalTok{)):}\r\n        \\NormalTok{v }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n        \\ControlFlowTok{assert} \\NormalTok{v.}\\BuiltInTok{eval}\\NormalTok{() }\\OperatorTok{==} \\FloatTok{0.2}  \\CommentTok{# Changed default initializer.}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsubsection{\\texorpdfstring{在\\texttt{tf.variable\\_scope()}中ops的名称}{在tf.variable\\_scope()中ops的名称}}\\label{ux5728tf.variableux5fscopeux4e2dopsux7684ux540dux79f0}\r\n\r\n我们讨论 \\texttt{tf.variable\\_scope}\r\n怎么处理变量的名字.但是又是如何在作用域中影响到\r\n其他ops的名字的呢？ops在一个变量作用域的内部创建，那么他应该是共享他的名字，这是很自然的想法.出于这样的原因，当我们用\\texttt{with\\ tf.variable\\_scope(\"name\")}时，这就间接地开启了一个\\texttt{tf.name\\_scope(\"name\")}.比如：\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{):}\r\n    \\NormalTok{x }\\OperatorTok{=} \\FloatTok{1.0} \\OperatorTok{+} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n\\ControlFlowTok{assert} \\NormalTok{x.op.name }\\OperatorTok{==} \\StringTok{\"foo/add\"}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n名称作用域可以被开启并添加到一个变量作用域中,然后他们只会影响到ops的名称,而不会影响到变量.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.variable_scope(}\\StringTok{\"foo\"}\\NormalTok{):}\r\n    \\ControlFlowTok{with} \\NormalTok{tf.name_scope(}\\StringTok{\"bar\"}\\NormalTok{):}\r\n        \\NormalTok{v }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"v\"}\\NormalTok{, [}\\DecValTok{1}\\NormalTok{])}\r\n        \\NormalTok{x }\\OperatorTok{=} \\FloatTok{1.0} \\OperatorTok{+} \\NormalTok{v}\r\n\\ControlFlowTok{assert} \\NormalTok{v.name }\\OperatorTok{==} \\StringTok{\"foo/v:0\"}\r\n\\ControlFlowTok{assert} \\NormalTok{x.op.name }\\OperatorTok{==} \\StringTok{\"foo/bar/add\"}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n当用一个引用对象而不是一个字符串去开启一个变量作用域时，我们就不会为ops改变当前的名称作用域.\r\n\r\n\\subsection{使用实例 }\\label{ux4f7fux7528ux5b9eux4f8b}\r\n\r\n这里有一些指向怎么使用变量作用域的文件.特别是，他被大量用于\r\n\\href{https://zh.wikipedia.org/wiki/\\%E9\\%80\\%92\\%E5\\%BD\\%92\\%E7\\%A5\\%9E\\%E7\\%BB\\%8F\\%E7\\%BD\\%91\\%E7\\%BB\\%9C}{时间递归神经网络}和\\texttt{sequence-to-sequence}模型,\r\n\r\nFile \\textbar{} What's in it? --- \\textbar{} ---\r\n\\texttt{models/image/cifar10.py} \\textbar{}图像中检测对象的模型.\r\n\\texttt{models/rnn/rnn\\_cell.py} \\textbar{}时间递归神经网络的元方法集.\r\n\\texttt{models/rnn/seq2seq.py}\r\n\\textbar{}为创建\\texttt{sequence-to-sequence}模型的方法集.\r\n原文：\\href{http://www.tensorflow.org/how_tos/variable_scope/index.md}{Sharing\r\nVariables}\r\n翻译：\\href{https://github.com/nb312}{nb312}校对：\\href{https://github.com/jikexueyuanwiki}{Wiki}"
  },
  {
    "path": "tex_pdf/how_tos/c3s03_viz_learning.tex",
    "content": "\r\n\r\n\\section{TensorBoard:可视化学习　}\\label{vis_learning}\r\n\r\nTensorBoard\r\n涉及到的运算，通常是在训练庞大的深度神经网络中出现的复杂而又难以理解的运算。\r\n\r\n为了更方便 TensorFlow 程序的理解、调试与优化，我们发布了一套叫做\r\nTensorBoard 的可视化工具。你可以用 TensorBoard 来展现你的 TensorFlow\r\n图像，绘制图像生成的定量指标图以及附加数据。\r\n\r\n当 TensorBoard 设置完成后，它应该是这样子的：\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics[width=.95\\textwidth]{../SOURCE/images/mnist_tensorboard.png}\r\n\\caption{MNIST TensorBoard}\r\n\\end{figure}\r\n\r\n\\subsection{数据序列化　}\\label{ux6570ux636eux5e8fux5217ux5316}\r\n\r\nTensorBoard 通过读取 TensorFlow 的事件文件来运行。TensorFlow\r\n的事件文件包括了你会在 TensorFlow 运行中涉及到的主要数据。下面是\r\nTensorBoard 中汇总数据（Summary data）的大体生命周期。\r\n\r\n首先，创建你想汇总数据的 TensorFlow\r\n图，然后再选择你想在哪个节点进行\\href{../api_docs/python/train.md\\#summary_options}{汇总(summary)操作}。\r\n\r\n比如，假设你正在训练一个卷积神经网络，用于识别 MNISt\r\n标签。你可能希望记录学习速度(learning\r\nrate)的如何变化，以及目标函数如何变化。通过向节点附加\\href{../api_docs/python/train.md\\#scalary_summary}{scalar\\_summary}操作来分别输出学习速度和期望误差。然后你可以给每个\r\nscalary\\_summary 分配一个有意义的 \\texttt{标签}，比如\r\n\\texttt{\\textquotesingle{}learning\\ rate\\textquotesingle{}} 和\r\n\\texttt{\\textquotesingle{}loss\\ function\\textquotesingle{}}。\r\n\r\n或者你还希望显示一个特殊层中激活的分布，或者梯度权重的分布。可以通过分别附加\r\n\\href{../api_docs/python/train.md\\#histogram_summary}{histogram\\_summary}\r\n运算来收集权重变量和梯度输出。\r\n\r\n所有可用的 summary\r\n操作详细信息，可以查看\\href{../api_docs/python/train.md\\#summary_operation}{summary\\_operation}文档。\r\n\r\n在TensorFlow中，所有的操作只有当你执行，或者另一个操作依赖于它的输出时才会运行。我们刚才创建的这些节点（summary\r\nnodes）都围绕着你的图像：没有任何操作依赖于它们的结果。因此，为了生成汇总信息，我们需要运行所有这些节点。这样的手动工作是很乏味的，因此可以使用\\href{../api_docs/python/train.md\\#scalary_summary}{tf.merge\\_all\\_summaries}来将他们合并为一个操作。\r\n\r\n然后你可以执行合并命令，它会依据特点步骤将所有数据生成一个序列化的\\texttt{Summary}\r\nprotobuf对象。最后，为了将汇总数据写入磁盘，需要将汇总的protobuf对象传递给\\href{../api_docs/python/train.md\\#SummaryWriter}{tf.train.Summarywriter}。\r\n\r\n\\texttt{SummaryWriter} 的构造函数中包含了参数 logdir。这个 logdir\r\n非常重要，所有事件都会写到它所指的目录下。此外，\\texttt{SummaryWriter}\r\n中还包含了一个可选择的参数 \\texttt{GraphDef}。如果输入了该参数，那么\r\nTensorBoard 也会显示你的图像。\r\n\r\n现在已经修改了你的图，也有了\r\n\\texttt{SummaryWriter}，现在就可以运行你的神经网络了！如果你愿意的话，你可以每一步执行一次合并汇总，这样你会得到一大堆训练数据。这很有可能超过了你想要的数据量。你也可以每一百步执行一次合并汇总，或者如下面代码里示范的这样。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{merged_summary_op }\\OperatorTok{=} \\NormalTok{tf.merge_all_summaries()}\r\n\\NormalTok{summary_writer }\\OperatorTok{=} \\NormalTok{tf.train.SummaryWriter(}\\StringTok{'/tmp/mnist_logs'}\\NormalTok{, sess.graph)}\r\n\\NormalTok{total_step }\\OperatorTok{=} \\DecValTok{0}\r\n\\ControlFlowTok{while} \\NormalTok{training:}\r\n  \\NormalTok{total_step }\\OperatorTok{+=} \\DecValTok{1}\r\n  \\NormalTok{session.run(training_op)}\r\n  \\ControlFlowTok{if} \\NormalTok{total_step }\\OperatorTok{%} \\DecValTok{100} \\OperatorTok{==} \\DecValTok{0}\\NormalTok{:}\r\n    \\NormalTok{summary_str }\\OperatorTok{=} \\NormalTok{session.run(merged_summary_op)}\r\n    \\NormalTok{summary_writer.add_summary(summary_str, total_step)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n现在已经准备好用 TensorBoard 来可视化这些数据了。\r\n\r\n\\subsection{启动TensorBoard　}\\label{ux542fux52a8tensorboard}\r\n\r\n输入下面的指令来启动TensorBoard\r\n\r\n\\begin{verbatim}\r\npython tensorflow/tensorboard/tensorboard.py --logdir=path/to/log-directory\r\n\\end{verbatim}\r\n\r\n这里的参数 \\texttt{logdir} 指向 \\texttt{SummaryWriter}\r\n序列化数据的存储路径。如果\\texttt{logdir}目录的子目录中包含另一次运行时的数据，那么\r\nTensorBoard 会展示所有运行的数据。一旦 TensorBoard\r\n开始运行，你可以通过在浏览器中输入 \\texttt{localhost:6006} 来查看\r\nTensorBoard。\r\n\r\n如果你已经通过pip安装了 TensorBoard，你可以通过执行更为简单地命令来访问\r\nTensorBoard\r\n\r\n\\begin{verbatim}\r\ntensorboard --logdir=/path/to/log-directory\r\n\\end{verbatim}\r\n\r\n进入 TensorBoard\r\n的界面时，你会在右上角看到导航选项卡，每一个选项卡将展现一组可视化的序列化数据集\r\n。对于你查看的每一个选项卡，如果 TensorBoard\r\n中没有数据与这个选项卡相关的话，则会显示一条提示信息指示你如何序列化相关数据。\r\n\r\n更多更详细的关于如何使用 graph 选项来显示你的图像的信息。参见\r\n\\href{./graph_viz.md}{TensorBoard:图表可视化}\r\n\r\n原文地址：\\href{http://tensorflow.org/how_tos/summaries_and_tensorboard/index.html\\#tensorboard-visualizing-learning}{TensorBoard:Visualizing\r\nLearning} 翻译：\\href{https://github.com/thylaco1eo}{thylaco1eo}\r\n校对：\\href{https://github.com/lucky521}{lucky521}\r\n\r\n"
  },
  {
    "path": "tex_pdf/how_tos/c3s04_graph_viz.tex",
    "content": "\r\n\r\n\\section{TensorBoard: 图表可视化\r\n}\\label{tensorboard-ux56feux8868ux53efux89c6ux5316}\r\n\r\nTensorFlow\r\n图表计算强大而又复杂，图表可视化在理解和调试时显得非常有帮助。\r\n下面是一个运作时的可式化例子。\r\n\r\n\\includegraphics{../images/graph_vis_animation.gif}\r\n``一个TensorFlow图表的可视化'') \\emph{一个TensorFlow图表的可视化。}\r\n\r\n为了显示自己的图表，需将 TensorBoard\r\n指向此工作的日志目录并运行，点击图表顶部窗格的标签页，然后在左上角的菜单中选择合适的运行。想要深入学习关于如何运行\r\nTensorBoard 以及如何保证所有必要信息被记录下来，请查看\r\n\\href{tensorflow-zh/SOURCE/how_tos/summaries_and_tensorboard/index.md}{Summaries\r\n和 TensorBoard}.\r\n\r\n\\subsection{名称域（Name scoping）和节点（Node）\r\n}\\label{ux540dux79f0ux57dfname-scopingux548cux8282ux70b9node}\r\n\r\n典型的 TensorFlow\r\n可以有数以千计的节点，如此多而难以一下全部看到，甚至无法使用标准图表工具来展示。为简单起见，我们为变量名划定范围，并且可视化把该信息用于在图表中的节点上定义一个层级。默认情况下，\r\n只有顶层节点会显示。下面这个例子使用\\href{tensorflow-zh/SOURCE/api_docs/python/framework.md\\#name_scope}{\\texttt{tf.name\\_scope}}在\\texttt{hidden}命名域下定义了三个操作:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ImportTok{import} \\NormalTok{tensorflow }\\ImportTok{as} \\NormalTok{tf}\r\n\r\n\\ControlFlowTok{with} \\NormalTok{tf.name_scope(}\\StringTok{'hidden'}\\NormalTok{) }\\ImportTok{as} \\NormalTok{scope:}\r\n  \\NormalTok{a }\\OperatorTok{=} \\NormalTok{tf.constant(}\\DecValTok{5}\\NormalTok{, name}\\OperatorTok{=}\\StringTok{'alpha'}\\NormalTok{)}\r\n  \\NormalTok{W }\\OperatorTok{=} \\NormalTok{tf.Variable(tf.random_uniform([}\\DecValTok{1}\\NormalTok{, }\\DecValTok{2}\\NormalTok{], }\\OperatorTok{-}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{1.0}\\NormalTok{), name}\\OperatorTok{=}\\StringTok{'weights'}\\NormalTok{)}\r\n  \\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.Variable(tf.zeros([}\\DecValTok{1}\\NormalTok{]), name}\\OperatorTok{=}\\StringTok{'biases'}\\NormalTok{)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n结果是得到了下面三个操作名:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\emph{hidden}/alpha\r\n\\item\r\n  \\emph{hidden}/weights\r\n\\item\r\n  \\emph{hidden}/biases\r\n\\end{itemize}\r\n\r\n默认地，三个操作名会折叠为一个节点并标注为\\texttt{hidden}。其额外细节并没有丢失，你可以双击，或点击右上方橙色的\\texttt{+}来展开节点，然后就会看到三个子节点\\texttt{alpha}，\\texttt{weights}和\\texttt{biases}了。\r\n\r\n这有一个生动的例子，例中有一个更复杂的节点，节点处于其初始和展开状态。\r\n\r\n\\begin{verbatim}\r\n<td style=\"width: 50%;\">\r\n  <img src=\"../images/pool1_collapsed.png\" alt=\"未展开的名称域\" title=\"未展开的名称域\" />\r\n</td>\r\n<td style=\"width: 50%;\">\r\n  <img src=\"../images/pool1_expanded.png\" alt=\"展开的名称域\" title=\"展开的名称域\" />\r\n</td>\r\n\\end{verbatim}\r\n\r\n\\begin{verbatim}\r\n<td style=\"width: 50%;\">\r\n  顶级名称域的初始视图<code>pool_1</code>，点击右上方橙色的<code>+</code>按钮或双击节点来展开。\r\n</td>\r\n<td style=\"width: 50%;\">\r\n  展开的<code>pool_1</code>名称域视图，点击右上方橙色的<code>-</code>按钮或双击节点来收起此名称域。\r\n</td>\r\n\\end{verbatim}\r\n\r\n通过名称域把节点分组来得到可读性高的图表很关键的。如果你在构建一个模型，名称域就可以用来控制可视化结果。\\textbf{你的名称域越好，可视性就越好。}\r\n\r\n上面的图像例子说明了可视化的另一方面， TensorFlow\r\n图表有两种连接关系：数据依赖和控制依赖。数据依赖显示两个操作之间的tensor流程，用实心箭头指示，而控制依赖用点线表示。在已展开的视图(上面的右图)中，除了用点线连接的\\texttt{CheckNumerics}和\\texttt{control\\_dependency}之外，所有连接都是数据依赖的。\r\n\r\n还有一种手段用来简化布局。大多数 TensorFlow\r\n图表有一部分节点，这部分节点和其他节点之间有很多连接。比如，许多节点在初始化阶段可能会有一个控制依赖，而绘制所有\\texttt{init}节点的边缘和其依赖可能会创造出一个混乱的视图。\r\n\r\n为了减少混乱，可视化把所有 high-degree\r\n节点分离到右边的一个\\emph{从属}区域，\r\n而不会绘制线条来表示他们的边缘。线条也不用来表示连接了，我们绘制了小\\emph{节点图标}来指示这些连接关系。分离出从属节点通常不会把关键信息删除掉，因为这些节点和内构功能是相关的。\r\n\r\n\\begin{verbatim}\r\n<td style=\"width: 50%;\">\r\n  <img src=\"../images/conv_1.png\" alt=\"conv_1是主图表的部分\" title=\"conv_1是主图表的部分\" />\r\n</td>\r\n<td style=\"width: 50%;\">\r\n  <img src=\"../images/save.png\" alt=\"save被抽出为从属节点\" title=\"save被抽出为从属节点\" />\r\n</td>\r\n\\end{verbatim}\r\n\r\n\\begin{verbatim}\r\n<td style=\"width: 50%;\">\r\n  节点<code>conv_1</code>被连接到<code>save</code>，注意其右边<code>save</code>节点图标。\r\n</td>\r\n<td style=\"width: 50%;\">\r\n  <code>save</code> has a high degree, 并会作为从属节点出现，与<code>conv_1</code>的连接作为一个节点图标显示在其左边。为了继续减少杂乱，既然<code>save</code>有很多连接，我们则只显示前5个，而把其余的缩略为<code>... 12 more</code>。\r\n</td>\r\n\\end{verbatim}\r\n\r\n最后一个结构上的简化法叫做\\emph{序列折叠（series collapsing）}。\r\n序列基序（Sequential\r\nmotifs）是拥有相同结构并且其名称结尾的数字不同的节点，它们被折叠进一个单独的节点块（stack）中。对长序列网络来说，序列折叠极大地简化了视图，对于已层叠的节点，双击会展开序列。\r\n\r\n\\begin{verbatim}\r\n<td style=\"width: 50%;\">\r\n  <img src=\"../images/series.png\" alt=\"节点序列\" title=\"节点序列\" />\r\n</td>\r\n<td style=\"width: 50%;\">\r\n  <img src=\"../images/series_expanded.png\" alt=\"展开的节点序列\" title=\"展开的节点序列\" />\r\n</td>\r\n\\end{verbatim}\r\n\r\n\\begin{verbatim}\r\n<td style=\"width: 50%;\">\r\n  一个节点序列的折叠视图。\r\n</td>\r\n<td style=\"width: 50%;\">\r\n  视图的一小块, 双击后展开。\r\n</td>\r\n\\end{verbatim}\r\n\r\n最后，针对易读性的最后一点要说到的是，可视化为常节点和摘要节点使用了特别的图标，总结起来有下面这些节点符号：\r\n\r\n\\begin{longtable}[c]{@{}ll@{}}\r\n\\toprule\r\n\\begin{minipage}[b]{0.05\\columnwidth}\\raggedright\\strut\r\n符号\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[b]{0.05\\columnwidth}\\raggedright\\strut\r\n意义\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\midrule\r\n\\endhead\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\includegraphics{../images/namespace_node.png}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\emph{High-level}节点代表一个名称域，双击则展开一个高层节点。\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\includegraphics{../images/horizontal_stack.png}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n彼此之间不连接的有限个节点序列。\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\includegraphics{../images/vertical_stack.png}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n彼此之间相连的有限个节点序列。\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\includegraphics{../images/op_node.png}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n一个单独的操作节点。\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\includegraphics{../images/constant.png}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n一个常量结点。\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\includegraphics{../images/summary.png}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n一个摘要节点。\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\includegraphics{../images/dataflow_edge.png}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n显示各操作间的数据流边。\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\includegraphics{../images/control_edge.png}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n显示各操作间的控制依赖边。\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\includegraphics{../images/reference_edge.png}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n引用边，表示出度操作节点可以使入度tensor发生变化。\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\bottomrule\r\n\\end{longtable}\r\n\r\n\\subsection{交互 }\\label{ux4ea4ux4e92}\r\n\r\n通过平移和缩放来导航图表，点击和拖动用于平移，滚动手势用于缩放。双击一个节点或点击其\\texttt{+}按钮来展开代表一组操作的名称域。右下角有一个小地图可以在缩放和平移时方便的改变当前视角。\r\n\r\n要关闭一个打开的节点，再次双击它或点击它的\\texttt{-}按钮，你也可以只点击一次来选中一个节点，节点的颜色会加深，并且会看到节点的详情，其连接到的节点会在可视化右上角的详情卡片显现。\r\n\r\n\\begin{verbatim}\r\n<td style=\"width: 50%;\">\r\n  <img src=\"../images/infocard.png\" alt=\"一个名称域的详情卡片\" title=\"一个名称域的详情卡片\" />\r\n</td>\r\n<td style=\"width: 50%;\">\r\n  <img src=\"../images/infocard_op.png\" alt=\"操作节点的详情卡片\" title=\"操作节点的详情卡片\" />\r\n</td>\r\n\\end{verbatim}\r\n\r\n\\begin{verbatim}\r\n<td style=\"width: 50%;\">\r\n  详情卡片展示<code>conv2</code>名称域的详细信息，名称域中操作节点的输入和输出被结合在一起，适用于不显示属性的名称域。\r\n</td>\r\n<td style=\"width: 50%;\">\r\n  详情卡片展示<code>DecodeRaw</code>操作节点，除了输入和输出，卡片也会展示与当前节点相关的设备和属性。\r\n</td>\r\n\\end{verbatim}\r\n\r\n选择对于 high-degree\r\n节点的理解也很有帮助，选择任意节点，则与它的其余连接相应的节点也会选中，这使得在进行例如查看哪一个节点是否已保存等操作时非常容易。\r\n\r\n点击详情卡片中的一个节点名称时会选中该节点，必要的话，视角会自动平移以使该节点可见。\r\n\r\n最后，使用图例上方的颜色菜单，你可以给你的图表选择两个颜色方案。默认的\\emph{结构视图}下，当两个\r\nhigh-level\r\n节点颜色一样时，其会以相同的彩虹色彩出现，而结构唯一的节点颜色是灰色。还有一个视图则展示了不同的操作运行于什么设备之上。名称域被恰当的根据其中的操作节点的设备片件来着色。\r\n\r\n下图是一张真实图表的图解：\r\n\r\n\\begin{verbatim}\r\n<td style=\"width: 50%;\">\r\n  <img src=\"../images/colorby_structure.png\" alt=\"按结构着色\" title=\"按结构着色\" />\r\n</td>\r\n<td style=\"width: 50%;\">\r\n  <img src=\"../images/colorby_device.png\" alt=\"按设备着色\" title=\"按设备着色\" />\r\n</td>\r\n\\end{verbatim}\r\n\r\n\\begin{verbatim}\r\n<td style=\"width: 50%;\">\r\n  结构视图：灰色节点的结构是唯一的。橙色的<code>conv1</code>和<code>conv2</code>节点有相同的结构, 其他颜色的节点也类似。\r\n</td>\r\n<td style=\"width: 50%;\">\r\n  设备视图：名称域根据其中的操作节点的设备片件来着色，在此紫色代表GPU，绿色代表CPU。\r\n</td>\r\n\\end{verbatim}\r\n\r\n原文:\r\n\\href{../images/index.html\\#tensorboard-graph-visualization}{TensorBoard:\r\nGraph Visualization} 翻译: {[}@Warln{]}(https://github.com/Warln) 校对:\r\n\\href{https://github.com/lucky521}{lucky521}\r\n"
  },
  {
    "path": "tex_pdf/how_tos/c3s05_reading_data.tex",
    "content": "\r\n\r\n\\section{数据读取 }\\label{ux6570ux636eux8bfbux53d6}\r\n\r\nTensorFlow程序读取数据一共有3种方法:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  供给数据(Feeding)： 在TensorFlow程序运行的每一步，\r\n  让Python代码来供给数据。\r\n\\item\r\n  从文件读取数据： 在TensorFlow图的起始，\r\n  让一个输入管线从文件中读取数据。\r\n\\item\r\n  预加载数据：\r\n  在TensorFlow图中定义常量或变量来保存所有数据(仅适用于数据量比较小的情况)。\r\n\\end{itemize}\r\n\r\n\\subsection{目录}\\label{ux76eeux5f55}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-reading-data}{数据读取}}{数据读取}}\\label{ux6570ux636eux8bfbux53d6-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{Feeding}{供给数据(Feeding)}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-reading-from-files}{从文件读取数据}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-filenames--shuffling--and-epoch-limits}{文件名,\r\n  乱序(shuffling), 和最大训练迭代数(epoch limits)}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-file-formats}{文件格式}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-preprocessing}{预处理}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-batching}{批处理}\r\n\\item\r\n  \\protect\\hyperlink{QueueRunner}{使用\\texttt{QueueRunner}创建预读线程}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-filtering-records-or-producing-multiple-examples-per-record}{对记录进行过滤或者为每个纪录创建多个样本}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-sparse-input-data}{序列化输入数据(Sparse\r\n  input data)}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-preloaded-data}{预加载数据}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-multiple-input-pipelines}{多管线输入}\r\n\\end{itemize}\r\n\r\n\\subsection{供给数据 }\\label{ux4f9bux7ed9ux6570ux636e}\r\n\r\nTensorFlow的数据供给机制允许你在TensorFlow运算图中将数据注入到任一张量中。因此，python运算可以把数据直接设置到TensorFlow图中。\r\n\r\n通过给run()或者eval()函数输入\\texttt{feed\\_dict}参数，\r\n可以启动运算过程。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session():}\r\n  \\BuiltInTok{input} \\OperatorTok{=} \\NormalTok{tf.placeholder(tf.float32)}\r\n  \\NormalTok{classifier }\\OperatorTok{=} \\NormalTok{...}\r\n  \\BuiltInTok{print} \\NormalTok{classifier.}\\BuiltInTok{eval}\\NormalTok{(feed_dict}\\OperatorTok{=}\\NormalTok{\\{}\\BuiltInTok{input}\\NormalTok{: my_python_preprocessing_fn()\\})}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n虽然你可以使用常量和变量来替换任何一个张量，\r\n但是最好的做法应该是使用\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#placeholder}{\\texttt{placeholder}\r\nop}节点。设计\\texttt{placeholder}节点的唯一的意图就是为了提供数据供给(feeding)的方法。\\texttt{placeholder}节点被声明的时候是未初始化的，\r\n也不包含数据， 如果没有为它供给数据， 则TensorFlow运算的时候会产生错误，\r\n所以千万不要忘了为\\texttt{placeholder}提供数据。\r\n\r\n可以在\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/fully_connected_feed.py}{\\texttt{tensorflow/g3doc/tutorials/mnist/fully\\_connected\\_feed.py}}找到使用\\texttt{placeholder}和MNIST训练的例子，\\href{tensorflow-zh/SOURCE/tutorials/mnist/tf/index.md}{MNIST\r\ntutorial}也讲述了这一例子。\r\n\r\n\\subsection{从文件读取数据\r\n}\\label{ux4eceux6587ux4ef6ux8bfbux53d6ux6570ux636e}\r\n\r\n一共典型的文件读取管线会包含下面这些步骤：\r\n\r\n\\begin{enumerate}\r\n\\def\\labelenumi{\\arabic{enumi}.}\r\n\\tightlist\r\n\\item\r\n  文件名列表\r\n\\item\r\n  \\emph{可配置的} 文件名乱序(shuffling)\r\n\\item\r\n  \\emph{可配置的} 最大训练迭代数(epoch limit)\r\n\\item\r\n  文件名队列\r\n\\item\r\n  针对输入文件格式的阅读器\r\n\\item\r\n  纪录解析器\r\n\\item\r\n  \\emph{可配置的}预处理器\r\n\\item\r\n  样本队列\r\n\\end{enumerate}\r\n\r\n\\subsubsection{文件名, 乱序(shuffling), 和最大训练迭代数(epoch limits)\r\n}\\label{ux6587ux4ef6ux540d-ux4e71ux5e8fshuffling-ux548cux6700ux5927ux8badux7ec3ux8fedux4ee3ux6570epoch-limits}\r\n\r\n可以使用字符串张量(比如\\texttt{{[}\"file0\",\\ \"file1\"{]}},\r\n\\texttt{{[}(\"file\\%d\"\\ \\%\\ i)\\ for\\ i\\ in\\ range(2){]}}，\r\n\\texttt{{[}(\"file\\%d\"\\ \\%\\ i)\\ for\\ i\\ in\\ range(2){]}})\r\n或者\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#match_filenames_once}{\\texttt{tf.train.match\\_filenames\\_once}\r\n函数}来产生文件名列表。\r\n\r\n将文件名列表交给\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#string_input_producer}{\\texttt{tf.train.string\\_input\\_producer}\r\n函数}.\\texttt{string\\_input\\_producer}来生成一个先入先出的队列，\r\n文件阅读器会需要它来读取数据。\r\n\r\n\\texttt{string\\_input\\_producer}\r\n提供的可配置参数来设置文件名乱序和最大的训练迭代数，\r\n\\texttt{QueueRunner}会为每次迭代(epoch)将所有的文件名加入文件名队列中，\r\n如果\\texttt{shuffle=True}的话，\r\n会对文件名进行乱序处理。这一过程是比较均匀的，因此它可以产生均衡的文件名队列。\r\n\r\n这个\\texttt{QueueRunner}的工作线程是独立于文件阅读器的线程，\r\n因此乱序和将文件名推入到文件名队列这些过程不会阻塞文件阅读器运行。\r\n\r\n\\subsubsection{文件格式 }\\label{ux6587ux4ef6ux683cux5f0f}\r\n\r\n根据你的文件格式， 选择对应的文件阅读器，\r\n然后将文件名队列提供给阅读器的\\texttt{read}方法。阅读器的\\texttt{read}方法会输出一个key来表征输入的文件和其中的纪录(对于调试非常有用)，同时得到一个字符串标量，\r\n这个字符串标量可以被一个或多个解析器，或者转换操作将其解码为张量并且构造成为样本。\r\n\r\n\\paragraph{CSV 文件 }\\label{csv-ux6587ux4ef6}\r\n\r\n从CSV文件中读取数据，\r\n需要使用\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#TextLineReader}{\\texttt{TextLineReader}}和\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#decode_csv}{\\texttt{decode\\_csv}}\r\n操作， 如下面的例子所示：\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{filename_queue }\\OperatorTok{=} \\NormalTok{tf.train.string_input_producer([}\\StringTok{\"file0.csv\"}\\NormalTok{, }\\StringTok{\"file1.csv\"}\\NormalTok{])}\r\n\r\n\\NormalTok{reader }\\OperatorTok{=} \\NormalTok{tf.TextLineReader()}\r\n\\NormalTok{key, value }\\OperatorTok{=} \\NormalTok{reader.read(filename_queue)}\r\n\r\n\\CommentTok{# Default values, in case of empty columns. Also specifies the type of the}\r\n\\CommentTok{# decoded result.}\r\n\\NormalTok{record_defaults }\\OperatorTok{=} \\NormalTok{[[}\\DecValTok{1}\\NormalTok{], [}\\DecValTok{1}\\NormalTok{], [}\\DecValTok{1}\\NormalTok{], [}\\DecValTok{1}\\NormalTok{], [}\\DecValTok{1}\\NormalTok{]]}\r\n\\NormalTok{col1, col2, col3, col4, col5 }\\OperatorTok{=} \\NormalTok{tf.decode_csv(}\r\n    \\NormalTok{value, record_defaults}\\OperatorTok{=}\\NormalTok{record_defaults)}\r\n\\NormalTok{features }\\OperatorTok{=} \\NormalTok{tf.concat(}\\DecValTok{0}\\NormalTok{, [col1, col2, col3, col4])}\r\n\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session() }\\ImportTok{as} \\NormalTok{sess:}\r\n  \\CommentTok{# Start populating the filename queue.}\r\n  \\NormalTok{coord }\\OperatorTok{=} \\NormalTok{tf.train.Coordinator()}\r\n  \\NormalTok{threads }\\OperatorTok{=} \\NormalTok{tf.train.start_queue_runners(coord}\\OperatorTok{=}\\NormalTok{coord)}\r\n\r\n  \\ControlFlowTok{for} \\NormalTok{i }\\OperatorTok{in} \\BuiltInTok{range}\\NormalTok{(}\\DecValTok{1200}\\NormalTok{):}\r\n    \\CommentTok{# Retrieve a single instance:}\r\n    \\NormalTok{example, label }\\OperatorTok{=} \\NormalTok{sess.run([features, col5])}\r\n\r\n  \\NormalTok{coord.request_stop()}\r\n  \\NormalTok{coord.join(threads)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n每次\\texttt{read}的执行都会从文件中读取一行内容， \\texttt{decode\\_csv}\r\n操作会解析这一行内容并将其转为张量列表。如果输入的参数有缺失，\\texttt{record\\_default}参数可以根据张量的类型来设置默认值。\r\n\r\n在调用\\texttt{run}或者\\texttt{eval}去执行\\texttt{read}之前，\r\n你必须调用\\texttt{tf.train.start\\_queue\\_runners}来将文件名填充到队列。否则\\texttt{read}操作会被阻塞到文件名队列中有值为止。\r\n\r\n\\paragraph{固定长度的记录\r\n}\\label{ux56faux5b9aux957fux5ea6ux7684ux8bb0ux5f55}\r\n\r\n从二进制文件中读取固定长度纪录，\r\n可以使用\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#FixedLengthRecordReader}{\\texttt{tf.FixedLengthRecordReader}}的\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#decode_raw}{\\texttt{tf.decode\\_raw}}操作。\\texttt{decode\\_raw}操作可以讲一个字符串转换为一个uint8的张量。\r\n\r\n举例来说，\\href{http://www.cs.toronto.edu/~kriz/cifar.html}{the CIFAR-10\r\ndataset}的文件格式定义是：每条记录的长度都是固定的，一个字节的标签，后面是3072字节的图像数据。uint8的张量的标准操作就可以从中获取图像片并且根据需要进行重组。\r\n例子代码可以在\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_input.py}{\\texttt{tensorflow/models/image/cifar10/cifar10\\_input.py}}找到，具体讲述可参见\\href{tensorflow-zh/SOURCE/tutorials/deep_cnn/index.md\\#prepare-the-data}{教程}.\r\n\r\n\\paragraph{标准TensorFlow格式\r\n}\\label{ux6807ux51c6tensorflowux683cux5f0f}\r\n\r\n另一种保存记录的方法可以允许你讲任意的数据转换为TensorFlow所支持的格式，\r\n这种方法可以使TensorFlow的数据集更容易与网络应用架构相匹配。这种建议的方法就是使用TFRecords文件，TFRecords文件包含了\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/example.proto}{\\texttt{tf.train.Example}\r\n协议内存块(protocol buffer)}(协议内存块包含了字段\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/feature.proto}{\\texttt{Features}})。你可以写一段代码获取你的数据，\r\n将数据填入到\\texttt{Example}协议内存块(protocol\r\nbuffer)，将协议内存块序列化为一个字符串，\r\n并且通过\\href{tensorflow-zh/SOURCE/api_docs/python/python_io.md\\#TFRecordWriter}{\\texttt{tf.python\\_io.TFRecordWriter}\r\nclass}写入到TFRecords文件。\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/convert_to_records.py}{\\texttt{tensorflow/g3doc/how\\_tos/reading\\_data/convert\\_to\\_records.py}}就是这样的一个例子。\r\n\r\n从TFRecords文件中读取数据，\r\n可以使用\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#TFRecordReader}{\\texttt{tf.TFRecordReader}}的\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#parse_single_example}{\\texttt{tf.parse\\_single\\_example}}解析器。这个\\texttt{parse\\_single\\_example}操作可以将\\texttt{Example}协议内存块(protocol\r\nbuffer)解析为张量。 MNIST的例子就使用了\\texttt{convert\\_to\\_records}\r\n所构建的数据。\r\n请参看\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/fully_connected_reader.py}{\\texttt{tensorflow/g3doc/how\\_tos/reading\\_data/fully\\_connected\\_reader.py}},\r\n您也可以将这个例子跟\\texttt{fully\\_connected\\_feed}的版本加以比较。\r\n\r\n\\subsubsection{预处理 }\\label{ux9884ux5904ux7406}\r\n\r\n你可以对输入的样本进行任意的预处理， 这些预处理不依赖于训练参数，\r\n你可以在\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10.py}{\\texttt{tensorflow/models/image/cifar10/cifar10.py}}找到数据归一化，\r\n提取随机数据片，增加噪声或失真等等预处理的例子。\r\n\r\n\\subsubsection{批处理 }\\label{ux6279ux5904ux7406}\r\n\r\n在数据输入管线的末端，\r\n我们需要有另一个队列来执行输入样本的训练，评价和推理。因此我们使用\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#shuffle_batch}{\\texttt{tf.train.shuffle\\_batch}\r\n函数}来对队列中的样本进行乱序处理\r\n\r\n示例:\r\n\r\n\\begin{verbatim}\r\ndef read_my_file_format(filename_queue):\r\n  reader = tf.SomeReader()\r\n  key, record_string = reader.read(filename_queue)\r\n  example, label = tf.some_decoder(record_string)\r\n  processed_example = some_processing(example)\r\n  return processed_example, label\r\n\r\ndef input_pipeline(filenames, batch_size, num_epochs=None):\r\n  filename_queue = tf.train.string_input_producer(\r\n      filenames, num_epochs=num_epochs, shuffle=True)\r\n  example, label = read_my_file_format(filename_queue)\r\n  # min_after_dequeue defines how big a buffer we will randomly sample\r\n  #   from -- bigger means better shuffling but slower start up and more\r\n  #   memory used.\r\n  # capacity must be larger than min_after_dequeue and the amount larger\r\n  #   determines the maximum we will prefetch.  Recommendation:\r\n  #   min_after_dequeue + (num_threads + a small safety margin) * batch_size\r\n  min_after_dequeue = 10000\r\n  capacity = min_after_dequeue + 3 * batch_size\r\n  example_batch, label_batch = tf.train.shuffle_batch(\r\n      [example, label], batch_size=batch_size, capacity=capacity,\r\n      min_after_dequeue=min_after_dequeue)\r\n  return example_batch, label_batch\r\n\\end{verbatim}\r\n\r\n如果你需要对不同文件中的样子有更强的乱序和并行处理，可以使用\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#shuffle_batch_join}{\\texttt{tf.train.shuffle\\_batch\\_join}\r\n函数}. 示例:\r\n\r\n\\begin{verbatim}\r\ndef read_my_file_format(filename_queue):\r\n  # Same as above\r\n\r\ndef input_pipeline(filenames, batch_size, read_threads, num_epochs=None):\r\n  filename_queue = tf.train.string_input_producer(\r\n      filenames, num_epochs=num_epochs, shuffle=True)\r\n  example_list = [read_my_file_format(filename_queue)\r\n                  for _ in range(read_threads)]\r\n  min_after_dequeue = 10000\r\n  capacity = min_after_dequeue + 3 * batch_size\r\n  example_batch, label_batch = tf.train.shuffle_batch_join(\r\n      example_list, batch_size=batch_size, capacity=capacity,\r\n      min_after_dequeue=min_after_dequeue)\r\n  return example_batch, label_batch\r\n\\end{verbatim}\r\n\r\n在这个例子中， 你虽然只使用了一个文件名队列，\r\n但是TensorFlow依然能保证多个文件阅读器从同一次迭代(epoch)的不同文件中读取数据，知道这次迭代的所有文件都被开始读取为止。（通常来说一个线程来对文件名队列进行填充的效率是足够的）\r\n\r\n另一种替代方案是：\r\n使用\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#shuffle_batch}{\\texttt{tf.train.shuffle\\_batch}\r\n函数},设置\\texttt{num\\_threads}的值大于1。\r\n这种方案可以保证同一时刻只在一个文件中进行读取操作(但是读取速度依然优于单线程)，而不是之前的同时读取多个文件。这种方案的优点是：\r\n* 避免了两个不同的线程从同一个文件中读取同一个样本。 *\r\n避免了过多的磁盘搜索操作。\r\n\r\n你一共需要多少个读取线程呢？\r\n函数\\texttt{tf.train.shuffle\\_batch*}为TensorFlow图提供了获取文件名队列中的元素个数之和的方法。\r\n如果你有足够多的读取线程，\r\n文件名队列中的元素个数之和应该一直是一个略高于0的数。具体可以参考\\href{tensorflow-zh/SOURCE/how_tos/summaries_and_tensorboard/index.md}{TensorBoard:可视化学习}.\r\n\r\n\\subsubsection{\\texorpdfstring{创建线程并使用\\texttt{QueueRunner}对象来预取\r\n}{创建线程并使用QueueRunner对象来预取 }}\\label{ux521bux5efaux7ebfux7a0bux5e76ux4f7fux7528queuerunnerux5bf9ux8c61ux6765ux9884ux53d6}\r\n\r\n简单来说：使用上面列出的许多\\texttt{tf.train}函数添加\\href{../../api_docs/python/train.md\\#QueueRunner}{\\texttt{QueueRunner}}到你的数据流图中。在你运行任何训练步骤之前，需要调用\\href{../../api_docs/python/train.md\\#start_queue_runners}{\\texttt{tf.train.start\\_queue\\_runners}}函数，否则数据流图将一直挂起。\\href{../../api_docs/python/train.md\\#start_queue_runners}{\\texttt{tf.train.start\\_queue\\_runners}}\r\n这个函数将会启动输入管道的线程，填充样本到队列中，以便出队操作可以从队列中拿到样本。这种情况下最好配合使用一个\\href{../../api_docs/python/train.md\\#Coordinator}{\\texttt{tf.train.Coordinator}}，这样可以在发生错误的情况下正确地关闭这些线程。如果你对训练迭代数做了限制，那么需要使用一个训练迭代数计数器，并且需要被初始化。推荐的代码模板如下：\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Create the graph, etc.}\r\n\\NormalTok{init_op }\\OperatorTok{=} \\NormalTok{tf.initialize_all_variables()}\r\n\r\n\\CommentTok{# Create a session for running operations in the Graph.}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session()}\r\n\r\n\\CommentTok{# Initialize the variables (like the epoch counter).}\r\n\\NormalTok{sess.run(init_op)}\r\n\r\n\\CommentTok{# Start input enqueue threads.}\r\n\\NormalTok{coord }\\OperatorTok{=} \\NormalTok{tf.train.Coordinator()}\r\n\\NormalTok{threads }\\OperatorTok{=} \\NormalTok{tf.train.start_queue_runners(sess}\\OperatorTok{=}\\NormalTok{sess, coord}\\OperatorTok{=}\\NormalTok{coord)}\r\n\r\n\\ControlFlowTok{try}\\NormalTok{:}\r\n    \\ControlFlowTok{while} \\OperatorTok{not} \\NormalTok{coord.should_stop():}\r\n        \\CommentTok{# Run training steps or whatever}\r\n        \\NormalTok{sess.run(train_op)}\r\n\r\n\\ControlFlowTok{except} \\NormalTok{tf.errors.OutOfRangeError:}\r\n    \\BuiltInTok{print} \\StringTok{'Done training -- epoch limit reached'}\r\n\\ControlFlowTok{finally}\\NormalTok{:}\r\n    \\CommentTok{# When done, ask the threads to stop.}\r\n    \\NormalTok{coord.request_stop()}\r\n\r\n\\CommentTok{# Wait for threads to finish.}\r\n\\NormalTok{coord.join(threads)}\r\n\\NormalTok{sess.close()}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\paragraph{疑问: 这是怎么回事?\r\n}\\label{ux7591ux95ee-ux8fd9ux662fux600eux4e48ux56deux4e8b}\r\n\r\n首先，我们先创建数据流图，这个数据流图由一些流水线的阶段组成，阶段间用队列连接在一起。第一阶段将生成文件名，我们读取这些文件名并且把他们排到文件名队列中。第二阶段从文件中读取数据（使用\\texttt{Reader}），产生样本，而且把样本放在一个样本队列中。根据你的设置，实际上也可以拷贝第二阶段的样本，使得他们相互独立，这样就可以从多个文件中并行读取。在第二阶段的最后是一个排队操作，就是入队到队列中去，在下一阶段出队。因为我们是要开始运行这些入队操作的线程，所以我们的训练循环会使得样本队列中的样本不断地出队。\r\n\r\n在\\texttt{tf.train}中要创建这些队列和执行入队操作，就要添加\\href{../../api_docs/python/train.md\\#QueueRunner}{\\texttt{tf.train.QueueRunner}}到一个使用\\href{../../api_docs/python/train.md\\#add_queue_runner}{\\texttt{tf.train.add\\_queue\\_runner}}函数的数据流图中。每个\\texttt{QueueRunner}负责一个阶段，处理那些需要在线程中运行的入队操作的列表。一旦数据流图构造成功，\\href{../../api_docs/python/train.md\\#start_queue_runners}{\\texttt{tf.train.start\\_queue\\_runners}}函数就会要求数据流图中每个\\texttt{QueueRunner}去开始它的线程运行入队操作。\r\n\r\n如果一切顺利的话，你现在可以执行你的训练步骤，同时队列也会被后台线程来填充。如果您设置了最大训练迭代数，在某些时候，样本出队的操作可能会得到一个\\href{../../api_docs/python/client.md\\#OutOfRangeError}{\\texttt{tf.OutOfRangeError}}的错误。这其实是TensorFlow的``文件结束''（EOF）\r\n------------\r\n这就意味着已经达到了最大训练迭代数，已经没有更多可用的样本了。\r\n\r\n最后一个因素是\\href{../../api_docs/python/train.md\\#Coordinator}{\\texttt{Coordinator}}。这是负责在收到任何关闭信号的时候，让所有的线程都知道。最常用的是在发生异常时这种情况就会呈现出来，比如说其中一个线程在运行某些操作时出现错误（或一个普通的Python异常）。\r\n\r\n想要了解更多的关于threading, queues, QueueRunners, and\r\nCoordinators的内容可以\\href{../../how_tos/threading_and_queues/index.md}{看这里}.\r\n\r\n\\paragraph{疑问: 在达到最大训练迭代数的时候如何清理关闭线程?\r\n}\\label{ux7591ux95ee-ux5728ux8fbeux5230ux6700ux5927ux8badux7ec3ux8fedux4ee3ux6570ux7684ux65f6ux5019ux5982ux4f55ux6e05ux7406ux5173ux95edux7ebfux7a0b}\r\n\r\n想象一下，你有一个模型并且设置了最大训练迭代数。这意味着，生成文件的那个线程将只会在产生\\texttt{OutOfRange}错误之前运行许多次。该\\texttt{QueueRunner}会捕获该错误，并且关闭文件名的队列，最后退出线程。关闭队列做了两件事情：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  如果还试着对文件名队列执行入队操作时将发生错误。任何线程不应该尝试去这样做，但是当队列因为其他错误而关闭时，这就会有用了。\r\n\\item\r\n  任何当前或将来出队操作要么成功（如果队列中还有足够的元素）或立即失败（发生\\texttt{OutOfRange}错误）。它们不会防止等待更多的元素被添加到队列中，因为上面的一点已经保证了这种情况不会发生。\r\n\\end{itemize}\r\n\r\n关键是，当在文件名队列被关闭时候，有可能还有许多文件名在该队列中，这样下一阶段的流水线（包括reader和其它预处理）还可以继续运行一段时间。\r\n一旦文件名队列空了之后，如果后面的流水线还要尝试从文件名队列中取出一个文件名（例如，从一个已经处理完文件的reader中），这将会触发\\texttt{OutOfRange}错误。在这种情况下，即使你可能有一个QueueRunner关联着多个线程。如果这不是在QueueRunner中的最后那个线程，\\texttt{OutOfRange}错误仅仅只会使得一个线程退出。这使得其他那些正处理自己的最后一个文件的线程继续运行，直至他们完成为止。\r\n（但如果假设你使用的是\\href{../../api_docs/python/train.md\\#Coordinator}{\\texttt{tf.train.Coordinator}}，其他类型的错误将导致所有线程停止）。一旦所有的reader线程触发\\texttt{OutOfRange}错误，然后才是下一个队列，再是样本队列被关闭。\r\n\r\n同样，样本队列中会有一些已经入队的元素，所以样本训练将一直持续直到样本队列中再没有样本为止。如果样本队列是一个\\href{../../api_docs/python/io_ops.md\\#RandomShuffleQueue}{\\texttt{RandomShuffleQueue}}，因为你使用了\\texttt{shuffle\\_batch}\r\n或者\r\n\\texttt{shuffle\\_batch\\_join}，所以通常不会出现以往那种队列中的元素会比\\texttt{min\\_after\\_dequeue}\r\n定义的更少的情况。\r\n然而，一旦该队列被关闭，\\texttt{min\\_after\\_dequeue}设置的限定值将失效，最终队列将为空。在这一点来说，当实际训练线程尝试从样本队列中取出数据时，将会触发\\texttt{OutOfRange}错误，然后训练线程会退出。一旦所有的培训线程完成，\\href{../../api_docs/python/train.md\\#Coordinator.join}{\\texttt{tf.train.Coordinator.join}}会返回，你就可以正常退出了。\r\n\r\n\\subsubsection{筛选记录或产生每个记录的多个样本\r\n}\\label{ux7b5bux9009ux8bb0ux5f55ux6216ux4ea7ux751fux6bcfux4e2aux8bb0ux5f55ux7684ux591aux4e2aux6837ux672c}\r\n\r\n举个例子，有形式为\\texttt{{[}x,\\ y,\\ z{]}}的样本，我们可以生成一批形式为\\texttt{{[}batch,\\ x,\\ y,\\ z{]}}的样本。\r\n如果你想滤除这个记录（或许不需要这样的设置），那么可以设置batch的大小为0；但如果你需要每个记录产生多个样本，那么batch的值可以大于1。\r\n然后很简单，只需调用批处理函数（比如： \\texttt{shuffle\\_batch} or\r\n\\texttt{shuffle\\_batch\\_join}）去设置\\texttt{enqueue\\_many=True}就可以实现。\r\n\r\n\\subsubsection{稀疏输入数据\r\n}\\label{ux7a00ux758fux8f93ux5165ux6570ux636e}\r\n\r\nSparseTensors这种数据类型使用队列来处理不是太好。如果要使用SparseTensors你就必须在批处理\\textbf{之后}使用\\href{../../api_docs/python/io_ops.md\\#parse_example}{\\texttt{tf.parse\\_example}}\r\n去解析字符串记录 (而不是在批处理\\textbf{之前}使用\r\n\\texttt{tf.parse\\_single\\_example}) 。\r\n\r\n\\subsection{预取数据 }\\label{ux9884ux53d6ux6570ux636e}\r\n\r\n这仅用于可以完全加载到存储器中的小的数据集。有两种方法：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  存储在常数中。\r\n\\item\r\n  存储在变量中，初始化后，永远不要改变它的值。\r\n\\end{itemize}\r\n\r\n使用常数更简单一些，但是会使用更多的内存（因为常数会内联的存储在数据流图数据结构中，这个结构体可能会被复制几次）。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{training_data }\\OperatorTok{=} \\NormalTok{...}\r\n\\NormalTok{training_labels }\\OperatorTok{=} \\NormalTok{...}\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session():}\r\n  \\NormalTok{input_data }\\OperatorTok{=} \\NormalTok{tf.constant(training_data)}\r\n  \\NormalTok{input_labels }\\OperatorTok{=} \\NormalTok{tf.constant(training_labels)}\r\n  \\NormalTok{...}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n要改为使用变量的方式，您就需要在数据流图建立后初始化这个变量。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{training_data }\\OperatorTok{=} \\NormalTok{...}\r\n\\NormalTok{training_labels }\\OperatorTok{=} \\NormalTok{...}\r\n\\ControlFlowTok{with} \\NormalTok{tf.Session() }\\ImportTok{as} \\NormalTok{sess:}\r\n  \\NormalTok{data_initializer }\\OperatorTok{=} \\NormalTok{tf.placeholder(dtype}\\OperatorTok{=}\\NormalTok{training_data.dtype,}\r\n                                    \\NormalTok{shape}\\OperatorTok{=}\\NormalTok{training_data.shape)}\r\n  \\NormalTok{label_initializer }\\OperatorTok{=} \\NormalTok{tf.placeholder(dtype}\\OperatorTok{=}\\NormalTok{training_labels.dtype,}\r\n                                     \\NormalTok{shape}\\OperatorTok{=}\\NormalTok{training_labels.shape)}\r\n  \\NormalTok{input_data }\\OperatorTok{=} \\NormalTok{tf.Variable(data_initalizer, trainable}\\OperatorTok{=}\\VariableTok{False}\\NormalTok{, collections}\\OperatorTok{=}\\NormalTok{[])}\r\n  \\NormalTok{input_labels }\\OperatorTok{=} \\NormalTok{tf.Variable(label_initalizer, trainable}\\OperatorTok{=}\\VariableTok{False}\\NormalTok{, collections}\\OperatorTok{=}\\NormalTok{[])}\r\n  \\NormalTok{...}\r\n  \\NormalTok{sess.run(input_data.initializer,}\r\n           \\NormalTok{feed_dict}\\OperatorTok{=}\\NormalTok{\\{data_initializer: training_data\\})}\r\n  \\NormalTok{sess.run(input_labels.initializer,}\r\n           \\NormalTok{feed_dict}\\OperatorTok{=}\\NormalTok{\\{label_initializer: training_lables\\})}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n设定\\texttt{trainable=False} 可以防止该变量被数据流图的\r\n\\texttt{GraphKeys.TRAINABLE\\_VARIABLES} 收集,\r\n这样我们就不会在训练的时候尝试更新它的值； 设定\r\n\\texttt{collections={[}{]}} 可以防止\\texttt{GraphKeys.VARIABLES}\r\n收集后做为保存和恢复的中断点。\r\n\r\n无论哪种方式，\\href{../../api_docs/python/io_ops.md\\#slice_input_producer}{\\texttt{tf.train.slice\\_input\\_producer\\ function}}函数可以被用来每次产生一个切片。这样就会让样本在整个迭代中被打乱，所以在使用批处理的时候不需要再次打乱样本。所以我们不使用\\texttt{shuffle\\_batch}函数，取而代之的是纯\\href{../../api_docs/python/io_ops.md\\#batch}{\\texttt{tf.train.batch}\r\n函数}。\r\n如果要使用多个线程进行预处理，需要将\\texttt{num\\_threads}参数设置为大于1的数字。\r\n\r\n在\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded.py}{\\texttt{tensorflow/g3doc/how\\_tos/reading\\_data/fully\\_connected\\_preloaded.py}}\r\n中可以找到一个MNIST例子，使用常数来预加载。\r\n另外使用变量来预加载的例子在\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/how_tos/reading_data/fully_connected_preloaded_var.py}{\\texttt{tensorflow/g3doc/how\\_tos/reading\\_data/fully\\_connected\\_preloaded\\_var.py}}，你可以用上面\r\n\\texttt{fully\\_connected\\_feed} 和 \\texttt{fully\\_connected\\_reader}\r\n的描述来进行比较。\r\n\r\n\\subsection{多输入管道 }\\label{ux591aux8f93ux5165ux7ba1ux9053}\r\n\r\n通常你会在一个数据集上面训练，然后在另外一个数据集上做评估计算(或称为\r\n``eval'')。 这样做的一种方法是，实际上包含两个独立的进程：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  训练过程中读取输入数据，并定期将所有的训练的变量写入还原点文件）。\r\n\\item\r\n  在计算过程中恢复还原点文件到一个推理模型中，读取有效的输入数据。\r\n\\end{itemize}\r\n\r\n这两个进程在下面的例子中已经完成了：\\href{../../tutorials/deep_cnn/index.md\\#save-and-restore-checkpoints}{the\r\nexample CIFAR-10 model}，有以下几个好处：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  eval被当做训练后变量的一个简单映射。\r\n\\item\r\n  你甚至可以在训练完成和退出后执行eval。\r\n\\end{itemize}\r\n\r\n您可以在同一个进程的相同的数据流图中有训练和eval，并分享他们的训练后的变量。参考\\href{../../how_tos/variable_scope/index.md}{the\r\nshared variables tutorial}.\r\n\r\n原文地址：\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/how_tos/reading_data/index.md}{Reading\r\ndata} 翻译：\\href{https://github.com/volvet}{volvet} and\r\n\\href{https://github.com/zhangkom}{zhangkom} 校对：\r\n\r\n"
  },
  {
    "path": "tex_pdf/how_tos/c3s06_threading_and_queues.tex",
    "content": "\r\n\r\n\\section{线程和队列 }\\label{ux7ebfux7a0bux548cux961fux5217}\r\n\r\n在使用TensorFlow进行异步计算时，队列是一种强大的机制。\r\n\r\n正如TensorFlow中的其他组件一样，队列就是TensorFlow图中的节点。这是一种有状态的节点，就像变量一样：其他节点可以修改它的内容。具体来说，其他节点可以把新元素插入到队列后端(rear)，也可以把队列前端(front)的元素删除。\r\n\r\n为了感受一下队列，让我们来看一个简单的例子。我们先创建一个``先入先出''的队列（FIFOQueue），并将其内部所有元素初始化为零。然后，我们构建一个TensorFlow图，它从队列前端取走一个元素，加上1之后，放回队列的后端。慢慢地，队列的元素的值就会增加。\r\n\r\n\\texttt{Enqueue}、\r\n\\texttt{EnqueueMany}和\\texttt{Dequeue}都是特殊的节点。他们需要获取队列指针，而非普通的值，如此才能修改队列内容。我们建议您将它们看作队列的方法。事实上，在Python\r\nAPI中，它们就是队列对象的方法（例如\\texttt{q.enqueue(...)}）。\r\n\r\n现在你已经对队列有了一定的了解，让我们深入到细节\\ldots{}\r\n\r\n\\subsection{队列使用概述 }\\label{ux961fux5217ux4f7fux7528ux6982ux8ff0}\r\n\r\n队列，如\\texttt{FIFOQueue}和\\texttt{RandomShuffleQueue}，在TensorFlow的张量异步计算时都非常重要。\r\n\r\n例如，一个典型的输入结构：是使用一个\\texttt{RandomShuffleQueue}来作为模型训练的输入：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  多个线程准备训练样本，并且把这些样本推入队列。\r\n\\item\r\n  一个训练线程执行一个训练操作，此操作会从队列中移除最小批次的样本（mini-batches)。\r\n\\end{itemize}\r\n\r\n这种结构具有许多优点，正如在\\href{../reading_data}{Reading data how\r\nto}中强调的，同时，\\href{../reading_data}{Reading data how\r\nto}也概括地描述了如何简化输入管道的构造过程。\r\n\r\nTensorFlow的\\texttt{Session}对象是可以支持多线程的，因此多个线程可以很方便地使用同一个会话（Session）并且并行地执行操作。然而，在Python程序实现这样的并行运算却并不容易。所有线程都必须能被同步终止，异常必须能被正确捕获并报告，回话终止的时候，\r\n队列必须能被正确地关闭。\r\n\r\n所幸TensorFlow提供了两个类来帮助多线程的实现：\\href{tensorflow-zh/SOURCE/api_docs/python/train.md\\#Coordinator}{tf.Coordinator}和\r\n\\href{tensorflow-zh/SOURCE/api_docs/python/train.md\\#QueueRunner}{tf.QueueRunner}。从设计上这两个类必须被一起使用。\\texttt{Coordinator}类可以用来同时停止多个工作线程并且向那个在等待所有工作线程终止的程序报告异常。\\texttt{QueueRunner}类用来协调多个工作线程同时将多个张量推入同一个队列中。\r\n\r\n\\subsection{Coordinator }\\label{coordinator}\r\n\r\nCoordinator类用来帮助多个线程协同工作，多个线程同步终止。 其主要方法有：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{should\\_stop()}:如果线程应该停止则返回True。\r\n\\item\r\n  \\texttt{request\\_stop(\\textless{}exception\\textgreater{})}:\r\n  请求该线程停止。\r\n\\item\r\n  \\texttt{join(\\textless{}list\\ of\\ threads\\textgreater{})}:等待被指定的线程终止。\r\n\\end{itemize}\r\n\r\n首先创建一个\\texttt{Coordinator}对象，然后建立一些使用\\texttt{Coordinator}对象的线程。这些线程通常一直循环运行，一直到\\texttt{should\\_stop()}返回True时停止。\r\n任何线程都可以决定计算什么时候应该停止。它只需要调用\\texttt{request\\_stop()}，同时其他线程的\\texttt{should\\_stop()}将会返回\\texttt{True}，然后都停下来。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 线程体：循环执行，直到`Coordinator`收到了停止请求。}\r\n\\CommentTok{# 如果某些条件为真，请求`Coordinator`去停止其他线程。}\r\n\\KeywordTok{def} \\NormalTok{MyLoop(coord):}\r\n  \\ControlFlowTok{while} \\OperatorTok{not} \\NormalTok{coord.should_stop():}\r\n    \\NormalTok{...do something...}\r\n    \\ControlFlowTok{if} \\NormalTok{...some condition...:}\r\n      \\NormalTok{coord.request_stop()}\r\n\r\n\\CommentTok{# Main code: create a coordinator.}\r\n\\NormalTok{coord }\\OperatorTok{=} \\NormalTok{Coordinator()}\r\n\r\n\\CommentTok{# Create 10 threads that run 'MyLoop()'}\r\n\\NormalTok{threads }\\OperatorTok{=} \\NormalTok{[threading.Thread(target}\\OperatorTok{=}\\NormalTok{MyLoop, args}\\OperatorTok{=}\\NormalTok{(coord)) }\\ControlFlowTok{for} \\NormalTok{i }\\OperatorTok{in} \\BuiltInTok{xrange}\\NormalTok{(}\\DecValTok{10}\\NormalTok{)]}\r\n\r\n\\CommentTok{# Start the threads and wait for all of them to stop.}\r\n\\ControlFlowTok{for} \\NormalTok{t }\\OperatorTok{in} \\NormalTok{threads: t.start()}\r\n\\NormalTok{coord.join(threads)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n显然，Coordinator可以管理线程去做不同的事情。上面的代码只是一个简单的例子，在设计实现的时候不必完全照搬。Coordinator还支持捕捉和报告异常,\r\n具体可以参考\\href{tensorflow-zh/SOURCE/api_docs/python/train.md\\#Coordinator}{Coordinator\r\nclass}的文档。\r\n\r\n\\subsection{QueueRunner }\\label{queuerunner}\r\n\r\n\\texttt{QueueRunner}类会创建一组线程，\r\n这些线程可以重复的执行Enquene操作，\r\n他们使用同一个Coordinator来处理线程同步终止。此外，一个QueueRunner会运行一个\\emph{closer\r\nthread}，当Coordinator收到异常报告时，这个\\emph{closer\r\nthread}会自动关闭队列。\r\n\r\n您可以使用一个queue runner，来实现上述结构。\r\n首先建立一个TensorFlow图表，这个图表使用队列来输入样本。增加处理样本并将样本推入队列中的操作。增加training操作来移除队列中的样本。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{example }\\OperatorTok{=} \\NormalTok{...ops to create one example...}\r\n\\CommentTok{# Create a queue, and an op that enqueues examples one at a time in the queue.}\r\n\\NormalTok{queue }\\OperatorTok{=} \\NormalTok{tf.RandomShuffleQueue(...)}\r\n\\NormalTok{enqueue_op }\\OperatorTok{=} \\NormalTok{queue.enqueue(example)}\r\n\\CommentTok{# Create a training graph that starts by dequeuing a batch of examples.}\r\n\\NormalTok{inputs }\\OperatorTok{=} \\NormalTok{queue.dequeue_many(batch_size)}\r\n\\NormalTok{train_op }\\OperatorTok{=} \\NormalTok{...use }\\StringTok{'inputs'} \\NormalTok{to build the training part of the graph...}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n在Python的训练程序中，创建一个\\texttt{QueueRunner}来运行几个线程，\r\n这几个线程处理样本，并且将样本推入队列。创建一个\\texttt{Coordinator}，让queue\r\nrunner使用\\texttt{Coordinator}来启动这些线程，创建一个训练的循环，\r\n并且使用\\texttt{Coordinator}来控制\\texttt{QueueRunner}的线程们的终止。\r\n\r\n\\begin{verbatim}\r\n# Create a queue runner that will run 4 threads in parallel to enqueue\r\n# examples.\r\nqr = tf.train.QueueRunner(queue, [enqueue_op] * 4)\r\n\r\n# Launch the graph.\r\nsess = tf.Session()\r\n# Create a coordinator, launch the queue runner threads.\r\ncoord = tf.train.Coordinator()\r\nenqueue_threads = qr.create_threads(sess, coord=coord, start=True)\r\n# Run the training loop, controlling termination with the coordinator.\r\nfor step in xrange(1000000):\r\n    if coord.should_stop():\r\n        break\r\n    sess.run(train_op)\r\n# When done, ask the threads to stop.\r\ncoord.request_stop()\r\n# And wait for them to actually do it.\r\ncoord.join(threads)\r\n\\end{verbatim}\r\n\r\n\\subsection{异常处理 }\\label{ux5f02ux5e38ux5904ux7406}\r\n\r\n通过queue\r\nrunners启动的线程不仅仅只处理推送样本到队列。他们还捕捉和处理由队列产生的异常，包括\\texttt{OutOfRangeError}异常，这个异常是用于报告队列被关闭。\r\n使用\\texttt{Coordinator}的训练程序在主循环中必须同时捕捉和报告异常。\r\n下面是对上面训练循环的改进版本。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{try}\\NormalTok{:}\r\n    \\ControlFlowTok{for} \\NormalTok{step }\\OperatorTok{in} \\BuiltInTok{xrange}\\NormalTok{(}\\DecValTok{1000000}\\NormalTok{):}\r\n        \\ControlFlowTok{if} \\NormalTok{coord.should_stop():}\r\n            \\ControlFlowTok{break}\r\n        \\NormalTok{sess.run(train_op)}\r\n\\ControlFlowTok{except} \\PreprocessorTok{Exception}\\NormalTok{, e:}\r\n   \\CommentTok{# Report exceptions to the coordinator.}\r\n   \\NormalTok{coord.request_stop(e)}\r\n\r\n\\CommentTok{# Terminate as usual.  It is innocuous to request stop twice.}\r\n\\NormalTok{coord.request_stop()}\r\n\\NormalTok{coord.join(threads)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n原文地址：\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/how_tos/threading_and_queues/index.md}{Threading\r\nand Queues} 翻译：\\href{https://github.com/zhangkom}{zhangkom}\r\n校对：\\href{https://github.com/volvet}{volvet}\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/how_tos/c3s07_adding_an_op.tex",
    "content": "\r\n\r\n\\section{增加一个新 Op }\\label{ux589eux52a0ux4e00ux4e2aux65b0-op}\r\n\r\n预备知识:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  对 C++ 有一定了解.\r\n\\item\r\n  已经\\href{tensorflow-zh/SOURCE/get_started/introduction.md\\#source}{下载\r\n  TensorFlow 源代码}并有能力编译它.\r\n\\end{itemize}\r\n\r\n如果现有的库没有涵盖你想要的操作, 你可以自己定制一个. 为了使定制的 Op\r\n能够兼容原有的库 , 你必须做以下工作:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  在一个 C++ 文件中注册新 Op. Op 的注册与实现是相互独立的.\r\n  在其注册时描述了 Op 该如何执行. 例如, 注册 Op 时定义了 Op 的名字,\r\n  并指定了它的输入和输出.\r\n\\item\r\n  使用 C++ 实现 Op. 每一个实现称之为一个 ``kernel'', 可以存在多个\r\n  kernel, 以适配不同的架构 (CPU, GPU 等)或不同的输入/输出类型.\r\n\\item\r\n  创建一个 Python 包装器（wrapper）. 这个包装器是创建 Op 的公开 API.\r\n  当注册 Op 时, 会自动生成一个默认 默认的包装器.\r\n  既可以直接使用默认包装器, 也可以添加一个新的包装器.\r\n\\item\r\n  (可选) 写一个函数计算 Op 的梯度.\r\n\\item\r\n  (可选) 写一个函数, 描述 Op 的输入和输出 shape. 该函数能够允许从 Op\r\n  推断 shape.\r\n\\item\r\n  测试 Op, 通常使用\r\n  Pyhton。如果你定义了梯度，你可以使用Python的\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/kernel_tests/gradient_checker.py}{GradientChecker}来测试它。\r\n\\end{itemize}\r\n\r\n\\subsection{内容}\\label{ux5185ux5bb9}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-adding-a-new-op}{增加一个新\r\nOp}}{增加一个新 Op}}\\label{ux589eux52a0ux4e00ux4e2aux65b0-op-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{defineux5finterface}{定义 Op 的接口}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-implement-the-kernel-for-the-op}{为\r\n  Op 实现 kernel}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-generate-the-client-wrapper}{生成客户端包装器}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-the-python-op-wrapper}{Python Op\r\n  包装器}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-the-c---op-wrapper}{C++ Op 包装器}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-verify-it-works}{检查 Op\r\n  能否正常工作}\r\n\\item\r\n  \\protect\\hyperlink{Validation}{验证条件}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-op-registration}{Op 注册}\r\n\\item\r\n  \\protect\\hyperlink{Attrs}{属性}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-attr-types}{属性类型}\r\n\\item\r\n  \\protect\\hyperlink{Polymorphism}{多态}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-inputs-and-outputs}{输入和输出}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-backwards-compatibility}{向后兼容性}\r\n\\item\r\n  \\protect\\hyperlink{mult-archs}{GPU 支持}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-implement-the-gradient-in-python}{使用\r\n  Python 实现梯度}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-implement-a-shape-function-in-python}{使用\r\n  Python 实现 shape 函数}\r\n\\end{itemize}\r\n\r\n\\subsection{定义 Op 的接口 }\\label{ux5b9aux4e49-op-ux7684ux63a5ux53e3}\r\n\r\n向 TensorFlow 系统注册来定义 Op 的接口. 在注册时, 指定 Op 的名称,\r\n它的输入(类型和名称) 和输出(类型和名称), 和所需要任何\r\n\\protect\\hyperlink{Attrs}{属性}的文档说明.\r\n\r\n为了让你有直观的认识, 创建一个简单的 Op 作为例子. 该 Op 接受一个\r\n\\texttt{int32} 类型 tensor 作为 输入, 输出这个 tensor 的一个副本,\r\n副本与原 tensor 唯一的区别在于第一个元素被置为 0. 创建 文件\r\n\\texttt{tensorflow/core/user\\_ops/zero\\_out.cc}, 并调用\r\n\\texttt{REGISTER\\_OP} 宏来定义 Op 的接口.\r\n\r\n\\begin{verbatim}\r\n #include \"tensorflow/core/framework/op.h\"\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Input(\"to_zero: int32\")\r\n    .Output(\"zeroed: int32\");\r\n\\end{verbatim}\r\n\r\n\\texttt{ZeroOut} Op 接受 32 位整型的 tensor \\texttt{to\\_zero} 作为输入,\r\n输出 32 位整型的 tensor \\texttt{zeroed}.\r\n\r\n\\begin{quote}\r\n命名的注意事项: Op 的名称必须是为唯一的, 并使用驼峰命名法. 以下划线\r\n\\texttt{\\_} 开始的名称保留为内部使用.\r\n\\end{quote}\r\n\r\n\\subsection{为 Op 实现 kernel }\\label{ux4e3a-op-ux5b9eux73b0-kernel}\r\n\r\n在定义接口之后, 提供一个或多个 Op 的实现. 为这些 kernel\r\n的每一个创建一个对应的类, 继承 \\texttt{OpKernel}, 覆盖 \\texttt{Compute}\r\n方法. \\texttt{Compute} 方法提供一个类型为 \\texttt{OpKernelContext*}\r\n的参数 \\texttt{context}, 用于访问一些有用的信息, 例如输入和输出的\r\ntensor.\r\n\r\n将 kernel 添加到刚才创建的文件中, kernel 看起来和下面的代码类似:\r\n\r\n\\begin{verbatim}\r\n #include \"tensorflow/core/framework/op_kernel.h\"\r\nusing namespace tensorflow;\r\nclass ZeroOutOp : public OpKernel {\r\n public:\r\n  explicit ZeroOutOp(OpKernelConstruction* context) : OpKernel(context) {}\r\n  void Compute(OpKernelContext* context) override {\r\n    // 获取输入 tensor.\r\n    const Tensor& input_tensor = context->input(0);\r\n    auto input = input_tensor.flat<int32>();\r\n   // 创建一个输出 tensor.\r\n    Tensor* output_tensor = NULL;\r\n    OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),\r\n                                                     &output_tensor));\r\n    auto output = output_tensor->template flat<int32>();\r\n    // 设置 tensor 除第一个之外的元素均设为 0.\r\n    const int N = input.size();\r\n    for (int i = 1; i < N; i++) {\r\n      output(i) = 0;\r\n    }\r\n    // 尽可能地保留第一个元素的值.\r\n    if (N > 0) output(0) = input(0);\r\n  }\r\n};\r\n\\end{verbatim}\r\n\r\n实现 kernel 后, 将其注册到 TensorFlow 系统中. 注册时, 可以指定该 kernel\r\n运行时的多个约束 条件. 例如可以指定一个 kernel 在 CPU 上运行, 另一个在\r\nGPU 上运行.\r\n\r\n将下列代码加入到 \\texttt{zero\\_out.cc} 中, 注册 \\texttt{ZeroOut} op:\r\n\r\n\\begin{verbatim}\r\nREGISTER_KERNEL_BUILDER(Name(\"ZeroOut\").Device(DEVICE_CPU), ZeroOutOp);\r\n\\end{verbatim}\r\n\r\n一旦\\href{tensorflow-zh/SOURCE/get_started/os_setup.md\\#create-pip}{创建和重新安装了\r\nTensorFlow}, Tensorflow 系统可以在需要时引用和使用该 Op.\r\n\r\n\\subsection{生成客户端包装器\r\n}\\label{ux751fux6210ux5ba2ux6237ux7aefux5305ux88c5ux5668}\r\n\r\n\\subsubsection{Python Op 包装器 }\\label{python-op-ux5305ux88c5ux5668}\r\n\r\n当编译 TensorFlow 时, 所有放在\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/user_ops/}{\\texttt{tensorflow/core/user\\_ops}}\r\n目录下 的 Op 会自动在\r\n\\texttt{bazel-genfiles/tensorflow/python/ops/gen\\_user\\_ops.py} 文件\r\n中生成 Python Op 包装器. 通过以下声明, 把那些 Op 引入到\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/user_ops/user_ops.py}{\\texttt{tensorflow/python/user\\_ops/user\\_ops.py}}\r\n中:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ImportTok{from} \\NormalTok{tensorflow.python.ops.gen_user_ops }\\ImportTok{import} \\OperatorTok{*}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n你可以选择性将部分函数替换为自己的实现. 为此, 首先要隐藏自动生成的代码,\r\n在\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/BUILD}{\\texttt{tensorflow/python/BUILD}}\r\n文件中, 将其名字添加到 \\texttt{\"user\\_ops\"} 的 \\texttt{hidden} 列表.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{tf_gen_op_wrapper_py(}\r\n    \\NormalTok{name }\\OperatorTok{=} \\StringTok{\"user_ops\"}\\NormalTok{,}\r\n    \\NormalTok{hidden }\\OperatorTok{=} \\NormalTok{[}\r\n        \\StringTok{\"Fact\"}\\NormalTok{,}\r\n    \\NormalTok{],}\r\n    \\NormalTok{require_shape_functions }\\OperatorTok{=} \\VariableTok{False}\\NormalTok{,}\r\n\\NormalTok{)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n紧接着 \\texttt{\"Fact\"} 列出自己的 Op. 然后, 在\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/user_ops/user_ops.py}{\\texttt{tensorflow/python/user\\_ops/user\\_ops.py}}\r\n中添加你的替代实现函数. 通常, 替代实现函数也会调用自动生成函数来真正把\r\nOp 添加 到图中. 被隐藏的自动生成函数位于 \\texttt{gen\\_user\\_ops} 包中,\r\n名称多了一个下划线前缀 (``\\texttt{\\_}''). 例如:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\KeywordTok{def} \\NormalTok{my_fact():}\r\n    \\CommentTok{\"\"\"覆盖一个 Op 自动生成代码的示例.\"\"\"}\r\n    \\ControlFlowTok{return} \\NormalTok{gen_user_ops._fact()}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsubsection{C++ Op 包装器 }\\label{c-op-ux5305ux88c5ux5668}\r\n\r\n当编译 TensorFlow 时, 所有\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/user_ops/}{\\texttt{tensorflow/core/user\\_ops}}\r\n文件夹 下的 Op 会自动创建 C++ Op 包装器. 例如,\r\n\\texttt{tensorflow/core/user\\_ops/zero\\_out.cc} 中的 Op 会自动在\r\n\\texttt{bazel-genfiles/tensorflow/cc/ops/user\\_ops.\\{h,cc\\}}\r\n中生成包装器.\r\n\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/cc/ops/standard_ops.h}{\\texttt{tensorflow/cc/ops/standard\\_ops.h}}\r\n通过下述申明, 导入用户自定义 Op 自动生成的包装器.\r\n\r\n\\begin{verbatim}\r\n #include \"tensorflow/cc/ops/user_ops.h\"\r\n\\end{verbatim}\r\n\r\n\\subsection{检查 Op 能否正常工作\r\n}\\label{ux68c0ux67e5-op-ux80fdux5426ux6b63ux5e38ux5de5ux4f5c}\r\n\r\n验证已经成功实现 Op 的方式是编写测试程序. 创建文件\r\n\\texttt{tensorflow/python/kernel\\_tests/zero\\_out\\_op\\_test.py},\r\n包含以下内容:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ImportTok{import} \\NormalTok{tensorflow }\\ImportTok{as} \\NormalTok{tf}\r\n\\KeywordTok{class} \\NormalTok{ZeroOutTest(tf.test.TestCase):}\r\n  \\KeywordTok{def} \\NormalTok{testZeroOut(}\\VariableTok{self}\\NormalTok{):}\r\n    \\ControlFlowTok{with} \\VariableTok{self}\\NormalTok{.test_session():}\r\n      \\NormalTok{result }\\OperatorTok{=} \\NormalTok{tf.user_ops.zero_out([}\\DecValTok{5}\\NormalTok{, }\\DecValTok{4}\\NormalTok{, }\\DecValTok{3}\\NormalTok{, }\\DecValTok{2}\\NormalTok{, }\\DecValTok{1}\\NormalTok{])}\r\n      \\VariableTok{self}\\NormalTok{.assertAllEqual(result.}\\BuiltInTok{eval}\\NormalTok{(), [}\\DecValTok{5}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{0}\\NormalTok{])}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n然后运行测试:\r\n\r\n\\begin{verbatim}\r\n$ bazel test tensorflow/python:zero_out_op_test\r\n\\end{verbatim}\r\n\r\n\\subsection{验证条件 }\\label{ux9a8cux8bc1ux6761ux4ef6}\r\n\r\n上述示例假定 Op 能够应用在任何 shape 的 tensor 上. 如果只想应用到 vector\r\n上 呢? 这意味需要在上述 OpKernel 实现中添加相关的检查.\r\n\r\n\\begin{verbatim}\r\n  void Compute(OpKernelContext* context) override {\r\n   // 获取输入 tensor\r\n    const Tensor& input_tensor = context->input(0);\r\n    OP_REQUIRES(context, TensorShapeUtils::IsVector(input_tensor.shape()),\r\n                errors::InvalidArgument(\"ZeroOut expects a 1-D vector.\"));\r\n    // ...\r\n  }\r\n\\end{verbatim}\r\n\r\nOP\\_REQUIRES 断言的输入是一个 vector, 如果不是 vector, 将设置\r\n\\texttt{InvalidArgument} 状态并返回.\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/lib/core/errors.h}{\\texttt{OP\\_REQUIRES}\r\n宏} 有三个参数:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{context}: 可以是一个 \\texttt{OpKernelContext} 或\r\n  \\texttt{OpKernelConstruction} 指针 (参见\r\n  \\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op_kernel.h}{\\texttt{tensorflow/core/framework/op\\_kernel.h}}),\r\n  其 \\texttt{SetStatus()} 方法将被使用到.\r\n\\item\r\n  检查条件:\r\n  \\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/public/tensor_shape.h}{\\texttt{tensorflow/core/public/tensor\\_shape.h}}\r\n  中有一些验证 tensor shape 的函数.\r\n\\item\r\n  条件不满足时产生的错误: 错误用一个 \\texttt{Status} 对象表示, 参见\r\n  \\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/public/status.h}{\\texttt{tensorflow/core/public/status.h}}.\r\n  \\texttt{Status} 包含一个类型 (通常是 \\texttt{InvalidArgument},\r\n  但也可以是任何类型) 和一个消息. 构造 一个错误的函数位于\r\n  \\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/lib/core/errors.h}{\\texttt{tensorflow/core/lib/core/errors.h}}\r\n  中.\r\n\\end{itemize}\r\n\r\n如果想要测试一个函数返回的 \\texttt{Status} 对象是否是一个错误, 可以使用\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/lib/core/errors.h}{\\texttt{OP\\_REQUIRES\\_OK}}.\r\n这些宏如果检测到错误, 会直接跳出函数, 终止函数执行.\r\n\r\n\\subsection{Op 注册 }\\label{op-ux6ce8ux518c}\r\n\r\n\\subsubsection{属性 }\\label{ux5c5eux6027}\r\n\r\nOp 可以有属性, 属性的值在 Op 添加到图中时被设置. 属性值用于配置 Op, 在\r\nkernel 实现中, Op 注册的输入和输出类型中, 均可访问这些属性值.\r\n尽可能地使用输入代替属性, 因为输入的灵活性更高, 例如可以在执行步骤中\r\n中被更改, 可以使用 feed 等等. 属性可用于实现一些输入无法做到的事情,\r\n例如影响 Op 签名 (即输入输出的数量和类型)\r\n的配置或只读配置可以通过属性实现.\r\n\r\n注册 Op 时可以用 \\texttt{Attr} 方法指定属性的名称和类型,\r\n以此来定义一个属性, 形式如下:\r\n\r\n\\begin{verbatim}\r\n<name>: <attr-type-expr>\r\n\\end{verbatim}\r\n\r\n\\texttt{\\textless{}name\\textgreater{}} 必须以字母开头, 可以由数字, 字母,\r\n下划线组成. \\texttt{\\textless{}attr-type-expr\\textgreater{}}\r\n是一个类型表达式, 形式\\protect\\hyperlink{attr-types}{如下}:\r\n\r\n例如, 如果想要 \\texttt{ZeroOut} Op 保存一个用户索引, 指示该 Op\r\n不仅仅只有一个元素, 你可以注册 Op 如下:\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Attr(\"preserve_index: int\")\r\n    .Input(\"to_zero: int32\")\r\n    .Output(\"zeroed: int32\");\r\n\\end{verbatim}\r\n\r\n你的 kernel 可以在构造函数里, 通过 \\texttt{context} 参数访问这个属性:\r\n\r\n\\begin{verbatim}\r\nclass ZeroOutOp : public OpKernel {\r\n public:\r\n  explicit ZeroOutOp(OpKernelConstruction * context) : OpKernel(context) {\r\n   // 获取欲保存的索引值\r\n    OP_REQUIRES_OK(context,\r\n                   context->GetAttr(\"preserve_index\", &preserve_index_));\r\n    // 检查 preserve_index 是否为正\r\n    OP_REQUIRES(context, preserve_index_ >= 0,\r\n                errors::InvalidArgument(\"Need preserve_index >= 0, got \",\r\n                                        preserve_index_));\r\n  }\r\n  void Compute(OpKernelContext* context) override {\r\n    // ...\r\n}\r\n private:\r\n  int preserve_index_;\r\n};\r\n\\end{verbatim}\r\n\r\n该值可以在 \\texttt{Compute} 方法中被使用:\r\n\r\n\\begin{verbatim}\r\nvoid Compute(OpKernelContext* context) override {\r\n    // ...\r\n   // 检查 preserve_index 范围是否合法\r\nOP_REQUIRES(context, preserve_index_ < input.dimension(0),\r\n                errors::InvalidArgument(\"preserve_index out of range\"));\r\n    // 设置输出 tensor 所有的元素值为 0\r\n   const int N = input.size();\r\n    for (int i = 0; i < N; i++) {\r\n      output_flat(i) = 0;\r\n    }\r\n    // 保存请求的输入值\r\n   output_flat(preserve_index_) = input(preserve_index_);\r\n  }\r\n\\end{verbatim}\r\n\r\n\\begin{quote}\r\n为了维持\\protect\\hyperlink{backwards-compatibility}{向后兼容性},\r\n将一个属性添加到一个已有的 Op 时,\r\n必须指定一个\\protect\\hyperlink{default-values-constraints}{默认值}:\r\n\\end{quote}\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"ZeroOut\")\r\n     .Attr(\"preserve_index: int = 0\")\r\n     .Input(\"to_zero: int32\")\r\n     .Output(\"zeroed: int32\");\r\n\\end{verbatim}\r\n\r\n\\subsubsection{属性类型 }\\label{ux5c5eux6027ux7c7bux578b}\r\n\r\n属性可以使用下面的类型:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{string}: 任何二进制字节流 (UTF8 不是必须的).\r\n\\item\r\n  \\texttt{int}: 一个有型整数.\r\n\\item\r\n  \\texttt{float}: 一个浮点数.\r\n\\item\r\n  \\texttt{bool}: 真或假.\r\n\\item\r\n  \\texttt{type}:\r\n  \\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/types.cc\\#DataTypeString}{\\texttt{DataType}}\r\n  非引用类型之一.\r\n\\item\r\n  \\texttt{shape}: 一个\r\n  \\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/tensor_shape.proto}{\\texttt{TensorShapeProto}}.\r\n\\item\r\n  \\texttt{tensor}: 一个\r\n  \\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/tensor.proto}{\\texttt{TensorProto}}.\r\n\\item\r\n  \\texttt{list(\\textless{}type\\textgreater{})}:\r\n  \\texttt{\\textless{}type\\textgreater{}} 列表, 其中\r\n  \\texttt{\\textless{}type\\textgreater{}} 是上述类型之一. 注意\r\n  \\texttt{list(list(\\textless{}type\\textgreater{}))} 是无效的.\r\n\\end{itemize}\r\n\r\n权威的列表以\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op_def_builder.cc\\#FinalizeAttr}{\\texttt{op\\_def\\_builder.cc:FinalizeAttr}}\r\n为准.\r\n\r\n\\paragraph{默认值和约束条件\r\n}\\label{ux9ed8ux8ba4ux503cux548cux7ea6ux675fux6761ux4ef6}\r\n\r\n属性可能有默认值, 一些类型的属性可以有约束条件.\r\n为了定义一个有约束条件的属性, 你可以使用下列的\r\n\\texttt{\\textless{}attr-type-expr\\textgreater{}} 形式:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{\\{\\textquotesingle{}\\textless{}string1\\textgreater{}\\textquotesingle{},\\ \\textquotesingle{}\\textless{}string2\\textgreater{}\\textquotesingle{}\\}}:\r\n  属性值必须是一个字符串, 取值可以为\r\n  \\texttt{\\textless{}string1\\textgreater{}} 或\r\n  \\texttt{\\textless{}string2\\textgreater{}}.\r\n  值的语法已经暗示了值的类型为 \\texttt{string}, 已经暗示了.\r\n  下述语句模拟了一个枚举值:\r\n\\end{itemize}\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"EnumExample\")\r\n      .Attr(\"e: {'apple', 'orange'}\");\r\n\\end{verbatim}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{\\{\\textless{}type1\\textgreater{},\\ \\textless{}type2\\textgreater{}\\}}:\r\n  值是 \\texttt{type} 类型, 且必须为\r\n  \\texttt{\\textless{}type1\\textgreater{}} 或\r\n  \\texttt{\\textless{}type2\\textgreater{}} 之一, 当然\r\n  \\texttt{\\textless{}type1\\textgreater{}} 和\r\n  \\texttt{\\textless{}type2\\textgreater{}} 必须都是有效的\r\n  \\href{tensorflow-zh/SOURCE/resources/dims_types.md\\#data-types}{tensor\r\n  类型}. 你无须指定属性的类型为 \\texttt{type}, 而是通过 \\texttt{\\{...\\}}\r\n  语句给出一个类型列表. 例如, 在下面的例子里, 属性 \\texttt{t}\r\n  的类型必须为 \\texttt{int32}, \\texttt{float}, 或 \\texttt{bool}:\r\n\\end{itemize}\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"RestrictedTypeExample\")\r\n      .Attr(\"t: {int32, float, bool}\");\r\n\\end{verbatim}\r\n\r\n\\begin{itemize}\r\n\\item\r\n  这里有一些常见类型约束条件的快捷方式:\r\n\\item\r\n  \\texttt{numbertype}: 限制类型为数字类型, 即非 string 非 bool 的类型.\r\n\\item\r\n  \\texttt{realnumbertype}: 与 \\texttt{numbertype} 区别是不支持复杂类型.\r\n\\item\r\n  \\texttt{quantizedtype}: 与 \\texttt{numbertype} 区别是只支持量化数值\r\n  (quantized number type).\r\n\\end{itemize}\r\n\r\n这些类型的列表在\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/types.h}{\\texttt{tensorflow/core/framework/types.h}}\r\n文件中通过函数定义 (如 \\texttt{NumberTypes()}). 本例中属性 \\texttt{t}\r\n必须为某种数字类型:\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"NumberType\")\r\n        .Attr(\"t: numbertype\");\r\n\\end{verbatim}\r\n\r\n对于这个 Op:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{tf.number_type(t}\\OperatorTok{=}\\NormalTok{tf.int32)  }\\CommentTok{# 有效}\r\n\\NormalTok{tf.number_type(t}\\OperatorTok{=}\\NormalTok{tf.}\\BuiltInTok{bool}\\NormalTok{)   }\\CommentTok{# 无效}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{int\\ \\textgreater{}=\\ \\textless{}n\\textgreater{}}:\r\n  值必须是一个整数, 且取值大于等于 \\texttt{\\textless{}n\\textgreater{}},\r\n  \\texttt{\\textless{}n\\textgreater{}} 是一个自然数.\r\n\\end{itemize}\r\n\r\n例如, 下列 Op 注册操作指定了属性 \\texttt{a} 的取值至少为 \\texttt{2}.\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"MinIntExample\")\r\n      .Attr(\"a: int >= 2\");\r\n\\end{verbatim}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{list(\\textless{}type\\textgreater{})\\ \\textgreater{}=\\ \\textless{}n\\textgreater{}}:\r\n  一个 \\texttt{\\textless{}type\\textgreater{}} 类型列表,\r\n  列表长度必须大于等于 \\texttt{\\textless{}n\\textgreater{}}.\r\n\\end{itemize}\r\n\r\n例如, 下面的 Op 注册操作指定属性 \\texttt{a} 是一个列表,\r\n列表中的元素类型是 \\texttt{int32} 或 \\texttt{float}列表长度至少为3.\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"TypeListExample\")\r\n      .Attr(\"a: list({int32, float}) >= 3\");\r\n\\end{verbatim}\r\n\r\n通过添加 \\texttt{=\\ \\textless{}default\\textgreater{}} 到约束条件末尾,\r\n给一个属性设置默认值 (使其在自动生成的代码里 变成可选属性), 如下:\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"AttrDefaultExample\")\r\n    .Attr(\"i: int = 0\");\r\n\\end{verbatim}\r\n\r\n默认值支持的语法将在最终 GraphDef 定义的 protobuf 表示中被使用.\r\n\r\n下面是给所有类型赋予默认值的例子:\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"AttrDefaultExampleForAllTypes\")\r\n   .Attr(\"s: string = 'foo'\")\r\n   .Attr(\"i: int = 0\")\r\n   .Attr(\"f: float = 1.0\")\r\n   .Attr(\"b: bool = true\")\r\n   .Attr(\"ty: type = DT_INT32\")\r\n   .Attr(\"sh: shape = { dim { size: 1 } dim { size: 2 } }\")\r\n   .Attr(\"te: tensor = { dtype: DT_INT32 int_val: 5 }\")\r\n   .Attr(\"l_empty: list(int) = []\")\r\n   .Attr(\"l_int: list(int) = [2, 3, 5, 7]\");\r\n\\end{verbatim}\r\n\r\n请特别注意那些类型值里面包含的\r\n\\href{tensorflow-zh/SOURCE/resources/dims_types.md\\#data-types}{\\texttt{DT\\_*}\r\n名称}.\r\n\r\n\\subsubsection{多态 }\\label{ux591aux6001}\r\n\r\n\\hypertarget{type-polymorphism}{\\paragraph{Type Polymorphism\r\n}\\label{type-polymorphism}}\r\n\r\n对于那些可以使用不同类型输入或产生不同类型输出的 Op, 可以注册 Op\r\n时为输入/输出类型里指定一个\\protect\\hyperlink{attrs}{属性}. 一般紧接着,\r\n会为每一个支持的类型注册一个 \\texttt{OpKernel}.\r\n\r\n例如, 除了 \\texttt{int32} 外, 想要 \\texttt{ZeroOut} Op 支持\r\n\\texttt{float}, 注册代码如下:\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Attr(\"T: {float, int32}\")\r\n    .Input(\"to_zero: <b>T</b>\")\r\n    .Output(\"zeroed: <b>T</b>\");\r\n\\end{verbatim}\r\n\r\n这段 Op 注册代码现在指定了输入的类型必须为 \\texttt{float} 或\r\n\\texttt{int32}, 而且 既然输入和输出制定了同样的类型 \\texttt{T},\r\n输出也同样如此.\r\n\r\n\\begin{quote}\r\n一个命名建议:\\{\\#naming\\} 输入, 输出, 和属性通常使用 snake\\_case 命名法.\r\n唯一的例外是属性被用作输入类型或是输入类型的一部分. 当添加到图中时,\r\n这些属性 可以被推断出来, 因此不会出现在 Op 的函数里. 例如, 最后一个\r\nZeroOut 定义 生成的 Python 函数如下:\r\n\\end{quote}\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\KeywordTok{def} \\NormalTok{zero_out(to_zero, name}\\OperatorTok{=}\\VariableTok{None}\\NormalTok{):}\r\n   \\CommentTok{\"\"\"...}\r\n\\CommentTok{   参数:}\r\n\\CommentTok{     to_zero: 一个 `Tensor`. 必须为下列类型之一:}\r\n\\CommentTok{         `float32`, `int32`.}\r\n\\CommentTok{     name: 操作的名字 (可选).}\r\n\r\n\\CommentTok{   返回值:}\r\n\\CommentTok{     一个 `Tensor`, 类型和 `to_zero` 一样.}\r\n\\CommentTok{   \"\"\"}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\begin{quote}\r\n如果输入的 \\texttt{to\\_zero} 是一个 \\texttt{int32} 的tensor, 然后\r\n\\texttt{T} 将被自动 设置为 \\texttt{int32} (实际上是 \\texttt{DT\\_INT32}).\r\n那些推导出的属性的名称字母全大写 或采用驼峰命名法.\r\n\r\n下面是一个输出类型自动推断的例子, 读者可以对比一下:\r\n\\end{quote}\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"StringToNumber\")\r\n     .Input(\"string_tensor: string\")\r\n     .Output(\"output: out_type\")\r\n     .Attr(\"out_type: {float, int32}\");\r\n     .Doc(R\"doc(\r\n Converts each string in the input Tensor to the specified numeric type.\r\n )doc\");\r\n\\end{verbatim}\r\n\r\n\\begin{quote}\r\n在这种情况下, 用户需要在生成的 Python 代码中指定输出类型.\r\n\\end{quote}\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\KeywordTok{def} \\NormalTok{string_to_number(string_tensor, out_type}\\OperatorTok{=}\\VariableTok{None}\\NormalTok{, name}\\OperatorTok{=}\\VariableTok{None}\\NormalTok{):}\r\n   \\CommentTok{\"\"\"将输入 Tensor 中的每一个字符串转化成指定的数字类型}\r\n\r\n\\CommentTok{   参数:}\r\n\\CommentTok{     string_tensor: 一个 `string` 类型的 `Tensor`.}\r\n\\CommentTok{     out_type: 一个可选的 `tf.DType`, 取值为 `tf.float32, tf.int32`.}\r\n\\CommentTok{       默认值是 `tf.float32`.}\r\n\\CommentTok{     name: 操作的名称 (可选).}\r\n\r\n\\CommentTok{   返回值:}\r\n\\CommentTok{     一个 `out_type` 类型的 `Tensor`.}\r\n\\CommentTok{   \"\"\"}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\begin{verbatim}\r\n #include \"tensorflow/core/framework/op_kernel.h\"\r\nclass ZeroOutInt32Op : public OpKernel {\r\n  // 和之前一样\r\n};\r\nclass ZeroOutFloatOp : public OpKernel {\r\n public:\r\n  explicit ZeroOutFloatOp(OpKernelConstruction * context)\r\n      : OpKernel(context) {}\r\n  void Compute(OpKernelContext * context) override {\r\n    // 获取输入 tensor\r\n    const Tensor& input_tensor = context->input(0);\r\n    auto input = input_tensor.flat<float>();\r\n    // 创建一个输出 tensor\r\n    Tensor * output = NULL;\r\n    OP_REQUIRES_OK(context,\r\n                    context->allocate_output(0, input_tensor.shape(), &output));\r\n    auto output_flat = output->template flat<float>();\r\n    // 设置输出 tensor 的所有元素为 0\r\n    const int N = input.size();\r\n    for (int i = 0; i &lt; N; i++) {\r\n      output_flat(i) = 0;\r\n    }<br/>\r\n    // 保留第一个输入值\r\n    if (N &gt; 0) output_flat(0) = input(0);\r\n  }\r\n};\r\n// 注意, TypeConstraint<int32>(\"T\") 意味着属性 \"T\" (在上面 Op 注册代码中\r\n// 定义的) 必须是 \"int32\", 才能实例化.\r\nREGISTER_KERNEL_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE_CPU)\r\n    .TypeConstraint&lt;int32&gt;(\"T\"),\r\n    ZeroOutOpInt32);\r\nREGISTER_KERNEL_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE_CPU)\r\n    .TypeConstraint<float>(\"T\"),\r\n    ZeroOutFloatOp);\r\n\\end{verbatim}\r\n\r\n\\begin{quote}\r\n为了保持\\protect\\hyperlink{backwards-compatibility}{向后兼容性},\r\n你在为一个 已有的 op 添加属性时,\r\n必须指定一个\\protect\\hyperlink{default-values-constraints}{默认值}:\r\n\\end{quote}\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"ZeroOut\")\r\n  .Attr(\"T: {float, int32} = DT_INT32\")\r\n  .Input(\"to_zero: T\")\r\n  .Output(\"zeroed: T\")\r\n\\end{verbatim}\r\n\r\n如果需要添加更多类型, 例如 \\texttt{double}:\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Attr(\"T: {float, double, int32}\")\r\n    .Input(\"to_zero: T\")\r\n    .Output(\"zeroed: T\");\r\n\\end{verbatim}\r\n\r\n为了避免为新增的类型写冗余的 \\texttt{OpKernel} 代码, 通常可以写一个 C++\r\n模板作为替代. 当然, 仍然需要为每一个重载版本定义一个 keneral 注册\r\n(\\texttt{REGISTER\\textbackslash{}\\_KERNEL\\textbackslash{}\\_BUILDER}\r\n调用).\r\n\r\n\\begin{verbatim}\r\ntemplate <typename T>;\r\nclass ZeroOutOp : public OpKernel {\r\n public:\r\n    explicit ZeroOutOp(OpKernelConstruction* context) : OpKernel(context) {}\r\n  void Compute(OpKernelContext* context) override {\r\n    // 获取输入 tensor\r\n     const Tensor& input_tensor = context->input(0);\r\n    auto input = input_tensor.flat<T>();\r\n    // 创建一个输出 tensor\r\n      Tensor* output = NULL;\r\n    OP_REQUIRES_OK(context,\r\n                   context->allocate_output(0, input_tensor.shape(), &output));\r\n    auto output_flat = output->template flat<T>();\r\n    // 设置输出 tensor 的所有元素为 0\r\n   const int N = input.size();\r\n    for (int i = 0; i < N; i++) {\r\n      output_flat(i) = 0;\r\n    }\r\n    // Preserve the first input value\r\n    if (N > 0) output_flat(0) = input(0);\r\n  }\r\n};\r\n};<br/>\r\n// 注意, TypeConstraint<int32>(\"T\") 意味着属性 \"T\" (在上面 Op 注册代码中\r\n// 定义的) 必须是 \"int32\", 才能实例化. </b>\r\nREGISTER_KERNEL_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE_CPU)\r\n    .TypeConstraint<int32>(\"T\"),\r\n    ZeroOutOp<int32>);\r\nREGISTER_KERNEL_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE_CPU)\r\n    .TypeConstraint<float>(\"T\"),\r\n    ZeroOutOp<float>);\r\nREGISTER_KERNEL_BUILDER(\r\n    Name(\"ZeroOut\")\r\n    .Device(DEVICE_CPU)\r\n    .TypeConstraint<double>(\"T\"),\r\n    ZeroOutOp<double>);\r\n\\end{verbatim}\r\n\r\n如果有很多重载版本, 可以将注册操作通过一个宏来实现.\r\n\r\n\\begin{verbatim}\r\n #include \"tensorflow/core/framework/op_kernel.h\"\r\n #define REGISTER_KERNEL(type)                                       \\\r\n  REGISTER_KERNEL_BUILDER(                                          \\\r\n      Name(\"ZeroOut\").Device(DEVICE_CPU).TypeConstraint<type>(\"T\"), \\\r\n      ZeroOutOp<type>)\r\nREGISTER_KERNEL(int32);\r\nREGISTER_KERNEL(float);\r\nREGISTER_KERNEL(double);\r\n #undef REGISTER_KERNEL\r\n\\end{verbatim}\r\n\r\n取决于注册 kernel 使用哪些类型,\r\n你可能可以使用\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/register_types.h}{\\texttt{tensorflow/core/framework/register\\_types.h}}\r\n提供的宏:\r\n\r\n\\begin{verbatim}\r\n #include \"tensorflow/core/framework/op_kernel.h\"\r\n #include \"tensorflow/core/framework/register_types.h\"\r\nREGISTER_OP(\"ZeroOut\")\r\n    .Attr(\"T: realnumbertype\")\r\n    .Input(\"to_zero: T\")\r\n    .Output(\"zeroed: T\");\r\ntemplate <typename T>\r\nclass ZeroOutOp : public OpKernel { ... };\r\n #define REGISTER_KERNEL(type)                                       \\\r\n  REGISTER_KERNEL_BUILDER(                                          \\\r\n      Name(\"ZeroOut\").Device(DEVICE_CPU).TypeConstraint<type>(\"T\"), \\\r\n      ZeroOutOp<type>)\r\nTF_CALL_REAL_NUMBER_TYPES(REGISTER_KERNEL);\r\n #undef REGISTER_KERNEL\r\n\\end{verbatim}\r\n\r\n\\paragraph{列表输入和输出\r\n}\\label{ux5217ux8868ux8f93ux5165ux548cux8f93ux51fa}\r\n\r\n除了能够使用不同类型的 tensor 作为输入或输出, Op 还支持使用多个 tensor\r\n作为输入或输出.\r\n\r\n在接下来的例子里, 属性 \\texttt{T} 存储了一个类型\\emph{列表},\r\n并同时作为输入 \\texttt{in} 和输出 \\texttt{out} 的类型.\r\n输入和输出均为指定类型的 tensor 列表. 既然输入和输出的类型均为\r\n\\texttt{T}, 它们的 tensor 数量和类型 是一致的.\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"PolymorphicListExample\")\r\n    .Attr(\"T: list(type)\")\r\n    .Input(\"in: T\")\r\n    .Output(\"out: T\");\r\n\\end{verbatim}\r\n\r\n可以为列表中可存放的类型设置约束条件. 在下一个例子中, 输入是\r\n\\texttt{float} 和 \\texttt{double} 类型的 tensor 列表. 例如, 这个 Op\r\n可接受的 输入类型为 \\texttt{(float,\\ double,\\ float)} 的数据,\r\n且在此情况下, 输出类型同样 为 \\texttt{(float,\\ double,\\ float)}.\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"ListTypeRestrictionExample\")\r\n    .Attr(\"T: list({float, double})\")\r\n    .Input(\"in: T\")\r\n    .Output(\"out: T\");\r\n\\end{verbatim}\r\n\r\n如果想要一个列表中的所有 tensor 是同一类型, 你需要写下列代码:\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"IntListInputExample\")\r\n    .Attr(\"N: int\")\r\n    .Input(\"in: N * int32\")\r\n    .Output(\"out: int32\");\r\n\\end{verbatim}\r\n\r\n这段代码接受 \\texttt{int32} tensor 列表, 并用一个 \\texttt{int} 属性\r\n\\texttt{N} 来指定列表的长度.\r\n\r\n这也可用于\\protect\\hyperlink{type-polymorphism}{类型推断}.\r\n在下一个例子中, 输入是一个 tensor 列表, 长度为 \\texttt{\"N\"}, 类型为\r\n\\texttt{\"T\"}, 输出是单个 \\texttt{\"T\"} 的 tensor:\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"SameListInputExample\")\r\n    .Attr(\"N: int\")\r\n    .Attr(\"T: type\")\r\n    .Input(\"in: N * T\")\r\n    .Output(\"out: T\");\r\n\\end{verbatim}\r\n\r\n默认情况下, tensor 列表的最小长度为1. 这个约束条件可以通过\r\n\\protect\\hyperlink{default-values-constraints}{为指定的属性增加一个\r\n\\texttt{\"\\textgreater{}=\"} 约束}来变更:\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"MinLengthIntListExample\")\r\n    .Attr(\"N: int >= 2\")\r\n    .Input(\"in: N * int32\")\r\n    .Output(\"out: int32\");\r\n\\end{verbatim}\r\n\r\n同样的语法也适用于 \\texttt{\"list(type)\"} 属性:\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"MinimumLengthPolymorphicListExample\")\r\n    .Attr(\"T: list(type) >= 3\")\r\n    .Input(\"in: T\")\r\n    .Output(\"out: T\");\r\n\\end{verbatim}\r\n\r\n\\subsubsection{输入和输出 }\\label{ux8f93ux5165ux548cux8f93ux51fa}\r\n\r\n总结一下上述内容, 一个 Op 注册操作可以指定多个输入和输出:\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"MultipleInsAndOuts\")\r\n    .Input(\"y: int32\")\r\n    .Input(\"z: float\")\r\n    .Output(\"a: string\")\r\n    .Output(\"b: int32\");\r\n\\end{verbatim}\r\n\r\n每一个输入或输出形式如下:\r\n\r\n\\begin{verbatim}\r\n<name>: <io-type-expr>\r\n\\end{verbatim}\r\n\r\n其中, \\texttt{\\textless{}name\\textgreater{}} 以字母打头, 且只能由数字,\r\n字母和下划线组成. \\texttt{\\textless{}io-type-expr\\textgreater{}} 可以是\r\n下列类型表达式之一:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{\\textless{}type\\textgreater{}}, 一个合法的输入类型, 如\r\n  \\texttt{float}, \\texttt{int32}, \\texttt{string}.\r\n  这可用于指定给定类型的单个 tensor.\r\n\\end{itemize}\r\n\r\n参见\\href{tensorflow-zh/SOURCE/resources/dims_types.md\\#data-types}{合法\r\nTensor 类型列表}.\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"BuiltInTypesExample\")\r\n      .Input(\"integers: int32\")\r\n      .Input(\"complex_numbers: scomplex64\");\r\n\\end{verbatim}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{\\textless{}attr-type\\textgreater{}},\r\n  一个\\protect\\hyperlink{attrs}{属性}和一个类型 \\texttt{type} 或类型列表\r\n  \\texttt{list(type)}(可能 包含类型限制).\r\n  该语法可实现\\protect\\hyperlink{Polymorphism}{多态 Op}.\r\n\\end{itemize}\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"PolymorphicSingleInput\")\r\n      .Attr(\"T: type\")\r\n      .Input(\"in: T);\r\nREGISTER_OP(\"RestrictedPolymorphicSingleInput\")\r\n      .Attr(\"T: {int32, int64}\")\r\n      .Input(\"in: T);\r\n\\end{verbatim}\r\n\r\n将属性的类型设置为 \\texttt{list(type)} 将允许你接受一个序列的 tensor.\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"ArbitraryTensorSequenceExample\")\r\n      .Attr(\"T: list(type)\")\r\n      .Input(\"in: T\")\r\n      .Output(\"out: T\");\r\nREGISTER_OP(\"RestrictedTensorSequenceExample\")\r\n      .Attr(\"T: list({int32, int64})\")\r\n      .Input(\"in: T\")\r\n      .Output(\"out: T\");\r\n\\end{verbatim}\r\n\r\n注意, 输入和输出均为 \\texttt{T}, 意味着输入和输出的类型与数量均相同.\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{\\textless{}number\\textgreater{}\\ *\\ \\textless{}type\\textgreater{}},\r\n  一组拥有相同类型的 tensor, \\texttt{\\textless{}number\\textgreater{}}\r\n  是一个 \\texttt{int} 类型属性的名称.\r\n  \\texttt{\\textless{}type\\textgreater{}}\r\n  可以是\\href{tensorflow-zh/SOURCE/resources/dims_types.md\\#data-types}{一个类似于\r\n  \\texttt{int32} 和 \\texttt{float} 的特定类型}, 或者一个 \\texttt{type}\r\n  类型属性的名字. 前者的例子如下, 该例子接受一个 \\texttt{int32} tensor\r\n  列表作为 Op 输入:\r\n\\end{itemize}\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"Int32SequenceExample\")\r\n      .Attr(\"NumTensors: int\")\r\n      .Input(\"in: NumTensors * int32\")\r\n\\end{verbatim}\r\n\r\n后者的例子如下, 该例子接受一个泛型 tensor 列表作为 Op 输入:\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"SameTypeSequenceExample\")\r\n      .Attr(\"NumTensors: int\")\r\n      .Attr(\"T: type\")\r\n      .Input(\"in: NumTensors * T\")\r\n\\end{verbatim}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  Tensor 的引用表示为 \\texttt{Ref(\\textless{}type\\textgreater{})}, 其中\r\n  \\texttt{\\textless{}type\\textgreater{}} 是上述类型之一.\r\n\\end{itemize}\r\n\r\n\\begin{quote}\r\n一个命名建议: 当使用属性表示一个输入的类型时, 该类型可以被推断出来.\r\n实现该特性, 将需要推断 的类型用大写名称表示 (如 \\texttt{T} 或\r\n\\texttt{N}), 其它的输入, 输出, 和属性像使用函数参数一样使用这些\r\n大写名称.\r\n参见之前的\\protect\\hyperlink{naming}{命名建议}章节查看更多细节.\r\n\\end{quote}\r\n\r\n更多细节参见\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op_def_builder.h}{\\texttt{tensorflow/core/framework/op\\_def\\_builder.h}}.\r\n\r\n\\subsubsection{向后兼容性 }\\label{ux5411ux540eux517cux5bb9ux6027}\r\n\r\n通常, 对规范的改变必须保持向后兼容性: Op 使用新规范后,\r\n需保证使用旧规范构造的序列化 GraphDef 仍能正确工作.\r\n\r\n下面是几种保持向后兼容性的方式:\r\n\r\n\\begin{enumerate}\r\n\\def\\labelenumi{\\arabic{enumi}.}\r\n\\tightlist\r\n\\item\r\n  任何添加到 Op 的新属性必须有默认值, 且默认值下的行为有明确定义.\r\n  将一个非多态的操作变为多态操作, 你\\emph{必须}为新的类型属性赋予默认值,\r\n  以保持原始的函数签名. 例如, 有如下操作:\r\n\\end{enumerate}\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"MyGeneralUnaryOp\")\r\n       .Input(\"in: float\")\r\n       .Output(\"out: float\");\r\n\\end{verbatim}\r\n\r\n可以通过下述方式将其变为多态, 且保持向后兼容性:\r\n\r\n\\begin{verbatim}\r\nREGISTER_OP(\"MyGeneralUnaryOp\")\r\n       .Input(\"in: T\")\r\n       .Output(\"out: T\")\r\n       .Attr(\"T: numerictype = float\");\r\n\\end{verbatim}\r\n\r\n1.放宽一个属性的约束条件是安全的. 例如, 你可以将\r\n\\texttt{\\{int32,\\ int64\\}} 变为 \\texttt{\\{int32,\\ int64,\\ float\\}},\r\n或者, 将 \\texttt{\\{\"apple\",\\ \"orange\"\\}} 变为\r\n\\texttt{\\{\"apple\",\\ \"banana\",\\ \"orange\"\\}}.\r\n\r\n2.通过给 Op 名称添加一些项目中唯一的标识作为前缀, 来为新建的 Op\r\n添加命名空间. 命名空间 可以预防你的 Op 与 TensorFlow 未来版本里的内置 Op\r\n产生命名冲突.\r\n\r\n3.超前计划! 尝试着去预测 Op 未来的的用途, 超前设计, 毕竟,\r\n一些签名的变更无法保证兼容性 (例如, 增加新的输入,\r\n或将原来的单元素输入变成一个列表).\r\n\r\n如果不能以兼容的方式改变一个操作, 那就创建一个全新的操作,\r\n来实现所需功能.\r\n\r\n\\subsection{GPU 支持 }\\label{gpu-ux652fux6301}\r\n\r\n你可以实现不同的 OpKernel, 将其中之一注册到 GPU, 另一个注册到 GPU,\r\n正如\\protect\\hyperlink{Polymorphism}{为不同的类型注册 kernel}一样.\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/}{\\texttt{tensorflow/core/kernels/}}\r\n中有一些 GPU 支持的例子. 注意, 一些 kernel 的 CPU 版本位于 \\texttt{.cc}\r\n文件, GPU 版本位于 \\texttt{\\_gpu.cu.cc} 文件, 共享的代码位于 \\texttt{.h}\r\n文件.\r\n\r\n例如,\r\n\\href{tensorflow-zh/SOURCE/api_docs/python/array_ops.md\\#pad}{\\texttt{pad}\r\nop} 除了 GPU kernel 外的其它代码 均在\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/pad_op.cc}{\\texttt{tensorflow/core/kernels/pad\\_op.cc}}\r\n中. GPU kernel 位于\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/pad_op_gpu.cu.cc}{\\texttt{tensorflow/core/kernels/pad\\_op\\_gpu.cu.cc}},\r\n共享的一个模板类代码定义在\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/pad_op.h}{\\texttt{tensorflow/core/kernels/pad\\_op.h}}.\r\n需要注意的事情是, 即使使用 \\texttt{pad} 的 GPU 版本时, 仍然需要将\r\n\\texttt{\"paddings\"} 输入放置到内存中. 为了实现这一点,\r\n将输入或输出标记为必须保存在内存中, 为 kernel 注册一个\r\n\\texttt{HostMemory()} 调用. 如下:\r\n\r\n\\begin{verbatim}\r\n #define REGISTER_GPU_KERNEL(T)                         \\\r\nREGISTER_KERNEL_BUILDER(Name(\"Pad\")                  \\\r\n                              .Device(DEVICE_GPU)      \\\r\n                              .TypeConstraint<T>(\"T\")  \\\r\n                              .HostMemory(\"paddings\"), \\\r\n                          PadOp<GPUDevice, T>)\r\n\\end{verbatim}\r\n\r\n\\subsection{使用 Python 实现梯度\r\n}\\label{ux4f7fux7528-python-ux5b9eux73b0ux68afux5ea6}\r\n\r\n给定一个 Op 组成的图, TensorFlow 使用自动微分 (反向传播) 来添加新的 Op\r\n以表示梯度运算, 同时 不影响已有的 Op\r\n(参见\\href{tensorflow-zh/SOURCE/api_docs/python/train.md\\#gradient-computation}{梯度运算}).\r\n为了使自动微分能够与新的 Op 协同工作, 必须注册一个梯度函数, 从 Op\r\n的输入计算梯度, 并返回代表 梯度值的输出.\r\n\r\n数学上, 如果一个 Op 计算 \\textbackslash{}(y = f(x)\\textbackslash{}),\r\n注册的梯度 Op 通过以下链式法则, 将 \\textbackslash{}(\\partial /\r\n\\partial y\\textbackslash{}) 的梯度运算转化为 \\textbackslash{}(\\partial /\r\n\\partial x\\textbackslash{}) 的梯度运算.\r\n\r\n\\[\\frac{\\partial}{\\partial x}\r\n    = \\frac{\\partial}{\\partial y} \\frac{\\partial y}{\\partial x}\r\n    = \\frac{\\partial}{\\partial y} \\frac{\\partial f}{\\partial x}.\\]\r\n\r\n在 \\texttt{ZeroOut} 的例子中, 输入中只有一个项会影响输出, 所以,\r\n代表输入的梯度值的 tensor 也只有 一个输入项. 如下所示:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ImportTok{from} \\NormalTok{tensorflow.python.framework }\\ImportTok{import} \\NormalTok{ops}\r\n\\ImportTok{from} \\NormalTok{tensorflow.python.ops }\\ImportTok{import} \\NormalTok{array_ops}\r\n\\ImportTok{from} \\NormalTok{tensorflow.python.ops }\\ImportTok{import} \\NormalTok{sparse_ops}\r\n\r\n\\AttributeTok{@ops.RegisterGradient}\\NormalTok{(}\\StringTok{\"ZeroOut\"}\\NormalTok{)}\r\n\\KeywordTok{def} \\NormalTok{_zero_out_grad(op, grad):}\r\n  \\CommentTok{\"\"\"`zero_out` 的梯度.}\r\n\r\n\\CommentTok{  参数:}\r\n\\CommentTok{    op: 欲进行微分的 `zero_out` `操作`, 可以用于获取原始 Op 的输入和输出.}\r\n\\CommentTok{    grad: 代表 `zero_out` 输出的梯度 Op.}\r\n\r\n\\CommentTok{  返回:}\r\n\\CommentTok{    代表输入 `zero_out` 的微分.}\r\n\\CommentTok{  \"\"\"}\r\n  \\NormalTok{to_zero }\\OperatorTok{=} \\NormalTok{op.inputs[}\\DecValTok{0}\\NormalTok{]}\r\n  \\NormalTok{shape }\\OperatorTok{=} \\NormalTok{array_ops.shape(to_zero)}\r\n  \\NormalTok{index }\\OperatorTok{=} \\NormalTok{array_ops.zeros_like(shape)}\r\n  \\NormalTok{first_grad }\\OperatorTok{=} \\NormalTok{array_ops.reshape(grad, [}\\OperatorTok{-}\\DecValTok{1}\\NormalTok{])[}\\DecValTok{0}\\NormalTok{]}\r\n  \\NormalTok{to_zero_grad }\\OperatorTok{=} \\NormalTok{sparse_ops.sparse_to_dense(index, shape, first_grad, }\\DecValTok{0}\\NormalTok{)}\r\n  \\ControlFlowTok{return} \\NormalTok{[to_zero_grad]  }\\CommentTok{# 单个 Tensor 的列表, 既然只有一个输入}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n使用\r\n\\href{tensorflow-zh/SOURCE/api_docs/python/framework.md\\#RegisterGradient}{\\texttt{ops.RegisterGradient}}\r\n注册梯度函数需要注意的一些细节:\r\n\r\n\\begin{itemize}\r\n\\item\r\n  对于仅有一个输出的 Op, 梯度函数使用\r\n  \\href{tensorflow-zh/SOURCE/api_docs/python/framework.md\\#Operation}{\\texttt{Operation}}\r\n  \\texttt{op} 和一个\r\n  \\href{tensorflow-zh/SOURCE/api_docs/python/framework.md\\#Tensor}{\\texttt{Tensor}}\r\n  \\texttt{grad} 作为参数, 并从\r\n  \\href{tensorflow-zh/SOURCE/api_docs/python/framework.md\\#Operation.inputs}{\\texttt{op.inputs{[}i{]}}},\r\n  \\href{tensorflow-zh/SOURCE/api_docs/python/framework.md\\#Operation.outputs}{\\texttt{op.outputs{[}i{]}}},\r\n  和 \\texttt{grad} 构建新的 Op. 属性的信息可以通过\r\n  \\href{tensorflow-zh/SOURCE/api_docs/python/framework.md\\#Operation.get_attr}{\\texttt{op.get\\_attr}}\r\n  获取.\r\n\\item\r\n  如果 Op 有多个输出, 梯度函数将使用 \\texttt{op} 和 \\texttt{grads}\r\n  作为参数, 其中, \\texttt{grads} 是一个 梯度 Op 的列表,\r\n  为每一个输出计算梯度. 梯度函数的输出必须是一个 \\texttt{Tensor}\r\n  对象列表, 对应到 每一个输入的梯度.\r\n\\item\r\n  如果没有为一些输入定义梯度, 譬如用作索引的整型, 这些输入返回的梯度为\r\n  \\texttt{None}. 举一个例子, 如果一个 Op 的输入为一个浮点数 tensor\r\n  \\texttt{x} 和一个整型索引 \\texttt{i}, 那么梯度函数将返回\r\n  \\texttt{{[}x\\_grad,\\ None{]}}.\r\n\\item\r\n  如果梯度对于一个 Op 来说毫无意义, 使用\r\n  \\texttt{ops.NoGradient(\"OpName\")} 禁用自动差分.\r\n\\end{itemize}\r\n\r\n注意当梯度函数被调用时, 作用的对象是数据流图中的 Op, 而不是 tensor\r\n数据本身. 因此, 只有在图运行时, 梯度运算才会被其它 tensorflow Op\r\n的执行动作所触发.\r\n\r\n\\subsection{在 Python 中实现一个形状函数\r\n}\\label{ux5728-python-ux4e2dux5b9eux73b0ux4e00ux4e2aux5f62ux72b6ux51fdux6570}\r\n\r\nTensorFlow Python API 有一个 ``形状推断'' 功能, 可以不执行图就获取\r\ntensor 的形状信息. 形状推断功能藉由每一个 Op 类型注册的 ``形状函数''\r\n来支持, 该函数有两个规则: 假设所有输入的 形状必须是兼容的,\r\n以及指定输出的形状. 一个形状函数以一个\r\n\\href{tensorflow-zh/SOURCE/api_docs/python/framework.md\\#Operation}{\\texttt{Operation}}\r\n作为输入, 返回一个\r\n\\href{tensorflow-zh/SOURCE/api_docs/python/framework.md\\#TensorShape}{\\texttt{TensorShape}}\r\n对象列表 (每一个输出一个对象). 使用\r\n\\href{tensorflow-zh/SOURCE/api_docs/python/framework.md\\#RegisterShape}{\\texttt{tf.RegisterShape}\r\n装饰器} 注册形状函数. 例如,\r\n\\protect\\hyperlink{defineux5finterface}{上文定义的 \\texttt{ZeroOut} Op}\r\n的形状函数如下:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\AttributeTok{@tf.RegisterShape}\\NormalTok{(}\\StringTok{\"ZeroOut\"}\\NormalTok{):}\r\n\\KeywordTok{def} \\NormalTok{_zero_out_shape(op):}\r\n  \\CommentTok{\"\"\"ZeroOut Op 的形状函数.}\r\n\r\n\\CommentTok{  这是 ZeroOut 形状函数的无约束版本, 为每一个输出产生的形状和对应的输入一样. }\r\n\\CommentTok{  \"\"\"}\r\n  \\ControlFlowTok{return} \\NormalTok{[op.inputs[}\\DecValTok{0}\\NormalTok{].get_shape()]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n一个形状函数也可以约束输入的形状. 下面是\r\n\\protect\\hyperlink{Validation}{ZeroOut 形状函数的 vector 输入约束}版本:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\AttributeTok{@tf.RegisterShape}\\NormalTok{(}\\StringTok{\"ZeroOut\"}\\NormalTok{):}\r\n\\KeywordTok{def} \\NormalTok{_zero_out_shape(op):}\r\n  \\CommentTok{\"\"\"ZeroOut Op 的形状函数.}\r\n\r\n\\CommentTok{  这是 ZeroOut 形状函数的约束版本, 要输入的 rank 必须是 1 (即使一个 vector).}\r\n\\CommentTok{  \"\"\"}\r\n  \\NormalTok{input_shape }\\OperatorTok{=} \\NormalTok{op.inputs[}\\DecValTok{0}\\NormalTok{].get_shape().with_rank(}\\DecValTok{1}\\NormalTok{)}\r\n  \\ControlFlowTok{return} \\NormalTok{[input_shape]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n如果 Op 是\\protect\\hyperlink{Polymorphism}{多输入的多态 Op},\r\n使用操作的属性来决定需要检查的形状数量:\r\n\r\n\\begin{verbatim}\r\n@tf.RegisterShape(\"IntListInputExample\")\r\ndef _int_list_input_example_shape(op):\r\n  \"\"\" \"IntListInputExample\" Op 的形状函数.\r\n\r\n  所有的输入和输出是同大小的矩阵.\r\n  \"\"\"\r\n  output_shape = tf.TensorShape(None)\r\n  for input in op.inputs:\r\n    output_shape = output_shape.merge_with(input.get_shape().with_rank(2))\r\n  return [output_shape]\r\n\\end{verbatim}\r\n\r\n既然形状推断是一个可选的特性, 且 tensor 的形状可能动态变化,\r\n形状函数必须足够健壮, 能够处理任意 输入形状信息缺失的情形.\r\n\\href{tensorflow-zh/SOURCE/api_docs/python/framework.md}{\\texttt{merge\\_with}}\r\n方法能够帮助 调用者判断两个形状是否是一样的, 即使两个形状的信息不全,\r\n该函数同样有效.\r\n所有的\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/}{标准\r\nPython Op} 的形状函数都已经定义好了, 并且已经有很多不同的使用示例.\r\n\r\n\\begin{quote}\r\n原文：\\href{http://www.tensorflow.org/how_tos/adding_an_op/index.html\\#adding-a-new-op}{Adding\r\na New Op} 翻译：{[}@doc001{]}(https://github.com/PFZheng)\r\n校对：{[}@ZHNathanielLee{]}(https://github.com/ZHNathanielLee)\r\n\\end{quote}\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/how_tos/c3s08_new_data_formats.tex",
    "content": "\r\n\r\n\\section{自定义数据读取\r\n}\\label{ux81eaux5b9aux4e49ux6570ux636eux8bfbux53d6}\r\n\r\n基本要求:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  熟悉 C++ 编程。\r\n\\item\r\n  确保\\href{tensorflow-zh/SOURCE/get_started/os_setup.md\\#source}{下载\r\n  TensorFlow 源文件}, 并可编译使用。\r\n\\end{itemize}\r\n\r\n我们将支持文件格式的任务分成两部分：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  文件格式: 我们使用 \\emph{Reader} Op来从文件中读取一个 \\emph{record}\r\n  (可以使任意字符串)。\r\n\\item\r\n  记录格式:\r\n  我们使用解码器或者解析运算将一个字符串记录转换为TensorFlow可以使用的张量。\r\n\\end{itemize}\r\n\r\n例如， 读取一个\r\n\\href{https://en.wikipedia.org/wiki/Comma-separated_values}{CSV\r\n文件}，我们使用\r\n\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#TextLineReader}{一个文本读写器}，\r\n然后是\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#decode_csv}{从一行文本中解析CSV数据的运算}。\r\n\r\n\\subsection{主要内容}\\label{ux4e3bux8981ux5185ux5bb9}\r\n\r\n\\subsubsection{\\texorpdfstring{\\protect\\hyperlink{AUTOGENERATED-custom-data-readers}{自定义数据读取}}{自定义数据读取}}\\label{ux81eaux5b9aux4e49ux6570ux636eux8bfbux53d6-1}\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-writing-a-reader-for-a-file-format}{编写一个文件格式读写器}\r\n\\item\r\n  \\protect\\hyperlink{AUTOGENERATED-writing-an-op-for-a-record-format}{编写一个记录格式Op}\r\n\\end{itemize}\r\n\r\n\\subsection{编写一个文件格式读写器\r\n}\\label{ux7f16ux5199ux4e00ux4e2aux6587ux4ef6ux683cux5f0fux8bfbux5199ux5668}\r\n\r\nReader\r\n是专门用来读取文件中的记录的。TensorFlow中内建了一些读写器Op的实例：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#TFRecordReader}{tf.TFRecordReader}\r\n  (\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/tf_record_reader_op.cc}{代码位于kernels/tf\\_record\\_reader\\_op.cc})\r\n\\item\r\n  \\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#FixedLengthRecordReader}{tf.FixedLengthRecordReader}\r\n  (\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/fixed_length_record_reader_op.cc}{代码位于\r\n  kernels/fixed\\_length\\_record\\_reader\\_op.cc})\r\n\\item\r\n  \\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#TextLineReader}{tf.TextLineReader}\r\n  (\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/text_line_reader_op.cc}{代码位于\r\n  kernels/text\\_line\\_reader\\_op.cc})\r\n\\end{itemize}\r\n\r\n你可以看到这些读写器的界面是一样的，唯一的差异是在它们的构造函数中。最重要的方法是\r\nRead。\r\n它需要一个行列参数，通过这个行列参数，可以在需要的时候随时读取文件名\r\n(例如： 当 Read Op首次运行，或者前一个 Read`\r\n从一个文件中读取最后一条记录时)。它将会生成两个标量张量：\r\n一个字符串和一个字符串关键值。\r\n\r\n新创建一个名为 SomeReader 的读写器，需要以下步骤：\r\n\r\n\\begin{enumerate}\r\n\\def\\labelenumi{\\arabic{enumi}.}\r\n\\tightlist\r\n\\item\r\n  在 C++ 中, 定义一个\r\n  \\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/reader_base.h}{tensorflow::ReaderBase}的子类，命名为\r\n  ``SomeReader''.\r\n\\item\r\n  在 C++ 中，注册一个新的读写器Op和Kernel，命名为 ``SomeReader''。\r\n\\item\r\n  在 Python 中, 定义一个\r\n  \\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/io_ops.py}{tf.ReaderBase}\r\n  的子类，命名为 ``SomeReader''。\r\n\\end{enumerate}\r\n\r\n你可以把所有的 C++ 代码放在\r\n\\texttt{tensorflow/core/user\\_ops/some\\_reader\\_op.cc}文件中.\r\n读取文件的代码将被嵌入到C++ 的 ReaderBase 类的迭代中。 这个 ReaderBase\r\n类 是在\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/kernels/reader_base.h}{tensorflow/core/kernels/reader\\_base.h}\r\n中定义的。 你需要执行以下的方法：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  OnWorkStartedLocked：打开下一个文件\r\n\\item\r\n  ReadLocked：读取一个记录或报告 EOF/error\r\n\\item\r\n  OnWorkFinishedLocked：关闭当前文件\r\n\\item\r\n  ResetLocked：清空记录，例如：一个错误记录\r\n\\end{itemize}\r\n\r\n以上这些方法的名字后面都带有 ``Locked''， 表示 ReaderBase\r\n在调用任何一个方法之前确保获得互斥锁，这样就不用担心线程安全（虽然只保护了该类中的元素而不是全局的）。\r\n\r\n对于 OnWorkStartedLocked, 需要打开的文件名是 \\texttt{current\\_work()}\r\n函数的返回值。此时的 ReadLocked 的数字签名如下:\r\n\r\n\\begin{verbatim}\r\nStatus ReadLocked(string* key, string* value, bool* produced, bool* at_end)\r\n\\end{verbatim}\r\n\r\n如果 ReadLocked 从文件中成功读取了一条记录，它将更新为：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  *key： 记录的标志位，通过该标志位可以重新定位到该记录。 可以包含从\r\n  current\\_work() 返回值获得的文件名，并追加一个记录号或其他信息。\r\n\\item\r\n  *value： 包含记录的内容。\r\n\\item\r\n  *produced： 设置为 true。\r\n\\end{itemize}\r\n\r\n当你在文件（EOF）末尾，设置 *at\\_end 为 true ，在任何情况下，都将返回\r\nStatus::OK()。 当出现错误的时候，只需要使用\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/lib/core/errors.h}{tensorflow/core/lib/core/errors.h}\r\n中的一个辅助功能就可以简单地返回，不需要做任何参数修改。\r\n\r\n接下来你讲创建一个实际的读写器Op。\r\n如果你已经熟悉了\\href{tensorflow-zh/SOURCE/how_tos/adding_an_op/index.md}{添加新的Op}\r\n那会很有帮助。 主要步骤如下：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  注册Op。\r\n\\item\r\n  定义并注册 OpKernel。\r\n\\end{itemize}\r\n\r\n要注册Op，你需要用到一个调用指令定义在\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/op.h}{tensorflow/core/framework/op.h}中的REGISTER\\_OP。\r\n\r\n读写器 Op 没有输入，只有 Ref(string) 类型的单输出。它们调用\r\nSetIsStateful()，并有一个 container 字符串和 shared\\_name 属性.\r\n你可以在一个 Doc 中定义配置或包含文档的额外属性。 例如：详见\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/ops/io_ops.cc}{tensorflow/core/ops/io\\_ops.cc}等：\r\n\r\n\\begin{verbatim}\r\n #include \"tensorflow/core/framework/op.h\"\r\nREGISTER_OP(\"TextLineReader\")\r\n    .Output(\"reader_handle: Ref(string)\")\r\n    .Attr(\"skip_header_lines: int = 0\")\r\n    .Attr(\"container: string = ''\")\r\n    .Attr(\"shared_name: string = ''\")\r\n    .SetIsStateful()\r\n    .Doc(R\"doc(\r\nA Reader that outputs the lines of a file delimited by '\\n'.\r\n)doc\");\r\n\\end{verbatim}\r\n\r\n要定义一个 OpKernel，\r\n读写器可以使用定义在\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/reader_op_kernel.h}{tensorflow/core/framework/reader\\_op\\_kernel.h}中的\r\nReaderOpKernel 的递减快捷方式，并运行一个叫 SetReaderFactory\r\n的构造函数。 定义所需要的类之后，你需要通过\r\nREGISTER\\_KERNEL\\_BUILDER(\\ldots{}) 注册这个类。\r\n\r\n一个没有属性的例子：\r\n\r\n\\begin{verbatim}\r\n #include \"tensorflow/core/framework/reader_op_kernel.h\"\r\nclass TFRecordReaderOp : public ReaderOpKernel {\r\n public:\r\n  explicit TFRecordReaderOp(OpKernelConstruction* context)\r\n      : ReaderOpKernel(context) {\r\n    Env* env = context->env();\r\n    SetReaderFactory([this, env]() { return new TFRecordReader(name(), env); });\r\n  }\r\n};\r\nREGISTER_KERNEL_BUILDER(Name(\"TFRecordReader\").Device(DEVICE_CPU),\r\n                        TFRecordReaderOp);\r\n\\end{verbatim}\r\n\r\n一个带有属性的例子：\r\n\r\n\\begin{verbatim}\r\n #include \"tensorflow/core/framework/reader_op_kernel.h\"\r\nclass TextLineReaderOp : public ReaderOpKernel {\r\n public:\r\n  explicit TextLineReaderOp(OpKernelConstruction* context)\r\n      : ReaderOpKernel(context) {\r\n    int skip_header_lines = -1;\r\n    OP_REQUIRES_OK(context,\r\n                   context->GetAttr(\"skip_header_lines\", &skip_header_lines));\r\n    OP_REQUIRES(context, skip_header_lines >= 0,\r\n                errors::InvalidArgument(\"skip_header_lines must be >= 0 not \",\r\n                                        skip_header_lines));\r\n    Env* env = context->env();\r\n    SetReaderFactory([this, skip_header_lines, env]() {\r\n      return new TextLineReader(name(), skip_header_lines, env);\r\n    });\r\n  }\r\n};\r\nREGISTER_KERNEL_BUILDER(Name(\"TextLineReader\").Device(DEVICE_CPU),\r\n                        TextLineReaderOp);\r\n\\end{verbatim}\r\n\r\n最后一步是添加 Python 包装器，你需要将 tensorflow.python.ops.io\\_ops\r\n导入到\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/user_ops/user_ops.py}{tensorflow/python/user\\_ops/user\\_ops.py}，并添加一个\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/io_ops.py}{io\\_ops.ReaderBase}的衍生函数。\r\n\r\n\\begin{verbatim}\r\nfrom tensorflow.python.framework import ops\r\nfrom tensorflow.python.ops import common_shapes\r\nfrom tensorflow.python.ops import io_ops\r\nclass SomeReader(io_ops.ReaderBase):\r\n    def __init__(self, name=None):\r\n        rr = gen_user_ops.some_reader(name=name)\r\n        super(SomeReader, self).__init__(rr)\r\nops.NoGradient(\"SomeReader\")\r\nops.RegisterShape(\"SomeReader\")(common_shapes.scalar_shape)\r\n\\end{verbatim}\r\n\r\n你可以在\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/python/ops/io_ops.py}{tensorflow/python/ops/io\\_ops.py}中查看一些范例。\r\n\r\n\\subsection{编写一个记录格式Op\r\n}\\label{ux7f16ux5199ux4e00ux4e2aux8bb0ux5f55ux683cux5f0fop}\r\n\r\n一般来说，这是一个普通的Op， 需要一个标量字符串记录作为输入， 因此遵循\r\n\\href{tensorflow-zh/SOURCE/how_tos/adding_an_op/index.md}{添加Op的说明}。\r\n你可以选择一个标量字符串作为输入，\r\n并包含在错误消息中报告不正确的格式化数据。\r\n\r\n用于解码记录的运算实例：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#parse_single_example}{tf.parse\\_single\\_example}\r\n  (and\r\n  \\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#parse_example}{tf.parse\\_example})\r\n\\item\r\n  \\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#decode_csv}{tf.decode\\_csv}\r\n\\item\r\n  \\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#decode_raw}{tf.decode\\_raw}\r\n\\end{itemize}\r\n\r\n请注意，使用多个Op 来解码某个特定的记录格式也是有效的。\r\n例如，你有一张以字符串格式保存在\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/example/example.proto}{tf.train.Example\r\n协议缓冲区}的图像文件。 根据该图像的格式， 你可能从\r\n\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#parse_single_example}{tf.parse\\_single\\_example}\r\n的Op 读取响应输出并调用\r\n\\href{tensorflow-zh/SOURCE/api_docs/python/image.md\\#decode_jpeg}{tf.decode\\_jpeg}，\r\n\\href{tensorflow-zh/SOURCE/api_docs/python/image.md\\#decode_png}{tf.decode\\_png}，\r\n或者\r\n\\href{tensorflow-zh/SOURCE/api_docs/python/io_ops.md\\#decode_raw}{tf.decode\\_raw}。通过读取\r\ntf.decode\\_raw\r\n的响应输出并使用\\href{tensorflow-zh/SOURCE/api_docs/python/array_ops.md\\#slice}{tf.slice}\r\n和\r\n\\href{tensorflow-zh/SOURCE/api_docs/python/array_ops.md\\#reshape}{tf.reshape}\r\n来提取数据是通用的方法。 \\textgreater{}\r\n原文：\\href{http://tensorflow.org/how_tos/new_data_formats/index.html\\#custom-data-readers}{Custom\r\nData Readers} 翻译：{[}@derekshang{]}(https://github.com/derekshang)\r\n校对：\\href{https://github.com/jikexueyuanwiki}{Wiki}"
  },
  {
    "path": "tex_pdf/how_tos/c3s09_using_gpu.tex",
    "content": "\r\n\r\n\\section{使用 GPUs }\\label{ux4f7fux7528-gpus}\r\n\r\n\\subsection{支持的设备 }\\label{ux652fux6301ux7684ux8bbeux5907}\r\n\r\n在一套标准的系统上通常有多个计算设备. TensorFlow 支持 CPU 和 GPU\r\n这两种设备. 我们用指定字符串 \\texttt{strings} 来标识这些设备. 比如:\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  \\texttt{\"/cpu:0\"}: 机器中的 CPU\r\n\\item\r\n  \\texttt{\"/gpu:0\"}: 机器中的 GPU, 如果你有一个的话.\r\n\\item\r\n  \\texttt{\"/gpu:1\"}: 机器中的第二个 GPU, 以此类推\\ldots{}\r\n\\end{itemize}\r\n\r\n如果一个 TensorFlow 的 operation 中兼有 CPU 和 GPU 的实现,\r\n当这个算子被指派设备时, GPU 有优先权. 比如\\texttt{matmul}中 CPU 和 GPU\r\nkernel 函数都存在. 那么在 \\texttt{cpu:0} 和 \\texttt{gpu:0} 中,\r\n\\texttt{matmul} operation 会被指派给 \\texttt{gpu:0} .\r\n\r\n\\subsection{记录设备指派情况\r\n}\\label{ux8bb0ux5f55ux8bbeux5907ux6307ux6d3eux60c5ux51b5}\r\n\r\n为了获取你的 operations 和 Tensor 被指派到哪个设备上运行, 用\r\n\\texttt{log\\_device\\_placement} 新建一个 \\texttt{session}, 并设置为\r\n\\texttt{True}.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 新建一个 graph.}\r\n\\NormalTok{a }\\OperatorTok{=} \\NormalTok{tf.constant([}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{2.0}\\NormalTok{, }\\FloatTok{3.0}\\NormalTok{, }\\FloatTok{4.0}\\NormalTok{, }\\FloatTok{5.0}\\NormalTok{, }\\FloatTok{6.0}\\NormalTok{], shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{2}\\NormalTok{, }\\DecValTok{3}\\NormalTok{], name}\\OperatorTok{=}\\StringTok{'a'}\\NormalTok{)}\r\n\\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.constant([}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{2.0}\\NormalTok{, }\\FloatTok{3.0}\\NormalTok{, }\\FloatTok{4.0}\\NormalTok{, }\\FloatTok{5.0}\\NormalTok{, }\\FloatTok{6.0}\\NormalTok{], shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{3}\\NormalTok{, }\\DecValTok{2}\\NormalTok{], name}\\OperatorTok{=}\\StringTok{'b'}\\NormalTok{)}\r\n\\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.matmul(a, b)}\r\n\\CommentTok{# 新建session with log_device_placement并设置为True.}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session(config}\\OperatorTok{=}\\NormalTok{tf.ConfigProto(log_device_placement}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{))}\r\n\\CommentTok{# 运行这个 op.}\r\n\\BuiltInTok{print} \\NormalTok{sess.run(c)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n你应该能看见以下输出:\r\n\r\n\\begin{verbatim}\r\nDevice mapping:\r\n/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci bus\r\nid: 0000:05:00.0\r\nb: /job:localhost/replica:0/task:0/gpu:0\r\na: /job:localhost/replica:0/task:0/gpu:0\r\nMatMul: /job:localhost/replica:0/task:0/gpu:0\r\n[[ 22.  28.]\r\n [ 49.  64.]]\r\n\\end{verbatim}\r\n\r\n\\subsection{手工指派设备 }\\label{ux624bux5de5ux6307ux6d3eux8bbeux5907}\r\n\r\n如果你不想使用系统来为 operation 指派设备, 而是手工指派设备, 你可以用\r\n\\texttt{with\\ tf.device} 创建一个设备环境, 这个环境下的 operation\r\n都统一运行在环境指定的设备上.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 新建一个graph.}\r\n\\ControlFlowTok{with} \\NormalTok{tf.device(}\\StringTok{'/cpu:0'}\\NormalTok{):}\r\n  \\NormalTok{a }\\OperatorTok{=} \\NormalTok{tf.constant([}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{2.0}\\NormalTok{, }\\FloatTok{3.0}\\NormalTok{, }\\FloatTok{4.0}\\NormalTok{, }\\FloatTok{5.0}\\NormalTok{, }\\FloatTok{6.0}\\NormalTok{], shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{2}\\NormalTok{, }\\DecValTok{3}\\NormalTok{], name}\\OperatorTok{=}\\StringTok{'a'}\\NormalTok{)}\r\n  \\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.constant([}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{2.0}\\NormalTok{, }\\FloatTok{3.0}\\NormalTok{, }\\FloatTok{4.0}\\NormalTok{, }\\FloatTok{5.0}\\NormalTok{, }\\FloatTok{6.0}\\NormalTok{], shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{3}\\NormalTok{, }\\DecValTok{2}\\NormalTok{], name}\\OperatorTok{=}\\StringTok{'b'}\\NormalTok{)}\r\n\\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.matmul(a, b)}\r\n\\CommentTok{# 新建session with log_device_placement并设置为True.}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session(config}\\OperatorTok{=}\\NormalTok{tf.ConfigProto(log_device_placement}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{))}\r\n\\CommentTok{# 运行这个op.}\r\n\\BuiltInTok{print} \\NormalTok{sess.run(c)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n你会发现现在 \\texttt{a} 和 \\texttt{b} 操作都被指派给了 \\texttt{cpu:0}.\r\n\r\n\\begin{verbatim}\r\nDevice mapping:\r\n/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci bus\r\nid: 0000:05:00.0\r\nb: /job:localhost/replica:0/task:0/cpu:0\r\na: /job:localhost/replica:0/task:0/cpu:0\r\nMatMul: /job:localhost/replica:0/task:0/gpu:0\r\n[[ 22.  28.]\r\n [ 49.  64.]]\r\n\\end{verbatim}\r\n\r\n\\subsection{在多GPU系统里使用单一GPU}\\label{ux5728ux591agpuux7cfbux7edfux91ccux4f7fux7528ux5355ux4e00gpu}\r\n\r\n如果你的系统里有多个 GPU, 那么 ID 最小的 GPU 会默认使用. 如果你想用别的\r\nGPU, 可以用下面的方法显式的声明你的偏好:\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 新建一个 graph.}\r\n\\ControlFlowTok{with} \\NormalTok{tf.device(}\\StringTok{'/gpu:2'}\\NormalTok{):}\r\n  \\NormalTok{a }\\OperatorTok{=} \\NormalTok{tf.constant([}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{2.0}\\NormalTok{, }\\FloatTok{3.0}\\NormalTok{, }\\FloatTok{4.0}\\NormalTok{, }\\FloatTok{5.0}\\NormalTok{, }\\FloatTok{6.0}\\NormalTok{], shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{2}\\NormalTok{, }\\DecValTok{3}\\NormalTok{], name}\\OperatorTok{=}\\StringTok{'a'}\\NormalTok{)}\r\n  \\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.constant([}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{2.0}\\NormalTok{, }\\FloatTok{3.0}\\NormalTok{, }\\FloatTok{4.0}\\NormalTok{, }\\FloatTok{5.0}\\NormalTok{, }\\FloatTok{6.0}\\NormalTok{], shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{3}\\NormalTok{, }\\DecValTok{2}\\NormalTok{], name}\\OperatorTok{=}\\StringTok{'b'}\\NormalTok{)}\r\n  \\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.matmul(a, b)}\r\n\\CommentTok{# 新建 session with log_device_placement 并设置为 True.}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session(config}\\OperatorTok{=}\\NormalTok{tf.ConfigProto(log_device_placement}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{))}\r\n\\CommentTok{# 运行这个 op.}\r\n\\BuiltInTok{print} \\NormalTok{sess.run(c)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n如果你指定的设备不存在, 你会收到 \\texttt{InvalidArgumentError} 错误提示:\r\n\r\n\\begin{verbatim}\r\nInvalidArgumentError: Invalid argument: Cannot assign a device to node 'b':\r\nCould not satisfy explicit device specification '/gpu:2'\r\n   [[Node: b = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [3,2]\r\n   values: 1 2 3...>, _device=\"/gpu:2\"]()]]\r\n\\end{verbatim}\r\n\r\n为了避免出现你指定的设备不存在这种情况, 你可以在创建的 \\texttt{session}\r\n里把参数 \\texttt{allow\\_soft\\_placement} 设置为 \\texttt{True}, 这样\r\ntensorFlow 会自动选择一个存在并且支持的设备来运行 operation.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 新建一个 graph.}\r\n\\ControlFlowTok{with} \\NormalTok{tf.device(}\\StringTok{'/gpu:2'}\\NormalTok{):}\r\n  \\NormalTok{a }\\OperatorTok{=} \\NormalTok{tf.constant([}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{2.0}\\NormalTok{, }\\FloatTok{3.0}\\NormalTok{, }\\FloatTok{4.0}\\NormalTok{, }\\FloatTok{5.0}\\NormalTok{, }\\FloatTok{6.0}\\NormalTok{], shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{2}\\NormalTok{, }\\DecValTok{3}\\NormalTok{], name}\\OperatorTok{=}\\StringTok{'a'}\\NormalTok{)}\r\n  \\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.constant([}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{2.0}\\NormalTok{, }\\FloatTok{3.0}\\NormalTok{, }\\FloatTok{4.0}\\NormalTok{, }\\FloatTok{5.0}\\NormalTok{, }\\FloatTok{6.0}\\NormalTok{], shape}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{3}\\NormalTok{, }\\DecValTok{2}\\NormalTok{], name}\\OperatorTok{=}\\StringTok{'b'}\\NormalTok{)}\r\n  \\NormalTok{c }\\OperatorTok{=} \\NormalTok{tf.matmul(a, b)}\r\n\\CommentTok{# 新建 session with log_device_placement 并设置为 True.}\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.Session(config}\\OperatorTok{=}\\NormalTok{tf.ConfigProto(}\r\n      \\NormalTok{allow_soft_placement}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{, log_device_placement}\\OperatorTok{=}\\VariableTok{True}\\NormalTok{))}\r\n\\CommentTok{# 运行这个 op.}\r\n\\BuiltInTok{print} \\NormalTok{sess.run(c)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsection{使用多个 GPU }\\label{ux4f7fux7528ux591aux4e2a-gpu}\r\n\r\n如果你想让 TensorFlow 在多个 GPU 上运行, 你可以建立 multi-tower 结构,\r\n在这个结构 里每个 tower 分别被指配给不同的 GPU 运行. 比如:\r\n\r\n\\begin{verbatim}\r\n# 新建一个 graph.\r\nc = []\r\nfor d in ['/gpu:2', '/gpu:3']:\r\n  with tf.device(d):\r\n    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])\r\n    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])\r\n    c.append(tf.matmul(a, b))\r\nwith tf.device('/cpu:0'):\r\n  sum = tf.add_n(c)\r\n# 新建session with log_device_placement并设置为True.\r\nsess = tf.Session(config=tf.ConfigProto(log_device_placement=True))\r\n# 运行这个op.\r\nprint sess.run(sum)\r\n\\end{verbatim}\r\n\r\n你会看到如下输出:\r\n\r\n\\begin{verbatim}\r\nDevice mapping:\r\n/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K20m, pci bus\r\nid: 0000:02:00.0\r\n/job:localhost/replica:0/task:0/gpu:1 -> device: 1, name: Tesla K20m, pci bus\r\nid: 0000:03:00.0\r\n/job:localhost/replica:0/task:0/gpu:2 -> device: 2, name: Tesla K20m, pci bus\r\nid: 0000:83:00.0\r\n/job:localhost/replica:0/task:0/gpu:3 -> device: 3, name: Tesla K20m, pci bus\r\nid: 0000:84:00.0\r\nConst_3: /job:localhost/replica:0/task:0/gpu:3\r\nConst_2: /job:localhost/replica:0/task:0/gpu:3\r\nMatMul_1: /job:localhost/replica:0/task:0/gpu:3\r\nConst_1: /job:localhost/replica:0/task:0/gpu:2\r\nConst: /job:localhost/replica:0/task:0/gpu:2\r\nMatMul: /job:localhost/replica:0/task:0/gpu:2\r\nAddN: /job:localhost/replica:0/task:0/cpu:0\r\n[[  44.   56.]\r\n [  98.  128.]]\r\n\\end{verbatim}\r\n\r\n\\href{tensorflow-zh/SOURCE/tutorials/deep_cnn/index.md}{cifar10\r\ntutorial} 这个例子很好的演示了怎样用GPU集群训练.\r\n\r\n\\begin{quote}\r\n原文:\\href{http://tensorflow.org/how_tos/using_gpu/index.md}{using\\_gpu}\r\n翻译:{[}@lianghyv{]}(https://github.com/lianghyv)\r\n校对:\\href{https://github.com/jikexueyuanwiki}{Wiki}\r\n\\end{quote}\r\n\r\n"
  },
  {
    "path": "tex_pdf/tensorflow_manual_cn.idx",
    "content": "\\indexentry{graph|hyperpage}{27}\n\\indexentry{session|hyperpage}{27}\n\\indexentry{tensor|hyperpage}{27}\n\\indexentry{variable|hyperpage}{27}\n\\indexentry{MNIST 数据集|hyperpage}{39}\n\\indexentry{Softmax regression|hyperpage}{42}\n\\indexentry{梯度下降法|hyperpage}{49}\n\\indexentry{Softmax regression|hyperpage}{54}\n\\indexentry{卷积神经网络|hyperpage}{57}\n"
  },
  {
    "path": "tex_pdf/tensorflow_manual_cn.ilg",
    "content": "This is makeindex, version 2.15 [TeX Live 2015/W32TeX] (kpathsea + Thai support).\nScanning input file tensorflow_manual_cn.idx....done (9 entries accepted, 0 rejected).\nSorting entries....done (33 comparisons).\nGenerating output file tensorflow_manual_cn.ind....done (30 lines written, 0 warnings).\nOutput written in tensorflow_manual_cn.ind.\nTranscript written in tensorflow_manual_cn.ilg.\n"
  },
  {
    "path": "tex_pdf/tensorflow_manual_cn.ind",
    "content": "\\begin{theindex}\n\n  \\item graph, \\hyperpage{26}\n\n  \\indexspace\n\n  \\item MNIST 数据集, \\hyperpage{37}\n\n  \\indexspace\n\n  \\item session, \\hyperpage{26}\n  \\item Softmax regression, \\hyperpage{40}, \\hyperpage{52}\n\n  \\indexspace\n\n  \\item tensor, \\hyperpage{26}\n\n  \\indexspace\n\n  \\item variable, \\hyperpage{26}\n\n  \\indexspace\n\n  \\item 卷积神经网络, \\hyperpage{55}\n\n  \\indexspace\n\n  \\item 梯度下降法, \\hyperpage{47}\n\n\\end{theindex}\n"
  },
  {
    "path": "tex_pdf/tensorflow_manual_cn.tex",
    "content": "%!TEX program = xelatex\r\n% Encoding: UTF8\r\n% SEIKA 2015\r\n\r\n%\\documentclass[a4paper,11pt,twoside]{book}\r\n\\documentclass[a4paper,11pt,twoside]{ctexbook}\r\n\r\n\\usepackage{makeidx}\r\n\\makeindex\r\n\r\n\\usepackage{geometry}\r\n\\geometry{left=3.5cm, right=3cm, top=3cm, bottom=3cm}\r\n%控制页眉页脚页码\r\n\\pagestyle{headings}\r\n%罗马字符页码\r\n%\\pagenumbering{roman}\r\n\r\n% \\usepackage{ctex}\r\n% \\usepackage{xeCJK}\r\n\r\n% \\CJKsetecglue{} % 禁用汉字与其他内容之间空格（空隙）\r\n\r\n% 支持西文字体\r\n\\usepackage{fourier}\r\n\\usepackage{courier}\r\n% \\usepackage{fontspec}\r\n\r\n\\newfontfamily\\CodeFont{Consolas}\r\n% \\newfontfamily\\CodeFont{Ubuntu Mono}\r\n% \\newfontfamily\\CodeFont{Menlo}\r\n% \\newfontfamily\\CodeFont{Lucida Console}\r\n% \\setmonofont{Lucida Console}\r\n\r\n\\usepackage{graphicx}\r\n% 支持插入eps图形文件\r\n% \\usepackage{epsfig}\r\n\r\n% 支持代码框插入\r\n\\usepackage{xcolor}\r\n\\definecolor{mygreen}{rgb}{0,0.6,0}\r\n\\definecolor{mygray}{rgb}{0.5,0.5,0.5}\r\n\\definecolor{mymauve}{rgb}{0.58,0,0.82}\r\n\\definecolor{codeback}{rgb}{0.8,0.87,0.95}\r\n\\definecolor{etc}{rgb}{0.24,0.07,0.18}\r\n\\definecolor{etc}{rgb}{0.54,0.37,0.48}\r\n\r\n\\usepackage{amsmath}\r\n\r\n% 支持超链接\r\n\\usepackage[colorlinks]{hyperref}\r\n\r\n\\usepackage{listings}\r\n\\lstset{ %\r\n  backgroundcolor=\\color{codeback},    % choose the background color; you must add \\usepackage{color} or \\usepackage{xcolor}\r\n  basicstyle=\\linespread{0.95}\\footnotesize\\CodeFont,   % the size of the fonts that are used for the code\r\n  breakatwhitespace=false,           % sets if automatic breaks should only happen at whitespace\r\n  breaklines=true,                   % sets automatic line breaking\r\n  captionpos=bl,                     % sets the caption-position to bottom\r\n  commentstyle=\\color{mygreen},      % comment style\r\n  deletekeywords={...},              % if you want to delete keywords from the given language\r\n  escapeinside={\\%*}{*)},            % if you want to add LaTeX within your code\r\n  extendedchars=true,                % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8\r\n  frame=single,                      % adds a frame around the code\r\n  frameround=tttt,\r\n  keepspaces=true,                   % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)\r\n  keywordstyle=\\color{blue},         % keyword style\r\n  language=Python,                   % the language of the code\r\n  morekeywords={*,...},              % if you want to add more keywords to the set\r\n  numbers=left,                      % where to put the line-numbers; possible values are (none, left, right)\r\n  numbersep=4pt,                     % how far the line-numbers are from the code\r\n  numberstyle=\\tiny\\CodeFont\\color{mygray},   % the style that is used for the line-numbers\r\n  rulecolor=\\color{mygray},          % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))\r\n  showspaces=false,                  % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'\r\n  showstringspaces=true,             % underline spaces within strings only\r\n  showtabs=true,                     % show tabs within strings adding particular underscores\r\n  stepnumber=1,                      % the step between two line-numbers. If it's 1, each line will be numbered\r\n  stringstyle=\\color{orange},        % string literal style\r\n  tabsize=2,                         % sets default tabsize to 2 spaces\r\n  %title=myPython.py                 % show the filename of files included with \\lstinputlisting; also try caption instead of title\r\n  xleftmargin = 2em,\r\n  xrightmargin = 2em,\r\n  aboveskip = 0.5 em\r\n}\r\n\r\n% \\setCJKmainfont[BoldFont={SimSun},ItalicFont={KaiTi}] %{SimSun}\r\n\r\n%%%%%%%%%%%%\r\n\\title{TensorFlow 指南}\r\n\\author{}\r\n\\date{\\today}\r\n\r\n% \\thanks{}\r\n\r\n\\begin{document}\r\n\r\n\\maketitle\r\n\\tableofcontents\r\n\r\n%%%% 第一章\r\n\\newpage\r\n\\chapter{起步}\r\n% \\section{Introduction}\r\n\\include{get_started/c1s01_introduction}\r\n\\include{get_started/c1s02_os_setup}\r\n\\include{get_started/c1s03_basic_usage}\r\n\r\n%%%% 第二章\r\n\\newpage\r\n\\chapter{基础教程}\r\n\\include{tutorials/c2s00_overview}\r\n\\include{tutorials/c2s01_minist_beginners}\r\n\\include{tutorials/c2s02_minist_pros}\r\n\\include{tutorials/c2s03_}\r\n\\include{tutorials/c2s04_}\r\n\\include{tutorials/c2s05_word2vec}\r\n\\include{tutorials/c2s06_recurrent}\r\n\\include{tutorials/c2s07_seq2seq}\r\n\\include{tutorials/c2s08_mandelbrot}\r\n\\include{tutorials/c2s09_pdes}\r\n\\include{tutorials/c2s10_mnist_download}\r\n\r\n\\newpage\r\n% Chapter 3 How to...\r\n% 第三章 运作方式\r\n\\chapter{运作方式}\r\n\\include{how_tos/c3s00_overview}\r\n\\include{how_tos/c3s01_variables}\r\n\\include{how_tos/c3s02_variable_scope}\r\n\\include{how_tos/c3s03_viz_learning}\r\n\\include{how_tos/c3s04_graph_viz}\r\n\\include{how_tos/c3s05_reading_data}\r\n\\include{how_tos/c3s06_threading_and_queues}\r\n\\include{how_tos/c3s07_adding_an_op}\r\n\\include{how_tos/c3s08_new_data_formats}\r\n\\include{how_tos/c3s09_using_gpu}\r\n\r\n🍁 % English parts\r\n\\newpage\r\n% Chapter 4 API (Python)\r\n\\chapter{Python API}\r\n\\include{api/c4s00}\r\n\\include{api/python/c4s01_framework}\r\n\\include{api/python/c4s02_constant_op}\r\n\\include{api/python/c4s03_state_ops}\r\n\\include{api/python/c4s04_array_ops}\r\n\\include{api/python/c4s05_math_ops}\r\n\\include{api/python/c4s06_control_flow_ops}\r\n\\include{api/python/c4s07_image}\r\n\\include{api/python/c4s08_sparse_ops}\r\n\\include{api/python/c4s09_io_ops}\r\n\\include{api/python/c4s10_python_io}\r\n\\include{api/python/c4s11_nn}\r\n\\include{api/python/c4s12_client}\r\n\\include{api/python/c4s13_train}\r\n\r\n\\newpage\r\n% Chapter 5 API (C++)\r\n\\chapter{C++ API}\r\n\r\n\\newpage\r\n\\chapter{资源}\r\n\r\n\\newpage\r\n\\chapter{其他}\r\n\r\n\\printindex\r\n\\addcontentsline{toc}{chapter}{索引}\r\n\r\n\\end{document}\r\n\r\n"
  },
  {
    "path": "tex_pdf/tutorials/c2s00_overview.tex",
    "content": "%!TEX program = xelatex\r\n% Encoding: UTF8\r\n% SEIKA 2015\r\n\r\n\r\n% Chapter 2 TutorialsHow to ...\r\n% Section 2.1\r\n\r\n% \\section{综述}\r\n\r\n\\textbf{综述}\r\n\r\n\\hyperref[MINIST_beginner]{\\textcolor{etc}{\\textbf{MNIST For ML Beginners}}  ||  \\textbf{MNIST 机器学习入门}}\r\n\r\n\\textcolor{etc}{If you're new to machine learning, we recommend starting here. You'll learn about a classic problem, handwritten digit classification (MNIST), and get a gentle introduction to multiclass classification.}\r\n\r\n如果你是机器学习领域的新手, 我们推荐你从本文开始阅读. 本文通过讲述一个经典的问题, 手写数字识别 (MNIST), 让你对多类分类 (multiclass classification) 问题有直观的了解.\r\n\r\n\\textcolor{etc}{\\textbf{Deep MNIST for Experts}}  ||  \\textbf{深入MNIST}\r\n\r\n\\textcolor{etc}{If you're already familiar with other deep learning software packages, and are already familiar with MNIST, this tutorial with give you a very brief primer on TensorFlow.}\r\n\r\n如果你已经对其它深度学习软件比较熟悉, 并且也对 MNIST 很熟悉, 这篇教程能够引导你对 TensorFlow 有初步了解.\r\n\r\n\\hyperref[MINIST_pros]{View Tutorial | 阅读该教程}\r\n\r\n\\textcolor{etc}{\\textbf{TensorFlow Mechanics 101}}  ||  \\textbf{}\r\n\r\n\\textcolor{etc}{This is a technical tutorial, where we walk you through the details of using TensorFlow infrastructure to train models at scale. We use again MNIST as the example.}\r\n\r\n这是一篇技术教程, 详细介绍了如何使用 TensorFlow 架构训练大规模模型. 本文继续使用MNIST 作为例子.\r\n\r\n\\hyperref[tf_mech101]{View Tutorial | 阅读该教程}\r\n\r\n\\textbf{Convolutional Neural Networks}\r\n\r\nAn introduction to convolutional neural networks using the CIFAR-10 data set. Convolutional neural nets are particularly tailored to images, since they exploit translation invariance to yield more compact and effective representations of visual content.\r\n\r\n这篇文章介绍了如何使用 TensorFlow 在 CIFAR-10 数据集上训练卷积神经网络. 卷积神经网络是为图像识别量身定做的一个模型. 相比其它模型, 该模型利用了平移不变性(translation invariance), 从而能够更更简洁有效地表示视觉内容.\r\n\r\nView Tutorial\r\n\r\n\\textbf{Vector Representations of Words}\r\n\r\nThis tutorial motivates why it is useful to learn to represent words as vectors (called word embeddings). It introduces the word2vec model as an efficient method for learning embeddings. It also covers the high-level details behind noise-contrastive training methods (the biggest recent advance in training embeddings).\r\n\r\n本文让你了解为什么学会使用向量来表示单词, 即单词嵌套 (word embedding), 是一件很有用的事情. 文章中介绍的 word2vec 模型, 是一种高效学习嵌套的方法. 本文还涉及了对比噪声(noise-contrastive) 训练方法的一些高级细节, 该训练方法是训练嵌套领域最近最大的进展.\r\n\r\nView Tutorial\r\n\r\n\\textbf{Recurrent Neural Networks}\r\n\r\nAn introduction to RNNs, wherein we train an LSTM network to predict the next word in an English sentence. (A task sometimes called language modeling.)\r\n\r\n一篇 RNN 的介绍文章, 文章中训练了一个 LSTM 网络来预测一个英文句子的下一个单词(该任务有时候被称作语言建模).\r\n\r\nView Tutorial\r\n\r\n\\textbf{Sequence-to-Sequence Models}\r\n\r\nA follow on to the RNN tutorial, where we assemble a sequence-to-sequence model for machine translation. You will learn to build your own English-to-French translator, entirely machine learned, end-to-end.\r\n\r\nRNN 教程的后续, 该教程采用序列到序列模型进行机器翻译. 你将学会构建一个完全基于机器学习,端到端的\\emph{英语-法语}翻译器.\r\n\r\nView Tutorial\r\n\r\n\\textbf{Mandelbrot Set}\r\n\r\nTensorFlow can be used for computation that has nothing to do with machine learning. Here's a naive implementation of Mandelbrot set visualization.\r\n\r\nTensorFlow 可以用于与机器学习完全无关的其它计算领域. 这里实现了一个原生的 Mandelbrot 集合的可视化程序.\r\n\r\nView Tutorial\r\n\r\n\\textbf{Partial Differential Equations}\r\n\r\nAs another example of non-machine learning computation, we offer an example of a naive PDE simulation of raindrops landing on a pond.\r\n\r\n这是另外一个非机器学习计算的例子, 我们利用一个原生实现的偏微分方程, 对雨滴落在池塘上的过程进行仿真.\r\n\r\nView Tutorial\r\n\r\n\\textbf{MNIST Data Download}\r\n\r\nDetails about downloading the MNIST handwritten digits data set. Exciting stuff.\r\n\r\n一篇关于下载 MNIST 手写识别数据集的详细教程.\r\n\r\nView Tutorial\r\n\r\n\\textbf{Image Recognition}\r\n\r\nHow to run object recognition using a convolutional neural network trained on ImageNet Challenge data and label set.\r\n\r\n如何利用受过训练的ImageNet挑战数据和标签集卷积神经网络来运行物体识别。\r\n\r\nView Tutorial\r\n\r\nWe will soon be releasing code for training a state-of-the-art Inception model.\r\n\r\nDeep Dream Visual Hallucinations\r\n\r\nBuilding on the Inception recognition model, we will release a TensorFlow version of the Deep Dream neural network visual hallucination software.\r\n\r\n我们也将公布一个训练高级的Iception模型所用的代码。\r\n\r\nCOMING SOON"
  },
  {
    "path": "tex_pdf/tutorials/c2s01_minist_beginners.tex",
    "content": "%!TEX program = xelatex\r\n% Encoding: UTF8\r\n% SEIKA 2015\r\n\r\n\r\n% Chapter 2 TutorialsHow to ...\r\n% Section 2.2\r\n\r\n\\newpage\r\n\\section {MNIST机器学习入门}\\label{MINIST_beginner}\r\n\r\nⒺ \\textcolor{etc}{This tutorial is intended for readers who are new to both machine learning and TensorFlow. If you already know what MNIST is, and what softmax (multinomial logistic) regression is, you might prefer this faster paced tutorial. Be sure to install TensorFlow before starting either tutorial.}\r\n\r\nⒸ 本教程的目标读者是对机器学习和TensorFlow都不太了解的新手．如果你已经了解MNIST和softmax回归(softmax regression)的相关知识，你可以阅读这个快速上手教程．\r\n\r\nⒺ \\textcolor{etc}{When one learns how to program, there's a tradition that the first thing you do is print \"Hello World.\" Just like programming has Hello World, machine learning has MNIST.}\r\n\r\nⒸ 当我们开始学习编程的时候，第一件事往往是学习打印“Hello World”．就好比编程入门有Hello World，机器学习入门有MNIST．\\index{MNIST 数据集}\r\n\r\nⒺ \\textcolor{etc}{MNIST is a simple computer vision dataset. It consists of images of handwritten digits like these:}\r\n\r\nⒸ MNIST是一个入门级的计算机视觉数据集，它包含各种手写数字图片：\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics[width=.55\\textwidth]{../SOURCE/images/MNIST.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\nⒺ \\textcolor{etc}{It also includes labels for each image, telling us which digit it is. For example, the labels for the above images are 5, 0, 4, and 1.}\r\n\r\nⒸ 它也包含每一张图片对应的标签，告诉我们这个是数字几．比如，上面这四张图片的标签分别是5,0,4,1．\r\n\r\nⒺ \\textcolor{etc}{In this tutorial, we're going to train a model to look at images and predict what digits they are. Our goal isn't to train a really elaborate model that achieves state-of-the-art performance -- although we'll give you code to do that later! -- but rather to dip a toe into using TensorFlow. As such, we're going to start with a very simple model, called a Softmax Regression.}\r\n\r\nⒸ 在此教程中，我们将训练一个机器学习模型用于预测图片里面的数字．我们的目的不是要设计一个世界一流的复杂模型---尽管我们会在之后给你源代码去实现一流的预测模型---而是要介绍下如何使用TensorFlow．所以，我们这里会从一个很简单的数学模型开始，它叫做Softmax Regression．\r\n\r\nⒺ \\textcolor{etc}{The actual code for this tutorial is very short, and all the interesting stuff happens in just three lines. However, it is very important to understand the ideas behind it: both how TensorFlow works and the core machine learning concepts. Because of this, we are going to very carefully work through the code.}\r\n\r\nⒸ 对应这个教程的实现代码很短，而且真正有意思的内容只包含在三行代码里面．但是，去理解包含在这些代码里面的设计思想是非常重要的：TensorFlow工作流程和机器学习的基本概念．因此，这个教程会很详细地介绍这些代码的实现原理．\r\n\r\n\\subsection {The MNIST Data  |  MNIST数据集}\r\n\r\nⒺ \\textcolor{etc}{The MNIST data is hosted on Yann LeCun's website. For your convenience, we've included some python code to download and install the data automatically. You can either download the code and import it as below, or simply copy and paste it in.}\r\n\r\nⒸ MNIST数据集的官网是\\href{http://yann.lecun.com/exdb/mnist/}{Yann LeCun's website}．在这里，我们提供了一份python源代码用于自动下载和安装这个数据集．你可以下载这段\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/mnist/input_data.py}{代码}，然后用下面的代码导入到你的项目里面，也可以直接复制粘贴到你的代码文件里面．\r\n\r\n\r\n%\\begin{lstlisting}[language={[ANSI]Python}]\r\n\\begin{lstlisting}\r\nimport input_data\r\nmnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{The downloaded data is split into three parts, 55,000 data points of training data \\lstinline{(mnist.train)}, 10,000 points of test data \\lstinline{(mnist.test)}, and 5,000 points of validation data \\lstinline{(mnist.validation)}. This split is very important: it's essential in machine learning that we have separate data which we don't learn from so that we can make sure that what we've learned actually generalizes!}\r\n\r\nⒸ 下载下来的数据集可被分为三部分：55000 行训练用点数据集（\\lstinline{mnist.train}），10000 行测试数据集(\\lstinline{mnist.test})，以及5000行验证数据集（\\lstinline{mnist.validation}）．这样的切分很重要：在机器学习模型设计时必须有一个单独的测试数据集不用于训练而是用来评估这个模型的性能，从而更加容易把设计的模型推广到其他数据集上（泛化）．\r\n\r\nⒺ \\textcolor{etc}{As mentioned earlier, every MNIST data point has two parts: an image of a handwritten digit and a corresponding label. We will call the images \"xs\" and the labels \"ys\". Both the training set and test set contain xs and ys, for example the training images are \\lstinline{mnist.train.images} and the train labels are \\lstinline{mnist.train.labels}.}\r\n\r\nⒸ 正如前面提到的一样，每一个MNIST数据单元有两部分组成：一张包含手写数字的图片和一个对应的标签．我们把这些图片设为“xs”，把这些标签设为“ys”．训练数据集和测试数据集都包含xs和ys，比如训练数据集的图片是\\lstinline{mnist.train.images} ，训练数据集的标签是\\lstinline{mnist.train.labels}．\r\n\r\nⒺ \\textcolor{etc}{Each image is 28 pixels by 28 pixels. We can interpret this as a big array of numbers:}\r\n\r\nⒸ 每一张图片包含$ 28 \\times 28$像素．我们可以用一个数字数组来表示这张图片：\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics[width=.8\\textwidth]{../SOURCE/images/MNIST-Matrix.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\nⒺ \\textcolor{etc}{We can flatten this array into a vector of $ 28 \\times 28 = 784$ numbers. It doesn't matter how we flatten the array, as long as we're consistent between images. From this perspective, the MNIST images are just a bunch of points in a 784-dimensional vector space, with a \\href{http://colah.github.io/posts/2014-10-Visualizing-MNIST/}{very rich structure} (warning: computationally intensive visualizations).}\r\n\r\nⒸ 我们把这个数组展开成一个向量，长度是 $ 28 \\times 28 = 784$．如何展开这个数组（数字间的顺序）不重要，只要保持各个图片采用相同的方式展开．从这个角度来看，MNIST数据集的图片就是在784维向量空间里面的点, 并且拥有比较\\href{http://colah.github.io/posts/2014-10-Visualizing-MNIST/}{复杂的结构} (注意: 此类数据的可视化是计算密集型的)．\r\n\r\nⒺ \\textcolor{etc}{Flattening the data throws away information about the 2D structure of the image. Isn't that bad? Well, the best computer vision methods do exploit this structure, and we will in later tutorials. But the simple method we will be using here, a softmax regression, won't.}\r\n\r\nⒸ 展平图片的数字数组会丢失图片的二维结构信息．这显然是不理想的，最优秀的计算机视觉方法会挖掘并利用这些结构信息，我们会在后续教程中介绍．但是在这个教程中我们忽略这些结构，所介绍的简单数学模型，softmax回归(softmax regression)，不会利用这些结构信息．\r\n\r\nⒺ \\textcolor{etc}{The result is that \\lstinline{mnist.train.images} is a tensor (an n-dimensional array) with a shape of \\lstinline{[55000, 784]}. The first dimension indexes the images and the second dimension indexes the pixels in each image. Each entry in the tensor is the pixel intensity between 0 and 1, for a particular pixel in a particular image.}\r\n\r\nⒸ 因此，在MNIST训练数据集中，\\lstinline{mnist.train.images}是一个形状为 \\lstinline{[55000, 784]} 的张量，第一个维度数字用来索引图片，第二个维度数字用来索引每张图片中的像素点．在此张量里的每一个元素，都表示某张图片里的某个像素的强度值，值介于0和1之间．\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics[width=.65\\textwidth]{../SOURCE/images/mnist-train-xs.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\nⒺ \\textcolor{etc}{The corresponding labels in MNIST are numbers between 0 and 9, describing which digit a given image is of. For the purposes of this tutorial, we're going to want our labels as \"one-hot vectors\". A one-hot vector is a vector which is 0 in most dimensions, and 1 in a single dimension. In this case, the $n$th digit will be represented as a vector which is 1 in the $n$th dimensions. For example, 3 would be $[0,0,0,1,0,0,0,0,0,0]$. Consequently, \\lstinline{mnist.train.labels} is a \\lstinline{[55000, 10]} array of floats.}\r\n\r\nⒸ 相对应的MNIST数据集的标签是介于0到9的数字，用来描述给定图片里表示的数字．为了用于这个教程，我们使标签数据是\"one-hot vectors\"． 一个one-hot向量除了某一位的数字是1以外其余各维度数字都是0．所以在此教程中，数字n将表示成一个只有在第$n$维度（从0开始）数字为1的10维向量．比如，标签3将表示成(\\lstinline{[0,0,0,1,0,0,0,0,0,0]})．因此，\\lstinline{mnist.train.labels}是一个 \\lstinline{[55000, 10]} 的数字矩阵．\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics[width=.7\\textwidth]{../SOURCE/images/mnist-train-ys.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\nⒺ \\textcolor{etc}{We're now ready to actually make our model!}\r\n\r\nⒸ 现在，我们准备开始真正的建模之旅！\r\n\r\n\\subsection {Softmax回归介绍}\r\n\r\nⒺ We know that every image in MNIST is a digit, whether it's a zero or a nine. We want to be able to look at an image and give probabilities for it being each digit. For example, our model might look at a picture of a nine and be 80\\% sure it's a nine, but give a 5\\% chance to it being an eight (because of the top loop) and a bit of probability to all the others because it isn't sure.\r\n\r\nⒸ 我们知道MNIST数据集的每一张图片都表示一个(0到9的)数字．那么，如果模型若能看到一张图就能知道它属于各个数字的对应概率就好了。比如，我们的模型可能看到一张数字\"9\"的图片，就判断出它是数字\"9\"的概率为80\\%，而有5\\%的概率属于数字\"8\"（因为8和9都有上半部分的小圆），同时给予其他数字对应的小概率（因为该图像代表它们的可能性微乎其微）．\\index{Softmax regression}\r\n\r\nⒺ This is a classic case where a softmax regression is a natural, simple model. If you want to assign probabilities to an object being one of several different things, softmax is the thing to do. Even later on, when we train more sophisticated models, the final step will be a layer of softmax.\r\n\r\nⒸ 这是能够体现softmax回归自然简约的一个典型案例．softmax模型可以用来给不同的对象分配概率．在后文，我们训练更加复杂的模型时，最后一步也往往需要用softmax来分配概率．\r\n\r\nⒺ A softmax regression has two steps: first we add up the evidence of our input being in certain classes, and then we convert that evidence into probabilities.\r\n\r\nⒸ softmax回归（softmax regression）分两步：首先对输入被分类对象属于某个类的“证据”相加求和，然后将这个“证据”的和转化为概率.\r\n\r\nⒺ To tally up the evidence that a given image is in a particular class, we do a weighted sum of the pixel intensities. The weight is negative if that pixel having a high intensity is evidence against the image being in that class, and positive if it is evidence in favor.\r\n\r\nⒸ 我们使用加权的方法来累积计算一张图片是否属于某类的“证据”。如果图片的像素强有力的体现该图不属于某个类，则权重为负数，相反如果这个像素拥有有利的证据支持这张图片属于这个类，那么权值为正．\r\n\r\nⒺ The following diagram shows the weights one model learned for each of these classes. Red represents negative weights, while blue represents positive weights.\r\n\r\nⒸ 下面的图片显示了一个模型学习到的图片上每个像素对于特定数字类的权值．红色代表负权值，蓝色代表正权值．\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics[width=.65\\textwidth]{../SOURCE/images/softmax-weights.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\nⒺ We also add some extra evidence called a bias. Basically, we want to be able to say that some things are more likely independent of the input. The result is that the evidence for a class $i$ given an input $x$ is:\r\n\r\nⒸ 我们也需要引入额外的“证据”，可称之为偏置量(bias）。总的来说，我们希望它代表了与所输入向无关的判断证据．因此对于给定的输入图片$x$代表某数字$i$的总体证据可以表示为:\r\n\\begin{equation}\r\nevidence_i = \\sum_j{W_{i,j}}x_j+b_i\r\n\\end{equation}\\\\\r\nⒺ where $W_i$ is the weights and $b_i$ is the bias for class $i$, and $j$ is an index for summing over the pixels in our input image $x$. We then convert the evidence tallies into our predicted probabilities y using the \"softmax\" function:\\\\\r\nⒸ 其中，$W_i$ 代表权重，$b_i$ 代表第 $i$ 类的偏置量，$j$ 代表给定图片 $x$ 的像素索引用于像素求和．然后用softmax函数可以把这些证据转换成概率$y$:\\\\\r\n\\begin{equation}\r\ny = softmax(evidence)\r\n\\end{equation}\\\\\r\nⒺ Here softmax is serving as an \"activation\" or \"link\" function, shaping the output of our linear function into the form we want -- in this case, a probability distribution over 10 cases. You can think of it as converting tallies of evidence into probabilities of our input being in each class. It's defined as:\\\\\r\nⒸ 这里的softmax可以看成是一个\\emph{激励}（activation）函数或是\\emph{链接}（link）函数，把我们定义的线性函数的输出转换成我们想要的格式，也就是关于10个数字类的概率分布．因此，给定一张图片，它对于每一个数字的吻合度可以被softmax函数转换成为一个概率值．softmax函数可以定义为：\\\\\r\n\\begin{equation}\r\nsoftmax(x) = normalize(exp(x))\r\n\\end{equation}\\\\\r\nⒺ If you expand that equation out, you get:\\\\\r\n展开等式右边的子式，可以得到：\\\\\r\n\\begin{equation}\r\nsoftmax(x)_i = \\frac{exp(x_i)}{\\sum_j{exp(x_j)}}\r\n\\end{equation}\\\\\r\nⒺ But it's often more helpful to think of softmax the first way: exponentiating its inputs and then normalizing them. The exponentiation means that one more unit of evidence increases the weight given to any hypothesis multiplicatively. And conversely, having one less unit of evidence means that a hypothesis gets a fraction of its earlier weight. No hypothesis ever has zero or negative weight. Softmax then normalizes these weights, so that they add up to one, forming a valid probability distribution. (To get more intuition about the softmax function, check out the \\href{http://neuralnetworksanddeeplearning.com/chap3.html#softmax}{section} on it in Michael Nieslen's book, complete with an interactive visualization.)\\\\\r\nⒸ 但是更多的时候把softmax模型函数定义为第一种形式：把输入值当成幂指数求值，再正则化这些结果值．这个幂运算表示，更大的证据对应更大的假设模型（hypothesis）里面的乘数权重值．反之，拥有更少的证据意味着在假设模型里面拥有更小的乘数系数．假设模型里的权值不可以是0值或者负值．Softmax然后会正则化这些权重值，使它们的总和等于1，以此构造一个有效的概率分布．（更多的关于Softmax函数的信息，可以参考Michael Nieslen的书里面的\\href{http://neuralnetworksanddeeplearning.com/chap3.html#softmax}{这个部分}，其中有关于softmax的可交互式的可视化解释．）\r\n\r\nⒺ You can picture our softmax regression as looking something like the following, although with a lot more $x$s. For each output, we compute a weighted sum of the $x$s, add a bias, and then apply softmax.\\\\\r\n对于softmax回归模型可以用下面的图解释，对于输入的$xs$ 加权求和，再分别加上一个偏置量，最后再输入到softmax函数中：\r\n\\begin{center}\r\n\\includegraphics[width=.65\\textwidth]{../SOURCE/images/softmax-regression-scalargraph.png}\r\n\\end{center}\r\nⒺ If we write that out as equations, we get:\\\\\r\n如果把它写成一个方程，可以得到：\r\n\\begin{center}\r\n\\includegraphics[width=.68\\textwidth]{../SOURCE/images/softmax-regression-scalarequation.png}\r\n\\end{center}\r\nⒺ We can \"vectorize\" this procedure, turning it into a matrix multiplication and vector addition. This is helpful for computational efficiency. (It's also a useful way to think.)\\\\\r\n我们也可以用向量表示这个计算过程：用矩阵乘法和向量相加．这有助于提高计算效率（也是一种更有效的思考方式）．\r\n\\begin{center}\r\n\\includegraphics[width=.68\\textwidth]{../SOURCE/images/softmax-regression-vectorequation.png}\r\n\\end{center}\r\nⒺ More compactly, we can just write:\\\\\r\n更进一步，可以写成更加紧凑的方式：\r\n\\begin{equation}\r\ny = softmax(W_x+b)\r\n\\end{equation}\r\n\r\n\\subsection {实现回归模型}\r\nⒺ To do efficient numerical computing in Python, we typically use libraries like NumPy that do expensive operations such as matrix multiplication outside Python, using highly efficient code implemented in another language. Unfortunately, there can still be a lot of overhead from switching back to Python every operation. This overhead is especially bad if you want to run computations on GPUs or in a distributed manner, where there can be a high cost to transferring data.\r\n\r\n为了在python中高效的进行数值计算，我们通常会调用（如NumPy）外部函数库，把类似矩阵乘法这样的复杂运算使用其他外部语言实现．不幸的是，从外部计算切换回Python的每一个操作，仍然是一个很大的开销．如果你用GPU来进行外部计算，这样的开销会更大．用分布式的计算方式，也会花费更多的资源用来传输数据．\r\n\r\nⒺ TensorFlow also does its heavy lifting outside python, but it takes things a step further to avoid this overhead. Instead of running a single expensive operation independently from Python, TensorFlow lets us describe a graph of interacting operations that run entirely outside Python. (Approaches like this can be seen in a few machine learning libraries.)\r\n\r\nTensorFlow也把复杂的计算放在python之外完成，但是为了避免前面说的那些开销，它做了进一步完善．TensorFlow不单独地运行单一的复杂计算，而是让我们可以先用图描述一系列可交互的计算操作，然后全部一起在Python之外运行．（这样类似的运行方式，可以在不少的机器学习库中看到．）\r\n\r\n\r\nⒺ To use TensorFlow, we need to import it.\r\n\r\n使用TensorFlow之前，首先导入它：\r\n\r\n\\begin{lstlisting}\r\nimport tensorflow as tf\r\n\\end{lstlisting}\r\n\r\nⒺ We describe these interacting operations by manipulating symbolic variables. Let's create one:\r\n\r\n我们通过操作符号变量来描述这些可交互的操作单元，可以用下面的方式创建一个：\r\n\r\n\\begin{lstlisting}\r\nx = tf.placeholder(\"float\", [None, 784])\r\n\\end{lstlisting}\\\\\r\nⒺ \\lstinline{x} isn't a specific value. It's a \\lstinline{placeholder}, a value that we'll input when we ask TensorFlow to run a computation. We want to be able to input any number of MNIST images, each flattened into a 784-dimensional vector. We represent this as a 2-D tensor of floating-point numbers, with a shape \\lstinline{[None, 784]}. (Here None means that a dimension can be of any length.)\\\\\r\n\\lstinline{x} 不是一个特定的值，而是一个占位符\\lstinline{placeholder}，我们在TensorFlow运行计算时输入这个值．我们希望能够输入任意数量的MNIST图像，每一张图展平成784维的向量．我们用2维的浮点数张量来表示这些图，这个张量的形状是 [None，784]．（这里的\\lstinline{None}表示此张量的第一个维度可以是任何长度的．）\r\n\r\n\r\nⒺ We also need the weights and biases for our model. We could imagine treating these like additional inputs, but TensorFlow has an even better way to handle it: \\lstinline{Variable}. A \\lstinline{Variable} is a modifiable tensor that lives in TensorFlow's graph of interacting operations. It can be used and even modified by the computation. For machine learning applications, one generally has the model parameters be \\lstinline{Variables}.\r\n\r\n我们的模型也需要权重值和偏置量，当然我们可以把它们当做是另外的输入（使用占位符），但TensorFlow有一个更好的方法来表示它们：\\lstinline{Variable}． 一个\\lstinline{Variable}代表一个可修改的张量，存在在TensorFlow的用于描述交互性操作的图中．它们可以用于计算输入值，也可以在计算中被修改．对于各种机器学习应用，一般都会有模型参数，可以用\\lstinline{Variable}表示．\r\n\r\n\\begin{lstlisting}\r\nW = tf.Variable(tf.zeros([784,10]))\r\nb = tf.Variable(tf.zeros([10]))\r\n\\end{lstlisting}\r\n\r\nⒺ We create these \\lstinline{Variables} by giving \\lstinline{tf.Variable} the initial value of the \\lstinline{Variable}: in this case, we initialize both W and b as tensors full of zeros. Since we are going to learn \\lstinline{W} and \\lstinline{b}, it doesn't matter very much what they initially are.\r\n\r\n我们赋予\\lstinline{tf.Variable} 不同的初值来创建不同的\\lstinline{Variable}：在这里，我们都用全为零的张量来初始化\\lstinline{W}和\\lstinline{b}．因为我们要学习\\lstinline{W}和\\lstinline{b}的值，它们的初值可以随意设置．\r\n\r\n\r\nⒺ Notice that W has a shape of \\lstinline{[784, 10]} because we want to multiply the 784-dimensional image vectors by it to produce 10-dimensional vectors of evidence for the difference classes. \\lstinline{b} has a shape of \\lstinline{[10]} so we can add it to the output.\r\n\r\n\r\n注意，\\lstinline{W}的维度是\\lstinline{[784，10]}，因为我们想要用784维的图片向量乘以它以得到一个10维的证据值向量，每一位对应不同数字类．\\lstinline{b}的形状是\\lstinline{[10]}，所以我们可以直接把它加到输出上面．\r\n\r\nⒺ We can now implement our model. It only takes one line!\r\n\r\n现在，可以实现我们的模型了，只需以下一行代码：\r\n\r\n\\begin{lstlisting}\r\ny = tf.nn.softmax(tf.matmul(x,W) + b)\r\n\\end{lstlisting}\r\n\r\nⒺ First, we multiply $x$ by $W$ with the expression \\lstinline{tf.matmul(x, W)}. This is flipped from when we multiplied them in our equation, where we had $W_x$, as a small trick to deal with x being a 2D tensor with multiple inputs. We then add b, and finally apply \\lstinline{tf.nn.softmax}.\r\n\r\n首先，我们用\\lstinline{tf.matmul(X，W)}表示$x$乘以$W$，对应之前等式里面的$W_x$，这里$x$是一个2维张量拥有多个输入．然后再加上$b$，把和输入到\\lstinline{tf.nn.softmax}函数里面．\r\n\r\nⒺ That's it. It only took us one line to define our model, after a couple short lines of setup. That isn't because TensorFlow is designed to make a softmax regression particularly easy: it's just a very flexible way to describe many kinds of numerical computations, from machine learning models to physics simulations. And once defined, our model can be run on different devices: your computer's CPU, GPUs, and even phones!\r\n\r\n至此，我们先用了几行简短的代码来设置变量，然后只用了一行代码来定义我们的模型．TensorFlow不仅仅可以使softmax回归模型计算变得特别简单，它也用这种非常灵活的方式来描述其他各种数值计算，从机器学习模型对物理学模拟仿真模型．一旦被定义好之后，我们的模型就可以在不同的设备上运行：计算机的CPU，GPU，甚至是手机！\r\n\r\n\\subsection{训练模型}\r\n\r\nⒺ In order to train our model, we need to define what it means for the model to be good. Well, actually, in machine learning we typically define what it means for a model to be bad, called the cost or loss, and then try to minimize how bad it is. But the two are equivalent.\r\n\r\n为了训练我们的模型，我们首先需要定义一个指标来评估这个模型是好的．其实，在机器学习，我们通常定义指标来表示一个模型是坏的，这个指标称为成本（cost）或损失（loss），然后尽量最小化这个指标．但是，这两种方式是相同的．\r\n\r\nⒺ One very common, very nice cost function is \"cross-entropy.\" Surprisingly, cross-entropy arises from thinking about information compressing codes in information theory but it winds up being an important idea in lots of areas, from gambling to machine learning. It's defined:\\\\\r\n一个非常常见的，非常漂亮的成本函数是“交叉熵”(cross-entropy)．交叉熵产生于信息论里面的信息压缩编码技术，但是它后来演变成为从博弈论到机器学习等其他领域里的重要技术手段．它的定义如下：\r\n\\\\\r\n\\begin{equation}\r\nH_{y'}(y) = -\\sum_i{y_{i}'log(y_i)}\r\n\\end{equation}\r\n\\\\\r\nⒺ where $y$ is our predicted probability distribution, and $y'$ is the true distribution (the one-hot vector we'll input). In some rough sense, the cross-entropy is measuring how inefficient our predictions are for describing the truth. Going into more detail about cross-entropy is beyond the scope of this tutorial, but it's well worth \\href{http://colah.github.io/posts/2015-09-Visual-Information/}{understanding}.\r\n\\\\\r\n$y$是我们预测的概率分布,$y'$是实际的分布（我们输入的one-hot vector)．比较粗糙的理解是，交叉熵是用来衡量我们的预测用于描述真相的低效性．更详细的关于交叉熵的解释超出本教程的范畴，但是你很有必要好好\\href{http://colah.github.io/posts/2015-09-Visual-Information/}{理解}它．\r\n\r\nⒺ To implement cross-entropy we need to first add a new placeholder to input the correct answers:\\\\\r\n为了计算交叉熵，我们首先需要添加一个新的占位符用于输入正确值：\r\n\\\\\r\n\\begin{lstlisting}\r\ny = tf.placeholder(\"float\", [None,10])\r\n\\end{lstlisting}\r\nⒺ Then we can implement the cross-entropy $-\\sum{y'log(y)}$,\\\\\r\n然后我们可以用 $-\\sum{y'log(y)}$ 计算交叉熵:\r\n\r\n\\begin{lstlisting}\r\ncross_entropy = -tf.reduce_sum(y_*tf.log(y))\r\n\\end{lstlisting}\r\n\r\nⒺ First, \\lstinline{tf.log} computes the logarithm of each element of \\lstinline{y.} Next, we multiply each element of \\lstinline{y_} with the corresponding element of \\lstinline{tf.log(y)}. Finally, \\lstinline{tf.reduce_sum} adds all the elements of the tensor.\r\n\r\n首先，用 \\lstinline{tf.log} 计算y的每个元素的对数．接下来，我们把\\lstinline{y_}的每一个元素和\\lstinline{tf.log(y_)}的对应元素相乘．最后，用\\lstinline{tf.reduce_sum}计算张量的所有元素的总和．\r\n\r\nⒺ Note that this isn't just the cross-entropy of the truth with a single prediction, but the sum of the cross-entropies for all the images we looked at. In this example, we have 100 images in each batch: how well we are doing on 100 data points is a much better description of how good our model is than a single data point.\r\n\r\n值得注意的是，这里的交叉熵不仅仅用来衡量单一的一对预测和真实值，而是所有100幅图片的交叉熵的总和．对于100个数据点的预测表现比单一数据点的表现能更好地描述我们的模型的性能．\r\n\r\nⒺ Now that we know what we want our model to do, it's very easy to have TensorFlow train it to do so. Because TensorFlow knows the entire graph of your computations, it can automatically use the \\href{http://colah.github.io/posts/2015-08-Backprop/}{backpropagation algorithm} to efficiently determine how your variables affect the cost you ask it minimize. Then it can apply your choice of optimization algorithm to modify the variables and reduce the cost.\r\n\r\n现在我们知道我们需要我们的模型做什么啦，用TensorFlow来训练它是非常容易的．因为TensorFlow拥有一张描述你各个计算单元的图，它可以自动地使用\\href{http://colah.github.io/posts/2015-08-Backprop/}{反向传播算法(backpropagation algorithm)}来有效地确定你的变量是如何影响你想要最小化的那个成本值的．然后，TensorFlow会用你选择的优化算法来不断地修改变量以降低成本．\r\n\r\n\\begin{lstlisting}\r\ntrain_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)\r\n\\end{lstlisting}\r\n\r\nⒺ In this case, we ask TensorFlow to minimize \\lstinline{cross_entropy} using the gradient descent algorithm with a learning rate of $0.01$. Gradient descent is a simple procedure, where TensorFlow simply shifts each variable a little bit in the direction that reduces the cost. But TensorFlow also provides \\href{https://www.tensorflow.org/versions/master/api_docs/python/train.html#optimizers}{many other optimization algorithms}: using one is as simple as tweaking one line.\r\n\r\n在这里，我们要求TensorFlow用梯度下降算法（gradient descent algorithm）以0.01的学习速率最小化交叉熵．梯度下降算法（gradient descent algorithm）是一个简单的学习过程，TensorFlow只需将每个变量一点点地往使成本不断降低的方向移动．当然TensorFlow也提供了\\href{https://www.tensorflow.org/versions/master/api_docs/python/train.html#optimizers}{其他许多优化算法}：只要简单地调整一行代码就可以使用其他的算法．\\index{梯度下降法}\r\n\r\nⒺ What TensorFlow actually does here, behind the scenes, is it adds new operations to your graph which implement backpropagation and gradient descent. Then it gives you back a single operation which, when run, will do a step of gradient descent training, slightly tweaking your variables to reduce the cost.\r\n\r\nTensorFlow在这里实际上所做的是，它会在后台给描述你的计算的那张图里面增加一系列新的计算操作单元用于实现反向传播算法和梯度下降算法．然后，它返回给你的只是一个单一的操作，当运行这个操作时，它用梯度下降算法训练你的模型，微调你的变量，不断减少成本．\r\n\r\nⒺ Now we have our model set up to train. One last thing before we launch it, we have to add an operation to initialize the variables we created:\r\n\r\n现在，我们已经设置好了我们的模型．在运行计算之前，我们需要添加一个操作来初始化我们创建的变量：\r\n\r\n\\begin{lstlisting}\r\ninit = tf.initialize_all_variables()\r\n\\end{lstlisting}\r\n\\\\\r\nⒺ We can now launch the model in a \\lstinline{Session}, and run the operation that initializes the variables:\\\\\r\n现在我们可以在一个 \\lstinline{Session} 里面启动我们的模型，并且初始化变量：\r\n\\begin{lstlisting}\r\nsess = tf.Session()\r\nsess.run(init)\r\n\\end{lstlisting}\r\n\\\\\r\nⒺ Let's train -- we'll run the training step 1000 times!\\\\\r\n然后开始训练模型，这里我们让模型循环训练1000次！\r\n\\begin{lstlisting}\r\nfor i in range(1000):\r\n    batch_xs, batch_ys = mnist.train.next_batch(100)\r\n    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})\r\n\\end{lstlisting}\r\n\r\nⒺ Each step of the loop, we get a \"batch\" of one hundred random data points from our training set. We run \\lstinline{train_step} feeding in the batches data to replace the placeholders.\r\n\r\n该循环的每个步骤中，我们都会随机抓取训练数据中的100个批处理数据点，然后我们用这些数据点作为参数替换之前的占位符来运行\\lstinline{train_step}．\r\n\r\nⒺ Using small batches of random data is called stochastic training -- in this case, stochastic gradient descent. Ideally, we'd like to use all our data for every step of training because that would give us a better sense of what we should be doing, but that's expensive. So, instead, we use a different subset every time. Doing this is cheap and has much of the same benefit.\r\n\r\n使用一小部分的随机数据来进行训练被称为随机训练(stochastic training)---在这里更确切的说是随机梯度下降训练．理想情况下，我们希望用我们所有的数据来进行每一步的训练，因为这能给我们更好的训练结果，但显然这需要很大的计算开销．所以，每一次训练我们可以使用不同的数据子集，这样做既可以减少计算开销，又可以最大化地学习到数据集的总体特性．\r\n\r\n\\subsection{Evaluating Our Model   ||   评估我们的模型}\r\n\r\nⒺ How well does our model do?\r\n\r\n那么我们的模型性能如何呢？\r\n\r\nⒺ Well, first let's figure out where we predicted the correct label. \\lstinline{tf.argmax} is an extremely useful function which gives you the index of the highest entry in a tensor along some axis. For example, \\lstinline{tf.argmax(y,1)} is the label our model thinks is most likely for each input, while \\lstinline{tf.argmax(y_,1)} is the correct label. We can use \\lstinline{tf.equal} to check if our prediction matches the truth.\r\n\r\n首先让我们找出那些预测正确的标签．\\lstinline{tf.argmax()}是一个非常有用的函数，它能给你在一个张量里沿着某条轴的最高条目的索引值．比如，\\lstinline{tf.argmax(y,1)}是模型认为每个输入最有可能对应的那些标签，而\\lstinline{tf.argmax(y_,1)}代表正确的标签．我们可以用\\lstinline{tf.equal} 来检测我们的预测是否真实标签匹配．\r\n\r\n\\begin{lstlisting}\r\ncorrect_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\r\n\\end{lstlisting}\r\n\r\nⒺ That gives us a list of booleans. To determine what fraction are correct, we cast to floating point numbers and then take the mean. For example, \\lstinline{[True, False, True, True]} would become \\lstinline{[1,0,1,1]} which would become $0.75$.\r\n\r\n这行代码会给我们一组布尔值．为了确定正确预测项的比例，我们可以把布尔值转换成浮点数，然后取平均值．例如，\\lstinline{[True, False, True, True]}会变成\\lstinline{[1,0,1,1]}，取平均值后得到 $0.75$ .\r\n\r\n\\begin{lstlisting}\r\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\r\n\\end{lstlisting}\r\n\r\nⒺ Finally, we ask for our accuracy on our test data.\r\n\r\n最后，我们计算所学习到的模型在测试数据集上面的正确率．\r\n\r\n\\begin{lstlisting}\r\nprint sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})\r\n\\end{lstlisting}\r\n\r\nⒺ This should be about 91\\%.\r\n\r\n最终结果值应该大约是91\\%．\r\n\r\nⒺ Is that good? Well, not really. In fact, it's pretty bad. This is because we're using a very simple model. With some small changes, we can get to 97\\%/. The best models can get to over 99.7\\% accuracy! (For more information, have a look at this \\href{http://rodrigob.github.io/are_we_there_yet/build/classification_datasets_results.html}{list of results})\r\n\r\n这个结果好吗？嗯，并不太好．事实上，这个结果是很差的．这是因为我们仅仅使用了一个非常简单的模型．不过，做一些小小的改进，我们就可以得到97\\%的正确率．最好的模型甚至可以获得超过99.7\\%的准确率！（想了解更多信息，请参考这个\\href{http://rodrigob.github.io/are_we_there_yet/build/classification_datasets_results.html}{结果对比列表}．)\r\n\r\nⒺ What matter is that we learned from this model. Still, if you're feeling a bit down about these results, check out the next tutorial where we do a lot better, and learn how to build more sophisticated models using TensorFlow!\r\n\r\n比结果更重要的是，我们从这个模型中学习到的设计思想．不过，如果你仍然对这里的结果有点失望，可以查看下个教程，在那里你将学到如何用TensorFlow构建更加复杂的模型以获得更好的性能！\r\n\r\n原文地址：\\href{http://tensorflow.org/tutorials/mnist/beginners/index.md}{MNIST For ML Beginners}\r\n"
  },
  {
    "path": "tex_pdf/tutorials/c2s02_minist_pros.tex",
    "content": "%!TEX program = xelatex\r\n% Encoding: UTF8\r\n% SEIKA 2015\r\n\r\n\r\n% Chapter 2 Tutorials\r\n% Section 2.3 minist_pros\r\n\r\n\r\n\\newpage\r\n\\section {\\textcolor{etc}{Deep MNIST for Experts}   ||   深入MNIST} \\label{MINIST_pros}\r\n\r\nⒺ \\textcolor{etc}{TensorFlow is a powerful library for doing large-scale numerical computation. One of the tasks at which it excels is implementing and training deep neural networks. In this tutorial we will learn the basic building blocks of a TensorFlow model while constructing a deep convolutional MNIST classifier.}\r\n\r\nTensorFlow是一个善于大规模数值计算的强大库件。它的一个强项就是训练并实现深度神经网络(deep neural networks)。在本小节中，我们将会学习TensorFlow模型构建的基本方法，并以此构建一个深度卷积MNIST分类器。\r\n\r\nⒺ \\textcolor{etc}{This introduction assumes familiarity with neural networks and the MNIST dataset. If you don't have a background with them, check out the \\hyperref[MINIST_beginner]{introduction for beginners}. Be sure to \\hyperref[download_install]{install TensorFlow} before starting.}\r\n\r\n本教程假设您已经熟悉神经网络和MNIST数据集。如果你尚未了解，请查看\\hyperref[MINIST_beginner]{新手指南}。再开始学习前请确保您已\\hyperref[download_install]{安装TensorFlow}。\r\n\r\n%\r\n%%\r\n\\subsection {Setup  |  安装}\r\n\r\nⒺ \\textcolor{etc}{Before we create our model, we will first load the MNIST dataset, and start a TensorFlow session.}\r\n\r\n在创建模型之前，我们会先加载MNIST数据集，然后启动一个TensorFlow会话。\r\n\r\n\\subsubsection {Load MNIST Data  |  加载MINIST数据}\r\n\r\n\\textcolor{etc}{For your convenience, we've included \\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/mnist/input_data.py}{a script} which automatically downloads and imports the MNIST dataset. It will create a directory \\lstinline{'MNIST_data'} in which to store the data files.}\r\n\r\n为了方便起见，我们已经准备了一个\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/mnist/input_data.py}{脚本}来自动下载和导入MNIST数据集。它会自动创建一个\\lstinline{'MNIST_data'}的目录来存储数据。\r\n\r\n\\begin{lstlisting}\r\nimport input_data\r\nmnist = input_data.read_data_sets('MNIST_data', one_hot=True)\r\n\\end{lstlisting}\r\n\r\n\\textcolor{etc}{Here \\lstinline{mnist} is a lightweight class which stores the training, validation, and testing sets as NumPy arrays. It also provides a function for iterating through data minibatches, which we will use below.}\r\n\r\n此处的 \\lstinline{mnist} 是一个以NumPy数组形式存储训练、验证和测试数据的轻量级类。我们将在之后使用到它提供的一个函数功能，用于迭代按批处理数据。\r\n\r\n\\subsubsection {Start TensorFlow InteractiveSession  |  开始TensorFlow交互会话}\r\n\r\nⒺ \\textcolor{etc}{Tensorflow relies on a highly efficient C++ backend to do its computation. The connection to this backend is called a session. The common usage for TensorFlow programs is to first create a graph and then launch it in a session.}\r\n\r\nTensorflow基于一个高效的C++后台模块进行运算。与这个后台模块的连接叫做\\emph{会话}(session)。TensorFlow编程的常规流程是先创建一个图，然后在session中加载它。\r\n\r\nⒺ \\textcolor{etc}{Here we instead use the convenient InteractiveSession class, which makes TensorFlow more flexible about how you structure your code. It allows you to interleave operations which build a \\hyperref[computation_graph]{computation graph} with ones that run the graph. This is particularly convenient when working in interactive contexts like IPython. If you are not using an InteractiveSession, then you should build the entire computation graph before starting a session and \\hyperref[launching_graph]{launching} the graph.}\r\n\r\n这里，我们使用更加方便的\\emph{交互会话}(InteractiveSession)类，它可以让您更加灵活地构建代码。交互会话能让你在运行图的时候，插入一些构建计算图的操作。这能给使用交互式文本shell如iPython带来便利。如果你没有使用InteractiveSession的话，你需要在开始session和加载图之前，构建整个计算图。\r\n\r\n\\begin{lstlisting}\r\nimport tensorflow as tf\r\nsess = tf.InteractiveSession()\r\n\\end{lstlisting}\r\n\r\n\\subsubsection {Computation Graph  |  计算图}\r\n\r\nⒺ \\textcolor{etc}{To do efficient numerical computing in Python, we typically use libraries like NumPy that do expensive operations such as matrix multiplication outside Python, using highly efficient code implemented in another language. Unfortunately, there can still be a lot of overhead from switching back to Python every operation. This overhead is especially bad if you want to run computations on GPUs or in a distributed manner, where there can be a high cost to transferring data.}\r\n\r\n为了高效地在Python里进行数值计算，我们一般会使用像NumPy这样用其他语言编写的库件，在Python外用其它执行效率高的语言完成这些高运算开销操作（如矩阵运算）。但是，每一步操作依然会需要切换回Python带来很大开销。特别的，这种开销会在GPU运算或是分布式集群运算这类高数据传输需求的运算形式上非常高昂。\r\n\r\nⒺ \\textcolor{etc}{TensorFlow also does its heavy lifting outside Python, but it takes things a step further to avoid this overhead. Instead of running a single expensive operation independently from Python, TensorFlow lets us describe a graph of interacting operations that run entirely outside Python. This approach is similar to that used in Theano or Torch.}\r\n\r\nTensorFlow将高运算量计算放在Python外进行，同时更进一步设法避免上述的额外运算开销。不同于在Python中独立运行运算开销昂贵的操作，TensorFlow让我们可以独立于Python以外以图的形式描述交互式操作。这与Theano、Torch的做法很相似。\r\n\r\nⒺ \\textcolor{etc}{The role of the Python code is therefore to build this external computation graph, and to dictate which parts of the computation graph should be run. See the \\hyperref[computation_graph]{Computation Graph} section of \\hyperref[basic_usage]{Basic Usage} for more detail.}\r\n\r\n因此，这里Python代码的角色是构建其外部将运行的\\emph{计算图}，并决定计算图的哪一部分将被运行。更多的细节和\\hyperref[basic_usage]{基本使用方法}请参阅\\hyperref[computation_graph]{计算图}章节。\r\n\r\n%\r\n%%\r\n\\subsection{Build a Softmax Regression Model  ||  构建 Softmax 回归模型}\r\n\r\nⒺ \\textcolor{etc}{In this section we will build a softmax regression model with a single linear layer. In the next section, we will extend this to the case of softmax regression with a multilayer convolutional network.}\r\n\r\n在这小节里，我们将会构建一个包含单个线性隐层的 softmax 回归模型。我们将在下一小结把它扩展成多层卷积网络 softmax回归模型。\r\n\\index{Softmax regression}\r\n\r\n%\r\n\\subsubsection{Placeholder  |  占位符}\r\n\r\nⒺ \\textcolor{etc}{We start building the computation graph by creating nodes for the input images and target output classes.}\r\n\r\n我们先从创建输入图像和输出类别的节点来创建计算图。\r\n\r\n\\begin{lstlisting}\r\nx = tf.placeholder(\"float\", shape=[None, 784])\r\ny_ = tf.placeholder(\"float\", shape=[None, 10])\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{Here \\lstinline{x} and \\lstinline{y} aren't specific values. Rather, they are each a \\lstinline{placeholder} --- a value that we'll input when we ask TensorFlow to run a computation.}\r\n\r\n这里的\\lstinline{x}和\\lstinline{y}并不代表具体值，他们是一个\\emph{占位符}(\\lstinline{placeholder}) --- 当TensorFlow运行时需要赋值的变量。\r\n\r\nⒺ \\textcolor{etc}{The input images \\lstinline{x} will consist of a 2d tensor of floating point numbers. Here we assign it a \\lstinline{shape} of \\lstinline{[None, 784]}, where \\lstinline{784} is the dimensionality of a single flattened MNIST image, and None indicates that the first dimension, corresponding to the batch size, can be of any size. The target output classes \\lstinline{y_} will also consist of a 2d tensor, where each row is a one-hot 10-dimensional vector indicating which digit class the corresponding MNIST image belongs to.}\r\n\r\n输入图片\\lstinline{x}是由浮点数值组成的2维张量(tensor)。这里，我们定义它为\\lstinline{[None, 784]}的\\lstinline{shape}，其中\\lstinline{784}是单张展开的MNIST图片的维度数。\\lstinline{None}对应\\lstinline{shape}的第一个维度，代表了这批输入图像的数量，可能是任意值。目标输出类\\lstinline{y_}也是一个2维张量，其中每一行为一个10维向量代表对应MNIST图片的所属数字的类别。\r\n\r\nⒺ \\textcolor{etc}{The shape argument to placeholder is optional, but it allows TensorFlow to automatically catch bugs stemming from inconsistent tensor shapes.}\r\n\r\n虽然\\lstinline{placeholder}的\\lstinline{shape}参数是可选的，但有了它，TensorFlow能够自动捕捉因数据维度不一致导致的错误。\r\n\r\n\r\n%%%%%\r\n% I, Seika, have revised to here\r\n% \\fcolorbox{gray}{yellow}{test 张量 \\emph{矢量} 1234 \\lstinline{shape}}\r\n%%%%%\r\n\r\n\\subsubsection{Variables  |  变量}\r\n\r\nⒺ \\textcolor{etc}{We now define the weights \\lstinline{W} and biases \\lstinline{b} for our model. We could imagine treating these like additional inputs, but TensorFlow has an even better way to handle them: \\lstinline{Variable}. A \\lstinline{Variable} is a value that lives in TensorFlow's computation graph. It can be used and even modified by the computation. In machine learning applications, one generally has the model parameters be \\lstinline{Variables}.}\r\n\r\n我们现在为模型定义权重\\lstinline{W}和偏置\\lstinline{b}。它们可以被视作是额外的输入量，但是TensorFlow有一个更好的方式来处理：\\lstinline{Variable}。一个\\lstinline{Variable}代表着在TensorFlow计算图中的一个值，它是能在计算过程中被读取和修改的。在机器学习的应用过程中，模型参数一般用\\lstinline{Variable}来表示。\r\n\r\n\\begin{lstlisting}\r\nW = tf.Variable(tf.zeros([784,10]))\r\nb = tf.Variable(tf.zeros([10]))\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{We pass the initial value for each parameter in the call to \\lstinline{tf.Variable}. In this case, we initialize both \\lstinline{W} and \\lstinline{b} as tensors full of zeros. \\lstinline{W} is a $784\\times10$ matrix (because we have $784$ input features and $10$ outputs) and \\lstinline{b} is a 10-dimensional vector (because we have $10$ classes).}\r\n\r\n我们在调用\\lstinline{tf.Variable}的时候传入初始值。在这个例子里，我们把\\lstinline{W}和\\lstinline{b}都初始化为零向量。\\lstinline{W}是一个$784\\times10$的矩阵（因为我们有784个特征和10个输出值）。\\lstinline{b}是一个10维的向量（因为我们有10个分类）。\r\n\r\nⒺ \\textcolor{etc}{Before \\lstinline{Variables} can be used within a session, they must be initialized using that session. This step takes the initial values (in this case tensors full of zeros) that have already been specified, and assigns them to each \\lstinline{Variable}. This can be done for all \\lstinline{Variables} at once.}\r\n\r\n\\lstinline{Variable}需要在\\lstinline{session}之前初始化，才能在\\lstinline{session}中使用。初始化需要初始值（本例当中是全为零）传入并赋值给每一个\\lstinline{Variable}。这个操作可以一次性完成。\r\n\r\n\\begin{lstlisting}\r\nsess.run(tf.initialize_all_variables())\r\n\\end{lstlisting}\r\n\r\n%\r\n%%\r\n\\subsubsection{Predicted Class and Cost Function  |  预测分类与损失函数}\r\n\r\nⒺ \\textcolor{etc}{We can now implement our regression model. It only takes one line! We multiply the vectorized input images \\lstinline{x} by the weight matrix \\lstinline{W}, add the bias \\lstinline{b}, and compute the softmax probabilities that are assigned to each class.}\r\n\r\n现在我们可以实现我们的regression模型了。这只需要一行！我们把图片\\lstinline{x}和权重矩阵\\lstinline{W}相乘，加上偏置\\lstinline{b}，然后计算每个分类的softmax概率值。\r\n\r\n\\begin{lstlisting}\r\ny = tf.nn.softmax(tf.matmul(x,W) + b)\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{The cost function to be minimized during training can be specified just as easily. Our cost function will be the cross-entropy between the target and the model's prediction.}\r\n\r\n在训练中最小化损失函数同样很简单。我们这里的损失函数用目标分类和模型预测分类之间的交叉熵。\r\n\r\n\\begin{lstlisting}\r\ncross_entropy = -tf.reduce_sum(y_*tf.log(y))\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{Note that \\lstinline{tf.reduce_sum} sums across all images in the minibatch, as well as all classes. We are computing the cross entropy for the entire minibatch.}\r\n\r\n注意，\\lstinline{tf.reduce_sum}把\\lstinline{minibatch}里的每张图片的交叉熵值都加起来了。我们计算的交叉熵是指整个\\lstinline{minibatch}的。\r\n\r\n%\r\n%%\r\n\\subsection{Train the Model | 训练模型}\r\n\r\nⒺ \\textcolor{etc}{Now that we have defined our model and training cost function, it is straightforward to train using TensorFlow. Because TensorFlow knows the entire computation graph, it can use automatic differentiation to find the gradients of the cost with respect to each of the variables. TensorFlow has a variety of \\hyperref[optimizers]{builtin optimization algorithms}. For this example, we will use steepest gradient descent, with a step length of 0.01, to descend the cross entropy.}\r\n\r\n我们已经定义好了模型和训练的时候用的损失函数，接下来使用TensorFlow来训练。因为TensorFlow知道整个计算图，它会用自动微分法来找到损失函数对于各个变量的梯度。TensorFlow有大量\\hyperref[optimizers]{内置优化算法}，这个例子中，我们用快速梯度下降法让交叉熵下降，步长为0.01。\r\n\r\n\\begin{lstlisting}\r\ntrain_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{What TensorFlow actually did in that single line was to add new operations to the computation graph. These operations included ones to compute gradients, compute parameter update steps, and apply update steps to the parameters.}\r\n\r\n这一行代码实际上是用来往计算图上添加一个新操作，其中包括计算梯度，计算每个参数的步长变化，并且计算出新的参数值。\r\n\r\nⒺ \\textcolor{etc}{The returned operation \\lstinline{train_step}, when run, will apply the gradient descent updates to the parameters. Training the model can therefore be accomplished by repeatedly running \\lstinline{train_step}.}\r\n\r\n\\lstinline{train_step}这个操作，用梯度下降来更新权值。因此，整个模型的训练可以通过反复地运行\\lstinline{train_step}来完成。\r\n\r\n\\begin{lstlisting}\r\nfor i in range(1000):\r\n    batch = mnist.train.next_batch(50)\r\n    train_step.run(feed_dict={x: batch[0], y_: batch[1]})\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{Each training iteration we load 50 training examples. We then run the \\lstinline{train_step} operation, using \\lstinline{feed_dict} to replace the \\lstinline{placeholder} tensors \\lstinline{x} and \\lstinline{y_} with the training examples. Note that you can replace any tensor in your computation graph using \\lstinline{feed_dict} --- it's not restricted to just \\lstinline{placeholders}.}\r\n\r\n每一步迭代，我们都会加载50个训练样本，然后执行一次\\lstinline{train_step，使用}\\lstinline{feed_dict}，用训练数据替换\\lstinline{placeholder}向量\\lstinline{x}和\\lstinline{y_}。注意，在计算图中，你可以用\\lstinline{feed_dict}来替代任何张量，并不仅限于替换\\lstinline{placeholder}。\r\n\r\n\\subsubsection{Evaluate the Model  |  评估模型}\r\n\r\nⒺ \\textcolor{etc}{How well did our model do?}\r\n\r\n我们的模型效果怎样？\r\n\r\nⒺ \\textcolor{etc}{First we'll figure out where we predicted the correct label. \\lstinline{tf.argmax} is an extremely useful function which gives you the index of the highest entry in a tensor along some axis. For example, \\lstinline{tf.argmax(y,1)} is the label our model thinks is most likely for each input, while \\lstinline{tf.argmax(y_,1)} is the true label. We can use \\lstinline{tf.equal} to check if our prediction matches the truth.}\r\n\r\n首先，要先知道我们哪些label是预测正确了。\\lstinline{tf.argmax}是一个非常有用的函数。它会返回一个张量某个维度中的最大值的索引。例如，\\lstinline{tf.argmax(y,1)}表示我们模型对每个输入的最大概率分类的分类值。而 \\lstinline{tf.argmax(y_,1)}表示真实分类值。我们可以用\\lstinline{tf.equal}来判断我们的预测是否与真实分类一致。\r\n\r\n\\begin{lstlisting}\r\ncorrect_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{That gives us a list of booleans. To determine what fraction are correct, we cast to floating point numbers and then take the mean. For example, \\lstinline{[True, False, True, True]} would become \\lstinline{[1,0,1,1]} which would become \\lstinline{0.75}.}\r\n\r\n这里返回一个布尔数组。为了计算我们分类的准确率，我们将布尔值转换为浮点数来代表对、错，然后取平均值。例如：\\lstinline{[True, False, True, True]}变为\\lstinline{[1,0,1,1]}，计算出平均值为\\lstinline{0.75}。\r\n\r\n\\begin{lstlisting}\r\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{Finally, we can evaluate our accuracy on the test data. This should be about 91\\% correct.}\r\n\r\n最后，我们可以计算出在测试数据上的准确率，大概是91\\%。\r\n\r\n\\begin{lstlisting}\r\nprint accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels})\r\n\\end{lstlisting}\r\n\r\n%\r\n%%\r\n\\subsection{Build a Multilayer Convolutional Network  |  构建多层卷积网络模型}\r\n\r\nⒺ \\textcolor{etc}{Getting 91\\% accuracy on MNIST is bad. It's almost embarrassingly bad. In this section, we'll fix that, jumping from a very simple model to something moderately sophisticated: a small convolutional neural network. This will get us to around 99.2\\% accuracy --- not state of the art, but respectable.}\r\n\r\n在MNIST上只有91\\%正确率，实在太糟糕。在这个小节里，我们用一个稍微复杂的模型：卷积神经网络来改善效果。这会达到大概99.2\\%的准确率。虽然不是最高，但是还是比较让人满意。\r\n\\index{卷积神经网络}\r\n\r\n%\r\n\\subsubsection{Weight Initialization | 权重初始化}\r\n\r\nⒺ \\textcolor{etc}{To create this model, we're going to need to create a lot of weights and biases. One should generally initialize weights with a small amount of noise for symmetry breaking, and to prevent 0 gradients. Since we're using ReLU neurons, it is also good practice to initialize them with a slightly positive initial bias to avoid \"dead neurons.\" Instead of doing this repeatedly while we build the model, let's create two handy functions to do it for us.}\r\n\r\n在创建模型之前，我们先来创建权重和偏置。一般来说，初始化时应加入轻微噪声，来打破对称性，防止零梯度的问题。因为我们用的是ReLU，所以用稍大于0的值来初始化偏置能够避免节点输出恒为0的问题（dead neurons）。为了不在建立模型的时候反复做初始化操作，我们定义两个函数用于初始化。\r\n\r\n\\begin{lstlisting}\r\ndef weight_variable(shape):\r\n    initial = tf.truncated_normal(shape, stddev=0.1)\r\n    return tf.Variable(initial)\r\n\r\ndef bias_variable(shape):\r\n    initial = tf.constant(0.1, shape=shape)\r\n    return tf.Variable(initial)\r\n\\end{lstlisting}\r\n\r\n\\subsubsection{Convolution and Pooling  |  卷积和池化}\r\n\r\nⒺ \\textcolor{etc}{TensorFlow also gives us a lot of flexibility in convolution and pooling operations. How do we handle the boundaries? What is our stride size? In this example, we're always going to choose the vanilla version. Our convolutions uses a stride of one and are zero padded so that the output is the same size as the input. Our pooling is plain old max pooling over $2\\times2$ blocks. To keep our code cleaner, let's also abstract those operations into functions.}\r\n\r\nTensorFlow在卷积和池化上有很强的灵活性。我们怎么处理边界？步长应该设多大？在这个实例里，我们会一直使用vanilla版本。我们的卷积使用1步长（stride size），0边距（padding size）的模板，保证输出和输入是同一个大小。我们的池化用简单传统的$2\\times2$大小的模板做max pooling。为了代码更简洁，我们把这部分抽象成一个函数。\r\n\r\n\\begin{lstlisting}\r\ndef conv2d(x, W):\r\n    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')\r\n\r\ndef max_pool_2x2(x):\r\n    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')\r\n\\end{lstlisting}\r\n\r\n\\subsubsection{First Convolutional Layer  |  第一层卷积}\r\n\r\nⒺ \\textcolor{etc}{We can now implement our first layer. It will consist of convolution, followed by max pooling. The convolutional will compute 32 features for each $5\\times5$ patch. Its weight tensor will have a shape of \\lstinline{[5, 5, 1, 32]}. The first two dimensions are the patch size, the next is the number of input channels, and the last is the number of output channels. We will also have a bias vector with a component for each output channel.}\r\n\r\n现在我们可以开始实现第一层了。它由一个卷积接一个max pooling完成。卷积在每个$5\\times5$的patch中算出32个特征。权重是一个\\lstinline{[5, 5, 1, 32]}的张量，前两个维度是patch的大小，接着是输入的通道数目，最后是输出的通道数目。输出对应一个同样大小的偏置向量。\r\n\r\n\\begin{lstlisting}\r\nW_conv1 = weight_variable([5, 5, 1, 32])\r\nb_conv1 = bias_variable([32])\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{To apply the layer, we first reshape \\lstinline{x} to a 4d tensor, with the second and third dimensions corresponding to image width and height, and the final dimension corresponding to the number of color channels.}\r\n\r\n为了用这一层，我们把\\lstinline{x}变成一个4d向量，第2、3维对应图片的宽高，最后一维代表颜色通道。\r\n\r\n\\begin{lstlisting}\r\nx_image = tf.reshape(x, [-1,28,28,1])\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{We then convolve \\lstinline{x_image} with the weight tensor, add the bias, apply the ReLU function, and finally max pool.}\r\n\r\n我们把\\lstinline{x_image}和权值向量进行卷积相乘，加上偏置，使用ReLU激活函数，最后max pooling。\r\n\r\n\\begin{lstlisting}\r\nh_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)\r\nh_pool1 = max_pool_2x2(h_conv1)\r\n\\end{lstlisting}\r\n\r\n%\r\n\\subsubsection{Second Convolutional Layer  |  第二层卷积}\r\n\r\nⒺ \\textcolor{etc}{In order to build a deep network, we stack several layers of this type. The second layer will have 64 features for each $5\\times5$ patch.}\r\n\r\n为了构建一个更深的网络，我们会把几个类似的层堆叠起来。第二层中，每个5x5的patch会得到64个特征。\r\n\r\n\\begin{lstlisting}\r\nW_conv2 = weight_variable([5, 5, 32, 64])\r\nb_conv2 = bias_variable([64])\r\n\r\nh_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)\r\nh_pool2 = max_pool_2x2(h_conv2)\r\n\\end{lstlisting}\r\n\r\n%\r\n\\subsubsection{Densely Connected Layer  |  密集连接层}\r\n\r\nⒺ \\textcolor{etc}{Now that the image size has been reduced to $7\\times7$, we add a fully-connected layer with 1024 neurons to allow processing on the entire image. We reshape the tensor from the pooling layer into a batch of vectors, multiply by a weight matrix, add a bias, and apply a ReLU.}\r\n\r\n现在，图片降维到$7\\times7$，我们加入一个有1024个神经元的全连接层，用于处理整个图片。我们把池化层输出的张量reshape成一些向量，乘上权重矩阵，加上偏置，使用ReLU激活。\r\n\r\n\\begin{lstlisting}\r\nW_fc1 = weight_variable([7 * 7 * 64, 1024])\r\nb_fc1 = bias_variable([1024])\r\n\r\nh_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])\r\nh_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)\r\n\\end{lstlisting}\r\n\r\n\\textbf{Dropout}\r\n\r\nⒺ \\textcolor{etc}{To reduce overfitting, we will apply dropout before the readout layer. We create a placeholder for the probability that a neuron's output is kept during dropout. This allows us to turn dropout on during training, and turn it off during testing. TensorFlow's tf.nn.dropout op automatically handles scaling neuron outputs in addition to masking them, so dropout just works without any additional scaling.}\r\n\r\n为了减少过拟合，我们在输出层之前加入dropout。我们用一个placeholder来代表一个神经元在dropout中被保留的概率。这样我们可以在训练过程中启用dropout，在测试过程中关闭dropout。 TensorFlow的\\lstinline{tf.nn.dropout}操作会自动处理神经元输出值的scale。所以用dropout的时候可以不用考虑scale。\r\n\r\n\\begin{lstlisting}\r\nkeep_prob = tf.placeholder(\"float\")\r\nh_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)\r\n\\end{lstlisting}\r\n\r\n%\r\n\\subsubsection{Readout Layer  |  输出层}\r\n\r\nⒺ \\textcolor{etc}{Finally, we add a softmax layer, just like for the one layer softmax regression above.}\r\n\r\n最后，我们添加一个softmax层，就像前面的单层softmax regression一样。\r\n\r\n\\begin{lstlisting}\r\nW_fc2 = weight_variable([1024, 10])\r\nb_fc2 = bias_variable([10])\r\n\r\ny_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)\r\n\\end{lstlisting}\r\n\r\n%\r\n\\subsubsection{Train and Evaluate the Model  |  训练和评估模型}\r\n\r\nⒺ \\textcolor{etc}{How well does this model do? To train and evaluate it we will use code that is nearly identical to that for the simple one layer SoftMax network above. The differences are that: we will replace the steepest gradient descent optimizer with the more sophisticated ADAM optimizer; we will include the additional parameter \\lstinline{keep_prob} in \\lstinline{feed_dict} to control the dropout rate; and we will add logging to every 100th iteration in the training process.}\r\n\r\n这次效果又有多好呢？我们用前面几乎一样的代码来测测看。只是我们会用更加复杂的ADAM优化器来做梯度最速下降，在feed\\_dict中加入额外的参数keep\\_prob来控制dropout比例。然后每100次迭代输出一次日志。\r\n\r\n\\begin{lstlisting}\r\ncross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))\r\ntrain_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)\r\ncorrect_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))\r\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\r\nsess.run(tf.initialize_all_variables())\r\nfor i in range(20000):\r\n  batch = mnist.train.next_batch(50)\r\n  if i%100 == 0:\r\n    train_accuracy = accuracy.eval(feed_dict={\r\n        x:batch[0], y_: batch[1], keep_prob: 1.0})\r\n    print \"step %d, training accuracy %g\"%(i, train_accuracy)\r\n  train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})\r\n\r\nprint \"test accuracy %g\"%accuracy.eval(feed_dict={\r\n    x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})\r\n\\end{lstlisting}\r\n\r\nⒺ \\textcolor{etc}{The final test set accuracy after running this code should be approximately 99.2\\%.}\r\n\r\n以上代码，在最终测试集上的准确率大概是99.2\\%。\r\n\r\nⒺ \\textcolor{etc}{We have learned how to quickly and easily build, train, and evaluate a fairly sophisticated deep learning model using TensorFlow.}\r\n\r\n目前为止，我们已经学会了用TensorFlow来快速和简易地搭建、训练和评估一个复杂一点儿的深度学习模型。\r\n\r\n原文地址：\\href{https://www.tensorflow.org/versions/master/tutorials/mnist/pros/index.html#deep-mnist-for-experts}{Deep MNIST for Experts}\r\n"
  },
  {
    "path": "tex_pdf/tutorials/c2s03_.tex",
    "content": "%!TEX program = xelatex\r\n% Encoding: UTF8\r\n% SEIKA 2015\r\n\r\n\r\n% Chapter 2 Tutorials\r\n% Section 2.4 TensorFlow Mechanics 101\r\n\r\n\r\n\\newpage\r\n\\section {TensorFlow Mechanics 101} \\label{tf_mech101}\r\n\r\nⒺ \\textcolor{etc}{\\textbf{Code}: \\href{https://tensorflow/g3doc/tutorials/mnist/}{tensorflow/examples/tutorials/mnist/}}\r\n\r\n\\textbf{代码地址}: \\href{https://tensorflow/g3doc/tutorials/mnist/}{tensorflow/g3doc/tutorials/mnist/}\r\n\r\nⒺ \\textcolor{etc}{The goal of this tutorial is to show how to use TensorFlow to train and evaluate a simple feed-forward neural network for handwritten digit classification using the (classic) MNIST data set. The intended audience for this tutorial is experienced machine learning users interested in using TensorFlow.}\r\n\r\n本篇教程的目的，是向大家展示如何利用TensorFlow使用（经典）MNIST数据集训练并评估一个用于识别手写数字的简易前馈神经网络（feed-forward neural network）。我们的目标读者是有兴趣使用TensorFlow的机器学习资深人士。\r\n\r\nⒺ \\textcolor{etc}{These tutorials are not intended for teaching Machine Learning in general.}\r\n\r\n因此，撰写该系列教程并不是为了教大家机器学习领域的基础知识。\r\n\r\nⒺ \\textcolor{etc}{Please ensure you have followed the instructions to \\href{https://www.tensorflow.org/versions/master/get_started/os_setup.html}{install TensorFlow}.}\r\n\r\n在学习本教程之前，请确保您已按照安装TensorFlow教程中的要求，完成了\\href{https://www.tensorflow.org/versions/master/get_started/os_setup.html}{安装}。\r\n\r\n\\subsection {教程使用的文件} \\label{minist_tf}\r\n\r\n本教程引用如下文件：\r\n\r\n% add table here\r\n\r\n只需要直接运行fully\\_connected\\_feed.py文件，就可以开始训练：\r\n\r\npython fully\\_connected\\_feed.py\r\n\r\n\\subsection {准备数据}\r\n\r\nMNIST是机器学习领域的一个经典问题，指的是让机器查看一系列大小为$28\\times28$像素的手写数字灰度图像，并判断这些图像代表0--9中的哪一个数字。\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics[width=.55\\textwidth]{../SOURCE/images/MNIST.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\n更多相关信息，请查阅Yann LeCun网站中关于MNIST的介绍 或者Chris Olah对MNIST的可视化探索。\r\n\r\n\\subsubsection{下载}\r\n\r\n在\\lstinline{run\\_training()}方法的一开始，\\lstinline{input\\_data.read\\_data\\_sets()}函数会确保你的本地训练文件夹中，已经下载了正确的数据，然后将这些数据解压并返回一个含有\\lstinline{DataSet}实例的字典。\r\n\r\n\\begin{lstlisting}\r\ndata_sets = input_data.read_data_sets(FLAGS.train_dir, FLAGS.fake_data)\r\n\\end{lstlisting}\\footnote{\\lstinline{fake_data}标记是用于单元测试的，读者可以不必理会。}\r\n\r\n% 数据集 | 目的\r\n% --- | ---\r\n% `data_sets.train` | 55000个图像和标签（labels），作为主要训练集。\r\n% `data_sets.validation` | 5000个图像和标签，用于迭代验证训练准确度。\r\n% `data_sets.test` | 10000个图像和标签，用于最终测试训练准确度（trained accuracy）。\r\n\r\nAdd table here\\footnote{了解更多数据有关信息，请查阅此系列教程的[数据下载](mnist/download/index.md)\r\n部分.}\r\n\r\n\\subsubsection{输入与占位符}\r\n% ### 输入与占位符（Inputs and Placeholders） <a class=\"md-anchor\" id=\"AUTOGENERATED-inputs-and-placeholders\"></a>\r\n\r\n\\lstinline{placeholder_inputs()}函数将生成两个\\lstinline{tf.placeholder}\r\n%[`tf.placeholder`](../api_docs/python/io_ops.md#placeholder)\r\n操作，定义传入图表中的shape参数，shape参数中包括\\lstinline{batch_size}值，后续还会将实际的训练用例传入图表。\r\n\r\n\\begin{lstlisting}\r\nimages_placeholder = tf.placeholder(tf.float32, shape=(batch_size, IMAGE_PIXELS))\r\nlabels_placeholder = tf.placeholder(tf.int32, shape=(batch_size))\r\n\\end{lstlisting}\r\n\r\n在训练循环（training loop）的后续步骤中，传入的整个图像和标签数据集会被切片，以符合每一个操作所设置的 \\lstinline{batch_size}值，占位符操作将会填补以符合这个\\lstinline{batch_size}值。然后使用\\lstinline{feed_dict}参数，将数据传入\\lstinline{sess.run()}函数。\r\n\r\n\\subsection {构建图表 （Build the Graph）}\r\n%## 构建图表 （Build the Graph）<a class=\"md-anchor\" id=\"AUTOGENERATED-build-the-graph\"></a>\r\n\r\n在为数据创建占位符之后，就可以运行\\lstinline{mnist.py}文件，经过三阶段的模式函数操作：\\lstinline{inference()}， \\lstinline{loss()\\lstinline}，和\\lstinline{training()}。图表就构建完成了。\r\n\r\n\\begin{enumerate}\r\n\r\n\\item \\lstinline{inference()} —— 尽可能地构建好图表，满足促使神经网络向前反馈并做出预测的要求。\r\n\r\n\\item \\lstinline{loss()} —— 往inference图表中添加生成损失（loss）所需要的操作（ops）。\r\n\r\n\\item \\lstinline{training()} —— 往损失图表中添加计算并应用梯度（gradients）所需的操作。\r\n\\end{enumerate}\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics[width=.95\\textwidth]{../SOURCE/images/mnist_subgraph.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\n\\subsubsection{推理（Inference）}\r\n%### 推理（Inference） <a class=\"md-anchor\" id=\"AUTOGENERATED-inference\"></a>\r\n\r\n\\lstinline{inference()}函数会尽可能地构建图表，做到返回包含了预测结果（output prediction）的Tensor。\r\n\r\n它接受图像占位符为输入，在此基础上借助ReLu(Rectified Linear Units)激活函数，构建一对完全连接层（layers），以及一个有着十个节点（node）、指明了输出logtis模型的线性层。\r\n\r\n每一层都创建于一个唯一的\\lstinline{tf.name_scope}%(../api_docs/python/framework.md#name_scope)\r\n之下，创建于该作用域之下的所有元素都将带有其前缀。\r\n\r\n\\begin{lstlisting}\r\nwith tf.name_scope('hidden1') as scope:\r\n\\end{lstlisting}\r\n\r\n在定义的作用域中，每一层所使用的权重和偏差都在\\lstinline{tf.Variable}\r\n%(../api_docs/python/state_ops.md#Variable)\r\n实例中生成，并且包含了各自期望的shape。\r\n\r\n\\begin{lstlisting}\r\nweights = tf.Variable(tf.truncated_normal([IMAGE_PIXELS, hidden1_units], stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))), name='weights')\r\nbiases = tf.Variable(tf.zeros([hidden1_units]), name='biases')\r\n\\end{lstlisting}\r\n\r\n例如，当这些层是在\\lstinline{hidden1}作用域下生成时，赋予权重变量的独特名称将会是\"\\lstinline{hidden1/weights}\"。\r\n\r\n每个变量在构建时，都会获得初始化操作（initializer ops）。\r\n\r\n在这种最常见的情况下，通过\\lstinline{tf.truncated_normal}\r\n%(../api_docs/python/constant_op.md#truncated_normal)\r\n函数初始化权重变量，给赋予的shape则是一个二维tensor，其中第一个维度代表该层中权重变量所连接（connect from）的单元数量，第二个维度代表该层中权重变量所连接到的（connect to）单元数量。对于名叫\\lstinline{hidden1}的第一层，相应的维度则是\\lstinline{[IMAGE_PIXELS, hidden1_units]}，因为权重变量将图像输入连接到了\\lstinline{hidden1}层。\\lstinline{tf.truncated_normal}初始函数将根据所得到的均值和标准差，生成一个随机分布。\r\n\r\n然后，通过\\lstinline{tf.zeros}\r\n%(../api_docs/python/constant_op.md#zeros)\r\n函数初始化偏差变量（biases），确保所有偏差的起始值都是0，而它们的shape则是其在该层中所接到的（connect to）单元数量。\r\n\r\n图表的三个主要操作，分别是两个\\lstinline{tf.nn.relu}\r\n%(../api_docs/python/nn.md#relu)\r\n操作，它们中嵌入了隐藏层所需的\\lstinline{tf.matmul}\r\n%(../api_docs/python/math_ops.md#matmul)\r\n；以及logits模型所需的另外一个\\lstinline{tf.matmul}。三者依次生成，各自的\\lstinline{tf.Variable}实例则与输入占位符或下一层的输出tensor所连接。\r\n\r\n\\begin{lstlisting}\r\nhidden1 = tf.nn.relu(tf.matmul(images, weights) + biases)\r\n\\end{lstlisting}\r\n\r\n\\begin{lstlisting}\r\nhidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases)\r\n\\end{lstlisting}\r\n\r\n\\begin{lstlisting}\r\nlogits = tf.matmul(hidden2, weights) + biases\r\n\\end{lstlisting}\r\n\r\n最后，程序会返回包含了输出结果的`logits`Tensor。\r\n\r\n\\subsubsection{损失（Loss）}\r\n\r\n\\lstinline{loss()}函数通过添加所需的损失操作，进一步构建图表。\r\n\r\n首先，\\lstinline{labels_placeholer}中的值，将被编码为一个含有1-hot values的Tensor。例如，如果类标识符为“3”，那么该值就会被转换为：\r\n\\lstinline{[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]}\r\n\r\n\\begin{lstlisting}\r\nbatch_size = tf.size(labels)\r\nlabels = tf.expand_dims(labels, 1)\r\nindices = tf.expand_dims(tf.range(0, batch_size, 1), 1)\r\nconcated = tf.concat(1, [indices, labels])\r\nonehot_labels = tf.sparse_to_dense(\r\n    concated, tf.pack([batch_size, NUM_CLASSES]), 1.0, 0.0)\r\n\\end{lstlisting}\r\n\r\n之后，又添加一个\\lstinline{tf.nn.softmax_cross_entropy_with_logits}\r\n%`](../api_docs/python/nn.md#softmax_cross_entropy_with_logits)\r\n操作\\footnote{交叉熵是信息理论中的概念，可以让我们描述如果基于已有事实，相信神经网络所做的推测最坏会导致什么结果。更多详情，请查阅博文《可视化信息理论》(http://colah.github.io/posts/2015-09-Visual-Information/)}，用来比较\\lstinline{inference()}函数与1-hot标签所输出的logits Tensor。\r\n\r\n\\begin{lstlisting}\r\ncross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, onehot_labels, name='xentropy')\r\n\\end{lstlisting}\r\n\r\n然后，使用\\lstinline{tf.reduce_mean}\r\n%(../api_docs/python/math_ops.md#reduce_mean)\r\n函数，计算batch维度（第一维度）下交叉熵（cross entropy）的平均值，将将该值作为总损失。\r\n\r\n\\begin{lstlisting}\r\nloss = tf.reduce_mean(cross_entropy, name='xentropy_mean')\r\n\\end{lstlisting}\r\n\r\n最后，程序会返回包含了损失值的Tensor。\r\n\r\n\\subsubsection{训练}\r\n\r\n\\lstinline{training()}函数添加了通过梯度下降（gradient descent）将损失最小化所需的操作。\r\n\r\n首先，该函数从\\lstinline{loss()}函数中获取损失Tensor，将其交给\\lstinline{[tf.scalar_summary]}\r\n% ](../api_docs/python/train.md#scalar_summary)\r\n，后者在与\\lstinline{SummaryWriter}（见下文）配合使用时，可以向事件文件（events file）中生成汇总值（summary values）。在本篇教程中，每次写入汇总值时，它都会释放损失Tensor的当前值（snapshot value）。\r\n\r\n\\begin{lstlisting}\r\ntf.scalar_summary(loss.op.name, loss)\r\n\\end{lstlisting}\r\n\r\n接下来，我们实例化一个\\lstinline{[tf.train.GradientDescentOptimizer]}\r\n% (../api_docs/python/train.md#GradientDescentOptimizer)\r\n，负责按照所要求的学习效率（learning rate）应用梯度下降法（gradients）。\r\n\r\n\\begin{lstlisting}\r\noptimizer = tf.train.GradientDescentOptimizer(FLAGS.learning_rate)\r\n\\end{lstlisting}\r\n\r\n之后，我们生成一个变量用于保存全局训练步骤（global training step）的数值，并使用\\lstinline{minimize()}\r\n% (../api_docs/python/train.md#Optimizer.minimize)\r\n函数更新系统中的三角权重（triangle weights）、增加全局步骤的操作。根据惯例，这个操作被称为\\lstinline{train_op}，是TensorFlow会话（session）诱发一个完整训练步骤所必须运行的操作（见下文）。\r\n\r\n\\begin{lstlisting}\r\nglobal_step = tf.Variable(0, name='global_step', trainable=False)\r\ntrain_op = optimizer.minimize(loss, global_step=global_step)\r\n\\end{lstlisting}\r\n\r\n最后，程序返回包含了训练操作（training op）输出结果的Tensor。\r\n\r\n\\subsection{训练模型}\r\n\r\n一旦图表构建完毕，就通过\\lstinline{fully_connected_feed.py}文件中的用户代码进行循环地迭代式训练和评估。\r\n\r\n\\subsubsection{图表 (The Graph)}\r\n\r\n在\\lstinline{run_training()}这个函数的一开始，是一个Python语言中的\\lstinline{with}命令，这个命令表明所有已经构建的操作都要与默认的\\lstinline{[`tf.Graph`]}\r\n%(../api_docs/python/framework.md#Graph)\r\n全局实例关联起来。\r\n\r\n\\begin{lstlisting}\r\nwith tf.Graph().as_default():\r\n\\end{lstlisting}\r\n\r\n\\lstinline{tf.Graph}实例是一系列可以作为整体执行的操作。TensorFlow的大部分场景只需要依赖默认图表一个实例即可。\r\n\r\n利用多个图表的更加复杂的使用场景也是可能的，但是超出了本教程的范围。\r\n\r\n\\subsubsection{会话 (The Session)}\r\n\r\n完成全部的构建准备、生成全部所需的操作之后，我们就可以创建一个\\lstinline{tf.Session}\r\n%(../api_docs/python/client.md#Session)\r\n，用于运行图表。\r\n\r\n\\begin{lstlisting}\r\nsess = tf.Session()\r\n\\end{lstlisting}\r\n\r\n另外，也可以利用\\lstinline{with}代码块生成\\lstinline{Session}，限制作用域：\r\n\r\n\\begin{lstlisting}\r\nwith tf.Session() as sess:\r\n\\end{lstlisting}\r\n\r\n\\lstinline{Session}函数中没有传入参数，表明该代码将会依附于（如果还没有创建会话，则会创建新的会话）默认的本地会话。\r\n\r\n生成会话之后，所有\\lstinline{tf.Variable}实例都会立即通过调用各自初始化操作中的\\lstinline{sess.run()}\r\n%(../api_docs/python/client.md#Session.run)\r\n函数进行初始化。\r\n\r\n\\begin{lstlisting}\r\ninit = tf.initialize_all_variables()\r\nsess.run(init)\r\n\\end{lstlisting}\r\n\r\n\\lstinline{sess.run()}\r\n%(../api_docs/python/client.md#Session.run)\r\n方法将会运行图表中与作为参数传入的操作相对应的完整子集。在初次调用时，\\lstinline{init}操作只包含了变量初始化程序\\lstinline{tf.group}\r\n%(../api_docs/python/control_flow_ops.md#group)\r\n。图表的其他部分不会在这里，而是在下面的训练循环运行。\r\n\r\n\\subsubsection{训练循环}\r\n\r\n完成会话中变量的初始化之后，就可以开始训练了。\r\n\r\n训练的每一步都是通过用户代码控制，而能实现有效训练的最简单循环就是：\r\n\r\n\\begin{lstlisting}\r\nfor step in xrange(max_steps):\r\n    sess.run(train_op)\r\n\\end{lstlisting}\r\n\r\n但是，本教程中的例子要更为复杂一点，原因是我们必须把输入的数据根据每一步的情况进行切分，以匹配之前生成的占位符。\r\n\r\n\\paragragh{向图表提}\r\n\r\n执行每一步时，我们的代码会生成一个反馈字典（feed dictionary），其中包含对应步骤中训练所要使用的例子，这些例子的哈希键就是其所代表的占位符操作。\r\n\r\n\\lstinline{fill_feed_dict}函数会查询给定的\\lstinline{DataSet}，索要下一批次\\lstinline{batch_size}的图像和标签，与占位符相匹配的Tensor则会包含下一批次的图像和标签。\r\n\r\n\\begin{lstlisting}\r\nimages_feed, labels_feed = data_set.next_batch(FLAGS.batch_size)\r\n\\end{lstlisting}\r\n\r\n然后，以占位符为哈希键，创建一个Python字典对象，键值则是其代表的反馈Tensor。\r\n\r\n\\begin{lstlisting}\r\nfeed_dict = {\r\n    images_placeholder: images_feed,\r\n    labels_placeholder: labels_feed,\r\n}\r\n\\end{lstlisting}\r\n\r\n这个字典随后作为\\lstinline{feed_dict}参数，传入\\lstinline{sess.run()}函数中，为这一步的训练提供输入样例。\r\n\r\n\\paragragh{检查状态}\r\n\r\n在运行\\lstinline{sess.run()}函数时，要在代码中明确其需要获取的两个值：\\lstinline{[train_op, loss]}。\r\n\r\n\\begin{lstlisting}\r\nfor step in xrange(FLAGS.max_steps):\r\n    feed_dict = fill_feed_dict(data_sets.train, images_placeholder, labels_placeholder)\r\n    _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)\r\n\\end{lstlisting}\r\n\r\n因为要获取这两个值，\\lstinline{sess.run()}会返回一个有两个元素的元组。其中每一个\\lstinline{Tensor}对象，对应了返回的元组中的numpy数组，而这些数组中包含了当前这步训练中对应Tensor的值。由于\\lstinline{train_op}并不会产生输出，其在返回的元祖中的对应元素就是\\lstinline{None}，所以会被抛弃。但是，如果模型在训练中出现偏差，\\lstinline{loss} Tensor的值可能会变成\\lstinline{NaN}，所以我们要获取它的值，并记录下来。\r\n\r\n假设训练一切正常，没有出现\\lstinline{NaN}，训练循环会每隔100个训练步骤，就打印一行简单的状态文本，告知用户当前的训练状态。\r\n\r\n\\begin{lstlisting}\r\nif step % 100 == 0:\r\n    print 'Step %d: loss = %.2f (%.3f sec)' % (step, loss_value, duration)\r\n\\end{lstlisting}\r\n\r\n\\paragraph{状态可视化}\r\n为了释放\\lstinline{[TensorBoard]}\r\n%(../how_tos/summaries_and_tensorboard.md)\r\n所使用的事件文件（events file），所有的即时数据（在这里只有一个）都要在图表构建阶段合并至一个操作（op）中。\r\n\r\n\\begin{lstlisting}\r\nsummary_op = tf.merge_all_summaries()\r\n\\end{lstlisting}\r\n\r\n在创建好会话（session）之后，可以实例化一个\\lstinline{tf.train.SummaryWriter}\r\n% (../api_docs/python/train.md#SummaryWriter)\r\n，用于写入包含了图表本身和即时数据具体值的事件文件。\r\n\r\n\\begin{lstlisting}\r\nsummary_writer = tf.train.SummaryWriter(FLAGS.train_dir, graph_def=sess.graph_def)\r\n\\end{lstlisting}\r\n\r\n最后，每次运行\\lstinline{summary_op}时，都会往事件文件中写入最新的即时数据，函数的输出会传入事件文件读写器（writer）的\\lstinline{add_summary()}函数。。\r\n\r\n\\begin{lstlisting}\r\nsummary_str = sess.run(summary_op, feed_dict=feed_dict)\r\nsummary_writer.add_summary(summary_str, step)\r\n\\end{lstlisting}\r\n\r\n事件文件写入完毕之后，可以就训练文件夹打开一个TensorBoard，查看即时数据的情况。\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics[width=.85\\textwidth]{../SOURCE/images/mnist_tensorboard.png}\r\n\\centering\r\n\\end{figure}\r\n\r\n% ![MNIST TensorBoard](../images/mnist_tensorboard.png \"MNIST TensorBoard\")\r\n\r\n**注意**：了解更多如何构建并运行TensorBoard的信息，请查看相关教程\\hyperref[vis_learning]{Tensorboard：训练过程可视化}。\r\n\r\n\\paragraph{保存检查点(checkpoint)}\r\n\r\n为了得到可以用来后续恢复模型以进一步训练或评估的检查点文件（checkpoint file），我们实例化一个\\lstinline{tf.train.Saver}\r\n% (../api_docs/python/state_ops.md#Saver)。\r\n\r\n\\begin{lstlisting}\r\nsaver = tf.train.Saver()\r\n\\end{lstlisting}\r\n\r\n在训练循环中，将定期调用\\lstinline{saver.save()}\r\n%(../api_docs/python/state_ops.md#Saver.save)\r\n方法，向训练文件夹中写入包含了当前所有可训练变量值得检查点文件。\r\n\r\n\\begin{lstlisting}\r\nsaver.save(sess, FLAGS.train_dir, global_step=step)\r\n\\end{lstlisting}\r\n\r\n这样，我们以后就可以使用\\lstinline{saver.restore()}\r\n%(../api_docs/python/state_ops.md#Saver.restore)\r\n方法，重载模型的参数，继续训练。\r\n\r\n\\begin{lstlisting}\r\nsaver.restore(sess, FLAGS.train_dir)\r\n\\end{lstlisting}\r\n\r\n\\subsection{评估模型}\r\n\r\n每隔一千个训练步骤，我们的代码会尝试使用训练数据集与测试数据集，对模型进行评估。\\lstinline{do_eval}函数会被调用三次，分别使用训练数据集、验证数据集合测试数据集。\r\n\r\n\\begin{lstlisting}\r\nprint 'Training Data Eval:'\r\ndo_eval(sess, eval_correct, images_placeholder, labels_placeholder, data_sets.train)\r\nprint 'Validation Data Eval:'\r\ndo_eval(sess, eval_correct, images_placeholder, labels_placeholder, data_sets.validation)\r\nprint 'Test Data Eval:'\r\ndo_eval(sess, eval_correct, images_placeholder, labels_placeholder, data_sets.test)\r\n\\end{lstlisting}\r\n\r\n>注意，更复杂的使用场景通常是，先隔绝\\lstinline{data_sets.test}测试数据集，只有在大量的超参数优化调整（hyperparameter tuning）之后才进行检查。但是，由于MNIST问题比较简单，我们在这里一次性评估所有的数据。\r\n\r\n\\subsubsection {构建评估图表（Eval Graph）}\r\n\r\n在打开默认图表（Graph）之前，我们应该先调用\\lstinline{get_data(train=False)}函数，抓取测试数据集。\r\n\r\n\\begin{lstlisting}\r\ntest_all_images, test_all_labels = get_data(train=False)\r\n\\end{lstlisting}\r\n\r\n在进入训练循环之前，我们应该先调用\\lstinline{mnist.py}文件中的\\lstinline{evaluation}函数，传入的logits和标签参数要与\\lstinline{loss}函数的一致。这样做事为了先构建Eval操作。\r\n\r\n\\begin{lstlisting}\r\neval_correct = mnist.evaluation(logits, labels_placeholder)\r\n\\end{lstlisting}\r\n\r\n\\lstinline{evaluation}函数会生成\\hyperref[(../api_docs/python/nn.md#in_top_k)]{\\lstinline{tf.nn.in_top_k}}\r\n操作，如果在$k$个最有可能的预测中可以发现真的标签，那么这个操作就会将模型输出标记为正确。在本文中，我们把$k$的值设置为1，也就是只有在预测是真的标签时，才判定它是正确的。\r\n\r\n\\begin{lstlisting}\r\neval_correct = tf.nn.in_top_k(logits, labels, 1)\r\n\\end{lstlisting}\r\n\r\n\\subsubsection {评估图表的输出（Eval Output）}\r\n\r\n之后，我们可以创建一个循环，往其中添加\\lstinline{feed_dict}，并在调用\\lstinline{sess.run()}函数时传入\\lstinline{eval_correct}操作，目的就是用给定的数据集评估模型。\r\n\r\n\\begin{lstlisting}\r\nfor step in xrange(steps_per_epoch):\r\n    feed_dict = fill_feed_dict(data_set,\r\n                               images_placeholder,\r\n                               labels_placeholder)\r\n    true_count += sess.run(eval_correct, feed_dict=feed_dict)\r\n\\end{lstlisting}\r\n\r\n\\lstinline{true_count}变量会累加所有\\lstinline{in_top_k}操作判定为正确的预测之和。接下来，只需要将正确测试的总数，除以例子总数，就可以得出准确率了。\r\n\r\n\\begin{lstlisting}\r\nprecision = float(true_count) / float(num_examples)\r\nprint '  Num examples: %d  Num correct: %d  Precision @ 1: %0.02f' % (\r\n    num_examples, true_count, precision)\r\n\\end{lstlisting}\r\n\r\n原文：\\href{http://www.tensorflow.org/tutorials/mnist/tf/index.md}{TensorFlow Mechanics 101}\r\n翻译：\\href{https://github.com/bingjin}{bingjin}\r\n校对：\\href{https://github.com/LichAmnesia}{LichAmnesia}"
  },
  {
    "path": "tex_pdf/tutorials/c2s04_.tex",
    "content": "%!TEX program = xelatex\r\n% Encoding: UTF8\r\n% SEIKA 2015\r\n\r\n\r\n% Chapter 2 Tutorials\r\n% Section 2.5\r\n\r\n\r\n\\newpage\r\n\\section {卷积神经网络} \\label{cnn}\r\n\r\n\\subsection {Overview}\r\n\r\n%Ⓔ CIFAR-10 classification is a common benchmark problem in machine learning. The problem is to classify RGB 32x32 pixel images across 10 categories: airplane, automobile, bird, cat, deer, dog, frog, horse, ship, and truck.\r\n\r\n对CIFAR-10 数据集的分类是机器学习中一个公开的基准测试问题，其任务是对一组32x32RGB的图像进行分类，这些图像涵盖了10个类别：\\lstinline{airplane}, \\lstinline{automobile}, \\lstinline{bird}, \\lstinline{cat}, \\lstinline{deer}, \\lstinline{dog}, \\lstinline{frog}, \\lstinline{horse}, \\lstinline{ship}, 和 \\lstinline{truck}.\\footnote{This tutorial is intended for advanced users of TensorFlow and assumes expertise and experience in machine learning}\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics[width=.65\\textwidth]{../SOURCE/images/cifar_samples.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\n想了解更多信息请参考\\href{http://www.cs.toronto.edu/~kriz/cifar.html}{CIFAR-10 page}，以及Alex Krizhevsky写的\\href{http://www.cs.toronto.edu/~kriz/learning-features-2009-TR.pdf}{技术报告}。\r\n\r\n\\subsubsection {目标}\r\n本教程的目标是建立一个用于识别图像的相对较小的卷积神经网络，在这一过程中，本教程会：\r\n\r\n\\begin{itemize}\r\n\\item 着重于建立一个规范的网络组织结构，训练并进行评估；\r\n\\item 为建立更大规模更加复杂的模型提供一个范例\r\n\\end{itemize}\r\n\r\n选择CIFAR-10是因为它的复杂程度足以用来检验TensorFlow中的大部分功能，并可将其扩展为更大的模型。与此同时由于模型较小所以训练速度很快，比较适合用来测试新的想法，检验新的技术。\r\n\r\n\\subsubsection {本教程的重点}\r\nCIFAR-10 教程演示了在TensorFlow上构建更大更复杂模型的几个种重要内容：\r\n\r\n\\begin{itemize}\r\n\\item 相关核心数学对象，如卷积、修正线性激活、最大池化以及局部响应归一化；\r\n\\item 训练过程中一些网络行为的可视化，这些行为包括输入图像、损失情况、网络行为的分布情况以及梯度；\r\n\\item 算法学习参数的移动平均值的计算函数，以及在评估阶段使用这些平均值提高预测性能；\r\n\\item 实现了一种机制，使得学习率随着时间的推移而递减；\r\n\\item 为输入数据设计预存取队列，将磁盘延迟和高开销的图像预处理操作与模型分离开来处理；\r\n\\end{itemize}\r\n\r\n我们也提供了模型的多GPU版本，用以表明：\r\n\r\n\\begin{itemize}\r\n\\item 可以配置模型后使其在多个GPU上并行的训练\r\n\\item 可以在多个GPU之间共享和更新变量值\r\n\\end{itemize}\r\n\r\n我们希望本教程给大家开了个头，使得在Tensorflow上可以为视觉相关工作建立更大型的Cnns模型\r\n\r\n\\subsubsection {模型架构}\r\n\r\n本教程中的模型是一个多层架构，由卷积层和非线性层(nonlinearities)交替多次排列后构成。这些层最终通过全连通层对接到softmax分类器上。这一模型除了最顶部的几层外，基本跟Alex Krizhevsky提出的模型一致。\r\n\r\n在一个GPU上经过几个小时的训练后，该模型达到了最高86\\%的精度。细节请查看下面的描述以及代码。模型中包含了1,068,298个学习参数，分类一副图像需要大概19.5M个乘加操作。\r\n\r\n\\subsection {Code Organization}\r\n\r\n本教程的代码位于\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/}{tensorflow/models/image/cifar10/}.\r\n\r\n% insert table here\r\n\r\n\r\n\\begin{longtable}[c]{@{}ll@{}}\r\n\\toprule\r\n\\begin{minipage}[b]{0.05\\columnwidth}\\raggedright\\strut\r\n文件\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[b]{0.05\\columnwidth}\\raggedright\\strut\r\n作用\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\midrule\r\n\\endhead\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_input.py}{\\lstinline{cifar10\\_input.py}}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n读取本地CIFAR-10的二进制文件格式的内容。\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10.py}{\\lstinline{cifar10.py}}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n建立CIFAR-10的模型。\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_train.py}{\\lstinline{cifar10\\_train.py}}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n在CPU或GPU上训练CIFAR-10的模型。\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_multi_gpu_train.py}{\\lstinline{cifar10\\_multi\\_gpu\\_train.py}}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n在多GPU上训练CIFAR-10的模型。\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10_eval.py}{\\lstinline{cifar10\\_eval.py}}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n评估CIFAR-10模型的预测性能。\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\bottomrule\r\n\\end{longtable}\r\n\r\n\\subsection{CIFAR-10 模型 }\\label{cifar-10-ux6a21ux578b}\r\n\r\nCIFAR-10 网络模型部分的代码位于\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/image/cifar10/cifar10.py}{\\lstinline{cifar10.py}}。完整的训练图中包含约765个操作，但是通过下面的模块来构造训练图可以最大限度的提高代码复用率:\r\n\r\n\\begin{enumerate}\r\n\\def\\labelenumi{\\arabic{enumi}.}\r\n\\tightlist\r\n\\item\r\n  \\protect\\hyperlink{model-inputs}{\\textbf{模型输入:}}\r\n  包括\\lstinline{inputs()} 、\r\n  \\lstinline{distorted\\_inputs()}等一些操作，分别用于读取CIFAR的图像并进行预处理，做为后续评估和训练的输入；\r\n\\item\r\n  \\protect\\hyperlink{model-prediction}{\\textbf{模型预测:}}\r\n  包括\\lstinline{inference()}等一些操作，用于进行统计计算，比如在提供的图像进行分类；\r\n  adds operations that perform inference, i.e.~classification, on\r\n  supplied images.\r\n\\item\r\n  \\protect\\hyperlink{model-training}{\\textbf{模型训练:}}\r\n  包括\\lstinline{loss()} 和\r\n  \\lstinline{train()}等一些操作，用于计算损失、计算梯度、进行变量更新以及呈现最终结果。\r\n\\end{enumerate}\r\n\r\n\\subsubsection{模型输入 }\\label{ux6a21ux578bux8f93ux5165}\r\n\r\n输入模型是通过 \\lstinline{inputs()}\r\n和\\lstinline{distorted\\_inputs()}函数建立起来的，这2个函数会从CIFAR-10二进制文件中读取图片文件，由于每个图片的存储字节数是固定的，因此可以使用\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/io_ops.md\\#FixedLengthRecordReader}{\\lstinline{tf.FixedLengthRecordReader}}函数。更多的关于\\lstinline{Reader}类的功能可以查看\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/how_tos/reading_data/index.md\\#reading-from-files}{Reading\r\nData}。\r\n\r\n图片文件的处理流程如下：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  图片会被统一裁剪到24x24像素大小，裁剪中央区域用于评估或\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/image.md\\#random_crop}{随机}裁剪用于训练；\r\n\\item\r\n  图片会进行\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/image.md\\#per_image_whitening}{近似的白化处理}，使得模型对图片的动态范围变化不敏感。\r\n\\end{itemize}\r\n\r\n对于训练，我们另外采取了一系列随机变换的方法来人为的增加数据集的大小：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  对图像进行\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/image.md\\#random_flip_left_right}{随机的左右翻转}；\r\n\\item\r\n  随机变换\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/image.md\\#random_brightness}{图像的亮度}；\r\n\\item\r\n  随机变换\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/image.md\\#tf_image_random_contrast}{图像的对比度}；\r\n\\end{itemize}\r\n\r\n可以在\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/image.md}{Images}页的列表中查看所有可用的变换，对于每个原始图我们还附带了一个\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md\\#image_summary}{\\lstinline{image\\_summary}}，以便于在TensorBoard中查看。这对于检查输入图像是否正确十分有用。\r\n\r\n从磁盘上加载图像并进行变换需要花费不少的处理时间。为了避免这些操作减慢训练过程，我们在16个独立的线程中并行进行这些操作，这16个线程被连续的安排在一个TensorFlow\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/io_ops.md\\#shuffle_batch}{队列}中。\r\n\r\n\\subsubsection{模型预测 }\\label{ux6a21ux578bux9884ux6d4b}\r\n\r\n模型的预测流程由\\lstinline{inference()}构造，该函数会添加必要的操作步骤用于计算预测值的\r\n\\emph{logits}，其对应的模型组织方式如下所示：\r\n\r\n\\begin{longtable}[c]{@{}ll@{}}\r\n\\toprule\r\n\\begin{minipage}[b]{0.05\\columnwidth}\\raggedright\\strut\r\nLayer 名称\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[b]{0.05\\columnwidth}\\raggedright\\strut\r\n描述\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\midrule\r\n\\endhead\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\lstinline{conv1}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n实现\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md\\#conv2d}{卷积}\r\n以及\r\n\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md\\#relu}{rectified\r\nlinear} activation.\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\lstinline{pool1}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md\\#max_pool}{max\r\npooling}.\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\lstinline{norm1}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md\\#local_response_normalization}{局部响应归一化}.\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\lstinline{conv2}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md\\#conv2d}{卷积}\r\nand\r\n\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md\\#relu}{rectified\r\nlinear} activation.\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\lstinline{norm2}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md\\#local_response_normalization}{局部响应归一化}.\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\lstinline{pool2}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md\\#max_pool}{max\r\npooling}.\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\lstinline{local3}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md}{基于修正线性激活的全连接层}.\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\lstinline{local4}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md}{基于修正线性激活的全连接层}.\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\lstinline{softmax\\_linear}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n进行线性变换以输出 logits.\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\bottomrule\r\n\\end{longtable}\r\n\r\n这里有一个由TensorBoard绘制的图形，用于描述模型建立过程中经过的步骤：\r\n\r\n\\begin{quote}\r\n\\textbf{练习}:\r\n\\lstinline{inference}的输出是未归一化的logits，尝试使用\\href{tensorflow-zh/SOURCE/api_docs/python/nn.md\\#softmax}{\\lstinline{tf.softmax()}}修改网络架构后返回归一化的预测值。\r\n\\end{quote}\r\n\r\n\\lstinline{inputs()} 和 \\lstinline{inference()}\r\n函数提供了评估模型时所需的所有构件，现在我们把讲解的重点从构建一个模型转向训练一个模型。\r\n\r\n\\begin{quote}\r\n\\textbf{练习:} \\lstinline{inference()}\r\n中的模型跟\\href{https://code.google.com/p/cuda-convnet/}{cuda-convnet}中描述的CIFAR-10模型有些许不同，其差异主要在于其顶层不是全连接层而是局部连接层，可以尝试修改网络架构来准确的复制全连接模型。\r\n\\end{quote}\r\n\r\n\\subsubsection{模型训练}\\label{ux6a21ux578bux8badux7ec3}\r\n\r\n训练一个可进行N维分类的网络的常用方法是使用\\href{https://en.wikipedia.org/wiki/Multinomial_logistic_regression}{多项式逻辑回归},又被叫做\\emph{softmax\r\n回归}。Softmax\r\n回归在网络的输出层上附加了一个\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md\\#softmax}{softmax}\r\nnonlinearity，并且计算归一化的预测值和label的\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/sparse_ops.md\\#sparse_to_dense}{1-hot\r\nencoding}的\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md\\#softmax_cross_entropy_with_logits}{交叉熵}。在正则化过程中，我们会对所有学习变量应用\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md\\#l2_loss}{权重衰减损失}。模型的目标函数是求交叉熵损失和所有权重衰减项的和，\\lstinline{loss()}函数的返回值就是这个值。\r\n\r\n在TensorBoard中使用\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md\\#scalar_summary}{scalar\\_summary}来查看该值的变化情况：\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics{../SOURCE/images/cifar_loss.png}\r\n\\caption{CIFAR-10 Loss}\r\n\\end{figure}\r\n\r\n我们使用标准的梯度下降算法来训练模型（也可以在\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md}{Training}中看看其他方法），其学习率随时间以指数形式衰减。\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics{../SOURCE/images/cifar_lr_decay.png}\r\n\\caption{CIFAR-10 Learning Rate Decay}\r\n\\end{figure}\r\n\r\n\\lstinline{train()}\r\n函数会添加一些操作使得目标函数最小化，这些操作包括计算梯度、更新学习变量（详细信息请查看\\href{tensorflow-zh/SOURCE/api_docs/python/train.md\\#GradientDescentOptimizer}{\\lstinline{GradientDescentOptimizer}}）。\\lstinline{train()}\r\n函数最终会返回一个用以对一批图像执行所有计算的操作步骤，以便训练并更新模型。\r\n\r\n\\subsection{开始执行并训练模型\r\n}\\label{ux5f00ux59cbux6267ux884cux5e76ux8badux7ec3ux6a21ux578b}\r\n\r\n我们已经把模型建立好了，现在通过执行脚本\\lstinline{cifar10\\_train.py}来启动训练过程。\r\n\r\n\\begin{lstlisting}\r\npython cifar10_train.py\r\n\\end{lstlisting}\r\n\r\n\\textbf{注意:}\r\n当第一次在CIFAR-10教程上启动任何任务时，会自动下载CIFAR-10数据集，该数据集大约有160M大小，因此第一次运行时泡杯咖啡小栖一会吧。\r\n\r\n你应该可以看到如下类似的输出:\r\n\r\n\\begin{lstlisting}\r\nFilling queue with 20000 CIFAR images before starting to train. This will take a few minutes.\r\n2015-11-04 11:45:45.927302: step 0, loss = 4.68 (2.0 examples/sec; 64.221 sec/batch)\r\n2015-11-04 11:45:49.133065: step 10, loss = 4.66 (533.8 examples/sec; 0.240 sec/batch)\r\n2015-11-04 11:45:51.397710: step 20, loss = 4.64 (597.4 examples/sec; 0.214 sec/batch)\r\n2015-11-04 11:45:54.446850: step 30, loss = 4.62 (391.0 examples/sec; 0.327 sec/batch)\r\n2015-11-04 11:45:57.152676: step 40, loss = 4.61 (430.2 examples/sec; 0.298 sec/batch)\r\n2015-11-04 11:46:00.437717: step 50, loss = 4.59 (406.4 examples/sec; 0.315 sec/batch)\r\n...\r\n\\end{lstlisting}\r\n\r\n脚本会在每10步训练过程后打印出总损失值，以及最后一批数据的处理速度。下面是几点注释：\r\n\r\n\\begin{itemize}\r\n\\item\r\n  第一批数据会非常的慢（大概要几分钟时间），因为预处理线程要把20,000个待处理的CIFAR图像填充到重排队列中；\r\n\\item\r\n  打印出来的损失值是最近一批数据的损失值的均值。请记住损失值是交叉熵和权重衰减项的和；\r\n\\item\r\n  上面打印结果中关于一批数据的处理速度是在Tesla\r\n  K40C上统计出来的，如果你运行在CPU上，性能会比此要低；\r\n\\end{itemize}\r\n\r\n\\begin{quote}\r\n\\textbf{练习:}\r\n当实验时，第一阶段的训练时间有时会非常的长，长到足以让人生厌。可以尝试减少初始化时初始填充到队列中图片数量来改变这种情况。在\\lstinline{cifar10.py}中搜索\\lstinline{NUM\\_EXAMPLES\\_PER\\_EPOCH\\_FOR\\_TRAIN}并修改之。\r\n\\end{quote}\r\n\r\n\\lstinline{cifar10\\_train.py}\r\n会周期性的在\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/tree/master/SOURCE/how_tos/variables/index.md\\#saving-and-restoring}{检查点文件}中\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/state_ops.md\\#Saver}{保存}模型中的所有参数，但是\\emph{不会}对模型进行评估。\\lstinline{cifar10\\_eval.py}会使用该检查点文件来测试预测性能（详见下面的描述：\\protect\\hyperlink{ux8bc4ux4f30ux6a21ux578b}{评估模型}）。\r\n\r\n如果按照上面的步骤做下来，你应该已经开始训练一个CIFAR-10模型了。\\href{https://www.youtube.com/watch?v=9bZkp7q19f0}{恭喜你!}\r\n\r\n\\lstinline{cifar10\\_train.py}输出的终端信息中提供了关于模型如何训练的一些信息，但是我们可能希望了解更多关于模型训练时的信息，比如：\\\\\r\n* 损失是\\emph{真的}在减小还是看到的只是噪声数据？\\\\\r\n* 为模型提供的图片是否合适？\\\\\r\n* 梯度、激活、权重的值是否合理？\\\\\r\n* 当前的学习率是多少？\r\n\r\n\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/tree/master/SOURCE/how_tos/summaries_and_tensorboard/index.md}{TensorBoard}提供了该功能，可以通过\\lstinline{cifar10\\_train.py}中的\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md\\#SummaryWriter}{\\lstinline{SummaryWriter}}周期性的获取并显示这些数据。\r\n\r\n比如我们可以在训练过程中查看\\lstinline{local3}的激活情况，以及其特征维度的稀疏情况：\r\n\r\n相比于总损失，在训练过程中的单项损失尤其值得人们的注意。但是由于训练中使用的数据批量比较小，损失值中夹杂了相当多的噪声。在实践过程中，我们也发现相比于原始值，损失值的移动平均值显得更为有意义。请参阅脚本\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md\\#ExponentialMovingAverage}{\\lstinline{ExponentialMovingAverage}}了解如何实现。\r\n\r\n\\hypertarget{ux8bc4ux4f30ux6a21ux578b}{\\subsection{评估模型\r\n}\\label{ux8bc4ux4f30ux6a21ux578b}}\r\n\r\n现在可以在另一部分数据集上来评估训练模型的性能。脚本文件\\lstinline{cifar10\\_eval.py}对模型进行了评估，利用\r\n\\lstinline{inference()}函数重构模型，并使用了在评估数据集所有10,000张CIFAR-10图片进行测试。最终计算出的精度为\\emph{1:N}，N=预测值中置信度最高的一项与图片真实label匹配的频次。(It\r\ncalculates the \\emph{precision at 1:} how often the top prediction\r\nmatches the true label of the image)。\r\n\r\n为了监控模型在训练过程中的改进情况，评估用的脚本文件会周期性的在最新的检查点文件上运行，这些检查点文件是由\\lstinline{cifar10\\_train.py}产生。\r\n\r\n\\begin{lstlisting}\r\npython cifar10_eval.py\r\n\\end{lstlisting}\r\n\r\n\\begin{quote}\r\n注意：不要在同一块GPU上同时运行训练程序和评估程序，因为可能会导致内存耗尽。尽可能的在其它单独的GPU上运行评估程序，或者在同一块GPU上运行评估程序时先挂起训练程序。\r\n\\end{quote}\r\n\r\n你可能会看到如下所示输出:\r\n\r\n\\begin{lstlisting}\r\n2015-11-06 08:30:44.391206: precision @ 1 = 0.860\r\n...\r\n\\end{lstlisting}\r\n\r\n评估脚本只是周期性的返回precision@1 (The script merely returns the\r\nprecision @ 1\r\nperiodically)--在该例中返回的准确率是86\\%。\\lstinline{cifar10\\_eval.py}\r\n同时也返回其它一些可以在TensorBoard中进行可视化的简要信息。可以通过这些简要信息在评估过程中进一步的了解模型。\r\n\r\n训练脚本会为所有学习变量计算其\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md\\#ExponentialMovingAverage}{移动均值},评估脚本则直接将所有学习到的模型参数替换成对应的移动均值。这一替代方式可以在评估过程中提升模型的性能。\r\n\r\n\\begin{quote}\r\n\\textbf{练习:} 通过precision @\r\n1测试发现，使用均值参数可以将预测性能提高约3\\%，在\\lstinline{cifar10\\_eval.py}中尝试修改为不采用均值参数的方式，并确认由此带来的预测性能下降。\r\n\\end{quote}\r\n\r\n\\subsection{在多个GPU板卡上训练模型\r\n}\\label{ux5728ux591aux4e2agpuux677fux5361ux4e0aux8badux7ec3ux6a21ux578b}\r\n\r\n现代的工作站可能包含多个GPU进行科学计算。TensorFlow可以利用这一环境在多个GPU卡上运行训练程序。\r\n\r\n在并行、分布式的环境中进行训练，需要对训练程序进行协调。对于接下来的描述，术语\\emph{模型拷贝}（\\emph{model\r\nreplica}）特指在一个数据子集中训练出来的模型的一份拷贝。\r\n\r\n如果天真的对模型参数的采用异步方式更新将会导致次优的训练性能，这是因为我们可能会基于一个旧的模型参数的拷贝去训练一个模型。但与此相反采用完全同步更新的方式，其速度将会变得和最慢的模型一样慢(Conversely,\r\nemploying fully synchronous updates will be as slow as the slowest model\r\nreplica.)。\r\n\r\n在具有多个GPU的工作站中，每个GPU的速度基本接近，并且都含有足够的内存来运行整个CIFAR-10模型。因此我们选择以下方式来设计我们的训练系统：\r\n\r\n\\begin{itemize}\r\n\\item\r\n  在每个GPU上放置单独的模型副本；\r\n\\item\r\n  等所有GPU处理完一批数据后再同步更新模型的参数；\r\n\\end{itemize}\r\n\r\n下图示意了该模型的结构：:\r\n\r\n可以看到，每一个GPU会用一批独立的数据计算梯度和估计值。这种设置可以非常有效的将一大批数据分割到各个GPU上。\r\n\r\n这一机制要求所有GPU能够共享模型参数。但是众所周知在GPU之间传输数据非常的慢，因此我们决定在CPU上存储和更新所有模型的参数(对应图中绿色矩形的位置)。这样一来，GPU在处理一批新的数据之前会更新一遍的参数。\r\n\r\n图中所有的GPU是同步运行的。所有GPU中的梯度会累积并求平均值(绿色方框部分)。模型参数会利用所有模型副本梯度的均值来更新。\r\n\r\n\\subsubsection{在多个设备中设置变量和操作}\\label{ux5728ux591aux4e2aux8bbeux5907ux4e2dux8bbeux7f6eux53d8ux91cfux548cux64cdux4f5c}\r\n\r\n在多个设备中设置变量和操作时需要做一些特殊的抽象。\r\n\r\n我们首先需要把在单个模型拷贝中计算估计值和梯度的行为抽象到一个函数中。在代码中，我们称这个抽象对象为``tower''。对于每一个``tower''我们都需要设置它的两个属性：\\\\\r\n*\r\n在一个tower中为所有操作设定一个唯一的名称。\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/framework.md\\#name_scope}{\\lstinline{tf.name\\_scope()}}通过添加一个范围前缀来提供该唯一名称。比如，第一个tower中的所有操作都会附带一个前缀\\lstinline{tower\\_0}，示例：\\lstinline{tower\\_0/conv1/Conv2D}；\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  在一个tower中运行操作的优先硬件设备。\r\n  \\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/framework.md\\#device}{\\lstinline{tf.device()}}\r\n  提供该信息。比如，在第一个tower中的所有操作都位于\r\n  \\lstinline{device(\\textquotesingle{}/gpu:0\\textquotesingle{})}范围中，暗含的意思是这些操作应该运行在第一块GPU上；\r\n\\end{itemize}\r\n\r\n为了在多个GPU上共享变量，所有的变量都绑定在CPU上，并通过\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/state_ops.md\\#get_variable}{\\lstinline{tf.get\\_variable()}}访问。可以查看\\href{https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/how_tos/variables/index.md}{Sharing\r\nVariables}以了解如何共享变量。\r\n\r\n\\subsubsection{启动并在多个GPU上训练模型\r\n}\\label{ux542fux52a8ux5e76ux5728ux591aux4e2agpuux4e0aux8badux7ec3ux6a21ux578b}\r\n\r\n如果你的机器上安装有多块GPU，你可以通过使用\\lstinline{cifar10\\_multi\\_gpu\\_train.py}脚本来加速模型训练。该脚本是训练脚本的一个变种，使用多个GPU实现模型并行训练。\r\n\r\n\\begin{lstlisting}\r\npython cifar10_multi_gpu_train.py --num_gpus=2\r\n\\end{lstlisting}\r\n\r\n训练脚本的输出如下所示：\r\n\r\n\\begin{lstlisting}\r\nFilling queue with 20000 CIFAR images before starting to train. This will take a few minutes.\r\n2015-11-04 11:45:45.927302: step 0, loss = 4.68 (2.0 examples/sec; 64.221 sec/batch)\r\n2015-11-04 11:45:49.133065: step 10, loss = 4.66 (533.8 examples/sec; 0.240 sec/batch)\r\n2015-11-04 11:45:51.397710: step 20, loss = 4.64 (597.4 examples/sec; 0.214 sec/batch)\r\n2015-11-04 11:45:54.446850: step 30, loss = 4.62 (391.0 examples/sec; 0.327 sec/batch)\r\n2015-11-04 11:45:57.152676: step 40, loss = 4.61 (430.2 examples/sec; 0.298 sec/batch)\r\n2015-11-04 11:46:00.437717: step 50, loss = 4.59 (406.4 examples/sec; 0.315 sec/batch)\r\n...\r\n\\end{lstlisting}\r\n\r\n需要注意的是默认的GPU使用数是1，此外，如果你的机器上只有一个GPU，那么所有的计算都只会在一个GPU上运行，即便你可能设置的是N个。\r\n\r\n\\begin{quote}\r\n\\textbf{练习:}\r\n\\lstinline{cifar10\\_train.py}中的批处理大小默认配置是128。尝试在2个GPU上运行\\lstinline{cifar10\\_multi\\_gpu\\_train.py}脚本，并且设定批处理大小为64，然后比较2种方式的训练速度。\r\n\\end{quote}\r\n\r\n\\subsection{下一步 }\\label{ux4e0bux4e00ux6b65}\r\n\r\n恭喜你! 你已经完成了CIFAR-10教程。 如果你对开发和训练自己的图像分类系统感兴趣，我们推荐你新建一个基于该教程的分支，并修改其中的内容以建立解决您问题的图像分类系统。\r\n\r\n\\begin{quote}\r\n\\textbf{练习:} 下载\\href{http://ufldl.stanford.edu/housenumbers/}{Street\r\nView House Numbers (SVHN)}\r\n数据集。新建一个CIFAR-10教程的分支，并将输入数据替换成SVHN。尝试改变网络结构以提高预测性能。\r\n\\end{quote}\r\n\r\n\\begin{quote}\r\n原文：\\href{http://tensorflow.org/tutorials/deep_cnn/index.md}{Convolutional\r\nNeural Networks} 翻译：\\href{https://github.com/oskycar}{oskycar}\r\n校对：\\href{https://github.com/zhyhooo}{KK4SBB}\r\n\\end{quote}\r\n"
  },
  {
    "path": "tex_pdf/tutorials/c2s05_word2vec.tex",
    "content": "%!TEX program = xelatex\r\n% Encoding: UTF8\r\n% SEIKA 2015\r\n\r\n\\section{Vector Representations of Words}\\label{word2vec}\r\n\r\nIn this tutorial we look at the word2vec model by \\href{http://papers.nips.cc/paper/5021-distributed-representations-of-words-and-phrases-and-their-compositionality.pdf}{Mikolov et al}. This model is used for learning vector representations of words, called \"word embeddings\".\r\n\r\n在本教程我们来看一下\\href{http://papers.nips.cc/paper/5021-distributed-representations-of-words-and-phrases-and-their-compositionality.pdf}{Mikolovet al}中提到的word2vec模型。该模型是用于学习文字的向量表示，称之为 \"wordembedding\"。\r\n\r\n\\subsection{亮点 }\\label{word2vec_hl}\r\n\r\n本教程旨在意在展现出在TensorfLow中构建word2vec模型有趣、本质的部分。\r\n\r\n\\begin{itemize}\r\n\\item 我们从我们为何需要使用向量表示文字开始。\r\n\\item 我们通过直观地例子观察模型背后的本质，以及它是如何训练的（通过一些数学方法评估）。\r\n\\item 同时我们也展示了TensorFlow对该模型的简单实现。\r\n\\item 最后，我们着眼于让给这个简单版本的模型表现更好。\r\n\\end{itemize}\r\n\r\n我们会在教程的推进中循序渐进地解释代码，但是如果你更希望直入主题，可以在\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py}{tensorflow/g3doc/tutorials/word2vec/word2vec\\_basic.py}查看到一个最简单的实现。这个基本的例子提供的代码可以完成下载一些数据，简单训练后展示结果。一旦你觉得已经完全掌握了这个简单版本，你可以查看\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/embedding/word2vec.py}{tensorflow/models/embedding/word2vec.py}，这里提供了一些更复杂的实现，同时也展示了TensorFlow的一些更进阶的特性，比如如何更高效地使用线程将数据送入文本模型，再比如如何在训练中设置检查点等等。\r\n\r\n但是首先，让我们来看一下为何需要学习word embeddings。如果你对word\r\nembeddings相关内容已经是个专家了，那么请安心跳过本节内容，直接深入细节干一些脏活吧。\r\n\r\n\\subsection{动机: 为什么需要学习 Word Embeddings?}\\label{ux52a8}\r\n\r\n通常图像或音频系统处理的是由图片中所有单个原始像素点强度值或者音频中功率谱密度的强度值，把它们编码成丰富、高纬度的向量数据集。对于物体或语音识别这一类的任务，我们所需的全部信息已经都存储在原始数据中（显然人类本身就是依赖原始数据进行日常的物体或语音识别的）。然后，自然语言处理系统通常将词汇作为离散的单一符号，例如\"cat\" 一词或可表示为\\lstinline{Id537} ，而\"dog\"一词或可表示为\\lstinline{Id143}。这些符号编码毫无规律，无法提供不同词汇之间可能存在的关联信息。换句话说，在处理关于\"dogs\" 一词的信息时，模型将无法利用已知的关于\"cats\"的信息（例如，它们都是动物，有四条腿，可作为宠物等等）。可见，将词汇表达为上述的独立离散符号将进一步导致数据稀疏，使我们在训练统计模型时不得不寻求更多的数据。而词汇的向量表示将克服上述的难题。\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics[width=.9\\textwidth]{../SOURCE/images/audio-image-text.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\n\\href{https://en.wikipedia.org/wiki/Vector_space_model}{向量空间模型(VSMs)}\r\n将词汇表达（嵌套）于一个连续的向量空间中，语义近似的词汇被映射为相邻的数据点。向量空间模型在自然语言处理领域中有着漫长且丰富的历史，不过几乎所有利用这一模型的方法都依赖于\\href{https://en.wikipedia.org/wiki/Distributional_semantics\\#Distributional_Hypothesis}{分布式假设}，其核心思想为出现于上下文情景中的词汇都有相类似的语义。采用这一假设的研究方法大致分为以下两类：\\emph{基于技术的方法}(如，\\href{https://en.wikipedia.org/wiki/Latent_semantic_analysis}{潜在语义分析})，和 \\emph{预测方法} (如，\\href{http://www.scholarpedia.org/article/Neural_net_language_models}{神经概率化语言模型}).\r\n\r\n其中它们的区别在如下论文中又详细阐述\\href{http://clic.cimec.unitn.it/marco/publications/acl2014/baroni-etal-countpredict-acl2014.pdf}{Baroni\r\netal.}，不过简而言之：基于计数的方法计算某词汇与其邻近词汇在一个大型语料库中共同出现的频率及其他统计量，然后将这些统计量映射到一个小型且稠密的向量中。预测方法则试图直接从某词汇的邻近词汇对其进行预测，在此过程中利用已经学习到的小型且稠密的\\emph{嵌套向量}。\r\n\r\nWord2vec是一种可以进行高效率词嵌套学习的预测模型。其两种变体分别为：连续词袋模型（CBOW）及Skip-Gram模型。从算法角度看，这两种方法非常相似，其区别为CBOW根据源词上下文词汇（\"the cat sits on the\"）来预测目标词汇（例如，\"mat\"），而Skip-Gram模型做法相反，它通过目标词汇来预测源词汇。Skip-Gram模型采取CBOW的逆过程的动机在于：CBOW算法对于很多分布式信息进行了平滑处理（例如将一整段上下文信息视为一个单一观察量）。很多情况下，对于小型的数据集，这一处理是有帮助的。相形之下，Skip-Gram模型将每个“上下文---目标词汇”的组合视为一个新观察量，这种做法在大型数据集中会更为有效。本教程余下部分将着重讲解Skip-Gram模型。\r\n\r\n\\subsection{处理噪声对比训练}\\label{ux5904ux7406ux566aux58f0ux5bf9ux6bd4ux8badux7ec3}\r\n\r\n神经概率化语言模型通常使用\\href{https://en.wikipedia.org/wiki/Maximum_likelihood}{极大似然法(ML)} 进行训练，其中通过\\href{https://en.wikipedia.org/wiki/Softmax_function}{\\emph{softmax}function} 来最大化当提供前一个单词$h$ (代表\"history\")，后一个单词的概率$w_t$ (代表\"target\")，\r\n\r\n\\begin{align}\r\nP(w_t|h) & = softmax(score(w_t,h)) \\\\\r\n         & = \\frac{exp\\{score(w_t,h)\\}}{\\sum_{Word w'in Vocab}{exp\\{score(w',h)\\}}}\r\n\\end{align}\r\n\r\n当$score(w_t,h)$ 计算了文字 $w_t$ 和 上下文 $h$的相容性（通常使用向量积）。我们使用对数似然函数来训练训练集的最大值，比如通过：\r\n\r\n\\begin{align}\r\nJ_{ML} & = \\log{P(w_t|h)} \\\\\r\n       & = score(w_t,h) - \\log{(\\sum_{Word w'in Vocab}{\\exp{\\{score(w',h)\\}}})}\r\n\\end{align}\r\n\r\n这里提出了一个解决语言概率模型的合适的通用方法。然而这个方法实际执行起来开销非常大，因为我们需要去计算并正则化当前上下文环境 $h$ 中所有其他 $V$ 单词 $w'$ 的概率得分，\\emph{在每一步训练迭代中}。\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics[width=.75\\textwidth]{../SOURCE/images/nce-nplm.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\n从另一个角度来说，当使用word2vec模型时，我们并不需要对概率模型中的所有特征进行学习。而CBOW模型和Skip-Gram模型为了避免这种情况发生，使用一个二分类器（逻辑回归）在同一个上下文环境里从\r\n\\textbf{k} 虚构的 (噪声) 单词区分出真正的目标单词\r\n\\includegraphics{../SOURCE/images/rw4.png}。我们下面详细阐述一下CBOW模型，对于Skip-Gram模型只要简单地做相反的操作即可。\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics[width=.75\\textwidth]{../SOURCE/images/softmax-nplm.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\n从数学角度来说，我们的目标是对每个样本最大化：\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics{../SOURCE/images/rw6.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\n其中 \\includegraphics{../SOURCE/images/rw7.png} 代表的是数据集在当前上下文\\textbf{h} ，根据所学习的嵌套向量 \\includegraphics{../SOURCE/images/theta.png}，目标单词 \\textbf{w}使用二分类逻辑回归计算得出的概率。在实践中，我们通过在噪声分布中绘制比对文字来获得近似的期望值（通过计算\\href{https://en.wikipedia.org/wiki/Monte_Carlo_integration}{蒙特卡洛平均值}）。\r\n\r\n当真实地目标单词被分配到较高的概率，同时噪声单词的概率很低时，目标函数也就达到最大值了。从技术层面来说，这种方法叫做\\href{http://papers.nips.cc/paper/5021-distributed-representations-of-words-and-phrases-and-their-compositionality.pdf}{负抽样}，而且使用这个损失函数在数学层面上也有很好的解释：这个更新过程也近似于softmax函数的更新。这在计算上将会有很大的优势，因为当计算这个损失函数时，只是有我们挑选出来的\\textbf{k} 个 \\emph{噪声单词}，而没有使用整个语料库\\textbf{V}。这使得训练变得非常快。我们实际上使用了与\\href{http://papers.nips.cc/paper/5165-learning-word-embeddings-efficiently-with-noise-contrastive-estimation.pdf}{noise-contrastiveestimation(NCE)}介绍的非常相似的方法，这在TensorFlow中已经封装了一个很便捷的函数\\lstinline{tf.nn.nce_loss()}。\r\n\r\n让我们在实践中来直观地体会它是如何运作的！\r\n\r\n\\subsection{Skip-gram 模型}\\label{skip-gram_model}\r\n\r\n下面来看一下这个数据集\r\n\r\n\\lstinline{\"the quick brown fox jumped over the lazy dog\"}\r\n\r\n我们首先对一些单词以及它们的上下文环境建立一个数据集。我们可以以任何合理的方式定义`上下文'，而通常上这个方式是根据文字的句法语境的（使用语法原理的方式处理当前目标单词可以看一下这篇文献\r\n\\href{https://levyomer.files.wordpress.com/2014/04/dependency-based-word-embeddings-acl-2014.pdf}{Levy et al.}，比如说把目标单词左边的内容当做一个“上下文”，或者以目标单词右边的内容，等等。现在我们把目标单词的左右单词视作一个上下文，\r\n使用大小为1的窗口，这样就得到这样一个由\\texttt{(上下文,\\ 目标单词)}\r\n组成的数据集：\r\n\r\n\\texttt{({[}the,\\ brown{]},\\ quick),\\ ({[}quick,\\ fox{]},\\ brown),\\ ({[}brown,\\ jumped{]},\\ fox),\\ ...}\r\n\r\n前文提到Skip-Gram模型是把目标单词和上下文颠倒过来，所以在这个问题中，举个例子，就是用'quick'来预测\r\n`the' 和 `brown' ，用 `brown' 预测 `quick' 和 `brown'\r\n。因此这个数据集就变成由\\texttt{(输入,\\ 输出)}组成的：\r\n\r\n\\texttt{(quick,\\ the),\\ (quick,\\ brown),\\ (brown,\\ quick),\\ (brown,\\ fox),\\ ...}\r\n\r\n目标函数通常是对整个数据集建立的，但是本问题中要对每一个样本（或者是一个\\texttt{batch\\_size}\r\n很小的样本集，通常设置为\\texttt{16\\ \\textless{}=\\ batch\\_size\\ \\textless{}=\\ 512}）在同一时间执行特别的操作，称之为\\href{https://en.wikipedia.org/wiki/Stochastic_gradient_descent}{随机梯度下降}\r\n(SGD)。我们来看一下训练过程中每一步的执行。\r\n\r\n假设用 \\textbf{t} 表示上面这个例子中\\texttt{quick} 来预测 \\texttt{the}\r\n的训练的单个循环。用 \\texttt{num\\_noise}\r\n定义从噪声分布中挑选出来的噪声（相反的）单词的个数，通常使用一元分布，\\textbf{P(w)}。为了简单起见，我们就定\\texttt{num\\_noise=1}，用\r\n\\texttt{sheep}\r\n选作噪声词。接下来就可以计算每一对观察值和噪声值的损失函数了，每一个执行步骤就可表示为：\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics{../SOURCE/images/vr4.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\n整个计算过程的目标是通过更新嵌套参数\r\n\\includegraphics{../SOURCE/images/theta.png}\r\n来逼近目标函数（这个这个例子中就是使目标函数最大化）。为此我们要计算损失函数中嵌套参数\r\n\\includegraphics{../SOURCE/images/theta.png} 的梯度，比如，\r\n\\includegraphics{../SOURCE/images/vr5.png}\r\n(幸好TensorFlow封装了工具函数可以简单调用!)。对于整个数据集，当梯度下降的过程中不断地更新参数，对应产生的效果就是不断地移动每个单词的嵌套向量，直到可以把真实单词和噪声单词很好得区分开。\r\n\r\n我们可以把学习向量映射到2维中以便我们观察，其中用到的技术可以参考\r\n\\href{http://lvdmaaten.github.io/tsne/}{t-SNE\r\n降维技术}。当我们用可视化的方式来观察这些向量，就可以很明显的获取单词之间语义信息的关系，这实际上是非常有用的。当我们第一次发现这样的诱导向量空间中，展示了一些特定的语义关系，这是非常有趣的，比如文字中\r\n\\emph{male-female}，\\emph{gender} 甚至还有 \\emph{country-capital}\r\n的关系, 如下方的图所示 (也可以参考\r\n\\href{http://www.aclweb.org/anthology/N13-1090}{Mikolov et al.,\r\n2013}论文中的例子)。\r\n\r\n这也解释了为什么这些向量在传统的NLP问题中可作为特性使用，比如用在对一个演讲章节打个标签，或者对一个专有名词的识别\r\n(看看如下这个例子 \\href{http://arxiv.org/pdf/1103.0398v1.pdf}{Collobert\r\net al.}或者 \\href{http://www.aclweb.org/anthology/P10-1040}{Turian et\r\nal.})。\r\n\r\n不过现在让我们用它们来画漂亮的图表吧！\r\n\r\n\\subsection{建立图形 }\\label{ux5efaux7acbux56feux5f62}\r\n\r\n这里谈得都是嵌套，那么先来定义一个嵌套参数矩阵。我们用唯一的随机值来初始化这个大矩阵。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{embeddings }\\OperatorTok{=} \\NormalTok{tf.Variable(}\r\n    \\NormalTok{tf.random_uniform([vocabulary_size, embedding_size], }\\OperatorTok{-}\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{1.0}\\NormalTok{))}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n对噪声-比对的损失计算就使用一个逻辑回归模型。对此，我们需要对语料库中的每个单词定义一个权重值和偏差值。(也可称之为\\texttt{输出权重}\r\n与之对应的 \\texttt{输入嵌套值})。定义如下。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{nce_weights }\\OperatorTok{=} \\NormalTok{tf.Variable(}\r\n  \\NormalTok{tf.truncated_normal([vocabulary_size, embedding_size],}\r\n                      \\NormalTok{stddev}\\OperatorTok{=}\\FloatTok{1.0} \\OperatorTok{/} \\NormalTok{math.sqrt(embedding_size)))}\r\n\\NormalTok{nce_biases }\\OperatorTok{=} \\NormalTok{tf.Variable(tf.zeros([vocabulary_size]))}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n我们有了这些参数之后，就可以定义Skip-Gram模型了。简单起见，假设我们已经把语料库中的文字整型化了，这样每个整型代表一个单词(细节请查看\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/word2vec/word2vec_basic.py}{tensorflow/g3doc/tutorials/word2vec/word2vec\\_basic.py})。Skip-Gram模型有两个输入。一个是一组用整型表示的上下文单词，另一个是目标单词。给这些输入建立占位符节点，之后就可以填入数据了。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 建立输入占位符}\r\n\\NormalTok{train_inputs }\\OperatorTok{=} \\NormalTok{tf.placeholder(tf.int32, shape}\\OperatorTok{=}\\NormalTok{[batch_size])}\r\n\\NormalTok{train_labels }\\OperatorTok{=} \\NormalTok{tf.placeholder(tf.int32, shape}\\OperatorTok{=}\\NormalTok{[batch_size, }\\DecValTok{1}\\NormalTok{])}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n然后我们需要对批数据中的单词建立嵌套向量，TensorFlow提供了方便的工具函数。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{embed }\\OperatorTok{=} \\NormalTok{tf.nn.embedding_lookup(embeddings, train_inputs)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n好了，现在我们有了每个单词的嵌套向量，接下来就是使用噪声-比对的训练方式来预测目标单词。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 计算 NCE 损失函数, 每次使用负标签的样本.}\r\n\\NormalTok{loss }\\OperatorTok{=} \\NormalTok{tf.reduce_mean(}\r\n  \\NormalTok{tf.nn.nce_loss(nce_weights, nce_biases, embed, train_labels,}\r\n                 \\NormalTok{num_sampled, vocabulary_size))}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n我们对损失函数建立了图形节点，然后我们需要计算相应梯度和更新参数的节点，比如说在这里我们会使用随机梯度下降法，TensorFlow也已经封装好了该过程。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 使用 SGD 控制器.}\r\n\\NormalTok{optimizer }\\OperatorTok{=} \\NormalTok{tf.train.GradientDescentOptimizer(learning_rate}\\OperatorTok{=}\\FloatTok{1.0}\\NormalTok{).minimize(loss)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsection{训练模型 }\\label{ux8badux7ec3ux6a21ux578b}\r\n\r\n训练的过程很简单，只要在循环中使用\\texttt{feed\\_dict}不断给占位符填充数据，同时调用\r\n\\href{tensorflow-zh/SOURCE/api_docs/python/client.md\\#Session.run}{\\texttt{session.run}}即可。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{for} \\NormalTok{inputs, labels }\\OperatorTok{in} \\NormalTok{generate_batch(...):}\r\n  \\NormalTok{feed_dict }\\OperatorTok{=} \\NormalTok{\\{training_inputs: inputs, training_labels: labels\\}}\r\n  \\NormalTok{_, cur_loss }\\OperatorTok{=} \\NormalTok{session.run([optimizer, loss], feed_dict}\\OperatorTok{=}\\NormalTok{feed_dict)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n完整地例子可参考\r\n\\href{./word2vec_basic.py}{tensorflow/g3doc/tutorials/word2vec/word2vec\\_basic.py}.\r\n\r\n\\subsection{嵌套学习结果可视化\r\n}\\label{ux5d4cux5957ux5b66ux4e60ux7ed3ux679cux53efux89c6ux5316}\r\n\r\n使用t-SNE来看一下嵌套学习完成的结果。\r\n\r\nEt voila!\r\n与预期的一样，相似的单词被聚类在一起。对word2vec模型更复杂的实现需要用到TensorFlow一些更高级的特性，具体是实现可以参考\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/embedding/word2vec.py}{tensorflow/models/embedding/word2vec.py}。\r\n\r\n\\subsection{嵌套学习的评估: 类比推理\r\n}\\label{ux5d4cux5957ux5b66ux4e60ux7684ux8bc4ux4f30-ux7c7bux6bd4ux63a8ux7406}\r\n\r\n词嵌套在NLP的预测问题中是非常有用且使用广泛地。如果要检测一个模型是否是可以成熟地区分词性或者区分专有名词的模型，最简单的办法就是直接检验它的预测词性、语义关系的能力，比如让它解决形如\\texttt{king\\ is\\ to\\ queen\\ as\\ father\\ is\\ to\\ ?}这样的问题。这种方法叫做\\emph{类比推理}\r\n，可参考\\href{http://msr-waypoint.com/en-us/um/people/gzweig/Pubs/NAACL2013Regularities.pdf}{Mikolov\r\nand colleagues}，数据集下载地址为:\r\nhttps://word2vec.googlecode.com/svn/trunk/questions-words.txt。\r\n\r\nTo see how we do this\r\nevaluation如何执行这样的评估，可以看\\texttt{build\\_eval\\_graph()}和\r\n\\texttt{eval()}这两个函数在下面源码中的使用\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/embedding/word2vec.py}{tensorflow/models/embedding/word2vec.py}.\r\n\r\n超参数的选择对该问题解决的准确性有巨大的影响。想要模型具有很好的表现，需要有一个巨大的训练数据集，同时仔细调整参数的选择并且使用例如二次抽样的一些技巧。不过这些问题已经超出了本教程的范围。\r\n\r\n\\subsection{优化实现 }\\label{ux4f18ux5316ux5b9eux73b0}\r\n\r\n以上简单的例子展示了TensorFlow的灵活性。比如说，我们可以很轻松得用现成的\\texttt{tf.nn.sampled\\_softmax\\_loss()}来代替\\texttt{tf.nn.nce\\_loss()}构成目标函数。如果你对损失函数想做新的尝试，你可以用TensorFlow手动编写新的目标函数的表达式，然后用控制器执行计算。这种灵活性的价值体现在，当我们探索一个机器学习模型时，我们可以很快地遍历这些尝试，从中选出最优。\r\n\r\n一旦你有了一个满意的模型结构，或许它就可以使实现运行地更高效（在短时间内覆盖更多的数据）。比如说，在本教程中使用的简单代码，实际运行速度都不错，因为我们使用Python来读取和填装数据，而这些在TensorFlow后台只需执行非常少的工作。如果你发现你的模型在输入数据时存在严重的瓶颈，你可以根据自己的实际问题自行实现一个数据阅读器，参考\r\n\\href{tensorflow-zh/SOURCE/how_tos/new_data_formats/index.md}{新的数据格式}。对于Skip-Gram\r\n模型，我们已经完成了如下这个例子\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/embedding/word2vec.py}{tensorflow/models/embedding/word2vec.py}。\r\n\r\n如果I/O问题对你的模型已经不再是个问题，并且想进一步地优化性能，或许你可以自行编写TensorFlow操作单元，详见\r\n\\href{tensorflow-zh/SOURCE/how_tos/adding_an_op/index.md}{添加一个新的操作}。相应的，我们也提供了Skip-Gram模型的例子\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/models/embedding/word2vec_optimized.py}{tensorflow/models/embedding/word2vec\\_optimized.py}。请自行调节以上几个过程的标准，使模型在每个运行阶段有更好地性能。\r\n\r\n\\subsection{总结 }\\label{ux603bux7ed3}\r\n\r\n在本教程中我们介绍了word2vec模型,它在解决词嵌套问题中具有良好的性能。我们解释了使用词嵌套模型的实用性，并且讨论了如何使用TensorFlow实现该模型的高效训练。总的来说，我们希望这个例子能够让向你展示TensorFlow可以提供实验初期的灵活性，以及在后期优化模型时对模型内部的可操控性。\r\n\r\n原文地址：\\href{https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/tutorials/word2vec/index.md}{Vector\r\nRepresentation of Words}\r\n翻译：\\href{https://github.com/btpeter}{btpeter} 校对：waiwaizheng\r\n\r\n\r\n"
  },
  {
    "path": "tex_pdf/tutorials/c2s06_recurrent.tex",
    "content": "\r\n\r\n\\section{循环神经网络 }\\label{ux5faaux73afux795eux7ecfux7f51ux7edc}\r\n\r\n\\subsection{介绍 }\\label{ux4ecbux7ecd}\r\n\r\n可以在\r\n\\href{http://colah.github.io/posts/2015-08-Understanding-LSTMs/}{this\r\ngreat article} 查看循环神经网络(RNN)以及 LSTM 的介绍。\r\n\r\n\\subsection{语言模型 }\\label{ux8bedux8a00ux6a21ux578b}\r\n\r\n此教程将展示如何在高难度的语言模型中训练循环神经网络。该问题的目标是获得一个能确定语句概率的概率模型。为了做到这一点，通过之前已经给出的词语来预测后面的词语。我们将使用\r\nPTB(Penn Tree Bank)\r\n数据集，这是一种常用来衡量模型的基准，同时它比较小而且训练起来相对快速。\r\n\r\n语言模型是很多有趣难题的关键所在，比如语音识别，机器翻译，图像字幕等。它很有意思---可以参看\r\n\\href{http://karpathy.github.io/2015/05/21/rnn-effectiveness/}{here}。\r\n\r\n本教程的目的是重现 \\href{http://arxiv.org/abs/1409.2329}{Zaremba et al.,\r\n2014} 的成果，他们在 PTB 数据集上得到了很棒的结果。\r\n\r\n\\subsection{教程文件 }\\label{ux6559ux7a0bux6587ux4ef6}\r\n\r\n本教程使用的下面文件的目录是 \\lstinline{models/rnn/ptb}:\r\n\r\n\\begin{longtable}[c]{@{}ll@{}}\r\n\\toprule\r\n文件 & 作用\\tabularnewline\r\n\\midrule\r\n\\endhead\r\n\\lstinline{ptb\\_word\\_lm.py} & 在 PTB\r\n数据集上训练一个语言模型.\\tabularnewline\r\n\\lstinline{reader.py} & 读取数据集.\\tabularnewline\r\n\\bottomrule\r\n\\end{longtable}\r\n\r\n\\subsection{下载及准备数据\r\n}\\label{ux4e0bux8f7dux53caux51c6ux5907ux6570ux636e}\r\n\r\n本教程需要的数据在 data/ 路径下，来源于 Tomas Mikolov 网站上的 PTB\r\n数据集\\lstinline{http://www.fit.vutbr.cz/\\textasciitilde{}imikolov/rnnlm/simple-examples.tgz}。\r\n\r\n该数据集已经预先处理过并且包含了全部的 10000\r\n个不同的词语，其中包括语句结束标记符，以及标记稀有词语的特殊符号\r\n\\lstinline{(\\textless{}unk\\textgreater{})} 。我们在 \\lstinline{reader.py}\r\n中转换所有的词语，让他们各自有唯一的整型标识符，便于神经网络处理。\r\n\r\n\\subsection{模型 }\\label{ux6a21ux578b}\r\n\r\n\\subsubsection{LSTM }\\label{lstm}\r\n\r\n模型的核心由一个 LSTM\r\n单元组成，其可以在某时刻处理一个词语，以及计算语句可能的延续性的概率。网络的存储状态由一个零矢量初始化并在读取每一个词语后更新。而且，由于计算上的原因，我们将以\r\n\\lstinline{batch\\_size} 为最小批量来处理数据。\r\n\r\n基础的伪代码就像下面这样：\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{lstm }\\OperatorTok{=} \\NormalTok{rnn_cell.BasicLSTMCell(lstm_size)}\r\n\\CommentTok{# 初始化 LSTM 存储状态.}\r\n\\NormalTok{state }\\OperatorTok{=} \\NormalTok{tf.zeros([batch_size, lstm.state_size])}\r\n\r\n\\NormalTok{loss }\\OperatorTok{=} \\FloatTok{0.0}\r\n\\ControlFlowTok{for} \\NormalTok{current_batch_of_words }\\OperatorTok{in} \\NormalTok{words_in_dataset:}\r\n    \\CommentTok{# 每次处理一批词语后更新状态值.}\r\n    \\NormalTok{output, state }\\OperatorTok{=} \\NormalTok{lstm(current_batch_of_words, state)}\r\n\r\n    \\CommentTok{# LSTM 输出可用于产生下一个词语的预测}\r\n    \\NormalTok{logits }\\OperatorTok{=} \\NormalTok{tf.matmul(output, softmax_w) }\\OperatorTok{+} \\NormalTok{softmax_b}\r\n    \\NormalTok{probabilities }\\OperatorTok{=} \\NormalTok{tf.nn.softmax(logits)}\r\n    \\NormalTok{loss }\\OperatorTok{+=} \\NormalTok{loss_function(probabilities, target_words)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsubsection{截断反向传播\r\n}\\label{ux622aux65adux53cdux5411ux4f20ux64ad}\r\n\r\n为使学习过程易于处理，通常的做法是将反向传播的梯度在（按时间）展开的步骤上照一个固定长度(\\lstinline{num\\_steps})截断。\r\n通过在一次迭代中的每个时刻上提供长度为 \\lstinline{num\\_steps}\r\n的输入和每次迭代完成之后反向传导，这会很容易实现。\r\n\r\n一个简化版的用于计算图创建的截断反向传播代码：\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 一次给定的迭代中的输入占位符.}\r\n\\NormalTok{words }\\OperatorTok{=} \\NormalTok{tf.placeholder(tf.int32, [batch_size, num_steps])}\r\n\r\n\\NormalTok{lstm }\\OperatorTok{=} \\NormalTok{rnn_cell.BasicLSTMCell(lstm_size)}\r\n\\CommentTok{# 初始化 LSTM 存储状态.}\r\n\\NormalTok{initial_state }\\OperatorTok{=} \\NormalTok{state }\\OperatorTok{=} \\NormalTok{tf.zeros([batch_size, lstm.state_size])}\r\n\r\n\\ControlFlowTok{for} \\NormalTok{i }\\OperatorTok{in} \\BuiltInTok{range}\\NormalTok{(}\\BuiltInTok{len}\\NormalTok{(num_steps)):}\r\n    \\CommentTok{# 每处理一批词语后更新状态值.}\r\n    \\NormalTok{output, state }\\OperatorTok{=} \\NormalTok{lstm(words[:, i], state)}\r\n\r\n    \\CommentTok{# 其余的代码.}\r\n    \\CommentTok{# ...}\r\n\r\n\\NormalTok{final_state }\\OperatorTok{=} \\NormalTok{state}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n下面展现如何实现迭代整个数据集：\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 一个 numpy 数组，保存每一批词语之后的 LSTM 状态.}\r\n\\NormalTok{numpy_state }\\OperatorTok{=} \\NormalTok{initial_state.}\\BuiltInTok{eval}\\NormalTok{()}\r\n\\NormalTok{total_loss }\\OperatorTok{=} \\FloatTok{0.0}\r\n\\ControlFlowTok{for} \\NormalTok{current_batch_of_words }\\OperatorTok{in} \\NormalTok{words_in_dataset:}\r\n    \\NormalTok{numpy_state, current_loss }\\OperatorTok{=} \\NormalTok{session.run([final_state, loss],}\r\n        \\CommentTok{# 通过上一次迭代结果初始化 LSTM 状态.}\r\n        \\NormalTok{feed_dict}\\OperatorTok{=}\\NormalTok{\\{initial_state: numpy_state, words: current_batch_of_words\\})}\r\n    \\NormalTok{total_loss }\\OperatorTok{+=} \\NormalTok{current_loss}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsubsection{输入 }\\label{ux8f93ux5165}\r\n\r\n在输入 LSTM 前，词语 ID 被嵌入到了一个密集的表示中(查看\r\n\\href{tensorflow-zh/SOURCE/tutorials/word2vec/index.md}{矢量表示教程})。这种方式允许模型高效地表示词语，也便于写代码：\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# embedding_matrix 张量的形状是： [vocabulary_size, embedding_size]}\r\n\\NormalTok{word_embeddings }\\OperatorTok{=} \\NormalTok{tf.nn.embedding_lookup(embedding_matrix, word_ids)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n嵌入的矩阵会被随机地初始化，模型会学会通过数据分辨不同词语的意思。\r\n\r\n\\subsubsection{损失函数 }\\label{ux635fux5931ux51fdux6570}\r\n\r\n我们想使目标词语的平均负对数概率最小\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics{../images/re.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\n实现起来并非很难，而且函数 \\lstinline{sequence\\_loss\\_by\\_example}\r\n已经有了，可以直接使用。\r\n\r\n论文中的典型衡量标准是每个词语的平均困惑度（perplexity），计算式为\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics{../images/re1.png}\r\n\\caption{}\r\n\\end{figure}\r\n\r\n同时我们会观察训练过程中的困惑度值（perplexity）。\r\n\r\n\\subsubsection{多个 LSTM 层堆叠\r\n}\\label{ux591aux4e2a-lstm-ux5c42ux5806ux53e0}\r\n\r\n要想给模型更强的表达能力，可以添加多层 LSTM\r\n来处理数据。第一层的输出作为第二层的输入，以此类推。\r\n\r\n类 \\lstinline{MultiRNNCell} 可以无缝的将其实现：\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{lstm }\\OperatorTok{=} \\NormalTok{rnn_cell.BasicLSTMCell(lstm_size)}\r\n\\NormalTok{stacked_lstm }\\OperatorTok{=} \\NormalTok{rnn_cell.MultiRNNCell([lstm] }\\OperatorTok{*} \\NormalTok{number_of_layers)}\r\n\r\n\\NormalTok{initial_state }\\OperatorTok{=} \\NormalTok{state }\\OperatorTok{=} \\NormalTok{stacked_lstm.zero_state(batch_size, tf.float32)}\r\n\\ControlFlowTok{for} \\NormalTok{i }\\OperatorTok{in} \\BuiltInTok{range}\\NormalTok{(}\\BuiltInTok{len}\\NormalTok{(num_steps)):}\r\n    \\CommentTok{# 每次处理一批词语后更新状态值.}\r\n    \\NormalTok{output, state }\\OperatorTok{=} \\NormalTok{stacked_lstm(words[:, i], state)}\r\n\r\n    \\CommentTok{# 其余的代码.}\r\n    \\CommentTok{# ...}\r\n\r\n\\NormalTok{final_state }\\OperatorTok{=} \\NormalTok{state}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsection{编译并运行代码\r\n}\\label{ux7f16ux8bd1ux5e76ux8fd0ux884cux4ee3ux7801}\r\n\r\n首先需要构建库，在 CPU 上编译：\r\n\r\n\\begin{verbatim}\r\nbazel build -c opt tensorflow/models/rnn/ptb:ptb_word_lm\r\n\\end{verbatim}\r\n\r\n如果你有一个强大的 GPU，可以运行：\r\n\r\n\\begin{verbatim}\r\nbazel build -c opt --config=cuda tensorflow/models/rnn/ptb:ptb_word_lm\r\n\\end{verbatim}\r\n\r\n运行模型：\r\n\r\n\\begin{verbatim}\r\nbazel-bin/tensorflow/models/rnn/ptb/ptb_word_lm \\\r\n  --data_path=/tmp/simple-examples/data/ --alsologtostderr --model small\r\n\\end{verbatim}\r\n\r\n教程代码中有 3 个支持的模型配置参数：``small''， ``medium'' 和\r\n``large''。它们指的是 LSTM 的大小，以及用于训练的超参数集。\r\n\r\n模型越大，得到的结果应该更好。在测试集中 \\lstinline{small}\r\n模型应该可以达到低于 120 的困惑度（perplexity），\\lstinline{large}\r\n模型则是低于 80，但它可能花费数小时来训练。\r\n\r\n\\subsection{除此之外？ }\\label{ux9664ux6b64ux4e4bux5916}\r\n\r\n还有几个优化模型的技巧没有提到，包括：\r\n\r\n\\begin{itemize}\r\n\\tightlist\r\n\\item\r\n  随时间降低学习率,\r\n\\item\r\n  LSTM 层间 dropout.\r\n\\end{itemize}\r\n\r\n继续学习和更改代码以进一步改善模型吧。\r\n\r\n原文：\\href{http://tensorflow.org/tutorials/recurrent/index.md}{Recurrent\r\nNeural Networks} 翻译：\\href{https://github.com/Warln}{Warln}\r\n校对：\\href{https://github.com/wanghong-yang}{HongyangWang}\r\n\r\n"
  },
  {
    "path": "tex_pdf/tutorials/c2s07_seq2seq.tex",
    "content": "\\section{Sequence-to-Sequence Models\r\n}\\label{sequence-to-sequence-models}\r\n\r\nRecurrent neural networks can learn to model language, as already\r\ndiscussed in the\r\n\\href{tensorflow-zh/SOURCE/tutorials/recurrent/index.md}{RNN Tutorial}\r\n(if you did not read it, please go through it before proceeding with\r\nthis one). This raises an interesting question: could we condition the\r\ngenerated words on some input and generate a meaningful response? For\r\nexample, could we train a neural network to translate from English to\r\nFrench? It turns out that the answer is \\emph{yes}.\r\n\r\nThis tutorial will show you how to build and train such a system\r\nend-to-end. You can start by running this binary.\r\n\r\n\\begin{verbatim}\r\nbazel run -c opt <...>/models/rnn/translate/translate.py\r\n  --data_dir [your_data_directory]\r\n\\end{verbatim}\r\n\r\nIt will download English-to-French translation data from the\r\n\\href{http://www.statmt.org/wmt15/translation-task.html}{WMT'15 Website}\r\nprepare it for training and train. It takes about 20GB of disk space,\r\nand a while to download and prepare (see\r\n\\protect\\hyperlink{runux5fit}{later} for details), so you can start and\r\nleave it running while reading this tutorial.\r\n\r\nThis tutorial references the following files from \\texttt{models/rnn}.\r\n\r\n\\begin{longtable}[c]{@{}ll@{}}\r\n\\toprule\r\n\\begin{minipage}[b]{0.05\\columnwidth}\\raggedright\\strut\r\nFile\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[b]{0.05\\columnwidth}\\raggedright\\strut\r\nWhat's in it?\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\midrule\r\n\\endhead\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\texttt{seq2seq.py}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\nLibrary for building sequence-to-sequence models.\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\texttt{translate/seq2seq\\_model.py}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\nNeural translation sequence-to-sequence model.\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\texttt{translate/data\\_utils.py}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\nHelper functions for preparing translation data.\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\texttt{translate/translate.py}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\nBinary that trains and runs the translation model.\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\bottomrule\r\n\\end{longtable}\r\n\r\n\\subsection{Sequence-to-Sequence Basics\r\n}\\label{sequence-to-sequence-basics}\r\n\r\nA basic sequence-to-sequence model, as introduced in\r\n\\href{http://arxiv.org/pdf/1406.1078v3.pdf}{Cho et al., 2014}, consists\r\nof two recurrent neural networks (RNNs): an \\emph{encoder} that\r\nprocesses the input and a \\emph{decoder} that generates the output. This\r\nbasic architecture is depicted below.\r\n\r\nEach box in the picture above represents a cell of the RNN, most\r\ncommonly a GRU cell or an LSTM cell (see the\r\n\\href{tensorflow-zh/SOURCE/tutorials/recurrent/index.md}{RNN Tutorial}\r\nfor an explanation of those). Encoder and decoder can share weights or,\r\nas is more common, use a different set of parameters. Mutli-layer cells\r\nhave been successfully used in sequence-to-sequence models too, e.g.~for\r\ntranslation \\href{http://arxiv.org/abs/1409.3215}{Sutskever et al.,\r\n2014}.\r\n\r\nIn the basic model depicted above, every input has to be encoded into a\r\nfixed-size state vector, as that is the only thing passed to the\r\ndecoder. To allow the decoder more direct access to the input, an\r\n\\emph{attention} mechanism was introduced in\r\n\\href{http://arxiv.org/abs/1409.0473}{Bahdanu et al., 2014}. We will not\r\ngo into the details of the attention mechanism (see the paper), suffice\r\nit to say that it allows the decoder to peek into the input at every\r\ndecoding step. A multi-layer sequence-to-sequence network with LSTM\r\ncells and attention mechanism in the decoder looks like this.\r\n\r\n\\subsection{TensorFlow seq2seq Library\r\n}\\label{tensorflow-seq2seq-library}\r\n\r\nAs you can see above, there are many different sequence-to-sequence\r\nmodels. Each of these models can use different RNN cells, but all of\r\nthem accept encoder inputs and decoder inputs. This motivates the\r\ninterfaces in the TensorFlow seq2seq library\r\n(\\texttt{models/rnn/seq2seq.py}). The basic RNN encoder-decoder\r\nsequence-to-sequence model works as follows.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{outputs, states }\\OperatorTok{=} \\NormalTok{basic_rnn_seq2seq(encoder_inputs, decoder_inputs, cell)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nIn the above call, \\texttt{encoder\\_inputs} are a list of tensors\r\nrepresenting inputs to the encoder, i.e., corresponding to the letters\r\n\\emph{A, B, C} in the first picture above. Similarly,\r\n\\texttt{decoder\\_inputs} are tensors representing inputs to the decoder,\r\n\\emph{GO, W, X, Y, Z} on the first picture.\r\n\r\nThe \\texttt{cell} argument is an instance of the\r\n\\texttt{models.rnn.rnn\\_cell.RNNCell} class that determines which cell\r\nwill be used inside the model. You can use an existing cell, such as\r\n\\texttt{GRUCell} or \\texttt{LSTMCell}, or you can write your own.\r\nMoreover, \\texttt{rnn\\_cell} provides wrappers to construct multi-layer\r\ncells, add dropout to cell inputs or outputs, or to do other\r\ntransformations, see the\r\n\\href{tensorflow-zh/SOURCE/tutorials/recurrent/index.md}{RNN Tutorial}\r\nfor examples.\r\n\r\nThe call to \\texttt{basic\\_rnn\\_seq2seq} returns two arguments:\r\n\\texttt{outputs} and \\texttt{states}. Both of them are lists of tensors\r\nof the same length as \\texttt{decoder\\_inputs}. Naturally,\r\n\\texttt{outputs} correspond to the outputs of the decoder in each\r\ntime-step, in the first picture above that would be \\emph{W, X, Y, Z,\r\nEOS}. The returned \\texttt{states} represent the internal state of the\r\ndecoder at every time-step.\r\n\r\nIn many applications of sequence-to-sequence models, the output of the\r\ndecoder at time t is fed back and becomes the input of the decoder at\r\ntime t+1. At test time, when decoding a sequence, this is how the\r\nsequence is constructed. During training, on the other hand, it is\r\ncommon to provide the correct input to the decoder at every time-step,\r\neven if the decoder made a mistake before. Functions in\r\n\\texttt{seq2seq.py} support both modes using the \\texttt{feed\\_previous}\r\nargument. For example, let's analyze the following use of an embedding\r\nRNN model.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{outputs, states }\\OperatorTok{=} \\NormalTok{embedding_rnn_seq2seq(}\r\n    \\NormalTok{encoder_inputs, decoder_inputs, cell,}\r\n    \\NormalTok{num_encoder_symbols, num_decoder_symbols,}\r\n    \\NormalTok{output_projection}\\OperatorTok{=}\\VariableTok{None}\\NormalTok{, feed_previous}\\OperatorTok{=}\\VariableTok{False}\\NormalTok{)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nIn the \\texttt{embedding\\_rnn\\_seq2seq} model, all inputs (both\r\n\\texttt{encoder\\_inputs} and \\texttt{decoder\\_inputs}) are\r\ninteger-tensors that represent discrete values. They will be embedded\r\ninto a dense representation (see the\r\n\\href{tensorflow-zh/SOURCE/tutorials/word2vec/index.md}{Vectors\r\nRepresentations Tutorial} for more details on embeddings), but to\r\nconstruct these embeddings we need to specify the maximum number of\r\ndiscrete symbols that will appear: \\texttt{num\\_encoder\\_symbols} on the\r\nencoder side, and \\texttt{num\\_decoder\\_symbols} on the decoder side.\r\n\r\nIn the above invocation, we set \\texttt{feed\\_previous} to False. This\r\nmeans that the decoder will use \\texttt{decoder\\_inputs} tensors as\r\nprovided. If we set \\texttt{feed\\_previous} to True, the decoder would\r\nonly use the first element of \\texttt{decoder\\_inputs}. All other\r\ntensors from this list would be ignored, and instead the previous output\r\nof the encoder would be used. This is used for decoding translations in\r\nour translation model, but it can also be used during training, to make\r\nthe model more robust to its own mistakes, similar to\r\n\\href{http://arxiv.org/pdf/1506.03099v2.pdf}{Bengio et al., 2015}.\r\n\r\nOne more important argument used above is \\texttt{output\\_projection}.\r\nIf not specified, the outputs of the embedding model will be tensors of\r\nshape batch-size by \\texttt{num\\_decoder\\_symbols} as they represent the\r\nlogits for each generated symbol. When training models with large output\r\nvocabularies, i.e., when \\texttt{num\\_decoder\\_symbols} is large, it is\r\nnot practical to store these large tensors. Instead, it is better to\r\nreturn smaller output tensors, which will later be projected onto a\r\nlarge output tensor using \\texttt{output\\_projection}. This allows to\r\nuse our seq2seq models with a sampled softmax loss, as described in\r\n\\href{http://arxiv.org/pdf/1412.2007v2.pdf}{Jean et. al., 2015}.\r\n\r\nIn addition to \\texttt{basic\\_rnn\\_seq2seq} and\r\n\\texttt{embedding\\_rnn\\_seq2seq} there are a few more\r\nsequence-to-sequence models in \\texttt{seq2seq.py}, take a look there.\r\nThey all have similar interfaces, so we will not describe them in\r\ndetail. We will use \\texttt{embedding\\_attention\\_seq2seq} for our\r\ntranslation model below.\r\n\r\n\\subsection{Neural Translation Model }\\label{neural-translation-model}\r\n\r\nWhile the core of the sequence-to-sequence model is constructed by the\r\nfunctions in \\texttt{models/rnn/seq2seq.py}, there are still a few\r\ntricks that are worth mentioning that are used in our translation model\r\nin \\texttt{models/rnn/translate/seq2seq\\_model.py}.\r\n\r\n\\subsubsection{Sampled softmax and output projection\r\n}\\label{sampled-softmax-and-output-projection}\r\n\r\nFor one, as already mentioned above, we want to use sampled softmax to\r\nhandle large output vocabulary. To decode from it, we need to keep track\r\nof the output projection. Both the sampled softmax loss and the output\r\nprojections are constructed by the following code in\r\n\\texttt{seq2seq\\_model.py}.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n  \\ControlFlowTok{if} \\NormalTok{num_samples }\\OperatorTok{>} \\DecValTok{0} \\OperatorTok{and} \\NormalTok{num_samples }\\OperatorTok{<} \\VariableTok{self}\\NormalTok{.target_vocab_size:}\r\n    \\NormalTok{w }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"proj_w\"}\\NormalTok{, [size, }\\VariableTok{self}\\NormalTok{.target_vocab_size])}\r\n    \\NormalTok{w_t }\\OperatorTok{=} \\NormalTok{tf.transpose(w)}\r\n    \\NormalTok{b }\\OperatorTok{=} \\NormalTok{tf.get_variable(}\\StringTok{\"proj_b\"}\\NormalTok{, [}\\VariableTok{self}\\NormalTok{.target_vocab_size])}\r\n    \\NormalTok{output_projection }\\OperatorTok{=} \\NormalTok{(w, b)}\r\n\r\n    \\KeywordTok{def} \\NormalTok{sampled_loss(inputs, labels):}\r\n      \\NormalTok{labels }\\OperatorTok{=} \\NormalTok{tf.reshape(labels, [}\\OperatorTok{-}\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{])}\r\n      \\ControlFlowTok{return} \\NormalTok{tf.nn.sampled_softmax_loss(w_t, b, inputs, labels, num_samples,}\r\n                                        \\VariableTok{self}\\NormalTok{.target_vocab_size)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nFirst, note that we only construct a sampled softmax if the number of\r\nsamples (512 by default) is smaller that the target vocabulary size. For\r\nvocabularies smaller than 512 it might be a better idea to just use a\r\nstandard softmax loss.\r\n\r\nThen, as you can see, we construct an output projection. It is a pair,\r\nconsisting of a weight matrix and a bias vector. If used, the rnn cell\r\nwill return vectors of shape batch-size by \\texttt{size}, rather than\r\nbatch-size by \\texttt{target\\_vocab\\_size}. To recover logits, we need\r\nto multiply by the weight matrix and add the biases, as is done in lines\r\n124-126 in \\texttt{seq2seq\\_model.py}.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{if} \\NormalTok{output_projection }\\OperatorTok{is} \\OperatorTok{not} \\VariableTok{None}\\NormalTok{:}\r\n  \\VariableTok{self}\\NormalTok{.outputs[b] }\\OperatorTok{=} \\NormalTok{[tf.matmul(output, output_projection[}\\DecValTok{0}\\NormalTok{]) }\\OperatorTok{+}\r\n                     \\NormalTok{output_projection[}\\DecValTok{1}\\NormalTok{] }\\ControlFlowTok{for} \\NormalTok{...]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsubsection{Bucketing and padding }\\label{bucketing-and-padding}\r\n\r\nIn addition to sampled softmax, our translation model also makes use of\r\n\\emph{bucketing}, which is a method to efficiently handle sentences of\r\ndifferent lengths. Let us first clarify the problem. When translating\r\nEnglish to French, we will have English sentences of different lengths\r\nL1 on input, and French sentences of different lengths L2 on output.\r\nSince the English sentence is passed as \\texttt{encoder\\_inputs}, and\r\nthe French sentence comes as \\texttt{decoder\\_inputs} (prefixed by a GO\r\nsymbol), we should in principle create a seq2seq model for every pair\r\n(L1, L2+1) of lengths of an English and French sentence. This would\r\nresult in an enormous graph consisting of many very similar subgraphs.\r\nOn the other hand, we could just pad every sentence with a special PAD\r\nsymbol. Then we'd need only one seq2seq model, for the padded lengths.\r\nBut on shorter sentence our model would be inefficient, encoding and\r\ndecoding many PAD symbols that are useless.\r\n\r\nAs a compromise between contructing a graph for every pair of lengths\r\nand padding to a single length, we use a number of \\emph{buckets} and\r\npad each sentence to the length of the bucket above it. In\r\n\\texttt{translate.py} we use the following default buckets.\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{buckets }\\OperatorTok{=} \\NormalTok{[(}\\DecValTok{5}\\NormalTok{, }\\DecValTok{10}\\NormalTok{), (}\\DecValTok{10}\\NormalTok{, }\\DecValTok{15}\\NormalTok{), (}\\DecValTok{20}\\NormalTok{, }\\DecValTok{25}\\NormalTok{), (}\\DecValTok{40}\\NormalTok{, }\\DecValTok{50}\\NormalTok{)]}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nThis means that if the input is an English sentence with 3 tokens, and\r\nthe corresponding output is a French sentence with 6 tokens, then they\r\nwill be put in the first bucket and padded to length 5 for encoder\r\ninputs, and length 10 for decoder inputs. If we have an English sentence\r\nwith 8 tokens and the corresponding French sentence has 18 tokens, then\r\nthey will not fit into the (10, 15) bucket, and so the (20, 25) bucket\r\nwill be used, i.e.~the English sentence will be padded to 20, and the\r\nFrench one to 25.\r\n\r\nRemember that when constructing decoder inputs we prepend the special\r\n\\texttt{GO} symbol to the input data. This is done in the\r\n\\texttt{get\\_batch()} function in \\texttt{seq2seq\\_model.py}, which also\r\nreverses the input English sentence. Reversing the inputs was shown to\r\nimprove results for the neural translation model in\r\n\\href{http://arxiv.org/abs/1409.3215}{Sutskever et al., 2014}. To put it\r\nall together, imagine we have the sentence ``I go.'', tokenized as\r\n\\texttt{{[}\"I\",\\ \"go\",\\ \".\"{]}} as input and the sentence ``Je vais.''\r\nas output, tokenized \\texttt{{[}\"Je\",\\ \"vais\",\\ \".\"{]}}. It will be put\r\nin the (5, 10) bucket, with encoder inputs representing\r\n\\texttt{{[}PAD\\ PAD\\ \".\"\\ \"go\"\\ \"I\"{]}} and decoder inputs\r\n\\texttt{{[}GO\\ \"Je\"\\ \"vais\"\\ \".\"\\ EOS\\ PAD\\ PAD\\ PAD\\ PAD\\ PAD{]}}.\r\n\r\n\\subsection{Let's Run It }\\label{lets-run-it}\r\n\r\nTo train the model described above, we need to a large English-French\r\ncorpus. We will use the \\emph{10\\^{}9-French-English corpus} from the\r\n\\href{http://www.statmt.org/wmt15/translation-task.html}{WMT'15 Website}\r\nfor training, and the 2013 news test from the same site as development\r\nset. Both data-sets will be downloaded to \\texttt{data\\_dir} and\r\ntraining will start, saving checkpoints in \\texttt{train\\_dir}, when\r\nthis command is run.\r\n\r\n\\begin{verbatim}\r\nbazel run -c opt <...>/models/rnn/translate:translate\r\n  --data_dir [your_data_directory] --train_dir [checkpoints_directory]\r\n  --en_vocab_size=40000 --fr_vocab_size=40000\r\n\\end{verbatim}\r\n\r\nIt takes about 18GB of disk space and several hours to prepare the\r\ntraining corpus. It is unpacked, vocabulary files are created in\r\n\\texttt{data\\_dir}, and then the corpus is tokenized and converted to\r\ninteger ids. Note the parameters that determine vocabulary sizes. In the\r\nexample above, all words outside the 40K most common ones will be\r\nconverted to an \\texttt{UNK} token representing unknown words. So if you\r\nchange vocabulary size, the binary will re-map the corpus to token-ids\r\nagain.\r\n\r\nAfter the data is prepared, training starts. Default parameters in\r\n\\texttt{translate} are set to quite large values. Large models trained\r\nover a long time give good results, but it might take too long or use\r\ntoo much memory for your GPU. You can request to train a smaller model\r\nas in the following example.\r\n\r\n\\begin{verbatim}\r\nbazel run -c opt <...>/models/rnn/translate:translate\r\n  --data_dir [your_data_directory] --train_dir [checkpoints_directory]\r\n  --size=256 --num_layers=2 --steps_per_checkpoint=50\r\n\\end{verbatim}\r\n\r\nThe above command will train a model with 2 layers (the default is 3),\r\neach layer with 256 units (default is 1024), and will save a checkpoint\r\nevery 50 steps (the default is 200). You can play with these parameters\r\nto find out how large a model can be to fit into the memory of your GPU.\r\n\r\nDuring training, every \\texttt{steps\\_per\\_checkpoint} steps the binary\r\nwill print out statistics from recent steps. With the default parameters\r\n(3 layers of size 1024), first messages look like this.\r\n\r\n\\begin{verbatim}\r\nglobal step 200 learning rate 0.5000 step-time 1.39 perplexity 1720.62\r\n  eval: bucket 0 perplexity 184.97\r\n  eval: bucket 1 perplexity 248.81\r\n  eval: bucket 2 perplexity 341.64\r\n  eval: bucket 3 perplexity 469.04\r\nglobal step 400 learning rate 0.5000 step-time 1.38 perplexity 379.89\r\n  eval: bucket 0 perplexity 151.32\r\n  eval: bucket 1 perplexity 190.36\r\n  eval: bucket 2 perplexity 227.46\r\n  eval: bucket 3 perplexity 238.66\r\n\\end{verbatim}\r\n\r\nYou can see that each step takes just under 1.4 seconds, the perplexity\r\non the training set and the perplexities on the development set for each\r\nbucket. After about 30K steps, we see perplexities on short sentences\r\n(bucket 0 and 1) going into single digits. Since the training corpus\r\ncontains \\textasciitilde{}22M sentences, one epoch (going through the\r\ntraining data once) takes about 340K steps with batch-size of 64. At\r\nthis point the model can be used for translating English sentences to\r\nFrench using the \\texttt{-\\/-decode} option.\r\n\r\n\\begin{verbatim}\r\nbazel run -c opt <...>/models/rnn/translate:translate --decode\r\n  --data_dir [your_data_directory] --train_dir [checkpoints_directory]\r\n\r\nReading model parameters from /tmp/translate.ckpt-340000\r\n>  Who is the president of the United States?\r\n Qui est le président des États-Unis ?\r\n\\end{verbatim}\r\n\r\n\\subsection{What Next? }\\label{what-next}\r\n\r\nThe example above shows how you can build your own English-to-French\r\ntranslator, end-to-end. Run it and see how the model performs for\r\nyourself. While it has reasonable quality, the default parameters will\r\nnot give you the best translation model. Here are a few things you can\r\nimprove.\r\n\r\nFirst of all, we use a very promitive tokenizer, the\r\n\\texttt{basic\\_tokenizer} function in \\texttt{data\\_utils}. A better\r\ntokenizer can be found on the\r\n\\href{http://www.statmt.org/wmt15/translation-task.html}{WMT'15\r\nWebsite}. Using that tokenizer, and a larger vocabulary, should improve\r\nyour translations.\r\n\r\nAlso, the default parameters of the translation model are not tuned. You\r\ncan try changing the learning rate, decay, or initializing the weights\r\nof your model in a different way. You can also change the default\r\n\\texttt{GradientDescentOptimizer} in \\texttt{seq2seq\\_model.py} to a\r\nmore advanced one, such as \\texttt{AdagradOptimizer}. Try these things\r\nand see how they improve your results!\r\n\r\nFinally, the model presented above can be used for any\r\nsequence-to-sequence task, not only for translation. Even if you want to\r\ntransform a sequence to a tree, for example to generate a parsing tree,\r\nthe same model as above can give state-of-the-art results, as\r\ndemonstrated in \\href{http://arxiv.org/abs/1412.7449}{Vinyals \\& Kaiser\r\net al., 2015}. So you can not only build your own translator, you can\r\nalso build a parser, a chat-bot, or any program that comes to your mind.\r\nExperiment!"
  },
  {
    "path": "tex_pdf/tutorials/c2s08_2_9_mandelbrot.tex",
    "content": "\r\n\r\n\\section{曼德布洛特(Mandelbrot)集合\r\n}\\label{ux66fcux5fb7ux5e03ux6d1bux7279mandelbrotux96c6ux5408}\r\n\r\n虽然可视化曼德布洛特(Mandelbrot)集合与机器学习没有任何关系，但这对于将TensorFlow应用在数学更广泛的领域是一个有趣的例子。实际上，这是tensorflow一个非常直截了当的可视化运用。（我们最终也许会提供一种更加精心设计的运用方式来生成真正更加美丽的图像。）\r\n\r\n说明：本教程使用了IPython的notebook。\r\n\r\n\\subsection{基本步骤 }\\label{ux57faux672cux6b65ux9aa4}\r\n\r\n首先，我们需要导入一些库。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 导入仿真库}\r\n\\ImportTok{import} \\NormalTok{tensorflow }\\ImportTok{as} \\NormalTok{tf}\r\n\\ImportTok{import} \\NormalTok{numpy }\\ImportTok{as} \\NormalTok{np}\r\n\r\n\\CommentTok{# 导入可视化库}\r\n\\ImportTok{import} \\NormalTok{PIL.Image}\r\n\\ImportTok{from} \\NormalTok{cStringIO }\\ImportTok{import} \\NormalTok{StringIO}\r\n\\ImportTok{from} \\NormalTok{IPython.display }\\ImportTok{import} \\NormalTok{clear_output, Image, display}\r\n\\ImportTok{import} \\NormalTok{scipy.ndimage }\\ImportTok{as} \\NormalTok{nd}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n现在我们将定义一个函数来显示迭代计算出的图像。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\KeywordTok{def} \\NormalTok{DisplayFractal(a, fmt}\\OperatorTok{=}\\StringTok{'jpeg'}\\NormalTok{):}\r\n  \\CommentTok{\"\"\"显示迭代计算出的彩色分形图像。\"\"\"}\r\n  \\NormalTok{a_cyclic }\\OperatorTok{=} \\NormalTok{(}\\FloatTok{6.28}\\OperatorTok{*}\\NormalTok{a}\\OperatorTok{/}\\FloatTok{20.0}\\NormalTok{).reshape(}\\BuiltInTok{list}\\NormalTok{(a.shape)}\\OperatorTok{+}\\NormalTok{[}\\DecValTok{1}\\NormalTok{])}\r\n  \\NormalTok{img }\\OperatorTok{=} \\NormalTok{np.concatenate([}\\DecValTok{10+20}\\OperatorTok{*}\\NormalTok{np.cos(a_cyclic),}\r\n                        \\DecValTok{30+50}\\OperatorTok{*}\\NormalTok{np.sin(a_cyclic),}\r\n                        \\DecValTok{155-80}\\OperatorTok{*}\\NormalTok{np.cos(a_cyclic)], }\\DecValTok{2}\\NormalTok{)}\r\n  \\NormalTok{img[a}\\OperatorTok{==}\\NormalTok{a.}\\BuiltInTok{max}\\NormalTok{()] }\\OperatorTok{=} \\DecValTok{0}\r\n  \\NormalTok{a }\\OperatorTok{=} \\NormalTok{img}\r\n  \\NormalTok{a }\\OperatorTok{=} \\NormalTok{np.uint8(np.clip(a, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{255}\\NormalTok{))}\r\n  \\NormalTok{f }\\OperatorTok{=} \\NormalTok{StringIO()}\r\n  \\NormalTok{PIL.Image.fromarray(a).save(f, fmt)}\r\n  \\NormalTok{display(Image(data}\\OperatorTok{=}\\NormalTok{f.getvalue()))}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsection{会话（session）和变量（variable）初始化\r\n}\\label{ux4f1aux8bddsessionux548cux53d8ux91cfvariableux521dux59cbux5316}\r\n\r\n为了操作的方便，我们常常使用交互式会话（interactive\r\nsession），但普通会话（regular session）也能正常使用。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n   \\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.InteractiveSession()}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n我们可以自由的混合使用NumPy和TensorFlow，这一点非常方便。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 使用NumPy创建一个在[-2,2]x[-2,2]范围内的2维复数数组}\r\n\r\n\\NormalTok{Y, X }\\OperatorTok{=} \\NormalTok{np.mgrid[}\\OperatorTok{-}\\FloatTok{1.3}\\NormalTok{:}\\FloatTok{1.3}\\NormalTok{:}\\FloatTok{0.005}\\NormalTok{, }\\OperatorTok{-}\\DecValTok{2}\\NormalTok{:}\\DecValTok{1}\\NormalTok{:}\\FloatTok{0.005}\\NormalTok{]}\r\n\\NormalTok{Z }\\OperatorTok{=} \\NormalTok{X}\\OperatorTok{+}\\NormalTok{1j}\\OperatorTok{*}\\NormalTok{Y}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n现在我们定义并初始化一组TensorFlow的张量 （tensors）。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{xs }\\OperatorTok{=} \\NormalTok{tf.constant(Z.astype(}\\StringTok{\"complex64\"}\\NormalTok{))}\r\n\\NormalTok{zs }\\OperatorTok{=} \\NormalTok{tf.Variable(xs)}\r\n\\NormalTok{ns }\\OperatorTok{=} \\NormalTok{tf.Variable(tf.zeros_like(xs, }\\StringTok{\"float32\"}\\NormalTok{))}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\nTensorFlow在使用之前需要你明确给定变量的初始值。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{tf.initialize_all_variables().run()}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsection{定义并运行计算\r\n}\\label{ux5b9aux4e49ux5e76ux8fd0ux884cux8ba1ux7b97}\r\n\r\n现在我们指定更多的计算\\ldots{}\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# 计算一个新值z: z^2 + x}\r\n\\NormalTok{zs_ }\\OperatorTok{=} \\NormalTok{zs}\\OperatorTok{*}\\NormalTok{zs }\\OperatorTok{+} \\NormalTok{xs}\r\n\r\n\\CommentTok{# 这个新值会发散吗？}\r\n\\NormalTok{not_diverged }\\OperatorTok{=} \\NormalTok{tf.complex_abs(zs_) }\\OperatorTok{<} \\DecValTok{4}\r\n\r\n\\CommentTok{# 更新zs并且迭代计算。}\r\n\\CommentTok{#}\r\n\\CommentTok{# 说明：在这些值发散之后，我们仍然在计算zs，这个计算消耗特别大！}\r\n\\CommentTok{#      如果稍微简单点，这里有更好的方法来处理。}\r\n\\CommentTok{#}\r\n\\NormalTok{step }\\OperatorTok{=} \\NormalTok{tf.group(}\r\n  \\NormalTok{zs.assign(zs_),}\r\n  \\NormalTok{ns.assign_add(tf.cast(not_diverged, }\\StringTok{\"float32\"}\\NormalTok{))}\r\n  \\NormalTok{)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\ldots{}继续执行几百个步骤\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\ControlFlowTok{for} \\NormalTok{i }\\OperatorTok{in} \\BuiltInTok{range}\\NormalTok{(}\\DecValTok{200}\\NormalTok{): step.run()}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n让我们看看我们得到了什么。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{DisplayFractal(ns.}\\BuiltInTok{eval}\\NormalTok{())}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics{../images/mandelbrot_output.jpg}\r\n\\caption{jpeg}\r\n\\end{figure}\r\n\r\n结果不错！\r\n\r\n\\begin{quote}\r\n原文：\\href{http://tensorflow.org/tutorials/mandelbrot/index.md}{Mandelbrot\r\nSet} 翻译：\\href{https://github.com/ericxk}{ericxk}\r\n校对：\\href{https://github.com/tensorfly}{tensorfly}\r\n\\end{quote}"
  },
  {
    "path": "tex_pdf/tutorials/c2s09_pdes.tex",
    "content": "\r\n\r\n\\section{偏微分方程 }\\label{ux504fux5faeux5206ux65b9ux7a0b}\r\n\r\n\\textbf{\\emph{TensorFlow}}\r\n不仅仅是用来机器学习，它更可以用来模拟仿真。在这里，我们将通过模拟仿真几滴落入一块方形水池的雨点的例子，来引导您如何使用\r\n\\textbf{\\emph{TensorFlow}} 中的偏微分方程来模拟仿真的基本使用方法。\r\n\r\n\\begin{quote}\r\n注：本教程最初是准备做为一个 \\textbf{IPython} 的手册。\r\n\\textgreater{}译者注:关于偏微分方程的相关知识，译者推荐读者查看\r\n\\href{http://open.163.com/}{\\textbf{网易公开课}}\r\n上的\\href{http://open.163.com/special/opencourse/multivariable.html}{\\textbf{《麻省理工学院公开课：多变量微积分》}}课程。\r\n\\end{quote}\r\n\r\n\\subsection{基本设置 }\\label{ux57faux672cux8bbeux7f6e}\r\n\r\n首先,我们需要导入一些必要的引用。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{#导入模拟仿真需要的库}\r\n\\ImportTok{import} \\NormalTok{tensorflow }\\ImportTok{as} \\NormalTok{tf}\r\n\\ImportTok{import} \\NormalTok{numpy }\\ImportTok{as} \\NormalTok{np}\r\n\r\n\\CommentTok{#导入可视化需要的库}\r\n\\ImportTok{import} \\NormalTok{PIL.Image}\r\n\\ImportTok{from} \\NormalTok{cStringIO }\\ImportTok{import} \\NormalTok{StringIO}\r\n\\ImportTok{from} \\NormalTok{IPython.display }\\ImportTok{import} \\NormalTok{clear_output, Image, display}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n然后，我们还需要一个用于表示池塘表面状态的函数。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\KeywordTok{def} \\NormalTok{DisplayArray(a, fmt}\\OperatorTok{=}\\StringTok{'jpeg'}\\NormalTok{, rng}\\OperatorTok{=}\\NormalTok{[}\\DecValTok{0}\\NormalTok{,}\\DecValTok{1}\\NormalTok{]):}\r\n  \\CommentTok{\"\"\"Display an array as a picture.\"\"\"}\r\n  \\NormalTok{a }\\OperatorTok{=} \\NormalTok{(a }\\OperatorTok{-} \\NormalTok{rng[}\\DecValTok{0}\\NormalTok{])}\\OperatorTok{/}\\BuiltInTok{float}\\NormalTok{(rng[}\\DecValTok{1}\\NormalTok{] }\\OperatorTok{-} \\NormalTok{rng[}\\DecValTok{0}\\NormalTok{])}\\OperatorTok{*}\\DecValTok{255}\r\n  \\NormalTok{a }\\OperatorTok{=} \\NormalTok{np.uint8(np.clip(a, }\\DecValTok{0}\\NormalTok{, }\\DecValTok{255}\\NormalTok{))}\r\n  \\NormalTok{f }\\OperatorTok{=} \\NormalTok{StringIO()}\r\n  \\NormalTok{PIL.Image.fromarray(a).save(f, fmt)}\r\n  \\NormalTok{display(Image(data}\\OperatorTok{=}\\NormalTok{f.getvalue()))}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n最后，为了方便演示，这里我们需要打开一个 \\textbf{\\emph{TensorFlow}}\r\n的交互会话（interactive\r\nsession）。当然为了以后能方便调用，我们可以把相关代码写到一个可以执行的\\textbf{\\emph{Python}}文件中。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{sess }\\OperatorTok{=} \\NormalTok{tf.InteractiveSession()}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsection{定义计算函数 }\\label{ux5b9aux4e49ux8ba1ux7b97ux51fdux6570}\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\KeywordTok{def} \\NormalTok{make_kernel(a):}\r\n  \\CommentTok{\"\"\"Transform a 2D array into a convolution kernel\"\"\"}\r\n  \\NormalTok{a }\\OperatorTok{=} \\NormalTok{np.asarray(a)}\r\n  \\NormalTok{a }\\OperatorTok{=} \\NormalTok{a.reshape(}\\BuiltInTok{list}\\NormalTok{(a.shape) }\\OperatorTok{+} \\NormalTok{[}\\DecValTok{1}\\NormalTok{,}\\DecValTok{1}\\NormalTok{])}\r\n  \\ControlFlowTok{return} \\NormalTok{tf.constant(a, dtype}\\OperatorTok{=}\\DecValTok{1}\\NormalTok{)}\r\n\r\n\\KeywordTok{def} \\NormalTok{simple_conv(x, k):}\r\n  \\CommentTok{\"\"\"A simplified 2D convolution operation\"\"\"}\r\n  \\NormalTok{x }\\OperatorTok{=} \\NormalTok{tf.expand_dims(tf.expand_dims(x, }\\DecValTok{0}\\NormalTok{), }\\OperatorTok{-}\\DecValTok{1}\\NormalTok{)}\r\n  \\NormalTok{y }\\OperatorTok{=} \\NormalTok{tf.nn.depthwise_conv2d(x, k, [}\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{, }\\DecValTok{1}\\NormalTok{], padding}\\OperatorTok{=}\\StringTok{'SAME'}\\NormalTok{)}\r\n  \\ControlFlowTok{return} \\NormalTok{y[}\\DecValTok{0}\\NormalTok{, :, :, }\\DecValTok{0}\\NormalTok{]}\r\n\r\n\\KeywordTok{def} \\NormalTok{laplace(x):}\r\n  \\CommentTok{\"\"\"Compute the 2D laplacian of an array\"\"\"}\r\n  \\NormalTok{laplace_k }\\OperatorTok{=} \\NormalTok{make_kernel([[}\\FloatTok{0.5}\\NormalTok{, }\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{0.5}\\NormalTok{],}\r\n                           \\NormalTok{[}\\FloatTok{1.0}\\NormalTok{, }\\OperatorTok{-}\\DecValTok{6}\\NormalTok{., }\\FloatTok{1.0}\\NormalTok{],}\r\n                           \\NormalTok{[}\\FloatTok{0.5}\\NormalTok{, }\\FloatTok{1.0}\\NormalTok{, }\\FloatTok{0.5}\\NormalTok{]])}\r\n  \\ControlFlowTok{return} \\NormalTok{simple_conv(x, laplace_k)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsection{定义偏微分方程\r\n}\\label{ux5b9aux4e49ux504fux5faeux5206ux65b9ux7a0b}\r\n\r\n首先,我们需要创建一个完美的 500 × 500\r\n的正方形池塘,就像是我们在现实中找到的一样。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{N }\\OperatorTok{=} \\DecValTok{500}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n然后，我们需要创建了一个池塘和几滴将要坠入池塘的雨滴。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Initial Conditions -- some rain drops hit a pond}\r\n\r\n\\CommentTok{# Set everything to zero}\r\n\\NormalTok{u_init }\\OperatorTok{=} \\NormalTok{np.zeros([N, N], dtype}\\OperatorTok{=}\\StringTok{\"float32\"}\\NormalTok{)}\r\n\\NormalTok{ut_init }\\OperatorTok{=} \\NormalTok{np.zeros([N, N], dtype}\\OperatorTok{=}\\StringTok{\"float32\"}\\NormalTok{)}\r\n\r\n\\CommentTok{# Some rain drops hit a pond at random points}\r\n\\ControlFlowTok{for} \\NormalTok{n }\\OperatorTok{in} \\BuiltInTok{range}\\NormalTok{(}\\DecValTok{40}\\NormalTok{):}\r\n  \\NormalTok{a,b }\\OperatorTok{=} \\NormalTok{np.random.randint(}\\DecValTok{0}\\NormalTok{, N, }\\DecValTok{2}\\NormalTok{)}\r\n  \\NormalTok{u_init[a,b] }\\OperatorTok{=} \\NormalTok{np.random.uniform()}\r\n\r\n\\NormalTok{DisplayArray(u_init, rng}\\OperatorTok{=}\\NormalTok{[}\\OperatorTok{-}\\FloatTok{0.1}\\NormalTok{, }\\FloatTok{0.1}\\NormalTok{])}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics{../images/pde_output_1.jpg}\r\n\\caption{jpeg}\r\n\\end{figure}\r\n\r\n现在，让我们来指定该微分方程的一些详细参数。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Parameters:}\r\n\\CommentTok{# eps -- time resolution}\r\n\\CommentTok{# damping -- wave damping}\r\n\\NormalTok{eps }\\OperatorTok{=} \\NormalTok{tf.placeholder(tf.float32, shape}\\OperatorTok{=}\\NormalTok{())}\r\n\\NormalTok{damping }\\OperatorTok{=} \\NormalTok{tf.placeholder(tf.float32, shape}\\OperatorTok{=}\\NormalTok{())}\r\n\r\n\\CommentTok{# Create variables for simulation state}\r\n\\NormalTok{U  }\\OperatorTok{=} \\NormalTok{tf.Variable(u_init)}\r\n\\NormalTok{Ut }\\OperatorTok{=} \\NormalTok{tf.Variable(ut_init)}\r\n\r\n\\CommentTok{# Discretized PDE update rules}\r\n\\NormalTok{U_ }\\OperatorTok{=} \\NormalTok{U }\\OperatorTok{+} \\NormalTok{eps }\\OperatorTok{*} \\NormalTok{Ut}\r\n\\NormalTok{Ut_ }\\OperatorTok{=} \\NormalTok{Ut }\\OperatorTok{+} \\NormalTok{eps }\\OperatorTok{*} \\NormalTok{(laplace(U) }\\OperatorTok{-} \\NormalTok{damping }\\OperatorTok{*} \\NormalTok{Ut)}\r\n\r\n\\CommentTok{# Operation to update the state}\r\n\\NormalTok{step }\\OperatorTok{=} \\NormalTok{tf.group(}\r\n  \\NormalTok{U.assign(U_),}\r\n  \\NormalTok{Ut.assign(Ut_))}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\subsection{开始仿真 }\\label{ux5f00ux59cbux4effux771f}\r\n\r\n为了能看清仿真效果，我们可以用一个简单的 \\textbf{for}\r\n循环来远行我们的仿真程序。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\CommentTok{# Initialize state to initial conditions}\r\n\\NormalTok{tf.initialize_all_variables().run()}\r\n\r\n\\CommentTok{# Run 1000 steps of PDE}\r\n\\ControlFlowTok{for} \\NormalTok{i }\\OperatorTok{in} \\BuiltInTok{range}\\NormalTok{(}\\DecValTok{1000}\\NormalTok{):}\r\n  \\CommentTok{# Step simulation}\r\n  \\NormalTok{step.run(\\{eps: }\\FloatTok{0.03}\\NormalTok{, damping: }\\FloatTok{0.04}\\NormalTok{\\})}\r\n  \\CommentTok{# Visualize every 50 steps}\r\n  \\ControlFlowTok{if} \\NormalTok{i }\\OperatorTok{%} \\DecValTok{50} \\OperatorTok{==} \\DecValTok{0}\\NormalTok{:}\r\n    \\NormalTok{clear_output()}\r\n    \\NormalTok{DisplayArray(U.}\\BuiltInTok{eval}\\NormalTok{(), rng}\\OperatorTok{=}\\NormalTok{[}\\OperatorTok{-}\\FloatTok{0.1}\\NormalTok{, }\\FloatTok{0.1}\\NormalTok{])}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics{../images/pde_output_2.jpg}\r\n\\caption{jpeg}\r\n\\end{figure}\r\n\r\n看！！ 雨点落在池塘中,和现实中一样的泛起了涟漪。\r\n\r\n\\begin{quote}\r\n原文链接:\\url{http://tensorflow.org/tutorials/pdes/index.md}\r\n翻译:{[}@wangaicc{]}(https://github.com/wangaicc)\r\n校对:{[}@tensorfly{]}(https://github.com/tensorfly)\r\n\\end{quote}"
  },
  {
    "path": "tex_pdf/tutorials/c2s10_mnist_download.tex",
    "content": "\r\n\r\n\\section{MNIST 数据下载 }\\label{mnist-ux6570ux636eux4e0bux8f7d}\r\n\r\n源码:\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/}{tensorflow/g3doc/tutorials/mnist/}\r\n\r\n本教程的目标是展示如何下载用于手写数字分类问题所要用到的（经典）MNIST数据集。\r\n\r\n\\subsection{教程 文件 }\\label{ux6559ux7a0b-ux6587ux4ef6}\r\n\r\n本教程需要使用以下文件：\r\n\r\n\\begin{longtable}[c]{@{}ll@{}}\r\n\\toprule\r\n\\begin{minipage}[b]{0.05\\columnwidth}\\raggedright\\strut\r\n文件\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[b]{0.05\\columnwidth}\\raggedright\\strut\r\n目的\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\midrule\r\n\\endhead\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/g3doc/tutorials/mnist/input_data.py}{\\texttt{input\\_data.py}}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n下载用于训练和测试的MNIST数据集的源码\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\bottomrule\r\n\\end{longtable}\r\n\r\n\\subsection{准备数据 }\\label{ux51c6ux5907ux6570ux636e}\r\n\r\nMNIST是在机器学习领域中的一个经典问题。该问题解决的是把28x28像素的灰度手写数字图片识别为相应的数字，其中数字的范围从0到9.\r\n\r\n\\begin{figure}[htbp]\r\n\\centering\r\n\\includegraphics{../images/mnist_digits.png}\r\n\\caption{MNIST Digits}\r\n\\end{figure}\r\n\r\n更多详情, 请参考 \\href{http://yann.lecun.com/exdb/mnist/}{Yann LeCun's\r\nMNIST page} 或\r\n\\href{http://colah.github.io/posts/2014-10-Visualizing-MNIST/}{Chris\r\nOlah's visualizations of MNIST}.\r\n\r\n\\subsubsection{下载 }\\label{ux4e0bux8f7d}\r\n\r\n\\href{http://yann.lecun.com/exdb/mnist/}{Yann LeCun's MNIST page}\r\n也提供了训练集与测试集数据的下载。\r\n\r\n\\begin{longtable}[c]{@{}ll@{}}\r\n\\toprule\r\n\\begin{minipage}[b]{0.05\\columnwidth}\\raggedright\\strut\r\n文件\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[b]{0.05\\columnwidth}\\raggedright\\strut\r\n内容\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\midrule\r\n\\endhead\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz}{\\texttt{train-images-idx3-ubyte.gz}}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n训练集图片 - 55000 张 训练图片, 5000 张 验证图片\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz}{\\texttt{train-labels-idx1-ubyte.gz}}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n训练集图片对应的数字标签\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz}{\\texttt{t10k-images-idx3-ubyte.gz}}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n测试集图片 - 10000 张 图片\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n\\href{http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz}{\\texttt{t10k-labels-idx1-ubyte.gz}}\r\n\\strut\\end{minipage} &\r\n\\begin{minipage}[t]{0.05\\columnwidth}\\raggedright\\strut\r\n测试集图片对应的数字标签\r\n\\strut\\end{minipage}\\tabularnewline\r\n\\bottomrule\r\n\\end{longtable}\r\n\r\n在 \\texttt{input\\_data.py} 文件中, \\texttt{maybe\\_download()}\r\n函数可以确保这些训练数据下载到本地文件夹中。\r\n\r\n文件夹的名字在 \\texttt{fully\\_connected\\_feed.py}\r\n文件的顶部由一个标记变量指定，你可以根据自己的需要进行修改。 \\#\\#\\# 解压\r\n与 重构\r\n\r\n这些文件本身并没有使用标准的图片格式储存，并且需要使用\\texttt{input\\_data.py}文件中\\texttt{extract\\_images()}\r\n和\\texttt{extract\\_labels()}函数来手动解压（页面中有相关说明）。\r\n\r\n图片数据将被解压成2维的tensor：\\texttt{{[}image\\ index,\\ pixel\\ index{]}}\r\n其中每一项表示某一图片中特定像素的强度值, 范围从 \\texttt{{[}0,\\ 255{]}}\r\n到 \\texttt{{[}-0.5,\\ 0.5{]}}。 ``image index''代表数据集中图片的编号,\r\n从0到数据集的上限值。``pixel index''代表该图片中像素点得个数,\r\n从0到图片的像素上限值。\r\n\r\n以\\texttt{train-*}开头的文件中包括60000个样本，其中分割出55000个样本作为训练集，其余的5000个样本作为验证集。因为所有数据集中28x28像素的灰度图片的尺寸为784，所以训练集输出的tensor格式为\\texttt{{[}55000,\\ 784{]}}。\r\n\r\n数字标签数据被解压称1维的tensor:\r\n\\texttt{{[}image\\ index{]}}，它定义了每个样本数值的类别分类。对于训练集的标签来说，这个数据规模就是:\\texttt{{[}55000{]}}。\r\n\r\n\\subsubsection{数据集 对象 }\\label{ux6570ux636eux96c6-ux5bf9ux8c61}\r\n\r\n底层的源码将会执行下载、解压、重构图片和标签数据来组成以下的数据集对象:\r\n\r\n\\begin{longtable}[c]{@{}ll@{}}\r\n\\toprule\r\n数据集 & 目的\\tabularnewline\r\n\\midrule\r\n\\endhead\r\n\\texttt{data\\_sets.train} & 55000 组 图片和标签,\r\n用于训练。\\tabularnewline\r\n\\texttt{data\\_sets.validation} & 5000 组 图片和标签,\r\n用于迭代验证训练的准确性。\\tabularnewline\r\n\\texttt{data\\_sets.test} & 10000 组 图片和标签,\r\n用于最终测试训练的准确性。\\tabularnewline\r\n\\bottomrule\r\n\\end{longtable}\r\n\r\n执行\\texttt{read\\_data\\_sets()}函数将会返回一个\\texttt{DataSet}实例，其中包含了以上三个数据集。函数\\texttt{DataSet.next\\_batch()}是用于获取以\\texttt{batch\\_size}为大小的一个元组，其中包含了一组图片和标签，该元组会被用于当前的TensorFlow运算会话中。\r\n\r\n\\begin{Shaded}\r\n\\begin{Highlighting}[]\r\n\\NormalTok{images_feed, labels_feed }\\OperatorTok{=} \\NormalTok{data_set.next_batch(FLAGS.batch_size)}\r\n\\end{Highlighting}\r\n\\end{Shaded}\r\n\r\n原文地址：\\href{https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/tutorials/mnist/download/index.md}{MNIST\r\nData Download} 翻译：\\href{https://github.com/btpeter}{btpeter}\r\n校对：waiwaizheng"
  }
]