<케라스에서 MNIST 데이터셋 적재하기>
from tensorflow.keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
<신경망 구조>
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential([
layers.Dense(512, activation="relu")
layers.Dense(10, activation="softmax")
])
신경망의 층
신경망 훈련을 위해 컴파일 단계에 포함될 추가 세 가지
신경망의 핵심 구성 요소 : Layer(층)
주어진 문제에 더 의미 있는 표현을 입력된 데이터로부터 추출
옵티마이저 : 성능 향상을 위해 입력 데이터 기반으로 모델 업데이트
손실 함수 : 훈련 데이터에서 모델의 성능을 측정하는 방법
훈련과 테스트 과정을 모니터링할 지표
추가설명
<컴파일 단계>
model.compile(optimizer = "rmsprop",
loss = "sparse_categorical_crosentropy",
metrics = ["accuracy"])
<이미지 데이터 준비하기>
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype("float32") / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype("float32") / 255
//[0, 255]사이의 값인 uint8타입에서 [0,1]의 값을 가지는 float32 크기인 배열로 전환
⇒ fit() 메서드 호출하면 훈련 데이터에 모델을 학습시킴
<모델 훈련하기>
model.fit(train_images, train_labels, epochs=5, batch_size=128)
x - 입력 데이터
y - 라벨값
batch_size - 몇 개의 샘플로 가중치를 갱신 할 것인지 지정
epoch - 학습 반복 횟수
(100개의 문제를 batch_size = 10, epochs = 5로 학습하면 10개씩 끊어서 정답을 맞추어 보고
가중치 갱신을 하며 100개의 문제를 5번 반복 학습 하는 것을 의미한다)
=> epochs가 너무 크면 과대적합(overfitting)이 발생할 수 있음
<모델을 사용하여 예측 만들기>
test_digits = test_images[0:10]
predictions = model.predict(test_digits)
predictions[0]
=>출력된 배열의 인덱스 i에 있는 숫자는 숫자 이미지 test_digits[0]이 클래스 i에 속할 확률에 해당함
<새로운 데이터에서 모델 평가하기>
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f"테스트 정확도 : {test_acc}")
텐서
랭크 - 0 텐서
랭크 - 1 텐서
랭크 - 2 텐서
랭크 - 3 텐서
3개의 핵심 속성
배치 데이터
텐서의 실제 사례
데이터를 위한 컨테이너
하나의 숫자만 담고 있는 텐서 : 스칼라(scalar)
텐서.ndim —> 텐서의 축 개수(=rank) 알 수 있음
숫자의 배열을 벡터(vector) 또는 랭크-1 텐서라고 부름
벡터의 배열을 행렬(matrix) 또는 랭크-2 텐서라고 부름
첫번째 축 : 행 / 두번째 축 : 열
행렬의 배열을 랭크-3 텐서라고 부름
이미지 (랭크-4 텐서)
동영상 (랭크-5 텐서)
축의 개수
크기
데이터 타입(dtype) : float16, float32, float64 ….
일반적으로 모든 데이터 텐서의 첫 번째 축은 Sample Axis(0번째 축)
딥러닝 모델은 한 번에 전체 데이터셋을 처리하지 않음
대신에 데이터를 작은 batch로 나눔 (한 번에 학습하는 데이터의 양)
백터 데이터 (samples, features)
시계열 데이터 or 시퀀스 데이터 (samples, timesteps, features)
이미지 (samples, height, width, channels)
동영상 (samples, frames, height, width, channels)
<aside> 📌 SUMMARY:
</aside>