示例#1
0
    def __init__(self, dimensions, connections, **kwargs):
        r"""
        See ``MultilayerPerceptron`` for full documentation.

        """

        Classifier.__init__(self)

        self.dimensions = dimensions
        self.connections = list(connections)

        try:
            self.thresholds = kwargs['thresholds']
            self.has_thresholds = True
        except KeyError:
            self.thresholds = None
            self.has_thresholds = False

        try:
            self.activations = kwargs['activations']
        except KeyError:
            self.activations = HeavisideActivationFunction()

        MultilayerPerceptron.check_size(self.dimensions, self.connections,
            self.thresholds, self.activations)

        self.notify_observers()
示例#2
0
    def __init__(self, alpha, threshold, sample, **kwargs):
        r"""See ``SVMClassifier`` for full documentation.

        """

        Classifier.__init__(self)

        num_patterns = len(sample)
        check_svm_classification_sample(sample)
        self.dim = len(sample[0].pattern)

        if len(alpha) != num_patterns:
            raise ValueError('The supplied sample and multipliers vector do \
not have the same size')

        self.sv_indices = [i for i in range(len(alpha)) if alpha[i] != 0]
        
        self.support_vectors = [sample[i].pattern for i in self.sv_indices]
        self.signed_alphas = [alpha[i] * sample[i].label
            for i in self.sv_indices]
        self.threshold = threshold

        try:
            self.kernel = kwargs['kernel']
        except KeyError:
            self.kernel = Kernel.get_default()
示例#3
0
    def __init__(self, weights, **kwargs):
        r"""
        See :class:`Perceptron` for full documentation.

        """

        Classifier.__init__(self)
        self.__weights = self.__activation = None
        try:
            threshold = kwargs['threshold']
            self.has_threshold = True
        except KeyError:
            self.has_threshold = False
            threshold = (0,) * len(weights)

        Perceptron.check_weights_and_threshold(weights, threshold)
        self.set_weights_and_threshold(weights, threshold)

        try:
            self.activation = kwargs['activation']
            #activation function
        except KeyError:
            self.activation = HeavisideActivationFunction()
示例#4
0
    def plot(self, *args, **kwargs):
        r"""
        Returns a graphic containing the plot of the perceptron output. Raises
        a ValueError if invoked on perceptrons not having two or three input
        units. The graphic is either a bi- or three-dimensional plot according
        to the following invocation syntax:

        - ``plot(x_range, y_range)`` returns a 2D plot.

        - ``plot(x_range, y_range, z_range)`` returns a 3D plot.

        INPUT:

        - ``self`` -- ``Perceptron`` object on which the function is invoked.

        - ``x_range`` -- bidimensional iterable containing the range of x
          variable.

        - ``y_range`` -- bidimensional iterable containing the range of y
          variable.

        - ``z_range`` -- bidimensional iterable containing the range of z
          variable.

        - ``shading`` -- boolean (default value: False) flag triggering the
          perceptron output visualization in form of a color gradient.

        - ``shading_color`` -- colormap (default value: Greys) colormap to be
          used in order to show perceptron output.

        - ``x_points`` -- integer (default: 50 in 2D, 35 in 3D) number of
          samples in x range.

        - ``y_points`` -- integer (default: 50 in 2D, 35 in 3D) number of
          samples in y range.

        - ``z_points`` -- integer (default: 50 in 2D, 35 in 3D) number of
          samples in z range.

        - ``output`` -- integer (default: unused) output to be selected when
          drawing the decision function if the perceptron has several output
          units, not specified when the perceptron has one output unit.

        - ``base`` -- matplotlib figure (default: a new figure) figure to be
          used to draw the decision function.

        - ``contours``-- iterable of numeric values (default: empty tuple)
          perceptron output values to be highlighted through contours.

        - ``contour_color`` -- iterable of colors or single color value
          (default: 'gray') colors of the drawn contours; if a single value is
          supplied, it refers to all contours.

        - ``contour_width`` -- iterable of numberic values or single numeric
          value (default: 1) width of the drawn contours; if a single numeric
          value is supplied, it refers to all contours.

        - ``contour_style`` -- iterable or single value of a valid style
          (default: '-') style of the drawn contours; if a single value is
          supplied, it refers to all contours.

        - ``color_bar`` -- boolean (default: False) flag setting the
          visualization of a color legend.

        - ``plotter`` -- Plotter object (default: SagePlotter() when the code
          is run within sage and MatplotlibPlotter() otherwise) to be used in
          order to render graphics.

        OUTPUT:

        graphic object -- the perceptron output values plot in function of
        possible input values, in form of a matplotlib figure or of a sage
        graphic.

        EXAMPLES:

        The ``plot`` function generates a graphic object summarizing the
        multilayer perceptron outputs for a given range of possible inputs.
        For instance, the following Heaviside-activated perceptron computes
        the binary XOR function, as shown by visual inspection of the plot:

        ::

            >>> from yaplf.models.neural import MuyltilayerPerceptron
            >>> p = MultilayerPerceptron([2, 2, 1], [[(1, -1), (-1, 1)],
            ... [(1, 1)]], thresholds = [(-1, -1), (-1,)])
            >>> p.plot((-0.1, 1.1), (-0.1, 1.1), plot_points = 100,
            ... shading = True)

        Here the first two arguments represent the ranges for the possible
        values for the two perceptron input units, and the obtained graph
        contains two black zones corresponding to the set of inputs mapped to
        `0`, while the remaining black zone corresponds to the output `1`.
        Visualization of these zones is activated by the ``shading`` named
        argument, while ``plot_points`` refers to the precision to be used in
        order to draw the above mentioned zones.

        ::

            >>> from yaplf.utility.activation import SigmoidActivationFunction
            >>> p = MultilayerPerceptron([2, 2, 1], [[(1, -1), (-1, 1)],
            ... [(1, 1)]], thresholds = [(-1, -1), (-1,)],
            ... activations = SigmoidActivationFunction(beta = 2))
            >>> p.plot((-0.1, 1.1), (-0.1, 1.1), shading=True,
            ... contours = (0.2, ), contour_color = ('red',))

        Only perceptrons having two or three inputs allow invocation of the
        ``plot`` function. In the second case it will be necessary to specify
        three input value ranges, and the result will be a 3D graph:

        ::

            >>> p = MultilayerPerceptron([3, 2, 1],
            ... [[(1, -1, -.5), (-1, 1, 1.3)], [(1, 1)]],
            ... thresholds = [(-1, -1), (-1,)],
            ... activations = SigmoidActivationFunction(beta = 2))
            >>> p.plot((-0.1, 1.1), (-0.1, 1.1), (0, 1),
            ... contours=(0.1, 0.5, 0.9),
            ... contour_color=('red', 'green', 'blue'))

        AUTHORS:

        - Dario Malchiodi (2010-03-22)

        """

        if not 1 < self.ml_perceptron.dimensions[0] < 4:
            raise ValueError('plot only works for 2-inputs and 3-inputs \
                perceptrons.')

        if len(args) != self.ml_perceptron.dimensions[0]:
            raise ValueError('plot ranges incompatible with perceptron \
                inputs.')

        try:
            shading = kwargs['shading']
            del kwargs['shading']
        except KeyError:
            shading = False
        try:
            shading_color = kwargs['shading_color']
            del kwargs['shading_color']
        except KeyError:
            shading_color = Greys

        kwargs['gradient'] = shading
        kwargs['gradient_color'] = shading_color

        return Classifier.plot(self.ml_perceptron, *args, **kwargs)