<jupyter_start><jupyter_text>基础CNN网络<jupyter_code>import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
print(tf.__version__)<jupyter_output>2.0.0-beta0<jupyter_text>构造数据<jupyter_code>(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
print(x_train.shape, ' ', y_train.shape)
print(x_test.shape, ' ', y_test.shape)
import matplotlib.pyplot as plt

%matplotlib inline
plt.imshow(x_train[0])
plt.show()
x_train = x_train.reshape((-1,28,28,1))
x_test = x_test.reshape((-1,28,28,1))<jupyter_output><empty_output><jupyter_text>构造网络<jupyter_code>model = keras.Sequential()<jupyter_output><empty_output><jupyter_text>卷积层<jupyter_code>model.add(layers.Conv2D(input_shape=(x_train.shape[1], x_train.shape[2], x_train.shape[3]),
                        filters=32, kernel_size=(3,3), strides=(1,1), padding='valid',
                       activation='relu'))<jupyter_output><empty_output><jupyter_text>池化层<jupyter_code>model.add(layers.MaxPool2D(pool_size=(2,2)))<jupyter_output><empty_output><jupyter_text>全连接层<jupyter_code>model.add(layers.Flatten())
model.add(layers.Dense(32, activation='relu'))

# 分类层
model.add(layers.Dense(10, activation='softmax'))<jupyter_output><empty_output><jupyter_text>模型配置<jupyter_code>model.compile(optimizer=keras.optimizers.Adam(),
             # loss=keras.losses.CategoricalCrossentropy(),  # 需要使用to_categorical
             loss=keras.losses.SparseCategoricalCrossentropy(),
              metrics=['accuracy'])

model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 26, 26, 32)        320       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 13, 13, 32)        0         
_________________________________________________________________
flatten (Flatten)            (None, 5408)              0         
_________________________________________________________________
dense (Dense)                (None, 32)                173088    
_________________________________________________________________
dense_1 (Dense)              (None, 10)                330       
=================================================================
Total params: 173,738
Trainable params: 173,738
Non-trainable params: 0
__________________________________________________[...]<jupyter_text>模型训练<jupyter_code>history = model.fit(x_train, y_train, batch_size=64, epochs=5, validation_split=0.1)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.legend(['training', 'valivation'], loc='upper left')
plt.show()
res = model.evaluate(x_test, y_test)<jupyter_output>10000/10000 [==============================] - 0s 33us/sample - loss: 0.0835 - accuracy: 0.9777
{'fraction_non_alphanumeric': 0.1715353260869565, 'fraction_numerical': 0.03872282608695652, 'mean_word_length': 5.252653927813164, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '343085', 'license': 'MIT', 'n_tokens_mistral': 1016, 'n_tokens_neox': 944, 'n_words': 117, 'path': '103_CNN_network.ipynb', 'repo_name': 'smile901122/tensorflow2.0_learning', 'size': '2944'}