# 【深度学习实践（二）】上手手写数字识别

### 1 设置运行设备

tf.config.set_visible_devices([gpu0],"GPU")

• 打印出一部分训练数据查看

### 3 构建神经网络模型并进行训练

#调整数据到我们需要的格式
train_images = train_images.reshape((60000, 28, 28, 1))
test_images = test_images.reshape((10000, 28, 28, 1))

train_images.shape,test_images.shape,train_labels.shape,test_labels.shape
"""

"""
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),  # 卷积层1，卷积核3*3
layers.MaxPooling2D((2, 2)),  # 池化层1，2*2采样
layers.Conv2D(64, (3, 3), activation='relu'),  # 卷积层2，卷积核3*3
layers.MaxPooling2D((2, 2)),  # 池化层2，2*2采样

layers.Flatten(),  # Flatten层
layers.Dense(64, activation='relu'),  # 全连接层
layers.Dense(10)  # 输出层
])
# 打印网络结构
model.summary()
• 编译与训练模型
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))

### 4 预测结果

plt.imshow(test_images[3])
plt.show()
newT=test_images[3].reshape(1,784)
print('Test图片向量化为：'+str(newT))

print('预测ing...')
resT=test_images[3].reshape(1,28,28,1)
pre=model.predict(resT)
print('即得到预测结果'+str(pre))

1，学习的收货

2，学习遇到的问题

THE END