关于Keras里的Sequential(序列模型)转化为Model(函数模型)的问题

更新时间:2023-06-09 11:14:02 阅读: 评论:0

关于Keras ⾥的Sequential (序列模型)转化为Model (函数模型)的问题⽂章⽬录
前⾔
想在keras模型上加上注意⼒机制,于是把keras的序列模型转化为函数模型,结果发现参数维度不⼀致的问题,结果也变差了。跟踪问题后续发现是转为函数模型后,⽹络共享层出现了问题。
⼀、序列模型
该部分采⽤的是add添加⽹络层,由于存在多次重复调⽤相同⽹络层的情况,因此封装成⼀个⾃定义函数:
竹外桃花三两枝的意思整体代码,该模型存在多个输⼊(6个):  def create_ba_network (input_dim ):      q = Sequential ()      q .add (Conv2D (64, 5, activation ='relu', padding ='same', name ='conv1', input_shape =input_dim ))      q .add (Conv2D (128, 4, activation ='relu', padding ='same', name ='conv2'))      q .add (Conv2D (256, 4, activation ='relu', padding ='same', name ='conv3'))      q .add (Conv2D (64, 1, activation ='relu', padding ='same', name ='conv4'))      q .add (MaxPooling2D (2, 2, name ='pool1'))      q .add (Flatten (name ='fla1'))      q .add (Den (512, activation ='relu', name ='den1'))      q .add (Reshape ((1, 512), name ='reshape'))
1
2
3
4
5论语20篇
6世纪雷锋
7
8
9
10    model
⽹络模型:
哄男朋友睡觉的故事
⼆、改为函数模型
1.错误代码
第⼀次更改⽹络模型后,虽然运⾏未报错,但参数变多,模型性能也下降了,如下:  def create_ba_network (input_dim ):        x = Conv2D (64, 5, activation ='relu', padding ='same')(input
_dim )        x = Conv2D (128, 4, activation ='relu', padding ='same')(x )        x = Conv2D (256, 4, activation ='relu', padding ='same')(x )        x = Conv2D (64, 1, activation ='relu', padding ='same')(x )        x = MaxPooling2D (2, 2)(x )        x = Flatten ()(x )        x = Den (512, activation ='relu')(x )        x = Reshape ((1, 512))(x )        return  x    input_1 = Input (shape =img_size )    input_2 = Input (shape =img_size )    input_3 = Input (shape =img_size )    input_4 = Input (shape =img_size )    input_5 = Input (shape =img_size )    input_6 = Input (shape =img_size )    ba_network_1 = create_ba_network (input_1)    ba_network_2 = create_ba_network (input_2)    ba_network_3 = create_ba_network (input_3)    ba_network_4 = create_ba_network (input_4)    ba_network_5 = create_ba_network (input_5)    ba_network_6 = create_ba_network (input_6)    # print ('the shape of ba1:', ba_network (input_1).shape )  # (, 1, 512)    out_all = Concatenate (axis = 1)(  # 维度不变, 维度拼接,第⼀维度变为原来的6倍        [ba_network_1, ba_network_2, ba_network_3, ba_network_4, ba_network_5, ba_network_6])    print ('****', out_all .shape )  # (, 6, 512)    lstm_layer = LSTM (128, name = 'lstm')(out_all )    out_puts = Den (3, activation = 'softmax', name = 'out')(lstm_layer )    model = Model (inputs = [input_1, input_2, input_3, input_4, input_5, input_6], outputs = out_puts )  # 6个输⼊    model .summary ()
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
结果模型输出如下:
可以看到,模型的参数变为了原来的6倍多,改了很多次,后来发现,原来是因为序列模型中的ba_network =
create_ba_network(img_size)相当于已将模型实例化成了⼀个model,后续调⽤时只传⼊参数,⽽不更改模型结构。
⽽改为Model API后:
怀孕能吃葡萄干吗ba_network_1 = create_ba_network(input_1)
...
ba_network_6 = create_ba_network(input_6)
前⾯定义的 def create_ba_network( inputs),并未进⾏实例化,后续相当于创建了6次相关⽹络层,应该先实例化,应当改为以下部分:
总结
Keras⾥的函数模型,如果想要多个输⼊共享多个⽹络层, 还是得将各个层实例化,不能偷懒。。。#
建⽴⽹络共享层x1 = Conv2D (64, 5, activation = 'relu', padding = 'same', name = 'conv1')x2 = Conv2D (128, 4, activation = 'relu', padding = 'same', name = 'conv2')x3 = Conv2D (256, 4, activation = 'relu', padding = 'same', name = 'conv3')x4 = Conv2D (64, 1, activation = 'relu', padding = 'same', name = 'conv4')x5 = MaxPooling2D (2, 2)x6 = Flatten ()x7 = Den (512, activation = 'relu')x8 = Reshape ((1, 512))input_1 = Input (shape = img_size )  # 得到6个输⼊input_2 = Input (shape = img_size )input_3 = Input (shape = img_size )input_4 = Input (shape = img_size )input_5 = Input (shape = img_size )input_6 = Input (shape = img_size )ba_network_1 = x8(x7(x6(x5(x4(x3(x2(x1(input_1))))))))ba_network_2 = x8(x7(x6(x5(x4(x3(x2(x1(input_2))))))))ba_network_3 = x8(x7(x6(x5(x4(x3(x2(x1(input_3))))))))ba_network_4 = x8(x7(x6(x5(x4(x3(x2(x1(input_4))))))))ba_network_5 = x8(x7(x6(x5(x4(x3(x2(x1(input_5))))))))ba_network_6 = x8(x7(x6(x5(x4(x3(x2(x1(input_6))))))))      # 输⼊连接out_all = Concatenate (axis = 1)(                            # 维度不变, 维度拼接,第⼀维度变为原来的6倍      [ba_network_1, ba_network_2, ba_network_3, ba_network_4, ba_network_5, ba_network_6])# lstm  layer lstm_layer = LSTM (128, name = 'lstm3')(out_all )# den  layer out_layer = Den (3, activation = 'softmax', name = 'out')(lstm_layer )model = Model (inputs = [input_1, input_2, input_3, input_4, input_5, input_6], outputs = out_layer )  # 6个输⼊model .summary ()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

本文发布于:2023-06-09 11:14:02,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/910728.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:模型   函数   实例   维度   络层
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图