Search code examples
pythontensorflowkerasneural-networkloss-function

Regularization Term in Loss Function doing mathematical operations and assigning values


I think I'm still struggling understanding the Tensorflows framework and how to manipulate it in a manner as with normal arrays and numpy.

With the loss i have the prediction and true value: y_pred and y_true and i want to iterate over the y_pred value and assign a value 1 or 0 to it, for the loss function according to some smaller, <, or larger, >, condition.

The Batch size is 1000 and therefore the input shape is (1000,16,16,1). I think the code will clarify what I want to do, I tried to simplify it as much as possible.

def regularization_term(y_true, y_pred):    
    test = y_pred

    for i in tf.range(len(y_pred)):
        for y in tf.range(len(y_pred[i])):
            for x in tf.range(len(y_pred[i][y])): 
                if(random.random()>y_pred[i][y][x][0]):
                    test[i][y][x][0] = 0
                else:
                    test[i][y][x][0] = 1

    return test * y_true

So I need a method to assign a value to the tensor 'test' and a way to ask this larger/smaller if condition from the y_pred. How could that be possible?

Thank you very much!!


Solution

  • You can simply use tf.keras.backend.switch

    According to what you reported, your regularization function is:

    def regularization(y_true, y_pred):
        
        zeros = tf.zeros_like(y_pred)
        random = tf.random.uniform(tf.shape(y_pred), minval=0, maxval=1)
    
        reg = tf.keras.backend.switch(random > y_pred, zeros, y_true)
        
        return reg
    

    simple test:

    y_true = tf.random.uniform(shape=(100,16,16,1), minval=0, maxval=1)
    y_pred = tf.random.uniform(shape=(100,16,16,1), minval=0, maxval=1)
    
    regularization(y_true, y_pred)