def setup_slow_walk(self):

        self.walk_input = theano.shared(np.ones((2, 2), dtype='int8'))
        self.walk_time = theano.shared(np.array(0, dtype='int64'))
        self.walk_hiddens = [
            theano.shared(np.ones((2, 2), dtype=theano.config.floatX))
            for layer in self.time_model.layers if has_hidden(layer)
        ]

        # correct for dropout
        if self.dropout > 0:
            masks = [1 - self.dropout for layer in self.time_model.layers]
            masks[0] = None
        else:
            masks = []

        new_states = self.time_model.forward(self.walk_input,
                                             prev_hiddens=self.walk_hiddens,
                                             dropout=masks)

        # Now new_states is a list of matrix [layer](notes, hidden_states) for each layer
        time_final = get_last_layer(new_states)

        start_note_values = theano.tensor.alloc(np.array(0, dtype=np.int8), 2)
        note_outputs_info = ([
            initial_state_with_taps(layer) for layer in self.pitch_model.layers
        ] + [dict(initial=start_note_values, taps=[-1])])

        notes_result, updates = theano.scan(fn=self._predict_step_note,
                                            sequences=[time_final],
                                            outputs_info=note_outputs_info)

        # Now notes_result is a list of matrix [layer/output](notes, onOrArtic)
        output = get_last_layer(notes_result)

        next_input = OutputFormToInputFormOp()(output, self.walk_time +
                                               1)  # TODO: Fix time
        #next_input = T.cast(T.alloc(0, 3, 4),'int64')

        slow_walk_results = (new_states[:-1] + notes_result[:-1] +
                             [next_input, output])

        updates.update({
            self.walk_time: self.walk_time + 1,
            self.walk_input: next_input
        })

        updates.update({
            hidden: newstate
            for hidden, newstate, layer in zip(self.walk_hiddens, new_states,
                                               self.time_model.layers)
            if has_hidden(layer)
        })

        self.slow_walk_fun = theano.function(inputs=[self.conservativity],
                                             outputs=slow_walk_results,
                                             updates=updates,
                                             allow_input_downcast=True)
Exemple #2
0
        def step_time(*states):
            # States is [ *hiddens, prev_result, time]
            hiddens = list(states[:-2])
            in_data = states[-2]
            time = states[-1]

            # correct for dropout
            if self.dropout > 0:
                masks = [1 - self.dropout for layer in self.time_model.layers]
                masks[0] = None
            else:
                masks = []

            new_states = self.time_model.forward(in_data,
                                                 prev_hiddens=hiddens,
                                                 dropout=masks)

            # Now new_states is a list of matrix [layer](notes, hidden_states) for each layer
            time_final = get_last_layer(new_states)

            start_note_values = theano.tensor.alloc(np.array(0, dtype=np.int8),
                                                    2)

            # This gets a little bit complicated. In the training case, we can pass in a combination of the
            # time net's activations with the known choices. But in the prediction case, those choices don't
            # exist yet. So instead of iterating over the combination, we iterate over only the activations,
            # and then combine in the previous outputs in the step. And then since we are passing outputs to
            # previous inputs, we need an additional outputs_info for the initial "previous" output of zero.
            note_outputs_info = ([
                initial_state_with_taps(layer)
                for layer in self.pitch_model.layers
            ] + [dict(initial=start_note_values, taps=[-1])])

            notes_result, updates = theano.scan(fn=self._predict_step_note,
                                                sequences=[time_final],
                                                outputs_info=note_outputs_info)

            # Now notes_result is a list of matrix [layer/output](notes, onOrArtic)
            output = get_last_layer(notes_result)

            # TODO: input should be changed to next segment instead of previous output
            next_input = OutputFormToInputFormOp()(output,
                                                   time + 1)  # TODO: Fix time
            #next_input = T.cast(T.alloc(0, 3, 4),'int64')

            return (ensure_list(new_states) +
                    [next_input, time + 1, output]), updates