<jupyter_start><jupyter_text>실전 문제 해결 (과적합)<jupyter_code>import tensorflow as tf
import numpy as np<jupyter_output><empty_output><jupyter_text>하이퍼 파라미터<jupyter_code>EPOCHS = 100<jupyter_output><empty_output><jupyter_text>모델 정의<jupyter_code>class ConvLNReluBock(tf.keras.Model):
    def __init__(self, filters, kernel_size):
        super(ConvLNReluBock, self).__init__()
        self.conv = tf.keras.layers.Conv2D(filters, kernel_size, padding='same', use_bias=False)
        self.ln = tf.keras.layers.LayerNormalization()
        
    def call(self, x, training=False, mask=None):
        x = self.conv(x)
        x = self.ln(x)
        return tf.nn.relu(x)

# Define network architecture
class MyModel(tf.keras.Model):
    def __init__(self):
        super(MyModel, self).__init__()
        self.conv1_1 = ConvLNReluBock(16, (3, 3))
        self.conv1_2 = ConvLNReluBock(16, (3, 3))
        self.pool1 = tf.keras.layers.MaxPool2D((2, 2))

        self.conv2_1 = ConvLNReluBock(32, (3, 3))
        self.conv2_2 = ConvLNReluBock(32, (3, 3))
        self.pool2 = tf.keras.layers.MaxPool2D((2, 2))

        self.conv3_1 = ConvLNReluBock(64, (3, 3))
        self.conv3_2 = ConvLNReluBock(64, (3, 3))

        self.flatten = tf.keras.layers.Flatten()
        self.dense1 = tf.keras.layers.Dense(1024, activation='relu', 
                                            kernel_regularizer=tf.keras.regularizers.l2(0.01))
        self.dense2 = tf.keras.layers.Dense(10, activation='softmax',
                                            kernel_regularizer=tf.keras.regularizers.l2(0.01))

    def call(self, x, training=False, mask=None):
        x = self.conv1_1(x)
        x = self.conv1_2(x)
        x = self.pool1(x)

        x = self.conv2_1(x)
        x = self.conv2_2(x)
        x = self.pool2(x)

        x = self.conv3_1(x)
        x = self.conv3_2(x)

        x = self.flatten(x)
        x = self.dense1(x)
        return self.dense2(x)<jupyter_output><empty_output><jupyter_text>데이터셋 준비<jupyter_code>cifar10 = tf.keras.datasets.cifar10

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

x_train = x_train.astype(np.float32)
x_test = x_test.astype(np.float32)

train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(32).prefetch(1024)
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32).prefetch(1024)<jupyter_output>Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
170500096/170498071 [==============================] - 697s 4us/step<jupyter_text>Keras API 모델 학습<jupyter_code>model = MyModel()
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.fit(train_ds, validation_data=test_ds, epochs=EPOCHS)<jupyter_output>Epoch 1/100
1563/1563 [==============================] - 228s 146ms/step - loss: 2.1967 - accuracy: 0.3981 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Epoch 2/100
1563/1563 [==============================] - 240s 154ms/step - loss: 1.2805 - accuracy: 0.5889 - val_loss: 1.2031 - val_accuracy: 0.6180
Epoch 3/100
1563/1563 [==============================] - 255s 163ms/step - loss: 1.0841 - accuracy: 0.6605 - val_loss: 1.0222 - val_accuracy: 0.6761
Epoch 4/100
1563/1563 [==============================] - 255s 163ms/step - loss: 0.9772 - accuracy: 0.6989 - val_loss: 1.0243 - val_accuracy: 0.6797
Epoch 5/100
1563/1563 [==============================] - 255s 163ms/step - loss: 0.9202 - accuracy: 0.7181 - val_loss: 0.9965 - val_accuracy: 0.6937
Epoch 6/100
1563/1563 [==============================] - 236s 151ms/step - loss: 0.8703 - accuracy: 0.7389 - val_loss: 0.9061 - val_accuracy: 0.7222
Epoch 7/100
1563/1563 [==============================] - 240s 154ms/step - loss: 0.8249 - accuracy[...]
{'fraction_non_alphanumeric': 0.19567483064095884, 'fraction_numerical': 0.10265763418447107, 'mean_word_length': 3.3280721533258175, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '628724', 'license': 'MIT', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1511, 'n_words': 239, 'path': 'RNN_TF20/11. Deep Learning Heuristic with Overfitting-Antonio.ipynb', 'repo_name': '46gom/DLND_Exercises', 'size': '3838'}