1.在anaconda中创建tensorflow环境
在当前命令窗口中加载anaconda的环境
# export PATH="/home/qy/software/anaconda3/bin:$PATH"
export PATH="/path/to/anaconda3/bin:$PATH"
创建名为tensorflow并且python版本为3.6的环境
conda create -n tensorflow python=3.6
输出的信息:
## Package Plan ##
environment location: /home/qy/software/anaconda3/envs/tensorflow
added / updated specs:
- python=3.6
# To activate this environment, use:
# > source activate tensorflow
#
# To deactivate an active environment, use:
# > source deactivate
2.安装tensorflow
2.1 CPU版本
source activate tensorflow #激活tensorflow环境
pip install tensorflow #使用Python自带的pip安装tensorflow
或者:
pip install --ignore-installed --upgrade tfBinaryURL
tfBinaryURL可在清华大学开源软件网站查得,选择cpu/gpu、安装系统、python版本及tensorflow版本后会自动生成。如选择cpu、Linux系统、cp36版本的python、1.4.0的Tensorflow后生成:https://mirrors.tuna.tsinghua.edu.cn/tensorflow/linux/cpu/tensorflow-1.4.0-cp36-cp36m-linux_x86_64.whl
如果安装过程中出错,重新运行该行命令即可。
2.2 在清华大学开源软件网站安装gpu版本:
https://mirror.tuna.tsinghua.edu.cn/help/tensorflow/
但是上图显示不完整,需要进入路径:https://mirrors.tuna.tsinghua.edu.cn/tensorflow/linux/gpu/ 选择合适的GPU的安装包:
tensorflow_gpu-1.5.0rc1-cp36-cp36m-linux_x86_64.whl> 188.4 MiB 2018-01-13 14:07
所以完整的命令是:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ \
https://mirrors.tuna.tsinghua.edu.cn/tensorflow/linux/gpu/tensorflow_gpu-1.5.0rc1-cp36-cp36m-linux_x86_64.whl
注:-i https://pypi.tuna.tsinghua.edu.cn/simple/
表示使用清华的pip源,下载速度显著提升。
3.验证安装成功
source activate tensorflow #首先激活tensorflow环境
python #启动python环境
# 验证TensorFlow是否安装成功
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print sess.run(hello)
Hello, TensorFlow! # 恭喜!安装成功!
4. ananconda 安装nb_conda,解决jupyter notebook中环境的选择问题
https://my.oschina.net/flyrobin/blog/1546363
进入到conda的目录:执行下面给的命令:
qy@BMEserver [08:42:05 AM] [~/anaconda3/bin] -> % ./conda install nb_conda #好像conda和pip是差不多的。
~/anaconda3/bin
:anaconda 环境中conda的位置
安装nb_conda并重新启动notebook,在kernel -> change kernel中即可切换到指定的虚拟环境,很方便。
5. Ubuntu 中anaconda为jupyter添加kernel
安装tensorflow的时候利用anaconda 创建了环境tensorflow(python=3.6),但是启动环境:
source activate tensorflow
jupyter notebook #anaconda 自带的
执行了上述命令后,jupyter启动的内核没有找到tensorflow
现在的问题是,如何往jupyter中添加之前创建的虚拟环境(tensorflow)
# 首先进入之前创建好的虚拟环境
source activate tensorflow
# 为该环境安装 ipython的内核
python3.5 -m pip install ipykernel
# 安装完成后在jupyter中添加新的核