访问量: 10 次浏览
TensorFlow和Pytorch是最近最流行的两个深度学习库。
这两个库都在主流深度学习领域发展了各自的利基,都有优秀的文档和教程,最重要的是,它们背后有一个旺盛的、支持性的社区。
虽然这两个库都采用了有向无环图(或DAG)来表示他们的机器学习和深度学习模型,但他们让数据和计算在图中流动的方式仍然有很大的区别。
这两个库之间的微妙区别是,Tensorflow(v < 2.0)允许静态图计算,而Pytorch允许动态图计算。
本文将通过代码实例,以直观的方式介绍这些差异。本文假设你对计算图有一定的了解,并对TensorFlow和Pytorch模块有基本了解。
节点和边的属性。
节点代表直接应用于通过边流入和流出的数据的操作。
对于上述方程组,我们在TensorFlow中实现它时可以记住以下几点。
tf.Placeholder() 对象,它可以接受任何所需数据类型的输入。session.run() 方法中的 feed\_dict 属性传入所需的输入值,以计算输出和梯度。现在让我们在TensorFlow中实现上述计算,观察操作是如何发生的。
# Importing tensorflow version 1
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
# Initializing placeholder variables of
# the graph
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
# Defining the operation
c = tf.multiply(a, b)
# Instantiating a tensorflow session
with tf.Session() as sess:
# Computing the output of the graph by giving
# respective input values
out = sess.run(, feed_dict={a: [15.0], b: [20.0]})[0][0]
# Computing the output gradient of the output with
# respect to the input 'a'
derivative_out_a = sess.run(tf.gradients(c, a), feed_dict={
a: [15.0], b: [20.0]})[0][0]
# Computing the output gradient of the output with
# respect to the input 'b'
derivative_out_b = sess.run(tf.gradients(c, b), feed_dict={
a: [15.0], b: [20.0]})[0][0]
# Displaying the outputs
print(f'c = {out}')
print(f'Derivative of c with respect to a = {derivative_out_a}')
print(f'Derivative of c with respect to b = {derivative_out_b}')
输出:
c = 300.0
Derivative of c with respect to a = 20.0
Derivative of c with respect to b = 15.0
我们可以看到,输出结果与我们在介绍部分的计算结果正确匹配,从而表明成功完成。
从代码中可以看出静态结构,因为我们可以看到,一旦在一个会话中,我们不能定义新的操作(或节点),但我们肯定可以使用 sess.run() 方法中的 feed\_dict 属性来改变输入变量。
节点和边的属性。
节点代表数据(以张量的形式),边代表应用于输入数据的操作。
对于导论中给出的方程,我们在Pytorch中实现它时,可以记住以下事项。
backward() 方法,计算相对于通过 .grad 指定符访问的两个输入的相应偏导。现在让我们看看一个代码例子来验证我们的发现。
# Importing torch
import torch
# Initializing input tensors
a = torch.tensor(15.0, requires_grad=True)
b = torch.tensor(20.0, requires_grad=True)
# Computing the output
c = a * b
# Computing the gradients
c.backward()
# Collecting the output gradient of the
# output with respect to the input 'a'
derivative_out_a = a.grad
# Collecting the output gradient of the
# output with respect to the input 'b'
derivative_out_b = b.grad
# Displaying the outputs
print(f'c = {c}')
print(f'Derivative of c with respect to a = {derivative_out_a}')
print(f'Derivative of c with respect to b = {derivative_out_b}')
输出:
c = 300.0
Derivative of c with respect to a = 20.0
Derivative of c with respect to b = 15.0
我们可以看到,输出结果与我们在介绍部分的计算结果正确匹配,从而表明成功完成。
从代码中可以看出动态结构。我们可以看到,所有的输入和输出都只能在运行时访问和改变,这与Tensorflow使用的方法完全不同。
Tensorflow 转向 Pytorch 的原因之一。由于节点是在任何信息流经它们之前动态创建的,因此错误变得非常容易发现,因为用户完全控制了训练过程中使用的变量。本文阐明了Tensorflow和Pytorch的建模结构的区别。文章还通过代码实例列出了两种方法的一些优缺点。
这些库的开发背后各自的组织在随后的迭代中不断改进,但读者现在可以在为他们的下一个项目选择最佳框架之前做出更明智的决定。