pocket

[λ”₯λŸ¬λ‹] λ”₯λŸ¬λ‹ μ•Œμ•„λ³΄κΈ°2 - ν…μ„œ 생성, ν…μ„œ νƒ€μž…, ν…μ„œ νƒ€μž… λ³€ν™˜, ν…μ„œ μ—°μ‚°, λ‹€μ–‘ν•œ ν•¨μˆ˜ 정리 λ³Έλ¬Έ

Deep Learning

[λ”₯λŸ¬λ‹] λ”₯λŸ¬λ‹ μ•Œμ•„λ³΄κΈ°2 - ν…μ„œ 생성, ν…μ„œ νƒ€μž…, ν…μ„œ νƒ€μž… λ³€ν™˜, ν…μ„œ μ—°μ‚°, λ‹€μ–‘ν•œ ν•¨μˆ˜ 정리

jpocket 2025. 6. 13. 09:00
λ°˜μ‘ν˜•

πŸ›  Tensor? ν…μ„œλž€?

λ”₯λŸ¬λ‹μ—μ„œ 데이터λ₯Ό ν‘œν˜„ν•˜κ³  κ³„μ‚°ν•˜κΈ° μœ„ν•œ κΈ°λ³Έ λ‹¨μœ„λ‘œ

λ”₯λŸ¬λ‹ 연산은 λͺ¨λ‘ ν…μ„œ μ—°μ‚°μœΌλ‘œ 이루어진닀.

 

ν…μ„œμ˜ 차원에 따라 이름이 λ‹€λ₯΄λ‹€.

ν…μ„œ 차원 μ„€λͺ… μ˜ˆμ‹œ μ‹€μ œ 상황 λΉ„μœ 
0차원 (슀칼라) 숫자 ν•˜λ‚˜, 크기 μ—†μŒ 5, 3.14 μ˜¨λ„κ³„μ˜ ν˜„μž¬ μ˜¨λ„ 22도
1차원 (벑터) μˆ«μžλ“€μ˜ λ‚˜μ—΄, μ„ ν˜• λ°°μ—΄ [5, 7, 9] μ‹œν—˜ 점수 리슀트: μˆ˜ν•™, μ˜μ–΄, κ³Όν•™
2차원 (ν–‰λ ¬) ν–‰κ³Ό 열이 μžˆλŠ” 데이터 ν…Œμ΄λΈ” [[1, 2], [3, 4]] μ—‘μ…€ ν‘œμ²˜λŸΌ (학생 x κ³Όλͺ© 점수)
3차원 (큐브) μ—¬λŸ¬ 개의 ν–‰λ ¬ → 쒅이 λ­‰μΉ˜ λŠλ‚Œ [[[1,2], [3,4]], [[5,6], [7,8]]] 흑백 이미지 μ—¬λŸ¬ μž₯ (예: 100μž₯의 28x28 사진)
4차원 각 λ°μ΄ν„°λ§ˆλ‹€ μ±„λ„κΉŒμ§€ ν¬ν•¨λœ ꡬ쑰 (batch, channel, height, width) RGB 컬러 이미지 100μž₯(100μž₯ × 3채널 × 28×28 ν”½μ…€)
5차원 μ‹œν€€μŠ€λ‚˜ μ‹œκ°„ μš”μ†Œκ°€ μžˆλŠ” 경우 (batch, time, channel, height, width)
(10, 100, 3, 28, 28)
10개의 λΉ„λ””μ˜€(=μ‹œν€€μŠ€)각 λΉ„λ””μ˜€μ— 100ν”„λ ˆμž„κ° ν”„λ ˆμž„μ€ RGB 이미지

 

 

 

πŸ”₯ν…μ„œ 생성

ν•¨μˆ˜/λ©”μ„œλ“œ κΈ°λŠ₯ μš”μ•½
tf.constant(value) μž…λ ₯κ°’μœΌλ‘œλΆ€ν„° TensorFlow ν…μ„œ 생성
tf.rank(tensor) ν…μ„œμ˜ 차원(μΆ•μ˜ 개수) λ°˜ν™˜

 

t0 = tf.constant(1) 
print(t0)
print(tf.rank(t0))

# tf.Tensor(1, shape=(), dtype=int32)
# tf.Tensor(0, shape=(), dtype=int32)


t1 = tf.constant([1, 2, 3]) 
print(t1)
print(tf.rank(t1))

# tf.Tensor([1 2 3], shape=(3,), dtype=int32)
# tf.Tensor(1, shape=(), dtype=int32)


t2 = tf.constant([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])
print(t2)
print(tf.rank(t2))

# tf.Tensor(
# [[1 2 3]
# [4 5 6]
# [7 8 9]], shape=(3, 3), dtype=int32)
# tf.Tensor(2, shape=(), dtype=int32)


t3 = tf.constant([
    [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]],  
    [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]],   
    [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]   
])
print(t3)
print(tf.rank(t3))
# tf.Tensor(
# [[[1 2 3]
#  [4 5 6]
#  [7 8 9]]
# 
# [[1 2 3]
#  [4 5 6]
#  [7 8 9]]
# 
# [[1 2 3]
#  [4 5 6]
#  [7 8 9]]], shape=(3, 3, 3), dtype=int32)
# tf.Tensor(3, shape=(), dtype=int32)

 

i = tf.constant(2)
print(i)

# tf.Tensor(2, shape=(), dtype=int32)


f = tf.constant(2.)
print(f)

# tf.Tensor(2.0, shape=(), dtype=float32)


s = tf.constant('Seoul')
print(s)

# tf.Tensor(b'Seoul', shape=(), dtype=string)


f16 = tf.constant(2., dtype=tf.float16)
print(f16)

# tf.Tensor(2.0, shape=(), dtype=float16)


i8 = tf.constant(2, dtype=tf.int8)
print(i8)

# tf.Tensor(2, shape=(), dtype=int8)

 

 

 

πŸ”₯ν…μ„œ νƒ€μž…

좜처: λͺ¨λ‘μ˜ μ—°κ΅¬μ†Œ

 

 

 

πŸ”₯ν…μ„œ νƒ€μž… λ³€ν™˜

f32 = tf.cast(f16, tf.float32)
print(f32)
# tf.Tensor(2.0, shape=(), dtype=float32)


i32 = tf.cast(i8, tf.int32)
print(f32)
# tf.Tensor(2.0, shape=(), dtype=float32)

 

 

 

πŸ”₯ν…μ„œμ˜ μ—°μ‚°

μ„œλ‘œ λ‹€λ₯Έ νƒ€μž…μ„ κ°€μ§€λŠ” ν…μ„œλŠ” 연산이 λ˜μ§€ μ•Šκ³  μ—λŸ¬ λ°œμƒ
λ”°λΌμ„œ νƒ€μž…μ„ λ™μΌν•˜κ²Œ λ³€ν™˜ν•΄μ£Όμ–΄μ•Ό 함

μ—°μ‚° 기호 TensorFlow ν•¨μˆ˜ μ„€λͺ…
+ tf.add() λ”ν•˜κΈ° μ—°μ‚°
- tf.subtract() λΉΌκΈ° μ—°μ‚°
* tf.multiply() κ³±ν•˜κΈ° μ—°μ‚°
/ tf.divide() λ‚˜λˆ„κΈ° μ—°μ‚°
@ tf.matmul() ν–‰λ ¬κ³± μ—°μ‚°
- tf.reduce_max() ν…μ„œ κ°’ 쀑 μ΅œλŒ€κ°’
- tf.argmax() μ΅œλŒ€κ°’μ˜ μœ„μΉ˜(인덱슀) λ°˜ν™˜
# 0차원 ν…μ„œ μ—°μ‚°

print(tf.constant(2) + tf.constant(2))
print(tf.constant(2) - tf.constant(2))
print(tf.add(tf.constant(2), tf.constant(2)))
print(tf.subtract(tf.constant(2), tf.constant(2)))
# tf.Tensor(4, shape=(), dtype=int32)
# tf.Tensor(0, shape=(), dtype=int32)
# tf.Tensor(4, shape=(), dtype=int32)
# tf.Tensor(0, shape=(), dtype=int32)


print(tf.constant(2) * tf.constant(2))
print(tf.constant(2) / tf.constant(2))
print(tf.multiply(tf.constant(2), tf.constant(2)))
print(tf.divide(tf.constant(2), tf.constant(2)))
# tf.Tensor(4, shape=(), dtype=int32)
# tf.Tensor(1.0, shape=(), dtype=float64)
# tf.Tensor(4, shape=(), dtype=int32)
# tf.Tensor(1.0, shape=(), dtype=float64)


# μ„œλ‘œ λ‹€λ₯Έ νƒ€μž…μ„ κ°€μ§€λŠ” ν…μ„œλŠ” 연산이 λ˜μ§€ μ•Šκ³  μ—λŸ¬ λ°œμƒ
print(tf.constant(2) + tf.constant(2.2))
# error

# λ”°λΌμ„œ νƒ€μž…μ„ λ™μΌν•˜κ²Œ λ³€ν™˜ν•΄μ£Όμ–΄μ•Ό 함
print(tf.cast(tf.constant(2), tf.float32) + tf.constant(2.2))
# tf.Tensor(4.2, shape=(), dtype=float32)



# 1차원 ν…μ„œ μ—°μ‚°
a = tf.constant([1]) 
b = tf.constant([1])
print(a)
print(b)
# tf.Tensor([1], shape=(1,), dtype=int32)
# tf.Tensor([1], shape=(1,), dtype=int32)

print(a+b)
print(a-b)
print(a * b)
print(a / b)
# tf.Tensor([2], shape=(1,), dtype=int32)
# tf.Tensor([0], shape=(1,), dtype=int32)
# tf.Tensor([1], shape=(1,), dtype=int32)
# tf.Tensor([1.], shape=(1,), dtype=float64)



# 2차원 ν…μ„œμ˜ μ—°μ‚°
a = tf.constant([[1, 2, 3],
                 [4, 5, 6]])
b = tf.constant([[7,8],
                 [9,10],
                 [11,12]])
print(a)
print(b)
# tf.Tensor(
# [[1 2 3]
#  [4 5 6]], shape=(2, 3), dtype=int32)

# tf.Tensor(
# [[ 7  8]
#  [ 9 10]
#  [11 12]], shape=(3, 2), dtype=int32)

print(a @ b) # matrix multiplication
# tf.Tensor(
# [[ 58  64]
#  [139 154]], shape=(2, 2), dtype=int32)

print(tf.matmul(a, b))
# tf.Tensor(
# [[ 58  64]
#  [139 154]], shape=(2, 2), dtype=int32)

c = tf.constant([[4.0, 5.0, 6.0], 
                 [10.0, 9.0, 8.0]])

print(tf.reduce_max(c))
print(tf.argmax(c))
print(tf.nn.softmax(c))
# tf.Tensor(10.0, shape=(), dtype=float32)
# tf.Tensor([1 1 1], shape=(3,), dtype=int64)
# tf.Tensor(
# [[0.09003057 0.24472848 0.6652409 ]
#  [0.6652409  0.24472848 0.09003057]], shape=(2, 3), dtype=float32)

 

 

 

πŸ”₯ν…μ„œμ˜ λ‹€μ–‘ν•œ ν•¨μˆ˜λ“€

https://docs.pytorch.org/tutorials/beginner/introyt/tensors_deeper_tutorial.html

ν•¨μˆ˜ / μ½”λ“œ μ„€λͺ… μ˜ˆμ‹œ μ½”λ“œ κ²°κ³Ό μ„€λͺ…
tf.transpose(tensor) ν…μ„œμ˜ 차원 μˆœμ„œλ₯Ό λ°”κΏ”μ„œ μ „μΉ˜ tf.transpose([[1, 2], [3, 4]]) 2x2 행렬을 μ „μΉ˜ν•΄μ„œ (2,2) 크기 μœ μ§€
tf.reshape(tensor, shape) 전체 μ›μ†Œ μˆ˜λ₯Ό μœ μ§€ν•œ μ±„ ν…μ„œ λͺ¨μ–‘ λ³€κ²½ tf.reshape([1,2,3,4,5,6], (2,3)) 1차원 (6,) → 2차원 (2,3)
tf.squeeze(tensor) 차원이 1인 좕을 제거 (μ••μΆ•) tf.squeeze([[[1], [2], [3]]]) (3,1) → (3,)
tf.expand_dims(tensor, axis) μ§€μ •ν•œ μœ„μΉ˜μ— 1차원 μΆ• μΆ”κ°€ tf.expand_dims([1, 2, 3], axis=1) (3,) → (3,1)
tf.split(tensor, num_or_size_splits, axis) ν…μ„œλ₯Ό μ§€μ •ν•œ 좕을 κΈ°μ€€μœΌλ‘œ λΆ„ν•  tf.split([1,2,3,4], num_or_size_splits=2, axis=0) [ [1,2], [3,4] ]
tf.concat(values, axis) μ—¬λŸ¬ ν…μ„œλ₯Ό μ§€μ •ν•œ μΆ• κΈ°μ€€μœΌλ‘œ μ—°κ²° tf.concat([[1,2], [3,4]], axis=0) [1,2,3,4]

좜처: λͺ¨λ‘μ˜ μ—°κ΅¬μ†Œ

 

λ°˜μ‘ν˜•