使用卷积提高识别精度

添加卷积层提高mnist的识别准确率


reshape

import tensorflow as tf
import numpy as np
from matplotlib.pyplot import plot
print(tf.__version__)
mnist = tf.keras.datasets.mnist
(train_images,train_labels),(test_images,test_labels) = mnist.load_data()
train_images=train_images/255
test_images=test_images/255
train_images = train_images.reshape(60000,28,28,1)
test_images = test_images.reshape(10000,28,28,1)
model= tf.keras.Sequential([

    tf.keras.layers.Conv2D(64,(3,3),activation='relu',input_shape=(28,28,1)),
    tf.keras.layers.MaxPool2D(2,2),
    tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
    tf.keras.layers.MaxPool2D(2,2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128,activation='relu'),
    tf.keras.layers.Dense(10,activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.summary()
#注意此处要对输入数据进行[reshape]操作,不然可能会出错。
model.fit(train_images.reshape[-1,28,28,1], train_labels, epochs=1)#对输入值进行reshape
test_loss = model.evaluate(test_images, test_labels)





你可能感兴趣的:(tensorflow)