π 211221_DL_(6).ν μcost
π 211221_DL_(7).ν μgradient
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
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