Cost Function
: to fit the paramters(W)
: 주어진 데이터를 통한 학습을 통해서 최적의 parameter값을 찾을 수 있음
def loss_fn(hypothesis, labels): cost = -tf.reduce_mean(labels * tf.log(hypothesis) + (1 - labels) * tf.log(1 - hypothesis)) return cost
: Cost값을 최소화하기 위한 과정은 실제 가설을 통해 나온 값에서 실제 라벨 값의 차이가 가장 최소화 되어야 함. 0에 가까워야 함.
: 가설을 통해 나온 값은 sigmoid와 같은 0과 1에 속하는 실수형 값
: 실제 우리가 원하는 값은 0과 1이 딱 끊어지는 값
: 두 개의 값을 빼보면 구불구불한 그래프가 나타남.
cost = -tf.reduce_mean(labels * tf.log(hypothesis) + (1 - labels) * tf.log(1 - hypothesis))
Optimization
: How to minimize the cost function
: Gradient와 learning rate를 통해 찾을 수 있음
def grad(hypothesis, labels): with tf.GradientTape() as tape: loss_value = loss_fn(hypothesis, labels) return tape.gradient(loss_value, [W,b]) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) optimizer = apply_gradients(grads_and_vars=zip(grads,[W,b]))