https://opentutorials.org/course/4570/28988
네번째 딥러닝 - 신경망의 완성:히든레이어 - 생활코딩
수업소개 히든레이어와 멀티레이어의 구조를 이해하고, 히든레이어를 추가한 멀티레이어 인공신경망 모델을 완성해 봅니다. 강의 멀티레이어 신경망 실습 소스코드 colab | backend.ai 보스
opentutorials.org
18강 히든레이어
퍼셉트론을 깊게 연결한 딥러닝을 만들어보자!
X = tf.keras.layers.Input(shape=[13])
H = tf.keras.layers.Dense(5, activation='swish')(X) #히든레이어 추가 함수. swish는 활성화 함수.
H = tf.keras.layers.Dense(3, activation='swish')(H) #히든레이어를 여러 개 추가할 수 있음.
H = tf.keras.layers.Dense(3, activation='swish')(H)
Y = tf.keras.layers.Dense(1)(H)
model.tf.keras.Models.Model(X, Y)
model.compile(loss='mse')
--> 히든레이어 3개 사용한 모델. 이전의 모델보다 훨씬 더 똑똑한 모델을 학습시킬 수 있음.
19강 히든레이어(실습)
보스턴 집값 예측
import tensorflow as tf
import pandas as pd
###########################
# 1.과거의 데이터를 준비합니다.
파일경로 = 'https://raw.githubusercontent.com/blackdew/tensorflow1/master/csv/boston.csv'
보스턴 = pd.read_csv(파일경로)
# 종속변수, 독립변수
독립 = 보스턴[['crim', 'zn', 'indus', 'chas', 'nox',
'rm', 'age', 'dis', 'rad', 'tax',
'ptratio', 'b', 'lstat']]
종속 = 보스턴[['medv']]
print(독립.shape, 종속.shape)
###########################
# 2. 모델의 구조를 만듭니다
X = tf.keras.layers.Input(shape=[13])
H = tf.keras.layers.Dense(10, activation='swish')(X) #hidden-layer
Y = tf.keras.layers.Dense(1)(H)
model = tf.keras.models.Model(X, Y)
model.compile(loss='mse')
# 모델 구조 확인
model.summary()
###########################
# 3.데이터로 모델을 학습(FIT)합니다.
model.fit(독립, 종속, epochs=100)
###########################
# 4. 모델을 이용합니다
print(model.predict(독립[:5]))
print(종속[:5])
###########################
# 모델의 수식 확인
print(model.get_weights())
아이리스 분류
import tensorflow as tf
import pandas as pd
###########################
# 1.과거의 데이터를 준비합니다.
파일경로 = 'https://raw.githubusercontent.com/blackdew/tensorflow1/master/csv/iris.csv'
아이리스 = pd.read_csv(파일경로)
# 원핫인코딩
아이리스 = pd.get_dummies(아이리스)
# 종속변수, 독립변수
독립 = 아이리스[['꽃잎길이', '꽃잎폭', '꽃받침길이', '꽃받침폭']]
종속 = 아이리스[['품종_setosa', '품종_versicolor', '품종_virginica']]
print(독립.shape, 종속.shape)
###########################
# 2. 모델의 구조를 만듭니다
X = tf.keras.layers.Input(shape=[4])
H = tf.keras.layers.Dense(8, activation="swish")(X)
H = tf.keras.layers.Dense(8, activation="swish")(H)
H = tf.keras.layers.Dense(8, activation="swish")(H)
Y = tf.keras.layers.Dense(3, activation='softmax')(H)
model = tf.keras.models.Model(X, Y)
model.compile(loss='categorical_crossentropy',
metrics='accuracy')
# 모델 구조 확인
model.summary()
###########################
# 3.데이터로 모델을 학습(FIT)합니다.
model.fit(독립, 종속, epochs=100)
###########################
# 4. 모델을 이용합니다
print(model.predict(독립[:5]))
print(종속[:5])
'스터디📖 > ML, DL' 카테고리의 다른 글
Tensorflow (python) - 21강 부록2: 모델을 위한 팁 (0) | 2021.09.28 |
---|---|
Tensorflow (python) - 20강 부록1: 데이터를 위한 팁 (0) | 2021.09.28 |
Tensorflow (python) - 14, 15, 16, 17강 세번째 딥러닝 - 아이리스 품종 분류 (0) | 2021.09.01 |
Tensorflow (python) - 13강 학습의 실제 (0) | 2021.08.27 |
Tensorflow (python) - 10, 11, 12강 두번째 딥러닝 - 보스턴 집값 예측 (0) | 2021.08.25 |