├── .gitignore ├── LICENSE.md ├── README.md ├── emb └── karate.emb ├── graph └── karate.edgelist ├── node2vec_spark ├── README.md ├── pom.xml └── src │ └── main │ ├── resources │ └── log4j2.properties │ └── scala │ └── com │ └── navercorp │ ├── Main.scala │ ├── Node2vec.scala │ ├── Word2vec.scala │ ├── graph │ ├── GraphOps.scala │ └── package.scala │ └── lib │ └── AbstractParams.scala ├── requirements.txt └── src ├── main.py └── node2vec.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | .DS_Store 3 | target 4 | bin 5 | build 6 | .gradle 7 | *.iml 8 | *.ipr 9 | *.iws 10 | *.log 11 | .classpath 12 | .project 13 | .settings 14 | .idea -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Aditya Grover 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # node2vec 2 | 3 | This repository provides a reference implementation of *node2vec* as described in the paper:
4 | > node2vec: Scalable Feature Learning for Networks.
5 | > Aditya Grover and Jure Leskovec.
6 | > Knowledge Discovery and Data Mining, 2016.
7 | > 8 | 9 | The *node2vec* algorithm learns continuous representations for nodes in any (un)directed, (un)weighted graph. Please check the [project page](https://snap.stanford.edu/node2vec/) for more details. 10 | 11 | ### Basic Usage 12 | 13 | #### Example 14 | To run *node2vec* on Zachary's karate club network, execute the following command from the project home directory:
15 | ``python src/main.py --input graph/karate.edgelist --output emb/karate.emd`` 16 | 17 | #### Options 18 | You can check out the other options available to use with *node2vec* using:
19 | ``python src/main.py --help`` 20 | 21 | #### Input 22 | The supported input format is an edgelist: 23 | 24 | node1_id_int node2_id_int 25 | 26 | The graph is assumed to be undirected and unweighted by default. These options can be changed by setting the appropriate flags. 27 | 28 | #### Output 29 | The output file has *n+1* lines for a graph with *n* vertices. 30 | The first line has the following format: 31 | 32 | num_of_nodes dim_of_representation 33 | 34 | The next *n* lines are as follows: 35 | 36 | node_id dim1 dim2 ... dimd 37 | 38 | where dim1, ... , dimd is the *d*-dimensional representation learned by *node2vec*. 39 | 40 | ### Citing 41 | If you find *node2vec* useful for your research, please consider citing the following paper: 42 | 43 | @inproceedings{node2vec-kdd2016, 44 | author = {Grover, Aditya and Leskovec, Jure}, 45 | title = {node2vec: Scalable Feature Learning for Networks}, 46 | booktitle = {Proceedings of the 22nd ACM SIGKDD International Conference on Knowledge Discovery and Data Mining}, 47 | year = {2016} 48 | } 49 | 50 | 51 | ### Miscellaneous 52 | 53 | Please send any questions you might have about the code and/or the algorithm to . 54 | 55 | *Note:* This is only a reference implementation of the *node2vec* algorithm and could benefit from several performance enhancement schemes, some of which are discussed in the paper. 56 | -------------------------------------------------------------------------------- /emb/karate.emb: -------------------------------------------------------------------------------- 1 | 34 128 2 | 1 -0.014876 -0.229356 0.023806 0.067841 0.090910 -0.144880 0.150130 0.075787 0.046873 -0.025290 -0.095061 0.084067 0.022556 -0.197516 0.051331 -0.241755 0.004764 -0.232580 -0.031161 -0.013199 0.233664 -0.011230 0.140365 0.096059 0.068695 -0.165448 -0.100326 -0.012106 0.011358 0.170880 -0.012065 0.044922 -0.139547 -0.037438 -0.095815 -0.138876 0.183298 0.116599 -0.055275 -0.080746 -0.100899 0.053135 0.141539 0.179031 -0.131575 0.127407 0.099880 -0.064466 -0.029267 0.157667 -0.028006 0.174557 -0.022628 0.148424 -0.174256 -0.179070 -0.059111 0.088243 0.086486 -0.033525 0.117324 -0.099924 -0.026303 0.151631 -0.063171 -0.079035 -0.073884 0.059158 0.136396 -0.156904 -0.089332 0.054849 -0.108355 -0.044886 0.131026 -0.063695 0.086190 0.014780 -0.002258 -0.120929 -0.115947 -0.029460 0.023848 -0.102442 -0.111003 0.058831 0.121057 0.008298 -0.056379 0.072060 0.048521 -0.006848 0.087435 -0.054377 -0.153402 0.207793 -0.070880 -0.107374 0.120661 -0.008031 -0.145239 0.157040 -0.122576 -0.109220 -0.100591 0.017339 0.173152 0.001466 0.015540 -0.049476 0.057062 0.019142 -0.027390 0.016272 -0.091249 -0.008336 -0.042350 0.178391 -0.066354 -0.029698 -0.038339 -0.048144 0.134590 -0.234350 -0.217189 0.074112 0.057659 0.201265 3 | 34 -0.043236 -0.184561 0.086898 0.155603 0.126673 -0.103197 0.194487 -0.020264 -0.096339 0.018373 -0.113667 0.012346 0.015797 -0.198612 -0.094771 -0.205460 0.049062 -0.081919 0.027739 0.088356 0.151644 -0.009231 0.201849 0.050836 -0.023565 -0.149574 -0.049034 -0.044401 -0.037658 0.224190 -0.010381 0.071883 -0.170195 0.034827 -0.167335 -0.075119 0.218483 0.090103 -0.035649 -0.134331 -0.168675 0.210952 0.128298 0.087990 -0.063460 0.092851 0.023417 -0.128993 -0.074394 0.129745 0.093197 0.249170 -0.001558 0.100948 -0.161581 -0.110734 -0.069279 0.041938 0.008046 0.054898 0.023925 -0.086794 -0.147930 0.155933 0.051813 -0.310828 -0.066930 -0.007734 0.062363 -0.038964 0.059932 0.004334 -0.072056 0.082316 -0.050440 -0.114736 0.039152 0.079540 0.074609 0.144051 -0.082203 -0.145513 0.004876 -0.252469 0.005756 0.245110 -0.101092 0.065562 -0.171087 0.005519 -0.005527 0.094499 0.054681 -0.044853 -0.037731 0.285537 -0.169364 0.082962 0.097452 -0.184904 -0.246170 0.201754 -0.079283 -0.180332 -0.009282 0.074115 0.221057 0.033268 0.021976 0.014238 0.086000 -0.090141 -0.070816 -0.046511 -0.025651 -0.059833 -0.086892 0.139139 -0.074664 0.109995 -0.096493 -0.132756 0.014005 -0.184265 0.008678 0.209333 0.024216 0.046068 4 | 33 -0.037151 -0.193182 0.077220 0.145653 0.123061 -0.110840 0.185281 -0.003266 -0.074293 0.013228 -0.108248 0.023448 0.017491 -0.203065 -0.068931 -0.211348 0.036744 -0.099373 0.013532 0.072291 0.169995 -0.009909 0.193386 0.056882 -0.015445 -0.150086 -0.060747 -0.037020 -0.029384 0.216726 -0.015124 0.072654 -0.162907 0.029320 -0.163866 -0.086833 0.208782 0.090374 -0.040612 -0.127639 -0.159738 0.193453 0.128516 0.092407 -0.070034 0.102198 0.026696 -0.120459 -0.073511 0.130051 0.074555 0.242593 0.000266 0.103154 -0.166959 -0.113425 -0.069956 0.046228 0.019883 0.047479 0.043045 -0.096340 -0.130470 0.151841 0.041735 -0.279075 -0.063606 0.000315 0.068115 -0.050509 0.045616 0.007611 -0.069524 0.064300 -0.031539 -0.102848 0.052158 0.066086 0.064956 0.109075 -0.078587 -0.132586 0.004139 -0.232968 -0.010310 0.221729 -0.075292 0.054786 -0.151444 0.012827 0.007983 0.083758 0.057488 -0.049456 -0.048113 0.276285 -0.158635 0.053341 0.102118 -0.161438 -0.231241 0.187751 -0.082484 -0.163907 -0.014804 0.060307 0.209479 0.030831 0.023088 0.002885 0.075017 -0.076178 -0.068085 -0.047820 -0.030464 -0.050482 -0.082474 0.133255 -0.076936 0.086339 -0.085110 -0.118404 0.027944 -0.186448 -0.009368 0.183909 0.023062 0.057413 5 | 3 -0.028561 -0.205411 0.056815 0.105204 0.107464 -0.127154 0.173082 0.029493 -0.026830 -0.005606 -0.101965 0.047164 0.018824 -0.210376 -0.017246 -0.231535 0.027110 -0.170617 -0.010041 0.031033 0.197806 -0.006828 0.168036 0.082497 0.027082 -0.157874 -0.080517 -0.019565 -0.000477 0.203291 -0.015622 0.058068 -0.156901 0.000501 -0.131919 -0.116712 0.199355 0.106443 -0.044448 -0.103124 -0.131104 0.115538 0.131251 0.140924 -0.098906 0.113108 0.063865 -0.097658 -0.058951 0.142671 0.027667 0.202568 -0.018042 0.131458 -0.174052 -0.145032 -0.066079 0.062356 0.048391 0.014275 0.072580 -0.089248 -0.084891 0.152485 -0.006358 -0.189735 -0.068464 0.033716 0.102633 -0.109285 -0.020809 0.035645 -0.092487 0.013135 0.052743 -0.089519 0.070987 0.045890 0.035846 -0.001593 -0.098935 -0.083680 0.012137 -0.175651 -0.054943 0.149670 0.019547 0.031285 -0.114596 0.042073 0.024935 0.042274 0.071669 -0.057264 -0.105576 0.250713 -0.121465 -0.015699 0.109578 -0.090905 -0.194813 0.173052 -0.108581 -0.135182 -0.051883 0.047640 0.190100 0.018619 0.022392 -0.026230 0.077392 -0.031160 -0.044318 -0.011937 -0.059609 -0.030396 -0.066347 0.157975 -0.064017 0.032325 -0.066449 -0.089884 0.073737 -0.207898 -0.113919 0.137631 0.044525 0.131904 6 | 2 -0.014195 -0.206891 0.039293 0.085593 0.095233 -0.124536 0.156750 0.051983 0.001926 -0.015428 -0.094487 0.068283 0.022594 -0.197800 0.013198 -0.227624 0.008343 -0.180030 -0.019804 0.011482 0.205034 -0.006173 0.155285 0.082322 0.037030 -0.151400 -0.087726 -0.019770 0.005120 0.180771 -0.010223 0.048888 -0.152389 -0.009914 -0.112808 -0.117258 0.191241 0.099122 -0.047605 -0.088254 -0.111873 0.091852 0.139276 0.156382 -0.111017 0.116605 0.073830 -0.084164 -0.043307 0.137957 0.005370 0.179475 -0.012714 0.137657 -0.170627 -0.155057 -0.059229 0.074431 0.059581 -0.005058 0.088855 -0.090385 -0.056987 0.144462 -0.028853 -0.128125 -0.068681 0.038364 0.110911 -0.127508 -0.048368 0.043526 -0.091543 -0.015820 0.086858 -0.069705 0.077332 0.030236 0.014120 -0.050888 -0.104370 -0.061443 0.009650 -0.136330 -0.077768 0.111878 0.061145 0.022060 -0.081506 0.053774 0.040389 0.023848 0.076200 -0.059771 -0.129641 0.226712 -0.095737 -0.059849 0.105248 -0.044966 -0.162578 0.155537 -0.106955 -0.124103 -0.077882 0.022313 0.180785 0.018175 0.012676 -0.037498 0.061517 -0.005846 -0.034860 -0.004758 -0.074445 -0.020110 -0.054517 0.165222 -0.071541 0.005364 -0.052736 -0.073605 0.103373 -0.216421 -0.150806 0.105295 0.055725 0.159106 7 | 4 -0.014641 -0.203711 0.034961 0.085020 0.095704 -0.131868 0.150660 0.056755 0.011886 -0.016651 -0.089766 0.067894 0.024813 -0.194787 0.020632 -0.224535 0.010860 -0.193962 -0.023700 0.008739 0.211455 -0.009271 0.145376 0.082361 0.047274 -0.151999 -0.089413 -0.014303 0.007932 0.174257 -0.014606 0.045983 -0.143194 -0.019511 -0.106699 -0.121798 0.188384 0.107257 -0.049663 -0.084457 -0.107101 0.077382 0.134446 0.157648 -0.108230 0.113253 0.079461 -0.079183 -0.043650 0.138382 0.001375 0.174463 -0.015088 0.134528 -0.166295 -0.153995 -0.057372 0.071027 0.069189 -0.016081 0.099031 -0.091601 -0.048610 0.142226 -0.029463 -0.114172 -0.072477 0.043485 0.114788 -0.127927 -0.054399 0.046114 -0.091841 -0.023762 0.094924 -0.068927 0.077757 0.029653 0.009168 -0.056918 -0.098812 -0.055162 0.015343 -0.122034 -0.078487 0.094937 0.074184 0.015670 -0.075195 0.058859 0.034632 0.014311 0.071581 -0.055793 -0.125368 0.216919 -0.088715 -0.066234 0.104393 -0.041869 -0.155419 0.150385 -0.102527 -0.107840 -0.081059 0.026027 0.165621 0.010435 0.014196 -0.037497 0.064977 -0.004085 -0.032967 0.003786 -0.069743 -0.013293 -0.051573 0.158915 -0.062837 -0.001615 -0.044520 -0.061123 0.099564 -0.209667 -0.159169 0.095354 0.052871 0.161271 8 | 32 -0.028685 -0.213070 0.047301 0.108818 0.109415 -0.128147 0.176653 0.027303 -0.025159 -0.004165 -0.105345 0.053321 0.018802 -0.204727 -0.014004 -0.228356 0.026948 -0.172561 -0.012788 0.035630 0.203829 -0.004683 0.168709 0.078730 0.022190 -0.164596 -0.086578 -0.023510 -0.008559 0.203468 -0.010573 0.059740 -0.148701 -0.003012 -0.134304 -0.112708 0.203686 0.109460 -0.050573 -0.109072 -0.136512 0.126445 0.128359 0.133714 -0.103106 0.115873 0.066037 -0.090537 -0.055603 0.139785 0.027898 0.210594 -0.017086 0.130375 -0.174136 -0.149086 -0.063542 0.062951 0.050607 0.008530 0.075894 -0.092578 -0.084852 0.157976 -0.014030 -0.184613 -0.072039 0.032966 0.101139 -0.104970 -0.026077 0.038771 -0.092542 0.011978 0.047402 -0.082716 0.062619 0.046412 0.032246 0.000488 -0.098291 -0.080592 0.019979 -0.166354 -0.054311 0.148484 0.019857 0.032152 -0.108305 0.035798 0.024141 0.041786 0.075691 -0.055285 -0.095965 0.244279 -0.114245 -0.016270 0.102579 -0.095522 -0.187384 0.167964 -0.106744 -0.134279 -0.050422 0.048754 0.190782 0.013687 0.018321 -0.018169 0.071399 -0.032380 -0.043966 -0.010604 -0.056195 -0.028461 -0.064954 0.154093 -0.059146 0.030605 -0.064815 -0.091060 0.073829 -0.205478 -0.104267 0.131276 0.034881 0.128345 9 | 24 -0.029315 -0.189772 0.068621 0.134244 0.113263 -0.111526 0.179366 0.006049 -0.055758 0.008379 -0.101671 0.030253 0.020045 -0.200031 -0.051633 -0.212913 0.031469 -0.122085 0.007588 0.056852 0.173856 -0.008787 0.179263 0.067520 0.002459 -0.148246 -0.065914 -0.037638 -0.025133 0.208168 -0.016622 0.063915 -0.162850 0.012209 -0.148385 -0.094801 0.212578 0.094363 -0.044566 -0.125276 -0.148900 0.172375 0.128087 0.110670 -0.080469 0.106983 0.040901 -0.105512 -0.063393 0.132020 0.064159 0.230891 -0.009803 0.113915 -0.160158 -0.123251 -0.063602 0.050361 0.029889 0.029696 0.051962 -0.091280 -0.110055 0.153433 0.019818 -0.245125 -0.068114 0.014500 0.076046 -0.066689 0.022533 0.020194 -0.083352 0.046498 -0.001833 -0.098371 0.049496 0.056079 0.051472 0.077022 -0.081356 -0.108193 0.010358 -0.201614 -0.025917 0.187518 -0.045225 0.052643 -0.135496 0.023657 0.010510 0.068199 0.063236 -0.049041 -0.063124 0.265099 -0.134215 0.031199 0.096965 -0.135953 -0.212055 0.182376 -0.084096 -0.150302 -0.031544 0.058467 0.196833 0.024433 0.022537 0.000038 0.078107 -0.060734 -0.059215 -0.031952 -0.043136 -0.043312 -0.075321 0.136955 -0.064975 0.072500 -0.075805 -0.107042 0.043740 -0.190984 -0.039007 0.166575 0.030490 0.083785 10 | 14 -0.027129 -0.198493 0.061423 0.105227 0.107909 -0.116048 0.165620 0.021164 -0.039371 0.000798 -0.105297 0.046454 0.020127 -0.201215 -0.025640 -0.218301 0.026254 -0.145145 -0.003993 0.036285 0.189837 -0.001548 0.169266 0.074046 0.012566 -0.145471 -0.070417 -0.019415 -0.011722 0.199796 -0.013031 0.061321 -0.155937 0.010501 -0.129864 -0.105259 0.194787 0.093700 -0.043040 -0.106005 -0.136448 0.129663 0.130622 0.132160 -0.088718 0.109683 0.051199 -0.096384 -0.058666 0.135832 0.033188 0.202058 -0.010150 0.124697 -0.166948 -0.134049 -0.066910 0.061093 0.035829 0.016422 0.067523 -0.093323 -0.088033 0.141622 0.001714 -0.188714 -0.062100 0.020227 0.088135 -0.091097 -0.010059 0.031509 -0.082984 0.023107 0.036634 -0.082443 0.064717 0.047751 0.036376 0.020414 -0.091274 -0.092302 0.010257 -0.170598 -0.046212 0.162372 -0.004781 0.033150 -0.109946 0.033680 0.021587 0.046615 0.063440 -0.057116 -0.089360 0.243070 -0.123019 0.000268 0.100286 -0.097457 -0.190594 0.162703 -0.092669 -0.130177 -0.047080 0.045170 0.186444 0.027099 0.014230 -0.019465 0.063650 -0.042349 -0.049969 -0.016807 -0.047529 -0.034889 -0.069347 0.142300 -0.068355 0.042539 -0.065908 -0.092345 0.059285 -0.191807 -0.077834 0.134570 0.036721 0.108272 11 | 30 -0.029805 -0.195961 0.067188 0.138667 0.112269 -0.110647 0.186154 -0.000130 -0.064409 0.011206 -0.110666 0.029180 0.017283 -0.202088 -0.059766 -0.216968 0.035102 -0.117452 0.007007 0.065951 0.173340 -0.011271 0.187594 0.059655 -0.000878 -0.153483 -0.060203 -0.036643 -0.028904 0.212611 -0.014515 0.062677 -0.160968 0.015566 -0.153099 -0.088347 0.209994 0.097673 -0.040055 -0.127585 -0.150775 0.176175 0.128623 0.109289 -0.077416 0.104198 0.038445 -0.114737 -0.066683 0.134714 0.065543 0.233909 -0.003867 0.105055 -0.159656 -0.122573 -0.070121 0.050437 0.028871 0.034093 0.049062 -0.093662 -0.113433 0.153901 0.031128 -0.253415 -0.068951 0.007318 0.077145 -0.065763 0.028671 0.015899 -0.078109 0.049580 -0.008314 -0.102949 0.051671 0.064192 0.055459 0.087741 -0.088441 -0.117364 0.004683 -0.212010 -0.019745 0.199981 -0.053953 0.052947 -0.143431 0.021250 0.012143 0.067632 0.060727 -0.045437 -0.058646 0.265112 -0.144213 0.040904 0.099171 -0.143674 -0.210708 0.183055 -0.085558 -0.160468 -0.027837 0.057454 0.201077 0.025756 0.017544 -0.000789 0.073068 -0.067587 -0.063775 -0.036334 -0.034737 -0.045930 -0.079362 0.137832 -0.069735 0.079231 -0.082655 -0.107603 0.038808 -0.186957 -0.032019 0.173386 0.033657 0.076414 12 | 6 0.002078 -0.268463 -0.003595 0.061690 0.088494 -0.169720 0.158606 0.118915 0.104116 -0.043795 -0.095630 0.123569 0.022248 -0.218749 0.115644 -0.277518 -0.014723 -0.313166 -0.060689 -0.047875 0.288100 -0.020190 0.132303 0.118331 0.117887 -0.200003 -0.132013 -0.017603 0.019659 0.177378 -0.006264 0.034392 -0.144590 -0.073253 -0.077074 -0.172127 0.207134 0.149657 -0.083728 -0.071973 -0.090280 0.020017 0.167903 0.226158 -0.175951 0.159062 0.152982 -0.053364 -0.014949 0.189750 -0.065455 0.174992 -0.030634 0.172406 -0.186966 -0.233308 -0.063218 0.106419 0.135712 -0.083162 0.167874 -0.111255 0.019922 0.172214 -0.106326 -0.006885 -0.089321 0.084059 0.181457 -0.217192 -0.148822 0.081893 -0.144617 -0.089499 0.217508 -0.047376 0.106679 -0.000777 -0.035308 -0.219362 -0.139127 0.023513 0.039169 -0.045416 -0.159940 -0.008134 0.218900 -0.008446 -0.026698 0.104784 0.072880 -0.043530 0.111360 -0.052411 -0.210585 0.205173 -0.034588 -0.189699 0.137564 0.055408 -0.113309 0.151570 -0.151678 -0.094919 -0.158202 -0.006576 0.163549 -0.015275 0.019636 -0.075174 0.055843 0.066792 -0.019707 0.035878 -0.116778 0.019480 -0.031952 0.205222 -0.063552 -0.076776 -0.017083 -0.022952 0.203400 -0.281455 -0.323782 0.030069 0.078347 0.272427 13 | 28 -0.031933 -0.197667 0.064274 0.115887 0.109673 -0.112831 0.172487 0.015226 -0.045440 0.000922 -0.100285 0.041999 0.019360 -0.195612 -0.036887 -0.218494 0.025783 -0.140635 0.001926 0.047809 0.179597 -0.007532 0.172192 0.069171 0.007049 -0.146625 -0.074777 -0.025957 -0.010022 0.198732 -0.015366 0.065234 -0.151395 0.012260 -0.137601 -0.103174 0.199766 0.096171 -0.039490 -0.110485 -0.138451 0.140136 0.128419 0.120470 -0.087535 0.103411 0.041446 -0.101929 -0.063897 0.129588 0.039896 0.211609 -0.006230 0.115810 -0.161193 -0.129435 -0.065928 0.050153 0.031238 0.023657 0.060655 -0.092088 -0.094960 0.147079 0.012121 -0.212376 -0.067530 0.022987 0.080594 -0.086674 0.001573 0.025931 -0.081351 0.024362 0.018821 -0.089987 0.060000 0.054546 0.038437 0.043187 -0.088256 -0.103463 0.010522 -0.187656 -0.037527 0.173049 -0.014719 0.041709 -0.121271 0.031382 0.019447 0.054744 0.061988 -0.056360 -0.078127 0.246409 -0.128369 0.010664 0.099073 -0.115142 -0.199420 0.175094 -0.094355 -0.143670 -0.035543 0.051635 0.192875 0.027025 0.020782 -0.017472 0.072300 -0.045097 -0.055823 -0.026098 -0.047309 -0.039429 -0.071946 0.141525 -0.068355 0.048830 -0.069811 -0.097455 0.053574 -0.189748 -0.071610 0.145047 0.036588 0.100697 14 | 9 -0.028741 -0.211076 0.054543 0.113454 0.112192 -0.125957 0.179462 0.027106 -0.028922 -0.004419 -0.107562 0.053037 0.021575 -0.217922 -0.024299 -0.237413 0.024300 -0.160490 -0.008231 0.041769 0.199263 -0.003726 0.183270 0.076422 0.018500 -0.159869 -0.083470 -0.031711 -0.011421 0.213216 -0.012059 0.066545 -0.164235 0.000059 -0.135374 -0.114367 0.211365 0.103426 -0.050803 -0.107760 -0.144874 0.131460 0.138487 0.143084 -0.099800 0.116327 0.059543 -0.104551 -0.061709 0.139687 0.031561 0.216445 -0.014551 0.132551 -0.174128 -0.147784 -0.069049 0.061083 0.044929 0.013560 0.073751 -0.095200 -0.094416 0.159048 0.001244 -0.203643 -0.068519 0.027594 0.098585 -0.101190 -0.011652 0.036458 -0.086973 0.017561 0.045833 -0.092934 0.063652 0.055411 0.034048 0.017822 -0.095928 -0.093888 0.008178 -0.187700 -0.052890 0.162931 0.005551 0.035677 -0.122494 0.039770 0.026802 0.046470 0.076745 -0.054114 -0.100411 0.253836 -0.121971 -0.008027 0.105489 -0.097454 -0.201514 0.171386 -0.106460 -0.139697 -0.050451 0.044910 0.193437 0.025752 0.016820 -0.024537 0.076425 -0.037152 -0.045500 -0.020131 -0.057735 -0.035467 -0.071962 0.158357 -0.070841 0.044458 -0.070259 -0.096717 0.068233 -0.206222 -0.099504 0.146867 0.042443 0.123567 15 | 8 -0.017865 -0.211108 0.047087 0.089959 0.095930 -0.129200 0.162559 0.046570 -0.004375 -0.008637 -0.098684 0.061683 0.021941 -0.204175 0.012324 -0.233112 0.015407 -0.184187 -0.015316 0.016029 0.207185 -0.006227 0.154479 0.078815 0.037447 -0.147990 -0.084808 -0.012550 0.005331 0.188156 -0.016680 0.052601 -0.150952 -0.007804 -0.117620 -0.122416 0.198871 0.103815 -0.043373 -0.089026 -0.118659 0.092678 0.133247 0.160776 -0.106863 0.122023 0.070368 -0.087064 -0.050659 0.143238 0.005149 0.183299 -0.011017 0.141231 -0.177581 -0.152867 -0.065982 0.068455 0.058193 -0.006387 0.090205 -0.095814 -0.063365 0.149157 -0.020920 -0.143041 -0.067499 0.037596 0.111694 -0.126333 -0.047938 0.041035 -0.095825 -0.012171 0.082612 -0.074008 0.073319 0.033771 0.017426 -0.041337 -0.098758 -0.070711 0.009757 -0.141484 -0.076250 0.121256 0.057527 0.022052 -0.087179 0.051636 0.038312 0.022068 0.071565 -0.062129 -0.121745 0.227285 -0.099703 -0.054338 0.100271 -0.059531 -0.161960 0.151497 -0.109298 -0.116800 -0.066790 0.026844 0.177323 0.011312 0.014639 -0.038243 0.063205 -0.009735 -0.033455 0.000628 -0.073667 -0.017436 -0.058714 0.157400 -0.066595 0.007332 -0.054345 -0.075661 0.096529 -0.204896 -0.138533 0.104377 0.054124 0.149600 16 | 7 0.004536 -0.254080 -0.005178 0.057268 0.083031 -0.160793 0.152251 0.109620 0.097156 -0.041831 -0.095120 0.116381 0.023259 -0.211618 0.107966 -0.263637 -0.010155 -0.292228 -0.062469 -0.041445 0.275748 -0.017947 0.131967 0.117549 0.107937 -0.190531 -0.131160 -0.015198 0.016388 0.167363 -0.002318 0.040055 -0.140953 -0.069910 -0.079856 -0.160896 0.193364 0.132179 -0.076877 -0.067679 -0.087655 0.021391 0.153731 0.214042 -0.160299 0.151596 0.140628 -0.047208 -0.016141 0.176074 -0.061066 0.166253 -0.026844 0.168321 -0.184576 -0.212191 -0.057258 0.100167 0.125962 -0.066334 0.153393 -0.106310 0.015351 0.163925 -0.100897 -0.008578 -0.081105 0.081465 0.172725 -0.203431 -0.141983 0.080377 -0.138018 -0.082646 0.200963 -0.042303 0.096555 -0.001276 -0.029828 -0.206510 -0.126383 0.019984 0.033506 -0.044342 -0.150970 0.000560 0.194671 -0.005433 -0.028069 0.098008 0.067910 -0.039512 0.106683 -0.054327 -0.194675 0.199312 -0.033676 -0.172281 0.121926 0.047836 -0.112250 0.145785 -0.146147 -0.089646 -0.142902 -0.010297 0.153126 -0.016220 0.020992 -0.066057 0.052915 0.063302 -0.019462 0.027044 -0.111650 0.011834 -0.033692 0.189723 -0.062758 -0.066532 -0.020178 -0.020366 0.188008 -0.256947 -0.294357 0.027996 0.071474 0.257231 17 | 11 -0.011104 -0.238662 0.017651 0.075369 0.092326 -0.150237 0.153379 0.086072 0.059588 -0.030736 -0.095822 0.094182 0.019264 -0.201241 0.068715 -0.252125 -0.001463 -0.247265 -0.041062 -0.019052 0.248472 -0.015812 0.142417 0.101158 0.078408 -0.176218 -0.110534 -0.015693 0.006325 0.172081 -0.007481 0.042705 -0.139252 -0.048455 -0.091122 -0.140135 0.198064 0.126991 -0.064129 -0.073303 -0.096557 0.056212 0.147887 0.185500 -0.139213 0.137412 0.115886 -0.059514 -0.031032 0.163974 -0.028957 0.175716 -0.019992 0.148652 -0.174109 -0.192077 -0.060479 0.089457 0.094409 -0.044620 0.125362 -0.103718 -0.012229 0.158348 -0.069101 -0.065849 -0.080717 0.060038 0.149384 -0.170787 -0.098626 0.059951 -0.119967 -0.054091 0.151870 -0.056308 0.087860 0.008745 -0.011980 -0.135536 -0.113264 -0.013711 0.025772 -0.088830 -0.123062 0.048005 0.141393 0.006782 -0.051220 0.078137 0.050331 -0.016018 0.092582 -0.048781 -0.161419 0.206455 -0.058673 -0.120110 0.115591 0.007645 -0.128734 0.152522 -0.128197 -0.101811 -0.113094 0.007255 0.165345 -0.009922 0.018239 -0.054996 0.055853 0.032588 -0.030402 0.013102 -0.091771 -0.001437 -0.045370 0.179492 -0.062742 -0.036980 -0.031203 -0.044651 0.151537 -0.240748 -0.230398 0.058123 0.057474 0.209633 18 | 26 -0.023020 -0.198035 0.052784 0.110189 0.107711 -0.121333 0.171473 0.021895 -0.032644 -0.004038 -0.101837 0.043534 0.021820 -0.197700 -0.022046 -0.215946 0.026357 -0.152653 -0.007251 0.039833 0.193019 -0.007593 0.170483 0.074832 0.018060 -0.156527 -0.080434 -0.030780 -0.016583 0.199059 -0.008473 0.058927 -0.147682 -0.004470 -0.132989 -0.106190 0.197362 0.098274 -0.048066 -0.107485 -0.132600 0.128865 0.123421 0.123282 -0.093707 0.113453 0.053405 -0.093328 -0.056185 0.139182 0.038118 0.209039 -0.012684 0.115021 -0.161031 -0.138554 -0.060245 0.059080 0.041824 0.012730 0.069979 -0.091850 -0.083236 0.151016 -0.000600 -0.187181 -0.064777 0.020779 0.092040 -0.088948 -0.010133 0.029883 -0.087822 0.018203 0.037744 -0.086909 0.054650 0.046275 0.033388 0.017380 -0.092644 -0.080701 0.014382 -0.165403 -0.043736 0.150511 -0.000230 0.035232 -0.110608 0.033784 0.020769 0.043880 0.070284 -0.044217 -0.083884 0.235181 -0.114249 -0.003872 0.101984 -0.097426 -0.181580 0.163574 -0.092290 -0.129696 -0.045518 0.044875 0.183125 0.017928 0.022999 -0.011724 0.063166 -0.037035 -0.048019 -0.016431 -0.049066 -0.030906 -0.062725 0.139018 -0.059069 0.036730 -0.063343 -0.088477 0.062320 -0.195873 -0.085636 0.136442 0.033857 0.105298 19 | 5 -0.004594 -0.243196 0.005785 0.065577 0.090126 -0.153967 0.154449 0.088095 0.073435 -0.034911 -0.090112 0.105713 0.019047 -0.202371 0.080980 -0.256649 -0.007039 -0.264985 -0.047738 -0.023556 0.256849 -0.011095 0.131734 0.108428 0.090315 -0.180211 -0.114032 -0.017658 0.012227 0.170835 -0.010182 0.037963 -0.142614 -0.050532 -0.080946 -0.151261 0.194782 0.130602 -0.067567 -0.069304 -0.087982 0.037704 0.145634 0.197020 -0.148580 0.139400 0.123792 -0.058108 -0.025629 0.162819 -0.041917 0.169209 -0.018981 0.148528 -0.171418 -0.200696 -0.057636 0.091955 0.108952 -0.060386 0.133370 -0.104544 -0.001118 0.159968 -0.081236 -0.038192 -0.073945 0.064121 0.156134 -0.179419 -0.115688 0.067210 -0.123707 -0.069146 0.169208 -0.053796 0.087754 0.002529 -0.021773 -0.162555 -0.120582 -0.002197 0.026520 -0.068466 -0.132423 0.023636 0.163203 0.004269 -0.038202 0.083627 0.058034 -0.025884 0.094432 -0.049474 -0.176491 0.197759 -0.052375 -0.139631 0.121160 0.028213 -0.123731 0.142645 -0.131270 -0.097683 -0.127133 0.000257 0.157086 -0.010924 0.011315 -0.059939 0.049982 0.040890 -0.020221 0.020389 -0.098609 0.009737 -0.034123 0.182243 -0.056289 -0.046906 -0.025897 -0.031945 0.160402 -0.243433 -0.254963 0.048785 0.060837 0.227462 20 | 20 -0.018263 -0.203119 0.044032 0.096708 0.093959 -0.128945 0.162148 0.043488 -0.007986 -0.009540 -0.096424 0.056310 0.026605 -0.195246 0.002651 -0.219970 0.010107 -0.169680 -0.017086 0.016119 0.202129 -0.011583 0.155043 0.076787 0.035868 -0.145195 -0.074411 -0.020829 0.000230 0.177906 -0.018463 0.050058 -0.145409 -0.009500 -0.112073 -0.111484 0.195414 0.098849 -0.049618 -0.097075 -0.119716 0.099080 0.128173 0.144992 -0.099394 0.113066 0.072445 -0.084550 -0.045729 0.134319 0.009104 0.186786 -0.007951 0.121093 -0.163520 -0.145787 -0.061469 0.061240 0.054717 -0.003879 0.090869 -0.089566 -0.062849 0.143736 -0.016331 -0.146340 -0.067350 0.033961 0.102259 -0.106254 -0.036726 0.037258 -0.089962 -0.005363 0.069195 -0.070990 0.071186 0.033198 0.022662 -0.022449 -0.093300 -0.071684 0.012518 -0.146311 -0.064636 0.120725 0.039939 0.025594 -0.091103 0.050819 0.034654 0.027948 0.068914 -0.054067 -0.108076 0.222217 -0.094144 -0.042908 0.096560 -0.056561 -0.162761 0.147145 -0.100650 -0.112914 -0.066399 0.030850 0.167822 0.012409 0.018386 -0.025553 0.059479 -0.014568 -0.037243 -0.002874 -0.064027 -0.021660 -0.052850 0.151709 -0.062739 0.015709 -0.051125 -0.065736 0.086524 -0.197463 -0.121859 0.108248 0.041986 0.136731 21 | 29 -0.029217 -0.192326 0.054517 0.112169 0.109076 -0.116282 0.168307 0.012174 -0.039994 0.001809 -0.098568 0.043412 0.016036 -0.194299 -0.030963 -0.214521 0.024745 -0.142613 0.001282 0.044642 0.182777 -0.008179 0.165616 0.068198 0.007098 -0.151797 -0.073324 -0.030752 -0.013393 0.199615 -0.007267 0.059101 -0.145442 0.004420 -0.136301 -0.096027 0.197822 0.096257 -0.041719 -0.111380 -0.130995 0.141976 0.118482 0.111795 -0.082441 0.109905 0.046891 -0.095926 -0.059965 0.130532 0.041412 0.206435 -0.013023 0.116034 -0.163667 -0.124636 -0.056119 0.056049 0.037650 0.022077 0.060789 -0.090658 -0.095441 0.145759 0.002065 -0.204009 -0.069661 0.018610 0.086614 -0.079012 -0.004151 0.026184 -0.085070 0.029903 0.024578 -0.082741 0.056755 0.050838 0.039933 0.035330 -0.089804 -0.088362 0.014101 -0.179165 -0.036571 0.161763 -0.008797 0.033754 -0.115875 0.024550 0.017247 0.047270 0.064382 -0.047846 -0.073251 0.242764 -0.122172 0.010656 0.094378 -0.111195 -0.190514 0.167477 -0.090849 -0.132405 -0.037286 0.048096 0.180554 0.017383 0.021162 -0.010220 0.072079 -0.041447 -0.053255 -0.023962 -0.050160 -0.038334 -0.069232 0.134364 -0.059308 0.047420 -0.068231 -0.094575 0.052337 -0.190503 -0.066888 0.144212 0.031240 0.094808 22 | 25 -0.035282 -0.206988 0.058312 0.120805 0.111692 -0.122058 0.177212 0.011913 -0.038791 0.000927 -0.108893 0.039183 0.017992 -0.200517 -0.036279 -0.230321 0.033777 -0.150954 0.003472 0.053558 0.195164 -0.010270 0.183469 0.076504 0.012123 -0.165208 -0.076565 -0.032481 -0.022911 0.213152 -0.007764 0.065099 -0.152898 0.001475 -0.147454 -0.099935 0.198991 0.104754 -0.047584 -0.118899 -0.147034 0.153900 0.125362 0.112688 -0.085567 0.114880 0.050960 -0.097374 -0.057590 0.140256 0.042312 0.225302 -0.010500 0.123003 -0.172205 -0.137021 -0.060565 0.052122 0.037197 0.026949 0.059353 -0.090034 -0.097365 0.150803 0.002949 -0.221907 -0.071696 0.022900 0.083551 -0.083490 0.000902 0.022435 -0.083410 0.035583 0.017835 -0.096991 0.054303 0.057029 0.046537 0.039250 -0.092136 -0.098973 0.014844 -0.194283 -0.038230 0.170998 -0.015587 0.034901 -0.129690 0.023305 0.011655 0.053895 0.073493 -0.053029 -0.081261 0.257914 -0.129617 0.012464 0.105111 -0.118070 -0.207143 0.181815 -0.098697 -0.145136 -0.036175 0.052328 0.198538 0.023094 0.025383 -0.012243 0.072062 -0.047573 -0.054932 -0.028930 -0.052588 -0.039808 -0.068874 0.144699 -0.067824 0.051959 -0.071644 -0.097496 0.054996 -0.205749 -0.075276 0.158864 0.029401 0.100663 23 | 31 -0.024721 -0.196744 0.058380 0.113582 0.105830 -0.121297 0.167197 0.019532 -0.034359 0.000843 -0.097467 0.050559 0.023386 -0.199146 -0.025068 -0.221343 0.019521 -0.144300 -0.002430 0.040385 0.189624 -0.010693 0.165790 0.073749 0.016852 -0.147830 -0.069561 -0.021950 -0.011725 0.194129 -0.016407 0.062914 -0.153591 0.008004 -0.129214 -0.098710 0.194018 0.095890 -0.038745 -0.108933 -0.131078 0.135748 0.126969 0.121269 -0.088126 0.106741 0.049527 -0.099419 -0.056658 0.134461 0.031972 0.206586 -0.006793 0.116740 -0.162558 -0.134426 -0.061724 0.054186 0.037368 0.011571 0.070313 -0.091875 -0.092170 0.139323 0.001843 -0.195481 -0.071051 0.018516 0.087994 -0.088950 -0.006768 0.022944 -0.078728 0.019804 0.034330 -0.084489 0.066822 0.045952 0.039824 0.025499 -0.089017 -0.093305 0.008225 -0.178437 -0.045249 0.156472 -0.003366 0.031600 -0.112111 0.031002 0.017235 0.050212 0.068844 -0.052336 -0.091898 0.240929 -0.123371 -0.004094 0.099499 -0.095697 -0.192394 0.166625 -0.092753 -0.139534 -0.047868 0.042766 0.186704 0.021796 0.018163 -0.016969 0.065114 -0.043754 -0.051935 -0.024208 -0.049579 -0.030092 -0.069234 0.149659 -0.071942 0.045299 -0.067596 -0.091622 0.060503 -0.197020 -0.083051 0.139514 0.033104 0.107531 24 | 22 -0.013324 -0.230635 0.030984 0.088591 0.098398 -0.146396 0.163462 0.062319 0.023642 -0.020648 -0.099384 0.080721 0.024131 -0.211143 0.037357 -0.247551 0.004881 -0.220844 -0.026082 0.005365 0.235157 -0.011633 0.157542 0.093512 0.056209 -0.167003 -0.100317 -0.023583 0.006637 0.191703 -0.011954 0.047766 -0.152447 -0.027922 -0.108449 -0.136209 0.200194 0.117877 -0.060827 -0.089997 -0.109545 0.076386 0.139793 0.174219 -0.123702 0.133703 0.092394 -0.075265 -0.038618 0.153266 -0.011535 0.186323 -0.021598 0.141207 -0.172176 -0.175036 -0.060644 0.081859 0.082517 -0.028496 0.111178 -0.097627 -0.040972 0.160566 -0.045475 -0.110720 -0.077981 0.050738 0.134783 -0.140888 -0.071112 0.053822 -0.108662 -0.030518 0.117828 -0.066935 0.077736 0.026950 0.008733 -0.083885 -0.111567 -0.043562 0.023202 -0.124658 -0.096351 0.084337 0.097076 0.012733 -0.073115 0.064600 0.044382 0.006441 0.085194 -0.057568 -0.141985 0.225707 -0.083323 -0.081665 0.116622 -0.032448 -0.156502 0.157640 -0.117900 -0.110662 -0.090711 0.023899 0.173482 0.005736 0.018212 -0.042824 0.065584 0.007001 -0.030511 0.008229 -0.081783 -0.010709 -0.054062 0.172114 -0.066413 -0.011092 -0.042839 -0.063262 0.123231 -0.230025 -0.185480 0.091342 0.055049 0.181622 25 | 18 -0.015417 -0.214277 0.033568 0.083404 0.097872 -0.136150 0.154404 0.056222 0.014330 -0.011620 -0.096729 0.072457 0.026223 -0.203257 0.023977 -0.237472 0.008288 -0.197167 -0.026483 0.010702 0.221724 -0.008589 0.155733 0.087548 0.049251 -0.153534 -0.090079 -0.016234 0.008239 0.184684 -0.013617 0.052859 -0.144751 -0.016284 -0.107096 -0.126905 0.189289 0.107405 -0.049989 -0.084131 -0.112761 0.084732 0.130879 0.158727 -0.113563 0.124991 0.077743 -0.078021 -0.041654 0.141569 -0.004045 0.177146 -0.013704 0.141408 -0.172169 -0.155901 -0.059869 0.069247 0.066292 -0.015319 0.100727 -0.096667 -0.051426 0.146661 -0.030962 -0.120108 -0.069545 0.041643 0.121472 -0.126646 -0.059835 0.045643 -0.093986 -0.024604 0.099842 -0.067088 0.080691 0.030676 0.013505 -0.064096 -0.106151 -0.051701 0.018067 -0.132257 -0.087097 0.100177 0.077086 0.015532 -0.081299 0.060097 0.039849 0.012022 0.080452 -0.060635 -0.132832 0.221661 -0.089939 -0.063792 0.107578 -0.042526 -0.156943 0.152924 -0.109913 -0.109055 -0.076827 0.024759 0.166726 0.006231 0.015524 -0.042275 0.060000 -0.004546 -0.034933 0.000960 -0.073690 -0.017648 -0.052686 0.157787 -0.060782 -0.004237 -0.047121 -0.067493 0.103352 -0.211196 -0.156758 0.091279 0.046849 0.158989 26 | 15 -0.038742 -0.181860 0.076422 0.131426 0.114511 -0.098082 0.178220 -0.010870 -0.073351 0.011362 -0.102118 0.022538 0.017705 -0.190122 -0.066676 -0.203583 0.031577 -0.098463 0.013143 0.065663 0.161812 -0.006469 0.180472 0.057628 -0.016408 -0.140837 -0.057834 -0.035330 -0.027581 0.201601 -0.016786 0.068714 -0.151775 0.025442 -0.151069 -0.079852 0.197741 0.085267 -0.030836 -0.120840 -0.152323 0.180546 0.119559 0.091588 -0.062273 0.098330 0.021480 -0.112452 -0.068518 0.119750 0.070742 0.224954 -0.001131 0.101519 -0.157838 -0.112446 -0.068340 0.037492 0.016034 0.044963 0.041362 -0.083752 -0.120886 0.138655 0.039096 -0.254089 -0.061011 0.000680 0.064335 -0.045800 0.042723 0.005416 -0.063940 0.055067 -0.029680 -0.099408 0.048734 0.063396 0.055471 0.101279 -0.075155 -0.121488 0.004649 -0.219147 -0.012046 0.203272 -0.072077 0.048097 -0.141964 0.013599 0.007170 0.079798 0.055884 -0.049460 -0.051760 0.258512 -0.143937 0.057159 0.090866 -0.145522 -0.209132 0.174501 -0.077477 -0.152952 -0.012560 0.055284 0.195636 0.030398 0.019197 -0.000547 0.072016 -0.072628 -0.064853 -0.043871 -0.032922 -0.043684 -0.075295 0.130494 -0.071538 0.086941 -0.077534 -0.114188 0.026202 -0.169823 -0.011775 0.173016 0.026535 0.057350 27 | 21 -0.037420 -0.185872 0.075212 0.132879 0.113678 -0.109162 0.182616 -0.006157 -0.069429 0.015099 -0.106679 0.026768 0.019289 -0.203249 -0.063559 -0.210844 0.034637 -0.106484 0.014566 0.065066 0.167636 -0.008758 0.178195 0.061537 -0.008662 -0.145704 -0.056369 -0.033000 -0.026404 0.205464 -0.012827 0.065584 -0.158924 0.023812 -0.155812 -0.089556 0.206468 0.094386 -0.034654 -0.124362 -0.155135 0.181627 0.122971 0.098689 -0.074483 0.101350 0.031976 -0.111959 -0.065660 0.129340 0.071359 0.233446 0.000091 0.104223 -0.162610 -0.115789 -0.067045 0.041616 0.017170 0.040659 0.043462 -0.090928 -0.119716 0.149512 0.037119 -0.255346 -0.065734 0.000813 0.065450 -0.061481 0.036765 0.007069 -0.071992 0.055703 -0.015099 -0.099670 0.046978 0.058795 0.057399 0.096904 -0.081081 -0.120390 0.004858 -0.215712 -0.017138 0.200160 -0.064753 0.048738 -0.137154 0.013772 0.011125 0.075829 0.053434 -0.044530 -0.054077 0.259626 -0.147324 0.047909 0.094240 -0.146305 -0.214405 0.176736 -0.080233 -0.151088 -0.017518 0.060732 0.197316 0.025517 0.018441 -0.000798 0.078102 -0.066112 -0.064402 -0.035222 -0.035596 -0.042058 -0.081538 0.131786 -0.067695 0.083589 -0.080054 -0.110993 0.029966 -0.183058 -0.017387 0.173030 0.031126 0.062610 28 | 17 -0.003820 -0.258748 0.007511 0.073799 0.087261 -0.166561 0.164332 0.103193 0.086301 -0.032979 -0.095967 0.114296 0.023835 -0.209231 0.087164 -0.264820 -0.005851 -0.289037 -0.053388 -0.027257 0.271622 -0.022929 0.143493 0.109600 0.104106 -0.188538 -0.123153 -0.017891 0.014887 0.181230 -0.006468 0.039014 -0.144970 -0.062753 -0.089660 -0.163364 0.210163 0.142884 -0.075443 -0.073802 -0.096341 0.035099 0.155266 0.212611 -0.160332 0.146925 0.138555 -0.055615 -0.024053 0.181979 -0.048630 0.178089 -0.024028 0.157596 -0.180789 -0.216782 -0.066465 0.103743 0.122200 -0.068508 0.147465 -0.110282 0.003898 0.167303 -0.087383 -0.037417 -0.086446 0.072099 0.169359 -0.193109 -0.129178 0.071290 -0.134761 -0.075162 0.187776 -0.051138 0.093057 0.001767 -0.021234 -0.181341 -0.128279 0.001945 0.036843 -0.066448 -0.146718 0.022767 0.183774 -0.000486 -0.038667 0.091760 0.056919 -0.029739 0.102607 -0.047634 -0.190349 0.213284 -0.046043 -0.152270 0.132497 0.031042 -0.125947 0.157345 -0.144651 -0.101663 -0.137798 0.000075 0.170512 -0.016977 0.015676 -0.058873 0.058126 0.048742 -0.019961 0.026767 -0.105519 0.007691 -0.039213 0.200967 -0.064217 -0.058417 -0.023250 -0.029369 0.180964 -0.262813 -0.285498 0.048213 0.068696 0.249874 29 | 27 -0.032797 -0.195288 0.069216 0.133867 0.116239 -0.108610 0.185740 0.001955 -0.069550 0.011745 -0.109189 0.025742 0.016789 -0.195331 -0.063692 -0.214459 0.037900 -0.112968 0.012206 0.062131 0.171920 -0.008059 0.188124 0.063971 -0.005678 -0.152287 -0.063619 -0.034477 -0.029278 0.212291 -0.014158 0.063290 -0.162670 0.017202 -0.150986 -0.090824 0.207567 0.092173 -0.041477 -0.128388 -0.156671 0.178513 0.125848 0.103988 -0.072492 0.104946 0.038800 -0.114130 -0.067412 0.133543 0.070577 0.235690 -0.006427 0.108785 -0.161936 -0.123446 -0.068509 0.048673 0.026509 0.039035 0.043219 -0.087330 -0.122572 0.151673 0.033088 -0.258004 -0.068483 0.008963 0.071063 -0.058590 0.029735 0.015208 -0.073341 0.049656 -0.017644 -0.104967 0.052495 0.060227 0.055990 0.094341 -0.085367 -0.121196 0.006500 -0.214212 -0.020933 0.204051 -0.055618 0.050813 -0.148297 0.018130 0.005314 0.070086 0.059821 -0.045203 -0.059863 0.264393 -0.141945 0.042087 0.099247 -0.147625 -0.218392 0.180336 -0.078466 -0.159995 -0.019518 0.060393 0.201583 0.023242 0.017542 0.004161 0.076312 -0.067760 -0.062782 -0.038626 -0.033126 -0.048242 -0.077472 0.137043 -0.071898 0.078713 -0.083230 -0.106774 0.034610 -0.186021 -0.027404 0.176257 0.028692 0.068829 30 | 13 -0.016251 -0.202210 0.044203 0.088466 0.096507 -0.126337 0.159309 0.039759 -0.004149 -0.007473 -0.097593 0.056936 0.024332 -0.200536 0.006949 -0.221349 0.011997 -0.176938 -0.019105 0.014213 0.206004 -0.006592 0.149878 0.080568 0.030047 -0.150166 -0.079204 -0.015752 0.001669 0.182771 -0.015711 0.054895 -0.149152 -0.011902 -0.116221 -0.118940 0.195229 0.105479 -0.047902 -0.094361 -0.116620 0.097874 0.131618 0.149932 -0.103497 0.118331 0.071413 -0.082989 -0.049628 0.140691 0.008150 0.189791 -0.012064 0.134192 -0.171061 -0.151393 -0.064362 0.064645 0.060668 -0.005979 0.090131 -0.090428 -0.066909 0.141823 -0.018341 -0.146798 -0.070384 0.033815 0.111008 -0.118907 -0.042392 0.039118 -0.091291 -0.008561 0.075513 -0.077024 0.069740 0.033468 0.017223 -0.035632 -0.096566 -0.069470 0.013172 -0.144055 -0.072647 0.118751 0.048097 0.022301 -0.091841 0.053665 0.037036 0.025523 0.069075 -0.060058 -0.112545 0.225052 -0.098872 -0.048194 0.099967 -0.061093 -0.164970 0.155288 -0.104344 -0.117836 -0.068449 0.027251 0.178255 0.012349 0.016290 -0.034276 0.068646 -0.016171 -0.038488 -0.000464 -0.068722 -0.024287 -0.060751 0.158546 -0.063426 0.011159 -0.052163 -0.070405 0.093148 -0.201658 -0.132882 0.112448 0.048381 0.147612 31 | 16 -0.035511 -0.202433 0.074765 0.137003 0.120944 -0.118830 0.192556 0.003611 -0.063796 0.008194 -0.113915 0.035100 0.017987 -0.209628 -0.054792 -0.230371 0.031344 -0.130028 0.005399 0.060351 0.183567 -0.007812 0.195586 0.067352 -0.000746 -0.156934 -0.066758 -0.034735 -0.023160 0.217018 -0.017857 0.071274 -0.169265 0.016625 -0.155325 -0.095593 0.218590 0.094721 -0.042875 -0.128845 -0.157651 0.177348 0.135764 0.114344 -0.078313 0.107115 0.041332 -0.114620 -0.072006 0.140880 0.065547 0.235994 -0.004975 0.119182 -0.171357 -0.127930 -0.069664 0.051912 0.027303 0.033553 0.053918 -0.097968 -0.121946 0.152773 0.029148 -0.257393 -0.069424 0.013343 0.078061 -0.073751 0.027466 0.012917 -0.077428 0.046133 -0.000511 -0.104481 0.061024 0.061056 0.054253 0.080574 -0.086068 -0.123117 0.007125 -0.222340 -0.023653 0.207997 -0.047278 0.051753 -0.143597 0.023012 0.008929 0.073471 0.060931 -0.052019 -0.070416 0.277388 -0.146257 0.033883 0.101535 -0.145533 -0.219929 0.189848 -0.087398 -0.162705 -0.030509 0.055889 0.206549 0.028086 0.023714 -0.004239 0.078097 -0.061218 -0.065298 -0.034074 -0.040147 -0.044006 -0.080637 0.149864 -0.073541 0.072015 -0.083348 -0.112196 0.039528 -0.199341 -0.039605 0.177897 0.033195 0.086814 32 | 10 -0.027585 -0.194088 0.059197 0.119915 0.110972 -0.109276 0.174774 0.014288 -0.042095 -0.000858 -0.100241 0.042340 0.015622 -0.194950 -0.039811 -0.211997 0.029892 -0.130081 -0.000935 0.049677 0.180622 -0.008247 0.175306 0.071121 0.006256 -0.149930 -0.067700 -0.032686 -0.017300 0.199301 -0.015064 0.058100 -0.155358 0.013960 -0.142041 -0.098388 0.204942 0.091471 -0.045583 -0.114575 -0.142506 0.146433 0.133451 0.124666 -0.082104 0.110766 0.052044 -0.105706 -0.057250 0.130125 0.046488 0.215138 -0.005023 0.115444 -0.165828 -0.127673 -0.063402 0.051871 0.031160 0.026004 0.060362 -0.090928 -0.104118 0.150557 0.012237 -0.215296 -0.068170 0.014316 0.081505 -0.084544 0.007917 0.021841 -0.077922 0.032457 0.022610 -0.088440 0.057599 0.052640 0.040163 0.045724 -0.092811 -0.102962 0.008316 -0.186414 -0.038811 0.175640 -0.023215 0.038689 -0.124989 0.026973 0.020717 0.056677 0.068148 -0.047932 -0.078206 0.253664 -0.129277 0.009820 0.098881 -0.111326 -0.200409 0.169981 -0.092098 -0.142026 -0.041227 0.047309 0.194378 0.028011 0.015665 -0.009721 0.075129 -0.051944 -0.050750 -0.027879 -0.043975 -0.038653 -0.069770 0.145200 -0.069432 0.052571 -0.072166 -0.100100 0.053520 -0.189645 -0.060939 0.148898 0.033137 0.097645 33 | 19 -0.027732 -0.191410 0.071374 0.126421 0.111070 -0.107876 0.176676 0.002585 -0.056623 0.007753 -0.107521 0.034261 0.017425 -0.196124 -0.054218 -0.209829 0.028584 -0.119391 0.010084 0.061913 0.173410 -0.011680 0.179995 0.065147 -0.001625 -0.142977 -0.063433 -0.032499 -0.024087 0.202601 -0.014654 0.065021 -0.157166 0.014068 -0.147521 -0.090713 0.201155 0.093299 -0.036933 -0.116991 -0.146786 0.164469 0.126893 0.108293 -0.072268 0.104702 0.041552 -0.111925 -0.064965 0.130148 0.057931 0.221071 -0.006557 0.107943 -0.158583 -0.118838 -0.068929 0.049770 0.029246 0.031526 0.049580 -0.091647 -0.108593 0.149711 0.025498 -0.241462 -0.062069 0.007796 0.072132 -0.069099 0.023806 0.015895 -0.073660 0.045420 -0.004704 -0.098964 0.055777 0.060156 0.047455 0.072451 -0.083713 -0.113153 0.005848 -0.206370 -0.026630 0.191575 -0.042831 0.048624 -0.137622 0.019955 0.012079 0.069761 0.063439 -0.046717 -0.067139 0.259465 -0.137931 0.031655 0.095115 -0.133943 -0.204654 0.176064 -0.087317 -0.151846 -0.029655 0.053095 0.194311 0.026293 0.017557 -0.005392 0.070930 -0.060431 -0.056958 -0.035492 -0.035889 -0.039722 -0.070587 0.140291 -0.072385 0.073148 -0.077481 -0.105852 0.037980 -0.184668 -0.039701 0.162597 0.027359 0.074613 34 | 23 -0.033210 -0.184668 0.068293 0.134566 0.114270 -0.104607 0.178711 0.001390 -0.065522 0.009054 -0.106138 0.027556 0.018583 -0.199027 -0.063989 -0.206200 0.033015 -0.105313 0.014056 0.067949 0.166519 -0.009827 0.178455 0.063617 -0.006858 -0.146158 -0.062987 -0.037576 -0.029466 0.209047 -0.015356 0.066887 -0.160449 0.021807 -0.155659 -0.090378 0.207961 0.088324 -0.040872 -0.123138 -0.151890 0.176836 0.126912 0.103274 -0.069964 0.100173 0.030907 -0.109336 -0.069626 0.127748 0.067974 0.227998 -0.000047 0.103285 -0.161713 -0.120993 -0.068281 0.045139 0.020741 0.034411 0.045739 -0.092077 -0.115655 0.148847 0.035084 -0.252089 -0.062115 0.008133 0.069460 -0.054618 0.033052 0.012278 -0.067598 0.053203 -0.019130 -0.097868 0.052464 0.061815 0.059491 0.093377 -0.076428 -0.121617 0.008539 -0.212696 -0.018011 0.203398 -0.061353 0.051035 -0.142295 0.014292 0.010089 0.074233 0.053472 -0.047926 -0.059945 0.256730 -0.142284 0.041962 0.092629 -0.139135 -0.213449 0.175437 -0.076828 -0.156503 -0.021848 0.053856 0.192573 0.025691 0.021557 0.001070 0.069397 -0.069436 -0.063409 -0.040655 -0.036039 -0.045850 -0.079422 0.133649 -0.068391 0.078443 -0.077553 -0.106411 0.028381 -0.181549 -0.019292 0.166210 0.027056 0.064981 35 | 12 -0.021273 -0.227668 0.038558 0.090407 0.099022 -0.140312 0.164310 0.052033 0.009098 -0.013410 -0.097127 0.077289 0.022158 -0.207640 0.021235 -0.240299 0.006905 -0.202974 -0.020711 0.006990 0.226572 -0.006636 0.162136 0.086941 0.045041 -0.167648 -0.092316 -0.020622 0.001998 0.192480 -0.011041 0.053884 -0.157959 -0.018872 -0.112976 -0.125813 0.202160 0.115976 -0.054787 -0.090532 -0.118860 0.091723 0.145099 0.166208 -0.120442 0.130763 0.087454 -0.084446 -0.048270 0.149799 -0.000930 0.194755 -0.016566 0.135677 -0.178469 -0.171928 -0.064194 0.077802 0.071331 -0.014707 0.104369 -0.097693 -0.055641 0.159833 -0.037280 -0.129457 -0.070626 0.042838 0.127276 -0.129173 -0.052652 0.049975 -0.101368 -0.018337 0.098129 -0.073238 0.081095 0.033745 0.013358 -0.056144 -0.107931 -0.054709 0.014202 -0.137441 -0.089349 0.108359 0.072167 0.020375 -0.085731 0.056013 0.039300 0.019503 0.078827 -0.056725 -0.129791 0.227478 -0.089512 -0.069089 0.113283 -0.044508 -0.168694 0.157203 -0.113424 -0.119602 -0.082808 0.021959 0.180182 0.008560 0.020195 -0.041412 0.060062 -0.004268 -0.036427 0.002955 -0.074725 -0.013607 -0.056984 0.172375 -0.070957 -0.001239 -0.051512 -0.070750 0.111761 -0.219853 -0.165831 0.102570 0.048231 0.167540 36 | -------------------------------------------------------------------------------- /graph/karate.edgelist: -------------------------------------------------------------------------------- 1 | 1 32 2 | 1 22 3 | 1 20 4 | 1 18 5 | 1 14 6 | 1 13 7 | 1 12 8 | 1 11 9 | 1 9 10 | 1 8 11 | 1 7 12 | 1 6 13 | 1 5 14 | 1 4 15 | 1 3 16 | 1 2 17 | 2 31 18 | 2 22 19 | 2 20 20 | 2 18 21 | 2 14 22 | 2 8 23 | 2 4 24 | 2 3 25 | 3 14 26 | 3 9 27 | 3 10 28 | 3 33 29 | 3 29 30 | 3 28 31 | 3 8 32 | 3 4 33 | 4 14 34 | 4 13 35 | 4 8 36 | 5 11 37 | 5 7 38 | 6 17 39 | 6 11 40 | 6 7 41 | 7 17 42 | 9 34 43 | 9 33 44 | 9 33 45 | 10 34 46 | 14 34 47 | 15 34 48 | 15 33 49 | 16 34 50 | 16 33 51 | 19 34 52 | 19 33 53 | 20 34 54 | 21 34 55 | 21 33 56 | 23 34 57 | 23 33 58 | 24 30 59 | 24 34 60 | 24 33 61 | 24 28 62 | 24 26 63 | 25 32 64 | 25 28 65 | 25 26 66 | 26 32 67 | 27 34 68 | 27 30 69 | 28 34 70 | 29 34 71 | 29 32 72 | 30 34 73 | 30 33 74 | 31 34 75 | 31 33 76 | 32 34 77 | 32 33 78 | 33 34 -------------------------------------------------------------------------------- /node2vec_spark/README.md: -------------------------------------------------------------------------------- 1 | # node2vec on spark 2 | 3 | This library is a implementation using scala for running on spark of *node2vec* as described in the paper: 4 | > node2vec: Scalable Feature Learning for Networks. 5 | > Aditya Grover and Jure Leskovec. 6 | > Knowledge Discovery and Data Mining, 2016. 7 | > 8 | 9 | The *node2vec* algorithm learns continuous representations for nodes in any (un)directed, (un)weighted graph. Please check the [project page](https://snap.stanford.edu/node2vec/) for more details. 10 | 11 | 12 | ### Building node2vec_spark 13 | **In order to build node2vec_spark, use the following:** 14 | 15 | ``` 16 | $ git clone https://github.com/Skarface-/node2vec.git 17 | $ mvn clean package 18 | ``` 19 | 20 | **and requires:**
21 | Maven 3.0.5 or newer
22 | Java 7+
23 | Scala 2.10 or newer. 24 | 25 | This will produce jar file in "node2vec_spark/target/" 26 | 27 | ### Examples 28 | This library has two functions: *randomwalk* and *embedding*.
29 | These were described in these papers [node2vec: Scalable Feature Learning for Networks](http://arxiv.org/abs/1607.00653) and [Efficient Estimation of Word Representations in Vector Space](https://arxiv.org/abs/1301.3781). 30 | 31 | ### Random walk 32 | Example: 33 | 34 | ./spark-submit --class com.navercorp.Main \ 35 | ./node2vec_spark/target/node2vec-0.0.1-SNAPSHOT.jar \ 36 | --cmd randomwalk --p 100.0 --q 100.0 --walkLength 40 \ 37 | --input --output 38 | 39 | #### Options 40 | Invoke a command without arguments to list available arguments and their default values: 41 | 42 | ``` 43 | --cmd COMMAND 44 | Functions: randomwalk or embedding. If you want to execute all functions "randomwalk" and "embedding" sequentially input "node2vec". Default "node2vec" 45 | --input [INPUT] 46 | Input edgelist path. The supported input format is an edgelist: "node1_id_int node2_id_int " 47 | --output [OUTPUT] 48 | Random paths path. 49 | --walkLength WALK_LENGTH 50 | Length of walk per source. Default is 80. 51 | --numWalks NUM_WALKS 52 | Number of walks per source. Default is 10. 53 | --p P 54 | Return hyperparaemter. Default is 1.0. 55 | --q Q 56 | Inout hyperparameter. Default is 1.0. 57 | --weighted Boolean 58 | Specifying (un)weighted. Default is true. 59 | --directed Boolean 60 | Specifying (un)directed. Default is false. 61 | --degree UPPER_BOUND_OF_NUMBER_OF_NEIGHBORS 62 | Specifying upper bound of number of neighbors. Default is 30. 63 | --indexed Boolean 64 | Specifying whether nodes in edgelist are indexed or not. Default is true. 65 | ``` 66 | 67 | * If "indexed" is set to false, *node2vec_spark* index nodes in input edgelist, example:
68 | **unindexed edgelist:**
69 | node1 node2 1.0
70 | node2 node7 1.0
71 | 72 | **indexed:**
73 | 1 2 1.0
74 | 2 3 1.0
75 | 76 | 1 node1
77 | 2 node2
78 | 3 node7 79 | 80 | #### Input 81 | The supported input format is an edgelist: 82 | 83 | node1_id_int node2_id_int 84 | or 85 | node1_str node2_str , Please set the option "indexed" to false 86 | 87 | 88 | #### Output 89 | The output file (number of nodes)*numWalks random paths as follows: 90 | 91 | src_node_id_int node1_id_int node2_id_int ... noden_id_int 92 | 93 | 94 | ### Embedding random paths 95 | Example: 96 | 97 | ./spark-submit --class com.navercorp.Main \ 98 | ./node2vec_spark/target/node2vec-0.0.1-SNAPSHOT.jar \ 99 | --cmd embedding --dim 50 --iter 20 \ 100 | --input --nodePath --output 101 | 102 | #### Options 103 | Invoke a command without arguments to list available arguments and their default values: 104 | 105 | ``` 106 | --cmd COMMAND 107 | embedding. If you want to execute sequentially all functions: "randomwalk" and "embedding", input "node2vec". default "node2vec" 108 | --input [INPUT] 109 | Input random paths. The supported input format is an random paths: "src_node_id_int node1_id_int ... noden_id_int" 110 | --output [OUTPUT] 111 | word2vec model(.bin) and embeddings(.emb). 112 | --nodePath [NODE\_PATH] 113 | Input node2index path. The supported input format: "node1_str node1_id_int" 114 | --iter ITERATION 115 | Number of epochs in SGD. Default 10. 116 | --dim DIMENSION 117 | Number of dimensions. Default is 128. 118 | --window WINDOW_SIZE 119 | Context size for optimization. Default is 10. 120 | 121 | ``` 122 | 123 | #### Input 124 | The supported input format is an random paths: 125 | 126 | src_node_id_int node1_id_int ... noden_id_int 127 | 128 | #### Output 129 | The output files are **embeddings and word2vec model.** The embeddings file has the following format: 130 | 131 | node1_str dim1 dim2 ... dimd 132 | 133 | where dim1, ... , dimd is the d-dimensional representation learned by word2vec. 134 | 135 | the output file *word2vec model* has the spark word2vec model format. please reference to https://spark.apache.org/docs/1.5.2/mllib-feature-extraction.html#word2vec 136 | 137 | ## References 138 | 1. [node2vec: Scalable Feature Learning for Networks](http://arxiv.org/abs/1607.00653) 139 | 2. [Efficient Estimation of Word Representations in Vector Space](https://arxiv.org/abs/1301.3781) -------------------------------------------------------------------------------- /node2vec_spark/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 4.0.0 5 | 6 | com.navercorp 7 | node2vec 8 | jar 9 | 0.0.1-SNAPSHOT 10 | 11 | node2vec_spark 12 | http://snap.stanford.edu/node2vec/ 13 | 14 | 15 | UTF-8 16 | bin 17 | 2.4.3 18 | 1.4.0 19 | 1.7 20 | 2.10 21 | 22 | 23 | 24 | 25 | 26 | org.scala-tools 27 | maven-scala-plugin 28 | 2.15.2 29 | 30 | 31 | 32 | compile 33 | 34 | 35 | 36 | 37 | 38 | org.apache.maven.plugins 39 | maven-dependency-plugin 40 | 2.4 41 | 42 | 43 | copy-dependencies 44 | package 45 | 46 | copy-dependencies 47 | 48 | 49 | ${project.build.directory}/lib 50 | 51 | 52 | 53 | 54 | 55 | org.apache.maven.plugins 56 | maven-shade-plugin 57 | 1.6 58 | 59 | 60 | package 61 | 62 | shade 63 | 64 | 65 | 66 | 67 | 68 | org.apache.maven.plugins 69 | maven-compiler-plugin 70 | 2.3.2 71 | 72 | 1.7 73 | 1.7 74 | UTF-8 75 | 76 | 77 | 78 | org.apache.maven.plugins 79 | maven-surefire-plugin 80 | 81 | false 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | org.apache.hadoop 90 | hadoop-hdfs 91 | 2.7.1 92 | 93 | 94 | org.scala-lang 95 | scala-library 96 | ${scala.binary.version}.5 97 | provided 98 | 99 | 100 | org.apache.spark 101 | spark-core_${scala.binary.version} 102 | 1.6.1 103 | provided 104 | 105 | 106 | org.apache.spark 107 | spark-mllib_${scala.binary.version} 108 | 1.6.1 109 | provided 110 | 111 | 112 | com.github.scopt 113 | scopt_${scala.binary.version} 114 | 3.3.0 115 | 116 | 117 | org.scala-lang 118 | scala-library 119 | 120 | 121 | 122 | 123 | com.google.guava 124 | guava 125 | 19.0 126 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /node2vec_spark/src/main/resources/log4j2.properties: -------------------------------------------------------------------------------- 1 | 2 | appender.out.type = Console 3 | appender.out.name = out 4 | appender.out.layout.type = PatternLayout 5 | appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n 6 | logger.springframework.name = org.springframework 7 | logger.springframework.level = WARN 8 | rootLogger.level = INFO 9 | rootLogger.appenderRef.out.ref = out 10 | -------------------------------------------------------------------------------- /node2vec_spark/src/main/scala/com/navercorp/Main.scala: -------------------------------------------------------------------------------- 1 | package com.navercorp 2 | 3 | import java.io.Serializable 4 | import org.apache.spark.{SparkContext, SparkConf} 5 | import scopt.OptionParser 6 | import com.navercorp.lib.AbstractParams 7 | 8 | object Main { 9 | object Command extends Enumeration { 10 | type Command = Value 11 | val node2vec, randomwalk, embedding = Value 12 | } 13 | import Command._ 14 | 15 | case class Params(iter: Int = 10, 16 | lr: Double = 0.025, 17 | numPartition: Int = 10, 18 | dim: Int = 128, 19 | window: Int = 10, 20 | walkLength: Int = 80, 21 | numWalks: Int = 10, 22 | p: Double = 1.0, 23 | q: Double = 1.0, 24 | weighted: Boolean = true, 25 | directed: Boolean = false, 26 | degree: Int = 30, 27 | indexed: Boolean = true, 28 | nodePath: String = null, 29 | input: String = null, 30 | output: String = null, 31 | cmd: Command = Command.node2vec) extends AbstractParams[Params] with Serializable 32 | val defaultParams = Params() 33 | 34 | val parser = new OptionParser[Params]("Node2Vec_Spark") { 35 | head("Main") 36 | opt[Int]("walkLength") 37 | .text(s"walkLength: ${defaultParams.walkLength}") 38 | .action((x, c) => c.copy(walkLength = x)) 39 | opt[Int]("numWalks") 40 | .text(s"numWalks: ${defaultParams.numWalks}") 41 | .action((x, c) => c.copy(numWalks = x)) 42 | opt[Double]("p") 43 | .text(s"return parameter p: ${defaultParams.p}") 44 | .action((x, c) => c.copy(p = x)) 45 | opt[Double]("q") 46 | .text(s"in-out parameter q: ${defaultParams.q}") 47 | .action((x, c) => c.copy(q = x)) 48 | opt[Boolean]("weighted") 49 | .text(s"weighted: ${defaultParams.weighted}") 50 | .action((x, c) => c.copy(weighted = x)) 51 | opt[Boolean]("directed") 52 | .text(s"directed: ${defaultParams.directed}") 53 | .action((x, c) => c.copy(directed = x)) 54 | opt[Int]("degree") 55 | .text(s"degree: ${defaultParams.degree}") 56 | .action((x, c) => c.copy(degree = x)) 57 | opt[Boolean]("indexed") 58 | .text(s"Whether nodes are indexed or not: ${defaultParams.indexed}") 59 | .action((x, c) => c.copy(indexed = x)) 60 | opt[String]("nodePath") 61 | .text("Input node2index file path: empty") 62 | .action((x, c) => c.copy(nodePath = x)) 63 | opt[String]("input") 64 | .required() 65 | .text("Input edge file path: empty") 66 | .action((x, c) => c.copy(input = x)) 67 | opt[String]("output") 68 | .required() 69 | .text("Output path: empty") 70 | .action((x, c) => c.copy(output = x)) 71 | opt[String]("cmd") 72 | .required() 73 | .text(s"command: ${defaultParams.cmd.toString}") 74 | .action((x, c) => c.copy(cmd = Command.withName(x))) 75 | note( 76 | """ 77 | |For example, the following command runs this app on a synthetic dataset: 78 | | 79 | | bin/spark-submit --class com.nhn.sunny.vegapunk.ml.model.Node2vec \ 80 | """.stripMargin + 81 | s"| --lr ${defaultParams.lr}" + 82 | s"| --iter ${defaultParams.iter}" + 83 | s"| --numPartition ${defaultParams.numPartition}" + 84 | s"| --dim ${defaultParams.dim}" + 85 | s"| --window ${defaultParams.window}" + 86 | s"| --input " + 87 | s"| --node " + 88 | s"| --output " 89 | ) 90 | } 91 | 92 | def main(args: Array[String]) = { 93 | parser.parse(args, defaultParams).map { param => 94 | val conf = new SparkConf().setAppName("Node2Vec") 95 | val context: SparkContext = new SparkContext(conf) 96 | 97 | Node2vec.setup(context, param) 98 | 99 | param.cmd match { 100 | case Command.node2vec => Node2vec.load() 101 | .initTransitionProb() 102 | .randomWalk() 103 | .embedding() 104 | .save() 105 | case Command.randomwalk => Node2vec.load() 106 | .initTransitionProb() 107 | .randomWalk() 108 | .saveRandomPath() 109 | case Command.embedding => { 110 | val randomPaths = Word2vec.setup(context, param).read(param.input) 111 | Word2vec.fit(randomPaths).save(param.output) 112 | Node2vec.loadNode2Id(param.nodePath).saveVectors() 113 | } 114 | } 115 | } getOrElse { 116 | sys.exit(1) 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /node2vec_spark/src/main/scala/com/navercorp/Node2vec.scala: -------------------------------------------------------------------------------- 1 | package com.navercorp 2 | 3 | 4 | import java.io.Serializable 5 | import scala.util.Try 6 | import scala.collection.mutable.ArrayBuffer 7 | import org.slf4j.{Logger, LoggerFactory} 8 | import org.apache.spark.SparkContext 9 | import org.apache.spark.rdd.RDD 10 | import org.apache.spark.graphx.{EdgeTriplet, Graph, _} 11 | import com.navercorp.graph.{GraphOps, EdgeAttr, NodeAttr} 12 | 13 | object Node2vec extends Serializable { 14 | lazy val logger: Logger = LoggerFactory.getLogger(getClass.getName); 15 | 16 | var context: SparkContext = null 17 | var config: Main.Params = null 18 | var node2id: RDD[(String, Long)] = null 19 | var indexedEdges: RDD[Edge[EdgeAttr]] = _ 20 | var indexedNodes: RDD[(VertexId, NodeAttr)] = _ 21 | var graph: Graph[NodeAttr, EdgeAttr] = _ 22 | var randomWalkPaths: RDD[(Long, ArrayBuffer[Long])] = null 23 | 24 | def setup(context: SparkContext, param: Main.Params): this.type = { 25 | this.context = context 26 | this.config = param 27 | 28 | this 29 | } 30 | 31 | def load(): this.type = { 32 | val bcMaxDegree = context.broadcast(config.degree) 33 | val bcEdgeCreator = config.directed match { 34 | case true => context.broadcast(GraphOps.createDirectedEdge) 35 | case false => context.broadcast(GraphOps.createUndirectedEdge) 36 | } 37 | 38 | val inputTriplets: RDD[(Long, Long, Double)] = config.indexed match { 39 | case true => readIndexedGraph(config.input) 40 | case false => indexingGraph(config.input) 41 | } 42 | 43 | indexedNodes = inputTriplets.flatMap { case (srcId, dstId, weight) => 44 | bcEdgeCreator.value.apply(srcId, dstId, weight) 45 | }.reduceByKey(_++_).map { case (nodeId, neighbors: Array[(VertexId, Double)]) => 46 | var neighbors_ = neighbors 47 | if (neighbors_.length > bcMaxDegree.value) { 48 | neighbors_ = neighbors.sortWith{ case (left, right) => left._2 > right._2 }.slice(0, bcMaxDegree.value) 49 | } 50 | 51 | (nodeId, NodeAttr(neighbors = neighbors_.distinct)) 52 | }.repartition(200).cache 53 | 54 | indexedEdges = indexedNodes.flatMap { case (srcId, clickNode) => 55 | clickNode.neighbors.map { case (dstId, weight) => 56 | Edge(srcId, dstId, EdgeAttr()) 57 | } 58 | }.repartition(200).cache 59 | 60 | this 61 | } 62 | 63 | def initTransitionProb(): this.type = { 64 | val bcP = context.broadcast(config.p) 65 | val bcQ = context.broadcast(config.q) 66 | 67 | graph = Graph(indexedNodes, indexedEdges) 68 | .mapVertices[NodeAttr] { case (vertexId, clickNode) => 69 | val (j, q) = GraphOps.setupAlias(clickNode.neighbors) 70 | val nextNodeIndex = GraphOps.drawAlias(j, q) 71 | clickNode.path = Array(vertexId, clickNode.neighbors(nextNodeIndex)._1) 72 | 73 | clickNode 74 | } 75 | .mapTriplets { edgeTriplet: EdgeTriplet[NodeAttr, EdgeAttr] => 76 | val (j, q) = GraphOps.setupEdgeAlias(bcP.value, bcQ.value)(edgeTriplet.srcId, edgeTriplet.srcAttr.neighbors, edgeTriplet.dstAttr.neighbors) 77 | edgeTriplet.attr.J = j 78 | edgeTriplet.attr.q = q 79 | edgeTriplet.attr.dstNeighbors = edgeTriplet.dstAttr.neighbors.map(_._1) 80 | 81 | edgeTriplet.attr 82 | }.cache 83 | 84 | this 85 | } 86 | 87 | def randomWalk(): this.type = { 88 | val edge2attr = graph.triplets.map { edgeTriplet => 89 | (s"${edgeTriplet.srcId}${edgeTriplet.dstId}", edgeTriplet.attr) 90 | }.repartition(200).cache 91 | edge2attr.first 92 | 93 | for (iter <- 0 until config.numWalks) { 94 | var prevWalk: RDD[(Long, ArrayBuffer[Long])] = null 95 | var randomWalk = graph.vertices.map { case (nodeId, clickNode) => 96 | val pathBuffer = new ArrayBuffer[Long]() 97 | pathBuffer.append(clickNode.path:_*) 98 | (nodeId, pathBuffer) 99 | }.cache 100 | var activeWalks = randomWalk.first 101 | graph.unpersist(blocking = false) 102 | graph.edges.unpersist(blocking = false) 103 | for (walkCount <- 0 until config.walkLength) { 104 | prevWalk = randomWalk 105 | randomWalk = randomWalk.map { case (srcNodeId, pathBuffer) => 106 | val prevNodeId = pathBuffer(pathBuffer.length - 2) 107 | val currentNodeId = pathBuffer.last 108 | 109 | (s"$prevNodeId$currentNodeId", (srcNodeId, pathBuffer)) 110 | }.join(edge2attr).map { case (edge, ((srcNodeId, pathBuffer), attr)) => 111 | try { 112 | val nextNodeIndex = GraphOps.drawAlias(attr.J, attr.q) 113 | val nextNodeId = attr.dstNeighbors(nextNodeIndex) 114 | pathBuffer.append(nextNodeId) 115 | 116 | (srcNodeId, pathBuffer) 117 | } catch { 118 | case e: Exception => throw new RuntimeException(e.getMessage) 119 | } 120 | }.cache 121 | 122 | activeWalks = randomWalk.first() 123 | prevWalk.unpersist(blocking=false) 124 | } 125 | 126 | 127 | if (randomWalkPaths != null) { 128 | val prevRandomWalkPaths = randomWalkPaths 129 | randomWalkPaths = randomWalkPaths.union(randomWalk).cache() 130 | randomWalkPaths.first 131 | prevRandomWalkPaths.unpersist(blocking = false) 132 | } else { 133 | randomWalkPaths = randomWalk 134 | } 135 | } 136 | 137 | this 138 | } 139 | 140 | def embedding(): this.type = { 141 | val randomPaths = randomWalkPaths.map { case (vertexId, pathBuffer) => 142 | Try(pathBuffer.map(_.toString).toIterable).getOrElse(null) 143 | }.filter(_!=null) 144 | 145 | Word2vec.setup(context, config).fit(randomPaths) 146 | 147 | this 148 | } 149 | 150 | def save(): this.type = { 151 | this.saveRandomPath() 152 | .saveModel() 153 | .saveVectors() 154 | } 155 | 156 | def saveRandomPath(): this.type = { 157 | randomWalkPaths 158 | .map { case (vertexId, pathBuffer) => 159 | Try(pathBuffer.mkString("\t")).getOrElse(null) 160 | } 161 | .filter(x => x != null && x.replaceAll("\\s", "").length > 0) 162 | .repartition(200) 163 | .saveAsTextFile(config.output) 164 | 165 | this 166 | } 167 | 168 | def saveModel(): this.type = { 169 | Word2vec.save(config.output) 170 | 171 | this 172 | } 173 | 174 | def saveVectors(): this.type = { 175 | val node2vector = context.parallelize(Word2vec.getVectors.toList) 176 | .map { case (nodeId, vector) => 177 | (nodeId.toLong, vector.mkString(",")) 178 | } 179 | 180 | if (this.node2id != null) { 181 | val id2Node = this.node2id.map{ case (strNode, index) => 182 | (index, strNode) 183 | } 184 | 185 | node2vector.join(id2Node) 186 | .map { case (nodeId, (vector, name)) => s"$name\t$vector" } 187 | .repartition(200) 188 | .saveAsTextFile(s"${config.output}.emb") 189 | } else { 190 | node2vector.map { case (nodeId, vector) => s"$nodeId\t$vector" } 191 | .repartition(200) 192 | .saveAsTextFile(s"${config.output}.emb") 193 | } 194 | 195 | this 196 | } 197 | 198 | def cleanup(): this.type = { 199 | node2id.unpersist(blocking = false) 200 | indexedEdges.unpersist(blocking = false) 201 | indexedNodes.unpersist(blocking = false) 202 | graph.unpersist(blocking = false) 203 | randomWalkPaths.unpersist(blocking = false) 204 | 205 | this 206 | } 207 | 208 | def loadNode2Id(node2idPath: String): this.type = { 209 | try { 210 | this.node2id = context.textFile(config.nodePath).map { node2index => 211 | val Array(strNode, index) = node2index.split("\\s") 212 | (strNode, index.toLong) 213 | } 214 | } catch { 215 | case e: Exception => logger.info("Failed to read node2index file.") 216 | this.node2id = null 217 | } 218 | 219 | this 220 | } 221 | 222 | def readIndexedGraph(tripletPath: String) = { 223 | val bcWeighted = context.broadcast(config.weighted) 224 | 225 | val rawTriplets = context.textFile(tripletPath) 226 | if (config.nodePath == null) { 227 | this.node2id = createNode2Id(rawTriplets.map { triplet => 228 | val parts = triplet.split("\\s") 229 | (parts.head, parts(1), -1) 230 | }) 231 | } else { 232 | loadNode2Id(config.nodePath) 233 | } 234 | 235 | rawTriplets.map { triplet => 236 | val parts = triplet.split("\\s") 237 | val weight = bcWeighted.value match { 238 | case true => Try(parts.last.toDouble).getOrElse(1.0) 239 | case false => 1.0 240 | } 241 | 242 | (parts.head.toLong, parts(1).toLong, weight) 243 | } 244 | } 245 | 246 | 247 | def indexingGraph(rawTripletPath: String): RDD[(Long, Long, Double)] = { 248 | val rawEdges = context.textFile(rawTripletPath).map { triplet => 249 | val parts = triplet.split("\\s") 250 | 251 | Try { 252 | (parts.head, parts(1), Try(parts.last.toDouble).getOrElse(1.0)) 253 | }.getOrElse(null) 254 | }.filter(_!=null) 255 | 256 | this.node2id = createNode2Id(rawEdges) 257 | 258 | rawEdges.map { case (src, dst, weight) => 259 | (src, (dst, weight)) 260 | }.join(node2id).map { case (src, (edge: (String, Double), srcIndex: Long)) => 261 | try { 262 | val (dst: String, weight: Double) = edge 263 | (dst, (srcIndex, weight)) 264 | } catch { 265 | case e: Exception => null 266 | } 267 | }.filter(_!=null).join(node2id).map { case (dst, (edge: (Long, Double), dstIndex: Long)) => 268 | try { 269 | val (srcIndex, weight) = edge 270 | (srcIndex, dstIndex, weight) 271 | } catch { 272 | case e: Exception => null 273 | } 274 | }.filter(_!=null) 275 | } 276 | 277 | def createNode2Id[T <: Any](triplets: RDD[(String, String, T)]) = triplets.flatMap { case (src, dst, weight) => 278 | Try(Array(src, dst)).getOrElse(Array.empty[String]) 279 | }.distinct().zipWithIndex() 280 | 281 | } 282 | -------------------------------------------------------------------------------- /node2vec_spark/src/main/scala/com/navercorp/Word2vec.scala: -------------------------------------------------------------------------------- 1 | package com.navercorp 2 | 3 | import org.apache.spark.SparkContext 4 | import org.apache.spark.mllib.feature.{Word2Vec, Word2VecModel} 5 | import org.apache.spark.rdd.RDD 6 | 7 | object Word2vec extends Serializable { 8 | var context: SparkContext = null 9 | var word2vec = new Word2Vec() 10 | var model: Word2VecModel = null 11 | 12 | def setup(context: SparkContext, param: Main.Params): this.type = { 13 | this.context = context 14 | /** 15 | * model = sg 16 | * update = hs 17 | */ 18 | word2vec.setLearningRate(param.lr) 19 | .setNumIterations(param.iter) 20 | .setNumPartitions(param.numPartition) 21 | .setMinCount(0) 22 | .setVectorSize(param.dim) 23 | 24 | val word2vecWindowField = word2vec.getClass.getDeclaredField("org$apache$spark$mllib$feature$Word2Vec$$window") 25 | word2vecWindowField.setAccessible(true) 26 | word2vecWindowField.setInt(word2vec, param.window) 27 | 28 | this 29 | } 30 | 31 | def read(path: String): RDD[Iterable[String]] = { 32 | context.textFile(path).repartition(200).map(_.split("\\s").toSeq) 33 | } 34 | 35 | def fit(input: RDD[Iterable[String]]): this.type = { 36 | model = word2vec.fit(input) 37 | 38 | this 39 | } 40 | 41 | def save(outputPath: String): this.type = { 42 | model.save(context, s"$outputPath.bin") 43 | this 44 | } 45 | 46 | def load(path: String): this.type = { 47 | model = Word2VecModel.load(context, path) 48 | 49 | this 50 | } 51 | 52 | def getVectors = this.model.getVectors 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /node2vec_spark/src/main/scala/com/navercorp/graph/GraphOps.scala: -------------------------------------------------------------------------------- 1 | package com.navercorp.graph 2 | 3 | import scala.collection.mutable.ArrayBuffer 4 | 5 | object GraphOps { 6 | def setupAlias(nodeWeights: Array[(Long, Double)]): (Array[Int], Array[Double]) = { 7 | val K = nodeWeights.length 8 | val J = Array.fill(K)(0) 9 | val q = Array.fill(K)(0.0) 10 | 11 | val smaller = new ArrayBuffer[Int]() 12 | val larger = new ArrayBuffer[Int]() 13 | 14 | val sum = nodeWeights.map(_._2).sum 15 | nodeWeights.zipWithIndex.foreach { case ((nodeId, weight), i) => 16 | q(i) = K * weight / sum 17 | if (q(i) < 1.0) { 18 | smaller.append(i) 19 | } else { 20 | larger.append(i) 21 | } 22 | } 23 | 24 | while (smaller.nonEmpty && larger.nonEmpty) { 25 | val small = smaller.remove(smaller.length - 1) 26 | val large = larger.remove(larger.length - 1) 27 | 28 | J(small) = large 29 | q(large) = q(large) + q(small) - 1.0 30 | if (q(large) < 1.0) smaller.append(large) 31 | else larger.append(large) 32 | } 33 | 34 | (J, q) 35 | } 36 | 37 | def setupEdgeAlias(p: Double = 1.0, q: Double = 1.0)(srcId: Long, srcNeighbors: Array[(Long, Double)], dstNeighbors: Array[(Long, Double)]): (Array[Int], Array[Double]) = { 38 | val neighbors_ = dstNeighbors.map { case (dstNeighborId, weight) => 39 | var unnormProb = weight / q 40 | if (srcId == dstNeighborId) unnormProb = weight / p 41 | else if (srcNeighbors.exists(_._1 == dstNeighborId)) unnormProb = weight 42 | 43 | (dstNeighborId, unnormProb) 44 | } 45 | 46 | setupAlias(neighbors_) 47 | } 48 | 49 | def drawAlias(J: Array[Int], q: Array[Double]): Int = { 50 | val K = J.length 51 | val kk = math.floor(math.random * K).toInt 52 | 53 | if (math.random < q(kk)) kk 54 | else J(kk) 55 | } 56 | 57 | lazy val createUndirectedEdge = (srcId: Long, dstId: Long, weight: Double) => { 58 | Array( 59 | (srcId, Array((dstId, weight))), 60 | (dstId, Array((srcId, weight))) 61 | ) 62 | } 63 | 64 | lazy val createDirectedEdge = (srcId: Long, dstId: Long, weight: Double) => { 65 | Array( 66 | (srcId, Array((dstId, weight))) 67 | ) 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /node2vec_spark/src/main/scala/com/navercorp/graph/package.scala: -------------------------------------------------------------------------------- 1 | package com.navercorp 2 | 3 | import java.io.Serializable 4 | 5 | package object graph { 6 | case class NodeAttr(var neighbors: Array[(Long, Double)] = Array.empty[(Long, Double)], 7 | var path: Array[Long] = Array.empty[Long]) extends Serializable 8 | 9 | case class EdgeAttr(var dstNeighbors: Array[Long] = Array.empty[Long], 10 | var J: Array[Int] = Array.empty[Int], 11 | var q: Array[Double] = Array.empty[Double]) extends Serializable 12 | } 13 | -------------------------------------------------------------------------------- /node2vec_spark/src/main/scala/com/navercorp/lib/AbstractParams.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.navercorp.lib 19 | 20 | import scala.reflect.runtime.universe._ 21 | 22 | /** 23 | * Abstract class for parameter case classes. 24 | * This overrides the [[toString]] method to print all case class fields by name and value. 25 | * @tparam T Concrete parameter class. 26 | */ 27 | abstract class AbstractParams[T: TypeTag] { 28 | 29 | private def tag: TypeTag[T] = typeTag[T] 30 | 31 | /** 32 | * Finds all case class fields in concrete class instance, and outputs them in JSON-style format: 33 | * { 34 | * [field name]:\t[field value]\n 35 | * [field name]:\t[field value]\n 36 | * ... 37 | * } 38 | */ 39 | override def toString: String = { 40 | val tpe = tag.tpe 41 | val allAccessors = tpe.declarations.collect { 42 | case m: MethodSymbol if m.isCaseAccessor => m 43 | } 44 | val mirror = runtimeMirror(getClass.getClassLoader) 45 | val instanceMirror = mirror.reflect(this) 46 | allAccessors.map { f => 47 | val paramName = f.name.toString 48 | val fieldMirror = instanceMirror.reflectField(f) 49 | val paramValue = fieldMirror.get 50 | s" $paramName:\t$paramValue" 51 | }.mkString("{\n", ",\n", "\n}") 52 | } 53 | } -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | networkx==1.11 2 | numpy==1.11.2 3 | gensim==0.13.3 4 | -------------------------------------------------------------------------------- /src/main.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Reference implementation of node2vec. 3 | 4 | Author: Aditya Grover 5 | 6 | For more details, refer to the paper: 7 | node2vec: Scalable Feature Learning for Networks 8 | Aditya Grover and Jure Leskovec 9 | Knowledge Discovery and Data Mining (KDD), 2016 10 | ''' 11 | 12 | import argparse 13 | import numpy as np 14 | import networkx as nx 15 | import node2vec 16 | from gensim.models import Word2Vec 17 | 18 | def parse_args(): 19 | ''' 20 | Parses the node2vec arguments. 21 | ''' 22 | parser = argparse.ArgumentParser(description="Run node2vec.") 23 | 24 | parser.add_argument('--input', nargs='?', default='graph/karate.edgelist', 25 | help='Input graph path') 26 | 27 | parser.add_argument('--output', nargs='?', default='emb/karate.emb', 28 | help='Embeddings path') 29 | 30 | parser.add_argument('--dimensions', type=int, default=128, 31 | help='Number of dimensions. Default is 128.') 32 | 33 | parser.add_argument('--walk-length', type=int, default=80, 34 | help='Length of walk per source. Default is 80.') 35 | 36 | parser.add_argument('--num-walks', type=int, default=10, 37 | help='Number of walks per source. Default is 10.') 38 | 39 | parser.add_argument('--window-size', type=int, default=10, 40 | help='Context size for optimization. Default is 10.') 41 | 42 | parser.add_argument('--iter', default=1, type=int, 43 | help='Number of epochs in SGD') 44 | 45 | parser.add_argument('--workers', type=int, default=8, 46 | help='Number of parallel workers. Default is 8.') 47 | 48 | parser.add_argument('--p', type=float, default=1, 49 | help='Return hyperparameter. Default is 1.') 50 | 51 | parser.add_argument('--q', type=float, default=1, 52 | help='Inout hyperparameter. Default is 1.') 53 | 54 | parser.add_argument('--weighted', dest='weighted', action='store_true', 55 | help='Boolean specifying (un)weighted. Default is unweighted.') 56 | parser.add_argument('--unweighted', dest='unweighted', action='store_false') 57 | parser.set_defaults(weighted=False) 58 | 59 | parser.add_argument('--directed', dest='directed', action='store_true', 60 | help='Graph is (un)directed. Default is undirected.') 61 | parser.add_argument('--undirected', dest='undirected', action='store_false') 62 | parser.set_defaults(directed=False) 63 | 64 | return parser.parse_args() 65 | 66 | def read_graph(): 67 | ''' 68 | Reads the input network in networkx. 69 | ''' 70 | if args.weighted: 71 | G = nx.read_edgelist(args.input, nodetype=int, data=(('weight',float),), create_using=nx.DiGraph()) 72 | else: 73 | G = nx.read_edgelist(args.input, nodetype=int, create_using=nx.DiGraph()) 74 | for edge in G.edges(): 75 | G[edge[0]][edge[1]]['weight'] = 1 76 | 77 | if not args.directed: 78 | G = G.to_undirected() 79 | 80 | return G 81 | 82 | def learn_embeddings(walks): 83 | ''' 84 | Learn embeddings by optimizing the Skipgram objective using SGD. 85 | ''' 86 | walks = [map(str, walk) for walk in walks] 87 | model = Word2Vec(walks, size=args.dimensions, window=args.window_size, min_count=0, sg=1, workers=args.workers, iter=args.iter) 88 | model.save_word2vec_format(args.output) 89 | 90 | return 91 | 92 | def main(args): 93 | ''' 94 | Pipeline for representational learning for all nodes in a graph. 95 | ''' 96 | nx_G = read_graph() 97 | G = node2vec.Graph(nx_G, args.directed, args.p, args.q) 98 | G.preprocess_transition_probs() 99 | walks = G.simulate_walks(args.num_walks, args.walk_length) 100 | learn_embeddings(walks) 101 | 102 | if __name__ == "__main__": 103 | args = parse_args() 104 | main(args) 105 | -------------------------------------------------------------------------------- /src/node2vec.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import networkx as nx 3 | import random 4 | 5 | 6 | class Graph(): 7 | def __init__(self, nx_G, is_directed, p, q): 8 | self.G = nx_G 9 | self.is_directed = is_directed 10 | self.p = p 11 | self.q = q 12 | 13 | def node2vec_walk(self, walk_length, start_node): 14 | ''' 15 | Simulate a random walk starting from start node. 16 | ''' 17 | G = self.G 18 | alias_nodes = self.alias_nodes 19 | alias_edges = self.alias_edges 20 | 21 | walk = [start_node] 22 | 23 | while len(walk) < walk_length: 24 | cur = walk[-1] 25 | cur_nbrs = sorted(G.neighbors(cur)) 26 | if len(cur_nbrs) > 0: 27 | if len(walk) == 1: 28 | walk.append(cur_nbrs[alias_draw(alias_nodes[cur][0], alias_nodes[cur][1])]) 29 | else: 30 | prev = walk[-2] 31 | next = cur_nbrs[alias_draw(alias_edges[(prev, cur)][0], 32 | alias_edges[(prev, cur)][1])] 33 | walk.append(next) 34 | else: 35 | break 36 | 37 | return walk 38 | 39 | def simulate_walks(self, num_walks, walk_length): 40 | ''' 41 | Repeatedly simulate random walks from each node. 42 | ''' 43 | G = self.G 44 | walks = [] 45 | nodes = list(G.nodes()) 46 | print 'Walk iteration:' 47 | for walk_iter in range(num_walks): 48 | print str(walk_iter+1), '/', str(num_walks) 49 | random.shuffle(nodes) 50 | for node in nodes: 51 | walks.append(self.node2vec_walk(walk_length=walk_length, start_node=node)) 52 | 53 | return walks 54 | 55 | def get_alias_edge(self, src, dst): 56 | ''' 57 | Get the alias edge setup lists for a given edge. 58 | ''' 59 | G = self.G 60 | p = self.p 61 | q = self.q 62 | 63 | unnormalized_probs = [] 64 | for dst_nbr in sorted(G.neighbors(dst)): 65 | if dst_nbr == src: 66 | unnormalized_probs.append(G[dst][dst_nbr]['weight']/p) 67 | elif G.has_edge(dst_nbr, src): 68 | unnormalized_probs.append(G[dst][dst_nbr]['weight']) 69 | else: 70 | unnormalized_probs.append(G[dst][dst_nbr]['weight']/q) 71 | norm_const = sum(unnormalized_probs) 72 | normalized_probs = [float(u_prob)/norm_const for u_prob in unnormalized_probs] 73 | 74 | return alias_setup(normalized_probs) 75 | 76 | def preprocess_transition_probs(self): 77 | ''' 78 | Preprocessing of transition probabilities for guiding the random walks. 79 | ''' 80 | G = self.G 81 | is_directed = self.is_directed 82 | 83 | alias_nodes = {} 84 | for node in G.nodes(): 85 | unnormalized_probs = [G[node][nbr]['weight'] for nbr in sorted(G.neighbors(node))] 86 | norm_const = sum(unnormalized_probs) 87 | normalized_probs = [float(u_prob)/norm_const for u_prob in unnormalized_probs] 88 | alias_nodes[node] = alias_setup(normalized_probs) 89 | 90 | alias_edges = {} 91 | triads = {} 92 | 93 | if is_directed: 94 | for edge in G.edges(): 95 | alias_edges[edge] = self.get_alias_edge(edge[0], edge[1]) 96 | else: 97 | for edge in G.edges(): 98 | alias_edges[edge] = self.get_alias_edge(edge[0], edge[1]) 99 | alias_edges[(edge[1], edge[0])] = self.get_alias_edge(edge[1], edge[0]) 100 | 101 | self.alias_nodes = alias_nodes 102 | self.alias_edges = alias_edges 103 | 104 | return 105 | 106 | 107 | def alias_setup(probs): 108 | ''' 109 | Compute utility lists for non-uniform sampling from discrete distributions. 110 | Refer to https://hips.seas.harvard.edu/blog/2013/03/03/the-alias-method-efficient-sampling-with-many-discrete-outcomes/ 111 | for details 112 | ''' 113 | K = len(probs) 114 | q = np.zeros(K) 115 | J = np.zeros(K, dtype=np.int) 116 | 117 | smaller = [] 118 | larger = [] 119 | for kk, prob in enumerate(probs): 120 | q[kk] = K*prob 121 | if q[kk] < 1.0: 122 | smaller.append(kk) 123 | else: 124 | larger.append(kk) 125 | 126 | while len(smaller) > 0 and len(larger) > 0: 127 | small = smaller.pop() 128 | large = larger.pop() 129 | 130 | J[small] = large 131 | q[large] = q[large] + q[small] - 1.0 132 | if q[large] < 1.0: 133 | smaller.append(large) 134 | else: 135 | larger.append(large) 136 | 137 | return J, q 138 | 139 | def alias_draw(J, q): 140 | ''' 141 | Draw sample from a non-uniform discrete distribution using alias sampling. 142 | ''' 143 | K = len(J) 144 | 145 | kk = int(np.floor(np.random.rand()*K)) 146 | if np.random.rand() < q[kk]: 147 | return kk 148 | else: 149 | return J[kk] --------------------------------------------------------------------------------