用于softmax激活的Keras docs可以指定激活应用于哪个轴。我的模型应该输出一个n×k的矩阵M,其中Mij是第i个字母是符号j的概率。

n = 7 # number of symbols in the ouput string (fixed)
k = len("0123456789") # the number of possible symbols

model = models.Sequential()
model.add(layers.Dense(16, activation='relu', input_shape=((N,))))
...
model.add(layers.Dense(n * k, activation=None))
model.add(layers.Reshape((n, k)))

model.add(layers.Dense(output_dim=n, activation='softmax(x, axis=1)'))

由于我不知道如何正确地为softmax激活指定轴(在我的情况下为k的轴),因此代码的最后一行未编译。

最佳答案

您必须在此处使用实际函数,而不是字符串。

为了方便起见,Keras允许您使用一些字符串。

激活功能可以在keras.activations中找到,并在the help file中列出。

from keras.activations import softmax

def softMaxAxis1(x):
    return softmax(x,axis=1)

.....
......
model.add(layers.Dense(output_dim=n, activation=softMaxAxis1))

甚至是自定义轴:
def softMaxAxis(axis):
    def soft(x):
        return softmax(x,axis=axis)
    return soft

...
model.add(layers.Dense(output_dim=n, activation=softMaxAxis(1)))

关于deep-learning - 在Keras层中使用softmax激活时如何指定轴?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45947111/

10-12 23:05