通过Huggingface分享与上传自己的训练模型

本文介绍了如何使用Git Bash在本地创建仓库,并通过Huggingface平台上传和管理模型。详细步骤包括克隆公共仓库、提交更改以及处理大文件的上传策略。注意,对于大文件,建议先上传小文件,再单独处理大文件,以确保操作成功。

使用GIT BASH,CD到本地想要建立仓库的地方。

使用Git Bash,CD到本地想要建立Git仓库的目录。在https://huggingface.co/中用自己的账号上传一个模型。以下步骤是public资源的上传方式,private资源需要在git clone命令中加入[username]:[password]@,注意是@,我之前误看成/后白花了不少时间。

git clone https://huggingface.co/dragonStyle/bert-303-step35000

执行完后会新建一个仓库目录。

cd bert-303-step35000(CD到这个仓库目录里)

git add .

下面的报警暂时不知道是否会产生什么影响。

warning: LF will be replaced by CRLF in config.json.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in train_results.txt.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in trainer_state.json.
The file will ha
使用 HuggingFace 加载和微调预训练模型非常方便,HuggingFace 提供了 `transformers` 库,支持大量的预训练模型(如 BERT、RoBERTa、GPT、T5 等),并提供简单接口用于加载模型、分词器和进行训练。 --- ## ✅ 一、加载预训练模型分词器 HuggingFace 的 `AutoModel` 和 `AutoTokenizer` 类可以自动识别模型和对应的分词器。 ```python from transformers import AutoTokenizer, AutoModelForSequenceClassification # 指定模型名称(如 HuggingFace 上的模型 ID) model_name = "bert-base-uncased" # 加载分词器 tokenizer = AutoTokenizer.from_pretrained(model_name) # 加载预训练模型,并指定输出类别数量(例如二分类) model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2) ``` --- ## ✅ 二、准备数据集 使用 `datasets` 库加载数据,并进行预处理。 ```python from datasets import load_dataset # 加载一个公开数据集(如 IMDB 电影评论) dataset = load_dataset("imdb") # 对数据进行编码 def tokenize_function(examples): return tokenizer(examples["text"], padding="max_length", truncation=True) tokenized_datasets = dataset.map(tokenize_function, batched=True) # 选择训练集和测试集 train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(1000)) eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(100)) ``` --- ## ✅ 三、定义训练参数和训练器 使用 `TrainingArguments` 和 `Trainer` 来简化训练流程。 ```python from transformers import TrainingArguments, Trainer training_args = TrainingArguments( output_dir="./results", evaluation_strategy="epoch", learning_rate=2e-5, per_device_train_batch_size=8, num_train_epochs=3, weight_decay=0.01, logging_dir="./logs", logging_steps=10, save_strategy="epoch" ) trainer = Trainer( model=model, args=training_args, train_dataset=train_dataset, eval_dataset=eval_dataset, ) ``` --- ## ✅ 四、开始训练 ```python trainer.train() ``` --- ## ✅ 五、保存模型 训练完成后,可以将模型保存到本地或上传HuggingFace Hub。 ```python model.save_pretrained("./my_finetuned_model") tokenizer.save_pretrained("./my_finetuned_model") ``` --- ## ✅ 六、使用模型进行预测 ```python from transformers import pipeline # 使用 pipeline 快速推理 classifier = pipeline("text-classification", model="./my_finetuned_model", tokenizer=tokenizer) # 测试一个句子 result = classifier("I love using HuggingFace Transformers!") print(result) ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AlphaFinance

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值