def get_detection_channels_from_state(self, state, target):
        """
        Returns monitoring channels when using the layer to do detection
        of binary events.

        Parameters
        ----------
        state : theano.gof.Variable
            Output of `fprop`
        target : theano.gof.Variable
            The targets from the dataset

        Returns
        -------
        channels : OrderedDict
            Dictionary mapping channel names to Theano channel values.
        """

        rval = OrderedDict()
        y_hat = state > 0.5
        y = target > 0.5
        wrong_bit = T.cast(T.neq(y, y_hat), state.dtype)
        rval['01_loss'] = wrong_bit.mean()
        rval['kl'] = self.cost(Y_hat=state, Y=target)

        y = T.cast(y, state.dtype)
        y_hat = T.cast(y_hat, state.dtype)
        tp = (y * y_hat).sum()
        fp = ((1 - y) * y_hat).sum()

        precision = compute_precision(tp, fp)
        recall = compute_recall(y, tp)
        f1 = compute_f1(precision, recall)

        rval['precision'] = precision
        rval['recall'] = recall
        rval['f1'] = f1

        tp = (y * y_hat).sum(axis=0)
        fp = ((1 - y) * y_hat).sum(axis=0)

        precision = compute_precision(tp, fp)

        rval['per_output_precision_max'] = precision.max()
        rval['per_output_precision_mean'] = precision.mean()
        rval['per_output_precision_min'] = precision.min()

        recall = compute_recall(y, tp)

        rval['per_output_recall_max'] = recall.max()
        rval['per_output_recall_mean'] = recall.mean()
        rval['per_output_recall_min'] = recall.min()

        f1 = compute_f1(precision, recall)

        rval['per_output_f1_max'] = f1.max()
        rval['per_output_f1_mean'] = f1.mean()
        rval['per_output_f1_min'] = f1.min()

        return rval
    def get_detection_channels_from_state(self, state, target):
        """
        Returns monitoring channels when using the layer to do detection
        of binary events.

        Parameters
        ----------
        state : theano.gof.Variable
            Output of `fprop`
        target : theano.gof.Variable
            The targets from the dataset

        Returns
        -------
        channels : OrderedDict
            Dictionary mapping channel names to Theano channel values.
        """

        rval = OrderedDict()
        y_hat = state > 0.5
        y = target > 0.5
        wrong_bit = T.cast(T.neq(y, y_hat), state.dtype)
        rval['01_loss'] = wrong_bit.mean()
        rval['kl'] = self.cost(Y_hat=state, Y=target)

        y = T.cast(y, state.dtype)
        y_hat = T.cast(y_hat, state.dtype)
        tp = (y * y_hat).sum()
        fp = ((1-y) * y_hat).sum()

        precision = compute_precision(tp, fp)
        recall = compute_recall(y, tp)
        f1 = compute_f1(precision, recall)

        rval['precision'] = precision
        rval['recall'] = recall
        rval['f1'] = f1

        tp = (y * y_hat).sum(axis=0)
        fp = ((1-y) * y_hat).sum(axis=0)

        precision = compute_precision(tp, fp)

        rval['per_output_precision_max'] = precision.max()
        rval['per_output_precision_mean'] = precision.mean()
        rval['per_output_precision_min'] = precision.min()

        recall = compute_recall(y, tp)

        rval['per_output_recall_max'] = recall.max()
        rval['per_output_recall_mean'] = recall.mean()
        rval['per_output_recall_min'] = recall.min()

        f1 = compute_f1(precision, recall)

        rval['per_output_f1_max'] = f1.max()
        rval['per_output_f1_mean'] = f1.mean()
        rval['per_output_f1_min'] = f1.min()

        return rval
Exemple #3
0
  def get_layer_monitoring_channels(self,state_below=None,state=None,target=None):
    rval=OrderedDict()
    W,=self.transformer.get_params()
    rval['norm']=T.sqrt(T.sqr(W).sum())
    if(target is not None) and ((state_below is not None) or (state is not None)):
        if state is None:
            state=self.fprop(state_below)
        target=1.-target  #0/1 dissim/sim to 1/0 distances
        rmse=T.sqrt(T.mean(T.sqr(state-target)))
        rval['rmse']=rmse.mean()
        if self.costfn=='margin':
            thresh=self.costparam
        elif self.costfn=='cauchy':
            thresh=2./(1.+T.exp(self.costparam))
        else:
            thresh=0.5
        yhat=state<thresh
        y=target<0.5
        wrong_bit=T.cast(T.neq(y,yhat),state.dtype)
        rval['01_loss']=wrong_bit.mean()

        y=T.cast(y,state.dtype)
        yhat=T.cast(yhat,state.dtype)
        tp=(y*yhat).sum()
        fp=((1-y)*yhat).sum()
        prec=compute_precision(tp,fp)
        rec=compute_recall(y,tp)
        f1=compute_f1(prec,rec)
        rval['neg_precision']=-prec
        rval['neg_recall']=-rec
        rval['neg_f1']=-f1
        return rval
Exemple #4
0
def test_compute_recall():
    """
    Tests whether compute_recall function works as
    expected.
    """
    tp_pyval = 4
    ys_pyval = np.asarray([0, 1, 1, 0, 1, 1, 0])

    tp = sharedX(tp_pyval, name="tp")
    ys = sharedX(ys_pyval, name="ys_pyval")
    recall_py = tp_pyval / ys_pyval.sum()
    recall = compute_recall(ys, tp)
    assert np.allclose(recall.eval(), recall_py)
Exemple #5
0
def test_compute_recall():
    """
    Tests whether compute_recall function works as
    expected.
    """
    tp_pyval = 4
    ys_pyval = np.asarray([0, 1, 1, 0, 1, 1, 0])

    tp = sharedX(tp_pyval, name="tp")
    ys = sharedX(ys_pyval, name="ys_pyval")
    recall_py = tp_pyval / ys_pyval.sum()
    recall = compute_recall(ys, tp)
    assert np.allclose(recall.eval(),
                       recall_py)
Exemple #6
0
    def get_layer_monitoring_channels(self,
                                      state_below=None,
                                      state=None,
                                      target=None):
        rval = OrderedDict()
        W, = self.transformer.get_params()
        rval['norm'] = T.sqrt(T.sqr(W).sum())
        if (target is not None) and ((state_below is not None) or
                                     (state is not None)):
            if state is None:
                state = self.fprop(state_below)
            target = 1. - target  #0/1 dissim/sim to 1/0 distances
            rmse = T.sqrt(T.mean(T.sqr(state - target)))
            rval['rmse'] = rmse.mean()
            if self.costfn == 'margin':
                thresh = self.costparam
            elif self.costfn == 'cauchy':
                thresh = 2. / (1. + T.exp(self.costparam))
            else:
                thresh = 0.5
            yhat = state < thresh
            y = target < 0.5
            wrong_bit = T.cast(T.neq(y, yhat), state.dtype)
            rval['01_loss'] = wrong_bit.mean()

            y = T.cast(y, state.dtype)
            yhat = T.cast(yhat, state.dtype)
            tp = (y * yhat).sum()
            fp = ((1 - y) * yhat).sum()
            prec = compute_precision(tp, fp)
            rec = compute_recall(y, tp)
            f1 = compute_f1(prec, rec)
            rval['neg_precision'] = -prec
            rval['neg_recall'] = -rec
            rval['neg_f1'] = -f1
            return rval