使用TensorFlow进行深度学习-第2部分
Hi All, this is a series of blogs that I intend to write about how to use TensorFlow 2.0 for deep learning.
大家好,我打算撰寫一系列博客,介紹如何使用TensorFlow 2.0進行深度學習。
In this blog, I will go over how to classify Fashion Mnist data set using TensorFlow 2.0. Last time in Part1 we used a simple Dense model to classify these images. In this blog Part2, we are going to use Conv2D layers for this task. Again the idea is still the same, using TensorFlow 2.0 inbuilt layers to show how easy it is to use Conv2D layers along with MaxPooling2D layer to show how the classification model can be improved from the prior dense model.
在此博客中,我將介紹如何使用TensorFlow 2.0對Fashion Mnist數據集進行分類。 上一次在Part1中,我們使用了一個簡單的Dense模型對這些圖像進行分類。 在此博客第2部分中,我們將使用Conv2D圖層來完成此任務。 再次,想法仍然是相同的,使用TensorFlow 2.0內置層來展示將Conv2D層與MaxPooling2D層一起使用是多么容易,以展示如何從先前的密集模型中改進分類模型。
Using the same template as we did before, here are five steps to run this classification.
使用與之前相同的模板,下面是執行此分類的五個步驟。
1. Dataset: Load the data set, do some feature engineering if needed.2. Build Model: Build a TensorFlow model with various layers.3. Compile Model: Here we compile the model, select the loss & Optimizer functions.4. Fit Model: Here we finally train the model using the training data and get some metrics.5. Evaluate Model: We check our model performance on the validation data.
1. 數據集:加載數據集,必要時進行一些功能設計。 構建模型:構建具有不同層的TensorFlow模型3。 編譯模型:這里我們編譯模型,選擇損失和優化器功能。4。 擬合模型:在這里,我們最終使用訓練數據來訓練模型并獲得一些指標。5。 評估模型:我們根據驗證數據檢查模型性能。
Dataset:This part remains the same as part1, as we are still using the same dataset. Fashion Minst: This is a dataset of 70,000 images. These are small grey scaled images with a standard size of 28x28 pixels. Here are a few examples.
數據集:該部分與part1相同,因為我們仍在使用相同的數據集。 Fashion Minst:這是一個包含70,000張圖像的數據集。 這些是標準尺寸為28x28像素的小型灰度圖像。 這里有一些例子。
First, let’s look at how to load data. This is going to use TensorFlow Datasets to do this.
首先,讓我們看一下如何加載數據。 這將使用TensorFlow數據集來做到這一點。
#Importsimport tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten,Conv2D,MaxPool2D,Dense#Load Dataset
mnist = tf.keras.datasets.fashion_mnist
(training_images, training_labels), (test_images, test_labels) =
fashion_mnist.load_data()#Reshape & Scaletraining_images=training_images.reshape(60000, 28, 28, 1)
training_images=training_images / 255.0
test_images = test_images.reshape(10000, 28, 28, 1)
test_images=test_images/255.0
As you can see this function loads all the 70k images and splits it into two parts 60k for training and 10k for testing.
如您所見,此功能將加載所有70k圖像,并將其分為兩部分:60k用于訓練,10k用于測試。
Build Model:
構建模型:
model= Sequential([Conv2D(16,(3,3),input_shape(28,28,1),
activation='relu'),
MaxPool2D(2,2),
Flatten(),
Dense(512,activation='relu'),
Dense(10,activation='softmax')
])
This is the updated model using Conv2D layer and MaxPooling2D layer. To get a little bit more into the details:
這是使用Conv2D層和MaxPooling2D層的更新模型。 要進一步了解細節:
# Here is the model summary:model.summary()Conv2D: This convolution layer can be thought of as matrix multiplication using the kernel size matrix in our example (3,3) so if our input size of the image is (28,28) our first Conv2D output would be a matrix of (28–3+1,28–3+1) so (26,26). We also have this process run for each filter so in our example of 16 filters the end dimensions are (26,26,16).
Conv2D:在我們的示例(3,3)中,可以使用內核大小矩陣將此卷積層視為矩陣乘法,因此,如果我們圖像的輸入大小為(28,28),則我們的第一個Conv2D輸出將是(28)的矩陣–3 + 1,28–3 + 1)如此(26,26)。 我們還為每個過濾器運行此過程,因此在我們的16個過濾器示例中,最終尺寸為(26,26,16)。
MaxPooling2D: After this Con2D layer, we use MaxPooling2D that dimensions are reduced to (13,13,16) when we use the unit size of (2,2) after the above layer.
MaxPooling2D:在此Con2D層之后,我們使用MaxPooling2D,當我們在上一層之后使用(2,2)的單位大小時,尺寸減小到(13,13,16)。
One more new parameter we used in the layers is the activation function ‘Relu’. There are also activation functions available from TensorFlow. Here is a link to all of them.
我們在圖層中使用的另一個新參數是激活函數“ Relu”。 TensorFlow還提供了激活功能。 這里 是所有這些的鏈接。
Compile Model:
編譯模型:
After we build the model we need to compile it. Here we need to select the loss functions and the optimizers. As you can see from the below code snippet this is very easy in TensorFlow. This is the same as we had in the last post.
構建模型后,我們需要對其進行編譯。 在這里,我們需要選擇損失函數和優化器。 從下面的代碼片段中可以看出,在TensorFlow中這非常容易。 這與上一篇文章中的相同。
model.compile(optimizer='Adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
Fit Model:Without further ado, here is the simple fit line used to train the model.
擬合模型:事不宜遲,這是用于訓練模型的簡單擬合線。
model.fit(train_rescale,train_label,epochs=5)Evaluate Model:Now the final test to see how the model performs on our test dataset.
評估模型:現在是最終測試,以查看模型在測試數據集上的表現。
model.evaluate(test_rescale,test_label)Here is a quick reminder of how the Dense only model performed.
這是有關僅密集模型的執行情況的快速提醒。
part1第一部分As you can see adding the 2 more lines in the model with Conv2D and MaxPooling2D improves the overall performance on the classification task from 87.72% to 91.12%.
如您所見,在使用Conv2D和MaxPooling2D的模型中添加另外2行,可以將分類任務的整體性能從87.72%提高到91.12%。
Good luck !!!
祝好運 !!!
翻譯自: https://medium.com/@sailaja.karra/deep-learning-using-tensorflow-part2-aa956b24d84d
總結
以上是生活随笔為你收集整理的使用TensorFlow进行深度学习-第2部分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: S12龙龟大乱斗怎么玩 出什么装备
- 下一篇: 360软件管家下载的软件存放位置在哪里