title: Understanding WeightedKappaLoss using Keras
body: <p>I'm using Keras to try to predict a vector of scores (0-1) using a sequence of events.</p>
  <p>For example, <strong>X</strong> is a sequence of 3 vectors comprised of 6 features each, while <strong>y</strong> is a vector of 3 scores:</p>
  <pre><code>X
  [
    [1,2,3,4,5,6], &lt;--- dummy data
    [1,2,3,4,5,6],
    [1,2,3,4,5,6]
  ]
  
  y
  [0.34 ,0.12 ,0.46] &lt;--- dummy data
  </code></pre>
  <p>I want to adress the problem as ordinal classification, so if the actual values are <code>[0.5,0.5,0.5]</code> the prediction <code>[0.49,0.49,0.49]</code> is better then <code>[0.3,0.3,0.3]</code>. My Original solution, was to use <code>sigmoid</code> activation on my last layer and <code>mse</code> as the loss function, so the output is ranged between 0-1 for each of the output neurons:</p>
  <pre><code>def get_model(num_samples, num_features, output_size):
      opt = Adam()
      model = Sequential()
      
      model.add(LSTM(config['lstm_neurons'], activation=config['lstm_activation'], input_shape=(num_samples, num_features)))
      model.add(Dropout(config['dropout_rate']))
  
      for layer in config['dense_layers']:
        model.add(Dense(layer['neurons'], activation=layer['activation']))
  
      model.add(Dense(output_size, activation='sigmoid'))
      model.compile(loss='mse', optimizer=opt, metrics=['mae', 'mse'])
  
      return model
  </code></pre>
  <p>My Goal is to understand the usage of <strong>WeightedKappaLoss</strong> and to implement it on my actual data. I've created <strong><a href="https://colab.research.google.com/drive/1dJLzdqWLBJDvnExTAv1Ty1kk9ccReluR?usp=sharing" rel="noreferrer">this Colab</a></strong> to fiddle around with the idea.  In the Colab, my data is a sequence shaped <code>(5000,3,3)</code> and my targets shape is <code>(5000, 4)</code> representing 1 of 4 possible classes.</p>
  <p>I want the model to understand that it needs to trim the floating point of the X in order to predict the right y class:</p>
  <pre><code>[[3.49877793, 3.65873511, 3.20218196],
   [3.20258153, 3.7578669 , 3.83365481],
   [3.9579924 , 3.41765455, 3.89652426]], ----&gt; y is 3 [0,0,1,0]
  
  [[1.74290875, 1.41573056, 1.31195701],
   [1.89952004, 1.95459796, 1.93148095],
   [1.18668981, 1.98982041, 1.89025326]], ----&gt; y is 1 [1,0,0,0]
  </code></pre>
  <p>New model code:</p>
  <pre><code>def get_model(num_samples, num_features, output_size):
      opt = Adam(learning_rate=config['learning_rate'])
      model = Sequential()
      
      model.add(LSTM(config['lstm_neurons'], activation=config['lstm_activation'], input_shape=(num_samples, num_features)))
      model.add(Dropout(config['dropout_rate']))
  
      for layer in config['dense_layers']:
        model.add(Dense(layer['neurons'], activation=layer['activation']))
  
      model.add(Dense(output_size, activation='softmax'))
      model.compile(loss=tfa.losses.WeightedKappaLoss(num_classes=4), optimizer=opt, metrics=[tfa.metrics.CohenKappa(num_classes=4)])
  
      return model
  </code></pre>
  <p>When fitting the model I can see the following metrics on TensorBoard:
  <a href="https://i.stack.imgur.com/2LslK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/2LslK.png" alt="enter image description here" /></a><a href="https://i.stack.imgur.com/zUCMN.png" rel="noreferrer"><img src="https://i.stack.imgur.com/zUCMN.png" alt="enter image description here" /></a></p>
  <p>I'm not sure about the following points and would appreciate clarification:</p>
  <ul>
  <li>Am I using it right?</li>
  <li>In my original problem, I'm predicting 3 scores, as opposed of the Colab example, where I'm predicting only 1. If I'm using WeightedKappaLoss, does it mean I'll need to convert each of the scores to a vector of 100 one-hot encoding?</li>
  <li>Is there a way to use the WeightedKappaLoss on the original floating point scores without converting to a classification problem?</li>
  </ul>