搭建深度学习开发环境,建议还是使用anaconda比较容易些,本人试图直接在python3.7下安装tensorflow和keras失败了,后来还是用anaconda搭建成功了。
介绍下过程:
安装Anaconda
1.下载
下载地址为:https://www.anaconda.com/download/#linux
2.安装anaconda,执行命令:
bash ~/下载/Anaconda3-2019.07-Linux-x86_64.sh
3.在安装过程中会显示配置路径
Prefix=/home/he/anaconda3/
4.安装完之后,运行python,仍是系统自带的python信息,需自己设置下环境变量
5.在终端输入$sudo vim /etc/profile,打开profile文件
6.在文件末尾添加一行:export PATH=/home/he/anaconda3/bin:$PATH,其中,将“/home/he/anaconda2/bin”替换为实际的安装路径,保存。
7.重启Linux
8.打开终端,输入python,出现如下界面,表明设置成功。
还可以用conda info 来查询安装信息
输入conda list 可以查询你现在安装了哪些库,常用的python, numpy, scipy名列其中。
如果你还有什么包没有安装上,可以运行conda install *** 来进行安装(***代表包名称),如果某个包版本不是最新的,运行conda update *** 就可以。
设置conda镜像源
因为默认的conda安装源在国外,安装时会比较慢,建议改成国内镜像源,这样安装其他包时会比较快。
终端中运行命令:
(1)清华源(TUNA)
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --setshow_channel_urls yes
(2)中科大源(USTC)
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --setshow_channel_urls yes
安装keras
先创建虚拟环境并安装tensorflow
1.打开终端
2.创建一个叫做keras的conda环境并激活
conda create -n keras python=3.6(我们使用Python3.6会更稳定些)
conda activate keras
3.安装tensorflow,输入以下命令:
conda install tensorflow
4.至此,我们已经安装好了TensorFlow。接下来可以测试验证下是否可以使用。在命令行输入:Python,进入Python编程环境。 然后输入:
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
正确的话,会输出: Hello, TensorFlow!
在虚拟环境中安装TensorFlow、Keras
1.首先,进入keras环境,安装ipython和jupyter。命令如下:
conda install ipython
conda install jupyter
2.然后,进去Jupyter,直接输入:
jupyter notebook (等待一下就会弹出浏览器,进入Jupyter)
3.安装scikit-learn,执行命令:
conda install scikit-learn
4.安装Keras,执行命令:
conda install keras
至此,深度学习,机器学习开发环境就已经安装完毕了,可以通过命令
jupyter notebook
打开notebook进行开发,输入以下代码,如果没有报错,就证明环境安装成功了。
import keras
注意:改完anaconda安装源,安装其他包时报错
我的报错信息:
CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/main/noarch/current_repodata.json>
解决方案:
1、创建channels配置文件的备份
cp ~/.condarc{,.bak}
查看配置文件的内容
cat ~/.condarc.bak
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
- https://nanomirrors.tuna.tsinghua.edu.cn/anaconda/cloud
- defaults
- https://nanomirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
- https://nanomirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/conda
- bioconda
- r
- conda-forge
show_channel_urls: true
2、删除部分内容
修改后配置文件的内容如下:
vim ~/.condarc
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
show_channel_urls: true
3、还不行就把配置文件中https改为http