ValueError:输入0与层lstm不兼容:预期形状=(无,无,3),找到的形状=[288,3,1]

2024-04-20 16:24:07 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试图建立一个LSTM模型来预测每一个时间步的二进制分类。我的数据是许多文本中每个段落的复杂性特征

    train_x = np.array(train_x)
    train_y = np.array(train_y)
    x_size = train_x.shape[0] * train_x.shape[1]
    train_x = train_x.reshape(x_size, train_x.shape[2])
    train_x = np.expand_dims(train_x, 1)
    train_x = train_x.transpose(0,2,1)
    train_y = train_y.flatten()

    test_x = np.array(test_x)
    test_y = np.array(test_y)
    x_test_size = test_x.shape[0] * test_x.shape[1]
    test_x = test_x.reshape(x_test_size, test_x.shape[2])
    test_x = np.expand_dims(test_x, 1)
    test_x = test_x.transpose(0, 2, 1)
    test_y = test_y.flatten()


    shape = train_x.shape # 3D: number of texts * number of padded paragraphs, number of features, 1
    time_steps = shape[0]  # number of padded pars * number of texts # this equals 288
    features = shape[1]  # number of features # this equals 3
    print(train_x.shape)
    print(train_y.shape)

    model = Sequential()
    model.add(layers.Masking(mask_value=0, input_shape=(time_steps, features)))
    model.add(layers.LSTM(128, return_sequences=True, return_state=False, input_shape=(time_steps, features)))  # 128 internal units
    model.add(layers.TimeDistributed(layers.Dense(1, activation='sigmoid')))
    #model.add(layers.Dense(len(train_y)))  # Dense layer
    model.compile(loss='binary_crossentropy', optimizer='adam')
    model.summary()

    model.fit(train_x, train_y, batch_size=train_y.shape[0])

    predictions = model.predict(test_x)

这就是错误:

 ValueError: Input 0 is incompatible with layer lstm: expected shape=(None, None, 3), found shape=[288, 3, 1]

我不明白它为什么期望(无,无,3)以及我能做些什么。 欢迎您的任何意见

谢谢


Tags: oftestaddnumbersizemodeltimelayers