지난번 시간이랑은 달리 X,Y data가 matrix로 되어있다.
multi - variable에 따른 hypothesis, cost
지난번 시간에 H(x) = W*x + b였다면, multi-variable 에서는 H(x)=X*W이다. (X,W는 행렬의 형태)
# Lab 4 Multi-variable linear regression
import tensorflow as tf
tf.set_random_seed(777) # for reproducibility
# 5행 3열의 X_data, 1열의 Y_data
x1_data = [73., 93., 89., 96., 73.]
x2_data = [80., 88., 91., 98., 66.]
x3_data = [75., 93., 90., 100., 70.]
y_data = [152., 185., 180., 196., 142.]
# placeholders for a tensor that will be always fed.
# X,Y의 노드를 생성시킨다.
x1 = tf.placeholder(tf.float32)
x2 = tf.placeholder(tf.float32)
x3 = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
#hypothesis 함수를 만족시키기 위한 w,b값을 랜덤으로 설정한다.
w1 = tf.Variable(tf.random_normal([1]), name='weight1')
w2 = tf.Variable(tf.random_normal([1]), name='weight2')
w3 = tf.Variable(tf.random_normal([1]), name='weight3')
b = tf.Variable(tf.random_normal([1]), name='bias')
hypothesis = x1 * w1 + x2 * w2 + x3 * w3 + b
# cost function
# 비용함수 정의
cost = tf.reduce_mean(tf.square(hypothesis - Y))
# Minimize. Need a very small learning rate for this data set
# 경사하강법을 이용한 비용함수의 최소화 과정
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-2)
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())
# cost, hypothesis, train에 대해서 running을 한다.
# x,y node에 x,y data를 삽입.
for step in range(2001):
cost_val, hy_val, _ = sess.run([cost, hypothesis, train],
feed_dict={x1: x1_data, x2: x2_data, x3: x3_data, Y: y_data})
# 10스텝씩 진행함.
# step, cost값,prediction값 = hypothesis 출력
if step % 10 == 0:
print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val)
'코딩이것저것' 카테고리의 다른 글
OCR_이미지를 텍스트로 변환 (0) | 2017.08.02 |
---|---|
openCV_영상에서 fullbody 감지 (0) | 2017.08.02 |
CNN_Linear Regression의 cost 최소화 알고리즘의 원리 설명 (0) | 2017.07.31 |
CNN_Logistic Classification의 가설 함수 정의 (0) | 2017.07.31 |
openCV_영상파일에서 얼굴 감지하기 (0) | 2017.07.28 |