如何在deeplearning4j中实现二阶分解层?

时间:2019-10-25 14:05:13

标签: python scala keras deep-learning deeplearning4j

我是deeplearning4j的新手,我正在尝试在deeplearning4j中实现二阶分解。我正在使用计算图来实现以下从python到scala的keras函数。 cat_2d是形状为(None,k)的输出张量的列表 其中k是嵌入矢量维。我将它们串联为embed_2d并实现了二阶分解。但是,我不确定如何在scala的deeplearning4j中复制相同的内容。请帮忙。

附加等效的python代码。

def fm:

    embed_2d = Concatenate(axis=1, name = 'concat_embed_2d')(cat_2d)
    tensor_sum = Lambda(lambda x: K.sum(x, axis = 1), name = 'sum_of_tensors')
    tensor_square = Lambda(lambda x: multiply([x,x]), name = 'square_of_tensors')

    sum_of_embed = tensor_sum(embed_2d)
    square_of_embed = tensor_square(embed_2d)

    square_of_sum = Multiply()([sum_of_embed, sum_of_embed])
    sum_of_square = tensor_sum(square_of_embed)

    sub = Subtract()([square_of_sum, sum_of_square])
    sub = Lambda(lambda x: x*0.5)(sub)

    fm_2d = Reshape((1,), name = 'fm_2d_output')(tensor_sum(sub))

    return fm_2d, embed_2d

0 个答案:

没有答案