반응형
1차 함수
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
#1 데이터 준비
#훈련데이터
x_train = np.array([1,2,3,4,5,6,7,8,9,10])
y_train = np.array([1,2,3,4,5,6,7,8,9,10])
#평가데이터
x_test = np.array([101,102,103,104,105,106,107,108,109,110])
y_test = np.array([101,102,103,104,105,106,107,108,109,110])
#2 모델 구성
model = Sequential()
#input_dim x_train으로 1개의 컬럼 데이터를 입력하며르 input_dim=1 이라고 표현하며 출력은 5라는 의미 결국 1개의 입력으로 5개의 노드로 출력
#입력은 첫라인에만 표현하고 두 번재 라인부터는 출력만 표시
model.add(Dense(5, input_dim=1, activation='relu'))
#5개의 입력으로 3개의 노드로 출력
model.add(Dense(3))
# 3개의 입력으로 1개의 노드로 출력
model.add(Dense(1, activation='relu'))
# 모델 구성 확인 함수
# param 개수는 입력 * 출력 1layer 1*5 = 5 , 2layer 5 * 3 = 15 , 3layer 3 * 1
# y = wx + b 인데 bias는까 빠졌다. 그래서 레이아웃 마다 1개의 bias가 추가된다고 생각하면 된디ㅏ.
# 결국 llayer (1+1) *5 = 10, 2layer (5+1) * 3 = 18, 3lyaer (3+1) * 1 = 4
# total param= 10 + 18 + 4 = 32
model.summary()
# 3 컴파일 , 훈련
# 컴파일
model.compile(loss='mse', optimizer='adam' ,metrics=['accuracy'])
# 실행 validation_data 훈련데이터와 평가 데이터를 나누기 위해 평가데이터 정의
model.fit(x_train, y_train, epochs=100, batch_size=1, validation_data= (x_test, y_test))
# 평가
loss, acc = model.evaluate(x_test, y_test, batch_size=1)
# 4, 평가 예측
# 실행 결과 확인
print("loss : ", loss)
print("acc : ", acc)
# x_test를 넣었을 때의 출력값
output = model.predict(x_test)
print("결과물 : \n", output)
2차 함수 모양을 띄는 모델과 데이터는 어떻게 1차 함수로 만들까?
y = ax**2+b
2차 함수를 1차 함수로 바꾸려면 미분하면 된다.
미분하면 y = 2ax + 0
회기 분석은 선형이기 때문에 딱 맞아떨어지지 않는다. 그래서 결괏값과 예측 분석을 하는 함수를 다른 것으로 쓰게 된다.
컴파일시 metrics=['accuracy'] 를 mse로 바꾸면 된다.
model.compile(loss='mse', optimizer='adam' ,metrics=['mse'])
반응형
'AI > DeepLearning' 카테고리의 다른 글
회귀 모델 3 훈련세트, 검증세트, 테스트세트 분리 (0) | 2020.06.26 |
---|---|
회귀모델의 판별식 R2, RMSE (0) | 2020.06.23 |
회귀 모델 1 1에서 10까지 예측하기 (0) | 2020.06.23 |
텐서플로우 개발 환경 구축 3 (PyCharm 설치) (0) | 2020.06.04 |
Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 (0) | 2020.06.02 |