*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

H(X) = WX
H(X) = XW

아래와 같은 Transpose 형태로 쓸 수도 있다 :

w = [w1 w2 w3] (세로)   x= [x1 x2 x3] (세로)
H(X) = WtX + 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], -11))
W2 = tf.Variable(tf.random_uniform([1], -11))
= tf.Variable(tf.random_uniform([1], -11))
 
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]
 
= tf.Variable(tf.random_uniform([1,2], -11))
= tf.Variable(tf.random_uniform([1], -11))
 
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]
 
= tf.Variable(tf.random_uniform([1,3], -11))
 
 
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를 추가한다.


data.zip

프로젝트 폴더에 놓는다


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)
 
= 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)
 
= tf.placeholder(tf.float32, shape=[None, 3])
= tf.placeholder(tf.float32, shape=[None, 1])
 
= tf.Variable(tf.random_normal([31]))
= 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: [[10070101]]}))
print(sess.run(hypothesis, feed_dict={X: [[6070110], [9010080]]}))
 
 
 
cs








*일반적인 직선을 나타내는 공식(선형을 나타내는 가설 Hypothesis) : 

y = wx + b

H(x) = wx + b


위 공식에서 W와 B가 미지수이다. 

결국 W와 b를 찾는것이 목적!!


w= weight 기울기

b= bias


(x, y) , (x1, y1), (x2, y2) 의 값이 있을 때

(x, H(x)), (x1, H(x1)), (x2, H(x2))


Cost = 1/N <<(H(x) - y)2


Cost는 w의 제곱식 

Cost는 작으면 작을 수록 좋다!(Loss가 적다는 의미)


*Linear Regression을 하기 위한 기본 적인 3가지 요소

- Hypothesis

- Cost 정의(에측값에서 제곱을 해서 평균을 구한 값)

- Cost를 최소화 하기 위한 알고리즘(Gradient Decent...)


* 최적의 Hypothesis의 선택 :
=> H(x) - y를 최소화하는 직선을 찾으면 됨!

- Cost = Hypothesis에서 예측값 목표를 뺀값의 제곱의 평균

(tf.reduce_mean(tf.square(hypothesis - y_data)))




*Linear Regression Example (with Pycharm) : 


1) Pycharm 가상환경에서 tensorflow 설치 :



(Terminal에서 'pip install tensorflow' 입력)



2) Pycharm에서 Python 파일 생성하기 :

실행하기 (shift + F10) or 마우스 우클릭 후 Run linear_Regression



3) tensorflow로 학습시키기 :



소스코드 :


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.2.3.]
y_data = [1.2.3.]
 
 
= tf.Variable(tf.random_uniform([1], -1.1.)) #초기 값을 랜덤하게 준다 -1에서 1 사이에서 1개
= tf.Variable(tf.random_uniform([1], -1.1.)) #초기 값을 랜덤하게 준다 -1에서 1 사이에서 1개
 
hypothesis = w * x_data + b
cost = tf.reduce_mean(tf.square(hypothesis - y_data)) # Cost를 구한다
 
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(cost)  #가장 작은 cost륵 가져온다
 
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
 
for step in range(2001):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(cost), sess.run(w), sess.run(b))
cs






*Tensorflow 란

Tensorflow는 Machine Intelligence를 위한 오픈소스 라이브러리이다.

구글이 만든 numerical computation을 위한 오픈소스 소프트웨어 라이브러리

data flow graphs를 사용할 수 있다는 특징


python으로 구현 가능

동일 코드를 CPU와 GPU에서 모두 사용 가능

데이터, 모델 병렬화

TensorBoard visualization


Tensor = Data

Flow = 흐름

즉 Tensorflow  = 데이터의 흐름



*Dataflow graph :

- Nodes in graph :

=> represent mathematical operations

- Edges :

=> represent the multidimensional data arrays

=> (tensor) communicated between them


*Basic Usage

- To use Tensorflow you need to understand how tensorflow:

=> represents computations as graphs

=> executes graphs in the context of Session

=> represents data as tensors

=> maintains state with variables

=> use feeds and fetches to get data into and out of arbitrary operations


*Tensorflow Basic

Session을 수행하여야 결과를 얻을 수 있다 :



*PlaceHolder 주기 :


1
"Mul: %i"%
cs




*Tensor Ranks, Shapes, and Types


a = [[1,2,3], [4,5,6], [7,8,9]]


a의 rank = 2

shape는 [3,3] 이다


1) Tensor Ranks

0  scalar  s=483

1  vector  v=[1.1 2.2, 3.3]

2  matrix  m=[[1,2,3], [4,5,6], [7,8,9]]

3  3-Tensor  t=[[[2],[4],[6]],[1],[2],[3]],[[5],[6],[7]]]

4  nTensor


2) Tensor Shapes

Rank Shape                    Dimension Number
0      []                                0-D

1      [D0]                            1-D

2      [D0, D1]                        2-D

3      [D0, D1, D2]                   3-D

n      [D0, D1, ..., Dn-1]            4-D



3) Tensor Data Type(아래 2가지를 가장 많이 사용함):

DT_FLOAT   tf.float32

DT_COUBLE  tf.float64

DT_INT32    tf.int32

DT_Complex64 tf.complex64




* Pycharm 설치


아래 링크에서 Community 버전 설치

https://www.jetbrains.com/pycharm/download/#section=windows







+ Recent posts