Tensorflow工具癖程序员

【TensorFlow实战——笔记】第1章:TensorFlow

2017-07-11  本文已影响2558人  陈康stozen

1.1 TensorFlow概要

TensorFlow是Google公司开源的分布式机器学习框架。它的前身是DistBelief,在Google大规模内部使用。TensorFlow最早由Google Brain研究组发起。

官网:http://www.tersorflow.org
Github网址:https://github.com/tensorflow/tensorflow
模型仓库网址:https://github.com/tensorflow/models

TensorFlow关键版本发布历史

前端API支持语言

运算操作硬件

应用场景

其他

1.2 TensorFlow编程模型简介

TensorFlow的计算可以表示为一种有向图(directed graph),或者称计算图(computation graph)。图中每一个运算操作(operation)是一个节点(node),节点之间的连接线称为边(edge)。计算图中的节点可以有任意多个输入和任意多个输出,每个节点都只有一个运算操作。在计算图中流动(flow)的数据被称为张量(tensor),故得名TensorFlow。

计算图示例

import tensorflow as tf
b=tf.Variable(tf.zeros([100]))      # 生成100维的向量,初始化为0
W=tf.Variable(tf.random_uniform([784,100],-1,1))    # 生成784x100的随机矩阵W
x=tf.placeholder(name="x")      # 输入的Placeholder
relu=tf.nn.relu(tf.matmul(W, x)+b)  # ReLU(Wx+b)
C=[...]         # 根据ReLU函数的结果计算Cost
s=tf.Session()
for step in range(0, 10):
    input=...construct 100-D input array... # 为输入创建一个100维的向量
    result=s.run(C, feed_dict={x: input})   # 获取Cost,供给输入x
    print(step, result)

Session是用户使用TensorFlow时交互的接口。Session可以通过Extend方法添加节点(node)和边(edge)。Variable是一类特殊的运算操作,可以将tensor存储在内存或显存中。

内建运算操作

工作组件

每一个worker管理多个设备,设备的name包含硬件类别、编号、任务号(单机版没有),例如:

单机模式:/job:localhost/device:cpu:0
分布式模式: /job:worker/task:17/device:gpu:3

运行模式

单机单设备(device)改造成单机多设备(device)

for i in range(8):
    for d in range(4):
        with tf.device("/gpu:%d" % d):
            input = x[i] if d is 0 else m[d-1]
            m[d], c[d] = LSTMCell(input, mprev[d], cpev[d])
            mprev[d] = m[d]
            cprev[d] = c[d]

分布式容错性

当一个故障被检测到,整个计算图会终止并重启。

扩展功能

性能优化

高度优化的三方计算库

加速神经网络训练的并行计算模式

上一篇 下一篇

猜你喜欢

热点阅读