ValueError:检查目标时出错:预期activation_5的形状为(1,),但数组的形状为(100,)

时间:2019-11-13 09:49:55

标签: python tensorflow keras lstm

我目前正在尝试使用顺序视频数据训练LSTM网络。现在仍然出现的问题是数据本身的输出形状存在错误。从视频中,我生成了100个时间步(帧)的686个样本剪辑。然后,使用另一个CNN,创建了每个图像的形状2048的嵌入。换句话说,在我的情况下,X_train的形状为(686,100,2048),Y_train的形状为(686,100)。现在,当我通过网络传递数据集时,会出现形状错误。

我的模特:

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

我这样运行:

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

导致此错误消息:enter image description here

关于我可能做错了什么的任何想法?

型号摘要: enter image description here

1 个答案:

答案 0 :(得分:0)

如果不具有错误丢失功能,则无需深入检查代码,就会发生99%的错误。

请使用loss ='categorical_crossentropy'而不是loss ='sparse_categorical_crossentropy'修改模型编译:

classification_model.compile(loss='categorical_crossentropy',........)

区别在于目标的编码。如果您的目标是一次性编码,请使用categorical_crossentropy。