温州市网站建设_网站建设公司_SQL Server_seo优化
2026/1/11 17:18:00 网站建设 项目流程

where python
D:\Python37\python.exe

cd D:\Python37
pip freeze |grep -i tensorflow
tensorflow==2.11.0
tensorflow-estimator==2.11.0
tensorflow-intel==2.11.0
tensorflow-io-gcs-filesystem==0.31.0

pip freeze |grep -i keras
keras==2.11.0

编写一个 keras 实现 mnist 数据集手写数字识别的示例 keras_mnist_1.py 如下

# -*- coding: utf-8 -*- from tensorflow import keras from keras import layers from keras.datasets import mnist # 在 keras 中加载 MNIST数据集 (train_images, train_labels), (test_images, test_labels) = mnist.load_data() print('mnist train:',train_images.shape, len(train_labels)) print('mnist test :',test_images.shape, len(test_labels)) # 神经网络模型 model = keras.Sequential([ layers.Dense(512, activation="relu"), layers.Dense(10, activation="softmax") ]) # 模型编译 model.compile(optimizer="rmsprop", loss="sparse_categorical_crossentropy", metrics=["accuracy"]) print(model.summary) # 准备图像数据 train_images = train_images.reshape((60000, 28 * 28)) train_X = train_images.astype("float32") / 255 test_images = test_images.reshape((10000, 28 * 28)) test_X = test_images.astype("float32") / 255 # 模型拟合 model.fit(train_X, train_labels, epochs=5, batch_size=128) # 使用模型进行预测 test_digits = test_images[0:10] predictions = model.predict(test_digits) print('predictions[0]:') print(predictions[0].argmax()) print(predictions[0][7]) print('test_labels_0:',test_labels[0]) # 在测试数据上评估模型 test_loss, test_acc = model.evaluate(test_X, test_labels) print(f"test_acc: {test_acc}")

运行 cmd
python keras_mnist_1.py

D:\python> python keras_mnist_1.py mnist train: (60000, 28, 28) 60000 mnist test : (10000, 28, 28) 10000 2026-01-11 09:54:07.683219: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. <bound method Model.summary of <keras.engine.sequential.Sequential object at 0x0000025721819348>> Epoch 1/5 469/469 [==============================] - 12s 15ms/step - loss: 0.2653 - accuracy: 0.9240 Epoch 2/5 469/469 [==============================] - 7s 15ms/step - loss: 0.1081 - accuracy: 0.9680 Epoch 3/5 469/469 [==============================] - 7s 15ms/step - loss: 0.0714 - accuracy: 0.9788 Epoch 4/5 469/469 [==============================] - 7s 15ms/step - loss: 0.0517 - accuracy: 0.9845 Epoch 5/5 469/469 [==============================] - 7s 15ms/step - loss: 0.0381 - accuracy: 0.9887 1/1 [==============================] - 1s 937ms/step predictions[0]: 7 1.0 test_labels_0: 7 313/313 [==============================] - 5s 8ms/step - loss: 0.0675 - accuracy: 0.9801 test_acc: 0.9800999760627747

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询