TensorFlow

![]() | 此條目包含過多行話或專業術語,可能需要簡化或提出進一步解釋。 |
![]() | |
開發者 | 谷歌大脑团队[1] |
---|---|
首次发布 | 2015年11月9日 |
当前版本 | 2.0.0[2](2019年10月1日 ) |
源代码库 | |
编程语言 | Python、C++、CUDA |
平台 | Linux、macOS、Windows、Android |
类型 | 机器学习库 |
许可协议 | Apache 2.0开源许可证 |
网站 | www |
TensorFlow是一个开源软件库,用于各种感知和语言理解任务的机器学习。[3]目前被50个团队[3]:min 0:15/2:17用于研究和生产许多Google商业产品[4]:p.2,如语音辨識、Gmail、Google 相册和搜索[3]:0:26/2:17,其中许多产品曾使用过其前任软件DistBelief。
TensorFlow最初由谷歌大脑团队开发,用于Google的研究和生产,于2015年11月9日在Apache 2.0开源许可证下发布。[1][5]
历史
DistBelief
从2010年开始,谷歌大脑建立DistBelief作为他们的第一代专有的机器学习系统。50多个团队在Google和其他Alphabet公司在商业产品部署了DistBelief的深度学习神经网络,包括Google搜索、Google语音搜索、广告、Google 相册、Google地图、Google街景、Google翻译和YouTube。[4][6]Google指派计算机科学家,如Geoffrey Hinton和Jeff Dean,简化和重构DistBelief的代码库,使其变成一个更快、更健壮的应用级别代码库,形成了TensorFlow。[7]2009年,Hinton领导的研究小组大大减少使用DistBelief的神经网络的错误数量,通过Hinton在广义反向传播的科学突破。最值得注意的是,Hinton的突破直接使Google语音识别软件中的错误减少至少25%。[8]
TensorFlow
TensorFlow是谷歌大脑的第二代机器学习系统。
从0.8.0版本(发布于2016年4月)开始原生的支持分布式运行。
从0.9.0版本(发布于2016年6月)开始支持iOS。
从0.12.0版本(发布于2016年12月)开始支持Windows系统。该移植代码主要由微软贡献。
1.0.0版本发布于2017年2月11日。虽然参考实现运行在单台设备,TensorFlow可以运行在多个CPU和GPU(和可选的CUDA扩展和图形处理器通用计算的SYCL扩展)。[9]TensorFlow可用于64位Linux、macOS和Windows,以及移动计算平台,包括Android和iOS。
TensorFlow的计算使用有状态的数据流图表示。TensorFlow的名字来源于这类神经网络对多维数组执行的操作。这些多维数组被称为张量(Tensor)。2016年6月,Jeff Dean称在GitHub有1500个库提到[需要解释]了TensorFlow,其中只有5个来自Google。[10]
张量处理单元(TPU)
2016年5月,Google宣布了张量处理单元(TPU),一个专为机器学习和TensorFlow全定制的专用集成电路。TPU是一个可编程的人工智能加速器,提供高吞吐量的低精度计算(如8位),面向使用或运行模型而不是训练模型。Google宣布他们已经在数据中心中运行TPU长达一年多,发现它们对机器学习提供一个数量级更优的每瓦特性能。[11]
2017年5月Google宣布第二代张量处理单元,并在Google Compute Engine中可用。[12]第二代TPU提供最高180 teraflops性能,组装成64个TPU的集群时提供最高11.5 petaflops性能。
TensorFlow Lite
2017年5月Google宣布从Android Oreo开始,提供一个专用于Android开发的软件栈TensorFlow Lite[13]。
应用
Google于2015年10月26日正式发布了RankBrain,由TensorFlow支持。
特性
TensorFlow提供了一个Python API,以及C++、Haskell、Java、Go和Rust API。第三方包可用于 C#、.NET Core、Julia、R和Scala。
TensorFlow的底层核心引擎由C++实现,通过gRPC实现网络互访、分布式执行。虽然它的Python/C++/Java API共享了大部分执行代码,但是有关于反向传播梯度计算的部分需要在不同语言单独实现。目前只有Python API较为丰富的实现了反向传播部分。所以大多数人使用Python进行模型训练,但是可以选择使用其它语言进行线上推理。
TensorFlow在Windows和Linux上支持使用Bazel或CMake构建,在某些平台上也支持直接使用GNU make进行编译。
例子
例:Hello World。
import tensorflow as tf
hw = tf.constant("Hello World")
with tf.Session() as sess:
print(sess.run(hw))
例:两个矩阵相乘。
import tensorflow as tf
# Build a dataflow graph.
c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d)
# Construct a `Session` to execute the graph.
with tf.Session() as sess:
# Execute the graph and store the value that `e` represents in `result`.
result = sess.run(e)
print(result)
例:使用Feeding在执行时传入参数
import tensorflow as tf
# Build a dataflow graph.
c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d)
# Construct a `Session` to execute the graph.
sess = tf.Session()
# Execute the graph and store the value that `e` represents in `result`.
result = sess.run(e,feed_dict={c:[[0.0, 0.0], [3.0, 4.0]]})
print(result)
sess.close()
TensorFlow的一大特色是其图中的节点可以是带状态的。
例:使用占位符
input_placeholder = tf.placeholder(tf.int32)
sess = tf.Session()
print (sess.run(input_placeholder, feed_dict={input_placeholder: 2}))
sess.close()
例:使用变量、给变量赋值
使用tf.get_variable()创建变量。tf.get_variable() 的前两个参数是必需的,其余参数是可选的。tf.get_variable(name,shape)。name 是一个唯一标识这个变量对象的字符串。它必须相对于全局图是唯一的,所以要明了你使用过的所有命名,确保没有重复。shape 是与张量形状对应的整数数组,按顺序每个维度只有一个整数。一个 3x8 矩阵形状是 [3, 8]。要创建一个标量,就需要使用形状为 [] 的空列表。有两种将值放入变量的方法:初始化器和 tf.assign()。初始化器应该把声明(tf.constant_initializer)与执行初始化(tf.global_variables_initializer)两种节点配合使用。
import tensorflow as tf
count_variable = tf.get_variable("count", [])
zero_node = tf.constant(0.)
assign_node = tf.assign(count_variable, zero_node)
sess = tf.Session()
sess.run(assign_node)
print (sess.run(count_variable))
const_init_node = tf.constant_initializer(0.)
count_variable1 = tf.get_variable("count1", [], initializer=const_init_node)
init = tf.global_variables_initializer()
sess.run(init)
print (sess.run(count_variable1))
sess.close()
例:带状态的图
import tensorflow as tf
# Build a dataflow graph.
count = tf.Variable([0],trainable=False)
init_op = tf.global_variables_initializer()
update_count = count.assign_add(tf.constant([2]))
# Construct a `Session` to execute the graph.
sess = tf.Session()
sess.run(init_op)
for step in range(10):
result = sess.run(update_count)
print("step %d: count = %g" % (step,result))
sess.close()
例:梯度计算
import tensorflow as tf
# Build a dataflow graph.
filename_queue = tf.train.string_input_producer(['1.txt'],num_epochs=1)
reader = tf.TextLineReader()
key,value = reader.read(filename_queue)
num = tf.decode_csv(value,record_defaults=[[0]])
x = tf.Variable([0])
loss = x * num
grads = tf.gradients([loss],x)
grad_x = grads[0]
def train_fn(sess):
train_fn.counter += 1
result = sess.run(grad_x)
print("step %d: grad = %g" % (train_fn.counter,result))
train_fn.counter = 0
sv = tf.train.Supervisor()
tf.train.basic_train_loop(sv,train_fn)
假设1.txt的内容为:
3
2
5
8
那么上述程序的输出应该为:
step 1: grad = 3
step 2: grad = 2
step 3: grad = 5
step 4: grad = 8
在TensorFlow2.0中,它废除了许多复杂的特性如 tf.Session , tf.placeHolder, 还有 tf.contrib ! 新版Hello World(使用动态图):
import tensorflow as tf
hw = tf.constant("Hello world!")
print(hw.numpy)
旧的方式可以在程序前加上
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
来完美执行(除了被完整删除的tf.contrib)。 TensorFlow2.0利用PyTorch的灵感,使用Keras,简化机器学习的过程。
应用
广泛的应用程序使用TensorFlow作为基础,其中它已成功实现自动化图像字幕软件,例如DeepDream。[14]2015年10月26日,Google正式启用了由TensorFlow提供支持的RankBrain。RankBrain现在处理大量的搜索查询,替换和补充传统的静态算法搜索结果。[15]
参阅
引用
- ^ 1.0 1.1 Credits. [2015-11-10]. (原始内容存档于2015-11-17).
- ^ TensorFlow Release. [1 Oct 2019] (美国英语).
- ^ 3.0 3.1 3.2 "TensorFlow: Open source machine learning" (页面存档备份,存于互联网档案馆) "It is machine learning software being used for various kinds of perceptual and language understanding tasks" — Jeffrey Dean, minute 0:47 / 2:17 from Youtube clip
- ^ 4.0 4.1 TensorFlow: Large-scale machine learning on heterogeneous systems (PDF). Google Research. 2015-11-09 [2015-11-10]. (原始内容存档 (PDF)于2015-11-20).
- ^ Google Just Open Sourced TensorFlow, Its Artificial Intelligence Engine. 2015-11-09 [2015-11-10]. (原始内容存档于2015-11-10).
- ^ Google Open-Sources The Machine Learning Tech Behind Google Photos Search, Smart Reply And More. 2015-11-09 [2015-11-11]. (原始内容存档于2015-11-10).
- ^ What Is TensorFlow, and Why Is Google So Excited About It?. 2015-11-11 [2015-11-11]. (原始内容存档于2015-11-10).
- ^ Google chairman: We’re making 'real progress' on artificial intelligence. 2015-11-25 [2015-11-25]. (原始内容存档于2015-11-25).
- ^ Metz, Cade. TensorFlow, Google's Open Source AI , Points to a Fast-Changing Hardware World. 2015-11-10 [2015-11-11]. (原始内容存档于2015-11-11).
- ^ Machine Learning: Google I/O 2016 Minute 07:30/44:44 (页面存档备份,存于互联网档案馆) accessdate=2016-06-05
- ^ Google supercharges machine learning tasks with TPU custom chip. [2016-05-19]. (原始内容存档于2016-05-18).
- ^ Build and train machine learning models on our new Google Cloud TPUs. Google. 2017-05-17 [2017-05-18]. (原始内容存档于2017-05-17) (英语).
- ^ Google’s new machine learning framework is going to put more AI on your phone. [2018-01-14]. (原始内容存档于2017-08-22).
- ^ Google Offers Up Its Entire Machine Learning Library as Open-Source Software. 2015-11-11 [2015-11-11]. (原始内容存档于2015-11-23).
- ^ Google releases TensorFlow – Search giant makes its artificial intelligence software available to the public. 2015-11-25 [2015-11-25]. (原始内容存档于2015-11-25).
外部链接
- 官方网站
- 官方网站(简体中文)
- GitHub上的TensorFlow