【问题标题】:Outputting matrix with softmax activation on each row in TensorFlow model在 TensorFlow 模型的每一行上输出带有 softmax 激活的矩阵
【发布时间】:2019-08-02 07:54:12
【问题描述】:

我正在构建一个 TensorFlow (2.0) 模型,该模型将图像(30x100 矩阵)作为输入,并希望能够具有以下形式的输出(和标签)

[
  [0.0, 0.3, 0.7, 0.0],
  [1.0, 0.0, 0.0, 0.0],
  [0.2, 0.2, 0.4, 0.2]
]

即每一行都有一个单独的 softmax 激活,这意味着它们总和为一个。在训练数据中,每行中只有一个元素为 1,其余元素为 0,我相信这被称为 one-hot encoding。因此我的问题是;如何配置最后一层(和损失函数)以在我的标签上使用 one-hot 编码?

【问题讨论】:

    标签: python tensorflow keras one-hot-encoding softmax


    【解决方案1】:

    它看起来是多标签分类的变体。为方便起见,我们可以连接 3 个 softmax 层的输出并使用二元交叉熵损失。对于预测,我们可以重塑连接的输出。

    工作代码

    inputs = Input(shape=(2,))
    output = Dense(4, activation='relu')(inputs)
    
    output_1 = Dense(4, activation='softmax')(output)
    output_2 = Dense(4, activation='softmax')(output)
    output_3 = Dense(4, activation='softmax')(output)
    
    # concatenate the outputs
    output = concatenate([output_1, output_2, output_3], axis=1)
    
    model = Model(inputs=inputs, outputs=output)
    model.compile(loss='binary_crossentropy', optimizer='adam') 
    
    # Two training examples each of 2 features
    x = np.array([[1,2],
                  [2,1]])
    
    # Output labels
    y = np.array([[[0,0,0,1],[0,0,1,0],[0,1,0,0]],
                  [[0,0,1,0],[0,0,1,0],[0,0,0,1]]])
    
    # Fatten the y per sample to match the model output shape
    model.fit(x, y.reshape(len(x), -1))
    
    # Predications
    y_hat = model.predict(x)
    y_hat = y_hat.reshape(len(x),-1,4)
    
    print (y_hat)
    

    输出:

    array([[[0.0418521 , 0.63207364, 0.06171123, 0.26436302],
            [0.54364955, 0.19503883, 0.09884372, 0.16246797],
            [0.06045745, 0.09223039, 0.7325132 , 0.11479893]],
    
           [[0.05648099, 0.40420422, 0.12369599, 0.41561884],
            [0.64175993, 0.14215547, 0.07769462, 0.13838997],
            [0.07918497, 0.1764104 , 0.57678604, 0.1676186 ]]], dtype=float32)
    

    您还可以使用y_hat.sum(axis=2) 验证每一行的总和为 1

    【讨论】:

      【解决方案2】:

      如果行彼此独立,在我看来,您的案例可以是多输出案例。您可以有 3 个输出,每个输出 5 个值。

      input1 = Input(shape = (input_shape))
      # some layers
      x = Dense(512, activation='relu')(input1)
      # .....
      
      outputs1 = Dense(5, activation='softmax', name="row1")(x)
      outputs2 = Dense(5, activation='softmax', name="row2")(x)
      outputs3 = Dense(5, activation='softmax', name="row3")(x)
      model = Model(input1, [outputs1, outputs2, outputs3])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-24
        • 2017-07-30
        • 2019-09-20
        • 1970-01-01
        • 1970-01-01
        • 2018-03-13
        • 2022-01-08
        • 1970-01-01
        相关资源
        最近更新 更多