0

Suppose I have a training data set shown X which is a 6000*51 matrix and the problem is a multi-output classification problem and the target matrix Y is 6000*10 and each column of the target matrix takes 0 or 1. I can define parameters based on the features as follows:

    n = 10
    p = X[:,:n]
    a = X[:,2:2*n]
    c = X[:,2*n]

Suppose the prediction of my model is Prediction. I want to define a loss function as follows:

-np.einsum('ij,ij ->i',p,y_test).mean() + 10 *   
np.mean( np.maximum(  np.einsum('ij,ij ->i',a,Prediction) - c, 0) )
def knapsack_loss(X, n, cvc=1):
    input_a  = X[:,n:2*n]
    input_a  = np.float64(deepcopy(input_a ))
    input_p   = X[:,:n]
    input_p   = np.float64(deepcopy(input_p))
    input_c = X[:,2*n]
    input_c = np.float64(deepcopy(input_c))
    
    def loss(y_true, y_pred):
        picks = y_pred
        
        return (-1 * K.batch_dot(picks, input_p, 1)) + cvc * K.maximum(
            K.batch_dot(picks, input_a, 1) - input_c, 0)
    return loss

def get_model(n_inputs, n_outputs):
    model = Sequential()
    model.add(Dense(100, input_dim=n_inputs, kernel_initializer='he_uniform', activation='relu'))
    model.add(Dense(n_outputs, activation='sigmoid'))
    model.compile(loss= knapsack_loss(X_train, n, cvc=1),optimizer='adam')
    return model

n_inputs, n_outputs = X.shape[1], Y.shape[1]

model = get_model(n_inputs, n_outputs)
model.fit(X_train, y_train, verbose=0, epochs=500)

When I run this code, I face the following error:

InvalidArgumentError: Incompatible shapes: [32] vs. [6000]
     [[{{node training_14/Adam/gradients/loss_22/dense_55_loss/loss/MatMul_1_grad/BroadcastGradientArgs}}]]

I would be thankful if someone can correct it or provide and synthetic example.

Innat
  • 16,113
  • 6
  • 53
  • 101
Amin
  • 127
  • 8
  • did u try your loss as a standalone function? the multiplications works? – Marco Cerliani Apr 22 '21 at 07:09
  • Unfortunately, I am new to Keras and I do not know how to do this. Could you please let me know how should I do it? – Amin Apr 22 '21 at 07:29
  • Maybe checkout this: https://stackoverflow.com/questions/45961428/make-a-custom-loss-function-in-keras – akocz Apr 22 '21 at 09:32
  • Thanks akocz. But my loss function is a bit different because I wanna use the original features and predictions in the calculation of the loss function. – Amin Apr 22 '21 at 18:53
  • Anyone can help me to define this loss function? – Amin Apr 23 '21 at 08:06

0 Answers0