Ubuntu20.04配置tensorflow
1、英伟达驱动安装
英伟达官网:
右上角"驱动程序",选择下载
删除原有驱动
sudo apt-get remove --purge nvidia*
禁用nouveau
sudo gedit /etc/modprobe.d/blacklist.conf
文件中添加
blacklist nouveau
blacklist lbm-nouveau
options nouveau modeset=0
alias nouveau off
alias lbm-nouveau off
关闭nouveau:
echo options nouveau modeset=0 | sudo tee -a /etc/modprobe.d/nouveau-kms.conf
重启电脑
update-initramfs -u
reboot
验证是否禁用成功,无输出即为成功
lsmod | grep nouveau
安装 NVIDIA 显卡驱动
安装g++依赖:不安装会出错
sudo apt install gcc g++ make
参考https://www.cnblogs.com/zhaoyingjie/p/15380694.html
-no-x-check:安装驱动时关闭X服务
-no-nouveau-check:安装驱动时禁用nouveau
-no-opengl-files:只安装驱动文件,不安装OpenGL文件
sudo service lightdm stop //这个是关闭图形界面,不执行会出错。
#给权限
sudo chmod a+x NVIDIA-Linux-x86_64-418.56.run
sudo ./NVIDIA-Linux-x86_64-396.18.run -no-x-check -no-nouveau-check -no-opengl-files
//只有禁用opengl这样安装才不会出现循环登陆的问题
sudo ./NVIDIA-Linux-x86_64-396.18.run
##其实直接sudu或者su->sh就行,可以选项里自己选
如果提示 unit lightdm.service not loaded
则先安装LightDm:
sudo apt install lightdm
安装完毕后跳出一个界面,选择lightdm,再
sudo service lightdm stop
安装过程中的选项
The distibution......----->Continue
EORRY:unable....'cc'....'gcc'----->OK
WARNING:unable to find .....32bit.....------>OK
AN incom....------>install and overwrite
would you like to run ....nvidia-xconfig......------>NO
Install of .....LInexx86_64.....----->OK
Installation has failed. Please see the file
'/var/log/nvidia-installer.log' for details. You may find
suggestions on fixing installation problems in the README available
on the Linux driver download page at www.nvidia.com.
打开图形界面
sudo service lightdm start //这个是打开图形界面
#验证
nvidia-smi
验证
nvidia-smi
2、cuda安装
直接sudo sh 安装包.run
点continue
输入accept
上图,前边装了驱动,第一个选项可以不装,剩下四个可选,tookit必须装
sudo sh cuda_10.2.89_440.33.01_linux.run
安装完会提示加入环境变量的一个path...一个lib...,复制下来贴到下边文件里
输入gedit ~/.bashrc命令打开文件,在文件结尾输入以下语句,保存
export PATH=/usr/local/cuda-10.2/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
更新环境变量
source ~/.bashrc
验证
nvcc -V 
3、cudnn安装
百度搜索'cudnn下载'
cuDNN Archive | NVIDIA Developer
英特尔芯片选x86_64,local install for linux
下载解压,
cuda/include/cudnn.h文件复制到usr/local/cuda/include文件夹,将cuda/lib64/下所有文件复制到/usr/local/cuda/lib64文件夹中,并添加读取权限
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
4、anaconda安装
下载包,一定在home下安装,否则会安装到root路径下
bash Anaconda3-5.3.1-Linux-x86_64.sh
加入环境变量
sudo gedit ~/.bashrc
添加
export PATH="/home/your_username/anaconda3/bin:$PATH"
重启环境变量
source ~/.bashrc
5、tensorflow和kera
conda查看源
conda config --show-sources
conda换源
# 中科大镜像源
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/
# 北京外国语大学源
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/main
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/r
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/pro
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/msys2
#清华源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
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/r
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
#pytorch镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
linux下的换源可直接修改文件
将以上配置文件写在 ~/.condarc
中
vim ~/.condarc
sudo gedit ~/.condarc
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- 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/pytorch/
ssl_verify: true
补充:
channels:
- https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
- https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
- https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
ssl_verify: true
channels:
- https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main/
- https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/free/
- https://mirrors.sjtug.sjtu.edu.cn/anaconda/cloud/conda-forge/
ssl_verify: true
创建conda虚拟环境
conda create --name tf python=3.8 -y
配置jupyter notebook公网访问
参考
设置 jupyter notebook 外网远程访问 - 泡沫° - 博客园
pip install jupyter notebook
生成notebook 配置文件
linux默认情况下,配置文件 ~/.jupyter/jupyter_notebook_config.py 并不存在,需要自行创建。使用下列命令生成配置文件
jupyter notebook --generate-config
#输出
Writing default config to: /home/ldq/.jupyter/jupyter_notebook_config.py
windows下的做法,运行cmd,然后输入下面的命令,会生成配置文件,windows的jupyter_notebook_config.py路径在C:\Users\Administrator.jupyter\
生成完这个配置文件后,我们接下来就是要生成一个密码,要不然谁都会可以访问这个jupyter notebook。
2. 生成密码
自动生成
从 jupyter notebook 5.0 版本开始,提供了一个命令来设置密码:jupyter notebook password,生成的密码存储在 jupyter_notebook_config.json。 在windows下是运行cmd,命令是一样的
$ jupyter notebook password
##
Enter password:
Verify password:
[NotebookPasswordApp] Wrote hashed password to /home/ldq/.jupyter/jupyter_notebook_config.json
然后我们打开 jupyter_notebook_config.json把里面的sha1或argon2的值 复制出来,红色内容即为要复制的内容,一这不要复制我这里的哦,如下所示:
{
"NotebookApp": {
"password": "argon2:$argon2id$v=19$m=10240,t=10,p=8$papAj0Mf23hTag3KcnbQNA$qvc/MzhuJUaq6JGOc6dN0w"
}
}
然后打开文件 jupyter_notebook_config.py,查找 c.NotebookApp.password,然后把前面的#号去掉,去掉#时要让命令顶格才能生效
再把刚刚复制sha1值粘贴进去。这样打开jupyter notebook就需要密码啦,这个密码就是你刚刚设置的密码
3.修改配置文件
在 jupyter_notebook_config.py 中找到下面的行,取消注释 就是把这几行代码最前面的#号去掉并修改。
c.NotebookApp.ip='*'       #在所有的网卡接口上开启服务
c.NotebookApp.port =8888 #可自行指定一个端口, 访问时使用该端口7777
c.NotebookApp.allow_remote_access = True  #允许远程
注:如果购买的是阿里云的服务器,或者腾讯云的服务器,一定要在控制台里面的安全组里添加相对应的端口,另外windows服务器的话,一定要在防火墙里也添加相对应的端口,否则会造成无法访问。
4.修改jupyter notebook默认工作路径
在jupyter_notebook_config.py文件中查找c.NotebookApp.notebook_dir,把前面的注释符号#号去掉,然后把后面的路径改成自己想设置成的路径,如下所示:
c.NotebookApp.notebook_dir = 'D:\\JupyterProject'
指定端口运行--port 1111
jupyter notebook --no-browser --port 6000 --ip=192.168.1.103
后台挂起
nohup jupyter notebook > ./jupyter.log 2>&1 & # 后台静默运行
打开 ./jupyter.log
,找到访问路径,将地址改成主机对应的 IP,即可正常访问。
想关掉后台运行的 Jupyter Notebook,可运行 ps -a
,知道对应的 PID,然后 kill -9 $PID
如果退出了 SSH 连接,下次连接想关闭后台的 Jupyter Notebook,运行 ps -ef | grep jupyter-notebook
,然后 kill -9 $PID
nohup jupyter notebook --port 9999 > ./jupyter.log 2>&1 &
nohup jupyter notebook --no-browser --port 9999 > ./jupyter.log 2>&1 &
#查看进程号
ps -ef | grep jupyter-notebook
#关进程
kill -9 $PID
Original: https://blog.csdn.net/weixin_50508551/article/details/122535880
Author: 夜以近乎昼
Title: Ubuntu20.04 配置tensorflow环境
相关阅读
Title: 【AMD显卡在WIndow10/11部署带GPU支持的深度学习环境(Pytorch-DirectML篇)】
全中文网首发,可以收藏本教程纪念一下
这篇是之前Tensorflow-DirectML的姊妹篇,如果对TF感兴趣的可以搜索我的博文。
首先我们丢一个官网地址,但是官网那个会误导新人如果你按那个操作的话。
https://docs.microsoft.com/en-us/windows/ai/directml/gpu-pytorch-windows
本篇博文,我们将安装PyTorch 1.8.0
PS:MS对FB还是蛮厚道的,编译了比较新的版本,不像TF,给了个1.X,太坏了,至少给个2.X吧
这里用我的方法操作:
首先声明一下,因为我的显卡是AMD RX6600,不带XT(性能和RTX3060 12G版本半斤八两),所以目前没有最新的WSL2驱动,如果有了的话欢迎告知我,所以我这里就用Windows本地演示。
第一步:安装AMD DirectML支持的SDK,相当于A卡的Cuda
可以参考:
https://blog.csdn.net/weixin_44029053/article/details/121581870
这篇博文Flag部分安装驱动的描述。
第二步:安装Pytorch-DirectML
1:下载安装Conda完整版或MiniConda,这边如果硬盘够大我推荐Conda完整版,地址:
https://www.anaconda.com/products/individual
下载Anaconda Individual Edition版,这个是个人免费版
Python版本随意,反正我们后面单独创建Python环境
2:在Conda下创建Python虚拟环境:
我这创建了两个,一个给Pytorch用,一个给Tensoflow用,因为我本来用Miniconda的,官方的
Pytorch-DirectML教程把我之前的Tensoflow-DirectML环境整没了,很难还原回去,所以。。
废话不多说,我们管理员权限打开PowerShell,输入
conda create -n your_env_name python=x.x
创建Python虚拟环境,your_env_name 是环境名字,x.x是python版本,这里我的是
conda create -n torch_dml python=3.8
必须用python38.
以下都用我的名字torch_dml 演示
然后激活torch_dml :
conda activate torch_dml
3:在激活的条件下,安装我们的Pytorch依赖库:
conda install pandas
conda install tensorboard
conda install matplotlib
conda install tqdm
conda install pyyaml
pip install opencv-python
pip install wget
pip install torchvision==0.9.0
PS:上面代码在安装torchvision的时候,会自动下载一个Pytorch CPU版本,这个没关系,他只是一个上层,我们在后面底层Tensor变量可以转化为GPU版本
上面说的"安装torchvision的时候,会自动下载一个Pytorch CPU版本",并且后面要安装的Pytorch-DirectML也会覆盖CPU版本的代码,这样的确代码是可以运行的,但是这样其实并不严谨,实际上我看了官方那个,CPU版本其实并不需要,强迫症的可以执行:
pip uninstall torch
上面安装代码你可以一行一行复制到PowerShell安装,防止某个安装失败
PS:博主是在开飞机的条件下安装的,如果没有飞机,那么你可以切换国内源
参考:
https://www.cnblogs.com/weiyunpeng/p/11893222.html
4:安装Pytorch-DirectML
依然在我们的Conda虚拟环境torch_dml下安装:
pip install pytorch-directml
5:安装VSCODE设置环境运行代码
输入代码运行:
import torch
tensor1 = torch.tensor([1]).to("dml")
tensor2 = torch.tensor([2]).to("dml")
dml_algebra = tensor1 + tensor2
print("result:",dml_algebra.item())
如果你出现我上面的"result: 3"这个结果,那么恭喜你,大获成功!
更多demo可以访问官方GitHub:
https://github.com/microsoft/DirectML/tree/master/PyTorch
还有没有好玩的?有哦,下一步,我们将用Pytorch-DirectML或Tensorflow-DirectML导出onnx,用我们的ONNX-DirectML版或者Vulkan在AMD GPU(Windows本地平台)进行GPU加速推理,敬请期待!
Original: https://blog.csdn.net/weixin_44029053/article/details/121591408
Author: superowner001
Title: 【AMD显卡在WIndow10/11部署带GPU支持的深度学习环境(Pytorch-DirectML篇)】