├── .gitignore ├── LICENSE ├── README.md ├── download_hgb_datasets.sh ├── main_rphgnn.py ├── rphgnn ├── __init__.py ├── callbacks.py ├── configs │ └── default_param_config.py ├── datasets │ ├── hgb.py │ └── load_data.py ├── global_configuration.py ├── layers │ ├── __init__.py │ ├── rphgnn_encoder.py │ ├── rphgnn_pre.py │ └── torch_train_model.py ├── losses.py └── utils │ ├── __init__.py │ ├── argparse_utils.py │ ├── graph_utils.py │ ├── metrics_utils.py │ ├── nested_data_utils.py │ ├── random_project_utils.py │ ├── random_utils.py │ └── torch_data_utils.py └── scripts ├── run_ACM.sh ├── run_DBLP.sh ├── run_Freebase.sh ├── run_IMDB.sh ├── run_OAG-L1-Field.sh ├── run_OAG-Venue.sh ├── run_OGBN-MAG.sh └── run_leaderboard_OGBN-MAG.sh /.gitignore: -------------------------------------------------------------------------------- 1 | /datasets/ 2 | /downloads/ 3 | /outputs/ 4 | /logs/ 5 | /cache/ 6 | /saved_models/ 7 | *.p 8 | *.pt 9 | *.out 10 | 11 | # Byte-compiled / optimized / DLL files 12 | __pycache__/ 13 | *.py[cod] 14 | *$py.class 15 | 16 | # C extensions 17 | *.so 18 | 19 | # Distribution / packaging 20 | .Python 21 | build/ 22 | develop-eggs/ 23 | dist/ 24 | downloads/ 25 | eggs/ 26 | .eggs/ 27 | lib/ 28 | lib64/ 29 | parts/ 30 | sdist/ 31 | var/ 32 | wheels/ 33 | share/python-wheels/ 34 | *.egg-info/ 35 | .installed.cfg 36 | *.egg 37 | MANIFEST 38 | 39 | # PyInstaller 40 | # Usually these files are written by a python script from a template 41 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 42 | *.manifest 43 | *.spec 44 | 45 | # Installer logs 46 | pip-log.txt 47 | pip-delete-this-directory.txt 48 | 49 | # Unit test / coverage reports 50 | htmlcov/ 51 | .tox/ 52 | .nox/ 53 | .coverage 54 | .coverage.* 55 | .cache 56 | nosetests.xml 57 | coverage.xml 58 | *.cover 59 | *.py,cover 60 | .hypothesis/ 61 | .pytest_cache/ 62 | cover/ 63 | 64 | # Translations 65 | *.mo 66 | *.pot 67 | 68 | # Django stuff: 69 | *.log 70 | local_settings.py 71 | db.sqlite3 72 | db.sqlite3-journal 73 | 74 | # Flask stuff: 75 | instance/ 76 | .webassets-cache 77 | 78 | # Scrapy stuff: 79 | .scrapy 80 | 81 | # Sphinx documentation 82 | docs/_build/ 83 | 84 | # PyBuilder 85 | .pybuilder/ 86 | target/ 87 | 88 | # Jupyter Notebook 89 | .ipynb_checkpoints 90 | 91 | # IPython 92 | profile_default/ 93 | ipython_config.py 94 | 95 | # pyenv 96 | # For a library or package, you might want to ignore these files since the code is 97 | # intended to run in multiple environments; otherwise, check them in: 98 | # .python-version 99 | 100 | # pipenv 101 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 102 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 103 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 104 | # install all needed dependencies. 105 | #Pipfile.lock 106 | 107 | # poetry 108 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 109 | # This is especially recommended for binary packages to ensure reproducibility, and is more 110 | # commonly ignored for libraries. 111 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 112 | #poetry.lock 113 | 114 | # pdm 115 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 116 | #pdm.lock 117 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 118 | # in version control. 119 | # https://pdm.fming.dev/#use-with-ide 120 | .pdm.toml 121 | 122 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 123 | __pypackages__/ 124 | 125 | # Celery stuff 126 | celerybeat-schedule 127 | celerybeat.pid 128 | 129 | # SageMath parsed files 130 | *.sage.py 131 | 132 | # Environments 133 | .env 134 | .venv 135 | env/ 136 | venv/ 137 | ENV/ 138 | env.bak/ 139 | venv.bak/ 140 | 141 | # Spyder project settings 142 | .spyderproject 143 | .spyproject 144 | 145 | # Rope project settings 146 | .ropeproject 147 | 148 | # mkdocs documentation 149 | /site 150 | 151 | # mypy 152 | .mypy_cache/ 153 | .dmypy.json 154 | dmypy.json 155 | 156 | # Pyre type checker 157 | .pyre/ 158 | 159 | # pytype static type analyzer 160 | .pytype/ 161 | 162 | # Cython debug symbols 163 | cython_debug/ 164 | 165 | # PyCharm 166 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 167 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 168 | # and can be added to the global gitignore or merged into this file. For a more nuclear 169 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 170 | #.idea/ 171 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RpHGNN 2 | Source code and dataset of the paper "[Efficient Heterogeneous Graph Learning via Random Projection](https://arxiv.org/abs/2310.14481)", which is accepted by IEEE Transactions on Knowledge and Data Engineering (TKDE 2024). 3 | 4 | 5 | 6 | ## Homepage and Paper 7 | 8 | + Homepage (RpHGNN): [https://github.com/CrawlScript/RpHGNN](https://github.com/CrawlScript/RpHGNN) 9 | + Paper Access: 10 | - **IEEE Xplore**: [https://ieeexplore.ieee.org/document/10643347](https://ieeexplore.ieee.org/document/10643347) 11 | - **ArXiv**: [https://arxiv.org/abs/2310.14481](https://arxiv.org/abs/2310.14481) 12 | 13 | 14 | 15 | 16 | ## Requirements 17 | 18 | + Linux 19 | + Python 3.7 20 | + torch==1.12.1+cu113 21 | + torchmetrics==0.11.4 22 | + dgl==1.0.2+cu113 23 | + ogb==1.3.5 24 | + shortuuid==1.0.11 25 | + pandas==1.3.5 26 | + gensim==4.2.0 27 | + numpy==1.21.6 28 | + tqdm==4.64.1 29 | 30 | 31 | ## Download Preparation 32 | 33 | For HGB datasets (ACM, DBLP, Freebase, and IMDB): 34 | 35 | ```shell 36 | sh download_hgb_datasets.sh 37 | ``` 38 | 39 | For OAG-Venue and OAG-L1-Field, we follow NARS' data prepatation in [https://github.com/facebookresearch/NARS/tree/main/oag_dataset](https://github.com/facebookresearch/NARS/tree/main/oag_dataset). 40 | After generating *.pk and *.npy files, you have to: 41 | - put these files in the directory 42 | - rename graph_field.pk to graph_L1.pk 43 | 44 | 45 | For OGBN-MAG, the code will automatically download it via the ogb package. 46 | 47 | 48 | For OAG-Venue and OAG-L1-Field, we adhere to NARS' data preparation instructions found at [https://github.com/facebookresearch/NARS/tree/main/oag_dataset](https://github.com/facebookresearch/NARS/tree/main/oag_dataset). 49 | After generating *.pk and *.npy files, you should: 50 | - Place these files in the directory `./datasets/nars_academic_oag/`. 51 | - Rename graph_field.pk to graph_L1.pk. 52 | 53 | 54 | 55 | ## Run RpHGNN 56 | 57 | You can run RpHGNN with the following command: 58 | ```shell 59 | sh scripts/run_ACM.sh 60 | 61 | sh scripts/run_DBLP.sh 62 | 63 | sh scripts/run_Freebase.sh 64 | 65 | sh scripts/run_IMDB.sh 66 | 67 | sh scripts/run_OGBN-MAG.sh 68 | 69 | sh scripts/run_OAG-Venue.sh 70 | 71 | sh scripts/run_OAG-L1-Field.sh 72 | ``` 73 | 74 | 75 | ## Run RpHGNN for OGB Leaderboards (ogbn-mag) 76 | 77 | To reproduce the results on the OGB Leaderboards (ogbn-mag), follow the steps below: 78 | 79 | - Preparing Pre-trained Embeddings (Optional): 80 | - If the cache/mag.p file does not exist (embeddings pre-trained via LINE [1]), our code will automatically pre-train it and save the pre-trained embeddings in the specified path. 81 | - Alternatively, if you'd prefer to skip the pre-training step, download the pre-trained embeddings mag.p directly from [Google Drive](https://drive.google.com/file/d/1Q7gD1xpmLeFJu5xWWY3nwa46cM8xYClH/view?usp=sharing) and place it in the `cache` directory. 82 | 83 | 84 | - Execute the script: 85 | 86 | ```shell 87 | sh scripts/run_leaderboard_OGBN-MAG.sh 88 | ``` 89 | 90 | This script will run the training and evaluation using random seeds from 0 to 9. The output for seed i will be saved in the file nohup_leaderboard_mag_i.out. 91 | 92 | 93 | References: 94 | - [1] Jian Tang, Meng Qu, Mingzhe Wang, Ming Zhang, Jun Yan, and Qiaozhu Mei. "Line: Large-scale information network embedding." In Proceedings of the 24th international conference on world wide web, pp. 1067-1077. 2015. 95 | 96 | 97 | 98 | ## Cite 99 | 100 | If you use RpHGNN in a scientific publication, we would appreciate citations to the following paper: 101 | 102 | ``` 103 | @ARTICLE{10643347, 104 | author={Hu, Jun and Hooi, Bryan and He, Bingsheng}, 105 | journal={IEEE Transactions on Knowledge and Data Engineering}, 106 | title={Efficient Heterogeneous Graph Learning via Random Projection}, 107 | year={2024}, 108 | volume={}, 109 | number={}, 110 | pages={1-14}, 111 | doi={10.1109/TKDE.2024.3434956}} 112 | ``` 113 | 114 | 115 | 116 | 117 | 118 | __License:__ [GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html) 119 | 120 | Copyright (c) 2023-2024 Xtra Computing Group, NUS, Singapore. 121 | 122 | -------------------------------------------------------------------------------- /download_hgb_datasets.sh: -------------------------------------------------------------------------------- 1 | # Directories 2 | DOWNLOADS_DIR="./downloads" 3 | DATASETS_DIR="./datasets" 4 | 5 | mkdir $DOWNLOADS_DIR 6 | mkdir $DATASETS_DIR 7 | 8 | wget -P $DOWNLOADS_DIR https://github.com/CrawlScript/gnn_datasets/raw/master/HGB/ACM.zip 9 | wget -P $DOWNLOADS_DIR https://github.com/CrawlScript/gnn_datasets/raw/master/HGB/DBLP.zip 10 | wget -P $DOWNLOADS_DIR https://github.com/CrawlScript/gnn_datasets/raw/master/HGB/Freebase.zip 11 | wget -P $DOWNLOADS_DIR https://github.com/CrawlScript/gnn_datasets/raw/master/HGB/IMDB.zip 12 | 13 | 14 | # Ensure the datasets directory exists 15 | mkdir -p $DATASETS_DIR 16 | 17 | # Unzip each file 18 | unzip $DOWNLOADS_DIR/ACM.zip -d $DATASETS_DIR 19 | unzip $DOWNLOADS_DIR/DBLP.zip -d $DATASETS_DIR 20 | unzip $DOWNLOADS_DIR/Freebase.zip -d $DATASETS_DIR 21 | unzip $DOWNLOADS_DIR/IMDB.zip -d $DATASETS_DIR 22 | -------------------------------------------------------------------------------- /main_rphgnn.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import numpy as np 4 | import torch 5 | import torch.nn.functional as F 6 | import torchmetrics 7 | 8 | import shutil 9 | import logging 10 | import sys 11 | import time 12 | import os 13 | import json 14 | import datetime 15 | import shortuuid 16 | from argparse import ArgumentParser 17 | 18 | from rphgnn.callbacks import EarlyStoppingCallback, LoggingCallback, TensorBoardCallback 19 | from rphgnn.layers.rphgnn_encoder import RpHGNNEncoder 20 | from rphgnn.losses import kl_loss 21 | from rphgnn.utils.metrics_utils import MRR, NDCG 22 | from rphgnn.utils.random_project_utils import create_func_torch_random_project_create_kernel_sparse, torch_random_project_common, torch_random_project_create_kernel_xavier, torch_random_project_create_kernel_xavier_no_norm 23 | from rphgnn.utils.torch_data_utils import NestedDataLoader 24 | from rphgnn.global_configuration import global_config 25 | from rphgnn.utils.argparse_utils import parse_bool 26 | from rphgnn.utils.random_utils import reset_seed 27 | from rphgnn.configs.default_param_config import load_default_param_config 28 | from rphgnn.datasets.load_data import load_dgl_data 29 | from rphgnn.utils.nested_data_utils import gather_h_y, nested_gather, nested_map 30 | from rphgnn.layers.rphgnn_pre import rphgnn_propagate_and_collect, rphgnn_propagate_and_collect_label 31 | 32 | 33 | np.set_printoptions(precision=4, suppress=True) 34 | 35 | logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) 36 | logger = logging.getLogger() 37 | 38 | 39 | parser = ArgumentParser() 40 | 41 | parser.add_argument("--dataset", type=str, required=True) 42 | parser.add_argument("--method", type=str, required=True) 43 | parser.add_argument("--use_nrl", type=parse_bool, required=True) 44 | parser.add_argument("--use_input", type=parse_bool, required=True) 45 | parser.add_argument("--use_label", type=parse_bool, required=True) 46 | parser.add_argument("--even_odd", type=str, required=False, default="all") 47 | parser.add_argument("--use_all_feat", type=parse_bool, required=True) 48 | parser.add_argument("--train_strategy", type=str, required=True) 49 | parser.add_argument("--output_dir", type=str, required=True) 50 | parser.add_argument("--gpus", type=str, required=True) 51 | parser.add_argument("--input_drop_rate", type=float, required=False, default=None) 52 | parser.add_argument("--drop_rate", type=float, required=False, default=None) 53 | parser.add_argument("--hidden_size", type=int, required=False, default=None) 54 | parser.add_argument("--squash_k", type=int, required=False, default=None) 55 | parser.add_argument("--num_epochs", type=int, required=False, default=None) 56 | parser.add_argument("--max_patience", type=int, required=False, default=None) 57 | parser.add_argument("--embedding_size", type=int, required=False, default=None) 58 | parser.add_argument("--rps", type=str, required=False, default="sp_3.0", help="random projection strategies") 59 | parser.add_argument("--seed", type=int, required=True) 60 | 61 | 62 | # sys.argv += cmd.split() 63 | args = parser.parse_args() 64 | 65 | method = args.method 66 | dataset = args.dataset 67 | use_all_feat = args.use_all_feat 68 | use_nrl = args.use_nrl 69 | use_label = args.use_label 70 | train_strategy = args.train_strategy 71 | use_input_features = args.use_input 72 | output_dir = args.output_dir 73 | gpu_ids = args.gpus 74 | device = "cuda" 75 | data_loader_device = device 76 | even_odd = args.even_odd 77 | random_projection_strategy = args.rps 78 | seed = args.seed 79 | 80 | os.environ["CUDA_VISIBLE_DEVICES"] = gpu_ids 81 | reset_seed(seed) 82 | print("seed = ", seed) 83 | 84 | 85 | global_config.torch_random_project = torch_random_project_common 86 | if random_projection_strategy.startswith("sp"): 87 | random_projection_sparsity = float(random_projection_strategy.split("_")[1]) 88 | global_config.torch_random_project_create_kernel = create_func_torch_random_project_create_kernel_sparse(s=random_projection_sparsity) 89 | print("setting random projection strategy: sparse({} ...)".format(random_projection_sparsity)) 90 | elif random_projection_strategy == "gaussian": 91 | global_config.torch_random_project_create_kernel = torch_random_project_create_kernel_xavier 92 | print("setting random projection strategy: gaussian ...") 93 | 94 | elif random_projection_strategy == "gaussian_no_norm": 95 | global_config.torch_random_project_create_kernel = torch_random_project_create_kernel_xavier_no_norm 96 | print("setting random projection strategy: gaussian ...") 97 | 98 | else: 99 | raise ValueError("unknown random projection strategy: {}".format(random_projection_strategy)) 100 | 101 | 102 | pre_device = "cpu" 103 | learning_rate = 3e-3 104 | l2_coef = None 105 | norm = "mean" 106 | squash_strategy = "project_norm_sum" 107 | target_h_dtype = torch.float16 108 | 109 | timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S") 110 | 111 | running_leaderboard_mag = dataset == "mag" and use_label 112 | 113 | # hyper-parameters for ogbn-mag learderboard (lp+cl) 114 | if running_leaderboard_mag: 115 | scheduler_gamma = 0.99 116 | num_views = 3 117 | cl_rate = 0.6 118 | model_save_dir = "saved_models" 119 | if not os.path.exists(model_save_dir): 120 | os.makedirs(model_save_dir) 121 | model_save_path = os.path.join(model_save_dir, "leaderboard_mag_seed_{}.pt".format(seed)) 122 | else: 123 | scheduler_gamma = None 124 | num_views = 1 125 | cl_rate = None 126 | model_save_path = None 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | arg_dict = {**vars(args)} 135 | arg_dict["date"] = timestamp 136 | del arg_dict["output_dir"] 137 | del arg_dict["gpus"] 138 | 139 | args_desc_items = [] 140 | for key, value in arg_dict.items(): 141 | args_desc_items.append(key) 142 | args_desc_items.append(str(value)) 143 | args_desc = "_".join(args_desc_items) 144 | 145 | uuid = "{}_{}".format(timestamp, shortuuid.uuid()) 146 | 147 | tmp_output_fname = "{}.json.tmp".format(uuid) 148 | tmp_output_fpath = os.path.join(output_dir, tmp_output_fname) 149 | 150 | output_fname = "{}.json".format(uuid) 151 | output_fpath = os.path.join(output_dir, output_fname) 152 | 153 | 154 | print(output_dir) 155 | print(os.path.exists(output_dir)) 156 | if not os.path.exists(output_dir): 157 | os.makedirs(output_dir) 158 | 159 | with open(tmp_output_fpath, "a", encoding="utf-8") as f: 160 | f.write("{}\n".format(json.dumps(arg_dict))) 161 | 162 | 163 | time_dict = { 164 | "start": time.time() 165 | } 166 | 167 | squash_k, inner_k, conv_filters, num_layers_list, hidden_size, merge_mode, input_drop_rate, drop_rate, \ 168 | use_pretrain_features, random_projection_align, input_random_projection_size, target_feat_random_project_size, add_self_group = load_default_param_config(dataset) 169 | 170 | 171 | embedding_size = None 172 | 173 | if args.embedding_size is not None: 174 | embedding_size = args.embedding_size 175 | print("reset embedding_size => {}".format(embedding_size)) 176 | 177 | with torch.no_grad(): 178 | hetero_graph, target_node_type, feature_node_types, (train_index, valid_index, test_index), \ 179 | batch_size, num_epochs, patience, validation_freq, convert_to_tensor = load_dgl_data( 180 | dataset, 181 | use_all_feat=use_all_feat, 182 | embedding_size=embedding_size, 183 | use_nrl=use_nrl 184 | ) 185 | 186 | 187 | if args.input_drop_rate is not None: 188 | input_drop_rate = args.input_drop_rate 189 | print("reset input_drop_rate => {}".format(input_drop_rate)) 190 | 191 | if args.drop_rate is not None: 192 | drop_rate = args.drop_rate 193 | print("reset drop_rate => {}".format(drop_rate)) 194 | 195 | if args.hidden_size is not None: 196 | hidden_size = args.hidden_size 197 | print("reset hidden_size => {}".format(hidden_size)) 198 | 199 | if args.squash_k is not None: 200 | squash_k = args.squash_k 201 | print("reset squash_k => {}".format(squash_k)) 202 | 203 | if args.num_epochs is not None: 204 | num_epochs = args.num_epochs 205 | print("reset num_epochs => {}".format(num_epochs)) 206 | 207 | if args.max_patience is not None: 208 | patience = args.max_patience 209 | print("reset patience => {}".format(patience)) 210 | 211 | y = hetero_graph.ndata["label"][target_node_type].detach().cpu().numpy() 212 | 213 | print("train_rate = {}\tvalid_rate = {}\ttest_rate = {}".format(len(train_index) / len(y), len(valid_index) / len(y), len(test_index) / len(y))) 214 | 215 | multi_label = len(y.shape) > 1 216 | 217 | if multi_label: 218 | num_classes = y.shape[-1] 219 | else: 220 | num_classes = y.max() + 1 221 | 222 | 223 | stage_output_dict = { 224 | "last": None 225 | } 226 | 227 | 228 | print("start pre-computation ...") 229 | 230 | log_dir = "logs/{}".format(args_desc) 231 | 232 | torch_y = torch.tensor(y).long() 233 | 234 | if multi_label: 235 | torch_y = torch_y.float() 236 | 237 | train_mask = np.zeros([len(y)]) 238 | train_mask[train_index] = 1.0 239 | torch_train_mask = torch.tensor(train_mask).bool() 240 | 241 | if even_odd == "odd": 242 | squash_k *= 2 243 | print("odd mode, squash_k =", squash_k) 244 | 245 | 246 | 247 | def create_label_target_h_list_list(): 248 | print("using new train_label_feat") 249 | train_label_feat = torch.ones([len(y), num_classes]).float() / num_classes 250 | train_label_feat[train_index] = F.one_hot(torch.tensor(y[train_index]), num_classes).float() 251 | 252 | label_target_h_list_list = rphgnn_propagate_and_collect_label(hetero_graph, target_node_type, y, train_label_feat) 253 | label_target_h_list_list = nested_map(label_target_h_list_list, lambda x: x.to(target_h_dtype).to(pre_device)) 254 | return label_target_h_list_list 255 | 256 | if use_label: 257 | if dataset != "mag": 258 | raise Exception("use_label is only supported for mag dataset") 259 | label_target_h_list_list = create_label_target_h_list_list() 260 | else: 261 | label_target_h_list_list = [] 262 | 263 | 264 | feat_target_h_list_list, target_sorted_keys = rphgnn_propagate_and_collect(hetero_graph, 265 | squash_k, 266 | inner_k, 267 | 0.0, 268 | target_node_type, 269 | use_input_features=use_input_features, squash_strategy=squash_strategy, 270 | train_label_feat=None, 271 | norm=norm, 272 | squash_even_odd=even_odd, 273 | collect_even_odd=even_odd, 274 | squash_self=False, 275 | target_feat_random_project_size=target_feat_random_project_size, 276 | add_self_group=add_self_group 277 | ) 278 | 279 | feat_target_h_list_list = nested_map(feat_target_h_list_list, lambda x: x.to(target_h_dtype).to(pre_device)) 280 | target_h_list_list = feat_target_h_list_list + label_target_h_list_list 281 | 282 | 283 | time_dict["pre_compute"] = time.time() 284 | pre_compute_time = time_dict["pre_compute"] - time_dict["start"] 285 | print("pre_compute time: ", pre_compute_time) 286 | 287 | 288 | accuracy_metric = torchmetrics.Accuracy("multilabel", num_labels=int(num_classes)) if multi_label else torchmetrics.Accuracy("multiclass" if multi_label else "multiclass", num_classes=int(num_classes)) 289 | if dataset in ["oag_L1", "oag_venue"]: 290 | metrics_dict = { 291 | "accuracy": accuracy_metric, 292 | "ndcg": NDCG(), 293 | "mrr": MRR() 294 | } 295 | else: 296 | metrics_dict = { 297 | "accuracy": accuracy_metric, 298 | "micro_f1": torchmetrics.F1Score(task="multilabel", num_labels=int(num_classes), average="micro") if multi_label else torchmetrics.F1Score(task="multiclass", num_classes=int(num_classes), average="micro"), 299 | "macro_f1": torchmetrics.F1Score(task="multilabel", num_labels=int(num_classes), average="macro") if multi_label else torchmetrics.F1Score(task="multiclass", num_classes=int(num_classes), average="macro"), 300 | } 301 | metrics_dict = {metric_name: metric.to(device) for metric_name, metric in metrics_dict.items()} 302 | 303 | 304 | print("create model ====") 305 | model = RpHGNNEncoder( 306 | conv_filters, 307 | [hidden_size] * num_layers_list[0], 308 | [hidden_size] * (num_layers_list[2] - 1) + [num_classes], 309 | merge_mode, 310 | input_shape=nested_map(target_h_list_list, lambda x: list(x.size())), 311 | input_drop_rate=input_drop_rate, 312 | drop_rate=drop_rate, 313 | activation="prelu", 314 | output_activation="identity", 315 | metrics_dict=metrics_dict, 316 | multi_label=multi_label, 317 | loss_func=kl_loss if dataset == "oag_L1" else None, 318 | learning_rate=learning_rate, 319 | scheduler_gamma=scheduler_gamma, 320 | train_strategy=train_strategy, 321 | num_views=num_views, 322 | cl_rate=cl_rate 323 | 324 | ).to(device) 325 | 326 | print(model) 327 | 328 | print("number of params:", sum(p.numel() for p in model.parameters())) 329 | logging_callback = LoggingCallback(tmp_output_fpath, {"pre_compute_time": pre_compute_time}) 330 | tensor_board_callback = TensorBoardCallback( 331 | "logs/{}/{}".format(dataset, timestamp) 332 | ) 333 | 334 | 335 | 336 | def train_and_eval(): 337 | 338 | train_h_list_list, train_y = nested_gather([target_h_list_list, torch_y], train_index) 339 | valid_h_list_list, valid_y = nested_gather([target_h_list_list, torch_y], valid_index) 340 | test_h_list_list, test_y = nested_gather([target_h_list_list, torch_y], test_index) 341 | 342 | 343 | if train_strategy == "common": 344 | train_data_loader = NestedDataLoader( 345 | [train_h_list_list, train_y], 346 | batch_size=batch_size, shuffle=True, device=data_loader_device 347 | ) 348 | 349 | elif train_strategy == "cl": 350 | 351 | seen_mask = torch.zeros_like(torch_y, dtype=torch.bool) 352 | seen_mask[train_index] = True 353 | seen_mask[valid_index] = True 354 | seen_mask[test_index] = True 355 | 356 | def get_seen(x): 357 | print("get seen ...") 358 | with torch.no_grad(): 359 | return nested_map(x, lambda x: x[seen_mask]) 360 | 361 | train_data_loader = NestedDataLoader( 362 | [get_seen(target_h_list_list), get_seen(torch_y), get_seen(torch_train_mask)], 363 | batch_size=batch_size, shuffle=True, device=data_loader_device 364 | ) 365 | 366 | else: 367 | raise Exception("invalid train strategy: {}".format(train_strategy)) 368 | 369 | 370 | 371 | valid_data_loader =NestedDataLoader( 372 | [valid_h_list_list, valid_y], 373 | batch_size=batch_size, shuffle=False, device=data_loader_device 374 | ) 375 | test_data_loader = NestedDataLoader( 376 | [test_h_list_list, test_y], 377 | batch_size=batch_size, shuffle=False, device=data_loader_device 378 | ) 379 | 380 | if dataset in ["oag_L1", "oag_venue"]: 381 | early_stop_strategy = "score" 382 | early_stop_metric_names = ["ndcg"] 383 | elif dataset in ["mag"]: 384 | early_stop_strategy = "score" 385 | early_stop_metric_names = ["accuracy"] 386 | elif dataset in ["dblp"]: 387 | early_stop_strategy = "loss" 388 | early_stop_metric_names = ["macro_f1", "micro_f1"] 389 | else: 390 | early_stop_strategy = "score" 391 | early_stop_metric_names = ["macro_f1", "micro_f1"] 392 | 393 | print("early_stop_metric_names = {}".format(early_stop_metric_names)) 394 | 395 | early_stopping_callback = EarlyStoppingCallback( 396 | early_stop_strategy, early_stop_metric_names, validation_freq, patience, test_data_loader, 397 | model_save_path=model_save_path 398 | ) 399 | 400 | 401 | model.fit( 402 | train_data=train_data_loader, 403 | epochs=num_epochs, 404 | validation_data=valid_data_loader, 405 | validation_freq=validation_freq, 406 | callbacks=[early_stopping_callback, logging_callback, tensor_board_callback], 407 | ) 408 | 409 | 410 | # For ogbn-mag leaderboard, we also evaluate it via OGB's official evaluator 411 | if running_leaderboard_mag: 412 | from ogb.nodeproppred import Evaluator 413 | evaluator = Evaluator("ogbn-mag") 414 | 415 | print("loading saved model ...") 416 | model.load_state_dict(torch.load(model_save_path)) 417 | model.eval() 418 | 419 | 420 | with torch.no_grad(): 421 | valid_y_pred = model.predict(valid_data_loader).argmax(dim=-1, keepdim=True) 422 | test_y_pred = model.predict(test_data_loader).argmax(dim=-1, keepdim=True) 423 | ogb_valid_acc = evaluator.eval({ 424 | 'y_true': torch_y[valid_index].unsqueeze(-1), 425 | 'y_pred': valid_y_pred 426 | })['acc'] 427 | ogb_test_acc = evaluator.eval({ 428 | 'y_true': torch_y[test_index].unsqueeze(-1), 429 | 'y_pred': test_y_pred 430 | })['acc'] 431 | 432 | print("Results of OGB Evaluator: valid_acc = {}, test_acc = {}".format(ogb_valid_acc, ogb_test_acc)) 433 | 434 | train_and_eval() 435 | 436 | shutil.move(tmp_output_fpath, output_fpath) 437 | print("move tmp file {} => {}".format(tmp_output_fpath, output_fpath)) 438 | 439 | 440 | 441 | -------------------------------------------------------------------------------- /rphgnn/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CrawlScript/RpHGNN/1a1779a747a28ac8d936280a6b96951636183965/rphgnn/__init__.py -------------------------------------------------------------------------------- /rphgnn/callbacks.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import torch 4 | import json 5 | import numpy as np 6 | import time 7 | 8 | 9 | class Callback(object): 10 | def __init__(self) -> None: 11 | self.model = None 12 | 13 | 14 | def on_train_begin(self): 15 | pass 16 | 17 | def on_epoch_end(self, epoch, logs=None): 18 | pass 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | class EarlyStoppingCallback(Callback): 27 | 28 | def __init__(self, strategy, metric_names, validation_freq, patience, test_data, 29 | model_save_path=None, 30 | update_callback=None 31 | # use_lp, label_prop_func 32 | ): 33 | super().__init__() 34 | # self.metric_name = metric_name 35 | 36 | # self.use_lp = use_lp 37 | self.strategy = strategy 38 | self.model_save_path = model_save_path 39 | 40 | # if self.use_lp: 41 | # self.label_prop_func = label_prop_func 42 | 43 | if isinstance(metric_names, str): 44 | metric_names = [metric_names] 45 | self.val_metric_names = ["val_{}".format(metric_name) for metric_name in metric_names] 46 | 47 | self.validation_freq = validation_freq 48 | self.patience = patience 49 | self.patience_counter = 0 50 | 51 | self.min_val_loss = 1000000.0 52 | self.max_val_score = 0.0 53 | 54 | 55 | self.early_stop_logs = None 56 | self.early_stop_epoch = -1 57 | 58 | self.test_data = test_data 59 | 60 | self.update_callback = update_callback 61 | 62 | 63 | 64 | def on_epoch_end(self, epoch, logs): 65 | if "val_loss" not in logs: 66 | return 67 | 68 | 69 | val_loss = logs["val_loss"] 70 | # val_score = logs[self.val_metric_name] 71 | 72 | val_scores = [logs[val_metric_name] for val_metric_name in self.val_metric_names] 73 | val_score = np.mean(val_scores) 74 | 75 | 76 | stop = False 77 | if self.strategy == "common": 78 | reset_patience_counter = val_score > self.max_val_score or val_loss < self.min_val_loss 79 | elif self.strategy == "loss": 80 | reset_patience_counter = val_loss < self.min_val_loss 81 | elif self.strategy == "score": 82 | reset_patience_counter = val_score > self.max_val_score 83 | else: 84 | raise ValueError("Unknown strategy: {}".format(self.strategy)) 85 | 86 | # if val_score > self.max_val_score or val_loss < self.min_val_loss: 87 | if reset_patience_counter: 88 | self.patience_counter = 0 89 | else: 90 | self.patience_counter += self.validation_freq 91 | if self.patience_counter > self.patience: 92 | stop = True 93 | self.model.stop_training = True 94 | 95 | 96 | if not stop: 97 | if self.strategy == "common": 98 | should_update = val_score > self.max_val_score and val_loss < self.min_val_loss 99 | elif self.strategy == "loss": 100 | should_update = val_loss < self.min_val_loss 101 | elif self.strategy == "score": 102 | should_update = val_score > self.max_val_score 103 | else: 104 | raise ValueError("Unknown strategy: {}".format(self.strategy)) 105 | 106 | # if val_score > self.max_val_score and val_loss < self.min_val_loss: 107 | if should_update: 108 | # if True: 109 | self.early_stop_logs = { 110 | "es_{}".format(key): value 111 | for key, value in logs.items() if key.startswith("val_") 112 | } 113 | 114 | 115 | self.max_val_score = val_score 116 | self.min_val_loss = val_loss 117 | self.early_stop_epoch = epoch 118 | if self.test_data is not None: 119 | self.early_stop_logs = { 120 | **self.early_stop_logs, 121 | **self.model.evaluate(self.test_data, log_prefix="es_eval") 122 | } 123 | 124 | if self.update_callback is not None: 125 | self.update_callback(epoch, logs, self.early_stop_logs, self) 126 | 127 | if self.model_save_path is not None: 128 | torch.save(self.model.state_dict(), self.model_save_path) 129 | 130 | 131 | # if self.use_lp: 132 | # label_prop_logs = self.label_prop_func(model, all_data_loader) 133 | # self.early_stop_logs = { 134 | # **self.early_stop_logs, 135 | # **label_prop_logs 136 | # } 137 | 138 | # for key, value in self.early_stop_logs.items(): 139 | # logs[key] = value 140 | 141 | logs["patience"] = self.patience_counter 142 | logs["early_stop_epoch"] = self.early_stop_epoch 143 | 144 | if self.early_stop_logs is not None: 145 | for key, value in self.early_stop_logs.items(): 146 | logs[key] = value 147 | 148 | 149 | 150 | 151 | 152 | 153 | class NumpyFloatValuesEncoder(json.JSONEncoder): 154 | def default(self, obj): 155 | if isinstance(obj, np.float32): 156 | return float(obj) 157 | return json.JSONEncoder.default(self, obj) 158 | 159 | 160 | 161 | class LoggingCallback(Callback): 162 | 163 | def __init__(self, log_path, extra_logs=None): 164 | super().__init__() 165 | 166 | self.log_path = log_path 167 | self.extra_logs = extra_logs if extra_logs is not None else {} 168 | self.start_time = None 169 | 170 | def on_train_begin(self): 171 | if self.start_time is None: 172 | self.start_time = time.time() 173 | 174 | def on_epoch_end(self, epoch, logs=None): 175 | 176 | # if "val_loss" not in logs: 177 | # return 178 | 179 | has_eval = False 180 | for key in logs: 181 | if key.startswith("es_eval_"): 182 | has_eval = True 183 | break 184 | 185 | if not has_eval: 186 | return 187 | 188 | train_time = time.time() - self.start_time 189 | 190 | logs.update({ 191 | **self.extra_logs, 192 | "epoch": epoch, 193 | "train_time": train_time 194 | }) 195 | 196 | # if "early_stop_epoch" in self.extra_logs: 197 | # early_stop_epoch = self.extra_logs[early_stop_epoch] 198 | # if early_stop_epoch == epoch: 199 | # pass 200 | 201 | if "pre_compute_time" in self.extra_logs: 202 | logs["all_time"] = self.extra_logs["pre_compute_time"] + train_time 203 | 204 | with open(self.log_path, "a", encoding="utf-8") as f: 205 | f.write("{}\n".format(json.dumps(logs, cls=NumpyFloatValuesEncoder))) 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | import torchvision.utils as vutils 215 | import torchvision.models as models 216 | from torchvision import datasets 217 | from tensorboardX import SummaryWriter 218 | 219 | 220 | 221 | class TensorBoardCallback(Callback): 222 | 223 | def __init__(self, log_dir='logs'): 224 | super().__init__() 225 | self.log_dir = log_dir 226 | self.writer = None 227 | 228 | def on_train_begin(self): 229 | if self.writer is None: 230 | self.writer = SummaryWriter(self.log_dir, flush_secs=1) 231 | 232 | def on_epoch_end(self, epoch, logs=None): 233 | 234 | for key, value in logs.items(): 235 | if key == "epoch": 236 | continue 237 | self.writer.add_scalar(key, value, epoch) 238 | 239 | 240 | 241 | 242 | -------------------------------------------------------------------------------- /rphgnn/configs/default_param_config.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | def load_default_param_config(dataset): 4 | 5 | use_pretrain_features = False 6 | 7 | random_projection_align = False 8 | input_random_projection_size = None 9 | 10 | merge_mode = "concat" 11 | target_feat_random_project_size = None 12 | add_self_group = False 13 | 14 | if dataset == "mag": 15 | 16 | input_drop_rate = 0.1 17 | drop_rate = 0.4 18 | 19 | hidden_size = 512 20 | 21 | inner_k = 2 22 | squash_k = 3 23 | 24 | 25 | conv_filters = 2 26 | num_layers_list = [2, 0, 2] 27 | 28 | 29 | elif dataset == "oag_venue": 30 | 31 | input_drop_rate = 0.5 32 | drop_rate = 0.5 33 | hidden_size = 512 34 | 35 | inner_k = 2 36 | squash_k = 3 37 | 38 | conv_filters = 2 39 | num_layers_list = [2, 0, 2] 40 | 41 | merge_mode = "mean" 42 | 43 | target_feat_random_project_size = 256 44 | add_self_group = True 45 | 46 | elif dataset == "oag_L1": 47 | 48 | input_drop_rate = 0.5 49 | drop_rate = 0.5 50 | hidden_size = 512 51 | 52 | inner_k = 2 53 | squash_k = 3 54 | 55 | conv_filters = 2 56 | num_layers_list = [2, 0, 2] 57 | 58 | merge_mode = "mean" 59 | target_feat_random_project_size = 256 60 | add_self_group = True 61 | 62 | 63 | elif dataset == "imdb": 64 | 65 | 66 | input_drop_rate = 0.8 67 | drop_rate = 0.8 68 | 69 | hidden_size = 512 70 | 71 | inner_k = 2 72 | squash_k = 4 73 | 74 | conv_filters = 2 75 | num_layers_list = [2, 0, 2] 76 | 77 | elif dataset == "dblp": 78 | 79 | 80 | input_drop_rate = 0.8 81 | drop_rate = 0.7 82 | 83 | input_random_projection_size = None 84 | 85 | 86 | hidden_size = 256 87 | 88 | inner_k = 2 89 | 90 | squash_k = 5 91 | 92 | 93 | conv_filters = 2 94 | num_layers_list = [2, 0, 2] 95 | 96 | 97 | elif dataset == "hgb_acm": 98 | 99 | 100 | 101 | input_drop_rate = 0.7 102 | drop_rate = 0.7 103 | 104 | input_random_projection_size = None 105 | 106 | hidden_size = 64 107 | 108 | 109 | inner_k = 2 110 | 111 | squash_k = 1 112 | 113 | 114 | conv_filters = 2 115 | num_layers_list = [2, 0, 2] 116 | merge_mode = "mean" 117 | 118 | 119 | elif dataset == "freebase": 120 | 121 | 122 | input_drop_rate = 0.7 123 | drop_rate = 0.7 124 | hidden_size = 128 125 | 126 | inner_k = 2 127 | 128 | squash_k = 5 129 | 130 | # k = 3 131 | validation_freq = 10 132 | conv_filters = 2 133 | num_layers_list = [1, 0, 1] 134 | 135 | 136 | return squash_k, inner_k, conv_filters, num_layers_list, hidden_size, merge_mode, input_drop_rate, drop_rate, \ 137 | use_pretrain_features, random_projection_align, input_random_projection_size, target_feat_random_project_size, add_self_group 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /rphgnn/datasets/hgb.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import scipy.sparse as sp 4 | from collections import Counter, defaultdict 5 | from sklearn.metrics import f1_score 6 | import time 7 | 8 | import datetime 9 | import dgl 10 | import errno 11 | import numpy as np 12 | import os 13 | import pickle 14 | import random 15 | import torch 16 | import copy 17 | import torch as th 18 | 19 | from dgl.data.utils import download, get_download_dir, _get_dgl_url 20 | from pprint import pprint 21 | from scipy import sparse 22 | from scipy import io as sio 23 | from sklearn.model_selection import train_test_split 24 | 25 | import sys 26 | 27 | class data_loader: 28 | def __init__(self, path): 29 | self.path = path 30 | self.nodes = self.load_nodes() 31 | self.links = self.load_links() 32 | self.labels_train = self.load_labels('label.dat') 33 | self.labels_test = self.load_labels('label.dat.test') 34 | 35 | # my 36 | 37 | new_data = {} 38 | for link_type, adj in self.links['data'].items(): 39 | # src_type = str(self.links['meta'][link_type][0]) 40 | # dst_type = str(self.links['meta'][link_type][1]) 41 | 42 | adj = adj.tocoo() 43 | 44 | src_type = self.links['meta'][link_type][0] 45 | dst_type = self.links['meta'][link_type][1] 46 | src_shift = self.nodes["shift"][src_type] 47 | dst_shift = self.nodes["shift"][dst_type] 48 | 49 | 50 | row, col = adj.row, adj.col 51 | 52 | # print("link_type: ", link_type, "min_src: ", row.min(), "min_dst: ", col.min(), "src_shift: ", src_shift, "dst_shift: ", dst_shift) 53 | 54 | row -= src_shift 55 | col -= dst_shift 56 | shape = [self.nodes["count"][src_type], self.nodes["count"][dst_type]] 57 | adj = sparse.csr_matrix((adj.data, (row, col)), shape=shape) 58 | new_data[link_type] = adj 59 | 60 | self.links['data'] = new_data 61 | 62 | 63 | 64 | 65 | def get_sub_graph(self, node_types_tokeep): 66 | """ 67 | node_types_tokeep is a list or set of node types that you want to keep in the sub-graph 68 | We only support whole type sub-graph for now. 69 | This is an in-place update function! 70 | return: old node type id to new node type id dict, old edge type id to new edge type id dict 71 | """ 72 | keep = set(node_types_tokeep) 73 | new_node_type = 0 74 | new_node_id = 0 75 | new_nodes = {'total': 0, 'count': Counter(), 'attr': {}, 'shift': {}} 76 | new_links = {'total': 0, 'count': Counter(), 'meta': {}, 'data': defaultdict(list)} 77 | new_labels_train = {'num_classes': 0, 'total': 0, 'count': Counter(), 'data': None, 'mask': None} 78 | new_labels_test = {'num_classes': 0, 'total': 0, 'count': Counter(), 'data': None, 'mask': None} 79 | old_nt2new_nt = {} 80 | old_idx = [] 81 | for node_type in self.nodes['count']: 82 | if node_type in keep: 83 | nt = node_type 84 | nnt = new_node_type 85 | old_nt2new_nt[nt] = nnt 86 | cnt = self.nodes['count'][nt] 87 | new_nodes['total'] += cnt 88 | new_nodes['count'][nnt] = cnt 89 | new_nodes['attr'][nnt] = self.nodes['attr'][nt] 90 | new_nodes['shift'][nnt] = new_node_id 91 | beg = self.nodes['shift'][nt] 92 | old_idx.extend(range(beg, beg + cnt)) 93 | 94 | cnt_label_train = self.labels_train['count'][nt] 95 | new_labels_train['count'][nnt] = cnt_label_train 96 | new_labels_train['total'] += cnt_label_train 97 | cnt_label_test = self.labels_test['count'][nt] 98 | new_labels_test['count'][nnt] = cnt_label_test 99 | new_labels_test['total'] += cnt_label_test 100 | 101 | new_node_type += 1 102 | new_node_id += cnt 103 | 104 | new_labels_train['num_classes'] = self.labels_train['num_classes'] 105 | new_labels_test['num_classes'] = self.labels_test['num_classes'] 106 | for k in ['data', 'mask']: 107 | new_labels_train[k] = self.labels_train[k][old_idx] 108 | new_labels_test[k] = self.labels_test[k][old_idx] 109 | 110 | old_et2new_et = {} 111 | new_edge_type = 0 112 | for edge_type in self.links['count']: 113 | h, t = self.links['meta'][edge_type] 114 | if h in keep and t in keep: 115 | et = edge_type 116 | net = new_edge_type 117 | old_et2new_et[et] = net 118 | new_links['total'] += self.links['count'][et] 119 | new_links['count'][net] = self.links['count'][et] 120 | new_links['meta'][net] = tuple(map(lambda x: old_nt2new_nt[x], self.links['meta'][et])) 121 | new_links['data'][net] = self.links['data'][et][old_idx][:, old_idx] 122 | new_edge_type += 1 123 | 124 | self.nodes = new_nodes 125 | self.links = new_links 126 | self.labels_train = new_labels_train 127 | self.labels_test = new_labels_test 128 | return old_nt2new_nt, old_et2new_et 129 | 130 | def get_meta_path(self, meta=[]): 131 | """ 132 | Get meta path matrix 133 | meta is a list of edge types (also can be denoted by a pair of node types) 134 | return a sparse matrix with shape [node_num, node_num] 135 | """ 136 | ini = sp.eye(self.nodes['total']) 137 | meta = [self.get_edge_type(x) for x in meta] 138 | for x in meta: 139 | ini = ini.dot(self.links['data'][x]) if x >= 0 else ini.dot(self.links['data'][-x - 1].T) 140 | return ini 141 | 142 | def dfs(self, now, meta, meta_dict): 143 | if len(meta) == 0: 144 | meta_dict[now[0]].append(now) 145 | return 146 | th_mat = self.links['data'][meta[0]] if meta[0] >= 0 else self.links['data'][-meta[0] - 1].T 147 | th_node = now[-1] 148 | for col in th_mat[th_node].nonzero()[1]: 149 | self.dfs(now + [col], meta[1:], meta_dict) 150 | 151 | def get_full_meta_path(self, meta=[], symmetric=False): 152 | """ 153 | Get full meta path for each node 154 | meta is a list of edge types (also can be denoted by a pair of node types) 155 | return a dict of list[list] (key is node_id) 156 | """ 157 | meta = [self.get_edge_type(x) for x in meta] 158 | if len(meta) == 1: 159 | meta_dict = {} 160 | start_node_type = self.links['meta'][meta[0]][0] if meta[0] >= 0 else self.links['meta'][-meta[0] - 1][1] 161 | for i in range(self.nodes['shift'][start_node_type], 162 | self.nodes['shift'][start_node_type] + self.nodes['count'][start_node_type]): 163 | meta_dict[i] = [] 164 | self.dfs([i], meta, meta_dict) 165 | else: 166 | meta_dict1 = {} 167 | meta_dict2 = {} 168 | mid = len(meta) // 2 169 | meta1 = meta[:mid] 170 | meta2 = meta[mid:] 171 | start_node_type = self.links['meta'][meta1[0]][0] if meta1[0] >= 0 else self.links['meta'][-meta1[0] - 1][1] 172 | for i in range(self.nodes['shift'][start_node_type], 173 | self.nodes['shift'][start_node_type] + self.nodes['count'][start_node_type]): 174 | meta_dict1[i] = [] 175 | self.dfs([i], meta1, meta_dict1) 176 | start_node_type = self.links['meta'][meta2[0]][0] if meta2[0] >= 0 else self.links['meta'][-meta2[0] - 1][1] 177 | for i in range(self.nodes['shift'][start_node_type], 178 | self.nodes['shift'][start_node_type] + self.nodes['count'][start_node_type]): 179 | meta_dict2[i] = [] 180 | if symmetric: 181 | for k in meta_dict1: 182 | paths = meta_dict1[k] 183 | for x in paths: 184 | meta_dict2[x[-1]].append(list(reversed(x))) 185 | else: 186 | for i in range(self.nodes['shift'][start_node_type], 187 | self.nodes['shift'][start_node_type] + self.nodes['count'][start_node_type]): 188 | self.dfs([i], meta2, meta_dict2) 189 | meta_dict = {} 190 | start_node_type = self.links['meta'][meta1[0]][0] if meta1[0] >= 0 else self.links['meta'][-meta1[0] - 1][1] 191 | for i in range(self.nodes['shift'][start_node_type], 192 | self.nodes['shift'][start_node_type] + self.nodes['count'][start_node_type]): 193 | meta_dict[i] = [] 194 | for beg in meta_dict1[i]: 195 | for end in meta_dict2[beg[-1]]: 196 | meta_dict[i].append(beg + end[1:]) 197 | return meta_dict 198 | 199 | def gen_file_for_evaluate(self, test_idx, label, mode='bi'): 200 | if test_idx.shape[0] != label.shape[0]: 201 | return 202 | if mode == 'multi': 203 | multi_label = [] 204 | for i in range(label.shape[0]): 205 | label_list = [str(j) for j in range(label[i].shape[0]) if label[i][j] == 1] 206 | multi_label.append(','.join(label_list)) 207 | label = multi_label 208 | elif mode == 'bi': 209 | label = np.array(label) 210 | else: 211 | return 212 | dirs = os.path.join(self.path, "preds"); 213 | if not os.path.exists(dirs): 214 | os.makedirs(dirs) 215 | file_name = str(int(time.time())) 216 | with open(os.path.join(dirs, file_name), "w") as f: 217 | for nid, l in zip(test_idx, label): 218 | f.write(f"{nid}\t\t{self.get_node_type(nid)}\t{l}\n") 219 | 220 | def evaluate(self, pred): 221 | y_true = self.labels_test['data'][self.labels_test['mask']] 222 | micro = f1_score(y_true, pred, average='micro') 223 | macro = f1_score(y_true, pred, average='macro') 224 | result = { 225 | 'micro-f1': micro, 226 | 'macro-f1': macro 227 | } 228 | return result 229 | 230 | def load_labels(self, name): 231 | """ 232 | return labels dict 233 | num_classes: total number of labels 234 | total: total number of labeled data 235 | count: number of labeled data for each node type 236 | data: a numpy matrix with shape (self.nodes['total'], self.labels['num_classes']) 237 | mask: to indicate if that node is labeled, if False, that line of data is masked 238 | """ 239 | labels = {'num_classes': 0, 'total': 0, 'count': Counter(), 'data': None, 'mask': None} 240 | nc = 0 241 | mask = np.zeros(self.nodes['total'], dtype=bool) 242 | data = [None for i in range(self.nodes['total'])] 243 | with open(os.path.join(self.path, name), 'r', encoding='utf-8') as f: 244 | for line in f: 245 | th = line.split('\t') 246 | node_id, node_name, node_type, node_label = int(th[0]), th[1], int(th[2]), list( 247 | map(int, th[3].split(','))) 248 | for label in node_label: 249 | nc = max(nc, label + 1) 250 | mask[node_id] = True 251 | data[node_id] = node_label 252 | labels['count'][node_type] += 1 253 | labels['total'] += 1 254 | labels['num_classes'] = nc 255 | new_data = np.zeros((self.nodes['total'], labels['num_classes']), dtype=int) 256 | for i, x in enumerate(data): 257 | if x is not None: 258 | for j in x: 259 | new_data[i, j] = 1 260 | labels['data'] = new_data 261 | labels['mask'] = mask 262 | return labels 263 | 264 | def get_node_type(self, node_id): 265 | for i in range(len(self.nodes['shift'])): 266 | if node_id < self.nodes['shift'][i] + self.nodes['count'][i]: 267 | return i 268 | 269 | def get_edge_type(self, info): 270 | if type(info) is int or len(info) == 1: 271 | return info 272 | for i in range(len(self.links['meta'])): 273 | if self.links['meta'][i] == info: 274 | return i 275 | info = (info[1], info[0]) 276 | for i in range(len(self.links['meta'])): 277 | if self.links['meta'][i] == info: 278 | return -i - 1 279 | raise Exception('No available edge type') 280 | 281 | def get_edge_info(self, edge_id): 282 | return self.links['meta'][edge_id] 283 | 284 | def list_to_sp_mat(self, li): 285 | data = [x[2] for x in li] 286 | i = [x[0] for x in li] 287 | j = [x[1] for x in li] 288 | return sp.coo_matrix((data, (i, j)), shape=(self.nodes['total'], self.nodes['total'])).tocsr() 289 | 290 | def load_links(self): 291 | """ 292 | return links dict 293 | total: total number of links 294 | count: a dict of int, number of links for each type 295 | meta: a dict of tuple, explaining the link type is from what type of node to what type of node 296 | data: a dict of sparse matrices, each link type with one matrix. Shapes are all (nodes['total'], nodes['total']) 297 | """ 298 | links = {'total': 0, 'count': Counter(), 'meta': {}, 'data': defaultdict(list)} 299 | with open(os.path.join(self.path, 'link.dat'), 'r', encoding='utf-8') as f: 300 | for line in f: 301 | th = line.split('\t') 302 | h_id, t_id, r_id, link_weight = int(th[0]), int(th[1]), int(th[2]), float(th[3]) 303 | if r_id not in links['meta']: 304 | h_type = self.get_node_type(h_id) 305 | t_type = self.get_node_type(t_id) 306 | links['meta'][r_id] = (h_type, t_type) 307 | links['data'][r_id].append((h_id, t_id, link_weight)) 308 | links['count'][r_id] += 1 309 | links['total'] += 1 310 | new_data = {} 311 | for r_id in links['data']: 312 | new_data[r_id] = self.list_to_sp_mat(links['data'][r_id]) 313 | links['data'] = new_data 314 | return links 315 | 316 | def load_nodes(self): 317 | """ 318 | return nodes dict 319 | total: total number of nodes 320 | count: a dict of int, number of nodes for each type 321 | attr: a dict of np.array (or None), attribute matrices for each type of nodes 322 | shift: node_id shift for each type. You can get the id range of a type by 323 | [ shift[node_type], shift[node_type]+count[node_type] ) 324 | """ 325 | nodes = {'total': 0, 'count': Counter(), 'attr': {}, 'shift': {}} 326 | with open(os.path.join(self.path, 'node.dat'), 'r', encoding='utf-8') as f: 327 | for line in f: 328 | th = line.split('\t') 329 | if len(th) == 4: 330 | # Then this line of node has attribute 331 | node_id, node_name, node_type, node_attr = th 332 | node_id = int(node_id) 333 | node_type = int(node_type) 334 | node_attr = list(map(float, node_attr.split(','))) 335 | nodes['count'][node_type] += 1 336 | nodes['attr'][node_id] = node_attr 337 | nodes['total'] += 1 338 | elif len(th) == 3: 339 | # Then this line of node doesn't have attribute 340 | node_id, node_name, node_type = th 341 | node_id = int(node_id) 342 | node_type = int(node_type) 343 | nodes['count'][node_type] += 1 344 | nodes['total'] += 1 345 | else: 346 | raise Exception("Too few information to parse!") 347 | shift = 0 348 | attr = {} 349 | for i in range(len(nodes['count'])): 350 | nodes['shift'][i] = shift 351 | if shift in nodes['attr']: 352 | mat = [] 353 | for j in range(shift, shift + nodes['count'][i]): 354 | mat.append(nodes['attr'][j]) 355 | attr[i] = np.array(mat) 356 | else: 357 | attr[i] = None 358 | shift += nodes['count'][i] 359 | nodes['attr'] = attr 360 | return nodes 361 | 362 | def load_imdb(feat_type=0, random_state=None): 363 | prefix = './datasets/IMDB' 364 | dl = data_loader(prefix) 365 | link_type_dic = {0: 'md', 1: 'dm', 2: 'ma', 3: 'am', 4: 'mk', 5: 'km'} 366 | movie_num = dl.nodes['count'][0] 367 | data_dic = {} 368 | for link_type in dl.links['data'].keys(): 369 | src_type = str(dl.links['meta'][link_type][0]) 370 | dst_type = str(dl.links['meta'][link_type][1]) 371 | data_dic[(src_type, link_type_dic[link_type], dst_type)] = dl.links['data'][link_type].nonzero() 372 | hg = dgl.heterograph(data_dic) 373 | 374 | # author feature 375 | if feat_type == 0: 376 | '''preprocessed feature''' 377 | features = th.FloatTensor(dl.nodes['attr'][0]) 378 | else: 379 | '''one-hot''' 380 | # indices = np.vstack((np.arange(author_num), np.arange(author_num))) 381 | # indices = th.LongTensor(indices) 382 | # values = th.FloatTensor(np.ones(author_num)) 383 | # features = th.sparse.FloatTensor(indices, values, th.Size([author_num,author_num])) 384 | features = th.FloatTensor(np.eye(movie_num)) 385 | 386 | # author labels 387 | 388 | labels = dl.labels_test['data'][:movie_num] + dl.labels_train['data'][:movie_num] 389 | labels = th.FloatTensor(labels) 390 | 391 | num_classes = 5 392 | 393 | train_valid_mask = dl.labels_train['mask'][:movie_num] 394 | test_mask = dl.labels_test['mask'][:movie_num] 395 | train_valid_indices = np.where(train_valid_mask == True)[0] 396 | 397 | # split_index = int(0.7 * np.shape(train_valid_indices)[0]) 398 | # train_indices = train_valid_indices[:split_index] 399 | # valid_indices = train_valid_indices[split_index:] 400 | 401 | val_ratio = 0.2 402 | random_index = np.random.permutation(len(train_valid_indices)) 403 | split_index = int((1.0 - val_ratio) * len(train_valid_indices)) 404 | train_indices = np.sort(train_valid_indices[random_index[:split_index]]) 405 | valid_indices = np.sort(train_valid_indices[random_index[split_index:]]) 406 | 407 | # val_ratio = 0.2 408 | # np_labels = labels.detach().cpu().numpy() 409 | # if random_state is not None: 410 | # print("split IMDB with random_state = {}".format(random_state)) 411 | # train_indices, valid_indices = train_test_split(train_valid_indices, test_size=val_ratio, stratify=np_labels[train_valid_indices], random_state=random_state) 412 | # train_indices = np.sort(train_indices) 413 | # valid_indices = np.sort(valid_indices) 414 | 415 | 416 | 417 | 418 | 419 | train_mask = copy.copy(train_valid_mask) 420 | valid_mask = copy.copy(train_valid_mask) 421 | train_mask[valid_indices] = False 422 | valid_mask[train_indices] = False 423 | test_indices = np.where(test_mask == True)[0] 424 | 425 | meta_paths = [['md', 'dm'], ['ma', 'am'], ['mk', 'km']] 426 | # return hg, features, labels, num_classes, train_indices, valid_indices, test_indices, \ 427 | # th.BoolTensor(train_mask), th.BoolTensor(valid_mask), th.BoolTensor(test_mask), meta_paths, dl 428 | 429 | target_node_type = '0' 430 | feature_node_types = [target_node_type] 431 | 432 | 433 | features_dict = dl.nodes["attr"] 434 | return hg, target_node_type, feature_node_types, features, features_dict, labels, num_classes, train_indices, valid_indices, test_indices, \ 435 | train_mask, valid_mask, test_mask 436 | 437 | 438 | def load_freebase(feat_type=1, random_state=None): 439 | dl = data_loader('./datasets/Freebase') 440 | link_type_dic = {0: '00', 1: '01', 2: '03', 3: '05', 4: '06', 441 | 5: '11', 442 | 6: '20', 7: '21', 8: '22', 9: '23', 10: '25', 443 | 11: '31', 12: '33', 13: '35', 444 | 14: '40', 15: '41', 16: '42', 17: '43', 18: '44', 19: '45', 20: '46', 21: '47', 445 | 22: '51', 23: '55', 446 | 24: '61', 25: '62', 26: '63', 27: '65', 28: '66', 29: '67', 447 | 30: '70', 31: '71', 32: '72', 33: '73', 34: '75', 35: '77', 448 | 36: '-00', 37: '10', 38: '30', 39: '50', 40: '60', 449 | 41: '-11', 450 | 42: '02', 43: '12', 44: '-22', 45: '32', 46: '52', 451 | 47: '13', 48: '-33', 49: '53', 452 | 50: '04', 51: '14', 52: '24', 53: '34', 54: '-44', 55: '54', 56: '64', 57: '74', 453 | 58: '15', 59: '-55', 454 | 60: '16', 61: '26', 62: '36', 63: '56', 64: '-66', 65: '76', 455 | 66: '07', 67: '17', 68: '27', 69: '37', 70: '57', 71: '-77', 456 | } 457 | book_num = dl.nodes['count'][0] 458 | data_dic = {} 459 | for link_type in dl.links['data'].keys(): 460 | src_type = str(dl.links['meta'][link_type][0]) 461 | dst_type = str(dl.links['meta'][link_type][1]) 462 | data_dic[(src_type, link_type_dic[link_type], dst_type)] = dl.links['data'][link_type].nonzero() 463 | # reverse 464 | if link_type_dic[link_type + 36][0] != '-': 465 | data_dic[(dst_type, link_type_dic[link_type + 36], src_type)] = dl.links['data'][link_type].T.nonzero() 466 | hg = dgl.heterograph(data_dic) 467 | 468 | if feat_type == 0: 469 | '''preprocessed feature''' 470 | features = th.FloatTensor(dl.nodes['attr'][0]) 471 | else: 472 | '''one-hot''' 473 | indices = np.vstack((np.arange(book_num), np.arange(book_num))) 474 | indices = th.LongTensor(indices) 475 | values = th.FloatTensor(np.ones(book_num)) 476 | features = th.sparse.FloatTensor(indices, values, th.Size([book_num, book_num])) 477 | # author labels 478 | 479 | labels = dl.labels_test['data'][:book_num] + dl.labels_train['data'][:book_num] 480 | labels = [np.argmax(l) for l in labels] # one-hot to value 481 | labels = th.LongTensor(labels) 482 | 483 | num_classes = 7 484 | 485 | train_valid_mask = dl.labels_train['mask'][:book_num] 486 | test_mask = dl.labels_test['mask'][:book_num] 487 | train_valid_indices = np.where(train_valid_mask == True)[0] 488 | 489 | # split_index = int(0.7 * np.shape(train_valid_indices)[0]) 490 | # train_indices = train_valid_indices[:split_index] 491 | # valid_indices = train_valid_indices[split_index:] 492 | 493 | # val_ratio = 0.2 494 | # random_index = np.random.permutation(len(train_valid_indices)) 495 | # split_index = int((1.0 - val_ratio) * len(train_valid_indices)) 496 | # train_indices = np.sort(train_valid_indices[random_index[:split_index]]) 497 | # valid_indices = np.sort(train_valid_indices[random_index[split_index:]]) 498 | 499 | val_ratio = 0.2 500 | np_labels = labels.detach().cpu().numpy() 501 | if random_state is not None: 502 | print("split Freebase with random_state = {}".format(random_state)) 503 | train_indices, valid_indices = train_test_split(train_valid_indices, test_size=val_ratio, stratify=np_labels[train_valid_indices], random_state=random_state) 504 | train_indices = np.sort(train_indices) 505 | valid_indices = np.sort(valid_indices) 506 | 507 | 508 | 509 | 510 | train_mask = copy.copy(train_valid_mask) 511 | valid_mask = copy.copy(train_valid_mask) 512 | train_mask[valid_indices] = False 513 | valid_mask[train_indices] = False 514 | test_indices = np.where(test_mask == True)[0] 515 | 516 | # meta_paths = [['00', '00'], ['01', '10'], ['05', '52', '20'], ['04', '40'], ['04', '43', '30'], ['06', '61', '10'], 517 | # ['07', '70'], ] 518 | # return hg, features, labels, num_classes, train_indices, valid_indices, test_indices, \ 519 | # th.BoolTensor(train_mask), th.BoolTensor(valid_mask), th.BoolTensor(test_mask), meta_paths 520 | target_node_type = '0' 521 | feature_node_types = [] 522 | 523 | features_dict = dl.nodes["attr"] 524 | return hg, target_node_type, feature_node_types, features, features_dict, labels, num_classes, train_indices, valid_indices, test_indices, \ 525 | train_mask, valid_mask, test_mask 526 | 527 | 528 | 529 | 530 | def load_dblp(feat_type=0, random_state=None): 531 | prefix = './datasets/DBLP' 532 | dl = data_loader(prefix) 533 | link_type_dic = {0: 'ap', 1: 'pc', 2: 'pt', 3: 'pa', 4: 'cp', 5: 'tp'} 534 | author_num = dl.nodes['count'][0] 535 | data_dic = {} 536 | for link_type in dl.links['data'].keys(): 537 | src_type = str(dl.links['meta'][link_type][0]) 538 | dst_type = str(dl.links['meta'][link_type][1]) 539 | data_dic[(src_type, link_type_dic[link_type], dst_type)] = dl.links['data'][link_type].nonzero() 540 | hg = dgl.heterograph(data_dic) 541 | 542 | # author feature 543 | if feat_type == 0: 544 | '''preprocessed feature''' 545 | features = th.FloatTensor(dl.nodes['attr'][0]) 546 | else: 547 | '''one-hot''' 548 | # indices = np.vstack((np.arange(author_num), np.arange(author_num))) 549 | # indices = th.LongTensor(indices) 550 | # values = th.FloatTensor(np.ones(author_num)) 551 | # features = th.sparse.FloatTensor(indices, values, th.Size([author_num,author_num])) 552 | features = th.FloatTensor(np.eye(author_num)) 553 | 554 | 555 | 556 | # author labels 557 | 558 | labels = dl.labels_test['data'][:author_num] + dl.labels_train['data'][:author_num] 559 | labels = [np.argmax(l) for l in labels] # one-hot to value 560 | labels = th.LongTensor(labels) 561 | 562 | num_classes = 4 563 | 564 | train_valid_mask = dl.labels_train['mask'][:author_num] 565 | test_mask = dl.labels_test['mask'][:author_num] 566 | train_valid_indices = np.where(train_valid_mask == True)[0] 567 | 568 | # split_index = int(0.7 * np.shape(train_valid_indices)[0]) 569 | # train_indices = train_valid_indices[:split_index] 570 | # valid_indices = train_valid_indices[split_index:] 571 | 572 | 573 | # raw version 574 | # val_ratio = 0.2 575 | # random_index = np.random.permutation(len(train_valid_indices)) 576 | # split_index = int((1.0 - val_ratio) * len(train_valid_indices)) 577 | # train_indices = np.sort(train_valid_indices[random_index[:split_index]]) 578 | # valid_indices = np.sort(train_valid_indices[random_index[split_index:]]) 579 | 580 | 581 | val_ratio = 0.2 582 | np_labels = labels.detach().cpu().numpy() 583 | if random_state is not None: 584 | print("split DBLP with random_state = {}".format(random_state)) 585 | train_indices, valid_indices = train_test_split(train_valid_indices, test_size=val_ratio, stratify=np_labels[train_valid_indices], random_state=random_state) 586 | train_indices = np.sort(train_indices) 587 | valid_indices = np.sort(valid_indices) 588 | 589 | 590 | train_mask = copy.copy(train_valid_mask) 591 | valid_mask = copy.copy(train_valid_mask) 592 | train_mask[valid_indices] = False 593 | valid_mask[train_indices] = False 594 | test_indices = np.where(test_mask == True)[0] 595 | 596 | # meta_paths = [['ap', 'pa'], ['ap', 'pt', 'tp', 'pa'], ['ap', 'pc', 'cp', 'pa']] 597 | # return hg, features, labels, num_classes, train_indices, valid_indices, test_indices, \ 598 | # th.BoolTensor(train_mask), th.BoolTensor(valid_mask), th.BoolTensor(test_mask), meta_paths 599 | target_node_type = '0' 600 | feature_node_types = [target_node_type] 601 | # feature_node_types = [] 602 | features_dict = dl.nodes["attr"] 603 | return hg, target_node_type, feature_node_types, features, features_dict, labels, num_classes, train_indices, valid_indices, test_indices, \ 604 | train_mask, valid_mask, test_mask 605 | 606 | 607 | 608 | def load_hgb_acm(feat_type=0, random_state=None): 609 | dl = data_loader('./datasets/ACM') 610 | link_type_dic = {0: 'pp', 1: '-pp', 2: 'pa', 3: 'ap', 4: 'ps', 5: 'sp', 6: 'pt', 7: 'tp'} 611 | paper_num = dl.nodes['count'][0] 612 | data_dic = {} 613 | for link_type in dl.links['data'].keys(): 614 | src_type = str(dl.links['meta'][link_type][0]) 615 | dst_type = str(dl.links['meta'][link_type][1]) 616 | data_dic[(src_type, link_type_dic[link_type], dst_type)] = dl.links['data'][link_type].nonzero() 617 | hg = dgl.heterograph(data_dic) 618 | 619 | # paper feature 620 | if feat_type == 0: 621 | '''preprocessed feature''' 622 | features = th.FloatTensor(dl.nodes['attr'][0]) 623 | else: 624 | '''one-hot''' 625 | features = th.FloatTensor(np.eye(paper_num)) 626 | 627 | # author labels 628 | 629 | labels = dl.labels_test['data'][:paper_num] + dl.labels_train['data'][:paper_num] 630 | labels = [np.argmax(l) for l in labels] # one-hot to value 631 | labels = th.LongTensor(labels) 632 | 633 | num_classes = 3 634 | 635 | train_valid_mask = dl.labels_train['mask'][:paper_num] 636 | test_mask = dl.labels_test['mask'][:paper_num] 637 | train_valid_indices = np.where(train_valid_mask == True)[0] 638 | 639 | # split_index = int(0.7 * np.shape(train_valid_indices)[0]) 640 | # train_indices = train_valid_indices[:split_index] 641 | # valid_indices = train_valid_indices[split_index:] 642 | 643 | # val_ratio = 0.2 644 | # random_index = np.random.permutation(len(train_valid_indices)) 645 | # split_index = int((1.0 - val_ratio) * len(train_valid_indices)) 646 | # train_indices = np.sort(train_valid_indices[random_index[:split_index]]) 647 | # valid_indices = np.sort(train_valid_indices[random_index[split_index:]]) 648 | 649 | 650 | val_ratio = 0.2 651 | np_labels = labels.detach().cpu().numpy() 652 | if random_state is not None: 653 | print("split HGB_ACM with random_state = {}".format(random_state)) 654 | train_indices, valid_indices = train_test_split(train_valid_indices, test_size=val_ratio, stratify=np_labels[train_valid_indices], random_state=random_state) 655 | train_indices = np.sort(train_indices) 656 | valid_indices = np.sort(valid_indices) 657 | 658 | 659 | train_mask = copy.copy(train_valid_mask) 660 | valid_mask = copy.copy(train_valid_mask) 661 | train_mask[valid_indices] = False 662 | valid_mask[train_indices] = False 663 | test_indices = np.where(test_mask == True)[0] 664 | 665 | meta_paths = [['pp', 'ps', 'sp'], ['-pp', 'ps', 'sp'], ['pa', 'ap'], ['ps', 'sp'], ['pt', 'tp']] 666 | # return hg, features, labels, num_classes, train_indices, valid_indices, test_indices, \ 667 | # th.BoolTensor(train_mask), th.BoolTensor(valid_mask), th.BoolTensor(test_mask), meta_paths 668 | 669 | target_node_type = '0' 670 | feature_node_types = [target_node_type] 671 | 672 | features_dict = dl.nodes["attr"] 673 | 674 | return hg, target_node_type, feature_node_types, features, features_dict, labels, num_classes, train_indices, valid_indices, test_indices, \ 675 | train_mask, valid_mask, test_mask -------------------------------------------------------------------------------- /rphgnn/datasets/load_data.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import torch 4 | import os 5 | import numpy as np 6 | 7 | from rphgnn.utils.graph_utils import add_random_feats, dgl_add_all_reversed_edges, dgl_remove_edges 8 | from .hgb import load_imdb, load_freebase, load_dblp, load_hgb_acm 9 | from tqdm import tqdm 10 | import pickle 11 | from gensim.models import Word2Vec 12 | import time 13 | import dgl 14 | from ogb.nodeproppred import DglNodePropPredDataset 15 | 16 | 17 | 18 | def load_mag(device): 19 | 20 | # path = args.use_emb 21 | home_dir = os.getenv("HOME") 22 | dataset = DglNodePropPredDataset( 23 | name="ogbn-mag", root=os.path.join(home_dir, ".ogb", "dataset")) 24 | g, labels = dataset[0] 25 | 26 | # my 27 | g = g.to(device) 28 | 29 | splitted_idx = dataset.get_idx_split() 30 | train_nid = splitted_idx["train"]['paper'] 31 | val_nid = splitted_idx["valid"]['paper'] 32 | test_nid = splitted_idx["test"]['paper'] 33 | features = g.nodes['paper'].data['feat'] 34 | g.nodes["paper"].data["feat"] = features.to(device) 35 | 36 | 37 | labels = labels['paper'].to(device).squeeze() 38 | n_classes = int(labels.max() - labels.min()) + 1 39 | train_nid, val_nid, test_nid = np.array(train_nid), np.array(val_nid), np.array(test_nid) 40 | 41 | 42 | target_node_type = "paper" 43 | feature_node_types = [target_node_type] 44 | 45 | return g, target_node_type, feature_node_types, labels, n_classes, train_nid, val_nid, test_nid 46 | 47 | def load_dgl_mag(embedding_size): 48 | device = "cpu" 49 | 50 | g, target_node_type, feature_node_types, labels, n_classes, train_index, valid_index, test_index = load_mag(device) 51 | 52 | g.nodes[target_node_type].data["label"] = labels 53 | 54 | 55 | # embedding_size = g.ndata["feat"][target_node_type].size(-1) * 4 56 | g = add_random_feats(g, embedding_size, excluded_ntypes=feature_node_types) 57 | 58 | return g, target_node_type, feature_node_types, (train_index, valid_index, test_index) 59 | 60 | def load_dgl_hgb(dataset, use_all_feat=False, embedding_size=None, random_state=None): 61 | 62 | if dataset == "imdb": 63 | load_func = load_imdb 64 | elif dataset == "dblp": 65 | load_func = load_dblp 66 | elif dataset == "hgb_acm": 67 | load_func = load_hgb_acm 68 | elif dataset == "freebase": 69 | load_func = load_freebase 70 | else: 71 | raise RuntimeError(f"Unsupported dataset {dataset}") 72 | 73 | # dgl_graph, target_node_type, feature_node_types, features, features_dict, labels, num_classes, train_indices, valid_indices, test_indices, train_mask, valid_mask, test_mask = load_func(random_state=random_state) 74 | 75 | dgl_graph, target_node_type, feature_node_types, features, features_dict, labels, _, train_indices, valid_indices, test_indices, _, _, _ = load_func(random_state=random_state) 76 | 77 | 78 | if use_all_feat: 79 | print("use all features ...") 80 | for int_ntype, value in features_dict.items(): 81 | ntype = str(int_ntype) 82 | if value is None: 83 | print("skip None ntype: ", ntype) 84 | else: 85 | 86 | print("set feature for ntype: ", ntype, dgl_graph.num_nodes(ntype), value.shape) 87 | dgl_graph.nodes[ntype].data["feat"] = torch.tensor(value).to(torch.float32) 88 | 89 | if embedding_size is None: 90 | embedding_size = features.size(-1) 91 | 92 | dgl_graph = add_random_feats(dgl_graph, embedding_size, 93 | excluded_ntypes=[ntype for ntype in dgl_graph.ntypes if "feat" in dgl_graph.nodes[ntype].data] 94 | ) 95 | 96 | else: 97 | if len(feature_node_types) == 0: 98 | dgl_graph = add_random_feats(dgl_graph, embedding_size, excluded_ntypes=None) 99 | else: 100 | dgl_graph.nodes[target_node_type].data["feat"] = features 101 | if embedding_size is None: 102 | embedding_size = features.size(-1) 103 | 104 | dgl_graph = add_random_feats(dgl_graph, embedding_size, 105 | excluded_ntypes=[ntype for ntype in dgl_graph.ntypes if "feat" in dgl_graph.nodes[ntype].data] 106 | ) 107 | 108 | dgl_graph.nodes[target_node_type].data["label"] = labels 109 | 110 | return dgl_graph, target_node_type, feature_node_types, (train_indices, valid_indices, test_indices) 111 | 112 | def load_dgl_hgb_acm(use_all_feat=False, embedding_size=None, random_state=None): 113 | return load_dgl_hgb("hgb_acm", use_all_feat=use_all_feat, embedding_size=embedding_size, random_state=random_state) 114 | 115 | def load_dgl_imdb(use_all_feat=False, embedding_size=None, random_state=None): 116 | return load_dgl_hgb("imdb", use_all_feat=use_all_feat, embedding_size=embedding_size, random_state=random_state) 117 | 118 | def load_dgl_dblp(use_all_feat=False, embedding_size=None, random_state=None): 119 | return load_dgl_hgb("dblp", use_all_feat=use_all_feat, embedding_size=embedding_size, random_state=random_state) 120 | 121 | def load_dgl_freebase(use_all_feat=False, embedding_size=None, random_state=None): 122 | return load_dgl_hgb("freebase", use_all_feat=use_all_feat, embedding_size=embedding_size, random_state=random_state) 123 | 124 | def load_oag(device, dataset, data_path="datasets/nars_academic_oag"): 125 | import pickle 126 | # assert args.data_dir is not None 127 | 128 | 129 | if dataset == "oag_L1": 130 | graph_file = "graph_L1.pk" 131 | predict_venue = False 132 | elif dataset == "oag_venue": 133 | graph_file = "graph_venue.pk" 134 | predict_venue = True 135 | else: 136 | raise RuntimeError(f"Unsupported dataset {dataset}") 137 | with open(os.path.join(data_path, graph_file), "rb") as f: 138 | dataset = pickle.load(f) 139 | n_classes = dataset["n_classes"] 140 | graph = dgl.heterograph(dataset["edges"]) 141 | graph = graph.to(device) 142 | train_nid, val_nid, test_nid = dataset["split"] 143 | 144 | 145 | with open(os.path.join(data_path, "paper.npy"), "rb") as f: 146 | # loading lang features of paper provided by HGT author 147 | paper_feat = torch.from_numpy(np.load(f)).float().to(device) 148 | graph.nodes["paper"].data["feat"] = paper_feat[:graph.number_of_nodes("paper")] 149 | 150 | if predict_venue: 151 | labels = torch.from_numpy(dataset["labels"]) 152 | else: 153 | labels = torch.zeros(graph.number_of_nodes("paper"), n_classes) 154 | for key in dataset["labels"]: 155 | labels[key, dataset["labels"][key]] = 1 156 | train_nid, val_nid, test_nid = np.array(train_nid), np.array(val_nid), np.array(test_nid) 157 | 158 | # return graph, labels, n_classes, train_nid, val_nid, test_nid 159 | 160 | target_node_type = "paper" 161 | feature_node_types = [target_node_type] 162 | 163 | return graph, target_node_type, feature_node_types, labels, n_classes, train_nid, val_nid, test_nid 164 | 165 | def load_dgl_oag(dataset, data_path="datasets/nars_academic_oag", embedding_size=None): 166 | g, target_node_type, feature_node_types, labels, n_classes, train_index, valid_index, test_index = load_oag(device="cpu", dataset=dataset, data_path=data_path) 167 | 168 | target_node_type = "paper" 169 | 170 | g = add_random_feats(g, embedding_size, excluded_ntypes=[target_node_type]) 171 | 172 | g.nodes[target_node_type].data["label"] = labels 173 | 174 | 175 | return g, target_node_type, feature_node_types, (train_index, valid_index, test_index) 176 | # return dgl_graph, target_node_type, (train_index, valid_index, test_index) 177 | 178 | def nrl_update_features(dataset, hetero_graph, excluded_ntypes, 179 | nrl_pretrain_epochs=40, embedding_size=512): 180 | 181 | start_time = time.time() 182 | nrl_cache_path = os.path.join("./cache/{}.p".format(dataset)) 183 | 184 | if os.path.exists(nrl_cache_path): 185 | print("loading cache: {}".format(nrl_cache_path)) 186 | with open(nrl_cache_path, "rb") as f: 187 | nrl_embedding_dict = pickle.load(f) 188 | else: 189 | 190 | vocab_corpus = [] 191 | for ntype in hetero_graph.ntypes: 192 | for i in tqdm(range(hetero_graph.num_nodes(ntype))): 193 | vocab_corpus.append(["{}_{}".format(ntype, i)]) 194 | 195 | 196 | corpus = [] 197 | for etype in hetero_graph.canonical_etypes: 198 | if etype[1].startswith("r."): 199 | print("skip etype: ", etype) 200 | continue 201 | row, col = hetero_graph.edges(etype=etype) 202 | for i, j in tqdm(zip(row, col)): 203 | corpus.append(["{}_{}".format(etype[0], i), "{}_{}".format(etype[2], j)]) 204 | 205 | print("start training word2vec") 206 | # word2vec_model = Word2Vec(sentences=vocab_corpus, vector_size=embedding_size, window=2, min_count=0, workers=4) 207 | word2vec_model = Word2Vec(sentences=vocab_corpus, vector_size=embedding_size, window=2, min_count=0, workers=4) 208 | for i in tqdm(range(nrl_pretrain_epochs)): 209 | print("train word2vec epoch {}".format(i)) 210 | word2vec_model.train(corpus, total_examples=len(corpus), epochs=1) 211 | 212 | # word2vec_model = Word2Vec(sentences=vocab_corpus, vector_size=embedding_size, window=2, min_count=0, workers=4, negative=20) 213 | 214 | # print("train word2vec ...") 215 | # word2vec_model.train(corpus, total_examples=len(corpus), epochs=nrl_pretrain_epochs) 216 | 217 | nrl_embedding_dict = {} 218 | for ntype in hetero_graph.ntypes: 219 | embeddings = np.array([word2vec_model.wv["{}_{}".format(ntype, i)] for i in range(hetero_graph.num_nodes(ntype))]) 220 | nrl_embedding_dict[ntype] = embeddings 221 | 222 | print("saving cache: {}".format(nrl_cache_path)) 223 | with open(nrl_cache_path, "wb") as f: 224 | pickle.dump(nrl_embedding_dict, f, protocol=4) 225 | 226 | 227 | 228 | print("nrl time: ", time.time() - start_time) 229 | 230 | 231 | for ntype in list(hetero_graph.ntypes): 232 | if ntype not in excluded_ntypes: 233 | print("using NRL embeddings for featureless nodetype: {}".format(ntype)) 234 | # hetero_graph.x_dict[node_type] = nrl_embedding_dict[node_type] 235 | hetero_graph.nodes[ntype].data["feat"] = torch.tensor(nrl_embedding_dict[ntype]) 236 | 237 | return hetero_graph 238 | 239 | def load_dgl_data(dataset, use_all_feat=False, embedding_size=None, use_nrl=False, random_state=None): 240 | 241 | 242 | batch_size = 10000 243 | num_epochs = 510 244 | patience = 30 245 | validation_freq = 10 246 | convert_to_tensor = True 247 | 248 | if dataset == "mag": 249 | hetero_graph, target_node_type, feature_node_types, (train_index, valid_index, test_index) = load_dgl_mag(embedding_size=embedding_size) 250 | 251 | convert_to_tensor = False 252 | num_epochs = 100 253 | patience = 10 254 | 255 | elif dataset in ["oag_L1", "oag_venue"]: 256 | 257 | batch_size = 3000 258 | if embedding_size is None: 259 | embedding_size = 128 * 2 260 | 261 | hetero_graph, target_node_type, feature_node_types, (train_index, valid_index, test_index) = load_dgl_oag(dataset, embedding_size=embedding_size) 262 | 263 | convert_to_tensor = False 264 | 265 | num_epochs = 200 266 | patience = 10 267 | 268 | 269 | 270 | elif dataset == "imdb": 271 | 272 | if embedding_size is None: 273 | embedding_size = 1024 274 | 275 | hetero_graph, target_node_type, feature_node_types, (train_index, valid_index, test_index) = load_dgl_imdb(use_all_feat=use_all_feat, 276 | embedding_size=embedding_size, random_state=random_state) 277 | 278 | etypes_to_remove = set() 279 | for etype in hetero_graph.canonical_etypes: 280 | etype_ = etype[1] 281 | items = list(etype_) 282 | print("items: ", items) 283 | if items[0] > items[1]: 284 | etypes_to_remove.add(etype) 285 | print("remove items: ", items) 286 | 287 | print("etypes_to_remove: ", etypes_to_remove) 288 | 289 | hetero_graph = dgl_remove_edges(hetero_graph, etypes_to_remove) 290 | print("remaining etypes: ", hetero_graph.canonical_etypes) 291 | 292 | num_epochs = 500 293 | patience = 200 294 | 295 | validation_freq = 1 296 | 297 | elif dataset == "dblp": 298 | 299 | if embedding_size is None: 300 | embedding_size = 1024 301 | hetero_graph, target_node_type, feature_node_types, (train_index, valid_index, test_index) = load_dgl_dblp(use_all_feat=use_all_feat, embedding_size=embedding_size, random_state=random_state) 302 | # hetero_graph, target_node_type, feature_node_types, (train_index, valid_index, test_index) = load_dgl_dblp(embedding_size=256) 303 | 304 | print("raw etypes: ", hetero_graph.canonical_etypes) 305 | etypes_to_remove = set() 306 | for etype in hetero_graph.canonical_etypes: 307 | etype_ = etype[1] 308 | items = list(etype_) 309 | print("items: ", items) 310 | if items[0] > items[1]: 311 | etypes_to_remove.add(etype) 312 | print("remove items: ", items) 313 | 314 | print("etypes_to_remove: ", etypes_to_remove) 315 | 316 | hetero_graph = dgl_remove_edges(hetero_graph, etypes_to_remove) 317 | 318 | print("remaining etypes: ", hetero_graph.canonical_etypes) 319 | 320 | # hetero_graph = dgl_add_duplicated_edges(hetero_graph, 3) 321 | # print("edges update duplication: ", hetero_graph.canonical_etypes) 322 | 323 | num_epochs = 500 324 | patience = 30 325 | # validation_freq = 1 326 | 327 | 328 | # hetero_graph = hetero_graph.add_reversed_edges(inplace=True) 329 | 330 | elif dataset == "hgb_acm": 331 | 332 | if embedding_size is None: 333 | embedding_size = 512 334 | 335 | hetero_graph, target_node_type, feature_node_types, (train_index, valid_index, test_index) = load_dgl_hgb_acm(use_all_feat=use_all_feat, embedding_size=embedding_size, random_state=random_state) 336 | # hetero_graph = hetero_graph.add_reversed_edges(inplace=True) 337 | 338 | num_epochs = 100 339 | patience = 20 340 | 341 | validation_freq = 1 342 | batch_size = 1000 343 | 344 | # for etype in hetero_graph.etypes: 345 | # print(etype) 346 | etypes_to_remove = set() 347 | for etype in hetero_graph.canonical_etypes: 348 | etype_ = etype[1] 349 | items = list(etype_) 350 | print("items: ", items) 351 | if etype_[0] == "-" or items[0] > items[1]: 352 | etypes_to_remove.add(etype) 353 | print("remove items: ", items) 354 | 355 | print("etypes_to_remove: ", etypes_to_remove) 356 | 357 | hetero_graph = dgl_remove_edges(hetero_graph, etypes_to_remove) 358 | print("remaining etypes: ", hetero_graph.canonical_etypes) 359 | 360 | 361 | elif dataset == "freebase": 362 | num_epochs = 200 363 | patience = 20 364 | # validation_freq = 1 365 | # hetero_graph, target_node_type, (train_index, valid_index, test_index) = load_dgl_freebase(embedding_size=128) 366 | if embedding_size is None: 367 | embedding_size = 512 368 | hetero_graph, target_node_type, feature_node_types, (train_index, valid_index, test_index) = load_dgl_freebase(use_all_feat=use_all_feat, embedding_size=embedding_size, random_state=random_state) 369 | 370 | 371 | 372 | etypes_to_remove = set() 373 | for etype in hetero_graph.canonical_etypes: 374 | etype_ = etype[1] 375 | items = [int(c) for c in list(etype_)] 376 | print("items: ", items) 377 | if items[0] > items[1]: 378 | etypes_to_remove.add(etype) 379 | print("remove items: ", items) 380 | 381 | print("etypes_to_remove: ", etypes_to_remove) 382 | 383 | hetero_graph = dgl_remove_edges(hetero_graph, etypes_to_remove) 384 | 385 | print("etypes: ", hetero_graph.canonical_etypes) 386 | 387 | 388 | 389 | # hetero_graph = dgl_add_label_nodes(hetero_graph, target_node_type, train_index) 390 | hetero_graph = dgl.add_reverse_edges(hetero_graph, ignore_bipartite=True) 391 | hetero_graph = dgl_add_all_reversed_edges(hetero_graph) 392 | 393 | 394 | 395 | 396 | if use_nrl: 397 | 398 | if dataset == "freebase": 399 | excluded_ntypes = [] 400 | else: 401 | excluded_ntypes = [target_node_type] 402 | 403 | hetero_graph = nrl_update_features(dataset, hetero_graph, excluded_ntypes) 404 | 405 | 406 | return hetero_graph, target_node_type, feature_node_types, (train_index, valid_index, test_index), \ 407 | batch_size, num_epochs, patience, validation_freq, convert_to_tensor 408 | -------------------------------------------------------------------------------- /rphgnn/global_configuration.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch 3 | 4 | class GlobalConfig(object): 5 | def __init__(self) -> None: 6 | 7 | self.embedding_generator = None 8 | self.rand_proj_generator = None 9 | 10 | self.torch_random_project = None 11 | self.torch_random_project_create_kernel = None 12 | 13 | 14 | 15 | global_config = GlobalConfig() -------------------------------------------------------------------------------- /rphgnn/layers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CrawlScript/RpHGNN/1a1779a747a28ac8d936280a6b96951636183965/rphgnn/layers/__init__.py -------------------------------------------------------------------------------- /rphgnn/layers/rphgnn_encoder.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import torch 4 | import torch.nn as nn 5 | from itertools import chain 6 | import torch.nn.functional as F 7 | 8 | from rphgnn.layers.torch_train_model import CommonTorchTrainModel 9 | 10 | 11 | 12 | class PReLU(nn.Module): 13 | 14 | __constants__ = ['num_parameters'] 15 | num_parameters: int 16 | 17 | def __init__(self, num_parameters: int = 1, init: float = 0.25, 18 | device=None, dtype=None) -> None: 19 | factory_kwargs = {'device': device, 'dtype': dtype} 20 | self.num_parameters = num_parameters 21 | super().__init__() 22 | 23 | # use alpha instead of weight 24 | self.alpha = nn.parameter.Parameter(torch.empty(num_parameters, **factory_kwargs).fill_(init)) 25 | 26 | def forward(self, input: torch.Tensor) -> torch.Tensor: 27 | return F.prelu(input, self.alpha) 28 | 29 | def extra_repr(self) -> str: 30 | return 'num_parameters={}'.format(self.num_parameters) 31 | 32 | 33 | class Lambda(nn.Module): 34 | def __init__(self, func) -> None: 35 | super().__init__() 36 | 37 | self.func = func 38 | 39 | def forward(self, x): 40 | return self.func(x) 41 | 42 | def create_act(name=None): 43 | 44 | if name is None: 45 | return nn.Identity() 46 | elif name == "relu": 47 | return nn.ReLU() 48 | elif name == "prelu": 49 | return PReLU() 50 | elif name == "softmax": 51 | return nn.Softmax(dim=-1) 52 | elif name == "sigmoid": 53 | return nn.Sigmoid() 54 | elif name == "identity": 55 | return Lambda(lambda x: x) 56 | else: 57 | raise Exception() 58 | 59 | 60 | class Linear(nn.Linear): 61 | def reset_parameters(self) -> None: 62 | nn.init.xavier_normal_(self.weight) 63 | nn.init.zeros_(self.bias) 64 | 65 | 66 | class Conv1d(nn.Conv1d): 67 | def reset_parameters(self) -> None: 68 | nn.init.xavier_normal_(self.weight) 69 | nn.init.zeros_(self.bias) 70 | 71 | 72 | class MLPConv1d(nn.Module): 73 | """ 74 | another implementation of Conv1d 75 | """ 76 | def __init__(self, in_channels, out_channels): 77 | super().__init__() 78 | self.linear = Linear(in_channels, out_channels) 79 | 80 | def forward(self, x): 81 | h = torch.permute(x, (0, 2, 1)) 82 | h = self.linear(h) 83 | h = torch.permute(h, (0, 2, 1)) 84 | h = h.contiguous() 85 | return h 86 | 87 | 88 | class MLP(nn.Module): 89 | 90 | def __init__(self, 91 | channels_list, 92 | input_shape, 93 | drop_rate=0.0, 94 | activation=None, 95 | output_drop_rate=0.0, 96 | output_activation=None, 97 | kernel_regularizer=None, 98 | *args, **kwargs): 99 | 100 | super().__init__(*args, **kwargs) 101 | 102 | self.kernel_regularizer = kernel_regularizer 103 | 104 | in_channels = input_shape[-1] 105 | channels_list = [in_channels] + channels_list 106 | 107 | layers = [] 108 | for i in range(len(channels_list) - 1): 109 | layers.append(Linear(channels_list[i], channels_list[i + 1])) 110 | if i < len(channels_list) - 2: 111 | layers.append(create_act(activation)) 112 | layers.append(nn.Dropout(drop_rate)) 113 | else: 114 | layers.append(create_act(output_activation)) 115 | layers.append(nn.Dropout(output_drop_rate)) 116 | 117 | 118 | self.layers = nn.Sequential(*layers) 119 | 120 | 121 | 122 | def forward(self, x): 123 | return self.layers(x) 124 | 125 | 126 | 127 | class GroupEncoders(nn.Module): 128 | 129 | def __init__(self, 130 | filters_list, 131 | drop_rate, 132 | input_shape, 133 | kernel_regularizer=None, 134 | *args, **kwargs): 135 | super().__init__(*args, **kwargs) 136 | self.hop_encoders = None 137 | self.filters_list = filters_list 138 | self.drop_rate = drop_rate 139 | self.kernel_regularizer = kernel_regularizer 140 | self.real_filters_list = None 141 | 142 | num_groups = len(input_shape) 143 | self.group_sizes = [group_shape[1] for group_shape in input_shape] 144 | self.real_filters_list = [self._get_real_filters(i) for i in range(num_groups)] 145 | 146 | self.group_encoders = nn.ModuleList([ 147 | nn.Sequential( 148 | Conv1d(group_size, real_filters, 1, stride=1), 149 | # # if too slow, comment MyConv1d (above) and uncomment MyMLPConv1d (below) 150 | # MLPConv1d(group_size, real_filters), 151 | Lambda(lambda x: x.view(x.size(0), -1)) 152 | ) 153 | for _, (group_size, real_filters) in enumerate(zip(self.group_sizes, self.real_filters_list)) 154 | ]) 155 | 156 | 157 | def _get_real_filters(self, i): 158 | 159 | if self.group_sizes[i] == 1: 160 | return 1 161 | elif isinstance(self.filters_list, list): 162 | return self.filters_list[i] 163 | else: 164 | return self.filters_list 165 | 166 | 167 | def forward(self, x_group_list): 168 | group_h_list = [] 169 | 170 | for i, (x_group, group_encoder) in enumerate(zip(x_group_list, self.group_encoders)): 171 | 172 | h = x_group 173 | group_h = group_encoder(h) 174 | group_h_list.append(group_h) 175 | 176 | return group_h_list 177 | 178 | 179 | 180 | class MultiGroupFusion(nn.Module): 181 | 182 | def __init__(self, 183 | group_channels_list, 184 | global_channels_list, 185 | merge_mode, 186 | input_shape, 187 | drop_rate=0.0, 188 | activation="prelu", 189 | output_activation=None, 190 | *args, **kwargs): 191 | super().__init__(*args, **kwargs) 192 | 193 | self.group_fc_list = None 194 | self.global_fc = None 195 | 196 | self.group_channels_list = group_channels_list 197 | self.global_channels_list = global_channels_list 198 | self.merge_mode = merge_mode 199 | self.drop_rate = drop_rate 200 | 201 | self.use_shared_group_fc = False 202 | self.group_encoder_mode = "common" 203 | 204 | num_groups = len(input_shape) 205 | self.num_groups = num_groups 206 | 207 | self.group_fc_list = nn.ModuleList([ 208 | MLP( 209 | group_channels_list, 210 | input_shape=group_input_shape, 211 | drop_rate=drop_rate, 212 | activation=activation, 213 | output_drop_rate=drop_rate, 214 | output_activation=activation 215 | ) 216 | for group_input_shape in input_shape 217 | ]) 218 | 219 | if merge_mode in ["mean", "free"]: 220 | global_input_shape = [-1, group_channels_list[-1]] 221 | elif merge_mode == "concat": 222 | global_input_shape = [-1, group_channels_list[-1] * num_groups] 223 | else: 224 | raise Exception("wrong merge mode: ", merge_mode) 225 | 226 | self.global_fc = MLP(self.global_channels_list, 227 | input_shape=global_input_shape, 228 | drop_rate=self.drop_rate, 229 | activation=activation, 230 | output_drop_rate=0.0, 231 | output_activation=output_activation) 232 | 233 | 234 | def forward(self, inputs): 235 | 236 | x_list = inputs 237 | group_h_list = [group_fc(x) for x, group_fc in zip(x_list, self.group_fc_list)] 238 | 239 | if self.merge_mode == "mean": 240 | global_h = torch.stack(group_h_list, dim=0).mean(dim=0) 241 | elif self.merge_mode == "concat": 242 | global_h = torch.concat(group_h_list, dim=-1) 243 | else: 244 | raise Exception("wrong merge mode: ", self.merge_mode) 245 | 246 | h = self.global_fc(global_h) 247 | 248 | return h 249 | 250 | 251 | 252 | class RpHGNNEncoder(CommonTorchTrainModel): 253 | 254 | def __init__(self, 255 | filters_list, 256 | group_channels_list, 257 | global_channels_list, 258 | merge_mode, 259 | input_shape, 260 | *args, 261 | input_drop_rate=0.0, 262 | drop_rate=0.0, 263 | activation="prelu", 264 | output_activation=None, 265 | **kwargs): 266 | 267 | super().__init__(*args, **kwargs) 268 | 269 | self.input_dropout = nn.Dropout(input_drop_rate) 270 | self.input_drop_rate = input_drop_rate 271 | 272 | group_encoders_input_shape = input_shape 273 | self.group_encoders = GroupEncoders(filters_list, drop_rate, group_encoders_input_shape) 274 | 275 | multi_group_fusion_input_shape = [[-1, group_input_shape[-1] * filters] 276 | for group_input_shape, filters in zip(group_encoders_input_shape, self.group_encoders.real_filters_list)] 277 | self.multi_group_fusion = MultiGroupFusion( 278 | group_channels_list, global_channels_list, 279 | merge_mode, 280 | input_shape=multi_group_fusion_input_shape, 281 | drop_rate=drop_rate, 282 | activation=activation, 283 | output_activation=output_activation) 284 | 285 | def forward(self, inputs): 286 | 287 | x_group_list = inputs 288 | dropped_x_group_list = [F.dropout(x_group, self.input_drop_rate, training=self.training, inplace=False) for x_group in x_group_list] 289 | 290 | h_list = self.group_encoders(dropped_x_group_list) 291 | h = self.multi_group_fusion(h_list) 292 | 293 | return h 294 | -------------------------------------------------------------------------------- /rphgnn/layers/rphgnn_pre.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import torch 4 | import dgl 5 | 6 | import numpy as np 7 | from rphgnn.utils.random_project_utils import * 8 | from itertools import chain 9 | import logging 10 | from rphgnn.global_configuration import global_config 11 | 12 | logger = logging.getLogger() 13 | 14 | 15 | 16 | 17 | def torch_svd(x): 18 | u, s, vh = torch.linalg.svd(x) 19 | 20 | print("svd: ", x.size(), u.size(), s.size(), vh.size()) 21 | h = u[:, :s.size(0)] * torch.sqrt(s) 22 | return h 23 | 24 | 25 | 26 | def get_raw_etype(etype): 27 | etype_ = etype[1] 28 | if etype_.startswith("r."): 29 | return get_reversed_etype(etype) 30 | else: 31 | return etype 32 | 33 | def rphgnn_propagate_then_update(g, current_k, inner_k, input_x_dim_dict, target_node_type, squash_strategy, norm=None, squash_even_odd="all", squash_self=True, collect_even_odd="all", diag_dict=None, train_label_feat=None): 34 | 35 | with g.local_scope(): 36 | 37 | # propagate 38 | for etype in g.canonical_etypes: 39 | last_key = "feat" 40 | for k_ in range(1, inner_k + 1): 41 | # print("etype: ", etype, "inner_k_: ", k_) 42 | odd_or_even = "odd" if k_ % 2 == 1 else "even" 43 | key = (odd_or_even, k_, etype) 44 | prop_etype = etype if odd_or_even == "odd" else get_reversed_etype(etype) 45 | 46 | # print("prop_etype: ", prop_etype) 47 | 48 | if norm == "mean": 49 | 50 | g.update_all( 51 | dgl.function.copy_u(last_key, "m"), 52 | dgl.function.mean("m", key), 53 | 54 | # message_func, 55 | # dgl.function.sum("m", key), 56 | 57 | etype=prop_etype 58 | ) 59 | last_key = key 60 | 61 | else: 62 | sp = torch.tensor(norm[2]) 63 | dp = torch.tensor(norm[4]) 64 | 65 | def message_func(edges): 66 | # return {'m': edges.src[last_key]} 67 | return {'m': edges.src[last_key] * \ 68 | torch.pow(edges.src[("deg", get_reversed_etype(prop_etype))].unsqueeze(-1) + 1e-8, sp) * \ 69 | torch.pow(edges.dst[("deg", prop_etype)].unsqueeze(-1) + 1e-8, dp)} 70 | 71 | g.update_all( 72 | message_func, 73 | dgl.function.sum("m", key), 74 | etype=prop_etype 75 | ) 76 | last_key = key 77 | 78 | 79 | new_x_dict = {} 80 | 81 | 82 | for ntype in g.ntypes: 83 | # print("deal with {} ...".format(ntype)) 84 | 85 | # sort keys by (etype, k) 86 | # [(odd, 1, etype0), (even, 2, etype0), (odd, 3, etype0), (even, 4, etype0), 87 | # (odd, 1, etype1), (even, 2, etype1), (odd, 3, etype1), (even, 4, etype1)] 88 | keys = [key for key in g.nodes[ntype].data.keys() 89 | if isinstance(key, tuple) and key[0] in ["even", "odd"]] 90 | sort_index = sorted(list(range(len(keys))), key=lambda i: (get_raw_etype(keys[i][-1]), keys[i][1])) 91 | sorted_keys = [keys[i] for i in sort_index] 92 | 93 | 94 | x = g.ndata["feat"][ntype] 95 | 96 | # collect for each ntype 97 | h_list = [] 98 | 99 | for key in sorted_keys: 100 | # print(key, g.nodes[ntype].data[key].size()) 101 | h = g.nodes[ntype].data[key] 102 | 103 | # label prop for target node type 104 | if ntype == target_node_type and diag_dict is not None: 105 | 106 | if key[0] == "even": 107 | diag = diag_dict[key[-1]] 108 | # diag = np.expand_dims(diag, axis=-1) 109 | h = (h - x * diag) / (1.0 - diag + 1e-8) 110 | 111 | if train_label_feat is not None: 112 | zero_mask = (h.sum(dim=-1) == 0.0) 113 | h[zero_mask] = torch.ones_like(h[zero_mask]) / h.size(-1) 114 | print("diag zero to mean for: {} {} {}".format(ntype, key, zero_mask.sum())) 115 | 116 | print("diag====", key) 117 | print("remove diag for: {} {}".format(ntype, key)) 118 | 119 | 120 | h_list.append(h) 121 | 122 | 123 | 124 | 125 | # each even_odd_iter covers an odd and an even, such as (1,2) or (3, 4) 126 | def get_even_odd_iter(data_list, i): 127 | """ 128 | input: [(odd, 1, etype0), (even, 2, etype0), (odd, 3, etype0), (even, 4, etype0), (odd, 1, etype1), (even, 2, etype1), (odd, 3, etype1), (even, 4, etype1)] 129 | 130 | output: odd+even of a given iteration i 131 | 132 | For exampe, if i == 0: 133 | output => [(odd, 1, etype0), (even, 2, etype0), (odd, 1, etype1), (even, 2, etype1)] 134 | """ 135 | return list(chain(*list(zip(data_list[i * 2::inner_k], data_list[i * 2 + 1::inner_k])))) 136 | 137 | even_odd_iter_h_list_list = [] 138 | 139 | for hop in range(inner_k // 2): 140 | even_odd_iter_h_list = get_even_odd_iter(h_list, hop) 141 | even_odd_iter_h_list_list.append(even_odd_iter_h_list) 142 | even_odd_iter_sorted_keys = get_even_odd_iter(sorted_keys, hop) 143 | # print("hop sorted keys: ", hop_sorted_keys) 144 | 145 | 146 | even_odd_iter_sorted_keys = [(key[0], key[2]) for key in even_odd_iter_sorted_keys] 147 | 148 | # push into outputs 149 | if ntype == target_node_type: 150 | # print("collect outputs for {}".format(ntype)) 151 | 152 | target_h_list_list = [[h.detach().cpu() for h in hop_h_list] 153 | for hop_h_list in even_odd_iter_h_list_list] 154 | target_sorted_keys = even_odd_iter_sorted_keys 155 | 156 | if collect_even_odd != "all": 157 | target_h_list_list = [[target_h for target_h, key in zip(target_h_list, target_sorted_keys) if key[0] == collect_even_odd] 158 | for target_h_list in target_h_list_list] 159 | target_sorted_keys = [key for key in target_sorted_keys if key[0] == collect_even_odd] 160 | 161 | 162 | 163 | 164 | squash_keys = [("self", )] if squash_self else [] 165 | squash_h_list = [x] if squash_self else [] 166 | 167 | for h, key in zip(even_odd_iter_h_list_list[0], even_odd_iter_sorted_keys): 168 | key_even_odd = key[0] 169 | 170 | use_key = None 171 | if squash_even_odd == "all": 172 | use_key = True 173 | elif squash_even_odd in ["even", "odd"]: 174 | use_key = key_even_odd == squash_even_odd 175 | else: 176 | raise ValueError("squash_even_odd must be all, even or odd") 177 | 178 | if use_key: 179 | squash_keys.append(key) 180 | squash_h_list.append(h) 181 | 182 | if squash_strategy == "sum": 183 | new_x = torch.stack(squash_h_list, dim=0).sum(dim=0) 184 | 185 | elif squash_strategy == "mean": 186 | new_x = torch.stack(squash_h_list, dim=0).mean(dim=0) 187 | 188 | elif squash_strategy == "norm_sum": 189 | normed_squash_h_list = [torch_normalize(h) for h in squash_h_list] 190 | new_x = torch.stack(normed_squash_h_list, dim=0).sum(dim=0) 191 | 192 | elif squash_strategy == "norm_mean": 193 | normed_squash_h_list = [torch_normalize(h) for h in squash_h_list] 194 | new_x = torch.stack(normed_squash_h_list, dim=0).mean(dim=0) 195 | 196 | elif squash_strategy == "norm_mean_norm": 197 | normed_squash_h_list = [torch_normalize(h) for h in squash_h_list] 198 | h = torch.stack(normed_squash_h_list, dim=0).mean(dim=0) 199 | h = torch_normalize(h) 200 | new_x = h 201 | 202 | elif squash_strategy == "project_norm_sum": 203 | new_x = torch_random_project_then_sum( 204 | squash_h_list, 205 | input_x_dim_dict[ntype], 206 | norm=True 207 | ) 208 | 209 | elif squash_strategy == "project_norm_mean": 210 | new_x = torch_random_project_then_mean( 211 | squash_h_list, 212 | input_x_dim_dict[ntype], 213 | norm=True 214 | ) 215 | else: 216 | raise ValueError("wrong squash_strategy: {}".format(squash_strategy)) 217 | 218 | new_x_dict[ntype] = new_x 219 | 220 | # print("update ndata") 221 | for ntype, new_x in new_x_dict.items(): 222 | g.nodes[ntype].data["feat"] = new_x 223 | 224 | if target_node_type is None: 225 | target_sorted_keys = None 226 | target_h_list_list = None 227 | 228 | return (target_h_list_list, target_sorted_keys), g 229 | 230 | 231 | def compute_deg_dict(g): 232 | 233 | with torch.no_grad(): 234 | 235 | deg_dict = {} 236 | def message_func(edges): 237 | return {'m': torch.ones([len(edges)])} 238 | 239 | for etype in g.canonical_etypes: 240 | key = ("deg", etype) 241 | g.update_all( 242 | message_func, 243 | dgl.function.sum("m", key), 244 | etype=etype 245 | ) 246 | deg = g.ndata[key][etype[-1]] 247 | deg_dict[etype] = deg 248 | 249 | return deg_dict 250 | 251 | 252 | def compute_diag_dict(g): 253 | import scipy.sparse as sp 254 | import numpy as np 255 | 256 | diag_dict = {} 257 | 258 | def norm_adj(adj): 259 | deg = np.array(adj.sum(axis=-1)).flatten() 260 | inv_deg = 1.0 / deg 261 | inv_deg[np.isnan(inv_deg)] = 0.0 262 | inv_deg[np.isinf(inv_deg)] = 0.0 263 | 264 | normed_adj = sp.diags(inv_deg) @ adj 265 | return normed_adj 266 | 267 | with torch.no_grad(): 268 | 269 | for etype in g.canonical_etypes: 270 | src, dst = g.edges(etype=etype) 271 | src = src.detach().cpu().numpy() 272 | dst = dst.detach().cpu().numpy() 273 | 274 | shape = [g.num_nodes(etype[0]), g.num_nodes(etype[-1])] 275 | 276 | adj = sp.csr_matrix((np.ones_like(src), (src, dst)), shape=shape) 277 | 278 | 279 | diag = (norm_adj(adj).multiply(norm_adj(adj.T).T)).sum(axis=-1) 280 | diag = np.array(diag).flatten().astype(np.float32) 281 | 282 | diag = np.expand_dims(diag, axis=-1) 283 | diag_dict[etype] = torch.tensor(diag) 284 | 285 | # print("compute diag for {}: {}".format(etype, diag)) 286 | 287 | 288 | return diag_dict 289 | 290 | 291 | 292 | 293 | def rphgnn_propagate_and_collect(g, k, inner_k, alpha, target_node_type, use_input_features, squash_strategy, train_label_feat, norm, squash_even_odd, collect_even_odd, squash_self=False, target_feat_random_project_size=None, add_self_group=False): 294 | 295 | with torch.no_grad(): 296 | 297 | raw_input_target_x = g.ndata["feat"][target_node_type] 298 | 299 | with g.local_scope(): 300 | 301 | featureless_node_types = [ntype for ntype in g.ntypes if ntype != target_node_type] 302 | embedding_size = g.ndata["feat"][featureless_node_types[0]].size(-1) 303 | 304 | if target_feat_random_project_size is not None: 305 | new_x = global_config.torch_random_project(raw_input_target_x, target_feat_random_project_size, norm=True) 306 | g.nodes[target_node_type].data["feat"] = new_x 307 | print("random_project_target_feat {} => {}...".format(raw_input_target_x.size(-1), new_x.size(-1))) 308 | 309 | if train_label_feat is not None: 310 | 311 | num_classes = train_label_feat.size(-1) 312 | for ntype in g.ntypes: 313 | if ntype == target_node_type: 314 | g.nodes[ntype].data["feat"] = train_label_feat 315 | else: 316 | g.nodes[ntype].data["feat"] = torch.ones([g.num_nodes(ntype), num_classes]) / num_classes 317 | 318 | diag_dict = compute_diag_dict(g) 319 | 320 | else: 321 | diag_dict = None 322 | 323 | input_x_dim_dict = { 324 | ntype: g.ndata["feat"][ntype].size(-1) 325 | for ntype in g.ntypes 326 | } 327 | 328 | input_x_dict = { 329 | ntype: g.ndata["feat"][ntype] for ntype in g.ntypes 330 | } 331 | 332 | 333 | 334 | input_target_x = g.ndata["feat"][target_node_type]#.detach().cpu().numpy() 335 | target_h_list_list = [] 336 | for k_ in range(k): 337 | # print("start propagate {} ...".format(k_)) 338 | 339 | print("start {} propagate-then-update iteration {} ...".format("feat" if train_label_feat is None else "pre-label", k_)) 340 | (target_h_list_list_, target_sorted_keys), g = rphgnn_propagate_then_update(g, k_, inner_k, input_x_dim_dict, target_node_type, squash_strategy=squash_strategy, norm=norm, squash_even_odd=squash_even_odd, collect_even_odd=collect_even_odd, squash_self=squash_self, diag_dict=diag_dict, train_label_feat=train_label_feat) 341 | 342 | target_h_list_list.extend(target_h_list_list_) 343 | 344 | 345 | for ntype in g.ntypes: 346 | g.nodes[ntype].data["feat"] = g.nodes[ntype].data["feat"] * (1 - alpha) + input_x_dict[ntype] * alpha 347 | 348 | 349 | target_h_list_list = [list(target_h_list) for target_h_list in zip(*target_h_list_list)] 350 | 351 | 352 | target_sorted_keys_ = target_sorted_keys[:] 353 | target_h_list_list_ = target_h_list_list[:] 354 | 355 | 356 | if train_label_feat is not None: 357 | 358 | target_sorted_keys = [] 359 | target_h_list_list = [] 360 | for key, target_h_list in zip(target_sorted_keys_, target_h_list_list_): 361 | if key[0] == "even": 362 | target_sorted_keys.append(key) 363 | target_h_list_list.append(target_h_list) 364 | elif key[0] == "odd": 365 | etype = key[-1] 366 | if etype[0] == etype[-1]: 367 | print("add homo for label: ", key) 368 | target_sorted_keys.append(key) 369 | target_h_list_list.append(target_h_list) 370 | 371 | target_h_list_list = [target_h_list[-1:] for target_h_list in target_h_list_list] 372 | 373 | 374 | if use_input_features: 375 | for target_h_list, key in zip(target_h_list_list, target_sorted_keys): 376 | if key[0] in ["even", "self"]: 377 | 378 | print("add input x to {}".format(key)) 379 | x = input_target_x 380 | x = x.detach().cpu() 381 | 382 | target_h_list.insert(0, x) 383 | 384 | 385 | # for target_h_list in target_h_list_list: 386 | # print("context: ") 387 | # for target_h in target_h_list: 388 | # print(target_h.shape) 389 | 390 | if add_self_group: 391 | target_h_list_list.append([raw_input_target_x.detach().cpu()]) 392 | target_sorted_keys.append(("self",)) 393 | 394 | print("target_sorted_keys: ", target_sorted_keys) 395 | target_h_list_list = [torch.stack(target_h_list, dim=1) for target_h_list in target_h_list_list] 396 | 397 | return target_h_list_list, target_sorted_keys 398 | 399 | 400 | 401 | def rphgnn_propagate_and_collect_label(hetero_graph, target_node_type, y, train_label_feat): 402 | 403 | label_target_h_list_list, _ = rphgnn_propagate_and_collect(hetero_graph, 404 | 1, 405 | 2, 406 | 0.0, 407 | target_node_type, use_input_features=False, 408 | squash_strategy="mean", 409 | train_label_feat=train_label_feat, 410 | norm="mean", 411 | squash_even_odd="all", 412 | collect_even_odd="all" 413 | ) 414 | 415 | 416 | return label_target_h_list_list -------------------------------------------------------------------------------- /rphgnn/layers/torch_train_model.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. 2 | # All rights reserved. 3 | # 4 | # This source code is licensed under the license found in the 5 | # LICENSE file in the root directory of this source tree. 6 | 7 | import numpy as np 8 | from sklearn.metrics import accuracy_score, f1_score 9 | import torch 10 | import torch.nn as nn 11 | from itertools import chain 12 | import torch.nn.functional as F 13 | from tqdm import tqdm 14 | import time 15 | 16 | from rphgnn.utils.metrics_utils import LogitsBasedMetric 17 | # x = torch.randn(350)# * 0.1 18 | 19 | # # x = torch.zeros(10) 20 | # # x[1] = 1.0 21 | 22 | # print(x) 23 | # x = F.softmax(x) 24 | 25 | # # print(x) 26 | # print(x.max()) 27 | # print(x.argmax()) 28 | 29 | 30 | # print("===========") 31 | 32 | # x = torch.pow(x * 350, 4.0) 33 | # # print(x) 34 | # x = F.softmax(x) 35 | # # print(x) 36 | # print(x.max()) 37 | # print(x.argmax()) 38 | 39 | # sdfsdf 40 | 41 | # x = torch.randn(100, 5, 40).to("cuda") 42 | 43 | # layer = torch.nn.Conv1d(6, 10, 1, 1).cuda() 44 | 45 | # print(layer(x)) 46 | # asdfasdf 47 | 48 | 49 | 50 | 51 | class TorchTrainModel(nn.Module): 52 | def __init__(self, metrics_dict=None, learning_rate=None, scheduler_gamma=None) -> None: 53 | 54 | super().__init__() 55 | 56 | self.metrics_dict = metrics_dict 57 | self.learning_rate = learning_rate 58 | self.scheduler_gamma = scheduler_gamma 59 | self.stop_training = False 60 | 61 | use_float16 = False 62 | 63 | if use_float16: 64 | self.autocast_dtype = torch.float16 65 | self.scalar = torch.cuda.amp.GradScaler() 66 | else: 67 | self.autocast_dtype = torch.float32 68 | self.scalar = None 69 | 70 | self.optimizer = None 71 | 72 | 73 | def predict(self, data_loader, training=False): 74 | last_status = self.training 75 | if training: 76 | self.train() 77 | else: 78 | self.eval() 79 | 80 | with torch.no_grad(): 81 | with torch.autocast(device_type=self.device, dtype=self.autocast_dtype): 82 | batch_y_pred_list = [] 83 | for step, (batch_x, batch_y) in enumerate(tqdm(data_loader)): 84 | batch_logits = self(batch_x) 85 | batch_y_pred = self.output_activation_func(batch_logits) 86 | # if multi_label: 87 | # batch_y_pred = (F.sigmoid(batch_logits) > 0.5).float() 88 | # else: 89 | # batch_y_pred = torch.argmax(batch_logits, dim=-1) 90 | 91 | # batch_y_pred_list.append(batch_y_pred.detach().cpu().numpy()) 92 | 93 | batch_y_pred_list.append(batch_y_pred.cpu()) 94 | 95 | # y_pred = np.concatenate(batch_y_pred_list, axis=0) 96 | 97 | y_pred = torch.concat(batch_y_pred_list, dim=0) 98 | 99 | self.train(last_status) 100 | return y_pred 101 | 102 | 103 | 104 | def evaluate(self, data_loader, log_prefix): 105 | self.eval() 106 | 107 | # for metric_name, metric in self.metrics_dict.items(): 108 | # metric.reset() 109 | # print("reset metric for evaluation: {}".format(metric_name)) 110 | 111 | with torch.no_grad(): 112 | with torch.autocast(device_type=self.device, dtype=self.autocast_dtype): 113 | batch_y_pred_list = [] 114 | batch_y_list = [] 115 | losses_list = [] 116 | for step, (batch_x, batch_y) in enumerate(tqdm(data_loader)): 117 | batch_logits = self(batch_x) 118 | 119 | batch_losses = self.loss_func(batch_logits, batch_y) 120 | 121 | if self.multi_label: 122 | batch_y_pred = (torch.sigmoid(batch_logits) > 0.5).float() 123 | else: 124 | batch_y_pred = torch.argmax(batch_logits, dim=-1) 125 | 126 | if self.metrics_dict is not None: 127 | for metric in self.metrics_dict.values(): 128 | if isinstance(metric, LogitsBasedMetric): 129 | metric(batch_logits, batch_y) 130 | else: 131 | metric(batch_y_pred, batch_y) 132 | 133 | losses_list.append(batch_losses.detach().cpu().numpy()) 134 | batch_y_pred_list.append(batch_y_pred.detach().cpu().numpy()) 135 | batch_y_list.append(batch_y.detach().cpu().numpy()) 136 | 137 | losses = np.concatenate(losses_list, axis=0) 138 | loss = losses.mean() 139 | 140 | # y_pred = np.concatenate(batch_y_pred_list, axis=0) 141 | # y_true = np.concatenate(batch_y_list, axis=0) 142 | 143 | 144 | # accuracy = accuracy_score(y_true, y_pred) 145 | 146 | # if dataset != "mag": 147 | # micro_f1 = f1_score(y_true, y_pred, average="micro") 148 | # macro_f1 = f1_score(y_true, y_pred, average="macro") 149 | 150 | 151 | logs = {} 152 | 153 | logs["{}_loss".format(log_prefix)] = loss 154 | # logs["{}_accuracy".format(log_prefix)] = accuracy 155 | 156 | # if dataset != "mag": 157 | # logs["{}_micro_f1".format(log_prefix)] = micro_f1 158 | # logs["{}_macro_f1".format(log_prefix)] = macro_f1 159 | 160 | if self.metrics_dict is not None: 161 | with torch.no_grad(): 162 | for metric_name, metric in self.metrics_dict.items(): 163 | logs["{}_{}".format(log_prefix, metric_name)] = metric.compute().item() 164 | metric.reset() 165 | 166 | return logs 167 | 168 | 169 | 170 | 171 | def train_step(self, batch_data): 172 | return {} 173 | 174 | 175 | 176 | def train_epoch(self, epoch, train_data_loader): 177 | self.train() 178 | 179 | batch_results_dict = {} 180 | step_pbar = tqdm(train_data_loader) 181 | for step, batch_data in enumerate(step_pbar): 182 | batch_result = self.train_step(batch_data) 183 | with torch.no_grad(): 184 | for key, value in batch_result.items(): 185 | if key not in batch_results_dict: 186 | batch_results_dict[key] = [] 187 | batch_results_dict[key].append(value) 188 | 189 | step_pbar.set_postfix( 190 | {key: "{:.4f}".format(value.item()) for key, value in batch_result.items()} 191 | ) 192 | 193 | 194 | if self.scheduler is not None: 195 | self.scheduler.step() 196 | print("current learning_rate: ", self.scheduler.get_last_lr()) 197 | 198 | with torch.no_grad(): 199 | logs = { 200 | key: torch.stack(value, dim=0).mean().item() for key, value in batch_results_dict.items() 201 | } 202 | 203 | return logs 204 | 205 | 206 | 207 | 208 | def fit(self, train_data, 209 | epochs, 210 | validation_data, 211 | validation_freq, 212 | callbacks=None, 213 | initial_epoch=0, 214 | ): 215 | 216 | if self.optimizer is None: 217 | self.optimizer = torch.optim.Adam(self.parameters(), lr=self.learning_rate) 218 | print("create optimizer ...") 219 | 220 | if self.scheduler_gamma is not None: 221 | self.scheduler = torch.optim.lr_scheduler.StepLR(self.optimizer, step_size=1, gamma=self.scheduler_gamma) 222 | else: 223 | self.scheduler = None 224 | 225 | if callbacks is None: 226 | callbacks = [] 227 | 228 | for callback in callbacks: 229 | callback.model = self 230 | 231 | for callback in callbacks: 232 | callback.on_train_begin() 233 | 234 | for epoch in range(initial_epoch, epochs): 235 | logs = {"epoch": epoch} 236 | self.train() 237 | print("start epoch {}:".format(epoch)) 238 | train_logs = self.train_epoch(epoch, train_data) 239 | # train_logs = {"train_{}".format(key): value for key, value in train_logs.items()} 240 | logs = { 241 | **logs, 242 | **train_logs 243 | } 244 | 245 | # if epoch % validation_freq == 0: 246 | 247 | if (epoch + 1) % validation_freq == 0: 248 | self.eval() 249 | eval_start_time = time.time() 250 | validation_logs = self.evaluate(validation_data, log_prefix="val") 251 | logs = { 252 | **logs, 253 | **validation_logs 254 | } 255 | print("==== eval_time: ", time.time() - eval_start_time) 256 | 257 | 258 | for callback in callbacks: 259 | callback.on_epoch_end(epoch, logs) 260 | 261 | 262 | if (epoch + 1) % validation_freq == 0: 263 | # np_logs = {key: np.array(value) for key, value in logs.items()} 264 | print("epoch = {}\tlogs = {}".format(epoch, logs)) 265 | 266 | if self.stop_training: 267 | print("early stop ...") 268 | break 269 | 270 | 271 | 272 | 273 | 274 | 275 | class CommonTorchTrainModel(TorchTrainModel): 276 | def __init__(self, metrics_dict=None, multi_label=False, loss_func=None, learning_rate=None, scheduler_gamma=None, train_strategy="common", num_views=None, cl_rate=None) -> None: 277 | 278 | super().__init__(metrics_dict, learning_rate, scheduler_gamma) 279 | 280 | self.multi_label = multi_label 281 | self.train_strategy = train_strategy 282 | self.num_views = num_views 283 | self.cl_rate = cl_rate 284 | self.device = "cuda" 285 | 286 | if loss_func is not None: 287 | self.loss_func = loss_func 288 | else: 289 | if self.multi_label: 290 | self.loss_func = torch.nn.BCEWithLogitsLoss(reduction="none") 291 | self.output_activation_func = torch.nn.Sigmoid() 292 | else: 293 | self.loss_func = torch.nn.CrossEntropyLoss(reduction="none") 294 | self.output_activation_func = torch.nn.Softmax(dim=-1) 295 | 296 | 297 | 298 | def weighted_cross_entropy(logits, labels): 299 | probs = F.softmax(logits, dim=-1) 300 | probs = probs[torch.arange(0, probs.size(0)), labels] 301 | probs = probs.detach() 302 | 303 | weights = torch.ones_like(probs) 304 | weights[probs > 0.8] = 0.0 305 | 306 | 307 | scale = torch.tensor(weights.size(0)).float() / (weights.sum() + 1e-8) 308 | weights *= scale 309 | 310 | losses = self.loss_func(logits, labels) 311 | loss = (losses * weights).mean() 312 | return loss 313 | 314 | 315 | self.optimizer = None 316 | 317 | 318 | def compute_kl_loss(self, logits, batch_x): 319 | batch_label_x = batch_x[-self.num_class_groups:] 320 | pseudo_label_list = [torch.stack(h_list, dim=0).mean(dim=0) for h_list in batch_label_x] 321 | pseudo_label = torch.stack(pseudo_label_list, dim=0).mean(dim=0) 322 | 323 | kl_loss = self.loss_func(logits, pseudo_label).mean() 324 | return kl_loss 325 | 326 | def compute_l2_loss(self): 327 | l2_loss = 0.0 328 | for name, param in self.named_parameters(): 329 | if "weight" in name: 330 | l2_loss += (param ** 2).sum() * 0.5 331 | print("l2_loss = {}".format(l2_loss.item())) 332 | return l2_loss * 1e-5 333 | 334 | 335 | def common_forward_and_compute_loss(self, batch_x, batch_y): 336 | 337 | logits = self(batch_x) 338 | losses = self.loss_func(logits, batch_y) 339 | loss = losses.mean() 340 | 341 | # l2_loss = self.compute_l2_loss() 342 | # loss += l2_loss 343 | 344 | # if self.num_class_groups is not None and self.num_class_groups > 0: 345 | # kl_loss = self.compute_kl_loss(logits, batch_x) 346 | # loss += kl_loss * 0.5 347 | 348 | # print("kl_loss = {}".format(kl_loss.item())) 349 | 350 | return logits, loss 351 | 352 | def cl_forward_and_compute_loss(self, batch_x, batch_y, batch_train_mask): 353 | 354 | 355 | ce_loss_list = [] 356 | y_pred_list = [] 357 | 358 | logits_list = [self(batch_x) for _ in range(self.num_views)] 359 | ce_loss_list = [self.loss_func(logits[batch_train_mask], batch_y[batch_train_mask]).mean() 360 | for logits in logits_list] 361 | 362 | # ce_loss_list = [self.weighted_loss(logits[batch_train_mask], batch_y[batch_train_mask]) 363 | # for logits in logits_list] 364 | 365 | ce_loss = torch.stack(ce_loss_list, dim=0).sum(dim=0) 366 | 367 | 368 | y_pred_list = [self.output_activation_func(logits) for logits in logits_list] 369 | 370 | 371 | # self.eval() 372 | # y_pred_list.append(self.output_activation_func(self(batch_x)).detach()) 373 | # self.train() 374 | 375 | stacked_y_preds = torch.stack(y_pred_list, dim=1) 376 | mean_y_pred = stacked_y_preds.mean(dim=1) 377 | 378 | pseudo_y = torch.argmax(mean_y_pred, dim=-1) 379 | 380 | # def compute_pseudo_acc(y_pred): 381 | # pseudo_y = y_pred.argmax(dim=-1) 382 | # unlabeled_peudo_y = pseudo_y[~batch_train_mask] 383 | # cl_acc = (unlabeled_peudo_y == batch_y[~batch_train_mask]).float().mean() 384 | # return cl_acc 385 | 386 | # for i, logits in enumerate(logits_list): 387 | # print("logits{}_acc = {}".format(i, compute_pseudo_acc(logits).item())) 388 | # print("cl_acc = {}".format(compute_pseudo_acc(mean_y_pred).item())) 389 | 390 | # ce for cl 391 | cl_loss_list = [self.loss_func(logits, pseudo_y).mean() 392 | for logits in logits_list] 393 | cl_loss = torch.stack(cl_loss_list, dim=0).sum(dim=0) 394 | 395 | 396 | loss = ce_loss + cl_loss * self.cl_rate 397 | 398 | 399 | # if self.num_class_groups is not None and self.num_class_groups > 0: 400 | # kl_loss_list = [self.compute_kl_loss(logits, batch_x) for logits in logits_list] 401 | # kl_loss = torch.stack(kl_loss_list, dim=0).sum(dim=0) 402 | # loss += kl_loss * 0.5 403 | 404 | # # print("kl_loss = {}".format(kl_loss.item())) 405 | 406 | return logits_list[0], loss 407 | 408 | def train_step(self, batch_data): 409 | 410 | # train_start_time = time.time() 411 | self.train() 412 | with torch.autocast(device_type=self.device, dtype=self.autocast_dtype): 413 | 414 | if self.train_strategy == "common": 415 | batch_x, batch_y = batch_data 416 | logits, loss = self.common_forward_and_compute_loss(batch_x, batch_y) 417 | 418 | elif self.train_strategy == "cl": 419 | batch_x, batch_y, batch_train_mask = batch_data 420 | logits, loss = self.cl_forward_and_compute_loss(batch_x, batch_y, batch_train_mask) 421 | 422 | elif self.train_strategy == "cl_conf": 423 | batch_x, batch_y, batch_train_mask = batch_data 424 | logits, loss = self.cl_conf_forward_and_compute_loss(batch_x, batch_y, batch_train_mask) 425 | 426 | elif self.train_strategy == "cl_cos": 427 | batch_x, batch_y, batch_train_mask = batch_data 428 | logits, loss = self.cl_cos_forward_and_compute_loss(batch_x, batch_y, batch_train_mask) 429 | 430 | elif self.train_strategy == "cl_soft": 431 | batch_x, batch_y, batch_train_mask = batch_data 432 | logits, loss = self.cl_soft_forward_and_compute_loss(batch_x, batch_y, batch_train_mask) 433 | elif self.train_strategy == "cl_weighted": 434 | batch_x, batch_y, batch_train_mask, weights = batch_data 435 | logits, loss = self.cl_weighted_forward_and_compute_loss(batch_x, batch_y, batch_train_mask, weights) 436 | 437 | else: 438 | raise Exception("not supported yet") 439 | 440 | # print("forward_time: ", time.time() - train_start_time) 441 | 442 | self.optimizer.zero_grad() 443 | if self.scalar is None: 444 | loss.backward() 445 | self.optimizer.step() 446 | else: 447 | self.scalar.scale(loss).backward() 448 | self.scalar.step(self.optimizer) 449 | self.scalar.update() 450 | 451 | with torch.no_grad(): 452 | with torch.autocast(device_type=self.device, dtype=self.autocast_dtype): 453 | if self.multi_label: 454 | batch_y_pred = logits > 0.0 455 | else: 456 | batch_y_pred = logits.argmax(dim=-1) 457 | 458 | batch_corrects = (batch_y_pred == batch_y).float() 459 | batch_accuracy = batch_corrects.mean() 460 | 461 | return { 462 | "loss": loss, 463 | "accuracy": batch_accuracy 464 | } 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | -------------------------------------------------------------------------------- /rphgnn/losses.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import torch.nn.functional as F 4 | 5 | def kl_loss(y_pred, y_true): 6 | y_pred = F.log_softmax(y_pred, dim=-1) 7 | losses = F.kl_div(y_pred, y_true, reduction='none').sum(dim=-1) 8 | return losses -------------------------------------------------------------------------------- /rphgnn/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CrawlScript/RpHGNN/1a1779a747a28ac8d936280a6b96951636183965/rphgnn/utils/__init__.py -------------------------------------------------------------------------------- /rphgnn/utils/argparse_utils.py: -------------------------------------------------------------------------------- 1 | def parse_bool(bool_str): 2 | if bool_str == "True": 3 | return True 4 | elif bool_str == "False": 5 | return False 6 | else: 7 | raise Exception("wrong bool_str: ", bool_str) -------------------------------------------------------------------------------- /rphgnn/utils/graph_utils.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import dgl 4 | import numpy as np 5 | import torch 6 | from rphgnn.global_configuration import global_config 7 | 8 | def dgl_remove_edges(g, etypes_to_remove): 9 | etypes_to_remove = set(etypes_to_remove) 10 | 11 | edge_dict = {} 12 | for etype in list(g.canonical_etypes): 13 | if etype not in etypes_to_remove: 14 | edge_dict[etype] = g.edges(etype=etype) 15 | 16 | new_g = dgl.heterograph(edge_dict) 17 | 18 | for key in g.ndata: 19 | print("key = ", key) 20 | value = {ntype: data for ntype, data in g.ndata[key].items() if ntype in new_g.ntypes} 21 | new_g.ndata[key] = value 22 | 23 | return new_g 24 | 25 | def dgl_add_all_reversed_edges(g): 26 | edge_dict = {} 27 | for etype in list(g.canonical_etypes): 28 | col, row = g.edges(etype=etype) 29 | edge_dict[etype] = (col, row) 30 | 31 | if etype[0] != etype[2]: 32 | new_etype = (etype[2], "r.{}".format(etype[1]), etype[0]) 33 | edge_dict[new_etype] = (row, col) 34 | 35 | new_g = dgl.heterograph(edge_dict) 36 | 37 | for key in g.ndata: 38 | print("key = ", key) 39 | new_g.ndata[key] = g.ndata[key] 40 | 41 | return new_g 42 | 43 | 44 | def add_random_feats(hetero_graph, embedding_size, excluded_ntypes=None): 45 | def normalize(x): 46 | return x / np.linalg.norm(x, axis=-1, keepdims=True) 47 | 48 | def create_embedding_for_node_type(ntype): 49 | num_nodes = hetero_graph.num_nodes(ntype) 50 | print("start random feature") 51 | embeddings = torch.randn([num_nodes, embedding_size], generator=global_config.embedding_generator) / np.sqrt(embedding_size) 52 | return embeddings 53 | 54 | for ntype in list(hetero_graph.ntypes): 55 | if excluded_ntypes is None or ntype not in excluded_ntypes: 56 | print("set data: ", ntype) 57 | hetero_graph.nodes[ntype].data["feat"] = create_embedding_for_node_type(ntype) 58 | 59 | return hetero_graph 60 | -------------------------------------------------------------------------------- /rphgnn/utils/metrics_utils.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import numpy as np 3 | import torchmetrics 4 | 5 | def dcg_at_k(r, k): 6 | r = np.asfarray(r)[:k] 7 | if r.size: 8 | return r[0] + np.sum(r[1:] / np.log2(np.arange(2, r.size + 1))) 9 | return 0. 10 | 11 | 12 | def ndcg_at_k(r, k): 13 | dcg_max = dcg_at_k(sorted(r, reverse=True), k) 14 | if not dcg_max: 15 | return 0. 16 | return dcg_at_k(r, k) / dcg_max 17 | 18 | 19 | def mean_reciprocal_rank(rs): 20 | rs = (np.asarray(r).nonzero()[0] for r in rs) 21 | return np.mean([1. / (r[0] + 1) if r.size else 0. for r in rs]) 22 | 23 | 24 | def ndcg_mrr(pred, labels): 25 | """ 26 | Compute both NDCG and MRR for single-label and multi-label. Code extracted from 27 | https://github.com/acbull/pyHGT/blob/f7c4be620242d8c1ab3055f918d4c082f5060e07/OAG/train_paper_venue.py#L316 (single label) 28 | and 29 | https://github.com/acbull/pyHGT/blob/f7c4be620242d8c1ab3055f918d4c082f5060e07/OAG/train_paper_field.py#L322 (multi-label) 30 | """ 31 | test_res = [] 32 | if len(labels.shape) == 1: 33 | # single-label 34 | for ai, bi in zip(labels, pred.argsort(descending = True)): 35 | test_res += [(bi == ai).int().tolist()] 36 | else: 37 | # multi-label 38 | for ai, bi in zip(labels, pred.argsort(descending = True)): 39 | test_res += [ai[bi].int().tolist()] 40 | ndcg = np.mean([ndcg_at_k(resi, len(resi)) for resi in test_res]) 41 | mrr = mean_reciprocal_rank(test_res) 42 | return ndcg, mrr 43 | 44 | 45 | ############################################################################### 46 | # Fast re-implementation of NDCG and MRR for a batch of nodes. 47 | # We provide unit test below using random input to verify correctness / 48 | # equivalence. 49 | ############################################################################### 50 | 51 | def batched_dcg_at_k(r, k): 52 | assert(len(r.shape) == 2 and r.size != 0 and k > 0) 53 | r = r[:, :k].float() 54 | # Usually, one defines DCG = \sum\limits_{i=0}^{n-1}\frac{r_i}/{log2(i+2)} 55 | # So, we should 56 | # return (r / torch.log2(torch.arange(0, r.shape[1], device=r.device, dtype=r.dtype).view(1, -1) + 2)).sum(dim=1) 57 | # However, HGT author implements DCG = r_0 + \sum\limits_{i=1}^{n-1}\frac{r_i}/{log2(i+1)}, which makes DCG and NDCG larger 58 | # Here, we follow HGT author for a fair comparison 59 | return r[:, 0] + (r[:, 1:] / torch.log2(torch.arange(1, r.shape[1], device=r.device, dtype=r.dtype).view(1, -1) + 1)).sum(dim=1) 60 | 61 | 62 | def batched_ndcg_at_k(r, k): 63 | dcg_max = batched_dcg_at_k(r.sort(dim=1, descending=True)[0], k) 64 | dcg_max_inv = 1.0 / dcg_max 65 | dcg_max_inv[torch.isinf(dcg_max_inv)] = 0 66 | return batched_dcg_at_k(r, k) * dcg_max_inv 67 | 68 | 69 | def batched_mrr(r): 70 | r = r != 0 71 | # torch 1.5 does not guarantee max returns first occurrence 72 | # https://pytorch.org/docs/1.5.0/torch.html?highlight=max#torch.max 73 | # So we get first occurrence of non-zero using numpy max 74 | max_indices = torch.from_numpy(r.cpu().numpy().argmax(axis=1)) 75 | max_values = r[torch.arange(r.shape[0]), max_indices] 76 | r = 1.0 / (max_indices.float() + 1) 77 | r[max_values == 0] = 0 78 | return r 79 | 80 | 81 | # def batched_ndcg_mrr(pred, labels): 82 | # pred = pred.argsort(descending=True) 83 | # if len(labels.shape) == 1: 84 | # # single-label 85 | # labels = labels.view(-1, 1) 86 | # rel = (pred == labels).int() 87 | # else: 88 | # # multi-label 89 | # rel = torch.gather(labels, 1, pred) 90 | # return batched_ndcg_at_k(rel, rel.shape[1]), batched_mrr(rel) 91 | 92 | def compute_batched_ndcg(pred, labels): 93 | pred = pred.argsort(descending=True) 94 | if len(labels.shape) == 1: 95 | # single-label 96 | labels = labels.view(-1, 1) 97 | rel = (pred == labels).int() 98 | else: 99 | # multi-label 100 | rel = torch.gather(labels, 1, pred) 101 | return batched_ndcg_at_k(rel, rel.shape[1]) 102 | 103 | def compute_batched_mrr(pred, labels): 104 | pred = pred.argsort(descending=True) 105 | if len(labels.shape) == 1: 106 | # single-label 107 | labels = labels.view(-1, 1) 108 | rel = (pred == labels).int() 109 | else: 110 | # multi-label 111 | rel = torch.gather(labels, 1, pred) 112 | return batched_mrr(rel) 113 | 114 | 115 | class LogitsBasedMetric(torchmetrics.Metric): 116 | pass 117 | 118 | 119 | class NDCG(LogitsBasedMetric): 120 | def __init__(self): 121 | super().__init__() 122 | self.add_state("ndcg_sum", default=torch.tensor(0.0), dist_reduce_fx="sum") 123 | self.add_state("total", default=torch.tensor(0), dist_reduce_fx="sum") 124 | 125 | def update(self, logits: torch.Tensor, target: torch.Tensor): 126 | batch_ndcgs = compute_batched_ndcg(logits, target) 127 | self.ndcg_sum += batch_ndcgs.sum() 128 | # self.total += target.numel() 129 | self.total += target.size(0) 130 | 131 | def compute(self): 132 | return self.ndcg_sum / self.total.float() 133 | 134 | 135 | class MRR(LogitsBasedMetric): 136 | def __init__(self): 137 | super().__init__() 138 | self.add_state("mrr_sum", default=torch.tensor(0.0), dist_reduce_fx="sum") 139 | self.add_state("total", default=torch.tensor(0), dist_reduce_fx="sum") 140 | 141 | def update(self, logits: torch.Tensor, target: torch.Tensor): 142 | batch_mrrs = compute_batched_mrr(logits, target) 143 | self.mrr_sum += batch_mrrs.sum() 144 | # self.total += target.numel() 145 | self.total += target.size(0) 146 | 147 | def compute(self): 148 | return self.mrr_sum / self.total.float() -------------------------------------------------------------------------------- /rphgnn/utils/nested_data_utils.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | 4 | def nested_map(data, func): 5 | if isinstance(data, list): 6 | return [nested_map(item, func) for item in data] 7 | else: 8 | return func(data) 9 | 10 | def gather_h_y(target_h_list_list, y, index): 11 | def func(data): 12 | return data[index] 13 | 14 | target_h_list_list_, y_ = nested_map(target_h_list_list, func), nested_map(y, func) 15 | 16 | return target_h_list_list_, y_ 17 | 18 | 19 | def nested_gather(nested_data, index): 20 | def func(data): 21 | return data[index] 22 | return nested_map(nested_data, func) 23 | -------------------------------------------------------------------------------- /rphgnn/utils/random_project_utils.py: -------------------------------------------------------------------------------- 1 | from dataclasses import dataclass 2 | import torch 3 | import torch.nn.functional as F 4 | import numpy as np 5 | from rphgnn.global_configuration import global_config 6 | 7 | 8 | 9 | 10 | 11 | def torch_normalize_l2(x): 12 | return F.normalize(x, dim=-1) 13 | 14 | torch_normalize = torch_normalize_l2 15 | 16 | def get_reversed_etype(etype): 17 | 18 | if etype[0] == etype[2]: 19 | return etype 20 | 21 | reversed_etype_ = etype[1][2:] if etype[1].startswith("r.") else "r.{}".format(etype[1]) 22 | return (etype[2], reversed_etype_, etype[0]) 23 | 24 | 25 | def create_func_torch_random_project_create_kernel_sparse(s=3.0): 26 | 27 | def torch_random_project_create_kernel_sparse(x, units, input_units=None, generator=None): 28 | 29 | if input_units is None: 30 | input_units = x.size(-1) 31 | shape = [input_units, units] 32 | 33 | stddev = 1.0 34 | 35 | if generator is None: 36 | probs = torch.rand(shape) 37 | else: 38 | print("generate fast random projection kernel with generator") 39 | probs = torch.rand(shape, generator=generator) 40 | 41 | 42 | fill = torch.ones(shape) * torch.sqrt(torch.tensor(s)) * stddev 43 | 44 | kernel = torch.zeros(shape) 45 | kernel = torch.where(probs >= (1.0 - 0.5 / s), fill, kernel) 46 | kernel = torch.where(probs < (0.5 / s), -fill, kernel) 47 | 48 | return kernel 49 | 50 | return torch_random_project_create_kernel_sparse 51 | 52 | 53 | def torch_random_project_create_kernel_xavier(x, units, input_units=None, generator=None): 54 | if input_units is None: 55 | input_units = x.size(-1) 56 | shape = [input_units, units] 57 | stddev = torch.sqrt(torch.tensor(2.0 / (shape[0] + shape[1]))) 58 | kernel = torch.randn(shape) * stddev 59 | return kernel 60 | 61 | 62 | def torch_random_project_create_kernel_xavier_no_norm(x, units, input_units=None, generator=None): 63 | if input_units is None: 64 | input_units = x.size(-1) 65 | shape = [input_units, units] 66 | 67 | stddev = 1.0 68 | kernel = torch.randn(shape) * stddev 69 | return kernel 70 | 71 | 72 | def torch_random_project_common(x, units, activation=False, norm=True, kernel=None, generator=None): 73 | 74 | if kernel is None: 75 | kernel = global_config.torch_random_project_create_kernel(x, units, generator=generator) 76 | 77 | h = x @ kernel 78 | 79 | if norm: 80 | h = torch_normalize(h) 81 | 82 | return h 83 | 84 | 85 | global_config.torch_random_project = torch_random_project_common 86 | global_config.torch_random_project_create_kernel = create_func_torch_random_project_create_kernel_sparse(s=3.0) 87 | 88 | 89 | 90 | 91 | def torch_random_project_then_sum(x_list, units, norm=True, generator=None): 92 | h_list = [global_config.torch_random_project(x, units, norm=norm, generator=generator) 93 | for x in x_list] 94 | h = torch.stack(h_list, dim=0).sum(dim=0) 95 | return h 96 | 97 | 98 | 99 | def torch_random_project_then_mean(x_list, units, norm=True, num_samplings=None): 100 | 101 | h_list = [global_config.torch_random_project(x, units, norm=norm) 102 | for x in x_list] 103 | h = torch.stack(h_list, dim=0).mean(dim=0) 104 | 105 | return h 106 | 107 | 108 | -------------------------------------------------------------------------------- /rphgnn/utils/random_utils.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import random 4 | import numpy as np 5 | import torch 6 | 7 | def reset_seed(seed): 8 | print("set seed: {} ...".format(seed)) 9 | random.seed(seed) 10 | torch.manual_seed(seed) 11 | np.random.seed(seed) 12 | 13 | torch.cuda.manual_seed_all(seed) 14 | torch.backends.cudnn.deterministic = True -------------------------------------------------------------------------------- /rphgnn/utils/torch_data_utils.py: -------------------------------------------------------------------------------- 1 | 2 | import torch 3 | from rphgnn.utils.nested_data_utils import nested_map 4 | import numpy as np 5 | 6 | def get_len(data): 7 | if isinstance(data, list): 8 | return get_len(data[0]) 9 | else: 10 | return data.size(0) 11 | 12 | def get_device(data): 13 | if isinstance(data, list): 14 | return get_device(data[0]) 15 | else: 16 | return data.device 17 | 18 | 19 | class NestedDataset(torch.utils.data.Dataset): 20 | 21 | def __init__(self, nested_data, device=None) -> None: 22 | self.nested_data = nested_data 23 | self.device = device 24 | 25 | def __getitem__(self, idx): 26 | 27 | def func(x): 28 | batch_data = x[idx] 29 | if self.device is not None: 30 | batch_data = batch_data.to(self.device) 31 | return batch_data 32 | 33 | batch_data = nested_map(self.nested_data, func) 34 | 35 | return batch_data 36 | 37 | def __len__(self): 38 | return np.ceil(get_len(self.nested_data)).astype(np.int32) 39 | 40 | 41 | class NestedDataLoader(torch.utils.data.DataLoader): 42 | def __init__(self, nested_data, batch_size, shuffle, device) -> None: 43 | dataset = NestedDataset(nested_data, device) 44 | if shuffle: 45 | sampler = torch.utils.data.RandomSampler(dataset) 46 | else: 47 | sampler = torch.utils.data.SequentialSampler(dataset) 48 | 49 | sampler = torch.utils.data.BatchSampler(sampler, batch_size=batch_size, drop_last=False) 50 | 51 | super().__init__( 52 | dataset=dataset, 53 | sampler=sampler, 54 | collate_fn=lambda batch: batch[0], 55 | ) 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /scripts/run_ACM.sh: -------------------------------------------------------------------------------- 1 | SEED=1 2 | GPU=0 3 | DATASET=hgb_acm 4 | METHOD=rphgnn 5 | USE_NRL=False 6 | TRAIN_STRATEGY=common 7 | USE_INPUT=True 8 | ALL_FEAT=True 9 | INPUT_DROP_RATE=0.8 10 | DROP_RATE=0.7 11 | HIDDEN_SIZE=64 12 | SQUASH_K=6 13 | EPOCHS=500 14 | MAX_PATIENCE=100 15 | EMBEDDING_SIZE=256 16 | USE_LABEL=False 17 | EVEN_ODD="all" 18 | 19 | python -u main_rphgnn.py \ 20 | --method ${METHOD} \ 21 | --dataset ${DATASET} \ 22 | --use_nrl ${USE_NRL} \ 23 | --use_label ${USE_LABEL} \ 24 | --even_odd ${EVEN_ODD} \ 25 | --train_strategy ${TRAIN_STRATEGY} \ 26 | --use_input ${USE_INPUT} \ 27 | --input_drop_rate ${INPUT_DROP_RATE} \ 28 | --drop_rate ${DROP_RATE} \ 29 | --hidden_size ${HIDDEN_SIZE} \ 30 | --squash_k ${SQUASH_K} \ 31 | --num_epochs ${EPOCHS} \ 32 | --max_patience ${MAX_PATIENCE} \ 33 | --embedding_size ${EMBEDDING_SIZE} \ 34 | --use_all_feat ${ALL_FEAT} \ 35 | --output_dir outputs/${DATASET}/${METHOD}/ \ 36 | --seed ${SEED} \ 37 | --gpus ${GPU} 38 | -------------------------------------------------------------------------------- /scripts/run_DBLP.sh: -------------------------------------------------------------------------------- 1 | SEED=1 2 | GPU=0 3 | DATASET=dblp 4 | METHOD=rphgnn 5 | USE_NRL=False 6 | TRAIN_STRATEGY=common 7 | USE_INPUT=False 8 | ALL_FEAT=True 9 | INPUT_DROP_RATE=0.8 10 | DROP_RATE=0.8 11 | HIDDEN_SIZE=512 12 | SQUASH_K=5 13 | EPOCHS=500 14 | MAX_PATIENCE=30 15 | EMBEDDING_SIZE=1024 16 | USE_LABEL=False 17 | EVEN_ODD="all" 18 | 19 | python -u main_rphgnn.py \ 20 | --method ${METHOD} \ 21 | --dataset ${DATASET} \ 22 | --use_nrl ${USE_NRL} \ 23 | --use_label ${USE_LABEL} \ 24 | --even_odd ${EVEN_ODD} \ 25 | --train_strategy ${TRAIN_STRATEGY} \ 26 | --use_input ${USE_INPUT} \ 27 | --input_drop_rate ${INPUT_DROP_RATE} \ 28 | --drop_rate ${DROP_RATE} \ 29 | --hidden_size ${HIDDEN_SIZE} \ 30 | --squash_k ${SQUASH_K} \ 31 | --num_epochs ${EPOCHS} \ 32 | --max_patience ${MAX_PATIENCE} \ 33 | --embedding_size ${EMBEDDING_SIZE} \ 34 | --use_all_feat ${ALL_FEAT} \ 35 | --output_dir outputs/${DATASET}/${METHOD}/ \ 36 | --seed ${SEED} \ 37 | --gpus ${GPU} 38 | -------------------------------------------------------------------------------- /scripts/run_Freebase.sh: -------------------------------------------------------------------------------- 1 | SEED=1 2 | GPU=0 3 | DATASET=freebase 4 | METHOD=rphgnn 5 | USE_NRL=False 6 | TRAIN_STRATEGY=common 7 | USE_INPUT=False 8 | ALL_FEAT=False 9 | INPUT_DROP_RATE=0.7 10 | DROP_RATE=0.7 11 | HIDDEN_SIZE=256 12 | SQUASH_K=6 13 | EPOCHS=150 14 | MAX_PATIENCE=50 15 | EMBEDDING_SIZE=512 16 | USE_LABEL=False 17 | EVEN_ODD="all" 18 | 19 | python -u main_rphgnn.py \ 20 | --method ${METHOD} \ 21 | --dataset ${DATASET} \ 22 | --use_nrl ${USE_NRL} \ 23 | --use_label ${USE_LABEL} \ 24 | --even_odd ${EVEN_ODD} \ 25 | --train_strategy ${TRAIN_STRATEGY} \ 26 | --use_input ${USE_INPUT} \ 27 | --input_drop_rate ${INPUT_DROP_RATE} \ 28 | --drop_rate ${DROP_RATE} \ 29 | --hidden_size ${HIDDEN_SIZE} \ 30 | --squash_k ${SQUASH_K} \ 31 | --num_epochs ${EPOCHS} \ 32 | --max_patience ${MAX_PATIENCE} \ 33 | --embedding_size ${EMBEDDING_SIZE} \ 34 | --use_all_feat ${ALL_FEAT} \ 35 | --output_dir outputs/${DATASET}/${METHOD}/ \ 36 | --seed ${SEED} \ 37 | --gpus ${GPU} 38 | -------------------------------------------------------------------------------- /scripts/run_IMDB.sh: -------------------------------------------------------------------------------- 1 | SEED=1 2 | GPU=0 3 | DATASET=imdb 4 | METHOD=rphgnn 5 | USE_NRL=False 6 | TRAIN_STRATEGY=common 7 | USE_INPUT=True 8 | ALL_FEAT=True 9 | INPUT_DROP_RATE=0.8 10 | DROP_RATE=0.7 11 | HIDDEN_SIZE=256 12 | SQUASH_K=3 13 | EPOCHS=500 14 | MAX_PATIENCE=50 15 | EMBEDDING_SIZE=1024 16 | USE_LABEL=False 17 | EVEN_ODD="all" 18 | 19 | python -u main_rphgnn.py \ 20 | --method ${METHOD} \ 21 | --dataset ${DATASET} \ 22 | --use_nrl ${USE_NRL} \ 23 | --use_label ${USE_LABEL} \ 24 | --even_odd ${EVEN_ODD} \ 25 | --train_strategy ${TRAIN_STRATEGY} \ 26 | --use_input ${USE_INPUT} \ 27 | --input_drop_rate ${INPUT_DROP_RATE} \ 28 | --drop_rate ${DROP_RATE} \ 29 | --hidden_size ${HIDDEN_SIZE} \ 30 | --squash_k ${SQUASH_K} \ 31 | --num_epochs ${EPOCHS} \ 32 | --max_patience ${MAX_PATIENCE} \ 33 | --embedding_size ${EMBEDDING_SIZE} \ 34 | --use_all_feat ${ALL_FEAT} \ 35 | --output_dir outputs/${DATASET}/${METHOD}/ \ 36 | --seed ${SEED} \ 37 | --gpus ${GPU} 38 | -------------------------------------------------------------------------------- /scripts/run_OAG-L1-Field.sh: -------------------------------------------------------------------------------- 1 | SEED=0 2 | GPU=0 3 | DATASET=oag_L1 4 | METHOD=rphgnn 5 | USE_NRL=False 6 | TRAIN_STRATEGY=common 7 | USE_INPUT=False 8 | ALL_FEAT=True 9 | INPUT_DROP_RATE=0.3 10 | DROP_RATE=0.5 11 | HIDDEN_SIZE=512 12 | SQUASH_K=3 13 | EPOCHS=200 14 | MAX_PATIENCE=0 15 | EMBEDDING_SIZE=384 16 | USE_LABEL=False 17 | EVEN_ODD="all" 18 | 19 | python -u main_rphgnn.py \ 20 | --method ${METHOD} \ 21 | --dataset ${DATASET} \ 22 | --use_nrl ${USE_NRL} \ 23 | --use_label ${USE_LABEL} \ 24 | --even_odd ${EVEN_ODD} \ 25 | --train_strategy ${TRAIN_STRATEGY} \ 26 | --use_input ${USE_INPUT} \ 27 | --input_drop_rate ${INPUT_DROP_RATE} \ 28 | --drop_rate ${DROP_RATE} \ 29 | --hidden_size ${HIDDEN_SIZE} \ 30 | --squash_k ${SQUASH_K} \ 31 | --num_epochs ${EPOCHS} \ 32 | --max_patience ${MAX_PATIENCE} \ 33 | --embedding_size ${EMBEDDING_SIZE} \ 34 | --use_all_feat ${ALL_FEAT} \ 35 | --output_dir outputs/${DATASET}/${METHOD}/ \ 36 | --seed ${SEED} \ 37 | --gpus ${GPU} 38 | -------------------------------------------------------------------------------- /scripts/run_OAG-Venue.sh: -------------------------------------------------------------------------------- 1 | SEED=0 2 | GPU=0 3 | DATASET=oag_venue 4 | METHOD=rphgnn 5 | USE_NRL=False 6 | TRAIN_STRATEGY=common 7 | USE_INPUT=True 8 | ALL_FEAT=True 9 | INPUT_DROP_RATE=0.5 10 | DROP_RATE=0.5 11 | HIDDEN_SIZE=512 12 | SQUASH_K=3 13 | EPOCHS=200 14 | MAX_PATIENCE=0 15 | EMBEDDING_SIZE=256 16 | USE_LABEL=False 17 | EVEN_ODD="all" 18 | 19 | python -u main_rphgnn.py \ 20 | --method ${METHOD} \ 21 | --dataset ${DATASET} \ 22 | --use_nrl ${USE_NRL} \ 23 | --use_label ${USE_LABEL} \ 24 | --even_odd ${EVEN_ODD} \ 25 | --train_strategy ${TRAIN_STRATEGY} \ 26 | --use_input ${USE_INPUT} \ 27 | --input_drop_rate ${INPUT_DROP_RATE} \ 28 | --drop_rate ${DROP_RATE} \ 29 | --hidden_size ${HIDDEN_SIZE} \ 30 | --squash_k ${SQUASH_K} \ 31 | --num_epochs ${EPOCHS} \ 32 | --max_patience ${MAX_PATIENCE} \ 33 | --embedding_size ${EMBEDDING_SIZE} \ 34 | --use_all_feat ${ALL_FEAT} \ 35 | --output_dir outputs/${DATASET}/${METHOD}/ \ 36 | --seed ${SEED} \ 37 | --gpus ${GPU} 38 | -------------------------------------------------------------------------------- /scripts/run_OGBN-MAG.sh: -------------------------------------------------------------------------------- 1 | SEED=2 2 | GPU=0 3 | DATASET=mag 4 | METHOD=rphgnn 5 | USE_NRL=False 6 | TRAIN_STRATEGY=common 7 | USE_INPUT=True 8 | ALL_FEAT=True 9 | INPUT_DROP_RATE=0.0 10 | DROP_RATE=0.5 11 | HIDDEN_SIZE=512 12 | SQUASH_K=5 13 | EPOCHS=200 14 | MAX_PATIENCE=30 15 | EMBEDDING_SIZE=512 16 | USE_LABEL=False 17 | EVEN_ODD="all" 18 | 19 | python -u main_rphgnn.py \ 20 | --method ${METHOD} \ 21 | --dataset ${DATASET} \ 22 | --use_nrl ${USE_NRL} \ 23 | --use_label ${USE_LABEL} \ 24 | --even_odd ${EVEN_ODD} \ 25 | --train_strategy ${TRAIN_STRATEGY} \ 26 | --use_input ${USE_INPUT} \ 27 | --input_drop_rate ${INPUT_DROP_RATE} \ 28 | --drop_rate ${DROP_RATE} \ 29 | --hidden_size ${HIDDEN_SIZE} \ 30 | --squash_k ${SQUASH_K} \ 31 | --num_epochs ${EPOCHS} \ 32 | --max_patience ${MAX_PATIENCE} \ 33 | --embedding_size ${EMBEDDING_SIZE} \ 34 | --use_all_feat ${ALL_FEAT} \ 35 | --output_dir outputs/${DATASET}/${METHOD}/ \ 36 | --seed ${SEED} \ 37 | --gpus ${GPU} 38 | -------------------------------------------------------------------------------- /scripts/run_leaderboard_OGBN-MAG.sh: -------------------------------------------------------------------------------- 1 | GPU=0 2 | DATASET=mag 3 | METHOD=rphgnn 4 | USE_NRL=True 5 | TRAIN_STRATEGY=cl 6 | USE_INPUT=True 7 | ALL_FEAT=True 8 | INPUT_DROP_RATE=0.1 9 | DROP_RATE=0.4 10 | HIDDEN_SIZE=512 11 | SQUASH_K=3 12 | EPOCHS=500 13 | MAX_PATIENCE=50 14 | EMBEDDING_SIZE=512 15 | USE_LABEL=True 16 | EVEN_ODD="all" 17 | 18 | 19 | mkdir cache 20 | 21 | for SEED in $(seq 0 9) 22 | do 23 | SEED=11 24 | echo $SEED 25 | python -u main_rphgnn.py \ 26 | --dataset ${DATASET} \ 27 | --method ${METHOD} \ 28 | --use_nrl ${USE_NRL} \ 29 | --use_label ${USE_LABEL} \ 30 | --even_odd ${EVEN_ODD} \ 31 | --train_strategy ${TRAIN_STRATEGY} \ 32 | --use_input ${USE_INPUT} \ 33 | --input_drop_rate ${INPUT_DROP_RATE} \ 34 | --drop_rate ${DROP_RATE} \ 35 | --hidden_size ${HIDDEN_SIZE} \ 36 | --squash_k ${SQUASH_K} \ 37 | --num_epochs ${EPOCHS} \ 38 | --max_patience ${MAX_PATIENCE} \ 39 | --embedding_size ${EMBEDDING_SIZE} \ 40 | --use_all_feat ${ALL_FEAT} \ 41 | --output_dir outputs/leaderboard_mag/ \ 42 | --gpus ${GPU} \ 43 | --seed ${SEED} > nohup_leaderboard_mag_${SEED}.out 2>&1 44 | done 45 | --------------------------------------------------------------------------------