How to implement Weighted Binary CrossEntropy on theano?
<p><strong>How to implement Weighted Binary CrossEntropy on theano?</strong></p>

<p>My Convolutional neural network only predict 0 ~~ 1 (sigmoid).</p>

<p><strong>I want to penalize my predictions in this way :</strong></p>

<p><a href="http://i.stack.imgur.com/m3rAL.png"><img src="http://i.stack.imgur.com/m3rAL.png" alt="Cost-Table"></a></p>

<p>Basically, i want to penalize MORE when the model predicts 0 but the truth was 1.</p>

<p><strong>Question :</strong> How can I create this <em>Weighted Binary CrossEntropy</em> function using theano and lasagne ?</p>

<p><strong>I tried this below</strong> </p>

<pre><code>prediction = lasagne.layers.get_output(model)


import theano.tensor as T
def weighted_crossentropy(predictions, targets):

    # Copy the tensor
    tgt = targets.copy("tgt")

    # Make it a vector
    # tgt = tgt.flatten()
    # tgt = tgt.reshape(3000)
    # tgt = tgt.dimshuffle(1,0)

    newshape = (T.shape(tgt)[0])
    tgt = T.reshape(tgt, newshape)

   #Process it so [index] &lt; 0.5 = 0 , and [index] &gt;= 0.5 = 1


    # Make it an integer.
    tgt = T.cast(tgt, 'int32')


    weights_per_label = theano.shared(lasagne.utils.floatX([0.2, 0.4]))

    weights = weights_per_label[tgt]  # returns a targets-shaped weight matrix
    loss = lasagne.objectives.aggregate(T.nnet.binary_crossentropy(predictions, tgt), weights=weights)

    return loss

loss_or_grads = weighted_crossentropy(prediction, self.target_var)
</code></pre>

<p><strong>But I get this error below :</strong></p>

<p><strong>TypeError: New shape in reshape must be a vector or a list/tuple of scalar. Got Subtensor{int64}.0 after conversion to a vector.</strong></p>

<hr>

<p>Reference : <a href="https://github.com/fchollet/keras/issues/2115">https://github.com/fchollet/keras/issues/2115</a></p>

<p>Reference : <a href="https://groups.google.com/forum/#!topic/theano-users/R_Q4uG9BXp8">https://groups.google.com/forum/#!topic/theano-users/R_Q4uG9BXp8</a></p>

<p>To address your syntax error:</p>

<p>Change</p>

<pre><code>newshape = (T.shape(tgt)[0])
tgt = T.reshape(tgt, newshape)
</code></pre>

<p>to</p>

<pre><code>newshape = (T.shape(tgt)[0],)
tgt = T.reshape(tgt, newshape)
</code></pre>

<p><code>T.reshape</code> expects a tuple of axes, you didn't provide this, hence the error.</p>

<p>Before penalizing false-negatives (prediction 0, truth 1) make sure that this prediction error is not based on the statistics of your training data, as <a href="http://stackoverflow.com/questions/39412051/how-to-implement-weighted-binary-crossentropy-on-theano#comment66149857_39412051">@uyaseen suggested</a>.</p>

python theano keras lasagne cross-entropy