DL07-TF试验MNIST

Demo1:全连接
# 第一步:导入包
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

# 第二步,创建session
#  InteractiveSession类允许将运行图的时候插入计算图;
#  如果您没有使用InteractiveSession,则应在开始会话前构建整个计算图,然后 启动图。
sess = tf.InteractiveSession()
mnist = input_data.read_data_sets('data/MNIST_data', one_hot=True)

# 第三步, 占位符
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])

# 第四步, 变量
#      784个输入特征和10个输出
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# 第五步, 初始化变量
sess.run(tf.global_variables_initializer())
# print(b.eval())

# 第六步,创建模型
# 回归模型。将矢量化输入图像x乘以权重矩阵W,添加偏置b。
y = tf.matmul(x, W) + b
# 第七步,定义损失函数
#      将softmax应用于模型的非规范化预测和所有类别总数
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
# 第八步,定义单步参数更新算子
#       计算梯度,计算每个参数的步长变化,对变化的参数进行更新。
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
# 第九步,迭代训练
#        输入数据来训练模型
for _ in range(1000):
    batch = mnist.train.next_batch(100)
    train_step.run(feed_dict={x: batch[0], y_: batch[1]})

# 第十步,评估模型
#         预测值与真实值的误差评估
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(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

### 查看一些信息
### 训练集的信息
train_shape = tf.shape(mnist.train.images)
print(train_shape.eval())
t_labels_shape = tf.shape(mnist.train.labels)
print(t_labels_shape.eval())
#### 测试集的信息
test_shape = tf.shape(mnist.test.images)
print(test_shape.eval())
t_test_shape = tf.shape(mnist.test.labels)
print(t_test_shape.eval())

0.9137001
[55000 784]
[55000 10]
[10000 784]
[10000 10]

Demo2:构建多层卷积网络
"""
构建多层卷积网络
"""
# 第一步:导入包
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

# 第二步,创建session
#  InteractiveSession类允许将运行图的时候插入计算图;
#  如果您没有使用InteractiveSession,则应在开始会话前构建整个计算图,然后 启动图。
sess = tf.InteractiveSession()
mnist = input_data.read_data_sets('data/MNIST_data', one_hot=True)


# 第二步,权重初始化函数
# 要创建该模型,我们需要创建很多权重置和偏置量。
#  通常,权重在初始化时应该加入少量的噪声来打破对称性以及避免0梯度。
#  由于我们使用ReLU神经元,因此较好的做法是用一个较小的正数来初始化偏置量,以避免神经元节点输出恒为0(dead neurons)。
#  为了不在构建模型时反复执行初始化操作,我们定义两个函数用于初始化。
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)


# 第三步,卷积函数。 卷积使用1步长,零填充,使输出与输入的大小相同。
def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')


# 第四步,池化函数。 用简单传统的2x2大小的模板做最大池化
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(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])

# 第六步,构造网络
# ##### 第一层卷积 它由一个卷积接一个最大池化构成
# 卷积在每5x5的patch中算出32个特征。
# 权重张量形状为[5, 5, 1, 32]:
# 前两个维度是patch大小,
# 第三个维度是图片颜色输入通道的数量(灰度图片为1,彩色图片为3),
# 最后一个是输出通道的数量。
W_conv1 = weight_variable([5, 5, 1, 32])
# 每个输出通道添加一个偏置量
b_conv1 = bias_variable([32])
# 首先reshape x形成4d张量,第二和第三维对应于图片的宽度和高度,最后的尺寸对应于颜色通道的数量。
x_image = tf.reshape(x, [-1, 28, 28, 1])
# x_image与权重张量进行卷积,添加偏置量,应用于ReLU函数,最后再加入最大池化。
# 该max_pool_2x2方法将图像大小减小到14x14。
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

############第二卷积层
# 堆叠几层这种类型的模块。第二层将为每个5x5patch提取64特征。
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)

##########密集连接层
# 现在图像尺寸已经缩小到7x7,我们添加了一个具有1024个神经元的全连接层,以便对整个图像进行处理。
# 从池化层将张量reshape成一些向量,然后乘以权重矩阵,添加偏置量并应用于ReLU。
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
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层
# 为了避免过拟合,我们在输出层之前加入dropout 。
# 创建一个占位符表示在神经元dropuot时保持不变的概率,可以让我们在训练过程中应用dropout,并在测试过程中将其关闭。
keep_prob = tf.placeholder(tf.float32)
# TensorFlow的tf.nn.dropoutop除了可以屏蔽神经元的输出外,还可以自动处理神经元输出scale
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

# #########输出层
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2

## 第七步 损失函数
cross_entropy = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=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, tf.float32))

# 第十步,初始化变量
sess.run(tf.global_variables_initializer())

# 第十一步,训练
for i in range(5000):
    batch = mnist.train.next_batch(50)
    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))
    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}))

# 第十二步,测试
for i in range(1):
    testSet = mnist.test.next_batch(100)
    print("test accuracy %g" % accuracy.eval(feed_dict={x: testSet[0], y_: testSet[1], keep_prob: 1.0}))

    # 查看其它信息
    print('x:', x.eval(feed_dict={x: testSet[0]}).shape)
    print('x_image:', x_image.eval(feed_dict={x: testSet[0]}).shape)
    print('h_conv1:', h_conv1.eval(feed_dict={x: testSet[0]}).shape)
    print('h_pool1:', h_pool1.eval(feed_dict={x: testSet[0]}).shape)
    print('h_conv2:', h_conv2.eval(feed_dict={x: testSet[0]}).shape)
    print('h_pool2:', h_pool2.eval(feed_dict={x: testSet[0]}).shape)
    print('h_fc1:', h_fc1.eval(feed_dict={x: testSet[0]}).shape)
    print('y_conv:', y_conv.eval(feed_dict={x: testSet[0], keep_prob: 1.0}).shape)

结果

step 100, training accuracy 0.74
step 200, training accuracy 0.92
step 300, training accuracy 0.92
step 400, training accuracy 0.98
step 500, training accuracy 0.92
step 600, training accuracy 0.98
step 700, training accuracy 0.94
step 800, training accuracy 0.98
step 900, training accuracy 1
step 1000, training accuracy 0.98
step 1100, training accuracy 0.94
step 1200, training accuracy 0.96
step 1300, training accuracy 0.96
step 1400, training accuracy 1
step 1500, training accuracy 0.92
step 1600, training accuracy 0.98
step 1700, training accuracy 0.94
step 1800, training accuracy 0.96
step 1900, training accuracy 0.98
step 2000, training accuracy 0.96
step 2100, training accuracy 0.98
step 2200, training accuracy 0.98
step 2300, training accuracy 0.98
step 2400, training accuracy 0.96
step 2500, training accuracy 0.96
step 2600, training accuracy 1
step 2700, training accuracy 0.94
step 2800, training accuracy 0.98
step 2900, training accuracy 0.98
step 3000, training accuracy 0.96
step 3100, training accuracy 1
step 3200, training accuracy 1
step 3300, training accuracy 0.94
step 3400, training accuracy 1
step 3500, training accuracy 0.98
step 3600, training accuracy 0.98
step 3700, training accuracy 1
step 3800, training accuracy 0.94
step 3900, training accuracy 0.96
step 4000, training accuracy 1
step 4100, training accuracy 1
step 4200, training accuracy 0.98
step 4300, training accuracy 0.98
step 4400, training accuracy 1
step 4500, training accuracy 0.98
step 4600, training accuracy 1
step 4700, training accuracy 0.96
step 4800, training accuracy 0.98
step 4900, training accuracy 1
test accuracy 0.94
x: (100, 784)
x_image: (100, 28, 28, 1)
h_conv1: (100, 28, 28, 32)
h_pool1: (100, 14, 14, 32)
h_conv2: (100, 14, 14, 64)
h_pool2: (100, 7, 7, 64)
h_fc1: (100, 1024)
y_conv: (100, 10)
问题

运行过程中,发现并没有运行GPU运行。采用GPU测试,如下的报错:

Device mapping: no known devices.
Traceback (most recent call last):
  File "C:\Users\win7\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1139, in _do_call
    return fn(*args)
  File "C:\Users\win7\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1117, in _run_fn
    self._extend_graph()
  File "C:\Users\win7\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1166, in _extend_graph
    self._session, graph_def.SerializeToString(), status)
  File "C:\Users\win7\AppData\Local\Programs\Python\Python35\lib\contextlib.py", line 66, in __exit__
    next(self.gen)
  File "C:\Users\win7\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation 'MatMul': Operation was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/cpu:0 ]. Make sure the device specification refers to a valid device.
     [[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/device:GPU:0"](a, b)]]

检查安装包的情况:
这里写图片描述
安装了两个版本的tensorflow,把cpu那个给删除掉;只保留gpu版本后:

Traceback (most recent call last):
  File "F:/code/quotesbot/quotesbot/demoDL/demo_MMIST_conv.py", line 113, in <module>
    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
AttributeError: module 'tensorflow' has no attribute 'constant'

把gpu版本也删除,安装最新版本1.4,可还是报出找不到cuDNN64_6.dll的错误,阅读文档发现:
这里写图片描述
仔细阅读了一下文档,从1.3版本以上就要这个了cuDNN64_6.dll了。

在运行构建多层卷积网络时GPU的内存问题:

ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[10000,32,28,28]
     [[Node: Conv2D = Conv2D[T=DT_FLOAT, data_format="NHWC", padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/gpu:0"](Reshape, Variable/read)]]

把测试数据取少一些:

testSet = mnist.test.next_batch(100)
print("test accuracy %g" % accuracy.eval(feed_dict={x: testSet[0], y_: testSet[1], keep_prob: 1.0}))
参考

http://blog.csdn.net/xiang_freedom/article/details/72934148
https://www.tensorflow.org/versions/r1.3/install/install_windows
http://cwiki.apachecn.org/pages/viewpage.action?pageId=10029431

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值