【问题标题】:regarding UnicodeDecodeError error while invoking model.fit in Keras关于在 Keras 中调用 model.fit 时出现 UnicodeDecodeError 错误
【发布时间】:2016-11-22 07:44:21
【问题描述】:

当使用 Theano 作为后端运行 Keras 实现时。我收到以下错误消息,我真的不知道可能的原因。如果您遇到此类问题,或者能够分享任何见解,我将不胜感激。

在下面,我用深黑色标记了两条线。第一行与导致问题的代码有关。底部的第二条深黑色线看起来像错误消息。

使用以下命令行编译时出现问题: /usr/bin/g++ -shared -g -O3 -fno-math-errno -Wno-unused-label -Wno-unused-variable -Wno-write-strings -march=core2 -mcx16 -msahf -maes -mpclmul -mpopcnt -mavx 跳过一些细节......

Traceback (most recent call last):                                                            
  File "train.py", line 144, in <module>                                                      
    train_and_predict()                                                                       
  File "train.py", line 129, in train_and_predict 

model.fit(imgs_train, imgs_mask_train, batch_size=32, nb_epoch=20, verbose=1, shuffle=True,callbacks=[model_checkpoint])                                                               
  File "/user/theano/lib/python3.4/site-packages/Keras-1.0.3-py3.4.egg/keras/engine/training.py", line 1037, in fit     
  self._make_train_function()                                                               
  File "/user/theano/lib/python3.4/site-packages/Keras-1.0.3-py3.4.egg/keras/engine/training.py", line 670, in _make_train_function                                         
    **self._function_kwargs)                                                                  
  File "/user/theano/lib/python3.4/site-packages/Keras-1.0.3-py3.4.egg/keras/backend/theano_backend.py", line 528, in function                                              
    return Function(inputs, outputs, updates=updates, **kwargs)                               
  File "/user/theano/lib/python3.4/site-packages/Keras-1.0.3-py3.4.egg/keras/backend/theano_backend.py", line 514, in __init__                                              
    **kwargs)                                                                                 

....跳过一些细节...

File "/user/theano/lib/python3.4/site-packages/Theano-0.8.2-py3.4.egg/theano/gof/cmodule.py", line 1142, in module_from_key
    module = lnk.compile_cmodule(location)
  File "/user/theano/lib/python3.4/site-packages/Theano-0.8.2-py3.4.egg/theano/gof/cc.py", line 1506, in compile_cmodule
    preargs=preargs)
  File "/user/theano/lib/python3.4/site-packages/Theano-0.8.2-py3.4.egg/theano/gof/cmodule.py", line 2183, in compile_str
    compile_stderr = decode(p_out[1])
  File "/user/theano/lib/python3.4/site-packages/Theano-0.8.2-py3.4.egg/theano/compat/__init__.py", line 42, in decode
    return x.decode()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 0: invalid start byte

【问题讨论】:

  • 不要给出整个错误日志,它会降低帖子的可读性。如果您可以根据您的要求突出显示导致错误的主要因素,那很好。
  • 感谢您的建议,我已经修改了原帖

标签: python theano keras


【解决方案1】:

我猜这个错误是因为您正在使用的数据集中有一些非 ascii 字符,并且无法对其进行编码/解码。避免此错误的一种简单方法是使用 encode() 函数将此类字符串编码为(如果 a 是具有非 ascii 字符的字符串):

a.encode('utf-8')

如果你是从文件中读取数据,你也可以设置encoding='utf8'作为参数来避免这样的问题。

【讨论】:

  • 嗨瓦西,感谢您的回复。更多细节,我用 Tensorflow 作为后端测试了相同的程序。看起来它正在运行。其次,输入数据 imags_train 和 image_mask_train 是 npy 文件。如何确保它们被编码为“utf-8”。
  • 如果你使用的是依赖于 tensorflow 和 theano 的东西,这完全有意义。你会从一个得到正常的行为,但从另一个得到不正常的行为。你是如何打开和阅读文件的?你可以使用 - open(file, mode='r', encoding='utf8') - 这样的东西吗?
  • 我明白了。让我试试。谢谢。
猜你喜欢
  • 2021-10-19
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
  • 2018-04-17
  • 2018-11-07
  • 2016-10-20
  • 2020-06-25
相关资源
最近更新 更多