Keras 保存功能不适用于文件路径中的 ASCII 字符

问题描述 投票:0回答:1

我正在遵循Keras文本分类教程,整个教程运行得非常完美。接下来,我想尝试一下.save和.load功能,这就是出现问题的地方。

代码行:

export_model.save('text_classification_model.keras')

给我错误:

UnicodeEncodeError                        Traceback (most recent call last)
Cell In[89], line 5
      1 #The export_model.save('text_classification_saved_model') line will save your model in a format that can be later loaded using tf.keras.models.load_model('text_classification_saved_model').
      2 #It will create a directory named my_model containing the model architecture, weights, and any relevant configuration. You can specify a different directory or path as needed.
      3 #export_model.save('D:\Desktop\SavedModel\model2.keras')
----> 5 export_model.save('text_classification_model.keras')

File c:\Users\gui-r\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\utils\traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File c:\Users\gui-r\AppData\Local\Programs\Python\Python311\Lib\encodings\cp1252.py:19, in IncrementalEncoder.encode(self, input, final)
     18 def encode(self, input, final=False):
---> 19     return codecs.charmap_encode(input,self.errors,encoding_table)[0]

UnicodeEncodeError: 'charmap' codec can't encode character '\x96' in position 3325: character maps to 

我不明白为什么,我的工作文件夹路径如下:

D:\Mon_Drive\Ingenierie_Mecanique\Info\IA\formation_ML_DL\Keras_Deep_Learning\Introduction\

我尝试了其他替代路径:

# With r'':
export_model.save(r'D:\Mon_Drive\Ingenierie_Mecanique\Info\IA\formation_ML_DL\Keras_Deep_Learning\Introduction\text_classification_model.keras')

#Forward Slashes :
export_model.save('D:/Mon_Drive/Ingenierie_Mecanique/Info/IA/formation_ML_DL/Keras_Deep_Learning/Introduction/text_classification_model.keras')

#Double backslashes :
export_model.save(r'D:\Mon_Drive\Ingenierie_Mecanique\Info\IA\formation_ML_DL\Keras_Deep_Learning\Introduction\text_classification_model.keras')
tensorflow keras path ascii
1个回答
0
投票

我在 tf 中启用了更详细的回溯,这使我上游到它打开词汇文件的位置,然后该文件在写入方法上崩溃。

如果您编辑 tf 代码以使其显式显示 utf-8,则它可以工作

我在这里发布了更多详细信息:

tensorflow2 save_model 方法因编码错误而失败

© www.soinside.com 2019 - 2024. All rights reserved.