在检查目标时出现错误:预期的conv2d_3有4个维度,但有形状的数组(1006,5)

时间:2022-02-25 09:48:37

Keras throws an error message on this trivial example. Need help. Also, is there a documentation on what the tensor dimensions it expects for Conv2D and Input? Spent too much time trying to find solutions and mutate/rotate the tensor every each way...

Keras在这个简单的示例上抛出了一个错误消息。需要帮助。还有关于它对对流和输入的张量尺寸期望的文档吗?花了太多的时间去寻找解决方案,并在每一种方式上改变张量……

My specs: Windows 10 x64, Python 3.6 (from Anaconda 3 x64), Keras 2.09, TensorFlow 1.4.0

我的规格:Windows 10 x64, Python 3.6(来自Anaconda 3 x64), Keras 2.09, TensorFlow 1.4.0。

import numpy as np
from keras.models import Model
from keras.layers import Conv2D, Input
from keras.utils.np_utils import to_categorical

n_samples, n_row, n_col, n_channels = 1006, 99, 81, 1

tX = np.random.rand(n_samples, n_row, n_col, n_channels)
tY = np.random.randint(0,5,n_samples)
inp = Input(shape=(n_row, n_col, n_channels))
lr = Conv2D(16, kernel_size=2, padding='same')(inp)
M = Model(inputs=inp, outputs=lr)
M.compile(optimizer='Adam',loss='categorical_crossentropy')
M.fit(tX, to_categorical(tY, num_classes=None))

gives the error message:

给了错误信息:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-11-27bd9e59639d>", line 14, in <module>
    M.fit(tX, to_categorical(tY, num_classes=None))
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\keras\engine\training.py", line 1581, in fit
    batch_size=batch_size)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\keras\engine\training.py", line 1418, in _standardize_user_data
    exception_prefix='target')
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\keras\engine\training.py", line 141, in _standardize_input_data
    str(array.shape))
ValueError: Error when checking target: expected conv2d_3 to have 4 dimensions, but got array with shape (1006, 5)

1 个解决方案

#1


1  

Well the problem is that your model makes no sense. The output of your convolution has shape (1006, 99, 81, 16), and since you built a model with this output, it is the output of your model as well. Your labels have shape (1006, 5). Both shapes have to match in order to compute the loss function, but since they don't, then Keras raises an error as the output shape is unexpected.

问题是你的模型毫无意义。您的卷积的输出有形状(1006、99、81、16),并且由于您用这个输出构建了一个模型,所以它也是您的模型的输出。您的标签有形状(1006,5)。两个形状都必须匹配以计算损失函数,但是由于它们没有,所以Keras会产生一个错误,因为输出形状是出乎意料的。

An easy way to fix it is to add a Dense(5, activation = "softmax") layer after he convolution, and making this the model output, as then the output shape will be (1006, 5). This layer just acts as a classifier over the convolution output.

一种简单的方法是在他卷积后添加一个密集的(5,激活= "softmax")层,并使其成为模型输出,然后输出形状将是(1006,5)。

#1


1  

Well the problem is that your model makes no sense. The output of your convolution has shape (1006, 99, 81, 16), and since you built a model with this output, it is the output of your model as well. Your labels have shape (1006, 5). Both shapes have to match in order to compute the loss function, but since they don't, then Keras raises an error as the output shape is unexpected.

问题是你的模型毫无意义。您的卷积的输出有形状(1006、99、81、16),并且由于您用这个输出构建了一个模型,所以它也是您的模型的输出。您的标签有形状(1006,5)。两个形状都必须匹配以计算损失函数,但是由于它们没有,所以Keras会产生一个错误,因为输出形状是出乎意料的。

An easy way to fix it is to add a Dense(5, activation = "softmax") layer after he convolution, and making this the model output, as then the output shape will be (1006, 5). This layer just acts as a classifier over the convolution output.

一种简单的方法是在他卷积后添加一个密集的(5,激活= "softmax")层,并使其成为模型输出,然后输出形状将是(1006,5)。