반응형
7-1 텐서플로와 케라스를 사용하여 신경망 만들기
- 케라스(Kears API)
딥러닝 패키지를 편리하게 사용하기 위해 만들어진 래퍼(Wrapper) 패키지
- 텐서플로 신경망 구현
# 훈련할 가중치 변수 선언
w = tf.Variable(tf.zeros(shape=(1)))
b = tf.Variable(tf.zeros(shape=(1)))
# 경사하강법 옵티마이저 설정
optimizer = tf.optimizers.SGD(lr = 0.01)
#에포크 횟수만큼 훈련
num_epochs = 10
for step in range(num_epochs):
# 자동 미분을 위한 연산 과정 기록
with tf.GradientTape() as tape:
z_net = w * x_train + b
z_net = tf.reshape(z_net, [-1])
sqr_errors = tf.square(y_train - z_net)
mean_cost = tf.reduce_mean(sqr_errors)
# 손실 함수에 대한 가중치의 그레이디언트 계산
grads = tape.gradient(mean_cost, [w, b])
# 옵티마이저에 그레이디언트 반영
optimizer.apply_gradients(zip(grads, [w, b]))
- 케라스로 구현
# 신경망 모델
model = tf.keras.model.Sequential()
# 완전 연결층 추가
model.add(tf.keras.layers.Dense(1))
# 옵티마이저와 손실 함수 지정
model.compile(optimizer = 'sgd', loss = 'mse')
# 훈련 데이터를 사용하여 에포크 획수만큼 훈련
model.fit(x_train, y_train, epochs = 10)
- Sequential 클래스의 사용 방법
Sequential 객체에 층 추가
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
model = Sequential([Dense(...),..])
add() 메서드 사용하여 층 추가
dense = Dense(...)
model.add(dense)
model = Sequential()
model.add(Dense(...))
model.add(Dense(...))
- Dense 클래스 사용 방법
뉴런의 개수를 지정하는 매개변수 unit
활성화 함수를 지정하는 매개변수 activation
- 모델의 최적화 알고리즘과 손실 함수 설정
최적화 알고리즘을 지정하는 매개변수 optimizer
손실 함수를 지정하는 매개변수 loss
반응형
- 케라스로 다중 분류 신경망 만들기
1. 모델 생성
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
model = Sequential()
2. 은닉층과 출력층을 모델에 추가
model.add(Dense(100, activation='sigmoid', input_shape=(784,)))
model.add(Dense(10, activation='softmax'))
3. 최적화 알고리즘과 손실 함수 지정
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
4. 모델 훈련
history = model.fit(x_train, y_train_encoded, epochs=40, validation_data=(x_val, y_val_encoded))
##결과:
Epoch 1/40
1500/1500 [==============================] - 7s 3ms/step - loss: 1.3560 - accuracy: 0.6576 - val_loss: 0.9579 - val_accuracy: 0.7210
Epoch 2/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.8419 - accuracy: 0.7402 - val_loss: 0.7535 - val_accuracy: 0.7562
Epoch 3/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.7128 - accuracy: 0.7647 - val_loss: 0.6663 - val_accuracy: 0.7745
Epoch 4/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.6472 - accuracy: 0.7816 - val_loss: 0.6151 - val_accuracy: 0.7938
Epoch 5/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.6047 - accuracy: 0.7940 - val_loss: 0.5774 - val_accuracy: 0.8018
Epoch 6/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.5741 - accuracy: 0.8038 - val_loss: 0.5517 - val_accuracy: 0.8105
Epoch 7/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.5511 - accuracy: 0.8104 - val_loss: 0.5309 - val_accuracy: 0.8168
Epoch 8/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.5326 - accuracy: 0.8176 - val_loss: 0.5145 - val_accuracy: 0.8215
Epoch 9/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.5178 - accuracy: 0.8213 - val_loss: 0.4999 - val_accuracy: 0.8264
Epoch 10/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.5055 - accuracy: 0.8254 - val_loss: 0.4889 - val_accuracy: 0.8300
Epoch 11/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4948 - accuracy: 0.8285 - val_loss: 0.4803 - val_accuracy: 0.8313
Epoch 12/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4859 - accuracy: 0.8318 - val_loss: 0.4718 - val_accuracy: 0.8350
Epoch 13/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.4781 - accuracy: 0.8335 - val_loss: 0.4642 - val_accuracy: 0.8380
Epoch 14/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4710 - accuracy: 0.8371 - val_loss: 0.4568 - val_accuracy: 0.8394
Epoch 15/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4645 - accuracy: 0.8372 - val_loss: 0.4514 - val_accuracy: 0.8403
Epoch 16/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4588 - accuracy: 0.8405 - val_loss: 0.4482 - val_accuracy: 0.8427
Epoch 17/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4536 - accuracy: 0.8420 - val_loss: 0.4415 - val_accuracy: 0.8437
Epoch 18/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4486 - accuracy: 0.8429 - val_loss: 0.4370 - val_accuracy: 0.8455
Epoch 19/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.4440 - accuracy: 0.8453 - val_loss: 0.4333 - val_accuracy: 0.8462
Epoch 20/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4399 - accuracy: 0.8458 - val_loss: 0.4291 - val_accuracy: 0.8478
Epoch 21/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.4359 - accuracy: 0.8482 - val_loss: 0.4257 - val_accuracy: 0.8514
Epoch 22/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4322 - accuracy: 0.8485 - val_loss: 0.4218 - val_accuracy: 0.8511
Epoch 23/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.4289 - accuracy: 0.8505 - val_loss: 0.4211 - val_accuracy: 0.8526
Epoch 24/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4257 - accuracy: 0.8511 - val_loss: 0.4169 - val_accuracy: 0.8537
Epoch 25/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4224 - accuracy: 0.8522 - val_loss: 0.4151 - val_accuracy: 0.8543
Epoch 26/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.4196 - accuracy: 0.8532 - val_loss: 0.4116 - val_accuracy: 0.8561
Epoch 27/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4166 - accuracy: 0.8543 - val_loss: 0.4083 - val_accuracy: 0.8568
Epoch 28/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4137 - accuracy: 0.8544 - val_loss: 0.4067 - val_accuracy: 0.8584
Epoch 29/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.4115 - accuracy: 0.8557 - val_loss: 0.4051 - val_accuracy: 0.8593
Epoch 30/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4090 - accuracy: 0.8571 - val_loss: 0.4021 - val_accuracy: 0.8583
Epoch 31/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.4065 - accuracy: 0.8576 - val_loss: 0.4005 - val_accuracy: 0.8602
Epoch 32/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4042 - accuracy: 0.8585 - val_loss: 0.3994 - val_accuracy: 0.8603
Epoch 33/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.4021 - accuracy: 0.8579 - val_loss: 0.3973 - val_accuracy: 0.8613
Epoch 34/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.3999 - accuracy: 0.8590 - val_loss: 0.3959 - val_accuracy: 0.8610
Epoch 35/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.3980 - accuracy: 0.8602 - val_loss: 0.3953 - val_accuracy: 0.8609
Epoch 36/40
1500/1500 [==============================] - 4s 3ms/step - loss: 0.3960 - accuracy: 0.8602 - val_loss: 0.3920 - val_accuracy: 0.8627
Epoch 37/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.3940 - accuracy: 0.8607 - val_loss: 0.3927 - val_accuracy: 0.8612
Epoch 38/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.3921 - accuracy: 0.8617 - val_loss: 0.3911 - val_accuracy: 0.8624
Epoch 39/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.3903 - accuracy: 0.8619 - val_loss: 0.3880 - val_accuracy: 0.8635
Epoch 40/40
1500/1500 [==============================] - 5s 3ms/step - loss: 0.3886 - accuracy: 0.8624 - val_loss: 0.3865 - val_accuracy: 0.8630
5. 손실과 정확도 그래프
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train_loss', 'val_loss'])
plt.show()
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train_accuracy', 'val_accuracy'])
plt.show()
6. 검증 세트 정확도 계산
loss, accuracy = model.evaluate(x_val, y_val_encoded, verbose=0)
print(accuracy)
##출력: 0.8629999756813049
- 케라스로 구현한 다중 분류 신경망이 좋지 않은 이유
케라스로 구현한 다중 분류 신경망이 MultiClassNetwork에 비해 성능이 뛰어나게 좋지 않은 이유는 이미지 데이터에 잘 맞는 모델이 아니기 때문임
※ 해당 내용은 <Do it! 딥러닝 입문>의 내용을 토대로 학습하며 정리한 내용입니다.
반응형
'딥러닝 학습' 카테고리의 다른 글
8장 이미지 분류 - 합성곱 신경망 (2) (0) | 2023.03.17 |
---|---|
8장 이미지 분류 - 합성곱 신경망 (1) (0) | 2023.03.16 |
7장 여러개를 분류 - 다중 분류 (2) (0) | 2023.03.14 |
7장 여러개를 분류 - 다중 분류 (1) (0) | 2023.03.13 |
6장 2개의 층을 연결 - 다층 신경망 (4) (0) | 2023.03.12 |