4Z1 - Artificial Intelligence/인공지능 개론

딥러닝의 기본 학습 - Multivariable linear regression-실습-3

Richrad Chung 2020. 8. 27. 13:06

> 텐서 플로우는 강력한 기능으로 처리해주는데 대용량의 로직 처리가 일품이다.

> 파이선의 슬라이스 기능이 있어 백터의 접근이 용이하다.

 

# Lab 3 Minimizing Cost
# This is optional
import tensorflow as tf

# tf Graph Input
X = [1, 2, 3]
Y = [1, 2, 3]

# Set wrong model weights
W = tf.Variable(5.)

# Linear model
hypothesis = X * W

# Manual gradient
gradient = tf.reduce_mean((W * X - Y) * X) * 2

# cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))

# Minimize: Gradient Descent Optimizer
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)

# Get gradients
gvs = optimizer.compute_gradients(cost)

# Optional: modify gradient if necessary
# gvs = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in gvs]

# Apply gradients
apply_gradients = optimizer.apply_gradients(gvs)

# Launch the graph in a session.
with tf.Session() as sess:
    # Initializes global variables in the graph.
    sess.run(tf.global_variables_initializer())

    for step in range(101):
        gradient_val, gvs_val, _ = sess.run([gradient, gvs, apply_gradients])
        print(step, gradient_val, gvs_val)


Queue Runners

 

 

 

 

tf.train.batch

# collect batches of csv in

train_x_batch, train_y_batch = \

   tf.train.batch([xy[0:-1], xy[-1:]], batch_size=10)

 

sess = tf.Session()

...

 

# Start populating the filename queue.

coord = tf.train.Coordinator()

threads = tf.train.start_queue_runners(sess=sess, coord=coord)

 

for step in range(2001):

   x_batch, y_batch = sess.run([train_x_batch, train_y_batch])

   ...

 

coord.request_stop()

coord.join(threads)

 

 

 

 

shuffle_batch

# min_after_dequeue defines how big a buffer we will randomly sample

#   from -- bigger means better shuffling but slower start up and more

#   memory used.

# capacity must be larger than min_after_dequeue and the amount larger

#   determines the maximum we will prefetch.  Recommendation:

#   min_after_dequeue + (num_threads + a small safety margin) * batch_size

min_after_dequeue = 10000

capacity = min_after_dequeue + 3 * batch_size

example_batch, label_batch = tf.train.shuffle_batch(

   [example, label], batch_size=batch_size, capacity=capacity,

   min_after_dequeue=min_after_dequeue)