简体   繁体   中英

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

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

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

This is the main code :

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])

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

Error :

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'

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM