1. 程式人生 > >win10下通過Anaconda安裝TensorFlow-GPU1.3版本,並配置pycharm運行Mnist手寫識別程序

win10下通過Anaconda安裝TensorFlow-GPU1.3版本,並配置pycharm運行Mnist手寫識別程序

mnist all -1 為什麽 提示 4.2 not correct sof

折騰了一天半終於裝好了win10下的TensorFlow-GPU版,在這裏做個記錄。

準備安裝包:

visual studio 2015;

Anaconda3-4.2.0-Windows-x86_64;

pycharm-community;

CUDA:cuda_8.0.61_win10;下載時選擇 exe(local)

CUDA補丁:cuda_8.0.61.2_windows;

cuDNN:cudnn-8.0-windows10-x64-v6.0;如果你安裝的TensorFlow版本和我一樣1.3,請下載cuDNN v6.0 for CUDA 8.0 (不要問我為什麽知道....)

開始:

1.安裝visual studio2015 可以只安裝 Visualc++部分

2.安裝CUDA:

按提示安裝;

裝完後在cmd裏查看版本號:nvcc -V

技術分享

3.安裝cuDNN庫:

把解壓文件放置到CUDA的相關文件夾裏:(懶得打字了)

技術分享==============》》 技術分享

4.安裝Anaconda:

我是選擇用Anaconda安裝TensorFlow,方便管理各種環境。

下載對應安裝包,按提示安裝。

裝好後,打開Anaconda Prompt.添加清華的鏡像源

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set
show_channel_urls yes

在win下更改Python的默認源為:

在當前的用戶目錄下新建pip文件夾,在pip文件夾裏新建pip.ini文件:

[global]  
index-url = http://mirrors.aliyun.com/pypi/simple/
//https://pypi.tuna.tsinghua.edu.cn/simple 清華源
[install]  
trusted-host=mirrors.aliyun.com

在Anaconda裏建立TensorFlow的環境:

conda create -n tensorflow Python=3.5

激活TensorFlow環境:

activate TensorFlow

關閉環境:

deactivate

5.安裝TensorFlow:

pip install --upgrade --ignore-installed tensorflow-gpu

簡單測試:

在TensorFlow環境裏打開Python:

import tensorflow as tf

hello = tf.constant("Hello!TensorFlow")
sess = tf.Session()
print(sess.run(hello))

技術分享

技術分享

見到b‘hello tensorflow‘ 測試成功。

6.安裝,配置pycharm:

下載對應安裝包。按提示安裝pycharm。

配置pycharm:

在選擇Python版本時,手動添加位於Anaconda的python版本,位置在Anaconda的安裝目錄下的envs文件夾裏:

技術分享

7.在pycharm裏新建mnist手寫識別程序:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32,[None,784])

w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x,w) + b)

y_ = tf.placeholder(tf.float32,[None,10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),reduction_indices=[1]))

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

tf.global_variables_initializer().run()

for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    train_step.run({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,tf.float32))

print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))

運行:

技術分享

至此全部安裝完畢。

安裝過程中的坑:

1.安裝TensorFlow1.3的cuDNN的版本要選V6的版本,不然會出現

No Module Named ‘_pywrap_tensorflow_internal‘

以及

DLL load failed: The specified module could not be found等錯誤。

win10下通過Anaconda安裝TensorFlow-GPU1.3版本,並配置pycharm運行Mnist手寫識別程序