【问题标题】:Keras NoneTypeError in deep auto-encoder-decoder structure / Shape error深度自动编码器-解码器结构中的 Keras NoneTypeError / 形状错误
【发布时间】:2021-04-23 07:35:53
【问题描述】:

我无法训练我的神经网络。我将神经网络定义如下:

shared = embedding_layer

inputA = keras.Input(shape=(40, ), name="anchor")  # Variable-length sequence of ints
inputP = keras.Input(shape=(40, ), name="positive")  # Variable-length sequence of ints
inputN = keras.Input(shape=(40, ), name="negative")  # Binary vectors of size num_tags

concatenated = layers.concatenate([inputA, inputP, inputN])

embedded_A = shared(concatenated)

encoded = Dense(900, activation = "relu")(embedded_A)
encoded = Dense(600, activation = "relu")(encoded)
encoded = Dense(300, activation = "relu")(encoded)
encoded = Dense(100, activation = "relu")(encoded)

decoded = Dense(100, activation = "relu")(encoded)
decoded = Dense(300, activation = "relu")(decoded)
decoded = Dense(600, activation = "relu")(decoded)
decoded = Dense(900, activation = "relu")(decoded)


predictionsA = Dense(40, activation="sigmoid", name ='outA')(decoded)
predictionsP = Dense(40, activation="sigmoid", name ='outB')(decoded)
predictionsN = Dense(40, activation="sigmoid", name ='outC')(decoded)

ml_model = keras.Model(
    inputs=[inputA, inputP, inputN],
    outputs=[predictionsA, predictionsP, predictionsN]
)


ml_model.compile(
    optimizer='adam',
    loss='mse'
)

ml_model.fit(
    {"anchor": anchor, "positive": positive, "negative": negative},
    {"outA": anchor, "outB": positive, 'outC': negative},
    epochs=2)

示意图类似于

嵌入层定义如下:

embedding_m = model.syn0
embedding_layer = Embedding(len(vocab),
                            300,
                            weights=[embedding_m],
                            input_length=40,
                            trainable=True)


What I feed into the network is three numpy arrays of shape (120000, 40) which look like this:


array([[   2334,   23764,    7590, ..., 3000001, 3000001, 3000001],
       [3000000,    1245,    1124, ..., 3000001, 3000001, 3000001],
       [    481,     491,    5202, ..., 3000001, 3000001, 3000001],
       ...,
       [3000000,     125,   20755, ..., 3000001, 3000001, 3000001],
       [1217971,  168575,     239, ...,    9383,    1039,   87315],
       [  12990,      91,  258231, ..., 3000001, 3000001, 3000001]])

而且输入和输出一样,因为我正在做一个自动编码器解码器。

我得到的错误是:

尺寸必须相等,但对于输入形状为 [32,120,40 ],[32,40]。

但我似乎无法找出原因,或者如何解决它......有什么想法吗?如果需要,我可以提供更多示例。我怀疑存在一些尺寸错误,因为理想情况下我希望输出的形状 (120000,40) 与输入完全相同。

【问题讨论】:

    标签: machine-learning keras nlp


    【解决方案1】:

    有问题的端编码器 - 解码器的修复版本:

    import tensorflow as tf
    from tensorflow import keras
    from tensorflow.keras import layers
    from keras.layers import Dense
    
    #shared = embedding_layer
    #Simulate that...
    shared=Dense(1,activation="relu")
    
    inputA = keras.Input(shape=(40, ), name="anchor")  # Variable-length sequence of ints
    inputP = keras.Input(shape=(40, ), name="positive")  # Variable-length sequence of ints
    inputN = keras.Input(shape=(40, ), name="negative")  # Binary vectors of size num_tags
    
    concatenated = layers.concatenate([inputA, inputP, inputN])
    
    embedded_A = shared(concatenated)
    
    encoded = Dense(900, activation = "relu")(embedded_A)
    encoded = Dense(600, activation = "relu")(encoded)
    encoded = Dense(300, activation = "relu")(encoded)
    encoded = Dense(100, activation = "relu")(encoded)
    
    #decoded = Dense(100, activation = "relu")(encoded)
    decoded = Dense(300, activation = "relu")(encoded)
    decoded = Dense(600, activation = "relu")(decoded)
    decoded = Dense(900, activation = "relu")(decoded)
    
    
    predictionsA = Dense(40, activation="sigmoid", name ='outA')(decoded)
    predictionsP = Dense(40, activation="sigmoid", name ='outB')(decoded)
    predictionsN = Dense(40, activation="sigmoid", name ='outC')(decoded)
    
    ml_model = keras.Model(
        inputs=[inputA, inputP, inputN],
        outputs=[predictionsA, predictionsP, predictionsN]
    )
    
    
    ml_model.compile(
        optimizer='adam',
        loss='mse'
    )
    
    #Simulate...
    anchor=tf.random.uniform((100,40))
    positive=tf.random.uniform((100,40))
    negative=tf.random.uniform((100,40))
    
    ml_model.fit(
        {"anchor": anchor, "positive": positive, "negative": negative},
        {"outA": anchor, "outB": positive, 'outC': negative},
        epochs=2)
    

    【讨论】:

      【解决方案2】:

      删除一个“解码”行以修复您的网络结构:

      【讨论】:

      • 还将第 24 行中的最后一部分更改为编码。我会将工作代码添加给您作为答案!
      • 太棒了,它有效!非常感谢。新问题是它只运行我的句子的一次迭代,所以我从最后一个输出节点的输出权重是 (300,40),遗憾的是不是 (120000, 300, 40),因为我对我的所有句子都期望。我应该把它放在一个循环中吗?