-
Linear Regression and How to minimize costMachine learning, Deep learning 2020. 7. 3. 02:15
예측값(가설) - 실제값 의 제곱을 평균낸 값 -> Cost
이를 파이썬으로 구현
import numpy as np X = np.array([1,2,3]) Y = np.array([1,2,3]) def cost_func(W,X,Y): c = 0 for i in range(len(X)): c += (W * X[i] - Y[i]) **2 return c/len(X) for feed_W in np.linspace(-3, 5, num = 15): curr_cost = cost_func(feed_W, X, Y) print("{:6.3f} | {:10.5f}".format(feed_W, curr_cost))
결과:
이를 Tensorflow로 구현을 해보면?
import tensorflow as tf X = np.array([1,2,3]) Y = np.array([1,2,3]) def cost_func(W, X, Y): hypothesis = X * W return tf.reduce_mean(tf.square(hypothesis - Y)) W_values = np.linspace(-3, 5, num = 15) cost_values = [] for feed_W in W_values: curr_cost = cost_func(feed_W, X, Y) cost_values.append(curr_cost) print('{:6.3f} | {:10.5f}'.format(feed_W, curr_cost))
alpha = 0.01 gradient = tf.reduce_mean(tf.multiply(tf.multiply(W, X) - Y, X)) descent = W - tf.multiply(alpha, gradient) W.assign(descent)
tf.set_random_seed(0) #random_seed, 다음에 이 코드를 다시 수행했을때 동일하게 수행되도록 특정 값으로 set합 x_data = [1., 2., 3., 4.] y_data = [1., 2., 3., 4.] W = tf.Variable(tf.random_normal([1], -100., 100.)) # random_normal: 정규분포를 따르는 random 넘버를 한 개짜리로 변수를 만들어서 W에 할당 for step in range(300): # 300번 실행 hypothesis = W * X cost = tf.reduce_mean(tf.square(hypothesis - Y)) alpha = 0.01 gradient = tf.reduce_mean(tf.multiply(tf.multiply(W, X) - Y, X)) descent = W - tf.multiply(alpha, gradient) W.assign(descent) if step % 10 == 0: # 10번에 1번씩 값 출력 print('{:5} | {:10.4f} | {:10.6f}'.format(step, cost.numpy(), W.numpy()[0]))
예측값(가설) - 실제값 의 제곱을 평균낸 값 -> Cost
이를 파이썬으로 구현
import numpy as np X = np.array([1,2,3]) Y = np.array([1,2,3]) def cost_func(W,X,Y): c = 0 for i in range(len(X)): c += (W * X[i] - Y[i]) **2 return c/len(X) for feed_W in np.linspace(-3, 5, num = 15): curr_cost = cost_func(feed_W, X, Y) print("{:6.3f} | {:10.5f}".format(feed_W, curr_cost))
결과:
이를 Tensorflow로 구현을 해보면?
import tensorflow as tf X = np.array([1,2,3]) Y = np.array([1,2,3]) def cost_func(W, X, Y): hypothesis = X * W return tf.reduce_mean(tf.square(hypothesis - Y)) W_values = np.linspace(-3, 5, num = 15) cost_values = [] for feed_W in W_values: curr_cost = cost_func(feed_W, X, Y) cost_values.append(curr_cost) print('{:6.3f} | {:10.5f}'.format(feed_W, curr_cost))
alpha = 0.01 gradient = tf.reduce_mean(tf.multiply(tf.multiply(W, X) - Y, X)) descent = W - tf.multiply(alpha, gradient) W.assign(descent)
tf.set_random_seed(0) #random_seed, 다음에 이 코드를 다시 수행했을때 동일하게 수행되도록 특정 값으로 set합 x_data = [1., 2., 3., 4.] y_data = [1., 2., 3., 4.] W = tf.Variable(tf.random_normal([1], -100., 100.)) # random_normal: 정규분포를 따르는 random 넘버를 한 개짜리로 변수를 만들어서 W에 할당 for step in range(300): # 300번 실행 hypothesis = W * X cost = tf.reduce_mean(tf.square(hypothesis - Y)) alpha = 0.01 gradient = tf.reduce_mean(tf.multiply(tf.multiply(W, X) - Y, X)) descent = W - tf.multiply(alpha, gradient) W.assign(descent) if step % 10 == 0: # 10번에 1번씩 값 출력 print('{:5} | {:10.4f} | {:10.6f}'.format(step, cost.numpy(), W.numpy()[0]))
'Machine learning, Deep learning' 카테고리의 다른 글
머신러닝에 대한 기본적인 질문들 (0) 2020.07.05 Multi-variable Linear Regression (0) 2020.07.03 Lecture4_Backpropagation and Neural Networks (0) 2020.07.02 Simple Linear Regression(단순선형회귀) (0) 2020.06.29 Regression_회귀의 기본 개념 (0) 2020.06.29