【权重小技巧(1)】.pt文件无法打开或乱码?如何查看.pt文件的具体内容?

这篇博客介绍了在VS Code环境下遇到的问题:如何加载和查看GTM-Transformer论文中的数据集。作者详细展示了如何使用torch库加载category_labels.pt文件,并提供了相关链接以供参考。

复现论文 GTM-Transformer 过程中的数据集
复现论文 GTM-Transformer 过程中的数据集
直接用vscode打开的效果没法看
直接用 vscode 打开的效果没法看

import torch

    # Load category and color encodings
cat_dict = torch.load('C:/自己替换/category_labels.pt')
for k, v in cat_dict.items():  # k 参数名 v 对应参数值
        print(k, v)

运行结果图:
在这里插入图片描述
参考博客:https://blog.csdn.net/weixin_45562000/article/details/114580412?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_utm_term~default-0.pc_relevant_default&spm=1001.2101.3001.4242.1&utm_relevant_index=3

import torch from transformers import AutoTokenizer, AutoModel, AutoModelForCausalLM, pipeline from langchain.vectorstores import FAISS from langchain.embeddings import HuggingFaceEmbeddings from langchain.document_loaders import TextLoader from langchain.text_splitter import CharacterTextSplitter 1. 加载本地嵌入模型(替换云端模型) -------------------------------------------------- 检查模型是否可用 try: # 加载本地嵌入模型 embedding_model_name = “/home/yechao.xie/bge-small-en-v1.5/” embeddings = HuggingFaceEmbeddings(model_name=embedding_model_name) model_name = "/data/yechao.xie/aiagent/Hunyuan-0.5B-Instruct" tokenizer = AutoTokenizer.from_pretrained( model_name, trust_remote_code=True ) model = AutoModelForCausalLM.from_pretrained( model_name, trust_remote_code=True, dtype=torch.float32, # FP16量化 device_map="auto" # 自动分配设备 ) except Exception as e: print(f"模型加载失败: {e}") print(“请确保模型已下载到指定路径”) exit() 2. 创建本地模型pipeline(完全离线) -------------------------------------------------- qa_pipeline = pipeline( “text-generation”, model=model, tokenizer=tokenizer, framework=“pt”, # 明确使用PyTorch ) 3. 构建本地知识库(避免云端依赖) -------------------------------------------------- def build_local_vectorstore(file_path: str): “”“构建本地向量数据库”“” try: loader = TextLoader(file_path, encoding=“utf-8”) documents = loader.load() text_splitter = CharacterTextSplitter( chunk_size=500, chunk_overlap=50, separator="\n" ) texts = text_splitter.split_documents(documents) vector_db = FAISS.from_documents(texts, embeddings) vector_db.save_local("local_vectorstore") return vector_db except Exception as e: print(f"知识库构建失败: {e}") return None 4. 离线RAG查询函数 -------------------------------------------------- def offline_rag_query(question: str, vector_db, top_k=3, max_length=300): “”“完全离线的RAG查询”“” try: # 向量检索相关文档 docs = vector_db.similarity_search(question, k=top_k) context = “\n\n”.join([doc.page_content for doc in docs]) # 构造本地化提示模板 prompt_template = """ 基于以下知识片段: {context} 问题:{question} 请给出详细回答: """ prompt = prompt_template.format(context=context, question=question) # 使用本地模型生成回答 response = qa_pipeline( prompt, max_new_tokens=max_length, temperature=0.7, do_sample=True, pad_token_id=tokenizer.eos_token_id, no_repeat_ngram_size=3 # 减少重复内容 ) # 提取纯回答内容 full_response = response[0]['generated_text'] answer = full_response.replace(prompt, "").strip() return full_response except Exception as e: return f"查询失败: {e}" 5. 本地模型使用示例 -------------------------------------------------- if name == “main”: # 构建本地知识库(首次运行需要构建) try: vector_db = FAISS.load_local(“local_vectorstore”, embeddings) print(“加载现有向量数据库”) except: print(“构建新向量数据库…”) vector_db = build_local_vectorstore(“/home/yechao.xie/knowledge.txt”) # 离线查询 question = "how to use USB camera" answer = offline_rag_query(question, vector_db) print("\n" + "="*50) print(f"问题: {question}") print("-"*50) print(f"回答: {answer}") print("="*50 + "\n") 请阅读以上代码确保输出不是乱码
最新发布
09-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值