如果我的最后一个Keras层X输出一个看起来像的张量,例如像这样:
{
{4, [22, 16, 11, 33], 24},
{4, [12, 33, 87, 17], 14},
{3, [92, 407, 25, 27], 34}
}
如何再添加一个图层Y来像这样转换X的输出:
{
{4, "22|16|11|33", 24},
{4, "12|33|87|17", 14},
{3, "92|407|25|27", 34}
}
换句话说,我需要将第二维转换为字符串,并将所有值连接在一起。
答案 0 :(得分:0)
我不确定Keras是否有解决此特定问题的方法。可能的解决方案(如果可以使用列表)是以下实现:
# Imagine you have a output layer like this:
a = [
[4, [22, 16, 11, 33], 24],
[4, [12, 33, 87, 17], 14],
[3, [92, 407, 25, 27], 34]
]
# "transpose" the list and get the second element
column1 = list(zip(*a))[1]
# Join all elements with "|"
newColumn1 = ['|'.join(str(e) for e in row) for row in column1]
# Put back the modified column
b = list(zip(*a))
b[1] = newColumn1
# "Untranspose" your list
a2 = list(zip(*b))
a2
您的输出将是:
[(4, '22|16|11|33', 24), (4, '12|33|87|17', 14), (3, '92|407|25|27', 34)]