使用tensorflow创建一个简单的神经网络
1. 加载数据
>>> import tensorflow as tf
>>> from tensorflow import keras
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> print(tf.__version__)
2.5.0
>>> fashion_mnist = keras.datasets.fashion_mnist
>>> (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 1us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 2s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 1us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
>>> class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat','Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
>>> train_images.shape
(60000, 28, 28)
>>> train_images.shape
(60000, 28, 28)
>>> len(train_labels)
60000
>>> train_labels
array([9, 0, 0, ..., 3, 0, 5], dtype=uint8)
对这7万张图像,其中6万张用于训练模型,1万张用于评估模型效果,通过以下代码,可以查看具体的图像内容
>>> train_images = train_images / 255.0
>>> test_images = test_images / 255.0
>>> plt.figure(figsize=(10, 10))
<Figure size 1000x1000 with 0 Axes>
>>> for i in range(25):
... plt.subplot(5, 5, i + 1)
... plt.xticks([])
... plt.yticks([])
... plt.grid(False)
... plt.imshow(train_images[i], cmap=plt.cm.binary)
... plt.xlabel(class_names[train_labels[i]])
...
>>> plt.show()
可视化结果如下
2. 构建神经网络
利用keras的高级API可以方便的构建神经网络模型,这里构建一个3层的神经网络,依次为输入层,隐藏层,输出层,代码如下
>>> model = keras.Sequential([
... keras.layers.Flatten(input_shape=(28, 28)),
... keras.layers.Dense(128, activation='relu'),
... keras.layers.Dense(10)
... ])
输入层的作用是将28X28像素的灰度图像转换为一维数组,没有任何参数;隐藏层包含了128个神经元,输出层包含了10个神经元,对应10个服装类别。
3. 编译
模型在训练之前,必须对其进行编译,主要是确定损失函数,优化器以及评估分类效果好坏的指标,代码如下
>>> model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])
4. 训练模型
使用训练集训练模型,代码如下
>>> model.fit(train_images, train_labels, epochs=10)
2021-06-16 09:40:47.034516: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)
Epoch 1/10
1875/1875 [==============================] - 6s 2ms/step - loss: 0.5006 - accuracy: 0.8248
Epoch 2/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3785 - accuracy: 0.8640
Epoch 3/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.3386 - accuracy: 0.8758
Epoch 4/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.3138 - accuracy: 0.8856
Epoch 5/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2969 - accuracy: 0.8906
Epoch 6/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2816 - accuracy: 0.8958
Epoch 7/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2700 - accuracy: 0.8996
Epoch 8/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2596 - accuracy: 0.9034
Epoch 9/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2485 - accuracy: 0.9078
Epoch 10/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2413 - accuracy: 0.9109
<tensorflow.python.keras.callbacks.History object at 0x00000286E608ED88>
5. 评估模型
使用测试集评估模型效果,代码如下
>>> test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
313/313 - 1s - loss: 0.3343 - accuracy: 0.8850
6. 使用模型进行预测
为了更好的显示预测结果,在模型的后面添加一层softmax层,表示每个类别对应的概率,代码如下
>>> probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
>>> predictions = probability_model.predict(test_images)
>>> predictions[0]
array([3.61419183e-07, 9.85207915e-09, 1.53500035e-09, 8.95436336e-10,
4.15553314e-10, 3.14166857e-04, 1.43166235e-05, 6.38929196e-03,
4.86064877e-09, 9.93281841e-01], dtype=float32)
>>> np.argmax(predictions[0])
9
>>> test_labels[0]
9
上述代码完整展示了神经网络模型的构建,训练,预测等过程,可以看到,通过tensorflow的API可以简单快速的构建一个神经网络模型。
赞 (0)