728x90

기본 환경: IDE: VS code, Language: Python

 

Model Construction 이후, 성능에 대한 판단 필요 → Model Performance Indicator

 

1. MAE: Mean Absolute Error, 평균 절대 오차

실제 값과 예측 값의 차이(실제 값 - 예측 값)를 절대값으로 변환 후 평균화

 

2. MSE: Mean Squared Error, 평균 제곱 오차

실제 값과 예측 값의 차이를 제곱 후 평균화

 

데이터의 모형에 따른 MAE, MSE 선택
MAE
1. 이상치에 민감하지 않음
2. 데이터 모형의 범위가 크게 분산되어 있을 때 사용(과다 측정 예방)
MSE
1. 이상치에 민감함
2. 데이터 모형의 범위가 좁을 때 사용(과소 측정 보완)

 

 Sequential Model의 mae, mse 지표 확인

# mae_and_mse.py

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

from sklearn.model_selection import train_test_split


# 1. Data
x = np.array(range(1,21))
y = np.array([1,2,4,3,5,7,9,3,8,12,13,8,14,15,9,6,17,23,21,20])

x_train, x_test, y_train, y_test = train_test_split(
    x,y,
    train_size=0.7,
    shuffle=True,
    random_state=123
)


# 2. Model Construction
model = Sequential()
model.add(Dense(64, input_dim=1))
model.add(Dense(32))
model.add(Dense(16))
model.add(Dense(1))


# 3. compile and train
model.compile(loss='mae', optimizer='adam', metrics=['mse']) # metrics를 활용한 여러 지표 확인
model.fit(x_train, y_train, epochs=128, batch_size=5)


# 4. Evalueate and Predict
loss = model.evaluate(x_test, y_test)
print("Loss: ", loss)



'''
# Result

mae: 3.0775
mse: 15.3362

'''

 

3. RMSE: Root MSE, 평균 오차

root(MSE)

⚠️ RMSE 지표는 Sequential 모델에서는 사용할 수 없는 지표이므로, 함수를 정의해서 사용

 

 Sequential Model의 rmse 지표 확인

# rmse_def.py

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

from sklearn.model_selection import train_test_split


# 1. Data
x = np.array(range(1,21))
y = np.array([1,2,4,3,5,7,9,3,8,12,13,8,14,15,9,6,17,23,21,20])

x_train, x_test, y_train, y_test = train_test_split(
    x,y,
    train_size=0.7,
    shuffle=True,
    random_state=123
)


# 2. Model Construction
model = Sequential()
model.add(Dense(64, input_dim=1))
model.add(Dense(32))
model.add(Dense(16))
model.add(Dense(1))


# 3. compile and train
model.compile(loss='mae', optimizer='adam', metrics=['mse'])
model.fit(x_train, y_train, epochs=128, batch_size=5)


# 4. Evalueate and Predict
loss = model.evaluate(x_test, y_test)
print("Loss: ", loss)

y_predict = model.predict(x_test)


from sklearn.metrics import mean_squared_error


def RMSE (y_test, y_predict):
    return np.sqrt(mean_squared_error(y_test, y_predict))
# def function_name(para1, para2):
    # return np.sqrt(mse), root(MSE)

print("RMSE: ", RMSE(y_test, y_predict))



'''
Result

MAE: 2.9459493160247803
MSE: 14.699475288391113
RMSE: 3.8339895717187855


'''

 

4. MSLE: Mean Squared Log Error, 평균 로그 오차

log(MSE)

 

5. MAPE: Mean Absolute Percentage Error, 평균 절대 비율 오차

MAE*100%

 

6. MPE: Mean Percentage Error, 평균 비율 오차

MAPE에서 절대값을 제외한 지표

모델이 실제값보다 낮은지 높은지 판단 가능

→ MAPE>0: 실제값>예측값

 

7. R2: R square, 결정 계수

회귀모형 내에서 설명변수 x로 설명할 수 있는 반응변수 y의 변동비율
총변동에서 설명 가능한 변동이 차지하는 비율
⚠️ 선형 관계에서 사용되는 지표이므로 2차 함수 등의 비선형 관계에서는 사용이 어려움

 

 Sequential Model의 r2 지표 확인

# r2_score.py

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

from sklearn.model_selection import train_test_split


# 1. Data
x = np.array(range(1,21))
y = np.array([1,2,4,3,5,7,9,3,8,12,13,8,14,15,9,6,17,23,21,20])

x_train, x_test, y_train, y_test = train_test_split(
    x,y,
    train_size=0.7,
    shuffle=True,
    random_state=123
)


# 2. Model Construction
model = Sequential()
model.add(Dense(64, input_dim=1))
model.add(Dense(32))
model.add(Dense(16))
model.add(Dense(1))


# 3. compile and train
model.compile(loss='mae', optimizer='adam', metrics=['mse'])
model.fit(x_train, y_train, epochs=128, batch_size=4)


# 4. Evalueate and Predict
loss = model.evaluate(x_test, y_test)
y_predict = model.predict(x_test)


from sklearn.metrics import mean_squared_error, r2_score # ','로 class 다중 삽입 가능


def RMSE (y_test, y_predict):
    return np.sqrt(mean_squared_error(y_test, y_predict))
print("RMSE: ", RMSE(y_test, y_predict))

r2 = r2_score(y_test, y_predict)
print("R: ", r2)



'''
Result for prediction

MAE: 3.0612
MSE: 15.1591
RMSE: 3.8482795786702315
-> loss: 낮을수록 고성능

R: 0.6485608399723322
-> accuracy: 높을수록 고성능

'''

 

➕ ScikitLearn Dataset을 이용한 예제

# indicator_with_california.py

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split


# 1. Data
datasets = fetch_california_housing()
x = datasets.data
y = datasets.target

x_train, x_test, y_train, y_test = train_test_split(
    x, y,
    train_size=0.7,
    random_state=123
)


# 2. model
model = Sequential()
model.add(Dense(64, input_dim=8))
model.add(Dense(32))
model.add(Dense(1))


# 3. compile and train
model.compile(loss='mae', optimizer = 'adam', metrics=['mse'])
model.fit(x_train, y_train, epochs=128, batch_size=64)


# 4. evaluate and predict
loss = model.evaluate(x_test, y_test)

y_predict = model.predict(x_test)


from sklearn.metrics import mean_squared_error, r2_score


def RMSE (y_test, y_predict):
    return np.sqrt(mean_squared_error(y_test, y_predict))
print("RMSE: ", RMSE(y_test, y_predict))

r2 = r2_score(y_test, y_predict)
print("R2: ", r2)



'''
Result

MAE: 0.6178
MSE: 0.8000
RMSE: 0.8944244298687739
R2: 0.39499231491934617
'''

 

8. Accuracy: 정확도

= (예측 결과가 동일한 데이터 건수/전체 예측 데이터 건수)

⚠️ 오차의 정도가 매우 낮음에도 불구하고 단순히 정오로만 판별하기 때문에 이진분류의 경우 정확도로만 평가하기에는 왜곡된 평가가 발생할 수 있으므로 보조 지표를 함께 사용해야 함

 

 

 

소스 코드

🔗 HJ0216/TIL

 

참고 자료

📑 [Scikit-learn] 회귀 모델 성능 측정 지표 : MAE, MSE, RMSE, MAPE, MPE

📑 Tutorial: Understanding Regression Error Metrics in Python

📑 [회귀분석] 결정계수(R²; Coefficient of Determination)

 

728x90
728x90

기본 환경: IDE: VS code, Language: Python

 

Matplotlib을 활용한 데이터 시각화

 

기본적인 DNN 모델 구축 및 시각화를 통한 예측의 정확도 판별

# matplotlib_scatter_and_plot.py

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

from sklearn.model_selection import train_test_split


# 1. Data
x = np.array(range(1,21))
y = np.array([1,2,4,3,5,7,9,3,8,12,13,8,14,15,9,6,17,23,21,20])

x_train, x_test, y_train, y_test = train_test_split(
    x,y,
    train_size=0.7,
    shuffle=True,
    random_state=123
)


# 2. Model Construction
model = Sequential()
model.add(Dense(64, input_dim=1))
model.add(Dense(32))
model.add(Dense(16))
model.add(Dense(1))


# 3. compile and train
model.compile(loss='mae', optimizer='adam')
model.fit(x_train, y_train, epochs=128, batch_size=4)


# 4. Evalueate and Predict
loss = model.evaluate(x_test, y_test) # 예측 전 평가

y_predict = model.predict(x) # training dataset을 predict에 사용
print("Result: ", y_predict)


import matplotlib.pyplot as plt

plt.scatter(x, y) # Scatter: 실제 x, y 데이터
plt.plot(x, y_predict, color="red") # plot: 실제 x, 예측 y 데이터
plt.show() # Scatter와 plot을 통해 시각적으로 예측을 비교, 분석할 수 있음



'''
Result

Epoch 128/128
4/4 [==============================] - 0s 5ms/step - loss: 1.9357

1/1 [==============================] - 0s 313ms/step - loss: 3.1006

Result:
[[ 1.0797057]
 [ 2.1201117]
 [ 3.160518 ]
 [ 4.2009234]
 [ 5.2413287]
 [ 6.2817335]
 [ 7.32214  ]
 [ 8.362547 ]
 [ 9.402951 ]
 [10.443358 ]
 [11.483764 ]
 [12.524168 ]
 [13.564573 ]
 [14.6049795]
 [15.645383 ]
 [16.685793 ]
 [17.7262   ]
 [18.766605 ]
 [19.807007 ]
 [20.847416 ]]
 
'''

 

plt.scatter(x, y): 파란색 점
plt.plot(x, y_predict, color="red"): 빨간색 실선

1. import matplotlib.pyplot as plt

matplotlib.pyplot library 함수를 사용하기 위해 import 후, 약칭 plt로 지정

 

2. plt.scatter(x, y)

실제 x, y 데이터를 scatter 함수에 대입하여, 산점도 반환

 

3. plt.plot(x, y_predict, color="red")

실제 x, 예측 y 데이터를 plot 함수에 대입하여, 예측 함수 반환

 

4. plt.show()

작성한 그래프 출력

 

 

 

➕ plot()

 - plot(y): plot 함수에 하나의 숫자 리스트가 대입될 경우, 해당 값을 y값으로 가정하고 x값 0, 1, 2 ...를 임의로 생성

 - plot(x,y): (x, y)에 대한 선형 함수 반환

 - plot(x1, y1, 'r--', x2, y2, 'bs'): (x1, y1)에 대한 빨간색 실선 및 (x2, y2)에 대한 파란색 네모 그래프 반환

→ 예시: (x, y) dataset과 (x, y_predict) dataset에 대한 그래프

# matplotlib_plot2.py

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

from sklearn.model_selection import train_test_split


# 1. Data
x = np.array(range(1,21))
y = np.array([1,2,4,3,5,7,9,3,8,12,13,8,14,15,9,6,17,23,21,20])

x_train, x_test, y_train, y_test = train_test_split(
    x,y,
    train_size=0.7,
    shuffle=True,
    random_state=123
)


# 2. Model Construction
model = Sequential()
model.add(Dense(64, input_dim=1))
model.add(Dense(32))
model.add(Dense(16))
model.add(Dense(1))


# 3. compile and train
model.compile(loss='mae', optimizer='adam')
model.fit(x_train, y_train, epochs=128, batch_size=4)


# 4. Evalueate and Predict
loss = model.evaluate(x_test, y_test)
y_predict = model.predict(x)


import matplotlib.pyplot as plt

plt.plot(x, y, "r--", x, y_predict, "bs")
plt.show()

 

➕ load_digits를 활용한 이미지 반환

# matshow_load_digits.py

import matplotlib.pyplot as plt

from sklearn.datasets import load_digits
# 손글씨로 쓴 숫자를 분류하는 datasets


datasets = load_digits()
# print(datasets.shape) # numpy.shape, datasets=scikit learn

plt.gray() # 이미지의 기본색조: 회색조
plt.matshow(datasets.images[0]) # 훈련용 데이터 손글씨 0
plt.matshow(datasets.images[1]) # 훈련용 데이터 손글씨 1
# plt.matshow(): array -> image로 반환
plt.show()

 

소스 코드

🔗 HJ0216/TIL

 

참고 자료

📑 01. Matplotlib 기본 사용

📑 np.unique(y[Python] Python 20일차(예제로 배우는 파이썬 데이터 시각화)

📑 [Python] 10일차 - matplotlib, histogram 등

 

728x90
728x90

기본 환경: IDE: VS code, Language: Python

 

인공지능 모델을 구성하여 기계 학습시킬 때 유의할 점

❗ Train Data Set과 Test Data Set을 나누는 것

Train Data로 Test 수행 시, 익숙한 데이터로만 기계 학습이 진행되어 새로운 데이터를 만나 Predict할 때 오히려 예측치의 Loss가 커지는 문제가 발생할 수 있음

 

Train Data Set과 Test Data Set 나누는 방법

1. 직접 Data 나누기

# split_train_test1.py

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense


# 1. Data
x = np.array([1,2,3,4,5,6,7,8,9,10])
y = np.array(range(10)) # [0,1,2,3,4,5,6,7,8,9]

x_train = np.array([1, 2, 3, 4, 5, 6, 7])
x_test = np.array([8, 9, 10])

y_train = np.array(range(7))
y_test = np.array(range(7, 10))


# 2. Model Construction
model = Sequential()
model.add(Dense(64, input_dim=1))
model.add(Dense(64))
model.add(Dense(16))
model.add(Dense(16))
model.add(Dense(1))


# 3. Compile and train
model.compile(loss='mae', optimizer='adam')
model.fit(x_train, y_train, epochs=256, batch_size=5)


# 4. evaluate and predict
loss = model.evaluate(x_test, y_test)
print("Loss: ", loss)

result = model.predict([11])
print("Result: ", result)


'''
# Result

Epoch 256/256
2/2 [==============================] - 0s 5ms/step - loss: 0.0557
1/1 [==============================] - 0s 288ms/step - loss: 0.0259
Loss:  0.02594725228846073
Result:  [[10.031645]]

'''

 

 

2. Slice

# split_train_test2.py

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense


# 1. Data
x = np.array([1,2,3,4,5,6,7,8,9,10])
y = np.array(range(10)) # [0,1,2,3,4,5,6,7,8,9]

x_train = x[:7] # 시작 생략 가능
x_test = x[7:] # 생략 시 끝 값 가져오기 가능
y_train = y[:7]
y_test = y[7:]
print(x_train, x_test, y_train, y_test)
# [1 2 3 4 5 6 7] [ 8  9 10] [0 1 2 3 4 5 6] [7 8 9]

# -로 위치 표현하는 방법
x_train2 = x[:-3]
x_test2 = x[-3:]
print(x_train2, x_test2)
# [1 2 3 4 5 6 7] [ 8  9 10]


# 2. Model Construction
model = Sequential()
model.add(Dense(64, input_dim=1))
model.add(Dense(64))
model.add(Dense(32))
model.add(Dense(16))
model.add(Dense(1))


# 3. Compile and train
model.compile(loss='mae', optimizer='adam')
model.fit(x_train, y_train, epochs=128, batch_size=2)


# 4. evaluate and predict
loss = model.evaluate(x_test, y_test)
print("Loss: ", loss)

result = model.predict([11])
print("Result: ", result)



'''
# Result

Epoch 128/128
4/4 [==============================] - 0s 2ms/step - loss: 0.0481
1/1 [==============================] - 0s 283ms/step - loss: 0.0506
Loss:  0.050618648529052734
Result:  [[9.937546]]

'''

 

 

3. train_test_split()

# split_train_test3.py

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.model_selection import train_test_split


# 1. Data
x = np.array([1,2,3,4,5,6,7,8,9,10])
y = np.array(range(10)) # [0,1,2,3,4,5,6,7,8,9]

x_train, x_test, y_train, y_test = train_test_split(
    x, y,
    train_size = 0.7,
    shuffle = True,
    random_state=1)
print("x_train, x_test: ", x_train, x_test, "\ny_train, y_test: ", y_train, y_test)
'''
train_test_split(arrays, test_size, train_size, random_state, shuffle, stratify)

parameter
arrays: 분할시킬 Data
test_size: 전체 Data 중 test로 사용할 test set 비율
train_size: 1 - test_size (생략 가능)
random_state: 입력 시, 함수 수행 시 마다 결과가 바뀌지 않음
* 같은 데이터로 계속 훈련을 해줘야하므로 random_state를 입력해줘야 함
shuffle: split 시, raw data 셔플 여부(Default = True)
shuffle 사용 시, 자료의 치우침 방지
stratify: 해당 Data 비율 유지
Ex. data = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1]
0의 비율: 70%, 1의 비율: 30%
stratify=Y로 설정 시, TestSet과 TrainSet에서 0의 비율과 1의 비율을 Data와 동일하게 유지
data(y)가 분류형 데이터일 경우만 사용 가능(비율 유지 기능이므로)
'''


# 2. Model Construction
model = Sequential()
model.add(Dense(64, input_dim=1))
model.add(Dense(64))
model.add(Dense(32))
model.add(Dense(16))
model.add(Dense(1))


# 3. Compile and train
model.compile(loss='mae', optimizer='adam')
model.fit(x_train, y_train, epochs=128, batch_size=4)


# 4. evaluate and predict
loss = model.evaluate(x_test, y_test)
print("Loss: ", loss)

result = model.predict([11])
print("Result: ", result)


'''
Result

Epoch 128/128
2/2 [==============================] - 0s 10ms/step - loss: 0.1477
1/1 [==============================] - 0s 480ms/step - loss: 0.2799
Loss:  0.27993300557136536
Result:  [[9.541215]]

'''

 

 

 

소스 코드

🔗 HJ0216/TIL

 

참고 자료

📑 [Python.NumPy] ndarray indexing과 slicing

📑 [Python] sklearn의 train_test_split() 사용법

 

728x90

'Naver Clould with BitCamp > Aartificial Intelligence' 카테고리의 다른 글

Model Performance Indicator  (0) 2023.01.21
Matplotlib: Scatter and plot  (0) 2023.01.21
Scalar, Vector, Matirx, Tensor  (0) 2023.01.20
MultiLayer Perceptron  (0) 2023.01.20
Hyper-parameter Tuning  (0) 2023.01.20
728x90

기본 환경: IDE: VS code, Language: Python

 

  • 스칼라(Scalar): 행렬을 이루는 각 요소(개체), 0차원(점)
  • 벡터(Vector): scalar의 모임, 1차원(선)
  • 행렬(Metrix): vector의 모임, 2차원(면)
  • 텐서(Tensor): metrix의 모임, 3차원(공간), 4차원(시공간) 등 3차원 이상의 다차원 공간

 

Data Shape

# data_shape.py

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense


# 1. data
a = np.array([1, 2, 3])
b = np.array([[1], [2], [3]])
c = np.array([[1, 2, 3]])
d = np.array([[1, 2],
              [3, 4],
              [5, 6]])
e = np.array([[1, 2, 3, 4],
             [11, 22, 33, 44],
             [111, 222, 333, 444]])
f = np.array([[[1, 2, 3],[4, 5, 6]],
              [[11, 22, 33],[44, 55, 66]],
              [[111, 222, 333],[444, 555, 666]]])
g = np.array([[[1, 2], [3, 4], [5, 6]],
             [[11, 22], [33, 44], [55, 66]]])
h = np.array([[[[1], [2], [3]],[[4], [5], [6]]],
              [[[11], [22], [33]],[[44], [55], [66]]]])
i = np.array([[[[1, 2], [3, 4], [5, 6], [7, 8]],
              [[11, 22], [33, 44], [55, 66], [77, 88]]],
              [[[1, 2], [3, 4], [5, 6], [7, 8]],
              [[11, 22], [33, 44], [55, 66], [77, 88]]]])
j = np.array([[[[1,2]]]])
# z = np.array([[1,2,3][4,5]]) # TypeError

print("a.shape: ", a.shape) # Vector: (3, )
print("b.shape: ", b.shape) # Metrix: (3, 1)
print("c.shape: ", c.shape) # Metirx: (1, 3)
print("d.shape: ", d.shape) # Metrix: (3, 2)
print("e.shape: ", e.shape) # Metrix: (3, 4)
print("f.shape: ", f.shape) # Tensor: (3, 2, 3)
print("g.shape: ", g.shape) # Tensor: (2, 3, 2)
print("h.shape: ", h.shape) # Tensor: (2, 2, 3, 1)
print("i.shape: ", i.shape) # Tensor: (2, 2, 4, 2)
print("j.shape: ", j.shape) # Tensor: (1, 1, 1, 2)
# print("z.shape: ", z.shape)



'''
# Result

a.shape:  (3,)
b.shape:  (3, 1)
c.shape:  (1, 3)
d.shape:  (3, 2)
e.shape:  (3, 4)
f.shape:  (3, 2, 3)
g.shape:  (2, 3, 2)
h.shape:  (2, 2, 3, 1)
i.shape:  (2, 2, 4, 2)
j.shape:  (1, 1, 1, 2)

'''

'''
x = [1. 2. 3]
x.shape #(3, ) = (Scalar, ): 스칼라 3개 → 벡터 1개 (출력이 ,로 끝남)

x = [[1], [2], [3]]
x.shape #(3, 1) = (Vector, Scalar): 벡터 3개, 스칼라 1개 → 행렬 1개

x = [[1,2,3]]
x.shape #(1, 3) = (Vector, Scalar): 벡터 1개, 스칼라 3개 → 행렬 1개

x = [[1,2][3,4]]
x.shape #(2, 2) = (Vector, Scalar): 벡터 1개, 스칼라 3개 → 행렬 1개

x = [[[1,2,3]]]
x.shape #(1, 1, 3) = (Metrix, Vector, Scalar): 행렬 1개, 벡터 1개, 스칼라 3개 → 텐서 1개

x = [[1,2,3], [4,5,6]]
x.shape #(2, 3) = (Vector, Scalar): 벡터 2개, 스칼라 3개 → 행렬 1개

x = [[[1], [2]]]
x.shape #(1, 2, 1) = (Metrix, Vector, Scalar): 행렬 1개, 벡터 2개, 스칼라 1개 → 텐서 1개

x = [[1,2,3], [4,5]]
x.shape #(2, 3) = (Vector, Scalar): 벡터 1개, 스칼라 2개 && 벡터 1개, 스칼라 3개  → 행렬 1개

shape: tensor2 ,tensor1, matrix, vector, scalar
→ 최종 도출되는 차원은 1개일 것이므로 출력이 생략 됨
'''

 

➕ Data type에 따른 Model Construction

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense


# 2. Model Construction
model = Sequential()
model.add(Dense(5, input_shape=(13,)))
# 2차원(Matrix) 이상의 input은 input_dim이 아닌 input_shape 사용
# (Vector, Scalar) 형태를 input_shape에 사용할 경우(scalar, ) 형태로 작성

Model에 Input_dim을 지정할 때, data type이 행렬(Matrix) 이상일 경우에는 Input_shape 사용

 

(506, 13): 13개의 특성(col)을 가진 506개의 data

→ input_shape=(13, )

(506, 10, 3): (10, 3)의 특성을 가진 506개의 data

→ input_shape=(10, 3)

 

 

 

소스 코드

🔗 HJ0216/TIL

 

참고 자료

📑 스칼라, 벡터, 행렬, 텐서

 

728x90

'Naver Clould with BitCamp > Aartificial Intelligence' 카테고리의 다른 글

Matplotlib: Scatter and plot  (0) 2023.01.21
Split training data and test data  (0) 2023.01.21
MultiLayer Perceptron  (0) 2023.01.20
Hyper-parameter Tuning  (0) 2023.01.20
Types of Artificial Neural Networks  (0) 2023.01.15
728x90

기본 환경: IDE: VS code, Language: Python

 

Perceptron

: 다수의 입력으로부터 하나의 결과를 내보내는 알고리즘

: x-입력값, w-가중치, y-출력값

 

 

1. 단층 퍼셉트론(SingleLayer Perceptron)

: 1개의 입력층과 1개의 출력층으로 이뤄진 퍼셉트론

: AND, NAND, OR 회로 구현은 가능하지만, XOR 구현이 불가

 

AND 회로

1개의 직선(단층 퍼셉트론)은 XOR 회로 생성 불가

❗ 2개 이상의 직선을 구현하는 다층 퍼셉트론으로 XOR 회로 생성 가능

 

2. 다층 퍼셉트론(MultiLayer Perceptron)

: 1개의 입력층, 1개의 출력층 그리고 입력층과 출력층 사이의 은닉층(Hidden Layer)으로 이뤄진 퍼셉트론

: NAND, OR, AND의 조합으로 XOR 구현

: 은닉층이 2개 이상인 신경망을 심층 신경망(Deep Neural Network, DNN)이라고 함

 

Hidden Layer가 2개 이상인 MLP, DNN 모델 만들기

# multiLayer_Perceptron.py

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense


# 1. Data
x = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
             [1, 1, 1, 1, 2, 1.3, 1.4, 1.5, 1.6, 1.4]]) # input_dim = 10(행렬에서 열 기준)
# np.array([[element1], [element2]]): 다중 배열을 만들 경우, element1과 element2의 개수를 일치 시켜야 함
y = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20]) # output_dim = 1

print(x.shape) # (2, 10)
print(y.shape) # (10, )
# Data.shape: (행: 데이터 개수, 열: 데이터 요소)
x = x.T
# Data.T 행렬 전환
print(x.shape) # (10, 2)
'''
x = np.array([1,1], 
             [2,1], 
             [3,1], 
             [4,1], 
             [5,2], 
             [6,1.3], 
             [7,1.4], 
             [8,1.5], 
             [9, 1.6], 
             [10, 1.4])
'''


# 2. Model
model = Sequential()
model.add(Dense(32, input_dim=2))
# input_dim = 2 (입력값 기준으로 열의 개수, Input Layer), output = 32(Hidden layer로 임의값 설정 가능)
model.add(Dense(32))
model.add(Dense(16))
model.add(Dense(8))
model.add(Dense(1))
# output_dim = 1 (출력값 기준으로 열의 개수, Output Layer)


# 3. Compile
model.compile(loss='mae', optimizer='adam')
model.fit(x, y, epochs=100, batch_size=1)


# 4. Evaluate and Predict
loss = model.evaluate(x, y)
print("loss: ", loss)

result = model.predict([[10, 1.4]])
# predict(x1, x2)에서 x2 값을 알 수 없으므로 우선 훈련값(x1, x2)을 넣어서 y와 유사한지 확인
print("[10, 1.4] result: ", result)



'''
Result

Train(Fit)
Epoch 100/100
10/10 [==============================] - 0s 667us/step - loss: 0.0817

Evaluate
1/1 [==============================] - 0s 98ms/step - loss: 0.1372

Predict
[10, 1.4] result:  [[19.758385]]

'''

 

 

 

➕ Model Summary 확인

# summary.py

import numpy as np

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense


# 1. Data
x = np.array([1,2,3])
y = np.array([1,2,3])


# 2. Model Instruction
model = Sequential()
model.add(Dense(5, input_dim=1))
model.add(Dense(4))
model.add(Dense(3))
model.add(Dense(2))
model.add(Dense(1))

model.summary()



'''
Model: "sequential"
_________________________________________________________________
 Layer (type)         Output Shape(노드의 개수)      Param #
                        - model.add(Dense(*))    parameter (연산): node + bias -> 실질적으로는  node+1이 됨
=================================================================
 dense (Dense)         (None, 5) (data 개수, node 개수) 10 (2*5)
 dense_1 (Dense)       (None, 4)                       24 (6*4)
 dense_2 (Dense)       (None, 3)                       15 (5*3)
 dense_3 (Dense)       (None, 2)                        8 (4*2)
 dense_4 (Dense)       (None, 1)                        3 (3*1)
=================================================================
Total params: 60
Trainable params: 60 -> 훈련이 필요한 나의 model에 대해서는 훈련이 필요
Non-trainable params: 0 -> 이미 훈련된 남의 model에 대해서는 훈련이 따로 필요없으므로 non-trainable에 사용
_________________________________________________________________
'''

 

Summary Node 보충 설명

 

소스 코드

🔗 HJ0216/TIL

 

참고 자료

📑 07-01 퍼셉트론(Perceptron)

 

728x90

'Naver Clould with BitCamp > Aartificial Intelligence' 카테고리의 다른 글

Split training data and test data  (0) 2023.01.21
Scalar, Vector, Matirx, Tensor  (0) 2023.01.20
Hyper-parameter Tuning  (0) 2023.01.20
Types of Artificial Neural Networks  (0) 2023.01.15
ANN Model Construction  (0) 2023.01.15