Exemplo n.º 1
0
def main(model_path):
    print 'Loading model...'
    model = serial.load(model_path)

    dataset_yaml_src = model.dataset_yaml_src
    dataset = yaml_parse.load(dataset_yaml_src)
    data_specs = (CompositeSpace([VectorSequenceSpace(dim=model.nvis),
                                 VectorSequenceSpace(dim=62)]),
                  ('features', 'phones'))
    it = dataset.iterator(mode='sequential', data_specs=data_specs,
                          num_batches=1, batch_size=1)
    original_sequence, phones = it.next()


    X = T.vector('X')
    p = T.vector('p')
    h = T.vector('h')
    out = T.vector('out')

    next_h, pred = model.fprop_step(X, p, h, out)
    fn = theano.function(inputs=[X, p, h, out], outputs=[next_h, pred],
                         on_unused_input='ignore')

    # Reconstruction
    numpy_h = numpy.zeros(model.nhid)
    numpy_out = numpy.zeros(1)
    x_t = numpy.copy(original_sequence[0])

    reconstruction_list = [original_sequence[0]]
    for p_t in phones:
        numpy_h, numpy_out = fn(x_t, p_t, numpy_h, numpy_out)
        reconstruction_list.append(numpy_out)
        x_t[:-1] = x_t[1:]
        x_t[-1] = numpy_out

    numpy_reconstruction = numpy.concatenate(reconstruction_list)
    numpy_reconstruction = numpy_reconstruction * dataset._std + dataset._mean
    numpy_reconstruction = numpy.cast['int16'](numpy_reconstruction)
    wf.write("reconstruction.wav", 16000, numpy_reconstruction)

    # One-on-one prediction
    numpy_h = numpy.zeros(model.nhid)
    numpy_out = numpy.zeros(1)

    prediction_list = [numpy.copy(original_sequence[0])]
    for x_t, p_t in zip(original_sequence, phones):
        numpy_h, numpy_out = fn(x_t, p_t, numpy_h, numpy_out)
        prediction_list.append(numpy_out)

    numpy_prediction = numpy.concatenate(prediction_list)
    numpy_prediction = numpy_prediction * dataset._std + dataset._mean
    numpy_prediction = numpy.cast['int16'](numpy_prediction)
    wf.write("prediction.wav", 16000, numpy_prediction)

    original= numpy.concatenate([original_sequence[0],
                                 original_sequence[1:, -1]])
    original= original * dataset._std + dataset._mean
    original= numpy.cast['int16'](original)
    wf.write("original.wav", 16000, original)
Exemplo n.º 2
0
def test_np_format_as_vectorsequence2vectorsequence():
    vector_sequence_space1 = VectorSequenceSpace(dim=3, dtype='float32')
    vector_sequence_space2 = VectorSequenceSpace(dim=3, dtype='float64')

    data = np.random.uniform(low=0.0, high=1.0, size=(10, 3))
    rval = vector_sequence_space1.np_format_as(data, vector_sequence_space2)

    assert np.all(rval == data)
Exemplo n.º 3
0
def test_np_format_as_vectorsequence2vectorsequence():
    vector_sequence_space1 = VectorSequenceSpace(dim=3, dtype='float32')
    vector_sequence_space2 = VectorSequenceSpace(dim=3, dtype='float64')

    data = np.random.uniform(low=0.0, high=1.0, size=(10, 3))
    rval = vector_sequence_space1.np_format_as(data, vector_sequence_space2)

    assert np.all(rval == data)
Exemplo n.º 4
0
 def _create_data_specs(self):
     ws = (self.window_size * 2 + 1)
     space = CompositeSpace(
         (IndexSequenceSpace(max_labels=self.vocab_size, dim=ws),
          IndexSequenceSpace(max_labels=self.total_feats,
                             dim=self.feat_num),
          VectorSequenceSpace(dim=self.tagger.n_classes * ws),
          VectorSequenceSpace(dim=self.n_classes)))
     source = ('words', 'features', 'tags', 'targets')
     self.data_specs = (space, source)
Exemplo n.º 5
0
    def __init__(self,
                 nvis,
                 nhid,
                 hidden_transition_model,
                 irange=0.05,
                 non_linearity='sigmoid',
                 use_ground_truth=True):
        allowed_non_linearities = {'sigmoid': T.nnet.sigmoid, 'tanh': T.tanh}
        self.nvis = nvis
        self.nhid = nhid
        self.hidden_transition_model = hidden_transition_model
        self.use_ground_truth = use_ground_truth
        self.alpha = sharedX(1)
        self.alpha_decrease_rate = 1.0  #0.99

        assert non_linearity in allowed_non_linearities
        self.non_linearity = allowed_non_linearities[non_linearity]

        # Space initialization
        self.input_space = CompositeSpace(
            [VectorSequenceSpace(dim=self.nvis),
             VectorSequenceSpace(dim=62)])
        self.hidden_space = VectorSpace(dim=self.nhid)
        self.output_space = VectorSequenceSpace(dim=1)
        self.input_source = ('features', 'phones')
        self.target_source = 'targets'

        # Features-to-hidden matrix
        W_value = numpy.random.uniform(low=-irange,
                                       high=irange,
                                       size=(self.nvis, self.nhid))
        self.W = sharedX(W_value, name='W')
        # Phones-to-hidden matrix
        V_value = numpy.random.uniform(low=-irange,
                                       high=irange,
                                       size=(62, self.nhid))
        self.V = sharedX(V_value, name='V')
        # Hidden biases
        b_value = numpy.zeros(self.nhid)
        self.b = sharedX(b_value, name='b')
        # Hidden-to-out matrix
        U_value = numpy.random.uniform(low=-irange,
                                       high=irange,
                                       size=(self.nhid, 1))
        self.U = sharedX(U_value, name='U')
        # Output bias
        c_value = numpy.zeros(1)
        self.c = sharedX(c_value, name='c')
Exemplo n.º 6
0
    def __init__(self,
                 data_generator=None,
                 n_classes=101,
                 n_examples=10,
                 n_frames=10,
                 n_features=4096):
        """
		:type data_generator: function
		:param data_generator: function used to generate data in the form of X, y tuple. X is a 3-dimensional array with dimensions (examples, frames/time, features). y is a 2-dimensional array with dimensions (examples, target values). Optional value defaults to generating random therefore 'hard' data.

		:type n_classes: int
		:param n_classes: the number of possible target values or n_classes

		:type n_examples: int
		:param n_examples: the number of examples to be generated in the dataset

		:type n_frames: int
		:param n_frames: the number of frames or time steps in each example

		:type n_features: int
		:param n_features: the number of features in each time step
		"""
        rng = np.random.RandomState(seed=42)
        self.n_features = n_features
        self.n_examples = n_examples
        if data_generator is None:
            data_generator = hard_data_generator
        self.data_generator = data_generator
        self.X, self.y = self.data_generator(n_classes, n_examples, n_frames,
                                             n_features)

        features_space = VectorSequenceSpace(dim=self.n_features)
        # features_space = SequenceDataSpace(VectorSpace(dim=self.n_features))

        targets_space = VectorSequenceSpace(dim=1)
        # targets_space = SequenceDataSpace(VectorSpace(dim=1))

        space_components = [features_space, targets_space]
        space = CompositeSpace(space_components)

        source = ('features', 'targets')

        self.data_specs = (space, source)

        self._iter_mode = resolve_iterator_class('shuffled_sequential')
        self._iter_data_specs = (CompositeSpace(
            (features_space, targets_space)), source)
Exemplo n.º 7
0
def test_np_format_as_indexsequence2vectorsequence():
    index_sequence_space = IndexSequenceSpace(max_labels=6, dim=1)
    vector_sequence_space = VectorSequenceSpace(dim=6)

    data = np.array([[0], [1], [4], [3]])
    rval = index_sequence_space.np_format_as(data, vector_sequence_space)
    true_val = np.array([[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0],
                         [0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0]])

    assert np.all(rval == true_val)
Exemplo n.º 8
0
    def __init__(self,
                 alpha_list=[1.4],
                 beta_list=[0.3],
                 init_state_list=[numpy.array([0, 0])],
                 num_samples=1000,
                 rng=None):
        # Validate parameters and set member variables
        self.alpha_list = alpha_list
        self.beta_list = beta_list

        if num_samples <= 0:
            raise ValueError("num_samples must be positive.")
        self.num_samples = num_samples
        self.num_examples = len(alpha_list)

        self.init_state_list = init_state_list

        # Initialize RNG
        if rng is None:
            self.rng = numpy.random.RandomState(self._default_seed)
        else:
            self.rng = numpy.random.RandomState(rng)

        X, y = self._generate_data()
        self.data = (X, y)

        # DataSpecs
        features_space = VectorSequenceSpace(dim=2)
        features_source = 'features'

        targets_space = VectorSequenceSpace(dim=2)
        targets_source = 'targets'

        space = CompositeSpace([features_space, targets_space])
        source = tuple([features_source, targets_source])
        self.data_specs = (space, source)

        # Defaults for iterators
        self._iter_mode = resolve_iterator_class('shuffled_sequential')
        self._iter_data_specs = (CompositeSpace(
            (features_space, targets_space)), (features_source,
                                               targets_source))
Exemplo n.º 9
0
def test_np_format_as_sequence2other():
    vector_sequence_space = VectorSequenceSpace(dim=3)
    vector_space = VectorSpace(dim=3)

    data = np.random.uniform(low=0.0, high=1.0, size=(10, 3))
    np.testing.assert_raises(ValueError, vector_sequence_space.np_format_as,
                             data, vector_space)

    index_sequence_space = IndexSequenceSpace(max_labels=6, dim=1)
    index_space = IndexSpace(max_labels=6, dim=1)

    data = np.random.randint(low=0, high=5, size=(10, 1))
    np.testing.assert_raises(ValueError, index_sequence_space.np_format_as,
                             data, index_space)
Exemplo n.º 10
0
        predictions = model.fprop(inputs)
        return T.mean(T.sqr(targets - predictions))


if __name__ == "__main__":
    model = ToyRNN(nvis=100, nhid=100)
    cost = RNNCost()

    features = T.matrix('features')
    phones = T.matrix('phones')
    targets = T.matrix('targets')

    cost_expression = cost.expr(model, ((features, phones), targets))

    fn = theano.function(inputs=[features, phones, targets],
                         outputs=cost_expression)

    valid_timit = TIMITSequences("valid", frame_length=100, audio_only=False)
    data_specs = (CompositeSpace([
        VectorSequenceSpace(dim=100),
        VectorSequenceSpace(dim=1),
        VectorSequenceSpace(dim=62)
    ]), ('features', 'targets', 'phones'))
    it = valid_timit.iterator(mode='sequential',
                              data_specs=data_specs,
                              num_batches=10,
                              batch_size=1)
    for f, t, p in it:
        print f.shape, p.shape, t.shape
        print fn(f, p, t)