#目前,神经网络框架分为静态图框架和动态图框架,,PyTorch 和 TensorFlow、 Caffe等框架最大的区别就是他们拥有不同的计算图表现形式。
# TensorFlow使用静态图,这意味着我们先定义计算图,,然后不断地使用它。而在PyTorch中,每次都会重新构建一个新的计算图。
#对于使用者来说,两种形式的计算图有着非常大的区别,,同时静态图和动态图都有它们各自的优点,
# 比如动态图比较方便debug,使用者能够用任何他们喜欢的方式debug, 同时非常直观,而静态图是通过先定义后运行的方式,之后再次运行的时候就不需要再重新构建计算图,所以速度会比动态图快。
##下面我们比较while循环语句在TensorFlow和PyTorch中的定义
TensorFlow
#TensorFlow
import tensorflow as tf
first_counter = tf.constant(0)
second_counter = tf.constant(10)
def cond(first_counter,second_counter,*args):
return first_counter < second_counter
def body(first_counter, second_counter):
first_counter = tf.add(first_counter,2)
second_counter = tf.add(second_counter,1)
return first_counter,second_counter
c1,c2 = tf.while_loop(cond, body, [first_counter,second_counter])
with tf.Sess