Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:检查目标时出错:预期密集_3具有形状(1),但获得具有形状(2,)的数组keras_Python_Tensorflow_Keras_Keras Layer - Fatal编程技术网

Python ValueError:检查目标时出错:预期密集_3具有形状(1),但获得具有形状(2,)的数组keras

Python ValueError:检查目标时出错:预期密集_3具有形状(1),但获得具有形状(2,)的数组keras,python,tensorflow,keras,keras-layer,Python,Tensorflow,Keras,Keras Layer,当我尝试从pyimagesearch学习CNN时,我发现了这个错误。我曾尝试将最后一个密度从3改为1,但它没有解决我的问题,我已经将它改为binnar_crossentroypy,但它仍然不起作用。这是我的代码。很抱歉,这是一个愚蠢的问题,也许是同一个问题,但我已经做了我能做的 ss# grab the image paths and randomly shuffle them imagePaths = sorted(list(paths.list_images(args["dataset"])

当我尝试从pyimagesearch学习CNN时,我发现了这个错误。我曾尝试将最后一个密度从3改为1,但它没有解决我的问题,我已经将它改为binnar_crossentroypy,但它仍然不起作用。这是我的代码。很抱歉,这是一个愚蠢的问题,也许是同一个问题,但我已经做了我能做的

ss# grab the image paths and randomly shuffle them
imagePaths = sorted(list(paths.list_images(args["dataset"])))
random.seed(42)
random.shuffle(imagePaths)


for imagePath in imagePaths:
# load the image, resize the image to be 32x32 pixels (ignoring
# aspect ratio), flatten the image into 32x32x3=3072 pixel image
# into a list, and store the image in the data list
image = cv2.imread(imagePath)
image = cv2.resize(image, (32, 32)).flatten()
data.append(image)

# extract the class label from the image path and update the
# labels list
label = imagePath.split(os.path.sep)[-2]
labels.append(label)

# scale the raw pixel intensities to the range [0, 1]
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)

 # partition the data into training and testing splits using 75% of
 # the data for training and the remaining 25% for testing
  (trainX, testX, trainY, testY) = train_test_split(data,
   labels, test_size=0.25, random_state=42)



 lb =  LabelEncoder()
 trainY = lb.fit_transform(trainY)
 testY = to_categorical(testY, 2)


 # define the 3072-1024-512-3 architecture using Keras
 model = Sequential()
 model.add(Dense(1024, input_shape=(3072,), activation="sigmoid"))
 model.add(Dense(512, activation="sigmoid"))
 model.add(Dense(1, activation="softmax"))

将此行
trany=to_category(trany,2)
添加到此行
testY=to_category(testY,2)
之后。并将最后一层更改为
model.add(稠密(2,activation=“softmax”)
,因为它应该与目标一样的2D矩阵相匹配。此外,如果损失函数不是分类函数,请确保它是分类函数。

错误可能来自以下行:

model.add(Dense(1, activation="softmax"))
神经网络期望数组y只有一个值。这没有任何意义,因为y的维度有两个值。因此,您应该尝试以下方法:

model.add(Dense(2, activation="softmax"))

如果2是您正在使用的类数。

我试图将其更改为模型。添加(稠密(2,activation=“softmax”))但得到此错误值错误:检查目标时出错:预期稠密(3)具有形状(2),但得到了具有形状(1)的数组,但我有一个问题,在pyimagesearch web中,作者告诉读者将其更改为二进制交叉tentropy,因为只有两个类,它对我的培训模型有效吗?很高兴我能提供帮助。根据我的理解,分类交叉熵更灵活,因为它还可以处理二进制类。相比之下,二进制交叉熵只适用于两类。在两个班上,他们做的是相同的。我有更多的问题为什么我需要用labelencoder对我的训练进行编码?