TensorFlow2安装教程

1.安装Anaconda3

清华镜像源:

Index of /anaconda/archive/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror

选择自己系统对应的anaconda版本,这里我选择的是2022.5发行anaconda3-windows-x86_64

TensorFlow2安装教程

在随后的安装程序中,一直点next即可

TensorFlow2安装教程

建议将anaconda3加入环境变量当中

TensorFlow2安装教程

2.创建TensorFlow2虚拟环境

在安装TensorFlow2之前,建议先创建一个TensorFlow的虚拟环境。

方法一:在anaconda3 navigator当中创建

TensorFlow2安装教程

来到environment页面,选择create,自行命名虚拟环境的名称,选择并记住选择的Python版本,随后点击Create,随后即可查看到刚才创建的环境

TensorFlow2安装教程

TensorFlow2安装教程

输入下行指令

conda create -n env_name python==3.9.12

TensorFlow2安装教程

其中注意到的问题是,当在命令行使用conda指令时,总出现”系统找不到制定的路径”的提示时,解决方法是在使用conda指令之前,先进入已创建虚拟环境当中。命令行输入以下代码可查看已创建的虚拟环境。

conda env list

可以看到我已经创建了两个环境

TensorFlow2安装教程

通过以下代码,可以激活对应的环境

activate python39

TensorFlow2安装教程

此时再运行conda指令就不会再提醒找不到指定路径了。同时可用以下代码退出环境。

conda deactivate

此外,进入已创建的虚拟环境,还可以通过anaconda prompt程序直接进入base环境

TensorFlow2安装教程

3.安装TensorFlow2

安装TensorFlow2之前,首先手动添加安装所需的清华镜像源。

以txt形式打开”C:\Users(你的用户名).condarc”文件,将内部文本替换为以下文本。

channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

尝试了很多.condarc的更改内容,只有以上的内容才能顺利安装TensorFlow2。其中出现过以下的报错:

ERROR conda.core.link:_execute(730): An error occurred while installing package ‘https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge::cudatoolkit-11.3.1-h280eb24_10’.
Rolling back transaction: done

LinkError: post-link script failed for package https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge::cudatoolkit-11.3.1-h280eb24_10
location of failed script: D:\Python\anaconda3\envs\env_name\Scripts.cudatoolkit-post-link.bat
==> script messages

TensorFlow2安装教程

tensorflow2分为cpu和gpu版本,具体差别参考TensorFlow的cpu与gpu的区别

然后进入之前创建的虚拟环境当中,根据你需要安装的版本输入以下代码。

注意,这里要比对当前虚拟环境的python版本和TensorFlow2版本,版本比对表链接:TensorFlow历代版本对应Python版本

之前环境配置的python版本是3.9.12,所以这里需要下载TensorFlow-gpu 2.6.0 版本,命令如下:

conda install tensorflow-gpu==2.6.0

运行之后会打印出将要安装的一系列依赖性文件,然后输入y

TensorFlow2安装教程

随后等待安装完成即可。

验证TensorFlow2的安装可在命令行输入python,随后import tensorflow 验证是否安装成功,安装失败会有以下提示:

TensorFlow2安装教程

可以反复尝试安装tensorflow2的指令直到打印successfully的提示。

Original: https://blog.csdn.net/weixin_64080879/article/details/125743906
Author: 好帆果子
Title: TensorFlow2安装教程

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/718793/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球