Learning Space/Machine Learning, Deep Learning

[Machine Learning] TensorFlow를 시작하며

JONNY ALOHA 2023. 6. 14. 14:47

본 문서는 TensorFlow 2.12.0 을 기준으로 작성하였습니다.

 

import tensorflow as tf

 

1.x와 2.x의 차이점

A. Session과 run의 사라짐

hello = tf.constant("hello, TensorFlow!")

print(hello)

2.0 이전까지는 Session 모듈을 실행해야만 연산이 가능했으나

2.0 이후부터는 Session 없이 선언과 동시에 실행이 되는 형태로 바뀌었다.

 

B. placeholder의 사라짐

2.0 이전에서는 아래와 같이

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
add_node = a + b

print(sess.run(add_node, feed_dict={a:3, b:2}))

placeholder 모듈을 이용해서 변수 선언 후 나중에 값을 할당하는 형태를 지원했다.

 

2.0 이후부터는

@tf.function
def add(a, b):
	return a + b
    
A = tf.constant(1)
B = tf.constant(2)

print(add(A, B))

@tf.function을 이용하여 함수를 정의하는 형태로 바뀌었다.

 

Tensor Ranks

# Scalar(magnitude only)
a = 100

# Vector(magnitude and direction)
v = [1.1, 2.2, 3.3]

# Matrix(table of numbers)
m = [[1,2,3], [4,5,6], [7,8,9]]

# 3-Tensor(cube of numbers)
t = [[[2], [4], [6]], [[6], [7], [8]], [[10], [12], [14]

# n-Tensor

Tensor Shapes

# Rank 0 & Dimension number 0-D
[]

# Rank 1 & Dimension number 1-D
[D0]

# Rank 2 & Dimension number 2-D
[D0, D1]

# Rank 3 & Dimension number 3-D
[D0, D1, D2]

# Rank n & Dimension number n-D
[D0, D1, D2, ---, Dn-1]

Tensor Types

# 32 bits floating point
tf.float32

# 64 bits floating point
tf.float64

# 8 bits signed integer
tf.int8

# 16 bits signed integer
tf.int16

# 32 bits signed integer
tf.int32

# 64 bits signed integer
tf.int64

 

728x90