[英]ValueError: Error when checking target: expected activation_5 to have shape (1,) but got array with shape (100,)
I'm currently trying to train a LSTM network with sequential video data.我目前正在尝试使用顺序视频数据训练 LSTM 网络。 The problem that keeps occurring now though is an error with the output shape of the data itself.
不过,现在不断发生的问题是数据本身的 output 形状存在错误。 From the video, I have generated 686 sample clips of 100 time steps (frames).
从视频中,我生成了 100 个时间步长(帧)的 686 个样本片段。 Then with another CNN, I've created an embedding of shape 2048 of each image.
然后使用另一个 CNN,我创建了每个图像的形状 2048 的嵌入。 In other words, the shape of X_train in my case is (686,100,2048) and the shape of Y_train is (686,100).
换句话说,在我的例子中,X_train 的形状是 (686,100,2048),而 Y_train 的形状是 (686,100)。 Now when I pass my dataset through my network, I get this shape error.
现在,当我通过我的网络传递我的数据集时,我得到了这个形状错误。
My model:我的 model:
from keras.layers import Activation, Input, Dense, Lambda, LSTM, Flatten
from keras.models import Model
def model_builder(input_shape):
base_input = Input(shape = input_shape)
x = LSTM(units=50, name='LSTM1', return_sequences=True)(base_input)
x = Flatten()(x)
x = Dense(units = 3)(x)
x = Activation('softmax')(x)
classification_model = Model(base_input, x,name='classifier')
classification_model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
return classification_model
And I run it like this:我像这样运行它:
batch_size = 64
epochs = 20
timesteps = 100
embedding_shape=2048
classification_model = model_builder((timesteps,embedding_shape))
try:
Y_train=Y_train.reshape((686,timesteps))
X_train = np.reshape(X_train,(686, timesteps,embedding_shape))
outcome = classification_model.fit(x=X_train, y=Y_train, batch_size=batch_size, epochs=epochs, verbose=1, callbacks=None, validation_split=(6200/68600), validation_data=None, shuffle=False, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, validation_freq=1)
except KeyboardInterrupt:
pass
Resulting in this error message:导致此错误消息:
Any ideas as to what I might be doing wrong?关于我可能做错了什么的任何想法?
Without checking your code in depth, 99% of this Errors occur if you have the false loss function.如果没有深入检查您的代码,如果您有错误丢失 function,则会发生 99% 的此类错误。
Please modify your model compilation with the loss='categorical_crossentropy' instead of loss='sparse_categorical_crossentropy':请使用 loss='categorical_crossentropy' 而不是 loss='sparse_categorical_crossentropy' 修改您的 model 编译:
classification_model.compile(loss='categorical_crossentropy',........)
The difference is the encodings of you targets.不同之处在于您的目标的编码。 If your targets are one-hot encoded, use categorical_crossentropy.
如果您的目标是一次性编码的,请使用 categorical_crossentropy。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.