## 첫λ²μ§Έ μ
import tensorflow as tf
## λλ²μ§Έ μ
print(tf.__version__)
## μΈλ²μ§Έ μ
(μ μ
μμ λ²μ μ΄ 2.15.0μ΄ μλλ©΄ νμ¬μ ν
μνλ‘μ°λ₯Ό μλμ κ°μ΄ μμ )
!pip uninstall tensorflow
## λ€λ²μ§Έ μ
- μμ κ° μλ£λλ©΄ μλμ κ°μ΄ 2.15.0 λ²μ μ μ¬μ€μΉ
!pip install tensorflow==2.15.0
## λ€μ―λ²μ§Έ μ
- λ€μ λ²μ νμΈ
print(tf.__version__)
## μ¬μ―λ²μ§Έ μ
# λ°μ΄ν° μ μ
height = tf.constant([170, 180, 175, 160], dtype=tf.float32)
foot_size = tf.constant([260, 270, 265, 255], dtype=tf.float32)
# λ³μ μ μ
a = tf.Variable(0.1) # μ΄κΈ°κ° 0.1
b = tf.Variable(0.2) # μ΄κΈ°κ° 0.2
# μμ€ ν¨μ μ μ
def loss_func():
loss = tf.constant(0.0, dtype=tf.float32) # loss μ΄κΈ°ν
for i in range(len(height)):
predicted = height[i] * a + b # μμΈ‘ κ°
loss += tf.square(foot_size[i] - predicted) # μμ€ κ³μ°
return loss
# κ²½μ¬νκ°λ² μ΅μ ν
opt = tf.keras.optimizers.Adam(learning_rate=0.1)
# νμ΅ λ£¨ν
for i in range(100):
opt.minimize(loss_func, var_list=[a, b]) # μμ€ ν¨μ μ΅μν
print(f"Step {i + 1}, a: {a.numpy()}, b: {b.numpy()}")