我是靠谱客的博主 欣慰老虎,这篇文章主要介绍Keras关于trainable的实验,现在分享给大家,希望可以做个参考。

复制代码
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Time : 2021/1/28 17:20 # @Author : Jin Echo """ 结论: 1.对已经编译后的模型设置不可训练trainable,设置之后还需要再编译才会生效,否则会有一个warning询问是否要进行编译 2.对组合网络,例如C = G+D,D编译后设置False,再C编译,则训练C时D参数不变;训练D时参数改变(D未编译) 此外,也对拼接的网络进行了实验 """ from keras.models import Sequential, Model from keras.layers import Input, Dense, Activation, Dropout, Subtract import numpy as np x = Input(shape=(10, )) y = Dense(10)(x) G = Model(inputs=x, outputs=y) # 110参数 # print('G.summary:') # G.summary G.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) # print('G.summary:') # G.summary() x = Input(shape=(10, )) x_gen = Input(shape=(10, )) output = Subtract()([x, x_gen]) output = Dense(1, activation='sigmoid')(output) D = Model(inputs=[x, x_gen], outputs=output) # 11参数 D.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) # print('D.summary:') # D.summary() x = Input(shape=(10, )) x_gen = G(x) pre = D([x, x_gen]) C = Model(inputs=x, outputs=pre) # 121参数 C.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) # print('C.summary:') # C.summary() """ # 实验1:D.fit # 对D,尽管设置了False,但未编译,D仍然进行了训练 # (每轮的loss在下降,训练前后的参数改变都可证明) # C与G、D参数同步,注意此时D、C的sumary结果很怪异,还会输出一个warning D.trainable = False G.summary() D.summary() C.summary() print(G.get_weights()) print(D.get_weights()) print(C.get_weights()) print('--------------------------------------------------------------------------') D.fit(x=[np.random.uniform(size=(2, 10)), np.random.uniform(size=(2, 10))], y=np.ones(shape=(2, 1)), epochs=3, verbose=2) G.summary() D.summary() C.summary() print(G.get_weights()) print(D.get_weights()) print(C.get_weights()) """ """ # 实验2:D.fit # 对D,设置了False,并且D编译,D才不会进行训练) # (每轮的loss不变,训练前后的参数不变都可证明) # C与G、D参数同步,注意此时C的sumary结果很怪异,还会输出一个warning D.trainable = False D.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) G.summary() D.summary() C.summary() print(G.get_weights()) print(D.get_weights()) print(C.get_weights()) print('--------------------------------------------------------------------------') D.fit(x=[np.random.uniform(size=(2, 10)), np.random.uniform(size=(2, 10))], y=np.ones(shape=(2, 1)), epochs=3, verbose=2) G.summary() D.summary() C.summary() print(G.get_weights()) print(D.get_weights()) print(C.get_weights()) """ """ # 实验3:D.fit # 对D,设置了False,并且C编译D不编译,D会进行训练 # (每轮的loss变化,训练前后D的参数变化都可证明) # C与G、D参数同步,注意此时D的sumary结果很怪异,还会输出一个warning D.trainable = False C.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) G.summary() D.summary() C.summary() print(G.get_weights()) print(D.get_weights()) print(C.get_weights()) print('--------------------------------------------------------------------------') D.fit(x=[np.random.uniform(size=(2, 10)), np.random.uniform(size=(2, 10))], y=np.ones(shape=(2, 1)), epochs=3, verbose=2) G.summary() D.summary() C.summary() print(G.get_weights()) print(D.get_weights()) print(C.get_weights()) """ """ # 实验4:C.fit # a.对D设置False,D编译,C并不编译 # a.结果:loss减小,G参数变化、D参数变化,C=G+D,训练前后C的参数与G和D同步变换,C.summary怪异出现warning(相当于False未设置成功) # b.对D设置False,C编译,D并不编译 # b.结果:loss减小,G参数变化、D参数不变、C=G+D,训练前后C的参数与G和D同步变换,D.summary怪异出现warning # c.对D设置False,D、C先后编译 # c.结果:参数变换情况与b相同,差别仅是无怪异无waring # d.对D设置False,C、D先后编译 # d.结果:与C相同 D.trainable = False C.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) D.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) G.summary() D.summary() C.summary() print(G.get_weights()) print(D.get_weights()) print(C.get_weights()) print('-------------------------------------------------------------------------------------------------------') C.fit(x=np.random.uniform(size=(2, 10)), y=np.ones(shape=(2, 1)), epochs=3, verbose=2) G.summary() D.summary() C.summary() print(G.get_weights()) print(D.get_weights()) print(C.get_weights()) """

最后

以上就是欣慰老虎最近收集整理的关于Keras关于trainable的实验的全部内容,更多相关Keras关于trainable内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部