title: keras: how to write customized loss function to aggregate over frame-level predictions to song-level prediction
body: <p>I am doing a song genre classification (2 classes). For each song, I have chopped them into small frames (5s) to generate MFCC as input features for a neural network and each frame has an associated song genre label.</p>
  
  <p>The data looks like the following:</p>
  
  <pre><code> name         label   feature
   ....
   song_i_frame1 label   feature_vector_frame1
   song_i_frame2 label   feature_vector_frame2
   ...
   song_i_framek label   feature_vector_framek
   ...
  </code></pre>
  
  <p>I know that I can randomly pick say 80% of songs (their small frames) as training data and the rest as testing. But now the way I write X_train is a frame at the frame level and biney cross-entropy loss function is defined at the frame level. I am wondering how I can customize the loss function such that it is minimized over the aggregation (e.g. majority vote of each frame prediction of the song) of frame level prediction. </p>
  
  <p>currently, what I have is:</p>
  
  <pre><code>model_19mfcc = Model(input_shape = (X_train19.shape[1], X_train19.shape[2]))
  model_19mfcc.compile(loss='binary_crossentropy', optimizer="RMSProp", metrics=["accuracy"])
  history_fit = model_19mfcc.fit(X_train19, y_train,validation_split=0.25, batch_size = 1800/50, epochs= 200)
  </code></pre>
  
  <p>Also, when I feed into the training and testing data into keras the corresponding ID (name) of the data is lost, is keeping the data (name, lebel, and feature) in a separate pandas dataframe and matching back the prediction from keras a good practice? or are there other good alternatives?</p>
  
  <p>Thanks in advance!</p>