|
1 | | -TensorFlow支持Ubuntu、Mac OS X、Windows,也支持从源码安装。本文以Ubuntu操作系统为例,来讲解TensorFlow的环境搭建过程。 |
| 1 | +TensorFlow支持Ubuntu、Mac OS X、Windows,也支持从源码安装。本文以**Ubuntu**操作系统为例,来讲解TensorFlow的环境搭建过程。 |
2 | 2 |
|
3 | | -TensorFlow有 |
| 3 | +### TensorFlow分为两个版本: |
| 4 | + |
| 5 | +* CPU支持版本 |
| 6 | + |
| 7 | + 如果系统没有安装GPU,那么只能使用TensorFlow的CPU支持版本。 |
| 8 | + |
| 9 | +* GPU支持版本 |
| 10 | + |
| 11 | + 如果系统安装了GPU,并且添加了其他相关支持(安装CUDA Toolkit、cuDNN、libcupti-dev库等),则可以使用TensorFlow的GPU支持版本。 |
| 12 | + |
| 13 | +### TensorFlow可以从以下方式安装: |
| 14 | + |
| 15 | +* virtualenv安装 |
| 16 | + |
| 17 | +* 原生的pip安装 |
| 18 | + |
| 19 | +* Docker安装 |
| 20 | + |
| 21 | +* Anaconda安装 |
| 22 | + |
| 23 | +* 从源码安装 |
| 24 | + |
| 25 | +### 在Ubuntu上安装TensorFlow |
| 26 | + |
| 27 | +首先,查看系统版本: |
| 28 | + |
| 29 | + $ cat /etc/lsb-release |
| 30 | + DISTRIB_ID=Ubuntu |
| 31 | + DISTRIB_RELEASE=14.04 |
| 32 | + DISTRIB_CODENAME=trusty |
| 33 | + DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS" |
| 34 | + |
| 35 | +由于没有安装GPU支持,因此只能在Ubuntu系统上安装TensorFlow的CPU支持版本。 |
| 36 | + |
| 37 | +其次,查看python和pip: |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +这里,采用Anaconda中的pip来安装,对应python2版本: |
| 42 | + |
| 43 | + pip install tensorflow |
| 44 | + |
| 45 | +安装完毕,我们可以使用python交互式终端运行下面代码: |
| 46 | + |
| 47 | + >>> import tensorflow as tf |
| 48 | + >>> sess = tf.Session() |
| 49 | + >>> hello = tf.constant('hello world!') |
| 50 | + >>> print(sess.run(hello)) |
| 51 | + hello world! |
| 52 | + |
| 53 | +### Python2与Python3的版本切换 |
| 54 | + |
| 55 | +我们来测试TensorFlow官方的一段代码: |
| 56 | + |
| 57 | + import numpy as np |
| 58 | + import tensorflow as tf |
| 59 | + |
| 60 | + ''' |
| 61 | + tf.estimator |
| 62 | + ''' |
| 63 | + |
| 64 | + x_train = np.array([1., 2., 3., 4.]) |
| 65 | + y_train = np.array([0., -1., -2., -3.]) |
| 66 | + x_eval = np.array([2., 5., 8., 1.]) |
| 67 | + y_eval = np.array([-1.01, -4.1, -7, 0.]) |
| 68 | + input_fn = tf.estimator.inputs.numpy_input_fn( |
| 69 | + {"x": x_train}, y_train, batch_size=4, num_epochs=None, shuffle=True) |
| 70 | + train_input_fn = tf.estimator.inputs.numpy_input_fn( |
| 71 | + {"x": x_train}, y_train, batch_size=4, num_epochs=1000, shuffle=False) |
| 72 | + eval_input_fn = tf.estimator.inputs.numpy_input_fn( |
| 73 | + {"x": x_eval}, y_eval, batch_size=4, num_epochs=1000, shuffle=False) |
| 74 | + |
| 75 | + feature_columns = [tf.feature_column.numeric_column("x", shape=[1])] |
| 76 | + estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns) |
| 77 | + |
| 78 | + estimator.train(input_fn=input_fn, steps=1000) |
| 79 | + |
| 80 | + train_metrics = estimator.evaluate(input_fn=train_input_fn) |
| 81 | + eval_metrics = estimator.evaluate(input_fn=eval_input_fn) |
| 82 | + print("train metrics: %r"% train_metrics) |
| 83 | + print("eval metrics: %r"% eval_metrics) |
| 84 | + |
| 85 | +将测试代码写进Test.py脚本,在python2环境下,运行结果如下: |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +##### 程序会报错,指出来tf.estimator模块下不存在LinearRegressor属性。 |
| 90 | + |
| 91 | +为什么会出现这个问题呢?我想可能是Python2下的tensorflow库中一些上层接口功能没有跟进,而且考虑到Python2版本到2020年会停止支持,因此Google官方可能对Python3下的tensorflow库支持会更全面一些。既然这样,从Python2环境切换到Python3环境,是一个避免这些问题的办法。 |
| 92 | + |
| 93 | +与Virualenv类似,Anaconda支持搭建Python的虚拟环境,也就是说,无论Anaconda是基于Python2版本还是Python3版本,我们都可以搭建Python的双版本共存环境,根据项目需要,随时可以切换版本。 |
| 94 | + |
| 95 | +##### 搭建Python3环境: |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +##### 激活python3环境,并运行Test.py: |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +运行成功! |
0 commit comments