MNIST数据如何与TensorFlow一起用于识别手写数字?
Tensorflow是Google提供的一种机器学习框架。它是一个开放源代码框架,与Python结合使用以实现算法,深度学习应用程序等等。它用于研究和生产目的。
可以使用下面的代码行在Windows上安装'tensorflow'软件包-
pip install tensorflow
Tensor是TensorFlow中使用的数据结构。它有助于连接流程图中的边缘。该流程图称为“数据流程图”。张量不过是多维数组或列表。
我们正在使用Google合作实验室来运行以下代码。GoogleColab或Colaboratory可以帮助通过浏览器运行Python代码,并且需要零配置和对GPU(图形处理单元)的免费访问。合作已建立在JupyterNotebook的基础上。
MNIST数据集包含手写数字,其中60000个数字用于训练模型,而10000个数字用于测试训练的模型。这些数字已经过大小归一化和居中调整以适合固定大小的图像。
以下是代码-
示例
import tensorflow as tf mnist = tf.keras.datasets.mnist print("Data is being loaded") (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10) ]) predictions = model(x_train[:1]).numpy() print("预测是: ") print(predictions) tf.nn.softmax(predictions).numpy() loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) print("The loss function has been defined") loss_fn(y_train[:1], predictions).numpy() model.compile(optimizer='adam', loss=loss_fn, metrics=['accuracy']) model.fit(x_train, y_train, epochs=9) print("The data is being fit to the model") model.evaluate(x_test, y_test, verbose=2) print("The results are being evaluated") probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax() ]) print("预测是: ") probability_model(x_test[:8])
代码信用-https://www.tensorflow.org/tutorials/quickstart/beginner
输出结果
Data is being loaded 预测是: [[-0.77715474 -0.21606012 -0.04190525 -0.22804758 0.03612506 0.5986039 0.6838669 -0.40150493 0.55429333 0.55918723]] The loss function has been defined Epoch 1/9 1875/1875 [==============================] - 4s 2ms/step - loss: 0.4934 - accuracy: 0.8564 Epoch 2/9 1875/1875 [==============================] - 4s 2ms/step - loss: 0.1511 - accuracy: 0.9566 Epoch 3/9 1875/1875 [==============================] - 4s 2ms/step - loss: 0.1046 - accuracy: 0.9690 Epoch 4/9 1875/1875 [==============================] - 4s 2ms/step - loss: 0.0861 - accuracy: 0.9733 Epoch 5/9 1875/1875 [==============================] - 4s 2ms/step - loss: 0.0712 - accuracy: 0.9779 Epoch 6/9 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0621 - accuracy: 0.9798 Epoch 7/9 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0544 - accuracy: 0.9822 Epoch 8/9 1875/1875 [==============================] - 4s 2ms/step - loss: 0.0505 - accuracy: 0.9837 Epoch 9/9 1875/1875 [==============================] - 4s 2ms/step - loss: 0.0442 - accuracy: 0.9853 The data is being fit to the model 313/313 - 0s - loss: 0.0691 - accuracy: 0.9792 The results are being evaluated 预测是: <tf.Tensor: shape=(8, 10), dtype=float32, numpy= array([[5.02094943e-09, 9.46477272e-11, 2.06162738e-07, 4.83285694e-05, 4.39524014e-13, 1.28035786e-08, 2.14148154e-16, 9.99951243e-01, 7.93324517e-09, 2.28497953e-07], [1.91046940e-12, 2.99942280e-06, 9.99997020e-01, 1.57965779e-10, 8.59648663e-21, 6.85132751e-09, 3.96876629e-11, 1.56446678e-15, 7.72609667e-12, 1.23343747e-16], [2.50448129e-09, 9.99879003e-01, 4.76238492e-05, 2.03783550e-08, 8.07857646e-07, 9.36941333e-08, 1.20849165e-06, 5.47862328e-05, 1.65028414e-05, 1.13786543e-10], [9.99895811e-01, 5.68212422e-10, 5.65115661e-05, 1.29087857e-06, 1.05537436e-06, 1.18774800e-07, 4.08086999e-05, 3.00701231e-06, 3.09539394e-09, 1.48017170e-06], [2.68701024e-06, 8.04860432e-12, 2.38641114e-05, 1.81536635e-07, 9.98934567e-01, 8.67565220e-08, 6.51489245e-07, 2.03823347e-05, 2.35504160e-07, 1.01726933e-03], [3.68854253e-11, 9.99950528e-01, 4.57934220e-07, 5.73739001e-10, 1.50253769e-07, 1.97563521e-10, 2.10510054e-09, 4.76488349e-05, 1.17310219e-06, 1.29714104e-11], [5.41477474e-10, 2.30981789e-09, 2.14095284e-08, 1.05171161e-07, 9.97043908e-01, 5.71491137e-06, 4.81293283e-10, 4.48020655e-05, 6.65718471e-05, 2.83887982e-03], [1.04743374e-10, 1.53262540e-08, 5.02297407e-05, 2.83393019e-04, 2.03316798e-04, 2.68098956e-05, 9.15681864e-10, 1.22959409e-05, 7.81168455e-06, 9.99416113e-01]], dtype=float32)>
解释
所需的软件包已下载并使用别名。
MNIST数据集是从源代码下载的。
数据集分为训练和测试数据。
使用“keras”包构建顺序模型。
预测是在测试数据集上进行的。
损失函数是使用“Keras”软件包中的“SparseCategoricalCrossentropy”方法定义的。
编译模型,然后将其拟合到数据。
通过使用测试数据集评估此训练数据。
预测显示在控制台上。