Mac和Ubuntu下修改pip源和TensorFlow(CPU
2019-03-14 本文已影响0人
sean_depp
Ubuntu18.04LTS下cuda10.0+cudnn7.5+TensorFlow1.13环境搭建
目录
- 前言
- macOS
- Ubuntu 18.04 LTS
- 最后
前言
这篇是对上一篇的补充, 主要是macOS和Ubuntu下的cpu版的TensorFlow安装, 整体都比较简单.
macOS
在用pip3下载之前, 我建议先修改下Mac的pip镜像源, 否则下载很糟心.
cd ~/
mkdir .pip
cd .pip
touch pip.conf
vim pip.conf
输入如下内容:
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
可选源还有很多, 但是阿里的足够了.
接着安装下virtualenv
sudo pip3 install --upgrade virtualenv
virtualenv --system-site-packages ~/tensorflow
cd ~/tensorflow
然后依据自己的bash版本输入如下指令的一条, 比如我就是zsh, 输入第一个指令, #号及之后部分都是注释, 不要输入:
source ~/tensorflow/bin/activate # If using bash, sh, ksh, or zsh
source ~/tensorflow/bin/activate.csh # If using csh or tcsh
然后py3.x用第二条, py2.7用第一条:
pip install --upgrade tensorflow # for Python 2.7
pip3 install --upgrade tensorflow # for Python 3.x
之后想要退出当前环境, 就输入如下指令:
deactivate
大体效果如下图:

接下来是一个测试小栗子:

#!/usr/local/bin/python3
import tensorflow as tf
hello = tf.constant('Hello, tf!')
sess = tf.Session()
print (sess.run(hello))
a = tf.constant(10)
b = tf.constant(32)
print (sess.run(a+b))
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
result = (sess.run(product))
print (result)
sess.close()
可能大家会疑惑一开始的那一段, 可以加入如下代码:
# Just disables the warning, doesn't enable AVX/FMA
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
这样就没有了, 具体原因, 自行google咯:

Ubuntu 18.04 LTS
整体操作逻辑差不多, 先改下镜像源:
apt-get install python-pip python-dev build-essential
pip install --upgrade pip
pip3 install --upgrade virtualenv
然后创建pip.conf文件:
mkdir ~/.pip
vim ~/.pip/pip.conf
添加源:
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
最后安装TensorFlow:
pip3 install tensorflow
复制之前的测试案例测试下:

最后
关于GPU版本的, 可以参看Ubuntu18.04LTS下cuda10.0+cudnn7.5+TensorFlow1.13环境搭建. 喜欢记得点赞哦, 有意见或者建议评论区见~