Boston Housing Dataset에서 Tensorflow를 사용하여 집값을 추정해 봅니다.
- 필요한 package들을 import합니다.
- 집의 data를 얻어오는 함수를 정의합니다.
boston housing dataset
- 집의 data를 읽어옵니다.
- 집의 data의 내용을 살펴봅니다.
정답
- 집 주변의 정보, 집의 가격을 넣어줄 placeholder를 x_input, y_input에 저장합니다.
$ x\_input= \begin{pmatrix} x_1^{(1)} & x_2^{(1)} & \cdots & x_{13}^{(1)} \\ x_1^{(2)} & x_2^{(2)} & \cdots & x_{13}^{(2)} \\ \vdots & \vdots & \ddots & \vdots \\ x_1^{(m)} & x_2^{(m)} & \cdots & x_{13}^{(m)} \end{pmatrix} $ $ y\_input= \begin{pmatrix} y^{(1)} \\ y^{(2)} \\ \vdots \\ y^{(m)} \end{pmatrix} $
- 계산도중에 사용할 variable을 만들어서 theta0_var, thetan_var에 저장합니다.
$ theta0\_var= \begin{pmatrix} \theta_0 \end{pmatrix} $ $ thetan\_var= \begin{pmatrix} \theta_1 \\ \theta_2 \\ \vdots \\ \theta_{13} \\ \end{pmatrix} $
- 집의 추정 가격의 계산과정을 정의합니다.
$ h_\theta(x_1,x_2,\cdots,x_{13})=\theta_0+\theta_1x_1+\theta_2x_2+\cdots+\theta_{13}x_{13} =\theta_0+ \begin{pmatrix} x_1 & x_2 & \cdots & x_{13} \end{pmatrix} \begin{pmatrix} \theta_1 \\ \theta_2 \\ \vdots \\ \theta_{13} \end{pmatrix} $ $ y\_output= \begin{pmatrix} h_{\theta}(x_1^{(1)},x_2^{(1)},\cdots,x_{13}^{(1)}) \\ h_{\theta}(x_1^{(2)},x_2^{(2)},\cdots,x_{13}^{(2)}) \\ \vdots \\ h_{\theta}(x_1^{(m)},x_2^{(m)},\cdots,x_{13}^{(m)}) \\ \end{pmatrix} = \begin{pmatrix} \theta_0 \\ \theta_0 \\ \vdots \\ \theta_0 \end{pmatrix} + \begin{pmatrix} x_1^{(1)} & x_2^{(1)} & \cdots & x_{13}^{(1)} \\ x_1^{(2)} & x_2^{(2)} & \cdots & x_{13}^{(2)} \\ \vdots & \vdots & \ddots & \vdots \\ x_1^{(m)} & x_2^{(m)} & \cdots & x_{13}^{(m)} \end{pmatrix} \begin{pmatrix} \theta_1 \\ \theta_2 \\ \vdots \\ \theta_{13} \end{pmatrix} $
- cost의 계산과정을 정의합니다.
$J(\theta_0,\theta_1,\cdots,\theta_{13})=\frac{1}{m}\sum_{i=1}^{m}(h_\theta(x_1^{(i)},x_2^{(i)},\cdots,x_{13}^{(i)})-y^{(i)})^{2}$ $h_\theta(x_1,x_2,\cdots,x_{13} )=\theta_0+\theta_1x_1+\theta_2x_2+\cdots+\theta_{13}x_{13}$ $ (y\_output-y\_input)^2= \begin{pmatrix} (h_{\theta}(x_1^{(1)},x_2^{(1)},\cdots,x_{13}^{(1)}) - y^{(1)})^2 \\ (h_{\theta}(x_1^{(2)},x_2^{(2)},\cdots,x_{13}^{(2)}) - y^{(2)})^2 \\ \vdots \\ (h_{\theta}(x_1^{(m)},x_2^{(m)},\cdots,x_{13}^{(m)}) - y^{(m)})^2 \end{pmatrix} $
- cost를 최소화시키는 operation을 AdamOptimizer으로 정의하여 train_step에 저장합니다.
- x_input, y_input, theta0_var, thetan_var, y_output, cost_output, train_step을 print로 출력해 봅니다. 모두 값이 보이지 않는 알아보기 힘든 결과가 출력됩니다.
- Tensorflow Session을 open합니다.
- theta0, thetan, cost를 초기화합니다.
- Gradient Descent를 사용하여 cost가 가장 작아지는
$\theta$ 값들을 찾습니다.
$\theta$ 값들을 theta0, thetan에 저장하고, cost를 cost에 저장하고, 추정된 집 가격을 y_pred에 저장합니다.
- 학습된
$\theta$ 값들을 출력하고 cost를 출력합니다.
- 집의 가격과 집의 추정 가격을 그래프로 그립니다.
- Tensorflow Session을 close합니다.
토론이 없습니다