python: AttributeError: Đối tượng 'str' không có thuộc tính 'khóa'

Sep 29 2020

Tôi đang cố gắng giải quyết vấn đề phân loại. Tôi không biết tại sao tôi lại gặp lỗi này:

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

Đây là mã chính :

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

nơi generate_arrays_for_traininghàm trả về xvà y. xlà một mảng 2D các số thực và ylà [0,1].

Lỗi :

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'

Trả lời

2 PoeDator Sep 29 2020 at 19:32

vấn đề của bạn là do class_weight="balanced"tham số mà bạn chuyển đếnmodel.fit()

theo tham chiếu model.fit () , tham số này phải là một dict: Các chỉ số lớp ánh xạ từ điển tùy chọn (số nguyên) thành một giá trị trọng số (float), được sử dụng để tính trọng số cho hàm mất (chỉ trong quá trình huấn luyện). Điều này có thể hữu ích để yêu cầu người mẫu "chú ý nhiều hơn" đến các mẫu từ một lớp ít được đại diện.

thử class_weight=Noneđể kiểm tra, nó sẽ thoát khỏi lỗi ban đầu. Sau đó, cung cấp mệnh lệnh thích hợp class_weightđể giải quyết vấn đề tập dữ liệu không cân bằng.