Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import tensorflow as tf
- # ประกาศ parameter ที่ต้องการทราบก่อน
- W = tf.Variable([.3], tf.float32)
- b = tf.Variable([-.3], tf.float32)
- # ตรงนี้เป็น model input output
- x = tf.placeholder(tf.float32)
- linear_model = W * x + b
- y = tf.placeholder(tf.float32)
- # loss เป็นตัวบอกว่า model ที่ได้ใน input เดียวกัน output มันคลาดเคลื่อนไปจาก datasets เท่าไร
- loss = tf.reduce_sum(tf.square(linear_model - y))
- # optimizer นี่ตามชื่อเลยครับ
- optimizer = tf.train.GradientDescentOptimizer(0.01)
- train = optimizer.minimize(loss) #ตรงนี้จะพยายามลด Loss ให้น้อยที่สุด
- # datasets ของเรา
- x_train = [1,2,3,4]
- y_train = [0,-1,-2,-3]
- # อันนี้เป็นขั้นตอน training
- init = tf.global_variables_initializer()
- sess = tf.Session()
- sess.run(init)
- for i in range(1000):
- sess.run(train, {x:x_train, y:y_train})
- # พอ train เสร็จเราก็จะมาดูผลที่ได้
- curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x:x_train, y:y_train})
- print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement