InvalidArgumentError: логиты и метки должны быть широковещательными: logits_size=[10,10] labels_size=[100,10]

#python #tensorflow #machine-learning #keras #deep-learning

#python #тензорный поток #машинное обучение #keras #глубокое обучение

Вопрос:

Я следил за онлайн-уроком, в котором они взяли набор данных для собак и кошек и использовали эти данные для создания модели CNN. Я работаю с Animals-10 из kaggle, следуя руководству. Когда я подгоняю модель, я получаю InvalidArgumentError: logits and labels must be broadcastable: logits_size=[10,10] labels_size=[100,10] [[{{node loss_17/dense_38_loss/softmax_cross_entropy_with_logits}}]] ошибку. Я немного погуглил, но ничего не могу понять, и все существующие решения кажутся специфичными для конкретной проблемы. Я застрял на этом некоторое время и не могу понять. Спасибо за вашу помощь.

Код

 # Preprocessing for training data
butterfly_path = './animals/butterfly/'
cat_path = './animals/cat/'
chicken_path = './animals/chicken/'
cow_path = './animals/cow/'
dog_path = './animals/dog/'
elephant_path = './animals/elephant/'
horse_path = './animals/horse/'
sheep_path = './animals/sheep/'
spider_path = './animals/spider/'
squirrel_path = './animals/squirrel/'
butterfly_files = [f for f in listdir(butterfly_path) if isfile(join(butterfly_path, f))]
cat_files = [f for f in listdir(cat_path) if isfile(join(cat_path, f))]
chicken_files = [f for f in listdir(chicken_path) if isfile(join(chicken_path, f))]
cow_files = [f for f in listdir(cow_path) if isfile(join(cow_path, f))]
dog_files = [f for f in listdir(dog_path) if isfile(join(dog_path, f))]
elephant_files = [f for f in listdir(elephant_path) if isfile(join(elephant_path, f))]
horse_files = [f for f in listdir(horse_path) if isfile(join(horse_path, f))]
sheep_files = [f for f in listdir(sheep_path) if isfile(join(sheep_path, f))]
spider_files = [f for f in listdir(spider_path) if isfile(join(spider_path, f))]
squirrel_files = [f for f in listdir(squirrel_path) if isfile(join(squirrel_path, f))]
X_train = []
y_train = []

# Convert images to arrays and append labels to list
for file in butterfly_files:
    img_path = butterfly_path   file
    y_train.append(0) # butterfly
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in cat_files:
    img_path = cat_path   file
    y_train.append(1) # cat
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in chicken_files:
    img_path = chicken_path   file
    y_train.append(2) # chicken
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in cow_files:
    img_path = cow_path   file
    y_train.append(3) # cow
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in dog_files:
    img_path = dog_path   file
    y_train.append(4) # dog
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in elephant_files:
    img_path = elephant_path   file
    y_train.append(5) # elephant
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in horse_files:
    img_path = horse_path   file
    y_train.append(6) # horse
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in sheep_files:
    img_path = sheep_path   file
    y_train.append(7) # sheep
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in spider_files:
    img_path = spider_path   file
    y_train.append(8)  # spider
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)
for file in squirrel_files:
    img_path = squirrel_path   file
    y_train.append(9) # squirrel
    img = plt.imread(img_path)
    if (img.shape[-1] == 3):
        grey_img = color.rgb2gray(img)
    elif (img.shape[-1] == 4):
        grey_img = color.rgb2gray(color.rgba2rgb(img))
    resized_img = resize(grey_img, (100, 100))
    X_train.append(resized_img)

X_train = np.array(X_train)
y_train = np.array(y_train)

new_X_train, new_y_train = sklearn.utils.shuffle(X_train, y_train, random_state = 0)
# Observe data shapes
print("Train Images: ", X_train.shape, "nTrain Labels: ", y_train.shape)
 

Вывод

 Train Images:  (26179, 100, 100) 
Train Labels:  (26179,)
 

Больше кода

 # Flatten data
new_X_train = new_X_train.reshape(-1, 100, 100, 1)

new_X_train.shape

new_X_train[0]
 

Вывод

 (26179, 100, 100, 1)

array([[[0.11311903],
        [0.11212739],
        [0.10983502],
        ...,
        [0.05591958],
        [0.05303901],
        [0.05191926]],

       [[0.11488738],
        [0.11407327],
        [0.11111339],
        ...,
        [0.05361823],
        [0.05217732],
        [0.05204283]],

       [[0.11666758],
        [0.11481607],
        [0.11202889],
        ...,
        [0.05181058],
        [0.05311151],
        [0.05275289]],

       ...,

       [[0.26910149],
        [0.26756103],
        [0.26437813],
        ...,
        [0.54128431],
        [0.52813318],
        [0.52014463]],

       [[0.26764131],
        [0.2660793 ],
        [0.26377079],
        ...,
        [0.53660792],
        [0.52183369],
        [0.51606187]],

       [[0.26523861],
        [0.26538986],
        [0.26358519],
        ...,
        [0.53761132],
        [0.51546228],
        [0.50781162]]])
 

Код, в котором я начинаю создавать модель

 # Convert labels to categorical
new_y_train = keras.utils.to_categorical(new_y_train, 10)

# Create the model
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(64, (3, 3), input_shape = new_X_train.shape[1:], activation = 'relu'))
model.add(keras.layers.MaxPooling2D(pool_size = (2, 2)))

model.add(keras.layers.Conv2D(64, (3, 3), activation = 'relu'))
model.add(keras.layers.MaxPooling2D(pool_size = (2, 2)))

model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(units = 64, activation = 'relu'))

model.add(keras.layers.Dense(units = 10, activation = 'softmax'))

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

# Fit model
model.fit(x = new_X_train, y = new_y_train, batch_size = 10, epochs = 1)
 

Комментарии:

1. не могли бы вы показать мне new_y_train.shape , пожалуйста?

2. Это (26179, 10, 10) @BDouchet

Ответ №1:

Ваш код работает:

 import tensorflow as tf
from tensorflow import keras
new_X_train = tf.random.uniform((26179, 100, 100, 1))
new_y_train = tf.random.uniform((26179,))
# Convert labels to categorical
new_y_train = keras.utils.to_categorical(new_y_train, 10)

# Create the model
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(64, (3, 3), input_shape = new_X_train.shape[1:], activation = 'relu'))
model.add(keras.layers.MaxPooling2D(pool_size = (2, 2)))

model.add(keras.layers.Conv2D(64, (3, 3), activation = 'relu'))
model.add(keras.layers.MaxPooling2D(pool_size = (2, 2)))

model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(units = 64, activation = 'relu'))

model.add(keras.layers.Dense(units = 10, activation = 'softmax'))

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

# Fit model
model.fit(x = new_X_train, y = new_y_train, batch_size = 10, epochs = 1)
 

Ответ №2:

Я думаю, что моя проблема была с этим фрагментом кода:

 # Convert labels to categorical
new_y_train = keras.utils.to_categorical(new_y_train, 10)
 

По какой-то причине я получал матрицу 10×10 для каждого элемента, но теперь, похоже, она работает и просто дает мне массив из 10 элементов

Комментарии:

1. Я думаю, вы запускаете его дважды, и, поскольку у вас одинаковое имя, это вызывает эту ошибку