【问题标题】:Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (1, 4000, 25)检查目标时出错:预期 dense_1 有 2 个维度,但得到了形状为 (1, 4000, 25) 的数组
【发布时间】:2020-01-10 18:45:58
【问题描述】:

我正在尝试训练 LSTM 网络进行无监督的二元分类。

我有一个整数矩阵作为输入,每一行都是不同的轨迹,每一列都是一个特征。

这是我使用的模型:

time_steps = 4000
features = 25
model = Sequential()
model.add(LSTM(128, input_shape=(time_steps, features), name='lstm'))
model.add(Dense(1, activation='relu'))
model.summary()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, x_train, batch_size=batch_size, epochs=epochs, verbose=2)

这是我得到的错误:

检查目标时出错:预期 dense_1 有 2 个维度,但得到的数组形状为 (1, 4000, 25)

它是在尝试运行时生成的model.fit

输入形成如下:

x_train = np.array([input_array[:4000]])

输入的每条轨迹都有 25 个特征。

我是该领域的新手,我不知道如何解决这个问题。 我查过类似的票,但没有一张能帮到我。

这是我分析的一些票:

Error when checking target: expected dense_1 to have 3 dimensions, but got array with shape (118, 1)

ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (68, 50, 50, 50, 1)

Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (1, 1226, 2)

ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (10000, 1)

【问题讨论】:

    标签: tensorflow keras classification lstm unsupervised-learning


    【解决方案1】:

    几点说明:

    • x_train 作为 LSTM 层的输入,它需要 3D 输入。第一个维度是样本,第二个维度是时间步长,最终维度是特征。
    • 当您调用 fit 时,您传递 x_train 两次,这意味着您希望目标与输入数据相同。如果您尝试进行自动编码器,这是有道理的,但是它不能在这种架构上工作。 Dense 层的输出将是单个值,它无法匹配输入的形状(即 3D)。下面我添加了一个 y_train,以便您的模型改为预测单个值。
    time_steps = 40
    features = 25
    x_train = np.random.normal(size=(10, time_steps, features))
    y_train =  np.random.normal(size=(10, ))
    model = Sequential()
    model.add(LSTM(128, input_shape=(time_steps, features), name='lstm'))
    model.add(Dense(1, activation='relu'))
    model.summary()
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    model.fit(x_train, y_train, batch_size=32, epochs=2, verbose=2)
    

    祝你好运!

    【讨论】:

      【解决方案2】:

      您尚未显示input_array 的形状,但您可以尝试使用np.reshape(input_array, (4000, 25)) 对其进行整形。阅读更多关于重塑here

      【讨论】:

        猜你喜欢
        • 2019-01-16
        • 2018-04-11
        • 2019-03-25
        • 1970-01-01
        • 2019-01-10
        • 1970-01-01
        • 1970-01-01
        • 2017-09-28
        • 1970-01-01
        相关资源
        最近更新 更多