Exemplo n.º 1
0
  def bias_term(self, inputs, n_features, activation):
    b = self.add_param(
      self.shared(
        value=numpy.zeros((n_features,), dtype='float32'),
        borrow=True,
        name="b_conv_" + self.name
      )
    )

    act = strtoact('identity') if activation == 'maxout' else strtoact(activation)
    output = act(inputs + b.dimshuffle("x", 0, "x", "x"))  # (time*batch, filter, out-row, out-col)
    output.name = "output_bias_term_"+self.name
    output = self.calculate_index(output)
    return output
Exemplo n.º 2
0
 def bias_term(self, inputs, n_features, activation):
     if self.base:
         b = self.add_param(self.base[0].b)
     else:
         b = self.add_param(
             self.shared(value=numpy.zeros((n_features, ), dtype='float32'),
                         borrow=True,
                         name="b_conv_" + self.name))
     self.b = b
     act = strtoact('identity') if activation == 'maxout' else strtoact(
         activation)
     output = act(inputs + b.dimshuffle(
         "x", 0, "x", "x"))  # (time*batch, filter, out-row, out-col)
     output.name = "output_bias_term_" + self.name
     output = self.calculate_index(output)
     return output
Exemplo n.º 3
0
  def __init__(self, n_features, filter, base = None, activation="tanh", **kwargs):
    if base is None:
      base = []
    kwargs['n_out'] = n_features
    super(ConvBaseLayer, self).__init__(**kwargs)
    assert len(self.sources) == 1
    self.source = self.sources[0]
    self.n_in = self.source.attrs['n_out']
    self.X = self.source.output
    assert self.X.ndim == 4
    self.n_features = n_features

    self.set_attr('n_features', n_features)
    self.set_attr('filter', filter)
    self.set_attr('activation', activation)
    self.set_attr('n_out', n_features if numpy.prod(filter) > 0 else self.n_in)

    #TODO: maybe this ordering is not consistent with Dewis implementation
    self.filter_height = filter[0]
    self.filter_width = filter[1]
    self.activation = strtoact(activation)
    if base:
      #self.W = self.add_param(base[0].W)
      #self.b = self.add_param(base[0].b)
      self.W = base[0].W
      self.b = base[0].b
    else:
      self.W = self.create_conv_weights(n_features, self.n_in, self.filter_height, self.filter_width)
      self.b = self.create_and_add_bias(n_features)
Exemplo n.º 4
0
  def __init__(self, n_features, filter, base = None, activation="tanh", **kwargs):
    if base is None:
      base = []
    kwargs['n_out'] = n_features
    super(ConvBaseLayer, self).__init__(**kwargs)
    assert len(self.sources) == 1
    self.source = self.sources[0]
    self.n_in = self.source.attrs['n_out']
    self.X = self.source.output
    assert self.X.ndim == 4
    self.n_features = n_features

    self.set_attr('n_features', n_features)
    self.set_attr('filter', filter)
    self.set_attr('activation', activation)
    self.set_attr('n_out', n_features if numpy.prod(filter) > 0 else self.n_in)

    #TODO: maybe this ordering is not consistent with Dewis implementation
    self.filter_height = filter[0]
    self.filter_width = filter[1]
    self.activation = strtoact(activation)
    if base:
      #self.W = self.add_param(base[0].W)
      #self.b = self.add_param(base[0].b)
      self.W = base[0].W
      self.b = base[0].b
    else:
      self.W = self.create_conv_weights(n_features, self.n_in, self.filter_height, self.filter_width)
      self.b = self.create_and_add_bias(n_features)
Exemplo n.º 5
0
    def __init__(self, **kwargs):
        super(NewConv, self).__init__(**kwargs)

        # our CRNN input is 3D tensor that consists of (time, batch, dim)
        # however, the convolution function only accept 4D tensor which is (batch size, stack size, nb row, nb col)
        # therefore, we should convert our input into 4D tensor
        inputs = self.sources[
            0].output  # (time, batch, input-dim = row * col * stack_size)
        time = inputs.shape[0]
        batch = inputs.shape[1]

        if self.status[0]:  # the previous layer is convolutional layer
            self.input = T.concatenate([s.Output for s in self.sources],
                                       axis=1)  # (batch, stack size, row, col)
        else:
            inputs2 = inputs.reshape(
                (time * batch, self.input_shape[0], self.input_shape[1],
                 self.filter_shape[1]))  # (time*batch, row, col, stack)
            self.input = inputs2.dimshuffle(0, 3, 1,
                                            2)  # (batch, stack_size, row, col)
        self.input.name = "conv_layer_input_final"

        if self.modes[3] != "tanh":
            act = strtoact(self.modes[3])
            self.modes[3] = "identity"

        self.Output = self.run_cnn(
            inputs=self.input,
            filter_shape=self.filter_shape,
            filter_dilation=self.filter_dilation,
            params=self.pool_params,
            modes=self.modes,
            others=self.other_params
        )  # (batch, nb feature maps, out-row, out-col)

        # our CRNN only accept 3D tensor (time, batch, dim)
        # so, we have to convert back the output to 3D tensor
        # self.make_output(self.Output2)
        if self.attrs['batch_norm']:
            self.Output = self.batch_norm(
                h=self.Output.reshape(
                    (self.Output.shape[0], self.Output.shape[1] *
                     self.Output.shape[2] * self.Output.shape[3])),
                dim=self.attrs['n_out'],
                force_sample=self.force_sample).reshape(self.Output.shape)
            if self.modes[3] != "tanh":
                self.Output = act(self.Output)

        if self.modes[3] == 'maxout':
            self.Output = T.max(self.Output, axis=1).dimshuffle(0, 'x', 1, 2)
            self.attrs['n_out'] /= self.attrs['n_features']
            self.attrs['n_features'] = 1

        output2 = self.Output.dimshuffle(
            0, 2, 3, 1)  # (batch, out-row, out-col, nb feature maps)
        self.output = output2.reshape(
            (time, batch, output2.shape[1] * output2.shape[2] *
             output2.shape[3]))  # (time, batch, out-dim)
Exemplo n.º 6
0
  def __init__(self, **kwargs):
    super(NewConv, self).__init__(**kwargs)

    # our CRNN input is 3D tensor that consists of (time, batch, dim)
    # however, the convolution function only accept 4D tensor which is (batch size, stack size, nb row, nb col)
    # therefore, we should convert our input into 4D tensor
    inputs = self.sources[0].output  # (time, batch, input-dim = row * col * stack_size)
    time = inputs.shape[0]
    batch = inputs.shape[1]

    if self.status[0]:  # the previous layer is convolutional layer
      self.input = T.concatenate([s.Output for s in self.sources], axis=1)  # (batch, stack size, row, col)
    else:
      # In case of spliced data, the last dim in inputs contains stacked frames (e.g. ASR).
      # Since Theano reshape will read _and_ write elements row-wise, we need to transpose the target matrix.
      # This is done by swapping target dimensions (reshape(.., input_shape[1], input_shape[0]) and subsequent
      # dimshuffle that puts row and col dim where Theano expects them.
      inputs2 = inputs.reshape((time * batch, self.input_shape[1],
                                self.input_shape[0], self.filter_shape[1]))  # (time*batch, row, col, stack)
      self.input = inputs2.dimshuffle(0, 3, 2, 1)  # (batch, stack_size, row, col)
    self.input.name = "conv_layer_input_final"

    if self.modes[3] != "tanh":
      act = strtoact(self.modes[3])
      self.modes[3] = "identity"

    self.Output = self.run_cnn(
      inputs=self.input,
      filter_shape=self.filter_shape,
      filter_dilation=self.filter_dilation,
      params=self.pool_params,
      modes=self.modes,
      others=self.other_params
    ) # (batch, nb feature maps, out-row, out-col)


    # our CRNN only accept 3D tensor (time, batch, dim)
    # so, we have to convert back the output to 3D tensor
    # self.make_output(self.Output2)
    if self.attrs['batch_norm']:
      self.Output = self.batch_norm(
        h=self.Output.reshape(
          (self.Output.shape[0],
           self.Output.shape[1] * self.Output.shape[2] * self.Output.shape[3])
        ),
        dim=self.attrs['n_out'],
        force_sample=self.force_sample
      ).reshape(self.Output.shape)
      if self.modes[3] != "tanh":
        self.Output = act(self.Output)

    if self.modes[3] == 'maxout':
      self.Output = T.max(self.Output, axis=1).dimshuffle(0, 'x', 1, 2)
      self.attrs['n_out'] //= self.attrs['n_features']
      self.attrs['n_features'] = 1

    output2 = self.Output.dimshuffle(0, 2, 3, 1)  # (batch, out-row, out-col, nb feature maps)
    self.output = output2.reshape((time, batch, output2.shape[1] * output2.shape[2] * output2.shape[3]))  # (time, batch, out-dim)
Exemplo n.º 7
0
  def __init__(self, **kwargs):
    super(NewConv, self).__init__(**kwargs)

    # our CRNN input is 3D tensor that consists of (time, batch, dim)
    # however, the convolution function only accept 4D tensor which is (batch size, stack size, nb row, nb col)
    # therefore, we should convert our input into 4D tensor
    inputs = self.sources[0].output  # (time, batch, input-dim = row * col * stack_size)
    time = inputs.shape[0]
    batch = inputs.shape[1]

    if self.status[0]:  # the previous layer is convolutional layer
      self.input = T.concatenate([s.Output for s in self.sources], axis=1)  # (batch, stack size, row, col)
    else:
      # In case of spliced data, the last dim in inputs contains stacked frames (e.g. ASR).
      # Since Theano reshape will read _and_ write elements row-wise, we need to transpose the target matrix.
      # This is done by swapping target dimensions (reshape(.., input_shape[1], input_shape[0]) and subsequent
      # dimshuffle that puts row and col dim where Theano expects them.
      inputs2 = inputs.reshape((time * batch, self.input_shape[1],
                                self.input_shape[0], self.filter_shape[1]))  # (time*batch, row, col, stack)
      self.input = inputs2.dimshuffle(0, 3, 2, 1)  # (batch, stack_size, row, col)
    self.input.name = "conv_layer_input_final"

    if self.modes[3] != "tanh":
      act = strtoact(self.modes[3])
      self.modes[3] = "identity"

    self.Output = self.run_cnn(
      inputs=self.input,
      filter_shape=self.filter_shape,
      filter_dilation=self.filter_dilation,
      params=self.pool_params,
      modes=self.modes,
      others=self.other_params
    ) # (batch, nb feature maps, out-row, out-col)


    # our CRNN only accept 3D tensor (time, batch, dim)
    # so, we have to convert back the output to 3D tensor
    # self.make_output(self.Output2)
    if self.attrs['batch_norm']:
      self.Output = self.batch_norm(
        h=self.Output.reshape(
          (self.Output.shape[0],
           self.Output.shape[1] * self.Output.shape[2] * self.Output.shape[3])
        ),
        dim=self.attrs['n_out'],
        force_sample=self.force_sample
      ).reshape(self.Output.shape)
      if self.modes[3] != "tanh":
        self.Output = act(self.Output)

    if self.modes[3] == 'maxout':
      self.Output = T.max(self.Output, axis=1).dimshuffle(0, 'x', 1, 2)
      self.attrs['n_out'] //= self.attrs['n_features']
      self.attrs['n_features'] = 1

    output2 = self.Output.dimshuffle(0, 2, 3, 1)  # (batch, out-row, out-col, nb feature maps)
    self.output = output2.reshape((time, batch, output2.shape[1] * output2.shape[2] * output2.shape[3]))  # (time, batch, out-dim)
Exemplo n.º 8
0
  def __init__(self, **kwargs):
    super(NewConv, self).__init__(**kwargs)

    # our CRNN input is 3D tensor that consists of (time, batch, dim)
    # however, the convolution function only accept 4D tensor which is (batch size, stack size, nb row, nb col)
    # therefore, we should convert our input into 4D tensor
    inputs = self.sources[0].output  # (time, batch, input-dim = row * col * stack_size)
    time = inputs.shape[0]
    batch = inputs.shape[1]

    if self.status[0]:
      self.input = T.concatenate([s.Output for s in self.sources], axis=1)  # (batch, stack size, row, col)
    else:
      inputs2 = inputs.reshape((time * batch, self.input_shape[0],
                                self.input_shape[1], self.filter_shape[1]))  # (time*batch, row, col, stack)
      self.input = inputs2.dimshuffle(0, 3, 1, 2)  # (batch, stack_size, row, col)
    self.input.name = "conv_layer_input_final"

    act = strtoact(self.modes[3])
    self.modes[3] = "identity"
    self.Output = self.run_cnn(
      inputs=self.input,
      filter_shape=self.filter_shape,
      params=self.pool_params,
      modes=self.modes,
      others=self.other_params
    )

    # our CRNN only accept 3D tensor (time, batch, dim)
    # so, we have to convert back the output to 3D tensor
    # self.make_output(self.Output2)
    if self.attrs['batch_norm']:
      self.Output = act(self.batch_norm(
        self.Output.reshape(
          (self.Output.shape[0],
           self.Output.shape[1] * self.Output.shape[2] * self.Output.shape[3])
        ), self.attrs['n_out']
      ).reshape(self.Output.shape))

    if self.modes[3] == 'maxout':
      self.Output = T.max(self.Output, axis=1).dimshuffle(0, 'x', 1, 2)
      self.attrs['n_out'] /= self.attrs['n_features']
      self.attrs['n_features'] = 1

    output2 = self.Output.dimshuffle(0, 2, 3, 1)  # (time*batch, out-row, out-col, filter)
    self.output = output2.reshape((time, batch, output2.shape[1] * output2.shape[2] * output2.shape[3]))  # (time, batch, out-dim)
Exemplo n.º 9
0
  def __init__(self, n_features, filter, activation="tanh", **kwargs):
    kwargs['n_out'] = n_features
    super(ConvBaseLayer, self).__init__(**kwargs)
    assert len(self.sources) == 1
    self.source = self.sources[0]
    self.n_in = self.source.attrs['n_out']
    self.X = self.source.output
    assert self.X.ndim == 4
    self.n_features = n_features

    self.set_attr('n_features', n_features)
    self.set_attr('filter', filter)
    self.set_attr('activation', activation)
    self.set_attr('n_out', n_features)

    #TODO: maybe this ordering is not consistent with Dewis implementation
    self.filter_height = filter[0]
    self.filter_width = filter[1]
    self.activation = strtoact(activation)
    self.W = self.create_conv_weights(n_features, self.n_in, self.filter_height, self.filter_width)
    self.b = self.create_and_add_bias(n_features)