C.W.K.
Stream
Lesson 01 of 06 · published

원소별 연산과 reduction

~11 min · ops, reduce, argmax

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

모든 loss function이 만들어지는 원자들

대부분 TF math는 원소별 작동해 — scalar 함수를 각 원소에 독립적으로 적용. 더하기, 빼기, 곱하기, 나누기, square, sqrt, exp, log, sin, sigmoid, tanh, ReLU. 이게 loss function이랑 activation이 만들어지는 원자들이야.

Reduction은 하나 이상의 axis를 축소해. tf.reduce_sum, tf.reduce_mean, tf.reduce_max, tf.reduce_min이 전역 (scalar 반환) 또는 지정 axis로 작동. keepdims=True 옵션은 축소된 차원을 size 1로 유지 — 원래 shape로 broadcast back할 때 핵심.

tf.argmax, tf.argmin은 max/min 원소의 index 반환 — classification에서 logit으로부터 예측 class 찾는 용도.

이게 왜 중요하냐면: MSE는 tf.reduce_mean(tf.square(y_true - y_pred)). cross-entropy는 tf.math.log + tf.reduce_sum. accuracy는 tf.argmax + tf.equal + tf.reduce_mean(tf.cast(...)). 이 원자들이 보이면 어떤 loss function도 읽을 수 있어.

Code

Element-wise ops·python
import tensorflow as tf

x = tf.constant([[1.0, 2.0], [3.0, 4.0]])
y = tf.constant([[5.0, 6.0], [7.0, 8.0]])

x + y                # element-wise add
x * y                # element-wise multiply (NOT matmul)
tf.pow(x, 2)         # x squared
tf.sqrt(x)
tf.math.exp(x)
tf.math.log(x)
tf.math.sigmoid(x)
tf.nn.relu(x)        # max(0, x)
Reductions·python
import tensorflow as tf

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

# Global → scalar
tf.reduce_sum(x)              # 21.0
tf.reduce_mean(x)             # 3.5

# Along axis → reduce that axis
tf.reduce_sum(x, axis=0)      # [5, 7, 9]
tf.reduce_sum(x, axis=1)      # [6, 15]

# keepdims preserves shape for broadcasting back
tf.reduce_sum(x, axis=1, keepdims=True)  # [[6], [15]] — shape (2, 1)

# argmax — used everywhere in classification
tf.argmax(x, axis=1)          # [2, 2] — index of max in each row

External links

Exercise

tf.subtract, tf.square, tf.reduce_mean만 써서 mean_squared_error(y_true, y_pred) 직접 구현. 랜덤 데이터로 tf.keras.losses.mse(y_true, y_pred)와 일치하는지 확인.

Progress

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

댓글 0

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

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