神经网络实现手写识别
import tensorflow as tf
mnist=tf.keras.datasets.mnist
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(r"C:\Users\asus\Desktop\MNIST_data", one_hot=True)
x_train=mnist.train.images/255.0 #训练用的图片
y_train=mnist.train.labels #训练的标签
y_train = np.argmax(y_train, axis=1)
x_test=mnist.test.images/255.0 #测试用的图片
y_test=mnist.test.labels #测试的标签
y_test=np.argmax(y_test, axis=1)
model=tf.keras.models.Sequential([tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128,activation='relu'), #第一层网络128个神经元
tf.keras.layers.Dense(10,activation='softmax')]) #第二层网络10个神经元
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
model.fit(x_train,y_train,batch_size=32,epochs=5,validation_data=(x_test,y_test),validation_freq=1)
model.summary()