Exemplo n.º 1
0
    def activate(self, inputs):
        """Return the model outputs for given inputs."""
        # Calculate similarity between input and each stored input
        # (gaussian of each distance)
        similarities = calculate.gaussian(
            _distances(inputs, self._input_matrix), self._variance)
        # Then scale each stored target by corresponding similarity, and sum
        output_vec = _weighted_sum_rows(self._target_matrix, similarities)

        if self._scale_by_similarity:
            output_vec /= numpy.sum(similarities)

        if self._scale_by_class:
            # Scale output by number of classes (sum of targets)
            # This minimizes the effect of unbalanced classes
            # Return 0 when target total is 0
            output_vec[:] = calculate.protvecdiv(output_vec,
                                                 self._target_totals)

        # Convert output to probabilities, and return
        output_vec /= sum(output_vec)
        return output_vec
Exemplo n.º 2
0
def test_protvecdiv_zero_den():
    # Returns 0 for position with 0 denominator
    # Also for 0 / 0
    assert (calculate.protvecdiv(numpy.array([1.0, 2.0, 0.0]),
                                 numpy.array([2.0, 0.0, 0.0])) == numpy.array(
                                     [0.5, 0.0, 0.0])).all()
Exemplo n.º 3
0
def test_protvecdiv_no_zero():
    assert (calculate.protvecdiv(numpy.array([1.0, 2.0, 3.0]),
                                 numpy.array([2.0, 2.0, 2.0])) == numpy.array(
                                     [0.5, 1.0, 1.5])).all()