关于在 Keras 中调用 model.fit 时出现 UnicodeDecodeError 错误

regarding UnicodeDecodeError error while invoking model.fit in Keras

当 运行 以 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'编解码器无法解码位置 0 中的字节 0xc0:起始字节无效

我猜这个错误是因为你正在使用的数据集中有一些非ascii字符,它不可能是encoded/decoded。避免此错误的一种简单方法是使用 encode() 函数对此类字符串进行编码(如果 a 是具有非 ascii 字符的字符串):

a.encode('utf-8')

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