TensorFlow 实战谷歌深度学习框架

TensorFlow 实战谷歌深度学习框架

  1. 实战谷歌深度学习框架5.2.1
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
INPUT_NODE=784
OUT_NODE=10
LAYER1_NODE=500
BATCH_SIZE=100
LEARNING_RATE_BASE=0.8
LEARNING_RATE_DECAY=0.99
REGULATION_RATE=0.0001
TRAING_STEPS=30000
MOVING_AVERAGE_DECAY=0.99
def inference(input_tensor,avg_class,weight1,biases1,weight2,biases2):
    if avg_class==None:
        layer1=tf.nn.relu(tf.matmul(input_tensor,weight1)+biases1)
        return tf.matmul(layer1,weight2)+biases2
    else:
        layer1=tf.nn.relu(tf.matmul(input_tensor,avg_class.average(weight1))+avg_class.average(biases1))
        return tf.matmul(layer1,avg_class.average(weight2))+avg_class.average(biases2)
    定义训练过程
def train(mnist):
    x=tf.placeholder(tf.float32,shape=[None,INPUT_NODE],name="x-input")
    y_=tf.placeholder(tf.float32,shape=[None,OUT_NODE])
    weight1=tf.Variable(tf.truncated_normal([INPUT_NODE,LAYER1_NODE],stddev=0.1))
    biases1=tf.Variable(tf.constant(0.1,shape=[LAYER1_NODE]))
    weight2=tf.Variable(tf.truncated_normal([LAYER1_NODE,OUT_NODE],stddev=0.1))
    biases2=tf.Variable(tf.constant(0.1,shape=[OUT_NODE]))
    y=inference(x,None,weight1,biases1,weight2,biases2)
    global_step=tf.Variable(0,trainable=False)
    variable_averages=tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step)
    variables_averages_op= variable_averages.apply(tf.trainable_variables())
    average_y=inference(x,variable_averages,weight1,biases1,weight2,biases2)
    cross_entropy=tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y,labels=tf.argmax(y_ ,1))
    cross_entropy_mean=tf.reduce_mean(cross_entropy)
    regularizer=tf.contrib.layers.l2_regularizer(REGULATION_RATE)
    REGULARIZATION=regularizer(weight1)+regularizer(weight2)
    loss= cross_entropy_mean + REGULARIZATION
    learning_rate=tf.train.exponential_decay(LEARNING_RATE_DECAY,
                                                LEARNING_RATE_BASE,
                                                global_step,
                                                mnist.train.num_examples/BATCH_SIZE,
                                                LEARNING_RATE_DECAY)
    train_step=tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step=global_step)
    train_op=tf.group(train_step,variables_averages_op)
    current_prediction=tf.equal(tf.argmax(average_y,1),tf.argmax(y_,1))
    accuracy=tf.reduce_mean(tf.cast(current_prediction,tf.float32))
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        validate_feed={x:mnist.validation.images,y_:mnist.validation.labels}
        test_feed={x:mnist.test.images,y_:mnist.test.labels}
        for i in range(TRAING_STEPS):
            if i % 1000 ==0:
                validate_acc=sess.run(accuracy,feed_dict=validate_feed)
                print("after %d training steps,test accuracy model is %g"%(i,validate_acc))
            xs,ys=mnist.train.next_batch(BATCH_SIZE)
            sess.run(train_op, feed_dict={x:xs,y_:ys})
        test_acc=sess.run(accuracy,feed_dict=test_feed)
        print("after %d training steps,the current accuracy is %g"%(TRAING_STEPS,test_acc))
def main(argv=None):
    mnist=input_data.read_data_sets("/tmp/data",one_hot=True)
    train(mnist)

if __name__ == '__main__':
    tf.app.run()
after 0 training steps,test accuracy model is 0.1326
after 1000 training steps,test accuracy model is 0.0958
after 2000 training steps,test accuracy model is 0.0958
after 3000 training steps,test accuracy model is 0.0958
after 4000 training steps,test accuracy model is 0.0958
after 5000 training steps,test accuracy model is 0.0958
after 6000 training steps,test accuracy model is 0.0958
after 7000 training steps,test accuracy model is 0.0958
after 8000 training steps,test accuracy model is 0.0958
after 9000 training steps,test accuracy model is 0.0958
after 10000 training steps,test accuracy model is 0.0958
after 11000 training steps,test accuracy model is 0.0958
after 12000 training steps,test accuracy model is 0.0958
after 13000 training steps,test accuracy model is 0.0958
after 14000 training steps,test accuracy model is 0.0958
after 15000 training steps,test accuracy model is 0.0958
after 16000 training steps,test accuracy model is 0.0958
after 17000 training steps,test accuracy model is 0.0958
after 18000 training steps,test accuracy model is 0.0958
after 19000 training steps,test accuracy model is 0.0958
after 20000 training steps,test accuracy model is 0.0958
after 21000 training steps,test accuracy model is 0.0958
after 22000 training steps,test accuracy model is 0.0958
after 23000 training steps,test accuracy model is 0.0958
after 24000 training steps,test accuracy model is 0.0958
after 25000 training steps,test accuracy model is 0.0958
after 26000 training steps,test accuracy model is 0.0958
after 27000 training steps,test accuracy model is 0.0958
after 28000 training steps,test accuracy model is 0.0958
after 29000 training steps,test accuracy model is 0.0958
after 30000 training steps,the current accuracy is 0.098

Process finished with exit code 0

不知为什么,输出的结果小数点后多了一个0,有帅哥看到望指正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值