Keras - 获取2个热阵列的模式

问题描述 投票:0回答:1

我正在构建一个简单的模型,它接收2个热阵列(预测)并获得预测模式。

例如,如果我们有2个数组:[0,1]和[0,1]我们的模式应该是类[0,1]另一个例子:[1,0] [1,0] [0,1]应该是类[1,0]

到目前为止我有这个:

def mode(inputs):
    vals = [np.where(x==1)[0][0] for x in inputs]
    return max(set(vals), key=vals.count)


img_in = np.array([[0,1]])
txt_in = np.array([[1,0]])

img_input = Input((2,), name='image_input')
txt_input = Input((2,), name='text_input')
img_input = Reshape((2,))(img_input)
txt_input = Reshape((2,))(txt_input)

x = Lambda(mode)([img_input, txt_input])


model = Model(inputs=[img_input, txt_input], outputs=[x])

我怎样才能获得向量的模式?

编辑:

我可以这样做:

def mode(inputs):
    s = K.sum(inputs, axis=0)
    s = K.argmax(s)
    return s

但是我收到了一个错误

TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int64 of argument 'x'.
python keras tensor
1个回答
0
投票

这是解决方案:

def mode(inputs):
    s = K.sum(inputs, axis=0)
    s = K.one_hot(K.argmax(s), K.shape(s)[0])
    return s
© www.soinside.com 2019 - 2024. All rights reserved.