gpt4 book ai didi

tensorflow - categorical_crossentropy 损失形状中的 ValueError

转载 作者:行者123 更新时间:2023-11-30 09:58:13 25 4
gpt4 key购买 nike

我正在构建一个多类 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.

最佳答案

custom_one_hot 函数返回一个 [M, 1, 3] 数组。您应该将其 reshape 为 [M, 3],因为 CNN 的输出是 [M, 3]。这里的M是批量大小。

关于tensorflow - categorical_crossentropy 损失形状中的 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60129234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com