ML/[강의] 딥러닝 CNN
경사하강법을 이용한 선형회귀 구현 (1)
spring_sunshine
2022. 10. 28. 16:46
보스턴 주택 가격 데이터 세트를 퍼셉트론 기반에서 학습 및 테스트
- 피쳐: 14개 - 이 중 2개만 사용 (RM: 방 개수 / LSTAT: 하위계층 비율)
- 타겟: 주택 가격
1) Weight와 Bias의 업데이트된 값을 계산하는 함수 생성
- w1: RM 피처의 Weight 값
- w2: LSTAT 피처의 Weight 값
- bias는 Bias
- N은 입력 데이터 건수
# gradient_descent()함수에서 반복적으로 호출되면서 update될 weight/bias 값을 계산하는 함수.
# rm은 RM(방 개수), lstat(하위계층 비율), target은 PRICE임. 전체 array가 다 입력됨.
# 반환 값은 weight와 bias가 update되어야 할 값과 Mean Squared Error 값을 loss로 반환.
def get_update_weights_value(bias, w1, w2, rm, lstat, target, learning_rate=0.01):
# 데이터 건수
N = len(target)
# 예측 값.
predicted = w1 * rm + w2*lstat + bias
# 실제값과 예측값의 차이
diff = target - predicted
# bias 를 array 기반으로 구하기 위해서 설정.
bias_factors = np.ones((N,))
# weight와 bias를 얼마나 update할 것인지를 계산.
w1_update = -(2/N)*learning_rate*(np.dot(rm.T, diff))
w2_update = -(2/N)*learning_rate*(np.dot(lstat.T, diff))
bias_update = -(2/N)*learning_rate*(np.dot(bias_factors.T, diff))
# Mean Squared Error값을 계산.
mse_loss = np.mean(np.square(diff))
# weight와 bias가 update되어야 할 값과 Mean Squared Error 값을 반환.
return bias_update, w1_update, w2_update, mse_loss
2) Gradient Descent를 적용하는 함수 생성
→ RM, LSTAT feature array와 PRICE target array를 입력 받아서 iter_epochs수만큼 반복적으로 Weight와 Bias를 update적용.
def gradient_descent(features, target, iter_epochs=1000, verbose=True):
# w1, w2는 numpy array 연산을 위해 1차원 array로 변환하되 초기 값은 0으로 설정
# bias도 1차원 array로 변환하되 초기 값은 1로 설정.
w1 = np.zeros((1,))
w2 = np.zeros((1,))
bias = np.zeros((1, ))
print('최초 w1, w2, bias:', w1, w2, bias)
# learning_rate와 RM, LSTAT 피처 지정. 호출 시 numpy array형태로 RM과 LSTAT으로 된 2차원 feature가 입력됨.
learning_rate = 0.01
rm = features[:, 0]
lstat = features[:, 1]
# iter_epochs 수만큼 반복하면서 weight와 bias update 수행.
for i in range(iter_epochs):
# weight/bias update 값 계산
bias_update, w1_update, w2_update, loss = get_update_weights_value(bias, w1, w2, rm, lstat, target, learning_rate)
# weight/bias의 update 적용.
w1 = w1 - w1_update
w2 = w2 - w2_update
bias = bias - bias_update
if verbose:
print('Epoch:', i+1,'/', iter_epochs)
print('w1:', w1, 'w2:', w2, 'bias:', bias, 'loss:', loss)
return w1, w2, bias
3) Gradient Descent 적용
- 신경망은 데이터 정규화 / 표준화 작업을 미리 선행해야 한다.
- 이를 위해 신경망의 MinMaxScaler를 이용하여 개별 feature 값은 0~1 사이 값으로 변환 후 학습을 적용하자.
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaled_features = scaler.fit_transform(bostonDF[['RM', 'LSTAT']])
w1, w2, bias = gradient_descent(scaled_features, bostonDF['PRICE'].values, iter_epochs=5000, verbose=True)
print('##### 최종 w1, w2, bias #######')
print(w1, w2, bias)