Tensorflow ValueError:logits和标签必须具有相同的形状((None,42)vs(None,1))

时间:2020-06-30 14:26:36

标签: tensorflow machine-learning keras

我运行model.fit()时出现Tensorflow错误。这是我的代码。

train_data = pd.read_csv('train.csv')
train_data = shuffle(train_data).reset_index(drop=True)
split_data = np.array_split(train_data, 50)
train_image = []
for i in tqdm(range(split_data[0].shape[0])):
    path = 'train/train/'+str(train_data['category'][i]).zfill(2)+'/'+train_data['filename'][i]
    img = image.load_img(path,target_size=(400,400,3))
    img = image.img_to_array(img)
    img = img/255
    train_image.append(img)
X = np.array(train_image)  # X.shape (2108, 400, 400, 3)
y = np.array(split_data[0]['category'])   # y.shape (2108,)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.1)

这是我的CNN模型。

model = Sequential()
model.add(Conv2D(filters=16, kernel_size=(5, 5), activation="relu", input_shape=(400,400,3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
...
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(42, activation='sigmoid'))

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test), batch_size=64)

运行model.fit()时出错

ValueError: logits and labels must have the same shape ((None, 42) vs (None, 1))

X_train

的值
array([[[[0.99607843, 0.99607843, 0.99607843],
         [0.99607843, 0.99607843, 0.99607843],
         [0.99607843, 0.99607843, 0.99607843],
         ...,
         [1.        , 1.        , 1.        ],
         [1.        , 1.        , 1.        ],
         [1.        , 1.        , 1.        ]],
         ...,
       ]]], dtype=float32)

y_train

的值
array([ 5, 41, 24, ..., 41, 19, 40], dtype=int64)

1 个答案:

答案 0 :(得分:2)

您正在执行多分类问题。您的标签也是整数编码

使用softmax作为最后一层的激活:Dense(42, activation='softmax')

sparse_categorical_crossentropy作为损失函数