tobby48 6 lat temu
rodzic
commit
fa2c598297

+ 16
- 0
src/main/python/kr/co/swh/lecture/opensource/tensorflow/two-subtract-input.py Wyświetl plik

@@ -0,0 +1,16 @@
1
+import tensorflow as tf
2
+
3
+# tf.placeholder(dtype, shape=None, name=None)
4
+# None : 정하지 않은 것. 어떤 것도 가능하다는 의미
5
+tensor_a = tf.placeholder(tf.float32)
6
+tensor_b = tf.placeholder(tf.float32)
7
+
8
+# tensor_c = tensor_a - tensor_b 와 같은 의미
9
+tensor_c = tf.subtract(tensor_a, tensor_b)
10
+
11
+# 상수 오퍼레이션이 아닌 Placeholder 오퍼레이션
12
+print(tf.get_default_graph().get_operations())
13
+
14
+sess = tf.Session()
15
+print(sess.run(tensor_c, feed_dict={tensor_a: 3, tensor_b: 2.5}))
16
+print(sess.run(tensor_c, feed_dict={tensor_a: [3,4], tensor_b: [2,1]}))   # share가 [2, 2] 형태도 가능

+ 41
- 0
src/main/python/kr/co/swh/lecture/opensource/tensorflow/two-sum-high.py Wyświetl plik

@@ -0,0 +1,41 @@
1
+import tensorflow as tf
2
+
3
+# 오퍼레이션 객체를 리스트로 출력(오퍼레이션이 없다)
4
+print(tf.get_default_graph().get_operations())
5
+# []
6
+
7
+# 오퍼레이션을 생성하여 그래프에 추가한 뒤, 
8
+# 각각의 오퍼레이션 출력텐서를 'tensor_a', 'tensor_b'가 가르킴(파이썬은 모두 객체)
9
+tensor_a = tf.constant(1.1, name="a")
10
+tensor_b = tf.constant(2.0, name="b")
11
+
12
+# tensor_c = tf.add(tensor_a, tensor_b) 와 같은 의미
13
+# 앞에서 정의한 상수텐서들을 연산할 오퍼레이션 추가, 해당 오퍼레이션의 출력텐서를 'tensor_c'가 가르킴
14
+tensor_c = tensor_a + tensor_b
15
+
16
+
17
+# Tensor("a:0", shape=(), dtype=float32)
18
+# 1. 'a:0'는 a의 첫번째 출력텐서
19
+# 2. 'shape=()'는 스칼라 텐서
20
+# 3. 'dtype=float32'는 32비트 부동소수형
21
+print(tensor_a)
22
+print(tensor_b)
23
+print(tensor_c)
24
+
25
+
26
+print(tensor_a.op)                # 텐서에 대한 오퍼레이션 정보
27
+print(tensor_a.op.node_def)        # 텐서에 대한 오퍼레이션 정보
28
+print(tensor_a.op.inputs)        # 상수 오퍼레이션의 입력은 출력해볼 수 없다.
29
+print(tensor_a.op.outputs)        # 상수 오퍼레이션의 출력텐서
30
+
31
+print(tensor_c.op.inputs)        # 연산 오퍼레이션은 상수 오퍼레이션과 달리 입력 텐서도 출력가능
32
+print(tensor_c.op.inputs[0])    # tensor_a
33
+print(tensor_c.op.inputs[1])    # tensor_b
34
+print(tensor_c.op.outputs)        # 연산 오퍼레이션의 출력텐서
35
+print(tensor_c.op)                # 텐서에 대한 오퍼레이션 정보
36
+
37
+print(tf.get_default_graph().get_operations())
38
+
39
+# tensor_c 변수가 가리키고있는 텐서 객체의 값을 연산한 뒤, 결과값을 정수로 출력
40
+with tf.Session() as sess:
41
+    print("%d" % sess.run(tensor_c))

+ 16
- 0
src/main/python/kr/co/swh/lecture/opensource/tensorflow/two-sum.py Wyświetl plik

@@ -0,0 +1,16 @@
1
+# 텐서플로우 모듈 import
2
+import tensorflow as tf
3
+# tf.constant 함수를 사용하여 상수 오퍼레이션 2개 생성
4
+# `name`은 오퍼레이션은 구분하기 위한 이름
5
+tensor_a = tf.constant(1, name="a")
6
+tensor_b = tf.constant(2, name="b")
7
+
8
+# tf.constant 함수를 사용하여 연산 오퍼레이션 생성
9
+tensor_c = tensor_a + tensor_b
10
+# 단순 print함수로 텐서를 실행할 땐 tensor정보 출력
11
+print(tensor_c)
12
+
13
+# 실제 연산을 위해서는 세션을 생성한 후, 실행하고자 하는 tensor을 Session의 run함수 매개변수로 넣어야 수행
14
+sess = tf.Session()
15
+print(sess.run(tensor_c))
16
+sess.close()