TF2.0模型創建
概述
這是TF2.0入門筆記【TF2.0模型創建、TF2.0模型訓練、TF2.0模型保存】中第一篇【TF2.0模型創建】,本篇將介紹模型的創建。
- tensorflow2.0模型創建方法我劃分爲三種方式:
- 1、通過tensorflow.keras.Sequential構造器創建模型
- 2、使用函數式API創建模型
- 3、通過繼承tensorflow.keras.Model類定義自己的模型
接下來將用代碼分別演示去構建一個簡單的模型
1、通過tensorflow.keras.Sequential構造器創建模型
第一種:通過tensorflow.keras.Sequential構造器創建模型
該方法就是不斷堆疊你需要的層,如該模型帶參數的一共有三層,一個卷積層,兩個全連接層(卷積之後通過 Flatten 層將其展平,從而接全連接層 )。
需要注意的是要在第一層指定輸入形狀 input_shape 。
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D, Input
from tensorflow.keras import Model, Sequential
model1 = Sequential()
model1.add(Conv2D(32, 3, activation='relu', padding='same', input_shape=(28, 28, 1)))
model1.add(Flatten())
model1.add(Dense(128, activation='relu'))
model1.add(Dense(10, activation='softmax'))
model1.summary()
運行輸出:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 28, 28, 32) 320
_________________________________________________________________
flatten (Flatten) (None, 25088) 0
_________________________________________________________________
dense (Dense) (None, 128) 3211392
_________________________________________________________________
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 3,213,002
Trainable params: 3,213,002
Non-trainable params: 0
_________________________________________________________________
2、使用函數式API創建模型
第二種:使用函數式API創建模型
這種方法比較靈活、自由,你可以輕易的創建多輸入、多輸出的模型。
用的 Input 層指定每個樣本的形狀,不管批次大小。最後通過 Model 類根據輸入和輸出來創建模型
input = Input((28,28,1))
x=Conv2D(32, 3, activation='relu', padding='same')(input)
x=Flatten()(x)
x=Dense(128, activation='relu')(x)
output=Dense(10, activation='softmax')(x)
model2=Model(input,output)
model2.summary()
運行輸出:
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 28, 28, 1)] 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 28, 28, 32) 320
_________________________________________________________________
flatten_1 (Flatten) (None, 25088) 0
_________________________________________________________________
dense_2 (Dense) (None, 128) 3211392
_________________________________________________________________
dense_3 (Dense) (None, 10) 1290
=================================================================
Total params: 3,213,002
Trainable params: 3,213,002
Non-trainable params: 0
_________________________________________________________________
3、通過繼承tensorflow.keras.Model類定義自己的模型
第三種:通過繼承tensorflow.keras.Model類定義自己的模型
在繼承類中, 我們需要重寫 __ init__()(構造函數)以及實現模型的前向傳遞 call(input)(模型調用)兩個方法, 你也可以根據需要添加自定義的方法
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = Conv2D(32, 3, activation='relu', padding='same')
self.flatten = Flatten()
self.d1 = Dense(128, activation='relu')
self.d2 = Dense(10, activation='softmax')
def call(self, x):
x = self.conv1(x)
x = self.flatten(x)
x = self.d1(x)
x = self.d2(x)
return x
model3 = MyModel()
input = Input((28,28,1))
_ = model3(input)
model3.summary()
運行輸出:
Model: "my_model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_2 (Conv2D) (None, 28, 28, 32) 320
_________________________________________________________________
flatten_2 (Flatten) (None, 25088) 0
_________________________________________________________________
dense_4 (Dense) (None, 128) 3211392
_________________________________________________________________
dense_5 (Dense) (None, 10) 1290
=================================================================
Total params: 3,213,002
Trainable params: 3,213,002
Non-trainable params: 0
_________________________________________________________________