Win10 Anaconda下安装tensorflow-gpu并在jupyter notebook中使用

参考的原博客:

1.https://www.cnblogs.com/guohaoblog/p/9319791.html

2.https://blog.csdn.net/Young__Fan/article/details/88130385

3.https://blog.csdn.net/u012605050/article/details/85158102

4.https://blog.csdn.net/yifansss/article/details/82745122


个别资源的下载地址:

cudnn-9.2-windows10-x64-v7.4.2.24:

https://download.csdn.net/download/qq_34597253/10978784

fashion-mnist数据集和论文:

https://download.csdn.net/download/qq_36387683/10465409


操作过程:

1.在Anaconda prompt中,建立一个conda环境,命名为tensorflow:

conda create -n tensorflow python=3.6

2.激活创建的环境:

activate tensorflow

3.下载指定版本的tensorflow-gpu(这里装的是1.10版本,最新的1.13可能要下cuda10.0,由于墙最近得到了buff,无法上官网查证):

pip install tensorflow-gpu==1.10.0

每个版本的tensorflow对CUDA和cuDNN的版本要求不一样,详见:https://www.tensorflow.org/install/source_windows

4.前往cuda官网下载CUDA Toolkit 9.0,及其相关patch,并安装:

https://developer.nvidia.com/cuda-90-download-archive?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exenetwork

5.下载cuDNN v7.4.2(CSDN上有资源,但是要消耗9个积分,我懒得注册nvidia账号就直接下载了):

https://developer.nvidia.com/rdp/cudnn-download

6.安装cuDNN v7.4.:详见开头原博客3

(1)解压cudnn-9.0-windows10-x64-v7.zip

(2)将以下文件复制到

      Copy <installpath>\cuda\bin\cudnn64_7.dll to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin.

      Copy <installpath>\cuda\ include\cudnn.h to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\include.

      Copy <installpath>\cuda\lib\x64\cudnn.lib to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\lib\x64.

(3)打开“运行”-“control sysdm.cpl”-高级-环境变量,确保以下配置路径正确:

         Variable Name: CUDA_PATH
         Variable Value: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0

7.vs相关:因为我的电脑里已经安装了visual studio 2015,所以vs相关的坑我都不用踩。需要安装vs相关文件可以参考博客3

8.在创建的环境下,测试一下tensorflow有没有装好(如果tensorflow安装好,就不会报任何错误。):

python
import tensorflow as tf

9.在创建的环境下,安装ipython和jupyer:

conda install ipython
conda install jupyter

10.在创建的环境下,接着输入(用意有待学习):

ipython kernelspec install-self --user

11.在创建的环境下,打开jupyter notebook:

jupyter notebook

新建一个代码,测试tensorflow能不能被导入


测试一下能不能正常跑程序:

1.首先在本地导入fashion-mnist数据集(因为网的问题只能这么做...),代码来自原博客4

from tensorflow.python.keras.utils import get_file
import gzip
import numpy as np

def load_data():
    base = "file:///D:/fasion/"
    files = [
        'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
        't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
    ]

    paths = []
    for fname in files:
        paths.append(get_file(fname, origin=base + fname))

    with gzip.open(paths[0], 'rb') as lbpath:
        y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)

    with gzip.open(paths[1], 'rb') as imgpath:
        x_train = np.frombuffer(
            imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)

    with gzip.open(paths[2], 'rb') as lbpath:
        y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)

    with gzip.open(paths[3], 'rb') as imgpath:
        x_test = np.frombuffer(
            imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)

    return (x_train, y_train), (x_test, y_test)

2.然后进行训练,代码来自原博客3

import tensorflow as tf
from tensorflow import keras
import time
 
# 获取数据
(train_images, train_labels), (test_images, test_labels) = load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
 
train_images = train_images / 255.0
test_images = test_images / 255.0
 
# 构建模型
with tf.device("/device:GPU:0"):
    time_start = time.time()
    model = keras.Sequential([
        keras.layers.Flatten(input_shape=(28, 28)),
        keras.layers.Dense(10000, activation=tf.nn.relu),
        keras.layers.Dense(10, activation=tf.nn.softmax)
    ])
    
    model.compile(optimizer=tf.train.AdamOptimizer(),
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
#训练模型
model.fit(train_images, train_labels, batch_size=32, epochs=5)
 
time_end = time.time()
print("The used time for GPU is %.2fs" %(time_end-time_start))

3.训练结果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值