CentOS 7 下使用虚拟环境Virtualenv安装Tensorflow cpu版记录

前端之家收集整理的这篇文章主要介绍了CentOS 7 下使用虚拟环境Virtualenv安装Tensorflow cpu版记录前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.首先安装pip-install

在使用centos7的软件包管理程序yum安装Python-pip的时候会报一下错误

No package python-pip available.
Error: Nothing to do
说没有python-pip软件包可以安装。

这是因为像centos这类衍生出来的发行版,他们的源有时候内容更新的比较滞后,或者说有时候一些扩展的源根本就没有。所以在使用yum来search python-pip的时候,会说没有找到该软件包。
因此为了能够安装这些包,需要先安装扩展源EPEL。EPEL(http://fedoraproject.org/wiki/EPEL) 是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS、ScientificLinux等提供高质量软件包的项目。
首先安装epel扩展源:

  1. sudo yum -y install epel-release

然后安装python-pip:

  1. sudo yum -y install python-pip

安装完之后别忘了清除一下cache:

  1. sudo yum clean all

搞定!

2.在隔离容器中安装TensorFlow

推荐使用virtualenv 创建一个隔离的容器,来安装 TensorFlow. 这是可选的,但是这样做能使排查安装问
题变得更容易,照着敲命令就行了

安装主要分成下面四个步骤:
● Install pip and Virtualenv.(这一步装过了)
● Create a Virtualenv environment.
● Activate the Virtualenv environment and install TensorFlow in it.
● After the install you will activate the Virtualenv environment each time you want to use TensorFlow.
Install pip and Virtualenv:
# Ubuntu/Linux 64-bit

  1. $ sudo apt-get install python-pip python-dev python-virtualenv

# Mac OS X

  1. $ sudo easy_install pip
  2. $ sudo pip install --upgrade virtualenv

Create a Virtualenv environment in the directory ~/tensorflow:

  1. $ virtualenv --system-site-packages ~/tensorflow

Activate the environment:

  1. $ source ~/tensorflow/bin/activate # If using bash
  2. $ source ~/tensorflow/bin/activate.csh # If using csh

(tensorflow)$ # Your prompt should change

Now,install TensorFlow just as you would for a regular Pip installation. First select the correct binary to install:
# Ubuntu/Linux 64-bit,cpu only,Python 2.7

  1. (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0rc0-cp27-none-linux_x86_64.whl

Finally install TensorFlow:
# Python 2

  1. (tensorflow)$ pip install --upgrade $TF_BINARY_URL

出现了如下错误

  1. InstallationError: Command python setup.py egg_info Failed with error code 1 in /root/tensorflow/build/mock

解决方案是:
Distribute has been merged into Setuptools as of version 0.7. If you are using a version <=0.6,upgrade using :

  1. pip install --upgrade setuptools

or

  1. easy_install -U setuptools.

其实就是安装的egg需要升级一下把,我猜测

升级之后重新 :

  1. 等待一段时间,(我似乎看到tensorflow在用gcc编译c++,c,时间还挺长大概十来分钟)
    看到
    Successfully installed tensorflow protobuf six wheel mock numpy funcsigs pbr
    Cleaning up
    ok

  2. 3.测试代码

  3. import tensorflow as tf

  4. import numpy as np

  5. # 使用 NumPy 生成假数据(phony data),总共 100 个点.

  6. x_data = np.float32(np.random.rand(2,100)) # 随机输入

  7. y_data = np.dot([0.100,0.200],x_data) + 0.300

  8. # 构造一个线性模型

  9. b = tf.Variable(tf.zeros([1]))

  10. W = tf.Variable(tf.random_uniform([1,2],-1.0,1.0))

  11. y = tf.matmul(W,x_data) + b

  12. # 最小化方差

  13. loss = tf.reduce_mean(tf.square(y - y_data))

  14. optimizer = tf.train.GradientDescentOptimizer(0.5)

  15. train = optimizer.minimize(loss)

  16. # 初始化变量

  17. init = tf.initialize_all_variables()

  18. # 启动图 (graph)

  19. sess = tf.Session()

  20. sess.run(init)

  21. # 拟合平面

  22. for step in xrange(0,201):

  23.         sess.run(train)

  24. if step % 20 == 0:

  25.         print step,sess.run(W),sess.run(b)


  26. 在命令行输入:

  27. source ~/tensorflow/bin/activate
  28. 激活tensorflow环境,运行上述代码

  29. (tensorflow)[root@www test]# python nihe.py
  30. # 得到最佳拟合结果

  31. W: [[0.100 0.200]],b: [0.300]
  32. 退出虚拟环境:

  33. (tensorflow)$ source deactivate

猜你在找的CentOS相关文章