#!/usr/bin/env python # coding: utf-8 # # Linear Regression cost 최소화의 TensorFlow 구현 # ## Simplified hypothesis # # \begin{equation} # H(x) = Wx # \end{equation} # # \begin{equation} # Cost(W) = \frac{1}{m} \sum_{i=1}^{m}(Wx^{(i)}-y^{(i)})^{2} # \end{equation} # In[1]: get_ipython().run_line_magic('matplotlib', 'inline') # Lab 3 Minimizing Cost import tensorflow as tf import matplotlib.pyplot as plt X = [1, 2, 3] Y = [1, 2, 3] W = tf.placeholder(tf.float32) # Our hypothesis for linear model X * W hypothesis = X * W # cost/loss function cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Launch the graph in a session. sess = tf.Session() # Initializes global variables in the graph. sess.run(tf.global_variables_initializer()) # Variables for plotting cost function # cost 함수를 그리기 위한 변수 W_history = [] cost_history = [] for i in range(-30, 50): curr_W = i * 0.1 curr_cost = sess.run(cost, feed_dict={W: curr_W}) W_history.append(curr_W) cost_history.append(curr_cost) # Show the cost function plt.plot(W_history, cost_history) plt.show() # ## Gradient Descent # # \begin{equation} # Cost(W) = \frac{1}{m} \sum_{i=1}^{m}(Wx^{(i)}-y^{(i)})^{2} # \end{equation} # # \begin{equation} # W := W - \alpha \frac{1}{m} \sum_{i=1}^{m} (Wx^{(i)} - y^{(i)})x^{(i)} # \end{equation} # # 위 수식은 다음처럼 코딩할 수 있음. # Minimize: Gradient Descent using derivative: W -= learning_rate * derivative learning_rate = 0.1 gradient = tf.reduce_mean((W * X - Y) * X) descent = W - learning_rate * gradient update = W.assign(descent) # In[2]: # Lab 3 Minimizing Cost import tensorflow as tf tf.set_random_seed(777) # for reproducibility x_data = [1, 2, 3] y_data = [1, 2, 3] # Try to find values for W and b to compute y_data = W * x_data + b # We know that W should be 1 and b should be 0 # But let's use TensorFlow to figure it out W = tf.Variable(tf.random_normal([1]), name='weight') X = tf.placeholder(tf.float32) Y = tf.placeholder(tf.float32) # Our hypothesis for linear model X * W hypothesis = X * W # cost/loss function cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Minimize: Gradient Descent using derivative: W -= learning_rate * derivative learning_rate = 0.1 gradient = tf.reduce_mean((W * X - Y) * X) descent = W - learning_rate * gradient update = W.assign(descent) # Launch the graph in a session. sess = tf.Session() # Initializes global variables in the graph. sess.run(tf.global_variables_initializer()) for step in range(21): sess.run(update, feed_dict={X: x_data, Y: y_data}) print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run(W)) # Gradient Descent를 다음처럼 간단하게 할 수 있음. # Minimize: Gradient Descent Magic optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1) train = optimizer.minimize(cost) # W = 5.0을 초기값을 주었을 때 # # cost가 최소가 되는 W를 찾는 프로그램 # In[3]: # Lab 3 Minimizing Cost import tensorflow as tf # tf Graph Input X = [1, 2, 3] Y = [1, 2, 3] # Set wrong model weights W = tf.Variable(5.0) # Linear model hypothesis = X * W # cost/loss function cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Minimize: Gradient Descent Magic optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1) train = optimizer.minimize(cost) # Launch the graph in a session. sess = tf.Session() # Initializes global variables in the graph. sess.run(tf.global_variables_initializer()) #for step in range(100): for step in range(10): print(step, sess.run(W)) sess.run(train) # W = 5.0을 초기값을 주었을 때 # # cost가 최소가 되는 W를 찾는 프로그램 # In[4]: # Lab 3 Minimizing Cost import tensorflow as tf # tf Graph Input X = [1, 2, 3] Y = [1, 2, 3] # Set wrong model weights W = tf.Variable(-3.0) # Linear model hypothesis = X * W # cost/loss function cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Minimize: Gradient Descent Magic optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1) train = optimizer.minimize(cost) # Launch the graph in a session. sess = tf.Session() # Initializes global variables in the graph. sess.run(tf.global_variables_initializer()) #for step in range(100): for step in range(10): print(step, sess.run(W)) sess.run(train) # 수동으로 구한 gradient와 텐서플로 optimizer에서 구한 gvs(gradient)가 같은지 확인. # In[5]: # 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 Magic optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) train = optimizer.minimize(cost) # Get gradients gvs = optimizer.compute_gradients(cost, [W]) # 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. sess = tf.Session() # Initializes global variables in the graph. sess.run(tf.global_variables_initializer()) for step in range(100): print(step, sess.run([gradient, W, gvs])) sess.run(apply_gradients) # In[ ]: