| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
- 칼만필터
- 리튬배터리
- Battery AI
- Incremental Capacity Analysis
- tensorflow
- 배터리 AI
- 배터리 딥러닝
- 머신러닝 코드
- 딥러닝
- state of health
- Machine Learning
- Azure
- eis
- 코드로 이해하는 딥러닝
- 코이딥
- 배터리 EIS
- Battery Deep Learning
- Battery modeling
- 머신러닝
- bms
- 딥러닝 코드
- 배터리 연구
- AzureML
- Deep learning
- Battery Management System
- 배터리 진단
- 배터리 모델링
- 배터리 열화
- 텐서플로우
- Battery SOH
- Today
- Total
Engineering insight
[코드로 이해하는 딥러닝 2-10] - Drop out 본문
[코드로 이해하는 딥러닝 2-10] - Drop out
Free-Nomad 2021. 1. 14. 06:06※ Tensorflow1문법을 2로만 바꾸는것이기에, 코드분석 위주로 진행하고있습니다.
[Tensorflow1 링크종합] https://limitsinx.tistory.com/50
※ 이 전글에서 정리한 코드/문법은 재설명하지 않으므로, 참고부탁드립니다.
코드만 해석하고 넘어가기에 다소 불친절한 글 일수 있습니다..
개념적인 부분은 하기 링크에 따로 정리해두어, 참조 부탁드립니다.
https://limitsinx.tistory.com/45
[코드로 이해하는 딥러닝 14] - Drop out
[코드로 이해하는 딥러닝 0] - 글연재에 앞서 https://limitsinx.tistory.com/27 [코드로 이해하는 딥러닝 1] - Tensorflow 시작 https://limitsinx.tistory.com/28 [코드로 이해하는 딥러닝 2] - Tensorflow 변..
limitsinx.tistory.com
Overfitting 문제를 해결하기 위한 Drop out을 코드로 구현해보도록 하겠습니다.
Drop out에 대한 개념적인 이해는 상기글 참조 부탁드립니다!
[코드 전문]
import numpy as np
import random
import tensorflow as tf
random.seed(777) # for reproducibility
learning_rate = 0.001
batch_size = 100
training_epochs = 15
nb_classes = 10
drop_rate = 0.3
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
print(x_train.shape)
x_train = x_train.reshape(x_train.shape[0], 28 * 28)
x_test = x_test.reshape(x_test.shape[0], 28 * 28)
y_train = tf.keras.utils.to_categorical(y_train, nb_classes)
y_test = tf.keras.utils.to_categorical(y_test, nb_classes)
tf.model = tf.keras.Sequential()
tf.model.add(tf.keras.layers.Dense(input_dim=784, units=512, kernel_initializer='glorot_normal', activation='relu'))
tf.model.add(tf.keras.layers.Dropout(drop_rate))
tf.model.add(tf.keras.layers.Dense(units=512, kernel_initializer='glorot_normal', activation='relu'))
tf.model.add(tf.keras.layers.Dropout(drop_rate))
tf.model.add(tf.keras.layers.Dense(units=512, kernel_initializer='glorot_normal', activation='relu'))
tf.model.add(tf.keras.layers.Dropout(drop_rate))
tf.model.add(tf.keras.layers.Dense(units=512, kernel_initializer='glorot_normal', activation='relu'))
tf.model.add(tf.keras.layers.Dropout(drop_rate))
tf.model.add(tf.keras.layers.Dense(units=nb_classes, kernel_initializer='glorot_normal', activation='softmax'))
tf.model.compile(loss='categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(lr=learning_rate), metrics=['accuracy'])
tf.model.summary()
history = tf.model.fit(x_train, y_train, batch_size=batch_size, epochs=training_epochs)
# predict 10 random hand-writing data
y_predicted = tf.model.predict(x_test)
for x in range(0, 10):
random_index = random.randint(0, x_test.shape[0]-1)
print("index: ", random_index,
"actual y: ", np.argmax(y_test[random_index]),
"predicted y: ", np.argmax(y_predicted[random_index]))
# evaluate test set
evaluation = tf.model.evaluate(x_test, y_test)
print('loss: ', evaluation[0])
print('accuracy', evaluation[1])
[코드 분석]

다른부분의 코드는 기존글과 완전히 동일합니다.
Dropout은 딱 1줄 코드만 추가해주면 되는데요
Layer를 설계하고 activation function을 통과시킨 후, tf.model.add(tf.keras.layers.Dropout(drop_rate)) 한줄만 추가해주시면 됩니다.
drop_rate는 보통 0.3으로 하는데요,
이건 0.7(70%)에 해당하는 노드들만 남겨두고 다 지워버리는것을 의미합니다.
저는 0.5로 하는 경우도있고, 이래저래 연구하는 분야마다 다르게 진행하는데요, 그래도 0.5 이상은 유지하고 있습니다.
(절반 이상의 노드를 지워버린다면 뭔가 성능이 반감될것 같은 개인적인 느낌이랄까요..)
[결과값]

15 epochs 만큼 학습시킨 결과, 정확도는 약 97%까지 상승하게 되었네요
해당 코드에서 사용한 스킬들로는
Deep Wide Neural Network ,ReLU, cross entropy, Drop out, Xavier initialization(glorot) 입니다.
CNN(Convolutional Neural Network)로 넘어가지 않고도 97%에 육박하는 정확도를 보여주었습니다.
이 이상도 다른 파라미터들의 변경을 통해 충분히 도달 가능하지만, CNN은 기본적인 코드만 돌려도 98%이상 정확도를 보여주고 있습니다.
물론, CNN은 보통 image처리에 많이 사용하기 때문에 (저의 연구분야에선 사용하지 않습니다. 차원의 수가 그렇게 높지 않기때문에) 연산량이 엄청나게 많다는 단점이 있습니다.
'DeepLearning Framework & Coding > Tensorflow 2.X' 카테고리의 다른 글
| [코드로 이해하는 딥러닝 2-11] - RNN(Recurrent Neural Network)/LSTM(Long-Short-Term-Memory) (2) | 2021.01.16 |
|---|---|
| [코드로 이해하는 딥러닝 2-11] - CNN(Convolutional Neural Network) (0) | 2021.01.15 |
| [코드로 이해하는 딥러닝 2-9] - MNIST를 DNN으로 학습시키기/ReLU (0) | 2021.01.13 |
| [코드로 이해하는 딥러닝 2-8] - Data Pre-processing (0) | 2021.01.12 |
| [코드로 이해하는 딥러닝 2-7] - Deep and Wide Neural Network(DWNN) (0) | 2021.01.11 |