zoukankan      html  css  js  c++  java
  • 学习进度笔记10

    观看Tensorflow案例实战视频课程10 训练神经网络

    def multilayer_perceptron(_X,_weights,_biases):
        layer_1=tf.nn.sigmoid(tf.add(tf.matmul(_X,_weights['w1']),_biases['b1']))
        layer_2=tf.nn.sigmoid(tf.add(tf.matmul(layer_1,_weights['w2']),_biases['b2']))
        return (tf.matmul(layer_2,_weights['out'])+_biases['out'])
    #PREDICTION
    pred=multilayer_perceptron(x,weights,biases)
    
    #LOSS AND OPTIMIZER
    cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred,y))
    optm=tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(cost)
    corr=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
    accr=tf.reduce_mean(tf.cast(corr,"float"))
    
    #INITIALIZER
    init=tf.global_variables_initializer()
    print("FUNCTIONS READY")
    
    training_epochs=20
    batch_size=100
    display_step=4
    #LAUNCH THE GRAPH
    sess=tf.Session()
    sess.run(init)
    #OPTIMIZE
    for epoch in range(training_epochs):
        avg_cost=0
        total_batch=int(mnist.train.num_examples/batch_size)
        #ITERATION
        for i in range(total_batch):
           batch_xs,batch_ys=mnist.train.next_batch(batch_size)
           feeds={x:batch_xs,y:batch_ys}
           sess.run(optm,feed_dict=feeds)
           avg_cost+=sess.run(cost,feed_dict=feeds)
        avg_cost=avg_cost/total_batch
        #DISPLAY
        if (epoch+1) % display_step==0:
            print("Epoch:%03d/%03d cost:%.9f" % (epoch,training_epochs,avg_cost))
            feeds={x:batch_xs,y:batch_ys}
            train_acc=sess.run(accr,feed_dict=feeds)
            print("TRAIN ACCURACY:%.3f" % (train_acc))
            feeds={x:mnist.test.images,y:mnist.test.labels}
            test_acc=sess.run(accr,feed_dict=feeds)
            print("TEST ACCURACY:%.3f" % (test_acc))
    print("OPTIMIZATION FINISHED")
  • 相关阅读:
    window7 上创建定时任务来运行自动化脚本
    初试接口测试
    list tuple dict (列表,元祖,字典间的相互转换)
    防止忘记的一些博客
    [python] 常用正则表达式爬取网页信息及分析HTML标签总结
    python正则表达式提取字符串
    关于json的dump和dumps
    三月23日测试Fiddler
    第六章 部署Python开发的web业务
    第五节 Nginx集群
  • 原文地址:https://www.cnblogs.com/zql-42/p/14624595.html
Copyright © 2011-2022 走看看