본문 바로가기

AI - ML

ML lab 01 - TensorFlow의 설치및 기본적인 operations (new)

### Why TensorFlow?

텐서플로는 오픈소스 기반으로 다른 라이브러리에 비해 2~4배 정도의 코드와 자료들이 있음.

상대적으로 접근이 쉬운 것이 장점.



### What is a Data Flow Graph?

노드 그래프 기반 오퍼레이션.



### Installing TensorFlow

1. python 설치

2. pip install tensoflow==1.5.0 // 아마 geforce 계열 gpu사용이 기본이겠지?

   인텔의 경우 샌디브릿지부터 AVX를 지원함. 암드의 경우 재규어, 퓨마, 불도저 시리즈부터.

   그 밑에 CPU는 텐서플로 1.5버전을 받아야함. // 회사 CPU가 고대 유물이라 1.5버전 받음.

   // 추가 수정 : 회사에 텐서플로용 1000만원짜리 서버가 있었음. 이걸 학습자들끼리 공유하여 사용하기로 결정!

3. bazel ... // 컴파일러

4. Google search / Community help



### Check installation and version

1. $ python3


1
2
import tensorflow as tf
tf.__version__

>> '1.12.0'


### Hello TensorFlow!

1
2
3
hello = tf.constant("Hello, TensorFlow!")
sess = tf.Session()
print(sess.run(hello))

>>  b'Hello, TensorFlow!' // b는 Bytes literals
1 번째 줄에서 객체를 할당하는 아주 쉬운 과정으로 보이나 실제로는 그래프 안에 노드를 생성하는 과정.

### Computational Graph
1
2
3
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0)
node3 = tf.add(node1, node2)
1
2
print("node1:", node1, "node2:", node2)
print("node3:", node3)
>> 
1
2
node1: Tensor("Const_6:0", shape=(), dtype=float32) node2: Tensor("Const_7:0", shape=(), dtype=float32)
node3: Tensor("Add:0", shape=(), dtype=float32)
1
2
3
sess = tf.Session()
print("sess.run(node1, node2):", sess.run([node1, node2]))
print("sess.run(node3): ", sess.run(node3))
>>
1
2
sess.run(node1, node2): [3.04.0]
sess.run(node3):  7.0


### TensorFlow Mechanics
1. Build graph using TensorFlow operations // 그래프를 정의함
2. feed data and run graph(operation) - sess.run(op) // 그래프에 값을 넘겨주고 세션을 통해 실행함
3. update variables in the graph(and return values) // 내부 변수를 업데이트하거나 출력값을 출력함

### Placeholder
1
2
3
4
5
6
= tf.placeholder(tf.float32)
= tf.placeholder(tf.float32)
adder_node = a + b
 
print(sess.run(adder_node, feed_dict={a: 3, b: 4.5}))
print(sess.run(adder_node, feed_dict={a: [1,3], b: [2,4]}))
>>
1
2
7.5
[3. 7.] // 아니 배열 연산이 가능하다고!!??? 스고이! 어썸!



### Everything is Tensor

Tensor를 이해하면 ML, DL을 잘 활용하는 것이 가능.

Tensor는 기본적으로 Array이다.



### Tensor Ranks, Shapes, and Types

Ranks - 차원

- Scalar // 0차

- Vector // 1차

- Matrix // 2차

- 3-Tensor // 3차

- n-Tensor // n차

Shapes - element에 몇개가 들어있는지

ex) t = [[1,2,3], [4,5,6], [7,8,9]] => [3, 3] / [D0, D1] / 2-D

Types - tf.float32 를 가장 많이 사용. 다른 언어와 동일함.



### 참고

https://www.youtube.com/watch?v=-57Ne86Ia8w&index=3&list=PLlMkM4tgfjnLSOjrEJN31gZATbcj_MpUm // 영상을 토대로 글을 작성함