对书中用leaky_relu激活函数训练DNN示例代码的分析

示例代码来自书中,有部分进行了修改:
加载数据集改为从本地导入
引入可视化,查看数据集

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

n_inputs=28*28 #mnist数据集中图片大小为28*28=784,共784个像素点
n_hidden1=300 #第一个隐藏层神经元个数
n_hidden2=100 #第二个隐藏层神经元个数
n_outputs=10  #输出层神经元个数,MNIST数据集中分类的个数为10

X=tf.placeholder(tf.float32,shape=(None,n_inputs),name="X") #占位节点,在tensorflow进行图计算过程中通过占位节点进行传值,将数据集通过占位节点参与图计算
y=tf.placeholder(tf.int32,shape=(None),name="y")


def leaky_relu(z,name=None):
    '''tf.maximum:用法tf.maximum(a,b),返回的是a,b之间的最大值,'''
    return tf.maximum(0.01*z,z,name=name)

'定义命名空间dnn'
with tf.name_scope('dnn'):
    hidden1=tf.layers.dense(X,n_hidden1,activation=leaky_relu,name='hidden1')
    hidden2=tf.layers.dense(hidden1,n_hidden2,activation=leaky_relu,name='hidden2')
    logits=tf.layers.dense(hidden2,n_outputs,name="outputs")

with tf.name_scope("loss"):
    xentropy=tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y,logits=logits)
    loss=tf.reduce_mean(xentropy,name="loss")

learning_rate=0.01

with tf.name_scope("train"):
    optimizer=tf.train.GradientDescentOptimizer(learning_rate)
    training_op=optimizer.minimize(loss)

with tf.name_scope("eval"):
    correct=tf.nn.in_top_k(logits,y,1)
    accuracy=tf.reduce_mean(tf.cast(correct,tf.float32))

init = tf.global_variables_initializer()

saver=tf.train.Saver()

datasets_path="../../Datasets/mnist/mnist.npz"

try:
    data=np.load(datasets_path)
    X_train, y_train, X_test, y_test = data['x_train'], data['y_train'], data['x_test'], data['y_test']

    #保存一条数据,方便查看
    #np.savetxt('test.txt', X_train[0], fmt='%3d', newline='\n\n')

    # matplotlib可视化查看
    '''
    for i in range(20):
        plt.subplot(4, 5, i + 1)
        plt.imshow(X_train[i], cmap=matplotlib.cm.binary, interpolation='none')
        plt.title("Class {}".format(y_train[i]))
        #plt.axis("off")
    #plt.show()
    '''

    #将数据归一化
    X_train=X_train.astype(np.float32).reshape(-1,28*28)/255.0
    X_test=X_test.astype(np.float32).reshape(-1,28*28)/255.0

except Exception as e:
    print('%s',e)

#将数据类型转为int32
y_train=y_train.astype(np.int32)
y_test=y_test.astype(np.int32)

#将训练集的前5000个实例作为正式数据集,该数据集不用来训练模型,放在最后用来评估模型
#5000以后的作为训练集,对模型进行训练
X_valid,X_train=X_train[:5000],X_train[5000:]
y_valid,y_train=y_train[:5000],y_train[5000:]

def shuffle_batch(X,y,batch_size):
    rnd_idx=np.random.permutation(len(X))
    n_batches=len(X) // batch_size
    for batch_idx in np.array_split(rnd_idx,n_batches):
        X_batch,y_batch=X[batch_idx],y[batch_idx]
        yield X_batch,y_batch #关键字yield的作用是返回一个生成器对象(迭代器)

n_epochs=40 #迭代次数
batch_size=50 #批次大小

with tf.Session() as sess:
    init.run()
    for epoch in range(n_epochs):
        for X_batch,y_batch in shuffle_batch(X_train,y_train,batch_size=batch_size):
            sess.run(training_op,feed_dict={X:X_batch,y:y_batch})

        if epoch % 5 ==0:
            acc_batch=accuracy.eval(feed_dict={X:X_batch,y:y_batch})
            acc_valid=accuracy.eval(feed_dict={X:X_valid,y:y_valid})
            print(epoch,"Batch accuracy:",acc_batch,"Validation accuracy:",acc_valid)

    save_path=saver.save(sess,"../checkpoint/leaky_relu_dnn/my_model_final.ckpt")

关于tf.maximum

tensorflow中tf.maximum,tf.minimum,tf.argmax,tf.argmain的对比
https://blog.csdn.net/uestc_c2_403/article/details/74370409

你可能感兴趣的:(Machine,Learning,Python)