TensorFlow–环境安装

1.打开Anaconda Prompt

输入

conda create -n TF2.1 python=3.7

选择Y

电脑如果有英伟达GPU,则输入如下命令,选择Y,同意安装相关软件包

conda activate TF2.1
conda install cudatoolkit=10.1

 安装完之后接着输入

conda activate TF2.1
conda install cudnn=7.6

安装tensorflow

conda activate TF=2.1
pip install tensorflow==2.1

若报错

ValueError: check_hostname requires server_hostname

解决:

pip install -i 镜像网址   模块名
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow==2.1

镜像源网址:
1)http://mirrors.aliyun.com/pypi/simple/ 阿里云
2)https://pypi.mirrors.ustc.edu.cn/simple/ 中国科技大学
3) http://pypi.douban.com/simple/ 豆瓣
4) https://pypi.tuna.tsinghua.edu.cn/simple/ 清华大学
5) http://pypi.mirrors.ustc.edu.cn/simple/ 中国科学技术大学

基于以上,输入以下命令行进行安装

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow==2.1

(换源下载后 如果仍然报错则可能是网络代理服务器打开了的问题,
在设置中关闭网络代理服务器即可成功下载安装扩展包。)

 报错

TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
 1. Downgrade the protobuf package to 3.20.x or lower.
 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates

可以用镜像加快下载速度

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple protobuf==3.19.0

进入验证

conda activate TF=2.1
(TF2.1) C:\Users\WJ>python
Python 3.7.13 (default, Mar 28 2022, 08:03:21) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2022-07-03 21:15:52.040725: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
>>> tf._version_
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute '_version_'
>>> tf.__version__
'2.1.0'
>>>

 安装成功

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
安装TensorFlow环境可以通过以下几种方式进行验证: 1. 在命令行中使用Python解释器进行验证。首先,在命令行中输入"python",进入Python环境。然后,输入"import tensorflow as tf"来导入TensorFlow模块。接下来,使用以下代码创建一个常量并打印它的值: ``` hello = tf.constant('Hello TensorFlow') sess = tf.Session() print(sess.run(hello)) ``` 如果输出结果为"b'Hello TensorFlow'",则表示安装成功。 2. 使用Anaconda安装并验证TensorFlow环境。首先,在Anaconda的应用程序中找到并启动Spyder。在Spyder中,输入上述代码并点击"Run"运行。如果在控制台中出现输出结果"b'Hello TensorFlow'",则表示安装成功。 3. 使用PyCharm安装并验证TensorFlow环境。在PyCharm中输入以下代码: ``` import tensorflow as tf hello = tf.constant('Hello TensorFlow') sess = tf.Session() print(sess.run(hello)) ``` 运行程序后,如果输出结果为"b'Hello TensorFlow'",则表示安装成功。 另外,你还可以使用conda命令来验证安装过程: 1. 使用"conda --version"命令检查Anaconda是否成功安装。 2. 使用"conda info --envs"命令检查当前安装环境。 3. 使用"conda search --full-name python"命令检查可用的Python版本。 4. 使用"conda create --name tensorflow python=3.7"命令安装不同版本的Python。 5. 激活名为tensorflow环境,使用"activate tensorflow"命令。 6. 使用"conda info --envs"命令确保tensorflow环境已经成功添加。 7. 使用"python --version"命令检查新环境中的Python版本。 8. 最后,使用"deactivate"命令退出当前环境。 综上所述,可以通过命令行验证、Anaconda或PyCharm安装和验证TensorFlow环境安装
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值