T5 model

1. download through script

from transformers import T5Tokenizer, T5Model

# Specify the model name
model_name = 't5-base'  # or 't5-small', 't5-large', 't5-3b', 't5-11b' depending on what you want

# Download the model and the tokenizer
model = T5Model.from_pretrained(model_name)
tokenizer = T5Tokenizer.from_pretrained(model_name)

# Save the model and the tokenizer to a directory
model.save_pretrained('T5-base')
tokenizer.save_pretrained('T5-base')

2. encode a sentence with T5

from transformers import T5Tokenizer, T5Model
import torch

# Specify the path to your locally saved model
model_dir = 'T5/t5-base'

# Load pre-trained model tokenizer (vocabulary)
tokenizer = T5Tokenizer.from_pretrained(model_dir)

# Sentence to encode
text = "Hello, my dog is cute"

# Tokenize our sentence with the T5 tokenizer.
tokenized_text = tokenizer.encode_plus(text, return_tensors="pt")

# Load pre-trained model (weights)
model = T5Model.from_pretrained(model_dir)

# Put the model in "evaluation" mode, meaning feed-forward operation.
model.eval()

# Predict hidden states features for each layer
with torch.no_grad():
    # Exclude token_type_ids for T5
    outputs = model(input_ids=tokenized_text['input_ids'], attention_mask=tokenized_text['attention_mask'])

# Get the hidden states from the model
hidden_states = outputs[0]

print(hidden_states)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值