python: AttributeError: l'oggetto 'str' non ha attributi 'chiavi'

Sep 29 2020

Sto cercando di risolvere il problema di classificazione. Non so perché ricevo questo errore:

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

Questo è il codice principale :

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

dove la generate_arrays_for_trainingfunzione ritorna xe y. xè una matrice 2D di numeri float ed yè [0,1].

Errore :

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'

Risposte

2 PoeDator Sep 29 2020 at 19:32

il tuo problema è causato dal class_weight="balanced"parametro a cui passimodel.fit()

secondo il riferimento model.fit () , questo parametro dovrebbe essere un dict: dizionario opzionale che mappa gli indici di classe (interi) su un valore di peso (float), usato per pesare la funzione di perdita (solo durante l'allenamento). Questo può essere utile per dire al modello di "prestare maggiore attenzione" ai campioni di una classe sottorappresentata.

prova class_weight=Noneper il test, dovrebbe eliminare l'errore originale. Successivamente fornire indicazioni adeguate class_weightper risolvere il problema del set di dati non bilanciato.