【初学者入门系列】Tensorflow学习路线指引
导
语 TensorFlow是业界广泛使用的开源机器学习框架,虽然有后起之秀不断挑战它的地位,凭借快速部署、适合产品级应用等特点,目前在AI开发者中仍然占据Top2的地位。
工具篇
目前神经网络框架分为静态图框架和动态图框架,TensorFlow1.x主要使用静态图,这意味着我们先定义计算图,然后不断使用它;而在TensorFlow2.x中,每次都会重新构建一个新的计算图。
对于使用者来说,两种形式的计算图有着非常大的区别,同时静态图和动态图都有他们各自的优点,比如动态图比较方便debug,使用者能够用任何他们喜欢的方式进行debug,同时非常直观;而静态图是通过先定义后运行的方式,之后再次运行的时候就不再需要重新构建计算图,所以速度会比动态图快一些。
Tensorflow1.x由于大部分版本不支持动态图机制,即不支持代码调试功能,并且部分语法(如条件判读、循环)与python原始语法差别较大,具体可参考如下示例代码:
#1-动态图:语法与python一致 import tensorflow as tf import tensorflow.contrib.eager as tfe tfe.enable_eager_execution() first_counter = tf.constant(0) second_counter = tf.constant(10) while first_counter<second_counter: first_counter = tf.add(first_counter, 2) second_counter = tf.add(second_counter, 1) print(first_counter,first_counter) print(second_counter,second_counter)
#2-静态图:需要学习新的写法(循环使用tf.while_loop) 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.Session() as sess: counter_1_res, counter_2_res = sess.run([c1, c2]) print(counter_1_res) print(counter_2_res)
因此推荐学习tf2.x 由于TensorFlow的库依赖较多,对于初学者来说,安装环境也是比较头疼的一件事。Tensorflow一键快速安装的方法可以参考我另一篇文章:
学习资源篇
如果想深入学习经典机器学习模型(如支持向量机、K-means),强烈推荐李宏毅老师的课程,做下课后练习效果更好; 如果想深入学习深度学习原理,推荐吴恩达老师的课程,入门还是很不错的。以上课程视频,在官网都可以找到,当然某站有搬运。 END
上一篇:
通过多线程提高代码的执行效率例子