0

I've been working on converting a CNN written in Lasagne to Keras (link to the Lasagne version: https://github.com/MTG/DeepConvSep/blob/master/examples/dsd100/trainCNN.py) I believe I've got most of it down, however, the loss function is the part I'm struggling to rewrite.

The output of the network has shape (32,513,30,4) and the loss function uses different layers (the last dim). I've tried to rewrite it as a custom loss function that I could just plug in into the model.compile() Here's the code I've written:


rand_num = np.random.uniform(size=(32,513,30,1))
epsilon=1e-8                                        
alpha=0.001                                        
beta=0.01                                          
beta_voc=0.03 

def loss_func(y_true, y_pred):

    global alpha, beta, beta_voc, rand_num

    voc =  y_pred[:,:,:,0:1] + epsilon * rand_num
    bass = y_pred[:,:,:,1:2] + epsilon * rand_num
    dru = y_pred[:,:,:,2:3] + epsilon * rand_num
    oth = y_pred[:,:,:,3:4] + epsilon * rand_num

    mask_vox = voc/(voc+bass+dru+oth)
    mask_bass = bass/(voc+bass+dru+oth)
    mask_drums = dru/(voc+bass+dru+oth)
    mask_oth = oth/(voc+bass+dru+oth)

    vocals = mask_vox * inp
    bass = mask_bass * inp
    drums = mask_drums * inp
    other = mask_oth * inp

    train_loss_vocals = mean_squared_error(y_true=y_true[:,:,:,0:1],y_pred=vocals)
    alpha_component = alpha*mean_squared_error(y_true=y_true[:,:,:,1:2],y_pred=vocals)
    alpha_component += alpha*mean_squared_error(y_true=y_true[:,:,:,2:3],y_pred=vocals)
    train_loss_recon_neg_voc = beta_voc*mean_squared_error(y_true=y_true[:,:,:,3:4],y_pred=vocals)

    train_loss_bass = mean_squared_error(y_true=y_true[:,:,:,1:2],y_pred=bass)
    alpha_component += alpha*mean_squared_error(y_true=y_true[:,:,:,0:1],y_pred=bass)
    alpha_component += alpha*mean_squared_error(y_true=y_true[:,:,:,2:3],y_pred=bass)
    train_loss_recon_neg = beta*mean_squared_error(y_true=y_true[:,:,:,3:4],y_pred=bass)

    train_loss_drums = mean_squared_error(y_true=y_true[:,:,:,2:3],y_pred=drums)
    alpha_component += alpha*mean_squared_error(y_true=y_true[:,:,:,0:1],y_pred=drums)
    alpha_component += alpha*mean_squared_error(y_true=y_true[:,:,:,1:2],y_pred=drums)
    train_loss_recon_neg += beta*mean_squared_error(y_true=y_true[:,:,:,3:4],y_pred=drums)

    vocals_error=train_loss_vocals.sum()
    drums_error=train_loss_drums.sum()
    bass_error=train_loss_bass.sum()
    negative_error=train_loss_recon_neg.sum()
    negative_error_voc=train_loss_recon_neg_voc.sum()
    alpha_component=alpha_component.sum()

    loss=abs(vocals_error+drums_error+bass_error-negative_error-alpha_component-negative_error_voc)

    return loss 

The first error I get is:

AttributeError: 'Tensor' object has no attribute 'sum'

However, I am not sure whether some other operations, even when incorrect, would throw an error.

I would really appreciate some help. Thank you.

Jan Gryga
  • 25
  • 4

1 Answers1

0

See for instance this example on how to define a custom loss in Keras.

y_pred being the output of a Keras model is a tensor. You have to adapt your operations to work with tensors, so instead of something like np.sum you would have to use K.sum with K being your keras backend.

Some operators are overloaded, so you can for instance add two tensors using +.

lmz
  • 366
  • 3
  • 13