3 3 keras模型構建的三種方式

2021-10-12 02:22:55 字數 4273 閱讀 5999

model = sequential(

)#卷積層conv_1_1

model.add(cov2d(input_shape =(64

,64,3),filters =

32,

kernel_size =

3, activation =

'relu'

, kernel_initializer =

'he_uniform'

, name = 'conv_1_1)

#卷積層 conv_1_2

model.add(cov2d(filters =

32, kernel_size =3,

activation =

'relu'

, kernel_initializer =

'he_uniform'

, name = 'conv_1_2)

#池化層max_pool_1

model.add(maxpool2d(pool_size =

32, name = 'max_pool_1)

)#展平層

model.add(flatten(name =

'flatten'))

#全連線層

model.add(dense(unit =

6, activation =

'softmax'

, name =

'logit'))

#設定損失函式loss、優化器optimizer、評價指標metrics

model.

compile

(loss=

"categorical_crossentropy"

, optimizer = tf.keras.optimizers.sgd(learning_rate =

0.001),

metrics =

["accuracy"

])

或者:

model = sequential(

[ conv2d(input_shape =(64

,64,3

), filters =32,

kernel_size =

3, activation =

'relu'

, name =

'conv_1_1'),

conv2d(filters =

32, kernel_size =

3, activation =

'relu'

, name =

'conv_1_2'),

maxpool2d(poo_size =

2, anme =

'max_pool_1'),

flatten(name =

'flatten'),

dense(units =

6, activation =

"softmax"

, name =

'logit')]

)#設定損失函式loss、優化器optimizer、評價標準metrics

model.

compile

(loss=

"categorical_crossentropy"

, optimizer = tf.keras.optimizers.sgd(learning_rate =

0.001),

metrics =

["accuracy"

])

適用場合:對於順序結構的模型(沒有多個輸入輸出,也沒有分支),優先使用sequential方法構建。

缺點:不能建立以下模型結構

#輸入層input

input

=input

(shape =(64

,64,3

), name =

'input'

)#卷積層conv_1_2

x = conv2d(filters =

32, kernel_size =

3, activation =

'relu'

, name =

'conv_1_1')(

input

)#卷積層con_1_2

x = conv2d(filters =

32, kernel_size =

3, activation =

'relu'

, name = 'conv_1_2)

(x)#池化層max_pool_1

x = maxpool2d(pool_size =

2, name = 'max_pool_1)

(x)#展平層

x = flatten(name =

'flatten'

)(x)

#全連線層

output = dense(units =

6, activation =

"softmax"

, name =

'logit'

)(x)

model = model(inputs =

input

, outputs = output)

#設定損失函式loss、優化器optimizer、評價標準metrics

model.

compile

(loss=

"categorical_crossentropy"

, optimizer = tf.keras.optimizers.sgd(learning_rate =

0.001),

metrics =

["accuracy"

])

#定義乙個子類來搭建模型

class

convmodel

(model)

:def

__init__

(self)

:#父類初始化

super

(convmodel, self)

.__init__(

)#卷積層conv_1_1

self.conv_1_1 = conv2d(input_shape =(64

,64,3

),filters =

32, kernel_size =

3, activation =

'relu'

, name =

'con_1_1'

)#卷積層conv_1_2

self.conv_1_2 = conv2d(filters =

32, kernel_size =3,

activation =

'relu'

, name =

'conv_1_2'

)#池化層max_pool_1

self.max_pool_1 = maxpool2d(pool.size =

2, name =

'max_pool_1'

)#展平層flatten

self.dense = dense(units =

6, activation =

"softmax"

, name =

'logit'

)def

call

(selfm, x)

: x = self.conv_1_1(x)

x = self.conv_1_2(x)

x = self.max_pool_1(x)

x = self.conv_2_1(x)

x = self.conv_2_2(x)

x = self.max_pool_2(x)

x = self.flatten(x)

x = self.dense(x)

return x

#類例項化

model = convmodel(

)

構造tf.keras.model的子類來編寫模型,需要覆寫model類中的__init__方法和call方法。

__init__方法中定義我們要使用的層,這裡可以使用keras自帶的層;

call方法中實現模型的網路層。

適用場合:需要編寫自定義的模型,如在網路中使用自定義的層、自定義的損失函式、自定義的啟用函式等。

構建File的三種方式

1.file src new file path 其中path是路徑的字串 2.file src new file 父路徑,子路徑 這中間是用逗號隔開的,file有關於父路徑和子路徑的方法 3.file src new file new file 父路徑 子路徑 path的兩種表示方法 1 path...

執行緒的三種建立方

一,繼承thread 重寫run class programmer extends thread public static void main string args 二,繼承runnable 實現run class programmer implements runnable public st...

VMware ESXi Vlan的三種實現方式

在vmware esx esxi網路中vlan實現方式可以分成3種,分別是通過物理交換機,虛擬交換機 vswitch 和esxi中的虛擬機器 vm 來新增vlan標記,具體方式如下 1 est external switch tagging 通過將交換機的埠劃分到不同的vlan實現虛擬機器的vlan...