C.W.K.
Stream
Lesson 03 of 07 · published

Tensor 속성: Shape, Dtype, Rank, Device

~10 min · shape, dtype, device, introspection

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

천 번은 돌릴 네 가지 점검

거의 모든 TF 디버깅 세션은 헷갈리는 거에 .shape.dtype 호출하면서 시작해. 이 속성들은 계산 안 돌려 — 메타데이터고 즉시 읽혀.

shape에 None 들어갈 수 있어. dynamic 차원이라는 뜻. model input spec에서 batch size나 sequence length가 graph build 시점에 고정 안 됐을 때 흔해. model.summary() 출력에서 (None, 224, 224, 3)는 "아무 batch size, 224×224 RGB image"라는 뜻.

.device 속성은 tensor가 어디 사는지 — CPU냐 GPU냐. GPU tensor에 .numpy() 부르면 CPU로 암묵적 복사가 일어나. 가끔 inspection엔 괜찮지만 tight loop에서는 비싸.

Code

Tensor introspection·python
import tensorflow as tf

t = tf.constant([[1.0, 2.0, 3.0],
                 [4.0, 5.0, 6.0]])

print(t.shape)             # TensorShape([2, 3])
print(t.shape[0])          # 2
print(t.shape[-1])         # 3
print(t.shape.as_list())   # [2, 3] — Python list, useful for math

print(t.ndim)              # 2
print(t.dtype)             # <dtype: 'float32'>
print(tf.size(t))          # tf.Tensor(6, ...)
print(t.device)            # /job:localhost/.../device:CPU:0

arr = t.numpy()
print(type(arr), arr.shape)  # <class 'numpy.ndarray'> (2, 3)
Why shapes contain None·python
# Inside a model summary you'll see (None, 224, 224, 3).
# None = dynamic dimension, decided at runtime.
# Frequently the batch dimension — same architecture, any batch size.

# When in doubt, force a fixed shape so error messages are louder
@tf.function(input_signature=[tf.TensorSpec(shape=[None, 784], dtype=tf.float32)])
def predict(x):
    return model(x)

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.