另一个“在检查目标时出错:期望density_2具有形状(4,),但是得到具有形状(1,)的阵列". [英] Yet another "Error when checking target: expected dense_2 to have shape (4,) but got array with shape (1,)"
问题描述
我正在Python 3中使用Keras.我遇到的问题似乎与许多其他问题相似,尽管我没有看到如何设置该问题,但我能告诉我的最好情况是我可能需要使用Flatten().参数正确.我收到错误消息:
I'm using Keras in Python 3. The issue I'm having seems to be similar to many others, and the best I can tell I might need to use Flatten(), though I am not seeing how to set the parameters correctly. I get the error:
ValueError:检查目标时出错:预期density_2具有形状 (4,)但形状为(1,)
ValueError: Error when checking target: expected dense_2 to have shape (4,) but got array with shape (1,)
我的数据还不是图像,但它们是我转为数据帧的序列.
My data is not of images (yet) but they are sequences I've turned in to data frames.
model = Sequential()
model.add(Dense(30, input_dim=16, activation='relu'))
model.add(Dense(len(TheBinsizeList), activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(Train_Columns, TrainTarget_Columns.to_frame(), epochs=100, batch_size=64)
print(Train_Columns.shape)
# Gives a value of (1627, 16)
print((TrainTarget_Columns.to_frame()).shape)
# Gives a value of (1627,1)
现在TrainTarget_Columns的值是以下两个元组的1627:
Now the value of TrainTarget_Columns are 1627 of these two tuples:
(1494,3)(1080,2)(1863,2)(919,4)(1700,2)(710,4)(1365,4)(1815,3)(1477,2)(1618) ,1)...
(1494, 3) (1080, 2) (1863, 2) (919, 4) (1700, 2) (710, 4) (1365, 4) (1815, 3) (1477, 2) (1618, 1)...
主题编号是每个小管中的第一个条目,第二个条目是作为训练目标的值.
The subject number is the first entry in each of the tuble, and the second entry is the value that is the training target.
虽然我看到将density_2中的TheBinsizeList从4更改为2会导致期望的形状从(4,)变为(2,),但我看不到如何正确使用Flatten(如果需要)格式化值.
While I see that changing TheBinsizeList from 4 to 2 in dense_2 causes the expected shape to go from (4,) to (2,) I don't see how to use Flatten (if that is what is needed) to correctly format the values.
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 30) 510
_________________________________________________________________
dense_2 (Dense) (None, 4) 124
=================================================================
Total params: 634
Trainable params: 634
Non-trainable params: 0
感谢您的任何帮助.
推荐答案
考虑到您的模型摘要,模型期望输入形状为(batch_size, 16)
且目标形状为(batch_size, 4)
.
Considering your model summary, the model expects an input of shape (batch_size, 16)
and a target of shape (batch_size, 4)
.
如果目标的形状为(1627,1)
,则说明您有问题.
If your target's shape is (1627,1)
there is your problem.
解决方案:将其更改为一个热门变量(例如使用tf.one_hot(y, n_classes)
),错误应消失
Solution: Change it to a one hot variable (e.g. using tf.one_hot(y, n_classes)
) and the error should disappear
import numpy as np
import tensorflow as tf
input_dim = 16
hidden_dim = 30
n_classes = 4
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(hidden_dim, input_dim=input_dim, activation='relu'))
model.add(tf.keras.layers.Dense(n_classes, input_dim=hidden_dim, activation='relu'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
X = np.random.randn(100, input_dim)
y = np.random.randint(0, n_classes, size=(100,))
model.fit(X, y)
# ValueError: Shapes (None, 1) and (None, 4) are incompatible
y = tf.one_hot(y, n_classes)
model.fit(X, y)
# Works !
这篇关于另一个“在检查目标时出错:期望density_2具有形状(4,),但是得到具有形状(1,)的阵列".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!