win10+anaconda2+cuda8.0+cudnn6.0安装tensorflow-gpu

来源:互联网 发布:知聊赚钱是真的吗 编辑:程序博客网 时间:2024/06/04 20:08

官方介绍如何在windows上安装tensorflow

https://www.tensorflow.org/install/install_windows

再次简单记录重要步骤:

C:> conda create -n tensorflow python=3.5
C:> activate tensorflow (tensorflow)C:>  # Your prompt should change
(tensorflow)C:> pip install --ignore-installed --upgrade tensorflow-gpu

安装完成之后,在anaconda2的安装目录下,会有已经创建好的tensorflow 的environment.


使用命令行 列出来已经安装的env:

conda info--envs


env还可以进行克隆,命令如下:

conda create -n my_tensorflow --clone tensorflow

删除环境,命令如下:

conda remove -n my_tensorflow --all

在命令行中使用 activate和deactivate两个命令选择env

如果习惯了使用IDE的用户该怎么办呢?

在pycharm-》settings-》Project Interpreter里找到要改环境的项目,选中,然后右上侧点击齿轮,Add local, 选刚才新建环境里的python.exe , 之后确认即可。

如下图所示:




使用MNIST数据集进行测试,最好可以将MNIST数据集先下载下来,不要让code自己去下载,代码中下载比较慢,将数据集下载下来直接放入code中指定的文件夹下面,文件名不要修改,直接将code run起来。差不多2分钟吧,精度99.8%

code如下:

# -*- coding: utf-8 -*- import tensorflow as tf#导入input_data用于自动下载和安装MNIST数据集from tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True)#最好可以将MNIST数据集先下载下来,不要让code自己去下载,代码中下载比较慢,将数据集下载下来直接放入code中指定的文件夹下面,文件名不要修改#创建一个交互式Sessionsess = tf.InteractiveSession()#创建两个占位符,x为输入网络的图像,y_为输入网络的图像类别x = tf.placeholder("float", shape=[None, 784])y_ = tf.placeholder("float", shape=[None, 10])#权重初始化函数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)#创建卷积op#x 是一个4维张量,shape为[batch,height,width,channels]#卷积核移动步长为1。填充类型为SAME,可以不丢弃任何像素点def conv2d(x, W):    return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding="SAME")#创建池化op#采用最大池化,也就是取窗口中的最大值作为结果#x 是一个4维张量,shape为[batch,height,width,channels]#ksize表示pool窗口大小为2x2,也就是高2,宽2#strides,表示在height和width维度上的步长都为2def max_pool_2x2(x):    return tf.nn.max_pool(x, ksize=[1,2,2,1],                          strides=[1,2,2,1], padding="SAME")#第1层,卷积层#初始化W为[5,5,1,32]的张量,表示卷积核大小为5*5,第一层网络的输入和输出神经元个数分别为1和32W_conv1 = weight_variable([5,5,1,32])#初始化b为[32],即输出大小b_conv1 = bias_variable([32])#把输入x(二维张量,shape为[batch, 784])变成4d的x_image,x_image的shape应该是[batch,28,28,1]#-1表示自动推测这个维度的sizex_image = tf.reshape(x, [-1,28,28,1])#把x_image和权重进行卷积,加上偏置项,然后应用ReLU激活函数,最后进行max_pooling#h_pool1的输出即为第一层网络输出,shape为[batch,14,14,1]h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)h_pool1 = max_pool_2x2(h_conv1)#第2层,卷积层#卷积核大小依然是5*5,这层的输入和输出神经元个数为32和64W_conv2 = weight_variable([5,5,32,64])b_conv2 = weight_variable([64])#h_pool2即为第二层网络输出,shape为[batch,7,7,1]h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)h_pool2 = max_pool_2x2(h_conv2)#第3层, 全连接层#这层是拥有1024个神经元的全连接层#W的第1维size为7*7*64,7*7是h_pool2输出的size,64是第2层输出神经元个数W_fc1 = weight_variable([7*7*64, 1024])b_fc1 = bias_variable([1024])#计算前需要把第2层的输出reshape成[batch, 7*7*64]的张量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层#为了减少过拟合,在输出层前加入dropoutkeep_prob = tf.placeholder("float")h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)#输出层#最后,添加一个softmax层#可以理解为另一个全连接层,只不过输出时使用softmax将网络输出值转换成了概率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)#预测值和真实值之间的交叉墒cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))#train op, 使用ADAM优化器来做梯度下降。学习率为0.0001train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)#评估模型,tf.argmax能给出某个tensor对象在某一维上数据最大值的索引。#因为标签是由0,1组成了one-hot vector,返回的索引就是数值为1的位置correct_predict = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))#计算正确预测项的比例,因为tf.equal返回的是布尔值,#使用tf.cast把布尔值转换成浮点数,然后用tf.reduce_mean求平均值accuracy = tf.reduce_mean(tf.cast(correct_predict, "float"))#初始化变量sess.run(tf.global_variables_initializer())#开始训练模型,循环20000次,每次随机从训练集中抓取50幅图像for i in range(20000):    batch = mnist.train.next_batch(50)    if i%100 == 0:        #每100次输出一次日志        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))    train_step.run(feed_dict={x:batch[0], y_:batch[1], keep_prob:0.5})#print("test accuracy %g" % accuracy.eval(feed_dict={x:mnist.test.images, y_:mnist.test.labels, keep_prob:1.0}))#直接使用上面的代码在2GB memory的GPU上会发生OOM,修改成下面的就可以了,分多次计算print("test end")for i in range(10):testSet = mnist.test.next_batch(50)print("test accuracy %g"%accuracy.eval(feed_dict={x:testSet[0],y_:testSet[1],keep_prob:1.0}))

阅读全文
0 0
原创粉丝点击