frt6668头像

python rnn onehot word2vec wordEmbedding

onehot 使用01代表关键字,但是比较稀疏,不连续

from itertools import chain
import jieba
from wordcloud import WordCloud
from tensorflow.keras.preprocessing.text import Tokenizer
import torch.nn as nn
# import torch.utils.tensorboard.summary as summary


items = ["我爱北京天安门","天安门上空飘扬着五星红旗","五星红旗是中华人民共和国的象征和标志","中华人民共和国的首都是北京"]
dict = chain(*map(lambda x: x, [jieba.lcut(item) for item in items]))
tokenizer = Tokenizer()
tokenizer.fit_on_texts(dict)
for key, value in tokenizer.word_index.items():
    temp = [0]*15
    temp[value-1] = 1
    print(f"{key}:{temp}")


# embedding = nn.Embedding(num_embeddings=15, embedding_dim=10)
# v = embedding(dict)
# print(v)






F:\Users\FRT\anaconda3\envs\test\python.exe E:\PycharmProjects\pythonProject\5.py 
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\FRT\AppData\Local\Temp\jieba.cache
Loading model cost 1.968 seconds.
Prefix dict has been built successfully.
北京:[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
天安门:[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
五星红旗:[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
是:[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
中华人民共和国:[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
的:[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
我:[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
爱:[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
上空:[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
飘扬:[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
着:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
象征:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
和:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
标志:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
首都:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]

Process finished with exit code 0
 

word2vec:cbow或者skipgram        使用两边预测中间或者两边预测中间

可以使用 facebook fasttext

fasttext于2024停止维护了https://github.com/facebookresearch/fastText

wordEmbedding  

from itertools import chain

# from tensorflow.keras.preprocessing.text import Tokenizer
import torch.nn as nn
import torch
# import torch.utils.tensorboard.summary as summary
embedding = nn.Embedding(num_embeddings=15, embedding_dim=10)

out = embedding(torch.randint(0, 15, (1, 10)))

print(out)


F:\Users\FRT\anaconda3\envs\test1\python.exe E:\PycharmProjects\pythonProject\5.py 
F:\Users\FRT\anaconda3\envs\test1\lib\python\site-packages\torch\_subclasses\functional_tensor.py:377: UserWarning: Failed to initialize NumPy: No module named 'numpy' (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\torch\csrc\utils\tensor_numpy.cpp:82.)
  cpu = _conversion_method_template(device=torch.device("cpu"))
tensor([[[ 0.3666,  0.8458,  0.3273, -0.8922, -0.2404, -0.9964, -0.0419,
           0.6741,  0.6711, -0.8970],
         [ 0.5502, -0.7511,  0.1944, -1.5899, -0.8633,  0.7071,  1.1062,
           1.3505,  0.0128,  1.0639],
         [-0.6336,  1.3766,  1.0069, -0.3985, -1.9157,  1.3479, -0.3454,
           0.6721, -0.4966, -0.2111],
         [ 1.5243, -0.0115,  0.3718, -0.8111,  0.5568,  0.4934,  1.3365,
          -0.1018,  0.0826,  2.9419],
         [-0.6336,  1.3766,  1.0069, -0.3985, -1.9157,  1.3479, -0.3454,
           0.6721, -0.4966, -0.2111],
         [ 0.3698,  1.3010, -0.1851,  1.3165, -1.0207, -0.0451, -1.1653,
          -0.7486, -0.9386,  0.9642],
         [ 0.3698,  1.3010, -0.1851,  1.3165, -1.0207, -0.0451, -1.1653,
          -0.7486, -0.9386,  0.9642],
         [ 0.3698,  1.3010, -0.1851,  1.3165, -1.0207, -0.0451, -1.1653,
          -0.7486, -0.9386,  0.9642],
         [-0.2199, -0.7231, -0.7433, -0.2609,  0.8209,  0.0136, -2.0771,
          -1.0934, -0.3795,  0.7131],
         [ 1.0978,  1.1203, -1.7744, -0.4933,  1.0554,  1.5636, -0.9913,
          -0.2035,  1.3337, -1.0802]]], grad_fn=<EmbeddingBackward0>)

Process finished with exit code 0
 

转载自 CSDN-专业IT技术社区

原文链接:https://blog.csdn.net/u010615629/article/details/170457419

文章来源转载

评论

评论列表

微信小程序
QQ小程序

关于作者

点赞数:0
关注数:0
粉丝:0
文章:0
关注标签:0
加入于:--