pytorch基于 Transformer 预训练模型的方法实现词嵌入(tiansz/bert-base-chinese)

以下是一个完整的词嵌入(Word Embedding)示例代码,使用 modelscope 下载 tiansz/bert-base-chinese 模型,并通过 transformers 加载模型,获取中文句子的词嵌入。

from modelscope.hub.snapshot_download import snapshot_download
from transformers import BertTokenizer, BertModel
import torch

# 下载模型到本地目录
model_dir = snapshot_download('tiansz/bert-base-chinese', cache_dir='./bert-base-chinese')
print(f"模型已下载到: {model_dir}")

# 本地模型路径
model_path = model_dir  # 使用下载的模型路径

# 从本地加载分词器和模型
tokenizer = BertTokenizer.from_pretrained(model_path)
model = BertModel.from_pretrained(model_path)

# 将模型设置为评估模式
model.eval()

# 输入句子
sentence = "你好,今天天气怎么样?"

# 分词并转换为模型输入格式
inputs = tokenizer(sentence, return_tensors='pt')

# 获取词嵌入
with torch.no_grad():
    outputs = model(**inputs)

# 输出的最后一层隐藏状态(即词嵌入)
last_hidden_states = outputs.last_hidden_state

# 打印词嵌入的形状
print("Embeddings shape:", last_hidden_states.shape)  # [batch_size, sequence_length, hidden_size]

# 获取所有 token 的文本表示
tokens = tokenizer.convert_ids_to_tokens(inputs['input_ids'][0])

# 打印每个 token 及其对应的嵌入
for i, (token, embedding) in enumerate(zip(tokens, last_hidden_states[0])):
    print(f"Token {i}: {token}")
    print(f"Embedding: {embedding[:10]}...")  # 只打印前 10 维
  1. 下载模型

    使用 modelscopesnapshot_download 方法下载 tiansz/bert-base-chinese 模型到本地目录 ./bert-base-chinese

  2. 加载模型

    使用 transformersBertTokenizerBertModel 从本地路径加载模型和分词器。

  3. 输入句子

    定义一个中文句子 "你好,今天天气怎么样?"

  4. 分词和编码

    使用分词器将句子转换为模型输入格式(包括 input_idsattention_mask)。

  5. 获取词嵌入

    将输入传递给模型,获取最后一层隐藏状态(即词嵌入)。

  6. 输出结果

    打印每个 token 及其对应的嵌入向量(只打印前 10 维)。

    Downloading Model to directory: ./bert-base-chinese/tiansz/bert-base-chinese
    模型已下载到: ./bert-base-chinese/tiansz/bert-base-chinese
    Embeddings shape: torch.Size([1, 13, 768])
    Token 0: [CLS]
    Embedding: tensor([ 1.0592, 0.1071, 0.4324, 0.0860, 0.9301, -0.6972, 0.7214, -0.0408,
    -0.1321, -0.1840])...
    Token 1: 你
    Embedding: tensor([ 0.2686, 0.1246, 0.4344, 0.5293, 0.7844, -0.7398, 0.4845, -0.3669,
    -0.6001, 0.8876])...
    Token 2: 好
    Embedding: tensor([ 0.9697, 0.3952, 0.6012, -0.0386, 0.6996, -0.4031, 1.0839, 0.0119,
    0.0551, 0.2817])...
    Token 3: ,
    Embedding: tensor([ 0.8255, 0.6987, 0.0310, 0.4167, -0.0159, -0.5835, 1.4922, 0.3883,
    0.9030, -0.1529])...
    Token 4: 今
    Embedding: tensor([ 0.1640, 0.2744, 0.6168, 0.0693, 1.0125, -0.4001, -0.2779, 0.6306,
    -0.1302, -0.0534])...
    Token 5: 天
    Embedding: tensor([ 0.5449, -0.1022, 0.0316, -0.4571, 0.6967, 0.0789, 0.6432, 0.0501,
    0.3832, -0.3269])...
    Token 6: 天
    Embedding: tensor([ 1.0107, -0.3673, -1.0272, -0.1893, 0.3766, 0.2341, 0.3552, 0.0228,
    -0.2411, -0.2227])...
    Token 7: 气
    Embedding: tensor([ 0.9320, -0.8562, -0.9696, 0.2202, 0.1046, 0.3335, -0.2725, -0.3014,
    -0.0057, -0.2503])...
    Token 8: 怎
    Embedding: tensor([ 0.7004, -0.3408, 0.1803, -0.0093, -0.0996, 0.9946, 0.0251, 0.0321,
    0.1867, -0.6998])...
    Token 9: 么
    Embedding: tensor([ 0.7296, 0.0704, 0.2153, -0.2680, -0.4890, 0.8920, 0.0324, -0.0820,
    0.5248, -0.6742])...
    Token 10: 样
    Embedding: tensor([ 0.2482, 0.0567, 0.2574, 0.1359, 0.4210, 0.9753, 0.2528, -0.2645,
    0.3426, -0.4405])...
    Token 11: ?
    Embedding: tensor([ 1.4162, 0.4149, 0.1098, -0.7175, 0.9875, -0.4366, 0.8482, 0.2046,
    0.2398, -0.1031])...
    Token 12: [SEP]
    Embedding: tensor([ 0.2140, 0.1362, 0.3720, 0.5722, 0.3005, -0.1858, 1.1392, 0.2413,
    -0.1240, 0.0177])...

相关推荐
纠结哥_Shrek14 小时前
pytorch基于GloVe实现的词嵌入
人工智能·pytorch·python
纠结哥_Shrek14 小时前
pytorch实现长短期记忆网络 (LSTM)
pytorch·机器学习·lstm
白白糖14 小时前
深度学习 Pytorch 神经网络的损失函数
人工智能·pytorch·深度学习·神经网络
纠结哥_Shrek1 天前
pytorch基于FastText实现词嵌入
人工智能·pytorch·python
纠结哥_Shrek1 天前
pytorch实现变分自编码器
人工智能·pytorch·python
纠结哥_Shrek1 天前
PyTorch 与 Python 版本对应关系
人工智能·pytorch·python
纠结哥_Shrek1 天前
pytorch实现门控循环单元 (GRU)
人工智能·pytorch·gru
纠结哥_Shrek1 天前
pytorch实现基于Word2Vec的词嵌入
人工智能·pytorch·word2vec