สมัครเรียนโทร. 085-350-7540 , 084-88-00-255 , ntprintf@gmail.com

Tutorial Machine Learning

MT001 Artificial Intelligence MT002 Machine Learning MT003 8 Fun Machine Learning Projects For Beginner MT004 Optimization MT005 Mathematical Optimization MT006 Top 20 Python Machine Learning Open Source Projects MT101 Introduction to Neural Networks MT102 Introduction to Deep Learning with TensorFlow MT103 Installing TensorFlow for Deep Learning MT104 TensorFlow Creating the Neural Network Model MT105 TensorFlow How the Network will run

Deep Learning with TensorFlow - How the Network will run

หมายเหตุ: บทความนี้เป็นส่วนหนึ่งของ "บทความชุด Article with Machine Translation" ซึ่งเป็นการแปลบทความจากต้นฉบับภาษาต่างประเทศด้วย Machine translation ดังนั้นจึงมีข้อผิดพลาดอยู่หลายจุด ขอให้ผู้อ่านโปรดใช้วิจารณญาณในการอ่าน ทาง EPT ไม่ขอรับประกันว่าข้อมูลที่ท่านได้อ่านเป็นข้อมูลที่ถูกต้อง และไม่ขอรับผิดชอบความเสียหายใด ๆ ที่เกิดต่อผู้อ่านทั้งทางร่างกาย จิตใจ ทรัพย์สิน ฯลฯ นะครับ

การเรียนรู้ที่ลึกซึ้งกับ TensorFlow - วิธีที่เครือข่ายจะรัน

 

https://youtu.be/PwAGxqrXSCs?list=PLQVvvaa0QuDfKTOs3Keq_kaG2P55YRn5v

ยินดีต้อนรับสู่บทที่ 4 ของการเรียนรู้ที่ลึกซึ้งด้วยเครือข่ายประสาทและ TensorFlow และเป็นบทที่ 46 ของชุดการเรียนการสอนในเรื่องของ machine learning เรากำลังจะทำการเขียนโค้ดสำหรับอะไรที่เกิดขึ้นระหว่าง Session ใน TensorFlow 

โค้ดที่นี่ได้รับการอัปเดตให้สนับสนุน TensorFlow 1.0 แต่ในวิดีโอมี 2 บรรทัดที่จำ เป็นที่จะต้องทำการอัปเดตเล็กน้อย 

ในการเรียนการสอนก่อนหน้านี้ เราได้สร้างโมเดลสำหรับเครือข่ายประสาทเทียม ของพวกเราและ set up กราฟการคำนวณด้วย TensorFlow ตอนนี้เราต้องการที่จะ set up การดำเนินการฝึกซึ่งจะเป็นสิ่งที่ถูกรันใน TensorFlow Session
ดำเนินการต่อในโค้ดของพวกเรา :

def train_neural_network(x):
    prediction = neural_network_model(x)
    cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y) )

 

ภายใต้ฟังก์ชั่นใหม่ คือ train_neural_network เราจะส่งผ่านข้อมูล เราจะผลิตการ ทำนายที่มีพื้นฐานอยู่บน output ของข้อมูลนั้นที่ผ่าน neural_network_model ของ พวกเรา ต่อไป เราจะสร้างตัวแปร cost สิ่งนี้จะวัดว่าเราผิดมากแค่ไหน และเป็นตัวแปรที่เราต้องการลดโดยการควบคุมน้ำหนักของพวกเรา ฟังก์ชั่น cost คือ ตัวที่เหมือนกันกับฟังก์ชั่น loss เพื่อที่จะเพิ่มประสิทธิภาพ cost ของเรา เราจะใช้ AdamOptimizer ซึ่งเป็นตัวเพิ่มประสิทธิภาพที่ได้รับความนิยมพร้อมกับ ตัวอื่นๆ ยกตัวอย่าง เช่น Stochastic Gradient Descent และ AdaGrad 

ภายใน AdamOptimizer() คุณสามารถระบุ learning_rate เป็น parameter ได้ ค่าเริ่มต้น คือ 0.001 ซึ่งมันจะโอเคสำหรับสภาพการณ์โดยส่วนใหญ่ ตอนนี้เราได้ ทำการกำหนดสิ่งเหล่านี้เรียบร้อยแล้ว เรากำลังจะไปเริ่มต้น session

อย่างแรก เรามีตัวแปร hm_epochs ที่รวดเร็ว ซึ่งจะกำหนดว่าต้องมี epochs กี่อัน (วัฏจักรของ feed forward และ back prop) ต่อไป เราจะใช้งานไวยากรณ์ with สำหรับการเปิดและการปิด session ของเราที่ได้ทำการถกเถียงกันในการเรียน การสอนก่อนหน้านี้ ในการเริ่มต้น เราจะเขียนชื่อตัวแปรทั้งหมดของเรา ต่อไปมา ถึงขั้นตอนสำคัญ :

for epoch in range(hm_epochs):
            epoch_loss = 0
            for _ in range(int(mnist.train.num_examples/batch_size)):
                epoch_x, epoch_y = mnist.train.next_batch(batch_size)
                _, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
                epoch_loss += c

            print('Epoch', epoch, 'completed out of',hm_epochs,'loss:',epoch_loss)

    

สำหรับแต่ละ epoch และสำหรับแต่ละ batch ในข้อมูลของเรา เรากำลังจะทำการ รันตัว optimizer ของเราและ cost ต่อ batch ของข้อมูลของเรา เพื่อที่จะไปตาม ทางของ loss/cost ของพวกเราในแต่ละขั้นตอน เราจะต้องทำการเพิ่ม cost ทั้งหมด ต่อ epoch สำหรับแต่ละ epoch เราแสดงผล loss ซึ่งควรจะถูกปฏิเสธใน แต่ละครั้ง สิ่งนี้จะมีประโยชน์สำหรับการทำงานนี้ ดังนั้น คุณจะเห็นการตอบแทน ที่ลดลงเมื่อ เวลาผ่านไป epochs อันแรกๆควรจะมีการปรับปรุงเป็นอย่างมาก แต่หลังจากประมาณ 10 หรือ 20 คุณจะเห็นว่ามันเล็กมากๆ ถ้ามีการเปลี่ยน แปลงหรือคุณอาจจะทำให้มันแย่ลง

ตอนนี้ ภายนอกของ epoch สำหรับ loop :

correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))

 

สิ่งนี้จะบอกเราว่ามีการทำนายที่เราสร้างขึ้นซึ่งทำให้มันมีการจับคู่ที่สมบูรณ์ไปยัง ฉลากของพวกเขามากแค่ไหน

accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
        print('Accuracy:',accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))

 

ตอนนี้เรามีตอนจบที่ถูกต้องแม่นยำบนชุดการทดสอบแล้ว ตอนนี้ คือ สิ่งทั้งหมดที่เราจะทำ :

ที่ไหนซักที่ระหว่าง epochs 10 และ 20 จะให้ความถูกต้องแม่นยำคุณ ~95% ความถูกต้อง 95% ฟังดูดี แต่จริงๆแล้วถือว่าแย่มากเมื่อเทียบกับวิธีที่ได้รับ ความนิยมมากกว่านี้ ที่จริงผมคิดว่าความถูกต้อง 95% ด้วยรูปแบบนี้ไม่มีอะไร น่าอัศจรรย์เลย พิจารณาว่าข้อมูลเดียวที่เราให้กับเครือข่ายเป็นค่า pixel เท่านั้น นั่นแหละ เราไม่ได้บอกให้มันมองหา patterns หรือวิธีบอก 4 จาก 9 หรือ 1 จาก 8 เครือข่ายคิดมันออกมาได้ง่ายๆด้วยโมเดลภายใน มีพื้นฐานทั้งหมดมาจากค่า pixel ที่เริ่มต้นและประสบความสำเร็จความถูกต้อง 95% นั่นมันทำให้ผมรู้สึก อัศจรรย์ใจ ถึงแม้ว่าสถานะของศิลปะ คือ มากกว่า 99% ก็ตาม     

โค้ดแบบเต็มๆที่นี่ :  

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot = True)

n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500

n_classes = 10
batch_size = 100

x = tf.placeholder('float', [None, 784])
y = tf.placeholder('float')

def neural_network_model(data):
    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([784, n_nodes_hl1])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}

    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}

    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}

    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
                    'biases':tf.Variable(tf.random_normal([n_classes])),}


    l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), hidden_1_layer['biases'])
    l1 = tf.nn.relu(l1)

    l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']), hidden_2_layer['biases'])
    l2 = tf.nn.relu(l2)

    l3 = tf.add(tf.matmul(l2,hidden_3_layer['weights']), hidden_3_layer['biases'])
    l3 = tf.nn.relu(l3)

    output = tf.matmul(l3,output_layer['weights']) + output_layer['biases']

    return output

def train_neural_network(x):
    prediction = neural_network_model(x)
    # OLD VERSION:
    #cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(prediction,y) )
    # NEW:
    cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y) )
    optimizer = tf.train.AdamOptimizer().minimize(cost)
   
    hm_epochs = 10
    with tf.Session() as sess:
        # OLD:
        #sess.run(tf.initialize_all_variables())
        # NEW:
        sess.run(tf.global_variables_initializer())

        for epoch in range(hm_epochs):
            epoch_loss = 0
            for _ in range(int(mnist.train.num_examples/batch_size)):
                epoch_x, epoch_y = mnist.train.next_batch(batch_size)
                _, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
                epoch_loss += c

            print('Epoch', epoch, 'completed out of',hm_epochs,'loss:',epoch_loss)

        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))

        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
        print('Accuracy:',accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))

train_neural_network(x)

 

ในการเรียนการสอนถัดไปเราจะพยายามใช้โมเดลที่แน่นอนนี้และประยุกต์ใช้มัน กับชุดข้อมูลใหม่ซึ่งไม่ได้ถูกตระเตรียมมาอย่างดีเพื่อเราเหมือนอย่างอันนี้ 

มันมี 4 quiz/question(s) สำหรับการเรียนการสอนนี้ https://pythonprogramming .net/+=1/ เพื่อเข้าถึงสิ่งเหล่านี้ ดาวน์โหลดวิดีโอและไร้โฆษณา

 

การเรียนการสอนถัดไป :

https://pythonprogramming.net/using-our-own-data-tensorflow-deep-learning-tutorial/?completed=/tensorflow-neural-network-session-machine-learning-tutorial/

References :

https://pythonprogramming.net/tensorflow-neural-network-session-machine-learning-tutorial/



บทความนี้อาจจะมีที่ผิด กรุณาตรวจสอบก่อนใช้

หากมีข้อผิดพลาด/ต้องการพูดคุยเพิ่มเติมเกี่ยวกับบทความนี้ กรุณาแจ้งที่ http://m.me/Expert.Programming.Tutor

ไม่อยากอ่าน Tutorial อยากมาเรียนเลยทำอย่างไร?

สมัครเรียน ONLINE ได้ทันทีที่ https://elearn.expert-programming-tutor.com

หรือติดต่อ

085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM

แผนที่ ที่ตั้งของอาคารของเรา

C Article


C++ Article


Java Article


C#.NET Article


VB.NET Article


Python Article


Golang Article


JavaScript Article


Perl Article


Lua Article


Rust Article


Article


Python


Python Numpy


Python Machine Learning



แผนผังการเรียนเขียนโปรแกรม

Link อื่นๆ

Allow sites to save and read cookie data.
Cookies are small pieces of data created by sites you visit. They make your online experience easier by saving browsing information. We use cookies to improve your experience on our website. By browsing this website, you agree to our use of cookies.

Copyright (c) 2013 expert-programming-tutor.com. All rights reserved. | 085-350-7540 | 084-88-00-255 | ntprintf@gmail.com

ติดต่อเราได้ที่

085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM
แผนที่ ที่ตั้งของอาคารของเรา