Anaconda安装
Anaconda是机器学习中常用的工具,使用它可以为你配置若干多个独立的虚拟环境来运行相应的程序,满足每个程序运行的不同需求。
##下载Anaconda
1.方法一:进入官网下载(https://www.anaconda.com/download)
直接点击下载即可,但是由于是国外网址,可能会下载速度很慢,推荐使用方法二
方法二:使用清华镜像站下载(https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/?C=M&O=D),下载速度要快很多。
使用conda创建虚拟环境
虚拟环境的创建
输入此条指令创建conda虚拟环境,可以自己更改名称和python版本
conda create -n 自己输入名称 python=版本号
如下所示,我示例创建名为TF2.1的环境
conda create -n TF2.1 python=3.7
删除环境
conda remove -n TF2.1 --all
输入y点击回车。
虚拟环境默认路径是C:/user/xxx/.conda/envs中,用户名是中文时后续步骤会出现问题。解决方法见https://zhuanlan.zhihu.com/p/687301312
激活虚拟环境
conda activate TF2.1
tensorflow安装
gpu版本安装
★注意:原生 Windows 上的 GPU 支持仅适用于 2.10 或更早版本,从 TF 2.11 开始,Windows 不支持 CUDA 构建。要在 Windows 上使用 TensorFlow GPU,您需要在 WSL2 中构建/安装 TensorFlow,或者使用 tensorflow-cpu 配合 TensorFlow-DirectML-Plugin。
参考CSDN博客地址:https://blog.csdn.net/weixin_46713695/article/details/135038527
1、安装cudatoolkit
conda install cudatoolkit=11.3.1
2、安装cudnn
conda install cudnn=8.2.1
3、安装tensorflow
pip install tensorflow-gpu==2.10
如果安装失败,尝试以下命令
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow
4、检查环境是否装好
进入python
python
然后输入
import tnsorflow as tf
tf.__version__
显示版本号即为安装成功。
PyCharm安装
PyCharm官网下载社区版
下载网址https://www.jetbrains.com/pycharm/download/other.html
最新版软件无法识别conda环境解决办法:
参考 (https://blog.csdn.net/2401_84495872/article/details/139919853#:~:text=%E5%9B%A0%E4%B8%BA%E6%98%AFConda%20e)
新建工程测试,工程配置见上面的链接
import tensorflow as tf
tensorflow_version = tf.__version__
gpu_avilable = tf.test.is_gpu_available()
print("tensorflow version: ", tensorflow_version,"\tGPU aviable:", gpu_avilable)
a = tf.constant([1.0,2.0], name = 'a')
b = tf.constant([1.0,2.0], name = 'b')
result = tf.add(a,b,name='add')
print(result)
结果输出;
tensorflow version: 2.17.0 GPU aviable: False
tf.Tensor([2. 4.], shape=(2,), dtype=float32)
安装成功。