python:AttributeError:'str'对象没有属性'keys' [英] python : AttributeError: 'str' object has no attribute 'keys'

查看:1732
本文介绍了python:AttributeError:'str'对象没有属性'keys'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决分类问题。我不知道为什么会收到此错误:

I'm trying to solve classification problem. I don't know why I'm getting this error:

AttributeError: str对象没有属性 keys

这是主要代码

def generate_arrays_for_training(indexPat, paths, start=0, end=100):       
    while True:
        from_=int(len(paths)/100*start)
        to_=int(len(paths)/100*end)
        for i in range(from_, int(to_)):
            f=paths[i]
            x = np.load(PathSpectogramFolder+f)
     if('P' in f):
                y = np.repeat([[0,1]],x.shape[0], axis=0)
            else:
                y =np.repeat([[1,0]],x.shape[0], axis=0) 
            yield(x,y)
history=model.fit_generator(generate_arrays_for_training(indexPat, filesPath, end=75) ## problem here
                   steps_per_epoch=int((len(filesPath)-int(len(filesPath)/100*25))), 
                   validation_steps=int((len(filesPath)-int(len(filesPath)/100*75))),
                   verbose=2,class_weight="balanced",
                  epochs=15, max_queue_size=2, shuffle=True, callbacks=[callback])

其中 generate_arrays_for_training 函数返回 x y x 是二维浮点数数组,而 y 是[0,1]。

where generate_arrays_for_training function return x and y . x is a 2D array of float numbers and y is [0,1].

错误

Traceback (most recent call last):
  File "/home/user1/thesis2/CNN_dwt2.py", line 437, in <module>
    main()
  File "/home/user1/thesis2/CNN_dwt2.py", line 316, in main
    history=model.fit_generator(generate_arrays_for_training(indexPat, filesPath, end=75), 
  File "/home/user1/.local/lib/python3.8/site-packages/tensorflow/python/util/deprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "/home/user1/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1815, in fit_generator
    return self.fit(
  File "/home/user1/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 108, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/home/user1/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1049, in fit
    data_handler = data_adapter.DataHandler(
  File "/home/user1/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1122, in __init__
    dataset = dataset.map(_make_class_weight_map_fn(class_weight))
  File "/home/user1/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1295, in _make_class_weight_map_fn
    class_ids = list(sorted(class_weight.keys()))

AttributeError: 'str' object has no attribute 'keys'


推荐答案

您的问题是由您传递给 model.fit()

your issue is caused by class_weight="balanced" parameter that you pass to model.fit()

根据 model.fit()参考,此参数应为dict:
可选的字典映射类索引(整数)到权重(浮点)值,用于对损失函数加权(仅在训练过程中)。这对于告诉模型多加注意可能很有用。

according to the model.fit() reference, this parameter should be a dict: Optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class.

尝试 class_weight = None 进行测试,它应该摆脱原始错误。稍后提供适当的字典作为 class_weight 来解决数据集不平衡的问题。

try class_weight=None for testing, it should get rid of the original error. Later provide proper dict as class_weight to address imbalanced dataset issue.

这篇关于python:AttributeError:'str'对象没有属性'keys'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆