window下安装 Keras、TensorFlow(先安装CUDA、cuDNN,再安装Keras、TensorFlow)

日萌社

人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新)


安装

TensorFlow2、CUDA10、cuDNN7.6.5

Anaconda3 python 3.7、TensorFlow2、CUDA10、cuDNN7.6.5

TensorFlow 2.0 环境搭建

window下安装 Keras、TensorFlow(先安装CUDA、cuDNN,再安装Keras、TensorFlow)

Ubuntu 安装 CUDA、cuDNN、anaconda、pycharm、tensorflow、pytorch、JDK1.8


下载NVIDIA驱动:https://www.geforce.cn/drivers     

TensorFlow2.0需要cuda10,所以应该装410.48以上版本驱动 


CUDA、cuDNN百度盘下载

链接:https://pan.baidu.com/s/1oqYxOYob9MBuqHYsxkS-IA 
提取码:y8ub

链接:https://pan.baidu.com/s/1YqfX0ObJSSUIaHYW3OW3cQ 
提取码:21lu 
链接:https://pan.baidu.com/s/1yF7e6ntWpXpdPWFLN4gi2g 
提取码:f1zi

CUDA、tensorflow版本清单:https://tensorflow.google.cn/install/source#linux
CUDA下载:https://developer.nvidia.com/cuda-toolkit-archive

CUDA、cuDNN、tensorflow版本要求清单 

注意:使用cuDNN v7.4.2.24可能会报错(亲测),但使用cuDNN v7.6.5.32的话,并不会出现“cuDNN v7.4.2.24可能会出现的”报错,虽然官网推荐组合是cuDNN v7.4.2.24,但建议使用cuDNN v7.6.5.32


======= 人工智能 Keras、TensorFlow 的环境安装 ====== 

1.window下 安装 anaconda(python 3.6 / python 3.7)

2.安装 ubuntu 16/18

3.window下安装 Keras、TensorFlow(先安装CUDA、cuDNN,再安装Keras、TensorFlow)

===== 第一步:先安装CUDA、cuDNN,再安装Keras、TensorFlow ====== 

/s/1A2i7uPvTUsDf_5F_Jo69bA 
提取码:u8wv 

1.tensorflow-gpu 和 CUDA 和 cuDNN 的版本对应
    CUDA、tensorflow版本清单:https://tensorflow.google.cn/install/source#linux
2.CUDA 下载:
    CUDA 各版本:https://developer.nvidia.com/cuda-toolkit-archive
3.cuDNN 下载:
    cuDNN 各版本:https://developer.nvidia.com/rdp/cudnn-archive


4.可以创建多个anaconda的虚拟环境(非必要选项)

    1.查看所有虚拟环境:conda info --env
    2.创建虚拟环境:conda create --name 环境名字 python=版本号
        1.conda create --name tensorflow1.4 python=3.7 
            虚拟环境:环境名字叫做tensorflow1.4,python版本为3.7
        2.conda create --name tensorflow1.9 python=3.7
            虚拟环境:环境名字叫做tensorflow1.9,python版本为3.7

    3.切换到某个anaconda的虚拟环境下:conda activate 环境名字
        conda activate tensorflow1.4
        conda activate tensorflow1.9

    4.删除虚拟环境:conda remove -n 环境名字 --all
        conda remove -n tensorflow1.4 --all
        conda remove -n tensorflow1.9 --all

5.安装 tensorflow-gpu 1.9.0、CUDA 9.0、cuDNN 7.1.4
    1.安装 CUDA 9.0
        1.先安装主体 cuda_9.0.176_win10-exe.exe,再安装补丁
          安装完成后,会自动删除解压目录的文件夹
        2.自定义安装,默认选择全部组件
        3.可默认安装在系统盘路径
        4.测试
            1.nvcc -V 查看到安装的版本信息

            2.cd C:\Program Files\NVIDIA Corporation\NVSMI    
              nvidia-smi.exe 查看到所监控的程序运行信息

    2.安装 cuDNN 7.1.4
        解压 cudnn-8.0-windows10-x64-v7.1-ga.zip,解压出一个叫做 cuda的文件夹 
        把该 cuda文件夹 中的所有文件 拷贝到 CUDA的安装目录下(C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0)

==== 第二步:再 window下 安装 Keras、TensorFlow ===== 

    3.安装 tensorflow-gpu 1.9.0
        1.准备工作
            python -m pip install --upgrade pip
            pip install msgpack
            conda install m2w64-toolchain

        2.从清华镜像源下载安装tensorflow-gpu:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu==1.9
         3.测试:tensorflow 的Hello World 例子
                import tensorflow as tf
                 hello = tf.constant("hello tensorflow")
                  sess = tf.Session()
                  print(sess.run(hello))
    4.安装 Theano、keras
        1.从清华镜像源下载安装keras:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple keras
        2.从清华镜像源下载安装Theano:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple Theano

    5.anaconda 一般默认安装好 matplotlib、scikit-learn、numpy
        pip install matplotlib
        pip install scikit-learn
        pip install numpy


 

========== window下 安装 Keras、TensorFlow 详细===========

注意1:下载TensorFlow等软件时,请使用国内镜像源下载,提高下载速度和成功率
    国内源:
        新版ubuntu要求使用https源,要注意。
        清华:https://pypi.tuna.tsinghua.edu.cn/simple
        阿里云:http://mirrors.aliyun.com/pypi/simple/
        中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
        华中理工大学:http://pypi.hustunique.com/
        山东理工大学:http://pypi.sdutlinux.org/ 
        豆瓣:http://pypi.douban.com/simple/

    临时使用:可以在使用pip的时候加参数-i https://pypi.tuna.tsinghua.edu.cn/simple
    例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider,这样就会从清华这边的镜像去安装pyspider库。

    永久修改:
        Linux下,修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)
        windows下,直接在user目录中创建一个pip目录,如:C:\Users\xx\pip,新建文件pip.ini 

    内容如下:
        [global]
        index-url = https://pypi.tuna.tsinghua.edu.cn/simple
        [install]
        trusted-host=mirrors.aliyun.com
 
注意2:pip/conda 安装和卸载 软件
           如果使用pip安装软件的话,必须使用pip卸载
    如果使用conda安装软件的话,必须使用conda卸载
1.pip 安装 numpy、scipy、scikit-leam、h5py、pillow
    自动安装:安装 Anaconda Python,它会自动安装 numpy、scipy、scikit-leam、h5py、pillow 以及许多其他用于科学计算的包。
    手动安装:pip install numpy scipy scikit-learn h5py pillow
    比如从清华镜像源下载:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy scipy scikit-learn h5py pillow


2.pip 安装 Theano
    pip install Theano
    比如从清华镜像源下载:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple Theano

3.pip 安装 TensorFlow
    注意:安装 TensorFlow 之前,应先安装 CUDA、cuDNN,因根据 TensorFlow 的版本 来安装对应的版本的 CUDA、cuDNN
    1.选择以下某个 TensorFlow 软件包来进行安装
        1.tensorflow 仅支持 CPU 的当前版本(建议新手使用)
          pip install tensorflow

        2.tensorflow-gpu 支持 GPU 的当前版本(Ubuntu 和 Windows)
          pip install tensorflow-gpu
 
        3.tf-nightly 仅支持 CPU 的每夜版(不稳定)
          pip install tf-nightly

        4.tf-nightly-gpu 支持 GPU 的每夜版(不稳定,Ubuntu 和 Windows)
          pip install tf-nightly-gpu

      比如从清华镜像源下载:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu
 
    2.软件包位置(请翻墙下载)
        有几个安装机制需要您提供 TensorFlow Python 软件包的网址。您所指定的值取决于您的 Python 版本。
        版本                网址
        Linux
            Python 2.7(仅支持 CPU)    https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.12.0-cp27-none-linux_x86_64.whl
            Python 2.7(支持 GPU)    https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.12.0-cp27-none-linux_x86_64.whl
            Python 3.4(仅支持 CPU)    https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.12.0-cp34-cp34m-linux_x86_64.whl
            Python 3.4(支持 GPU)    https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.12.0-cp34-cp34m-linux_x86_64.whl
            Python 3.5(仅支持 CPU)    https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.12.0-cp35-cp35m-linux_x86_64.whl
            Python 3.5(支持 GPU)    https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.12.0-cp35-cp35m-linux_x86_64.whl
            Python 3.6(仅支持 CPU)    https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.12.0-cp36-cp36m-linux_x86_64.whl
            Python 3.6(支持 GPU)    https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.12.0-cp36-cp36m-linux_x86_64.whl

        macOS(仅支持 CPU)
            Python 2.7    https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.12.0-py2-none-any.whl
            Python 3.4、3.5、3.6    https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.12.0-py3-none-any.whl

        Windows
            Python 3.5(仅支持 CPU)    https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.12.0-cp35-cp35m-win_amd64.whl
            Python 3.5(支持 GPU)    https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.12.0-cp35-cp35m-win_amd64.whl
            Python 3.6(仅支持 CPU)    https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.12.0-cp36-cp36m-win_amd64.whl
            Python 3.6(支持 GPU)    https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.12.0-cp36-cp36m-win_amd64.whl

        安装 GPU版本的tensorflow,比如 pip 为 Python 3.6版本:(请翻墙下载)
            pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.12.0-cp36-cp36m-win_amd64.whl
            pip install msgpack
         pip为 python 3.7版本时,装 tensorflow-gpu 是 1.13.1 版本
        pip为 python 3.6版本时,装 tensorflow-gpu 1.12.0 版本
 
4.安装 Keras
    pip install keras
    比如从清华镜像源下载:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple keras
 
5.测试 Theano、TensorFlow、Keras
    现在来测试环境 先看看如何在 Theano 中定义 sigmoid 函数,只需写下数据公式,并按矩阵元素作函数计算即可。
    1.测试 Theano
        打开 Spyder(Python 3.7)编辑器,在控制台输入以下代码
        import theano 
        import theano.tensor as T
        x = T.dmatrix('x') 
        s = 1 / (1 + T.exp (-x)) 
        logistic = theano.function([x],s) 
        logistic([[0,1],[-1,-2]]) 
        输出结果:array([[0.5, 0.73105858], [0.26894142, 0.11920292]])

        import theano 时出现的报错
            1.报错:WARNING (theano.configdefaults): g++ not available, if using conda: `conda install m2w64-toolchain`
              解决:conda install m2w64-toolchain

            2.报错:ImportError: Something is wrong with the numpy installation. While importing we detected an older version of numpy in 
                    ['G:\\\\anaconda\\\\lib\\\\site-packages\\\\numpy']. One method of fixing this is to repeatedly uninstall numpy until none is found,
                    then reinstall this version.
              解决:多次执行 pip uninstall numpy 直到卸载干净


                然后再次执行 pip install numpy
 
 

    2.tensorflow 的 hellow world 例子
        1.import tensorflow as tf
          hello = tf.constant("hello tensorflow")
          sess = tf.Session()
          print(sess.run(hello))
 

            报错:ImportError: DLL load failed: 找不到指定的模块。
            解决1:安装 Microsoft Visual C++ 2017 Redistributable
            解决2:pip list 查看到 Pillow 5.1.0 升级到 Pillow  5.4.1
                conda uninstall pillow
                conda update pip
                pip install pillow
            解决3:卸载已安装的tensorflow,重装 tensorflow 为其他版本
                   已安装的tensorflow:
                    tensorflow-gpu 1.12.0
                   如果使用pip安装tensorflow的话,必须使用pip卸载。
                   如果使用conda安装tensorflow的话,必须使用conda卸载。
                   卸载 已安装的tensorflow:                
                    pip uninstall tensorflow-gpu
                   重装 tensorflow 为 1.4版本,同时应安装对应匹配版本的 CUDA 8.0、cuDNN 6.0 
                    pip install tensorflow-gpu==1.4
                    比如从清华镜像源下载:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu==1.4
                   重装 tensorflow 为 1.9版本,同时应安装对应匹配版本的 CUDA 9.0、cuDNN 7.1.4
                    pip install tensorflow-gpu==1.9
                    比如从清华镜像源下载:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu==1.9

            报错:ImportError: Could not find 'cudart64_80.dll'. 
                  TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. 
                  Download and install CUDA 8.0 from this URL: https://developer.nvidia.com/cuda-toolkit
            解决:根据TensorFlow的版本安装匹配版本的CUDA、cuDNN

        2.测试 TensorFlow,简单地导入 MNIST 数据集
            from tensorflow.examples.tutorials.mnist import input_data 
              mnist = input_data.read_data_sets("MNIST_data/",one_hot=True) 


  

测试GPU是否能正常运行tensorflow 

>>> import tensorflow as tf
>>> tf.test.is_gpu_available()
import tensorflow as tf
import timeit

with tf.device('/cpu:0'):
    cpu_a = tf.random.normal([10000, 1000])
    cpu_b = tf.random.normal([1000, 2000])
    print(cpu_a.device, cpu_b.device)

with tf.device('/gpu:0'):
    gpu_a = tf.random.normal([10000, 1000])
    gpu_b = tf.random.normal([1000, 2000])
    print(gpu_a.device, gpu_b.device)


def cpu_run():
    with tf.device('/cpu:0'):
        c = tf.matmul(cpu_a, cpu_b)
    return c


def gpu_run():
    with tf.device('/gpu:0'):
        c = tf.matmul(gpu_a, gpu_b)
    return c


# warm up
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('warmup:', cpu_time, gpu_time)

cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('run time:', cpu_time, gpu_time)
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

あずにゃん

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值