1
$\begingroup$

I have implemented a custom loss function. While training the model, I want this loss function to be calculated per batch.

def calculate_additional_loss(inputs,outputs):
  # In order to be able to sum up the additional loss with other losses, it 
    #has to be consistent regarding the other losses
  #Idea: do something similar to additional_loss = 
    mse(pixel_inputs,pixel_outputs)
    x_test_normalized = tf.round(original_dim * inputs)
    x_decoded_normalized = tf.round(original_dim* outputs)
    error = tf.constant(0, dtype= tf.float32)
    additional_loss= tf.constant(0, dtype= tf.float32)
    for i in range(batch_size):
        #add padding
        reshaped_elem_1 = K.reshape(x_test_normalized[i], [DIM,DIM])

        a = K.reshape(reshaped_elem_1[:,DIM-1], [DIM,1])
        b = K.reshape(reshaped_elem_1[:,1], [DIM,1])

        reshaped_elem_1 = tf.concat ([b,reshaped_elem_1], axis= 1)
        reshaped_elem_1 = tf.concat ([reshaped_elem_1,a], axis= 1)        
        c= K.reshape(reshaped_elem_1[DIM-1,:], [1,DIM+2])
        d= K.reshape(reshaped_elem_1[1,:], [1,DIM+2])
        reshaped_elem_1 = tf.concat ([d,reshaped_elem_1],axis=0)
        reshaped_elem_1 = tf.concat ([reshaped_elem_1,c],axis=0)
    for (i,j) in range(reshaped_elem_1.shape[0],reshaped_elem_1.shape[1]):
        error = tf.add(error, tf.pow((reshaped_elem_1[i,j]- 
                reshaped_elem_1[i,j+1]),-2), tf.pow((reshaped_elem_1[i,j]- 
                reshaped_elem_1[i,j-1]),-2), tf.pow((reshaped_elem_1[i,j]- 
                reshaped_elem_1[i-1,j]),-2), tf.pow((reshaped_elem_1[i,j]- 
                reshaped_elem_1[i+1,j]),-2))
       additional_loss = tf.add(additional_loss, tf.divide(error, 
           original_dim))
    final_loss = tf.divide(additional_loss, batch_size)
    return final_loss

The call to this function before training is :

sess = tf.Session()
K.set_session(sess)
K.set_learning_phase(0)
models = (encoder, decoder)
additional_loss = 0*mse(inputs,outputs)
additional_loss += calculate_additional_loss(inputs,outputs)
vae_loss = K.mean(boxes_loss)
#vae_loss = K.mean(reconstruction_loss + kl_loss + additional_loss)
vae.add_loss(vae_loss)
vae.compile(optimizer='adam', metrics= ['acc', accuracy_2])
vae.summary()

plot_model(vae,

       to_file='vae_mlp.png',

       show_shapes=True)
model = vae.fit(x_train, epochs=epochs, batch_size=batch_size, 
 validation_data=(x_test, None), verbose = 1, callbacks=[CustomMetrics()])

The problem is that I don't understand why this loss function is outputting zero when the model is training. Is there a problem is my function. I tried so hard to write it with keras or tensorflow operations/symboles, but keras doesn't have a lot of available functions.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.