Iris Flower Dataset에서 Tensorflow를 사용하여 꽃의 종류를 추정해 봅니다.
- 필요한 package들을 import합니다.
- iris data를 얻어오는 함수를 정의합니다.
iris dataset
- iris data를 읽어옵니다.
- iris data의 내용을 살펴봅니다.
정답
- 꽃의 길이 정보, 꽃의 종류를 넣어줄 placeholder를 x_input, y_input에 저장합니다.
$ x\_input= \begin{pmatrix} x_1^{(1)} & x_2^{(1)} & x_3^{(1)} & x_4^{(1)} \\ x_1^{(2)} & x_2^{(2)} & x_3^{(2)} & x_4^{(2)} \\ \vdots & \vdots & \vdots & \vdots \\ x_1^{(m)} & x_2^{(m)} & x_3^{(m)} & x_{4}^{(m)} \end{pmatrix} $ $ y\_input= \begin{pmatrix} y_{(st)}^{(1)} & y_{(vs)}^{(1)} & y_{(vg)}^{(1)} \\ y_{(st)}^{(2)} & y_{(vs)}^{(2)} & y_{(vg)}^{(2)} \\ \vdots & \vdots & \vdots \\ y_{(st)}^{(m)} & y_{(vs)}^{(m)} & y_{(vg)}^{(m)} \\ \end{pmatrix} $
- 계산도중에 사용할 variable을 만들어서 theta0_var, thetan_var에 저장합니다.
$ theta0\_var= \begin{pmatrix} \theta_{(st)0} & \theta_{(vs)0} & \theta_{(vg)0} \end{pmatrix} $ $ thetan\_var= \begin{pmatrix} \theta_{(st)1} & \theta_{(vs)1} & \theta_{(vg)1} \\ \theta_{(st)2} & \theta_{(vs)2} & \theta_{(vg)2} \\ \theta_{(st)3} & \theta_{(vs)3} & \theta_{(vg)3} \\ \theta_{(st)4} & \theta_{(vs)4} & \theta_{(vg)4} \\ \end{pmatrix} $
- 꽃의 종류를 추정하는 계산과정을 정의합니다.
tf.nn.softmax 설명$ \begin{aligned} logit&= \begin{pmatrix} \theta_{(st)0} & \theta_{(vs)0} & \theta_{(vg)0} \\ \theta_{(st)0} & \theta_{(vs)0} & \theta_{(vg)0} \\ \vdots & \vdots & \vdots \\ \theta_{(st)0} & \theta_{(vs)0} & \theta_{(vg)0} \\ \end{pmatrix} + \begin{pmatrix} x_1^{(1)} & x_2^{(1)} & x_3^{(1)} & x_4^{(1)} \\ x_1^{(2)} & x_2^{(2)} & x_3^{(2)} & x_4^{(2)} \\ \vdots & \vdots & \vdots & \vdots \\ x_1^{(m)} & x_2^{(m)} & x_3^{(m)} & x_{4}^{(m)} \end{pmatrix} \begin{pmatrix} \theta_{(st)1} & \theta_{(vs)1} & \theta_{(vg)1} \\ \theta_{(st)2} & \theta_{(vs)2} & \theta_{(vg)2} \\ \theta_{(st)3} & \theta_{(vs)3} & \theta_{(vg)3} \\ \theta_{(st)4} & \theta_{(vs)4} & \theta_{(vg)4} \\ \end{pmatrix} \\ &= \begin{pmatrix} \theta_{(st)0}+\sum_{i=1}^{4}\theta_{(st)i} x^{(1)}_i & \theta_{(vs)0}+\sum_{i=1}^{4}\theta_{(vs)i} x^{(1)}_i & \theta_{(vg)0}+\sum_{i=1}^{4}\theta_{(vg)i} x^{(1)}_i \\ \theta_{(st)0}+\sum_{i=1}^{4}\theta_{(st)i} x^{(2)}_i & \theta_{(vs)0}+\sum_{i=1}^{4}\theta_{(vs)i} x^{(2)}_i & \theta_{(vg)0}+\sum_{i=1}^{4}\theta_{(vg)i} x^{(2)}_i \\ \vdots & \vdots & \vdots \\ \theta_{(st)0}+\sum_{i=1}^{4}\theta_{(st)i} x^{(m)}_i & \theta_{(vs)0}+\sum_{i=1}^{4}\theta_{(vs)i} x^{(m)}_i & \theta_{(vg)0}+\sum_{i=1}^{4}\theta_{(vg)i} x^{(m)}_i \\ \end{pmatrix} \end{aligned} $
- cost의 계산과정을 정의합니다.
tf.nn.softmax_cross_entropy_with_logits_v2 설명
- cost를 최소화시키는 operation을 AdamOptimizer으로 정의하여 train_step에 저장합니다.
- x_input, y_input, theta0_var, thetan_var, logit, y_output, cross_entropy, cost_output, train_step을 print로 출력해 봅니다. 모두 값이 보이지 않는 알아보기 힘든 결과가 출력됩니다.
- Tensorflow Session을 open합니다.
- theta0, thetan, cost를 초기화합니다.
- Gradient Descent를 사용하여 cost가 가장 작아지는
$\theta$ 값들을 찾습니다.
$\theta$ 값들을 theta0, thetan에 저장하고, cost를 cost에 저장하고, 추정된 꽃의 종류를 y_pred에 저장합니다.
- 학습된
$\theta$ 값들을 출력하고 cost를 출력합니다.
- 꽃의 분류 결과를 출력합니다.
- 꽃의 분류 정확도를 출력합니다.
- 잘못 분류된 꽃에 대한 정보를 출력합니다.
- 꽃의 길이 정보와 꽃의 분류를 그래프로 그리고 잘못 분류된 꽃도 그래프에 그립니다.
- x_axis와 y_axis를 다양하게 변경해서 그래프를 그려 봅니다.
- Tensorflow Session을 close합니다.
토론이 없습니다