Notice
Recent Posts
Recent Comments
Link
- Today
- Total
μΌ | μ | ν | μ | λͺ© | κΈ | ν |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- νμ΄μ¬
- 2μ£Όμ°¨
- 1μ£Όμ°¨
- λ°μΈ4κΈ°
- python
- Jupyter Notebook
- error
- νκ³
- data
- github
- μ£ΌνΌν°λ ΈνΈλΆ
- νμ΄μ¬λ¬Έλ²
- ML
- ν¨μνμ©
- AI
- λ₯λ¬λ
- λ³μ
- λͺ¨λμμ°κ΅¬μ
- ν¨μ
- λ°μ΄ν°
- λ°μ΄ν°μ¬μ΄μΈν°μ€νΈ
- λ°μ΄ν°λ² μ΄μ€
- sql
- νκ³ λ‘
- λμ λ리
- bigquery
- λΉ μΏΌλ¦¬
- κ°λ μ 리
- κΉνλΈ
- λ¨Έμ λ¬λ
Archives
[λ₯λ¬λ] λ₯λ¬λ μμ보기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] |
λ°μν