利用深度神经网络进行基本图像分类
此代码在colab已经经过测试并且可以查看TensorBoard:地址
环境:Tensorflow-2.1.0、Python3.6、google colab
In[1]:
1 | import ssl |
In[2]:
1 | import matplotlib as mpl |
Out[1]:
matplotlib 3.1.2
numpy 1.18.1
pandas 0.25.3
sklearn 0.22.1
tensorflow 2.1.0
tensorflow_core.keras 2.2.4-tf
In[3]:
1 | fashion_mnist=keras.datasets.fashion_mnist |
In[4]
1 | x_valid,x_train=x_train_all[:5000],x_train_all[5000:] |
Out[2]
(5000, 28, 28) (5000,)
(55000, 28, 28) (5000,)
(10000, 28, 28) (10000,)
In[5]:
1 | print(np.max(x_train),np.min(x_train)) |
Out[3]:
255 0
In[6]:
1 | # x= (x-u)/std |
Out[4]:
2.0231433 -0.8105136
In[7]:
1 | def show_single_image(img_arr): |
Out[5]:
In[8]:
1 | def show_images(n_rows,n_cols,x_data,y_data,class_names): |
Out[6]:
In[9]:
1 | # 构建一个三层的神经网络 |
tensorflow.keras.model.compile
In[10]:
1 | model.layers |
Out[7]:
[<tensorflow.python.keras.layers.core.Flatten at 0x136ef6a90>,
<tensorflow.python.keras.layers.core.Dense at 0x136ef6ad0>,
<tensorflow.python.keras.layers.core.Dense at 0x136eb5f90>,
<tensorflow.python.keras.layers.core.Dense at 0x13689dcd0>,
<tensorflow.python.keras.layers.core.Dense at 0x136ed6990>,
<tensorflow.python.keras.layers.core.Dense at 0x136894090>,
<tensorflow.python.keras.layers.core.Dense at 0x13689b450>,
<tensorflow.python.keras.layers.core.Dense at 0x13689b110>,
<tensorflow.python.keras.layers.core.Dense at 0x111477050>,
<tensorflow.python.keras.layers.core.Dense at 0x13a68c7d0>,
<tensorflow.python.keras.layers.core.Dense at 0x1114bd810>,
<tensorflow.python.keras.layers.core.Dense at 0x13a7035d0>,
<tensorflow.python.keras.layers.core.Dense at 0x13a6a9050>,
<tensorflow.python.keras.layers.core.Dense at 0x13a6a9bd0>,
<tensorflow.python.keras.layers.core.Dense at 0x107df4650>,
<tensorflow.python.keras.layers.core.Dense at 0x107e2b410>,
<tensorflow.python.keras.layers.core.Dense at 0x13a724910>,
<tensorflow.python.keras.layers.core.Dense at 0x13a6dd210>,
<tensorflow.python.keras.layers.core.Dense at 0x13e4b1b90>,
<tensorflow.python.keras.layers.core.Dense at 0x137080b10>,
<tensorflow.python.keras.layers.core.Dense at 0x13e4a1a10>,
<tensorflow.python.keras.layers.noise.AlphaDropout at 0x136ed69d0>,
<tensorflow.python.keras.layers.core.Dense at 0x1370af710>]
In[11]:
1 | model.summary() #打印模型的结构 |
Out[8]:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) (None, 784) 0
_________________________________________________________________
dense (Dense) (None, 100) 78500
_________________________________________________________________
dense_1 (Dense) (None, 100) 10100
_________________________________________________________________
dense_2 (Dense) (None, 100) 10100
_________________________________________________________________
dense_3 (Dense) (None, 100) 10100
_________________________________________________________________
dense_4 (Dense) (None, 100) 10100
_________________________________________________________________
dense_5 (Dense) (None, 100) 10100
_________________________________________________________________
dense_6 (Dense) (None, 100) 10100
_________________________________________________________________
dense_7 (Dense) (None, 100) 10100
_________________________________________________________________
dense_8 (Dense) (None, 100) 10100
_________________________________________________________________
dense_9 (Dense) (None, 100) 10100
_________________________________________________________________
dense_10 (Dense) (None, 100) 10100
_________________________________________________________________
dense_11 (Dense) (None, 100) 10100
_________________________________________________________________
dense_12 (Dense) (None, 100) 10100
_________________________________________________________________
dense_13 (Dense) (None, 100) 10100
_________________________________________________________________
dense_14 (Dense) (None, 100) 10100
_________________________________________________________________
dense_15 (Dense) (None, 100) 10100
_________________________________________________________________
dense_16 (Dense) (None, 100) 10100
_________________________________________________________________
dense_17 (Dense) (None, 100) 10100
_________________________________________________________________
dense_18 (Dense) (None, 100) 10100
_________________________________________________________________
dense_19 (Dense) (None, 100) 10100
_________________________________________________________________
alpha_dropout (AlphaDropout) (None, 100) 0
_________________________________________________________________
dense_20 (Dense) (None, 10) 1010
=================================================================
Total params: 271,410
Trainable params: 271,410
Non-trainable params: 0
_________________________________________________________________
In[12]:
1 | # 第一层 [None, 784] * w + b -> [None, 300] w.shape=[784,300], b=[300] |
In[13]:
1 | # TensorBoard,EarlyStopping,ModelCheckPoint |
Out[9]:
Train on 55000 samples, validate on 5000 samples
Epoch 1/10
55000/55000 [==============================] - 11s 195us/sample - loss: 0.6998 - accuracy: 0.7617 - val_loss: 0.5656 - val_accuracy: 0.8536
Epoch 2/10
55000/55000 [==============================] - 8s 144us/sample - loss: 0.4626 - accuracy: 0.8428 - val_loss: 0.6195 - val_accuracy: 0.8558
Epoch 3/10
55000/55000 [==============================] - 9s 162us/sample - loss: 0.4065 - accuracy: 0.8581 - val_loss: 0.5300 - val_accuracy: 0.8668
Epoch 4/10
55000/55000 [==============================] - 8s 142us/sample - loss: 0.3755 - accuracy: 0.8717 - val_loss: 0.6154 - val_accuracy: 0.8664
Epoch 5/10
55000/55000 [==============================] - 8s 152us/sample - loss: 0.3521 - accuracy: 0.8774 - val_loss: 0.5287 - val_accuracy: 0.8766
Epoch 6/10
55000/55000 [==============================] - 8s 153us/sample - loss: 0.3343 - accuracy: 0.8815 - val_loss: 0.4956 - val_accuracy: 0.8856
Epoch 7/10
55000/55000 [==============================] - 8s 143us/sample - loss: 0.3193 - accuracy: 0.8857 - val_loss: 0.5349 - val_accuracy: 0.8716
Epoch 8/10
55000/55000 [==============================] - 8s 144us/sample - loss: 0.3085 - accuracy: 0.8916 - val_loss: 0.5294 - val_accuracy: 0.8842
Epoch 9/10
55000/55000 [==============================] - 8s 145us/sample - loss: 0.2955 - accuracy: 0.8946 - val_loss: 0.4668 - val_accuracy: 0.8822
Epoch 10/10
55000/55000 [==============================] - 8s 152us/sample - loss: 0.2839 - accuracy: 0.8975 - val_loss: 0.4907 - val_accuracy: 0.8860
In[14]:
1 | type(history) |
Out[10]:
tensorflow.python.keras.callbacks.History
In[15]:
1 | history.history |
Out[11]:
{'loss': [0.6997782779043371,
0.4625766654621471,
0.4064591264659708,
0.37553091496554286,
0.35207060631621967,
0.3343261710730466,
0.3193407494176518,
0.30854321794509887,
0.29552189791636035,
0.28390346760099583],
'accuracy': [0.76165456,
0.84276366,
0.85809094,
0.8717273,
0.87743634,
0.8814909,
0.8856909,
0.89156365,
0.89461815,
0.8974909],
'val_loss': [0.5656432386219501,
0.6194551657661795,
0.5299783393919468,
0.6153697894215584,
0.5287225459891837,
0.49561948866695166,
0.5348633618405322,
0.5294362505242228,
0.4668049536377192,
0.4907300000913441],
'val_accuracy': [0.8536,
0.8558,
0.8668,
0.8664,
0.8766,
0.8856,
0.8716,
0.8842,
0.8822,
0.886]}
In[16]
1 | def plot_learning_curves(history): |
Out[12]:
In[17]:
1 | model.evaluate(x_test_scaled,y_test) # 在测试集上验证 |
tensorflow.keras.model.evaluate
Out[13]:
10000/10000 [==============================] - 1s 95us/sample - loss: 0.5799 - accuracy: 0.8741
Out[14]:
[0.5799049034297467, 0.8741]
汇总:
1 | #!/usr/bin/env python |