TensorFlow从入门到实战资料汇总

2017-02-02 06:08 | 数据派

来源:DataCastle数据城堡

TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学**系统,其命名来源于本身的运行原理。Tensor(张量)意味着N维数组,Flow(流)意味着基于数据流图的计算,TensorFlow为张量从图象的一端流动到另一端计算过程。TensorFlow是将复杂的数据结构传输至人工智能神经网中进行分析和处理过程的系统。

TensorFlow已经开源一年多了,在谷歌的支持下,TensorFlow 已成为 GitHub 上最受欢迎的机器学**开源项目。TensorFlow支持CNN、RNN和LSTM算法,这都是目前在Image,Speech和NLP最流行的深度神经网络模型。


Data Flow Graph

不久前,Google Brain 工程师团队宣布在 TensorFlow 0.12 中加入初步的 Windows 支持。TensorFlow可以想象的空间非常之大,谷歌也将继续在TensorFlow的框架内继续研究,涉及到自然语言处理、机器翻译、模式识别等领域。

本文将对TensorFlow的基本使用及可以实现的案例进行介绍。

TensorFlow基础用法

1. 基本操作

import tensorflow as tf

import numpy as np

2. 乘法

a = tf.placeholder("float") # 创建符号变量

b = tf.placeholder("float")

y = tf.mul(a, b) # 乘法操作,作用在符号变量上。

sess = tf.Session() # 创建会话,计算符号变量表达式

a1 = 4

b1 = 5

print "%f + %f = %f"%(4, 5,  sess.run(y, feed_dict={a: a1, b: b1})

3. 线性回归(模型:Y=W∗X+b)

# 生成训练数据 + 噪声,下面为了拟合 $$ Y = 2X $$

trX = np.linspace(-1, 1, 101)

trY = 2 * trX + np.random.randn(*trX.shape) * 0.33 # y=2x,但是加入了噪声

X = tf.placeholder("float") #输入输出符号变量

Y = tf.placeholder("float")

# 定义模型

def model(X, w):

return tf.mul(X, w) # 线性回归只需要调用乘法操作即可。

# 模型权重 W 用变量表示

w = tf.Variable(0.0, name="weights") # 共享变量

y_model = model(X, w)

# 定义损失函数

cost = (tf.pow(Y-y_model, 2)) # 平方损失函数

# 构建优化器,最小化损失函数。

train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

# 构建会话

sess = tf.Session()

# 初始化所有的符号共享变量

init = tf.initialize_all_variables()

# 运行会话

sess.run(init)

# 迭代训练

for i in range(100):

for (x, y) in zip(trX, trY):

sess.run(train_op, feed_dict={X: x, Y: y})

# 打印权重w

print(sess.run(w))

4. 逻辑回归(模型:y=sigmoid(X∗W+b) )

# 初始化权重w

def init_weights(shape):

return tf.Variable(tf.random_normal(shape, stddev=0.01))

# 定义模型

def model(X, w):

return tf.matmul(X, w)

# 获取mnist 数据

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels

# 定义占位符变量

X = tf.placeholder("float", [None, 784])

Y = tf.placeholder("float", [None, 10])

w = init_weights([784, 10])

py_x = model(X, w)

# 定义损失函数,交叉熵损失函数

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y))

# 训练操作,最小化损失函数

train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost)

# 预测操作,

predict_op = tf.argmax(py_x, 1)

# 定义会话

sess = tf.Session()

init = tf.initialize_all_variables()

sess.run(init)

# 调用多次梯度下降

for i in range(100):

# 训练,每个batch,

for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)):

sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})

# 测试,每个epoch

print i, np.mean(np.argmax(teY, axis=1) ==

sess.run(predict_op, feed_dict={X: teX, Y: teY}))

TensorFlow实现案例

https://github.com/aymericdamien/TensorFlow-Examples

这是github上的一个教程和开源项目集,涵盖了TensorFlow从入门到案例实现的各个层面的信息,足够让你从初步认识TensorFlow到自己进行项目实践。

1 - 入门

Hello World

笔记:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/1_Introduction/helloworld.ipynb

代码:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/1_Introduction/helloworld.py

基本操作

笔记:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/1_Introduction/basic_operations.ipynb

代码: https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/1_Introduction/basic_operations.py

2 - 基本模型

最近邻

笔记: https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/2_BasicModels/nearest_neighbor.ipynb

代码: https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/2_BasicModels/nearest_neighbor.py

线性回归

笔记:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/2_BasicModels/linear_regression.ipynb

代码:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/2_BasicModels/linear_regression.py

Logistic 回归

笔记:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/2_BasicModels/logistic_regression.ipynb

代码: https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/2_BasicModels/logistic_regression.py

3 - 神经网络

多层感知器

笔记: https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/3_NeuralNetworks/multilayer_perceptron.ipynb

代码: https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/multilayer_perceptron.py

卷积神经网络

笔记: https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/3_NeuralNetworks/convolutional_network.ipynb

代码:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/convolutional_network.py

循环神经网络(LSTM)

笔记:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/3_NeuralNetworks/recurrent_network.ipynb

代码:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/recurrent_network.py

双向循环神经网络(LSTM)

笔记: https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/3_NeuralNetworks/bidirectional_rnn.ipynb

代码:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/bidirectional_rnn.py

动态循环神经网络(LSTM)

代码: https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/dynamic_rnn.py

自编码器

笔记:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/3_NeuralNetworks/autoencoder.ipynb

代码:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/autoencoder.py

4 - 实用技术

保存和恢复模型

笔记: https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/4_Utils/save_restore_model.ipynb

代码:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/4_Utils/save_restore_model.py

图和损失可视化

笔记:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/4_Utils/tensorboard_basic.ipynb

代码:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/4_Utils/tensorboard_basic.py

Tensorboard——高级可视化

代码:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/4_Utils/tensorboard_advanced.py

5 - 多 GPU

多 GPU 上的基本操作

笔记:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/5_MultiGPU/multigpu_basics.ipynb

代码: https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/5_MultiGPU/multigpu_basics.py

数据集

一些案例需要 MNIST 数据集进行训练和测试。不要担心,运行这些案例时,该数据集会被自动下载下来(使用 input_data.py)。MNIST 是一个手写数字的数据库,查看这个笔记了解关于该数据集的描述: https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/0_Prerequisite/mnist_dataset_intro.ipynb

官方网站:http://yann.lecun.com/exdb/mnist/

更多案例

接下来的示例来自 TFLearn ,这是一个为 TensorFlow 提供了简化的接口的库。你可以看看,这里有很多示例和预构建的运算和层。

示例:https://github.com/tflearn/tflearn/tree/master/examples

预构建的运算和层:http://tflearn.org/doc_index/#api

教程

TFLearn 快速入门。通过一个具体的机器学**任务学** TFLearn 基础。开发和训练一个深度神经网络分类器。

笔记:

https://github.com/tflearn/tflearn/blob/master/tutorials/intro/quickstart.md

基础

线性回归,使用 TFLearn 实现线性回归:

https://github.com/tflearn/tflearn/blob/master/examples/basics/linear_regression.py

逻辑运算符。使用 TFLearn 实现逻辑运算符:

https://github.com/tflearn/tflearn/blob/master/examples/basics/logical.py

权重保持。保存和还原一个模型:

https://github.com/tflearn/tflearn/blob/master/examples/basics/weights_persistence.py

微调。在一个新任务上微调一个预训练的模型:

https://github.com/tflearn/tflearn/blob/master/examples/basics/finetuning.py

使用 HDF5。使用 HDF5 处理大型数据集:

https://github.com/tflearn/tflearn/blob/master/examples/basics/use_hdf5.py

使用 DASK。使用 DASK 处理大型数据集:

https://github.com/tflearn/tflearn/blob/master/examples/basics/use_dask.py

计算机视觉

多层感知器。一种用于 MNIST 分类任务的多层感知实现:

https://github.com/tflearn/tflearn/blob/master/examples/images/dnn.py

卷积网络(MNIST)。用于分类 MNIST 数据集的一种卷积神经网络实现:

https://github.com/tflearn/tflearn/blob/master/examples/images/convnet_mnist.py

卷积网络(CIFAR-10)。用于分类 CIFAR-10 数据集的一种卷积神经网络实现:

https://github.com/tflearn/tflearn/blob/master/examples/images/convnet_cifar10.py

网络中的网络。用于分类 CIFAR-10 数据集的 Network in Network 实现:

https://github.com/tflearn/tflearn/blob/master/examples/images/network_in_network.py

Alexnet。将 Alexnet 应用于 Oxford Flowers 17 分类任务:

https://github.com/tflearn/tflearn/blob/master/examples/images/alexnet.py

VGGNet。将 VGGNet 应用于 Oxford Flowers 17 分类任务:

https://github.com/tflearn/tflearn/blob/master/examples/images/vgg_network.py

VGGNet Finetuning (Fast Training)。使用一个预训练的 VGG 网络并将其约束到你自己的数据上,以便实现快速训练:

https://github.com/tflearn/tflearn/blob/master/examples/images/vgg_network_finetuning.py

RNN Pixels。使用 RNN(在像素的序列上)分类图像:

https://github.com/tflearn/tflearn/blob/master/examples/images/rnn_pixels.py

Highway Network。用于分类 MNIST 数据集的 Highway Network 实现:

https://github.com/tflearn/tflearn/blob/master/examples/images/highway_dnn.py

Highway Convolutional Network。用于分类 MNIST 数据集的 Highway Convolutional Network 实现:

https://github.com/tflearn/tflearn/blob/master/examples/images/convnet_highway_mnist.py

Residual Network (MNIST):

https://github.com/tflearn/tflearn/blob/master/examples/images/residual_network_mnist.py

应用于 MNIST 分类任务的一种瓶颈残差网络(bottleneck residual network):https://github.com/tflearn/tflearn/blob/master/examples/images/residual_network_mnist.py

Residual Network (CIFAR-10)。应用于 CIFAR-10 分类任务的一种残差网络:

https://github.com/tflearn/tflearn/blob/master/examples/images/residual_network_cifar10.py

Google Inception(v3)。应用于 Oxford Flowers 17 分类任务的谷歌 Inception v3 网络:

https://github.com/tflearn/tflearn/blob/master/examples/images/googlenet.py

自编码器。用于 MNIST 手写数字的自编码器:

https://github.com/tflearn/tflearn/blob/master/examples/images/autoencoder.py

自然语言处理

循环神经网络(LSTM),应用 LSTM 到 IMDB 情感数据集分类任务: https://github.com/tflearn/tflearn/blob/master/examples/nlp/lstm.py

双向 RNN(LSTM),将一个双向 LSTM 应用到 IMDB 情感数据集分类任务:

https://github.com/tflearn/tflearn/blob/master/examples/nlp/bidirectional_lstm.py

动态 RNN(LSTM),利用动态 LSTM 从 IMDB 数据集分类可变长度文本:

https://github.com/tflearn/tflearn/blob/master/examples/nlp/dynamic_lstm.py

城市名称生成,使用 LSTM 网络生成新的美国城市名:

https://github.com/tflearn/tflearn/blob/master/examples/nlp/lstm_generator_cityname.py

莎士比亚手稿生成,使用 LSTM 网络生成新的莎士比亚手稿:

https://github.com/tflearn/tflearn/blob/master/examples/nlp/lstm_generator_shakespeare.py

Seq2seq,seq2seq 循环网络的教学示例:

https://github.com/tflearn/tflearn/blob/master/examples/nlp/seq2seq_example.py

CNN Seq,应用一个 1-D 卷积网络从 IMDB 情感数据集中分类词序列:

https://github.com/tflearn/tflearn/blob/master/examples/nlp/cnn_sentence_classification.py

强化学**

Atari Pacman 1-step Q-Learning,使用 1-step Q-learning 教一台机器玩 Atari 游戏:

https://github.com/tflearn/tflearn/blob/master/examples/reinforcement_learning/atari_1step_qlearning.py

Recommender-Wide&Deep Network,推荐系统中 wide & deep 网络的教学示例:

https://github.com/tflearn/tflearn/blob/master/examples/others/recommender_wide_and_deep.py

Notebooks

Spiral Classification Problem,对斯坦福 CS231n spiral 分类难题的 TFLearn 实现:

https://github.com/tflearn/tflearn/blob/master/examples/notebooks/spiral.ipynb

可延展的 TensorFlow

层,与 TensorFlow 一起使用  TFLearn 层:

https://github.com/tflearn/tflearn/blob/master/examples/extending_tensorflow/layers.py

训练器,使用 TFLearn 训练器类训练任何 TensorFlow 图:

https://github.com/tflearn/tflearn/blob/master/examples/extending_tensorflow/layers.py

Bulit-in Ops,连同 TensorFlow 使用 TFLearn built-in 操作:

https://github.com/tflearn/tflearn/blob/master/examples/extending_tensorflow/builtin_ops.py

Summaries,连同 TensorFlow 使用 TFLearn summarizers:

https://github.com/tflearn/tflearn/blob/master/examples/extending_tensorflow/summaries.py

Variables,连同 TensorFlow 使用 TFLearn Variables:

https://github.com/tflearn/tflearn/blob/master/examples/extending_tensorflow/variables.py

TensorFlow从入门到实战资料汇总 2017-02-02 06:08 | 数据派的更多相关文章

  1. 机器学习&深度学习经典资料汇总,data.gov.uk大量公开数据

    <Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost到随机森林.D ...

  2. [转载]机器学习&深度学习经典资料汇总,全到让人震惊

    自学成才秘籍!机器学习&深度学习经典资料汇总 转自:中国大数据: http://www.thebigdata.cn/JiShuBoKe/13299.html [日期:2015-01-27] 来 ...

  3. redis学习资料汇总

    redis学习资料汇总 2017年01月07日 22:10:37 阅读数:281 转载:http://blog.csdn.net/wtyvhreal/article/details/50427627 ...

  4. d3可视化实战00:d3的使用心得和学习资料汇总

    最近以来,我使用d3进行我的可视化工具的开发已经3个月了,同时也兼用其他一些图表类库,自我感觉稍微有点心得.之前我也写过相关文章,我涉及的数据可视化的实现技术和工具,但是那篇文章对于项目开发而言太浅了 ...

  5. 转:python的nltk中文使用和学习资料汇总帮你入门提高

    python的nltk中文使用和学习资料汇总帮你入门提高 转:http://blog.csdn.net/huyoo/article/details/12188573 nltk的安装 nltk初步使用入 ...

  6. Spring Boot 从入门到实战汇总

    之前写过几篇spring boot入门到实战的博文,因为某些原因没能继续. 框架更新迭代很快,之前还是基于1.x,现在2.x都出来很久了.还是希望能从基于该框架项目开发的整体有一个比较系统的梳理,于是 ...

  7. 一份超全的Python学习资料汇总

    一.学习Python必备技能图谱二.0基础如何系统学习Python?一.Python的普及入门1.1 Python入门学习须知和书本配套学习建议1.2 Python简史1.3 Python的市场需求及 ...

  8. 【转】自学成才秘籍!机器学习&深度学习经典资料汇总

      小编都深深的震惊了,到底是谁那么好整理了那么多干货性的书籍.小编对此人表示崇高的敬意,小编不是文章的生产者,只是文章的搬运工. <Brief History of Machine Learn ...

  9. MongoDB资料汇总

    MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. 它的特点是高性能.易部署.易使用,存储数据非常方便.主要功能特性有: 面向集合存 ...

随机推荐

  1. BZOJ4514 [Sdoi2016]数字配对 【费用流】

    题目 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对,并获得 ci×c ...

  2. Java面试题之HashMap如何有效减少碰撞

    1.扰动函数算法,促使元素位置分布均匀,减少碰撞几率: 2.使用final对象,并采用合适的equals方法和hashCode方法:

  3. Python基础教程总结(一)

    引言: 一直都听说Python很强大,以前只是浏览了一些博客,发现有点像数学建模时使用的Matlab,就没有深入去了解了.如今Python使用的地方越来越多,最近又在学习机器学习方面的知识,因此想系统 ...

  4. scrapy之spiders

    官方文档:https://docs.scrapy.org/en/latest/topics/spiders.html# 一句话总结:spider是定义爬取的动作(是否跟进新的链接)及分析网页结构(提取 ...

  5. docker的通俗理解

    自己买了个服务器,前不久搭建好的一个网站,想要再搞一个站点,无奈只能修改端口后,再部署另外一个站点.繁琐的配置运行环境,迁移网站,是否让你感觉到很繁琐?服务器不想用了,想搬迁到另外一台服务器去部署,先 ...

  6. Foj 2148 二维几何(点是否在三角形内)

    题目大意:给n个坐标(不存在三点共线的点),求能够组成多少个凸四边形. #include<iostream> #include<cstdio> #include<cmat ...

  7. 我要好offer之 搜索算法大总结

    1. 二分搜索 详见笔者博文:二分搜索的那些事儿,非常全面 2. 矩阵二分搜索 (1) 矩阵每行递增,且下一行第一个元素大于上一个最后一个元素 (2) 矩阵每行递增,且每列也递增 3. DFS 深度优 ...

  8. FZOJ Problem 2110 Star

                                                                                                        ...

  9. 洛谷 [P2886] 牛继电器Cow Relays

    最短路 + 矩阵快速幂 我们可以改进矩阵快速幂,使得它适合本题 用图的邻接矩阵和快速幂实现 注意 dis[i][i] 不能置为 0 #include <iostream> #include ...

  10. button 默认类型是submit

    “form表单里的按钮,明明是button而不是input type = submit,点击还是提交,如何让它不提交?” (这个问题被我遇到了,很坑爹的,知道了就不以为然了.) 因为 button 默 ...