
머신러닝의 기본요소
(train_images, train_labels), _ = mnist.load_data()
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype("float32")
. 이 예제에서는 검증 데이터셋을 사용하지 않기 때문에 _ 변수로 무시합니다.
train_images 배열을 28x28 크기의 이미지 데이터를 784차원 벡터로 변환합니다. 이를 위해 reshape() 함수를 사용
train_images 배열의 값을 0과 1 사이의 값으로 정규화합니다. astype() 함수를 사용하여 배열의 데이터 타입을 float32로 변환한 뒤, 255로 나누어서 정규화합니다.
def get_model():
model = keras.Sequential([
layers.Dense(512, activation="relu"),
layers.Dense(10, activation="softmax")
])
model.compile(optimizer="rmsprop",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
return model
import matplotlib.pyplot as plt
Matplotlib의 pyplot 모듈을 사용하여 그래프를 그리고 있습니다
plt.plot() : 그리기 , plt.show() : 보여주기
random_train_labels = train_labels[:]
np.random.shuffle(random_train_labels)
model.compile(optimizer="rmsprop",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
model.fit(train_images, random_train_labels,
epochs=100,
batch_size=128,
validation_split=0.2)
model.compile(optimizer=keras.optimizers.RMSprop(1.),
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
성능이 많이 올랐다 : 30-> 89

loss 값이 줄어든다고 하여 무조건 좋은 것 x - > overfitting될 가능성 0
model = keras.Sequential([
layers.Dense(96,activation= "relu"),
layers.Dense(96,activation = "relu"),
layers.Dense(10, activation = "softmax"),])
model = keras.Sequential([
layers.Dense(16,
kernel_regularizer=regularizers.l2(0.002),
activation="relu"),
from tensorflow.keras import regularizers
regularizers.l1(0.001)
regularizers.l1_l2(l1=0.001, l2=0.001)
regularizers.l1(0.001) : L1 규제 객체를 생성하며, 규제 강도(regularization strength)를 0.001로 설정합니다. L1 규제는 가중치의 절대값을 사용하여 가중치의 크기를 제한하는 방법입니다.
regularizers.l1_l2(l1=0.001, l2=0.001) : L1-L2 규제 객체를 생성하며, L1 규제와 L2 규제의 규제 강도를 각각 0.001로 설정합니다. L1-L2 규제는 가중치의 절대값과 제곱값을 모두 사용하여 가중치의 크기를 제한하는 방법입니다.
model = keras.Sequential([
layers.Dense(16, activation="relu"),
layers.Dropout(0.5),
layers.Dense(16, activation="relu"),
layers.Dropout(0.5),
layers.Dense(1, activation="sigmoid")
])