【问题标题】:How to output a 2-D matrix from a neural network in Keras with softmax applying for each column?如何从 Keras 中的神经网络输出二维矩阵,并为每一列应用 softmax?
【发布时间】:2019-05-21 21:27:16
【问题描述】:

我正在尝试实现一个神经网络,它为每列输出一个带有 softmax 激活的二维矩阵。

我已经用下面的代码做到了,但是当列数增加时它似乎很慢。

input = Input(shape=[100])
h1 = Dense(200, activation='relu')(input)
output = []
for i in range(n_cols):
    output.append(Dense(n_rows, activation='softmax')(h1))
outputs = concatenate(output, axis=1)
model = Model(inputs=input, outputs=output)

有人可以向我建议更快的方法吗?任何帮助将不胜感激。

【问题讨论】:

    标签: python-3.x tensorflow keras neural-network


    【解决方案1】:

    您可以尝试使用list comprehension,众所周知,它通常比 for 循环快。

    代替:

    output = []
    for i in range(n_cols):
        output.append(Dense(n_rows, activation='softmax')(h1))
    

    你可以试试:

    output = [(Dense(n_rows, activation='softmax')(h1)) for i in range(n_cols)]
    

    不过,一般来说,添加的列越多,速度就越慢。

    【讨论】:

    • 谢谢,速度有点快,但是列数大的时候还是很慢。您对矩阵中的处理有任何想法吗,例如输出中所有列的softmax?
    • 我不知道。您可能会考虑使用 Keras 的后端之一而不是 Keras。
    猜你喜欢
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 2017-01-06
    相关资源
    最近更新 更多