鱼C论坛

 找回密码
 立即注册
查看: 393|回复: 10

tensorflow代码解读

[复制链接]
发表于 2024-11-2 12:08:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. max_features = 75000
  2. embedding_dim = 64
  3. sequence_length = 512*2
  4. vectorize_layer = tf.keras.layers.TextVectorization(
  5.     standardize=Clean ,
  6.     max_tokens=max_features,
  7.     ngrams = (3,5),
  8.     output_mode="int",
  9.     output_sequence_length=sequence_length,
  10.     pad_to_max_tokens=True
  11. )
  12. vectorize_layer.adapt(train_data['text'])
  13. Text = vectorize_layer(train_data['text']).numpy()
  14. Text
复制代码
  1. sm = SMOTE(random_state=44)
  2. Text,labels= sm.fit_resample(Text,train_data['label'])
复制代码

生成训练集和测试集前上述文本处理代码如何解读
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-11-2 12:08:46 | 显示全部楼层
这段代码主要是用TensorFlow和SMOTE进行文本数据的预处理和数据增强。以下是对代码的逐步解读:

1. **参数设置**:
   - `max_features = 75000`: 设置文本向量化时的最大特征数,即词汇表的大小。
   - `embedding_dim = 64`: 嵌入维度,虽然在这段代码中没有直接使用。
   - `sequence_length = 512*2`: 输出序列的最大长度。

2. **文本向量化层**:
   - `vectorize_layer = tf.keras.layers.TextVectorization(...)`: 创建一个文本向量化层。
     - `standardize=Clean`: 使用自定义的标准化函数`Clean`。
     - `max_tokens=max_features`: 限制词汇表的大小。
     - `ngrams=(3,5)`: 生成3到5个词的n-gram。
     - `output_mode="int"`: 输出整数编码的序列。
     - `output_sequence_length=sequence_length`: 输出序列的长度。
     - `pad_to_max_tokens=True`: 填充到最大特征数。

3. **适配训练数据**:
   - `vectorize_layer.adapt(train_data)`: 让向量化层适应训练数据,构建词汇表。

4. **文本向量化**:
   - `Text = vectorize_layer(train_data).numpy()`: 将训练数据转换为整数序列。

5. **SMOTE数据增强**:
   - `sm = SMOTE(random_state=44)`: 创建一个SMOTE对象,用于处理不平衡数据。
   - `Text, labels = sm.fit_resample(Text, train_data)`: 使用SMOTE对文本数据进行过采样,生成平衡的数据集。

这段代码的主要目的是将文本数据转换为适合机器学习模型输入的格式,并通过SMOTE进行数据增强,以处理类别不平衡问题。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-11-2 12:56:48 | 显示全部楼层
FishC 发表于 2024-11-2 12:08
这段代码主要是用TensorFlow和SMOTE进行文本数据的预处理和数据增强。以下是对代码的逐步解读:

1. **参 ...
  1. #模型参数设置
  2. from transformers import AutoTokenizer, AutoConfig
  3. class Config:
  4.     debug = False
  5.     num_workers = 4
  6.     llm_backbone =  "/kaggle/input/llm-detect-deberta-xlarge/pytorch/fold4/1"
  7.     tokenizer_path = "/kaggle/input/llm-detect-deberta-xlarge/pytorch/fold4/1"
  8.    
  9.     tokenizer = AutoTokenizer.from_pretrained(
  10.         tokenizer_path, use_fast=True, trust_remote_code=True,
  11.     )
  12.     batch_size = 8
  13.     max_len = 512
  14.     seed = 42
  15.     num_labels = 1
  16.     gradient_checkpointing = False
  17. CFG = Config()
复制代码


上述代码报错
  1. ---------------------------------------------------------------------------
  2. OSError                                   Traceback (most recent call last)
  3. Cell In[36], line 3
  4.       1 #模型参数设置
  5.       2 from transformers import AutoTokenizer, AutoConfig
  6. ----> 3 class Config:
  7.       4     debug = False
  8.       5     num_workers = 4

  9. Cell In[36], line 9, in Config()
  10.       6 llm_backbone =  "/kaggle/input/llm-detect-deberta-xlarge/pytorch/fold4/1"
  11.       7 tokenizer_path = "/kaggle/input/llm-detect-deberta-xlarge/pytorch/fold4/1"
  12. ----> 9 tokenizer = AutoTokenizer.from_pretrained(
  13.      10     tokenizer_path, use_fast=True, trust_remote_code=True,
  14.      11 )
  15.      12 batch_size = 8
  16.      13 max_len = 512

  17. File /opt/conda/lib/python3.10/site-packages/transformers/models/auto/tokenization_auto.py:926, in AutoTokenizer.from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs)
  18.     923 tokenizer_class_py, tokenizer_class_fast = TOKENIZER_MAPPING[type(config)]
  19.     925 if tokenizer_class_fast and (use_fast or tokenizer_class_py is None):
  20. --> 926     return tokenizer_class_fast.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
  21.     927 else:
  22.     928     if tokenizer_class_py is not None:

  23. File /opt/conda/lib/python3.10/site-packages/transformers/tokenization_utils_base.py:2200, in PreTrainedTokenizerBase.from_pretrained(cls, pretrained_model_name_or_path, cache_dir, force_download, local_files_only, token, revision, trust_remote_code, *init_inputs, **kwargs)
  24.    2197 # If one passes a GGUF file path to `gguf_file` there is no need for this check as the tokenizer will be
  25.    2198 # loaded directly from the GGUF file.
  26.    2199 if all(full_file_name is None for full_file_name in resolved_vocab_files.values()) and not gguf_file:
  27. -> 2200     raise EnvironmentError(
  28.    2201         f"Can't load tokenizer for '{pretrained_model_name_or_path}'. If you were trying to load it from "
  29.    2202         "'https://huggingface.co/models style="color:rgb(175,0,0)">', make sure you don't have a local directory with the same name. "
  30.    2203         f"Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory "
  31.    2204         f"containing all relevant files for a {cls.__name__} tokenizer."
  32.    2205     )
  33.    2207 for file_id, file_path in vocab_files.items():
  34.    2208     if file_id not in resolved_vocab_files:

  35. OSError: Can't load tokenizer for '/kaggle/input/llm-detect-deberta-xlarge/pytorch/fold4/1'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure '/kaggle/input/llm-detect-deberta-xlarge/pytorch/fold4/1' is the correct path to a directory containing all relevant files for a DebertaTokenizerFast tokenizer.
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-11-2 12:56:58 | 显示全部楼层
Terence888 发表于 2024-11-02 12:56
#模型参数设置
from transformers import AutoTokeni ...


这个错误信息表明,程序在尝试加载指定路径下的tokenizer时失败了。以下是一些可能的解决方案:

1. **检查路径**: 确保路径`/kaggle/input/llm-detect-deberta-xlarge/pytorch/fold4/1`是正确的,并且该目录下包含了所有必要的tokenizer文件(如`vocab.json`、`merges.txt`等)。

2. **文件完整性**: 确保这些文件没有损坏或丢失。你可以尝试在本地或其他环境中验证这些文件是否可以正常加载。

3. **Hugging Face模型**: 如果你是从Hugging Face下载的模型,确保下载过程没有中断,并且所有文件都已正确下载。

4. **本地目录冲突**: 确保在你的工作环境中没有与Hugging Face模型同名的本地目录,这可能会导致路径解析问题。

5. **使用其他路径**: 如果可能,尝试使用其他路径或重新下载模型到一个新的目录,然后更新`tokenizer_path`。

如果以上方法都无法解决问题,建议检查Hugging Face的文档或社区论坛,看看是否有其他人遇到过类似的问题。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-11-2 13:06:29 | 显示全部楼层
FishC 发表于 2024-11-2 12:56
这个错误信息表明,程序在尝试加载指定路径下的tokenizer时失败了。以下是一些可能的解决方案:

1.  ...
  1. llm_backbone
复制代码

这个是表示什么意思
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-11-2 13:12:53 | 显示全部楼层
Terence888 发表于 2024-11-02 13:06
llm_backbone
这个是表示什么意思


`llm_backbone`通常用于表示一个大型语言模型(Large Language Model)的基础架构或主干网络。在机器学习和深度学习中,"backbone"指的是模型的核心部分,负责提取输入数据的特征。在这个上下文中,`llm_backbone`可能指的是一个预训练的语言模型(例如BERT、GPT、DeBERTa等),用于进一步的微调或特定任务的训练。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-11-2 13:15:01 | 显示全部楼层
FishC 发表于 2024-11-2 12:56
这个错误信息表明,程序在尝试加载指定路径下的tokenizer时失败了。以下是一些可能的解决方案:

1.  ...

我试图使用kaggle提供的baseline,
  1. #模型参数设置
  2. from transformers import AutoTokenizer, AutoConfig
  3. class Config:
  4.     debug = False
  5.     num_workers = 4
  6.     llm_backbone =  "/kaggle/input/llm-detect-deberta-xlarge/pytorch/fold4/1/microsoft-deberta-xlarge_fold4_best.pth"
  7.     tokenizer_path = '/kaggle/input/llm-detect-deberta-xlarge/pytorch/fold0/2/microsoft-deberta-xlarge_fold0_best.pth'
  8.     tokenizer = AutoTokenizer.from_pretrained(
  9.         tokenizer_path, use_fast=True, trust_remote_code=True,
  10.     )
  11.     batch_size = 8
  12.     max_len = 512
  13.     seed = 42
  14.     num_labels = 1
  15.     gradient_checkpointing = False
  16. CFG = Config()
复制代码



报错提示如下
  1. ---------------------------------------------------------------------------
  2. HFValidationError                         Traceback (most recent call last)
  3. File /opt/conda/lib/python3.10/site-packages/transformers/utils/hub.py:403, in cached_file(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_gated_repo, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs)
  4.     401 try:
  5.     402     # Load from URL or cache if already cached
  6. --> 403     resolved_file = hf_hub_download(
  7.     404         path_or_repo_id,
  8.     405         filename,
  9.     406         subfolder=None if len(subfolder) == 0 else subfolder,
  10.     407         repo_type=repo_type,
  11.     408         revision=revision,
  12.     409         cache_dir=cache_dir,
  13.     410         user_agent=user_agent,
  14.     411         force_download=force_download,
  15.     412         proxies=proxies,
  16.     413         resume_download=resume_download,
  17.     414         token=token,
  18.     415         local_files_only=local_files_only,
  19.     416     )
  20.     417 except GatedRepoError as e:

  21. File /opt/conda/lib/python3.10/site-packages/huggingface_hub/utils/_deprecation.py:101, in _deprecate_arguments.<locals>._inner_deprecate_positional_args.<locals>.inner_f(*args, **kwargs)
  22.     100     warnings.warn(message, FutureWarning)
  23. --> 101 return f(*args, **kwargs)

  24. File /opt/conda/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py:106, in validate_hf_hub_args.<locals>._inner_fn(*args, **kwargs)
  25.     105 if arg_name in ["repo_id", "from_id", "to_id"]:
  26. --> 106     validate_repo_id(arg_value)
  27.     108 elif arg_name == "token" and arg_value is not None:

  28. File /opt/conda/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py:154, in validate_repo_id(repo_id)
  29.     153 if repo_id.count("/") > 1:
  30. --> 154     raise HFValidationError(
  31.     155         "Repo id must be in the form 'repo_name' or 'namespace/repo_name':"
  32.     156         f" '{repo_id}'. Use `repo_type` argument if needed."
  33.     157     )
  34.     159 if not REPO_ID_REGEX.match(repo_id):

  35. HFValidationError: Repo id must be in the form 'repo_name' or 'namespace/repo_name': '/kaggle/input/llm-detect-deberta-xlarge/pytorch/fold0/2/microsoft-deberta-xlarge_fold0_best.pth'. Use `repo_type` argument if needed.

  36. The above exception was the direct cause of the following exception:

  37. OSError                                   Traceback (most recent call last)
  38. Cell In[39], line 3
  39.       1 #模型参数设置
  40.       2 from transformers import AutoTokenizer, AutoConfig
  41. ----> 3 class Config:
  42.       4     debug = False
  43.       5     num_workers = 4

  44. Cell In[39], line 8, in Config()
  45.       6 llm_backbone =  "/kaggle/input/llm-detect-deberta-xlarge/pytorch/fold4/1/microsoft-deberta-xlarge_fold4_best.pth"
  46.       7 tokenizer_path = '/kaggle/input/llm-detect-deberta-xlarge/pytorch/fold0/2/microsoft-deberta-xlarge_fold0_best.pth'
  47. ----> 8 tokenizer = AutoTokenizer.from_pretrained(
  48.       9     tokenizer_path, use_fast=True, trust_remote_code=True,
  49.      10 )
  50.      11 batch_size = 8
  51.      12 max_len = 512

  52. File /opt/conda/lib/python3.10/site-packages/transformers/models/auto/tokenization_auto.py:844, in AutoTokenizer.from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs)
  53.     841     return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
  54.     843 # Next, let's try to use the tokenizer_config file to get the tokenizer class.
  55. --> 844 tokenizer_config = get_tokenizer_config(pretrained_model_name_or_path, **kwargs)
  56.     845 if "_commit_hash" in tokenizer_config:
  57.     846     kwargs["_commit_hash"] = tokenizer_config["_commit_hash"]

  58. File /opt/conda/lib/python3.10/site-packages/transformers/models/auto/tokenization_auto.py:676, in get_tokenizer_config(pretrained_model_name_or_path, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, **kwargs)
  59.     673     token = use_auth_token
  60.     675 commit_hash = kwargs.get("_commit_hash", None)
  61. --> 676 resolved_config_file = cached_file(
  62.     677     pretrained_model_name_or_path,
  63.     678     TOKENIZER_CONFIG_FILE,
  64.     679     cache_dir=cache_dir,
  65.     680     force_download=force_download,
  66.     681     resume_download=resume_download,
  67.     682     proxies=proxies,
  68.     683     token=token,
  69.     684     revision=revision,
  70.     685     local_files_only=local_files_only,
  71.     686     subfolder=subfolder,
  72.     687     _raise_exceptions_for_gated_repo=False,
  73.     688     _raise_exceptions_for_missing_entries=False,
  74.     689     _raise_exceptions_for_connection_errors=False,
  75.     690     _commit_hash=commit_hash,
  76.     691 )
  77.     692 if resolved_config_file is None:
  78.     693     logger.info("Could not locate the tokenizer configuration file, will try to use the model config instead.")

  79. File /opt/conda/lib/python3.10/site-packages/transformers/utils/hub.py:469, in cached_file(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_gated_repo, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs)
  80.     467     raise EnvironmentError(f"There was a specific connection error when trying to load {path_or_repo_id}:\n{err}")
  81.     468 except HFValidationError as e:
  82. --> 469     raise EnvironmentError(
  83.     470         f"Incorrect path_or_model_id: '{path_or_repo_id}'. Please provide either the path to a local folder or the repo_id of a model on the Hub."
  84.     471     ) from e
  85.     472 return resolved_file

  86. OSError: Incorrect path_or_model_id: '/kaggle/input/llm-detect-deberta-xlarge/pytorch/fold0/2/microsoft-deberta-xlarge_fold0_best.pth'. Please provide either the path to a local folder or the repo_id of a model on the Hub.
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-11-2 13:15:08 | 显示全部楼层
Terence888 发表于 2024-11-02 13:15
我试图使用kaggle提供的baseline,#模型参数设置
fro ...


这个错误信息表明,`AutoTokenizer.from_pretrained`方法期望的是一个模型目录路径或Hugging Face Hub上的模型ID,而不是一个具体的文件路径(如`.pth`文件)。以下是一些可能的解决方案:

1. **检查路径**: 确保`tokenizer_path`指向的是一个包含tokenizer配置文件的目录,而不是一个具体的模型权重文件(`.pth`)。

2. **正确的目录结构**: 确保目录中包含必要的tokenizer文件,如`tokenizer_config.json`、`vocab.json`、`merges.txt`等。

3. **使用Hugging Face Hub模型**: 如果你想使用Hugging Face Hub上的模型,确保提供的是模型的repo ID,而不是本地路径。

4. **分开加载模型和tokenizer**: 如果你有一个特定的模型权重文件(`.pth`),你可能需要分别加载模型和tokenizer。通常,tokenizer是从一个预训练模型的目录中加载,而模型权重可以单独加载。

如果你有一个完整的模型目录,可以尝试将`tokenizer_path`指向该目录,并确保目录中有必要的tokenizer文件。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-11-2 14:26:04 | 显示全部楼层
FishC 发表于 2024-11-2 13:15
这个错误信息表明,`AutoTokenizer.from_pretrained`方法期望的是一个模型目录路径或Hugging Face Hub ...
  1. #Tokenizer通常用于将文本转换为序列,即文本向量化
  2. from transformers import AutoTokenizer, AutoConfig
  3. tokenizer_path = '/kaggle/input/debertaxlarge/deberta-xlarge'
  4. tokenizer = AutoTokenizer.from_pretrained(
  5.         tokenizer_path, use_fast=True, trust_remote_code=True,
  6.     )
复制代码


跳出如下警告,新版本的transformer怎么调用自动向量化函数
  1. /opt/conda/lib/python3.10/site-packages/transformers/tokenization_utils_base.py:1617: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be deprecated in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884
  2.   warnings.warn(
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-11-2 23:12:30 | 显示全部楼层
FishC 发表于 2024-11-2 13:15
这个错误信息表明,`AutoTokenizer.from_pretrained`方法期望的是一个模型目录路径或Hugging Face Hub ...
  1. /opt/conda/lib/python3.10/site-packages/transformers/tokenization_utils_base.py:1617: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be deprecated in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884
  2.   warnings.warn(
复制代码


如何修改排除如下警告
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-11-2 23:12:37 | 显示全部楼层
Terence888 发表于 2024-11-02 23:12
/opt/conda/lib/python3.10/site-packages/transforme ...


要消除这个警告,你需要在使用`transformers`库的代码中显式地设置`clean_up_tokenization_spaces`参数。这个参数通常在调用`tokenizer.decode()`或`tokenizer.batch_decode()`方法时使用。

假设你在某个地方使用了`tokenizer.decode()`,你可以这样修改代码:

  1. decoded_text = tokenizer.decode(encoded_text, clean_up_tokenization_spaces=False)
复制代码


如果你使用的是`tokenizer.batch_decode()`,同样地:

  1. decoded_texts = tokenizer.batch_decode(encoded_texts, clean_up_tokenization_spaces=False)
复制代码


通过显式设置`clean_up_tokenization_spaces`参数为`False`,可以避免这个警告。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 16:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表