ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • TensorFlow 기초 9 - 선형회귀 모형 작성 = 수식 사용(Keras 없이 tensorflow만 사용) - GradientTape
    TensorFlow 2022. 11. 29. 17:32

     

     

     

    # 선형회귀 모형 작성 : 수식 사용
    # tensorflow 사용
    
    import tensorflow as tf
    import numpy as np
    from keras.optimizers import SGD, RMSprop, Adam
    
    x = [1., 2., 3., 4., 5.]
    y = [1.2, 2.8, 3.0, 3.5, 6.0]
    
    print(np.corrcoef(x, y)) # 상관계수(r) = 0.937
    # 인과 관계가 있다 가정하고 회귀분석 작업을 진행
    
    tf.random.set_seed(123)
    w = tf.Variable(tf.random.normal((1,)))
    b = tf.Variable(tf.random.normal((1,)))
    print(w.numpy(), ' ', b.numpy())
    
    # 선형회귀식을 얻기 위해 cost를 줄여가는 작업
    opti = SGD()
    
    @tf.function
    def train_func(x, y):
        with tf.GradientTape() as tape:
            hypo = tf.add(tf.multiply(w, x), b) # y = wx + b, 예측값
            loss = tf.reduce_mean(tf.square(tf.subtract(hypo, y))) # subtract 빼기 함수
            
        grad = tape.gradient(loss, [w, b]) # 자동 미분을 해 줌
        opti.apply_gradients(zip(grad, [w, b]))
        return loss
            
    w_vals = []
    cost_vals = []
    
    for i in range(1, 101): # epochs
        loss_val = train_func(x, y)
        cost_vals.append(loss_val.numpy())
        w_vals.append(w.numpy())
        if i % 10 == 0: # 10의 배수일 경우에만 출력
            print(loss_val)
    
    print('cost :', cost_vals)
    print('w :', w_vals)
    
    print()
    # w, cost를 시각화
    import matplotlib.pyplot as plt
    plt.plot(w_vals, cost_vals, 'o--')
    plt.xlabel('w')
    plt.ylabel('cost')
    plt.show()
    
    print('cost가 최소일 때 w :', w.numpy())
    print('cost가 최소일 때 b :', b.numpy()) # yhat = 0.90847826 * x + 0.6487321
    
    # 선형회귀식으로 시각화
    y_pred = tf.multiply(x, w) + b
    print('y_pred :', y_pred)
    print('y :', y)
    
    plt.scatter(x, y, label='real')
    plt.plot(x, y_pred, 'b-', label='pred')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend()
    plt.show()
    
    print()
    # 새 값으로 정량적 예측
    new_x = [3.5, 9.7]
    new_pred = tf.multiply(new_x, w) + b
    print('예측값 :', new_pred.numpy())
    
    
    <console>
    [[1.         0.93713845]
     [0.93713845 1.        ]]
    [-0.8980837]   [0.33875433]
    tf.Tensor(0.664091, shape=(), dtype=float32)
    tf.Tensor(0.35622105, shape=(), dtype=float32)
    tf.Tensor(0.35087812, shape=(), dtype=float32)
    tf.Tensor(0.34716052, shape=(), dtype=float32)
    tf.Tensor(0.34369203, shape=(), dtype=float32)
    tf.Tensor(0.34045076, shape=(), dtype=float32)
    tf.Tensor(0.3374218, shape=(), dtype=float32)
    tf.Tensor(0.3345912, shape=(), dtype=float32)
    tf.Tensor(0.33194596, shape=(), dtype=float32)
    tf.Tensor(0.32947397, shape=(), dtype=float32)
    cost : [39.713856, 23.294338, 13.725664, 8.149324, 4.899521, 3.0055096, 1.9015872, 1.2580913, 0.88291013, 0.664091, 0.5363928, 0.46179694, 0.4181475, 0.39253327, 0.37743038, 0.36845425, 0.36304945, 0.3597273, 0.35761958, 0.35622105, 0.35523695, 0.35449535, 0.35389638, 0.35338143, 0.35291666, 0.35248226, 0.3520667, 0.35166317, 0.35126784, 0.35087812, 0.35049295, 0.35011154, 0.34973326, 0.34935778, 0.34898525, 0.34861523, 0.3482477, 0.3478829, 0.3475204, 0.34716052, 0.34680304, 0.3464479, 0.3460952, 0.3457449, 0.34539694, 0.34505135, 0.34470809, 0.3443671, 0.34402838, 0.34369203, 0.34335798, 0.34302616, 0.34269652, 0.34236914, 0.34204403, 0.34172112, 0.3414003, 0.34108162, 0.34076506, 0.34045076, 0.3401385, 0.3398285, 0.33952048, 0.33921456, 0.33891064, 0.3386088, 0.33830896, 0.33801132, 0.33771548, 0.3374218, 0.33712998, 0.33684015, 0.33655232, 0.33626643, 0.33598256, 0.33570048, 0.3354203, 0.33514208, 0.33486572, 0.3345912, 0.3343185, 0.3340477, 0.3337787, 0.33351144, 0.33324623, 0.33298248, 0.3327208, 0.33246067, 0.33220252, 0.33194596, 0.33169118, 0.33143792, 0.3311866, 0.33093697, 0.33068895, 0.3304426, 0.33019796, 0.32995504, 0.32971364, 0.32947397]
    w : [array([-0.48163053], dtype=float32), array([-0.1635837], dtype=float32), array([0.07934122], dtype=float32), array([0.26491904], dtype=float32), array([0.4067187], dtype=float32), array([0.5150985], dtype=float32), array([0.5979658], dtype=float32), array([0.6613568], dtype=float32), array([0.70987964], dtype=float32), array([0.7470519], dtype=float32), array([0.7755589], dtype=float32), array([0.7974506], dtype=float32), array([0.81429183], dtype=float32), array([0.82727724], dtype=float32), array([0.8373187], dtype=float32), array([0.8451124], dtype=float32), array([0.85118973], dtype=float32), array([0.85595644], dtype=float32), array([0.8597222], dtype=float32), array([0.86272335], dtype=float32), array([0.8651405], dtype=float32), array([0.8671113], dtype=float32), array([0.86874104], dtype=float32), array([0.8701099], dtype=float32), array([0.8712793], dtype=float32), array([0.8722959], dtype=float32), array([0.8731955], dtype=float32), array([0.87400544], dtype=float32), array([0.8747464], dtype=float32), array([0.8754343], dtype=float32), array([0.8760813], dtype=float32), array([0.8766967], dtype=float32), array([0.87728757], dtype=float32), array([0.87785923], dtype=float32), array([0.8784159], dtype=float32), array([0.87896067], dtype=float32), array([0.8794959], dtype=float32), array([0.88002354], dtype=float32), array([0.88054496], dtype=float32), array([0.8810612], dtype=float32), array([0.88157314], dtype=float32), array([0.88208133], dtype=float32), array([0.8825863], dtype=float32), array([0.8830884], dtype=float32), array([0.88358796], dtype=float32), array([0.8840851], dtype=float32), array([0.88458014], dtype=float32), array([0.88507307], dtype=float32), array([0.885564], dtype=float32), array([0.8860531], dtype=float32), array([0.88654035], dtype=float32), array([0.88702583], dtype=float32), array([0.8875095], dtype=float32), array([0.88799155], dtype=float32), array([0.88847184], dtype=float32), array([0.88895047], dtype=float32), array([0.8894275], dtype=float32), array([0.88990283], dtype=float32), array([0.89037657], dtype=float32), array([0.8908487], dtype=float32), array([0.8913192], dtype=float32), array([0.8917881], dtype=float32), array([0.8922554], dtype=float32), array([0.8927212], dtype=float32), array([0.8931853], dtype=float32), array([0.8936479], dtype=float32), array([0.8941089], dtype=float32), array([0.8945683], dtype=float32), array([0.8950262], dtype=float32), array([0.89548254], dtype=float32), array([0.8959373], dtype=float32), array([0.8963906], dtype=float32), array([0.89684236], dtype=float32), array([0.89729255], dtype=float32), array([0.89774126], dtype=float32), array([0.8981884], dtype=float32), array([0.8986341], dtype=float32), array([0.8990782], dtype=float32), array([0.8995208], dtype=float32), array([0.89996195], dtype=float32), array([0.9004016], dtype=float32), array([0.90083975], dtype=float32), array([0.90127647], dtype=float32), array([0.9017117], dtype=float32), array([0.90214545], dtype=float32), array([0.9025777], dtype=float32), array([0.9030085], dtype=float32), array([0.90343785], dtype=float32), array([0.90386575], dtype=float32), array([0.9042922], dtype=float32), array([0.9047172], dtype=float32), array([0.90514076], dtype=float32), array([0.9055629], dtype=float32), array([0.9059836], dtype=float32), array([0.90640295], dtype=float32), array([0.90682083], dtype=float32), array([0.9072373], dtype=float32), array([0.9076524], dtype=float32), array([0.90806603], dtype=float32), array([0.90847826], dtype=float32)]
    
    cost가 최소일 때 w : [0.90847826]
    cost가 최소일 때 b : [0.6487321]
    y_pred : tf.Tensor([1.5572104 2.4656887 3.374167  4.282645  5.1911235], shape=(5,), dtype=float32)
    y : [1.2, 2.8, 3.0, 3.5, 6.0]
    
    예측값 : [3.828406 9.460971]

    Keras를 사용하지 않고 tensorflow로만 사용하여 선형회귀식을 구현하였다. 

    tensorflow 2.0 버전부터 GradientTape()을 사용할 수 있으므로 그것을 사용하였다.

     

     

     

     

    cost와 w(기울기) 시각화

     

     

    예측값과 실제값

     

     

    댓글

Designed by Tistory.