深度学习框架-tensorflow基础入门

本篇博文只是用来记录自己的学习过程,作为2018年积累的开始,从这篇博文开始,我要养成一周一篇博文的习惯。今年要实现50篇博文的目标。

目录

tensorflow的基础使用

创建图,启动图

import tensorflow as tf
#创建一个常量op
m1 = tf.constant([[3,3]])
#创建一个常量op
m2 = tf.constant([[2],[3]])
#创建一个矩阵乘法op,把m1和m2传入
product = tf.matmul(m1,m2)
print(product)

结果是:
Tensor(“MatMul_1:0”, shape=(1, 1), dtype=int32)
说明这是一个张量

#定义一个会话,自动启动默认图
sess = tf.Session()
#调用sess的run方法来执行矩阵乘法op
#run(product)触发了图中3个op
result = sess.run(product)
print(result)
sess.close()

结果是:
[[15]]

需要建立一个会话,才能得到两个常量的乘积

#上面这段启动session可以改写成如下形式,此时就不用手动的去调用sess.close去关闭session
with tf.Session() as sess:
    #调用sess的run方法来执行矩阵乘法op
    #run(product)触发了图中3个op
    result = sess.run(product)
    print(result)

变量

import tensorflow as tf
#定义变量
x = tf.Variable([1,2])
a = tf.constant([3,3])
#定义减法和加法的op
sub = tf.subtract(x,a)
add = tf.add(x,sub)

#使用变量前要先初始化
init = tf.global_variables_initializer()

#定义会话
with tf.Session() as sess:
    #先运行初始化变量
    sess.run(init)

    print(sess.run(sub))
    print(sess.run(add))

[-2 -1]
[-1 1]

#变量可以初始化,初始化的值为0,名字为counter
state = tf.Variable(0,name='counter')

new_value = tf.add(state,1)

#调用赋值操作,不能直接用等号赋值
update = tf.assign(state,new_value)

#因为state是变量,所以需要初始化
init = tf.global_variables_initializer()

#定义会话
with tf.Session() as sess:
    #先运行初始化变量
    sess.run(init)
    #查看初始化的值
    print(sess.run(state))

    for _ in range(5):
        sess.run(update)
        print(sess.run(state))

运行结果:
0
1
2
3
4
5

fetch and feed

import tensorflow as tf
#Fetch: 可以同时运行多个op
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)

add = tf.add(input2,input3)
mul = tf.multiply(input1,add)

with tf.Session() as sess:
    #fetch多个op时,只要用中括号将多个op括起来即可
    result = sess.run([add,mul])
    print(result)

[7.0, 21.0]

#Feed
#创建占位符 :placeholder
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)

#可以在运行output时,再将input1和input2的值传入,传入时采用字典的形式
output = tf.multiply(input1,input2)
with tf.Session() as sess:
    #传入时采用字典的形式
    print(sess.run(output,feed_dict={input1:[8.],input2:[3.]}))

[ 24.]

tensorflow的简单使用

import tensorflow as tf
import numpy as np
#使用numpy生成100个随机点
x_data = np.random.rand(100)
y_data = x_data*0.1 + 0.2

#构建一个线性模型
b = tf.Variable(0.)
k = tf.Variable(0.)
y = x_data*k + b

#使用tensorflow训练出模型,即得到k和b的值
#定义二次代价函数
loss = tf.reduce_mean(tf.square(y_data - y))
#使用梯度下降函数来进行训练的优化器,下面学习率是0.2
optimizer = tf.train.GradientDescentOptimizer(0.2)
#最小化代价函数
train = optimizer.minimize(loss)


#初始化变量
init = tf.global_variables_initializer()
with tf.Session() as sess:
    #先运行初始化变量
    sess.run(init)

    for step in range(201):
        sess.run(train)
        if step%20 ==0 :
            print(step,sess.run([k,b]))

0 [0.0517033, 0.099609137]
20 [0.1019343, 0.1989941]
40 [0.10121739, 0.19936697]
60 [0.10076618, 0.19960159]
80 [0.10048222, 0.19974925]
100 [0.1003035, 0.19984218]
120 [0.10019101, 0.19990067]
140 [0.10012019, 0.19993751]
160 [0.10007564, 0.19996066]
180 [0.10004761, 0.19997525]
200 [0.10002997, 0.19998442]

tensorflow线性回归以及分类的简单实用,softmax介绍

非线性回归

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#使用numpy生成200个随机点
#生成范围[-0.5,0.5]的200个点,下面生成200行1列的数据
x_data = np.linspace(-0.5,0.5,200)[:,np.newaxis]
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) + noise


#定义两个placeholder
#下面[]里面表示行不确定,列只有一列
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])

#定义神经网络中间层
#神经元定义为[1,10],是因为输入的神经元是1个,中间层的神经元定义了10个
Weight_L1 = tf.Variable(tf.random_normal([1,10]))
biases_L1 = tf.Variable(tf.zeros([1,10]))
Wx_plus_b_L1 = tf.matmul(x,Weight_L1) + biases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1)

#定义神经网络输出层
Weight_L2 = tf.Variable(tf.random_normal([10,1]))
biases_L2 = tf.Variable(tf.zeros([1,1]))
Wx_plus_b_L2 = tf.matmul(L1,Weight_L2) + biases_L2
prediction = tf.nn.tanh(Wx_plus_b_L2)


#二次代价函数
loss = tf.reduce_mean(tf.square(y-prediction))


#使用梯度下降法训练
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)


with tf.Session() as sess:
    #变量初始化
    sess.run(tf.global_variables_initializer())
    for _ in range(2000):
        sess.run(train_step,feed_dict={x:x_data,y:y_data})

    #获得预测值
    prediction_value = sess.run(prediction, feed_dict={x:x_data})

    #画图
    plt.figure()
    plt.scatter(x_data,y_data)
    plt.plot(x_data,prediction_value,'r-',lw=5)
    plt.show()

MNIST数据集分类简单版

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#载入数据集
#one_hot是将数据改成0-1的格式
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

#每个批次的大小
batch_size = 100

#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#定义两个placeholder,一个图片是28x28=784
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

#创建一个简单的神经网络
Weight_L1 = tf.Variable(tf.zeros([784,10]))
biases_L1 = tf.Variable(tf.zeros([10]))
Wx_plus_b_L1 = tf.matmul(x,Weight_L1) + biases_L1
prediction = tf.nn.softmax(Wx_plus_b_L1)



#二次代价函数
loss = tf.reduce_mean(tf.square(y-prediction))

#使用梯度下降法训练
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#初始化变量
init = tf.global_variables_initializer()

#结果存在一个布尔型的列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #argmax返回一个张量中最大值存在的位置

#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #将布尔型转换为float32, true->1 false ->0

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(21):
        for batch in range (n_batch):
            #获取每个batch的图片
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter " + str(epoch) + ",Testing Accuracy "+ str(acc))

Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
Iter 0,Testing Accuracy 0.8318
Iter 1,Testing Accuracy 0.8694
Iter 2,Testing Accuracy 0.8811
Iter 3,Testing Accuracy 0.8876
Iter 4,Testing Accuracy 0.8932
Iter 5,Testing Accuracy 0.897
Iter 6,Testing Accuracy 0.8992
Iter 7,Testing Accuracy 0.9021
Iter 8,Testing Accuracy 0.9024
Iter 9,Testing Accuracy 0.9056
Iter 10,Testing Accuracy 0.9064
Iter 11,Testing Accuracy 0.9063
Iter 12,Testing Accuracy 0.9079
Iter 13,Testing Accuracy 0.9095
Iter 14,Testing Accuracy 0.9096
Iter 15,Testing Accuracy 0.9114
Iter 16,Testing Accuracy 0.9113
Iter 17,Testing Accuracy 0.9121
Iter 18,Testing Accuracy 0.913
Iter 19,Testing Accuracy 0.9137
Iter 20,Testing Accuracy 0.9135

#第三章作业,修改神经网络使得识别率达到95%以上
#载入数据集
#one_hot是将数据改成0-1的格式
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

#每个批次的大小
batch_size = 100

#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#定义两个placeholder,一个图片是28x28=784
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

#创建一个简单的神经网络
Weight_L1 = tf.Variable(tf.zeros([784,20]))
biases_L1 = tf.Variable(tf.zeros([20]))
Wx_plus_b_L1 = tf.matmul(x,Weight_L1) + biases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1)


#定义神经网络输出层
Weight_L2 = tf.Variable(tf.random_normal([20,10]))
biases_L2 = tf.Variable(tf.zeros([10]))
Wx_plus_b_L2 = tf.matmul(L1,Weight_L2) + biases_L2
prediction = tf.nn.softmax(Wx_plus_b_L2)

#二次代价函数
loss = tf.reduce_mean(tf.square(y-prediction))

#使用梯度下降法训练
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#初始化变量
init = tf.global_variables_initializer()

#结果存在一个布尔型的列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #argmax返回一个张量中最大值存在的位置

#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #将布尔型转换为float32, true->1 false ->0

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(100):
        for batch in range (n_batch):
            #获取每个batch的图片
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter " + str(epoch) + ",Testing Accuracy "+ str(acc))

交叉熵,过拟合,dropout以及tensorflow中各种优化器的介绍

交叉熵

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#将二次代价函数替换成交叉熵
# tf.nn.sigmod_cross_entropy_with_logits() 来表示跟sigmod搭配使用的交叉熵
# tf.nn.softmax_cross_entropy_with_logits() 来表示跟softmax搭配使用的交叉熵
#载入数据集
#one_hot是将数据改成0-1的格式
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

#每个批次的大小
batch_size = 100

#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#定义两个placeholder,一个图片是28x28=784
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

#创建一个简单的神经网络
Weight_L1 = tf.Variable(tf.zeros([784,10]))
biases_L1 = tf.Variable(tf.zeros([10]))
Wx_plus_b_L1 = tf.matmul(x,Weight_L1) + biases_L1
prediction = tf.nn.softmax(Wx_plus_b_L1)



#二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))

#交叉熵
#首先看输入logits,它的shape是[batch_size, num_classes] ,一般来讲,就是神经网络最后一层的输入z。
#另外一个输入是labels,它的shape也是[batch_size, num_classes],就是我们神经网络期望的输出。
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))
#使用梯度下降法训练
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#初始化变量
init = tf.global_variables_initializer()

#结果存在一个布尔型的列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #argmax返回一个张量中最大值存在的位置

#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #将布尔型转换为float32, true->1 false ->0

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(21):
        for batch in range (n_batch):
            #获取每个batch的图片
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter " + str(epoch) + ",Testing Accuracy "+ str(acc))

Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
Iter 0,Testing Accuracy 0.8242
Iter 1,Testing Accuracy 0.8941
Iter 2,Testing Accuracy 0.9006
Iter 3,Testing Accuracy 0.9053
Iter 4,Testing Accuracy 0.9087
Iter 5,Testing Accuracy 0.9102
Iter 6,Testing Accuracy 0.9111
Iter 7,Testing Accuracy 0.9139
Iter 8,Testing Accuracy 0.9151
Iter 9,Testing Accuracy 0.9156
Iter 10,Testing Accuracy 0.9172
Iter 11,Testing Accuracy 0.9189
Iter 12,Testing Accuracy 0.919
Iter 13,Testing Accuracy 0.9186
Iter 14,Testing Accuracy 0.9213
Iter 15,Testing Accuracy 0.9216
Iter 16,Testing Accuracy 0.9209
Iter 17,Testing Accuracy 0.9212
Iter 18,Testing Accuracy 0.9216
Iter 19,Testing Accuracy 0.9211
Iter 20,Testing Accuracy 0.9218

dropout

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#将二次代价函数替换成交叉熵
# tf.nn.sigmod_cross_entropy_with_logits() 来表示跟sigmod搭配使用的交叉熵
# tf.nn.softmax_cross_entropy_with_logits() 来表示跟softmax搭配使用的交叉熵
#载入数据集
#one_hot是将数据改成0-1的格式
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

#每个批次的大小
batch_size = 100

#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#定义两个placeholder,一个图片是28x28=784
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
keep_prob = tf.placeholder(tf.float32) #dropout的参数,表示多少神经元被激活,1 表示所有神经元都是工作的

#创建一个简单的神经网络
Weight_L1 = tf.Variable(tf.truncated_normal([784,2000],stddev=0.1))
biases_L1 = tf.Variable(tf.zeros([2000])+0.1)
Wx_plus_b_L1 = tf.nn.tanh(tf.matmul(x,Weight_L1) + biases_L1)
L1_drop = tf.nn.dropout(Wx_plus_b_L1,keep_prob)


Weight_L2 = tf.Variable(tf.truncated_normal([2000,2000],stddev=0.1))
biases_L2 = tf.Variable(tf.zeros([2000])+0.1)
Wx_plus_b_L2 = tf.nn.tanh(tf.matmul(L1_drop,Weight_L2) + biases_L2)
L2_drop = tf.nn.dropout(Wx_plus_b_L2,keep_prob)


Weight_L3 = tf.Variable(tf.truncated_normal([2000,1000],stddev=0.1))
biases_L3 = tf.Variable(tf.zeros([1000])+0.1)
Wx_plus_b_L3 = tf.nn.tanh(tf.matmul(L2_drop,Weight_L3) + biases_L3)
L3_drop = tf.nn.dropout(Wx_plus_b_L3,keep_prob)

Weight_L4 = tf.Variable(tf.truncated_normal([1000,10],stddev=0.1))
biases_L4 = tf.Variable(tf.zeros([10])+0.1)
prediction = tf.nn.softmax(tf.matmul(L3_drop,Weight_L4) + biases_L4)

#二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))

#交叉熵
#首先看输入logits,它的shape是[batch_size, num_classes] ,一般来讲,就是神经网络最后一层的输入z。
#另外一个输入是labels,它的shape也是[batch_size, num_classes],就是我们神经网络期望的输出。
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))
#使用梯度下降法训练
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#初始化变量
init = tf.global_variables_initializer()

#结果存在一个布尔型的列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #argmax返回一个张量中最大值存在的位置

#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #将布尔型转换为float32, true->1 false ->0

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(21):
        for batch in range (n_batch):
            #获取每个batch的图片
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1.0})

        test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
        train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob:1.0})

        print("Iter " + str(epoch) + ",Testing Accuracy "+ str(test_acc)+", training Accuracy "+str(train_acc))

optimizer优化器

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#将二次代价函数替换成交叉熵
# tf.nn.sigmod_cross_entropy_with_logits() 来表示跟sigmod搭配使用的交叉熵
# tf.nn.softmax_cross_entropy_with_logits() 来表示跟softmax搭配使用的交叉熵
#载入数据集
#one_hot是将数据改成0-1的格式
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

#每个批次的大小
batch_size = 100

#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#定义两个placeholder,一个图片是28x28=784
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

#创建一个简单的神经网络
Weight_L1 = tf.Variable(tf.zeros([784,10]))
biases_L1 = tf.Variable(tf.zeros([10]))
Wx_plus_b_L1 = tf.matmul(x,Weight_L1) + biases_L1
prediction = tf.nn.softmax(Wx_plus_b_L1)



#二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))

#交叉熵
#首先看输入logits,它的shape是[batch_size, num_classes] ,一般来讲,就是神经网络最后一层的输入z。
#另外一个输入是labels,它的shape也是[batch_size, num_classes],就是我们神经网络期望的输出。
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))

#使用梯度下降法训练
#train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
#使用其他优化器
train_step = tf.train.AdamOptimizer(1e-2).minimize(loss)

#初始化变量
init = tf.global_variables_initializer()

#结果存在一个布尔型的列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #argmax返回一个张量中最大值存在的位置

#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #将布尔型转换为float32, true->1 false ->0

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(21):
        for batch in range (n_batch):
            #获取每个batch的图片
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter " + str(epoch) + ",Testing Accuracy "+ str(acc))

Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
Iter 0,Testing Accuracy 0.9219
Iter 1,Testing Accuracy 0.9259
Iter 2,Testing Accuracy 0.9234
Iter 3,Testing Accuracy 0.9283
Iter 4,Testing Accuracy 0.93
Iter 5,Testing Accuracy 0.9295
Iter 6,Testing Accuracy 0.9289
Iter 7,Testing Accuracy 0.931
Iter 8,Testing Accuracy 0.9324
Iter 9,Testing Accuracy 0.9294
Iter 10,Testing Accuracy 0.9309
Iter 11,Testing Accuracy 0.9313
Iter 12,Testing Accuracy 0.9297
Iter 13,Testing Accuracy 0.9319
Iter 14,Testing Accuracy 0.9295
Iter 15,Testing Accuracy 0.9304
Iter 16,Testing Accuracy 0.9323
Iter 17,Testing Accuracy 0.9324
Iter 18,Testing Accuracy 0.9314
Iter 19,Testing Accuracy 0.9318
Iter 20,Testing Accuracy 0.9307

本章作业,继续优化网络,提高识别率

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#将二次代价函数替换成交叉熵
# tf.nn.sigmod_cross_entropy_with_logits() 来表示跟sigmod搭配使用的交叉熵
# tf.nn.softmax_cross_entropy_with_logits() 来表示跟softmax搭配使用的交叉熵
#载入数据集
#one_hot是将数据改成0-1的格式
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

#每个批次的大小
batch_size = 100

#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#定义两个placeholder,一个图片是28x28=784
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
keep_prob = tf.placeholder(tf.float32) #dropout的参数,表示多少神经元被激活,1 表示所有神经元都是工作的
#学习率的变量
lr = tf.Variable(0.001,dtype = tf.float32)

#创建一个简单的神经网络
Weight_L1 = tf.Variable(tf.truncated_normal([784,500],stddev=0.1))
biases_L1 = tf.Variable(tf.zeros([500])+0.1)
Wx_plus_b_L1 = tf.nn.tanh(tf.matmul(x,Weight_L1) + biases_L1)
L1_drop = tf.nn.dropout(Wx_plus_b_L1,keep_prob)


Weight_L2 = tf.Variable(tf.truncated_normal([500,300],stddev=0.1))
biases_L2 = tf.Variable(tf.zeros([300])+0.1)
Wx_plus_b_L2 = tf.nn.tanh(tf.matmul(L1_drop,Weight_L2) + biases_L2)
L2_drop = tf.nn.dropout(Wx_plus_b_L2,keep_prob)


Weight_L3 = tf.Variable(tf.truncated_normal([300,10],stddev=0.1))
biases_L3 = tf.Variable(tf.zeros([10])+0.1)
#Wx_plus_b_L3 = tf.nn.tanh(tf.matmul(L2_drop,Weight_L3) + biases_L3)
#L3_drop = tf.nn.dropout(Wx_plus_b_L3,keep_prob)

#Weight_L4 = tf.Variable(tf.truncated_normal([300,10],stddev=0.1))
#biases_L4 = tf.Variable(tf.zeros([10])+0.1)
prediction = tf.nn.softmax(tf.matmul(L2_drop,Weight_L3) + biases_L3)

#二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))

#交叉熵
#首先看输入logits,它的shape是[batch_size, num_classes] ,一般来讲,就是神经网络最后一层的输入z。
#另外一个输入是labels,它的shape也是[batch_size, num_classes],就是我们神经网络期望的输出。
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))
#使用梯度下降法训练
#train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
#使用其他优化器
train_step = tf.train.AdamOptimizer(lr).minimize(loss)


#初始化变量
init = tf.global_variables_initializer()

#结果存在一个布尔型的列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #argmax返回一个张量中最大值存在的位置

#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #将布尔型转换为float32, true->1 false ->0

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(51):
        sess.run(tf.assign(lr,0.001 * (0.95 ** epoch)))
        for batch in range (n_batch):
            #获取每个batch的图片
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1.0})

        learning_rate = sess.run(lr)

        test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
        train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob:1.0})

        print("Iter " + str(epoch) + ",Testing Accuracy "+ str(test_acc)+", training Accuracy "+str(train_acc))

Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
Iter 0,Testing Accuracy 0.952, training Accuracy 0.955818
Iter 1,Testing Accuracy 0.9604, training Accuracy 0.9672
Iter 2,Testing Accuracy 0.9675, training Accuracy 0.976364
Iter 3,Testing Accuracy 0.9712, training Accuracy 0.982236
Iter 4,Testing Accuracy 0.9736, training Accuracy 0.985655
Iter 5,Testing Accuracy 0.9751, training Accuracy 0.988509
Iter 6,Testing Accuracy 0.9755, training Accuracy 0.989909
Iter 7,Testing Accuracy 0.9752, training Accuracy 0.990855
Iter 8,Testing Accuracy 0.9784, training Accuracy 0.992382
Iter 9,Testing Accuracy 0.977, training Accuracy 0.9936
Iter 10,Testing Accuracy 0.9782, training Accuracy 0.993345
Iter 11,Testing Accuracy 0.9764, training Accuracy 0.9944
Iter 12,Testing Accuracy 0.978, training Accuracy 0.994891
Iter 13,Testing Accuracy 0.9789, training Accuracy 0.995309
Iter 14,Testing Accuracy 0.9792, training Accuracy 0.995327
Iter 15,Testing Accuracy 0.9787, training Accuracy 0.995309
Iter 16,Testing Accuracy 0.9792, training Accuracy 0.9958
Iter 17,Testing Accuracy 0.9799, training Accuracy 0.996
Iter 18,Testing Accuracy 0.9801, training Accuracy 0.995927
Iter 19,Testing Accuracy 0.9806, training Accuracy 0.9962
Iter 20,Testing Accuracy 0.9805, training Accuracy 0.996055
Iter 21,Testing Accuracy 0.9815, training Accuracy 0.9964
Iter 22,Testing Accuracy 0.9808, training Accuracy 0.996545
Iter 23,Testing Accuracy 0.9802, training Accuracy 0.996273
Iter 24,Testing Accuracy 0.982, training Accuracy 0.996618
Iter 25,Testing Accuracy 0.9814, training Accuracy 0.996709
Iter 26,Testing Accuracy 0.9808, training Accuracy 0.996727
Iter 27,Testing Accuracy 0.9815, training Accuracy 0.9968
Iter 28,Testing Accuracy 0.9813, training Accuracy 0.996836
Iter 29,Testing Accuracy 0.9818, training Accuracy 0.997018
Iter 30,Testing Accuracy 0.9819, training Accuracy 0.997036
Iter 31,Testing Accuracy 0.9818, training Accuracy 0.997091
Iter 32,Testing Accuracy 0.9822, training Accuracy 0.997145
Iter 33,Testing Accuracy 0.9812, training Accuracy 0.9972
Iter 34,Testing Accuracy 0.9813, training Accuracy 0.997255
Iter 35,Testing Accuracy 0.9822, training Accuracy 0.997273
Iter 36,Testing Accuracy 0.9817, training Accuracy 0.997309
Iter 37,Testing Accuracy 0.9824, training Accuracy 0.997345
Iter 38,Testing Accuracy 0.9809, training Accuracy 0.997345
Iter 39,Testing Accuracy 0.9819, training Accuracy 0.997364
Iter 40,Testing Accuracy 0.981, training Accuracy 0.997236
Iter 41,Testing Accuracy 0.9818, training Accuracy 0.9974
Iter 42,Testing Accuracy 0.9821, training Accuracy 0.997418
Iter 43,Testing Accuracy 0.9816, training Accuracy 0.997473
Iter 44,Testing Accuracy 0.9815, training Accuracy 0.997473
Iter 45,Testing Accuracy 0.9817, training Accuracy 0.997491
Iter 46,Testing Accuracy 0.9817, training Accuracy 0.997491
Iter 47,Testing Accuracy 0.9819, training Accuracy 0.997491
Iter 48,Testing Accuracy 0.9819, training Accuracy 0.997509
Iter 49,Testing Accuracy 0.9819, training Accuracy 0.997527
Iter 50,Testing Accuracy 0.9818, training Accuracy 0.997527

tensorboard可视化

tensorboard网络结构

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#以3-2的程序为例介绍tensorboard的用法
#载入数据集
#one_hot是将数据改成0-1的格式
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

#每个批次的大小
batch_size = 100

#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size


#*****要可视化网络结果,必须要用到命名空间***********
with tf.name_scope('input'):
    #定义两个placeholder,一个图片是28x28=784
    x = tf.placeholder(tf.float32,[None,784],name='x-input')
    y = tf.placeholder(tf.float32,[None,10],name='y-input')

#创建一个简单的神经网络
with tf.name_scope('layer'):
    with tf.name_scope('wights'):
        Weight_L1 = tf.Variable(tf.zeros([784,10]),name='W')
    with tf.name_scope('biases'):
        biases_L1 = tf.Variable(tf.zeros([10]),name='b')
    with tf.name_scope('wx_plus_b'):
        Wx_plus_b_L1 = tf.matmul(x,Weight_L1) + biases_L1
    with tf.name_scope('softmax'):
        prediction = tf.nn.softmax(Wx_plus_b_L1)



#二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))
with tf.name_scope('loss'):
    loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))
#使用梯度下降法训练
with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#初始化变量
init = tf.global_variables_initializer()

#结果存在一个布尔型的列表中
with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
        correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #argmax返回一个张量中最大值存在的位置
    with tf.name_scope('accuracy'):
        #求准确率
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #将布尔型转换为float32, true->1 false ->0

with tf.Session() as sess:
    sess.run(init)
    writer = tf.summary.FileWriter('C:/Users/zgyxf183/Desktop/logs',sess.graph)
    for epoch in range(1):
        for batch in range (n_batch):
            #获取每个batch的图片
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter " + str(epoch) + ",Testing Accuracy "+ str(acc))

tensorboard网络运行

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

#每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#参数概要
def variable_summaries(var):
    with tf.name_scope('summaries'):
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean', mean)#平均值
        with tf.name_scope('stddev'):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
        tf.summary.scalar('stddev', stddev)#标准差
        tf.summary.scalar('max', tf.reduce_max(var))#最大值
        tf.summary.scalar('min', tf.reduce_min(var))#最小值
        tf.summary.histogram('histogram', var)#直方图

#命名空间
with tf.name_scope('input'):
    #定义两个placeholder
    x = tf.placeholder(tf.float32,[None,784],name='x-input')
    y = tf.placeholder(tf.float32,[None,10],name='y-input')

with tf.name_scope('layer'):
    #创建一个简单的神经网络
    with tf.name_scope('wights'):
        W = tf.Variable(tf.zeros([784,10]),name='W')
        variable_summaries(W)
    with tf.name_scope('biases'):    
        b = tf.Variable(tf.zeros([10]),name='b')
        variable_summaries(b)
    with tf.name_scope('wx_plus_b'):
        wx_plus_b = tf.matmul(x,W) + b
    with tf.name_scope('softmax'):
        prediction = tf.nn.softmax(wx_plus_b)

#二次代价函数
# loss = tf.reduce_mean(tf.square(y-prediction))
with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
    tf.summary.scalar('loss',loss)
with tf.name_scope('train'):
    #使用梯度下降法
    train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#初始化变量
init = tf.global_variables_initializer()

with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
        #结果存放在一个布尔型列表中
        correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
    with tf.name_scope('accuracy'):
        #求准确率
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
        tf.summary.scalar('accuracy',accuracy)

#合并所有的summary
merged = tf.summary.merge_all()

with tf.Session() as sess:
    sess.run(init)
    writer = tf.summary.FileWriter('logs/',sess.graph)
    for epoch in range(51):
        for batch in range(n_batch):
            batch_xs,batch_ys =  mnist.train.next_batch(batch_size)
            summary,_ = sess.run([merged,train_step],feed_dict={x:batch_xs,y:batch_ys})

        writer.add_summary(summary,epoch)
        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))

tensorboard可视化

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib.tensorboard.plugins import projector
#载入数据集
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
#运行次数
max_steps = 1001
#图片数量
image_num = 3000
#文件路径
DIR = "C:/Users/zgyxf183/Documents/jupyter/tensorFlow Learning/"

#定义会话
sess = tf.Session()

#载入图片
embedding = tf.Variable(tf.stack(mnist.test.images[:image_num]), trainable=False, name='embedding')

#参数概要
def variable_summaries(var):
    with tf.name_scope('summaries'):
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean', mean)#平均值
        with tf.name_scope('stddev'):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
        tf.summary.scalar('stddev', stddev)#标准差
        tf.summary.scalar('max', tf.reduce_max(var))#最大值
        tf.summary.scalar('min', tf.reduce_min(var))#最小值
        tf.summary.histogram('histogram', var)#直方图

#命名空间
with tf.name_scope('input'):
    #这里的none表示第一个维度可以是任意的长度
    x = tf.placeholder(tf.float32,[None,784],name='x-input')
    #正确的标签
    y = tf.placeholder(tf.float32,[None,10],name='y-input')

#显示图片
with tf.name_scope('input_reshape'):
    image_shaped_input = tf.reshape(x, [-1, 28, 28, 1])
    tf.summary.image('input', image_shaped_input, 10)

with tf.name_scope('layer'):
    #创建一个简单神经网络
    with tf.name_scope('weights'):
        W = tf.Variable(tf.zeros([784,10]),name='W')
        variable_summaries(W)
    with tf.name_scope('biases'):
        b = tf.Variable(tf.zeros([10]),name='b')
        variable_summaries(b)
    with tf.name_scope('wx_plus_b'):
        wx_plus_b = tf.matmul(x,W) + b
    with tf.name_scope('softmax'):    
        prediction = tf.nn.softmax(wx_plus_b)

with tf.name_scope('loss'):
    #交叉熵代价函数
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
    tf.summary.scalar('loss',loss)
with tf.name_scope('train'):
    #使用梯度下降法
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

#初始化变量
sess.run(tf.global_variables_initializer())

with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
        #结果存放在一个布尔型列表中
        correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
    with tf.name_scope('accuracy'):
        #求准确率
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))#把correct_prediction变为float32类型
        tf.summary.scalar('accuracy',accuracy)

#产生metadata文件
if tf.gfile.Exists(DIR + 'projector/projector/metadata.tsv'):
    tf.gfile.DeleteRecursively(DIR + 'projector/projector/metadata.tsv')
with open(DIR + 'projector/projector/metadata.tsv', 'w') as f:
    labels = sess.run(tf.argmax(mnist.test.labels[:],1))
    for i in range(image_num):   
        f.write(str(labels[i]) + '\n')        

#合并所有的summary
merged = tf.summary.merge_all()   


projector_writer = tf.summary.FileWriter(DIR + 'projector/projector',sess.graph)
saver = tf.train.Saver()
config = projector.ProjectorConfig()
embed = config.embeddings.add()
embed.tensor_name = embedding.name
embed.metadata_path = DIR + 'projector/projector/metadata.tsv'
embed.sprite.image_path = DIR + 'projector/data/mnist_10k_sprite.png'
embed.sprite.single_image_dim.extend([28,28])
projector.visualize_embeddings(projector_writer,config)

for i in range(max_steps):
    #每个批次100个样本
    batch_xs,batch_ys = mnist.train.next_batch(100)
    run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
    run_metadata = tf.RunMetadata()
    summary,_ = sess.run([merged,train_step],feed_dict={x:batch_xs,y:batch_ys},options=run_options,run_metadata=run_metadata)
    projector_writer.add_run_metadata(run_metadata, 'step%03d' % i)
    projector_writer.add_summary(summary, i)

    if i%100 == 0:
        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print ("Iter " + str(i) + ", Testing Accuracy= " + str(acc))

saver.save(sess, DIR + 'projector/projector/a_model.ckpt', global_step=max_steps)
projector_writer.close()
sess.close()

卷积神经网络

卷积神经网络应用于MNIST数据集分类

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#将二次代价函数替换成交叉熵
# tf.nn.sigmod_cross_entropy_with_logits() 来表示跟sigmod搭配使用的交叉熵
# tf.nn.softmax_cross_entropy_with_logits() 来表示跟softmax搭配使用的交叉熵
#载入数据集
#one_hot是将数据改成0-1的格式
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

#每个批次的大小
batch_size = 100

#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#初始化权值
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')



#定义两个placeholder,一个图片是28x28=784
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

#改变x的格式转为4D的向量[batch, in_height, in_width, in_channels]`
#输入的数据是784行的一列数据,要转换成 28x28
x_image = tf.reshape(x,[-1,28,28,1])

#初始化第一个卷积层的权值和偏置
W_conv1 = weight_variable([5,5,1,32])#5*5的采样窗口,32个卷积核从1个平面抽取特征
b_conv1 = bias_variable([32])#每一个卷积核一个偏置值


#把x_image和权值向量进行卷积,再加上偏置值,然后应用于relu激活函数
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

#初始化第二个卷积层的权值和偏置
W_conv2 = weight_variable([5,5,32,64])#5*5的采样窗口,64个卷积核从32个平面抽取特征
b_conv2 = bias_variable([64])#每一个卷积核一个偏置值

#把h_pool1和权值向量进行卷积,再加上偏置值,然后应用于relu激活函数
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)#进行max-pooling

#28*28的图片第一次卷积后还是28*28,第一次池化后变为14*14
#第二次卷积后为14*14,第二次池化后变为了7*7
#进过上面操作后得到64张7*7的平面



#初始化第一个全连接层的权值
W_fc1 = weight_variable([7*7*64,1024])#上一层有7*7*64个神经元,全连接层有1024个神经元
b_fc1 = bias_variable([1024])#1024个节点


#把池化层2的输出扁平化为1维
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)


#keep_prob用来表示神经元的输出概率
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)

#初始化第二个全连接层
W_fc2 = weight_variable([1024,10])
b_fc2 = bias_variable([10])

#计算输出
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2) + b_fc2)

#二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))

#交叉熵
#首先看输入logits,它的shape是[batch_size, num_classes] ,一般来讲,就是神经网络最后一层的输入z。
#另外一个输入是labels,它的shape也是[batch_size, num_classes],就是我们神经网络期望的输出。
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))
#使用梯度下降法训练
#train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
#使用其他优化器
train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)


#初始化变量
init = tf.global_variables_initializer()

#结果存在一个布尔型的列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #argmax返回一个张量中最大值存在的位置

#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #将布尔型转换为float32, true->1 false ->0

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(51):

        for batch in range (n_batch):
            #获取每个batch的图片
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.7})

        #learning_rate = sess.run(lr)

        test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
        train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob:1.0})

        print("Iter " + str(epoch) + ",Testing Accuracy "+ str(test_acc)+", training Accuracy "+str(train_acc))

作业,卷积神经网络可视化

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data',one_hot=True)

#每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#参数概要
def variable_summaries(var):
    with tf.name_scope('summaries'):
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean', mean)#平均值
        with tf.name_scope('stddev'):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
        tf.summary.scalar('stddev', stddev)#标准差
        tf.summary.scalar('max', tf.reduce_max(var))#最大值
        tf.summary.scalar('min', tf.reduce_min(var))#最小值
        tf.summary.histogram('histogram', var)#直方图

#初始化权值
def weight_variable(shape,name):
    initial = tf.truncated_normal(shape,stddev=0.1)#生成一个截断的正态分布
    return tf.Variable(initial,name=name)

#初始化偏置
def bias_variable(shape,name):
    initial = tf.constant(0.1,shape=shape)
    return tf.Variable(initial,name=name)

#卷积层
def conv2d(x,W):
    #x input tensor of shape `[batch, in_height, in_width, in_channels]`
    #W filter / kernel tensor of shape [filter_height, filter_width, in_channels, out_channels]
    #`strides[0] = strides[3] = 1`. strides[1]代表x方向的步长,strides[2]代表y方向的步长
    #padding: A `string` from: `"SAME", "VALID"`
    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')

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

#命名空间
with tf.name_scope('input'):
    #定义两个placeholder
    x = tf.placeholder(tf.float32,[None,784],name='x-input')
    y = tf.placeholder(tf.float32,[None,10],name='y-input')
    with tf.name_scope('x_image'):
        #改变x的格式转为4D的向量[batch, in_height, in_width, in_channels]`
        x_image = tf.reshape(x,[-1,28,28,1],name='x_image')


with tf.name_scope('Conv1'):
    #初始化第一个卷积层的权值和偏置
    with tf.name_scope('W_conv1'):
        W_conv1 = weight_variable([5,5,1,32],name='W_conv1')#5*5的采样窗口,32个卷积核从1个平面抽取特征
    with tf.name_scope('b_conv1'):  
        b_conv1 = bias_variable([32],name='b_conv1')#每一个卷积核一个偏置值

    #把x_image和权值向量进行卷积,再加上偏置值,然后应用于relu激活函数
    with tf.name_scope('conv2d_1'):
        conv2d_1 = conv2d(x_image,W_conv1) + b_conv1
    with tf.name_scope('relu'):
        h_conv1 = tf.nn.relu(conv2d_1)
    with tf.name_scope('h_pool1'):
        h_pool1 = max_pool_2x2(h_conv1)#进行max-pooling

with tf.name_scope('Conv2'):
    #初始化第二个卷积层的权值和偏置
    with tf.name_scope('W_conv2'):
        W_conv2 = weight_variable([5,5,32,64],name='W_conv2')#5*5的采样窗口,64个卷积核从32个平面抽取特征
    with tf.name_scope('b_conv2'):  
        b_conv2 = bias_variable([64],name='b_conv2')#每一个卷积核一个偏置值

    #把h_pool1和权值向量进行卷积,再加上偏置值,然后应用于relu激活函数
    with tf.name_scope('conv2d_2'):
        conv2d_2 = conv2d(h_pool1,W_conv2) + b_conv2
    with tf.name_scope('relu'):
        h_conv2 = tf.nn.relu(conv2d_2)
    with tf.name_scope('h_pool2'):
        h_pool2 = max_pool_2x2(h_conv2)#进行max-pooling

#28*28的图片第一次卷积后还是28*28,第一次池化后变为14*14
#第二次卷积后为14*14,第二次池化后变为了7*7
#进过上面操作后得到64张7*7的平面

with tf.name_scope('fc1'):
    #初始化第一个全连接层的权值
    with tf.name_scope('W_fc1'):
        W_fc1 = weight_variable([7*7*64,1024],name='W_fc1')#上一场有7*7*64个神经元,全连接层有1024个神经元
    with tf.name_scope('b_fc1'):
        b_fc1 = bias_variable([1024],name='b_fc1')#1024个节点

    #把池化层2的输出扁平化为1维
    with tf.name_scope('h_pool2_flat'):
        h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64],name='h_pool2_flat')
    #求第一个全连接层的输出
    with tf.name_scope('wx_plus_b1'):
        wx_plus_b1 = tf.matmul(h_pool2_flat,W_fc1) + b_fc1
    with tf.name_scope('relu'):
        h_fc1 = tf.nn.relu(wx_plus_b1)

    #keep_prob用来表示神经元的输出概率
    with tf.name_scope('keep_prob'):
        keep_prob = tf.placeholder(tf.float32,name='keep_prob')
    with tf.name_scope('h_fc1_drop'):
        h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob,name='h_fc1_drop')

with tf.name_scope('fc2'):
    #初始化第二个全连接层
    with tf.name_scope('W_fc2'):
        W_fc2 = weight_variable([1024,10],name='W_fc2')
    with tf.name_scope('b_fc2'):    
        b_fc2 = bias_variable([10],name='b_fc2')
    with tf.name_scope('wx_plus_b2'):
        wx_plus_b2 = tf.matmul(h_fc1_drop,W_fc2) + b_fc2
    with tf.name_scope('softmax'):
        #计算输出
        prediction = tf.nn.softmax(wx_plus_b2)

#交叉熵代价函数
with tf.name_scope('cross_entropy'):
    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction),name='cross_entropy')
    tf.summary.scalar('cross_entropy',cross_entropy)

#使用AdamOptimizer进行优化
with tf.name_scope('train'):
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

#求准确率
with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
        #结果存放在一个布尔列表中
        correct_prediction = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))#argmax返回一维张量中最大的值所在的位置
    with tf.name_scope('accuracy'):
        #求准确率
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
        tf.summary.scalar('accuracy',accuracy)

#合并所有的summary
merged = tf.summary.merge_all()

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    train_writer = tf.summary.FileWriter('logs/train',sess.graph)
    test_writer = tf.summary.FileWriter('logs/test',sess.graph)
    for i in range(1001):
        #训练模型
        batch_xs,batch_ys =  mnist.train.next_batch(batch_size)
        sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.5})
        #记录训练集计算的参数
        summary = sess.run(merged,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1.0})
        train_writer.add_summary(summary,i)
        #记录测试集计算的参数
        batch_xs,batch_ys =  mnist.test.next_batch(batch_size)
        summary = sess.run(merged,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1.0})
        test_writer.add_summary(summary,i)

        if i%100==0:
            test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
            train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images[:10000],y:mnist.train.labels[:10000],keep_prob:1.0})
            print ("Iter " + str(i) + ", Testing Accuracy= " + str(test_acc) + ", Training Accuracy= " + str(train_acc))

递归神经网络LSTM的讲解,以及LSTM网络的使用

递归神经网络RNN

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#载入数据集
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)

# 输入图片是28*28
n_inputs = 28 #输入一行,一行有28个数据
max_time = 28 #一共28行
lstm_size = 100 #隐层单元
n_classes = 10 # 10个分类
batch_size = 50 #每批次50个样本
n_batch = mnist.train.num_examples // batch_size #计算一共有多少个批次

#这里的none表示第一个维度可以是任意的长度
x = tf.placeholder(tf.float32,[None,784])
#正确的标签
y = tf.placeholder(tf.float32,[None,10])

#初始化权值
weights = tf.Variable(tf.truncated_normal([lstm_size, n_classes], stddev=0.1))
#初始化偏置值
biases = tf.Variable(tf.constant(0.1, shape=[n_classes]))


#定义RNN网络
def RNN(X,weights,biases):
    # inputs=[batch_size, max_time, n_inputs]
    inputs = tf.reshape(X,[-1,max_time,n_inputs])
    #定义LSTM基本CELL
    lstm_cell = tf.contrib.rnn.core_rnn_cell.BasicLSTMCell(lstm_size)
#    final_state[state, batch_size, cell.state_size]
#    final_state[0]是cell state
#    final_state[1]是hidden_state
#    outputs: The RNN output `Tensor`.
#       If time_major == False (default), this will be a `Tensor` shaped:
#         `[batch_size, max_time, cell.output_size]`.
#       If time_major == True, this will be a `Tensor` shaped:
#         `[max_time, batch_size, cell.output_size]`.
    outputs,final_state = tf.nn.dynamic_rnn(lstm_cell,inputs,dtype=tf.float32)
    results = tf.nn.softmax(tf.matmul(final_state[1],weights) + biases)
    return results


#计算RNN的返回结果
prediction= RNN(x, weights, biases)  
#损失函数
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))
#使用AdamOptimizer进行优化
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
#结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))#把correct_prediction变为float32类型
#初始化
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(6):
        for batch in range(n_batch):
            batch_xs,batch_ys =  mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print ("Iter " + str(epoch) + ", Testing Accuracy= " + str(acc))
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
Iter 0, Testing Accuracy= 0.7221
Iter 1, Testing Accuracy= 0.8016
Iter 2, Testing Accuracy= 0.8763
Iter 3, Testing Accuracy= 0.9103
Iter 4, Testing Accuracy= 0.9223
Iter 5, Testing Accuracy= 0.9311
发布了21 篇原创文章 · 获赞 10 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Tracy_Yuan2014/article/details/79183738
今日推荐