【问题标题】:Decoding problem when using hyperas to find parameters of Keras model, maybe due to the `Trial` function in `hyperopt`使用 hyperas 查找 Keras 模型的参数时出现解码问题,可能是由于 `hyperopt` 中的 `Trial` 函数
【发布时间】:2019-03-19 16:04:37
【问题描述】:

我正在使用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())

我认为问题的根源是由于我加载的 numpy .npy 文件是 ascii 编码格式数据。 那么,如何将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)

我想我最好把所有的traceback放在这里,所有的代码如下: https://github.com/MinghaoDu1994/MyPythonFunctions/blob/master/1Dcnn

我认为问题出在hyperopt 中的功能Trials,但我没有找到像我这样的相关问题。

【问题讨论】:

  • 从错误及其回溯中你知道哪个数组有问题吗? labelsample 数组与模型调用之间有什么联系?我没有看到这些变量。这些npy 文件的来源是什么。请注意,encoding 参数的适用性非常有限(阅读文档)。
  • 不,回溯只是告诉我错误来自第一个引用的代码,labelsample 包含 data=data 参数。我搜索了这个错误的解决方案,并推断它是由于我输入的数据造成的。我的npy 文件是由 python3 从我的其他程序生成的,因此,我认为这里不应该出现此错误。我已阅读文档并在此处尝试参数 encoding 以防万一,但它没有。

标签: python-3.x numpy keras hyperopt hyperas


【解决方案1】:

问题已解决。 在调用optim.minimize函数的时候,我们首先要定义两个函数,分别命名为datamodel,而不是我命名的create_model或者别的什么。这是一个非常严格的限制。

【讨论】:

    【解决方案2】:

    我可以通过将 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)
    

    hyperas 读取get_hyperopt_model_string() 中的某种脚本文件。我不知道是什么变量控制了这个读取,也许是notebook。我认为您从 npy 文件加载的数组与此问题无关。它正在解码一个大字符串(位置 4785),而不是数组的某个元素。

    简而言之,这是hyperas 模型问题,而不是npy 文件问题。

    【讨论】:

    • 我找不到任何控制读取的相关参数,也许我最好更改问题的描述。非常感谢。
    猜你喜欢
    • 2019-07-22
    • 2011-10-16
    • 2019-07-24
    • 2011-04-24
    • 2021-05-29
    • 1970-01-01
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多