본문 바로가기

Tech/머신러닝

[정리] 모두를 위한 딥러닝 04 - Multi-Variable Linear Regression

네이버 공유하기
728x90

[정리] 모두를 위한 딥러닝 04 - Multi-Variable Linear Regression by 김성훈 


강의 웹사이트 : http://hunkim.github.io/ml/

Lec = 강의 / Lab = 실습


x 가 여러개인 경우, 행렬을 이용해서 Y 를 예측하는 방법.


이를 파이썬으로 구현하면 아래와 같다.


import tensorflow as tf

x_data = [[73., 80., 75.], [93., 88., 93.],

        [89., 91., 90.], [96., 98., 100.], [73., 66., 70.]]

y_data = [[152.], [185.], [180.], [196.], [142.]]

# placeholders for a tensor that will be always fed.

X = tf.placeholder(tf.float32, shape=[None, 3]) # [n, 3] 행렬. x_data 세트 = n 개

Y = tf.placeholder(tf.float32, shape=[None, 1]) # [n, 1] 행렬.


W = tf.Variable(tf.random_normal([3, 1]), name='weight')

b = tf.Variable(tf.random_normal([1]), name='bias')


# Hypothesis

hypothesis = tf.matmul(X, W) + b

# Simplified cost/loss function

cost = tf.reduce_mean(tf.square(hypothesis - Y))

# Minimize

optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)

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(2001):

  cost_val, hy_val, _ = sess.run(

      [cost, hypothesis, train], feed_dict={X: x_data, Y: y_data})

  if step % 10 == 0:

      print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val)


소스코드 : https://github.com/hunkim/DeepLearningZeroToAll

반응형
네이버 공유하기


* 쿠팡 파트너스 활동을 통해 일정액의 수수료를 제공받을 수 있습니다.