tobby48 6 years ago
parent
commit
0e34299c77
1 changed files with 36 additions and 0 deletions
  1. 36
    0
      src/main/python/kr/co/swh/lecture/opensource/tensorflow/cost.py

+ 36
- 0
src/main/python/kr/co/swh/lecture/opensource/tensorflow/cost.py View File

@@ -0,0 +1,36 @@
1
+# 텐서플로우 모듈 import
2
+import tensorflow as tf
3
+
4
+# 학습 데이터
5
+xData = [1, 2, 3]
6
+yData = [1, 2, 3]
7
+
8
+# tf.Variable : 텐서플로우가 학습하는 과정에서 변경되는 변수(텐서플로우 전용 변수)
9
+# tf.random_normal(shape)
10
+W = tf.Variable(tf.random_normal([1]), name="weight")   # Rank가 1이며 Shape는 값을 하나만 가지는 Vector
11
+b = tf.Variable(tf.random_normal([1]), name="bias")
12
+print(W)
13
+print(b)
14
+
15
+# 가설 만들기
16
+# H(x) = Wx + b
17
+H = W * xData + b
18
+print(H)
19
+
20
+# Cost / Loss 구하기
21
+# tf.square(x, y) : x와 y의 거리를 제곱
22
+# tf.reduce_mean(x) : x를 평균
23
+diff = tf.square(H - yData)
24
+cost = tf.reduce_mean(diff)
25
+print(diff)
26
+print(cost)
27
+
28
+sess = tf.Session()
29
+# tf.Variable 인 Variable오퍼레이션을 사용할 땐 반드시 미리 초기화
30
+sess.run(tf.global_variables_initializer())
31
+
32
+print(sess.run(W))
33
+print(sess.run(b))
34
+print(sess.run(H))
35
+print(sess.run(diff))
36
+print(sess.run(cost))