**
**
这篇紧接着上一篇的博文
深度学习笔记-----基于TensorFlow2.2.0代码练习(第三课&第四课)
主要写的是TensorFlow2.0的代码练习,跟随着[KGP Talkie的【TensorFlow 2.0】实战进阶教程]进行学习,并将其中一些不适用的代码错误进行修改。
本文跟随视频油管非常火的【TensorFlow 2.0】实战进阶教程(中英字幕+代码实战)第五课
————————————————
首先,需要有登登登的能力,因为这是在goodle colab上进行编写。除此之外,需要有一个谷歌的账号。
为以后需要,登陆谷歌云
点击新建按钮,新建一个colab文件夹,这样会和你的colab进行同步保存。
第一次时候,需要在更多----关联更多应用-----加载colabpratory
!pip install tensorflow-gpu
Collecting tensorflow-gpu
[?25l Downloading https://files.pythonhosted.org/packages/31/bf/c28971266ca854a64f4b26f07c4112ddd61f30b4d1f18108b954a746f8ea/tensorflow_gpu-2.2.0-cp36-cp36m-manylinux2010_x86_64.whl (516.2MB)
[K |████████████████████████████████| 516.2MB 30kB/s
[?25hRequirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (1.12.0)
Requirement already satisfied: grpcio>=1.8.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (1.29.0)
Requirement already satisfied: wheel>=0.26; python_version >= "3" in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (0.34.2)
Requirement already satisfied: absl-py>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (0.9.0)
Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (1.1.0)
Requirement already satisfied: scipy==1.4.1; python_version >= "3" in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (1.4.1)
Requirement already satisfied: wrapt>=1.11.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (1.12.1)
Requirement already satisfied: astunparse==1.6.3 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (1.6.3)
Requirement already satisfied: tensorflow-estimator<2.3.0,>=2.2.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (2.2.0)
Requirement already satisfied: tensorboard<2.3.0,>=2.2.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (2.2.2)
Requirement already satisfied: gast==0.3.3 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (0.3.3)
Requirement already satisfied: h5py<2.11.0,>=2.10.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (2.10.0)
Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (3.2.1)
Requirement already satisfied: protobuf>=3.8.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (3.10.0)
Requirement already satisfied: google-pasta>=0.1.8 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (0.2.0)
Requirement already satisfied: keras-preprocessing>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (1.1.2)
Requirement already satisfied: numpy<2.0,>=1.16.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu) (1.18.4)
Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (3.2.2)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (0.4.1)
Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (1.0.1)
Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (2.23.0)
Requirement already satisfied: google-auth<2,>=1.6.3 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (1.7.2)
Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (47.1.1)
Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (1.6.0.post3)
Requirement already satisfied: importlib-metadata; python_version < "3.8" in /usr/local/lib/python3.6/dist-packages (from markdown>=2.6.8->tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (1.6.0)
Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (1.3.0)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (2.9)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (1.24.3)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (2020.4.5.1)
Requirement already satisfied: cachetools<3.2,>=2.0.0 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (3.1.1)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (0.2.8)
Requirement already satisfied: rsa<4.1,>=3.1.4 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (4.0)
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.6/dist-packages (from importlib-metadata; python_version < "3.8"->markdown>=2.6.8->tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (3.1.0)
Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.6/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (3.1.0)
Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /usr/local/lib/python3.6/dist-packages (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow-gpu) (0.4.8)
Installing collected packages: tensorflow-gpu
Successfully installed tensorflow-gpu-2.2.0
!pip install mlxtend==0.17.0
Collecting mlxtend==0.17.0
[?25l Downloading https://files.pythonhosted.org/packages/52/04/c362f34f666f0ddc7cf593805e64d64fa670ed96fd9302e68549dd48287d/mlxtend-0.17.0-py2.py3-none-any.whl (1.3MB)
[K |████████████████████████████████| 1.3MB 3.5MB/s
[?25hRequirement already satisfied: pandas>=0.24.2 in /usr/local/lib/python3.6/dist-packages (from mlxtend==0.17.0) (1.0.4)
Requirement already satisfied: scikit-learn>=0.20.3 in /usr/local/lib/python3.6/dist-packages (from mlxtend==0.17.0) (0.22.2.post1)
Requirement already satisfied: scipy>=1.2.1 in /usr/local/lib/python3.6/dist-packages (from mlxtend==0.17.0) (1.4.1)
Requirement already satisfied: matplotlib>=3.0.0 in /usr/local/lib/python3.6/dist-packages (from mlxtend==0.17.0) (3.2.1)
Requirement already satisfied: joblib>=0.13.2 in /usr/local/lib/python3.6/dist-packages (from mlxtend==0.17.0) (0.15.1)
Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from mlxtend==0.17.0) (47.1.1)
Requirement already satisfied: numpy>=1.16.2 in /usr/local/lib/python3.6/dist-packages (from mlxtend==0.17.0) (1.18.4)
Requirement already satisfied: python-dateutil>=2.6.1 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.24.2->mlxtend==0.17.0) (2.8.1)
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.24.2->mlxtend==0.17.0) (2018.9)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=3.0.0->mlxtend==0.17.0) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=3.0.0->mlxtend==0.17.0) (1.2.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=3.0.0->mlxtend==0.17.0) (2.4.7)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil>=2.6.1->pandas>=0.24.2->mlxtend==0.17.0) (1.12.0)
Installing collected packages: mlxtend
Found existing installation: mlxtend 0.14.0
Uninstalling mlxtend-0.14.0:
Successfully uninstalled mlxtend-0.14.0
Successfully installed mlxtend-0.17.0
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Flatten, Dense, Conv2D, MaxPool2D, Dropout
原始错误:
File “”, line 2
from tensorflow.keras.models import Sequential()
^
SyntaxError: invalid syntax
此问题是因为运行from tensorflow.keras import Sequential()出错解决办法将()删除
print(tf.__version__)
2.2.0
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from tensorflow.keras.datasets import cifar10
(X_train, y_train),(X_test, y_test)= cifar10.load_data()
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
170500096/170498071 [==============================] - 3s 0us/step
class_name = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']
#归一化
X_train = X_train/255
X_test = X_test/255
#读取训练和测试集的大小
X_train.shape
X_test.shape
(10000, 32, 32, 3)
建立CNN模型
model = Sequential()#获取模型
#添加第一个卷积层
model.add(Conv2D(filters=32, kernel_size=(3,3),padding='same',activation='relu',input_shape = [32,32,3]))
#添加第二个卷积层
model.add(Conv2D(filters=32, kernel_size=(3,3),padding='same',activation='relu'))
#添加池化层
model.add(MaxPool2D(pool_size=(2,2),strides=2, padding='valid'))
#使用dropout防止过拟合
model.add(Dropout(0.5))
#添加CF
model.add(Flatten())
model.add(Dense(units=128, activation='relu'))
#添加输出层
model.add(Dense(units=10, activation='softmax'))
#打印模型的概要
model.summary()
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_3 (Conv2D) (None, 32, 32, 32) 896
_________________________________________________________________
conv2d_4 (Conv2D) (None, 32, 32, 32) 9248
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 16, 16, 32) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 16, 16, 32) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 8192) 0
_________________________________________________________________
dense_2 (Dense) (None, 128) 1048704
_________________________________________________________________
dense_3 (Dense) (None, 10) 1290
=================================================================
Total params: 1,060,138
Trainable params: 1,060,138
Non-trainable params: 0
_________________________________________________________________
建立优化模型
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['sparse_categorical_accuracy'])
拟合数据
history = model.fit(X_train,y_train,batch_size=10,epochs=10,verbose=1,validation_data=(X_test,y_test))
Epoch 1/10
5000/5000 [==============================] - 193s 39ms/step - loss: 1.3741 - sparse_categorical_accuracy: 0.5066 - val_loss: 1.1207 - val_sparse_categorical_accuracy: 0.5974
Epoch 2/10
5000/5000 [==============================] - 194s 39ms/step - loss: 1.0584 - sparse_categorical_accuracy: 0.6255 - val_loss: 0.9787 - val_sparse_categorical_accuracy: 0.6551
Epoch 3/10
5000/5000 [==============================] - 198s 40ms/step - loss: 0.9279 - sparse_categorical_accuracy: 0.6737 - val_loss: 0.9196 - val_sparse_categorical_accuracy: 0.6789
Epoch 4/10
5000/5000 [==============================] - 192s 38ms/step - loss: 0.8342 - sparse_categorical_accuracy: 0.7046 - val_loss: 0.9135 - val_sparse_categorical_accuracy: 0.6815
Epoch 5/10
5000/5000 [==============================] - 192s 38ms/step - loss: 0.7624 - sparse_categorical_accuracy: 0.7309 - val_loss: 0.8846 - val_sparse_categorical_accuracy: 0.6934
Epoch 6/10
5000/5000 [==============================] - 194s 39ms/step - loss: 0.6978 - sparse_categorical_accuracy: 0.7528 - val_loss: 0.9018 - val_sparse_categorical_accuracy: 0.6914
Epoch 7/10
5000/5000 [==============================] - 194s 39ms/step - loss: 0.6547 - sparse_categorical_accuracy: 0.7680 - val_loss: 0.8921 - val_sparse_categorical_accuracy: 0.6999
Epoch 8/10
5000/5000 [==============================] - 193s 39ms/step - loss: 0.6064 - sparse_categorical_accuracy: 0.7841 - val_loss: 0.8963 - val_sparse_categorical_accuracy: 0.6906
Epoch 9/10
5000/5000 [==============================] - 194s 39ms/step - loss: 0.5679 - sparse_categorical_accuracy: 0.7990 - val_loss: 0.8977 - val_sparse_categorical_accuracy: 0.6981
Epoch 10/10
5000/5000 [==============================] - 193s 39ms/step - loss: 0.5351 - sparse_categorical_accuracy: 0.8106 - val_loss: 0.9061 - val_sparse_categorical_accuracy: 0.6992
绘制曲线
epoch_range = range(1,11)
plt.plot(epoch_range, history.history['sparse_categorical_accuracy'])
plt.plot(epoch_range, history.history['val_sparse_categorical_accuracy'])
plt.title('Model Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend(['Train','Val'], loc ='upper left')
plt.show()
plt.plot(epoch_range, history.history['loss'])
plt.plot(epoch_range, history.history['val_loss'])
plt.title('Model Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend(['Train','Val'], loc ='upper left')
plt.show()
验证集在3次训练之后,loss并没有太大下降,表明,其没有学习
绘制confusion 矩阵
from mlxtend.plotting import plot_confusion_matrix
from sklearn.metrics import confusion_matrix
y_pred = model.predict_classes(X_test)
WARNING:tensorflow:From :1: Sequential.predict_classes (from tensorflow.python.keras.engine.sequential) is deprecated and will be removed after 2021-01-01.
Instructions for updating:
Please use instead:* `np.argmax(model.predict(x), axis=-1)`, if your model does multi-class classification (e.g. if it uses a `softmax` last-layer activation).* `(model.predict(x) > 0.5).astype("int32")`, if your model does binary classification (e.g. if it uses a `sigmoid` last-layer activation).
y_pred
array([5, 8, 8, ..., 5, 1, 7])
mat = confusion_matrix(y_test,y_pred)
plot_confusion_matrix(conf_mat= mat,figsize=(8,8),class_names=class_name,show_normed=True)
(