categorical_crossentropy损失形状中的ValueError

时间:2020-02-08 17:22:26

标签: tensorflow machine-learning keras deep-learning conv-neural-network

我正在构建多类CNN模型,但由于损失形状误差而无法编译该模型。

  • 输出层和标签都应具有正确的形状;标签为(m,1,3),最后的致密层包含3个具有softmax激活的感知
  • loss ='categorical_crossentropy'
import numpy as np
import pandas as pd
from preprocess import DataLoader

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv3D, Dropout, MaxPooling3D
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras import optimizers

target_width = 160
target_height = 192
target_depth = 192

num_classes = 3
batch_size = 4

data_loader = DataLoader(target_shape=(target_width, target_height, target_depth))
train, test = data_loader.Get_Data_List()

print("Train size: " + str(len(train)))
print("Test size: " + str(len(test)))

def custom_one_hot(labels):
  label_dict = {"stableAD":np.array([0,0,1]),
              "stableMCI":np.array([0,1,0]),
              "stableNL":np.array([1,0,0])}
  encoded_labels = []
  for label in labels:
    encoded_labels.append(label_dict[label].reshape(1,3))
  return np.asarray(encoded_labels)

def additional_data_prep(train, test):
  # Extract data from tuples
  train_labels, train_data = zip(*train)
  test_labels, test_data = zip(*test)
  X_train = np.asarray(train_data)
  X_test = np.asarray(test_data)
  y_train = custom_one_hot(train_labels)
  y_test = custom_one_hot(test_labels)
  return X_train, y_train, X_test, y_test

X, y, X_test, y_test = additional_data_prep(train, test)

X = np.expand_dims(X, axis=-1).reshape((X.shape[0],target_width,target_height,target_depth,1))
X_test = np.expand_dims(X_test, axis=-1).reshape((X_test.shape[0],target_width,target_height,target_depth,1))

model = Sequential()
model.add(Conv3D(24, kernel_size=(13, 11, 11), activation='relu', input_shape=(target_width,target_height,target_depth,1), padding='same', strides=4))
model.add(MaxPooling3D(pool_size=(3, 3, 3), strides=2))
model.add(Dropout(0.1))
model.add(Conv3D(48, kernel_size=(6, 5, 5), activation='relu', padding='same'))
model.add(MaxPooling3D(pool_size=(3, 3, 3), strides=2))
model.add(Dropout(0.1))
model.add(Conv3D(24, kernel_size=(4, 3, 3), activation='relu'))
model.add(MaxPooling3D(pool_size=(3, 3, 3), strides=2))
model.add(Dropout(0.1))
model.add(Conv3D(8, kernel_size=(2, 2, 2), activation='relu'))
model.add(MaxPooling3D(pool_size=(1, 1, 1), strides=2))
model.add(Dropout(0.1))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer=optimizers.Adam(learning_rate=0.0015),
              metrics=['accuracy','categorical_crossentropy'])

model.fit(X, y, batch_size=batch_size, epochs=10, verbose=2, use_multiprocessing=True)  

model.evaluate(X_test, y_test, verbose=2, use_multiprocessing=True)

此错误消息的结果:

Traceback (most recent call last):
  File "train.py", line 70, in <module>
    model.fit(X, y, batch_size=batch_size, epochs=10, verbose=2, use_multiprocessing=True)
  File "/home/554282/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 819, in fit
    use_multiprocessing=use_multiprocessing)
  File "/home/554282/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 235, in fit
    use_multiprocessing=use_multiprocessing)
  File "/home/554282/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 593, in _process_training_inputs
    use_multiprocessing=use_multiprocessing)
  File "/home/554282/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 646, in _process_inputs
    x, y, sample_weight=sample_weights)
  File "/home/554282/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 2383, in _standardize_user_data
    batch_size=batch_size)
  File "/home/554282/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 2489, in _standardize_tensors
    y, self._feed_loss_fns, feed_output_shapes)
  File "/home/554282/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_utils.py", line 810, in check_loss_and_target_compatibility
    ' while using as loss `' + loss_name + '`. '
ValueError: A target array with shape (8, 1, 3) was passed for an output of shape (None, 3) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output.

1 个答案:

答案 0 :(得分:0)

custom_one_hot函数返回一个[M, 1, 3]数组。由于CNN的输出为[M, 3],因此应将其重塑为[M, 3]。 M是批量大小。