当前位置: 首页> 科技> 互联网 > 重庆网站设计总部_河南建筑工程信息网官网_搜索引擎优化的技巧_小红书搜索指数

重庆网站设计总部_河南建筑工程信息网官网_搜索引擎优化的技巧_小红书搜索指数

时间:2025/7/12 7:43:26来源:https://blog.csdn.net/qq_36663518/article/details/144965110 浏览次数:0次
重庆网站设计总部_河南建筑工程信息网官网_搜索引擎优化的技巧_小红书搜索指数
import os
import torch
from torch.utils.data import DataLoader, Dataset
from transformers import BertTokenizer, BertForSequenceClassification, AdamW
from sklearn.metrics import accuracy_score, classification_report
from tqdm import tqdm# 自定义数据集
class CustomDataset(Dataset):def __init__(self, texts, labels, tokenizer, max_length=128):self.texts = textsself.labels = labelsself.tokenizer = tokenizerself.max_length = max_lengthdef __len__(self):return len(self.texts)def __getitem__(self, idx):text = self.texts[idx]label = self.labels[idx]encoding = self.tokenizer(text,max_length=self.max_length,padding="max_length",truncation=True,return_tensors="pt")return {"input_ids": encoding["input_ids"].squeeze(0),"attention_mask": encoding["attention_mask"].squeeze(0),"label": torch.tensor(label, dtype=torch.long)}# 训练函数
def train_model(model, train_loader, optimizer, device, num_epochs=3):model.train()for epoch in range(num_epochs):total_loss = 0for batch in tqdm(train_loader, desc=f"Training Epoch {epoch + 1}/{num_epochs}"):input_ids = batch["input_ids"].to(device)attention_mask = batch["attention_mask"].to(device)labels = batch["label"].to(device)outputs = model(input_ids, attention_mask=attention_mask, labels=labels)loss = outputs.losstotal_loss += loss.item()optimizer.zero_grad()loss.backward()optimizer.step()print(f"Epoch {epoch + 1} Loss: {total_loss / len(train_loader)}")# 评估函数
def evaluate_model(model, val_loader, device):model.eval()predictions, true_labels = [], []with torch.no_grad():for batch in val_loader:input_ids = batch["input_ids"].to(device)attention_mask = batch["attention_mask"].to(device)labels = batch["label"].to(device)outputs = model(input_ids, attention_mask=attention_mask)logits = outputs.logitspreds = torch.argmax(logits, dim=1).cpu().numpy()predictions.extend(preds)true_labels.extend(labels.cpu().numpy())accuracy = accuracy_score(true_labels, predictions)report = classification_report(true_labels, predictions)print(f"Validation Accuracy: {accuracy}")print("Classification Report:")print(report)# 模型保存函数
def save_model(model, tokenizer, output_dir):os.makedirs(output_dir, exist_ok=True)model.save_pretrained(output_dir)tokenizer.save_pretrained(output_dir)print(f"Model saved to {output_dir}")# 模型加载函数
def load_model(output_dir, device):tokenizer = BertTokenizer.from_pretrained(output_dir)model = BertForSequenceClassification.from_pretrained(output_dir)model.to(device)print(f"Model loaded from {output_dir}")return model, tokenizer# 推理预测函数
def predict(texts, model, tokenizer, device, max_length=128):model.eval()encodings = tokenizer(texts,max_length=max_length,padding="max_length",truncation=True,return_tensors="pt")input_ids = encodings["input_ids"].to(device)attention_mask = encodings["attention_mask"].to(device)with torch.no_grad():outputs = model(input_ids, attention_mask=attention_mask)logits = outputs.logitsprobabilities = torch.softmax(logits, dim=1).cpu().numpy()predictions = torch.argmax(logits, dim=1).cpu().numpy()return predictions, probabilities# 主函数
def main():# 配置参数config = {"train_batch_size": 16,"val_batch_size": 16,"learning_rate": 5e-5,"num_epochs": 5,"max_length": 128,"device_id": 7,  # 指定 GPU ID"model_dir": "model","local_model_path": "roberta_tiny_model",  # 指定本地模型路径,如果为 None 则使用预训练模型"pretrained_model_name": "uer/chinese_roberta_L-12_H-128",  # 预训练模型名称}# 设置设备device = torch.device(f"cuda:{config['device_id']}" if torch.cuda.is_available() else "cpu")print(f"Using device: {device}")# 加载分词器和模型tokenizer = BertTokenizer.from_pretrained(config["local_model_path"])model = BertForSequenceClassification.from_pretrained(config["local_model_path"], num_labels=2)model.to(device)# 示例数据train_texts = ["This is a great product!", "I hate this service."]train_labels = [1, 0]val_texts = ["Awesome experience.", "Terrible product."]val_labels = [1, 0]# 创建数据集和数据加载器train_dataset = CustomDataset(train_texts, train_labels, tokenizer, config["max_length"])val_dataset = CustomDataset(val_texts, val_labels, tokenizer, config["max_length"])train_loader = DataLoader(train_dataset, batch_size=config["train_batch_size"], shuffle=True)val_loader = DataLoader(val_dataset, batch_size=config["val_batch_size"])# 定义优化器optimizer = AdamW(model.parameters(), lr=config["learning_rate"])# 训练模型train_model(model, train_loader, optimizer, device, num_epochs=config["num_epochs"])# 评估模型evaluate_model(model, val_loader, device)# 保存模型save_model(model, tokenizer, config["model_dir"])# 加载模型loaded_model, loaded_tokenizer = load_model(config["model_dir"], "cpu")# 推理预测new_texts = ["I love this!", "It's the worst."]predictions, probabilities = predict(new_texts, loaded_model, loaded_tokenizer,  "cpu")for text, pred, prob in zip(new_texts, predictions, probabilities):print(f"Text: {text}")print(f"Predicted Label: {pred} (Probability: {prob})")if __name__ == "__main__":main()
关键字:重庆网站设计总部_河南建筑工程信息网官网_搜索引擎优化的技巧_小红书搜索指数

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: