πŸ“„ 211221_DL_(6).ν…μ„œcost

πŸ“„ 211221_DL_(7).ν…μ„œgradient

🐿️ ν…μ„œ cost

import tensorflow as tf
import numpy as np

x_data=[1,2,3]
y_data=[1,2,3]

w=3
x=tf.constant(x_data, tf.float32)
y=tf.constant(y_data, tf.float32)
hx=w*x #tf.multiply(w,x) => [3,6,9] 와 동일
sq=tf.square(hx-y) # (hx-y) **2 와 동일
cost=tf.reduce_mean(sq)
cost.numpy()

18.666666

🍫 ν•¨μˆ˜λ‘œ 생성

def cost(w):
    hx=w*x
    cost=tf.reduce_mean((hx-y)**2)
    return cost
cost(3).numpy()

18.666666

🐿️ ν…μ„œ gradient

import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras.optimizers import SGD

x_data=np.array([1,2,3,4,5])
y_data=np.array([3,5,7,9,11])

x=tf.constant(x_data, tf.float32)
y=tf.constant(y_data, tf.float32)

w=tf.Variable(10.0)
b=tf.Variable(10.0)
def compute_cost():
    hx=w*x+b
    cost=tf.reduce_mean((hx-y)**2)
    return cost