<jupyter_start><jupyter_text>Early Stopping ------<jupyter_code>import random
import numpy as np
import os
import tensorflow as tf
def seed_everything(seed: int = 42):
    random.seed(seed)
    np.random.seed(seed)
    os.environ["PYTHONHASHSEED"] = str(seed)
    tf.random.set_seed(seed)
    
seed_everything()<jupyter_output><empty_output><jupyter_text>0 .기존 작업<jupyter_code>from tensorflow.keras.datasets import fashion_mnist

(x, y), (x_test, y_test) = fashion_mnist.load_data()


from tensorflow.keras.utils import to_categorical

x = x.astype('float32') 
x_test = x_test.astype('float32')

x /= 255
x_test /= 255

y = to_categorical(y, 10)
y_test = to_categorical(y_test, 10)


from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten

def create_model():
    model = Sequential()
    model.add(Flatten(input_shape=(28, 28)))
    model.add(Dense(units=32, activation='relu'))
    model.add(Dense(64, 'relu'))
    model.add(Dense(128, 'relu'))
    model.add(Dense(units=10, activation='softmax'))
    
    model.compile(optimizer='adam', 
                  loss='categorical_crossentropy',
                 metrics=['accuracy'])

    return model

model = create_model()
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten (Flatten)            (None, 784)               0         
_________________________________________________________________
dense (Dense)                (None, 32)                25120     
_________________________________________________________________
dense_1 (Dense)              (None, 64)                2112      
_________________________________________________________________
dense_2 (Dense)              (None, 128)               8320      
_________________________________________________________________
dense_3 (Dense)              (None, 10)                1290      
=================================================================
Total params: 36,842
Trainable params: 36,842
Non-trainable params: 0
____________________________________________________[...]<jupyter_text>---  1. callback 만들기https://keras.io/callbacks/earlystopping- monitor: early stopping을 결정하는 기준- min_delta: 이거보다 변화가 작으면 멈춘다.- patience: 이 숫자의 epoch만큼 변하지 개선(감소)되지 않으면 멈춘다. (2로 설정시, 2연속 val_loss가 증가하면 train 중지)<jupyter_code>from tensorflow.keras.callbacks import EarlyStopping
es = EarlyStopping(monitor='val_loss', patience=2, verbose=1)
model = create_model()

history = model.fit(x, y,  epochs = 20, validation_split = 1/6,callbacks = [es], batch_size= 512)
model = create_model()

history = model.fit(x, y,  epochs = 20, validation_split = 1/6,callbacks = [es], batch_size= 512)
import matplotlib.pyplot as plt
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(1, len(loss) + 1)

plt.figure(figsize=(20,5))
plt.plot(epochs, loss, 'r', label = 'Training loss')
plt.plot(epochs, val_loss, 'b', label = 'Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend() 
plt.xlim(5,9)
plt.ylim(0.3,0.4)

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15148755312689738, 'fraction_numerical': 0.031572556162720096, 'mean_word_length': 4.1244167962674965, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '294466', 'license': 'MIT', 'n_tokens_mistral': 1155, 'n_tokens_neox': 1087, 'n_words': 218, 'path': 'Early_Stopping.ipynb', 'repo_name': 'jiwonlee-0218/colab', 'size': '3294'}