This question might be very specific application related but I was blocked and I thought this is a good place to ask. Let's say we have an LSTM in Keras that is sequence to sequence, for example Part of Speech Tagger. The last layer gives me, sequence of labels with the probability of each label. Consider the following predicted output;
这个问题可能是非常具体的应用程序相关但我被阻止了,我认为这是一个很好的问题。假设我们在Keras中有一个LSTM,它是序列的序列,例如Part of Speech Tagger。最后一层给出了标签序列以及每个标签的概率。考虑以下预测输出;
A = [[0.1, 0.3, 0.2, 0.4],[0.2, 0.2, 0.2, 0.4],[0.5, 0.2, 0.1, 0.1]]
Basically this is a sequence of length 3 that has 4 possible tags at each time point of the sequence.
基本上,这是长度为3的序列,在序列的每个时间点具有4个可能的标签。
Now what I would like to do is change this sequence into following.
现在我想做的是将此序列更改为以下内容。
A' = [[0, 0, 0, 1],[0, 0, 0, 1],[1, 0, 0, 0]]
In other words, I want to put one at the location of the maximum probability and change all other ones to 0. Helps are very appreciated.
换句话说,我想把一个放在最大概率的位置,并将所有其他的改为0.帮助非常感谢。
1 个解决方案
#1
3
you can use this slightly modified version of a sampling function:
您可以使用此略微修改的采样函数版本:
def set_max_to_one(preds, temperature=0.01):
preds = np.log(preds) / temperature
exp_preds = np.exp(preds)
preds = exp_preds.T / np.sum(exp_preds, axis=1)
return preds.astype("int16").T
This returns what you expect. You can fiddle around with the temperature so that it is stable and doesn't return NA, but using 0.01 should be good enough. You might also want to change the type of the output array.
这会返回您的期望。你可以调整温度,使其稳定并且不会返回NA,但使用0.01应该足够好。您可能还想更改输出数组的类型。
Note that this will work if you use a numpy array object, if you want to use it for a keras tensor you will need to modify it (keeping into account batch size for example). Hope this helps
请注意,如果您使用numpy数组对象,这将有效,如果您想将其用于keras张量,则需要对其进行修改(例如,考虑批量大小)。希望这可以帮助
EDIT:
编辑:
This should work in keras:
这应该在keras中工作:
import keras.backend as K
def set_max_to_one(x, temperature=0.01):
x = K.log(x)/temperature
return K.round(K.softmax(x))
Instead of backend.softmax()
you could use layers.core.Activation()
if you wanted to set the axis
value.
如果要设置轴值,可以使用layers.core.Activation()而不是backend.softmax()。
Note that the output is still a tensor of float, not of int, but I can't find out how to change the tensor type. It shouldn't make much difference.
请注意,输出仍然是float的张量,而不是int,但我无法找到如何更改张量类型。它不应该有太大的区别。
#1
3
you can use this slightly modified version of a sampling function:
您可以使用此略微修改的采样函数版本:
def set_max_to_one(preds, temperature=0.01):
preds = np.log(preds) / temperature
exp_preds = np.exp(preds)
preds = exp_preds.T / np.sum(exp_preds, axis=1)
return preds.astype("int16").T
This returns what you expect. You can fiddle around with the temperature so that it is stable and doesn't return NA, but using 0.01 should be good enough. You might also want to change the type of the output array.
这会返回您的期望。你可以调整温度,使其稳定并且不会返回NA,但使用0.01应该足够好。您可能还想更改输出数组的类型。
Note that this will work if you use a numpy array object, if you want to use it for a keras tensor you will need to modify it (keeping into account batch size for example). Hope this helps
请注意,如果您使用numpy数组对象,这将有效,如果您想将其用于keras张量,则需要对其进行修改(例如,考虑批量大小)。希望这可以帮助
EDIT:
编辑:
This should work in keras:
这应该在keras中工作:
import keras.backend as K
def set_max_to_one(x, temperature=0.01):
x = K.log(x)/temperature
return K.round(K.softmax(x))
Instead of backend.softmax()
you could use layers.core.Activation()
if you wanted to set the axis
value.
如果要设置轴值,可以使用layers.core.Activation()而不是backend.softmax()。
Note that the output is still a tensor of float, not of int, but I can't find out how to change the tensor type. It shouldn't make much difference.
请注意,输出仍然是float的张量,而不是int,但我无法找到如何更改张量类型。它不应该有太大的区别。