Ejemplo n.º 1
0
    def __init__(self,
                 outputs,
                 activation=Activations,
                 input_shape=None,
                 weights=None,
                 bias=None,
                 **kwargs):

        if isinstance(outputs, int) and outputs > 0:
            self.outputs = outputs
        else:
            raise ValueError('Parameter "outputs" must be an integer and > 0')

        self.weights = weights
        self.bias = bias

        activation = _check_activation(self, activation)

        self.activation = activation.activate
        self.gradient = activation.gradient

        super(Connected_layer, self).__init__(input_shape=input_shape)

        if input_shape is not None:
            self._build()

        self.weights_update = None
        self.bias_update = None
        self.optimizer = None
Ejemplo n.º 2
0
  def __init__(self, activation=Activations, alpha=1., beta=1., **kwargs):

    '''
    Shortcut layer: activation of the linear combination of the output of two layers

                layer1 * alpha + layer2 * beta = output

    Now working only with same shapes input

    Parameters :
      activation   : activation function of the layer
      alpha        : float, default = 1., first weight of the combination
      beta         : float, default = 1., second weight of the combination

    '''

    activation = _check_activation(self, activation)

    self.activation = activation.activate
    self.gradient = activation.gradient

    self.alpha, self.beta = alpha, beta

    self.output, self.delta = (None, None)
    self._out_shape = None
    self.ix, self.jx, self.kx = (None, None, None)
    self.iy, self.jy, self.ky = (None, None, None)
Ejemplo n.º 3
0
    def __init__(self, input_shape=None, activation=Activations, **kwargs):

        activation = _check_activation(self, activation)

        self.activation = activation.activate
        self.gradient = activation.gradient

        super(Activation_layer, self).__init__(input_shape=input_shape)
Ejemplo n.º 4
0
    def __init__(self,
                 outputs,
                 steps,
                 activation=Activations,
                 input_shape=None,
                 weights=None,
                 recurrent_weights=None,
                 bias=None,
                 return_sequence=False):
        '''
    Recurrent Neural Network layer. Build a Recurrent fully connected architecture.

    Parameters
    ----------
    outputs : integer, number of outputs of the layer.
    steps   : integer, number of timesteps of the recurrency.
    activation : activation function object. activation function, applied to every hidden state
    input_shape : tuple of int, default None. Used for a single layer model.
      "None" is used when the layer is part of a network.
    weights : numpy array, default None. numpy array of weights
      of shapes (inputs, outputs). If None, the init is random.
    recurrent_weights : numpy array, default None. numpy array of weights
      of shapes (outputs, outputs). If None, the init is random.
    bias : numpy array, default None. shape (outputs, )
      If None, the init is zeros.
    '''

        if isinstance(outputs, int) and outputs > 0:
            self.outputs = outputs
        else:
            raise ValueError('Parameter "outputs" must be an integer and > 0')

        if isinstance(steps, int) and steps > 0:
            self.steps = steps
        else:
            raise ValueError('Parameter "steps" must be an integer and > 0')

        self.activation = _check_activation(layer=self,
                                            activation_func=activation)
        self.return_sequence = return_sequence

        self.weights = weights
        self.recurrent_weights = recurrent_weights
        self.bias = bias

        self.weights_update = None
        self.recurrent_weights_update = None
        self.bias_update = None
        self.states = None
        self.optimizer = None

        if input_shape is not None:
            super(SimpleRNN_layer, self).__init__(input_shape=input_shape)
            self._build()
Ejemplo n.º 5
0
  def __init__(self, filters, size, stride=None, input_shape=None,
               weights=None, bias=None,
               pad=False,
               activation=Activations,
               **kwargs):

    if isinstance(filters, int) and filters > 0:
      self.channels_out = filters
    else:
      raise ValueError('Parameter "filters" must be an integer and > 0')

    self.size = size
    if not hasattr(self.size, '__iter__'):
      self.size = (int(self.size), int(self.size))

    if self.size[0] <= 0. or self.size[1] <= 0.:
      raise LayerError('Convolutional layer. Incompatible size values. They must be both > 0')

    if not stride:
      self.stride = size
    else:
      self.stride = stride

    if not hasattr(self.stride, '__iter__'):
      self.stride = (int(self.stride), int(self.stride))

    if self.stride[0] <= 0. or self.stride[1] <= 0.:
      raise LayerError('Convolutional layer. Incompatible stride values. They must be both > 0')

    if len(self.size) != 2 or len(self.stride) != 2:
      raise LayerError('Convolutional layer. Incompatible stride/size dimensions. They must be a 1D-2D tuple of values')

    # Weights and bias
    self.weights = weights
    self.bias    = bias

    # Activation function
    activation = _check_activation(self, activation)

    self.activation = activation.activate
    self.gradient   = activation.gradient

    # Padding
    self.pad = pad
    self.pad_left, self.pad_right, self.pad_bottom, self.pad_top = (0, 0, 0, 0)

    # Output, Delta and Updates
    self.weights_update = None
    self.bias_update    = None
    self.optimizer      = None

    if input_shape is not None:
      super(Convolutional_layer, self).__init__(input_shape=input_shape)
      self._build()
Ejemplo n.º 6
0
    def __init__(self, activation=Activations, alpha=1., beta=1., **kwargs):

        activation = _check_activation(self, activation)

        self.activation = activation.activate
        self.gradient = activation.gradient

        self.alpha, self.beta = alpha, beta

        self.ix, self.jx, self.kx = (None, ) * 3
        self.iy, self.jy, self.ky = (None, ) * 3

        super(Shortcut_layer, self).__init__()
Ejemplo n.º 7
0
    def __init__(self, activation=Activations, **kwargs):
        '''
    Activation layer

    Parameters :
      activation : activation function of the layer
    '''

        activation = _check_activation(self, activation)

        self.activation = activation.activate
        self.gradient = activation.gradient

        self.output, self.delta = (None, None)
        self._out_shape = None
Ejemplo n.º 8
0
  def __init__(self, input_shape=None, activation=Activations, **kwargs):
    '''
    Activation layer

    Parameters
    ----------
      input_shape : tuple of 4 integers: input shape of the layer.
      activation : activation function of the layer.
    '''

    activation = _check_activation(self, activation)

    self.activation = activation.activate
    self.gradient = activation.gradient

    super(Activation_layer, self).__init__(input_shape=input_shape)
Ejemplo n.º 9
0
  def __init__(self, outputs, activation=Activations, input_shape=None, weights=None, bias=None, **kwargs):
    '''
    Connected layer

    Parameters
    ----------
      outputs     : integer, number of outputs of the layers
      activation  : activation function of the layer
      input_shape : tuple, default None. Shape of the input in the format (batch, w, h, c),
                    None is used when the layer is part of a Network model.
      weights     : array of shape (w * h * c, outputs), default is None. Weights of the dense layer.
                    If None, weights init is random.
      bias        : array of shape (outputs, ), default None. Bias of the connected layer.
                    If None, bias init is random
    '''

    if isinstance(outputs, int) and outputs > 0:
      self.outputs = outputs
    else :
      raise ValueError('Parameter "outputs" must be an integer and > 0')

    self.weights = weights
    self.bias = bias

    activation = _check_activation(self, activation)

    self.activation = activation.activate
    self.gradient   = activation.gradient

    super(Connected_layer, self).__init__(input_shape=input_shape)

    if input_shape is not None:
      self._build()

    self.weights_update = None
    self.bias_update    = None
    self.optimizer      = None
Ejemplo n.º 10
0
    def __init__(self,
                 input_shape,
                 filters,
                 size,
                 stride=None,
                 weights=None,
                 bias=None,
                 pad=False,
                 activation=Activations,
                 **kwargs):
        '''
    Convolution Layer: the output is the convolution of of the input batch
    with a group of kernel of shape size = (kx,ky) with step stride.

    Parameters:
      input_shape  : tuple, shape of the input batch of image (batch, w, h, channels_in)
      filters      : int, Number of filters to be slided over the input, and also
              the number of channels of the output
      size        : tuple of int, size of the kernel of shape (kx, ky)
      stride      : tuple of int, step of the kernel of shape (st1, st2)
      weights     : filters array, with shape (kx, ky, channels_in, channels_out)
      pad         : boolean, if False the image is cutted along the last raws and columns, if True
                    the input is padded following keras SAME padding
      activation  : activation function of the layer
    '''

        self.batch, self.w, self.h, self.c = input_shape
        self.channels_out = filters

        self.size = size
        if not hasattr(self.size, '__iter__'):
            self.size = (int(self.size), int(self.size))

        if not stride:
            self.stride = size
        else:
            self.stride = stride

        if not hasattr(self.stride, '__iter__'):
            self.stride = (int(self.stride), int(self.stride))

        assert len(self.size) == 2 and len(self.stride) == 2

        self.pad = pad
        self.pad_left, self.pad_right, self.pad_bottom, self.pad_top = (0, 0,
                                                                        0, 0)

        activation = _check_activation(self, activation)

        # Activation function
        self.activation = activation.activate
        self.gradient = activation.gradient

        self.delta, self.output = (None, None)

        # Weights and bias
        if weights is None:
            scale = np.sqrt(2 / (self.size[0] * self.size[1] * self.c))
            self.weights = np.random.normal(loc=scale,
                                            scale=1.,
                                            size=(self.size[0], self.size[1],
                                                  self.c, self.channels_out))
        else:
            self.weights = weights

        if bias is None:
            self.bias = np.zeros(shape=(self.channels_out, ), dtype=float)
        else:
            self.bias = bias

        # Updates
        self.weights_update = None
        self.bias_update = None
        self.optimizer = None
Ejemplo n.º 11
0
    def __init__(self,
                 filters,
                 size,
                 stride=None,
                 input_shape=None,
                 weights=None,
                 bias=None,
                 pad=False,
                 activation=Activations,
                 **kwargs):
        '''
    Convolution Layer: the output is the convolution of the input images
    with a group of kernel of shape size = (kx,ky) with step stride.

    Parameters
    ----------
      filters : integer. Number of filters to be slided over the input, and also
                the number of channels of the output (channels_out)
      size    : tuple of int, size of the kernel of shape (kx, ky).
      stride  : tuple of int, default None. Step of the kernel, with shape (st1, st2).
                If None, stride is assigned size values.
      input_shape : tuple, default None. Shape of the input in the format (batch, w, h, c),
                    None is used when the layer is part of a Network model.
      weights : numpy array, default None. filters of the convolutionanl layer,
                with shape (kx, ky, channels_in, filters). If None, random weights are initialized
      bias : numpy array, default None. Bias of the convolutional layer.
             If None, bias init is random with shape (filters, )
      pad  : boolean, default False. If False the image is cutted along the last raws and columns, if True
             the input is padded following keras SAME padding
      activation : activation function of the layer
    '''

        if isinstance(filters, int) and filters > 0:
            self.channels_out = filters
        else:
            raise ValueError('Parameter "filters" must be an integer and > 0')

        self.size = size
        if not hasattr(self.size, '__iter__'):
            self.size = (int(self.size), int(self.size))

        if self.size[0] <= 0. or self.size[1] <= 0.:
            raise LayerError(
                'Convolutional layer. Incompatible size values. They must be both > 0'
            )

        if not stride:
            self.stride = size
        else:
            self.stride = stride

        if not hasattr(self.stride, '__iter__'):
            self.stride = (int(self.stride), int(self.stride))

        if self.stride[0] <= 0. or self.stride[1] <= 0.:
            raise LayerError(
                'Convolutional layer. Incompatible stride values. They must be both > 0'
            )

        if len(self.size) != 2 or len(self.stride) != 2:
            raise LayerError(
                'Convolutional layer. Incompatible stride/size dimensions. They must be a 1D-2D tuple of values'
            )

        # Weights and bias
        self.weights = weights
        self.bias = bias

        # Activation function
        activation = _check_activation(self, activation)

        self.activation = activation.activate
        self.gradient = activation.gradient

        # Padding
        self.pad = pad
        self.pad_left, self.pad_right, self.pad_bottom, self.pad_top = (0, 0,
                                                                        0, 0)

        # Output, Delta and Updates
        self.weights_update = None
        self.bias_update = None
        self.optimizer = None

        if input_shape is not None:
            super(Convolutional_layer, self).__init__(input_shape=input_shape)
            self._build()
Ejemplo n.º 12
0
  def __init__(self, outputs, steps, activation=Activations, input_shape=None, weights=None, bias=None, **kwargs):
    '''
    RNN layer

    Parameters
    ----------
      outputs     : integer, number of outputs of the layers
      steps       : integer, number of mini-batch/steps to perform.
      activation  : activation function of the layer
      input_shape : tuple, default None. Shape of the input in the format (batch, w, h, c),
                    None is used when the layer is part of a Network model.
      weights     : array of shape (w * h * c, outputs), default is None. Weights of the dense layer.
                    If None, weights init is random.
      bias        : array of shape (outputs, ), default None. Bias of the connected layer.
                    If None, bias init is random
    '''

    if isinstance(outputs, int) and outputs > 0:
      self.outputs = outputs
    else :
      raise ValueError('Parameter "outputs" must be an integer and > 0')

    if isinstance(steps, int) and steps > 0:
      self.steps = steps
    else :
      raise ValueError('Parameter "steps" must be an integer and > 0')

    if weights is None:
      weights = (None, None, None)
    else:
      if np.shape(weights)[0] != 3:
        raise ValueError('Wrong number of init "weights". There are 3 connected layers into the RNN cell.')

    if bias is None:
      bias = (None, None, None)
    else:
      if np.shape(bias)[0] != 3:
        raise ValueError('Wrong number of init "biases". There are 3 connected layers into the RNN cell.')

    self.activation = _check_activation(self, activation)

    # if input shape is passed, init of weights, else done in  __call__
    if input_shape is not None:
      b, w, h, c = input_shape

      if b < self.steps:
        class_name = self.__class__.__name__
        prev_name  = layer.__class__.__name__
        raise LayerError('Incorrect steps found. Layer {} cannot be connected to the previous {} layer.'.format(class_name, prev_name))

      self.batch = b // self.steps
      self.input_shape = (self.batch, w, h, c)
      indices = np.arange(0, b)
      self.batches = np.lib.stride_tricks.as_strided(indices, shape=(self.steps, self.batch), strides=(self.batch * 8, 8)).copy()

      self.input_layer  = Connected_layer(self.outputs, self.activation, input_shape=(self.batches[-1][-1] + 1, w, h, c), weights=weights[0], bias=bias[0])
      self.self_layer   = Connected_layer(self.outputs, self.activation, weights=weights[1], bias=bias[1])(self.input_layer)
      self.output_layer = Connected_layer(self.outputs, self.activation, weights=weights[2], bias=bias[2])(self.self_layer)

      self.state = np.zeros(shape=(self.batch, w, h, self.outputs), dtype=float)

    else:
      self.batch = None
      self.input_shape = None
      self.batches = None

      self.input_layer = None
      self.self_layer = None
      self.output_layer = None
      self.state = None

    self.prev_state = None
    self.output, self.delta = (None, None)