-
import tensorflow as tf mnist = tf.keras.datasets.mnist #下载mnist图像的数据 (x_train, y_train),(x_test, y_test) = mnist.load_data() #划分训练集和测试集 x_train, x_test = x_train / 255.0, x_test / 255.0 #归一化处理[0,1] #序贯(Sequential)模型 model = tf.keras.Sequential([ tf.keras.layers.Flatten(input_shape=[28,28]), #展平图像数据 tf.keras.layers.Dense(512, activation=tf.nn.relu), #全连接层512列 tf.keras.layers.Dropout(0.2), #需要断开的神经元的比例 tf.keras.layers.Dense(10, activation=tf.nn.softmax) #全连接层10列 ]) model.compile(optimizer='adam', #优化器 loss='sparse_categorical_crossentropy', #损失函数交叉熵 metrics=['accuracy']) #评价指标列表 model.fit(x_train, y_train, epochs=5,batch_size=50) #fit开始训练迭代5轮 model.evaluate(x_test, y_test) #对测试集进行测试
- 运行得到输出结果:
- 55900/60000 [==========================>...]55900/60000 [==========================>...] - ETA: 0s - loss: 0.0460 - acc: 0.9857
- 56400/60000 [===========================>..]56400/60000 [===========================>..] - ETA: 0s - loss: 0.0461 - acc: 0.9857
- 56900/60000 [===========================>..]56900/60000 [===========================>..] - ETA: 0s - loss: 0.0460 - acc: 0.9856
- 57400/60000 [===========================>..]57400/60000 [===========================>..] - ETA: 0s - loss: 0.0463 - acc: 0.9856
- 57900/60000 [===========================>..]57900/60000 [===========================>..] - ETA: 0s - loss: 0.0464 - acc: 0.9856
- 58400/60000 [============================>.]58400/60000 [============================>.] - ETA: 0s - loss: 0.0466 - acc: 0.9855
- 58900/60000 [============================>.]58900/60000 [============================>.] - ETA: 0s - loss: 0.0465 - acc: 0.9855
- 59400/60000 [============================>.]59400/60000 [============================>.] - ETA: 0s - loss: 0.0468 - acc: 0.9854
- 59900/60000 [============================>.]59900/60000 [============================>.] - ETA: 0s - loss: 0.0469 - acc: 0.9853
- 60000/60000 [==============================]60000/60000 [==============================] - 7s 116us/step - loss: 0.0469 - acc: 0.9853
- 32/10000 [..............................] 32/10000 [..............................] - ETA: 8s
- 928/10000 [=>............................] 928/10000 [=>............................] - ETA: 0s
- 1856/10000 [====>.........................] 1856/10000 [====>.........................] - ETA: 0s
- 2720/10000 [=======>......................] 2720/10000 [=======>......................] - ETA: 0s
- 3584/10000 [=========>....................] 3584/10000 [=========>....................] - ETA: 0s
- 4544/10000 [============>.................] 4544/10000 [============>.................] - ETA: 0s
- 5376/10000 [===============>..............] 5376/10000 [===============>..............] - ETA: 0s
- 6272/10000 [=================>............] 6272/10000 [=================>............] - ETA: 0s
- 7136/10000 [====================>.........] 7136/10000 [====================>.........] - ETA: 0s
- 8096/10000 [=======================>......] 8096/10000 [=======================>......] - ETA: 0s
- 9056/10000 [==========================>...] 9056/10000 [==========================>...] - ETA: 0s
- 9888/10000 [============================>.] 9888/10000 [============================>.] - ETA: 0s
- 10000/10000 [==============================]10000/10000 [==============================] - 1s 60us/step
- [loss,accuracy]
- [0.06254660903802142, 0.9796]