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

b4wnujal  于 2023-11-19  发布在  其他
关注(0)|答案(1)|浏览(140)

我跟着Keras text classification tutorial,整个教程顺利工作。接下来,我想尝试.保存和.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')

xu3bshqb

xu3bshqb1#

我也遇到了同样的问题。首先,我在tf中启用了更详细的traceback选项,这导致了我的错误-它在代码的上游部分,在那里它打开了词汇表文件,然后它试图写入时崩溃(你的错误)。
如果您编辑tf文件打开代码以使编码utf-8显式,
我在这里发布了更多细节:
tensorflow 2保存_model方法因编码错误而失败

相关问题