먼저 데이터를 전처리하고 모델링 한다.
import tensorflow.keras
from keras.models import Sequential
from keras.layers import Dense
from sklearn.preprocessing import MinMaxScaler
def build_model():
model = Sequential()
model.add(Dense(units = 5, activation = 'relu', input_shape = (5, )))
model.add(Dense(units = 25, activation = 'relu'))
model.add(Dense(units = 10, activation = 'relu'))
model.add(Dense(units = 1, activation = 'linear'))
model.compile('adam', 'mse')
return model
model = build_model()
마지막 레이어는 활성함수를 linear로 해줘야하고 compile loss 는 mse로 정해준다
epoch_history = model.fit(X_train, y_train, epochs = 20, batch_size = 10)
y_pred = model.predict(X_test)
# mse 계산
((y_test - y_pred) ** 2).mean()
# 예측 데이터 되돌리기
sc_y.inverse_transform(y_pred)
모델 학습을 시키고 예측을 한뒤 mse 측정을 한다.
inverse_transform으로 원래 단위로 되돌릴 수 있다.
'인공지능 > 텐서플로우' 카테고리의 다른 글
validation 데이터란 무엇이고 코드에서 사용하는 방법 (validation_split) (0) | 2022.06.13 |
---|---|
learning rate를 옵티마이저에서 셋팅하는 방법 (0) | 2022.06.13 |
tensorflow GridSearch를 이용한, 최적의 하이퍼 파라미터 찾기 (0) | 2022.06.10 |
tensorflow로 분류의 문제 모델링 하는 법 (0) | 2022.06.10 |
tensorflow에서 학습시 epochs와 batch_size (0) | 2022.06.10 |