3 minute read


개인적으로 공부한 내용을 조금 더 추가하였습니다.


1. 모두를 위한 딥러닝 Lab2 - Linear regression

Linear regression 코드입니다.

import numpy as np
import tensorflow as tf

tf.__version__ # '2.8.0'

x_train = [1, 2 ,3, 4]
y_train = [0, -1, -2, -3]

tf.model = tf.keras.Sequential() # 훈련과 추론이 가능한 tf.keras.Model 객체를 반환합니다. Sequential을 통해 신경망 모델을 만들고 이 모델에 Layer를 쌓는 방법으로 사용할 수 있습니다.
tf.model.add(tf.keras.layers.Dense(units=1, input_dim=1)) #unist == 해당 은닉층에서 활동하는 뉴런의 수, input_dim == 입력층의 뉴런 수 || units == output shape, input_dim == input shape

sgd = tf.keras.optimizers.SGD(lr=0.1) # SGD == standard gradient descendent, lr == learning rate
tf.model.compile(loss='mse', optimizer=sgd) # mse == mean_squared_error, 1/m * sig (y'-y)^2, compile메소드는 학습 과정을 설정하는 메소드입니다. (모델 빌드 및 실행 전의 훈련 준비 단계)

# prints summary of the model to the terminal
tf.model.summary()

# fit() executes training
tf.model.fit(x_train, y_train, epochs=200) # 주어진 epochs 수 만큼 모델을 학습시킵니다.

# predict() returns predicted value
y_predict = tf.model.predict(np.array([5, 4])) # 임의의 입력에 대한 모델의 출력값을 확인합니다.
print(np.array([5,4]))
print(y_predict)

y_predict의 값은 \([[-3.9981258] [-2.9990356]]\) 로 나오게 됩니다.

image

그래프로 보았을 때 y의 예측값이 거의 정확하게 나오는 것을 확인할 수 있습니다.



2. 모두를 위한 딥러닝 Lab3 - Linear regression에서 Cost function 최소화를 Tensorlfow2.0으로 구현

사실 강의와 좀 다른게, TF2.0으로 구현된 깃허브의 코드를 보면 matplot을 임포트해서 그래프를 보여주는 코드가 추가된 것 밖에 없습니다. 게다가 강의에서는 w에 대한 Cost를 보여주는 그래프라면, 새로 개편된 코드에서는 epochs에 대한 cost(Loss)의 그래프입니다. 그래서 내용을 조금 더 추가하여 epoch값에 따른 cost plot과 w값에 따른 cost plot을 그려보기로 했습니다.

2.1 epoch에 따른 Cost(Loss) plot 그리기

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

x_train = [1, 2, 3, 4]
y_train = [0, -1, -2, -3]

tf.model = tf.keras.Sequential()
tf.model.add(tf.keras.layers.Dense(units=1, input_dim=1))

sgd = tf.keras.optimizers.SGD(lr=0.1)
tf.model.compile(optimizer = sgd, loss = 'mse')

tf.model.summary()

history = tf.model.fit(x_train, y_train, epochs=100)

y_predict = tf.model.predict(np.array([5, 4]))
print(y_predict)

print(history.history.keys())

#Plot training & validation loss values
plt.plot(history.history['loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left') # 범례 추가하기. 범례란 데이터의 종류를 표기하기 위한 텍스트이다.
plt.show()

history 객체에 대하여

2.3 epoch가 아닌 W에 대한 Cost Function plot 그리기

해당 블로그에서 코드를 참조했습니다.

Hypothesis

\[H(x) = Wx \]

Cost function

\[cost(W) = \frac{1}{m} \sum_{i=1}^m (Wx_i - y_i)^2 \]

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

print("TensorFlow Version: %s" % (tf.__version__))

X = np.array([1,2,3,4,5])
Y = np.array([1,2,3,4,5])

# Cost function 정의
def cost_func(W, X, Y):
    err = 0
    for i in range(len(X)):
        err += (W * X[i] - Y[i]) ** 2
    cost = err / len(X)

    # 또는 TF2.0에서는 다음과 같이 사용가능합니다
    # hypothesis = W * X
    # cost = tf.reduce_mean(tf.square(hypothesis-Y))

    return cost

cost_list = []
W_range = np.linspace(-2, 4, num = 100)

for feed_W in W_range:
    curr_cost = cost_func(feed_W, X, Y) # W에 대한 cost를 계산해서 curr_cost에 값을 반환한다.
    cost_list.append(curr_cost)
    
    if len(cost_list) % 20 == 0:
            print("W: %s \nCost: %s \n" % (feed_W, curr_cost))

minimum_index = cost_list.index(min(cost_list))
optimal_W = W_range[minimum_index]
optimal_cost = cost_list[minimum_index]

print("Optimal W : %s" % optimal_W)
print("Optimal Cost: %s" % optimal_cost)

plt.title("Cost according to W", size=15)
plt.plot(W_range, cost_list, color='orange')
plt.axvline(x = optimal_W, color = 'red')
plt.xlabel("Weights")
plt.ylabel("Cost")
plt.show()

2.4 TF2.0 버전으로 Cost Function, Gradient Descent 구현하기

\[Cost(W) = {1\over m} \sum_{i=1}^m (Wx_i - y_i)^2 \] \[W:= W - \alpha{1\over m} \sum_{i=1}^m (Wx_i - y_i)x_i \]

# Gradient Descent을 정의하기 (1번 방식)
# Gradient Descen의 수식을 코드로 표현하면 다음과 같습니다.
alpha = 0.01 # learning_rate
gradient = tf.reduce_mean(tf.multiply(tf.multiply(W, X) - Y, X))
descent = W - tf.multiply(alpha, gradient)
W.assign(descent)

또는 다음과 Gradient Descent 구현을 다음과 같이 정의할 수 있습니다.

# Gradient Descent을 정의하기 (2번 방식)
learning_rate = 0.01
gradient = tf.reduce_mean((W * X - Y) * X)
descent = W - learning_radte * gradient
W.assign(descent)

W에 대한 Cost function의 도함수를 사용하여 가중치를 업데이트합니다.

tf.random.set_seed(2020)

x_data = [1,2,3,4,5]
y_data = [1,3,5,7,9]

W = tf.Variable(tf.random.normal([1], mean=0.0))

for step in range(300):

    hypothesis = W * X
    cost = tf.reduce_mean(tf.square(hypothesis-Y))

    alpha = 0.01
    gradient = tf.reduce_mean(tf.multiply(tf.multiply(W, X) - Y, X))
    descent = W - tf.multiply(alpha, gradient)
    W.assign(descent)

    if step % 50 == 0:
        print("#%s \t W: %s \t Cost: %s" % (step, W.numpy(), cost.numpy()))

참고자료

TF2.0으로 Cost function, gradient descent 구현 참고자료
tf.multiply vs tf.matmul
모두를 위한 딥러닝 Lab3 강의