{"id":8123276,"url":"https://github.com/yongyehuang/Tensorflow-Tutorial","last_synced_at":"2025-07-25T04:30:55.444Z","repository":{"id":234015762,"uuid":"89056693","full_name":"yongyehuang/Tensorflow-Tutorial","owner":"yongyehuang","description":"Some interesting TensorFlow tutorials for beginners.","archived":false,"fork":false,"pushed_at":"2018-05-22T10:41:58.000Z","size":489305,"stargazers_count":901,"open_issues_count":4,"forks_count":371,"subscribers_count":52,"default_branch":"master","last_synced_at":"2024-11-21T09:42:51.743Z","etag":null,"topics":["cnn","lstm","tensorflow","tensorflow-tutorials"],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yongyehuang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-22T08:50:46.000Z","updated_at":"2024-09-21T13:11:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"c25899fc-4ed7-417d-9d23-1d5693cbcf55","html_url":"https://github.com/yongyehuang/Tensorflow-Tutorial","commit_stats":{"total_commits":55,"total_committers":1,"mean_commits":55.0,"dds":0.0,"last_synced_commit":"88b5cdbdf402a8ae7f65a540e5f81f3da8f45e7d"},"previous_names":["yongyehuang/tensorflow-tutorial"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yongyehuang%2FTensorflow-Tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yongyehuang%2FTensorflow-Tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yongyehuang%2FTensorflow-Tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yongyehuang%2FTensorflow-Tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yongyehuang","download_url":"https://codeload.github.com/yongyehuang/Tensorflow-Tutorial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227518307,"owners_count":17782962,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cnn","lstm","tensorflow","tensorflow-tutorials"],"created_at":"2024-04-17T23:42:40.130Z","updated_at":"2024-12-01T08:30:36.467Z","avatar_url":"https://github.com/yongyehuang.png","language":"Jupyter Notebook","funding_links":[],"categories":["Jupyter Notebook"],"sub_categories":[],"readme":"# Tensorflow-Tutorial\n\n2018-04 更新说明\n\n时间过去一年，TensorFlow 已经从 1.0 版本更新到了 1.8 版本，而且最近更新的非常频繁。最烦的就是每次更新很多 API 都改了，一些老版本的代码就跑不通了。因为本项目关注的人越来越多了，所以自己也感觉到非常有必要更新并更正一些之前的错误，否则误人子弟就不好了。这里不少内容可以直接在官方的教程中找到，官方文档也在不断完善中，我也是把里边的例子跑一下，加深理解而已，更多的还是要自己在具体任务中去搭模型，训模型才能很好地掌握。\n\n这一次更新主要内容如下：\n\n- 使用较新版本的 tfmaster\n- 所有的代码改成 python3.5\n- 重新整理了基础用例\n- 添加实战例子\n\n因为工作和学习比较忙，所以这些内容也没办法一下子完成。和之前的版本不同，之前我是作为一个入门菜鸟一遍学一边做笔记。虽然现在依然还是理解得不够，但是比之前掌握的知识应该多了不少，希望能够整理成一个更好的教程。\n\n之前的代码我放在了另外一个分支上： https://github.com/yongyehuang/Tensorflow-Tutorial/tree/1.2.1\n\n如果有什么问题或者建议，欢迎开issue或者邮件与我联系：yongye@bupt.edu.cn\n\n\n## 运行环境\n- python 3.5\n- tensorflow master (gpu version)\n\n\n## 文件结构\n```\n|- Tensorflow-Tutorial\n|　　|- example-notebook　　　　　# 入门教程 notebook 版\n|　　|- example-python　　　　　　# 入门教程 .py 版\n|　　|- utils　　　　　           # 一些工具函数(logging, tf.flags)\n|　　|- models　　　　　          # 一些实战的例子(BN, GAN, 序列标注，seq2seq 等，持续更新)\n|　　|- data　　　　　　　        # 数据\n|　　|- doc　　　　　　　　　　   # 相关文档\n```\n\n## 1.入门例子\n#### T_01.TensorFlow 的基本用法\n- [notebook](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_01%20Basic%20Usage.ipynb)\n\n介绍 TensorFlow 的变量、常量和基本操作，最后介绍了一个非常简单的回归拟合例子。\n\n\n\n#### T_02.实现一个两层的全连接网络对 MNIST 进行分类\n- [notebook](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_02%20A%20simple%20feedforward%20network%20for%20MNIST.ipynb)\n\n\n\n#### T_03.TensorFlow 变量命名管理机制\n- [notebook1](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_03_1%20The%20usage%20of%20%20name_scope%20and%20variable_scope.ipynb)\n介绍  tf.Variable() 和 tf.get_variable() 创建变量的区别；介绍如何使用 tf.name_scope() 和 tf.variable_scope() 管理命名空间。\n\n- [notebook2](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_03_2%20The%20usage%20of%20%20Collection.ipynb)\n除了使用变量命名来管理变量之外，还经常用到 collection 的方式来聚合一些变量或者操作。\n\n\n\n#### T_04.实现一个两层的卷积神经网络（CNN）对 MNIST 进行分类\n- [notebook1-使用原生API构建CNN](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_04_1%20Convolutional%20network%20for%20MNIST(1).ipynb)\n\n构建一个非常简单的 CNN 网络，同时输出中间各个核的可视化来理解 CNN 的原理。\n\u003ccenter\u003e\u003cimg src=\"https://raw.githubusercontent.com/yongyehuang/Tensorflow-Tutorial/master/figs/conv_mnist.png\" width=\"60%\" height=\"60%\"\u003e\n第一层卷积核可视化\u003c/center\u003e\n\n- [notebook2-自定义函数构建CNN](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_04_2%20Convolutional%20network%20for%20MNIST(2).ipynb)\n\n- [notebook3-使用tf.layers高级API构建CNN](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_04_3%20Convolutional%20network%20for%20MNIST(3).ipynb)\n\n- [code-加入 BN 层的 CNN](https://github.com/yongyehuang/Tensorflow-Tutorial/tree/master/utils_and_models/m01_batch_normalization)\n\n在上一个例子的基础上，加入 BN 层。在 CNN 中，使用 BN 层可以加速收敛速度，同时也能够减小初始化方式的影响。在使用 BN 层的时候要注意训练时用的是 mini-batch 的均值方差，测试时用的是指数平均的均值方差。所以在训练的过程中，一定要记得更新并保存均值方差。\n\n在这个小网络中：迭代 10000 步，batch_size=100，大概耗时 45s；添加了 BN 层之后，迭代同样的次数，大概耗时 90s.\n\n\n#### T_05.实现多层的 LSTM 和 GRU 网络对 MNIST 进行分类\n- [LSTM-notebook](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_05_1%20An%20understandable%20example%20to%20implement%20Multi-LSTM%20for%20MNIST.ipynb)\n- [GRU-notebook](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_05_2%20An%20understandable%20example%20to%20implement%20Multi-GRU%20for%20MNIST.ipynb)\n- [Bi-GRU-notebook](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_05_3%20Bi-GRU%20for%20MNIST.ipynb)\n\n\u003ccenter\u003e\u003cimg src=\"https://raw.githubusercontent.com/yongyehuang/Tensorflow-Tutorial/master/figs/lstm_8.png\" width=\"30%\" height=\"30%\"\u003e\n字符 8\u003c/center\u003e\n\n\u003ccenter\u003e\u003cimg src=\"https://raw.githubusercontent.com/yongyehuang/Tensorflow-Tutorial/master/figs/lstm_mnist.png\" width=\"50%\" height=\"50%\"\u003e\nlstm 对字符 8 的识别过程\u003c/center\u003e\n\n\n\n#### T_06.tensorboard 的简单用法\n- [notebook](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_06%20A%20very%20simple%20example%20for%20tensorboard.ipynb)\n\n\u003ccenter\u003e\u003cimg src=\"https://raw.githubusercontent.com/yongyehuang/Tensorflow-Tutorial/master/figs/graph2.png\" width=\"60%\" height=\"60%\"\u003e\n简单的 tensorboard 可视化\u003c/center\u003e\n\n\n\n#### T_07.使用 tf.train.Saver() 来保存模型\n- [notebook](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_07%20How%20to%20save%20the%20model.ipynb)\n\n\n\n#### T_08.【迁移学习】往一个已经保存好的 模型添加新的变量\n- [notebook](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_08%20%20%5Btransfer%20learning%5D%20Add%20new%20variables%20to%20graph%20and%20save%20the%20new%20model.ipynb)\n\n\n\n#### T_09.使用 tfrecord 打包不定长的序列数据\n- [notebook](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_09%20%5Btfrecord%5D%20use%20tfrecord%20to%20store%20sequences%20of%20different%20length.ipynb)\n- [reader-code](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/utils_and_models/u02_tfrecord/tfrecord_2_seqence_reader.py)\n- [writer-code](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/utils_and_models/u02_tfrecord/tfrecord_2_seqence_writer.py)\n\n\n\n#### T_10.使用 tf.data.Dataset 和 tfrecord 给 numpy 数据构建数据集\n - [dataset-notebook](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_10%20%5BDataset%5D%20numpy%20data.ipynb)\n - [tfrecord-reader-code](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/utils_and_models/u02_tfrecord/tfrecord_1_numpy_reader.py)\n - [tfrecord-writer-code](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/utils_and_models/u02_tfrecord/tfrecord_1_numpy_writer.py)\n\n\n下面是对 MNIST 数据训练集 55000 个样本 读取的一个速度比较，统一 `batch_size=128`，主要比较 `one-shot` 和 `initializable` 两种迭代方式：\n\n|iter_mode|buffer_size|100 batch(s)|\n|:----:|:---:|:---:|\n|one-shot|2000|125|\n|one-shot|5000|149|\n|initializable|2000|0.7|\n|initializable|5000|0.7|\n\n可以看到，使用 `initializable` 方式的速度明显要快很多。因为使用 `one-shot` 方式会把整个矩阵放在图中，计算非常非常慢。\n\n\n\n#### T_11.使用 tf.data.Dataset 和 tfrecord 给 图片数据 构建数据集\n- [dataset-notebook](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_11%20%5BDataset%5D%20image%20data.ipynb)\n- [tfrecord-writer-code](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/utils_and_models/u02_tfrecord/tfrecord_3_image_writer.py)\n- [tfrecord-reader-code](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/utils_and_models/u02_tfrecord/tfrecord_3_image_reader.py)\n\n对于 png 数据的读取,我尝试了 3 组不同的方式: one-shot 方式, tf 的队列方式(queue), tfrecord 方式. 同样是在机械硬盘上操作, 结果是 tfrecord 方式明显要快一些。（batch_size=128,图片大小为256*256,机械硬盘)\n\n|iter_mode|buffer_size|100 batch(s)|\n|:----:|:---:|:---:|\n|one-shot|2000|75|\n|one-shot|5000|86|\n|tf.queue|2000|11|\n|tf.queue|5000|11|\n|tfrecord|2000|5.3|\n|tfrecord|5000|5.3|\n\n如果是在 SSD 上面的话,tf 的队列方式应该也是比较快的.打包成 tfrecord 格式只是减少了小文件的读取，其实现也是使用队列的。\n\n\n#### T_12.TensorFlow 高级API tf.layers 的使用\n- [notebook](https://github.com/yongyehuang/Tensorflow-Tutorial/blob/master/example-notebook/Tutorial_12%20%5Btf.layer%5Dhigh%20layer%20API.ipynb)\n\n使用 TensorFlow 原生的 API 能够帮助自己很好的理解网络的细节，但是往往比较低效。 tf.layers 和 tf.keras 一样，是一个封装得比较好的一个高级库，接口用着挺方便的。所以在开发的时候，可以使用高级的接口能够有效的提高工作效率。\n\n\n# 2.TensorFlow 实战(持续更新)\n下面的每个例子都是相互独立的，每个文件夹下面的代码都是可以单独运行的，不依赖于其他文件夹。\n\n## [m01_batch_normalization: Batch Normalization 的使用](https://github.com/yongyehuang/Tensorflow-Tutorial/tree/master/models/m01_batch_normalization)\n参考：[tensorflow中batch normalization的用法](https://www.cnblogs.com/hrlnw/p/7227447.html)\n\n## [m02_dcgan: 使用 DCGAN 生成二次元头像](https://github.com/yongyehuang/Tensorflow-Tutorial/tree/master/models/m02_dcgan)\n参考：\n- [原论文:Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks](https://link.zhihu.com/?target=https%3A//arxiv.org/abs/1511.06434)\n- [GAN学习指南：从原理入门到制作生成Demo](https://zhuanlan.zhihu.com/p/24767059)\n- [代码：carpedm20/DCGAN-tensorflow](https://github.com/carpedm20/DCGAN-tensorflow)\n- [代码：aymericdamien/TensorFlow-Examples](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/dcgan.py)\n\n这里的 notebook 和 .py 文件的内容是一样的。本例子和下面的 GAN 模型用的数据集也是用了[GAN学习指南：从原理入门到制作生成Demo](https://zhuanlan.zhihu.com/p/24767059) 的二次元头像，感觉这里例子比较有意思。如果想使用其他数据集的话，只需要把数据集换一下就行了。\n\n下载链接: https://pan.baidu.com/s/1HBJpfkIFaGh0s2nfNXJsrA 密码: x39r\n\n下载后把所有的图片解压到一个文件夹中，比如本例中是： `data_path = '../../data/anime/'`\n\n运行： `python dcgan.py `\n\n## [m03_wgan: 使用 WGAN 生成二次元头像](https://github.com/yongyehuang/Tensorflow-Tutorial/tree/master/models/m03_wgan)\n这里的生成器和判别器我只实现了 DCGAN，没有实现 MLP. 如果想实现的话可以参考下面的两个例子。\n参考：\n- [原论文:Wasserstein GAN](https://arxiv.org/pdf/1701.07875.pdf)\n- [代码：jiamings/wgan](https://github.com/jiamings/wgan)\n- [代码：Zardinality/WGAN-tensorflow](https://github.com/Zardinality/WGAN-tensorflow)\n\n原版的 wgan： `python wgan.py `\n\n改进的 wgan-gp: `python wgan_gp.py`\n\n\n## [m04_pix2pix: image-to-image](https://github.com/yongyehuang/Tensorflow-Tutorial/tree/master/models/m04_pix2pix)\n代码来自：[affinelayer/pix2pix-tensorflow](https://github.com/affinelayer/pix2pix-tensorflow)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyongyehuang%2FTensorflow-Tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyongyehuang%2FTensorflow-Tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyongyehuang%2FTensorflow-Tutorial/lists"}