如何使用 tensorflow hub.module 访问嵌入?
How to access the embeddings using tensorflow hub.module?
我正在使用以下代码访问使用 TF Hub 通用句子编码器的嵌入。
import tensorflow as tf
import tensorflow_hub as hub
model = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
def embed(input):
return model(input)
messages = ["There is no hard limit on how long the paragraph is. Roughly, the longer the more 'diluted' the embedding will be."]
message_embeddings = embed(messages)
我现在如何访问实际的向量?
可以从变量访问实际嵌入向量,message_embeddings
。
message_embeddings
是shape=(1, 512)
的Vector,也就是说USE-4
返回的Vector的Dimensionality
是512。
换句话说,每个句子都被编码成 512
列向量。
代码的输出,
print(message_embeddings)
是
tf.Tensor(
[[-0.00366504 -0.00703163 -0.0061244 0.02026021 -0.09436475 0.00027828
0.05004153 -0.01591516 0.088241 0.07551358 -0.01868021 0.04386544
0.00105771 0.03730893 -0.05554571 0.02852311 0.01709696 0.08152976
-0.03092775 0.00683713 -0.08059237 0.042355 -0.07580714 -0.00443942
-0.03430099 0.03240041 -0.05212452 -0.04247908 -0.05534476 -0.02328587
-0.0438301 -0.03972115 0.01639873 0.00163302 0.07708091 -0.02310511
0.01288455 0.04831124 0.0089498 -0.02632253 -0.01840279 0.02118563
0.03758964 0.08740229 0.02880297 -0.00486817 0.0115555 -0.00451289
-0.00162866 0.01446948 0.00189139 -0.07941346 -0.0216493 -0.02580371
-0.00930381 -0.00526039 -0.01272183 0.02215818 0.04742621 0.02226813
0.0110765 -0.01790449 0.01739751 -0.08388933 0.05826297 -0.05230762
-0.07484917 0.06905693 0.01646299 0.00850342 -0.0022191 -0.07555264
0.01601691 0.06028103 0.00524664 0.03776945 -0.05246941 0.03556651
0.06253887 -0.04647287 -0.03415112 -0.03473583 0.04833042 -0.01264609
0.01788526 -0.07143527 -0.02432756 0.04081429 -0.0524265 -0.05402376
-0.02753968 0.06558003 0.01936845 -0.08112626 0.0157347 0.05620547
-0.06219236 -0.03654391 0.03936478 -0.01247254 -0.03957544 0.07394353
-0.06131149 -0.0550663 0.08301188 -0.01699291 0.03726438 0.00248359
-0.00569713 0.04109528 -0.05154289 0.05428214 -0.06594346 0.06009263
0.02753788 0.01492724 -0.01422153 0.02779302 0.02881143 -0.01985389
0.05809831 -0.02661227 -0.06907296 0.01192496 -0.03630216 0.03146286
-0.02979902 0.05192203 -0.0479207 0.03564131 0.05351846 0.02681697
0.02597373 -0.03392426 -0.05286925 -0.05110073 0.01331552 -0.00612995
-0.04932296 -0.0185418 -0.0841584 0.02415963 -0.01051812 0.05603031
-0.0083728 -0.05966095 0.0321536 -0.03968453 0.03799454 -0.05958865
-0.07585841 0.04390398 -0.03674331 0.01918785 0.03446485 -0.04106916
-0.05183128 0.02947152 -0.03531763 0.03698466 0.06261521 -0.00646621
0.01130813 -0.02275244 -0.04280937 0.01955702 -0.03919312 0.00476116
0.01887495 -0.00195181 -0.02401051 -0.06942239 -0.06978329 0.06458326
0.00362934 0.03588834 0.04921037 -0.03195003 0.02806171 -0.0193333
0.00994556 -0.02342404 0.10165592 -0.02853323 0.04147425 0.00914851
0.00497671 0.00073764 -0.00318258 0.03595887 -0.01817959 0.01496308
-0.03551586 0.02536247 -0.07170779 -0.03153825 -0.04042004 -0.01769615
0.00958568 0.00038516 0.00799816 0.04089458 0.02171035 -0.08852603
-0.06747856 0.05664572 -0.06597329 0.02299296 0.03397151 -0.03845559
0.00395073 0.00314357 0.01119022 0.05957965 -0.05583638 0.02908287
0.0112076 0.07695369 -0.03935304 -0.02383705 -0.04208985 -0.00359387
0.06851663 -0.05395376 -0.00246254 -0.01888378 -0.01391678 -0.07573339
0.05811501 0.02059502 -0.00418438 -0.01210096 -0.06286791 -0.07645103
-0.02463043 -0.03153505 0.05593796 -0.02202086 -0.00274707 0.04458077
-0.06263509 0.06126784 -0.04235342 0.00322403 0.02189728 -0.06388599
-0.03919036 -0.00010863 0.02531325 0.02581233 -0.01304512 -0.03001025
-0.02754986 0.0531372 -0.02369525 -0.04376267 0.0641819 0.09532097
-0.06730784 0.04478338 0.02004733 0.05244097 -0.01885018 -0.06137342
-0.08407518 -0.00084469 -0.02145135 -0.0091182 -0.06907462 0.06986497
0.0600312 -0.04390564 -0.00131028 0.06390417 0.03533437 0.03813365
0.04030495 -0.01402102 -0.06857175 -0.06571147 0.01421791 -0.0381003
-0.04138157 0.05040992 -0.05724671 0.01490439 -0.07905842 -0.03806996
-0.01071311 -0.01229521 -0.00771822 -0.03641455 -0.04578875 0.00925799
0.0403841 0.00132017 0.031641 0.01162737 0.0101506 -0.01761867
0.0579349 0.03595775 -0.01147426 -0.01525036 0.05006553 0.03747585
-0.05307707 -0.08915938 0.02942844 -0.05546442 -0.0128964 0.04225868
-0.01534053 -0.04580414 0.01088955 -0.03184818 0.02326705 -0.08861458
-0.07253686 -0.02572111 -0.03711193 0.0474383 -0.05628109 -0.01391787
0.00941848 -0.06177152 -0.06071901 -0.0092127 -0.10220838 -0.01376523
0.03162379 0.03983926 0.00640659 -0.00418033 -0.01612685 0.01891562
-0.04313575 0.01139805 -0.00378637 0.08349139 0.08300766 -0.0494319
-0.03658734 0.00325003 -0.05251636 -0.04457545 -0.079386 -0.05799922
-0.01254137 0.02311826 -0.00766293 -0.06729192 -0.03971054 -0.0663051
0.08720677 0.04582898 -0.08557201 -0.01054355 -0.02762848 0.06243869
-0.08848279 0.02289506 0.05723204 -0.01221769 -0.0393519 -0.00582338
0.02841124 -0.03293297 -0.03143778 -0.00352248 0.0073043 0.01209227
-0.00148794 0.03695554 0.03136331 -0.03311655 -0.0221175 -0.07959055
-0.04138357 -0.00950083 -0.01173625 0.01499144 -0.0121095 0.00823302
0.07642982 0.05198056 0.05955188 0.03240911 0.09211077 -0.05317325
-0.06024589 0.00489183 0.04719653 0.02498623 0.03750401 -0.02352423
0.05042319 -0.01633615 -0.02236294 0.04443104 0.02694818 0.00881322
0.02469178 -0.06206469 -0.00215397 -0.02641553 0.00405129 -0.07184313
-0.02841844 0.0309756 0.02459977 -0.03155032 0.01407542 0.00524732
-0.01893367 0.0102607 -0.00333736 0.02885202 -0.03275619 -0.08507563
0.02076722 -0.02471628 -0.00449985 0.0004644 -0.0923043 0.02101186
0.0352884 0.03790538 -0.00372656 0.06751391 0.02638355 0.01678842
0.03843728 0.10451197 -0.06375936 -0.05324562 0.03276567 -0.01112294
-0.0082361 -0.01735083 -0.03767544 -0.04266915 -0.04767371 0.07573947
-0.01247379 -0.01048137 -0.02308911 -0.01484709 -0.00733855 0.06788232
-0.08163249 -0.01530467 -0.01805264 -0.07910046 -0.06530869 0.07402557
0.06713054 -0.01659747 -0.00980262 0.05586078 0.03396358 -0.06102567
-0.06640005 0.02269907 0.03265672 -0.01353668 -0.08313932 -0.02356159
-0.03383274 0.05942128 -0.08610516 -0.08445066 -0.01306568 -0.05279852
0.00986506 0.00461306 0.08119206 0.00604 0.10107437 0.00191085
-0.05926891 0.01157635 0.0284292 -0.08671403 0.01851062 0.05745851
-0.06798992 0.02700593 0.00208116 -0.00829788 0.08901995 -0.00418414
-0.06217562 -0.07832154 0.02027107 0.06713033 0.04617893 0.05885412
-0.04505047 0.09581003 0.033753 -0.00888314 -0.07608356 -0.03729891
0.02724086 0.02371461 -0.01081131 -0.00809431 -0.04376922 -0.04656423
0.00886904 0.01995739]], shape=(1, 512), dtype=float32)
希望这对您有所帮助。快乐学习!
我正在使用以下代码访问使用 TF Hub 通用句子编码器的嵌入。
import tensorflow as tf
import tensorflow_hub as hub
model = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
def embed(input):
return model(input)
messages = ["There is no hard limit on how long the paragraph is. Roughly, the longer the more 'diluted' the embedding will be."]
message_embeddings = embed(messages)
我现在如何访问实际的向量?
可以从变量访问实际嵌入向量,message_embeddings
。
message_embeddings
是shape=(1, 512)
的Vector,也就是说USE-4
返回的Vector的Dimensionality
是512。
换句话说,每个句子都被编码成 512
列向量。
代码的输出,
print(message_embeddings)
是
tf.Tensor(
[[-0.00366504 -0.00703163 -0.0061244 0.02026021 -0.09436475 0.00027828
0.05004153 -0.01591516 0.088241 0.07551358 -0.01868021 0.04386544
0.00105771 0.03730893 -0.05554571 0.02852311 0.01709696 0.08152976
-0.03092775 0.00683713 -0.08059237 0.042355 -0.07580714 -0.00443942
-0.03430099 0.03240041 -0.05212452 -0.04247908 -0.05534476 -0.02328587
-0.0438301 -0.03972115 0.01639873 0.00163302 0.07708091 -0.02310511
0.01288455 0.04831124 0.0089498 -0.02632253 -0.01840279 0.02118563
0.03758964 0.08740229 0.02880297 -0.00486817 0.0115555 -0.00451289
-0.00162866 0.01446948 0.00189139 -0.07941346 -0.0216493 -0.02580371
-0.00930381 -0.00526039 -0.01272183 0.02215818 0.04742621 0.02226813
0.0110765 -0.01790449 0.01739751 -0.08388933 0.05826297 -0.05230762
-0.07484917 0.06905693 0.01646299 0.00850342 -0.0022191 -0.07555264
0.01601691 0.06028103 0.00524664 0.03776945 -0.05246941 0.03556651
0.06253887 -0.04647287 -0.03415112 -0.03473583 0.04833042 -0.01264609
0.01788526 -0.07143527 -0.02432756 0.04081429 -0.0524265 -0.05402376
-0.02753968 0.06558003 0.01936845 -0.08112626 0.0157347 0.05620547
-0.06219236 -0.03654391 0.03936478 -0.01247254 -0.03957544 0.07394353
-0.06131149 -0.0550663 0.08301188 -0.01699291 0.03726438 0.00248359
-0.00569713 0.04109528 -0.05154289 0.05428214 -0.06594346 0.06009263
0.02753788 0.01492724 -0.01422153 0.02779302 0.02881143 -0.01985389
0.05809831 -0.02661227 -0.06907296 0.01192496 -0.03630216 0.03146286
-0.02979902 0.05192203 -0.0479207 0.03564131 0.05351846 0.02681697
0.02597373 -0.03392426 -0.05286925 -0.05110073 0.01331552 -0.00612995
-0.04932296 -0.0185418 -0.0841584 0.02415963 -0.01051812 0.05603031
-0.0083728 -0.05966095 0.0321536 -0.03968453 0.03799454 -0.05958865
-0.07585841 0.04390398 -0.03674331 0.01918785 0.03446485 -0.04106916
-0.05183128 0.02947152 -0.03531763 0.03698466 0.06261521 -0.00646621
0.01130813 -0.02275244 -0.04280937 0.01955702 -0.03919312 0.00476116
0.01887495 -0.00195181 -0.02401051 -0.06942239 -0.06978329 0.06458326
0.00362934 0.03588834 0.04921037 -0.03195003 0.02806171 -0.0193333
0.00994556 -0.02342404 0.10165592 -0.02853323 0.04147425 0.00914851
0.00497671 0.00073764 -0.00318258 0.03595887 -0.01817959 0.01496308
-0.03551586 0.02536247 -0.07170779 -0.03153825 -0.04042004 -0.01769615
0.00958568 0.00038516 0.00799816 0.04089458 0.02171035 -0.08852603
-0.06747856 0.05664572 -0.06597329 0.02299296 0.03397151 -0.03845559
0.00395073 0.00314357 0.01119022 0.05957965 -0.05583638 0.02908287
0.0112076 0.07695369 -0.03935304 -0.02383705 -0.04208985 -0.00359387
0.06851663 -0.05395376 -0.00246254 -0.01888378 -0.01391678 -0.07573339
0.05811501 0.02059502 -0.00418438 -0.01210096 -0.06286791 -0.07645103
-0.02463043 -0.03153505 0.05593796 -0.02202086 -0.00274707 0.04458077
-0.06263509 0.06126784 -0.04235342 0.00322403 0.02189728 -0.06388599
-0.03919036 -0.00010863 0.02531325 0.02581233 -0.01304512 -0.03001025
-0.02754986 0.0531372 -0.02369525 -0.04376267 0.0641819 0.09532097
-0.06730784 0.04478338 0.02004733 0.05244097 -0.01885018 -0.06137342
-0.08407518 -0.00084469 -0.02145135 -0.0091182 -0.06907462 0.06986497
0.0600312 -0.04390564 -0.00131028 0.06390417 0.03533437 0.03813365
0.04030495 -0.01402102 -0.06857175 -0.06571147 0.01421791 -0.0381003
-0.04138157 0.05040992 -0.05724671 0.01490439 -0.07905842 -0.03806996
-0.01071311 -0.01229521 -0.00771822 -0.03641455 -0.04578875 0.00925799
0.0403841 0.00132017 0.031641 0.01162737 0.0101506 -0.01761867
0.0579349 0.03595775 -0.01147426 -0.01525036 0.05006553 0.03747585
-0.05307707 -0.08915938 0.02942844 -0.05546442 -0.0128964 0.04225868
-0.01534053 -0.04580414 0.01088955 -0.03184818 0.02326705 -0.08861458
-0.07253686 -0.02572111 -0.03711193 0.0474383 -0.05628109 -0.01391787
0.00941848 -0.06177152 -0.06071901 -0.0092127 -0.10220838 -0.01376523
0.03162379 0.03983926 0.00640659 -0.00418033 -0.01612685 0.01891562
-0.04313575 0.01139805 -0.00378637 0.08349139 0.08300766 -0.0494319
-0.03658734 0.00325003 -0.05251636 -0.04457545 -0.079386 -0.05799922
-0.01254137 0.02311826 -0.00766293 -0.06729192 -0.03971054 -0.0663051
0.08720677 0.04582898 -0.08557201 -0.01054355 -0.02762848 0.06243869
-0.08848279 0.02289506 0.05723204 -0.01221769 -0.0393519 -0.00582338
0.02841124 -0.03293297 -0.03143778 -0.00352248 0.0073043 0.01209227
-0.00148794 0.03695554 0.03136331 -0.03311655 -0.0221175 -0.07959055
-0.04138357 -0.00950083 -0.01173625 0.01499144 -0.0121095 0.00823302
0.07642982 0.05198056 0.05955188 0.03240911 0.09211077 -0.05317325
-0.06024589 0.00489183 0.04719653 0.02498623 0.03750401 -0.02352423
0.05042319 -0.01633615 -0.02236294 0.04443104 0.02694818 0.00881322
0.02469178 -0.06206469 -0.00215397 -0.02641553 0.00405129 -0.07184313
-0.02841844 0.0309756 0.02459977 -0.03155032 0.01407542 0.00524732
-0.01893367 0.0102607 -0.00333736 0.02885202 -0.03275619 -0.08507563
0.02076722 -0.02471628 -0.00449985 0.0004644 -0.0923043 0.02101186
0.0352884 0.03790538 -0.00372656 0.06751391 0.02638355 0.01678842
0.03843728 0.10451197 -0.06375936 -0.05324562 0.03276567 -0.01112294
-0.0082361 -0.01735083 -0.03767544 -0.04266915 -0.04767371 0.07573947
-0.01247379 -0.01048137 -0.02308911 -0.01484709 -0.00733855 0.06788232
-0.08163249 -0.01530467 -0.01805264 -0.07910046 -0.06530869 0.07402557
0.06713054 -0.01659747 -0.00980262 0.05586078 0.03396358 -0.06102567
-0.06640005 0.02269907 0.03265672 -0.01353668 -0.08313932 -0.02356159
-0.03383274 0.05942128 -0.08610516 -0.08445066 -0.01306568 -0.05279852
0.00986506 0.00461306 0.08119206 0.00604 0.10107437 0.00191085
-0.05926891 0.01157635 0.0284292 -0.08671403 0.01851062 0.05745851
-0.06798992 0.02700593 0.00208116 -0.00829788 0.08901995 -0.00418414
-0.06217562 -0.07832154 0.02027107 0.06713033 0.04617893 0.05885412
-0.04505047 0.09581003 0.033753 -0.00888314 -0.07608356 -0.03729891
0.02724086 0.02371461 -0.01081131 -0.00809431 -0.04376922 -0.04656423
0.00886904 0.01995739]], shape=(1, 512), dtype=float32)
希望这对您有所帮助。快乐学习!