TensorFlow定义模型的三种方法

Define Model

背景介绍

  Define Model(定义模型):是深度学习中的基础内容,想使用深度学习方法解决实际问题,首先就需要建立一个模型,今天以LeNet-5模型为例,给入门的小伙伴们提供TensorFlow2.0三种自定义模型的方法。

model

第一种方法

通过**keras.Sequential来创建一个序列模型,也是最简单的一种模型创建方法,缺点也很明显,灵活度很低,不能共享某一层,不能有分支,不能有多个输入输出。只适合于串联模型的创建(如VGG16),不适合并联模型的创建(如Inception-V3,ResNet50)**。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import tensorflow.keras as keras


if __name__ == '__main__':

model = keras.models.Sequential([keras.layers.Input(shape=(28, 28, 1), name='input'),
keras.layers.Conv2D(6, kernel_size=(5, 5), padding='same', activation='relu', name='conv1'),
keras.layers.MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='maxpool1'),
keras.layers.Conv2D(16, kernel_size=(5, 5), padding='valid', activation='relu', name='conv2'),
keras.layers.MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='maxpool2'),
keras.layers.Flatten(name='flatten'),
keras.layers.Dense(120, activation='relu', name='dense1'),
keras.layers.Dense(84, activation='relu', name='dense2'),
keras.layers.Dense(10, activation='softmax', name='dense3')], name='LeNet-5')

model.build(input_shape=(None, 28, 28, 1))
model.summary()

model1

第二种方法

通过函数API来创建一个模型,是一种灵活的方式,可以定义更加复杂的模型,灵活度很高,可以共享层,可以分支,且可以满足多个输入输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import tensorflow.keras as keras


def lenet(input_shape):

net = dict()

net['input'] = keras.layers.Input(shape=input_shape, name='input')
net['conv1'] = keras.layers.Conv2D(6, kernel_size=(5, 5), activation='relu', padding='same', name='conv1')(net['input'])
net['maxpool1'] = keras.layers.MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='maxpool1')(net['conv1'])
net['conv2'] = keras.layers.Conv2D(16, kernel_size=(5, 5), activation='relu', padding='valid', name='conv2')(net['maxpool1'])
net['maxpool2'] = keras.layers.MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='maxpool2')(net['conv2'])
net['flatten'] = keras.layers.Flatten(name='flatten')(net['maxpool2'])
net['dense1'] = keras.layers.Dense(120, activation='relu', name='dense1')(net['flatten'])
net['dense2'] = keras.layers.Dense(84, activation='relu', name='dense2')(net['dense1'])
net['dense3'] = keras.layers.Dense(10, activation='softmax', name='dense3')(net['dense2'])

model = keras.Model(net['input'], net['dense3'], name='LeNet-5')

return model


if __name__ == '__main__':

model = lenet((28, 28, 1))

model.build(input_shape=(None, 28, 28, 1))
model.summary()

model2

第三种方法

通过继承keras.Model来创建模型,是一种最灵活的方式,可以满足任何模型的定义,但是难度也更大,必须通过重载call函数自定义调用方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import tensorflow.keras as keras


class LeNet_5(keras.Model):
def __init__(self):
super(LeNet_5, self).__init__(name='LeNet-5')

self.conv1 = keras.layers.Conv2D(6, kernel_size=(5, 5), padding='same', activation='relu', name='conv1')
self.maxpool1 = keras.layers.MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='maxpool1')
self.conv2 = keras.layers.Conv2D(16, kernel_size=(5, 5), padding='valid', activation='relu', name='conv2')
self.maxpool2 = keras.layers.MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='maxpool2')
self.flatten = keras.layers.Flatten(name='flatten')
self.dense1 = keras.layers.Dense(120, activation='relu', name='dense1')
self.dense2 = keras.layers.Dense(84, activation='relu', name='dense2')
self.dense3 = keras.layers.Dense(10, activation='softmax', name='dense3')

def call(self, inputs, training=None, mask=None):

conv1 = self.conv1(inputs)
maxpool1 = self.maxpool1(conv1)
conv2 = self.conv2(maxpool1)
maxpool2 = self.maxpool2(conv2)
flatten = self.flatten(maxpool2)
dense1 = self.dense1(flatten)
dense2 = self.dense2(dense1)
output = self.dense3(dense2)

return output


if __name__ == '__main__':

model = LeNet_5()

model.build(input_shape=(None, 28, 28, 1))
model.summary()

model3

小结

  模型的定义没有固定的方式,小伙伴们可以根据平时的代码习惯进行选择,但是第二种方法和第三种方法必须要掌握一种,否则很难定义复杂的网络模型,希望小伙伴们可以多多练习。

-------------本文结束感谢您的阅读-------------
0%