我正在使用hyperas模块调优我的Keras模型并返回错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 4785: ordinal not in range(128)调用位置出现错误,trials的语法
if __name__ == '__main__':
best_run, best_model = optim.minimize(model=create_model,
data=data,
algo=tpe.suggest,
max_evals=20,
trials=Trials())我认为问题的根源是由于我加载了一个ascii编码格式的numpy .npy文件。那么,如何将ascii格式转换为utf-8格式呢?
通过添加encoding='latin1',我看到了一些类似这样的解决方案,但它不起作用。
label =np.load(os.getcwd()+'/Simu_Sample_label_1000.npy',encoding="latin1")
sample=np.load(os.getcwd()+'/Training_Sample_1000.npy',encoding="latin1")在这里添加我的整个回溯:
In [3]: %run 1dCNN.py
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
~/subg_ps/cnn_train/1dCNN.py in <module>()
127 algo=tpe.suggest,
128 max_evals=20,
--> 129 trials=Trials())
130 trX, trY, teX, teY = data()
131 print("Evalutation of best performing model:")
~/anaconda3/lib/python3.6/site-packages/hyperas/optim.py in minimize(model, data, algo, max_evals, trials, functions, rseed, notebook_name, verbose, eval_space, return_space, keep_temp)
67 notebook_name=notebook_name,
68 verbose=verbose,
---> 69 keep_temp=keep_temp)
70
71 best_model = None
~/anaconda3/lib/python3.6/site-packages/hyperas/optim.py in base_minimizer(model, data, functions, algo, max_evals, trials, rseed, full_model_string, notebook_name, verbose, stack, keep_temp)
96 model_str = full_model_string
97 else:
---> 98 model_str = get_hyperopt_model_string(model, data, functions, notebook_name, verbose, stack)
99 temp_file = './temp_model.py'
100 write_temp_files(model_str, temp_file)
~/anaconda3/lib/python3.6/site-packages/hyperas/optim.py in get_hyperopt_model_string(model, data, functions, notebook_name, verbose, stack)
184 calling_script_file = os.path.abspath(inspect.stack()[stack][1])
185 with open(calling_script_file, 'r') as f:
--> 186 source = f.read()
187
188 cleaned_source = remove_all_comments(source)
~/anaconda3/lib/python3.6/encodings/ascii.py in decode(self, input, final)
24 class IncrementalDecoder(codecs.IncrementalDecoder):
25 def decode(self, input, final=False):
---> 26 return codecs.ascii_decode(input, self.errors)[0]
27
28 class StreamWriter(Codec,codecs.StreamWriter):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 4785: ordinal not in range(128)我想我最好把所有的回溯和所有的代码放在这里:https://github.com/MinghaoDu1994/MyPythonFunctions/blob/master/1Dcnn
我认为这个问题是由于hyperopt中的Trials函数造成的,但我没有找到任何像我这样的相关问题。
发布于 2019-03-21 14:06:56
问题已经解决了。在调用optim.minimize函数时,我们必须首先定义两个名为data和model的函数,而不是我命名为create_model或其他任何名称的函数。这是一个非常严格的限制。
发布于 2019-03-20 10:22:53
我可以通过将unicode字符串(默认为PY3)转换为字节字符串,然后尝试对其执行decode操作来重新创建错误:
In [347]: astr = 'abc'+chr(0xe8)+'xyz'
In [348]: astr
Out[348]: 'abcèxyz'
In [349]: astr.encode('latin1')
Out[349]: b'abc\xe8xyz'
In [350]: astr.encode('latin1').decode('ascii')
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-350-1825a76f5d5b> in <module>
----> 1 astr.encode('latin1').decode('ascii')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 3: ordinal not in range(128)在get_hyperopt_model_string()中读取某种脚本文件的hyperas。我不知道控制读操作的是什么变量,可能是notebook。我不认为您从npy文件加载的数组与这个问题有任何关系。它正在解码一个大字符串(位置4785),而不是数组中的某个元素。
简而言之,这是一个hyperas模型问题,而不是一个npy文件问题。
https://stackoverflow.com/questions/55245327
复制相似问题