티스토리 뷰
앞서 강좌에서 Tensorflow의 환경 구축을 완료해보았습니다.
1.기본 소스코드 분석
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2018-03-11 00:55:58.218567: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
>>> print(sess.run(hello))
Hello, TensorFlow!
이 코드는 앞선 강좌에서 환경구축을 완료한후 기본 소스코드를 작성했던것 입니다.
(1)코드 분석을 해보면, tensorflow의 모듈을 가져와서 tf라는 이름으로 가정합니다.
(2) tf(tensorflow)를 사용하여 constant를 하나 만드는데, 이때 하나의 노드로서 생성이 된다고 합니다.
그래프속에 노드가 만들어 진다고 생각하면된다.
(3) 그리고 이것들을 실행하기위해서는 Session을 생성 해주어야 하는데, Session을 생성하고 run 메소드를 사용하여 tensorflow가 실행되어지게 된다.
+ 추가로 출력된 결과 앞에 b'출력결과'가 나오는경우가 있는데,
이경우는 Bytes literals 의미를 나타내는것이다.
2. 실습 예제 1) Constant (Node create)
>>> node1 = tf.constant(3.0,tf.float32)
>>> node2 = tf.constant(4.0)
>>> node3 = tf.add(node1,node2)
>>> print("node1:",node1,"node2:",node2)
('node1:', <tf.Tensor 'Const:0' shape=() dtype=float32>, 'node2:', <tf.Tensor 'Const_1:0' shape=() dtype=float32>)
>>> print("node3: ",node3)
('node3: ', <tf.Tensor 'Add:0' shape=() dtype=float32>)
소스코드 분석
tf라는 텐서플로우를 활용하여 node1,2의 Node 생성
node1 + node2 => 새로운 node3을 만든다.
이때 신기한것이, print로 Node를 출력하게 되면 들어있는 값이 출력되는것이 아니라, 현재 Node의 상태를 출력시키게 하는데, 그 이유는 node는 Tensor 안의 그래프의 하나의 요소를 나타내는것이기 때문이다.
그렇다면? 이것들을 제대로 출력시키기 위해서는 어떻게 해야할까?
바로 session을 생성하여 run을 시켜주면된다.
>>> print("node1:node2",sess.run([node1,node2]))
('node1:node2', [3.0, 4.0])
>>> print("node3:",sess.run([node3]))
('node3:', [7.0])
이런식으로 세션을 만들어주면, 이 노드의 값들이 출력되어지는것을 알 수 있다.
node3은 node1과 node2의 값들이 더해진값들이
잘 들어간것을 볼 수 있다.
3. Tensorflow Machine Structure
1) Bulid graph using Tensorflow operations
빌드과정
>>> node1 = tf.constant(3.0,tf.float32)
>>> node2 = tf.constant(4.0)
>>> node3 = tf.add(node1,node2)
2)feed data and run graph
ex) sess.run(op)
>>> print("node1:node2",sess.run([node1,node2]))
('node1:node2', [3.0, 4.0])
>>> print("node3:",sess.run([node3]))
('node3:', [7.0])
3)update variable in the graph
그래프 상태 업데이트!
4) 실습 2) Placeholder (Node Create)
그래프를 만들어 놓고, 실행시키는 단계에서 노드에 값을 던져주고 싶을 때는?
Node를 placeholder라는 특별한 노드로 만들어 준다.
그 이후 Session을 만들고 Run을 하게 될경우면 실행단계에서 노드 값을 뿌려줄 수가 있다
>>> import tensorflow as tf
>>> a = tf.placeholder(tf.float32)
>>> b = tf.placeholder(tf.float32)
>>> adder_node = a+b
>>> sess = tf.Session()
2018-03-11 01:57:47.794808: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
>>> print(sess.run(adder_node,feed_dict ={a:3,b: 4.5}))
7.5
>>> print(sess.run(adder_node,feed_dict ={a:[1,3],b:[2,4]}))
[3. 7.]
5. Tensor,Ranks,Shapes, Types
n - n-Tensor
'AI' 카테고리의 다른 글
[머신러닝-Tensorflow] Lec-04 Linear Regression Cost 최소화 알고리즘 (0) | 2018.03.11 |
---|---|
[머신러닝-Tensorflow] Lec-03 Linear Regression Implementation (0) | 2018.03.11 |
[머신러닝-Tensorflow] Lec-02 Linear Regression (0) | 2018.03.11 |
[머신러닝-Tensorflow] Lec-01 Mac Tensorflow Setting(virtualenv) (0) | 2018.03.11 |
[머신러닝-Tensorflow] Lec-00 ML 기본 개념 (0) | 2018.03.10 |
- Total
- Today
- Yesterday
- 백준알고리즘
- Spring
- 텐서플로우
- 스프링
- 백준
- TensorFlow
- 리버싱
- db
- 머신러닝
- 프로그래밍
- 노드
- 알고리즘
- Android
- BFS
- MVC
- 안드로이드
- 복습
- node.js
- Controller
- programming
- 감자개발자
- C언어
- 감자코딩
- node
- 학교
- 코드엔진
- 초보자를 위한 C언어 300제
- 개발하는 관광이
- Algorigm
- C langauge
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |