【问题标题】:Trouble going from CuDNNLSTM into a Dense Layer从 CuDNNLSTM 到密集层的麻烦
【发布时间】:2019-02-15 13:01:31
【问题描述】:

我有一个问题,我花了太长时间处理。我已经阅读了很多文档并观看了很多关于此的教程,但不明白为什么当我从示例中修改我的网络并在其中提供我自己的数据时总是讨厌它!

我有一个形状的数据集:

train_X.shape = (312,5000,7)
train_Y.shape = (312,5000,1)

我有正确的顺序(312 个样本,5000 个时间步长,7 个数据通道)。然后,我为这 312 个样本中的每一个创建了一个标签集,如果它是我想要的还是不想要的,它只会说 0 或 1。这与 Coursera 中完成的激活词工作非常相似,但我根本不想使用语音或音频,这是传感器数据(时间驱动的传感器数据)。

我有 2 个问题,我想如果有人能回答第一个问题,我可能会弄清楚,或者如果你能明白我为什么在这里失败,那也将不胜感激。

我决定构建一个相当深的模型(我的浅层模型表现不佳),并从原始模型中重新设计了它。我的问题是它根本不再喜欢维度。

model = Sequential()
model.add(CuDNNLSTM(LSTM_units,
                  input_shape = (x_train.shape[1:]),
                  return_sequences=True))
model.add(Dropout(DR))

model.add(CuDNNLSTM(LSTM_units,
                   return_sequences=True))
model.add(Dropout(DR))

model.add(CuDNNLSTM(LSTM_units))
model.add(Dropout(DR))    

model.add(Dense(32, activation='relu'))
model.add(Dropout(DR))

model.add(Dense(2, activation='softmax'))

我的编译工作得很好,很高兴并给了我一个摘要详细说明我的模型将在 LSTM 结束时慢慢地从 (None, 5000, 18) --> (None, 128) 变为 ( None, 32) --> (None, 2) 因为我有 2 个类(0 或 1)。

在尝试拟合模型时,我当然会被告知:

Error when checking target: expected dense_14 to have 2 dimensions, but got array with shape (312, 5000, 1)

我尝试在将矩阵传递给密集层时对其进行整形,但这似乎根本不起作用......

所以我的问题是: 1) 在构建模型方面我做错了什么?

2) 有没有办法...在每一步发生时打印张量大小,而不是在 model.summary 命令中?

提前致谢。

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    我在这里找到了解决方案

    Incompatible dense layer error in keras

    我认为我不能将我的问题标记为重复的问题...基本上我遇到的问题是我要求网络给我一个 (batch_size, 2) 的输出,而实际上我想得到输入中每个数据点的答案!

    我将几个教程中的不良做法合二为一,给我带来了很多问题。希望这可以帮助任何有同样问题的人。我的更新代码也在下面发布。

    def create_model(x_train, GRU_units,DR):
    model = Sequential()
    print("Adding first layer...")
    model.add(CuDNNLSTM(GRU_units,
                      input_shape = (x_train.shape[1:]),
                      return_sequences=True))
    model.add(Dropout(DR))
    print(model.output_shape)
    
    print("Adding second layer...")
    model.add(CuDNNLSTM(GRU_units,
                       return_sequences=True))
    model.add(Dropout(DR))
    print(model.output_shape)
    
    print("Adding third layer...")
    model.add(CuDNNLSTM(GRU_units, 
                        return_sequences=True))
    model.add(Dropout(DR))
    print(model.output_shape)    
    
    print("Adding fourth layer (Dense 32)...")
    model.add(TimeDistributed(Dense(32, activation='relu')))
    model.add(Dropout(DR))
    print(model.output_shape)
    
    print("Adding output layer...")
    model.add(TimeDistributed(Dense(1, activation='sigmoid')))
    print(model.output_shape)
    
    return model
    

    【讨论】:

      猜你喜欢
      • 2021-04-05
      • 1970-01-01
      • 2012-01-20
      • 1970-01-01
      • 2020-11-02
      • 2017-09-20
      • 2016-05-05
      • 2011-07-10
      相关资源
      最近更新 更多