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

时间:2016-11-22 07:44:21

标签: python theano 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:&#39; utf-8&#39;编解码器不能解码位置0中的字节0xc0:无效的起始字节

1 个答案:

答案 0 :(得分:0)

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

a.encode('utf-8')

如果您正在从文件中读取数据,您还可以将encoding='utf8'设置为参数以避免此类问题。