AlcalaDataset
commited on
Commit
•
717576e
1
Parent(s):
f211081
new
Browse files- LICENSE +353 -0
- README.md +110 -3
- pytorch/gcn2_cora_UAF.py +185 -0
- pytorch/mnist_UAF.py +167 -0
- pytorch/pna_UAF.py +248 -0
- tensorflow/CNNUAF_VGG16.py +454 -0
- tensorflow/CNNUAF_VGGBC.py +383 -0
- tensorflow/DDPG_UAF.py +437 -0
- tensorflow/mnist_UAF.py +140 -0
LICENSE
ADDED
@@ -0,0 +1,353 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
|
2 |
+
|
3 |
+
Creative Commons Corporation ("Creative Commons") is not a law firm and
|
4 |
+
does not provide legal services or legal advice. Distribution of
|
5 |
+
Creative Commons public licenses does not create a lawyer-client or
|
6 |
+
other relationship. Creative Commons makes its licenses and related
|
7 |
+
information available on an "as-is" basis. Creative Commons gives no
|
8 |
+
warranties regarding its licenses, any material licensed under their
|
9 |
+
terms and conditions, or any related information. Creative Commons
|
10 |
+
disclaims all liability for damages resulting from their use to the
|
11 |
+
fullest extent possible.
|
12 |
+
|
13 |
+
Using Creative Commons Public Licenses
|
14 |
+
|
15 |
+
Creative Commons public licenses provide a standard set of terms and
|
16 |
+
conditions that creators and other rights holders may use to share
|
17 |
+
original works of authorship and other material subject to copyright and
|
18 |
+
certain other rights specified in the public license below. The
|
19 |
+
following considerations are for informational purposes only, are not
|
20 |
+
exhaustive, and do not form part of our licenses.
|
21 |
+
|
22 |
+
Considerations for licensors: Our public licenses are intended for use
|
23 |
+
by those authorized to give the public permission to use material in
|
24 |
+
ways otherwise restricted by copyright and certain other rights. Our
|
25 |
+
licenses are irrevocable. Licensors should read and understand the terms
|
26 |
+
and conditions of the license they choose before applying it. Licensors
|
27 |
+
should also secure all rights necessary before applying our licenses so
|
28 |
+
that the public can reuse the material as expected. Licensors should
|
29 |
+
clearly mark any material not subject to the license. This includes
|
30 |
+
other CC-licensed material, or material used under an exception or
|
31 |
+
limitation to copyright. More considerations for licensors :
|
32 |
+
wiki.creativecommons.org/Considerations_for_licensors
|
33 |
+
|
34 |
+
Considerations for the public: By using one of our public licenses, a
|
35 |
+
licensor grants the public permission to use the licensed material under
|
36 |
+
specified terms and conditions. If the licensor's permission is not
|
37 |
+
necessary for any reason–for example, because of any applicable
|
38 |
+
exception or limitation to copyright–then that use is not regulated by
|
39 |
+
the license. Our licenses grant only permissions under copyright and
|
40 |
+
certain other rights that a licensor has authority to grant. Use of the
|
41 |
+
licensed material may still be restricted for other reasons, including
|
42 |
+
because others have copyright or other rights in the material. A
|
43 |
+
licensor may make special requests, such as asking that all changes be
|
44 |
+
marked or described. Although not required by our licenses, you are
|
45 |
+
encouraged to respect those requests where reasonable. More
|
46 |
+
considerations for the public :
|
47 |
+
wiki.creativecommons.org/Considerations_for_licensees
|
48 |
+
|
49 |
+
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
|
50 |
+
Public License
|
51 |
+
|
52 |
+
By exercising the Licensed Rights (defined below), You accept and agree
|
53 |
+
to be bound by the terms and conditions of this Creative Commons
|
54 |
+
Attribution-NonCommercial-ShareAlike 4.0 International Public License
|
55 |
+
("Public License"). To the extent this Public License may be interpreted
|
56 |
+
as a contract, You are granted the Licensed Rights in consideration of
|
57 |
+
Your acceptance of these terms and conditions, and the Licensor grants
|
58 |
+
You such rights in consideration of benefits the Licensor receives from
|
59 |
+
making the Licensed Material available under these terms and conditions.
|
60 |
+
|
61 |
+
Section 1 – Definitions.
|
62 |
+
|
63 |
+
- a. Adapted Material means material subject to Copyright and Similar
|
64 |
+
Rights that is derived from or based upon the Licensed Material and
|
65 |
+
in which the Licensed Material is translated, altered, arranged,
|
66 |
+
transformed, or otherwise modified in a manner requiring permission
|
67 |
+
under the Copyright and Similar Rights held by the Licensor. For
|
68 |
+
purposes of this Public License, where the Licensed Material is a
|
69 |
+
musical work, performance, or sound recording, Adapted Material is
|
70 |
+
always produced where the Licensed Material is synched in timed
|
71 |
+
relation with a moving image.
|
72 |
+
- b. Adapter's License means the license You apply to Your Copyright
|
73 |
+
and Similar Rights in Your contributions to Adapted Material in
|
74 |
+
accordance with the terms and conditions of this Public License.
|
75 |
+
- c. BY-NC-SA Compatible License means a license listed at
|
76 |
+
creativecommons.org/compatiblelicenses, approved by Creative Commons
|
77 |
+
as essentially the equivalent of this Public License.
|
78 |
+
- d. Copyright and Similar Rights means copyright and/or similar
|
79 |
+
rights closely related to copyright including, without limitation,
|
80 |
+
performance, broadcast, sound recording, and Sui Generis Database
|
81 |
+
Rights, without regard to how the rights are labeled or categorized.
|
82 |
+
For purposes of this Public License, the rights specified in Section
|
83 |
+
2(b)(1)-(2) are not Copyright and Similar Rights.
|
84 |
+
- e. Effective Technological Measures means those measures that, in
|
85 |
+
the absence of proper authority, may not be circumvented under laws
|
86 |
+
fulfilling obligations under Article 11 of the WIPO Copyright Treaty
|
87 |
+
adopted on December 20, 1996, and/or similar international
|
88 |
+
agreements.
|
89 |
+
- f. Exceptions and Limitations means fair use, fair dealing, and/or
|
90 |
+
any other exception or limitation to Copyright and Similar Rights
|
91 |
+
that applies to Your use of the Licensed Material.
|
92 |
+
- g. License Elements means the license attributes listed in the name
|
93 |
+
of a Creative Commons Public License. The License Elements of this
|
94 |
+
Public License are Attribution, NonCommercial, and ShareAlike.
|
95 |
+
- h. Licensed Material means the artistic or literary work, database,
|
96 |
+
or other material to which the Licensor applied this Public License.
|
97 |
+
- i. Licensed Rights means the rights granted to You subject to the
|
98 |
+
terms and conditions of this Public License, which are limited to
|
99 |
+
all Copyright and Similar Rights that apply to Your use of the
|
100 |
+
Licensed Material and that the Licensor has authority to license.
|
101 |
+
- j. Licensor means the individual(s) or entity(ies) granting rights
|
102 |
+
under this Public License.
|
103 |
+
- k. NonCommercial means not primarily intended for or directed
|
104 |
+
towards commercial advantage or monetary compensation. For purposes
|
105 |
+
of this Public License, the exchange of the Licensed Material for
|
106 |
+
other material subject to Copyright and Similar Rights by digital
|
107 |
+
file-sharing or similar means is NonCommercial provided there is no
|
108 |
+
payment of monetary compensation in connection with the exchange.
|
109 |
+
- l. Share means to provide material to the public by any means or
|
110 |
+
process that requires permission under the Licensed Rights, such as
|
111 |
+
reproduction, public display, public performance, distribution,
|
112 |
+
dissemination, communication, or importation, and to make material
|
113 |
+
available to the public including in ways that members of the public
|
114 |
+
may access the material from a place and at a time individually
|
115 |
+
chosen by them.
|
116 |
+
- m. Sui Generis Database Rights means rights other than copyright
|
117 |
+
resulting from Directive 96/9/EC of the European Parliament and of
|
118 |
+
the Council of 11 March 1996 on the legal protection of databases,
|
119 |
+
as amended and/or succeeded, as well as other essentially equivalent
|
120 |
+
rights anywhere in the world.
|
121 |
+
- n. You means the individual or entity exercising the Licensed Rights
|
122 |
+
under this Public License. Your has a corresponding meaning.
|
123 |
+
|
124 |
+
Section 2 – Scope.
|
125 |
+
|
126 |
+
- a. License grant.
|
127 |
+
- 1. Subject to the terms and conditions of this Public License,
|
128 |
+
the Licensor hereby grants You a worldwide, royalty-free,
|
129 |
+
non-sublicensable, non-exclusive, irrevocable license to
|
130 |
+
exercise the Licensed Rights in the Licensed Material to:
|
131 |
+
- A. reproduce and Share the Licensed Material, in whole or in
|
132 |
+
part, for NonCommercial purposes only; and
|
133 |
+
- B. produce, reproduce, and Share Adapted Material for
|
134 |
+
NonCommercial purposes only.
|
135 |
+
- 2. Exceptions and Limitations. For the avoidance of doubt, where
|
136 |
+
Exceptions and Limitations apply to Your use, this Public
|
137 |
+
License does not apply, and You do not need to comply with its
|
138 |
+
terms and conditions.
|
139 |
+
- 3. Term. The term of this Public License is specified in Section
|
140 |
+
6(a).
|
141 |
+
- 4. Media and formats; technical modifications allowed. The
|
142 |
+
Licensor authorizes You to exercise the Licensed Rights in all
|
143 |
+
media and formats whether now known or hereafter created, and to
|
144 |
+
make technical modifications necessary to do so. The Licensor
|
145 |
+
waives and/or agrees not to assert any right or authority to
|
146 |
+
forbid You from making technical modifications necessary to
|
147 |
+
exercise the Licensed Rights, including technical modifications
|
148 |
+
necessary to circumvent Effective Technological Measures. For
|
149 |
+
purposes of this Public License, simply making modifications
|
150 |
+
authorized by this Section 2(a)(4) never produces Adapted
|
151 |
+
Material.
|
152 |
+
- 5. Downstream recipients.
|
153 |
+
- A. Offer from the Licensor – Licensed Material. Every
|
154 |
+
recipient of the Licensed Material automatically receives an
|
155 |
+
offer from the Licensor to exercise the Licensed Rights
|
156 |
+
under the terms and conditions of this Public License.
|
157 |
+
- B. Additional offer from the Licensor – Adapted Material.
|
158 |
+
Every recipient of Adapted Material from You automatically
|
159 |
+
receives an offer from the Licensor to exercise the Licensed
|
160 |
+
Rights in the Adapted Material under the conditions of the
|
161 |
+
Adapter's License You apply.
|
162 |
+
- C. No downstream restrictions. You may not offer or impose
|
163 |
+
any additional or different terms or conditions on, or apply
|
164 |
+
any Effective Technological Measures to, the Licensed
|
165 |
+
Material if doing so restricts exercise of the Licensed
|
166 |
+
Rights by any recipient of the Licensed Material.
|
167 |
+
- 6. No endorsement. Nothing in this Public License constitutes or
|
168 |
+
may be construed as permission to assert or imply that You are,
|
169 |
+
or that Your use of the Licensed Material is, connected with, or
|
170 |
+
sponsored, endorsed, or granted official status by, the Licensor
|
171 |
+
or others designated to receive attribution as provided in
|
172 |
+
Section 3(a)(1)(A)(i).
|
173 |
+
- b. Other rights.
|
174 |
+
- 1. Moral rights, such as the right of integrity, are not
|
175 |
+
licensed under this Public License, nor are publicity, privacy,
|
176 |
+
and/or other similar personality rights; however, to the extent
|
177 |
+
possible, the Licensor waives and/or agrees not to assert any
|
178 |
+
such rights held by the Licensor to the limited extent necessary
|
179 |
+
to allow You to exercise the Licensed Rights, but not otherwise.
|
180 |
+
- 2. Patent and trademark rights are not licensed under this
|
181 |
+
Public License.
|
182 |
+
- 3. To the extent possible, the Licensor waives any right to
|
183 |
+
collect royalties from You for the exercise of the Licensed
|
184 |
+
Rights, whether directly or through a collecting society under
|
185 |
+
any voluntary or waivable statutory or compulsory licensing
|
186 |
+
scheme. In all other cases the Licensor expressly reserves any
|
187 |
+
right to collect such royalties, including when the Licensed
|
188 |
+
Material is used other than for NonCommercial purposes.
|
189 |
+
|
190 |
+
Section 3 – License Conditions.
|
191 |
+
|
192 |
+
Your exercise of the Licensed Rights is expressly made subject to the
|
193 |
+
following conditions.
|
194 |
+
|
195 |
+
- a. Attribution.
|
196 |
+
- 1. If You Share the Licensed Material (including in modified
|
197 |
+
form), You must:
|
198 |
+
- A. retain the following if it is supplied by the Licensor
|
199 |
+
with the Licensed Material:
|
200 |
+
- i. identification of the creator(s) of the Licensed
|
201 |
+
Material and any others designated to receive
|
202 |
+
attribution, in any reasonable manner requested by the
|
203 |
+
Licensor (including by pseudonym if designated);
|
204 |
+
- ii. a copyright notice;
|
205 |
+
- iii. a notice that refers to this Public License;
|
206 |
+
- iv. a notice that refers to the disclaimer of
|
207 |
+
warranties;
|
208 |
+
- v. a URI or hyperlink to the Licensed Material to the
|
209 |
+
extent reasonably practicable;
|
210 |
+
|
211 |
+
- B. indicate if You modified the Licensed Material and retain
|
212 |
+
an indication of any previous modifications; and
|
213 |
+
- C. indicate the Licensed Material is licensed under this
|
214 |
+
Public License, and include the text of, or the URI or
|
215 |
+
hyperlink to, this Public License.
|
216 |
+
- 2. You may satisfy the conditions in Section 3(a)(1) in any
|
217 |
+
reasonable manner based on the medium, means, and context in
|
218 |
+
which You Share the Licensed Material. For example, it may be
|
219 |
+
reasonable to satisfy the conditions by providing a URI or
|
220 |
+
hyperlink to a resource that includes the required information.
|
221 |
+
- 3. If requested by the Licensor, You must remove any of the
|
222 |
+
information required by Section 3(a)(1)(A) to the extent
|
223 |
+
reasonably practicable.
|
224 |
+
- b. ShareAlike.In addition to the conditions in Section 3(a), if You
|
225 |
+
Share Adapted Material You produce, the following conditions also
|
226 |
+
apply.
|
227 |
+
- 1. The Adapter's License You apply must be a Creative Commons
|
228 |
+
license with the same License Elements, this version or later,
|
229 |
+
or a BY-NC-SA Compatible License.
|
230 |
+
- 2. You must include the text of, or the URI or hyperlink to, the
|
231 |
+
Adapter's License You apply. You may satisfy this condition in
|
232 |
+
any reasonable manner based on the medium, means, and context in
|
233 |
+
which You Share Adapted Material.
|
234 |
+
- 3. You may not offer or impose any additional or different terms
|
235 |
+
or conditions on, or apply any Effective Technological Measures
|
236 |
+
to, Adapted Material that restrict exercise of the rights
|
237 |
+
granted under the Adapter's License You apply.
|
238 |
+
|
239 |
+
Section 4 – Sui Generis Database Rights.
|
240 |
+
|
241 |
+
Where the Licensed Rights include Sui Generis Database Rights that apply
|
242 |
+
to Your use of the Licensed Material:
|
243 |
+
|
244 |
+
- a. for the avoidance of doubt, Section 2(a)(1) grants You the right
|
245 |
+
to extract, reuse, reproduce, and Share all or a substantial portion
|
246 |
+
of the contents of the database for NonCommercial purposes only;
|
247 |
+
- b. if You include all or a substantial portion of the database
|
248 |
+
contents in a database in which You have Sui Generis Database
|
249 |
+
Rights, then the database in which You have Sui Generis Database
|
250 |
+
Rights (but not its individual contents) is Adapted Material,
|
251 |
+
including for purposes of Section 3(b); and
|
252 |
+
- c. You must comply with the conditions in Section 3(a) if You Share
|
253 |
+
all or a substantial portion of the contents of the database.
|
254 |
+
For the avoidance of doubt, this Section 4 supplements and does not
|
255 |
+
replace Your obligations under this Public License where the
|
256 |
+
Licensed Rights include other Copyright and Similar Rights.
|
257 |
+
|
258 |
+
Section 5 – Disclaimer of Warranties and Limitation of Liability.
|
259 |
+
|
260 |
+
- a. Unless otherwise separately undertaken by the Licensor, to the
|
261 |
+
extent possible, the Licensor offers the Licensed Material as-is and
|
262 |
+
as-available, and makes no representations or warranties of any kind
|
263 |
+
concerning the Licensed Material, whether express, implied,
|
264 |
+
statutory, or other. This includes, without limitation, warranties
|
265 |
+
of title, merchantability, fitness for a particular purpose,
|
266 |
+
non-infringement, absence of latent or other defects, accuracy, or
|
267 |
+
the presence or absence of errors, whether or not known or
|
268 |
+
discoverable. Where disclaimers of warranties are not allowed in
|
269 |
+
full or in part, this disclaimer may not apply to You.
|
270 |
+
- b. To the extent possible, in no event will the Licensor be liable
|
271 |
+
to You on any legal theory (including, without limitation,
|
272 |
+
negligence) or otherwise for any direct, special, indirect,
|
273 |
+
incidental, consequential, punitive, exemplary, or other losses,
|
274 |
+
costs, expenses, or damages arising out of this Public License or
|
275 |
+
use of the Licensed Material, even if the Licensor has been advised
|
276 |
+
of the possibility of such losses, costs, expenses, or damages.
|
277 |
+
Where a limitation of liability is not allowed in full or in part,
|
278 |
+
this limitation may not apply to You.
|
279 |
+
- c. The disclaimer of warranties and limitation of liability provided
|
280 |
+
above shall be interpreted in a manner that, to the extent possible,
|
281 |
+
most closely approximates an absolute disclaimer and waiver of all
|
282 |
+
liability.
|
283 |
+
|
284 |
+
Section 6 – Term and Termination.
|
285 |
+
|
286 |
+
- a. This Public License applies for the term of the Copyright and
|
287 |
+
Similar Rights licensed here. However, if You fail to comply with
|
288 |
+
this Public License, then Your rights under this Public License
|
289 |
+
terminate automatically.
|
290 |
+
- b. Where Your right to use the Licensed Material has terminated
|
291 |
+
under Section 6(a), it reinstates:
|
292 |
+
|
293 |
+
- 1. automatically as of the date the violation is cured, provided
|
294 |
+
it is cured within 30 days of Your discovery of the violation;
|
295 |
+
or
|
296 |
+
- 2. upon express reinstatement by the Licensor.
|
297 |
+
|
298 |
+
For the avoidance of doubt, this Section 6(b) does not affect any
|
299 |
+
right the Licensor may have to seek remedies for Your violations of
|
300 |
+
this Public License.
|
301 |
+
|
302 |
+
- c. For the avoidance of doubt, the Licensor may also offer the
|
303 |
+
Licensed Material under separate terms or conditions or stop
|
304 |
+
distributing the Licensed Material at any time; however, doing so
|
305 |
+
will not terminate this Public License.
|
306 |
+
- d. Sections 1, 5, 6, 7, and 8 survive termination of this Public
|
307 |
+
License.
|
308 |
+
|
309 |
+
Section 7 – Other Terms and Conditions.
|
310 |
+
|
311 |
+
- a. The Licensor shall not be bound by any additional or different
|
312 |
+
terms or conditions communicated by You unless expressly agreed.
|
313 |
+
- b. Any arrangements, understandings, or agreements regarding the
|
314 |
+
Licensed Material not stated herein are separate from and
|
315 |
+
independent of the terms and conditions of this Public License.
|
316 |
+
|
317 |
+
Section 8 – Interpretation.
|
318 |
+
|
319 |
+
- a. For the avoidance of doubt, this Public License does not, and
|
320 |
+
shall not be interpreted to, reduce, limit, restrict, or impose
|
321 |
+
conditions on any use of the Licensed Material that could lawfully
|
322 |
+
be made without permission under this Public License.
|
323 |
+
- b. To the extent possible, if any provision of this Public License
|
324 |
+
is deemed unenforceable, it shall be automatically reformed to the
|
325 |
+
minimum extent necessary to make it enforceable. If the provision
|
326 |
+
cannot be reformed, it shall be severed from this Public License
|
327 |
+
without affecting the enforceability of the remaining terms and
|
328 |
+
conditions.
|
329 |
+
- c. No term or condition of this Public License will be waived and no
|
330 |
+
failure to comply consented to unless expressly agreed to by the
|
331 |
+
Licensor.
|
332 |
+
- d. Nothing in this Public License constitutes or may be interpreted
|
333 |
+
as a limitation upon, or waiver of, any privileges and immunities
|
334 |
+
that apply to the Licensor or You, including from the legal
|
335 |
+
processes of any jurisdiction or authority.
|
336 |
+
|
337 |
+
Creative Commons is not a party to its public licenses. Notwithstanding,
|
338 |
+
Creative Commons may elect to apply one of its public licenses to
|
339 |
+
material it publishes and in those instances will be considered the
|
340 |
+
"Licensor." The text of the Creative Commons public licenses is
|
341 |
+
dedicated to the public domain under the CC0 Public Domain Dedication.
|
342 |
+
Except for the limited purpose of indicating that material is shared
|
343 |
+
under a Creative Commons public license or as otherwise permitted by the
|
344 |
+
Creative Commons policies published at creativecommons.org/policies,
|
345 |
+
Creative Commons does not authorize the use of the trademark "Creative
|
346 |
+
Commons" or any other trademark or logo of Creative Commons without its
|
347 |
+
prior written consent including, without limitation, in connection with
|
348 |
+
any unauthorized modifications to any of its public licenses or any
|
349 |
+
other arrangements, understandings, or agreements concerning use of
|
350 |
+
licensed material. For the avoidance of doubt, this paragraph does not
|
351 |
+
form part of the public licenses.
|
352 |
+
|
353 |
+
Creative Commons may be contacted at creativecommons.org.
|
README.md
CHANGED
@@ -1,3 +1,110 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Universal Activation Function
|
2 |
+
|
3 |
+
Tensorflow and Pytorch source code for the paper
|
4 |
+
|
5 |
+
[Yuen, Brosnan, Minh Tu Hoang, Xiaodai Dong, and Tao Lu. "Universal activation function for machine learning." Scientific reports 11, no. 1 (2021): 1-11.](https://www.nature.com/articles/s41598-021-96723-8)
|
6 |
+
|
7 |
+
|
8 |
+
# Getting the code
|
9 |
+
|
10 |
+
Requires [Docker](https://docs.docker.com/get-docker/)
|
11 |
+
|
12 |
+
|
13 |
+
Use git to pull this repo
|
14 |
+
```
|
15 |
+
git clone https://github.com/SensorOrgNet/Universal_Activation_Function.git
|
16 |
+
```
|
17 |
+
|
18 |
+
|
19 |
+
# Running the Tensorflow 2 version
|
20 |
+
|
21 |
+
|
22 |
+
Install CUDA 11.2 container
|
23 |
+
```
|
24 |
+
docker run --name UAF --gpus all -v /home/username/UAF/:/workspace -w /workspace -it nvcr.io/nvidia/cuda:11.2.0-cudnn8-devel-ubuntu20.04 bash
|
25 |
+
```
|
26 |
+
|
27 |
+
|
28 |
+
Install python
|
29 |
+
```
|
30 |
+
apt update
|
31 |
+
apt install python3-pip
|
32 |
+
```
|
33 |
+
|
34 |
+
|
35 |
+
Install pytorch and pytorch geometric
|
36 |
+
```
|
37 |
+
pip3 install tensorflow==2.7.0
|
38 |
+
```
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
Run the MLP with UAF for MNIST dataset
|
44 |
+
```
|
45 |
+
cd Universal_Activation_Function/tensorflow/
|
46 |
+
python3 ./mnist_UAF.py
|
47 |
+
```
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
# Running the Pytorch version
|
55 |
+
|
56 |
+
|
57 |
+
Install CUDA 11.3 container
|
58 |
+
```
|
59 |
+
docker run --name UAF --gpus all -v /home/username/UAF/:/workspace -w /workspace -it nvcr.io/nvidia/cuda:11.3.0-cudnn8-devel-ubuntu20.04 bash
|
60 |
+
```
|
61 |
+
|
62 |
+
Install python
|
63 |
+
```
|
64 |
+
apt update
|
65 |
+
apt install python3-pip
|
66 |
+
```
|
67 |
+
|
68 |
+
Install pytorch and pytorch geometric
|
69 |
+
```
|
70 |
+
pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio==0.10.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
|
71 |
+
pip3 install torch-scatter -f https://data.pyg.org/whl/torch-1.10.0+cu113.html
|
72 |
+
pip3 install torch-sparse -f https://data.pyg.org/whl/torch-1.10.0+cu113.html
|
73 |
+
pip3 install torch-cluster -f https://data.pyg.org/whl/torch-1.10.0+cu113.html
|
74 |
+
pip3 install torch-spline-conv -f https://data.pyg.org/whl/torch-1.10.0+cu113.html
|
75 |
+
pip3 install torch-geometric
|
76 |
+
```
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
Run the CNN with UAF for MNIST dataset
|
82 |
+
```
|
83 |
+
cd Universal_Activation_Function/pytorch/
|
84 |
+
python3 ./mnist_UAF.py
|
85 |
+
```
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
Run the GCN2 with UAF for CORA dataset. The fold number is represented by the number at the end
|
94 |
+
```
|
95 |
+
cd Universal_Activation_Function/pytorch/
|
96 |
+
python3 ./gcn2_cora_UAF.py 0
|
97 |
+
```
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
Run the PNA with UAF for ZNC dataset. The fold number is represented by the number at the end
|
104 |
+
```
|
105 |
+
cd Universal_Activation_Function/pytorch/
|
106 |
+
python3 ./pna_UAF.py 0
|
107 |
+
```
|
108 |
+
|
109 |
+
|
110 |
+
|
pytorch/gcn2_cora_UAF.py
ADDED
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os.path as osp
|
2 |
+
import sys
|
3 |
+
import time
|
4 |
+
|
5 |
+
|
6 |
+
import torch
|
7 |
+
from torch.nn import Linear
|
8 |
+
import torch.nn.functional as F
|
9 |
+
from torch_geometric.datasets import Planetoid
|
10 |
+
import torch_geometric.transforms as T
|
11 |
+
from torch_geometric.nn import GCN2Conv
|
12 |
+
from torch_geometric.nn.conv.gcn_conv import gcn_norm
|
13 |
+
import numpy as np
|
14 |
+
|
15 |
+
train_pred = []
|
16 |
+
train_act = []
|
17 |
+
|
18 |
+
test_pred = []
|
19 |
+
test_act = []
|
20 |
+
|
21 |
+
fold = int(sys.argv[1])
|
22 |
+
|
23 |
+
st = time.process_time()
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
dataset = 'Cora'
|
28 |
+
path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', dataset)
|
29 |
+
transform = T.Compose([T.NormalizeFeatures(), T.ToSparseTensor()])
|
30 |
+
dataset = Planetoid(path, dataset, transform=transform)
|
31 |
+
data = dataset[0]
|
32 |
+
data.adj_t = gcn_norm(data.adj_t) # Pre-process GCN normalization.
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
ims = []
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
class Net(torch.nn.Module):
|
44 |
+
def __init__(self, hidden_channels, num_layers, alpha, theta,
|
45 |
+
shared_weights=True, dropout=0.0):
|
46 |
+
super(Net, self).__init__()
|
47 |
+
|
48 |
+
self.lins = torch.nn.ModuleList()
|
49 |
+
self.lins.append(Linear(dataset.num_features, hidden_channels))
|
50 |
+
self.lins.append(Linear(hidden_channels, dataset.num_classes))
|
51 |
+
|
52 |
+
self.convs = torch.nn.ModuleList()
|
53 |
+
for layer in range(num_layers):
|
54 |
+
self.convs.append(
|
55 |
+
GCN2Conv(hidden_channels, alpha, theta, layer + 1,
|
56 |
+
shared_weights, normalize=False))
|
57 |
+
|
58 |
+
self.dropout = dropout
|
59 |
+
|
60 |
+
self.A = torch.nn.Parameter(torch.tensor(1.1, requires_grad=True))
|
61 |
+
self.B = torch.nn.Parameter(torch.tensor(-0.01, requires_grad=True))
|
62 |
+
self.C = torch.nn.Parameter(torch.tensor(1e-9, requires_grad=True))
|
63 |
+
self.D = torch.nn.Parameter(torch.tensor(-0.9, requires_grad=True))
|
64 |
+
self.E = torch.nn.Parameter(torch.tensor(0.00001, requires_grad=True))
|
65 |
+
|
66 |
+
|
67 |
+
def UAF(self, input):
|
68 |
+
ims.append(np.array([self.A.cpu().detach().item(),self.B.cpu().detach().item(),self.C.cpu().detach().item(),self.D.cpu().detach().item(),self.E.cpu().detach().item()]))
|
69 |
+
P1 = (self.A*(input+self.B)) + torch.clamp((self.C * torch.square(input)),-100.0,100.0)
|
70 |
+
P2 = (self.D*(input-self.B))
|
71 |
+
|
72 |
+
P3 = torch.nn.ReLU()(P1) + torch.log1p(torch.exp(-torch.abs(P1)))
|
73 |
+
P4 = torch.nn.ReLU()(P2) + torch.log1p(torch.exp(-torch.abs(P2)))
|
74 |
+
return P3 - P4 + self.E
|
75 |
+
|
76 |
+
def forward(self, x, adj_t):
|
77 |
+
x = F.dropout(x, self.dropout, training=self.training)
|
78 |
+
x = x_0 = self.UAF(self.lins[0](x))
|
79 |
+
|
80 |
+
for conv in self.convs:
|
81 |
+
x = F.dropout(x, self.dropout, training=self.training)
|
82 |
+
x = conv(x, x_0, adj_t)
|
83 |
+
x = self.UAF(x)
|
84 |
+
|
85 |
+
x = F.dropout(x, self.dropout, training=self.training)
|
86 |
+
x = self.lins[1](x)
|
87 |
+
|
88 |
+
return x.log_softmax(dim=-1)
|
89 |
+
|
90 |
+
|
91 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
92 |
+
model = Net(hidden_channels=64, num_layers=64, alpha=0.1, theta=0.5,
|
93 |
+
shared_weights=True, dropout=0.6).to(device)
|
94 |
+
data = data.to(device)
|
95 |
+
optimizer = torch.optim.Adam([
|
96 |
+
dict(params=model.convs.parameters(), weight_decay=0.01),
|
97 |
+
dict(params=model.lins.parameters(), weight_decay=5e-4)
|
98 |
+
], lr=0.01)
|
99 |
+
|
100 |
+
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min',
|
101 |
+
factor=0.7, patience=50,
|
102 |
+
min_lr=0.00001)
|
103 |
+
|
104 |
+
|
105 |
+
optimizer2 = torch.optim.Adam([
|
106 |
+
dict(params=model.A),
|
107 |
+
dict(params=model.B),
|
108 |
+
dict(params=model.C, weight_decay=1e5),
|
109 |
+
dict(params=model.D),
|
110 |
+
dict(params=model.E)
|
111 |
+
], lr=0.005)
|
112 |
+
scheduler2 = torch.optim.lr_scheduler.StepLR(optimizer2, step_size=240, gamma=1e-10)
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
def train():
|
117 |
+
model.train()
|
118 |
+
optimizer2.zero_grad()
|
119 |
+
optimizer.zero_grad()
|
120 |
+
out = model(data.x, data.adj_t)
|
121 |
+
loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
|
122 |
+
|
123 |
+
train_pred_temp = out[data.train_mask].cpu().detach().numpy()
|
124 |
+
train_act_temp = data.y[data.train_mask].cpu().detach().numpy()
|
125 |
+
|
126 |
+
train_pred.append(train_pred_temp)
|
127 |
+
train_act.append(train_act_temp)
|
128 |
+
|
129 |
+
|
130 |
+
loss.backward()
|
131 |
+
optimizer.step()
|
132 |
+
optimizer2.step()
|
133 |
+
return float(loss)
|
134 |
+
|
135 |
+
|
136 |
+
@torch.no_grad()
|
137 |
+
def test():
|
138 |
+
model.eval()
|
139 |
+
pred, accs = model(data.x, data.adj_t).argmax(dim=-1), []
|
140 |
+
|
141 |
+
test_pred_temp = pred[data.test_mask].cpu().detach().numpy()
|
142 |
+
test_act_temp = data.y[data.test_mask].cpu().detach().numpy()
|
143 |
+
|
144 |
+
test_pred.append(test_pred_temp)
|
145 |
+
test_act.append(test_act_temp)
|
146 |
+
|
147 |
+
for _, mask in data('train_mask', 'val_mask', 'test_mask'):
|
148 |
+
accs.append(int((pred[mask] == data.y[mask]).sum()) / int(mask.sum()))
|
149 |
+
return accs
|
150 |
+
|
151 |
+
|
152 |
+
best_val_acc = test_acc = 0
|
153 |
+
for epoch in range(1, 1001):
|
154 |
+
loss = train()
|
155 |
+
train_acc, val_acc, tmp_test_acc = test()
|
156 |
+
if val_acc > best_val_acc:
|
157 |
+
best_val_acc = val_acc
|
158 |
+
test_acc = tmp_test_acc
|
159 |
+
lr = scheduler.optimizer.param_groups[0]['lr']
|
160 |
+
if (epoch == 241):
|
161 |
+
scheduler.optimizer.param_groups[0]['lr'] = 0.05
|
162 |
+
scheduler.step(-val_acc)
|
163 |
+
scheduler2.step()
|
164 |
+
|
165 |
+
print(f'Epoch: {epoch:04d}, Loss: {loss:.4f} Train: {train_acc:.4f}, '
|
166 |
+
f'lr: {lr:.7f}, Test: {tmp_test_acc:.4f}, '
|
167 |
+
f'Final Test: {test_acc:.4f}')
|
168 |
+
|
169 |
+
|
170 |
+
|
171 |
+
elapsed_time = time.process_time() - st
|
172 |
+
|
173 |
+
np.save("time_" + str(fold), np.array([elapsed_time]))
|
174 |
+
|
175 |
+
|
176 |
+
np.save("train_pred_" + str(fold), train_pred)
|
177 |
+
np.save("train_act_" + str(fold), train_act)
|
178 |
+
|
179 |
+
|
180 |
+
np.save("test_pred_" + str(fold), test_pred)
|
181 |
+
np.save("test_act_" + str(fold), test_act)
|
182 |
+
|
183 |
+
|
184 |
+
ims = np.asarray(ims)
|
185 |
+
np.save("ims_" + str(fold),ims)
|
pytorch/mnist_UAF.py
ADDED
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import print_function
|
2 |
+
import argparse
|
3 |
+
import torch
|
4 |
+
import torch.nn as nn
|
5 |
+
import torch.nn.functional as F
|
6 |
+
import torch.optim as optim
|
7 |
+
from torchvision import datasets, transforms
|
8 |
+
from torch.optim.lr_scheduler import StepLR
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
class UAF(nn.Module):
|
13 |
+
def __init__(self):
|
14 |
+
super().__init__()
|
15 |
+
self.A = nn.Parameter(torch.tensor(30.0, requires_grad=True))
|
16 |
+
self.B = nn.Parameter(torch.tensor(0.0000012, requires_grad=True))
|
17 |
+
self.C = nn.Parameter(torch.tensor(0.0000001, requires_grad=True))
|
18 |
+
self.D = nn.Parameter(torch.tensor(29.0, requires_grad=True))
|
19 |
+
self.E = nn.Parameter(torch.tensor(0.00000102, requires_grad=True))
|
20 |
+
|
21 |
+
def forward(self, input):
|
22 |
+
P1 = (self.A*(input+self.B)) + (self.C * torch.square(input))
|
23 |
+
P2 = (self.D*(input-self.B))
|
24 |
+
|
25 |
+
P3 = nn.ReLU()(P1) + torch.log1p(torch.exp(-torch.abs(P1)))
|
26 |
+
P4 = nn.ReLU()(P2) + torch.log1p(torch.exp(-torch.abs(P2)))
|
27 |
+
return P3 - P4 + self.E
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
class Net(nn.Module):
|
36 |
+
def __init__(self):
|
37 |
+
super(Net, self).__init__()
|
38 |
+
self.conv1 = nn.Conv2d(1, 32, 3, 1)
|
39 |
+
self.conv2 = nn.Conv2d(32, 64, 3, 1)
|
40 |
+
self.dropout1 = nn.Dropout(0.25)
|
41 |
+
self.dropout2 = nn.Dropout(0.5)
|
42 |
+
self.fc1 = nn.Linear(9216, 128)
|
43 |
+
self.fc2 = nn.Linear(128, 10)
|
44 |
+
self.act0 = UAF()
|
45 |
+
self.act1 = UAF()
|
46 |
+
self.act2 = UAF()
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
def forward(self, x):
|
52 |
+
x = self.conv1(x)
|
53 |
+
x = self.act0(x)
|
54 |
+
x = self.conv2(x)
|
55 |
+
x = self.act1(x)
|
56 |
+
x = F.max_pool2d(x, 2)
|
57 |
+
x = self.dropout1(x)
|
58 |
+
x = torch.flatten(x, 1)
|
59 |
+
x = self.fc1(x)
|
60 |
+
x = self.act2(x)
|
61 |
+
x = self.dropout2(x)
|
62 |
+
x = self.fc2(x)
|
63 |
+
output = F.log_softmax(x, dim=1)
|
64 |
+
return output
|
65 |
+
|
66 |
+
|
67 |
+
def train(args, model, device, train_loader, optimizer, epoch):
|
68 |
+
model.train()
|
69 |
+
for batch_idx, (data, target) in enumerate(train_loader):
|
70 |
+
data, target = data.to(device), target.to(device)
|
71 |
+
optimizer.zero_grad()
|
72 |
+
output = model(data)
|
73 |
+
loss = F.nll_loss(output, target)
|
74 |
+
loss.backward()
|
75 |
+
optimizer.step()
|
76 |
+
if batch_idx % args.log_interval == 0:
|
77 |
+
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
|
78 |
+
epoch, batch_idx * len(data), len(train_loader.dataset),
|
79 |
+
100. * batch_idx / len(train_loader), loss.item()))
|
80 |
+
if args.dry_run:
|
81 |
+
break
|
82 |
+
|
83 |
+
|
84 |
+
def test(model, device, test_loader):
|
85 |
+
model.eval()
|
86 |
+
test_loss = 0
|
87 |
+
correct = 0
|
88 |
+
with torch.no_grad():
|
89 |
+
for data, target in test_loader:
|
90 |
+
data, target = data.to(device), target.to(device)
|
91 |
+
output = model(data)
|
92 |
+
test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss
|
93 |
+
pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability
|
94 |
+
correct += pred.eq(target.view_as(pred)).sum().item()
|
95 |
+
|
96 |
+
test_loss /= len(test_loader.dataset)
|
97 |
+
|
98 |
+
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
|
99 |
+
test_loss, correct, len(test_loader.dataset),
|
100 |
+
100. * correct / len(test_loader.dataset)))
|
101 |
+
|
102 |
+
|
103 |
+
def main():
|
104 |
+
# Training settings
|
105 |
+
parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
|
106 |
+
parser.add_argument('--batch-size', type=int, default=64, metavar='N',
|
107 |
+
help='input batch size for training (default: 64)')
|
108 |
+
parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N',
|
109 |
+
help='input batch size for testing (default: 1000)')
|
110 |
+
parser.add_argument('--epochs', type=int, default=14, metavar='N',
|
111 |
+
help='number of epochs to train (default: 14)')
|
112 |
+
parser.add_argument('--lr', type=float, default=0.89, metavar='LR',
|
113 |
+
help='learning rate (default: 0.9)')
|
114 |
+
parser.add_argument('--gamma', type=float, default=0.7, metavar='M',
|
115 |
+
help='Learning rate step gamma (default: 0.7)')
|
116 |
+
parser.add_argument('--no-cuda', action='store_true', default=False,
|
117 |
+
help='disables CUDA training')
|
118 |
+
parser.add_argument('--dry-run', action='store_true', default=False,
|
119 |
+
help='quickly check a single pass')
|
120 |
+
parser.add_argument('--seed', type=int, default=1, metavar='S',
|
121 |
+
help='random seed (default: 1)')
|
122 |
+
parser.add_argument('--log-interval', type=int, default=10, metavar='N',
|
123 |
+
help='how many batches to wait before logging training status')
|
124 |
+
parser.add_argument('--save-model', action='store_true', default=False,
|
125 |
+
help='For Saving the current Model')
|
126 |
+
args = parser.parse_args()
|
127 |
+
use_cuda = not args.no_cuda and torch.cuda.is_available()
|
128 |
+
|
129 |
+
torch.manual_seed(args.seed)
|
130 |
+
|
131 |
+
device = torch.device("cuda" if use_cuda else "cpu")
|
132 |
+
|
133 |
+
train_kwargs = {'batch_size': args.batch_size}
|
134 |
+
test_kwargs = {'batch_size': args.test_batch_size}
|
135 |
+
if use_cuda:
|
136 |
+
cuda_kwargs = {'num_workers': 1,
|
137 |
+
'pin_memory': True,
|
138 |
+
'shuffle': True}
|
139 |
+
train_kwargs.update(cuda_kwargs)
|
140 |
+
test_kwargs.update(cuda_kwargs)
|
141 |
+
|
142 |
+
transform=transforms.Compose([
|
143 |
+
transforms.ToTensor(),
|
144 |
+
transforms.Normalize((0.1307,), (0.3081,))
|
145 |
+
])
|
146 |
+
dataset1 = datasets.MNIST('../data', train=True, download=True,
|
147 |
+
transform=transform)
|
148 |
+
dataset2 = datasets.MNIST('../data', train=False,
|
149 |
+
transform=transform)
|
150 |
+
train_loader = torch.utils.data.DataLoader(dataset1,**train_kwargs)
|
151 |
+
test_loader = torch.utils.data.DataLoader(dataset2, **test_kwargs)
|
152 |
+
|
153 |
+
model = Net().to(device)
|
154 |
+
optimizer = optim.Adadelta(model.parameters(), lr=args.lr)
|
155 |
+
|
156 |
+
scheduler = StepLR(optimizer, step_size=1, gamma=args.gamma)
|
157 |
+
for epoch in range(1, args.epochs + 1):
|
158 |
+
train(args, model, device, train_loader, optimizer, epoch)
|
159 |
+
test(model, device, test_loader)
|
160 |
+
scheduler.step()
|
161 |
+
|
162 |
+
if args.save_model:
|
163 |
+
torch.save(model.state_dict(), "mnist_cnn.pt")
|
164 |
+
|
165 |
+
|
166 |
+
if __name__ == '__main__':
|
167 |
+
main()
|
pytorch/pna_UAF.py
ADDED
@@ -0,0 +1,248 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os.path as osp
|
2 |
+
|
3 |
+
import torch
|
4 |
+
import torch.nn.functional as F
|
5 |
+
from torch.nn import ModuleList, Embedding
|
6 |
+
from torch.nn import Sequential, ReLU, Linear
|
7 |
+
from torch.optim.lr_scheduler import ReduceLROnPlateau
|
8 |
+
from torch_geometric.utils import degree
|
9 |
+
from torch_geometric.datasets import ZINC
|
10 |
+
from torch_geometric.data import DataLoader
|
11 |
+
from torch_geometric.nn import PNAConv, BatchNorm, global_add_pool
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
import sys
|
19 |
+
import time
|
20 |
+
|
21 |
+
|
22 |
+
import numpy as np
|
23 |
+
|
24 |
+
train_pred = []
|
25 |
+
train_act = []
|
26 |
+
|
27 |
+
test_pred = []
|
28 |
+
test_act = []
|
29 |
+
|
30 |
+
fold = int(sys.argv[1])
|
31 |
+
|
32 |
+
st = time.process_time()
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', 'ZINC')
|
41 |
+
train_dataset = ZINC(path, subset=True, split='train')
|
42 |
+
val_dataset = ZINC(path, subset=True, split='val')
|
43 |
+
test_dataset = ZINC(path, subset=True, split='test')
|
44 |
+
|
45 |
+
train_loader = DataLoader(train_dataset, batch_size=128, shuffle=True)
|
46 |
+
val_loader = DataLoader(val_dataset, batch_size=128)
|
47 |
+
test_loader = DataLoader(test_dataset, batch_size=128)
|
48 |
+
|
49 |
+
# Compute in-degree histogram over training data.
|
50 |
+
deg = torch.zeros(5, dtype=torch.long)
|
51 |
+
for data in train_dataset:
|
52 |
+
d = degree(data.edge_index[1], num_nodes=data.num_nodes, dtype=torch.long)
|
53 |
+
deg += torch.bincount(d, minlength=deg.numel())
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
class UAF(torch.nn.Module):
|
59 |
+
def __init__(self):
|
60 |
+
super().__init__()
|
61 |
+
self.A = torch.nn.Parameter(torch.tensor(1.1, requires_grad=True))
|
62 |
+
self.B = torch.nn.Parameter(torch.tensor(-0.01, requires_grad=True))
|
63 |
+
self.C = torch.nn.Parameter(torch.tensor(0.00001, requires_grad=True))
|
64 |
+
self.D = torch.nn.Parameter(torch.tensor(-0.9, requires_grad=True))
|
65 |
+
self.E = torch.nn.Parameter(torch.tensor(0.00001, requires_grad=True))
|
66 |
+
|
67 |
+
self.Softplus = torch.nn.Softplus()
|
68 |
+
def forward(self, input):
|
69 |
+
|
70 |
+
return self.Softplus((self.A*(input+self.B)) + (self.C * torch.square(input))) - self.Softplus((self.D*(input-self.B))) + self.E
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
class Net(torch.nn.Module):
|
75 |
+
def __init__(self):
|
76 |
+
super(Net, self).__init__()
|
77 |
+
|
78 |
+
self.node_emb = Embedding(21, 75)
|
79 |
+
self.edge_emb = Embedding(4, 50)
|
80 |
+
|
81 |
+
aggregators = ['mean', 'min', 'max', 'std']
|
82 |
+
scalers = ['identity', 'amplification', 'attenuation']
|
83 |
+
|
84 |
+
self.convs = ModuleList()
|
85 |
+
self.batch_norms = ModuleList()
|
86 |
+
for _ in range(4):
|
87 |
+
conv = PNAConv(in_channels=75, out_channels=75,
|
88 |
+
aggregators=aggregators, scalers=scalers, deg=deg,
|
89 |
+
edge_dim=50, towers=5, pre_layers=1, post_layers=1,
|
90 |
+
divide_input=False)
|
91 |
+
self.convs.append(conv)
|
92 |
+
self.batch_norms.append(BatchNorm(75))
|
93 |
+
|
94 |
+
self.func = UAF()
|
95 |
+
|
96 |
+
|
97 |
+
self.mlp = Sequential(Linear(75, 50), self.func, Linear(50, 25), self.func,
|
98 |
+
Linear(25, 1))
|
99 |
+
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
def forward(self, x, edge_index, edge_attr, batch):
|
104 |
+
x = self.node_emb(x.squeeze())
|
105 |
+
edge_attr = self.edge_emb(edge_attr)
|
106 |
+
|
107 |
+
for conv, batch_norm in zip(self.convs, self.batch_norms):
|
108 |
+
x = self.func(batch_norm(conv(x, edge_index, edge_attr)))
|
109 |
+
|
110 |
+
x = global_add_pool(x, batch)
|
111 |
+
return self.mlp(x)
|
112 |
+
|
113 |
+
|
114 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
115 |
+
model = Net().to(device)
|
116 |
+
#optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
|
117 |
+
optimizer = torch.optim.Adam([
|
118 |
+
dict(params=model.convs.parameters()),
|
119 |
+
dict(params=model.batch_norms.parameters()),
|
120 |
+
dict(params=model.mlp.parameters())
|
121 |
+
], lr=0.001)
|
122 |
+
scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=20,
|
123 |
+
min_lr=0.00001)
|
124 |
+
|
125 |
+
|
126 |
+
|
127 |
+
optimizer2 = torch.optim.Adam([
|
128 |
+
dict(params=model.func.parameters())
|
129 |
+
], lr=0.001)
|
130 |
+
scheduler2 = torch.optim.lr_scheduler.StepLR(optimizer2, step_size=300, gamma=1e-10)
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
|
137 |
+
def train(epoch):
|
138 |
+
model.train()
|
139 |
+
|
140 |
+
|
141 |
+
train_pred_temp = []
|
142 |
+
train_act_temp = []
|
143 |
+
first = True
|
144 |
+
|
145 |
+
|
146 |
+
|
147 |
+
total_loss = 0
|
148 |
+
for data in train_loader:
|
149 |
+
data = data.to(device)
|
150 |
+
optimizer.zero_grad()
|
151 |
+
optimizer2.zero_grad()
|
152 |
+
out = model(data.x, data.edge_index, data.edge_attr, data.batch)
|
153 |
+
loss = (out.squeeze() - data.y).abs().mean()
|
154 |
+
|
155 |
+
|
156 |
+
pred = out.squeeze()
|
157 |
+
if (first):
|
158 |
+
train_pred_temp = pred.cpu().detach().numpy()
|
159 |
+
train_act_temp = data.y.cpu().detach().numpy()
|
160 |
+
first = False
|
161 |
+
else:
|
162 |
+
train_pred_temp = np.append(train_pred_temp, pred.cpu().detach().numpy())
|
163 |
+
train_act_temp = np.append(train_act_temp, data.y.cpu().detach().numpy())
|
164 |
+
|
165 |
+
|
166 |
+
|
167 |
+
loss.backward()
|
168 |
+
total_loss += loss.item() * data.num_graphs
|
169 |
+
optimizer.step()
|
170 |
+
optimizer2.step()
|
171 |
+
|
172 |
+
|
173 |
+
train_pred.append(train_pred_temp)
|
174 |
+
train_act.append(train_act_temp)
|
175 |
+
return total_loss / len(train_loader.dataset)
|
176 |
+
|
177 |
+
|
178 |
+
@torch.no_grad()
|
179 |
+
def test(loader):
|
180 |
+
model.eval()
|
181 |
+
|
182 |
+
|
183 |
+
test_pred_temp = []
|
184 |
+
test_act_temp = []
|
185 |
+
first = True
|
186 |
+
|
187 |
+
total_error = 0
|
188 |
+
for data in loader:
|
189 |
+
data = data.to(device)
|
190 |
+
out = model(data.x, data.edge_index, data.edge_attr, data.batch)
|
191 |
+
total_error += (out.squeeze() - data.y).abs().sum().item()
|
192 |
+
|
193 |
+
|
194 |
+
pred = out.squeeze()
|
195 |
+
if (first):
|
196 |
+
test_pred_temp = pred.cpu().detach().numpy()
|
197 |
+
test_act_temp = data.y.cpu().detach().numpy()
|
198 |
+
first = False
|
199 |
+
else:
|
200 |
+
test_pred_temp = np.append(test_pred_temp, pred.cpu().detach().numpy())
|
201 |
+
test_act_temp = np.append(test_act_temp, data.y.cpu().detach().numpy())
|
202 |
+
|
203 |
+
|
204 |
+
test_pred.append(test_pred_temp)
|
205 |
+
test_act.append(test_act_temp)
|
206 |
+
return total_error / len(loader.dataset)
|
207 |
+
|
208 |
+
|
209 |
+
|
210 |
+
@torch.no_grad()
|
211 |
+
def test_val(loader):
|
212 |
+
model.eval()
|
213 |
+
|
214 |
+
total_error = 0
|
215 |
+
for data in loader:
|
216 |
+
data = data.to(device)
|
217 |
+
out = model(data.x, data.edge_index, data.edge_attr, data.batch)
|
218 |
+
total_error += (out.squeeze() - data.y).abs().sum().item()
|
219 |
+
return total_error / len(loader.dataset)
|
220 |
+
|
221 |
+
|
222 |
+
|
223 |
+
|
224 |
+
for epoch in range(1, 601):
|
225 |
+
loss = train(epoch)
|
226 |
+
val_mae = test_val(val_loader)
|
227 |
+
test_mae = test(test_loader)
|
228 |
+
if (epoch == 302):
|
229 |
+
scheduler.optimizer.param_groups[0]['lr'] = 0.001
|
230 |
+
scheduler.step(val_mae)
|
231 |
+
scheduler2.step()
|
232 |
+
print(f'Epoch: {epoch:02d}, Loss: {loss:.4f}, Val: {val_mae:.4f}, '
|
233 |
+
f'Test: {test_mae:.4f}')
|
234 |
+
|
235 |
+
|
236 |
+
|
237 |
+
|
238 |
+
elapsed_time = time.process_time() - st
|
239 |
+
|
240 |
+
np.save("time_" + str(fold), np.array([elapsed_time]))
|
241 |
+
|
242 |
+
|
243 |
+
np.save("train_pred_" + str(fold), train_pred)
|
244 |
+
np.save("train_act_" + str(fold), train_act)
|
245 |
+
|
246 |
+
|
247 |
+
np.save("test_pred_" + str(fold), test_pred)
|
248 |
+
np.save("test_act_" + str(fold), test_act)
|
tensorflow/CNNUAF_VGG16.py
ADDED
@@ -0,0 +1,454 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import numpy as np
|
3 |
+
import sys
|
4 |
+
from sklearn.metrics import precision_recall_fscore_support
|
5 |
+
import time
|
6 |
+
|
7 |
+
|
8 |
+
def softplus(x):
|
9 |
+
return np.maximum(0, x) + np.log1p(np.exp(-np.abs(x)))
|
10 |
+
|
11 |
+
|
12 |
+
def main():
|
13 |
+
|
14 |
+
neural_network_model_file = "./mlp.ckpt"
|
15 |
+
|
16 |
+
|
17 |
+
img_size = 32
|
18 |
+
num_channels = 3
|
19 |
+
output_size = 10
|
20 |
+
neuron_size = 4096
|
21 |
+
init_val = 1E-2
|
22 |
+
lr = 0.00001
|
23 |
+
|
24 |
+
size = int(sys.argv[1])
|
25 |
+
|
26 |
+
fold = int(sys.argv[2])
|
27 |
+
|
28 |
+
|
29 |
+
print ("Setting network up")
|
30 |
+
with tf.device("/device:GPU:0"):
|
31 |
+
is_training = tf.placeholder(tf.bool)
|
32 |
+
x = tf.placeholder("float32", [None, img_size, img_size, num_channels] , name="x")
|
33 |
+
y = tf.placeholder("float32", [None, output_size] )
|
34 |
+
learning_rate = tf.placeholder("float32", None)
|
35 |
+
|
36 |
+
conv1_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 3, 64], mean=0, stddev=init_val))
|
37 |
+
conv2_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 64, 64], mean=0, stddev=init_val))
|
38 |
+
|
39 |
+
conv3_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 64, 128], mean=0, stddev=init_val))
|
40 |
+
conv4_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 128, 128], mean=0, stddev=init_val))
|
41 |
+
|
42 |
+
conv5_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 128, 256], mean=0, stddev=init_val))
|
43 |
+
conv6_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 256, 256], mean=0, stddev=init_val))
|
44 |
+
conv7_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 256, 256], mean=0, stddev=init_val))
|
45 |
+
|
46 |
+
conv8_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 256, 512], mean=0, stddev=init_val))
|
47 |
+
conv9_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 512, 512], mean=0, stddev=init_val))
|
48 |
+
conv10_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 512, 512], mean=0, stddev=init_val))
|
49 |
+
|
50 |
+
|
51 |
+
conv11_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 512, 512], mean=0, stddev=init_val))
|
52 |
+
conv12_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 512, 512], mean=0, stddev=init_val))
|
53 |
+
conv13_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 512, 512], mean=0, stddev=init_val))
|
54 |
+
|
55 |
+
|
56 |
+
cnnbias = {
|
57 |
+
'b1': tf.Variable(tf.random_normal([ 64 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
58 |
+
'b2': tf.Variable(tf.random_normal([ 64 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
59 |
+
|
60 |
+
'b3': tf.Variable(tf.random_normal([ 128 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
61 |
+
'b4': tf.Variable(tf.random_normal([ 128 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
62 |
+
|
63 |
+
'b5': tf.Variable(tf.random_normal([ 256 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
64 |
+
'b6': tf.Variable(tf.random_normal([ 256 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
65 |
+
'b7': tf.Variable(tf.random_normal([ 256 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
66 |
+
|
67 |
+
|
68 |
+
'b8': tf.Variable(tf.random_normal([ 512 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
69 |
+
'b9': tf.Variable(tf.random_normal([ 512 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
70 |
+
'b10': tf.Variable(tf.random_normal([ 512 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
71 |
+
|
72 |
+
|
73 |
+
'b11': tf.Variable(tf.random_normal([ 512 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
74 |
+
'b12': tf.Variable(tf.random_normal([ 512 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
75 |
+
'b13': tf.Variable(tf.random_normal([ 512 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32)
|
76 |
+
|
77 |
+
}
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
A = tf.Variable(tf.random_normal([1], mean=1.1, stddev=0.0, dtype=tf.float32), dtype=tf.float32)
|
82 |
+
B = tf.Variable(tf.random_normal([1], mean=-0.01, stddev=0.0, dtype=tf.float32), dtype=tf.float32)
|
83 |
+
C = tf.constant(0.0, dtype=tf.float32)
|
84 |
+
D = tf.Variable(tf.random_normal([1], mean=-0.9, stddev=0.0, dtype=tf.float32), dtype=tf.float32)
|
85 |
+
E = tf.Variable(tf.random_normal([1], mean=0.0, stddev=0.0, dtype=tf.float32), dtype=tf.float32)
|
86 |
+
|
87 |
+
three = tf.constant(3.0, dtype=tf.float32)
|
88 |
+
two = tf.constant(2.0, dtype=tf.float32)
|
89 |
+
one = tf.constant(1.0, dtype=tf.float32)
|
90 |
+
eps = tf.constant(1E-8, dtype=tf.float32)
|
91 |
+
|
92 |
+
def UAF(vv,UAF_A,UAF_B,UAF_C,UAF_D,UAF_E):
|
93 |
+
P1 = tf.multiply(UAF_A,(vv+UAF_B)) +tf.multiply(UAF_C,tf.pow(vv,two))
|
94 |
+
P2 = tf.multiply(UAF_D,(vv-UAF_B))
|
95 |
+
|
96 |
+
P3 = tf.nn.relu(P1) + tf.math.log1p(tf.exp(-tf.abs(P1)))
|
97 |
+
P4 = tf.nn.relu(P2) + tf.math.log1p(tf.exp(-tf.abs(P2)))
|
98 |
+
return P3 - P4 + UAF_E
|
99 |
+
|
100 |
+
|
101 |
+
conv1 = tf.nn.conv2d(x, conv1_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b1']
|
102 |
+
conv1_bn = tf.layers.batch_normalization(conv1,training = is_training)
|
103 |
+
conv1_act = UAF(conv1_bn,A,B,C,D,E)
|
104 |
+
|
105 |
+
conv2 = tf.nn.conv2d(conv1_act, conv2_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b2']
|
106 |
+
conv2_bn = tf.layers.batch_normalization(conv2,training = is_training)
|
107 |
+
conv2_act = UAF(conv2_bn,A,B,C,D,E)
|
108 |
+
conv2_pool = tf.nn.max_pool(conv2_act, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
|
109 |
+
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
conv3 = tf.nn.conv2d(conv2_pool, conv3_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b3']
|
114 |
+
conv3_bn = tf.layers.batch_normalization(conv3,training = is_training)
|
115 |
+
conv3_act = UAF(conv3_bn,A,B,C,D,E)
|
116 |
+
|
117 |
+
conv4 = tf.nn.conv2d(conv3_act, conv4_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b4']
|
118 |
+
conv4_bn = tf.layers.batch_normalization(conv4,training = is_training)
|
119 |
+
conv4_act = UAF(conv4_bn,A,B,C,D,E)
|
120 |
+
conv4_pool = tf.nn.max_pool(conv4_act, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
|
121 |
+
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
conv5 = tf.nn.conv2d(conv4_pool, conv5_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b5']
|
127 |
+
conv5_bn = tf.layers.batch_normalization(conv5,training = is_training)
|
128 |
+
conv5_act = UAF(conv5_bn,A,B,C,D,E)
|
129 |
+
|
130 |
+
conv6 = tf.nn.conv2d(conv5_act, conv6_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b6']
|
131 |
+
conv6_bn = tf.layers.batch_normalization(conv6,training = is_training)
|
132 |
+
conv6_act = UAF(conv6_bn,A,B,C,D,E)
|
133 |
+
|
134 |
+
conv7 = tf.nn.conv2d(conv6_act, conv7_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b7']
|
135 |
+
conv7_bn = tf.layers.batch_normalization(conv7,training = is_training)
|
136 |
+
conv7_act = UAF(conv7_bn,A,B,C,D,E)
|
137 |
+
|
138 |
+
conv7_pool = tf.nn.max_pool(conv7_act, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
|
139 |
+
|
140 |
+
|
141 |
+
|
142 |
+
|
143 |
+
|
144 |
+
conv8 = tf.nn.conv2d(conv7_pool, conv8_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b8']
|
145 |
+
conv8_bn = tf.layers.batch_normalization(conv8,training = is_training)
|
146 |
+
conv8_act = UAF(conv8_bn,A,B,C,D,E)
|
147 |
+
|
148 |
+
conv9 = tf.nn.conv2d(conv8_act, conv9_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b9']
|
149 |
+
conv9_bn = tf.layers.batch_normalization(conv9,training = is_training)
|
150 |
+
conv9_act = UAF(conv9_bn,A,B,C,D,E)
|
151 |
+
|
152 |
+
conv10 = tf.nn.conv2d(conv9_act, conv10_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b10']
|
153 |
+
conv10_bn = tf.layers.batch_normalization(conv10,training = is_training)
|
154 |
+
conv10_act = UAF(conv10_bn,A,B,C,D,E)
|
155 |
+
|
156 |
+
conv10_pool = tf.nn.max_pool(conv10_act, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
|
157 |
+
|
158 |
+
|
159 |
+
|
160 |
+
|
161 |
+
conv11 = tf.nn.conv2d(conv10_pool, conv11_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b11']
|
162 |
+
conv11_bn = tf.layers.batch_normalization(conv11,training = is_training)
|
163 |
+
conv11_act = UAF(conv11_bn,A,B,C,D,E)
|
164 |
+
|
165 |
+
conv12 = tf.nn.conv2d(conv11_act, conv12_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b12']
|
166 |
+
conv12_bn = tf.layers.batch_normalization(conv12,training = is_training)
|
167 |
+
conv12_act = UAF(conv12_bn,A,B,C,D,E)
|
168 |
+
|
169 |
+
conv13 = tf.nn.conv2d(conv12_act, conv13_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b13']
|
170 |
+
conv13_bn = tf.layers.batch_normalization(conv13,training = is_training)
|
171 |
+
conv13_act = UAF(conv13_bn,A,B,C,D,E)
|
172 |
+
|
173 |
+
|
174 |
+
# 9
|
175 |
+
flat = tf.contrib.layers.flatten(conv13_act)
|
176 |
+
|
177 |
+
|
178 |
+
weights = {
|
179 |
+
'w1': tf.Variable(tf.random_normal([ 2048, neuron_size ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
180 |
+
'w2': tf.Variable(tf.random_normal([ neuron_size, neuron_size ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
181 |
+
'w3': tf.Variable(tf.random_normal([ neuron_size, output_size ], stddev=init_val, dtype=tf.float32), dtype=tf.float32)
|
182 |
+
}
|
183 |
+
|
184 |
+
bias = {
|
185 |
+
'b1': tf.Variable(tf.random_normal([ neuron_size ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
186 |
+
'b2': tf.Variable(tf.random_normal([ neuron_size ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
187 |
+
'b3': tf.Variable(tf.random_normal([ output_size ], stddev=init_val, dtype=tf.float32), dtype=tf.float32)
|
188 |
+
}
|
189 |
+
|
190 |
+
|
191 |
+
# 10
|
192 |
+
Y1 = tf.layers.batch_normalization(tf.matmul( flat , weights['w1'] )+bias['b1'],training = is_training)
|
193 |
+
Y1act = UAF(Y1,A,B,C,D,E)
|
194 |
+
|
195 |
+
Y2 = tf.layers.batch_normalization(tf.matmul( Y1act , weights['w2'] )+bias['b2'],training = is_training)
|
196 |
+
Y2act = UAF(Y2,A,B,C,D,E)
|
197 |
+
|
198 |
+
# 12
|
199 |
+
yhat = UAF(tf.matmul( Y2act , weights['w3'] )+bias['b3'],A,B,C,D,E)
|
200 |
+
|
201 |
+
|
202 |
+
train_vars = [conv1_filter
|
203 |
+
, conv2_filter
|
204 |
+
, conv3_filter
|
205 |
+
, conv4_filter
|
206 |
+
, conv5_filter
|
207 |
+
, conv6_filter
|
208 |
+
, conv7_filter
|
209 |
+
, conv8_filter
|
210 |
+
, conv9_filter
|
211 |
+
, conv10_filter
|
212 |
+
, conv11_filter
|
213 |
+
, conv12_filter
|
214 |
+
, conv13_filter
|
215 |
+
|
216 |
+
|
217 |
+
|
218 |
+
, cnnbias['b1']
|
219 |
+
, cnnbias['b2']
|
220 |
+
, cnnbias['b3']
|
221 |
+
, cnnbias['b4']
|
222 |
+
, cnnbias['b5']
|
223 |
+
, cnnbias['b6']
|
224 |
+
, cnnbias['b7']
|
225 |
+
, cnnbias['b8']
|
226 |
+
, cnnbias['b9']
|
227 |
+
, cnnbias['b10']
|
228 |
+
, cnnbias['b11']
|
229 |
+
, cnnbias['b12']
|
230 |
+
, cnnbias['b13']
|
231 |
+
|
232 |
+
|
233 |
+
,weights['w1']
|
234 |
+
,weights['w2']
|
235 |
+
,weights['w3']
|
236 |
+
|
237 |
+
,bias['b1']
|
238 |
+
,bias['b2']
|
239 |
+
,bias['b3']
|
240 |
+
]
|
241 |
+
|
242 |
+
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
|
243 |
+
update_ops2 = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
|
244 |
+
|
245 |
+
|
246 |
+
|
247 |
+
#ta = three*tf.math.sigmoid(tf.Variable(tf.random_normal([1], mean=0.5, stddev=0.000000001, dtype=tf.float32), dtype=tf.float32))
|
248 |
+
#vc = tf.Variable(tf.random_normal([1], mean=0.2, stddev=0.000000000001, dtype=tf.float32), dtype=tf.float32)
|
249 |
+
#tc = tf.nn.relu(vc) + tf.math.log1p(tf.exp(-tf.abs(vc))) + eps
|
250 |
+
|
251 |
+
#loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = yhat, labels = y))
|
252 |
+
loss = tf.math.sqrt(tf.reduce_mean( tf.square(tf.subtract(y, yhat)) ) )
|
253 |
+
|
254 |
+
|
255 |
+
|
256 |
+
|
257 |
+
train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
|
258 |
+
train_op2 = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss, var_list=train_vars)
|
259 |
+
train_op = tf.group([train_op, update_ops])
|
260 |
+
train_op2 = tf.group([train_op2, update_ops2])
|
261 |
+
|
262 |
+
|
263 |
+
|
264 |
+
|
265 |
+
tf.set_random_seed(4123)
|
266 |
+
|
267 |
+
with tf.device("/device:CPU:0"):
|
268 |
+
saver = tf.train.Saver()
|
269 |
+
|
270 |
+
with tf.Session() as sess:
|
271 |
+
sess.run(tf.global_variables_initializer())
|
272 |
+
|
273 |
+
#tf.train.write_graph(sess.graph_def, '.', 'cnn_lstm.pbtxt')
|
274 |
+
|
275 |
+
#saver.restore(sess, neural_network_model_file)
|
276 |
+
ims = []
|
277 |
+
testloss = []
|
278 |
+
trainloss = []
|
279 |
+
|
280 |
+
prev = 10000
|
281 |
+
gl = 100
|
282 |
+
thres = 1E-10
|
283 |
+
epoch = 0
|
284 |
+
c = 1000
|
285 |
+
|
286 |
+
|
287 |
+
resetvar = 0
|
288 |
+
test_img = np.load("test_img.npy").astype("float32")
|
289 |
+
test_label = np.load("test_label.npy").astype("float32")
|
290 |
+
|
291 |
+
#for epoch in range(4000):
|
292 |
+
st = time.process_time()
|
293 |
+
while(1==1):
|
294 |
+
train_img = np.load("train_img"+str(epoch%size)+".npy").astype("float32")
|
295 |
+
train_label = np.load("train_label"+str(epoch%size)+".npy").astype("float32")
|
296 |
+
|
297 |
+
|
298 |
+
|
299 |
+
feed_dict = {y: train_label,
|
300 |
+
learning_rate: lr,
|
301 |
+
is_training: True,
|
302 |
+
x: train_img}
|
303 |
+
|
304 |
+
|
305 |
+
if (gl < 0.08):
|
306 |
+
lr = 0.000001
|
307 |
+
else:
|
308 |
+
lr = 0.00001
|
309 |
+
|
310 |
+
if (resetvar == 0):
|
311 |
+
_, gl= sess.run([ train_op, loss],
|
312 |
+
feed_dict=feed_dict)
|
313 |
+
if (gl < 0.01):
|
314 |
+
resetvar = 1
|
315 |
+
UA,UB,UD,UE= sess.run([ A,B,D,E],
|
316 |
+
feed_dict=feed_dict)
|
317 |
+
sess.run(tf.global_variables_initializer())
|
318 |
+
A.load(UA)
|
319 |
+
B.load(UB)
|
320 |
+
D.load(UD)
|
321 |
+
E.load(UE)
|
322 |
+
elif (resetvar == 1):
|
323 |
+
if (gl < 0.01):
|
324 |
+
resetvar = 2
|
325 |
+
_, gl= sess.run([ train_op2, loss],
|
326 |
+
feed_dict=feed_dict)
|
327 |
+
elif (resetvar == 2):
|
328 |
+
_, gl= sess.run([ train_op, loss],
|
329 |
+
feed_dict=feed_dict)
|
330 |
+
|
331 |
+
|
332 |
+
if (epoch % 100) == 0:
|
333 |
+
feed_dict = {is_training: False,
|
334 |
+
x: test_img}
|
335 |
+
|
336 |
+
output_y = sess.run(yhat,
|
337 |
+
feed_dict=feed_dict)
|
338 |
+
|
339 |
+
y_pred = np.argmax(output_y,axis=1).astype("int32")
|
340 |
+
y_true = np.argmax(test_label,axis=1).astype("int32")
|
341 |
+
_,_,f1,_ = precision_recall_fscore_support(y_true, y_pred, average='macro')
|
342 |
+
testloss.append(f1)
|
343 |
+
print(precision_recall_fscore_support(y_true, y_pred, average='macro'))
|
344 |
+
|
345 |
+
|
346 |
+
feed_dict = {
|
347 |
+
is_training: False,
|
348 |
+
x: train_img}
|
349 |
+
|
350 |
+
output_y = sess.run(yhat,
|
351 |
+
feed_dict=feed_dict)
|
352 |
+
|
353 |
+
y_pred = np.argmax(output_y,axis=1).astype("int32")
|
354 |
+
y_true = np.argmax(train_label,axis=1).astype("int32")
|
355 |
+
_,_,f1,_ = precision_recall_fscore_support(y_true, y_pred, average='macro')
|
356 |
+
trainloss.append(f1)
|
357 |
+
|
358 |
+
#UA,UB,UC,UD,UE= sess.run([ A,B,C,D,E],
|
359 |
+
#feed_dict=feed_dict)
|
360 |
+
|
361 |
+
UA,UB,UD,UE= sess.run([ A,B,D,E],
|
362 |
+
feed_dict=feed_dict)
|
363 |
+
|
364 |
+
ims.append(np.array([UA,UB,0.0,UD,UE]))
|
365 |
+
|
366 |
+
|
367 |
+
|
368 |
+
|
369 |
+
|
370 |
+
|
371 |
+
#loss_val.append([gl,dl])
|
372 |
+
print(epoch)
|
373 |
+
epoch = epoch + 1
|
374 |
+
print(gl)
|
375 |
+
prev = c
|
376 |
+
c = gl
|
377 |
+
|
378 |
+
|
379 |
+
if (epoch > 12000):
|
380 |
+
elapsed_time = time.process_time() - st
|
381 |
+
|
382 |
+
data_dir = "time_"+str(size)+ "_" + str(fold) + ".npy"
|
383 |
+
np.save(data_dir, np.array([elapsed_time, epoch]))
|
384 |
+
|
385 |
+
|
386 |
+
feed_dict = {
|
387 |
+
is_training: False,
|
388 |
+
x: test_img}
|
389 |
+
|
390 |
+
output_y = sess.run(yhat,
|
391 |
+
feed_dict=feed_dict)
|
392 |
+
|
393 |
+
|
394 |
+
y_pred = np.argmax(output_y,axis=1).astype("int32")
|
395 |
+
y_true = np.argmax(test_label,axis=1).astype("int32")
|
396 |
+
|
397 |
+
data_dir = "test_pred_"+str(size) + "_" + str(fold) + ".npy"
|
398 |
+
np.save(data_dir, y_pred)
|
399 |
+
|
400 |
+
data_dir = "test_true_"+str(size) + "_" + str(fold) + ".npy"
|
401 |
+
np.save(data_dir, y_true)
|
402 |
+
|
403 |
+
|
404 |
+
|
405 |
+
|
406 |
+
|
407 |
+
feed_dict = {
|
408 |
+
is_training: False,
|
409 |
+
x: train_img}
|
410 |
+
|
411 |
+
output_y = sess.run(yhat,
|
412 |
+
feed_dict=feed_dict)
|
413 |
+
|
414 |
+
|
415 |
+
y_pred = np.argmax(output_y,axis=1).astype("int32")
|
416 |
+
y_true = np.argmax(train_label,axis=1).astype("int32")
|
417 |
+
|
418 |
+
data_dir = "train_pred_"+str(size) + "_" + str(fold) + ".npy"
|
419 |
+
np.save(data_dir, y_pred)
|
420 |
+
|
421 |
+
data_dir = "train_true_"+str(size) + "_" + str(fold) + ".npy"
|
422 |
+
np.save(data_dir, y_true)
|
423 |
+
|
424 |
+
|
425 |
+
trainloss = np.array(trainloss)
|
426 |
+
np.save("trainloss"+str(size) + "_" + str(fold) + ".npy", trainloss)
|
427 |
+
|
428 |
+
testloss = np.array(testloss)
|
429 |
+
np.save("testloss"+str(size) + "_" + str(fold) + ".npy", testloss)
|
430 |
+
|
431 |
+
ims = np.asarray(ims)
|
432 |
+
np.save("ims"+str(size) + "_" + str(fold) + ".npy", ims)
|
433 |
+
|
434 |
+
break
|
435 |
+
|
436 |
+
|
437 |
+
|
438 |
+
|
439 |
+
#ani = animation.ArtistAnimation(fig, ims, interval=100, blit=True, repeat_delay=2000)
|
440 |
+
#ani.save('animation.gif', writer='imagemagick', fps=10)
|
441 |
+
|
442 |
+
saver.save(sess, neural_network_model_file)
|
443 |
+
|
444 |
+
|
445 |
+
|
446 |
+
|
447 |
+
|
448 |
+
|
449 |
+
|
450 |
+
|
451 |
+
|
452 |
+
|
453 |
+
if __name__ == '__main__':
|
454 |
+
main()
|
tensorflow/CNNUAF_VGGBC.py
ADDED
@@ -0,0 +1,383 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import numpy as np
|
3 |
+
import sys
|
4 |
+
from sklearn.metrics import precision_recall_fscore_support
|
5 |
+
import time
|
6 |
+
|
7 |
+
|
8 |
+
def softplus(x):
|
9 |
+
return np.maximum(0, x) + np.log1p(np.exp(-np.abs(x)))
|
10 |
+
|
11 |
+
|
12 |
+
def main():
|
13 |
+
|
14 |
+
neural_network_model_file = "./mlp.ckpt"
|
15 |
+
|
16 |
+
|
17 |
+
img_size = 32
|
18 |
+
num_channels = 3
|
19 |
+
output_size = 10
|
20 |
+
neuron_size = 1024
|
21 |
+
init_val = 1E-2
|
22 |
+
lr = 0.0007
|
23 |
+
|
24 |
+
size = int(sys.argv[1])
|
25 |
+
|
26 |
+
fold = int(sys.argv[2])
|
27 |
+
|
28 |
+
input_keep_prob = 0.8
|
29 |
+
|
30 |
+
|
31 |
+
print ("Setting network up")
|
32 |
+
with tf.device("/device:GPU:0"):
|
33 |
+
is_training = tf.placeholder(tf.bool)
|
34 |
+
keep_prob = tf.placeholder(tf.float32, name='keep_prob')
|
35 |
+
x = tf.placeholder("float32", [None, img_size, img_size, num_channels] , name="x")
|
36 |
+
y = tf.placeholder("float32", [None, output_size] )
|
37 |
+
learning_rate = tf.placeholder("float32", None)
|
38 |
+
|
39 |
+
conv1_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 3, 64], mean=0, stddev=init_val))
|
40 |
+
conv2_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 64, 64], mean=0, stddev=init_val))
|
41 |
+
|
42 |
+
conv3_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 64, 128], mean=0, stddev=init_val))
|
43 |
+
conv4_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 128, 128], mean=0, stddev=init_val))
|
44 |
+
|
45 |
+
conv5_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 128, 256], mean=0, stddev=init_val))
|
46 |
+
conv6_filter = tf.Variable(tf.truncated_normal(shape=[3, 3, 256, 256], mean=0, stddev=init_val))
|
47 |
+
|
48 |
+
|
49 |
+
cnnbias = {
|
50 |
+
'b1': tf.Variable(tf.random_normal([ 64 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
51 |
+
'b2': tf.Variable(tf.random_normal([ 64 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
52 |
+
'b3': tf.Variable(tf.random_normal([ 128 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
53 |
+
'b4': tf.Variable(tf.random_normal([ 128 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
54 |
+
'b5': tf.Variable(tf.random_normal([ 256 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
55 |
+
'b6': tf.Variable(tf.random_normal([ 256 ], stddev=init_val, dtype=tf.float32), dtype=tf.float32)
|
56 |
+
}
|
57 |
+
|
58 |
+
|
59 |
+
|
60 |
+
A = tf.Variable(tf.random_normal([1], mean=1.1, stddev=0.0, dtype=tf.float32), dtype=tf.float32)
|
61 |
+
B = tf.Variable(tf.random_normal([1], mean=-0.01, stddev=0.0, dtype=tf.float32), dtype=tf.float32)
|
62 |
+
C = tf.constant(0.0, dtype=tf.float32)
|
63 |
+
D = tf.Variable(tf.random_normal([1], mean=-0.9, stddev=0.0, dtype=tf.float32), dtype=tf.float32)
|
64 |
+
E = tf.Variable(tf.random_normal([1], mean=0.0, stddev=0.0, dtype=tf.float32), dtype=tf.float32)
|
65 |
+
|
66 |
+
three = tf.constant(3.0, dtype=tf.float32)
|
67 |
+
two = tf.constant(2.0, dtype=tf.float32)
|
68 |
+
one = tf.constant(1.0, dtype=tf.float32)
|
69 |
+
eps = tf.constant(1E-8, dtype=tf.float32)
|
70 |
+
|
71 |
+
def UAF(vv,UAF_A,UAF_B,UAF_C,UAF_D,UAF_E):
|
72 |
+
P1 = tf.multiply(UAF_A,(vv+UAF_B)) +tf.multiply(UAF_C,tf.pow(vv,two))
|
73 |
+
P2 = tf.multiply(UAF_D,(vv-UAF_B))
|
74 |
+
|
75 |
+
P3 = tf.nn.relu(P1) + tf.math.log1p(tf.exp(-tf.abs(P1)))
|
76 |
+
P4 = tf.nn.relu(P2) + tf.math.log1p(tf.exp(-tf.abs(P2)))
|
77 |
+
return P3 - P4 + UAF_E
|
78 |
+
|
79 |
+
|
80 |
+
conv1 = tf.nn.conv2d(x, conv1_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b1']
|
81 |
+
conv1_bn = tf.layers.batch_normalization(conv1,training = is_training)
|
82 |
+
conv1_act = UAF(conv1_bn,A,B,C,D,E)
|
83 |
+
|
84 |
+
conv2 = tf.nn.conv2d(conv1_act, conv2_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b2']
|
85 |
+
conv2_bn = tf.layers.batch_normalization(conv2,training = is_training)
|
86 |
+
conv2_act = UAF(conv2_bn,A,B,C,D,E)
|
87 |
+
conv2_pool = tf.nn.max_pool(conv2_act, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
|
88 |
+
|
89 |
+
conv2_drop = tf.nn.dropout(conv2_pool, keep_prob)
|
90 |
+
|
91 |
+
|
92 |
+
conv3 = tf.nn.conv2d(conv2_drop, conv3_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b3']
|
93 |
+
conv3_bn = tf.layers.batch_normalization(conv3,training = is_training)
|
94 |
+
conv3_act = UAF(conv3_bn,A,B,C,D,E)
|
95 |
+
|
96 |
+
conv4 = tf.nn.conv2d(conv3_act, conv4_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b4']
|
97 |
+
conv4_bn = tf.layers.batch_normalization(conv4,training = is_training)
|
98 |
+
conv4_act = UAF(conv4_bn,A,B,C,D,E)
|
99 |
+
conv4_pool = tf.nn.max_pool(conv4_act, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
|
100 |
+
|
101 |
+
|
102 |
+
conv4_drop = tf.nn.dropout(conv4_pool, keep_prob)
|
103 |
+
|
104 |
+
|
105 |
+
|
106 |
+
conv5 = tf.nn.conv2d(conv4_drop, conv5_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b5']
|
107 |
+
conv5_bn = tf.layers.batch_normalization(conv5,training = is_training)
|
108 |
+
conv5_act = UAF(conv5_bn,A,B,C,D,E)
|
109 |
+
|
110 |
+
conv6 = tf.nn.conv2d(conv5_act, conv6_filter, strides=[1,1,1,1], padding='SAME') + cnnbias['b6']
|
111 |
+
conv6_bn = tf.layers.batch_normalization(conv6,training = is_training)
|
112 |
+
conv6_act = UAF(conv6_bn,A,B,C,D,E)
|
113 |
+
conv6_pool = tf.nn.max_pool(conv6_act, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
|
114 |
+
|
115 |
+
conv6_drop = tf.nn.dropout(conv6_pool, keep_prob)
|
116 |
+
|
117 |
+
|
118 |
+
# 9
|
119 |
+
flat = tf.contrib.layers.flatten(conv6_drop)
|
120 |
+
|
121 |
+
|
122 |
+
weights = {
|
123 |
+
'w1': tf.Variable(tf.random_normal([ 4096, neuron_size ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
124 |
+
'w2': tf.Variable(tf.random_normal([ neuron_size, neuron_size ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
125 |
+
'w3': tf.Variable(tf.random_normal([ neuron_size, output_size ], stddev=init_val, dtype=tf.float32), dtype=tf.float32)
|
126 |
+
}
|
127 |
+
|
128 |
+
bias = {
|
129 |
+
'b1': tf.Variable(tf.random_normal([ neuron_size ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
130 |
+
'b2': tf.Variable(tf.random_normal([ neuron_size ], stddev=init_val, dtype=tf.float32), dtype=tf.float32),
|
131 |
+
'b3': tf.Variable(tf.random_normal([ output_size ], stddev=init_val, dtype=tf.float32), dtype=tf.float32)
|
132 |
+
}
|
133 |
+
|
134 |
+
|
135 |
+
# 10
|
136 |
+
Y1 = tf.layers.batch_normalization(tf.matmul( flat , weights['w1'] )+bias['b1'],training = is_training)
|
137 |
+
Y1_drop = tf.nn.dropout(Y1, keep_prob)
|
138 |
+
Y1act = UAF(Y1_drop,A,B,C,D,E)
|
139 |
+
|
140 |
+
|
141 |
+
Y2 = tf.layers.batch_normalization(tf.matmul( Y1act , weights['w2'] )+bias['b2'],training = is_training)
|
142 |
+
Y2_drop = tf.nn.dropout(Y2, keep_prob)
|
143 |
+
Y2act = UAF(Y2_drop,A,B,C,D,E)
|
144 |
+
|
145 |
+
# 12
|
146 |
+
yhat = UAF(tf.matmul( Y2act , weights['w3'] )+bias['b3'],A,B,C,D,E)
|
147 |
+
|
148 |
+
|
149 |
+
train_vars = [conv1_filter
|
150 |
+
, conv2_filter
|
151 |
+
, conv3_filter
|
152 |
+
, conv4_filter
|
153 |
+
, conv5_filter
|
154 |
+
, conv6_filter
|
155 |
+
|
156 |
+
, cnnbias['b1']
|
157 |
+
, cnnbias['b2']
|
158 |
+
, cnnbias['b3']
|
159 |
+
, cnnbias['b4']
|
160 |
+
, cnnbias['b5']
|
161 |
+
, cnnbias['b6']
|
162 |
+
|
163 |
+
,weights['w1']
|
164 |
+
,weights['w2']
|
165 |
+
,weights['w3']
|
166 |
+
|
167 |
+
,bias['b1']
|
168 |
+
,bias['b2']
|
169 |
+
,bias['b3']
|
170 |
+
]
|
171 |
+
|
172 |
+
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
|
173 |
+
update_ops2 = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
|
174 |
+
|
175 |
+
|
176 |
+
|
177 |
+
#ta = three*tf.math.sigmoid(tf.Variable(tf.random_normal([1], mean=0.5, stddev=0.000000001, dtype=tf.float32), dtype=tf.float32))
|
178 |
+
#vc = tf.Variable(tf.random_normal([1], mean=0.2, stddev=0.000000000001, dtype=tf.float32), dtype=tf.float32)
|
179 |
+
#tc = tf.nn.relu(vc) + tf.math.log1p(tf.exp(-tf.abs(vc))) + eps
|
180 |
+
|
181 |
+
#loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = yhat, labels = y))
|
182 |
+
loss = tf.math.sqrt(tf.reduce_mean( tf.square(tf.subtract(y, yhat)) ) )
|
183 |
+
|
184 |
+
|
185 |
+
|
186 |
+
|
187 |
+
train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
|
188 |
+
train_op2 = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss, var_list=train_vars)
|
189 |
+
train_op = tf.group([train_op, update_ops])
|
190 |
+
train_op2 = tf.group([train_op2, update_ops2])
|
191 |
+
|
192 |
+
|
193 |
+
|
194 |
+
|
195 |
+
tf.set_random_seed(4123)
|
196 |
+
|
197 |
+
with tf.device("/device:CPU:0"):
|
198 |
+
saver = tf.train.Saver()
|
199 |
+
|
200 |
+
with tf.Session() as sess:
|
201 |
+
sess.run(tf.global_variables_initializer())
|
202 |
+
|
203 |
+
#tf.train.write_graph(sess.graph_def, '.', 'cnn_lstm.pbtxt')
|
204 |
+
|
205 |
+
#saver.restore(sess, neural_network_model_file)
|
206 |
+
ims = []
|
207 |
+
testloss = []
|
208 |
+
trainloss = []
|
209 |
+
stats = []
|
210 |
+
|
211 |
+
prev = 10000
|
212 |
+
gl = 100
|
213 |
+
thres = 1E-10
|
214 |
+
epoch = 0
|
215 |
+
c = 1000
|
216 |
+
|
217 |
+
|
218 |
+
resetvar = 0
|
219 |
+
test_img = np.load("test_img.npy").astype("float32")
|
220 |
+
test_label = np.load("test_label.npy").astype("float32")
|
221 |
+
|
222 |
+
#for epoch in range(4000):
|
223 |
+
st = time.process_time()
|
224 |
+
while(1==1):
|
225 |
+
train_img = np.load("train_img"+str(epoch%size)+".npy").astype("float32")
|
226 |
+
train_label = np.load("train_label"+str(epoch%size)+".npy").astype("float32")
|
227 |
+
|
228 |
+
|
229 |
+
|
230 |
+
feed_dict = {y: train_label,
|
231 |
+
learning_rate: lr,
|
232 |
+
is_training: True,
|
233 |
+
keep_prob: input_keep_prob,
|
234 |
+
x: train_img}
|
235 |
+
|
236 |
+
|
237 |
+
if (gl < 0.04):
|
238 |
+
lr = 0.00004
|
239 |
+
else:
|
240 |
+
lr = 0.003
|
241 |
+
|
242 |
+
|
243 |
+
if (gl < 0.14):
|
244 |
+
_, gl= sess.run([ train_op2, loss],
|
245 |
+
feed_dict=feed_dict)
|
246 |
+
else:
|
247 |
+
_, gl= sess.run([ train_op, loss],
|
248 |
+
feed_dict=feed_dict)
|
249 |
+
|
250 |
+
|
251 |
+
|
252 |
+
if (epoch % 100) == 0:
|
253 |
+
feed_dict = {
|
254 |
+
is_training: False,
|
255 |
+
keep_prob: 1.0,
|
256 |
+
x: test_img}
|
257 |
+
|
258 |
+
output_y = sess.run(yhat,
|
259 |
+
feed_dict=feed_dict)
|
260 |
+
|
261 |
+
y_pred = np.argmax(output_y,axis=1).astype("int32")
|
262 |
+
y_true = np.argmax(test_label,axis=1).astype("int32")
|
263 |
+
pr,re,f1,_ = precision_recall_fscore_support(y_true, y_pred, average='macro')
|
264 |
+
testloss.append(f1)
|
265 |
+
print(precision_recall_fscore_support(y_true, y_pred, average='macro'))
|
266 |
+
stats.append(np.array([pr,re,f1]))
|
267 |
+
|
268 |
+
feed_dict = {
|
269 |
+
is_training: False,
|
270 |
+
keep_prob: 1.0,
|
271 |
+
x: train_img}
|
272 |
+
|
273 |
+
output_y = sess.run(yhat,
|
274 |
+
feed_dict=feed_dict)
|
275 |
+
|
276 |
+
y_pred = np.argmax(output_y,axis=1).astype("int32")
|
277 |
+
y_true = np.argmax(train_label,axis=1).astype("int32")
|
278 |
+
_,_,f1,_ = precision_recall_fscore_support(y_true, y_pred, average='macro')
|
279 |
+
trainloss.append(f1)
|
280 |
+
|
281 |
+
#UA,UB,UC,UD,UE= sess.run([ A,B,C,D,E],
|
282 |
+
#feed_dict=feed_dict)
|
283 |
+
|
284 |
+
UA,UB,UD,UE= sess.run([ A,B,D,E],
|
285 |
+
feed_dict=feed_dict)
|
286 |
+
|
287 |
+
ims.append(np.array([UA,UB,0.0,UD,UE]))
|
288 |
+
|
289 |
+
|
290 |
+
|
291 |
+
|
292 |
+
|
293 |
+
|
294 |
+
#loss_val.append([gl,dl])
|
295 |
+
print(epoch)
|
296 |
+
epoch = epoch + 1
|
297 |
+
print(gl)
|
298 |
+
prev = c
|
299 |
+
c = gl
|
300 |
+
|
301 |
+
|
302 |
+
if (epoch > 10000):
|
303 |
+
elapsed_time = time.process_time() - st
|
304 |
+
|
305 |
+
data_dir = "time_"+str(size)+ "_" + str(fold) + ".npy"
|
306 |
+
np.save(data_dir, np.array([elapsed_time, epoch]))
|
307 |
+
|
308 |
+
|
309 |
+
feed_dict = {
|
310 |
+
is_training: False,
|
311 |
+
keep_prob: 1.0,
|
312 |
+
x: test_img}
|
313 |
+
|
314 |
+
output_y = sess.run(yhat,
|
315 |
+
feed_dict=feed_dict)
|
316 |
+
|
317 |
+
|
318 |
+
y_pred = np.argmax(output_y,axis=1).astype("int32")
|
319 |
+
y_true = np.argmax(test_label,axis=1).astype("int32")
|
320 |
+
|
321 |
+
data_dir = "test_pred_"+str(size) + "_" + str(fold) + ".npy"
|
322 |
+
np.save(data_dir, y_pred)
|
323 |
+
|
324 |
+
data_dir = "test_true_"+str(size) + "_" + str(fold) + ".npy"
|
325 |
+
np.save(data_dir, y_true)
|
326 |
+
|
327 |
+
|
328 |
+
|
329 |
+
|
330 |
+
|
331 |
+
feed_dict = {
|
332 |
+
is_training: False,
|
333 |
+
keep_prob: 1.0,
|
334 |
+
x: train_img}
|
335 |
+
|
336 |
+
output_y = sess.run(yhat,
|
337 |
+
feed_dict=feed_dict)
|
338 |
+
|
339 |
+
|
340 |
+
y_pred = np.argmax(output_y,axis=1).astype("int32")
|
341 |
+
y_true = np.argmax(train_label,axis=1).astype("int32")
|
342 |
+
|
343 |
+
data_dir = "train_pred_"+str(size) + "_" + str(fold) + ".npy"
|
344 |
+
np.save(data_dir, y_pred)
|
345 |
+
|
346 |
+
data_dir = "train_true_"+str(size) + "_" + str(fold) + ".npy"
|
347 |
+
np.save(data_dir, y_true)
|
348 |
+
|
349 |
+
|
350 |
+
trainloss = np.array(trainloss)
|
351 |
+
np.save("trainloss"+str(size) + "_" + str(fold) + ".npy", trainloss)
|
352 |
+
|
353 |
+
testloss = np.array(testloss)
|
354 |
+
np.save("testloss"+str(size) + "_" + str(fold) + ".npy", testloss)
|
355 |
+
|
356 |
+
ims = np.asarray(ims)
|
357 |
+
np.save("ims"+str(size) + "_" + str(fold) + ".npy", ims)
|
358 |
+
|
359 |
+
stats = np.asarray(stats)
|
360 |
+
np.save("stats"+str(size) + "_" + str(fold) + ".npy", stats)
|
361 |
+
|
362 |
+
|
363 |
+
break
|
364 |
+
|
365 |
+
|
366 |
+
|
367 |
+
|
368 |
+
#ani = animation.ArtistAnimation(fig, ims, interval=100, blit=True, repeat_delay=2000)
|
369 |
+
#ani.save('animation.gif', writer='imagemagick', fps=10)
|
370 |
+
|
371 |
+
saver.save(sess, neural_network_model_file)
|
372 |
+
|
373 |
+
|
374 |
+
|
375 |
+
|
376 |
+
|
377 |
+
|
378 |
+
|
379 |
+
|
380 |
+
|
381 |
+
|
382 |
+
if __name__ == '__main__':
|
383 |
+
main()
|
tensorflow/DDPG_UAF.py
ADDED
@@ -0,0 +1,437 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import numpy as np
|
3 |
+
import gym
|
4 |
+
import os
|
5 |
+
import shutil
|
6 |
+
import sys
|
7 |
+
import time
|
8 |
+
|
9 |
+
|
10 |
+
np.random.seed(1)
|
11 |
+
tf.set_random_seed(1)
|
12 |
+
|
13 |
+
|
14 |
+
fold = int(sys.argv[1])
|
15 |
+
|
16 |
+
st = time.process_time()
|
17 |
+
|
18 |
+
MAX_EPISODES = 1500
|
19 |
+
LR_A = 0.0005 # learning rate for actor
|
20 |
+
LR_C = 0.0005 # learning rate for critic
|
21 |
+
GAMMA = 0.999 # reward discount
|
22 |
+
REPLACE_ITER_A = 1700
|
23 |
+
REPLACE_ITER_C = 1500
|
24 |
+
MEMORY_CAPACITY = 200000
|
25 |
+
BATCH_SIZE = 32
|
26 |
+
DISPLAY_THRESHOLD = 100 # display until the running reward > 100
|
27 |
+
DATA_PATH = './data'
|
28 |
+
LOAD_MODEL = False
|
29 |
+
SAVE_MODEL_ITER = 100000
|
30 |
+
RENDER = False
|
31 |
+
OUTPUT_GRAPH = False
|
32 |
+
ENV_NAME = 'BipedalWalker-v2'
|
33 |
+
|
34 |
+
GLOBAL_STEP = tf.Variable(0, trainable=False)
|
35 |
+
INCREASE_GS = GLOBAL_STEP.assign(tf.add(GLOBAL_STEP, 1))
|
36 |
+
LR_A = tf.train.exponential_decay(LR_A, GLOBAL_STEP, 10000, .97, staircase=True)
|
37 |
+
LR_C = tf.train.exponential_decay(LR_C, GLOBAL_STEP, 10000, .97, staircase=True)
|
38 |
+
END_POINT = (200 - 10) * (14/30) # from game
|
39 |
+
|
40 |
+
env = gym.make(ENV_NAME)
|
41 |
+
env.seed(1)
|
42 |
+
|
43 |
+
STATE_DIM = env.observation_space.shape[0] # 24
|
44 |
+
ACTION_DIM = env.action_space.shape[0] # 4
|
45 |
+
ACTION_BOUND = env.action_space.high # [1, 1, 1, 1]
|
46 |
+
|
47 |
+
# all placeholder for tf
|
48 |
+
with tf.name_scope('S'):
|
49 |
+
S = tf.placeholder(tf.float32, shape=[None, STATE_DIM], name='s')
|
50 |
+
with tf.name_scope('R'):
|
51 |
+
R = tf.placeholder(tf.float32, [None, 1], name='r')
|
52 |
+
with tf.name_scope('S_'):
|
53 |
+
S_ = tf.placeholder(tf.float32, shape=[None, STATE_DIM], name='s_')
|
54 |
+
|
55 |
+
############################### Actor ####################################
|
56 |
+
|
57 |
+
class Actor(object):
|
58 |
+
def __init__(self, sess, action_dim, action_bound, learning_rate, t_replace_iter):
|
59 |
+
self.sess = sess
|
60 |
+
self.a_dim = action_dim
|
61 |
+
self.action_bound = action_bound
|
62 |
+
self.lr = learning_rate
|
63 |
+
self.t_replace_iter = t_replace_iter
|
64 |
+
self.t_replace_counter = 0
|
65 |
+
|
66 |
+
with tf.variable_scope('Actor'):
|
67 |
+
# input s, output a
|
68 |
+
self.a = self._build_net(S, scope='eval_net', trainable=True)
|
69 |
+
|
70 |
+
# input s_, output a, get a_ for critic
|
71 |
+
self.a_ = self._build_net(S_, scope='target_net', trainable=False)
|
72 |
+
|
73 |
+
self.e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Actor/eval_net')
|
74 |
+
self.t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Actor/target_net')
|
75 |
+
|
76 |
+
def _build_net(self, s, scope, trainable):
|
77 |
+
with tf.variable_scope(scope):
|
78 |
+
init_w = tf.random_normal_initializer(0., 0.01)
|
79 |
+
init_b = tf.constant_initializer(0.01)
|
80 |
+
|
81 |
+
two = tf.constant(2.0, dtype=tf.float32)
|
82 |
+
def UAF(x):
|
83 |
+
UAF_A = tf.Variable(tf.random_normal([1], mean=1.1, stddev=0.0, dtype=tf.float32), dtype=tf.float32)
|
84 |
+
UAF_B = tf.Variable(tf.random_normal([1], mean=-0.01, stddev=0.0, dtype=tf.float32), dtype=tf.float32)
|
85 |
+
UAF_C = tf.Variable(tf.random_normal([1], mean=-0.1, stddev=0.0, dtype=tf.float32), dtype=tf.float32)
|
86 |
+
UAF_D = tf.Variable(tf.random_normal([1], mean=-0.9, stddev=0.0, dtype=tf.float32), dtype=tf.float32)
|
87 |
+
UAF_E = tf.Variable(tf.random_normal([1], mean=0.01, stddev=0.0, dtype=tf.float32), dtype=tf.float32)
|
88 |
+
|
89 |
+
P1 = tf.multiply(UAF_A,(x+UAF_B)) +tf.multiply(UAF_C,tf.pow(x,two))
|
90 |
+
P2 = tf.multiply(UAF_D,(x-UAF_B))
|
91 |
+
|
92 |
+
P3 = tf.nn.relu(P1) + tf.math.log1p(tf.exp(-tf.abs(P1)))
|
93 |
+
P4 = tf.nn.relu(P2) + tf.math.log1p(tf.exp(-tf.abs(P2)))
|
94 |
+
return P3 - P4 + UAF_E
|
95 |
+
|
96 |
+
|
97 |
+
net = tf.layers.dense(s, 500, activation=UAF,
|
98 |
+
kernel_initializer=init_w, bias_initializer=init_b, name='l1', trainable=trainable)
|
99 |
+
net = tf.layers.dense(net, 200, activation=UAF,
|
100 |
+
kernel_initializer=init_w, bias_initializer=init_b, name='l2', trainable=trainable)
|
101 |
+
|
102 |
+
with tf.variable_scope('a'):
|
103 |
+
actions = tf.layers.dense(net, self.a_dim, activation=tf.nn.tanh, kernel_initializer=init_w,
|
104 |
+
bias_initializer=init_b, name='a', trainable=trainable)
|
105 |
+
scaled_a = tf.multiply(actions, self.action_bound, name='scaled_a') # Scale output to -action_bound to action_bound
|
106 |
+
return scaled_a
|
107 |
+
|
108 |
+
def learn(self, s): # batch update
|
109 |
+
self.sess.run(self.train_op, feed_dict={S: s})
|
110 |
+
if self.t_replace_counter % self.t_replace_iter == 0:
|
111 |
+
self.sess.run([tf.assign(t, e) for t, e in zip(self.t_params, self.e_params)])
|
112 |
+
self.t_replace_counter += 1
|
113 |
+
|
114 |
+
def choose_action(self, s):
|
115 |
+
s = s[np.newaxis, :] # single state
|
116 |
+
return self.sess.run(self.a, feed_dict={S: s})[0] # single action
|
117 |
+
|
118 |
+
def add_grad_to_graph(self, a_grads):
|
119 |
+
with tf.variable_scope('policy_grads'):
|
120 |
+
# ys = policy;
|
121 |
+
# xs = policy's parameters;
|
122 |
+
# self.a_grads = the gradients of the policy to get more Q
|
123 |
+
# tf.gradients will calculate dys/dxs with a initial gradients for ys, so this is dq/da * da/dparams
|
124 |
+
self.policy_grads_and_vars = tf.gradients(ys=self.a, xs=self.e_params, grad_ys=a_grads)
|
125 |
+
|
126 |
+
with tf.variable_scope('A_train'):
|
127 |
+
opt = tf.train.RMSPropOptimizer(-self.lr) # (- learning rate) for ascent policy
|
128 |
+
self.train_op = opt.apply_gradients(zip(self.policy_grads_and_vars, self.e_params), global_step=GLOBAL_STEP)
|
129 |
+
|
130 |
+
|
131 |
+
############################### Critic ####################################
|
132 |
+
|
133 |
+
class Critic(object):
|
134 |
+
def __init__(self, sess, state_dim, action_dim, learning_rate, gamma, t_replace_iter, a, a_):
|
135 |
+
self.sess = sess
|
136 |
+
self.s_dim = state_dim
|
137 |
+
self.a_dim = action_dim
|
138 |
+
self.lr = learning_rate
|
139 |
+
self.gamma = gamma
|
140 |
+
self.t_replace_iter = t_replace_iter
|
141 |
+
self.t_replace_counter = 0
|
142 |
+
|
143 |
+
with tf.variable_scope('Critic'):
|
144 |
+
# Input (s, a), output q
|
145 |
+
self.a = a
|
146 |
+
self.q = self._build_net(S, self.a, 'eval_net', trainable=True)
|
147 |
+
|
148 |
+
# Input (s_, a_), output q_ for q_target
|
149 |
+
self.q_ = self._build_net(S_, a_, 'target_net', trainable=False) # target_q is based on a_ from Actor's target_net
|
150 |
+
|
151 |
+
self.e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Critic/eval_net')
|
152 |
+
self.t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Critic/target_net')
|
153 |
+
|
154 |
+
with tf.variable_scope('target_q'):
|
155 |
+
self.target_q = R + self.gamma * self.q_
|
156 |
+
|
157 |
+
with tf.variable_scope('abs_TD'):
|
158 |
+
self.abs_td = tf.abs(self.target_q - self.q)
|
159 |
+
self.ISWeights = tf.placeholder(tf.float32, [None, 1], name='IS_weights')
|
160 |
+
with tf.variable_scope('TD_error'):
|
161 |
+
self.loss = tf.reduce_mean(self.ISWeights * tf.squared_difference(self.target_q, self.q))
|
162 |
+
|
163 |
+
with tf.variable_scope('C_train'):
|
164 |
+
self.train_op = tf.train.AdamOptimizer(self.lr).minimize(self.loss, global_step=GLOBAL_STEP)
|
165 |
+
|
166 |
+
with tf.variable_scope('a_grad'):
|
167 |
+
self.a_grads = tf.gradients(self.q, a)[0] # tensor of gradients of each sample (None, a_dim)
|
168 |
+
|
169 |
+
def _build_net(self, s, a, scope, trainable):
|
170 |
+
with tf.variable_scope(scope):
|
171 |
+
init_w = tf.random_normal_initializer(0., 0.01)
|
172 |
+
init_b = tf.constant_initializer(0.01)
|
173 |
+
|
174 |
+
two = tf.constant(2.0, dtype=tf.float32)
|
175 |
+
def UAF2(x):
|
176 |
+
return tf.nn.relu(x)
|
177 |
+
|
178 |
+
|
179 |
+
with tf.variable_scope('l1'):
|
180 |
+
n_l1 = 700
|
181 |
+
# combine the action and states together in this way
|
182 |
+
w1_s = tf.get_variable('w1_s', [self.s_dim, n_l1], initializer=init_w, trainable=trainable)
|
183 |
+
w1_a = tf.get_variable('w1_a', [self.a_dim, n_l1], initializer=init_w, trainable=trainable)
|
184 |
+
b1 = tf.get_variable('b1', [1, n_l1], initializer=init_b, trainable=trainable)
|
185 |
+
net = UAF2(tf.matmul(s, w1_s) + tf.matmul(a, w1_a) + b1)
|
186 |
+
with tf.variable_scope('l2'):
|
187 |
+
net = tf.layers.dense(net, 20, activation=UAF2, kernel_initializer=init_w,
|
188 |
+
bias_initializer=init_b, name='l2', trainable=trainable)
|
189 |
+
with tf.variable_scope('q'):
|
190 |
+
q = tf.layers.dense(net, 1, kernel_initializer=init_w, bias_initializer=init_b, trainable=trainable) # Q(s,a)
|
191 |
+
return q
|
192 |
+
|
193 |
+
def learn(self, s, a, r, s_, ISW):
|
194 |
+
_, abs_td = self.sess.run([self.train_op, self.abs_td], feed_dict={S: s, self.a: a, R: r, S_: s_, self.ISWeights: ISW})
|
195 |
+
if self.t_replace_counter % self.t_replace_iter == 0:
|
196 |
+
self.sess.run([tf.assign(t, e) for t, e in zip(self.t_params, self.e_params)])
|
197 |
+
self.t_replace_counter += 1
|
198 |
+
return abs_td
|
199 |
+
|
200 |
+
|
201 |
+
class SumTree(object):
|
202 |
+
"""
|
203 |
+
This SumTree code is modified version and the original code is from:
|
204 |
+
https://github.com/jaara/AI-blog/blob/master/SumTree.py
|
205 |
+
|
206 |
+
Story the data with it priority in tree and data frameworks.
|
207 |
+
"""
|
208 |
+
data_pointer = 0
|
209 |
+
|
210 |
+
def __init__(self, capacity):
|
211 |
+
self.capacity = capacity # for all priority values
|
212 |
+
self.tree = np.zeros(2 * capacity - 1)+1e-5
|
213 |
+
# [--------------Parent nodes-------------][-------leaves to recode priority-------]
|
214 |
+
# size: capacity - 1 size: capacity
|
215 |
+
self.data = np.zeros(capacity, dtype=object) # for all transitions
|
216 |
+
# [--------------data frame-------------]
|
217 |
+
# size: capacity
|
218 |
+
|
219 |
+
def add_new_priority(self, p, data):
|
220 |
+
leaf_idx = self.data_pointer + self.capacity - 1
|
221 |
+
|
222 |
+
self.data[self.data_pointer] = data # update data_frame
|
223 |
+
self.update(leaf_idx, p) # update tree_frame
|
224 |
+
self.data_pointer += 1
|
225 |
+
if self.data_pointer >= self.capacity: # replace when exceed the capacity
|
226 |
+
self.data_pointer = 0
|
227 |
+
|
228 |
+
def update(self, tree_idx, p):
|
229 |
+
change = p - self.tree[tree_idx]
|
230 |
+
|
231 |
+
self.tree[tree_idx] = p
|
232 |
+
self._propagate_change(tree_idx, change)
|
233 |
+
|
234 |
+
def _propagate_change(self, tree_idx, change):
|
235 |
+
"""change the sum of priority value in all parent nodes"""
|
236 |
+
parent_idx = (tree_idx - 1) // 2
|
237 |
+
self.tree[parent_idx] += change
|
238 |
+
if parent_idx != 0:
|
239 |
+
self._propagate_change(parent_idx, change)
|
240 |
+
|
241 |
+
def get_leaf(self, lower_bound):
|
242 |
+
leaf_idx = self._retrieve(lower_bound) # search the max leaf priority based on the lower_bound
|
243 |
+
data_idx = leaf_idx - self.capacity + 1
|
244 |
+
return [leaf_idx, self.tree[leaf_idx], self.data[data_idx]]
|
245 |
+
|
246 |
+
def _retrieve(self, lower_bound, parent_idx=0):
|
247 |
+
"""
|
248 |
+
Tree structure and array storage:
|
249 |
+
|
250 |
+
Tree index:
|
251 |
+
0 -> storing priority sum
|
252 |
+
/ \
|
253 |
+
1 2
|
254 |
+
/ \ / \
|
255 |
+
3 4 5 6 -> storing priority for transitions
|
256 |
+
|
257 |
+
Array type for storing:
|
258 |
+
[0,1,2,3,4,5,6]
|
259 |
+
"""
|
260 |
+
left_child_idx = 2 * parent_idx + 1
|
261 |
+
right_child_idx = left_child_idx + 1
|
262 |
+
|
263 |
+
if left_child_idx >= len(self.tree): # end search when no more child
|
264 |
+
return parent_idx
|
265 |
+
|
266 |
+
if self.tree[left_child_idx] == self.tree[right_child_idx]:
|
267 |
+
return self._retrieve(lower_bound, np.random.choice([left_child_idx, right_child_idx]))
|
268 |
+
if lower_bound <= self.tree[left_child_idx]: # downward search, always search for a higher priority node
|
269 |
+
return self._retrieve(lower_bound, left_child_idx)
|
270 |
+
else:
|
271 |
+
return self._retrieve(lower_bound - self.tree[left_child_idx], right_child_idx)
|
272 |
+
|
273 |
+
@property
|
274 |
+
def root_priority(self):
|
275 |
+
return self.tree[0] # the root
|
276 |
+
|
277 |
+
|
278 |
+
class Memory(object): # stored as ( s, a, r, s_ ) in SumTree
|
279 |
+
"""
|
280 |
+
This SumTree code is modified version and the original code is from:
|
281 |
+
https://github.com/jaara/AI-blog/blob/master/Seaquest-DDQN-PER.py
|
282 |
+
"""
|
283 |
+
epsilon = 0.001 # small amount to avoid zero priority
|
284 |
+
alpha = 0.6 # [0~1] convert the importance of TD error to priority
|
285 |
+
beta = 0.4 # importance-sampling, from initial value increasing to 1
|
286 |
+
beta_increment_per_sampling = 1e-5 # annealing the bias
|
287 |
+
abs_err_upper = 1 # for stability refer to paper
|
288 |
+
|
289 |
+
def __init__(self, capacity):
|
290 |
+
self.tree = SumTree(capacity)
|
291 |
+
|
292 |
+
def store(self, error, transition):
|
293 |
+
p = self._get_priority(error)
|
294 |
+
self.tree.add_new_priority(p, transition)
|
295 |
+
|
296 |
+
def prio_sample(self, n):
|
297 |
+
batch_idx, batch_memory, ISWeights = [], [], []
|
298 |
+
segment = self.tree.root_priority / n
|
299 |
+
self.beta = np.min([1, self.beta + self.beta_increment_per_sampling]) # max = 1
|
300 |
+
|
301 |
+
min_prob = np.min(self.tree.tree[-self.tree.capacity:]) / self.tree.root_priority
|
302 |
+
maxiwi = np.power(self.tree.capacity * min_prob, -self.beta) # for later normalizing ISWeights
|
303 |
+
for i in range(n):
|
304 |
+
a = segment * i
|
305 |
+
b = segment * (i + 1)
|
306 |
+
lower_bound = np.random.uniform(a, b)
|
307 |
+
while True:
|
308 |
+
idx, p, data = self.tree.get_leaf(lower_bound)
|
309 |
+
if type(data) is int:
|
310 |
+
i -= 1
|
311 |
+
lower_bound = np.random.uniform(segment * i, segment * (i+1))
|
312 |
+
else:
|
313 |
+
break
|
314 |
+
prob = p / self.tree.root_priority
|
315 |
+
ISWeights.append(self.tree.capacity * prob)
|
316 |
+
batch_idx.append(idx)
|
317 |
+
batch_memory.append(data)
|
318 |
+
|
319 |
+
ISWeights = np.vstack(ISWeights)
|
320 |
+
ISWeights = np.power(ISWeights, -self.beta) / maxiwi # normalize
|
321 |
+
return batch_idx, np.vstack(batch_memory), ISWeights
|
322 |
+
|
323 |
+
def random_sample(self, n):
|
324 |
+
idx = np.random.randint(0, self.tree.capacity, size=n, dtype=np.int)
|
325 |
+
return np.vstack(self.tree.data[idx])
|
326 |
+
|
327 |
+
def update(self, idx, error):
|
328 |
+
p = self._get_priority(error)
|
329 |
+
self.tree.update(idx, p)
|
330 |
+
|
331 |
+
def _get_priority(self, error):
|
332 |
+
error += self.epsilon # avoid 0
|
333 |
+
clipped_error = np.clip(error, 0, self.abs_err_upper)
|
334 |
+
return np.power(clipped_error, self.alpha)
|
335 |
+
|
336 |
+
|
337 |
+
sess = tf.Session()
|
338 |
+
|
339 |
+
# Create actor and critic.
|
340 |
+
actor = Actor(sess, ACTION_DIM, ACTION_BOUND, LR_A, REPLACE_ITER_A)
|
341 |
+
critic = Critic(sess, STATE_DIM, ACTION_DIM, LR_C, GAMMA, REPLACE_ITER_C, actor.a, actor.a_)
|
342 |
+
actor.add_grad_to_graph(critic.a_grads)
|
343 |
+
|
344 |
+
M = Memory(MEMORY_CAPACITY)
|
345 |
+
|
346 |
+
saver = tf.train.Saver(max_to_keep=100)
|
347 |
+
|
348 |
+
if LOAD_MODEL:
|
349 |
+
all_ckpt = tf.train.get_checkpoint_state('./data', 'checkpoint').all_model_checkpoint_paths
|
350 |
+
saver.restore(sess, all_ckpt[-1])
|
351 |
+
else:
|
352 |
+
if os.path.isdir(DATA_PATH): shutil.rmtree(DATA_PATH)
|
353 |
+
os.mkdir(DATA_PATH)
|
354 |
+
sess.run(tf.global_variables_initializer())
|
355 |
+
|
356 |
+
if OUTPUT_GRAPH:
|
357 |
+
tf.summary.FileWriter('logs', graph=sess.graph)
|
358 |
+
|
359 |
+
var = 3 # control exploration
|
360 |
+
var_min = 0.01
|
361 |
+
|
362 |
+
|
363 |
+
dataarr = []
|
364 |
+
|
365 |
+
|
366 |
+
for i_episode in range(MAX_EPISODES):
|
367 |
+
# s = (hull angle speed, angular velocity, horizontal speed, vertical speed, position of joints and joints angular speed, legs contact with ground, and 10 lidar rangefinder measurements.)
|
368 |
+
s = env.reset()
|
369 |
+
ep_r = 0
|
370 |
+
while True:
|
371 |
+
if RENDER:
|
372 |
+
env.render()
|
373 |
+
a = actor.choose_action(s)
|
374 |
+
a = np.clip(np.random.normal(a, var), -1, 1) # add randomness to action selection for exploration
|
375 |
+
s_, r, done, _ = env.step(a) # r = total 300+ points up to the far end. If the robot falls, it gets -100.
|
376 |
+
|
377 |
+
if r == -100: r = -2
|
378 |
+
ep_r += r
|
379 |
+
|
380 |
+
transition = np.hstack((s, a, [r], s_))
|
381 |
+
max_p = np.max(M.tree.tree[-M.tree.capacity:])
|
382 |
+
M.store(max_p, transition)
|
383 |
+
|
384 |
+
if GLOBAL_STEP.eval(sess) > MEMORY_CAPACITY/20:
|
385 |
+
var = max([var*0.9999, var_min]) # decay the action randomness
|
386 |
+
tree_idx, b_M, ISWeights = M.prio_sample(BATCH_SIZE) # for critic update
|
387 |
+
b_s = b_M[:, :STATE_DIM]
|
388 |
+
b_a = b_M[:, STATE_DIM: STATE_DIM + ACTION_DIM]
|
389 |
+
b_r = b_M[:, -STATE_DIM - 1: -STATE_DIM]
|
390 |
+
b_s_ = b_M[:, -STATE_DIM:]
|
391 |
+
|
392 |
+
abs_td = critic.learn(b_s, b_a, b_r, b_s_, ISWeights)
|
393 |
+
actor.learn(b_s)
|
394 |
+
for i in range(len(tree_idx)): # update priority
|
395 |
+
idx = tree_idx[i]
|
396 |
+
M.update(idx, abs_td[i])
|
397 |
+
if GLOBAL_STEP.eval(sess) % SAVE_MODEL_ITER == 0:
|
398 |
+
ckpt_path = os.path.join(DATA_PATH, 'DDPG.ckpt')
|
399 |
+
save_path = saver.save(sess, ckpt_path, global_step=GLOBAL_STEP, write_meta_graph=False)
|
400 |
+
print("\nSave Model %s\n" % save_path)
|
401 |
+
|
402 |
+
if done:
|
403 |
+
if "running_r" not in globals():
|
404 |
+
running_r = ep_r
|
405 |
+
else:
|
406 |
+
running_r = 0.95*running_r + 0.05*ep_r
|
407 |
+
#if running_r > DISPLAY_THRESHOLD: RENDER = True
|
408 |
+
#else: RENDER = False
|
409 |
+
|
410 |
+
stats = np.array([running_r, env.unwrapped.hull.position[0] ])
|
411 |
+
dataarr.append(stats)
|
412 |
+
|
413 |
+
done = '| Achieve ' if env.unwrapped.hull.position[0] >= END_POINT else '| -----'
|
414 |
+
print('Episode:', i_episode,
|
415 |
+
done,
|
416 |
+
'| Running_r: %i' % int(running_r),
|
417 |
+
'| Epi_r: %.2f' % ep_r,
|
418 |
+
'| Exploration: %.3f' % var,
|
419 |
+
'| Pos: %.i' % int(env.unwrapped.hull.position[0]),
|
420 |
+
'| LR_A: %.6f' % sess.run(LR_A),
|
421 |
+
'| LR_C: %.6f' % sess.run(LR_C),
|
422 |
+
)
|
423 |
+
break
|
424 |
+
|
425 |
+
s = s_
|
426 |
+
sess.run(INCREASE_GS)
|
427 |
+
|
428 |
+
|
429 |
+
elapsed_time = time.process_time() - st
|
430 |
+
|
431 |
+
data_dir = "time_" + str(fold) + ".npy"
|
432 |
+
np.save(data_dir, np.array([elapsed_time]))
|
433 |
+
|
434 |
+
|
435 |
+
dataarr = np.asarray(dataarr)
|
436 |
+
data_dir = "stats_" + str(fold) + ".npy"
|
437 |
+
np.save(data_dir, dataarr)
|
tensorflow/mnist_UAF.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
print("TensorFlow version:", tf.__version__)
|
3 |
+
|
4 |
+
from tensorflow.keras.layers import Dense, Flatten, Conv2D
|
5 |
+
from tensorflow.keras import Model
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
mnist = tf.keras.datasets.mnist
|
10 |
+
|
11 |
+
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
12 |
+
x_train, x_test = x_train / 255.0, x_test / 255.0
|
13 |
+
|
14 |
+
# Add a channels dimension
|
15 |
+
x_train = x_train[..., tf.newaxis].astype("float32")
|
16 |
+
x_test = x_test[..., tf.newaxis].astype("float32")
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
train_ds = tf.data.Dataset.from_tensor_slices(
|
21 |
+
(x_train, y_train)).shuffle(10000).batch(32)
|
22 |
+
|
23 |
+
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
class UAF(Model):
|
30 |
+
def __init__(self):
|
31 |
+
super(UAF, self).__init__()
|
32 |
+
self.A = tf.Variable(10.0, trainable=True)
|
33 |
+
self.B = tf.Variable(0.0000012, trainable=True)
|
34 |
+
self.C = tf.Variable(0.0000001, trainable=True)
|
35 |
+
self.D = tf.Variable(9.0, trainable=True)
|
36 |
+
self.E = tf.Variable(0.00000102, trainable=True)
|
37 |
+
|
38 |
+
|
39 |
+
def call(self, input):
|
40 |
+
P1 = (self.A*(input+self.B)) + (self.C * tf.math.square(input))
|
41 |
+
P2 = (self.D*(input-self.B))
|
42 |
+
|
43 |
+
P3 = tf.nn.relu(P1) + tf.math.log1p(tf.math.exp(-tf.math.abs(P1)))
|
44 |
+
P4 = tf.nn.relu(P2) + tf.math.log1p(tf.math.exp(-tf.math.abs(P2)))
|
45 |
+
|
46 |
+
return P3 - P4 + self.E
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
class MyModel(Model):
|
51 |
+
def __init__(self):
|
52 |
+
super(MyModel, self).__init__()
|
53 |
+
self.conv1 = Conv2D(32, 3, activation=None)
|
54 |
+
self.flatten = Flatten()
|
55 |
+
self.d1 = Dense(128, activation=None)
|
56 |
+
self.d2 = Dense(10, activation=None)
|
57 |
+
self.act0 = UAF()
|
58 |
+
self.act1 = UAF()
|
59 |
+
self.act2 = UAF()
|
60 |
+
|
61 |
+
|
62 |
+
def call(self, x):
|
63 |
+
x = self.conv1(x)
|
64 |
+
x = self.act0(x)
|
65 |
+
x = self.flatten(x)
|
66 |
+
x = self.d1(x)
|
67 |
+
x = self.act1(x)
|
68 |
+
x = self.d2(x)
|
69 |
+
return self.act2(x)
|
70 |
+
|
71 |
+
# Create an instance of the model
|
72 |
+
model = MyModel()
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
|
79 |
+
|
80 |
+
optimizer = tf.keras.optimizers.Adam()
|
81 |
+
|
82 |
+
|
83 |
+
|
84 |
+
train_loss = tf.keras.metrics.Mean(name='train_loss')
|
85 |
+
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
|
86 |
+
|
87 |
+
test_loss = tf.keras.metrics.Mean(name='test_loss')
|
88 |
+
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
@tf.function
|
94 |
+
def train_step(images, labels):
|
95 |
+
with tf.GradientTape() as tape:
|
96 |
+
# training=True is only needed if there are layers with different
|
97 |
+
# behavior during training versus inference (e.g. Dropout).
|
98 |
+
predictions = model(images, training=True)
|
99 |
+
loss = loss_object(labels, predictions)
|
100 |
+
gradients = tape.gradient(loss, model.trainable_variables)
|
101 |
+
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
|
102 |
+
|
103 |
+
train_loss(loss)
|
104 |
+
train_accuracy(labels, predictions)
|
105 |
+
|
106 |
+
|
107 |
+
@tf.function
|
108 |
+
def test_step(images, labels):
|
109 |
+
# training=False is only needed if there are layers with different
|
110 |
+
# behavior during training versus inference (e.g. Dropout).
|
111 |
+
predictions = model(images, training=False)
|
112 |
+
t_loss = loss_object(labels, predictions)
|
113 |
+
|
114 |
+
test_loss(t_loss)
|
115 |
+
test_accuracy(labels, predictions)
|
116 |
+
|
117 |
+
|
118 |
+
EPOCHS = 20
|
119 |
+
|
120 |
+
for epoch in range(EPOCHS):
|
121 |
+
# Reset the metrics at the start of the next epoch
|
122 |
+
train_loss.reset_states()
|
123 |
+
train_accuracy.reset_states()
|
124 |
+
test_loss.reset_states()
|
125 |
+
test_accuracy.reset_states()
|
126 |
+
|
127 |
+
for images, labels in train_ds:
|
128 |
+
train_step(images, labels)
|
129 |
+
|
130 |
+
for test_images, test_labels in test_ds:
|
131 |
+
test_step(test_images, test_labels)
|
132 |
+
|
133 |
+
print(
|
134 |
+
f'Epoch {epoch + 1}, '
|
135 |
+
f'Loss: {train_loss.result()}, '
|
136 |
+
f'Accuracy: {train_accuracy.result() * 100}, '
|
137 |
+
f'Test Loss: {test_loss.result()}, '
|
138 |
+
f'Test Accuracy: {test_accuracy.result() * 100}'
|
139 |
+
)
|
140 |
+
|