[딥러닝 Express] Chapter 08. 심층 신경망 [예제: 패션 아이템 분류]

배규리·2024년 1월 26일

AI 기초

목록 보기
21/32
post-thumbnail

문제

패션 MNIST 데이터셋

  • 10개의 범주(Category)
  • 70000개의 데이터
  • 해상도 28x28

직접 구현한 코드😊

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import datasets, layers, models
import cv2
# 데이터셋 불러오기
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
print(train_images[0].shape)
print(len(train_images))
# 데이터 정규화
train_images = train_images / 255.0
test_images = test_images / 255.0


# 모델 구축
model = models.Sequential()
model.add(layers.Flatten(input_shape=(28,28)))
model.add(layers.Dense(128, activation="relu"))
model.add(layers.Dense(10, activation="softmax"))

model.compile(optimizer="adam",
              loss="sparse_categorical_crossentropy",
              metrics=["accuracy"])

history = model.fit(train_images,
          train_labels,
          validation_data = (test_images, test_labels),
          epochs=15,
          batch_size = 256,
          verbose = 2)

history_dict = history.history
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
acc = history_dict['accuracy']
epochs = range(1, len(acc)+1) # 에포크 수

# 그래프 그리기
plt.plot(loss_values)
plt.plot(val_loss_values)
plt.xlabel("epochs")
plt.ylabel("loss")
plt.legend(["train error", "val error"], loc = "upper left")
plt.show()

profile
백엔드 개발은 취미인 AI 개발자🥹

0개의 댓글