Win10 tensorflow use notes - installation and training mnist

1. Because caffe has been installed and visualized under win10 before, that is, the cuda, cudnn, Anaconda2 and other libraries have been installed, and it is much easier to install tensorflow. Similar to Linux, pip is used to install the command line tool, but cmd is not used. Under win10, an administrator is required, and Window PowerShell is used to install it.

2. The window version of tensorflow requires python3.5 and above, and Anaconda2 corresponds to 2.7, so you have to install Anaconda3 separately. The installation directory of Anaconda3 must be selected in the *\Anaconda2\envs subdirectory, and define the name of the installation file, such as python3. 5. In the installation process, you can choose to add environment variables and default python3.5, because the settings have been made in the installation of Anaconda2, and you can switch the python version back and forth through the conda environment.

conda switch python version:

conda create --name python3.5 python=3.5 //python3.5 is the Anaconda3 file name installed under envs

activate python3.5 //Activate

After success, you can see that there are more paths (python3.5), you can enter python to view the version

deactivate python3.5 //return to previous python version

3. Start the installation of tensorflow under PowerShell, and activate the python3.5 version with cmd under PowerShell, because PowerShell cannot be activated directly.

4. pip install --ignore-installed --upgrade pip setuptools //Avoid pip install tensorflow error

5. pip --default-timeout=100 install --upgrade https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.4.0-cp35-cp35m-win_amd64.whl//Start installing tensorflow, --default -timeout=100 is because the native image needs to be downloaded over the wall. The download speed is impressive, and it often times out. You can choose a domestic image. The latest tensorflow version can be downloaded from https://pypi.python.org/pypi/tensorflow/1.4 .0 to get.



6. In the testing process, tensorflow also needs cudnn64_6.dll , decompress cudnn6.0, copy the cudnn64_6.dll file to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin, and then test the official website sample.



7. To train mnist, download the mnist dataset to the newly created MNIST_data folder.

train_mnist.py

import tensorflow as tf
#import tensorflow.examples.tutorials.mnist.input_data as input_data #download dataset
from tensorflow.examples.tutorials.mnist import input_data #Read the next good dataset

mnist=input_data.read_data_sets("MNIST_data",one_hot=True) #mnist dataset 4 files are placed in the MNIST_data folder

x = tf.placeholder(tf.float32, [None, 784])  #28*28维
y_actual = tf.placeholder(tf.float32, shape=[None, 10])

#定义一个函数,用于初始化所有的权值 W
def weight_variable(shape):
  initial = tf.truncated_normal(shape, stddev=0.1)
  return tf.Variable(initial)

#定义一个函数,用于初始化所有的偏置项 b
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(x):
  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')

#构建网络
x_image = tf.reshape(x, [-1,28,28,1])         #转换输入数据shape,以便于用于网络中
W_conv1 = weight_variable([5, 5, 1, 32])      
b_conv1 = bias_variable([32])       
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)     #第一个卷积层
h_pool1 = max_pool(h_conv1)                                  #第一个池化层

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(h_conv2)                                   #第二个池化层

W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])              #reshape成向量
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)    #第一个全连接层

keep_prob = tf.placeholder("float") 
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)                  #dropout层

W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_predict=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)   #softmax层

cross_entropy = -tf.reduce_sum(y_actual*tf.log(y_predict))     #交叉熵
train_step = tf.train.GradientDescentOptimizer(1e-3).minimize(cross_entropy)    #梯度下降法
correct_prediction = tf.equal(tf.argmax(y_predict,1), tf.argmax(y_actual,1))    
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))                 #精确度计算
sess=tf.InteractiveSession()                          
sess.run(tf.initialize_all_variables())
for i in range(20000):
  batch = mnist.train.next_batch(50)
  if i%100 == 0:                  #训练100次,验证一次
    train_acc = accuracy.eval(feed_dict={x:batch[0], y_actual: batch[1], keep_prob: 1.0})
    print('step',i,'training accuracy',train_acc)
    train_step.run(feed_dict={x: batch[0], y_actual: batch[1], keep_prob: 0.5})

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



准确率有点低,看了下训练文件只有2层卷积层,添加了softmax就回归了,网络还不是Lenet-5模型,但是tensorflow安装测试应该就没什么问题了。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325607812&siteId=291194637