简体   繁体   中英

Custom keras loss function which contains a function of the outputs

I'm working on a specific problem and I need to implement a very particular loss function. Here some explanation : assuming that I have some snapshots called , each column of corresponds to a snapshot. And some modes called . Each column of corresponds to a mode.

My neural network takes some inputs and gives N outputs say .

The custom loss function I want to implement in Keras is the following :

Such that , and

Finally, the problem can be seen as

  1. For each column of , ie each snapshot, some sensors are also measured, ie

  2. with a measure sensor , rebuild a snapshot as

  3. the goal of the algorithm, is to find the best neural network such that is close to


Actually I've got something like :

def customLoss(modes,snap):
    def diff(y_true,y_pred):
        predField=modes[:,0]*y_pred[...,0]
        for ii in range(1,modes.shape[1]):
            predField+=modes[:,ii]*y_pred[...,ii]

        realData=#I don t know how can I extract from snap the correct columns

        return K.sum(K.square(predField-realData))

    return diff

Some dummies datas look like :

m=100
n=10000
N=30
ns=8

snap=np.random.rand((m,n))
modes=np.random.rand((m,N))
x=np.random.rand((ns,n))

Do you have any ideas how can I implement this?

Thanks
Charles

Hope i got your dimensions right -

def customLoss(modes, snap):
    modesT = K.transpose(modes) #We want to switch row-columns
    snapT = K.transpose(snap) # As y_pred is Nx1.

    def diff(y_true, y_pred):
        predFields = modesT * y_pred #This will result in Nxm, where each row is g_i(x_i)phi_i(x_i)
        return K.sum(K.square(snapT - predFields))
    return diff

As prediction return in shape (Batchsize, features) , and Keras plays well with rows for sample, and not columns, you need to swap the dimensions of both mean and snap .
After doing that, and executing the predFields line in the function, you will have the following:

  • snapT : of shape (N,m)
  • predFields :of shape (N,m) , where each row i corresponds to g_i(x)phi_i(x) .

Since both matrices are of the same shape, its simple subtracting between them, and each row from snapT will be substracted by the correct row from predFields .

Note that I have made the assumption that each column i from snap "belongs" to sample i from the prediction.
Here is some toy example, where i tested this setting:

from keras import backend as K
import tensorflow as tf
import numpy as np

def customLoss(modes, snap):
    modesT = K.transpose(modes) #We want to switch row-columns
    snapT = K.transpose(snap) # As y_pred is Nx1.

    def diff(y_true, y_pred):
        predFields = modesT * y_pred #This will result in Nxm, where each row is g_i(x_i)phi_i(x_i)
        return K.sum(K.square(snapT - predFields))
    return diff

m = 3
N = 9

snapshot = np.random.randint(0,100, size =(m,N))
modes = np.random.randint(1,10, size = (m,N))

ytrue = np.arange(0,N).reshape((-1,1))
ypred = np.arange(0,N).reshape((-1,1))

loss = customLoss(modes,snapshot)

with tf.Session() as sess:
    x = loss(ytrue,ypred)
    print (sess.run(x))

If i still haven't got what you wanted right, some sample input and output could help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM