*multi-variable linear regression은 이전 linear regression과 다르게 x값이 여러개이다
1) one-variable regression Hypothesis
H(x) = Wx + b
2) two-variable regression Hypothesis
H(x1, x2) = w1x1 + w2x2 + b
3) multi-variable regression Hypothesis
H(x1,x2...xn) = w1x1 + w2x2 + w3x3 ... + wnxn + b
위 수식은 비효율적 따라서 아래 Matrix Multiplication 수식으로 변경해준다
H(x1, x2...xn) = [w1, w2, w3] [x1 x2 x3] (세로) + b
H(x1, x2...xn) = [x1, x2, x3] [w1 w2 w3] (세로) + b
H(X) = WX + b
H(X) = XW + b
b term을 없앤 simplified된 형태 => w 괄호 안으로 넣어준다.
H(x1, x2...xn) = [b w1, w2, w3] [x1 x2 x3] (세로) + b
H(x1, x2...xn) = [x1, x2, x3] [b w1 w2 w3] (세로) + b
*Multi-variable linear regression에서의 Cost function :
Cost Function은 이전 linear regression과 똑같다 :
Gradient Descent 알고리즘을 사용한다.
cost(W,b) = 1/m 평균(H(x)- y ) 제곱
* Multi-variable linear regression 구현 실습 :
1) 2개의 x variable( 비효율적인 방법 = matrix 형태아님 ) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import tensorflow as tf x1_data = [1.,0.,3.,0.,5.] x2_data = [0.,2.,0.,4.,0.] y_data = [1,2,3,4,5] W1 = tf.Variable(tf.random_uniform([1], -1, 1)) W2 = tf.Variable(tf.random_uniform([1], -1, 1)) b = tf.Variable(tf.random_uniform([1], -1, 1)) hypothesis = W1*x1_data + W2*x2_data + b cost = tf.reduce_mean(tf.square(hypothesis - y_data)) optimizer = tf.train.GradientDescentOptimizer(0.1) train = optimizer.minimize(cost) sess = tf.Session() sess.run(tf.global_variables_initializer()) for step in range(2001) : sess.run(train) if step % 20 == 0: print(step, sess.run(cost), sess.run(W1), sess.run(W2), sess.run(b)) | cs |
2) 2개의 x variable( 효율적인 방법 = matrix 형태 ) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import tensorflow as tf x_data = [[1.,0.,3.,0.,5.],[0.,2.,0.,4.,0.]] y_data = [1,2,3,4,5] W = tf.Variable(tf.random_uniform([1,2], -1, 1)) b = tf.Variable(tf.random_uniform([1], -1, 1)) hypothesis = tf.matmul(W, x_data) + b # H(X) = WX + b cost = tf.reduce_mean(tf.square(hypothesis - y_data)) optimizer = tf.train.GradientDescentOptimizer(0.1) train = optimizer.minimize(cost) sess = tf.Session() sess.run(tf.global_variables_initializer()) for step in range(2001) : sess.run(train) if step % 20 == 0: print(step, sess.run(cost), sess.run(W), sess.run(b)) | cs |
1) 과 2) 의 비교
b term을 없애고 matrix를 [1,3]으로 변경 시 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import tensorflow as tf x_data = [[1,1,1,1,1], [1.,0.,3.,0.,5.], [0.,2.,0.,4.,0.]] y_data = [1,2,3,4,5] W = tf.Variable(tf.random_uniform([1,3], -1, 1)) hypothesis = tf.matmul(W, x_data) # H(X) = WX cost = tf.reduce_mean(tf.square(hypothesis - y_data)) optimizer = tf.train.GradientDescentOptimizer(0.1) train = optimizer.minimize(cost) sess = tf.Session() sess.run(tf.global_variables_initializer()) for step in range(2001) : sess.run(train) if step % 20 == 0: print(step, sess.run(cost), sess.run(W)) | cs |
*Data를 load 하여 학습 시키기 :
=> Pycharm에 data를 추가한다.
프로젝트 폴더에 놓는다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import tensorflow as tf import numpy as np xy = np.loadtxt('./data/03train.txt', dtype='float32') print(xy) x_data = xy[:, 0 : -1] y_data = xy[:, [-1]] print(x_data.shape, y_data.shape) W = tf.Variable(tf.random_uniform([3,1], -1., 1.)) # 3 = X 열의 개수 hypothesis = tf.matmul(x_data, W) cost = tf.reduce_mean(tf.square(hypothesis - y_data)) optimizer = tf.train.GradientDescentOptimizer(0.1) train = optimizer.minimize(cost) sess =tf.Session() sess.run(tf.global_variables_initializer()) for step in range(2001) : sess.run(train) if step % 20 == 0: # 20 번에 1번씩 print(step, sess.run(cost), sess.run(W)) | cs |
*CSV 파일을 읽어서 출력하기(delimiter는 , 콤마로 ) :
1 2 3 4 5 6 7 8 9 10 | import tensorflow as tf import numpy as np xy = np.loadtxt('./data/test-score.csv', delimiter=',', dtype='float32') print(xy) x_data = xy[:, 0 : -1] y_data = xy[:, [-1]] print(x_data.shape, y_data.shape) | cs |
*File input linear regression :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import tensorflow as tf import numpy as np tf.set_random_seed(777) xy = np.loadtxt('./data/test-score.csv', delimiter=',', dtype=np.float32) x_data = xy[:, 0:-1] y_data = xy[:, [-1]] print(x_data.shape, x_data, len(x_data)) print(y_data.shape, y_data) X = tf.placeholder(tf.float32, shape=[None, 3]) Y = tf.placeholder(tf.float32, shape=[None, 1]) W = tf.Variable(tf.random_normal([3, 1])) b = tf.Variable(tf.random_normal([1])) hypothesis = tf.matmul(X, W) + b cost = tf.reduce_mean(tf.square(hypothesis - Y)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5) train = optimizer.minimize(cost) sess = tf.Session() sess.run(tf.global_variables_initializer()) for step in range(2001): cost_val, hy_val, _ = sess.run( [cost, hypothesis, train], feed_dict={X: x_data, Y: y_data}) if step % 10 == 0: # 10번에 1번씩 print(step, "Cost: ", cost_val, sess.run(W), sess.run(b)) print("=====prediction=====") print(sess.run(hypothesis, feed_dict={X: [[100, 70, 101]]})) print(sess.run(hypothesis, feed_dict={X: [[60, 70, 110], [90, 100, 80]]})) | cs |
'Python 활용 딥러닝' 카테고리의 다른 글
Softmax Classification(multinominal classification) (0) | 2018.12.18 |
---|---|
Logistic Regression 사용법 (0) | 2018.12.18 |
Linear Regression 응용 (Placeholder) (0) | 2018.12.18 |
Linear Regression with 텐서플로우 (Pycharm 환경) (0) | 2018.12.18 |
모두를 위한 머신러닝 + 기계학습 절차 Machine Learning Concept (0) | 2018.12.18 |