使用Bert预训练模型库小例子

使用Hugging face获取bert预训练模型,用于文本分类


“我爱你宝贝”文本处理

#导入Hugging face提供的transformers预训练模型库
from transformers import AutoTokenizer,TFAutoModel
#引入编码器和编码器模型
tokenizer = AutoTokenizer.from_pretrained("bert-base-chinese")
model = TFAutoModel.from_pretrained("bert-base-chinese")
#使用encoding获取token
input_ids = tokenizer.encode('我爱你宝贝',return_tensors='tf')
print(input_ids)
print('\n')
#获取三个不同的token表示
inputs = tokenizer('我爱你宝贝',return_tensors='tf')
print(inputs)
print('\n')
#我爱你宝贝的embedding值
embedding = model(input_ids)
print(embedding)

其中embedding层的理解:添加链接描述
输出结果:

tf.Tensor([[ 101 2769 4263  872 2140 6564  102]], shape=(1, 7), dtype=int32)


{'input_ids': <tf.Tensor: shape=(1, 7), dtype=int32, numpy=array([[ 101, 2769, 4263,  872, 2140, 6564,  102]])>, 'token_type_ids': <tf.Tensor: shape=(1, 7), dtype=int32, numpy=array([[0, 0, 0, 0, 0, 0, 0]])>, 'attention_mask': <tf.Tensor: shape=(1, 7), dtype=int32, numpy=array([[1, 1, 1, 1, 1, 1, 1]])>}


TFBaseModelOutputWithPoolingAndCrossAttentions(last_hidden_state=<tf.Tensor: shape=(1, 7, 768), dtype=float32, numpy=
array([[[ 0.22551674,  0.2334234 , -0.0965183 , ...,  0.23391467,
          0.12448315, -0.44088104],
        [ 0.09874093, -0.05070332,  0.1096081 , ..., -0.63226104,
         -0.36233944, -0.0175628 ],
        [ 0.781981  , -0.41929916, -1.0031092 , ...,  0.3007508 ,
          0.51924443, -0.3144598 ],
        ...,
        [ 0.45716444, -0.89420587, -0.7758167 , ..., -0.3758897 ,
          0.59490716, -0.5254687 ],
        [ 0.5230568 ,  0.04452747, -0.6349134 , ..., -0.1745303 ,
         -0.36457115, -0.2131085 ],
        [-0.37484476, -0.09532013, -0.22620994, ...,  0.09939153,
          0.02245922, -0.5798783 ]]], dtype=float32)>, pooler_output=<tf.Tensor: shape=(1, 768), dtype=float32, numpy=
array([[ 0.99951375,  0.9999585 ,  0.9992242 ,  0.9521314 ,  0.6772908 ,
         0.95550525, -0.8690266 , -0.8966604 ,  0.9862441 , -0.99944973,
         0.9999984 ,  0.99952954, -0.6903689 , -0.94052505,  0.999907  ,
        -0.99993294, -0.48965782,  0.99795926,  0.98542386, -0.41674206,
         0.99983186, -0.99999464, -0.9364351 , -0.00638309,  0.30202952,
         0.9982603 ,  0.95836174, -0.85390073, -0.999881  ,  0.99924105,
         0.9449711 ,  0.999661  ,  0.77207   , -0.9999498 , -0.99956626,
         0.83075035,  0.52835643,  0.98546475, -0.42761192, -0.88479286,
        -0.95826435, -0.7290919 , -0.57517326, -0.9991556 , -0.9649854 ,
         0.8852386 , -1.        , -0.9999767 , -0.0638053 ,  0.99986666,
        -0.9731981 , -0.99992514,  0.9107537 , -0.6539564 , -0.59388995,
         0.989578  , -0.99978274,  0.9870353 ,  1.        ,  0.8631872 ,
         0.9998498 , -0.969598  ,  0.13232271, -0.999937  ,  0.99999726,
        -0.99966043, -0.9971533 ,  0.3563019 ,  0.99992895,  0.9999971 ,
        -0.9614805 ,  0.9980497 ,  0.99999785,  0.69701064,  0.9113608 ,
         0.9994441 , -0.9994306 ,  0.79707325, -0.9999976 ,  0.30055138,
         0.9999992 ,  0.9980529 , -0.98713857,  0.9572292 , -0.9946065 ,
        -0.999979  , -0.9990661 ,  0.9999576 , -0.43093085,  0.99787974,
         0.9981298 , -0.99936944, -0.999999  ,  0.999266  , -0.9997233 ,
        -0.9996603 , -0.20400661,  0.9990691 , -0.29508907, -0.8908759 ,
        -0.86387736,  0.7382464 , -0.99878937, -0.9980564 ,  0.9502611 ,
         0.9989777 ,  0.8173981 , -0.9997744 ,  0.99990267,  0.78615505,
        -1.        , -0.95035785, -0.9999165 , -0.8321738 , -0.99752355,
         0.9999309 ,  0.81204945, -0.1566339 ,  0.99993634, -0.9978586 ,
         0.97382927, -0.998567  , -0.8548095 , -0.3403496 ,  0.9902874 ,
         0.9999813 ,  0.9991445 , -0.9954823 ,  0.99946445,  0.99999285,
         0.9831586 ,  0.9952379 , -0.9989788 ,  0.989922  ,  0.7232165 ,
        -0.97291386, -0.53481036, -0.9572743 ,  0.99999964,  0.9920743 ,
         0.9897406 , -0.9922357 ,  0.9994006 , -0.9986608 ,  0.9999755 ,
        -0.9999974 ,  0.9997875 , -0.99999416, -0.9994994 ,  0.99797654,
         0.95532805,  0.9999995 , -0.8909783 ,  0.9999773 , -0.99596953,
        -0.9998977 ,  0.9993657 ,  0.40027827,  0.9921006 , -0.9999804 ,
         0.98359376, -0.03588968, -0.5064751 , -0.32479793, -1.        ,
         0.9999594 , -0.90203744,  0.9999972 ,  0.9954126 , -0.9955841 ,
        -0.9808943 , -0.9968977 ,  0.66703343, -0.99971366, -0.96350837,
         0.99084705, -0.2077221 ,  0.99952465, -0.7488135 , -0.9851071 ,
         0.87106735, -0.88655657, -0.9999118 ,  0.98245955, -0.7387749 ,
         0.90645766,  0.28922993,  0.62658584,  0.984331  ,  0.8896224 ,
        -0.85062736,  0.9999931 ,  0.6364938 ,  0.9975679 ,  0.9993347 ,
         0.1677256 , -0.7637223 , -0.9097002 , -0.99999416, -0.89005023,
         0.9999735 , -0.7679188 , -0.99959093,  0.8759577 , -0.9999887 ,
         0.8598826 ,  0.673738  ,  0.19542514, -0.9997235 , -0.9999812 ,
         0.9999633 , -0.9919162 , -0.999416  ,  0.13023168, -0.4842012 ,
        -0.04413388, -0.9995727 ,  0.80254287,  0.9825549 , -0.1383966 ,
         0.9795868 , -0.8842249 , -0.99779236,  0.9992252 , -0.9800692 ,
         0.7213256 ,  0.9732129 ,  0.99999905,  0.98257124, -0.89109504,
        -0.2989888 ,  0.99994105,  0.62901574, -1.        ,  0.9641136 ,
        -0.9954345 , -0.48818251,  0.9999681 , -0.99599165,  0.8507665 ,
         0.9999939 ,  0.9715773 ,  1.        ,  0.23156907, -0.99945706,
        -0.9995524 ,  0.99999917,  0.96865034,  0.9999502 , -0.99948746,
        -0.99917036,  0.42960355, -0.73817587, -0.9999985 , -0.999202  ,
        -0.3500754 ,  0.99888885,  0.99999183,  0.36791554, -0.9992521 ,
        -0.9908179 , -0.9998418 ,  0.99999774, -0.98939997,  0.99997765,
         0.9902801 , -0.99194014, -0.9611848 ,  0.55937403, -0.8523637 ,
        -0.9996996 ,  0.79383516, -0.9998934 , -0.9933539 , -0.99996805,
         0.9318411 , -0.99970216, -0.9999995 ,  0.96765715,  0.9999861 ,
         0.8565581 , -0.9999471 ,  0.9998005 ,  0.99741864, -0.7784989 ,
        -0.99968934,  0.8392054 , -0.9999996 ,  0.9999998 , -0.9994854 ,
         0.93166095, -0.8005207 , -0.9949263 , -0.41130367,  0.9995809 ,
         0.99713916, -0.9951959 , -0.18618147, -0.99500746, -0.99870086,
        -0.7509314 ,  0.92250127, -0.80903697,  0.49754295, -0.9628656 ,
        -0.9807618 ,  0.891106  , -0.9792823 , -0.9998097 ,  0.7365432 ,
         0.99999714, -0.9586837 ,  1.        ,  0.9671251 ,  0.9999995 ,
         0.936821  , -0.9994404 ,  0.999575  ,  0.70637655, -0.860012  ,
        -0.99922866, -0.9715462 ,  0.94944346,  0.49256256, -0.18733643,
        -0.99984694,  0.99992305,  0.9888529 ,  0.93001896,  0.8485084 ,
        -0.41235867, -0.25817376,  0.96832037, -0.9961326 ,  0.99835235,
        -0.9997181 , -0.9917206 ,  0.9997797 ,  0.9999836 ,  0.998336  ,
         0.63495827, -0.9512613 ,  0.9934805 , -0.9985411 ,  0.99970275,
        -0.9998071 ,  0.9992197 , -0.9934266 ,  0.1454727 , -0.92392623,
        -0.99633276,  0.999995  ,  0.9878752 , -0.42107308,  0.9998178 ,
        -0.9878549 ,  0.9945993 ,  0.9835186 ,  0.9953428 ,  0.96659714,
         0.98338646,  0.99998194, -0.99612033, -0.9935541 , -0.92737514,
        -0.99712515, -0.9990756 , -0.9999986 ,  0.58737713, -0.9984052 ,
        -0.9961222 , -0.6528244 ,  0.7096545 ,  0.95586807, -0.38046107,
         0.74068713,  0.473657  ,  0.6141841 , -0.20215689,  0.5548849 ,
         0.98438865, -0.9971496 , -0.9946532 , -0.99999297, -0.9993225 ,
         0.94200975,  0.9998952 , -0.9998909 ,  0.99921596, -0.9999831 ,
        -0.9985282 ,  0.99882054, -0.79864633, -0.8544362 ,  0.99968654,
        -0.9999791 ,  0.92368674,  0.9997159 ,  1.        ,  0.9998181 ,
         0.9999495 , -0.87195814, -0.9999577 , -0.9986795 , -0.99996364,
        -0.99999803, -0.999907  ,  0.92731327,  0.75620824, -0.99999505,
        -0.7328234 ,  0.99145705,  0.999997  ,  0.988811  , -0.99976283,
        -0.77284473, -0.9995287 , -0.99966085,  0.9989632 , -0.9446032 ,
        -0.99988997,  0.99617827, -0.05445317,  0.9999809 , -0.7030514 ,
         0.92149156,  0.8852448 ,  0.9638466 ,  0.99250394, -0.9999869 ,
         0.6906826 ,  0.9999937 ,  0.92754287, -0.9999963 , -0.9679839 ,
        -0.78732204, -0.99995714, -0.70257646,  0.77674127,  0.9999505 ,
        -0.9999676 , -0.833869  , -0.99814373,  0.92392987,  0.99506813,
         0.99963397,  0.9997788 ,  0.87824357,  0.8918311 ,  0.9900744 ,
         0.32406652,  0.99987346,  0.4580068 , -0.998568  ,  0.99954724,
        -0.66389877,  0.63000214, -0.9998998 ,  0.99870896,  0.8527977 ,
         0.99994916,  0.9983771 ,  0.02654447, -0.9966444 , -0.9209089 ,
         0.9962621 ,  0.9999987 , -0.99568087, -0.9748604 , -0.99959505,
        -0.9998028 , -0.99858516, -0.9630988 , -0.23769446, -0.99604124,
        -0.9997537 ,  0.71335334,  0.8423182 ,  0.9999992 ,  0.9999803 ,
         0.99913895, -0.979068  , -0.9597613 ,  0.9927908 ,  0.33670604,
         0.9602235 , -0.857165  , -0.99999964, -0.9990654 , -0.9995447 ,
         0.9999045 ,  0.23595741, -0.77770877, -0.8659783 ,  0.09423243,
         0.93034405, -0.99995977, -0.63107926, -0.99041086,  0.8261902 ,
         0.9999982 , -0.9980706 ,  0.9996412 , -0.99944156,  0.8404289 ,
         0.74455214,  0.9685205 ,  0.99934334, -0.3510783 ,  0.22488539,
        -0.74096036,  0.9169256 ,  0.91471493,  0.99873304, -0.98465353,
         0.56764007,  0.9996666 , -0.9769553 ,  0.99997365,  0.42663586,
         0.8431777 ,  0.85645   ,  0.9999884 ,  0.7255782 ,  0.9998816 ,
         0.9856115 ,  0.9999716 ,  0.9999753 , -0.99225116,  0.753374  ,
         0.24214263, -0.9915894 , -0.81609374,  0.94391257,  0.99974936,
         0.53414357, -0.99619836, -0.99977595,  0.9915747 ,  0.9998634 ,
         1.        , -0.78952557,  0.9991585 , -0.58179784,  0.9320144 ,
         0.91929686,  0.83287185,  0.2685785 ,  0.78830725,  0.99521255,
         0.999661  , -0.9999786 , -0.9999019 , -1.        ,  0.9999993 ,
         0.99958885, -0.7078647 , -0.9999997 ,  0.9996776 , -0.4789279 ,
         0.80940926,  0.99401075,  0.4332002 , -0.9423578 ,  0.91720015,
        -0.999896  ,  0.23369907,  0.85708565,  0.93265694,  0.47120795,
         0.9996671 , -0.9998968 , -0.09277818,  0.99999547, -0.49733144,
         0.9999703 ,  0.47026387, -0.9984143 ,  0.999415  , -0.9957429 ,
        -0.99995595, -0.47652856,  0.9999315 ,  0.99897647, -0.0714012 ,
        -0.660234  ,  0.9999095 , -0.99962586,  0.9999663 , -0.99998087,
         0.35069603, -0.99974513,  0.9999557 , -0.9897224 , -0.9954708 ,
        -0.92215693,  0.45113984,  0.97567034, -0.863757  ,  0.9999685 ,
         0.0522829 , -0.943007  ,  0.45604616, -0.948004  , -0.99760824,
        -0.98861593,  0.5688474 , -0.9999684 ,  0.7760274 , -0.3933425 ,
        -0.9369243 , -0.9757494 , -0.99997884,  0.99997   , -0.7008528 ,
        -0.99214804,  0.9999752 , -0.98768944, -0.99999917,  0.9772849 ,
        -0.99743456,  0.12691952,  0.9924299 ,  0.66166747,  0.31455383,
        -0.9999932 ,  0.71131855,  0.9999092 , -0.9988719 , -0.9272292 ,
        -0.95572054, -0.9974266 ,  0.9393952 ,  0.999705  ,  0.87913877,
        -0.55481017,  0.88223374,  0.9862479 ,  0.8830329 ,  0.50108755,
         0.68632865, -0.99755585, -0.9988226 , -0.9862296 , -0.9964491 ,
        -0.99990624, -0.9999877 ,  0.9999996 ,  0.9997305 ,  0.9999743 ,
        -0.35132548, -0.7829424 ,  0.95596844,  0.9905246 , -0.99990946,
        -0.8540817 ,  0.8835654 ,  0.8612701 , -0.2369774 , -0.99967813,
        -0.40206575, -0.999984  , -0.98034614,  0.60247403, -0.96062344,
         0.59596944,  0.999995  ,  0.99994415, -0.999774  , -0.99862605,
        -0.9982397 , -0.99851394,  0.9999655 ,  0.99967384,  0.99990207,
        -0.93322766, -0.7911631 ,  0.9989075 , -0.94033885,  0.00478262,
        -0.99875724, -0.99710125, -0.9999525 ,  0.965285  , -0.99473554,
        -0.99982005,  0.9963372 ,  0.99995404,  0.81072396, -0.9999799 ,
        -0.62058717,  0.9999424 ,  0.9989505 ,  0.99999917,  0.43074998,
         0.99993634, -0.99659646,  0.9925494 , -0.9946671 ,  0.99999356,
        -0.9999964 ,  0.9999916 ,  0.9999731 ,  0.9993531 ,  0.9986887 ,
        -0.9972332 ,  0.91004014, -0.9830419 , -0.6273834 ,  0.9469267 ,
        -0.64788556, -0.99702114,  0.30507144,  0.9915664 , -0.97477734,
         0.99999803,  0.52740014,  0.4617958 ,  0.88484925,  0.20282666,
         0.999112  ,  0.6436424 , -0.9994612 ,  0.976302  ,  0.99895203,
         0.99344224,  0.99999726,  0.9824398 ,  0.9999667 , -0.9545235 ,
        -0.9966018 ,  0.99578434, -0.77346295,  0.37438408, -0.9998523 ,
         0.99999285,  0.9999461 , -0.9998808 , -0.9971094 ,  0.4471147 ,
         0.93076485,  0.9999655 ,  0.99595094,  0.99771625,  0.9032935 ,
        -0.06329969,  0.99881697, -0.9992911 ,  0.9953064 , -0.99379414,
        -0.99307436,  0.99998784, -0.9926064 ,  0.99973637, -0.994869  ,
         0.99974704, -0.996418  ,  0.98057634,  0.9941156 ,  0.9678984 ,
        -0.98769575,  0.99999505,  0.49111098, -0.99909806, -0.99963087,
        -0.99700135, -0.97970676,  0.80996   ]], dtype=float32)>, past_key_values=None, hidden_states=None, attentions=None, cross_attentions=None)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值