【TensorFlow快速入门系列02】进阶操作

1 Tensorboard

1.1 网络结构
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 = 64
# 一个周期批次数
n_batch = mnist.train.num_examples // batch_size

# 命名空间:输入
with tf.name_scope('input'):
    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('weights'):
        W = tf.Variable(tf.truncated_normal([784,10], stddev=0.1))
    with tf.name_scope('biases'):
        b = tf.Variable(tf.zeros([10]) + 0.1)
    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.losses.mean_squared_error(y, prediction)

# 命名空间:训练
with tf.name_scope('train'):
    train = tf.train.GradientDescentOptimizer(0.3).minimize(loss)

# 命名空间:精度
with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
        correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
    with tf.name_scope('accuracy'):
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

with tf.Session() as sess:
    # 变量初始化
    sess.run(tf.global_variables_initializer())
    # 写下网络结构日志
    writer = tf.summary.FileWriter('logs/',sess.graph) 

查看步骤:

  1. 打开cmd,切换tensorflow环境
  2. 输入命令:tensorboard --logdir=存放路径
  3. 打开浏览器,输入cmd生成的网页地址

在这里插入图片描述

1.2 记录数据
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 = 64
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'):
    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)

with tf.name_scope('loss'):
    loss = tf.losses.mean_squared_error(y, prediction)
    tf.summary.scalar('loss',loss)
    
with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(0.3).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))
    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 batch in range(5001):
        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,batch)
        if batch % 1000==0:
            acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
            print("Iter " + str(batch) + ",Testing Accuracy " + str(acc))

Iter 0,Testing Accuracy 0.2874
Iter 1000,Testing Accuracy 0.8802
Iter 2000,Testing Accuracy 0.896
Iter 3000,Testing Accuracy 0.9017
Iter 4000,Testing Accuracy 0.9058
Iter 5000,Testing Accuracy 0.9084
在这里插入图片描述

2 模型保存与载入

2.1 Checkpoint方式
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 = 64
n_batch = mnist.train.num_examples // batch_size

x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W)+b)

correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

# 定义saver用于载入模型,在指定路径下最多保留5个模型
saver = tf.train.Saver(max_to_keep=5)

loss = tf.losses.softmax_cross_entropy(y,prediction)
train_step = tf.train.AdamOptimizer(0.001).minimize(loss)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}))
    # 载入训练好的模型参数文件
    saver.restore(sess,'models/my_model.ckpt')
    print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}))
    
    for epoch in range(11):
        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))
        # 保存模型,global_step可以用来表示模型的训练次数或者训练周期数
        saver.save(sess,'models/my_model.ckpt',global_step=epoch)

Iter 0,Testing Accuracy 0.9306
Iter 1,Testing Accuracy 0.9304
Iter 2,Testing Accuracy 0.9298
Iter 3,Testing Accuracy 0.9306
Iter 4,Testing Accuracy 0.9321
Iter 5,Testing Accuracy 0.9306
Iter 6,Testing Accuracy 0.9313
Iter 7,Testing Accuracy 0.9305
Iter 8,Testing Accuracy 0.9319
Iter 9,Testing Accuracy 0.9317
Iter 10,Testing Accuracy 0.9326

在这里插入图片描述

2.2 Protocol_buffer方式
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 = 64
n_batch = mnist.train.num_examples // batch_size

x = tf.placeholder(tf.float32,[None,784], name='x-input')
y = tf.placeholder(tf.float32,[None,10], name='y-input')

W = tf.Variable(tf.truncated_normal([784,10],stddev=0.1))
b = tf.Variable(tf.zeros([10])+0.1)
prediction = tf.nn.softmax(tf.matmul(x,W)+b, name='output')

loss = tf.losses.softmax_cross_entropy(y,prediction)
train_step = tf.train.AdamOptimizer(0.001).minimize(loss, name='train')

init = tf.global_variables_initializer()

correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32), name='accuracy')

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(11):
        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))
    # 保存模型参数和结构,把变量变成常量
    # output_node_names设置可以输出的tensor
    output_graph_def = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, output_node_names=['output','accuracy'])
    # 保存模型到目录下的models文件夹中
    with tf.gfile.FastGFile('pb_models/my_model.pb',mode='wb') as f:
        f.write(output_graph_def.SerializeToString())

Iter 0,Testing Accuracy 0.9028
Iter 1,Testing Accuracy 0.9142
Iter 2,Testing Accuracy 0.9185
Iter 3,Testing Accuracy 0.923
Iter 4,Testing Accuracy 0.9249
Iter 5,Testing Accuracy 0.9262
Iter 6,Testing Accuracy 0.9278
Iter 7,Testing Accuracy 0.9276
Iter 8,Testing Accuracy 0.9284
Iter 9,Testing Accuracy 0.9292
Iter 10,Testing Accuracy 0.9287

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

mnist = input_data.read_data_sets("MNIST_data",one_hot=True)# 载入模型
with tf.gfile.FastGFile('pb_models/my_model.pb', 'rb') as f:
    # 创建一个图
    graph_def = tf.GraphDef()
    # 把模型文件载入到图中
    graph_def.ParseFromString(f.read())
    # 载入图到当前环境中
    tf.import_graph_def(graph_def, name='')with tf.Session() as sess:
    # 根据tensor的名字获取到对应的output,":0"是保存模型参数时自动加上的,所以这里也要写上
    output = sess.graph.get_tensor_by_name('output:0')
    # 根据tensor的名字获取到对应的accuracy
    accuracy = sess.graph.get_tensor_by_name('accuracy:0')
    print(sess.run(accuracy,feed_dict={'x-input:0':mnist.test.images,'y-input:0':mnist.test.labels}))

0.9287

3 CNN案例

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 = 64
n_batch = mnist.train.num_examples // batch_size

x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[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)# 卷积层
def conv2d(x,W):
    # x维度:高,宽,通道 
    # W维度:卷积核高,卷积核宽,输入通道,输出通道
    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')# x转4D:批次,高,宽,通道
x_image = tf.reshape(x,[-1,28,28,1])# 初始化第一个卷积层的权值和偏置
W_conv1 = weight_variable([5,5,1,32]) # 5*5的采样窗口,输入通道数是1,输出通道数是32
b_conv1 = bias_variable([32])# 把x_image和权值向量进行卷积,再加上偏置值,然后应用于relu激活函数,再进行max_pooling
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的采样窗口,输入通道数是32,输出通道数是64
b_conv2 = bias_variable([64])# 把h_pool1和权值向量进行卷积,再加上偏置值,然后应用于relu激活函数,再进行max_pooling
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)# 初始化第一个全连接层的权值
# 上一层输出7*7*64个神经元,全连接层有1024个神经元
W_fc1 = weight_variable([7*7*64,1024])
b_fc1 = bias_variable([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)# 交叉熵损失
cross_entropy = tf.losses.softmax_cross_entropy(y,prediction)
# 使用Adam优化器
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# 结果存放在一个布尔列表中
correct_prediction = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
# 求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for epoch in range(21):
        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,keep_prob:0.7})
​
        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
        print ("Iter " + str(epoch) + ", Testing Accuracy= " + str(acc))

Iter 0, Testing Accuracy= 0.9634
Iter 1, Testing Accuracy= 0.9775
Iter 2, Testing Accuracy= 0.9785
Iter 3, Testing Accuracy= 0.9844
Iter 4, Testing Accuracy= 0.9862
Iter 5, Testing Accuracy= 0.9863
Iter 6, Testing Accuracy= 0.9872
Iter 7, Testing Accuracy= 0.9875
Iter 8, Testing Accuracy= 0.9894
Iter 9, Testing Accuracy= 0.9898
Iter 10, Testing Accuracy= 0.9903
Iter 11, Testing Accuracy= 0.9896
Iter 12, Testing Accuracy= 0.9905
Iter 13, Testing Accuracy= 0.9909
Iter 14, Testing Accuracy= 0.9893
Iter 15, Testing Accuracy= 0.9899
Iter 16, Testing Accuracy= 0.9892
Iter 17, Testing Accuracy= 0.9913
Iter 18, Testing Accuracy= 0.9918
Iter 19, Testing Accuracy= 0.9915
Iter 20, Testing Accuracy= 0.9908

4 LSTM案例

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 
max_time = 28 
lstm_size = 100 # 隐层单元
n_classes = 10 # 10个类别
batch_size = 64 
n_batch = mnist.train.num_examples // batch_size 
​
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 = tf.reshape(X,[-1,max_time,n_inputs])
    # 定义LSTM
    lstm_cell = tf.nn.rnn_cell.LSTMCell(lstm_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
        
prediction= RNN(x, weights, biases)  

loss = tf.losses.softmax_cross_entropy(y,prediction)
train_step = tf.train.AdamOptimizer(1e-3).minimize(loss)
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

init = tf.global_variables_initializer()with tf.Session() as sess:
    sess.run(init)
    for epoch in range(11):
        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))

Iter 0, Testing Accuracy= 0.922
Iter 1, Testing Accuracy= 0.9511
Iter 2, Testing Accuracy= 0.9543
Iter 3, Testing Accuracy= 0.9663
Iter 4, Testing Accuracy= 0.97
Iter 5, Testing Accuracy= 0.9705
Iter 6, Testing Accuracy= 0.9734
Iter 7, Testing Accuracy= 0.973
Iter 8, Testing Accuracy= 0.9786
Iter 9, Testing Accuracy= 0.9745
Iter 10, Testing Accuracy= 0.9766

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值