forked from hzy46/Deep-Learning-21-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
joy.he
committed
Mar 14, 2018
0 parents
commit fb71a7a
Showing
6,982 changed files
with
702,161 additions
and
0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
## Deep-Learning-21-Examples | ||
|
||
本工程是《21个项目玩转深度学习————基于TensorFlow的实践详解》的配套代码。写作这本书的初衷是希望提供较为系统详细的深度学习方面的实践案例,让读者可以在自己动手实验的过程中掌握深度学习的基本原理和TensorFlow的使用方法。 | ||
|
||
代码推荐的运行环境为:Ubuntu 14.04,Python 2.7、TensorFlow >= 1.4.0。请尽量使用类UNIX系统和Python 2运行本书的代码。 | ||
|
||
在运行过程中如果发现问题可以通过issue提出来。 | ||
|
||
### 快速指引 | ||
|
||
本书包含的项目由三部分组成:第1~11章主要介绍CNN相关的项目,其中8~11章为GAN模型和它的几个重要变体;第12~17章主要介绍RNN、LSTM 相关的项目;第18~21章主要介绍强化学习相关的项目。 | ||
|
||
点击下面的章节名可查看代码的详细运行方法和指令: | ||
|
||
|
||
1. [MNIST机器学习入门](chapter_1/README.md) | ||
2. [CIFAR-10与ImageNet图像识别](chapter_2/README.md) | ||
3. [打造自己的图像识别模型](chapter_3/README.md) | ||
4. [Deep Dream](chapter_4/README.md) | ||
5. [深度学习中的目标检测](chapter_5/README.md) | ||
6. [人脸检测和人脸识别](chapter_6/README.md) | ||
7. [图像风格迁移](chapter_7/README.md) | ||
8. [GAN与DCGAN入门](chapter_8/README.md) | ||
9. [pix2pix模型与自动上色技术](chapter_9/README.md) | ||
10. [超分辨率:让图像变得更清晰](chapter_10/README.md) | ||
11. [CycleGAN与非配对图像转换](chapter_11/README.md) | ||
12. [RNN基本结构与Char RNN文本生成](chapter_12/README.md) | ||
13. [序列分类问题详解](chapter_13/README.md) | ||
14. [词的向量表示:word2vec与词嵌入](chapter_14/README.md) | ||
15. [在TensorFlow中进行时间序列预测](chapter_15/README.md) | ||
16. [神经网络机器翻译技术](chapter_16/README.md) | ||
17. [看图说话:将图像转换为文字](chapter_17/README.md) | ||
18. [强化学习入门之Q Learning](chapter_18/README.md) | ||
19. [强化学习入门之SARSA算法](chapter_19/README.md) | ||
20. [深度强化学习:Deep Q Learning](chapter_20/README.md) | ||
21. [策略梯度(Policy Gradient)算法](chapter_21/README.md) | ||
|
||
### 各章data文件夹下载地址 | ||
|
||
本工程主要包含的是项目的源代码文件,以chapter\_1/ ~ chapter\_21/ 标识。在有的章节中,还会额外需要一些数据集、模型等文件,我们用chapter_<章节名>\_data/来表示。 | ||
|
||
例如:第四章有chapter\_4/和chapter\_4\_data/两个文件夹,前者是源代码,后者是在运行程序时会用的数据文件。 | ||
|
||
由于数据文件通常比较大,建议读者在用到时候再进行下载,下载地址为:[https://pan.baidu.com/s/1i7pKvFf](https://pan.baidu.com/s/1i7pKvFf),提取码:1kmf。 | ||
|
||
### 其他链接 | ||
|
||
- 意见反馈邮箱:hzydl21 [at] 163.com | ||
- 我的专栏博客:[AI Insight](https://zhuanlan.zhihu.com/ai-insight) | ||
|
||
### 致谢 | ||
|
||
感谢电子工业出版社的孙学瑛女士和孙亚东先生为本书付出的努力,同时也感谢刘婧源同学给本书的宝贵建议。本书对于来自于网络的代码,在书中和配套代码的Readme文件中,均作了引用说明,在此也对原作者表示深深的感谢。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
### 1. MNIST机器学习入门 | ||
|
||
**1.1.1 简介** | ||
|
||
下载MNIST数据集,并打印一些基本信息: | ||
``` | ||
python download.py | ||
``` | ||
|
||
**1.1.2 实验:将MNIST数据集保存为图片** | ||
|
||
``` | ||
python save_pic.py | ||
``` | ||
|
||
**1.1.3 图像标签的独热表示** | ||
|
||
打印MNIST数据集中图片的标签: | ||
``` | ||
python label.py | ||
``` | ||
|
||
**1.2.1 Softmax 回归** | ||
|
||
``` | ||
python softmax_regression.py | ||
``` | ||
|
||
**1.2.2 两层卷积网络分类** | ||
``` | ||
python softmax_regression.py | ||
``` | ||
|
||
#### 可能出现的错误 | ||
|
||
下载数据集时可能出现网络问题,可以用下面两种方法中的一种解决:1. 使用合适的代理 2.在MNIST的官方网站上下载文件train-images-idx3-ubyte.gz、train-labels-idx1-ubyte.gz、t10k-images-idx3-ubyte.gz、t10k-labels-idx1-ubyte.gz,并将它们存储在MNIST_data/文件夹中。 | ||
|
||
|
||
#### 拓展阅读 | ||
|
||
- 本章介绍的MNIST 数据集经常被用来检验机器学习模型的性能,在它的官网(地址:http://yann.lecun.com/exdb/mnist/ )中,可以找到多达68 种模型在该数据集上的准确率数据,包括相应的论文出处。这些模型包括线性分类器、K 近邻方法、普通的神经网络、卷积神经网络等。 | ||
- 本章的两个MNIST 程序实际上来自于TensorFlow 官方的两个新手教程,地址为https://www.tensorflow.org/get_started/mnist/beginners 和 https://www.tensorflow.org/get_started/mnist/pros 。读者可以将本书的内容和官方的教程对照起来进行阅读。这两个新手教程的中文版地址为http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html 和http://www.tensorfly.cn/tfdoc/tutorials/mnist_pros.html。 | ||
- 本章简要介绍了TensorFlow 的tf.Tensor 类。tf.Tensor 类是TensorFlow的核心类,常用的占位符(tf.placeholder)、变量(tf.Variable)都可以看作特殊的Tensor。读者可以参阅https://www.tensorflow.org/programmers_guide/tensors 来更深入地学习它的原理。 | ||
- 常用tf.Variable 类来存储模型的参数, 读者可以参阅[https://www.tensorflow.org/programmers_guide/variables](https://www.tensorflow.org/programmers_guide/variables) 详细了解它的运行机制, 文档的中文版地址为http://www.tensorfly.cn/tfdoc/how_tos/ variables.html。 | ||
- 只有通过会话(Session)才能计算出tf.Tensor 的值。强烈建议读者 在学习完tf.Tensor 和tf.Variable 后,阅读https://www.tensorflow.org/programmers_guide/graphs 中的内容,该文档描述了TensorFlow 中 计算图和会话的基本运行原理,对理解TensorFlow 的底层原理有很 大帮助。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# coding: utf-8 | ||
import tensorflow as tf | ||
from tensorflow.examples.tutorials.mnist import input_data | ||
|
||
|
||
def weight_variable(shape): | ||
initial = tf.truncated_normal(shape, stddev=0.1) | ||
return tf.Variable(initial) | ||
|
||
|
||
def bias_variable(shape): | ||
initial = tf.constant(0.1, shape=shape) | ||
return tf.Variable(initial) | ||
|
||
|
||
def conv2d(x, W): | ||
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') | ||
|
||
|
||
def max_pool_2x2(x): | ||
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], | ||
strides=[1, 2, 2, 1], padding='SAME') | ||
|
||
|
||
if __name__ == '__main__': | ||
# 读入数据 | ||
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) | ||
# x为训练图像的占位符、y_为训练图像标签的占位符 | ||
x = tf.placeholder(tf.float32, [None, 784]) | ||
y_ = tf.placeholder(tf.float32, [None, 10]) | ||
|
||
# 将单张图片从784维向量重新还原为28x28的矩阵图片 | ||
x_image = tf.reshape(x, [-1, 28, 28, 1]) | ||
|
||
# 第一层卷积层 | ||
W_conv1 = weight_variable([5, 5, 1, 32]) | ||
b_conv1 = bias_variable([32]) | ||
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) | ||
h_pool1 = max_pool_2x2(h_conv1) | ||
|
||
# 第二层卷积层 | ||
W_conv2 = weight_variable([5, 5, 32, 64]) | ||
b_conv2 = bias_variable([64]) | ||
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) | ||
h_pool2 = max_pool_2x2(h_conv2) | ||
|
||
# 全连接层,输出为1024维的向量 | ||
W_fc1 = weight_variable([7 * 7 * 64, 1024]) | ||
b_fc1 = bias_variable([1024]) | ||
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) | ||
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) | ||
# 使用Dropout,keep_prob是一个占位符,训练时为0.5,测试时为1 | ||
keep_prob = tf.placeholder(tf.float32) | ||
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) | ||
|
||
# 把1024维的向量转换成10维,对应10个类别 | ||
W_fc2 = weight_variable([1024, 10]) | ||
b_fc2 = bias_variable([10]) | ||
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 | ||
|
||
# 我们不采用先Softmax再计算交叉熵的方法,而是直接用tf.nn.softmax_cross_entropy_with_logits直接计算 | ||
cross_entropy = tf.reduce_mean( | ||
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)) | ||
# 同样定义train_step | ||
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) | ||
|
||
# 定义测试的准确率 | ||
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) | ||
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) | ||
|
||
# 创建Session和变量初始化 | ||
sess = tf.InteractiveSession() | ||
sess.run(tf.global_variables_initializer()) | ||
|
||
# 训练20000步 | ||
for i in range(20000): | ||
batch = mnist.train.next_batch(50) | ||
# 每100步报告一次在验证集上的准确度 | ||
if i % 100 == 0: | ||
train_accuracy = accuracy.eval(feed_dict={ | ||
x: batch[0], y_: batch[1], keep_prob: 1.0}) | ||
print("step %d, training accuracy %g" % (i, train_accuracy)) | ||
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) | ||
|
||
# 训练结束后报告在测试集上的准确度 | ||
print("test accuracy %g" % accuracy.eval(feed_dict={ | ||
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# coding:utf-8 | ||
# 从tensorflow.examples.tutorials.mnist引入模块。这是TensorFlow为了教学MNIST而提前编制的程序 | ||
from tensorflow.examples.tutorials.mnist import input_data | ||
# 从MNIST_data/中读取MNIST数据。这条语句在数据不存在时,会自动执行下载 | ||
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) | ||
|
||
# 查看训练数据的大小 | ||
print(mnist.train.images.shape) # (55000, 784) | ||
print(mnist.train.labels.shape) # (55000, 10) | ||
|
||
# 查看验证数据的大小 | ||
print(mnist.validation.images.shape) # (5000, 784) | ||
print(mnist.validation.labels.shape) # (5000, 10) | ||
|
||
# 查看测试数据的大小 | ||
print(mnist.test.images.shape) # (10000, 784) | ||
print(mnist.test.labels.shape) # (10000, 10) | ||
|
||
# 打印出第0幅图片的向量表示 | ||
print(mnist.train.images[0, :]) | ||
|
||
# 打印出第0幅图片的标签 | ||
print(mnist.train.labels[0, :]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# coding: utf-8 | ||
from tensorflow.examples.tutorials.mnist import input_data | ||
import numpy as np | ||
# 读取mnist数据集。如果不存在会事先下载。 | ||
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) | ||
|
||
# 看前20张训练图片的label | ||
for i in range(20): | ||
# 得到one-hot表示,形如(0, 1, 0, 0, 0, 0, 0, 0, 0, 0) | ||
one_hot_label = mnist.train.labels[i, :] | ||
# 通过np.argmax我们可以直接获得原始的label | ||
# 因为只有1位为1,其他都是0 | ||
label = np.argmax(one_hot_label) | ||
print('mnist_train_%d.jpg label: %d' % (i, label)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#coding: utf-8 | ||
from tensorflow.examples.tutorials.mnist import input_data | ||
import scipy.misc | ||
import os | ||
|
||
# 读取MNIST数据集。如果不存在会事先下载。 | ||
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) | ||
|
||
# 我们把原始图片保存在MNIST_data/raw/文件夹下 | ||
# 如果没有这个文件夹会自动创建 | ||
save_dir = 'MNIST_data/raw/' | ||
if os.path.exists(save_dir) is False: | ||
os.makedirs(save_dir) | ||
|
||
# 保存前20张图片 | ||
for i in range(20): | ||
# 请注意,mnist.train.images[i, :]就表示第i张图片(序号从0开始) | ||
image_array = mnist.train.images[i, :] | ||
# TensorFlow中的MNIST图片是一个784维的向量,我们重新把它还原为28x28维的图像。 | ||
image_array = image_array.reshape(28, 28) | ||
# 保存文件的格式为 mnist_train_0.jpg, mnist_train_1.jpg, ... ,mnist_train_19.jpg | ||
filename = save_dir + 'mnist_train_%d.jpg' % i | ||
# 将image_array保存为图片 | ||
# 先用scipy.misc.toimage转换为图像,再调用save直接保存。 | ||
scipy.misc.toimage(image_array, cmin=0.0, cmax=1.0).save(filename) | ||
|
||
print('Please check: %s ' % save_dir) | ||
|
Oops, something went wrong.