From 7f1c980bd3b36dddf786717151f85fa4443d9a97 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 06:50:03 +0000 Subject: [PATCH 01/38] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20TensorFlow/?= =?UTF-8?q?built-in/recommendation/DIEN=5FID0109=5Ffor=5FTensorFlow=20?= =?UTF-8?q?=E4=B8=BA=20TensorFlow/built-in/recommendation/DIEN=5Fsmall=5Fk?= =?UTF-8?q?ernel=5FID0109=5Ffor=5FTensorFlow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LICENSE | 0 .../README.md | 0 .../modelzoo_level.txt | 0 .../prepare_data.sh | 0 .../requirements.txt | 0 .../run.sh | 0 .../script/Dice.py | 0 .../script/data_iterator.py | 0 .../script/generate_voc.py | 0 .../script/local_aggretor.py | 0 .../script/model.py | 0 .../script/process_data.py | 0 .../script/rnn.py | 0 .../script/shuffle.py | 0 .../script/split_by_user.py | 0 .../script/train.py | 0 .../script/utils.py | 0 .../test/train_full_1p.sh | 0 .../test/train_performance_1p.sh | 0 19 files changed, 0 insertions(+), 0 deletions(-) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/LICENSE (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/README.md (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/modelzoo_level.txt (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/prepare_data.sh (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/requirements.txt (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/run.sh (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/script/Dice.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/script/data_iterator.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/script/generate_voc.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/script/local_aggretor.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/script/model.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/script/process_data.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/script/rnn.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/script/shuffle.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/script/split_by_user.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/script/train.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/script/utils.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/test/train_full_1p.sh (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_small_kernel_ID0109_for_TensorFlow}/test/train_performance_1p.sh (100%) diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/LICENSE b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/LICENSE similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/LICENSE rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/LICENSE diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/README.md b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/README.md similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/README.md rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/README.md diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/modelzoo_level.txt b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/modelzoo_level.txt similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/modelzoo_level.txt rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/modelzoo_level.txt diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/prepare_data.sh b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/prepare_data.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/prepare_data.sh rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/prepare_data.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/requirements.txt b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/requirements.txt similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/requirements.txt rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/requirements.txt diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/run.sh b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/run.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/run.sh rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/run.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/Dice.py b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/Dice.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/Dice.py rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/Dice.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/data_iterator.py b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/data_iterator.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/data_iterator.py rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/data_iterator.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/generate_voc.py b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/generate_voc.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/generate_voc.py rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/generate_voc.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/local_aggretor.py b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/local_aggretor.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/local_aggretor.py rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/local_aggretor.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model.py b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/model.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model.py rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/model.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/process_data.py b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/process_data.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/process_data.py rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/process_data.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/rnn.py b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/rnn.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/rnn.py rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/rnn.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/shuffle.py b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/shuffle.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/shuffle.py rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/shuffle.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/split_by_user.py b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/split_by_user.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/split_by_user.py rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/split_by_user.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/train.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/train.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/utils.py b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/utils.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/utils.py rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/utils.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_full_1p.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_full_1p.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_performance_1p.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh rename to TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_performance_1p.sh -- Gitee From df9ef5bac975ebe0e1d8ea3fab70471cb0f219a8 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 06:50:33 +0000 Subject: [PATCH 02/38] update DIEN_small_kernel_ID0109_for_TensorFlow/test/train_full_1p.sh. --- .../test/train_full_1p.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_full_1p.sh b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_full_1p.sh index 510d4fce0..63423cabf 100644 --- a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_full_1p.sh +++ b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_full_1p.sh @@ -15,7 +15,7 @@ data_path="" #基础参数,需要模型审视修改 #网络名称,同目录名称 -Network="DIEN_ID0109_for_TensorFlow" +Network="DIEN_small_kernel_ID0109_for_TensorFlow" #训练epoch train_epochs=25400 #训练batch_size -- Gitee From 9f9f2c53ccca23ae28c28123e2a4f8a7239f25ac Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 06:51:07 +0000 Subject: [PATCH 03/38] update DIEN_small_kernel_ID0109_for_TensorFlow/test/train_performance_1p.sh. --- .../test/train_performance_1p.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_performance_1p.sh b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_performance_1p.sh index 0772a23aa..39017e1b9 100644 --- a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_performance_1p.sh +++ b/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_performance_1p.sh @@ -15,7 +15,7 @@ data_path="" #基础参数,需要模型审视修改 #网络名称,同目录名称 -Network="DIEN_ID0109_for_TensorFlow" +Network="DIEN_small_kernel_ID0109_for_TensorFlow" #训练epoch train_epochs=500 #训练batch_size -- Gitee From cb015944264a4a33bb02b0090ce67dc61a599be6 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 06:59:05 +0000 Subject: [PATCH 04/38] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20DIEN=5FID0109=5Ffor?= =?UTF-8?q?=5FTensorFlow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../built-in/recommendation/DIEN_ID0109_for_TensorFlow/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/.keep diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/.keep b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/.keep new file mode 100644 index 000000000..e69de29bb -- Gitee From ac86a8e968fc0c93e1e92df3fb6944b803d51e71 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:00:44 +0000 Subject: [PATCH 05/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/LICENSE. --- .../DIEN_ID0109_for_TensorFlow/LICENSE | 284 ++++++++++++++++++ 1 file changed, 284 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/LICENSE diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/LICENSE b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/LICENSE new file mode 100644 index 000000000..0b7e6dfc9 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/LICENSE @@ -0,0 +1,284 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +------------------ +Files: third_party/compute_library/... + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +------------------ +Files: ACKNOWLEDGEMENTS +LICENSE + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +------------------ +Files: third_party/hexagon + +Copyright (c) 2016-2019, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted (subject to the limitations in the +disclaimer below) provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE +GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT +HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file -- Gitee From b55ab1239a875cf472c8cda11a9855fb02976076 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:01:30 +0000 Subject: [PATCH 06/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/README.md. --- .../DIEN_ID0109_for_TensorFlow/README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/README.md diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/README.md b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/README.md new file mode 100644 index 000000000..b3709a264 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/README.md @@ -0,0 +1,36 @@ +Deep Interest Evolution Network for Click-Through Rate Prediction +https://arxiv.org/abs/1809.03672 + +prepare data +method 1 +You can get the data from amazon website and process it using the script + +sh prepare_data.sh +method 2 (recommended) +Because getting and processing the data is time consuming,so we had processed it and upload it for you. You can unzip it to use directly. + +tar -jxvf data.tar.gz +mv data/* . +tar -jxvf data1.tar.gz +mv data1/* . +tar -jxvf data2.tar.gz +mv data2/* . +When you see the files below, you can do the next work. + +cat_voc.pkl +mid_voc.pkl +uid_voc.pkl +local_train_splitByUser +local_test_splitByUser +reviews-info +item-info +train model +python train.py train [model name] +The model blelow had been supported: + +DNN +PNN +Wide (Wide&Deep NN) +DIN (https://arxiv.org/abs/1706.06978) +DIEN (https://arxiv.org/pdf/1809.03672.pdf) +Note: we use tensorflow 1.4. \ No newline at end of file -- Gitee From 9d4ae78a330250cf6406dfc712cc64ad116e5a77 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:02:28 +0000 Subject: [PATCH 07/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/modelzoo_level.txt. --- .../DIEN_ID0109_for_TensorFlow/modelzoo_level.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/modelzoo_level.txt diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/modelzoo_level.txt b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/modelzoo_level.txt new file mode 100644 index 000000000..ac1435c99 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/modelzoo_level.txt @@ -0,0 +1,3 @@ +FuncStatus:OK +PerfStatus:OK +PrecisionStatus:NOK \ No newline at end of file -- Gitee From ef5ee6a0cda415648dee6f7ea38c30b7de9a9574 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:03:04 +0000 Subject: [PATCH 08/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/requirements.txt. --- .../recommendation/DIEN_ID0109_for_TensorFlow/requirements.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/requirements.txt diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/requirements.txt b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/requirements.txt new file mode 100644 index 000000000..a927d86a0 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/requirements.txt @@ -0,0 +1,2 @@ +tensorflow +python \ No newline at end of file -- Gitee From b5f385686021128494e65bbed1b2cb29d30f2cee Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:03:12 +0000 Subject: [PATCH 09/38] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/.keep diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/.keep b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/.keep new file mode 100644 index 000000000..e69de29bb -- Gitee From ebaa60adbbef719f54a5b669f334e41cda3c3bf9 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:04:33 +0000 Subject: [PATCH 10/38] add DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh. --- .../test/train_performance_1p.sh | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh new file mode 100644 index 000000000..52792b570 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh @@ -0,0 +1,170 @@ +#!/bin/bash + +#当前路径,不需要修改 +cur_path=`pwd` + +#集合通信参数,不需要修改 + +export RANK_SIZE=1 +export JOB_ID=10087 +RANK_ID_START=0 +RankSize=1 +# 数据集路径,保持为空,不需要修改 +data_path="" + +#基础参数,需要模型审视修改 +#网络名称,同目录名称 +Network="DIEN_ID0109_for_TensorFlow" +#训练epoch +train_epochs=500 +#训练batch_size +batch_size=128 +#训练step +train_steps=500 +#学习率 +learning_rate= + +#维测参数,precision_mode需要模型审视修改 +precision_mode="allow_mix_precision" +#维持参数,以下不需要修改 +over_dump=False +data_dump_flag=False +data_dump_step="10" +profiling=False + +# 帮助信息,不需要修改 +if [[ $1 == --help || $1 == -h ]];then + echo"usage:./train_performance_1P.sh " + echo " " + echo "parameter explain: + --precision_mode precision mode(allow_fp32_to_fp16/force_fp16/must_keep_origin_dtype/allow_mix_precision) + --over_dump if or not over detection, default is False + --data_dump_flag data dump flag, default is False + --data_dump_step data dump step, default is 10 + --profiling if or not profiling for performance debug, default is False + --data_path source data of training + -h/--help show help message + " + exit 1 +fi + +#参数校验,不需要修改 +for para in $* +do + if [[ $para == --precision_mode* ]];then + precision_mode=`echo ${para#*=}` + elif [[ $para == --over_dump* ]];then + over_dump=`echo ${para#*=}` + over_dump_path=${cur_path}/output/overflow_dump + mkdir -p ${over_dump_path} + elif [[ $para == --data_dump_flag* ]];then + data_dump_flag=`echo ${para#*=}` + data_dump_path=${cur_path}/output/data_dump + mkdir -p ${data_dump_path} + elif [[ $para == --data_dump_step* ]];then + data_dump_step=`echo ${para#*=}` + elif [[ $para == --profiling* ]];then + profiling=`echo ${para#*=}` + profiling_dump_path=${cur_path}/output/profiling + mkdir -p ${profiling_dump_path} + elif [[ $para == --data_path* ]];then + data_path=`echo ${para#*=}` + fi +done + +#校验是否传入data_path,不需要修改 +if [[ $data_path == "" ]];then + echo "[Error] para \"data_path\" must be confing" + exit 1 +fi + +cd $cur_path/.. +cp -r $data_path/* ./ +tar -jxvf data.tar.gz +mv data/* . +tar -jxvf data1.tar.gz +mv data1/* . +tar -jxvf data2.tar.gz +mv data2/* . +rm -rf data* +mkdir dnn_save_path +mkdir dnn_best_model +cd $cur_path/../script +sed -i "s|pass|break|g" train.py + + +#训练开始时间,不需要修改 +start_time=$(date +%s) + +#进入训练脚本目录,需要模型审视修改 +cd $cur_path/.. + +for((RANK_ID=$RANK_ID_START;RANK_ID<$((RANK_SIZE+RANK_ID_START));RANK_ID++)); +do + #设置环境变量,不需要修改 + echo "Device ID: $ASCEND_DEVICE_ID" + export RANK_ID=$RANK_ID + + #创建DeviceID输出目录,不需要修改 + if [ -d ${cur_path}/output/${ASCEND_DEVICE_ID} ];then + rm -rf ${cur_path}/output/${ASCEND_DEVICE_ID} + mkdir -p ${cur_path}/output/$ASCEND_DEVICE_ID/ckpt + else + mkdir -p ${cur_path}/output/$ASCEND_DEVICE_ID/ckpt + fi + + #执行训练脚本,以下传参不需要修改,其他需要模型审视修改 + #--data_dir, --model_dir, --precision_mode, --over_dump, --over_dump_path,--data_dump_flag,--data_dump_step,--data_dump_path,--profiling,--profiling_dump_path + nohup python3 script/train.py train DIEN > ${cur_path}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 & +done +wait + +#训练结束时间,不需要修改 +end_time=$(date +%s) +e2e_time=$(( $end_time - $start_time )) +cd $cur_path/../script +sed -i "s|break|pass|g" train.py + +#结果打印,不需要修改 +echo "------------------ Final result ------------------" +# #输出性能FPS,需要模型审视修改 +Time=`grep perf $cur_path/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log|awk 'END {print $14}'` +FPS=`awk 'BEGIN{printf "%.2f\n", '${batch_size}'/'${Time}'}'` +#打印,不需要修改 +echo "Final Performance item/sec : $FPS" + +#输出训练精度,需要模型审视修改 +train_accuracy=`grep "train_accuracy" ${cur_path}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log|awk 'END {print $8}'` +#打印,不需要修改 +echo "Final Train Accuracy : ${train_accuracy}" +echo "E2E Training Duration sec : $e2e_time" + +#性能看护结果汇总 +#训练用例信息,不需要修改 +BatchSize=${batch_size} +DeviceType=`uname -m` +CaseName=${Network}_bs${BatchSize}_${RANK_SIZE}'p'_'perf' + +##获取性能数据,不需要修改 +#吞吐量 +ActualFPS=${FPS} +#单迭代训练时长 +TrainingTime=`awk 'BEGIN{printf "%.2f\n",'${BatchSize}'/'${FPS}'}'` + +#从train_$ASCEND_DEVICE_ID.log提取Loss到train_${CaseName}_loss.txt中,需要根据模型审视 +grep train_loss $cur_path/output/$ASCEND_DEVICE_ID/train_$ASCEND_DEVICE_ID.log|awk '{print $5}' > $cur_path/output/$ASCEND_DEVICE_ID/train_${CaseName}_loss.txt + +#最后一个迭代loss值,不需要修改 +ActualLoss=`awk 'END {print}' $cur_path/output/$ASCEND_DEVICE_ID/train_${CaseName}_loss.txt` + +#关键信息打印到${CaseName}.log中,不需要修改 +echo "Network = ${Network}" > $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "RankSize = ${RANK_SIZE}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "BatchSize = ${BatchSize}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "DeviceType = ${DeviceType}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "CaseName = ${CaseName}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "ActualFPS = ${ActualFPS}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "TrainAccuracy = ${train_accuracy}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "TrainingTime = ${TrainingTime}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "ActualLoss = ${ActualLoss}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "E2ETrainingTime = ${e2e_time}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log \ No newline at end of file -- Gitee From 0629272da4bb27ae5d2a1784fed2c99c8c4661d2 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:05:33 +0000 Subject: [PATCH 11/38] add DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh. --- .../test/train_full_1p.sh | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh new file mode 100644 index 000000000..ba8673a71 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh @@ -0,0 +1,167 @@ +#!/bin/bash + +#当前路径,不需要修改 +cur_path=`pwd` + +#集合通信参数,不需要修改 + +export RANK_SIZE=1 +export JOB_ID=10087 +RANK_ID_START=0 +RankSize=1 +# 数据集路径,保持为空,不需要修改 +data_path="" +#export ASCEND_SLOG_PRINT_TO_STDOUT=1 + +#基础参数,需要模型审视修改 +#网络名称,同目录名称 +Network="DIEN_ID0109_for_TensorFlow" +#训练epoch +train_epochs=25400 +#训练batch_size +batch_size=128 +#训练step +train_steps=25400 +#学习率 +learning_rate= + +#维测参数,precision_mode需要模型审视修改 +precision_mode="allow_mix_precision" +#维持参数,以下不需要修改 +over_dump=False +data_dump_flag=False +data_dump_step="10" +profiling=False + +# 帮助信息,不需要修改 +if [[ $1 == --help || $1 == -h ]];then + echo"usage:./train_full_1P.sh " + echo " " + echo "parameter explain: + --precision_mode precision mode(allow_fp32_to_fp16/force_fp16/must_keep_origin_dtype/allow_mix_precision) + --over_dump if or not over detection, default is False + --data_dump_flag data dump flag, default is False + --data_dump_step data dump step, default is 10 + --profiling if or not profiling for performance debug, default is False + --data_path source data of training + -h/--help show help message + " + exit 1 +fi + +#参数校验,不需要修改 +for para in $* +do + if [[ $para == --precision_mode* ]];then + precision_mode=`echo ${para#*=}` + elif [[ $para == --over_dump* ]];then + over_dump=`echo ${para#*=}` + over_dump_path=${cur_path}/output/overflow_dump + mkdir -p ${over_dump_path} + elif [[ $para == --data_dump_flag* ]];then + data_dump_flag=`echo ${para#*=}` + data_dump_path=${cur_path}/output/data_dump + mkdir -p ${data_dump_path} + elif [[ $para == --data_dump_step* ]];then + data_dump_step=`echo ${para#*=}` + elif [[ $para == --profiling* ]];then + profiling=`echo ${para#*=}` + profiling_dump_path=${cur_path}/output/profiling + mkdir -p ${profiling_dump_path} + elif [[ $para == --data_path* ]];then + data_path=`echo ${para#*=}` + fi +done + +#校验是否传入data_path,不需要修改 +if [[ $data_path == "" ]];then + echo "[Error] para \"data_path\" must be confing" + exit 1 +fi + +cd $cur_path/.. +cp -r $data_path/* ./ +tar -jxvf data.tar.gz +mv data/* . +tar -jxvf data1.tar.gz +mv data1/* . +tar -jxvf data2.tar.gz +mv data2/* . +rm -rf data* + + +#训练开始时间,不需要修改 +start_time=$(date +%s) + +#进入训练脚本目录,需要模型审视修改 +cd $cur_path/.. + +for((RANK_ID=$RANK_ID_START;RANK_ID<$((RANK_SIZE+RANK_ID_START));RANK_ID++)); +do + #设置环境变量,不需要修改 + echo "Device ID: $ASCEND_DEVICE_ID" + export RANK_ID=$RANK_ID + + #创建DeviceID输出目录,不需要修改 + if [ -d ${cur_path}/output/${ASCEND_DEVICE_ID} ];then + rm -rf ${cur_path}/output/${ASCEND_DEVICE_ID} + mkdir -p ${cur_path}/output/$ASCEND_DEVICE_ID/ckpt + else + mkdir -p ${cur_path}/output/$ASCEND_DEVICE_ID/ckpt + fi + + #执行训练脚本,以下传参不需要修改,其他需要模型审视修改 + #--data_dir, --model_dir, --precision_mode, --over_dump, --over_dump_path,--data_dump_flag,--data_dump_step,--data_dump_path,--profiling,--profiling_dump_path + nohup python3 script/train.py train DIEN > ${cur_path}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 & +done +wait + +#训练结束时间,不需要修改 +end_time=$(date +%s) +e2e_time=$(( $end_time - $start_time )) + + + +#结果打印,不需要修改 +echo "------------------ Final result ------------------" +# #输出性能FPS,需要模型审视修改 +Time=`grep perf $cur_path/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log|awk 'END {print $14}'` +FPS=`awk 'BEGIN{printf "%.2f\n", '${batch_size}'/'${Time}'}'` +#打印,不需要修改 +echo "Final Performance item/sec : $FPS" + +#输出训练精度,需要模型审视修改 +train_accuracy=`grep "train_accuracy" ${cur_path}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log|awk 'END {print $8}'` +#打印,不需要修改 +echo "Final Train Accuracy : ${train_accuracy}" +echo "E2E Training Duration sec : $e2e_time" + +#性能看护结果汇总 +#训练用例信息,不需要修改 +BatchSize=${batch_size} +DeviceType=`uname -m` +CaseName=${Network}_bs${BatchSize}_${RANK_SIZE}'p'_'acc' + +##获取性能数据,不需要修改 +#吞吐量 +ActualFPS=${FPS} +#单迭代训练时长 +TrainingTime=`awk 'BEGIN{printf "%.2f\n",'${BatchSize}'/'${FPS}'}'` + +#从train_$ASCEND_DEVICE_ID.log提取Loss到train_${CaseName}_loss.txt中,需要根据模型审视 +grep train_loss $cur_path/output/$ASCEND_DEVICE_ID/train_$ASCEND_DEVICE_ID.log|awk '{print $5}' > $cur_path/output/$ASCEND_DEVICE_ID/train_${CaseName}_loss.txt + +#最后一个迭代loss值,不需要修改 +ActualLoss=`awk 'END {print}' $cur_path/output/$ASCEND_DEVICE_ID/train_${CaseName}_loss.txt` + +#关键信息打印到${CaseName}.log中,不需要修改 +echo "Network = ${Network}" > $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "RankSize = ${RANK_SIZE}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "BatchSize = ${BatchSize}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "DeviceType = ${DeviceType}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "CaseName = ${CaseName}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "ActualFPS = ${ActualFPS}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "TrainAccuracy = ${train_accuracy}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "TrainingTime = ${TrainingTime}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "ActualLoss = ${ActualLoss}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "E2ETrainingTime = ${e2e_time}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -- Gitee From 00e439161d22c3a00f5155bb91abbdd378310460 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:06:08 +0000 Subject: [PATCH 12/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/ prepare_data.sh. --- .../DIEN_ID0109_for_TensorFlow/ prepare_data.sh | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/ prepare_data.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/ prepare_data.sh b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/ prepare_data.sh new file mode 100644 index 000000000..d1d2c1fb1 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/ prepare_data.sh @@ -0,0 +1,9 @@ +export PATH="~/anaconda4/bin:$PATH" +wget http://snap.stanford.edu/data/amazon/productGraph/categoryFiles/reviews_Books.json.gz +wget http://snap.stanford.edu/data/amazon/productGraph/categoryFiles/meta_Books.json.gz +gunzip reviews_Books.json.gz +gunzip meta_Books.json.gz +python script/process_data.py meta_Books.json reviews_Books_5.json +python script/local_aggretor.py +python script/split_by_user.py +python script/generate_voc.py \ No newline at end of file -- Gitee From 32c2cbcff36a3775b70915395df3105d52b2dda1 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:07:09 +0000 Subject: [PATCH 13/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/run.sh. --- .../built-in/recommendation/DIEN_ID0109_for_TensorFlow/run.sh | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/run.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/run.sh b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/run.sh new file mode 100644 index 000000000..5d07d3cce --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/run.sh @@ -0,0 +1,3 @@ +mkdir dnn_save_path +mkdir dnn_best_model +CUDA_VISIBLE_DEVICES=0 /usr/bin/python2.7 script/train.py train DIEN >train_dein2.log 2>&1 & \ No newline at end of file -- Gitee From 8b98156b54c02d7e41391b1dc3657f7df83fe272 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:07:26 +0000 Subject: [PATCH 14/38] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../recommendation/DIEN_ID0109_for_TensorFlow/script/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/.keep diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/.keep b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/.keep new file mode 100644 index 000000000..e69de29bb -- Gitee From d0b50b8b31059bda766008408723bc47cffb7059 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:08:21 +0000 Subject: [PATCH 15/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/Dice.py. --- .../DIEN_ID0109_for_TensorFlow/script/Dice.py | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/Dice.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/Dice.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/Dice.py new file mode 100644 index 000000000..851e7c2c0 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/Dice.py @@ -0,0 +1,65 @@ +# +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from npu_bridge.npu_init import * +import tensorflow as tf + +def dice(_x, axis=-1, epsilon=0.000000001, name=''): + with tf.variable_scope(name, reuse=tf.AUTO_REUSE): + alphas = tf.get_variable('alpha'+name, _x.get_shape()[-1], + initializer=tf.constant_initializer(0.0), + dtype=tf.float32) + input_shape = list(_x.get_shape()) + + reduction_axes = list(range(len(input_shape))) + del reduction_axes[axis] + broadcast_shape = [1] * len(input_shape) + broadcast_shape[axis] = input_shape[axis] + + # case: train mode (uses stats of the current batch) + mean = tf.reduce_mean(_x, axis=reduction_axes) + brodcast_mean = tf.reshape(mean, broadcast_shape) + std = tf.reduce_mean(tf.square(_x - brodcast_mean) + epsilon, axis=reduction_axes) + std = tf.sqrt(std) + brodcast_std = tf.reshape(std, broadcast_shape) + x_normed = (_x - brodcast_mean) / (brodcast_std + epsilon) + # x_normed = tf.layers.batch_normalization(_x, center=False, scale=False) + x_p = tf.sigmoid(x_normed) + + + return alphas * (1.0 - x_p) * _x + x_p * _x + +def parametric_relu(_x): + alphas = tf.get_variable('alpha', _x.get_shape()[-1], + initializer=tf.constant_initializer(0.0), + dtype=tf.float32) + pos = tf.nn.relu(_x) + neg = alphas * (_x - abs(_x)) * 0.5 + + return pos + neg -- Gitee From 99f777911dbc90d799c6d3a3ec4bea13ec3269c5 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:09:05 +0000 Subject: [PATCH 16/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/data_iterator.py. --- .../script/data_iterator.py | 249 ++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/data_iterator.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/data_iterator.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/data_iterator.py new file mode 100644 index 000000000..a7af91760 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/data_iterator.py @@ -0,0 +1,249 @@ +# +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from npu_bridge.npu_init import * +import numpy +import json +import pickle as pkl +import random + +import gzip + +import shuffle + +def unicode_to_utf8(d): + return dict((key.encode("UTF-8"), value) for (key,value) in d.items()) + +def load_dict(filename): + try: + with open(filename, 'rb') as f: + return unicode_to_utf8(json.load(f)) + except: + with open(filename, 'rb') as f: + return pkl.load(f) + + +def fopen(filename, mode='r'): + if filename.endswith('.gz'): + return gzip.open(filename, mode) + return open(filename, mode) + + +class DataIterator: + + def __init__(self, source, + uid_voc, + mid_voc, + cat_voc, + batch_size=128, + maxlen=100, + skip_empty=False, + shuffle_each_epoch=False, + sort_by_length=True, + max_batch_size=20, + minlen=None): + if shuffle_each_epoch: + self.source_orig = source + self.source = shuffle.main(self.source_orig, temporary=True) + else: + self.source = fopen(source, 'r') + self.source_dicts = [] + for source_dict in [uid_voc, mid_voc, cat_voc]: + self.source_dicts.append(load_dict(source_dict)) + + f_meta = open("item-info", "r", encoding='utf-8') + meta_map = {} + for line in f_meta: + arr = line.strip().split("\t") + if arr[0] not in meta_map: + meta_map[arr[0]] = arr[1] + self.meta_id_map ={} + for key in meta_map: + val = meta_map[key] + if key in self.source_dicts[1]: + mid_idx = self.source_dicts[1][key] + else: + mid_idx = 0 + if val in self.source_dicts[2]: + cat_idx = self.source_dicts[2][val] + else: + cat_idx = 0 + self.meta_id_map[mid_idx] = cat_idx + + f_review = open("reviews-info", "r", encoding='utf-8') + self.mid_list_for_random = [] + for line in f_review: + arr = line.strip().split("\t") + tmp_idx = 0 + if arr[1] in self.source_dicts[1]: + tmp_idx = self.source_dicts[1][arr[1]] + self.mid_list_for_random.append(tmp_idx) + + self.batch_size = batch_size + self.maxlen = maxlen + self.minlen = minlen + self.skip_empty = skip_empty + + self.n_uid = len(self.source_dicts[0]) + self.n_mid = len(self.source_dicts[1]) + self.n_cat = len(self.source_dicts[2]) + + self.shuffle = shuffle_each_epoch + self.sort_by_length = sort_by_length + + self.source_buffer = [] + self.k = batch_size * max_batch_size + + self.end_of_data = False + + def get_n(self): + return self.n_uid, self.n_mid, self.n_cat + + def __iter__(self): + return self + + def reset(self): + if self.shuffle: + self.source= shuffle.main(self.source_orig, temporary=True) + else: + self.source.seek(0) + + def __next__(self): + if self.end_of_data: + self.end_of_data = False + self.reset() + raise StopIteration + + source = [] + target = [] + + if len(self.source_buffer) == 0: + for k_ in range(self.k): + ss = self.source.readline() + if ss == "": + break + self.source_buffer.append(ss.strip("\n").split("\t")) + + # sort by history behavior length + if self.sort_by_length: + his_length = numpy.array([len(s[4].split("")) for s in self.source_buffer]) + tidx = his_length.argsort() + + _sbuf = [self.source_buffer[i] for i in tidx] + self.source_buffer = _sbuf + else: + self.source_buffer.reverse() + + if len(self.source_buffer) == 0: + self.end_of_data = False + self.reset() + raise StopIteration + + try: + + # actual work here + while True: + + # read from source file and map to word index + try: + ss = self.source_buffer.pop() + except IndexError: + break + + uid = self.source_dicts[0][ss[1]] if ss[1] in self.source_dicts[0] else 0 + mid = self.source_dicts[1][ss[2]] if ss[2] in self.source_dicts[1] else 0 + cat = self.source_dicts[2][ss[3]] if ss[3] in self.source_dicts[2] else 0 + tmp = [] + for fea in ss[4].split(""): + m = self.source_dicts[1][fea] if fea in self.source_dicts[1] else 0 + tmp.append(m) + mid_list = tmp + + tmp1 = [] + for fea in ss[5].split(""): + c = self.source_dicts[2][fea] if fea in self.source_dicts[2] else 0 + tmp1.append(c) + cat_list = tmp1 + + # read from source file and map to word index + + #if len(mid_list) > self.maxlen: + # continue + if self.minlen != None: + if len(mid_list) <= self.minlen: + continue + if self.skip_empty and (not mid_list): + continue + + noclk_mid_list = [] + noclk_cat_list = [] + for pos_mid in mid_list: + noclk_tmp_mid = [] + noclk_tmp_cat = [] + noclk_index = 0 + while True: + noclk_mid_indx = random.randint(0, len(self.mid_list_for_random)-1) + noclk_mid = self.mid_list_for_random[noclk_mid_indx] + if noclk_mid == pos_mid: + continue + noclk_tmp_mid.append(noclk_mid) + noclk_tmp_cat.append(self.meta_id_map[noclk_mid]) + noclk_index += 1 + if noclk_index >= 5: + break + noclk_mid_list.append(noclk_tmp_mid) + noclk_cat_list.append(noclk_tmp_cat) + source.append([uid, mid, cat, mid_list, cat_list, noclk_mid_list, noclk_cat_list]) + target.append([float(ss[0]), 1-float(ss[0])]) + + if len(source) >= self.batch_size or len(target) >= self.batch_size: + break + except IOError: + self.end_of_data = True + + # all sentence pairs in maxibatch filtered out because of length + if len(source) < self.batch_size or len(target) < self.batch_size: + source, target = self.__next__() + + return source, target + +if __name__ == "__main__": + train_data = DataIterator("local_train_splitByUser",\ + "uid_voc.pkl",\ + "mid_voc.pkl",\ + "cat_voc.pkl",\ + 8192,\ + 100,\ + shuffle_each_epoch=False) + for epoch in range(3): + i = 0 + for source,target in train_data: + print(">>>>>>>>>>>>>>>>>>>>", i) + i += 1 + print("!!!!!!!!!!!!!!!!end") -- Gitee From d2b397d817bb4202b5d21b7121465fec6652b329 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:09:41 +0000 Subject: [PATCH 17/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/generate_voc.py. --- .../script/generate_voc.py | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/generate_voc.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/generate_voc.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/generate_voc.py new file mode 100644 index 000000000..a44015fbb --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/generate_voc.py @@ -0,0 +1,95 @@ +# +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from npu_bridge.npu_init import * +import cPickle + +f_train = open("local_train_splitByUser", "r") +uid_dict = {} +mid_dict = {} +cat_dict = {} + +iddd = 0 +for line in f_train: + arr = line.strip("\n").split("\t") + clk = arr[0] + uid = arr[1] + mid = arr[2] + cat = arr[3] + mid_list = arr[4] + cat_list = arr[5] + if uid not in uid_dict: + uid_dict[uid] = 0 + uid_dict[uid] += 1 + if mid not in mid_dict: + mid_dict[mid] = 0 + mid_dict[mid] += 1 + if cat not in cat_dict: + cat_dict[cat] = 0 + cat_dict[cat] += 1 + if len(mid_list) == 0: + continue + for m in mid_list.split(""): + if m not in mid_dict: + mid_dict[m] = 0 + mid_dict[m] += 1 + #print iddd + iddd+=1 + for c in cat_list.split(""): + if c not in cat_dict: + cat_dict[c] = 0 + cat_dict[c] += 1 + +sorted_uid_dict = sorted(uid_dict.iteritems(), key=lambda x:x[1], reverse=True) +sorted_mid_dict = sorted(mid_dict.iteritems(), key=lambda x:x[1], reverse=True) +sorted_cat_dict = sorted(cat_dict.iteritems(), key=lambda x:x[1], reverse=True) + +uid_voc = {} +index = 0 +for key, value in sorted_uid_dict: + uid_voc[key] = index + index += 1 + +mid_voc = {} +mid_voc["default_mid"] = 0 +index = 1 +for key, value in sorted_mid_dict: + mid_voc[key] = index + index += 1 + +cat_voc = {} +cat_voc["default_cat"] = 0 +index = 1 +for key, value in sorted_cat_dict: + cat_voc[key] = index + index += 1 + +cPickle.dump(uid_voc, open("uid_voc.pkl", "w")) +cPickle.dump(mid_voc, open("mid_voc.pkl", "w")) +cPickle.dump(cat_voc, open("cat_voc.pkl", "w")) -- Gitee From 5772cf88428fcfa1de0412a2381cd8a90f04b524 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:10:12 +0000 Subject: [PATCH 18/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/local_aggretor.py. --- .../script/local_aggretor.py | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/local_aggretor.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/local_aggretor.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/local_aggretor.py new file mode 100644 index 000000000..351a6a359 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/local_aggretor.py @@ -0,0 +1,75 @@ +# +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from npu_bridge.npu_init import * +import sys +import hashlib +import random + +fin = open("jointed-new-split-info", "r") +ftrain = open("local_train", "w") +ftest = open("local_test", "w") + +last_user = "0" +common_fea = "" +line_idx = 0 +for line in fin: + items = line.strip().split("\t") + ds = items[0] + clk = int(items[1]) + user = items[2] + movie_id = items[3] + dt = items[5] + cat1 = items[6] + + if ds=="20180118": + fo = ftrain + else: + fo = ftest + if user != last_user: + movie_id_list = [] + cate1_list = [] + #print >> fo, items[1] + "\t" + user + "\t" + movie_id + "\t" + cat1 +"\t" + "" + "\t" + "" + else: + history_clk_num = len(movie_id_list) + cat_str = "" + mid_str = "" + for c1 in cate1_list: + cat_str += c1 + "" + for mid in movie_id_list: + mid_str += mid + "" + if len(cat_str) > 0: cat_str = cat_str[:-1] + if len(mid_str) > 0: mid_str = mid_str[:-1] + if history_clk_num >= 1: # 8 is the average length of user behavior + print >> fo, items[1] + "\t" + user + "\t" + movie_id + "\t" + cat1 +"\t" + mid_str + "\t" + cat_str + last_user = user + if clk: + movie_id_list.append(movie_id) + cate1_list.append(cat1) + line_idx += 1 -- Gitee From 849ccb1884df686b1bf49b0f0d2ce4b34529ed7b Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:11:17 +0000 Subject: [PATCH 19/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model.py. --- .../script/model.py | 370 ++++++++++++++++++ 1 file changed, 370 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model.py new file mode 100644 index 000000000..5e02e2e3e --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model.py @@ -0,0 +1,370 @@ +from npu_bridge.estimator.npu.npu_dynamic_rnn import DynamicGRUV2 +from npu_bridge.estimator.npu.npu_dynamic_rnn import DynamicAUGRU +import tensorflow as tf + +# from tensorflow.python.ops.rnn_cell import LSTMCell +# from tensorflow.python.ops.rnn import bidirectional_dynamic_rnn as bi_rnn +#from tensorflow.python.ops.rnn import dynamic_rnn +from my_rnn import dynamic_rnn +# from tensorflow.contrib import rnn +from tensorflow.contrib.rnn import GRUCell +from utils import * +from Dice import dice + +class Model(object): + def __init__(self, batch_size, maxlen, n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE, use_negsampling = False): + with tf.name_scope('Inputs'): + self.mid_his_batch_ph = tf.placeholder(tf.int32, [batch_size, maxlen], name='mid_his_batch_ph') + self.cat_his_batch_ph = tf.placeholder(tf.int32, [batch_size, maxlen], name='cat_his_batch_ph') + self.uid_batch_ph = tf.placeholder(tf.int32, [batch_size, ], name='uid_batch_ph') + self.mid_batch_ph = tf.placeholder(tf.int32, [batch_size, ], name='mid_batch_ph') + self.cat_batch_ph = tf.placeholder(tf.int32, [batch_size, ], name='cat_batch_ph') + self.mask = tf.placeholder(tf.float32, [batch_size, maxlen], name='mask') + self.seq_len_ph = tf.placeholder(tf.int32, [batch_size], name='seq_len_ph') + self.target_ph = tf.placeholder(tf.float32, [batch_size, 2], name='target_ph') + self.lr = tf.placeholder(tf.float64, []) + self.use_negsampling =use_negsampling + if use_negsampling: + self.noclk_mid_batch_ph = tf.placeholder(tf.int32, [batch_size, maxlen, 5], name='noclk_mid_batch_ph') #generate 3 item IDs from negative sampling. + self.noclk_cat_batch_ph = tf.placeholder(tf.int32, [batch_size, maxlen, 5], name='noclk_cat_batch_ph') + + # Embedding layer + with tf.name_scope('Embedding_layer'): + self.uid_embeddings_var = tf.get_variable("uid_embedding_var", [n_uid, EMBEDDING_DIM]) + tf.summary.histogram('uid_embeddings_var', self.uid_embeddings_var) + self.uid_batch_embedded = embedding_lookup_npu(self.uid_embeddings_var, self.uid_batch_ph) + + self.mid_embeddings_var = tf.get_variable("mid_embedding_var", [n_mid, EMBEDDING_DIM]) + tf.summary.histogram('mid_embeddings_var', self.mid_embeddings_var) + self.mid_batch_embedded = embedding_lookup_npu(self.mid_embeddings_var, self.mid_batch_ph) + self.mid_his_batch_embedded = embedding_lookup_npu(self.mid_embeddings_var, self.mid_his_batch_ph) + if self.use_negsampling: + self.noclk_mid_his_batch_embedded = embedding_lookup_npu(self.mid_embeddings_var, self.noclk_mid_batch_ph) + + self.cat_embeddings_var = tf.get_variable("cat_embedding_var", [n_cat, EMBEDDING_DIM]) + tf.summary.histogram('cat_embeddings_var', self.cat_embeddings_var) + self.cat_batch_embedded = embedding_lookup_npu(self.cat_embeddings_var, self.cat_batch_ph) + self.cat_his_batch_embedded = embedding_lookup_npu(self.cat_embeddings_var, self.cat_his_batch_ph) + if self.use_negsampling: + self.noclk_cat_his_batch_embedded = embedding_lookup_npu(self.cat_embeddings_var, self.noclk_cat_batch_ph) + + self.item_eb = tf.concat([self.mid_batch_embedded, self.cat_batch_embedded], 1) + self.item_his_eb = tf.concat([self.mid_his_batch_embedded, self.cat_his_batch_embedded], 2) + self.item_his_eb_sum = tf.reduce_sum(self.item_his_eb, 1) + if self.use_negsampling: + self.noclk_item_his_eb = tf.concat( + [self.noclk_mid_his_batch_embedded[:, :, 0, :], self.noclk_cat_his_batch_embedded[:, :, 0, :]], -1)# 0 means only using the first negative item ID. 3 item IDs are inputed in the line 24. + self.noclk_item_his_eb = tf.reshape(self.noclk_item_his_eb, + [-1, tf.shape(self.noclk_mid_his_batch_embedded)[1], EMBEDDING_DIM * 2])# cat embedding 18 concate item embedding 18. + + self.noclk_his_eb = tf.concat([self.noclk_mid_his_batch_embedded, self.noclk_cat_his_batch_embedded], -1) + self.noclk_his_eb_sum_1 = tf.reduce_sum(self.noclk_his_eb, 2) + self.noclk_his_eb_sum = tf.reduce_sum(self.noclk_his_eb_sum_1, 1) + + def build_fcn_net(self, inp, use_dice = False): + bn1 = tf.layers.batch_normalization(inputs=inp, name='bn1') + print(">>>>>>>>>>>>>>>>>>>>bn1", bn1.get_shape().as_list()) + dnn1 = tf.layers.dense(bn1, 200, activation=None, name='f1') + if use_dice: + dnn1 = dice(dnn1, name='dice_1') + else: + dnn1 = prelu(dnn1, 'prelu1') + + dnn2 = tf.layers.dense(dnn1, 80, activation=None, name='f2') + if use_dice: + dnn2 = dice(dnn2, name='dice_2') + else: + dnn2 = prelu(dnn2, 'prelu2') + dnn3 = tf.layers.dense(dnn2, 2, activation=None, name='f3') + self.y_hat = tf.nn.softmax(dnn3) + 0.00000001 + + with tf.name_scope('Metrics'): + # Cross-entropy loss and optimizer initialization + ctr_loss = - tf.reduce_mean(tf.log(self.y_hat) * self.target_ph) + self.loss = ctr_loss + if self.use_negsampling: + self.loss += self.aux_loss + tf.summary.scalar('loss', self.loss) + self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr, epsilon=1e-04).minimize(self.loss) + # self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.loss) + + + # self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr) + # grads = self.optimizer.compute_gradients(self.loss * (2**12)) + # grads = [(grad / (2**12), var) for grad, var in grads] + # grads = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in grads] + # self.optimizer = self.optimizer.apply_gradients(grads) + # Accuracy metric + self.accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.round(self.y_hat), self.target_ph), tf.float32)) + tf.summary.scalar('accuracy', self.accuracy) + + self.merged = tf.summary.merge_all() + + def auxiliary_loss(self, h_states, click_seq, noclick_seq, mask, stag = None): + mask = tf.cast(mask, tf.float32) + click_input_ = tf.concat([h_states, click_seq], -1) + noclick_input_ = tf.concat([h_states, noclick_seq], -1) + click_prop_ = self.auxiliary_net(click_input_, stag = stag)[:, :, 0] + noclick_prop_ = self.auxiliary_net(noclick_input_, stag = stag)[:, :, 0] + click_loss_ = - tf.reshape(tf.log(click_prop_), [-1, tf.shape(click_seq)[1]]) * mask + noclick_loss_ = - tf.reshape(tf.log(1.0 - noclick_prop_), [-1, tf.shape(noclick_seq)[1]]) * mask + loss_ = tf.reduce_sum(click_loss_ + noclick_loss_) / (tf.reduce_sum(mask) + 1e-6) + return loss_ + + def auxiliary_net(self, in_, stag='auxiliary_net'): + print(">>>>>>>>>>>>>>>>>>>>>in_", in_.get_shape().as_list()) + bn1 = tf.layers.batch_normalization(inputs=in_, name='bn1' + stag, reuse=tf.AUTO_REUSE) + dnn1 = tf.layers.dense(bn1, 100, activation=None, name='f1' + stag, reuse=tf.AUTO_REUSE) + dnn1 = tf.nn.sigmoid(dnn1) + dnn2 = tf.layers.dense(dnn1, 50, activation=None, name='f2' + stag, reuse=tf.AUTO_REUSE) + dnn2 = tf.nn.sigmoid(dnn2) + dnn3 = tf.layers.dense(dnn2, 2, activation=None, name='f3' + stag, reuse=tf.AUTO_REUSE) + y_hat = tf.nn.softmax(dnn3) + 0.00000001 + return y_hat + + def train(self, sess, inps): + if self.use_negsampling: + # outputs = sess.run(self.rnn_outputs, feed_dict={ + # self.uid_batch_ph: inps[0], + # self.mid_batch_ph: inps[1], + # self.cat_batch_ph: inps[2], + # self.mid_his_batch_ph: inps[3], + # self.cat_his_batch_ph: inps[4], + # self.mask: inps[5], + # self.target_ph: inps[6], + # self.seq_len_ph: inps[7], + # self.lr: inps[8], + # self.noclk_mid_batch_ph: inps[9], + # self.noclk_cat_batch_ph: inps[10], + # }) + # print(outputs) + # return + + # op = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) + # for key in op: + # if "dynamic_gru_v2" in key.name: + # print(key.name, sess.run(key)) + + loss, accuracy, aux_loss, _ = sess.run([self.loss, self.accuracy, self.aux_loss, self.optimizer], feed_dict={ + self.uid_batch_ph: inps[0], + self.mid_batch_ph: inps[1], + self.cat_batch_ph: inps[2], + self.mid_his_batch_ph: inps[3], + self.cat_his_batch_ph: inps[4], + self.mask: inps[5], + self.target_ph: inps[6], + self.seq_len_ph: inps[7], + self.lr: inps[8], + self.noclk_mid_batch_ph: inps[9], + self.noclk_cat_batch_ph: inps[10], + }) + # print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>mid_his_batch_ph") + # print(inps[3]) + # print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>embedding") + # print(item_his_eb_time_major) + # print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>output") + # print(outputs) + return loss, accuracy, aux_loss + else: + loss, accuracy, _, summary_str = sess.run([self.loss, self.accuracy, self.optimizer, self.merged], feed_dict={ + self.uid_batch_ph: inps[0], + self.mid_batch_ph: inps[1], + self.cat_batch_ph: inps[2], + self.mid_his_batch_ph: inps[3], + self.cat_his_batch_ph: inps[4], + self.mask: inps[5], + self.target_ph: inps[6], + self.seq_len_ph: inps[7], + self.lr: inps[8], + }) + return loss, accuracy, 0, summary_str + + def calculate(self, sess, inps): + if self.use_negsampling: + probs, loss, accuracy, aux_loss = sess.run([self.y_hat, self.loss, self.accuracy, self.aux_loss], feed_dict={ + self.uid_batch_ph: inps[0], + self.mid_batch_ph: inps[1], + self.cat_batch_ph: inps[2], + self.mid_his_batch_ph: inps[3], + self.cat_his_batch_ph: inps[4], + self.mask: inps[5], + self.target_ph: inps[6], + self.seq_len_ph: inps[7], + self.noclk_mid_batch_ph: inps[8], + self.noclk_cat_batch_ph: inps[9], + }) + return probs, loss, accuracy, aux_loss + else: + probs, loss, accuracy = sess.run([self.y_hat, self.loss, self.accuracy], feed_dict={ + self.uid_batch_ph: inps[0], + self.mid_batch_ph: inps[1], + self.cat_batch_ph: inps[2], + self.mid_his_batch_ph: inps[3], + self.cat_his_batch_ph: inps[4], + self.mask: inps[5], + self.target_ph: inps[6], + self.seq_len_ph: inps[7] + }) + return probs, loss, accuracy, 0 + + def save(self, sess, path): + saver = tf.train.Saver() + saver.save(sess, save_path=path) + + def restore(self, sess, path): + saver = tf.train.Saver() + saver.restore(sess, save_path=path) + print('model restored from %s' % path) + + def summary_op(self, summary_writer, summary_str, step): + summary_writer.add_summary(summary_str, global_step=step) + +# DIEN +class Model_DIN_V2_Gru_Vec_attGru_Neg(Model): + def __init__(self, batch_size, maxlen, n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE, use_negsampling=True): + super(Model_DIN_V2_Gru_Vec_attGru_Neg, self).__init__(batch_size, maxlen, n_uid, n_mid, n_cat, + EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE, + use_negsampling) + + # RNN layer(-s) + with tf.name_scope('rnn_1'): + # rnn_outputs, _ = tf.nn.dynamic_rnn(GRUCell(HIDDEN_SIZE), inputs=self.item_his_eb, + # sequence_length=self.seq_len_ph, dtype=tf.float32, + # scope="gru1") + + item_his_eb_fp16 = tf.cast(self.item_his_eb, tf.float16, name="cast_fp16") + item_his_eb_time_major = tf.transpose(item_his_eb_fp16, [1, 0, 2], name="transpose_time_major") + gruv2 = DynamicGRUV2(HIDDEN_SIZE, dtype=tf.float16) + rnn_outputs, _, _, _, _, _ = gruv2(item_his_eb_time_major) + rnn_outputs_time_major = tf.transpose(rnn_outputs, [1, 0, 2], name="rnn_outputs_transpose_time_major") + rnn_outputs = tf.cast(rnn_outputs_time_major, tf.float32) + # mask_dim = tf.expand_dims(self.mask, -1) + # rnn_outputs = rnn_outputs * mask_dim + # print("?????????????????rnn_outputs", rnn_outputs) + + # item_his_eb_fp16 = tf.cast(self.item_his_eb, tf.float16, name="cast_fp16") + # gru = tf.keras.layers.CuDNNGRU(HIDDEN_SIZE, return_sequences=True) + # rnn_outputs = gru(item_his_eb_fp16) + # # print(rnn_outputs) + # self.rnn_outputs = rnn_outputs + # rnn_outputs = tf.cast(rnn_outputs, tf.float32) + + # from tensorflow.contrib.cudnn_rnn.python.layers import CudnnGRU + # from tensorflow.python.framework import dtypes + + # item_his_eb_fp16 = tf.cast(self.item_his_eb, tf.float16, name="cast_fp16") + # gru = CudnnGRU(num_layers=1, num_units=HIDDEN_SIZE, dtype=dtypes.float16) + # rnn_outputs, _ = gru(inputs=item_his_eb_fp16, sequence_lengths=self.seq_len_ph) + # # print(rnn_outputs) + # self.rnn_outputs = rnn_outputs + # rnn_outputs = tf.cast(rnn_outputs, tf.float32) + + # rnn_outputs, _ = dynamic_rnn(GRUCell(HIDDEN_SIZE), inputs=self.item_his_eb, + # sequence_length=self.seq_len_ph, dtype=tf.float32, + # scope="gru1") + # self.rnn_outputs = rnn_outputs + + # item_his_eb_fp16 = tf.cast(self.item_his_eb, tf.float16, name="cast_fp16") + # rnn_outputs, _ = dynamic_rnn(GRUCell(HIDDEN_SIZE), inputs=item_his_eb_fp16, + # sequence_length=self.seq_len_ph, dtype=tf.float16, + # scope="gru1") + # rnn_outputs = tf.cast(rnn_outputs, tf.float32) + # self.rnn_outputs = rnn_outputs + + tf.summary.histogram('GRU_outputs', rnn_outputs) + + aux_loss_1 = self.auxiliary_loss(rnn_outputs[:, :-1, :], self.item_his_eb[:, 1:, :], + self.noclk_item_his_eb[:, 1:, :], + self.mask[:, 1:], stag="gru") + self.aux_loss = aux_loss_1 + + # Attention layer + with tf.name_scope('Attention_layer_1'): + att_outputs, alphas = din_fcn_attention(self.item_eb, rnn_outputs, ATTENTION_SIZE, self.mask, + softmax_stag=1, stag='1_1', mode='LIST', return_alphas=True) + tf.summary.histogram('alpha_outputs', alphas) + + with tf.name_scope('rnn_2'): + rnn_outputs_fp16 = tf.cast(rnn_outputs, tf.float16) + alphas_fp16 = tf.cast(alphas, tf.float16) + rnn_outputs_time_major = tf.transpose(rnn_outputs_fp16, [1, 0, 2], name="gru2_transpose_time_major") + alphas_fp16_time_major = tf.transpose(alphas_fp16, [1, 0], name="att_transpose_time_major") + + augru = DynamicAUGRU(HIDDEN_SIZE, dtype=tf.float16) + rnn_outputs2, _, _, _, _, _, _ = augru(rnn_outputs_time_major, alphas_fp16_time_major) + + rnn_outputs2_time_major = tf.transpose(rnn_outputs2, [1, 0, 2], name="gru2_rnn_outputs_transpose_time_major") + rnn_outputs2 = tf.cast(rnn_outputs2_time_major, tf.float32) + final_state2 = tf.batch_gather(rnn_outputs2, self.seq_len_ph[:, None] - 1) + final_state2 = tf.squeeze(final_state2, 1) + + # rnn_outputs2, final_state2 = dynamic_rnn(VecAttGRUCell(HIDDEN_SIZE), inputs=rnn_outputs, + # att_scores = tf.expand_dims(alphas, -1), + # sequence_length=self.seq_len_ph, dtype=tf.float32, + # scope="gru2") + # rnn_outputs2, final_state2 = dynamic_rnn(VecAttGRUCell(HIDDEN_SIZE), inputs=rnn_outputs, + # att_scores = tf.expand_dims(alphas, -1), + # dtype=tf.float32, + # scope="gru2") + # final_state2 = tf.batch_gather(rnn_outputs2, self.seq_len_ph[:, None]-1) + # final_state2 = tf.squeeze(final_state2, 1) + + + # rnn_outputs2, final_state2 = tf.nn.dynamic_rnn(GRUCell(HIDDEN_SIZE), inputs=rnn_outputs, + # sequence_length=self.seq_len_ph, dtype=tf.float32, + # scope="gru2") + # print("@@@@@@@@@@@@@@@@@@@", rnn_outputs2.get_shape().as_list()) + tf.summary.histogram('GRU2_Final_State', final_state2) + + inp = tf.concat([self.uid_batch_embedded, self.item_eb, self.item_his_eb_sum, self.item_eb * self.item_his_eb_sum, final_state2], 1) + self.build_fcn_net(inp, use_dice=True) + + +class Model_DIN_V2_Gru_Vec_attGru(Model): + def __init__(self, n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE, use_negsampling=False): + super(Model_DIN_V2_Gru_Vec_attGru, self).__init__(n_uid, n_mid, n_cat, + EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE, + use_negsampling) + + # RNN layer(-s) + with tf.name_scope('rnn_1'): + rnn_outputs, _ = dynamic_rnn(GRUCell(HIDDEN_SIZE), inputs=self.item_his_eb, + sequence_length=self.seq_len_ph, dtype=tf.float32, + scope="gru1") + tf.summary.histogram('GRU_outputs', rnn_outputs) + + # Attention layer + with tf.name_scope('Attention_layer_1'): + att_outputs, alphas = din_fcn_attention(self.item_eb, rnn_outputs, ATTENTION_SIZE, self.mask, + softmax_stag=1, stag='1_1', mode='LIST', return_alphas=True) + tf.summary.histogram('alpha_outputs', alphas) + + with tf.name_scope('rnn_2'): + rnn_outputs2, final_state2 = dynamic_rnn(VecAttGRUCell(HIDDEN_SIZE), inputs=rnn_outputs, + att_scores = tf.expand_dims(alphas, -1), + sequence_length=self.seq_len_ph, dtype=tf.float32, + scope="gru2") + tf.summary.histogram('GRU2_Final_State', final_state2) + + #inp = tf.concat([self.uid_batch_embedded, self.item_eb, final_state2, self.item_his_eb_sum], 1) + inp = tf.concat([self.uid_batch_embedded, self.item_eb, self.item_his_eb_sum, self.item_eb * self.item_his_eb_sum, final_state2], 1) + self.build_fcn_net(inp, use_dice=True) + + +@tf.custom_gradient +def gather_npu(params, indices): + def grad(dy): + params_shape = tf.shape(params, out_type=tf.int64) + params_shape = tf.cast(params_shape, tf.int32) + grad_gather = tf.unsorted_segment_sum(dy, indices, params_shape[0]) + return grad_gather, None + return tf.gather(params, indices), grad + +@tf.custom_gradient +def embedding_lookup_npu(params, indices): + def grad(dy): + params_shape = tf.shape(params, out_type=tf.int64) + params_shape = tf.cast(params_shape, tf.int32) + grad_embedding_lookup = tf.unsorted_segment_sum(dy, indices, params_shape[0]) + return grad_embedding_lookup, None + return tf.nn.embedding_lookup(params, indices), grad \ No newline at end of file -- Gitee From 9245ddd1380be806bd85a6610ee749426bb597c5 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:11:54 +0000 Subject: [PATCH 20/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/process_data.py. --- .../script/process_data.py | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/process_data.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/process_data.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/process_data.py new file mode 100644 index 000000000..64ee24598 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/process_data.py @@ -0,0 +1,131 @@ +# +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from npu_bridge.npu_init import * +import sys +import random +import time + +def process_meta(file): + fi = open(file, "r") + fo = open("item-info", "w") + for line in fi: + obj = eval(line) + cat = obj["categories"][0][-1] + print>>fo, obj["asin"] + "\t" + cat + +def process_reviews(file): + fi = open(file, "r") + user_map = {} + fo = open("reviews-info", "w") + for line in fi: + obj = eval(line) + userID = obj["reviewerID"] + itemID = obj["asin"] + rating = obj["overall"] + time = obj["unixReviewTime"] + print>>fo, userID + "\t" + itemID + "\t" + str(rating) + "\t" + str(time) + +def manual_join(): + f_rev = open("reviews-info", "r") + user_map = {} + item_list = [] + for line in f_rev: + line = line.strip() + items = line.split("\t") + #loctime = time.localtime(float(items[-1])) + #items[-1] = time.strftime('%Y-%m-%d', loctime) + if items[0] not in user_map: + user_map[items[0]]= [] + user_map[items[0]].append(("\t".join(items), float(items[-1]))) + item_list.append(items[1]) + f_meta = open("item-info", "r") + meta_map = {} + for line in f_meta: + arr = line.strip().split("\t") + if arr[0] not in meta_map: + meta_map[arr[0]] = arr[1] + arr = line.strip().split("\t") + fo = open("jointed-new", "w") + for key in user_map: + sorted_user_bh = sorted(user_map[key], key=lambda x:x[1]) + for line, t in sorted_user_bh: + items = line.split("\t") + asin = items[1] + j = 0 + while True: + asin_neg_index = random.randint(0, len(item_list) - 1) + asin_neg = item_list[asin_neg_index] + if asin_neg == asin: + continue + items[1] = asin_neg + print>>fo, "0" + "\t" + "\t".join(items) + "\t" + meta_map[asin_neg] + j += 1 + if j == 1: #negative sampling frequency + break + if asin in meta_map: + print>>fo, "1" + "\t" + line + "\t" + meta_map[asin] + else: + print>>fo, "1" + "\t" + line + "\t" + "default_cat" + + +def split_test(): + fi = open("jointed-new", "r") + fo = open("jointed-new-split-info", "w") + user_count = {} + for line in fi: + line = line.strip() + user = line.split("\t")[1] + if user not in user_count: + user_count[user] = 0 + user_count[user] += 1 + fi.seek(0) + i = 0 + last_user = "A26ZDKC53OP6JD" + for line in fi: + line = line.strip() + user = line.split("\t")[1] + if user == last_user: + if i < user_count[user] - 2: # 1 + negative samples + print>> fo, "20180118" + "\t" + line + else: + print>>fo, "20190119" + "\t" + line + else: + last_user = user + i = 0 + if i < user_count[user] - 2: + print>> fo, "20180118" + "\t" + line + else: + print>>fo, "20190119" + "\t" + line + i += 1 + +process_meta(sys.argv[1]) +process_reviews(sys.argv[2]) +manual_join() +split_test() \ No newline at end of file -- Gitee From 6761d939ceb50e5f0ec18604d669e25f31451f71 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:13:04 +0000 Subject: [PATCH 21/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/run.py. --- .../DIEN_ID0109_for_TensorFlow/script/run.py | 1475 +++++++++++++++++ 1 file changed, 1475 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/run.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/run.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/run.py new file mode 100644 index 000000000..bb1532f59 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/run.py @@ -0,0 +1,1475 @@ +# Copyright 2015 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ============================================================================== + +"""RNN helpers for TensorFlow models. + + +@@bidirectional_dynamic_rnn +@@dynamic_rnn +@@raw_rnn +@@static_rnn +@@static_state_saving_rnn +@@static_bidirectional_rnn +""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from npu_bridge.npu_init import * + +from tensorflow.python.framework import constant_op +from tensorflow.python.framework import dtypes +from tensorflow.python.framework import ops +from tensorflow.python.framework import tensor_shape +from tensorflow.python.ops import array_ops +from tensorflow.python.ops import control_flow_ops +from tensorflow.python.ops import math_ops +from tensorflow.python.ops import rnn_cell_impl +from tensorflow.python.ops import tensor_array_ops +from tensorflow.python.ops import variable_scope as vs +from tensorflow.python.util import nest + + +# pylint: disable=protected-access +_concat = rnn_cell_impl._concat +assert_like_rnncell = rnn_cell_impl.assert_like_rnncell +# pylint: enable=protected-access + + +def _transpose_batch_time(x): + """Transpose the batch and time dimensions of a Tensor. + + Retains as much of the static shape information as possible. + + Args: + x: A tensor of rank 2 or higher. + + Returns: + x transposed along the first two dimensions. + + Raises: + ValueError: if `x` is rank 1 or lower. + """ + x_static_shape = x.get_shape() + if x_static_shape.ndims is not None and x_static_shape.ndims < 2: + raise ValueError( + "Expected input tensor %s to have rank at least 2, but saw shape: %s" % + (x, x_static_shape)) + x_rank = array_ops.rank(x) + x_t = array_ops.transpose( + x, array_ops.concat( + ([1, 0], math_ops.range(2, x_rank)), axis=0)) + x_t.set_shape( + tensor_shape.TensorShape([ + x_static_shape[1].value, x_static_shape[0].value + ]).concatenate(x_static_shape[2:])) + return x_t + + +def _best_effort_input_batch_size(flat_input): + """Get static input batch size if available, with fallback to the dynamic one. + + Args: + flat_input: An iterable of time major input Tensors of shape [max_time, + batch_size, ...]. All inputs should have compatible batch sizes. + + Returns: + The batch size in Python integer if available, or a scalar Tensor otherwise. + + Raises: + ValueError: if there is any input with an invalid shape. + """ + for input_ in flat_input: + shape = input_.shape + if shape.ndims is None: + continue + if shape.ndims < 2: + raise ValueError( + "Expected input tensor %s to have rank at least 2" % input_) + batch_size = shape[1].value + if batch_size is not None: + return batch_size + # Fallback to the dynamic batch size of the first input. + return array_ops.shape(flat_input[0])[1] + + +def _infer_state_dtype(explicit_dtype, state): + """Infer the dtype of an RNN state. + + Args: + explicit_dtype: explicitly declared dtype or None. + state: RNN's hidden state. Must be a Tensor or a nested iterable containing + Tensors. + + Returns: + dtype: inferred dtype of hidden state. + + Raises: + ValueError: if `state` has heterogeneous dtypes or is empty. + """ + if explicit_dtype is not None: + return explicit_dtype + elif nest.is_sequence(state): + inferred_dtypes = [element.dtype for element in nest.flatten(state)] + if not inferred_dtypes: + raise ValueError("Unable to infer dtype from empty state.") + all_same = all([x == inferred_dtypes[0] for x in inferred_dtypes]) + if not all_same: + raise ValueError( + "State has tensors of different inferred_dtypes. Unable to infer a " + "single representative dtype.") + return inferred_dtypes[0] + else: + return state.dtype + + +# pylint: disable=unused-argument +def _rnn_step( + time, sequence_length, min_sequence_length, max_sequence_length, + zero_output, state, call_cell, state_size, skip_conditionals=False): + """Calculate one step of a dynamic RNN minibatch. + + Returns an (output, state) pair conditioned on the sequence_lengths. + When skip_conditionals=False, the pseudocode is something like: + + if t >= max_sequence_length: + return (zero_output, state) + if t < min_sequence_length: + return call_cell() + + # Selectively output zeros or output, old state or new state depending + # on if we've finished calculating each row. + new_output, new_state = call_cell() + final_output = np.vstack([ + zero_output if time >= sequence_lengths[r] else new_output_r + for r, new_output_r in enumerate(new_output) + ]) + final_state = np.vstack([ + state[r] if time >= sequence_lengths[r] else new_state_r + for r, new_state_r in enumerate(new_state) + ]) + return (final_output, final_state) + + Args: + time: Python int, the current time step + sequence_length: int32 `Tensor` vector of size [batch_size] + min_sequence_length: int32 `Tensor` scalar, min of sequence_length + max_sequence_length: int32 `Tensor` scalar, max of sequence_length + zero_output: `Tensor` vector of shape [output_size] + state: Either a single `Tensor` matrix of shape `[batch_size, state_size]`, + or a list/tuple of such tensors. + call_cell: lambda returning tuple of (new_output, new_state) where + new_output is a `Tensor` matrix of shape `[batch_size, output_size]`. + new_state is a `Tensor` matrix of shape `[batch_size, state_size]`. + state_size: The `cell.state_size` associated with the state. + skip_conditionals: Python bool, whether to skip using the conditional + calculations. This is useful for `dynamic_rnn`, where the input tensor + matches `max_sequence_length`, and using conditionals just slows + everything down. + + Returns: + A tuple of (`final_output`, `final_state`) as given by the pseudocode above: + final_output is a `Tensor` matrix of shape [batch_size, output_size] + final_state is either a single `Tensor` matrix, or a tuple of such + matrices (matching length and shapes of input `state`). + + Raises: + ValueError: If the cell returns a state tuple whose length does not match + that returned by `state_size`. + """ + + # Convert state to a list for ease of use + flat_state = nest.flatten(state) + flat_zero_output = nest.flatten(zero_output) + + def _copy_one_through(output, new_output): + # If the state contains a scalar value we simply pass it through. + if output.shape.ndims == 0: + return new_output + copy_cond = (time >= sequence_length) + with ops.colocate_with(new_output): + return array_ops.where(copy_cond, output, new_output) + + def _copy_some_through(flat_new_output, flat_new_state): + # Use broadcasting select to determine which values should get + # the previous state & zero output, and which values should get + # a calculated state & output. + flat_new_output = [ + _copy_one_through(zero_output, new_output) + for zero_output, new_output in zip(flat_zero_output, flat_new_output)] + flat_new_state = [ + _copy_one_through(state, new_state) + for state, new_state in zip(flat_state, flat_new_state)] + return flat_new_output + flat_new_state + + def _maybe_copy_some_through(): + """Run RNN step. Pass through either no or some past state.""" + new_output, new_state = call_cell() + + nest.assert_same_structure(state, new_state) + + flat_new_state = nest.flatten(new_state) + flat_new_output = nest.flatten(new_output) + return control_flow_ops.cond( + # if t < min_seq_len: calculate and return everything + time < min_sequence_length, lambda: flat_new_output + flat_new_state, + # else copy some of it through + lambda: _copy_some_through(flat_new_output, flat_new_state)) + + # TODO(ebrevdo): skipping these conditionals may cause a slowdown, + # but benefits from removing cond() and its gradient. We should + # profile with and without this switch here. + if skip_conditionals: + # Instead of using conditionals, perform the selective copy at all time + # steps. This is faster when max_seq_len is equal to the number of unrolls + # (which is typical for dynamic_rnn). + new_output, new_state = call_cell() + nest.assert_same_structure(state, new_state) + new_state = nest.flatten(new_state) + new_output = nest.flatten(new_output) + final_output_and_state = _copy_some_through(new_output, new_state) + else: + empty_update = lambda: flat_zero_output + flat_state + final_output_and_state = control_flow_ops.cond( + # if t >= max_seq_len: copy all state through, output zeros + time >= max_sequence_length, empty_update, + # otherwise calculation is required: copy some or all of it through + _maybe_copy_some_through) + + if len(final_output_and_state) != len(flat_zero_output) + len(flat_state): + raise ValueError("Internal error: state and output were not concatenated " + "correctly.") + final_output = final_output_and_state[:len(flat_zero_output)] + final_state = final_output_and_state[len(flat_zero_output):] + + for output, flat_output in zip(final_output, flat_zero_output): + output.set_shape(flat_output.get_shape()) + for substate, flat_substate in zip(final_state, flat_state): + substate.set_shape(flat_substate.get_shape()) + + final_output = nest.pack_sequence_as( + structure=zero_output, flat_sequence=final_output) + final_state = nest.pack_sequence_as( + structure=state, flat_sequence=final_state) + + return final_output, final_state + + +def _reverse_seq(input_seq, lengths): + """Reverse a list of Tensors up to specified lengths. + + Args: + input_seq: Sequence of seq_len tensors of dimension (batch_size, n_features) + or nested tuples of tensors. + lengths: A `Tensor` of dimension batch_size, containing lengths for each + sequence in the batch. If "None" is specified, simply reverses + the list. + + Returns: + time-reversed sequence + """ + if lengths is None: + return list(reversed(input_seq)) + + flat_input_seq = tuple(nest.flatten(input_) for input_ in input_seq) + + flat_results = [[] for _ in range(len(input_seq))] + for sequence in zip(*flat_input_seq): + input_shape = tensor_shape.unknown_shape( + ndims=sequence[0].get_shape().ndims) + for input_ in sequence: + input_shape.merge_with(input_.get_shape()) + input_.set_shape(input_shape) + + # Join into (time, batch_size, depth) + s_joined = array_ops.stack(sequence) + + # Reverse along dimension 0 + s_reversed = array_ops.reverse_sequence(s_joined, lengths, 0, 1) + # Split again into list + result = array_ops.unstack(s_reversed) + for r, flat_result in zip(result, flat_results): + r.set_shape(input_shape) + flat_result.append(r) + + results = [nest.pack_sequence_as(structure=input_, flat_sequence=flat_result) + for input_, flat_result in zip(input_seq, flat_results)] + return results + + +def bidirectional_dynamic_rnn(cell_fw, cell_bw, inputs, sequence_length=None, + initial_state_fw=None, initial_state_bw=None, + dtype=None, parallel_iterations=None, + swap_memory=False, time_major=False, scope=None): + """Creates a dynamic version of bidirectional recurrent neural network. + + Takes input and builds independent forward and backward RNNs. The input_size + of forward and backward cell must match. The initial state for both directions + is zero by default (but can be set optionally) and no intermediate states are + ever returned -- the network is fully unrolled for the given (passed in) + length(s) of the sequence(s) or completely unrolled if length(s) is not + given. + + Args: + cell_fw: An instance of RNNCell, to be used for forward direction. + cell_bw: An instance of RNNCell, to be used for backward direction. + inputs: The RNN inputs. + If time_major == False (default), this must be a tensor of shape: + `[batch_size, max_time, ...]`, or a nested tuple of such elements. + If time_major == True, this must be a tensor of shape: + `[max_time, batch_size, ...]`, or a nested tuple of such elements. + sequence_length: (optional) An int32/int64 vector, size `[batch_size]`, + containing the actual lengths for each of the sequences in the batch. + If not provided, all batch entries are assumed to be full sequences; and + time reversal is applied from time `0` to `max_time` for each sequence. + initial_state_fw: (optional) An initial state for the forward RNN. + This must be a tensor of appropriate type and shape + `[batch_size, cell_fw.state_size]`. + If `cell_fw.state_size` is a tuple, this should be a tuple of + tensors having shapes `[batch_size, s] for s in cell_fw.state_size`. + initial_state_bw: (optional) Same as for `initial_state_fw`, but using + the corresponding properties of `cell_bw`. + dtype: (optional) The data type for the initial states and expected output. + Required if initial_states are not provided or RNN states have a + heterogeneous dtype. + parallel_iterations: (Default: 32). The number of iterations to run in + parallel. Those operations which do not have any temporal dependency + and can be run in parallel, will be. This parameter trades off + time for space. Values >> 1 use more memory but take less time, + while smaller values use less memory but computations take longer. + swap_memory: Transparently swap the tensors produced in forward inference + but needed for back prop from GPU to CPU. This allows training RNNs + which would typically not fit on a single GPU, with very minimal (or no) + performance penalty. + time_major: The shape format of the `inputs` and `outputs` Tensors. + If true, these `Tensors` must be shaped `[max_time, batch_size, depth]`. + If false, these `Tensors` must be shaped `[batch_size, max_time, depth]`. + Using `time_major = True` is a bit more efficient because it avoids + transposes at the beginning and end of the RNN calculation. However, + most TensorFlow data is batch-major, so by default this function + accepts input and emits output in batch-major form. + scope: VariableScope for the created subgraph; defaults to + "bidirectional_rnn" + + Returns: + A tuple (outputs, output_states) where: + outputs: A tuple (output_fw, output_bw) containing the forward and + the backward rnn output `Tensor`. + If time_major == False (default), + output_fw will be a `Tensor` shaped: + `[batch_size, max_time, cell_fw.output_size]` + and output_bw will be a `Tensor` shaped: + `[batch_size, max_time, cell_bw.output_size]`. + If time_major == True, + output_fw will be a `Tensor` shaped: + `[max_time, batch_size, cell_fw.output_size]` + and output_bw will be a `Tensor` shaped: + `[max_time, batch_size, cell_bw.output_size]`. + It returns a tuple instead of a single concatenated `Tensor`, unlike + in the `bidirectional_rnn`. If the concatenated one is preferred, + the forward and backward outputs can be concatenated as + `tf.concat(outputs, 2)`. + output_states: A tuple (output_state_fw, output_state_bw) containing + the forward and the backward final states of bidirectional rnn. + + Raises: + TypeError: If `cell_fw` or `cell_bw` is not an instance of `RNNCell`. + """ + + assert_like_rnncell("cell_fw must be an instance of RNNCell", cell_fw) + assert_like_rnncell("cell_bw must be an instance of RNNCell", cell_bw) + #if not _like_rnncell(cell_fw): + # raise TypeError("cell_fw must be an instance of RNNCell") + #if not _like_rnncell(cell_bw): + # raise TypeError("cell_bw must be an instance of RNNCell") + + with vs.variable_scope(scope or "bidirectional_rnn"): + # Forward direction + with vs.variable_scope("fw") as fw_scope: + output_fw, output_state_fw = dynamic_rnn( + cell=cell_fw, inputs=inputs, sequence_length=sequence_length, + initial_state=initial_state_fw, dtype=dtype, + parallel_iterations=parallel_iterations, swap_memory=swap_memory, + time_major=time_major, scope=fw_scope) + + # Backward direction + if not time_major: + time_dim = 1 + batch_dim = 0 + else: + time_dim = 0 + batch_dim = 1 + + def _reverse(input_, seq_lengths, seq_dim, batch_dim): + if seq_lengths is not None: + return array_ops.reverse_sequence( + input=input_, seq_lengths=seq_lengths, + seq_dim=seq_dim, batch_dim=batch_dim) + else: + return array_ops.reverse(input_, axis=[seq_dim]) + + with vs.variable_scope("bw") as bw_scope: + inputs_reverse = _reverse( + inputs, seq_lengths=sequence_length, + seq_dim=time_dim, batch_dim=batch_dim) + tmp, output_state_bw = dynamic_rnn( + cell=cell_bw, inputs=inputs_reverse, sequence_length=sequence_length, + initial_state=initial_state_bw, dtype=dtype, + parallel_iterations=parallel_iterations, swap_memory=swap_memory, + time_major=time_major, scope=bw_scope) + + output_bw = _reverse( + tmp, seq_lengths=sequence_length, + seq_dim=time_dim, batch_dim=batch_dim) + + outputs = (output_fw, output_bw) + output_states = (output_state_fw, output_state_bw) + + return (outputs, output_states) + + +def dynamic_rnn(cell, inputs, att_scores=None, sequence_length=None, initial_state=None, + dtype=None, parallel_iterations=None, swap_memory=False, + time_major=False, scope=None): + """Creates a recurrent neural network specified by RNNCell `cell`. + + Performs fully dynamic unrolling of `inputs`. + + Example: + + ```python + # create a BasicRNNCell + rnn_cell = tf.nn.rnn_cell.BasicRNNCell(hidden_size) + + # 'outputs' is a tensor of shape [batch_size, max_time, cell_state_size] + + # defining initial state + initial_state = rnn_cell.zero_state(batch_size, dtype=tf.float32) + + # 'state' is a tensor of shape [batch_size, cell_state_size] + outputs, state = tf.nn.dynamic_rnn(rnn_cell, input_data, + initial_state=initial_state, + dtype=tf.float32) + ``` + + ```python + # create 2 LSTMCells + rnn_layers = [tf.nn.rnn_cell.LSTMCell(size) for size in [128, 256]] + + # create a RNN cell composed sequentially of a number of RNNCells + multi_rnn_cell = tf.nn.rnn_cell.MultiRNNCell(rnn_layers) + + # 'outputs' is a tensor of shape [batch_size, max_time, 256] + # 'state' is a N-tuple where N is the number of LSTMCells containing a + # tf.contrib.rnn.LSTMStateTuple for each cell + outputs, state = tf.nn.dynamic_rnn(cell=multi_rnn_cell, + inputs=data, + dtype=tf.float32) + ``` + + + Args: + cell: An instance of RNNCell. + inputs: The RNN inputs. + If `time_major == False` (default), this must be a `Tensor` of shape: + `[batch_size, max_time, ...]`, or a nested tuple of such + elements. + If `time_major == True`, this must be a `Tensor` of shape: + `[max_time, batch_size, ...]`, or a nested tuple of such + elements. + This may also be a (possibly nested) tuple of Tensors satisfying + this property. The first two dimensions must match across all the inputs, + but otherwise the ranks and other shape components may differ. + In this case, input to `cell` at each time-step will replicate the + structure of these tuples, except for the time dimension (from which the + time is taken). + The input to `cell` at each time step will be a `Tensor` or (possibly + nested) tuple of Tensors each with dimensions `[batch_size, ...]`. + sequence_length: (optional) An int32/int64 vector sized `[batch_size]`. + Used to copy-through state and zero-out outputs when past a batch + element's sequence length. So it's more for correctness than performance. + initial_state: (optional) An initial state for the RNN. + If `cell.state_size` is an integer, this must be + a `Tensor` of appropriate type and shape `[batch_size, cell.state_size]`. + If `cell.state_size` is a tuple, this should be a tuple of + tensors having shapes `[batch_size, s] for s in cell.state_size`. + dtype: (optional) The data type for the initial state and expected output. + Required if initial_state is not provided or RNN state has a heterogeneous + dtype. + parallel_iterations: (Default: 32). The number of iterations to run in + parallel. Those operations which do not have any temporal dependency + and can be run in parallel, will be. This parameter trades off + time for space. Values >> 1 use more memory but take less time, + while smaller values use less memory but computations take longer. + swap_memory: Transparently swap the tensors produced in forward inference + but needed for back prop from GPU to CPU. This allows training RNNs + which would typically not fit on a single GPU, with very minimal (or no) + performance penalty. + time_major: The shape format of the `inputs` and `outputs` Tensors. + If true, these `Tensors` must be shaped `[max_time, batch_size, depth]`. + If false, these `Tensors` must be shaped `[batch_size, max_time, depth]`. + Using `time_major = True` is a bit more efficient because it avoids + transposes at the beginning and end of the RNN calculation. However, + most TensorFlow data is batch-major, so by default this function + accepts input and emits output in batch-major form. + scope: VariableScope for the created subgraph; defaults to "rnn". + + Returns: + A pair (outputs, state) where: + + outputs: The RNN output `Tensor`. + + If time_major == False (default), this will be a `Tensor` shaped: + `[batch_size, max_time, cell.output_size]`. + + If time_major == True, this will be a `Tensor` shaped: + `[max_time, batch_size, cell.output_size]`. + + Note, if `cell.output_size` is a (possibly nested) tuple of integers + or `TensorShape` objects, then `outputs` will be a tuple having the + same structure as `cell.output_size`, containing Tensors having shapes + corresponding to the shape data in `cell.output_size`. + + state: The final state. If `cell.state_size` is an int, this + will be shaped `[batch_size, cell.state_size]`. If it is a + `TensorShape`, this will be shaped `[batch_size] + cell.state_size`. + If it is a (possibly nested) tuple of ints or `TensorShape`, this will + be a tuple having the corresponding shapes. If cells are `LSTMCells` + `state` will be a tuple containing a `LSTMStateTuple` for each cell. + + Raises: + TypeError: If `cell` is not an instance of RNNCell. + ValueError: If inputs is None or an empty list. + """ + assert_like_rnncell("cell must be an instance of RNNCell", cell) + #if not _like_rnncell(cell): + # raise TypeError("cell must be an instance of RNNCell") + + # By default, time_major==False and inputs are batch-major: shaped + # [batch, time, depth] + # For internal calculations, we transpose to [time, batch, depth] + flat_input = nest.flatten(inputs) + + if not time_major: + # (B,T,D) => (T,B,D) + flat_input = [ops.convert_to_tensor(input_) for input_ in flat_input] + flat_input = tuple(_transpose_batch_time(input_) for input_ in flat_input) + + parallel_iterations = parallel_iterations or 32 + if sequence_length is not None: + sequence_length = math_ops.to_int32(sequence_length) + if sequence_length.get_shape().ndims not in (None, 1): + raise ValueError( + "sequence_length must be a vector of length batch_size, " + "but saw shape: %s" % sequence_length.get_shape()) + sequence_length = array_ops.identity( # Just to find it in the graph. + sequence_length, name="sequence_length") + + # Create a new scope in which the caching device is either + # determined by the parent scope, or is set to place the cached + # Variable using the same placement as for the rest of the RNN. + with vs.variable_scope(scope or "rnn") as varscope: + if varscope.caching_device is None: + varscope.set_caching_device(lambda op: op.device) + batch_size = _best_effort_input_batch_size(flat_input) + + if initial_state is not None: + state = initial_state + else: + if not dtype: + raise ValueError("If there is no initial_state, you must give a dtype.") + state = cell.zero_state(batch_size, dtype) + + def _assert_has_shape(x, shape): + x_shape = array_ops.shape(x) + packed_shape = array_ops.stack(shape) + return control_flow_ops.Assert( + math_ops.reduce_all(math_ops.equal(x_shape, packed_shape)), + ["Expected shape for Tensor %s is " % x.name, + packed_shape, " but saw shape: ", x_shape]) + + if sequence_length is not None: + # Perform some shape validation + with ops.control_dependencies( + [_assert_has_shape(sequence_length, [batch_size])]): + sequence_length = array_ops.identity( + sequence_length, name="CheckSeqLen") + + inputs = nest.pack_sequence_as(structure=inputs, flat_sequence=flat_input) + + (outputs, final_state) = _dynamic_rnn_loop( + cell, + inputs, + state, + parallel_iterations=parallel_iterations, + swap_memory=swap_memory, + att_scores = att_scores, + sequence_length=sequence_length, + dtype=dtype) + + # Outputs of _dynamic_rnn_loop are always shaped [time, batch, depth]. + # If we are performing batch-major calculations, transpose output back + # to shape [batch, time, depth] + if not time_major: + # (T,B,D) => (B,T,D) + outputs = nest.map_structure(_transpose_batch_time, outputs) + + return (outputs, final_state) + + +def _dynamic_rnn_loop(cell, + inputs, + initial_state, + parallel_iterations, + swap_memory, + att_scores = None, + sequence_length=None, + dtype=None): + """Internal implementation of Dynamic RNN. + + Args: + cell: An instance of RNNCell. + inputs: A `Tensor` of shape [time, batch_size, input_size], or a nested + tuple of such elements. + initial_state: A `Tensor` of shape `[batch_size, state_size]`, or if + `cell.state_size` is a tuple, then this should be a tuple of + tensors having shapes `[batch_size, s] for s in cell.state_size`. + parallel_iterations: Positive Python int. + swap_memory: A Python boolean + sequence_length: (optional) An `int32` `Tensor` of shape [batch_size]. + dtype: (optional) Expected dtype of output. If not specified, inferred from + initial_state. + + Returns: + Tuple `(final_outputs, final_state)`. + final_outputs: + A `Tensor` of shape `[time, batch_size, cell.output_size]`. If + `cell.output_size` is a (possibly nested) tuple of ints or `TensorShape` + objects, then this returns a (possibly nsted) tuple of Tensors matching + the corresponding shapes. + final_state: + A `Tensor`, or possibly nested tuple of Tensors, matching in length + and shapes to `initial_state`. + + Raises: + ValueError: If the input depth cannot be inferred via shape inference + from the inputs. + """ + state = initial_state + assert isinstance(parallel_iterations, int), "parallel_iterations must be int" + + state_size = cell.state_size + + flat_input = nest.flatten(inputs) + flat_output_size = nest.flatten(cell.output_size) + + # Construct an initial output + input_shape = array_ops.shape(flat_input[0]) + time_steps = input_shape[0] + batch_size = _best_effort_input_batch_size(flat_input) + + inputs_got_shape = tuple(input_.get_shape().with_rank_at_least(3) + for input_ in flat_input) + + const_time_steps, const_batch_size = inputs_got_shape[0].as_list()[:2] + + for shape in inputs_got_shape: + if not shape[2:].is_fully_defined(): + raise ValueError( + "Input size (depth of inputs) must be accessible via shape inference," + " but saw value None.") + got_time_steps = shape[0].value + got_batch_size = shape[1].value + if const_time_steps != got_time_steps: + raise ValueError( + "Time steps is not the same for all the elements in the input in a " + "batch.") + if const_batch_size != got_batch_size: + raise ValueError( + "Batch_size is not the same for all the elements in the input.") + + # Prepare dynamic conditional copying of state & output + def _create_zero_arrays(size): + size = _concat(batch_size, size) + return array_ops.zeros( + array_ops.stack(size), _infer_state_dtype(dtype, state)) + + flat_zero_output = tuple(_create_zero_arrays(output) + for output in flat_output_size) + zero_output = nest.pack_sequence_as(structure=cell.output_size, + flat_sequence=flat_zero_output) + + if sequence_length is not None: + min_sequence_length = math_ops.reduce_min(sequence_length) + max_sequence_length = math_ops.reduce_max(sequence_length) + + time = array_ops.constant(0, dtype=dtypes.int32, name="time") + + with ops.name_scope("dynamic_rnn") as scope: + base_name = scope + + def _create_ta(name, dtype): + return tensor_array_ops.TensorArray(dtype=dtype, + size=time_steps, + tensor_array_name=base_name + name) + + output_ta = tuple(_create_ta("output_%d" % i, + _infer_state_dtype(dtype, state)) + for i in range(len(flat_output_size))) + input_ta = tuple(_create_ta("input_%d" % i, flat_input[i].dtype) + for i in range(len(flat_input))) + + input_ta = tuple(ta.unstack(input_) + for ta, input_ in zip(input_ta, flat_input)) + + def _time_step(time, output_ta_t, state, att_scores=None): + """Take a time step of the dynamic RNN. + + Args: + time: int32 scalar Tensor. + output_ta_t: List of `TensorArray`s that represent the output. + state: nested tuple of vector tensors that represent the state. + + Returns: + The tuple (time + 1, output_ta_t with updated flow, new_state). + """ + + input_t = tuple(ta.read(time) for ta in input_ta) + # Restore some shape information + for input_, shape in zip(input_t, inputs_got_shape): + input_.set_shape(shape[1:]) + + input_t = nest.pack_sequence_as(structure=inputs, flat_sequence=input_t) + if att_scores is not None: + att_score = att_scores[:, time, :] + call_cell = lambda: cell(input_t, state, att_score) + else: + call_cell = lambda: cell(input_t, state) + + if sequence_length is not None: + (output, new_state) = _rnn_step( + time=time, + sequence_length=sequence_length, + min_sequence_length=min_sequence_length, + max_sequence_length=max_sequence_length, + zero_output=zero_output, + state=state, + call_cell=call_cell, + state_size=state_size, + skip_conditionals=True) + else: + (output, new_state) = call_cell() + + # Pack state if using state tuples + output = nest.flatten(output) + + output_ta_t = tuple( + ta.write(time, out) for ta, out in zip(output_ta_t, output)) + if att_scores is not None: + return (time + 1, output_ta_t, new_state, att_scores) + else: + return (time + 1, output_ta_t, new_state) + + if att_scores is not None: + _, output_final_ta, final_state, _ = control_flow_ops.while_loop( + cond=lambda time, *_: time < time_steps, + body=_time_step, + loop_vars=(time, output_ta, state, att_scores), + parallel_iterations=parallel_iterations, + swap_memory=swap_memory) + else: + _, output_final_ta, final_state = control_flow_ops.while_loop( + cond=lambda time, *_: time < time_steps, + body=_time_step, + loop_vars=(time, output_ta, state), + parallel_iterations=parallel_iterations, + swap_memory=swap_memory) + + # Unpack final output if not using output tuples. + final_outputs = tuple(ta.stack() for ta in output_final_ta) + + # Restore some shape information + for output, output_size in zip(final_outputs, flat_output_size): + shape = _concat( + [const_time_steps, const_batch_size], output_size, static=True) + output.set_shape(shape) + + final_outputs = nest.pack_sequence_as( + structure=cell.output_size, flat_sequence=final_outputs) + + return (final_outputs, final_state) + + +def raw_rnn(cell, loop_fn, + parallel_iterations=None, swap_memory=False, scope=None): + """Creates an `RNN` specified by RNNCell `cell` and loop function `loop_fn`. + + **NOTE: This method is still in testing, and the API may change.** + + This function is a more primitive version of `dynamic_rnn` that provides + more direct access to the inputs each iteration. It also provides more + control over when to start and finish reading the sequence, and + what to emit for the output. + + For example, it can be used to implement the dynamic decoder of a seq2seq + model. + + Instead of working with `Tensor` objects, most operations work with + `TensorArray` objects directly. + + The operation of `raw_rnn`, in pseudo-code, is basically the following: + + ```python + time = tf.constant(0, dtype=tf.int32) + (finished, next_input, initial_state, _, loop_state) = loop_fn( + time=time, cell_output=None, cell_state=None, loop_state=None) + emit_ta = TensorArray(dynamic_size=True, dtype=initial_state.dtype) + state = initial_state + while not all(finished): + (output, cell_state) = cell(next_input, state) + (next_finished, next_input, next_state, emit, loop_state) = loop_fn( + time=time + 1, cell_output=output, cell_state=cell_state, + loop_state=loop_state) + # Emit zeros and copy forward state for minibatch entries that are finished. + state = tf.where(finished, state, next_state) + emit = tf.where(finished, tf.zeros_like(emit), emit) + emit_ta = emit_ta.write(time, emit) + # If any new minibatch entries are marked as finished, mark these. + finished = tf.logical_or(finished, next_finished) + time += 1 + return (emit_ta, state, loop_state) + ``` + + with the additional properties that output and state may be (possibly nested) + tuples, as determined by `cell.output_size` and `cell.state_size`, and + as a result the final `state` and `emit_ta` may themselves be tuples. + + A simple implementation of `dynamic_rnn` via `raw_rnn` looks like this: + + ```python + inputs = tf.placeholder(shape=(max_time, batch_size, input_depth), + dtype=tf.float32) + sequence_length = tf.placeholder(shape=(batch_size,), dtype=tf.int32) + inputs_ta = tf.TensorArray(dtype=tf.float32, size=max_time) + inputs_ta = inputs_ta.unstack(inputs) + + cell = tf.contrib.rnn.LSTMCell(num_units) + + def loop_fn(time, cell_output, cell_state, loop_state): + emit_output = cell_output # == None for time == 0 + if cell_output is None: # time == 0 + next_cell_state = cell.zero_state(batch_size, tf.float32) + else: + next_cell_state = cell_state + elements_finished = (time >= sequence_length) + finished = tf.reduce_all(elements_finished) + next_input = tf.cond( + finished, + lambda: tf.zeros([batch_size, input_depth], dtype=tf.float32), + lambda: inputs_ta.read(time)) + next_loop_state = None + return (elements_finished, next_input, next_cell_state, + emit_output, next_loop_state) + + outputs_ta, final_state, _ = raw_rnn(cell, loop_fn) + outputs = outputs_ta.stack() + ``` + + Args: + cell: An instance of RNNCell. + loop_fn: A callable that takes inputs + `(time, cell_output, cell_state, loop_state)` + and returns the tuple + `(finished, next_input, next_cell_state, emit_output, next_loop_state)`. + Here `time` is an int32 scalar `Tensor`, `cell_output` is a + `Tensor` or (possibly nested) tuple of tensors as determined by + `cell.output_size`, and `cell_state` is a `Tensor` + or (possibly nested) tuple of tensors, as determined by the `loop_fn` + on its first call (and should match `cell.state_size`). + The outputs are: `finished`, a boolean `Tensor` of + shape `[batch_size]`, `next_input`: the next input to feed to `cell`, + `next_cell_state`: the next state to feed to `cell`, + and `emit_output`: the output to store for this iteration. + + Note that `emit_output` should be a `Tensor` or (possibly nested) + tuple of tensors with shapes and structure matching `cell.output_size` + and `cell_output` above. The parameter `cell_state` and output + `next_cell_state` may be either a single or (possibly nested) tuple + of tensors. The parameter `loop_state` and + output `next_loop_state` may be either a single or (possibly nested) tuple + of `Tensor` and `TensorArray` objects. This last parameter + may be ignored by `loop_fn` and the return value may be `None`. If it + is not `None`, then the `loop_state` will be propagated through the RNN + loop, for use purely by `loop_fn` to keep track of its own state. + The `next_loop_state` parameter returned may be `None`. + + The first call to `loop_fn` will be `time = 0`, `cell_output = None`, + `cell_state = None`, and `loop_state = None`. For this call: + The `next_cell_state` value should be the value with which to initialize + the cell's state. It may be a final state from a previous RNN or it + may be the output of `cell.zero_state()`. It should be a + (possibly nested) tuple structure of tensors. + If `cell.state_size` is an integer, this must be + a `Tensor` of appropriate type and shape `[batch_size, cell.state_size]`. + If `cell.state_size` is a `TensorShape`, this must be a `Tensor` of + appropriate type and shape `[batch_size] + cell.state_size`. + If `cell.state_size` is a (possibly nested) tuple of ints or + `TensorShape`, this will be a tuple having the corresponding shapes. + The `emit_output` value may be either `None` or a (possibly nested) + tuple structure of tensors, e.g., + `(tf.zeros(shape_0, dtype=dtype_0), tf.zeros(shape_1, dtype=dtype_1))`. + If this first `emit_output` return value is `None`, + then the `emit_ta` result of `raw_rnn` will have the same structure and + dtypes as `cell.output_size`. Otherwise `emit_ta` will have the same + structure, shapes (prepended with a `batch_size` dimension), and dtypes + as `emit_output`. The actual values returned for `emit_output` at this + initializing call are ignored. Note, this emit structure must be + consistent across all time steps. + + parallel_iterations: (Default: 32). The number of iterations to run in + parallel. Those operations which do not have any temporal dependency + and can be run in parallel, will be. This parameter trades off + time for space. Values >> 1 use more memory but take less time, + while smaller values use less memory but computations take longer. + swap_memory: Transparently swap the tensors produced in forward inference + but needed for back prop from GPU to CPU. This allows training RNNs + which would typically not fit on a single GPU, with very minimal (or no) + performance penalty. + scope: VariableScope for the created subgraph; defaults to "rnn". + + Returns: + A tuple `(emit_ta, final_state, final_loop_state)` where: + + `emit_ta`: The RNN output `TensorArray`. + If `loop_fn` returns a (possibly nested) set of Tensors for + `emit_output` during initialization, (inputs `time = 0`, + `cell_output = None`, and `loop_state = None`), then `emit_ta` will + have the same structure, dtypes, and shapes as `emit_output` instead. + If `loop_fn` returns `emit_output = None` during this call, + the structure of `cell.output_size` is used: + If `cell.output_size` is a (possibly nested) tuple of integers + or `TensorShape` objects, then `emit_ta` will be a tuple having the + same structure as `cell.output_size`, containing TensorArrays whose + elements' shapes correspond to the shape data in `cell.output_size`. + + `final_state`: The final cell state. If `cell.state_size` is an int, this + will be shaped `[batch_size, cell.state_size]`. If it is a + `TensorShape`, this will be shaped `[batch_size] + cell.state_size`. + If it is a (possibly nested) tuple of ints or `TensorShape`, this will + be a tuple having the corresponding shapes. + + `final_loop_state`: The final loop state as returned by `loop_fn`. + + Raises: + TypeError: If `cell` is not an instance of RNNCell, or `loop_fn` is not + a `callable`. + """ + + assert_like_rnncell("cell must be an instance of RNNCell", cell) + #if not _like_rnncell(cell): + # raise TypeError("cell must be an instance of RNNCell") + if not callable(loop_fn): + raise TypeError("loop_fn must be a callable") + + parallel_iterations = parallel_iterations or 32 + + # Create a new scope in which the caching device is either + # determined by the parent scope, or is set to place the cached + # Variable using the same placement as for the rest of the RNN. + with vs.variable_scope(scope or "rnn") as varscope: + if varscope.caching_device is None: + varscope.set_caching_device(lambda op: op.device) + + time = constant_op.constant(0, dtype=dtypes.int32) + (elements_finished, next_input, initial_state, emit_structure, + init_loop_state) = loop_fn( + time, None, None, None) # time, cell_output, cell_state, loop_state + flat_input = nest.flatten(next_input) + + # Need a surrogate loop state for the while_loop if none is available. + loop_state = (init_loop_state if init_loop_state is not None + else constant_op.constant(0, dtype=dtypes.int32)) + + input_shape = [input_.get_shape() for input_ in flat_input] + static_batch_size = input_shape[0][0] + + for input_shape_i in input_shape: + # Static verification that batch sizes all match + static_batch_size.merge_with(input_shape_i[0]) + + batch_size = static_batch_size.value + if batch_size is None: + batch_size = array_ops.shape(flat_input[0])[0] + + nest.assert_same_structure(initial_state, cell.state_size) + state = initial_state + flat_state = nest.flatten(state) + flat_state = [ops.convert_to_tensor(s) for s in flat_state] + state = nest.pack_sequence_as(structure=state, + flat_sequence=flat_state) + + if emit_structure is not None: + flat_emit_structure = nest.flatten(emit_structure) + flat_emit_size = [emit.shape if emit.shape.is_fully_defined() else + array_ops.shape(emit) for emit in flat_emit_structure] + flat_emit_dtypes = [emit.dtype for emit in flat_emit_structure] + else: + emit_structure = cell.output_size + flat_emit_size = nest.flatten(emit_structure) + flat_emit_dtypes = [flat_state[0].dtype] * len(flat_emit_size) + + flat_emit_ta = [ + tensor_array_ops.TensorArray( + dtype=dtype_i, dynamic_size=True, size=0, name="rnn_output_%d" % i) + for i, dtype_i in enumerate(flat_emit_dtypes)] + emit_ta = nest.pack_sequence_as(structure=emit_structure, + flat_sequence=flat_emit_ta) + flat_zero_emit = [ + array_ops.zeros(_concat(batch_size, size_i), dtype_i) + for size_i, dtype_i in zip(flat_emit_size, flat_emit_dtypes)] + zero_emit = nest.pack_sequence_as(structure=emit_structure, + flat_sequence=flat_zero_emit) + + def condition(unused_time, elements_finished, *_): + return math_ops.logical_not(math_ops.reduce_all(elements_finished)) + + def body(time, elements_finished, current_input, + emit_ta, state, loop_state): + """Internal while loop body for raw_rnn. + + Args: + time: time scalar. + elements_finished: batch-size vector. + current_input: possibly nested tuple of input tensors. + emit_ta: possibly nested tuple of output TensorArrays. + state: possibly nested tuple of state tensors. + loop_state: possibly nested tuple of loop state tensors. + + Returns: + Tuple having the same size as Args but with updated values. + """ + (next_output, cell_state) = cell(current_input, state) + + nest.assert_same_structure(state, cell_state) + nest.assert_same_structure(cell.output_size, next_output) + + next_time = time + 1 + (next_finished, next_input, next_state, emit_output, + next_loop_state) = loop_fn( + next_time, next_output, cell_state, loop_state) + + nest.assert_same_structure(state, next_state) + nest.assert_same_structure(current_input, next_input) + nest.assert_same_structure(emit_ta, emit_output) + + # If loop_fn returns None for next_loop_state, just reuse the + # previous one. + loop_state = loop_state if next_loop_state is None else next_loop_state + + def _copy_some_through(current, candidate): + """Copy some tensors through via array_ops.where.""" + def copy_fn(cur_i, cand_i): + with ops.colocate_with(cand_i): + return array_ops.where(elements_finished, cur_i, cand_i) + return nest.map_structure(copy_fn, current, candidate) + + emit_output = _copy_some_through(zero_emit, emit_output) + next_state = _copy_some_through(state, next_state) + + emit_ta = nest.map_structure( + lambda ta, emit: ta.write(time, emit), emit_ta, emit_output) + + elements_finished = math_ops.logical_or(elements_finished, next_finished) + + return (next_time, elements_finished, next_input, + emit_ta, next_state, loop_state) + + returned = control_flow_ops.while_loop( + condition, body, loop_vars=[ + time, elements_finished, next_input, + emit_ta, state, loop_state], + parallel_iterations=parallel_iterations, + swap_memory=swap_memory) + + (emit_ta, final_state, final_loop_state) = returned[-3:] + + if init_loop_state is None: + final_loop_state = None + + return (emit_ta, final_state, final_loop_state) + + +def static_rnn(cell, + inputs, + initial_state=None, + dtype=None, + sequence_length=None, + scope=None): + """Creates a recurrent neural network specified by RNNCell `cell`. + + The simplest form of RNN network generated is: + + ```python + state = cell.zero_state(...) + outputs = [] + for input_ in inputs: + output, state = cell(input_, state) + outputs.append(output) + return (outputs, state) + ``` + However, a few other options are available: + + An initial state can be provided. + If the sequence_length vector is provided, dynamic calculation is performed. + This method of calculation does not compute the RNN steps past the maximum + sequence length of the minibatch (thus saving computational time), + and properly propagates the state at an example's sequence length + to the final state output. + + The dynamic calculation performed is, at time `t` for batch row `b`, + + ```python + (output, state)(b, t) = + (t >= sequence_length(b)) + ? (zeros(cell.output_size), states(b, sequence_length(b) - 1)) + : cell(input(b, t), state(b, t - 1)) + ``` + + Args: + cell: An instance of RNNCell. + inputs: A length T list of inputs, each a `Tensor` of shape + `[batch_size, input_size]`, or a nested tuple of such elements. + initial_state: (optional) An initial state for the RNN. + If `cell.state_size` is an integer, this must be + a `Tensor` of appropriate type and shape `[batch_size, cell.state_size]`. + If `cell.state_size` is a tuple, this should be a tuple of + tensors having shapes `[batch_size, s] for s in cell.state_size`. + dtype: (optional) The data type for the initial state and expected output. + Required if initial_state is not provided or RNN state has a heterogeneous + dtype. + sequence_length: Specifies the length of each sequence in inputs. + An int32 or int64 vector (tensor) size `[batch_size]`, values in `[0, T)`. + scope: VariableScope for the created subgraph; defaults to "rnn". + + Returns: + A pair (outputs, state) where: + + - outputs is a length T list of outputs (one for each input), or a nested + tuple of such elements. + - state is the final state + + Raises: + TypeError: If `cell` is not an instance of RNNCell. + ValueError: If `inputs` is `None` or an empty list, or if the input depth + (column size) cannot be inferred from inputs via shape inference. + """ + + assert_like_rnncell("cell must be an instance of RNNCell", cell) + #if not _like_rnncell(cell): + # raise TypeError("cell must be an instance of RNNCell") + if not nest.is_sequence(inputs): + raise TypeError("inputs must be a sequence") + if not inputs: + raise ValueError("inputs must not be empty") + + outputs = [] + # Create a new scope in which the caching device is either + # determined by the parent scope, or is set to place the cached + # Variable using the same placement as for the rest of the RNN. + with vs.variable_scope(scope or "rnn") as varscope: + if varscope.caching_device is None: + varscope.set_caching_device(lambda op: op.device) + + # Obtain the first sequence of the input + first_input = inputs + while nest.is_sequence(first_input): + first_input = first_input[0] + + # Temporarily avoid EmbeddingWrapper and seq2seq badness + # TODO(lukaszkaiser): remove EmbeddingWrapper + if first_input.get_shape().ndims != 1: + + input_shape = first_input.get_shape().with_rank_at_least(2) + fixed_batch_size = input_shape[0] + + flat_inputs = nest.flatten(inputs) + for flat_input in flat_inputs: + input_shape = flat_input.get_shape().with_rank_at_least(2) + batch_size, input_size = input_shape[0], input_shape[1:] + fixed_batch_size.merge_with(batch_size) + for i, size in enumerate(input_size): + if size.value is None: + raise ValueError( + "Input size (dimension %d of inputs) must be accessible via " + "shape inference, but saw value None." % i) + else: + fixed_batch_size = first_input.get_shape().with_rank_at_least(1)[0] + + if fixed_batch_size.value: + batch_size = fixed_batch_size.value + else: + batch_size = array_ops.shape(first_input)[0] + if initial_state is not None: + state = initial_state + else: + if not dtype: + raise ValueError("If no initial_state is provided, " + "dtype must be specified") + state = cell.zero_state(batch_size, dtype) + + if sequence_length is not None: # Prepare variables + sequence_length = ops.convert_to_tensor( + sequence_length, name="sequence_length") + if sequence_length.get_shape().ndims not in (None, 1): + raise ValueError( + "sequence_length must be a vector of length batch_size") + + def _create_zero_output(output_size): + # convert int to TensorShape if necessary + size = _concat(batch_size, output_size) + output = array_ops.zeros( + array_ops.stack(size), _infer_state_dtype(dtype, state)) + shape = _concat(fixed_batch_size.value, output_size, static=True) + output.set_shape(tensor_shape.TensorShape(shape)) + return output + + output_size = cell.output_size + flat_output_size = nest.flatten(output_size) + flat_zero_output = tuple( + _create_zero_output(size) for size in flat_output_size) + zero_output = nest.pack_sequence_as( + structure=output_size, flat_sequence=flat_zero_output) + + sequence_length = math_ops.to_int32(sequence_length) + min_sequence_length = math_ops.reduce_min(sequence_length) + max_sequence_length = math_ops.reduce_max(sequence_length) + + for time, input_ in enumerate(inputs): + if time > 0: + varscope.reuse_variables() + # pylint: disable=cell-var-from-loop + call_cell = lambda: cell(input_, state) + # pylint: enable=cell-var-from-loop + if sequence_length is not None: + (output, state) = _rnn_step( + time=time, + sequence_length=sequence_length, + min_sequence_length=min_sequence_length, + max_sequence_length=max_sequence_length, + zero_output=zero_output, + state=state, + call_cell=call_cell, + state_size=cell.state_size) + else: + (output, state) = call_cell() + + outputs.append(output) + + return (outputs, state) + + +def static_state_saving_rnn(cell, + inputs, + state_saver, + state_name, + sequence_length=None, + scope=None): + """RNN that accepts a state saver for time-truncated RNN calculation. + + Args: + cell: An instance of `RNNCell`. + inputs: A length T list of inputs, each a `Tensor` of shape + `[batch_size, input_size]`. + state_saver: A state saver object with methods `state` and `save_state`. + state_name: Python string or tuple of strings. The name to use with the + state_saver. If the cell returns tuples of states (i.e., + `cell.state_size` is a tuple) then `state_name` should be a tuple of + strings having the same length as `cell.state_size`. Otherwise it should + be a single string. + sequence_length: (optional) An int32/int64 vector size [batch_size]. + See the documentation for rnn() for more details about sequence_length. + scope: VariableScope for the created subgraph; defaults to "rnn". + + Returns: + A pair (outputs, state) where: + outputs is a length T list of outputs (one for each input) + states is the final state + + Raises: + TypeError: If `cell` is not an instance of RNNCell. + ValueError: If `inputs` is `None` or an empty list, or if the arity and + type of `state_name` does not match that of `cell.state_size`. + """ + state_size = cell.state_size + state_is_tuple = nest.is_sequence(state_size) + state_name_tuple = nest.is_sequence(state_name) + + if state_is_tuple != state_name_tuple: + raise ValueError("state_name should be the same type as cell.state_size. " + "state_name: %s, cell.state_size: %s" % (str(state_name), + str(state_size))) + + if state_is_tuple: + state_name_flat = nest.flatten(state_name) + state_size_flat = nest.flatten(state_size) + + if len(state_name_flat) != len(state_size_flat): + raise ValueError("#elems(state_name) != #elems(state_size): %d vs. %d" % + (len(state_name_flat), len(state_size_flat))) + + initial_state = nest.pack_sequence_as( + structure=state_size, + flat_sequence=[state_saver.state(s) for s in state_name_flat]) + else: + initial_state = state_saver.state(state_name) + + (outputs, state) = static_rnn( + cell, + inputs, + initial_state=initial_state, + sequence_length=sequence_length, + scope=scope) + + if state_is_tuple: + flat_state = nest.flatten(state) + state_name = nest.flatten(state_name) + save_state = [ + state_saver.save_state(name, substate) + for name, substate in zip(state_name, flat_state) + ] + else: + save_state = [state_saver.save_state(state_name, state)] + + with ops.control_dependencies(save_state): + last_output = outputs[-1] + flat_last_output = nest.flatten(last_output) + flat_last_output = [ + array_ops.identity(output) for output in flat_last_output + ] + outputs[-1] = nest.pack_sequence_as( + structure=last_output, flat_sequence=flat_last_output) + + return (outputs, state) + + +def static_bidirectional_rnn(cell_fw, + cell_bw, + inputs, + initial_state_fw=None, + initial_state_bw=None, + dtype=None, + sequence_length=None, + scope=None): + """Creates a bidirectional recurrent neural network. + + Similar to the unidirectional case above (rnn) but takes input and builds + independent forward and backward RNNs with the final forward and backward + outputs depth-concatenated, such that the output will have the format + [time][batch][cell_fw.output_size + cell_bw.output_size]. The input_size of + forward and backward cell must match. The initial state for both directions + is zero by default (but can be set optionally) and no intermediate states are + ever returned -- the network is fully unrolled for the given (passed in) + length(s) of the sequence(s) or completely unrolled if length(s) is not given. + + Args: + cell_fw: An instance of RNNCell, to be used for forward direction. + cell_bw: An instance of RNNCell, to be used for backward direction. + inputs: A length T list of inputs, each a tensor of shape + [batch_size, input_size], or a nested tuple of such elements. + initial_state_fw: (optional) An initial state for the forward RNN. + This must be a tensor of appropriate type and shape + `[batch_size, cell_fw.state_size]`. + If `cell_fw.state_size` is a tuple, this should be a tuple of + tensors having shapes `[batch_size, s] for s in cell_fw.state_size`. + initial_state_bw: (optional) Same as for `initial_state_fw`, but using + the corresponding properties of `cell_bw`. + dtype: (optional) The data type for the initial state. Required if + either of the initial states are not provided. + sequence_length: (optional) An int32/int64 vector, size `[batch_size]`, + containing the actual lengths for each of the sequences. + scope: VariableScope for the created subgraph; defaults to + "bidirectional_rnn" + + Returns: + A tuple (outputs, output_state_fw, output_state_bw) where: + outputs is a length `T` list of outputs (one for each input), which + are depth-concatenated forward and backward outputs. + output_state_fw is the final state of the forward rnn. + output_state_bw is the final state of the backward rnn. + + Raises: + TypeError: If `cell_fw` or `cell_bw` is not an instance of `RNNCell`. + ValueError: If inputs is None or an empty list. + """ + + if not _like_rnncell(cell_fw): + raise TypeError("cell_fw must be an instance of RNNCell") + if not _like_rnncell(cell_bw): + raise TypeError("cell_bw must be an instance of RNNCell") + if not nest.is_sequence(inputs): + raise TypeError("inputs must be a sequence") + if not inputs: + raise ValueError("inputs must not be empty") + + with vs.variable_scope(scope or "bidirectional_rnn"): + # Forward direction + with vs.variable_scope("fw") as fw_scope: + output_fw, output_state_fw = static_rnn( + cell_fw, + inputs, + initial_state_fw, + dtype, + sequence_length, + scope=fw_scope) + + # Backward direction + with vs.variable_scope("bw") as bw_scope: + reversed_inputs = _reverse_seq(inputs, sequence_length) + tmp, output_state_bw = static_rnn( + cell_bw, + reversed_inputs, + initial_state_bw, + dtype, + sequence_length, + scope=bw_scope) + + output_bw = _reverse_seq(tmp, sequence_length) + # Concat each of the forward/backward outputs + flat_output_fw = nest.flatten(output_fw) + flat_output_bw = nest.flatten(output_bw) + + flat_outputs = tuple( + array_ops.concat([fw, bw], 1) + for fw, bw in zip(flat_output_fw, flat_output_bw)) + + outputs = nest.pack_sequence_as( + structure=output_fw, flat_sequence=flat_outputs) + + return (outputs, output_state_fw, output_state_bw) -- Gitee From 31af51f5068c1d157c8a2244da80826298c463e9 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:13:36 +0000 Subject: [PATCH 22/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/shuffle.py. --- .../script/shuffle.py | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/shuffle.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/shuffle.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/shuffle.py new file mode 100644 index 000000000..d929e4630 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/shuffle.py @@ -0,0 +1,71 @@ +# +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from npu_bridge.npu_init import * +import os +import sys +import random + +import tempfile +from subprocess import call + + +def main(file, temporary=False): + tf_os, tpath = tempfile.mkstemp(dir='~/DIN-V2-CODE') + tf = open(tpath, 'w') + + fd = open(file, "r") + for l in fd: + print >> tf, l.strip("\n") + tf.close() + + lines = open(tpath, 'r').readlines() + random.shuffle(lines) + if temporary: + path, filename = os.path.split(os.path.realpath(file)) + fd = tempfile.TemporaryFile(prefix=filename + '.shuf', dir=path) + else: + fd = open(file + '.shuf', 'w') + + for l in lines: + s = l.strip("\n") + print >> fd, s + + if temporary: + fd.seek(0) + else: + fd.close() + + os.remove(tpath) + + return fd + + +if __name__ == '__main__': + main(sys.argv[1]) -- Gitee From 41031db073899b2a419dc99e39e99eb320f7c03c Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:14:31 +0000 Subject: [PATCH 23/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/split_by_user.py. --- .../script/split_by_user.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/split_by_user.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/split_by_user.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/split_by_user.py new file mode 100644 index 000000000..50b21cbd2 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/split_by_user.py @@ -0,0 +1,48 @@ +# +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from npu_bridge.npu_init import * +import random + +fi = open("local_test", "r") +ftrain = open("local_train_splitByUser", "w") +ftest = open("local_test_splitByUser", "w") + +while True: + rand_int = random.randint(1, 10) + noclk_line = fi.readline().strip() + clk_line = fi.readline().strip() + if noclk_line == "" or clk_line == "": + break + if rand_int == 2: + print >> ftest, noclk_line + print >> ftest, clk_line + else: + print >> ftrain, noclk_line + print >> ftrain, clk_line \ No newline at end of file -- Gitee From a8a07fadc542e9b8af49cd419dd7e4e95598f70d Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:15:06 +0000 Subject: [PATCH 24/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py. --- .../script/train.py | 303 ++++++++++++++++++ 1 file changed, 303 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py new file mode 100644 index 000000000..c3a1465f9 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py @@ -0,0 +1,303 @@ +from npu_bridge.estimator import npu_ops +from tensorflow.core.protobuf.rewriter_config_pb2 import RewriterConfig + +import numpy +from data_iterator import DataIterator +import tensorflow as tf +# tf.enable_eager_execution() + +# from model import * +from model_general import * +import time +import random +import sys +from utils import * + +EMBEDDING_DIM = 64 +HIDDEN_SIZE = 16 * 2 +ATTENTION_SIZE = 16 * 2 +best_auc = 0.0 + +def prepare_data(input, target, maxlen = None, return_neg = False): + # x: a list of sentences + lengths_x = [len(s[4]) for s in input] + seqs_mid = [inp[3] for inp in input] + seqs_cat = [inp[4] for inp in input] + noclk_seqs_mid = [inp[5] for inp in input] + noclk_seqs_cat = [inp[6] for inp in input] + + if maxlen is not None: + new_seqs_mid = [] + new_seqs_cat = [] + new_noclk_seqs_mid = [] + new_noclk_seqs_cat = [] + new_lengths_x = [] + for l_x, inp in zip(lengths_x, input): + if l_x > maxlen: + new_seqs_mid.append(inp[3][l_x - maxlen:]) + new_seqs_cat.append(inp[4][l_x - maxlen:]) + new_noclk_seqs_mid.append(inp[5][l_x - maxlen:]) + new_noclk_seqs_cat.append(inp[6][l_x - maxlen:]) + new_lengths_x.append(maxlen) + else: + new_seqs_mid.append(inp[3]) + new_seqs_cat.append(inp[4]) + new_noclk_seqs_mid.append(inp[5]) + new_noclk_seqs_cat.append(inp[6]) + new_lengths_x.append(l_x) + lengths_x = new_lengths_x + seqs_mid = new_seqs_mid + seqs_cat = new_seqs_cat + noclk_seqs_mid = new_noclk_seqs_mid + noclk_seqs_cat = new_noclk_seqs_cat + + if len(lengths_x) < 1: + return None, None, None, None + + n_samples = len(seqs_mid) + # maxlen_x = numpy.max(lengths_x) + if maxlen is not None: + maxlen_x = maxlen + else: + maxlen_x = numpy.max(lengths_x) + neg_samples = len(noclk_seqs_mid[0][0]) + + mid_his = numpy.zeros((n_samples, maxlen_x)).astype('int64') + cat_his = numpy.zeros((n_samples, maxlen_x)).astype('int64') + noclk_mid_his = numpy.zeros((n_samples, maxlen_x, neg_samples)).astype('int64') + noclk_cat_his = numpy.zeros((n_samples, maxlen_x, neg_samples)).astype('int64') + mid_mask = numpy.zeros((n_samples, maxlen_x)).astype('float32') + for idx, [s_x, s_y, no_sx, no_sy] in enumerate(zip(seqs_mid, seqs_cat, noclk_seqs_mid, noclk_seqs_cat)): + mid_mask[idx, :lengths_x[idx]] = 1. + mid_his[idx, :lengths_x[idx]] = s_x + cat_his[idx, :lengths_x[idx]] = s_y + noclk_mid_his[idx, :lengths_x[idx], :] = no_sx + noclk_cat_his[idx, :lengths_x[idx], :] = no_sy + # lengths_x[idx] = maxlen + + uids = numpy.array([inp[0] for inp in input]) + mids = numpy.array([inp[1] for inp in input]) + cats = numpy.array([inp[2] for inp in input]) + + if return_neg: + return uids, mids, cats, mid_his, cat_his, mid_mask, numpy.array(target), numpy.array(lengths_x), noclk_mid_his, noclk_cat_his + + else: + return uids, mids, cats, mid_his, cat_his, mid_mask, numpy.array(target), numpy.array(lengths_x) + +def eval(sess, test_data, model, model_path): + # import pdb + # pdb.set_trace() + + loss_sum = 0. + accuracy_sum = 0. + aux_loss_sum = 0. + nums = 0 + stored_arr = [] + for src, tgt in test_data: + # print("!!!!!!", num) + nums += 1 + # uids, mids, cats, mid_his, cat_his, mid_mask, target, sl, noclk_mids, noclk_cats = prepare_data(src, tgt, return_neg=True) + uids, mids, cats, mid_his, cat_his, mid_mask, target, sl, noclk_mids, noclk_cats = prepare_data(src, tgt, maxlen=100, return_neg=True) + prob, loss, acc, aux_loss = model.calculate(sess, [uids, mids, cats, mid_his, cat_his, mid_mask, target, sl, noclk_mids, noclk_cats]) + loss_sum += loss + aux_loss_sum = aux_loss + accuracy_sum += acc + prob_1 = prob[:, 0].tolist() + target_1 = target[:, 0].tolist() + for p ,t in zip(prob_1, target_1): + stored_arr.append([p, t]) + test_auc = calc_auc(stored_arr) + accuracy_sum = accuracy_sum / nums + loss_sum = loss_sum / nums + aux_loss_sum / nums + global best_auc + if best_auc < test_auc: + best_auc = test_auc + model.save(sess, model_path) + return test_auc, loss_sum, accuracy_sum, aux_loss_sum + +def train( + train_file = "local_train_splitByUser", + test_file = "local_test_splitByUser", + uid_voc = "uid_voc.pkl", + mid_voc = "mid_voc.pkl", + cat_voc = "cat_voc.pkl", + batch_size = 128, + maxlen = 100, + test_iter = 100, + save_iter = 100, + model_type = 'DNN', + seed = 2, +): + model_path = "dnn_save_path/ckpt_noshuff" + model_type + str(seed) + best_model_path = "dnn_best_model/ckpt_noshuff" + model_type + str(seed) + tensorboard_path = "tensorboard_log" + gpu_options = tf.GPUOptions(allow_growth=True) + + config = tf.ConfigProto() + custom_op = config.graph_options.rewrite_options.custom_optimizers.add() + custom_op.name = "NpuOptimizer" + custom_op.parameter_map["use_off_line"].b = True #在昇腾AI处理器执行训练 + #custom_op.parameter_map["precision_mode"].s = tf.compat.as_bytes('allow_mix_precision') + custom_op.parameter_map["precision_mode"].s = tf.compat.as_bytes('allow_fp32_to_fp16') +# custom_op.parameter_map["modify_mixlist"].s = tf.compat.as_bytes("/npu/mwx927052/DIEN_ID0109_for_TensorFlow/ops_info.json") + + #custom_op.parameter_map["dump_path"].s = tf.compat.as_bytes("/npu/mwx927052/dien-npu/overflow") + #custom_op.parameter_map["enable_dump_debug"].b = True + #custom_op.parameter_map["dump_debug_mode"].s = tf.compat.as_bytes("all") + #custom_op.parameter_map["profiling_mode"].b = True + #custom_op.parameter_map["profiling_options"].s = tf.compat.as_bytes('{"output":"/npu/mwx927052/dien-npu/profiling","task_trace":"on","training_trace":"on","aicpu":"on","fp_point":"","bp_point":""}') + # custom_op.parameter_map["variable_memory_max_size"].s = tf.compat.as_bytes("12*1024*1024*1024") + config.graph_options.rewrite_options.remapping = RewriterConfig.OFF #关闭remap开关 + + # custom_op.parameter_map["enable_dump"].b = True + # custom_op.parameter_map["dump_path"].s = tf.compat.as_bytes("/data1/d00564369/dien/dump_mix") + # custom_op.parameter_map["dump_step"].s = tf.compat.as_bytes("0-5") + # custom_op.parameter_map["dump_mode"].s = tf.compat.as_bytes("all") + + with tf.Session(config=config) as sess: + train_data = DataIterator(train_file, uid_voc, mid_voc, cat_voc, batch_size, maxlen, shuffle_each_epoch=False) + test_data = DataIterator(test_file, uid_voc, mid_voc, cat_voc, batch_size, maxlen) + n_uid, n_mid, n_cat = train_data.get_n() + if model_type == 'DNN': + model = Model_DNN(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'PNN': + model = Model_PNN(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'Wide': + model = Model_WideDeep(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'DIN': + model = Model_DIN(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'DIN-V2-gru-att-gru': + model = Model_DIN_V2_Gru_att_Gru(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'DIN-V2-gru-gru-att': + model = Model_DIN_V2_Gru_Gru_att(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'DIN-V2-gru-qa-attGru': + model = Model_DIN_V2_Gru_QA_attGru(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'DIN-V2-gru-vec-attGru': + model = Model_DIN_V2_Gru_Vec_attGru(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'DIEN': + model = Model_DIN_V2_Gru_Vec_attGru_Neg(batch_size, maxlen, n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + else: + print ("Invalid model_type : %s", model_type) + return + # model = Model_DNN(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + sess.run(tf.global_variables_initializer()) + sess.run(tf.local_variables_initializer()) + sys.stdout.flush() + print('test_auc: %.4f ---- test_loss: %.4f ---- test_accuracy: %.4f ---- test_aux_loss: %.4f' % eval(sess, test_data, model, best_model_path)) + sys.stdout.flush() + + # summary_writer = tf.summary.FileWriter(tensorboard_path, tf.get_default_graph()) + + start = time.time() + iter = 0 + start_epoch = 0 + start_iter = 0 + a = 0 + lr = 0.001 * 0.5**(start_epoch) + + for itr in range(start_epoch, 3): + loss_sum = 0.0 + accuracy_sum = 0. + aux_loss_sum = 0. + # for src, tgt in train_data: + for iter, (src, tgt) in enumerate(train_data, start=start_iter): + # if a > 200: + # break + start_time = time.time() + uids, mids, cats, mid_his, cat_his, mid_mask, target, sl, noclk_mids, noclk_cats = prepare_data(src, tgt, maxlen, return_neg=True) + loss, acc, aux_loss = model.train(sess, [uids, mids, cats, mid_his, cat_his, mid_mask, target, sl, lr, noclk_mids, noclk_cats]) + end_time = time.time() + print("step_time:", end_time - start_time) + # tf.io.write_graph(sess.graph_def, '/data1/d00564369/dien-npu', 'train_graph.pbtxt') + loss_sum += loss + accuracy_sum += acc + aux_loss_sum += aux_loss + a += 1 + # sys.stdout.flush() + if (iter % test_iter) == 0: + avg_time_per_step = (time.time() - start) / test_iter + avg_examples_per_second = (test_iter * batch_size)/(time.time() - start) + + print("avg_time_per_step: ", avg_time_per_step) + print("avg_examples_per_second: ", avg_examples_per_second) + print("step_time:", end_time - start_time) + + print('[epoch: %d, iter: %d] ----> train_loss: %.4f ---- train_accuracy: %.4f ---- train_aux_loss: %.4f' % \ + (itr, iter, loss_sum / test_iter, accuracy_sum / test_iter, aux_loss_sum / test_iter)) + # print('test_auc: %.4f ----test_loss: %.4f ---- test_accuracy: %.4f ---- test_aux_loss: %.4f' % eval(sess, test_data, model, best_model_path)) + # model.summary_op(summary_writer, summary_str, iter) + start = time.time() + loss_sum = 0.0 + accuracy_sum = 0.0 + aux_loss_sum = 0.0 + #if (iter % save_iter) == 0: + if (iter % 10000) == 0: + print('save model epoch {}, iter: {}'.format(itr, iter)) + model.save(sess, model_path + "--" + str(itr)+"--"+str(iter)) + + start_iter = 0 + + lr *= 0.5 + +def test( + train_file = "local_train_splitByUser", + test_file = "local_test_splitByUser", + uid_voc = "uid_voc.pkl", + mid_voc = "mid_voc.pkl", + cat_voc = "cat_voc.pkl", + batch_size = 128, + maxlen = 100, + model_type = 'DNN', + seed = 2 +): + + model_path = "dnn_best_model/ckpt_noshuff" + model_type + str(seed) + gpu_options = tf.GPUOptions(allow_growth=True) + with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: + train_data = DataIterator(train_file, uid_voc, mid_voc, cat_voc, batch_size, maxlen) + # tofix, in test and eval stage, last batch cannot be discarded + test_data = DataIterator(test_file, uid_voc, mid_voc, cat_voc, batch_size, maxlen) + n_uid, n_mid, n_cat = train_data.get_n() + if model_type == 'DNN': + model = Model_DNN(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'PNN': + model = Model_PNN(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'Wide': + model = Model_WideDeep(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'DIN': + model = Model_DIN(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'DIN-V2-gru-att-gru': + model = Model_DIN_V2_Gru_att_Gru(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'DIN-V2-gru-gru-att': + model = Model_DIN_V2_Gru_Gru_att(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'DIN-V2-gru-qa-attGru': + model = Model_DIN_V2_Gru_QA_attGru(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'DIN-V2-gru-vec-attGru': + model = Model_DIN_V2_Gru_Vec_attGru(n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + elif model_type == 'DIEN': + model = Model_DIN_V2_Gru_Vec_attGru_Neg(batch_size, maxlen, n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE) + else: + print ("Invalid model_type : %s", model_type) + return + model.restore(sess, model_path) + print('test_auc: %.4f ----test_loss: %.4f ---- test_accuracy: %.4f ---- test_aux_loss: %.4f' % eval(sess, test_data, model, model_path)) + +if __name__ == '__main__': + if len(sys.argv) == 4: + SEED = int(sys.argv[3]) + else: + SEED = 3 + tf.set_random_seed(SEED) + numpy.random.seed(SEED) + random.seed(SEED) + + if sys.argv[1] == 'train': + train(model_type=sys.argv[2], seed=SEED) + elif sys.argv[1] == 'test': + test(model_type=sys.argv[2], seed=SEED) + else: + print('do nothing...') + + -- Gitee From fe0cba388d6e398cf5d2b5a7a876a842dc9c1d8d Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:17:38 +0000 Subject: [PATCH 25/38] update TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py. --- .../script/train.py | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py index c3a1465f9..b2cc74f59 100644 --- a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py @@ -139,16 +139,7 @@ def train( custom_op = config.graph_options.rewrite_options.custom_optimizers.add() custom_op.name = "NpuOptimizer" custom_op.parameter_map["use_off_line"].b = True #在昇腾AI处理器执行训练 - #custom_op.parameter_map["precision_mode"].s = tf.compat.as_bytes('allow_mix_precision') - custom_op.parameter_map["precision_mode"].s = tf.compat.as_bytes('allow_fp32_to_fp16') -# custom_op.parameter_map["modify_mixlist"].s = tf.compat.as_bytes("/npu/mwx927052/DIEN_ID0109_for_TensorFlow/ops_info.json") - - #custom_op.parameter_map["dump_path"].s = tf.compat.as_bytes("/npu/mwx927052/dien-npu/overflow") - #custom_op.parameter_map["enable_dump_debug"].b = True - #custom_op.parameter_map["dump_debug_mode"].s = tf.compat.as_bytes("all") - #custom_op.parameter_map["profiling_mode"].b = True - #custom_op.parameter_map["profiling_options"].s = tf.compat.as_bytes('{"output":"/npu/mwx927052/dien-npu/profiling","task_trace":"on","training_trace":"on","aicpu":"on","fp_point":"","bp_point":""}') - # custom_op.parameter_map["variable_memory_max_size"].s = tf.compat.as_bytes("12*1024*1024*1024") + custom_op.parameter_map["precision_mode"].s = tf.compat.as_bytes('allow_mix_precision') config.graph_options.rewrite_options.remapping = RewriterConfig.OFF #关闭remap开关 # custom_op.parameter_map["enable_dump"].b = True @@ -203,8 +194,8 @@ def train( aux_loss_sum = 0. # for src, tgt in train_data: for iter, (src, tgt) in enumerate(train_data, start=start_iter): - # if a > 200: - # break + if a > 500: + pass start_time = time.time() uids, mids, cats, mid_his, cat_his, mid_mask, target, sl, noclk_mids, noclk_cats = prepare_data(src, tgt, maxlen, return_neg=True) loss, acc, aux_loss = model.train(sess, [uids, mids, cats, mid_his, cat_his, mid_mask, target, sl, lr, noclk_mids, noclk_cats]) @@ -215,18 +206,18 @@ def train( accuracy_sum += acc aux_loss_sum += aux_loss a += 1 - # sys.stdout.flush() + sys.stdout.flush() if (iter % test_iter) == 0: avg_time_per_step = (time.time() - start) / test_iter - avg_examples_per_second = (test_iter * batch_size)/(time.time() - start) - + #avg_examples_per_second = (test_iter * batch_size)/(time.time() - start) + avg_examples_per_second = batch_size/(end_time - start_time) print("avg_time_per_step: ", avg_time_per_step) print("avg_examples_per_second: ", avg_examples_per_second) print("step_time:", end_time - start_time) print('[epoch: %d, iter: %d] ----> train_loss: %.4f ---- train_accuracy: %.4f ---- train_aux_loss: %.4f' % \ (itr, iter, loss_sum / test_iter, accuracy_sum / test_iter, aux_loss_sum / test_iter)) - # print('test_auc: %.4f ----test_loss: %.4f ---- test_accuracy: %.4f ---- test_aux_loss: %.4f' % eval(sess, test_data, model, best_model_path)) + print('test_auc: %.4f ----test_loss: %.4f ---- test_accuracy: %.4f ---- test_aux_loss: %.4f' % eval(sess, test_data, model, best_model_path)) # model.summary_op(summary_writer, summary_str, iter) start = time.time() loss_sum = 0.0 -- Gitee From 2ac41ad6837cc03eb36e5ce88d9c634e3ec2d383 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:18:39 +0000 Subject: [PATCH 26/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/utils.py. --- .../script/utils.py | 482 ++++++++++++++++++ 1 file changed, 482 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/utils.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/utils.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/utils.py new file mode 100644 index 000000000..6c5d5a372 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/utils.py @@ -0,0 +1,482 @@ +import tensorflow as tf +# from tensorflow.python.ops.rnn_cell import * +from tensorflow.contrib.rnn import * +from tensorflow.contrib.rnn.python.ops.rnn_cell import _Linear +# from tensorflow.python.ops import math_ops +# from tensorflow.python.ops import init_ops +# from tensorflow.python.ops import array_ops +# from tensorflow.python.ops import variable_scope as vs +import math + + +class QAAttGRUCell(RNNCell): + """Gated Recurrent Unit cell (cf. http://arxiv.org/abs/1406.1078). + Args: + num_units: int, The number of units in the GRU cell. + activation: Nonlinearity to use. Default: `tanh`. + reuse: (optional) Python boolean describing whether to reuse variables + in an existing scope. If not `True`, and the existing scope already has + the given variables, an error is raised. + kernel_initializer: (optional) The initializer to use for the weight and + projection matrices. + bias_initializer: (optional) The initializer to use for the bias. + """ + + def __init__(self, + num_units, + activation=None, + reuse=None, + kernel_initializer=None, + bias_initializer=None): + super(QAAttGRUCell, self).__init__(_reuse=reuse) + self._num_units = num_units + # self._activation = activation or math_ops.tanh + self._activation = activation or tf.tanh + self._kernel_initializer = kernel_initializer + self._bias_initializer = bias_initializer + self._gate_linear = None + self._candidate_linear = None + + @property + def state_size(self): + return self._num_units + + @property + def output_size(self): + return self._num_units + + def __call__(self, inputs, state, att_score): + return self.call(inputs, state, att_score) + + def call(self, inputs, state, att_score=None): + """Gated recurrent unit (GRU) with nunits cells.""" + if self._gate_linear is None: + bias_ones = self._bias_initializer + if self._bias_initializer is None: + # bias_ones = init_ops.constant_initializer(1.0, dtype=inputs.dtype) + bias_ones = tf.constant_initializer(1.0) + with tf.variable_scope("gates"): # Reset gate and update gate. + self._gate_linear = _Linear( + [inputs, state], + 2 * self._num_units, + True, + bias_initializer=bias_ones, + kernel_initializer=self._kernel_initializer) + + # value = math_ops.sigmoid(self._gate_linear([inputs, state])) + value = tf.sigmoid(self._gate_linear([inputs, state])) + r, u = tf.split(value=value, num_or_size_splits=2, axis=1) + + r_state = r * state + if self._candidate_linear is None: + with tf.variable_scope("candidate"): + self._candidate_linear = _Linear( + [inputs, r_state], + self._num_units, + True, + bias_initializer=self._bias_initializer, + kernel_initializer=self._kernel_initializer) + c = self._activation(self._candidate_linear([inputs, r_state])) + new_h = (1. - att_score) * state + att_score * c + return new_h, new_h + +class VecAttGRUCell(RNNCell): + """Gated Recurrent Unit cell (cf. http://arxiv.org/abs/1406.1078). + Args: + num_units: int, The number of units in the GRU cell. + activation: Nonlinearity to use. Default: `tanh`. + reuse: (optional) Python boolean describing whether to reuse variables + in an existing scope. If not `True`, and the existing scope already has + the given variables, an error is raised. + kernel_initializer: (optional) The initializer to use for the weight and + projection matrices. + bias_initializer: (optional) The initializer to use for the bias. + """ + + def __init__(self, + num_units, + activation=None, + reuse=None, + kernel_initializer=None, + bias_initializer=None): + super(VecAttGRUCell, self).__init__(_reuse=reuse) + self._num_units = num_units + # self._activation = activation or math_ops.tanh + self._activation = activation or tf.tanh + self._kernel_initializer = kernel_initializer + self._bias_initializer = bias_initializer + self._gate_linear = None + self._candidate_linear = None + + @property + def state_size(self): + return self._num_units + + @property + def output_size(self): + return self._num_units + def __call__(self, inputs, state, att_score): + return self.call(inputs, state, att_score) + def call(self, inputs, state, att_score=None): + """Gated recurrent unit (GRU) with nunits cells.""" + if self._gate_linear is None: + bias_ones = self._bias_initializer + if self._bias_initializer is None: + bias_ones = tf.constant_initializer(1.0) + # bias_ones = init_ops.constant_initializer(1.0, dtype=inputs.dtype) + with tf.variable_scope("gates"): # Reset gate and update gate. + self._gate_linear = _Linear( + [inputs, state], + 2 * self._num_units, + True, + bias_initializer=bias_ones, + kernel_initializer=self._kernel_initializer) + + # value = math_ops.sigmoid(self._gate_linear([inputs, state])) + value = tf.sigmoid(self._gate_linear([inputs, state])) + r, u = tf.split(value=value, num_or_size_splits=2, axis=1) + + r_state = r * state + if self._candidate_linear is None: + with tf.variable_scope("candidate"): + self._candidate_linear = _Linear( + [inputs, r_state], + self._num_units, + True, + bias_initializer=self._bias_initializer, + kernel_initializer=self._kernel_initializer) + c = self._activation(self._candidate_linear([inputs, r_state])) + print("????????????????? att", att_score.get_shape().as_list()) + print("????????????????? u", u.get_shape().as_list()) + u = (1.0 - att_score) * u + new_h = u * state + (1 - u) * c + return new_h, new_h + +class VecAttGRUCellV2(RNNCell): + """Gated Recurrent Unit cell (cf. http://arxiv.org/abs/1406.1078). + Args: + num_units: int, The number of units in the GRU cell. + activation: Nonlinearity to use. Default: `tanh`. + reuse: (optional) Python boolean describing whether to reuse variables + in an existing scope. If not `True`, and the existing scope already has + the given variables, an error is raised. + kernel_initializer: (optional) The initializer to use for the weight and + projection matrices. + bias_initializer: (optional) The initializer to use for the bias. + """ + + def __init__(self, + num_units, + activation=None, + reuse=None, + kernel_initializer=None, + bias_initializer=None): + super(VecAttGRUCell, self).__init__(_reuse=reuse) + self._num_units = num_units + # self._activation = activation or math_ops.tanh + self._activation = activation or tf.tanh + self._kernel_initializer = kernel_initializer + self._bias_initializer = bias_initializer + self._gate_linear = None + self._candidate_linear = None + + @property + def state_size(self): + return self._num_units + + @property + def output_size(self): + return self._num_units + def __call__(self, inputs, state, att_score): + return self.call(inputs, state, att_score) + def call(self, inputs, state, att_score=None): + """Gated recurrent unit (GRU) with nunits cells.""" + stdv = 1.0 / math.sqrt(self._num_units) + with tf.variable_scope("input_gates"): # Reset gate and update gate. + self._input_gate_linear = _Linear( + inputs, + 3 * self._num_units, + True, + bias_initializer=init_ops.random_uniform_initializer(-stdv, stdv), + kernel_initializer=init_ops.random_uniform_initializer(-stdv, stdv)) + with tf.variable_scope("hidden_gates"): # Reset gate and update gate. + self._hidden_gate_linear = _Linear( + state, + 3 * self._num_units, + True, + bias_initializer=init_ops.random_uniform_initializer(-stdv, stdv), + kernel_initializer=init_ops.random_uniform_initializer(-stdv, stdv)) + + # value = math_ops.sigmoid(self._gate_linear([inputs, state])) + input_value = self._input_gate_linear(inputs) + hidden_value = self._hidden_gate_linear(state) + i_i, i_r, i_n = tf.split(value=input_value, num_or_size_splits=3, axis=1) + h_i, h_r, h_n = tf.split(value=hidden_value, num_or_size_splits=3, axis=1) + + i_t = tf.sigmoid(i_i + h_i) + u_t = (1.0 - att_score) * i_t + r_t = tf.sigmoid(i_r + h_r) + n_t = self._activation(i_n + r_t * h_n) + new_h = u_t * state + (1 - u_t) * n_t + + return new_h, new_h + + +def prelu(_x, scope=''): + """parametric ReLU activation""" + with tf.variable_scope(name_or_scope=scope, default_name="prelu"): + _alpha = tf.get_variable("prelu_"+scope, shape=_x.get_shape()[-1], + dtype=_x.dtype, initializer=tf.constant_initializer(0.1)) + return tf.maximum(0.0, _x) + _alpha * tf.minimum(0.0, _x) + +def calc_auc(raw_arr): + """Summary + + Args: + raw_arr (TYPE): Description + + Returns: + TYPE: Description + """ + + arr = sorted(raw_arr, key=lambda d:d[0], reverse=True) + pos, neg = 0., 0. + for record in arr: + if record[1] == 1.: + pos += 1 + else: + neg += 1 + + fp, tp = 0., 0. + xy_arr = [] + for record in arr: + if record[1] == 1.: + tp += 1 + else: + fp += 1 + xy_arr.append([fp/neg, tp/pos]) + + auc = 0. + prev_x = 0. + prev_y = 0. + for x, y in xy_arr: + if x != prev_x: + auc += ((x - prev_x) * (y + prev_y) / 2.) + prev_x = x + prev_y = y + + return auc + +def attention(query, facts, attention_size, mask, stag='null', mode='LIST', softmax_stag=1, time_major=False, return_alphas=False): + if isinstance(facts, tuple): + # In case of Bi-RNN, concatenate the forward and the backward RNN outputs. + facts = tf.concat(facts, 2) + + if time_major: + # (T,B,D) => (B,T,D) + facts = tf.transpose(facts, [1, 0, 2]) + + mask = tf.equal(mask, tf.ones_like(mask)) + hidden_size = facts.get_shape().as_list()[-1] # D value - hidden size of the RNN layer + input_size = query.get_shape().as_list()[-1] + + # Trainable parameters + w1 = tf.Variable(tf.random_normal([hidden_size, attention_size], stddev=0.1)) + w2 = tf.Variable(tf.random_normal([input_size, attention_size], stddev=0.1)) + b = tf.Variable(tf.random_normal([attention_size], stddev=0.1)) + v = tf.Variable(tf.random_normal([attention_size], stddev=0.1)) + + with tf.name_scope('v'): + # Applying fully connected layer with non-linear activation to each of the B*T timestamps; + # the shape of `tmp` is (B,T,D)*(D,A)=(B,T,A), where A=attention_size + tmp1 = tf.tensordot(facts, w1, axes=1) + tmp2 = tf.tensordot(query, w2, axes=1) + tmp2 = tf.reshape(tmp2, [-1, 1, tf.shape(tmp2)[-1]]) + tmp = tf.tanh((tmp1 + tmp2) + b) + + # For each of the timestamps its vector of size A from `tmp` is reduced with `v` vector + v_dot_tmp = tf.tensordot(tmp, v, axes=1, name='v_dot_tmp') # (B,T) shape + key_masks = mask # [B, 1, T] + # key_masks = tf.expand_dims(mask, 1) # [B, 1, T] + paddings = tf.ones_like(v_dot_tmp) * (-2 ** 32 + 1) + v_dot_tmp = tf.where(key_masks, v_dot_tmp, paddings) # [B, 1, T] + alphas = tf.nn.softmax(v_dot_tmp, name='alphas') # (B,T) shape + + # Output of (Bi-)RNN is reduced with attention vector; the result has (B,D) shape + #output = tf.reduce_sum(facts * tf.expand_dims(alphas, -1), 1) + output = facts * tf.expand_dims(alphas, -1) + output = tf.reshape(output, tf.shape(facts)) + # output = output / (facts.get_shape().as_list()[-1] ** 0.5) + if not return_alphas: + return output + else: + return output, alphas + +def din_attention(query, facts, attention_size, mask, stag='null', mode='SUM', softmax_stag=1, time_major=False, return_alphas=False): + if isinstance(facts, tuple): + # In case of Bi-RNN, concatenate the forward and the backward RNN outputs. + facts = tf.concat(facts, 2) + print ("querry_size mismatch") + query = tf.concat(values = [ + query, + query, + ], axis=1) + + if time_major: + # (T,B,D) => (B,T,D) + facts = tf.transpose(facts, [1, 0, 2]) + mask = tf.equal(mask, tf.ones_like(mask)) + facts_size = facts.get_shape().as_list()[-1] # D value - hidden size of the RNN layer + querry_size = query.get_shape().as_list()[-1] + queries = tf.tile(query, [1, tf.shape(facts)[1]]) + queries = tf.reshape(queries, tf.shape(facts)) + din_all = tf.concat([queries, facts, queries-facts, queries*facts], axis=-1) + d_layer_1_all = tf.layers.dense(din_all, 80, activation=tf.nn.sigmoid, name='f1_att' + stag) + d_layer_2_all = tf.layers.dense(d_layer_1_all, 40, activation=tf.nn.sigmoid, name='f2_att' + stag) + d_layer_3_all = tf.layers.dense(d_layer_2_all, 1, activation=None, name='f3_att' + stag) + d_layer_3_all = tf.reshape(d_layer_3_all, [-1, 1, tf.shape(facts)[1]]) + scores = d_layer_3_all + # Mask + # key_masks = tf.sequence_mask(facts_length, tf.shape(facts)[1]) # [B, T] + key_masks = tf.expand_dims(mask, 1) # [B, 1, T] + paddings = tf.ones_like(scores) * (-2 ** 32 + 1) + scores = tf.where(key_masks, scores, paddings) # [B, 1, T] + + # Scale + # scores = scores / (facts.get_shape().as_list()[-1] ** 0.5) + + # Activation + if softmax_stag: + scores = tf.nn.softmax(scores) # [B, 1, T] + + # Weighted sum + if mode == 'SUM': + output = tf.matmul(scores, facts) # [B, 1, H] + # output = tf.reshape(output, [-1, tf.shape(facts)[-1]]) + else: + scores = tf.reshape(scores, [-1, tf.shape(facts)[1]]) + output = facts * tf.expand_dims(scores, -1) + output = tf.reshape(output, tf.shape(facts)) + return output + +def din_fcn_attention(query, facts, attention_size, mask, stag='null', mode='SUM', softmax_stag=1, time_major=False, return_alphas=False, forCnn=False): + if isinstance(facts, tuple): + # In case of Bi-RNN, concatenate the forward and the backward RNN outputs. + facts = tf.concat(facts, 2) + if len(facts.get_shape().as_list()) == 2: + facts = tf.expand_dims(facts, 1) + + if time_major: + # (T,B,D) => (B,T,D) + facts = tf.transpose(facts, [1, 0, 2]) + # Trainable parameters + mask = tf.equal(mask, tf.ones_like(mask)) + facts_size = facts.get_shape().as_list()[-1] # D value - hidden size of the RNN layer + querry_size = query.get_shape().as_list()[-1] + query = tf.layers.dense(query, facts_size, activation=None, name='f1' + stag) + query = prelu(query) + queries = tf.tile(query, [1, tf.shape(facts)[1]]) + queries = tf.reshape(queries, tf.shape(facts)) + din_all = tf.concat([queries, facts, queries-facts, queries*facts], axis=-1) + d_layer_1_all = tf.layers.dense(din_all, 80, activation=tf.nn.sigmoid, name='f1_att' + stag) + d_layer_2_all = tf.layers.dense(d_layer_1_all, 40, activation=tf.nn.sigmoid, name='f2_att' + stag) + d_layer_3_all = tf.layers.dense(d_layer_2_all, 1, activation=None, name='f3_att' + stag) + d_layer_3_all = tf.reshape(d_layer_3_all, [-1, 1, tf.shape(facts)[1]]) + scores = d_layer_3_all + # Mask + # key_masks = tf.sequence_mask(facts_length, tf.shape(facts)[1]) # [B, T] + key_masks = tf.expand_dims(mask, 1) # [B, 1, T] + paddings = tf.ones_like(scores) * (-2 ** 32 + 1) + if not forCnn: + scores = tf.where(key_masks, scores, paddings) # [B, 1, T] + + # Scale + # scores = scores / (facts.get_shape().as_list()[-1] ** 0.5) + + # Activation + if softmax_stag: + scores = tf.nn.softmax(scores) # [B, 1, T] + + # Weighted sum + if mode == 'SUM': + output = tf.matmul(scores, facts) # [B, 1, H] + # output = tf.reshape(output, [-1, tf.shape(facts)[-1]]) + else: + scores = tf.reshape(scores, [-1, tf.shape(facts)[1]]) + output = facts * tf.expand_dims(scores, -1) + output = tf.reshape(output, tf.shape(facts)) + if return_alphas: + return output, scores + return output + +def self_attention(facts, ATTENTION_SIZE, mask, stag='null'): + if len(facts.get_shape().as_list()) == 2: + facts = tf.expand_dims(facts, 1) + + def cond(batch, output, i): + return tf.less(i, tf.shape(batch)[1]) + + def body(batch, output, i): + self_attention_tmp = din_fcn_attention(batch[:, i, :], batch[:, 0:i+1, :], + ATTENTION_SIZE, mask[:, 0:i+1], softmax_stag=1, stag=stag, + mode='LIST') + self_attention_tmp = tf.reduce_sum(self_attention_tmp, 1) + output = output.write(i, self_attention_tmp) + return batch, output, i + 1 + + output_ta = tf.TensorArray(dtype=tf.float32, + size=0, + dynamic_size=True, + element_shape=(facts[:, 0, :].get_shape())) + _, output_op, _ = tf.while_loop(cond, body, [facts, output_ta, 0]) + self_attention = output_op.stack() + self_attention = tf.transpose(self_attention, perm = [1, 0, 2]) + return self_attention + +def self_all_attention(facts, ATTENTION_SIZE, mask, stag='null'): + if len(facts.get_shape().as_list()) == 2: + facts = tf.expand_dims(facts, 1) + + def cond(batch, output, i): + return tf.less(i, tf.shape(batch)[1]) + + def body(batch, output, i): + self_attention_tmp = din_fcn_attention(batch[:, i, :], batch, + ATTENTION_SIZE, mask, softmax_stag=1, stag=stag, + mode='LIST') + self_attention_tmp = tf.reduce_sum(self_attention_tmp, 1) + output = output.write(i, self_attention_tmp) + return batch, output, i + 1 + + output_ta = tf.TensorArray(dtype=tf.float32, + size=0, + dynamic_size=True, + element_shape=(facts[:, 0, :].get_shape())) + _, output_op, _ = tf.while_loop(cond, body, [facts, output_ta, 0]) + self_attention = output_op.stack() + self_attention = tf.transpose(self_attention, perm = [1, 0, 2]) + return self_attention + +def din_fcn_shine(query, facts, attention_size, mask, stag='null', mode='SUM', softmax_stag=1, time_major=False, return_alphas=False): + if isinstance(facts, tuple): + # In case of Bi-RNN, concatenate the forward and the backward RNN outputs. + facts = tf.concat(facts, 2) + + if time_major: + # (T,B,D) => (B,T,D) + facts = tf.transpose(facts, [1, 0, 2]) + # Trainable parameters + mask = tf.equal(mask, tf.ones_like(mask)) + facts_size = facts.get_shape().as_list()[-1] # D value - hidden size of the RNN layer + querry_size = query.get_shape().as_list()[-1] + query = tf.layers.dense(query, facts_size, activation=None, name='f1_trans_shine' + stag) + query = prelu(query) + queries = tf.tile(query, [1, tf.shape(facts)[1]]) + queries = tf.reshape(queries, tf.shape(facts)) + din_all = tf.concat([queries, facts, queries-facts, queries*facts], axis=-1) + d_layer_1_all = tf.layers.dense(din_all, facts_size, activation=tf.nn.sigmoid, name='f1_shine_att' + stag) + d_layer_2_all = tf.layers.dense(d_layer_1_all, facts_size, activation=tf.nn.sigmoid, name='f2_shine_att' + stag) + d_layer_2_all = tf.reshape(d_layer_2_all, tf.shape(facts)) + output = d_layer_2_all + return output + -- Gitee From 4f1eb173c32b8cc924dd1cf6e19d5cc25ff18d34 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:19:15 +0000 Subject: [PATCH 27/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model_general.py. --- .../script/model_general.py | 463 ++++++++++++++++++ 1 file changed, 463 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model_general.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model_general.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model_general.py new file mode 100644 index 000000000..822ba0981 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model_general.py @@ -0,0 +1,463 @@ +import tensorflow as tf +from my_rnn import dynamic_rnn +# from tensorflow.contrib import rnn +from tensorflow.contrib.rnn import GRUCell +from utils import * +from Dice import dice +from tensorflow.contrib.cudnn_rnn.python.layers import CudnnGRU +from tensorflow.python.framework import dtypes +import numpy as np +from npu_bridge.npu_init import * + +fusion_mode = 1 +npu_mode = 1 +fp16_and_no_seq_len_mode = 1 + +id_num = 2 + +class Model(object): + def __init__(self, batch_size, maxlen, n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE, use_negsampling = False): + with tf.name_scope('Inputs'): + self.batch_size = batch_size + self.maxlen = maxlen + self.mid_his_batch_ph = tf.placeholder(tf.int32, [batch_size, maxlen], name='mid_his_batch_ph') + self.cat_his_batch_ph = tf.placeholder(tf.int32, [batch_size, maxlen], name='cat_his_batch_ph') + self.uid_batch_ph = tf.placeholder(tf.int32, [batch_size, ], name='uid_batch_ph') + self.mid_batch_ph = tf.placeholder(tf.int32, [batch_size, ], name='mid_batch_ph') + self.cat_batch_ph = tf.placeholder(tf.int32, [batch_size, ], name='cat_batch_ph') + self.mask = tf.placeholder(tf.float32, [batch_size, maxlen], name='mask') + self.seq_len_ph = tf.placeholder(tf.int32, [batch_size], name='seq_len_ph') + self.target_ph = tf.placeholder(tf.float32, [batch_size, 2], name='target_ph') + self.lr = tf.placeholder(tf.float64, []) + + if id_num > 2: + self.fake_his_batch_ph = [] + self.fake_batch_ph = [] + for i in range(id_num-2): + fake_his_batch_ph_name = 'fake_his_batch_ph' + str(i) + fake_batch_ph_name = "fake_batch_ph" + str(i) + fake_his_batch_ph_i = tf.placeholder(tf.int32, [batch_size, maxlen], name=fake_his_batch_ph_name) + fake_batch_ph_i = tf.placeholder(tf.int32, [batch_size, ], name=fake_batch_ph_name) + self.fake_his_batch_ph.append(fake_his_batch_ph_i) + self.fake_batch_ph.append(fake_batch_ph_i) + + self.use_negsampling =use_negsampling + if use_negsampling: + self.noclk_mid_batch_ph = tf.placeholder(tf.int32, [batch_size, maxlen, 5], name='noclk_mid_batch_ph') #generate 3 item IDs from negative sampling. + self.noclk_cat_batch_ph = tf.placeholder(tf.int32, [batch_size, maxlen, 5], name='noclk_cat_batch_ph') + if id_num > 2: + self.noclk_fake_batch_ph = [] + for i in range(id_num-2): + noclk_fake_batch_ph_name = 'noclk_fake_batch_ph' + str(i) + noclk_fake_batch_ph_i = tf.placeholder(tf.int32, [batch_size, maxlen, 5], name=noclk_fake_batch_ph_name) + + self.noclk_fake_batch_ph.append(noclk_fake_batch_ph_i) + + # Embedding layer + with tf.name_scope('Embedding_layer'): + self.uid_embeddings_var = tf.get_variable("uid_embedding_var", [n_uid, EMBEDDING_DIM]) + tf.summary.histogram('uid_embeddings_var', self.uid_embeddings_var) + self.uid_batch_embedded = embedding_lookup_npu(self.uid_embeddings_var, self.uid_batch_ph) + + self.mid_embeddings_var = tf.get_variable("mid_embedding_var", [n_mid, EMBEDDING_DIM]) + tf.summary.histogram('mid_embeddings_var', self.mid_embeddings_var) + self.mid_batch_embedded = embedding_lookup_npu(self.mid_embeddings_var, self.mid_batch_ph) + self.mid_his_batch_embedded = embedding_lookup_npu(self.mid_embeddings_var, self.mid_his_batch_ph) + if self.use_negsampling: + self.noclk_mid_his_batch_embedded = embedding_lookup_npu(self.mid_embeddings_var, self.noclk_mid_batch_ph) + + self.cat_embeddings_var = tf.get_variable("cat_embedding_var", [n_cat, EMBEDDING_DIM]) + tf.summary.histogram('cat_embeddings_var', self.cat_embeddings_var) + self.cat_batch_embedded = embedding_lookup_npu(self.cat_embeddings_var, self.cat_batch_ph) + self.cat_his_batch_embedded = embedding_lookup_npu(self.cat_embeddings_var, self.cat_his_batch_ph) + if self.use_negsampling: + self.noclk_cat_his_batch_embedded = embedding_lookup_npu(self.cat_embeddings_var, self.noclk_cat_batch_ph) + + if id_num > 2: + self.fake_batch_embedded = [] + self.fake_his_batch_embedded = [] + self.noclk_fake_his_batch_embedded = [] + + for i in range(id_num - 2): + table_name = "fake_embeddings_var" + str(i) + self.fake_embeddings_var = tf.get_variable(table_name, [n_cat, EMBEDDING_DIM]) + self.fake_batch_embedded.append(embedding_lookup_npu(self.fake_embeddings_var, self.fake_batch_ph[i])) + self.fake_his_batch_embedded.append(embedding_lookup_npu(self.fake_embeddings_var, self.fake_his_batch_ph[i])) + if self.use_negsampling: + self.noclk_fake_his_batch_embedded.append(embedding_lookup_npu(self.fake_embeddings_var, self.noclk_fake_batch_ph[i])) + + if id_num > 2: + self.item_eb = tf.concat([self.mid_batch_embedded, self.cat_batch_embedded] + self.fake_batch_embedded, 1) + self.item_his_eb = tf.concat([self.mid_his_batch_embedded, self.cat_his_batch_embedded] + self.fake_his_batch_embedded, 2) + else: + self.item_eb = tf.concat([self.mid_batch_embedded, self.cat_batch_embedded], 1) + self.item_his_eb = tf.concat([self.mid_his_batch_embedded, self.cat_his_batch_embedded], 2) + self.item_his_eb_sum = tf.reduce_sum(self.item_his_eb, 1) + if self.use_negsampling: + if id_num > 2: + concat_set = [] + for i in range(id_num - 2): + concat_set.append(self.noclk_fake_his_batch_embedded[i][:, :, 0, :]) + self.noclk_item_his_eb = tf.concat( + [self.noclk_mid_his_batch_embedded[:, :, 0, :], self.noclk_cat_his_batch_embedded[:, :, 0, :]] + concat_set, -1)# 0 means only using the first negative item ID. 3 item IDs are inputed in the line 24. + else: + self.noclk_item_his_eb = tf.concat( + [self.noclk_mid_his_batch_embedded[:, :, 0, :], self.noclk_cat_his_batch_embedded[:, :, 0, :]], -1)# 0 means only using the first negative item ID. 3 item IDs are inputed in the line 24. + self.noclk_item_his_eb = tf.reshape(self.noclk_item_his_eb, + [-1, tf.shape(self.noclk_mid_his_batch_embedded)[1], EMBEDDING_DIM * id_num])# cat embedding 18 concate item embedding 18. + + # self.noclk_his_eb = tf.concat([self.noclk_mid_his_batch_embedded, self.noclk_cat_his_batch_embedded], -1) + # self.noclk_his_eb_sum_1 = tf.reduce_sum(self.noclk_his_eb, 2) + # self.noclk_his_eb_sum = tf.reduce_sum(self.noclk_his_eb_sum_1, 1) + + def build_fcn_net(self, inp, use_dice = False): + bn1 = tf.layers.batch_normalization(inputs=inp, name='bn1') + dnn1 = tf.layers.dense(bn1, 200, activation=None, name='f1') + if use_dice: + if fp16_and_no_seq_len_mode: + dnn1 = dice(dnn1, epsilon=1e-7, name='dice_1') + else: + dnn1 = dice(dnn1, name='dice_1') + else: + dnn1 = prelu(dnn1, 'prelu1') + + dnn2 = tf.layers.dense(dnn1, 80, activation=None, name='f2') + if use_dice: + if fp16_and_no_seq_len_mode: + dnn2 = dice(dnn2, epsilon=1e-7, name='dice_2') + else: + dnn2 = dice(dnn2, name='dice_2') + else: + dnn2 = prelu(dnn2, 'prelu2') + dnn3 = tf.layers.dense(dnn2, 2, activation=None, name='f3') + if fp16_and_no_seq_len_mode: + self.y_hat = tf.nn.softmax(dnn3) + 1e-7 + else: + self.y_hat = tf.nn.softmax(dnn3) + 0.00000001 + + with tf.name_scope('Metrics'): + # Cross-entropy loss and optimizer initialization + ctr_loss = - tf.reduce_mean(tf.log(self.y_hat) * self.target_ph) + self.loss = ctr_loss + if self.use_negsampling: + self.loss += self.aux_loss + tf.summary.scalar('loss', self.loss) + if fp16_and_no_seq_len_mode: + print("111111111111111111111111111 if") + # self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr, epsilon=6e-8).minimize(self.loss) + self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr, epsilon=1e-4) + + # loss_scale_manager = FixedLossScaleManager(loss_scale=1) + # self.optimizer = NPULossScaleOptimizer(self.optimizer, loss_scale_manager) + + # grads = self.optimizer.compute_gradients(self.loss * 2**12) + # grads = [(grad / 2**12, var) for grad, var in grads] + grads = self.optimizer.compute_gradients(self.loss) + grads = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in grads] + self.optimizer = self.optimizer.apply_gradients(grads) + ### for npu + # loss_scale_manager = FixedLossScaleManager(loss_scale=1) + # self.optimizer = NPULossScaleOptimizer(self.optimizer, loss_scale_manager) + + ### for npu + else: + print("222222222222222222222222222 else") + #self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.loss) + ### for npu + self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr) + loss_scale_manager = FixedLossScaleManager(loss_scale=1) + self.optimizer = NPULossScaleOptimizer(self.optimizer, loss_scale_manager) + self.optimizer = self.optimizer.minimize(self.loss) + ### for npu + + # Accuracy metric + self.accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.round(self.y_hat), self.target_ph), tf.float32)) + tf.summary.scalar('accuracy', self.accuracy) + + self.merged = tf.summary.merge_all() + + def auxiliary_loss(self, h_states, click_seq, noclick_seq, mask, stag = None): + mask = tf.cast(mask, tf.float32) + click_input_ = tf.concat([h_states, click_seq], -1) + noclick_input_ = tf.concat([h_states, noclick_seq], -1) + click_prop_ = self.auxiliary_net(click_input_, stag = stag)[:, :, 0] + noclick_prop_ = self.auxiliary_net(noclick_input_, stag = stag)[:, :, 0] + click_loss_ = - tf.reshape(tf.log(click_prop_), [-1, tf.shape(click_seq)[1]]) * mask + noclick_loss_ = - tf.reshape(tf.log(1.0 - noclick_prop_), [-1, tf.shape(noclick_seq)[1]]) * mask + loss_ = tf.reduce_sum(click_loss_ + noclick_loss_) / (tf.reduce_sum(mask) + 1e-7) + return loss_ + + def auxiliary_net(self, in_, stag='auxiliary_net'): + print(">>>>>>>>>>>>>>>>>>>>>in_", in_.get_shape().as_list()) + bn1 = tf.layers.batch_normalization(inputs=in_, name='bn1' + stag, reuse=tf.AUTO_REUSE) + dnn1 = tf.layers.dense(bn1, 100, activation=None, name='f1' + stag, reuse=tf.AUTO_REUSE) + dnn1 = tf.nn.sigmoid(dnn1) + dnn2 = tf.layers.dense(dnn1, 50, activation=None, name='f2' + stag, reuse=tf.AUTO_REUSE) + dnn2 = tf.nn.sigmoid(dnn2) + dnn3 = tf.layers.dense(dnn2, 2, activation=None, name='f3' + stag, reuse=tf.AUTO_REUSE) + if fp16_and_no_seq_len_mode: + y_hat = tf.nn.softmax(dnn3) + 1e-7 + else: + y_hat = tf.nn.softmax(dnn3) + 0.00000001 + return y_hat + + def train(self, sess, inps): + if self.use_negsampling: + # op = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) + # for key in op: + # if "dynamic_gru_v2" in key.name: + # print(key.name, sess.run(key)) + + if id_num > 2: + fake_his_batch_ph = np.random.randint(0, 1600, size=(id_num - 2, self.batch_size, self.maxlen), dtype = np.int32) + fake_batch_ph = np.random.randint(0, 1600, size=(id_num - 2, self.batch_size, ), dtype = np.int32) + noclk_fake_batch_ph = np.random.randint(0, 1600, size=(id_num - 2, self.batch_size, self.maxlen, 5), dtype = np.int32) + + feed_dict={ + self.uid_batch_ph: inps[0], + self.mid_batch_ph: inps[1], + self.cat_batch_ph: inps[2], + self.mid_his_batch_ph: inps[3], + self.cat_his_batch_ph: inps[4], + self.mask: inps[5], + self.target_ph: inps[6], + self.seq_len_ph: inps[7], + self.lr: inps[8], + self.noclk_mid_batch_ph: inps[9], + self.noclk_cat_batch_ph: inps[10], + } + if id_num > 2: + for i in range(id_num-2): + feed_dict[self.fake_his_batch_ph[i]] = fake_his_batch_ph[i] + feed_dict[self.fake_batch_ph[i]] = fake_batch_ph[i] + feed_dict[self.noclk_fake_batch_ph[i]] = noclk_fake_batch_ph[i] + + loss, accuracy, aux_loss, _= sess.run([self.loss, self.accuracy, self.aux_loss, self.optimizer], feed_dict=feed_dict) + else: + loss, accuracy, aux_loss, _= sess.run([self.loss, self.accuracy, self.aux_loss, self.optimizer], feed_dict={ + self.uid_batch_ph: inps[0], + self.mid_batch_ph: inps[1], + self.cat_batch_ph: inps[2], + self.mid_his_batch_ph: inps[3], + self.cat_his_batch_ph: inps[4], + self.mask: inps[5], + self.target_ph: inps[6], + self.seq_len_ph: inps[7], + self.lr: inps[8], + self.noclk_mid_batch_ph: inps[9], + self.noclk_cat_batch_ph: inps[10], + }) + return loss, accuracy, aux_loss + else: + loss, accuracy, _, summary_str = sess.run([self.loss, self.accuracy, self.optimizer, self.merged], feed_dict={ + self.uid_batch_ph: inps[0], + self.mid_batch_ph: inps[1], + self.cat_batch_ph: inps[2], + self.mid_his_batch_ph: inps[3], + self.cat_his_batch_ph: inps[4], + self.mask: inps[5], + self.target_ph: inps[6], + self.seq_len_ph: inps[7], + self.lr: inps[8], + }) + return loss, accuracy, 0, summary_str + + def calculate(self, sess, inps): + if self.use_negsampling: + probs, loss, accuracy, aux_loss = sess.run([self.y_hat, self.loss, self.accuracy, self.aux_loss], feed_dict={ + self.uid_batch_ph: inps[0], + self.mid_batch_ph: inps[1], + self.cat_batch_ph: inps[2], + self.mid_his_batch_ph: inps[3], + self.cat_his_batch_ph: inps[4], + self.mask: inps[5], + self.target_ph: inps[6], + self.seq_len_ph: inps[7], + self.noclk_mid_batch_ph: inps[8], + self.noclk_cat_batch_ph: inps[9], + }) + return probs, loss, accuracy, aux_loss + else: + probs, loss, accuracy = sess.run([self.y_hat, self.loss, self.accuracy], feed_dict={ + self.uid_batch_ph: inps[0], + self.mid_batch_ph: inps[1], + self.cat_batch_ph: inps[2], + self.mid_his_batch_ph: inps[3], + self.cat_his_batch_ph: inps[4], + self.mask: inps[5], + self.target_ph: inps[6], + self.seq_len_ph: inps[7] + }) + return probs, loss, accuracy, 0 + + def save(self, sess, path): + # pass + saver = tf.train.Saver() + saver.save(sess, save_path=path) + + def restore(self, sess, path): + saver = tf.train.Saver() + saver.restore(sess, save_path=path) + print('model restored from %s' % path) + + def summary_op(self, summary_writer, summary_str, step): + summary_writer.add_summary(summary_str, global_step=step) + +# DIEN +class Model_DIN_V2_Gru_Vec_attGru_Neg(Model): + def __init__(self, batch_size, maxlen, n_uid, n_mid, n_cat, EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE, use_negsampling=True): + super(Model_DIN_V2_Gru_Vec_attGru_Neg, self).__init__(batch_size, maxlen, n_uid, n_mid, n_cat, + EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE, + use_negsampling) + + # RNN layer(-s) + with tf.name_scope('rnn_1'): + + # NPU GRUV2 + # item_his_eb_fp16 = tf.cast(self.item_his_eb, tf.float16, name="cast_fp16") + # item_his_eb_time_major = tf.transpose(item_his_eb_fp16, [1, 0, 2], name="transpose_time_major") + # gruv2 = DynamicGRUV2(HIDDEN_SIZE, dtype=tf.float16) + # rnn_outputs, _, _, _, _, _ = gruv2(item_his_eb_time_major) + # rnn_outputs_time_major = tf.transpose(rnn_outputs, [1, 0, 2], name="rnn_outputs_transpose_time_major") + # rnn_outputs = tf.cast(rnn_outputs_time_major, tf.float32) + + # GPU CuDNNGRU fp16 no seq_len + + if npu_mode == 0: + if fusion_mode: + if fp16_and_no_seq_len_mode: + item_his_eb_fp16 = tf.cast(self.item_his_eb, tf.float16, name="cast_fp16") + gru = tf.keras.layers.CuDNNGRU(HIDDEN_SIZE, return_sequences=True) + rnn_outputs = gru(item_his_eb_fp16) + rnn_outputs = tf.cast(rnn_outputs, tf.float32) + else: + gru = CudnnGRU(num_layers=1, num_units=HIDDEN_SIZE) + rnn_outputs, _ = gru(inputs=self.item_his_eb, sequence_lengths=self.seq_len_ph) + else: + if fp16_and_no_seq_len_mode: + item_his_eb_fp16 = tf.cast(self.item_his_eb, tf.float16, name="cast_fp16") + rnn_outputs, _ = dynamic_rnn(GRUCell(HIDDEN_SIZE), inputs=item_his_eb_fp16, + dtype=tf.float16, + scope="gru1") + rnn_outputs = tf.cast(rnn_outputs, tf.float32) + else: + rnn_outputs, _ = dynamic_rnn(GRUCell(HIDDEN_SIZE), inputs=self.item_his_eb, + sequence_length=self.seq_len_ph, dtype=tf.float32, + scope="gru1") + else: + if fusion_mode: + if fp16_and_no_seq_len_mode: + from npu_bridge.estimator.npu.npu_dynamic_rnn import DynamicGRUV2 + from npu_bridge.estimator.npu.npu_dynamic_rnn import DynamicAUGRU + item_his_eb_fp16 = tf.cast(self.item_his_eb, tf.float16, name="cast_fp16") + item_his_eb_time_major = tf.transpose(item_his_eb_fp16, [1, 0, 2], name="transpose_time_major") + gruv2 = DynamicGRUV2(HIDDEN_SIZE, dtype=tf.float16) + rnn_outputs, _, _, _, _, _ = gruv2(item_his_eb_time_major) + rnn_outputs_time_major = tf.transpose(rnn_outputs, [1, 0, 2], name="rnn_outputs_transpose_time_major") + rnn_outputs = tf.cast(rnn_outputs_time_major, tf.float32) + else: + print("ERROR: not supported!") + return + else: + if fp16_and_no_seq_len_mode: + item_his_eb_fp16 = tf.cast(self.item_his_eb, tf.float16, name="cast_fp16") + rnn_outputs, _ = dynamic_rnn(GRUCell(HIDDEN_SIZE), inputs=item_his_eb_fp16, + dtype=tf.float16, + scope="gru1") + rnn_outputs = tf.cast(rnn_outputs, tf.float32) + else: + rnn_outputs, _ = dynamic_rnn(GRUCell(HIDDEN_SIZE), inputs=self.item_his_eb, + sequence_length=self.seq_len_ph, dtype=tf.float32, + scope="gru1") + + tf.summary.histogram('GRU_outputs', rnn_outputs) + + aux_loss_1 = self.auxiliary_loss(rnn_outputs[:, :-1, :], self.item_his_eb[:, 1:, :], + self.noclk_item_his_eb[:, 1:, :], + self.mask[:, 1:], stag="gru") + self.aux_loss = aux_loss_1 + + # Attention layer + with tf.name_scope('Attention_layer_1'): + att_outputs, alphas = din_fcn_attention(self.item_eb, rnn_outputs, ATTENTION_SIZE, self.mask, + softmax_stag=1, stag='1_1', mode='LIST', return_alphas=True) + tf.summary.histogram('alpha_outputs', alphas) + + with tf.name_scope('rnn_2'): + if npu_mode == 0: + if fp16_and_no_seq_len_mode: + rnn_outputs_fp16 = tf.cast(rnn_outputs, tf.float16) + alphas_fp16 = tf.cast(alphas, tf.float16) + rnn_outputs2, final_state2 = dynamic_rnn(VecAttGRUCell(HIDDEN_SIZE), inputs=rnn_outputs_fp16, + att_scores = tf.expand_dims(alphas_fp16, -1), + dtype=tf.float16, + scope="gru2") + rnn_outputs2 = tf.cast(rnn_outputs2, tf.float32) + final_state2 = tf.cast(final_state2, tf.float32) + final_state2 = tf.batch_gather(rnn_outputs2, self.seq_len_ph[:, None] - 1) + final_state2 = tf.squeeze(final_state2, 1) + else: + rnn_outputs2, final_state2 = dynamic_rnn(VecAttGRUCell(HIDDEN_SIZE), inputs=rnn_outputs, + att_scores = tf.expand_dims(alphas, -1), + sequence_length=self.seq_len_ph, dtype=tf.float32, + scope="gru2") + else: + if fusion_mode: + if fp16_and_no_seq_len_mode: + rnn_outputs_fp16 = tf.cast(rnn_outputs, tf.float16) + alphas_fp16 = tf.cast(alphas, tf.float16) + rnn_outputs_time_major = tf.transpose(rnn_outputs_fp16, [1, 0, 2], name="gru2_transpose_time_major") + alphas_fp16_time_major = tf.transpose(alphas_fp16, [1, 0], name="att_transpose_time_major") + + augru = DynamicAUGRU(HIDDEN_SIZE, dtype=tf.float16) + rnn_outputs2, _, _, _, _, _, _ = augru(rnn_outputs_time_major, alphas_fp16_time_major) + + rnn_outputs2_time_major = tf.transpose(rnn_outputs2, [1, 0, 2], name="gru2_rnn_outputs_transpose_time_major") + rnn_outputs2 = tf.cast(rnn_outputs2_time_major, tf.float32) + final_state2 = tf.batch_gather(rnn_outputs2, self.seq_len_ph[:, None] - 1) + final_state2 = tf.squeeze(final_state2, 1) + else: + print("ERROR: not supported!") + return + else: + if fp16_and_no_seq_len_mode: + rnn_outputs_fp16 = tf.cast(rnn_outputs, tf.float16) + alphas_fp16 = tf.cast(alphas, tf.float16) + rnn_outputs2, final_state2 = dynamic_rnn(VecAttGRUCell(HIDDEN_SIZE), inputs=rnn_outputs_fp16, + att_scores = tf.expand_dims(alphas_fp16, -1), + dtype=tf.float16, + scope="gru2") + rnn_outputs2 = tf.cast(rnn_outputs2, tf.float32) + final_state2 = tf.cast(final_state2, tf.float32) + final_state2 = tf.batch_gather(rnn_outputs2, self.seq_len_ph[:, None] - 1) + final_state2 = tf.squeeze(final_state2, 1) + else: + rnn_outputs2, final_state2 = dynamic_rnn(VecAttGRUCell(HIDDEN_SIZE), inputs=rnn_outputs, + att_scores = tf.expand_dims(alphas, -1), + sequence_length=self.seq_len_ph, dtype=tf.float32, + scope="gru2") + + + tf.summary.histogram('GRU2_Final_State', final_state2) + + inp = tf.concat([self.uid_batch_embedded, self.item_eb, self.item_his_eb_sum, self.item_eb * self.item_his_eb_sum, final_state2], 1) + self.build_fcn_net(inp, use_dice=True) + + +@tf.custom_gradient +def gather_npu(params, indices): + def grad(dy): + params_shape = tf.shape(params, out_type=tf.int64) + params_shape = tf.cast(params_shape, tf.int32) + grad_gather = tf.unsorted_segment_sum(dy, indices, params_shape[0]) + return grad_gather, None + return tf.gather(params, indices), grad + +@tf.custom_gradient +def embedding_lookup_npu(params, indices): + def grad(dy): + params_shape = tf.shape(params, out_type=tf.int64) + params_shape = tf.cast(params_shape, tf.int32) + grad_embedding_lookup = tf.unsorted_segment_sum(dy, indices, params_shape[0]) + return grad_embedding_lookup, None + return tf.nn.embedding_lookup(params, indices), grad -- Gitee From 82a8e6e0d0fee537d0475e4109f00a37044b8423 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:33:25 +0000 Subject: [PATCH 28/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/my_rnn.py. --- .../script/my_rnn.py | 1577 +++++++++++++++++ 1 file changed, 1577 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/my_rnn.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/my_rnn.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/my_rnn.py new file mode 100644 index 000000000..d322fa335 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/my_rnn.py @@ -0,0 +1,1577 @@ +# Copyright 2015 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""RNN helpers for TensorFlow models.""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +from tensorflow.python.eager import context +from tensorflow.python.framework import constant_op +from tensorflow.python.framework import dtypes +from tensorflow.python.framework import ops +from tensorflow.python.framework import tensor_shape +from tensorflow.python.framework import tensor_util +from tensorflow.python.keras.engine import base_layer +from tensorflow.python.ops import array_ops +from tensorflow.python.ops import control_flow_ops +from tensorflow.python.ops import control_flow_util +from tensorflow.python.ops import math_ops +from tensorflow.python.ops import rnn_cell_impl +from tensorflow.python.ops import tensor_array_ops +from tensorflow.python.ops import variable_scope as vs +from tensorflow.python.util import deprecation +from tensorflow.python.util import nest +from tensorflow.python.util.tf_export import tf_export + +# pylint: disable=protected-access +_concat = rnn_cell_impl._concat +# pylint: enable=protected-access + + +def _transpose_batch_time(x): + """Transposes the batch and time dimensions of a Tensor. + If the input tensor has rank < 2 it returns the original tensor. Retains as + much of the static shape information as possible. + Args: + x: A Tensor. + Returns: + x transposed along the first two dimensions. + """ + x_static_shape = x.get_shape() + if x_static_shape.rank is not None and x_static_shape.rank < 2: + return x + + x_rank = array_ops.rank(x) + x_t = array_ops.transpose( + x, array_ops.concat(([1, 0], math_ops.range(2, x_rank)), axis=0)) + x_t.set_shape( + tensor_shape.TensorShape( + [x_static_shape.dims[1].value, + x_static_shape.dims[0].value]).concatenate(x_static_shape[2:])) + return x_t + + +def _best_effort_input_batch_size(flat_input): + """Get static input batch size if available, with fallback to the dynamic one. + Args: + flat_input: An iterable of time major input Tensors of shape `[max_time, + batch_size, ...]`. All inputs should have compatible batch sizes. + Returns: + The batch size in Python integer if available, or a scalar Tensor otherwise. + Raises: + ValueError: if there is any input with an invalid shape. + """ + for input_ in flat_input: + shape = input_.shape + if shape.rank is None: + continue + if shape.rank < 2: + raise ValueError("Expected input tensor %s to have rank at least 2" % + input_) + batch_size = shape.dims[1].value + if batch_size is not None: + return batch_size + # Fallback to the dynamic batch size of the first input. + return array_ops.shape(flat_input[0])[1] + + +def _infer_state_dtype(explicit_dtype, state): + """Infer the dtype of an RNN state. + Args: + explicit_dtype: explicitly declared dtype or None. + state: RNN's hidden state. Must be a Tensor or a nested iterable containing + Tensors. + Returns: + dtype: inferred dtype of hidden state. + Raises: + ValueError: if `state` has heterogeneous dtypes or is empty. + """ + if explicit_dtype is not None: + return explicit_dtype + elif nest.is_sequence(state): + inferred_dtypes = [element.dtype for element in nest.flatten(state)] + if not inferred_dtypes: + raise ValueError("Unable to infer dtype from empty state.") + all_same = all(x == inferred_dtypes[0] for x in inferred_dtypes) + if not all_same: + raise ValueError( + "State has tensors of different inferred_dtypes. Unable to infer a " + "single representative dtype.") + return inferred_dtypes[0] + else: + return state.dtype + + +def _maybe_tensor_shape_from_tensor(shape): + if isinstance(shape, ops.Tensor): + return tensor_shape.as_shape(tensor_util.constant_value(shape)) + else: + return shape + + +def _should_cache(): + """Returns True if a default caching device should be set, otherwise False.""" + if context.executing_eagerly(): + return False + # Don't set a caching device when running in a loop, since it is possible that + # train steps could be wrapped in a tf.while_loop. In that scenario caching + # prevents forward computations in loop iterations from re-reading the + # updated weights. + ctxt = ops.get_default_graph()._get_control_flow_context() # pylint: disable=protected-access + return control_flow_util.GetContainingWhileContext(ctxt) is None + + +def _is_keras_rnn_cell(rnn_cell): + """Check whether the cell is a Keras RNN cell. + The Keras RNN cell accept the state as a list even the state is a single + tensor, whereas the TF RNN cell does not wrap single state tensor in list. + This behavior difference should be unified in future version. + Args: + rnn_cell: An RNN cell instance that either follow the Keras interface or TF + RNN interface. + Returns: + Boolean, whether the cell is an Keras RNN cell. + """ + # Cell type check is not strict enough since there are cells created by other + # library like Deepmind that didn't inherit tf.nn.rnn_cell.RNNCell. + # Keras cells never had zero_state method, which was from the original + # interface from TF RNN cell. + return (not isinstance(rnn_cell, rnn_cell_impl.RNNCell) and + isinstance(rnn_cell, base_layer.Layer) and + getattr(rnn_cell, "zero_state", None) is None) + + +# pylint: disable=unused-argument +def _rnn_step(time, + sequence_length, + min_sequence_length, + max_sequence_length, + zero_output, + state, + call_cell, + state_size, + skip_conditionals=False): + """Calculate one step of a dynamic RNN minibatch. + Returns an (output, state) pair conditioned on `sequence_length`. + When skip_conditionals=False, the pseudocode is something like: + if t >= max_sequence_length: + return (zero_output, state) + if t < min_sequence_length: + return call_cell() + # Selectively output zeros or output, old state or new state depending + # on whether we've finished calculating each row. + new_output, new_state = call_cell() + final_output = np.vstack([ + zero_output if time >= sequence_length[r] else new_output_r + for r, new_output_r in enumerate(new_output) + ]) + final_state = np.vstack([ + state[r] if time >= sequence_length[r] else new_state_r + for r, new_state_r in enumerate(new_state) + ]) + return (final_output, final_state) + Args: + time: int32 `Tensor` scalar. + sequence_length: int32 `Tensor` vector of size [batch_size]. + min_sequence_length: int32 `Tensor` scalar, min of sequence_length. + max_sequence_length: int32 `Tensor` scalar, max of sequence_length. + zero_output: `Tensor` vector of shape [output_size]. + state: Either a single `Tensor` matrix of shape `[batch_size, state_size]`, + or a list/tuple of such tensors. + call_cell: lambda returning tuple of (new_output, new_state) where + new_output is a `Tensor` matrix of shape `[batch_size, output_size]`. + new_state is a `Tensor` matrix of shape `[batch_size, state_size]`. + state_size: The `cell.state_size` associated with the state. + skip_conditionals: Python bool, whether to skip using the conditional + calculations. This is useful for `dynamic_rnn`, where the input tensor + matches `max_sequence_length`, and using conditionals just slows + everything down. + Returns: + A tuple of (`final_output`, `final_state`) as given by the pseudocode above: + final_output is a `Tensor` matrix of shape [batch_size, output_size] + final_state is either a single `Tensor` matrix, or a tuple of such + matrices (matching length and shapes of input `state`). + Raises: + ValueError: If the cell returns a state tuple whose length does not match + that returned by `state_size`. + """ + + # Convert state to a list for ease of use + flat_state = nest.flatten(state) + flat_zero_output = nest.flatten(zero_output) + + # Vector describing which batch entries are finished. + copy_cond = time >= sequence_length + + def _copy_one_through(output, new_output): + # TensorArray and scalar get passed through. + if isinstance(output, tensor_array_ops.TensorArray): + return new_output + if output.shape.rank == 0: + return new_output + # Otherwise propagate the old or the new value. + with ops.colocate_with(new_output): + return array_ops.where(copy_cond, output, new_output) + + def _copy_some_through(flat_new_output, flat_new_state): + # Use broadcasting select to determine which values should get + # the previous state & zero output, and which values should get + # a calculated state & output. + flat_new_output = [ + _copy_one_through(zero_output, new_output) + for zero_output, new_output in zip(flat_zero_output, flat_new_output) + ] + flat_new_state = [ + _copy_one_through(state, new_state) + for state, new_state in zip(flat_state, flat_new_state) + ] + return flat_new_output + flat_new_state + + def _maybe_copy_some_through(): + """Run RNN step. Pass through either no or some past state.""" + new_output, new_state = call_cell() + + nest.assert_same_structure(zero_output, new_output) + nest.assert_same_structure(state, new_state) + + flat_new_state = nest.flatten(new_state) + flat_new_output = nest.flatten(new_output) + return control_flow_ops.cond( + # if t < min_seq_len: calculate and return everything + time < min_sequence_length, + lambda: flat_new_output + flat_new_state, + # else copy some of it through + lambda: _copy_some_through(flat_new_output, flat_new_state)) + + # TODO(ebrevdo): skipping these conditionals may cause a slowdown, + # but benefits from removing cond() and its gradient. We should + # profile with and without this switch here. + if skip_conditionals: + # Instead of using conditionals, perform the selective copy at all time + # steps. This is faster when max_seq_len is equal to the number of unrolls + # (which is typical for dynamic_rnn). + new_output, new_state = call_cell() + nest.assert_same_structure(zero_output, new_output) + nest.assert_same_structure(state, new_state) + new_state = nest.flatten(new_state) + new_output = nest.flatten(new_output) + final_output_and_state = _copy_some_through(new_output, new_state) + else: + empty_update = lambda: flat_zero_output + flat_state + final_output_and_state = control_flow_ops.cond( + # if t >= max_seq_len: copy all state through, output zeros + time >= max_sequence_length, + empty_update, + # otherwise calculation is required: copy some or all of it through + _maybe_copy_some_through) + + if len(final_output_and_state) != len(flat_zero_output) + len(flat_state): + raise ValueError("Internal error: state and output were not concatenated " + "correctly.") + final_output = final_output_and_state[:len(flat_zero_output)] + final_state = final_output_and_state[len(flat_zero_output):] + + for output, flat_output in zip(final_output, flat_zero_output): + output.set_shape(flat_output.get_shape()) + for substate, flat_substate in zip(final_state, flat_state): + if not isinstance(substate, tensor_array_ops.TensorArray): + substate.set_shape(flat_substate.get_shape()) + + final_output = nest.pack_sequence_as( + structure=zero_output, flat_sequence=final_output) + final_state = nest.pack_sequence_as( + structure=state, flat_sequence=final_state) + + return final_output, final_state + + +def _reverse_seq(input_seq, lengths): + """Reverse a list of Tensors up to specified lengths. + Args: + input_seq: Sequence of seq_len tensors of dimension (batch_size, n_features) + or nested tuples of tensors. + lengths: A `Tensor` of dimension batch_size, containing lengths for each + sequence in the batch. If "None" is specified, simply reverses the list. + Returns: + time-reversed sequence + """ + if lengths is None: + return list(reversed(input_seq)) + + flat_input_seq = tuple(nest.flatten(input_) for input_ in input_seq) + + flat_results = [[] for _ in range(len(input_seq))] + for sequence in zip(*flat_input_seq): + input_shape = tensor_shape.unknown_shape(rank=sequence[0].get_shape().rank) + for input_ in sequence: + input_shape.merge_with(input_.get_shape()) + input_.set_shape(input_shape) + + # Join into (time, batch_size, depth) + s_joined = array_ops.stack(sequence) + + # Reverse along dimension 0 + s_reversed = array_ops.reverse_sequence(s_joined, lengths, 0, 1) + # Split again into list + result = array_ops.unstack(s_reversed) + for r, flat_result in zip(result, flat_results): + r.set_shape(input_shape) + flat_result.append(r) + + results = [ + nest.pack_sequence_as(structure=input_, flat_sequence=flat_result) + for input_, flat_result in zip(input_seq, flat_results) + ] + return results + + +@deprecation.deprecated(None, "Please use `keras.layers.Bidirectional(" + "keras.layers.RNN(cell))`, which is equivalent to " + "this API") +@tf_export(v1=["nn.bidirectional_dynamic_rnn"]) +def bidirectional_dynamic_rnn(cell_fw, + cell_bw, + inputs, + sequence_length=None, + initial_state_fw=None, + initial_state_bw=None, + dtype=None, + parallel_iterations=None, + swap_memory=False, + time_major=False, + scope=None): + """Creates a dynamic version of bidirectional recurrent neural network. + Takes input and builds independent forward and backward RNNs. The input_size + of forward and backward cell must match. The initial state for both directions + is zero by default (but can be set optionally) and no intermediate states are + ever returned -- the network is fully unrolled for the given (passed in) + length(s) of the sequence(s) or completely unrolled if length(s) is not + given. + Args: + cell_fw: An instance of RNNCell, to be used for forward direction. + cell_bw: An instance of RNNCell, to be used for backward direction. + inputs: The RNN inputs. + If time_major == False (default), this must be a tensor of shape: + `[batch_size, max_time, ...]`, or a nested tuple of such elements. + If time_major == True, this must be a tensor of shape: `[max_time, + batch_size, ...]`, or a nested tuple of such elements. + sequence_length: (optional) An int32/int64 vector, size `[batch_size]`, + containing the actual lengths for each of the sequences in the batch. If + not provided, all batch entries are assumed to be full sequences; and time + reversal is applied from time `0` to `max_time` for each sequence. + initial_state_fw: (optional) An initial state for the forward RNN. This must + be a tensor of appropriate type and shape `[batch_size, + cell_fw.state_size]`. If `cell_fw.state_size` is a tuple, this should be a + tuple of tensors having shapes `[batch_size, s] for s in + cell_fw.state_size`. + initial_state_bw: (optional) Same as for `initial_state_fw`, but using the + corresponding properties of `cell_bw`. + dtype: (optional) The data type for the initial states and expected output. + Required if initial_states are not provided or RNN states have a + heterogeneous dtype. + parallel_iterations: (Default: 32). The number of iterations to run in + parallel. Those operations which do not have any temporal dependency and + can be run in parallel, will be. This parameter trades off time for + space. Values >> 1 use more memory but take less time, while smaller + values use less memory but computations take longer. + swap_memory: Transparently swap the tensors produced in forward inference + but needed for back prop from GPU to CPU. This allows training RNNs which + would typically not fit on a single GPU, with very minimal (or no) + performance penalty. + time_major: The shape format of the `inputs` and `outputs` Tensors. If true, + these `Tensors` must be shaped `[max_time, batch_size, depth]`. If false, + these `Tensors` must be shaped `[batch_size, max_time, depth]`. Using + `time_major = True` is a bit more efficient because it avoids transposes + at the beginning and end of the RNN calculation. However, most TensorFlow + data is batch-major, so by default this function accepts input and emits + output in batch-major form. + scope: VariableScope for the created subgraph; defaults to + "bidirectional_rnn" + Returns: + A tuple (outputs, output_states) where: + outputs: A tuple (output_fw, output_bw) containing the forward and + the backward rnn output `Tensor`. + If time_major == False (default), + output_fw will be a `Tensor` shaped: + `[batch_size, max_time, cell_fw.output_size]` + and output_bw will be a `Tensor` shaped: + `[batch_size, max_time, cell_bw.output_size]`. + If time_major == True, + output_fw will be a `Tensor` shaped: + `[max_time, batch_size, cell_fw.output_size]` + and output_bw will be a `Tensor` shaped: + `[max_time, batch_size, cell_bw.output_size]`. + It returns a tuple instead of a single concatenated `Tensor`, unlike + in the `bidirectional_rnn`. If the concatenated one is preferred, + the forward and backward outputs can be concatenated as + `tf.concat(outputs, 2)`. + output_states: A tuple (output_state_fw, output_state_bw) containing + the forward and the backward final states of bidirectional rnn. + Raises: + TypeError: If `cell_fw` or `cell_bw` is not an instance of `RNNCell`. + """ + rnn_cell_impl.assert_like_rnncell("cell_fw", cell_fw) + rnn_cell_impl.assert_like_rnncell("cell_bw", cell_bw) + + with vs.variable_scope(scope or "bidirectional_rnn"): + # Forward direction + with vs.variable_scope("fw") as fw_scope: + output_fw, output_state_fw = dynamic_rnn( + cell=cell_fw, + inputs=inputs, + sequence_length=sequence_length, + initial_state=initial_state_fw, + dtype=dtype, + parallel_iterations=parallel_iterations, + swap_memory=swap_memory, + time_major=time_major, + scope=fw_scope) + + # Backward direction + if not time_major: + time_axis = 1 + batch_axis = 0 + else: + time_axis = 0 + batch_axis = 1 + + def _reverse(input_, seq_lengths, seq_axis, batch_axis): + if seq_lengths is not None: + return array_ops.reverse_sequence( + input=input_, + seq_lengths=seq_lengths, + seq_axis=seq_axis, + batch_axis=batch_axis) + else: + return array_ops.reverse(input_, axis=[seq_axis]) + + with vs.variable_scope("bw") as bw_scope: + + def _map_reverse(inp): + return _reverse( + inp, + seq_lengths=sequence_length, + seq_axis=time_axis, + batch_axis=batch_axis) + + inputs_reverse = nest.map_structure(_map_reverse, inputs) + tmp, output_state_bw = dynamic_rnn( + cell=cell_bw, + inputs=inputs_reverse, + sequence_length=sequence_length, + initial_state=initial_state_bw, + dtype=dtype, + parallel_iterations=parallel_iterations, + swap_memory=swap_memory, + time_major=time_major, + scope=bw_scope) + + output_bw = _reverse( + tmp, + seq_lengths=sequence_length, + seq_axis=time_axis, + batch_axis=batch_axis) + + outputs = (output_fw, output_bw) + output_states = (output_state_fw, output_state_bw) + + return (outputs, output_states) + + +@deprecation.deprecated( + None, + "Please use `keras.layers.RNN(cell)`, which is equivalent to this API") +@tf_export(v1=["nn.dynamic_rnn"]) +def dynamic_rnn(cell, + inputs, + att_scores=None, + sequence_length=None, + initial_state=None, + dtype=None, + parallel_iterations=None, + swap_memory=False, + time_major=False, + scope=None): + """Creates a recurrent neural network specified by RNNCell `cell`. + Performs fully dynamic unrolling of `inputs`. + Example: + ```python + # create a BasicRNNCell + rnn_cell = tf.compat.v1.nn.rnn_cell.BasicRNNCell(hidden_size) + # 'outputs' is a tensor of shape [batch_size, max_time, cell_state_size] + # defining initial state + initial_state = rnn_cell.zero_state(batch_size, dtype=tf.float32) + # 'state' is a tensor of shape [batch_size, cell_state_size] + outputs, state = tf.compat.v1.nn.dynamic_rnn(rnn_cell, input_data, + initial_state=initial_state, + dtype=tf.float32) + ``` + ```python + # create 2 LSTMCells + rnn_layers = [tf.compat.v1.nn.rnn_cell.LSTMCell(size) for size in [128, 256]] + # create a RNN cell composed sequentially of a number of RNNCells + multi_rnn_cell = tf.compat.v1.nn.rnn_cell.MultiRNNCell(rnn_layers) + # 'outputs' is a tensor of shape [batch_size, max_time, 256] + # 'state' is a N-tuple where N is the number of LSTMCells containing a + # tf.nn.rnn_cell.LSTMStateTuple for each cell + outputs, state = tf.compat.v1.nn.dynamic_rnn(cell=multi_rnn_cell, + inputs=data, + dtype=tf.float32) + ``` + Args: + cell: An instance of RNNCell. + inputs: The RNN inputs. + If `time_major == False` (default), this must be a `Tensor` of shape: + `[batch_size, max_time, ...]`, or a nested tuple of such elements. + If `time_major == True`, this must be a `Tensor` of shape: `[max_time, + batch_size, ...]`, or a nested tuple of such elements. This may also be + a (possibly nested) tuple of Tensors satisfying this property. The + first two dimensions must match across all the inputs, but otherwise the + ranks and other shape components may differ. In this case, input to + `cell` at each time-step will replicate the structure of these tuples, + except for the time dimension (from which the time is taken). The input + to `cell` at each time step will be a `Tensor` or (possibly nested) + tuple of Tensors each with dimensions `[batch_size, ...]`. + sequence_length: (optional) An int32/int64 vector sized `[batch_size]`. Used + to copy-through state and zero-out outputs when past a batch element's + sequence length. This parameter enables users to extract the last valid + state and properly padded outputs, so it is provided for correctness. + initial_state: (optional) An initial state for the RNN. If `cell.state_size` + is an integer, this must be a `Tensor` of appropriate type and shape + `[batch_size, cell.state_size]`. If `cell.state_size` is a tuple, this + should be a tuple of tensors having shapes `[batch_size, s] for s in + cell.state_size`. + dtype: (optional) The data type for the initial state and expected output. + Required if initial_state is not provided or RNN state has a heterogeneous + dtype. + parallel_iterations: (Default: 32). The number of iterations to run in + parallel. Those operations which do not have any temporal dependency and + can be run in parallel, will be. This parameter trades off time for + space. Values >> 1 use more memory but take less time, while smaller + values use less memory but computations take longer. + swap_memory: Transparently swap the tensors produced in forward inference + but needed for back prop from GPU to CPU. This allows training RNNs which + would typically not fit on a single GPU, with very minimal (or no) + performance penalty. + time_major: The shape format of the `inputs` and `outputs` Tensors. If true, + these `Tensors` must be shaped `[max_time, batch_size, depth]`. If false, + these `Tensors` must be shaped `[batch_size, max_time, depth]`. Using + `time_major = True` is a bit more efficient because it avoids transposes + at the beginning and end of the RNN calculation. However, most TensorFlow + data is batch-major, so by default this function accepts input and emits + output in batch-major form. + scope: VariableScope for the created subgraph; defaults to "rnn". + Returns: + A pair (outputs, state) where: + outputs: The RNN output `Tensor`. + If time_major == False (default), this will be a `Tensor` shaped: + `[batch_size, max_time, cell.output_size]`. + If time_major == True, this will be a `Tensor` shaped: + `[max_time, batch_size, cell.output_size]`. + Note, if `cell.output_size` is a (possibly nested) tuple of integers + or `TensorShape` objects, then `outputs` will be a tuple having the + same structure as `cell.output_size`, containing Tensors having shapes + corresponding to the shape data in `cell.output_size`. + state: The final state. If `cell.state_size` is an int, this + will be shaped `[batch_size, cell.state_size]`. If it is a + `TensorShape`, this will be shaped `[batch_size] + cell.state_size`. + If it is a (possibly nested) tuple of ints or `TensorShape`, this will + be a tuple having the corresponding shapes. If cells are `LSTMCells` + `state` will be a tuple containing a `LSTMStateTuple` for each cell. + Raises: + TypeError: If `cell` is not an instance of RNNCell. + ValueError: If inputs is None or an empty list. + """ + rnn_cell_impl.assert_like_rnncell("cell", cell) + + with vs.variable_scope(scope or "rnn") as varscope: + # Create a new scope in which the caching device is either + # determined by the parent scope, or is set to place the cached + # Variable using the same placement as for the rest of the RNN. + if _should_cache(): + if varscope.caching_device is None: + varscope.set_caching_device(lambda op: op.device) + + # By default, time_major==False and inputs are batch-major: shaped + # [batch, time, depth] + # For internal calculations, we transpose to [time, batch, depth] + flat_input = nest.flatten(inputs) + + if not time_major: + # (B,T,D) => (T,B,D) + flat_input = [ops.convert_to_tensor(input_) for input_ in flat_input] + flat_input = tuple(_transpose_batch_time(input_) for input_ in flat_input) + + parallel_iterations = parallel_iterations or 32 + if sequence_length is not None: + sequence_length = math_ops.cast(sequence_length, dtypes.int32) + if sequence_length.get_shape().rank not in (None, 1): + raise ValueError( + "sequence_length must be a vector of length batch_size, " + "but saw shape: %s" % sequence_length.get_shape()) + sequence_length = array_ops.identity( # Just to find it in the graph. + sequence_length, + name="sequence_length") + + batch_size = _best_effort_input_batch_size(flat_input) + + if initial_state is not None: + state = initial_state + else: + if not dtype: + raise ValueError("If there is no initial_state, you must give a dtype.") + if getattr(cell, "get_initial_state", None) is not None: + state = cell.get_initial_state( + inputs=None, batch_size=batch_size, dtype=dtype) + else: + state = cell.zero_state(batch_size, dtype) + + def _assert_has_shape(x, shape): + x_shape = array_ops.shape(x) + packed_shape = array_ops.stack(shape) + return control_flow_ops.Assert( + math_ops.reduce_all(math_ops.equal(x_shape, packed_shape)), [ + "Expected shape for Tensor %s is " % x.name, packed_shape, + " but saw shape: ", x_shape + ]) + + if not context.executing_eagerly() and sequence_length is not None: + # Perform some shape validation + with ops.control_dependencies( + [_assert_has_shape(sequence_length, [batch_size])]): + sequence_length = array_ops.identity( + sequence_length, name="CheckSeqLen") + + inputs = nest.pack_sequence_as(structure=inputs, flat_sequence=flat_input) + + (outputs, final_state) = _dynamic_rnn_loop( + cell, + inputs, + state, + parallel_iterations=parallel_iterations, + swap_memory=swap_memory, + att_scores=att_scores, + sequence_length=sequence_length, + dtype=dtype) + + # Outputs of _dynamic_rnn_loop are always shaped [time, batch, depth]. + # If we are performing batch-major calculations, transpose output back + # to shape [batch, time, depth] + if not time_major: + # (T,B,D) => (B,T,D) + outputs = nest.map_structure(_transpose_batch_time, outputs) + + return (outputs, final_state) + + +def _dynamic_rnn_loop(cell, + inputs, + initial_state, + parallel_iterations, + swap_memory, + att_scores=None, + sequence_length=None, + dtype=None): + """Internal implementation of Dynamic RNN. + Args: + cell: An instance of RNNCell. + inputs: A `Tensor` of shape [time, batch_size, input_size], or a nested + tuple of such elements. + initial_state: A `Tensor` of shape `[batch_size, state_size]`, or if + `cell.state_size` is a tuple, then this should be a tuple of tensors + having shapes `[batch_size, s] for s in cell.state_size`. + parallel_iterations: Positive Python int. + swap_memory: A Python boolean + sequence_length: (optional) An `int32` `Tensor` of shape [batch_size]. + dtype: (optional) Expected dtype of output. If not specified, inferred from + initial_state. + Returns: + Tuple `(final_outputs, final_state)`. + final_outputs: + A `Tensor` of shape `[time, batch_size, cell.output_size]`. If + `cell.output_size` is a (possibly nested) tuple of ints or `TensorShape` + objects, then this returns a (possibly nested) tuple of Tensors matching + the corresponding shapes. + final_state: + A `Tensor`, or possibly nested tuple of Tensors, matching in length + and shapes to `initial_state`. + Raises: + ValueError: If the input depth cannot be inferred via shape inference + from the inputs. + ValueError: If time_step is not the same for all the elements in the + inputs. + ValueError: If batch_size is not the same for all the elements in the + inputs. + """ + state = initial_state + assert isinstance(parallel_iterations, int), "parallel_iterations must be int" + + state_size = cell.state_size + + flat_input = nest.flatten(inputs) + flat_output_size = nest.flatten(cell.output_size) + + # Construct an initial output + input_shape = array_ops.shape(flat_input[0]) + time_steps = input_shape[0] + batch_size = _best_effort_input_batch_size(flat_input) + + inputs_got_shape = tuple( + input_.get_shape().with_rank_at_least(3) for input_ in flat_input) + + const_time_steps, const_batch_size = inputs_got_shape[0].as_list()[:2] + + for shape in inputs_got_shape: + if not shape[2:].is_fully_defined(): + raise ValueError( + "Input size (depth of inputs) must be accessible via shape inference," + " but saw value None.") + got_time_steps = shape.dims[0].value + got_batch_size = shape.dims[1].value + if const_time_steps != got_time_steps: + raise ValueError( + "Time steps is not the same for all the elements in the input in a " + "batch.") + if const_batch_size != got_batch_size: + raise ValueError( + "Batch_size is not the same for all the elements in the input.") + + # Prepare dynamic conditional copying of state & output + def _create_zero_arrays(size): + size = _concat(batch_size, size) + return array_ops.zeros( + array_ops.stack(size), _infer_state_dtype(dtype, state)) + + flat_zero_output = tuple( + _create_zero_arrays(output) for output in flat_output_size) + zero_output = nest.pack_sequence_as( + structure=cell.output_size, flat_sequence=flat_zero_output) + + if sequence_length is not None: + min_sequence_length = math_ops.reduce_min(sequence_length) + max_sequence_length = math_ops.reduce_max(sequence_length) + else: + max_sequence_length = time_steps + + time = array_ops.constant(0, dtype=dtypes.int32, name="time") + + with ops.name_scope("dynamic_rnn") as scope: + base_name = scope + + def _create_ta(name, element_shape, dtype): + return tensor_array_ops.TensorArray( + dtype=dtype, + size=time_steps, + element_shape=element_shape, + tensor_array_name=base_name + name) + + in_graph_mode = not context.executing_eagerly() + if in_graph_mode: + output_ta = tuple( + _create_ta( + "output_%d" % i, + element_shape=( + tensor_shape.TensorShape([const_batch_size]).concatenate( + _maybe_tensor_shape_from_tensor(out_size))), + dtype=_infer_state_dtype(dtype, state)) + for i, out_size in enumerate(flat_output_size)) + input_ta = tuple( + _create_ta( + "input_%d" % i, + element_shape=flat_input_i.shape[1:], + dtype=flat_input_i.dtype) + for i, flat_input_i in enumerate(flat_input)) + input_ta = tuple( + ta.unstack(input_) for ta, input_ in zip(input_ta, flat_input)) + else: + output_ta = tuple([0 for _ in range(time_steps.numpy())] + for i in range(len(flat_output_size))) + input_ta = flat_input + + def _time_step(time, output_ta_t, state, att_scores=None): + """Take a time step of the dynamic RNN. + Args: + time: int32 scalar Tensor. + output_ta_t: List of `TensorArray`s that represent the output. + state: nested tuple of vector tensors that represent the state. + Returns: + The tuple (time + 1, output_ta_t with updated flow, new_state). + """ + + if in_graph_mode: + input_t = tuple(ta.read(time) for ta in input_ta) + # Restore some shape information + for input_, shape in zip(input_t, inputs_got_shape): + input_.set_shape(shape[1:]) + else: + input_t = tuple(ta[time.numpy()] for ta in input_ta) + + input_t = nest.pack_sequence_as(structure=inputs, flat_sequence=input_t) + is_keras_rnn_cell = _is_keras_rnn_cell(cell) + if is_keras_rnn_cell and not nest.is_sequence(state): + state = [state] + + if att_scores is not None: + att_score = att_scores[:, time, :] + call_cell = lambda: cell(input_t, state, att_score) + else: + call_cell = lambda: cell(input_t, state) + + # Keras RNN cells only accept state as list, even if it's a single tensor. + # is_keras_rnn_cell = _is_keras_rnn_cell(cell) + # if is_keras_rnn_cell and not nest.is_sequence(state): + # state = [state] + # call_cell = lambda: cell(input_t, state) + + if sequence_length is not None: + (output, new_state) = _rnn_step( + time=time, + sequence_length=sequence_length, + min_sequence_length=min_sequence_length, + max_sequence_length=max_sequence_length, + zero_output=zero_output, + state=state, + call_cell=call_cell, + state_size=state_size, + skip_conditionals=True) + else: + (output, new_state) = call_cell() + + # Keras cells always wrap state as list, even if it's a single tensor. + if is_keras_rnn_cell and len(new_state) == 1: + new_state = new_state[0] + # Pack state if using state tuples + output = nest.flatten(output) + + if in_graph_mode: + output_ta_t = tuple( + ta.write(time, out) for ta, out in zip(output_ta_t, output)) + else: + for ta, out in zip(output_ta_t, output): + ta[time.numpy()] = out + + if att_scores is not None: + return (time + 1, output_ta_t, new_state, att_scores) + else: + return (time + 1, output_ta_t, new_state) + + if in_graph_mode: + # Make sure that we run at least 1 step, if necessary, to ensure + # the TensorArrays pick up the dynamic shape. + loop_bound = math_ops.minimum(time_steps, + math_ops.maximum(1, max_sequence_length)) + else: + # Using max_sequence_length isn't currently supported in the Eager branch. + loop_bound = time_steps + + if att_scores is not None: + _, output_final_ta, final_state, _ = control_flow_ops.while_loop( + cond=lambda time, *_: time < loop_bound, + body=_time_step, + loop_vars=(time, output_ta, state, att_scores), + parallel_iterations=parallel_iterations, + maximum_iterations=time_steps, + swap_memory=swap_memory) + else: + _, output_final_ta, final_state = control_flow_ops.while_loop( + cond=lambda time, *_: time < loop_bound, + body=_time_step, + loop_vars=(time, output_ta, state), + parallel_iterations=parallel_iterations, + maximum_iterations=time_steps, + swap_memory=swap_memory) + + # Unpack final output if not using output tuples. + if in_graph_mode: + final_outputs = tuple(ta.stack() for ta in output_final_ta) + # Restore some shape information + for output, output_size in zip(final_outputs, flat_output_size): + shape = _concat([const_time_steps, const_batch_size], + output_size, + static=True) + output.set_shape(shape) + else: + final_outputs = output_final_ta + + final_outputs = nest.pack_sequence_as( + structure=cell.output_size, flat_sequence=final_outputs) + if not in_graph_mode: + final_outputs = nest.map_structure_up_to( + cell.output_size, lambda x: array_ops.stack(x, axis=0), final_outputs) + + return (final_outputs, final_state) + + +@tf_export(v1=["nn.raw_rnn"]) +def raw_rnn(cell, + loop_fn, + parallel_iterations=None, + swap_memory=False, + scope=None): + """Creates an `RNN` specified by RNNCell `cell` and loop function `loop_fn`. + **NOTE: This method is still in testing, and the API may change.** + This function is a more primitive version of `dynamic_rnn` that provides + more direct access to the inputs each iteration. It also provides more + control over when to start and finish reading the sequence, and + what to emit for the output. + For example, it can be used to implement the dynamic decoder of a seq2seq + model. + Instead of working with `Tensor` objects, most operations work with + `TensorArray` objects directly. + The operation of `raw_rnn`, in pseudo-code, is basically the following: + ```python + time = tf.constant(0, dtype=tf.int32) + (finished, next_input, initial_state, emit_structure, loop_state) = loop_fn( + time=time, cell_output=None, cell_state=None, loop_state=None) + emit_ta = TensorArray(dynamic_size=True, dtype=initial_state.dtype) + state = initial_state + while not all(finished): + (output, cell_state) = cell(next_input, state) + (next_finished, next_input, next_state, emit, loop_state) = loop_fn( + time=time + 1, cell_output=output, cell_state=cell_state, + loop_state=loop_state) + # Emit zeros and copy forward state for minibatch entries that are finished. + state = tf.where(finished, state, next_state) + emit = tf.where(finished, tf.zeros_like(emit_structure), emit) + emit_ta = emit_ta.write(time, emit) + # If any new minibatch entries are marked as finished, mark these. + finished = tf.logical_or(finished, next_finished) + time += 1 + return (emit_ta, state, loop_state) + ``` + with the additional properties that output and state may be (possibly nested) + tuples, as determined by `cell.output_size` and `cell.state_size`, and + as a result the final `state` and `emit_ta` may themselves be tuples. + A simple implementation of `dynamic_rnn` via `raw_rnn` looks like this: + ```python + inputs = tf.compat.v1.placeholder(shape=(max_time, batch_size, input_depth), + dtype=tf.float32) + sequence_length = tf.compat.v1.placeholder(shape=(batch_size,), + dtype=tf.int32) + inputs_ta = tf.TensorArray(dtype=tf.float32, size=max_time) + inputs_ta = inputs_ta.unstack(inputs) + cell = tf.compat.v1.nn.rnn_cell.LSTMCell(num_units) + def loop_fn(time, cell_output, cell_state, loop_state): + emit_output = cell_output # == None for time == 0 + if cell_output is None: # time == 0 + next_cell_state = cell.zero_state(batch_size, tf.float32) + else: + next_cell_state = cell_state + elements_finished = (time >= sequence_length) + finished = tf.reduce_all(elements_finished) + next_input = tf.cond( + finished, + lambda: tf.zeros([batch_size, input_depth], dtype=tf.float32), + lambda: inputs_ta.read(time)) + next_loop_state = None + return (elements_finished, next_input, next_cell_state, + emit_output, next_loop_state) + outputs_ta, final_state, _ = raw_rnn(cell, loop_fn) + outputs = outputs_ta.stack() + ``` + Args: + cell: An instance of RNNCell. + loop_fn: A callable that takes inputs `(time, cell_output, cell_state, + loop_state)` and returns the tuple `(finished, next_input, + next_cell_state, emit_output, next_loop_state)`. Here `time` is an int32 + scalar `Tensor`, `cell_output` is a `Tensor` or (possibly nested) tuple of + tensors as determined by `cell.output_size`, and `cell_state` is a + `Tensor` or (possibly nested) tuple of tensors, as determined by the + `loop_fn` on its first call (and should match `cell.state_size`). + The outputs are: `finished`, a boolean `Tensor` of + shape `[batch_size]`, `next_input`: the next input to feed to `cell`, + `next_cell_state`: the next state to feed to `cell`, + and `emit_output`: the output to store for this iteration. Note that + `emit_output` should be a `Tensor` or (possibly nested) tuple of tensors + which is aggregated in the `emit_ta` inside the `while_loop`. For the + first call to `loop_fn`, the `emit_output` corresponds to the + `emit_structure` which is then used to determine the size of the + `zero_tensor` for the `emit_ta` (defaults to `cell.output_size`). For + the subsequent calls to the `loop_fn`, the `emit_output` corresponds to + the actual output tensor that is to be aggregated in the `emit_ta`. The + parameter `cell_state` and output `next_cell_state` may be either a + single or (possibly nested) tuple of tensors. The parameter + `loop_state` and output `next_loop_state` may be either a single or + (possibly nested) tuple of `Tensor` and `TensorArray` objects. This + last parameter may be ignored by `loop_fn` and the return value may be + `None`. If it is not `None`, then the `loop_state` will be propagated + through the RNN loop, for use purely by `loop_fn` to keep track of its + own state. The `next_loop_state` parameter returned may be `None`. The + first call to `loop_fn` will be `time = 0`, `cell_output = None`, + `cell_state = None`, and `loop_state = None`. For this call: The + `next_cell_state` value should be the value with which to initialize the + cell's state. It may be a final state from a previous RNN or it may be + the output of `cell.zero_state()`. It should be a (possibly nested) + tuple structure of tensors. If `cell.state_size` is an integer, this + must be a `Tensor` of appropriate type and shape `[batch_size, + cell.state_size]`. If `cell.state_size` is a `TensorShape`, this must be + a `Tensor` of appropriate type and shape `[batch_size] + + cell.state_size`. If `cell.state_size` is a (possibly nested) tuple of + ints or `TensorShape`, this will be a tuple having the corresponding + shapes. The `emit_output` value may be either `None` or a (possibly + nested) tuple structure of tensors, e.g., `(tf.zeros(shape_0, + dtype=dtype_0), tf.zeros(shape_1, dtype=dtype_1))`. If this first + `emit_output` return value is `None`, then the `emit_ta` result of + `raw_rnn` will have the same structure and dtypes as `cell.output_size`. + Otherwise `emit_ta` will have the same structure, shapes (prepended with + a `batch_size` dimension), and dtypes as `emit_output`. The actual + values returned for `emit_output` at this initializing call are ignored. + Note, this emit structure must be consistent across all time steps. + parallel_iterations: (Default: 32). The number of iterations to run in + parallel. Those operations which do not have any temporal dependency and + can be run in parallel, will be. This parameter trades off time for + space. Values >> 1 use more memory but take less time, while smaller + values use less memory but computations take longer. + swap_memory: Transparently swap the tensors produced in forward inference + but needed for back prop from GPU to CPU. This allows training RNNs which + would typically not fit on a single GPU, with very minimal (or no) + performance penalty. + scope: VariableScope for the created subgraph; defaults to "rnn". + Returns: + A tuple `(emit_ta, final_state, final_loop_state)` where: + `emit_ta`: The RNN output `TensorArray`. + If `loop_fn` returns a (possibly nested) set of Tensors for + `emit_output` during initialization, (inputs `time = 0`, + `cell_output = None`, and `loop_state = None`), then `emit_ta` will + have the same structure, dtypes, and shapes as `emit_output` instead. + If `loop_fn` returns `emit_output = None` during this call, + the structure of `cell.output_size` is used: + If `cell.output_size` is a (possibly nested) tuple of integers + or `TensorShape` objects, then `emit_ta` will be a tuple having the + same structure as `cell.output_size`, containing TensorArrays whose + elements' shapes correspond to the shape data in `cell.output_size`. + `final_state`: The final cell state. If `cell.state_size` is an int, this + will be shaped `[batch_size, cell.state_size]`. If it is a + `TensorShape`, this will be shaped `[batch_size] + cell.state_size`. + If it is a (possibly nested) tuple of ints or `TensorShape`, this will + be a tuple having the corresponding shapes. + `final_loop_state`: The final loop state as returned by `loop_fn`. + Raises: + TypeError: If `cell` is not an instance of RNNCell, or `loop_fn` is not + a `callable`. + """ + rnn_cell_impl.assert_like_rnncell("cell", cell) + + if not callable(loop_fn): + raise TypeError("loop_fn must be a callable") + + parallel_iterations = parallel_iterations or 32 + + # Create a new scope in which the caching device is either + # determined by the parent scope, or is set to place the cached + # Variable using the same placement as for the rest of the RNN. + with vs.variable_scope(scope or "rnn") as varscope: + if _should_cache(): + if varscope.caching_device is None: + varscope.set_caching_device(lambda op: op.device) + + time = constant_op.constant(0, dtype=dtypes.int32) + (elements_finished, next_input, + initial_state, emit_structure, init_loop_state) = loop_fn( + time, None, None, None) # time, cell_output, cell_state, loop_state + flat_input = nest.flatten(next_input) + + # Need a surrogate loop state for the while_loop if none is available. + loop_state = ( + init_loop_state if init_loop_state is not None else + constant_op.constant(0, dtype=dtypes.int32)) + + input_shape = [input_.get_shape() for input_ in flat_input] + static_batch_size = tensor_shape.dimension_at_index(input_shape[0], 0) + + for input_shape_i in input_shape: + # Static verification that batch sizes all match + static_batch_size.merge_with( + tensor_shape.dimension_at_index(input_shape_i, 0)) + + batch_size = tensor_shape.dimension_value(static_batch_size) + const_batch_size = batch_size + if batch_size is None: + batch_size = array_ops.shape(flat_input[0])[0] + + nest.assert_same_structure(initial_state, cell.state_size) + state = initial_state + flat_state = nest.flatten(state) + flat_state = [ops.convert_to_tensor(s) for s in flat_state] + state = nest.pack_sequence_as(structure=state, flat_sequence=flat_state) + + if emit_structure is not None: + flat_emit_structure = nest.flatten(emit_structure) + flat_emit_size = [ + emit.shape if emit.shape.is_fully_defined() else array_ops.shape(emit) + for emit in flat_emit_structure + ] + flat_emit_dtypes = [emit.dtype for emit in flat_emit_structure] + else: + emit_structure = cell.output_size + flat_emit_size = nest.flatten(emit_structure) + flat_emit_dtypes = [flat_state[0].dtype] * len(flat_emit_size) + + flat_emit_ta = [ + tensor_array_ops.TensorArray( + dtype=dtype_i, + dynamic_size=True, + element_shape=(tensor_shape.TensorShape([ + const_batch_size + ]).concatenate(_maybe_tensor_shape_from_tensor(size_i))), + size=0, + name="rnn_output_%d" % i) + for i, (dtype_i, + size_i) in enumerate(zip(flat_emit_dtypes, flat_emit_size)) + ] + emit_ta = nest.pack_sequence_as( + structure=emit_structure, flat_sequence=flat_emit_ta) + flat_zero_emit = [ + array_ops.zeros(_concat(batch_size, size_i), dtype_i) + for size_i, dtype_i in zip(flat_emit_size, flat_emit_dtypes) + ] + zero_emit = nest.pack_sequence_as( + structure=emit_structure, flat_sequence=flat_zero_emit) + + def condition(unused_time, elements_finished, *_): + return math_ops.logical_not(math_ops.reduce_all(elements_finished)) + + def body(time, elements_finished, current_input, emit_ta, state, + loop_state): + """Internal while loop body for raw_rnn. + Args: + time: time scalar. + elements_finished: batch-size vector. + current_input: possibly nested tuple of input tensors. + emit_ta: possibly nested tuple of output TensorArrays. + state: possibly nested tuple of state tensors. + loop_state: possibly nested tuple of loop state tensors. + Returns: + Tuple having the same size as Args but with updated values. + """ + (next_output, cell_state) = cell(current_input, state) + + nest.assert_same_structure(state, cell_state) + nest.assert_same_structure(cell.output_size, next_output) + + next_time = time + 1 + (next_finished, next_input, next_state, emit_output, + next_loop_state) = loop_fn(next_time, next_output, cell_state, + loop_state) + + nest.assert_same_structure(state, next_state) + nest.assert_same_structure(current_input, next_input) + nest.assert_same_structure(emit_ta, emit_output) + + # If loop_fn returns None for next_loop_state, just reuse the + # previous one. + loop_state = loop_state if next_loop_state is None else next_loop_state + + def _copy_some_through(current, candidate): + """Copy some tensors through via array_ops.where.""" + + def copy_fn(cur_i, cand_i): + # TensorArray and scalar get passed through. + if isinstance(cur_i, tensor_array_ops.TensorArray): + return cand_i + if cur_i.shape.rank == 0: + return cand_i + # Otherwise propagate the old or the new value. + with ops.colocate_with(cand_i): + return array_ops.where(elements_finished, cur_i, cand_i) + + return nest.map_structure(copy_fn, current, candidate) + + emit_output = _copy_some_through(zero_emit, emit_output) + next_state = _copy_some_through(state, next_state) + + emit_ta = nest.map_structure(lambda ta, emit: ta.write(time, emit), + emit_ta, emit_output) + + elements_finished = math_ops.logical_or(elements_finished, next_finished) + + return (next_time, elements_finished, next_input, emit_ta, next_state, + loop_state) + + returned = control_flow_ops.while_loop( + condition, + body, + loop_vars=[ + time, elements_finished, next_input, emit_ta, state, loop_state + ], + parallel_iterations=parallel_iterations, + swap_memory=swap_memory) + + (emit_ta, final_state, final_loop_state) = returned[-3:] + + if init_loop_state is None: + final_loop_state = None + + return (emit_ta, final_state, final_loop_state) + + +@deprecation.deprecated(None, + "Please use `keras.layers.RNN(cell, unroll=True)`, " + "which is equivalent to this API") +@tf_export(v1=["nn.static_rnn"]) +def static_rnn(cell, + inputs, + initial_state=None, + dtype=None, + sequence_length=None, + scope=None): + """Creates a recurrent neural network specified by RNNCell `cell`. + The simplest form of RNN network generated is: + ```python + state = cell.zero_state(...) + outputs = [] + for input_ in inputs: + output, state = cell(input_, state) + outputs.append(output) + return (outputs, state) + ``` + However, a few other options are available: + An initial state can be provided. + If the sequence_length vector is provided, dynamic calculation is performed. + This method of calculation does not compute the RNN steps past the maximum + sequence length of the minibatch (thus saving computational time), + and properly propagates the state at an example's sequence length + to the final state output. + The dynamic calculation performed is, at time `t` for batch row `b`, + ```python + (output, state)(b, t) = + (t >= sequence_length(b)) + ? (zeros(cell.output_size), states(b, sequence_length(b) - 1)) + : cell(input(b, t), state(b, t - 1)) + ``` + Args: + cell: An instance of RNNCell. + inputs: A length T list of inputs, each a `Tensor` of shape `[batch_size, + input_size]`, or a nested tuple of such elements. + initial_state: (optional) An initial state for the RNN. If `cell.state_size` + is an integer, this must be a `Tensor` of appropriate type and shape + `[batch_size, cell.state_size]`. If `cell.state_size` is a tuple, this + should be a tuple of tensors having shapes `[batch_size, s] for s in + cell.state_size`. + dtype: (optional) The data type for the initial state and expected output. + Required if initial_state is not provided or RNN state has a heterogeneous + dtype. + sequence_length: Specifies the length of each sequence in inputs. An int32 + or int64 vector (tensor) size `[batch_size]`, values in `[0, T)`. + scope: VariableScope for the created subgraph; defaults to "rnn". + Returns: + A pair (outputs, state) where: + - outputs is a length T list of outputs (one for each input), or a nested + tuple of such elements. + - state is the final state + Raises: + TypeError: If `cell` is not an instance of RNNCell. + ValueError: If `inputs` is `None` or an empty list, or if the input depth + (column size) cannot be inferred from inputs via shape inference. + """ + rnn_cell_impl.assert_like_rnncell("cell", cell) + if not nest.is_sequence(inputs): + raise TypeError("inputs must be a sequence") + if not inputs: + raise ValueError("inputs must not be empty") + + outputs = [] + # Create a new scope in which the caching device is either + # determined by the parent scope, or is set to place the cached + # Variable using the same placement as for the rest of the RNN. + with vs.variable_scope(scope or "rnn") as varscope: + if _should_cache(): + if varscope.caching_device is None: + varscope.set_caching_device(lambda op: op.device) + + # Obtain the first sequence of the input + first_input = inputs + while nest.is_sequence(first_input): + first_input = first_input[0] + + # Temporarily avoid EmbeddingWrapper and seq2seq badness + # TODO(lukaszkaiser): remove EmbeddingWrapper + if first_input.get_shape().rank != 1: + + input_shape = first_input.get_shape().with_rank_at_least(2) + fixed_batch_size = input_shape.dims[0] + + flat_inputs = nest.flatten(inputs) + for flat_input in flat_inputs: + input_shape = flat_input.get_shape().with_rank_at_least(2) + batch_size, input_size = tensor_shape.dimension_at_index( + input_shape, 0), input_shape[1:] + fixed_batch_size.merge_with(batch_size) + for i, size in enumerate(input_size.dims): + if tensor_shape.dimension_value(size) is None: + raise ValueError( + "Input size (dimension %d of inputs) must be accessible via " + "shape inference, but saw value None." % i) + else: + fixed_batch_size = first_input.get_shape().with_rank_at_least(1)[0] + + if tensor_shape.dimension_value(fixed_batch_size): + batch_size = tensor_shape.dimension_value(fixed_batch_size) + else: + batch_size = array_ops.shape(first_input)[0] + if initial_state is not None: + state = initial_state + else: + if not dtype: + raise ValueError("If no initial_state is provided, " + "dtype must be specified") + if getattr(cell, "get_initial_state", None) is not None: + state = cell.get_initial_state( + inputs=None, batch_size=batch_size, dtype=dtype) + else: + state = cell.zero_state(batch_size, dtype) + + if sequence_length is not None: # Prepare variables + sequence_length = ops.convert_to_tensor( + sequence_length, name="sequence_length") + if sequence_length.get_shape().rank not in (None, 1): + raise ValueError( + "sequence_length must be a vector of length batch_size") + + def _create_zero_output(output_size): + # convert int to TensorShape if necessary + size = _concat(batch_size, output_size) + output = array_ops.zeros( + array_ops.stack(size), _infer_state_dtype(dtype, state)) + shape = _concat( + tensor_shape.dimension_value(fixed_batch_size), + output_size, + static=True) + output.set_shape(tensor_shape.TensorShape(shape)) + return output + + output_size = cell.output_size + flat_output_size = nest.flatten(output_size) + flat_zero_output = tuple( + _create_zero_output(size) for size in flat_output_size) + zero_output = nest.pack_sequence_as( + structure=output_size, flat_sequence=flat_zero_output) + + sequence_length = math_ops.cast(sequence_length, dtypes.int32) + min_sequence_length = math_ops.reduce_min(sequence_length) + max_sequence_length = math_ops.reduce_max(sequence_length) + + # Keras RNN cells only accept state as list, even if it's a single tensor. + is_keras_rnn_cell = _is_keras_rnn_cell(cell) + if is_keras_rnn_cell and not nest.is_sequence(state): + state = [state] + for time, input_ in enumerate(inputs): + if time > 0: + varscope.reuse_variables() + # pylint: disable=cell-var-from-loop + call_cell = lambda: cell(input_, state) + # pylint: enable=cell-var-from-loop + if sequence_length is not None: + (output, state) = _rnn_step( + time=time, + sequence_length=sequence_length, + min_sequence_length=min_sequence_length, + max_sequence_length=max_sequence_length, + zero_output=zero_output, + state=state, + call_cell=call_cell, + state_size=cell.state_size) + else: + (output, state) = call_cell() + outputs.append(output) + # Keras RNN cells only return state as list, even if it's a single tensor. + if is_keras_rnn_cell and len(state) == 1: + state = state[0] + + return (outputs, state) + + +@deprecation.deprecated(None, + "Please use `keras.layers.RNN(cell, stateful=True)`, " + "which is equivalent to this API") +@tf_export(v1=["nn.static_state_saving_rnn"]) +def static_state_saving_rnn(cell, + inputs, + state_saver, + state_name, + sequence_length=None, + scope=None): + """RNN that accepts a state saver for time-truncated RNN calculation. + Args: + cell: An instance of `RNNCell`. + inputs: A length T list of inputs, each a `Tensor` of shape `[batch_size, + input_size]`. + state_saver: A state saver object with methods `state` and `save_state`. + state_name: Python string or tuple of strings. The name to use with the + state_saver. If the cell returns tuples of states (i.e., `cell.state_size` + is a tuple) then `state_name` should be a tuple of strings having the same + length as `cell.state_size`. Otherwise it should be a single string. + sequence_length: (optional) An int32/int64 vector size [batch_size]. See the + documentation for rnn() for more details about sequence_length. + scope: VariableScope for the created subgraph; defaults to "rnn". + Returns: + A pair (outputs, state) where: + outputs is a length T list of outputs (one for each input) + states is the final state + Raises: + TypeError: If `cell` is not an instance of RNNCell. + ValueError: If `inputs` is `None` or an empty list, or if the arity and + type of `state_name` does not match that of `cell.state_size`. + """ + state_size = cell.state_size + state_is_tuple = nest.is_sequence(state_size) + state_name_tuple = nest.is_sequence(state_name) + + if state_is_tuple != state_name_tuple: + raise ValueError("state_name should be the same type as cell.state_size. " + "state_name: %s, cell.state_size: %s" % + (str(state_name), str(state_size))) + + if state_is_tuple: + state_name_flat = nest.flatten(state_name) + state_size_flat = nest.flatten(state_size) + + if len(state_name_flat) != len(state_size_flat): + raise ValueError("#elems(state_name) != #elems(state_size): %d vs. %d" % + (len(state_name_flat), len(state_size_flat))) + + initial_state = nest.pack_sequence_as( + structure=state_size, + flat_sequence=[state_saver.state(s) for s in state_name_flat]) + else: + initial_state = state_saver.state(state_name) + + (outputs, state) = static_rnn( + cell, + inputs, + initial_state=initial_state, + sequence_length=sequence_length, + scope=scope) + + if state_is_tuple: + flat_state = nest.flatten(state) + state_name = nest.flatten(state_name) + save_state = [ + state_saver.save_state(name, substate) + for name, substate in zip(state_name, flat_state) + ] + else: + save_state = [state_saver.save_state(state_name, state)] + + with ops.control_dependencies(save_state): + last_output = outputs[-1] + flat_last_output = nest.flatten(last_output) + flat_last_output = [ + array_ops.identity(output) for output in flat_last_output + ] + outputs[-1] = nest.pack_sequence_as( + structure=last_output, flat_sequence=flat_last_output) + + if state_is_tuple: + state = nest.pack_sequence_as( + structure=state, + flat_sequence=[array_ops.identity(s) for s in flat_state]) + else: + state = array_ops.identity(state) + + return (outputs, state) + + +@deprecation.deprecated(None, "Please use `keras.layers.Bidirectional(" + "keras.layers.RNN(cell, unroll=True))`, which is " + "equivalent to this API") +@tf_export(v1=["nn.static_bidirectional_rnn"]) +def static_bidirectional_rnn(cell_fw, + cell_bw, + inputs, + initial_state_fw=None, + initial_state_bw=None, + dtype=None, + sequence_length=None, + scope=None): + """Creates a bidirectional recurrent neural network. + Similar to the unidirectional case above (rnn) but takes input and builds + independent forward and backward RNNs with the final forward and backward + outputs depth-concatenated, such that the output will have the format + [time][batch][cell_fw.output_size + cell_bw.output_size]. The input_size of + forward and backward cell must match. The initial state for both directions + is zero by default (but can be set optionally) and no intermediate states are + ever returned -- the network is fully unrolled for the given (passed in) + length(s) of the sequence(s) or completely unrolled if length(s) is not given. + Args: + cell_fw: An instance of RNNCell, to be used for forward direction. + cell_bw: An instance of RNNCell, to be used for backward direction. + inputs: A length T list of inputs, each a tensor of shape [batch_size, + input_size], or a nested tuple of such elements. + initial_state_fw: (optional) An initial state for the forward RNN. This must + be a tensor of appropriate type and shape `[batch_size, + cell_fw.state_size]`. If `cell_fw.state_size` is a tuple, this should be a + tuple of tensors having shapes `[batch_size, s] for s in + cell_fw.state_size`. + initial_state_bw: (optional) Same as for `initial_state_fw`, but using the + corresponding properties of `cell_bw`. + dtype: (optional) The data type for the initial state. Required if either + of the initial states are not provided. + sequence_length: (optional) An int32/int64 vector, size `[batch_size]`, + containing the actual lengths for each of the sequences. + scope: VariableScope for the created subgraph; defaults to + "bidirectional_rnn" + Returns: + A tuple (outputs, output_state_fw, output_state_bw) where: + outputs is a length `T` list of outputs (one for each input), which + are depth-concatenated forward and backward outputs. + output_state_fw is the final state of the forward rnn. + output_state_bw is the final state of the backward rnn. + Raises: + TypeError: If `cell_fw` or `cell_bw` is not an instance of `RNNCell`. + ValueError: If inputs is None or an empty list. + """ + rnn_cell_impl.assert_like_rnncell("cell_fw", cell_fw) + rnn_cell_impl.assert_like_rnncell("cell_bw", cell_bw) + if not nest.is_sequence(inputs): + raise TypeError("inputs must be a sequence") + if not inputs: + raise ValueError("inputs must not be empty") + + with vs.variable_scope(scope or "bidirectional_rnn"): + # Forward direction + with vs.variable_scope("fw") as fw_scope: + output_fw, output_state_fw = static_rnn( + cell_fw, + inputs, + initial_state_fw, + dtype, + sequence_length, + scope=fw_scope) + + # Backward direction + with vs.variable_scope("bw") as bw_scope: + reversed_inputs = _reverse_seq(inputs, sequence_length) + tmp, output_state_bw = static_rnn( + cell_bw, + reversed_inputs, + initial_state_bw, + dtype, + sequence_length, + scope=bw_scope) + + output_bw = _reverse_seq(tmp, sequence_length) + # Concat each of the forward/backward outputs + flat_output_fw = nest.flatten(output_fw) + flat_output_bw = nest.flatten(output_bw) + + flat_outputs = tuple( + array_ops.concat([fw, bw], 1) + for fw, bw in zip(flat_output_fw, flat_output_bw)) + + outputs = nest.pack_sequence_as( + structure=output_fw, flat_sequence=flat_outputs) + + return (outputs, output_state_fw, output_state_bw) \ No newline at end of file -- Gitee From d7845a2234049202043faf55dcbf520f4d7302f1 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:34:05 +0000 Subject: [PATCH 29/38] add TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/read_params.py. --- .../script/read_params.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/read_params.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/read_params.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/read_params.py new file mode 100644 index 000000000..a879de615 --- /dev/null +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/read_params.py @@ -0,0 +1,18 @@ +import tensorflow as tf + + +def read_checkpoint(): + w = [] + checkpoint_path = 'dnn_best_model/ckpt_noshuffDIEN3' + reader = tf.train.NewCheckpointReader(checkpoint_path) + var = reader.get_variable_to_shape_map() + for key in var: + # if 'moving' in key: + # if 'weights' in key and 'conv' in key and 'Mo' not in key: + print('tensorname:', key) + print(reader.get_tensor(key)) + + +if __name__ == '__main__': + # tf.app.run() + read_checkpoint() \ No newline at end of file -- Gitee From d89512ac942c7560fff0be8b4e41b3ecce3789f7 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:53:47 +0000 Subject: [PATCH 30/38] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20TensorFlow/?= =?UTF-8?q?built-in/recommendation/DIEN=5FID0109=5Ffor=5FTensorFlow=20?= =?UTF-8?q?=E4=B8=BA=20TensorFlow/built-in/recommendation/DIEN=5Fbig=5Fker?= =?UTF-8?q?nel=5FID0109=5Ffor=5FTensorFlow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ prepare_data.sh | 0 .../.keep | 0 .../LICENSE | 0 .../README.md | 0 .../modelzoo_level.txt | 0 .../requirements.txt | 0 .../run.sh | 0 .../script/.keep | 0 .../script/Dice.py | 0 .../script/data_iterator.py | 0 .../script/generate_voc.py | 0 .../script/local_aggretor.py | 0 .../script/model.py | 0 .../script/model_general.py | 0 .../script/my_rnn.py | 0 .../script/process_data.py | 0 .../script/read_params.py | 0 .../script/run.py | 0 .../script/shuffle.py | 0 .../script/split_by_user.py | 0 .../script/train.py | 0 .../script/utils.py | 0 .../test/.keep | 0 .../test/train_full_1p.sh | 0 .../test/train_performance_1p.sh | 0 25 files changed, 0 insertions(+), 0 deletions(-) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/ prepare_data.sh (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/.keep (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/LICENSE (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/README.md (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/modelzoo_level.txt (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/requirements.txt (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/run.sh (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/.keep (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/Dice.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/data_iterator.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/generate_voc.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/local_aggretor.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/model.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/model_general.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/my_rnn.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/process_data.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/read_params.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/run.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/shuffle.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/split_by_user.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/train.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/script/utils.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/test/.keep (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/test/train_full_1p.sh (100%) rename TensorFlow/built-in/recommendation/{DIEN_ID0109_for_TensorFlow => DIEN_big_kernel_ID0109_for_TensorFlow}/test/train_performance_1p.sh (100%) diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/ prepare_data.sh b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/ prepare_data.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/ prepare_data.sh rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/ prepare_data.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/.keep b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/.keep similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/.keep rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/.keep diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/LICENSE b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/LICENSE similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/LICENSE rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/LICENSE diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/README.md b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/README.md similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/README.md rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/README.md diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/modelzoo_level.txt b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/modelzoo_level.txt similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/modelzoo_level.txt rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/modelzoo_level.txt diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/requirements.txt b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/requirements.txt similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/requirements.txt rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/requirements.txt diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/run.sh b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/run.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/run.sh rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/run.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/.keep b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/.keep similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/.keep rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/.keep diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/Dice.py b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/Dice.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/Dice.py rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/Dice.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/data_iterator.py b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/data_iterator.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/data_iterator.py rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/data_iterator.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/generate_voc.py b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/generate_voc.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/generate_voc.py rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/generate_voc.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/local_aggretor.py b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/local_aggretor.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/local_aggretor.py rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/local_aggretor.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model.py b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/model.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model.py rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/model.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model_general.py b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/model_general.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model_general.py rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/model_general.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/my_rnn.py b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/my_rnn.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/my_rnn.py rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/my_rnn.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/process_data.py b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/process_data.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/process_data.py rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/process_data.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/read_params.py b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/read_params.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/read_params.py rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/read_params.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/run.py b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/run.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/run.py rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/run.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/shuffle.py b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/shuffle.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/shuffle.py rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/shuffle.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/split_by_user.py b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/split_by_user.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/split_by_user.py rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/split_by_user.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/train.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/train.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/utils.py b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/utils.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/utils.py rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/utils.py diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/.keep b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/test/.keep similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/.keep rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/test/.keep diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/test/train_full_1p.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/test/train_full_1p.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh b/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/test/train_performance_1p.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh rename to TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/test/train_performance_1p.sh -- Gitee From af8d33a8151c0d41a43cdb626bfc0676ac5fb543 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:53:56 +0000 Subject: [PATCH 31/38] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20TensorFlow/?= =?UTF-8?q?built-in/recommendation/DIEN=5Fsmall=5Fkernel=5FID0109=5Ffor=5F?= =?UTF-8?q?TensorFlow=20=E4=B8=BA=20TensorFlow/built-in/recommendation/DIE?= =?UTF-8?q?N=5FID0109=5Ffor=5FTensorFlow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LICENSE | 0 .../README.md | 0 .../modelzoo_level.txt | 0 .../prepare_data.sh | 0 .../requirements.txt | 0 .../run.sh | 0 .../script/Dice.py | 0 .../script/data_iterator.py | 0 .../script/generate_voc.py | 0 .../script/local_aggretor.py | 0 .../script/model.py | 0 .../script/process_data.py | 0 .../script/rnn.py | 0 .../script/shuffle.py | 0 .../script/split_by_user.py | 0 .../script/train.py | 0 .../script/utils.py | 0 .../test/train_full_1p.sh | 0 .../test/train_performance_1p.sh | 0 19 files changed, 0 insertions(+), 0 deletions(-) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/LICENSE (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/README.md (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/modelzoo_level.txt (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/prepare_data.sh (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/requirements.txt (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/run.sh (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/script/Dice.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/script/data_iterator.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/script/generate_voc.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/script/local_aggretor.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/script/model.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/script/process_data.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/script/rnn.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/script/shuffle.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/script/split_by_user.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/script/train.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/script/utils.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/test/train_full_1p.sh (100%) rename TensorFlow/built-in/recommendation/{DIEN_small_kernel_ID0109_for_TensorFlow => DIEN_ID0109_for_TensorFlow}/test/train_performance_1p.sh (100%) diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/LICENSE b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/LICENSE similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/LICENSE rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/LICENSE diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/README.md b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/README.md similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/README.md rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/README.md diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/modelzoo_level.txt b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/modelzoo_level.txt similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/modelzoo_level.txt rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/modelzoo_level.txt diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/prepare_data.sh b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/prepare_data.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/prepare_data.sh rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/prepare_data.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/requirements.txt b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/requirements.txt similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/requirements.txt rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/requirements.txt diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/run.sh b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/run.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/run.sh rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/run.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/Dice.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/Dice.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/Dice.py rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/Dice.py diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/data_iterator.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/data_iterator.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/data_iterator.py rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/data_iterator.py diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/generate_voc.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/generate_voc.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/generate_voc.py rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/generate_voc.py diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/local_aggretor.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/local_aggretor.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/local_aggretor.py rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/local_aggretor.py diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/model.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/model.py rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/model.py diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/process_data.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/process_data.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/process_data.py rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/process_data.py diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/rnn.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/rnn.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/rnn.py rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/rnn.py diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/shuffle.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/shuffle.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/shuffle.py rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/shuffle.py diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/split_by_user.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/split_by_user.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/split_by_user.py rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/split_by_user.py diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/train.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/train.py rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/train.py diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/utils.py b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/utils.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/script/utils.py rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/script/utils.py diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_full_1p.sh b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_full_1p.sh rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_performance_1p.sh b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_small_kernel_ID0109_for_TensorFlow/test/train_performance_1p.sh rename to TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh -- Gitee From 547ce3adcc12458c8f807e0105037aa66e466c3a Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:54:15 +0000 Subject: [PATCH 32/38] update TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh. --- .../DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh index 63423cabf..510d4fce0 100644 --- a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_full_1p.sh @@ -15,7 +15,7 @@ data_path="" #基础参数,需要模型审视修改 #网络名称,同目录名称 -Network="DIEN_small_kernel_ID0109_for_TensorFlow" +Network="DIEN_ID0109_for_TensorFlow" #训练epoch train_epochs=25400 #训练batch_size -- Gitee From db309a231089fc73e851e6df061834630ad6a8be Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 07:54:30 +0000 Subject: [PATCH 33/38] update TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh. --- .../DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh index 39017e1b9..0772a23aa 100644 --- a/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh +++ b/TensorFlow/built-in/recommendation/DIEN_ID0109_for_TensorFlow/test/train_performance_1p.sh @@ -15,7 +15,7 @@ data_path="" #基础参数,需要模型审视修改 #网络名称,同目录名称 -Network="DIEN_small_kernel_ID0109_for_TensorFlow" +Network="DIEN_ID0109_for_TensorFlow" #训练epoch train_epochs=500 #训练batch_size -- Gitee From 18973c898bf7e6e49915db1f8774a62e7b9a329d Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 08:00:43 +0000 Subject: [PATCH 34/38] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20TensorFlow/?= =?UTF-8?q?built-in/recommendation/DIEN=5Fbig=5Fkernel=5FID0109=5Ffor=5FTe?= =?UTF-8?q?nsorFlow=20=E4=B8=BA=20TensorFlow/built-in/recommendation/DIEN?= =?UTF-8?q?=5FID3065=5Ffor=5FTensorFlow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ prepare_data.sh | 0 .../.keep | 0 .../LICENSE | 0 .../README.md | 0 .../modelzoo_level.txt | 0 .../requirements.txt | 0 .../run.sh | 0 .../script/.keep | 0 .../script/Dice.py | 0 .../script/data_iterator.py | 0 .../script/generate_voc.py | 0 .../script/local_aggretor.py | 0 .../script/model.py | 0 .../script/model_general.py | 0 .../script/my_rnn.py | 0 .../script/process_data.py | 0 .../script/read_params.py | 0 .../script/run.py | 0 .../script/shuffle.py | 0 .../script/split_by_user.py | 0 .../script/train.py | 0 .../script/utils.py | 0 .../test/.keep | 0 .../test/train_full_1p.sh | 0 .../test/train_performance_1p.sh | 0 25 files changed, 0 insertions(+), 0 deletions(-) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/ prepare_data.sh (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/.keep (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/LICENSE (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/README.md (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/modelzoo_level.txt (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/requirements.txt (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/run.sh (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/.keep (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/Dice.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/data_iterator.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/generate_voc.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/local_aggretor.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/model.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/model_general.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/my_rnn.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/process_data.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/read_params.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/run.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/shuffle.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/split_by_user.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/train.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/script/utils.py (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/test/.keep (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/test/train_full_1p.sh (100%) rename TensorFlow/built-in/recommendation/{DIEN_big_kernel_ID0109_for_TensorFlow => DIEN_ID3065_for_TensorFlow}/test/train_performance_1p.sh (100%) diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/ prepare_data.sh b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/ prepare_data.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/ prepare_data.sh rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/ prepare_data.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/.keep b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/.keep similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/.keep rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/.keep diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/LICENSE b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/LICENSE similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/LICENSE rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/LICENSE diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/README.md b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/README.md similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/README.md rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/README.md diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/modelzoo_level.txt b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/modelzoo_level.txt similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/modelzoo_level.txt rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/modelzoo_level.txt diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/requirements.txt b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/requirements.txt similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/requirements.txt rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/requirements.txt diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/run.sh b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/run.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/run.sh rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/run.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/.keep b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/.keep similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/.keep rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/.keep diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/Dice.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/Dice.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/Dice.py rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/Dice.py diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/data_iterator.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/data_iterator.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/data_iterator.py rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/data_iterator.py diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/generate_voc.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/generate_voc.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/generate_voc.py rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/generate_voc.py diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/local_aggretor.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/local_aggretor.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/local_aggretor.py rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/local_aggretor.py diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/model.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/model.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/model.py rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/model.py diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/model_general.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/model_general.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/model_general.py rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/model_general.py diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/my_rnn.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/my_rnn.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/my_rnn.py rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/my_rnn.py diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/process_data.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/process_data.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/process_data.py rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/process_data.py diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/read_params.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/read_params.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/read_params.py rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/read_params.py diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/run.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/run.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/run.py rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/run.py diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/shuffle.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/shuffle.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/shuffle.py rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/shuffle.py diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/split_by_user.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/split_by_user.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/split_by_user.py rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/split_by_user.py diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/train.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/train.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/train.py rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/train.py diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/utils.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/utils.py similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/script/utils.py rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/utils.py diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/test/.keep b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/.keep similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/test/.keep rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/.keep diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/test/train_full_1p.sh b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_full_1p.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/test/train_full_1p.sh rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_full_1p.sh diff --git a/TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/test/train_performance_1p.sh b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_performance_1p.sh similarity index 100% rename from TensorFlow/built-in/recommendation/DIEN_big_kernel_ID0109_for_TensorFlow/test/train_performance_1p.sh rename to TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_performance_1p.sh -- Gitee From 1d1f7332ffc87330cd83b35faef446229d5d46a2 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 08:01:35 +0000 Subject: [PATCH 35/38] update TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_performance_1p.sh. --- .../DIEN_ID3065_for_TensorFlow/test/train_performance_1p.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_performance_1p.sh b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_performance_1p.sh index 52792b570..261950488 100644 --- a/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_performance_1p.sh +++ b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_performance_1p.sh @@ -14,7 +14,7 @@ data_path="" #基础参数,需要模型审视修改 #网络名称,同目录名称 -Network="DIEN_ID0109_for_TensorFlow" +Network="DIEN_ID3065_for_TensorFlow" #训练epoch train_epochs=500 #训练batch_size -- Gitee From 1cdd07b23a82dcf5c618af3cbc4b36f1c6ebef64 Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 08:01:51 +0000 Subject: [PATCH 36/38] update TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_full_1p.sh. --- .../DIEN_ID3065_for_TensorFlow/test/train_full_1p.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_full_1p.sh b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_full_1p.sh index ba8673a71..ecbd25457 100644 --- a/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_full_1p.sh +++ b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_full_1p.sh @@ -15,7 +15,7 @@ data_path="" #基础参数,需要模型审视修改 #网络名称,同目录名称 -Network="DIEN_ID0109_for_TensorFlow" +Network="DIEN_ID3065_for_TensorFlow" #训练epoch train_epochs=25400 #训练batch_size -- Gitee From ba7931422be9607edc5bd17f56565cf941f89f5f Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Mon, 28 Mar 2022 08:04:52 +0000 Subject: [PATCH 37/38] update TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_performance_1p.sh. --- .../test/train_performance_1p.sh | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_performance_1p.sh b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_performance_1p.sh index 261950488..c9355df63 100644 --- a/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_performance_1p.sh +++ b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/test/train_performance_1p.sh @@ -158,13 +158,40 @@ grep train_loss $cur_path/output/$ASCEND_DEVICE_ID/train_$ASCEND_DEVICE_ID.log|a ActualLoss=`awk 'END {print}' $cur_path/output/$ASCEND_DEVICE_ID/train_${CaseName}_loss.txt` #关键信息打印到${CaseName}.log中,不需要修改 +#echo "Network = ${Network}" > $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +#echo "RankSize = ${RANK_SIZE}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +#echo "BatchSize = ${BatchSize}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +#echo "DeviceType = ${DeviceType}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +#echo "CaseName = ${CaseName}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +#echo "ActualFPS = ${ActualFPS}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +#echo "TrainAccuracy = ${train_accuracy}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +#echo "TrainingTime = ${TrainingTime}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +#echo "ActualLoss = ${ActualLoss}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +#echo "E2ETrainingTime = ${e2e_time}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log + +##获取错误信息 +#系统错误信息 +error_msg="cannot import name 'DynamicAUGRU' from 'npu_bridge.estimator.npu.npu_dynamic_rnn'" +#判断错误信息是否和历史状态一致,此处无需修改 +Status=`grep "${error_msg}" $cur_path/output/$ASCEND_DEVICE_ID/train_$ASCEND_DEVICE_ID.log | wc -l` +#失败阶段,枚举值图准备FAIL/图拆分FAIL/图优化FAIL/图编译FAIL/图执行FAIL/流程OK +ModelStatus="图执行FAIL" +#DTS单号或者issue链接 +DTS_Number="AR0001TRFQ Florence V100R001C25_SF_02_020_SR_0800_AR_0001 +AR0001TRFR Florence V100R001C25_SF_02_020_SR_0800_AR_0002 +AR0001TRFS Florence V100R001C25_SF_02_020_SR_0800_AR_0003 +AR0001TRFT Florence V100R001C25_SF_02_020_SR_0801_AR_0001 +AR0001TRFU Florence V100R001C25_SF_02_020_SR_0801_AR_0002" + +#关键信息打印到CaseName.log中,此处无需修改 echo "Network = ${Network}" > $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -echo "RankSize = ${RANK_SIZE}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -echo "BatchSize = ${BatchSize}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "RankSize = ${RankSize}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log echo "DeviceType = ${DeviceType}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log echo "CaseName = ${CaseName}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -echo "ActualFPS = ${ActualFPS}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -echo "TrainAccuracy = ${train_accuracy}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -echo "TrainingTime = ${TrainingTime}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -echo "ActualLoss = ${ActualLoss}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "ModelStatus = ${ModelStatus}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "DTS_Number = ${DTS_Number}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "Status = ${Status}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "error_msg = ${error_msg}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "BatchSize = ${BatchSize}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "E2ETrainingTime = ${e2e_time}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log echo "E2ETrainingTime = ${e2e_time}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log \ No newline at end of file -- Gitee From 53215d3d66c475e0eacc2917f777a31afc66b3bd Mon Sep 17 00:00:00 2001 From: majun121 <867479212@qq.com> Date: Tue, 29 Mar 2022 01:20:48 +0000 Subject: [PATCH 38/38] update TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/model_general.py. --- .../DIEN_ID3065_for_TensorFlow/script/model_general.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/model_general.py b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/model_general.py index 822ba0981..1ff7d9739 100644 --- a/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/model_general.py +++ b/TensorFlow/built-in/recommendation/DIEN_ID3065_for_TensorFlow/script/model_general.py @@ -143,9 +143,8 @@ class Model(object): self.loss += self.aux_loss tf.summary.scalar('loss', self.loss) if fp16_and_no_seq_len_mode: - print("111111111111111111111111111 if") # self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr, epsilon=6e-8).minimize(self.loss) - self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr, epsilon=1e-4) + self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr) # loss_scale_manager = FixedLossScaleManager(loss_scale=1) # self.optimizer = NPULossScaleOptimizer(self.optimizer, loss_scale_manager) @@ -161,7 +160,6 @@ class Model(object): ### for npu else: - print("222222222222222222222222222 else") #self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.loss) ### for npu self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr) -- Gitee