[TensorFlow]Windows下安装并运行Hello World

参考网址:https://www.tensorflow.org/install/pip (或要VPN)

 

建议电脑是独显N卡机,安装前先升级驱动,减少不必要错误


1.下载Microsoft Visual C++ 2015 Redistributable Update 3(已安装更高级版或安装VS2015或VS2017可以跳过)

https://www.microsoft.com/zh-CN/download/details.aspx?id=53587

 

2.启用长路径。
https://superuser.com/a/1119980

 

3. 安装Python3.7.4,在安装界面上勾选添加环境变量复选框

https://www.python.org/downloads/

 

修改pip源(永久)

新建C:\Users\Administrator\pip\pip.ini,添加代码:

[global]
timeout=6000
index-url=https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host=pypi.tuna.tsinghua.edu.cn

 

批量更新包(可选)

参考:http://blog.sciencenet.cn/home.php?mod=space&uid=478347&do=blog&id=1115281

import pip
# pip V10.0.0以上版本需要导入下面的包
from pip._internal.utils.misc import get_installed_distributions
from subprocess import call

for dist in get_installed_distributions():
    call('python -m pip install --upgrade --user ' + dist.project_name, shell=True)

 PS:numpy不要装得太新(v1.17.0+),不然有很多警告

python -m pip install numpy==1.16.4

 

4.cuda 10.0和cudnn 7.6.1

https://developer.nvidia.com/cuda-downloads

https://developer.nvidia.com/rdp/cudnn-archive (注意对应cuda版本)

cudnn安装方法参考:https://jingyan.baidu.com/article/39810a236d660bb636fda6d4.html(简单来说就是解压cudnn,将文件粘贴到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0)

添加环境变量到PATH

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\include

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\lib\x64 

 

PS:我的电脑不支持10.1,当运行hello world时就会报错

ImportError: Could not find 'cudart64_100.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. Download and install CUDA 10.0 from this URL: https://developer.nvidia.com/cuda-90-download-archive

 

 

5. 安装tensorflow-gpu 1.14.0

pip install tensorflow-gpu==1.14.0

 

6.测试

import tensorflow as tf

h = tf.constant("Hello")
w = tf.constant(" World!")
hw = h + w

# 警告信息提示要将 tf.Session() 替换为 tf.compat.v1.Session()
with tf.compat.v1.Session() as sess:  
    ans = sess.run(hw)

print(ans)

 

屏蔽GPU输出信息可以添加上这句:

参考:https://blog.csdn.net/dcrmg/article/details/80029741

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
TF_CPP_MIN_LOG_LEVEL
0 :输出所有信息(默认值)
1 :屏蔽通知信息
2 :屏蔽通知信息和警告信息
3 :屏蔽通知信息、警告信息和报错信息

 

关于Anaconda安装

 只需要将上面的python安装替换为Anaconda(好像会自带python,没测试)

 1.安装Anaconda 2019.07(注意对应Python版本)

https://www.anaconda.com/distribution/

# 指定升级某一个库,例如 pandas
conda update pandas

# 升级所有可升级的库
conda update --all

 

2.安装OpenSSL v1.1.1c,不然Conda安装时可能会出下面的错误

http://slproweb.com/products/Win32OpenSSL.html

 

3.打开CMD,添加Conda源(可以不要https)

conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/

conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/

PS:可以直接打开C:\Users\Administrator\.condarc编辑:

channels:
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
show_channel_urls: true

 

7. 安装tensorflow-gpu 1.14.0

conda install tensorflow-gpu==1.14.0

 

 

亲测:

Win7 旗舰版(台式机+独显)

python-3.7.3-amd64.exe

Anaconda3-2019.07-Windows-x86_64.exe(PS:控制面板-程序和功能看到:Anaconda3 2019.07 (Python 3.7.3 64-bit))

Win64OpenSSL-1_1_1c.exe

cuda_10.1.168_425.25_windows.exe

cudnn-10.1-windows7-x64-v7.5.0.56.zip

TensorFlow-GPU 1.14.0

 

Win10专业版(台式机+独立N卡)

python-3.7.3-amd64.exe

Anaconda3-2019.07-Windows-x86_64.exe

cuda_10.0.130_411.31_win10

cudnn-10.0-windows10-x64-v7.6.0.64.zip

TensorFlow-GPU 1.13.1

同一台机

python-3.7.4-amd64.exe

cuda_10.0.130_411.31_win10

cudnn-10.0-windows10-x64-v7.6.1.34.zip

TensorFlow-GPU 1.14.0

 

集显笔记本多次尝试失败,甚至安装到virtualenv,tf-nightly都失败,只剩下重装了T_T

 

官方错误列表:https://www.tensorflow.org/install/errors

转载于:https://www.cnblogs.com/hcbin/p/11324712.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值