conda安装tensorflow

Posted 海里的鱼2022

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了conda安装tensorflow相关的知识,希望对你有一定的参考价值。

conda安装会自动检查依赖库和版本冲突,在tensorflow安装中尝试一下。

1. 首先,安装conda工具,anconda太重,可以安装miniconda

链接 Installing on Linux — conda 4.12.0.post6+e7012690 documentation

下载最新的 .sh文件,就可以运行安装了, 注意64位。

2. 安装完成后,默认是python3.8,所以可能需要管理多个虚拟环境

常见命令

2.conda常用的命令

1)查看安装了哪些包

conda list

2)查看当前存在哪些虚拟环境

conda env list 
conda info -e

3)检查更新当前conda

conda update conda

3.Python创建虚拟环境

conda create -n your_env_name python=x.x

anaconda命令创建python版本为x.x,名字为your_env_name的虚拟环境。your_env_name文件可以在Anaconda安装目录envs文件下找到

4.激活或者切换虚拟环境

打开命令行,输入python --version检查当前 python 版本。

Linux:  source activate your_env_nam
Windows: activate your_env_name

5.对虚拟环境中安装额外的包

conda install -n your_env_name [package]

6.关闭虚拟环境(即从当前环境退出返回使用PATH环境中的默认python版本)

deactivate env_name
或者`activate root`切回root环境
Linux下:source deactivate 

7.删除虚拟环境

conda remove -n your_env_name --all

8.删除环境钟的某个包

conda remove --name $your_env_name  $package_name 

8、设置国内镜像

http://Anaconda.org的服务器在国外,安装多个packages时,conda下载的速度经常很慢。清华TUNA镜像源有Anaconda仓库的镜像,将其加入conda的配置即可:

# 添加Anaconda的TUNA镜像

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

# TUNA的help中镜像地址加有引号,需要去掉

# 设置搜索时显示通道地址

conda config --set show_channel_urls yes

9、恢复默认镜像

conda config --remove-key channels

安装tensorflow

验证GPU正确安装

>>> import tensorflow
>>> import tensorflow as tf
>>> tf.__version__
'2.2.0'
>>> print(tf.test.is_gpu_available())
WARNING:tensorflow:From <stdin>:1: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
2020-12-06 10:43:26.108332: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2020-12-06 10:43:26.135734: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 2599990000 Hz
2020-12-06 10:43:26.136128: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x563b0662e970 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-12-06 10:43:26.136145: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-12-06 10:43:26.143076: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2020-12-06 10:43:26.158243: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-12-06 10:43:26.158536: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1660 Ti computeCapability: 7.5
coreClock: 1.59GHz coreCount: 24 deviceMemorySize: 5.80GiB deviceMemoryBandwidth: 268.26GiB/s
2020-12-06 10:43:26.158685: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-12-06 10:43:26.159938: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
2020-12-06 10:43:26.161054: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10
2020-12-06 10:43:26.161250: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10
2020-12-06 10:43:26.162503: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10
2020-12-06 10:43:26.163365: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10
2020-12-06 10:43:26.166072: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2020-12-06 10:43:26.166153: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-12-06 10:43:26.166463: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-12-06 10:43:26.166713: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2020-12-06 10:43:26.166740: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-12-06 10:43:26.230760: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-12-06 10:43:26.230791: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]      0
2020-12-06 10:43:26.230799: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0:   N
2020-12-06 10:43:26.230963: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-12-06 10:43:26.231265: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-12-06 10:43:26.231517: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-12-06 10:43:26.231817: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/device:GPU:0 with 5058 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1660 Ti, pci bus id: 0000:01:00.0, compute capability: 7.5)
2020-12-06 10:43:26.233295: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x563b07f1e220 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2020-12-06 10:43:26.233307: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): GeForce GTX 1660 Ti, Compute Capability 7.5
True
>>> tf.config.list_physical_devices('GPU')
2020-12-06 10:44:00.341738: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-12-06 10:44:00.342919: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1660 Ti computeCapability: 7.5
coreClock: 1.59GHz coreCount: 24 deviceMemorySize: 5.80GiB deviceMemoryBandwidth: 268.26GiB/s
2020-12-06 10:44:00.343016: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-12-06 10:44:00.343065: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
2020-12-06 10:44:00.343109: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10
2020-12-06 10:44:00.343151: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10
2020-12-06 10:44:00.343193: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10
2020-12-06 10:44:00.343235: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10
2020-12-06 10:44:00.343279: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2020-12-06 10:44:00.343442: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-12-06 10:44:00.344645: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-12-06 10:44:00.345707: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
>>>


注意,其实很多文档提到的测试方法其实已经过时了

WARNING:tensorflow:From <stdin>:1: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.

以上是关于conda安装tensorflow的主要内容,如果未能解决你的问题,请参考以下文章

conda 安装 tensorflow

观点 | 别再使用pip安装TensorFlow了!用conda吧~

为啥在 conda 安装后 Tensorflow 无法识别我的 GPU?

国内源升级tensorflow2和tensorflow probability

为啥 conda 无法在 Windows 上正确安装 tensorflow gpu?

使用conda安装tensorflow-gpu