Win10 Anaconda下配置cuda10.0+cudnn7.41+tensorflow1.13.1+jupyter notebook

cuda10.0+cudnn7.41+tensorflow1.13.1
電腦配置:聯想小新pro13,win10,cuda10.2.95
建議: 電腦cuda高的,可以安裝cuda版本低的。
我一開始安裝的cuda10.2+cudnn7.65+tensorflow-gpu2.0,新的tensorflow有很多語句不一樣,太麻煩了,又改爲1.13.1版本。

1.安裝anaconda

我是安裝的3.5.2版本,點擊獲取:下載鏈接
安裝步驟:
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
配置環境變量
將以下三個路徑加入,注意這裏要換成你自己的安裝路徑。

D:\xuexiRJ\ana
D:\xuexiRJ\ana\Scripts
D:\xuexiRJ\ana\Library\bin
在這裏插入圖片描述
打開prompt:
輸入:conda -V,
輸出:conda 4.54.
安裝成功!!!

2.安裝cuda10.0

然後進入NVIDIA控制面板->幫助>系統信息->組件 查看可以使用的cuda版本
在這裏插入圖片描述
進入官網:https://developer.nvidia.com/cuda-toolkit-archive
找到和顯卡信息相匹配的cuda(cuda是向下兼容的)

在這裏插入圖片描述
在這裏插入圖片描述加粗樣式
手動添加環境變量,如下圖所示
在這裏插入圖片描述

3.安裝cuDNN7.3.1

官網註冊完才能下載。下面鏈接包含cuda,cudnn
百度雲鏈接:https://pan.baidu.com/s/1f8jT57CuTwEcLpiLQEXQRw
提取碼:9zou
在這裏插入圖片描述
在這裏插入圖片描述
將cudn中的三個文件複製到cuda安裝路徑中,如下圖所示(圖應該是10.0)
(一開始安裝的cuda10.2)
在這裏插入圖片描述

4.安裝tensorflow -gpu1.13.1版本

先創建環境

conda create --name tfgpu python=3.5

進入環境

activate tfgpu
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu==1.13.1

5.安裝jupyternotebook

還是在tfgpu環境中執行

conda install jupyter notebook

下面測試:
在這裏插入圖片描述

6.測試二

tensorflow官網文檔minist進階程序,下面程序詳細解釋

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('D:/E-WenDang/ruanjianDM/jupyternoerbookDM/MachineLearning/TensorFlow/tensorflowBJ/mnist/mnist_data/', one_hot=True)

def weight_variable(shape):
  initial = tf.truncated_normal(shape, stddev=0.1)
  return tf.Variable(initial)

def bias_variable(shape):
  initial = tf.constant(0.1, shape=shape)
  return tf.Variable(initial)
def conv2d(x, W):
  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1], padding='SAME')

x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])
"第一層卷積:28->14"
W_conv1 = weight_variable([5, 5, 1, 32])#前兩個維度是patch的大小,接着是輸入的通道數目,最後是輸出的通道數目。
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1,28,28,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

"第二層卷積:14->7"
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

"全連接層:1024人爲固定"
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
#池化層輸出的張量reshape成一些向量,乘上權重矩陣,加上偏置,然後對其使用ReLU
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

"減少過擬合,我們在輸出層之前加入dropout"
keep_prob = tf.placeholder("float")#一個神經元的輸出在dropout中保持不變的概率
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

"輸出層"
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)


from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)

cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
session.run(tf.initialize_all_variables())
for i in range(2000):
    batch = mnist.train.next_batch(50)
    train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
    if i%100 == 0:
        train_accuracy = accuracy.eval(feed_dict={
            x:batch[0], y_: batch[1], keep_prob: 1.0})
        print ("step %d, training accuracy %g"%(i, train_accuracy))

print ("test accuracy %g"%accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

程序報錯,按錯誤提示:將cudnn按上面步驟改爲7.4.1版本,程序成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章