Keras,如何从距离矩阵中构建包含每对的自定义损耗t? 问题定义

Keras,如何从距离矩阵中构建包含每对的自定义损耗t? 问题定义,keras,keras-layer,Keras,Keras Layer,我有一个距离矩阵M。这个距离矩阵M定义s.t [M]_i_j = D(batch_i,batch_j) where D is some distance function. 因此M是batchsize X batchsize 现在,对于我的自定义损耗,我希望能够计算矩阵中几乎每两对的总和。为此,我有另一个矩阵S,该矩阵定义为 [S]_i_j = 1 <=> batch_i and batch_j are **not** from the same class. [S]_i_j =

我有一个距离矩阵M。这个距离
矩阵M
定义s.t

[M]_i_j = D(batch_i,batch_j) where D is some distance function.
因此M是
batchsize X batchsize

现在,对于我的
自定义损耗
,我希望能够计算矩阵中几乎每两对的总和。为此,我有另一个
矩阵S
,该矩阵定义为

[S]_i_j = 1 <=> batch_i and batch_j are **not** from the same class.
[S]_i_j = 0 <=> batch_i and batch_j are from the same class. 

Now I want to sum  `Max{0,[M]_i_s+[M]_i_n}`,
For all pairs s.t  [M]_i_s is cell s.t [S]_i_j = 0 and [M]_i_n is a cell s.t [S]_i_n =1 
[S]\u i\u j=1批次\u i和批次\u j**不**来自同一类。
[S] _i_j=0批次_i和批次_j来自同一类。
现在我想求和`Max{0,[M]\u I\u s+[M]\u I\u n}`,
对于所有对,s.t[M]\u i_s是单元s.t[s]\u i_j=0,[M]\u i_n是单元s.t[s]\u i_n=1
我唯一的选择是 迭代M,并考虑在做这件事的时候。但是在Keras中,我不能迭代张量
  • 创建一个大小为(batchsize)^2x(可能的对数)的矩阵P,然后将矩阵M展平为向量并计算(展平(M))*P
  • 第一个我做不到。第二个,我没有用
    张量
    操作成功创建这个矩阵。我想我可以预先计算这个矩阵P,并将其设置为预测向量。但是
    Keras
    强制
    预测
    向量与
    批次
    大小相同

    Keras
    中,有没有关于如何执行1或2的想法,或者其他关于如何解决此问题的想法

    谢谢