如何使用mxnet符号执行线性代数函数以编写自定义损失函数(例如,在线三重态挖掘)?)

时间:2018-12-31 11:39:50

标签: mxnet

我在张量流中提到了this的实现。它需要输出批处理嵌入的形状,但是我无法获得mxnet符号的实际形状。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用infer_shape()获得符号的形状。 要在MXNet中实现三重丢失,您可能需要检查以下线程:https://github.com/apache/incubator-mxnet/issues/6909因此,您可以通过以下方式实现它:

kernels = [(1, feature_size), (2, feature_size), (3, feature_size)]
for i in range(len(kernels)):
    conv_weight.append(mx.sym.Variable('conv' + str(i) + '_weight'))
    conv_bias.append(mx.sym.Variable('conv' + str(i) + '_bias'))

fa = get_conv(data=anchor,
              kernels=kernels, conv_weight=conv_weight, conv_bias=conv_bias,
              entity_weight=entity_weight, entity_bias=entity_bias,
              feature_name='fa')  # share weight.
fs = get_conv(data=same,
              kernels=kernels, conv_weight=conv_weight, conv_bias=conv_bias,
              entity_weight=entity_weight, entity_bias=entity_bias,
              feature_name='fs')
fd = get_conv(data=diff, 
              kernels=kernels, conv_weight=conv_weight, conv_bias=conv_bias,
              entity_weight=entity_weight, entity_bias=entity_bias,
              feature_name='fd') 

"""
triple-loss
"""
fs = fa - fs
fd = fa - fd
fs = fs * fs
fd = fd * fd
fs = mx.sym.sum(fs, axis=1, keepdims=1)
fd = mx.sym.sum(fd, axis=1, keepdims=1)
loss = fd - fs
loss = one - loss  # a scalar
loss = mx.sym.Activation(data=loss, act_type='relu')  # acts like a norm.
triple_loss = mx.sym.MakeLoss(loss)