<jupyter_start><jupyter_text>准备数据<jupyter_code>import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, optimizers, datasets
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # or any {'0', '1', '2'}

def mnist_dataset():
    (x, y), (x_test, y_test) = datasets.mnist.load_data()
    x = x.reshape(x.shape[0], 28, 28,1)
    x_test = x_test.reshape(x_test.shape[0], 28, 28,1)
    ds = tf.data.Dataset.from_tensor_slices((x, y))
    ds = ds.map(prepare_mnist_features_and_labels)
    ds = ds.take(20000).shuffle(20000).batch(100)
    
    test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test))
    test_ds = test_ds.map(prepare_mnist_features_and_labels)
    test_ds = test_ds.take(20000).shuffle(20000).batch(20000)
    return ds, test_ds

def prepare_mnist_features_and_labels(x, y):
    x = tf.cast(x, tf.float32) / 255.0
    y = tf.cast(y, tf.int64)
    return x, y<jupyter_output><empty_output><jupyter_text>建立模型<jupyter_code>class myConvModel(keras.Model):
    def __init__(self):
        super(myConvModel, self).__init__()
        self.l1_conv = Conv2D(32, (5, 5), activation='relu', padding='same')
        self.l2_conv = Conv2D(64, (5, 5), activation='relu', padding='same')
        self.pool = MaxPooling2D(pool_size=(2, 2), strides=2)
        self.flat = Flatten()
        self.dense1 = layers.Dense(100, activation='tanh')
        self.dense2 = layers.Dense(10)
    @tf.function
    def call(self, x):
        h1 = self.l1_conv(x)
        h1_pool = self.pool(h1)
        h2 = self.l2_conv(h1_pool)
        h2_pool = self.pool(h2)
        flat_h = self.flat(h2_pool)
        dense1 = self.dense1(flat_h)
        logits = self.dense2(dense1)
        probs = tf.nn.softmax(logits, axis=-1)
        return probs

model = myConvModel()
optimizer = optimizers.Adam()<jupyter_output><empty_output><jupyter_text>编译， fit以及evaluate<jupyter_code>model.compile(optimizer=optimizer,
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
train_ds, test_ds = mnist_dataset()
model.fit(train_ds, epochs=5)
model.evaluate(test_ds)<jupyter_output>Epoch 1/5
200/200 [==============================] - 27s 134ms/step - loss: 0.2757 - accuracy: 0.8197
Epoch 2/5
200/200 [==============================] - 26s 132ms/step - loss: 0.0678 - accuracy: 0.9782
Epoch 3/5
200/200 [==============================] - 24s 122ms/step - loss: 0.0405 - accuracy: 0.9880
Epoch 4/5
200/200 [==============================] - 27s 133ms/step - loss: 0.0252 - accuracy: 0.9939
Epoch 5/5
200/200 [==============================] - 25s 127ms/step - loss: 0.0181 - accuracy: 0.9954
1/1 [==============================] - 5s 5s/step - loss: 0.0372 - accuracy: 0.9865
{'fraction_non_alphanumeric': 0.19575971731448763, 'fraction_numerical': 0.0784452296819788, 'mean_word_length': 3.855917667238422, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, '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': '643339', 'license': 'Apache-2.0', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1074, 'n_words': 194, 'path': 'assignments/nndl-exercise/chap5_CNN/tutorial_mnist_conv-keras.ipynb', 'repo_name': 'Cannizza-zzk/DL_review', 'size': '2830'}