机器学习–Windows10下使用GPU配置Tensorflow环境运行MNIST

在爬了无数个帖子之后,发现使用Anaconda+python3.6+cuda配置多次无效(昨天配到3点,今天9点起床又配到现在),终于决定近期放弃使用Anaconda配置。直接如下配置环境:

环境安装:

下载并安装 python3.5.3

下载并安装Python的包管理工具pip

下载完(get-pip.py文件复制到迅雷下载更快)后在cmd到指定文件下使用命令安装:

python get-pip.py

 

cmd中利用pip安装numpy

pip install numpy

PowerShell(管理员)下利用pip安装tensorflow

pip install tensorflow

GPU版:

(需要安装相关的英伟达文件 https://developer.nvidia.com/cuda-downloads 以及 https://developer.nvidia.com/cudnn  博主使用cuda9.0  cudnn7.1  tensorflow1.8.0)

配置cudnn时候不是覆盖文件,而是将bin文件添加至path环境,然后编译其他

pip install tensorflow-gpu

验证CUDA

注意大小写:

nvcc -V

使用VS和CUDA编译测试文件

进入“C:\ProgramData\NVIDIA Corporation\CUDA Samples\v8.0”文件夹,双击打开 “Samples vs2015”这个文件。

选择编译生成1_Utilities中所有的文件。具体操作就是在1_Utilities上右键选择Build,注意选择上方的的64位和Release

deviceQuery和bandwidthTest验证

运行我们刚才编译出来的deviceQuery.exe,也就是在cmd中运行这个文件,下图中左下的红框显示 result = pass代表安装测试成功

查看所有安装

CMD下键入

pip list

 

 

测试Tensorflow是否安装成功

按在CMD中键入Python后

import tensorflow

没有出现报错,即为成功

 

检测Tensorflow是否可以使用GPU

cmd中键入python之后:

import tensorflow as tf
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print (sess.run(c))

结果如图:

即为成功

开始运行所需文件

MNIST_data(4个):

t10k-images-idx3-ubyte.gz

t10k-labels-idx1-ubyte.gz

train-images-idx3-ubyte.gz

train-labels-idx1-ubyte.gz

Python源码:

input_data.py

tes.py

文档结构放置:(如图)

运行结果如图:

由图可知,精度为0.9188

 

相关源码:

tes.py:

import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10])+0.1)
y = tf.nn.softmax(tf.matmul(x,W) + b)

y_ = tf.placeholder("float", [None,10])

cross_entropy = -tf.reduce_sum(y_*tf.log(y))

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

init = tf.global_variables_initializer()

sess = tf.Session()
sess.run(init)

for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

input_data.py:

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.

#

# Licensed under the Apache License, Version 2.0 (the "License");

# you may not use this file except in compliance with the License.

# You may obtain a copy of the License at

#

#     http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS,

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and

# limitations under the License.

# ==============================================================================



"""Functions for downloading and reading MNIST data."""

from __future__ import absolute_import

from __future__ import division

from __future__ import print_function



# pylint: disable=unused-import

import gzip

import os

import tempfile



import numpy

from six.moves import urllib

from six.moves import xrange  # pylint: disable=redefined-builtin

import tensorflow as tf

from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets

# pylint: enable=unused-import

 

 



— 于 共写了3221个字
— 文内使用到的标签:

发表评论

电子邮件地址不会被公开。