여러개의 입력(feature)의 Linear Regression TensorFlow 구현

업데이트:

본 글은 모두를 위한 머신러닝/딥러닝 강의를 참고하여 작성하였습니다.
소스 코드는 DeepLearningZeroToAll를 참고 하여 작성하였습니다.

ML lab 04-1: multi-variable linear regression을 TensorFlow에서 구현하기

Hypothesis using matrix

여러개의 입력(feature)의 Linear Regression에서 다루었던 내용을 TensorFlow로 구현해 보겠습니다.

$x$ 변수의 개수를 세 개를 기준
여기서 사용할 가설과 수식은

\[H(x_1, x_2, x_3) = w_1x_1 + w_2x_2 + w_3x_3 + b\] \[\mathbf{cost}(W, b) = { {1} \over {m} }\sum_{i = 1}^m(H(x_{1i}, x_{2i}, x_{3i}) - y_i)^2\]

이와 같습니다.

import tensorflow as tf
import numpy as np

x_data = [[73., 80., 75.],
          [93., 88., 93.],
          [89., 91., 90.],
          [96., 98., 100.],
          [73., 66., 70.]]
y_data = [[152.],
          [185.],
          [180.],
          [196.],
          [142.]]

$X$는 5행 3열로,
5개의 값을 갖고 있는 세 개의 $x$로 구성 돼 있습니다.

tf.model = tf.keras.Sequential()
tf.model.add(tf.keras.layers.Dense(units = 1, input_dim = 3))
tf.model.add(tf.keras.layers.Activation('linear'))

모델의 경우 3개의 값을 입력받고
하나의 값을 출력하는 것인데
여기서의 의미는
길이가 5인 $x$ 벡터 세 개를 넣어
길이가 5인 $y$ 벡터를 하나 출력한다
입니다.

tf.model.compile(loss = 'mse', optimizer = tf.keras.optimizers.SGD(lr = 1e-5))
tf.model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 1)                 4         
_________________________________________________________________
activation (Activation)      (None, 1)                 0         
=================================================================
Total params: 4
Trainable params: 4
Non-trainable params: 0
_________________________________________________________________
history = tf.model.fit(x_data, y_data, epochs = 100)
y_predict = tf.model.predict(np.array([[72., 93., 90.]]))
print(y_predict)
[[165.74065]]

태그:

카테고리:

업데이트:

댓글남기기