zoukankan      html  css  js  c++  java
  • 培训课件-第二天上午

    #coding=utf-8
    import tensorflow as tf
    import numpy as np
    import matplotlib .pyplot as plt
    from tensorflow .examples .tutorials .mnist import input_data


    #下载数据集
    mnist=input_data .read_data_sets ("./data_mnist/",one_hot= True )

    #假设值参数
    batch_size=100
    iter=np.int(mnist .train.images.shape[0]/batch_size )
    print(iter )

    train_state=True

    #定义指数衰减学习率


    LEARNING_REATE_BASE=0.1
    LEARNING_RATE_step=250
    LEARNING_RAETE_DECAY=0.99


    global_step=tf.Variable (initial_value= 0,trainable= False )
    learning_rate=tf.train.exponential_decay(learning_rate=LEARNING_REATE_BASE,
    global_step= global_step ,
    decay_steps= LEARNING_RATE_step ,
    decay_rate=LEARNING_RAETE_DECAY ,
    staircase= True )



    #定义droupout


    keep_dropout=0.85


    #将加载的数据显示一下

    # image,label=mnist.train.next_batch(1)
    # image=image.reshape(28,28)
    # print(image.shape)
    # print(label)
    #
    # plt.figure()
    # plt.imshow (image)
    # plt.show()


    #定义一个BP网络

    #定义一个



    #定义网络的输入变量
    x_input=tf.placeholder ( shape= [None,784],dtype= tf.float32 )

    y_input=tf.placeholder (shape= [None,10],dtype= tf.float32 )



    #定义输入层

    W1=tf.Variable (tf.truncated_normal (shape= [784,1000],stddev= 1,seed= 1))
    b1=tf.Variable (tf.constant (shape= [1000],value=0.1))
    L1=tf.nn.tanh(tf.matmul (x_input ,W1 )+b1 )


    #定义隐含层

    W2=tf.Variable (tf.truncated_normal (shape=[1000,100],stddev= 1,seed=1))
    b2=tf.Variable (tf.constant (shape=[100],value=0.1))
    L2=tf.nn.tanh(tf.matmul (L1 ,W2)+b2)


    h_dropout=tf.nn.dropout (L2,keep_prob= keep_dropout )

    #定义输出层
    W3=tf.Variable (tf.truncated_normal (shape= [100,10],stddev= 1,seed=1))
    b3=tf.Variable (tf.constant (shape=[10],value=0.1))
    prediction=tf.nn.softmax (tf.matmul (h_dropout,W3)+b3 )



    #求准确率
    correct_predict=tf.equal (tf.argmax (prediction ,axis= 1),tf.argmax (y_input ,axis= 1))
    acc=tf.reduce_mean (tf.cast(correct_predict ,dtype= tf.float32 ))




    #定义反向传导过程
    #定义损失函数
    cross_entropy=-tf.reduce_mean (y_input *tf.log(prediction ))
    train_step=tf.train.GradientDescentOptimizer (learning_rate).minimize(cross_entropy ,global_step= global_step )



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



    #保存模型

    saver=tf.train.Saver ()


    #绘图

    with tf.Session () as sess:
    sess.run(init )
    if(train_state ):
    for i in range(150):
    X,Y=mnist .test.next_batch(100)
    xt, yt = mnist.train.next_batch(100)
    for j in range(iter ):
    xs,ys=mnist.train.next_batch(batch_size )
    sess.run(train_step,feed_dict= {x_input :xs,y_input :ys})
    acc1,learning_rate1= sess.run([acc ,learning_rate],feed_dict={x_input: X, y_input: Y})
    acc2_train = sess.run(acc, feed_dict={x_input: xt, y_input: yt})
    # print("acc=,lea_rate=",acc1,learning_rate1)
    print("test_acc= train_acc=", acc1, acc2_train)
    saver .save(sess,save_path= "./model/save_net.ckpt")
    else:
    saver=tf.train.import_meta_graph(meta_graph_or_file= "./model/save_net.ckpt.meta")
    saver .restore(sess,save_path= "./model/save_net.ckpt")
    X, Y = mnist.test.next_batch(100)
    xt,yt=mnist.train.next_batch (100)
    acc2_test = sess.run(acc, feed_dict={x_input: X, y_input: Y})
    acc2_train= sess.run(acc, feed_dict={x_input: xt, y_input: yt})
    print("test_acc= train_acc=",acc2_test,acc2_train)
  • 相关阅读:
    [注]打动我的50句广告语
    [SD.TEAM语录]AC语录
    [SD.TEAM语录]阿翔语录
    [安卓基础] 009.组件Activity详解
    [Python自学] day-21 (1) (请求信息、html模板继承与导入、自定义模板函数、自定义分页)
    [Python自学] day-20 (Django-ORM、Ajax)
    [Python自学] day-19 (2) (Django-ORM)
    [Python自学] day-19 (1) (FBV和CBV、路由系统)
    [Python自学] day-18 (2) (MTV架构、Django框架、模板语言)
    [Python自学] day-18 (1) (JS正则、第三方组件)
  • 原文地址:https://www.cnblogs.com/shuimuqingyang/p/11115886.html
Copyright © 2011-2022 走看看